test.c 1.1 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450
  1. /* test.c
  2. *
  3. * Copyright (C) 2006-2020 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #ifndef WOLFSSL_USER_SETTINGS
  25. #include <wolfssl/options.h>
  26. #endif
  27. #include <wolfssl/wolfcrypt/settings.h>
  28. #include <wolfssl/version.h>
  29. #include <wolfssl/wolfcrypt/wc_port.h>
  30. #ifndef NO_CRYPT_TEST
  31. #if defined(HAVE_STACK_SIZE) && !defined(HAVE_WOLFCRYPT_TEST_OPTIONS)
  32. #define HAVE_WOLFCRYPT_TEST_OPTIONS
  33. #endif
  34. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  35. #include <wolfssl/ssl.h>
  36. #define err_sys err_sys_remap /* remap err_sys */
  37. #include <wolfssl/test.h>
  38. #undef err_sys
  39. #endif
  40. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  41. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  42. #include <stdint.h>
  43. #endif
  44. #if defined(HAVE_STACK_SIZE_VERBOSE)
  45. #ifdef WOLFSSL_TEST_MAX_RELATIVE_STACK_BYTES
  46. static ssize_t max_relative_stack = WOLFSSL_TEST_MAX_RELATIVE_STACK_BYTES;
  47. #else
  48. static ssize_t max_relative_stack = -1;
  49. #endif
  50. #else
  51. #define STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max, ...) (__VA_ARGS__, 0)
  52. #define STACK_SIZE_INIT()
  53. #endif
  54. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  55. #ifdef WOLFSSL_TEST_MAX_RELATIVE_HEAP_ALLOCS
  56. static ssize_t max_relative_heap_allocs = WOLFSSL_TEST_MAX_RELATIVE_HEAP_ALLOCS;
  57. #else
  58. static ssize_t max_relative_heap_allocs = -1;
  59. #endif
  60. #ifdef WOLFSSL_TEST_MAX_RELATIVE_HEAP_BYTES
  61. static ssize_t max_relative_heap_bytes = WOLFSSL_TEST_MAX_RELATIVE_HEAP_BYTES;
  62. #else
  63. static ssize_t max_relative_heap_bytes = -1;
  64. #endif
  65. #define PRINT_HEAP_CHECKPOINT() { \
  66. const ssize_t _rha = wolfCrypt_heap_peakAllocs_checkpoint() - heap_baselineAllocs; \
  67. const ssize_t _rhb = wolfCrypt_heap_peakBytes_checkpoint() - heap_baselineBytes; \
  68. printf(" relative heap peak usage: %ld alloc%s, %ld bytes\n", \
  69. _rha, \
  70. _rha == 1 ? "" : "s", \
  71. _rhb); \
  72. if ((max_relative_heap_allocs > 0) && (_rha > max_relative_heap_allocs)) \
  73. return err_sys("heap allocs exceed designated max.", -1); \
  74. if ((max_relative_heap_bytes > 0) && (_rhb > max_relative_heap_bytes)) \
  75. return err_sys("heap bytes exceed designated max.", -1); \
  76. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint(); \
  77. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint(); \
  78. }
  79. #else
  80. #define PRINT_HEAP_CHECKPOINT()
  81. #endif
  82. #ifdef __GNUC__
  83. _Pragma("GCC diagnostic ignored \"-Wunused-function\"");
  84. #endif
  85. #ifdef USE_FLAT_TEST_H
  86. #include "test.h"
  87. #else
  88. #include "wolfcrypt/test/test.h"
  89. #endif
  90. /* printf mappings */
  91. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  92. #include <mqx.h>
  93. #include <stdlib.h>
  94. /* see wc_port.h for fio.h and nio.h includes */
  95. #elif defined(FREESCALE_KSDK_BM)
  96. #include "fsl_debug_console.h"
  97. #undef printf
  98. #define printf PRINTF
  99. #elif defined(WOLFSSL_APACHE_MYNEWT)
  100. #include <assert.h>
  101. #include <string.h>
  102. #include "sysinit/sysinit.h"
  103. #include "os/os.h"
  104. #ifdef ARCH_sim
  105. #include "mcu/mcu_sim.h"
  106. #endif
  107. #include "os/os_time.h"
  108. #elif defined(WOLFSSL_ESPIDF)
  109. #include <time.h>
  110. #include <sys/time.h>
  111. #elif defined(WOLFSSL_ZEPHYR)
  112. #include <stdio.h>
  113. #define printf printk
  114. #elif defined(MICRIUM)
  115. #include <os.h>
  116. #if (OS_VERSION < 50000)
  117. #include <bsp_ser.h>
  118. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  119. #undef printf
  120. #define printf BSP_Ser_Printf
  121. #else
  122. #include <stdio.h>
  123. #endif
  124. #elif defined(WOLFSSL_PB)
  125. #include <stdarg.h>
  126. int wolfssl_pb_print(const char*, ...);
  127. #undef printf
  128. #define printf wolfssl_pb_print
  129. #elif defined(WOLFSSL_TELIT_M2MB)
  130. #include "wolfssl/wolfcrypt/wc_port.h" /* for m2mb headers */
  131. #include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
  132. /* remap printf */
  133. #undef printf
  134. #define printf M2M_LOG_INFO
  135. /* OS requires occasional sleep() */
  136. #ifndef TEST_SLEEP_MS
  137. #define TEST_SLEEP_MS 50
  138. #endif
  139. #define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
  140. /* don't use file system for these tests, since ./certs dir isn't loaded */
  141. #undef NO_FILESYSTEM
  142. #define NO_FILESYSTEM
  143. #elif defined(THREADX) && !defined(WOLFSSL_WICED) && !defined(THREADX_NO_DC_PRINTF)
  144. /* since just testing, use THREADX log printf instead */
  145. int dc_log_printf(char*, ...);
  146. #undef printf
  147. #define printf dc_log_printf
  148. #else
  149. #ifdef XMALLOC_USER
  150. #include <stdlib.h> /* we're using malloc / free direct here */
  151. #endif
  152. #if !defined(STRING_USER) && !defined(WOLFSSL_LINUXKM)
  153. #include <stdio.h>
  154. #endif
  155. #if defined(WOLFSSL_LINUXKM) && !defined(DEBUG_WOLFSSL_VERBOSE)
  156. #undef printf
  157. #define printf(...) ({})
  158. #endif
  159. /* enable way for customer to override test/bench printf */
  160. #ifdef XPRINTF
  161. #undef printf
  162. #define printf XPRINTF
  163. #endif
  164. #endif
  165. #include <wolfssl/wolfcrypt/memory.h>
  166. #include <wolfssl/wolfcrypt/wc_port.h>
  167. #include <wolfssl/wolfcrypt/logging.h>
  168. #include <wolfssl/wolfcrypt/types.h>
  169. #include <wolfssl/wolfcrypt/asn.h>
  170. #include <wolfssl/wolfcrypt/md2.h>
  171. #include <wolfssl/wolfcrypt/md5.h>
  172. #include <wolfssl/wolfcrypt/md4.h>
  173. #include <wolfssl/wolfcrypt/sha.h>
  174. #include <wolfssl/wolfcrypt/sha256.h>
  175. #include <wolfssl/wolfcrypt/sha512.h>
  176. #include <wolfssl/wolfcrypt/rc2.h>
  177. #include <wolfssl/wolfcrypt/arc4.h>
  178. #if defined(WC_NO_RNG)
  179. #include <wolfssl/wolfcrypt/integer.h>
  180. #else
  181. #include <wolfssl/wolfcrypt/random.h>
  182. #endif
  183. #include <wolfssl/wolfcrypt/coding.h>
  184. #include <wolfssl/wolfcrypt/signature.h>
  185. #include <wolfssl/wolfcrypt/rsa.h>
  186. #include <wolfssl/wolfcrypt/des3.h>
  187. #include <wolfssl/wolfcrypt/aes.h>
  188. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  189. #include <wolfssl/wolfcrypt/cmac.h>
  190. #include <wolfssl/wolfcrypt/poly1305.h>
  191. #include <wolfssl/wolfcrypt/camellia.h>
  192. #include <wolfssl/wolfcrypt/hmac.h>
  193. #include <wolfssl/wolfcrypt/dh.h>
  194. #include <wolfssl/wolfcrypt/dsa.h>
  195. #include <wolfssl/wolfcrypt/srp.h>
  196. #include <wolfssl/wolfcrypt/idea.h>
  197. #include <wolfssl/wolfcrypt/hc128.h>
  198. #include <wolfssl/wolfcrypt/rabbit.h>
  199. #include <wolfssl/wolfcrypt/chacha.h>
  200. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  201. #include <wolfssl/wolfcrypt/pwdbased.h>
  202. #include <wolfssl/wolfcrypt/ripemd.h>
  203. #include <wolfssl/wolfcrypt/error-crypt.h>
  204. #ifdef HAVE_ECC
  205. #include <wolfssl/wolfcrypt/ecc.h>
  206. #endif
  207. #ifdef HAVE_CURVE25519
  208. #include <wolfssl/wolfcrypt/curve25519.h>
  209. #endif
  210. #ifdef HAVE_ED25519
  211. #include <wolfssl/wolfcrypt/ed25519.h>
  212. #endif
  213. #ifdef HAVE_CURVE448
  214. #include <wolfssl/wolfcrypt/curve448.h>
  215. #endif
  216. #ifdef HAVE_ED448
  217. #include <wolfssl/wolfcrypt/ed448.h>
  218. #endif
  219. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  220. #include <wolfssl/wolfcrypt/blake2.h>
  221. #endif
  222. #ifdef WOLFSSL_SHA3
  223. #include <wolfssl/wolfcrypt/sha3.h>
  224. #endif
  225. #ifdef HAVE_LIBZ
  226. #include <wolfssl/wolfcrypt/compress.h>
  227. #endif
  228. #ifdef HAVE_PKCS7
  229. #include <wolfssl/wolfcrypt/pkcs7.h>
  230. #endif
  231. #ifdef HAVE_FIPS
  232. #include <wolfssl/wolfcrypt/fips_test.h>
  233. #endif
  234. #ifdef HAVE_SELFTEST
  235. #include <wolfssl/wolfcrypt/selftest.h>
  236. #endif
  237. #ifdef WOLFSSL_ASYNC_CRYPT
  238. #include <wolfssl/wolfcrypt/async.h>
  239. #endif
  240. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  241. #include <wolfssl/wolfcrypt/logging.h>
  242. #endif
  243. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  244. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  245. #endif
  246. #ifdef WOLF_CRYPTO_CB
  247. #include <wolfssl/wolfcrypt/cryptocb.h>
  248. #ifdef HAVE_INTEL_QA_SYNC
  249. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  250. #endif
  251. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  252. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  253. #endif
  254. #endif
  255. #ifdef _MSC_VER
  256. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  257. #pragma warning(disable: 4996)
  258. #endif
  259. #ifdef OPENSSL_EXTRA
  260. #ifndef WOLFCRYPT_ONLY
  261. #include <wolfssl/openssl/evp.h>
  262. #endif
  263. #include <wolfssl/openssl/rand.h>
  264. #include <wolfssl/openssl/hmac.h>
  265. #include <wolfssl/openssl/aes.h>
  266. #include <wolfssl/openssl/des.h>
  267. #endif
  268. #if defined(NO_FILESYSTEM)
  269. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  270. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  271. #define USE_CERT_BUFFERS_2048
  272. #endif
  273. #if !defined(USE_CERT_BUFFERS_256)
  274. #define USE_CERT_BUFFERS_256
  275. #endif
  276. #endif
  277. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  278. #define ENABLE_ECC384_CERT_GEN_TEST
  279. #endif
  280. #include <wolfssl/certs_test.h>
  281. #ifdef HAVE_NTRU
  282. #include "libntruencrypt/ntru_crypto.h"
  283. #endif
  284. #ifdef WOLFSSL_STATIC_MEMORY
  285. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  286. #else
  287. #define HEAP_HINT NULL
  288. #endif /* WOLFSSL_STATIC_MEMORY */
  289. /* these cases do not have intermediate hashing support */
  290. #if (defined(WOLFSSL_AFALG_XILINX_SHA3) && !defined(WOLFSSL_AFALG_HASH_KEEP)) \
  291. && !defined(WOLFSSL_XILINX_CRYPT)
  292. #define NO_INTM_HASH_TEST
  293. #endif
  294. #if defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_MULTI_ATTRIB)
  295. static void initDefaultName(void);
  296. #endif
  297. /* for async devices */
  298. static int devId = INVALID_DEVID;
  299. #ifdef HAVE_WNR
  300. const char* wnrConfigFile = "wnr-example.conf";
  301. #endif
  302. #define TEST_STRING "Everyone gets Friday off."
  303. #define TEST_STRING_SZ 25
  304. typedef struct testVector {
  305. const char* input;
  306. const char* output;
  307. size_t inLen;
  308. size_t outLen;
  309. } testVector;
  310. #ifndef WOLFSSL_TEST_SUBROUTINE
  311. #define WOLFSSL_TEST_SUBROUTINE
  312. #endif
  313. WOLFSSL_TEST_SUBROUTINE int error_test(void);
  314. WOLFSSL_TEST_SUBROUTINE int base64_test(void);
  315. WOLFSSL_TEST_SUBROUTINE int base16_test(void);
  316. WOLFSSL_TEST_SUBROUTINE int asn_test(void);
  317. WOLFSSL_TEST_SUBROUTINE int md2_test(void);
  318. WOLFSSL_TEST_SUBROUTINE int md5_test(void);
  319. WOLFSSL_TEST_SUBROUTINE int md4_test(void);
  320. WOLFSSL_TEST_SUBROUTINE int sha_test(void);
  321. WOLFSSL_TEST_SUBROUTINE int sha224_test(void);
  322. WOLFSSL_TEST_SUBROUTINE int sha256_test(void);
  323. WOLFSSL_TEST_SUBROUTINE int sha512_test(void);
  324. WOLFSSL_TEST_SUBROUTINE int sha384_test(void);
  325. WOLFSSL_TEST_SUBROUTINE int sha3_test(void);
  326. WOLFSSL_TEST_SUBROUTINE int shake256_test(void);
  327. WOLFSSL_TEST_SUBROUTINE int hash_test(void);
  328. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void);
  329. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void);
  330. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void);
  331. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void);
  332. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void);
  333. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void);
  334. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void);
  335. /* WOLFSSL_TEST_SUBROUTINE */ static int hkdf_test(void);
  336. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void);
  337. WOLFSSL_TEST_SUBROUTINE int arc4_test(void);
  338. WOLFSSL_TEST_SUBROUTINE int rc2_test(void);
  339. WOLFSSL_TEST_SUBROUTINE int hc128_test(void);
  340. WOLFSSL_TEST_SUBROUTINE int rabbit_test(void);
  341. WOLFSSL_TEST_SUBROUTINE int chacha_test(void);
  342. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void);
  343. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void);
  344. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void);
  345. WOLFSSL_TEST_SUBROUTINE int des_test(void);
  346. WOLFSSL_TEST_SUBROUTINE int des3_test(void);
  347. WOLFSSL_TEST_SUBROUTINE int aes_test(void);
  348. WOLFSSL_TEST_SUBROUTINE int aes192_test(void);
  349. WOLFSSL_TEST_SUBROUTINE int aes256_test(void);
  350. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void);
  351. WOLFSSL_TEST_SUBROUTINE int cmac_test(void);
  352. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void);
  353. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void);
  354. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void);
  355. WOLFSSL_TEST_SUBROUTINE int gmac_test(void);
  356. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void);
  357. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void);
  358. WOLFSSL_TEST_SUBROUTINE int camellia_test(void);
  359. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void);
  360. WOLFSSL_TEST_SUBROUTINE int rsa_test(void);
  361. WOLFSSL_TEST_SUBROUTINE int dh_test(void);
  362. WOLFSSL_TEST_SUBROUTINE int dsa_test(void);
  363. WOLFSSL_TEST_SUBROUTINE int srp_test(void);
  364. #ifndef WC_NO_RNG
  365. WOLFSSL_TEST_SUBROUTINE int random_test(void);
  366. #endif /* WC_NO_RNG */
  367. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void);
  368. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void);
  369. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  370. WOLFSSL_TEST_SUBROUTINE int openssl_test(void); /* test mini api */
  371. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void);
  372. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void);
  373. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void);
  374. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void);
  375. #endif
  376. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void);
  377. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void);
  378. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void);
  379. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void);
  380. #ifdef HAVE_ECC
  381. WOLFSSL_TEST_SUBROUTINE int ecc_test(void);
  382. #ifdef HAVE_ECC_ENCRYPT
  383. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void);
  384. #endif
  385. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  386. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256)
  387. /* skip for ATECC508/608A, cannot import private key buffers */
  388. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void);
  389. #endif
  390. #endif
  391. #ifdef HAVE_CURVE25519
  392. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void);
  393. #endif
  394. #ifdef HAVE_ED25519
  395. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void);
  396. #endif
  397. #ifdef HAVE_CURVE448
  398. WOLFSSL_TEST_SUBROUTINE int curve448_test(void);
  399. #endif
  400. #ifdef HAVE_ED448
  401. WOLFSSL_TEST_SUBROUTINE int ed448_test(void);
  402. #endif
  403. #ifdef HAVE_BLAKE2
  404. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void);
  405. #endif
  406. #ifdef HAVE_BLAKE2S
  407. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void);
  408. #endif
  409. #ifdef HAVE_LIBZ
  410. WOLFSSL_TEST_SUBROUTINE int compress_test(void);
  411. #endif
  412. #ifdef HAVE_PKCS7
  413. #ifndef NO_PKCS7_ENCRYPTED_DATA
  414. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void);
  415. #endif
  416. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  417. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void);
  418. #endif
  419. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void);
  420. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void);
  421. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  422. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void);
  423. #endif
  424. #ifndef NO_AES
  425. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key,
  426. word32 keySz);
  427. #endif
  428. #endif
  429. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  430. !defined(NO_FILESYSTEM)
  431. WOLFSSL_TEST_SUBROUTINE int cert_test(void);
  432. #endif
  433. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  434. !defined(NO_FILESYSTEM)
  435. WOLFSSL_TEST_SUBROUTINE int certext_test(void);
  436. #endif
  437. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  438. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  439. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void);
  440. #endif
  441. #ifdef HAVE_IDEA
  442. WOLFSSL_TEST_SUBROUTINE int idea_test(void);
  443. #endif
  444. WOLFSSL_TEST_SUBROUTINE int memory_test(void);
  445. #ifdef HAVE_VALGRIND
  446. WOLFSSL_TEST_SUBROUTINE int mp_test(void);
  447. #endif
  448. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  449. WOLFSSL_TEST_SUBROUTINE int prime_test(void);
  450. #endif
  451. #ifdef ASN_BER_TO_DER
  452. WOLFSSL_TEST_SUBROUTINE int berder_test(void);
  453. #endif
  454. WOLFSSL_TEST_SUBROUTINE int logging_test(void);
  455. WOLFSSL_TEST_SUBROUTINE int mutex_test(void);
  456. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  457. WOLFSSL_TEST_SUBROUTINE int memcb_test(void);
  458. #endif
  459. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  460. WOLFSSL_TEST_SUBROUTINE int blob_test(void);
  461. #endif
  462. #ifdef WOLF_CRYPTO_CB
  463. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void);
  464. #endif
  465. #ifdef WOLFSSL_CERT_PIV
  466. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void);
  467. #endif
  468. /* General big buffer size for many tests. */
  469. #define FOURK_BUF 4096
  470. #define ERROR_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  471. #ifdef HAVE_STACK_SIZE
  472. static THREAD_RETURN err_sys(const char* msg, int es)
  473. #else
  474. static int err_sys(const char* msg, int es)
  475. #endif
  476. {
  477. (void)msg;
  478. (void)es;
  479. #ifdef WOLFSSL_LINUXKM
  480. lkm_printf("%s error = %d\n", msg, es);
  481. EXIT_TEST(es);
  482. #else
  483. printf("%s error = %d\n", msg, es);
  484. EXIT_TEST(-1);
  485. #endif
  486. }
  487. #ifndef HAVE_WOLFCRYPT_TEST_OPTIONS
  488. /* func_args from test.h, so don't have to pull in other stuff */
  489. typedef struct func_args {
  490. int argc;
  491. char** argv;
  492. int return_code;
  493. } func_args;
  494. #endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */
  495. #ifdef HAVE_FIPS
  496. static void myFipsCb(int ok, int err, const char* hash)
  497. {
  498. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  499. printf("message = %s\n", wc_GetErrorString(err));
  500. printf("hash = %s\n", hash);
  501. if (err == IN_CORE_FIPS_E) {
  502. printf("In core integrity hash check failure, copy above hash\n");
  503. printf("into verifyCore[] in fips_test.c and rebuild\n");
  504. }
  505. }
  506. #endif /* HAVE_FIPS */
  507. #ifdef WOLFSSL_STATIC_MEMORY
  508. #ifdef BENCH_EMBEDDED
  509. static byte gTestMemory[14000];
  510. #elif defined(WOLFSSL_CERT_EXT)
  511. static byte gTestMemory[140000];
  512. #elif defined(USE_FAST_MATH) && !defined(ALT_ECC_SIZE)
  513. static byte gTestMemory[160000];
  514. #else
  515. static byte gTestMemory[80000];
  516. #endif
  517. #endif
  518. #ifdef WOLFSSL_PB
  519. static int wolfssl_pb_print(const char* msg, ...)
  520. {
  521. int ret;
  522. va_list args;
  523. char tmpBuf[80];
  524. va_start(args, msg);
  525. ret = vsprint(tmpBuf, msg, args);
  526. va_end(args);
  527. fnDumpStringToSystemLog(tmpBuf);
  528. return ret;
  529. }
  530. #endif /* WOLFSSL_PB */
  531. /* optional macro to add sleep between tests */
  532. #ifdef TEST_SLEEP
  533. #include <stdarg.h> /* for var args */
  534. static WC_INLINE void test_pass(const char* fmt, ...)
  535. {
  536. va_list args;
  537. va_start(args, fmt);
  538. STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args));
  539. va_end(args);
  540. PRINT_HEAP_CHECKPOINT();
  541. TEST_SLEEP();
  542. }
  543. #else
  544. /* redirect to printf */
  545. #define test_pass(...) { \
  546. if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK \
  547. (max_relative_stack, printf(__VA_ARGS__)) < 0) { \
  548. return err_sys("post-test check failed", -1); \
  549. } \
  550. PRINT_HEAP_CHECKPOINT(); \
  551. }
  552. /* stub the sleep macro */
  553. #define TEST_SLEEP()
  554. #endif
  555. #ifdef HAVE_STACK_SIZE
  556. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  557. #else
  558. int wolfcrypt_test(void* args)
  559. #endif
  560. {
  561. int ret;
  562. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  563. long heap_baselineAllocs, heap_baselineBytes;
  564. #endif
  565. STACK_SIZE_INIT();
  566. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  567. (void)wolfCrypt_heap_peakAllocs_checkpoint();
  568. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint();
  569. (void)wolfCrypt_heap_peakBytes_checkpoint();
  570. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint();
  571. #endif
  572. printf("------------------------------------------------------------------------------\n");
  573. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  574. printf("------------------------------------------------------------------------------\n");
  575. if (args) {
  576. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  577. int ch;
  578. #endif
  579. ((func_args*)args)->return_code = -1; /* error state */
  580. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  581. while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:m:a:h")) != -1) {
  582. switch(ch) {
  583. case 's':
  584. #ifdef HAVE_STACK_SIZE_VERBOSE
  585. max_relative_stack = (ssize_t)atoi(myoptarg);
  586. break;
  587. #else
  588. return err_sys("-s (max relative stack bytes) requires HAVE_STACK_SIZE_VERBOSE (--enable-stacksize=verbose).", -1);
  589. #endif
  590. case 'm':
  591. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  592. max_relative_heap_bytes = (ssize_t)atoi(myoptarg);
  593. break;
  594. #else
  595. return err_sys("-m (max relative heap memory bytes) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  596. #endif
  597. case 'a':
  598. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  599. max_relative_heap_allocs = (ssize_t)atoi(myoptarg);
  600. break;
  601. #else
  602. return err_sys("-a (max relative heap allocs) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  603. #endif
  604. case 'h':
  605. return err_sys("\
  606. options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
  607. [-a max_relative_heap_allocs] [-h]\n", 0);
  608. default:
  609. return err_sys("unknown test option. try -h.", -1);
  610. }
  611. }
  612. #endif
  613. }
  614. #ifdef WOLFSSL_STATIC_MEMORY
  615. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  616. WOLFMEM_GENERAL, 1) != 0) {
  617. printf("unable to load static memory.\n");
  618. return(EXIT_FAILURE);
  619. }
  620. #endif
  621. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  622. wolfSSL_Debugging_ON();
  623. #endif
  624. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  625. wc_SetLoggingHeap(HEAP_HINT);
  626. #endif
  627. #ifdef HAVE_FIPS
  628. wolfCrypt_SetCb_fips(myFipsCb);
  629. #endif
  630. #if !defined(NO_BIG_INT)
  631. if (CheckCtcSettings() != 1) {
  632. printf("Sizeof mismatch (build) %x != (run) %x\n",
  633. CTC_SETTINGS, CheckRunTimeSettings());
  634. return err_sys("Build vs runtime math mismatch\n", -1000);
  635. }
  636. #if defined(USE_FAST_MATH) && \
  637. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  638. if (CheckFastMathSettings() != 1)
  639. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  640. -1001);
  641. #endif /* USE_FAST_MATH */
  642. #endif /* !NO_BIG_INT */
  643. #if defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_MULTI_ATTRIB)
  644. initDefaultName();
  645. #endif
  646. #ifdef WOLFSSL_ASYNC_CRYPT
  647. ret = wolfAsync_DevOpen(&devId);
  648. if (ret < 0) {
  649. printf("Async device open failed\nRunning without async\n");
  650. }
  651. #else
  652. (void)devId;
  653. #endif /* WOLFSSL_ASYNC_CRYPT */
  654. #ifdef WOLF_CRYPTO_CB
  655. #ifdef HAVE_INTEL_QA_SYNC
  656. devId = wc_CryptoCb_InitIntelQa();
  657. if (INVALID_DEVID == devId) {
  658. printf("Couldn't init the Intel QA\n");
  659. }
  660. #endif
  661. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  662. devId = wc_CryptoCb_InitOcteon();
  663. if (INVALID_DEVID == devId) {
  664. printf("Couldn't init the Cavium Octeon\n");
  665. }
  666. #endif
  667. #endif
  668. #ifdef HAVE_SELFTEST
  669. if ( (ret = wolfCrypt_SelfTest()) != 0)
  670. return err_sys("CAVP selftest failed!\n", ret);
  671. else
  672. test_pass("CAVP selftest passed!\n");
  673. #endif
  674. if ( (ret = error_test()) != 0)
  675. return err_sys("error test failed!\n", ret);
  676. else
  677. test_pass("error test passed!\n");
  678. if ( (ret = memory_test()) != 0)
  679. return err_sys("MEMORY test failed!\n", ret);
  680. else
  681. test_pass("MEMORY test passed!\n");
  682. #ifndef NO_CODING
  683. if ( (ret = base64_test()) != 0)
  684. return err_sys("base64 test failed!\n", ret);
  685. else
  686. test_pass("base64 test passed!\n");
  687. #ifdef WOLFSSL_BASE16
  688. if ( (ret = base16_test()) != 0)
  689. return err_sys("base16 test failed!\n", ret);
  690. else
  691. test_pass("base16 test passed!\n");
  692. #endif
  693. #endif /* !NO_CODING */
  694. #ifndef NO_ASN
  695. if ( (ret = asn_test()) != 0)
  696. return err_sys("asn test failed!\n", ret);
  697. else
  698. test_pass("asn test passed!\n");
  699. #endif
  700. #ifndef WC_NO_RNG
  701. if ( (ret = random_test()) != 0)
  702. return err_sys("RANDOM test failed!\n", ret);
  703. else
  704. test_pass("RANDOM test passed!\n");
  705. #endif /* WC_NO_RNG */
  706. #ifndef NO_MD5
  707. if ( (ret = md5_test()) != 0)
  708. return err_sys("MD5 test failed!\n", ret);
  709. else
  710. test_pass("MD5 test passed!\n");
  711. #endif
  712. #ifdef WOLFSSL_MD2
  713. if ( (ret = md2_test()) != 0)
  714. return err_sys("MD2 test failed!\n", ret);
  715. else
  716. test_pass("MD2 test passed!\n");
  717. #endif
  718. #ifndef NO_MD4
  719. if ( (ret = md4_test()) != 0)
  720. return err_sys("MD4 test failed!\n", ret);
  721. else
  722. test_pass("MD4 test passed!\n");
  723. #endif
  724. #ifndef NO_SHA
  725. if ( (ret = sha_test()) != 0)
  726. return err_sys("SHA test failed!\n", ret);
  727. else
  728. test_pass("SHA test passed!\n");
  729. #endif
  730. #ifdef WOLFSSL_SHA224
  731. if ( (ret = sha224_test()) != 0)
  732. return err_sys("SHA-224 test failed!\n", ret);
  733. else
  734. test_pass("SHA-224 test passed!\n");
  735. #endif
  736. #ifndef NO_SHA256
  737. if ( (ret = sha256_test()) != 0)
  738. return err_sys("SHA-256 test failed!\n", ret);
  739. else
  740. test_pass("SHA-256 test passed!\n");
  741. #endif
  742. #ifdef WOLFSSL_SHA384
  743. if ( (ret = sha384_test()) != 0)
  744. return err_sys("SHA-384 test failed!\n", ret);
  745. else
  746. test_pass("SHA-384 test passed!\n");
  747. #endif
  748. #ifdef WOLFSSL_SHA512
  749. if ( (ret = sha512_test()) != 0)
  750. return err_sys("SHA-512 test failed!\n", ret);
  751. else
  752. test_pass("SHA-512 test passed!\n");
  753. #endif
  754. #ifdef WOLFSSL_SHA3
  755. if ( (ret = sha3_test()) != 0)
  756. return err_sys("SHA-3 test failed!\n", ret);
  757. else
  758. test_pass("SHA-3 test passed!\n");
  759. #endif
  760. #ifdef WOLFSSL_SHAKE256
  761. if ( (ret = shake256_test()) != 0)
  762. return err_sys("SHAKE256 test failed!\n", ret);
  763. else
  764. test_pass("SHAKE256 test passed!\n");
  765. #endif
  766. if ( (ret = hash_test()) != 0)
  767. return err_sys("Hash test failed!\n", ret);
  768. else
  769. test_pass("Hash test passed!\n");
  770. #ifdef WOLFSSL_RIPEMD
  771. if ( (ret = ripemd_test()) != 0)
  772. return err_sys("RIPEMD test failed!\n", ret);
  773. else
  774. test_pass("RIPEMD test passed!\n");
  775. #endif
  776. #ifdef HAVE_BLAKE2
  777. if ( (ret = blake2b_test()) != 0)
  778. return err_sys("BLAKE2b test failed!\n", ret);
  779. else
  780. test_pass("BLAKE2b test passed!\n");
  781. #endif
  782. #ifdef HAVE_BLAKE2S
  783. if ( (ret = blake2s_test()) != 0)
  784. return err_sys("BLAKE2s test failed!\n", ret);
  785. else
  786. test_pass("BLAKE2s test passed!\n");
  787. #endif
  788. #ifndef NO_HMAC
  789. #ifndef NO_MD5
  790. if ( (ret = hmac_md5_test()) != 0)
  791. return err_sys("HMAC-MD5 test failed!\n", ret);
  792. else
  793. test_pass("HMAC-MD5 test passed!\n");
  794. #endif
  795. #ifndef NO_SHA
  796. if ( (ret = hmac_sha_test()) != 0)
  797. return err_sys("HMAC-SHA test failed!\n", ret);
  798. else
  799. test_pass("HMAC-SHA test passed!\n");
  800. #endif
  801. #ifdef WOLFSSL_SHA224
  802. if ( (ret = hmac_sha224_test()) != 0)
  803. return err_sys("HMAC-SHA224 test failed!\n", ret);
  804. else
  805. test_pass("HMAC-SHA224 test passed!\n");
  806. #endif
  807. #ifndef NO_SHA256
  808. if ( (ret = hmac_sha256_test()) != 0)
  809. return err_sys("HMAC-SHA256 test failed!\n", ret);
  810. else
  811. test_pass("HMAC-SHA256 test passed!\n");
  812. #endif
  813. #ifdef WOLFSSL_SHA384
  814. if ( (ret = hmac_sha384_test()) != 0)
  815. return err_sys("HMAC-SHA384 test failed!\n", ret);
  816. else
  817. test_pass("HMAC-SHA384 test passed!\n");
  818. #endif
  819. #ifdef WOLFSSL_SHA512
  820. if ( (ret = hmac_sha512_test()) != 0)
  821. return err_sys("HMAC-SHA512 test failed!\n", ret);
  822. else
  823. test_pass("HMAC-SHA512 test passed!\n");
  824. #endif
  825. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  826. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  827. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  828. if ( (ret = hmac_sha3_test()) != 0)
  829. return err_sys("HMAC-SHA3 test failed!\n", ret);
  830. else
  831. test_pass("HMAC-SHA3 test passed!\n");
  832. #endif
  833. #ifdef HAVE_HKDF
  834. if ( (ret = hkdf_test()) != 0)
  835. return err_sys("HMAC-KDF test failed!\n", ret);
  836. else
  837. test_pass("HMAC-KDF test passed!\n");
  838. #endif
  839. #endif /* !NO_HMAC */
  840. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  841. if ( (ret = x963kdf_test()) != 0)
  842. return err_sys("X963-KDF test failed!\n", ret);
  843. else
  844. test_pass("X963-KDF test passed!\n");
  845. #endif
  846. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  847. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  848. if ( (ret = gmac_test()) != 0)
  849. return err_sys("GMAC test failed!\n", ret);
  850. else
  851. test_pass("GMAC test passed!\n");
  852. #endif
  853. #ifdef WC_RC2
  854. if ( (ret = rc2_test()) != 0)
  855. return err_sys("RC2 test failed!\n", ret);
  856. else
  857. test_pass("RC2 test passed!\n");
  858. #endif
  859. #ifndef NO_RC4
  860. if ( (ret = arc4_test()) != 0)
  861. return err_sys("ARC4 test failed!\n", ret);
  862. else
  863. test_pass("ARC4 test passed!\n");
  864. #endif
  865. #ifndef NO_HC128
  866. if ( (ret = hc128_test()) != 0)
  867. return err_sys("HC-128 test failed!\n", ret);
  868. else
  869. test_pass("HC-128 test passed!\n");
  870. #endif
  871. #ifndef NO_RABBIT
  872. if ( (ret = rabbit_test()) != 0)
  873. return err_sys("Rabbit test failed!\n", ret);
  874. else
  875. test_pass("Rabbit test passed!\n");
  876. #endif
  877. #ifdef HAVE_CHACHA
  878. if ( (ret = chacha_test()) != 0)
  879. return err_sys("Chacha test failed!\n", ret);
  880. else
  881. test_pass("Chacha test passed!\n");
  882. #endif
  883. #ifdef HAVE_XCHACHA
  884. if ( (ret = XChaCha_test()) != 0)
  885. return err_sys("XChacha test failed!\n", ret);
  886. else
  887. test_pass("XChacha test passed!\n");
  888. #endif
  889. #ifdef HAVE_POLY1305
  890. if ( (ret = poly1305_test()) != 0)
  891. return err_sys("POLY1305 test failed!\n", ret);
  892. else
  893. test_pass("POLY1305 test passed!\n");
  894. #endif
  895. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  896. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  897. return err_sys("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  898. else
  899. test_pass("ChaCha20-Poly1305 AEAD test passed!\n");
  900. #endif
  901. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  902. if ( (ret = XChaCha20Poly1305_test()) != 0)
  903. return err_sys("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  904. else
  905. test_pass("XChaCha20-Poly1305 AEAD test passed!\n");
  906. #endif
  907. #ifndef NO_DES3
  908. if ( (ret = des_test()) != 0)
  909. return err_sys("DES test failed!\n", ret);
  910. else
  911. test_pass("DES test passed!\n");
  912. #endif
  913. #ifndef NO_DES3
  914. if ( (ret = des3_test()) != 0)
  915. return err_sys("DES3 test failed!\n", ret);
  916. else
  917. test_pass("DES3 test passed!\n");
  918. #endif
  919. #ifndef NO_AES
  920. if ( (ret = aes_test()) != 0)
  921. return err_sys("AES test failed!\n", ret);
  922. else
  923. test_pass("AES test passed!\n");
  924. #ifdef WOLFSSL_AES_192
  925. if ( (ret = aes192_test()) != 0)
  926. return err_sys("AES192 test failed!\n", ret);
  927. else
  928. test_pass("AES192 test passed!\n");
  929. #endif
  930. #ifdef WOLFSSL_AES_256
  931. if ( (ret = aes256_test()) != 0)
  932. return err_sys("AES256 test failed!\n", ret);
  933. else
  934. test_pass("AES256 test passed!\n");
  935. #endif
  936. #ifdef WOLFSSL_AES_OFB
  937. if ( (ret = aesofb_test()) != 0)
  938. return err_sys("AES-OFB test failed!\n", ret);
  939. else
  940. test_pass("AESOFB test passed!\n");
  941. #endif
  942. #ifdef HAVE_AESGCM
  943. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  944. if ( (ret = aesgcm_test()) != 0)
  945. return err_sys("AES-GCM test failed!\n", ret);
  946. #endif
  947. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  948. !(defined(WOLF_CRYPTO_CB) && \
  949. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  950. if ((ret = aesgcm_default_test()) != 0) {
  951. return err_sys("AES-GCM test failed!\n", ret);
  952. }
  953. #endif
  954. test_pass("AES-GCM test passed!\n");
  955. #endif
  956. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  957. if ( (ret = aesccm_test()) != 0)
  958. return err_sys("AES-CCM test failed!\n", ret);
  959. else
  960. test_pass("AES-CCM test passed!\n");
  961. #endif
  962. #ifdef HAVE_AES_KEYWRAP
  963. if ( (ret = aeskeywrap_test()) != 0)
  964. return err_sys("AES Key Wrap test failed!\n", ret);
  965. else
  966. test_pass("AES Key Wrap test passed!\n");
  967. #endif
  968. #endif
  969. #ifdef HAVE_CAMELLIA
  970. if ( (ret = camellia_test()) != 0)
  971. return err_sys("CAMELLIA test failed!\n", ret);
  972. else
  973. test_pass("CAMELLIA test passed!\n");
  974. #endif
  975. #ifdef HAVE_IDEA
  976. if ( (ret = idea_test()) != 0)
  977. return err_sys("IDEA test failed!\n", ret);
  978. else
  979. test_pass("IDEA test passed!\n");
  980. #endif
  981. #ifndef NO_RSA
  982. #ifdef WC_RSA_NO_PADDING
  983. if ( (ret = rsa_no_pad_test()) != 0)
  984. return err_sys("RSA NOPAD test failed!\n", ret);
  985. else
  986. test_pass("RSA NOPAD test passed!\n");
  987. #endif
  988. if ( (ret = rsa_test()) != 0)
  989. return err_sys("RSA test failed!\n", ret);
  990. else
  991. test_pass("RSA test passed!\n");
  992. #endif
  993. #ifndef NO_DH
  994. if ( (ret = dh_test()) != 0)
  995. return err_sys("DH test failed!\n", ret);
  996. else
  997. test_pass("DH test passed!\n");
  998. #endif
  999. #ifndef NO_DSA
  1000. if ( (ret = dsa_test()) != 0)
  1001. return err_sys("DSA test failed!\n", ret);
  1002. else
  1003. test_pass("DSA test passed!\n");
  1004. #endif
  1005. #ifdef WOLFCRYPT_HAVE_SRP
  1006. if ( (ret = srp_test()) != 0)
  1007. return err_sys("SRP test failed!\n", ret);
  1008. else
  1009. test_pass("SRP test passed!\n");
  1010. #endif
  1011. #ifndef NO_PWDBASED
  1012. if ( (ret = pwdbased_test()) != 0)
  1013. return err_sys("PWDBASED test failed!\n", ret);
  1014. else
  1015. test_pass("PWDBASED test passed!\n");
  1016. #endif
  1017. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1018. if ( (ret = openssl_test()) != 0)
  1019. return err_sys("OPENSSL test failed!\n", ret);
  1020. else
  1021. test_pass("OPENSSL test passed!\n");
  1022. if ( (ret = openSSL_evpMD_test()) != 0)
  1023. return err_sys("OPENSSL (EVP MD) test failed!\n", ret);
  1024. else
  1025. test_pass("OPENSSL (EVP MD) passed!\n");
  1026. if ( (ret = openssl_pkey0_test()) != 0)
  1027. return err_sys("OPENSSL (PKEY0) test failed!\n", ret);
  1028. else
  1029. test_pass("OPENSSL (PKEY0) passed!\n");
  1030. if ( (ret = openssl_pkey1_test()) != 0)
  1031. return err_sys("OPENSSL (PKEY1) test failed!\n", ret);
  1032. else
  1033. test_pass("OPENSSL (PKEY1) passed!\n");
  1034. if ( (ret = openssl_evpSig_test()) != 0)
  1035. return err_sys("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1036. else
  1037. test_pass("OPENSSL (EVP Sign/Verify) passed!\n");
  1038. #endif
  1039. #ifdef HAVE_ECC
  1040. if ( (ret = ecc_test()) != 0)
  1041. return err_sys("ECC test failed!\n", ret);
  1042. else
  1043. test_pass("ECC test passed!\n");
  1044. #if defined(HAVE_ECC_ENCRYPT) && defined(WOLFSSL_AES_128)
  1045. if ( (ret = ecc_encrypt_test()) != 0)
  1046. return err_sys("ECC Enc test failed!\n", ret);
  1047. else
  1048. test_pass("ECC Enc test passed!\n");
  1049. #endif
  1050. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1051. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256)
  1052. /* skip for ATECC508/608A, cannot import private key buffers */
  1053. if ( (ret = ecc_test_buffers()) != 0)
  1054. return err_sys("ECC buffer test failed!\n", ret);
  1055. else
  1056. test_pass("ECC buffer test passed!\n");
  1057. #endif
  1058. #endif
  1059. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1060. !defined(NO_FILESYSTEM)
  1061. if ( (ret = cert_test()) != 0)
  1062. return err_sys("CERT test failed!\n", ret);
  1063. else
  1064. test_pass("CERT test passed!\n");
  1065. #endif
  1066. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1067. !defined(NO_FILESYSTEM)
  1068. if ( (ret = certext_test()) != 0)
  1069. return err_sys("CERT EXT test failed!\n", ret);
  1070. else
  1071. test_pass("CERT EXT test passed!\n");
  1072. #endif
  1073. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1074. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1075. if ( (ret = decodedCertCache_test()) != 0)
  1076. return err_sys("DECODED CERT CACHE test failed!\n", ret);
  1077. else
  1078. test_pass("DECODED CERT CACHE test passed!\n");
  1079. #endif
  1080. #ifdef HAVE_CURVE25519
  1081. if ( (ret = curve25519_test()) != 0)
  1082. return err_sys("CURVE25519 test failed!\n", ret);
  1083. else
  1084. test_pass("CURVE25519 test passed!\n");
  1085. #endif
  1086. #ifdef HAVE_ED25519
  1087. if ( (ret = ed25519_test()) != 0)
  1088. return err_sys("ED25519 test failed!\n", ret);
  1089. else
  1090. test_pass("ED25519 test passed!\n");
  1091. #endif
  1092. #ifdef HAVE_CURVE448
  1093. if ( (ret = curve448_test()) != 0)
  1094. return err_sys("CURVE448 test failed!\n", ret);
  1095. else
  1096. test_pass("CURVE448 test passed!\n");
  1097. #endif
  1098. #ifdef HAVE_ED448
  1099. if ( (ret = ed448_test()) != 0)
  1100. return err_sys("ED448 test failed!\n", ret);
  1101. else
  1102. test_pass("ED448 test passed!\n");
  1103. #endif
  1104. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  1105. if ( (ret = cmac_test()) != 0)
  1106. return err_sys("CMAC test failed!\n", ret);
  1107. else
  1108. test_pass("CMAC test passed!\n");
  1109. #endif
  1110. #ifdef HAVE_LIBZ
  1111. if ( (ret = compress_test()) != 0)
  1112. return err_sys("COMPRESS test failed!\n", ret);
  1113. else
  1114. test_pass("COMPRESS test passed!\n");
  1115. #endif
  1116. #ifdef HAVE_PKCS7
  1117. #ifndef NO_PKCS7_ENCRYPTED_DATA
  1118. if ( (ret = pkcs7encrypted_test()) != 0)
  1119. return err_sys("PKCS7encrypted test failed!\n", ret);
  1120. else
  1121. test_pass("PKCS7encrypted test passed!\n");
  1122. #endif
  1123. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  1124. if ( (ret = pkcs7compressed_test()) != 0)
  1125. return err_sys("PKCS7compressed test failed!\n", ret);
  1126. else
  1127. test_pass("PKCS7compressed test passed!\n");
  1128. #endif
  1129. if ( (ret = pkcs7signed_test()) != 0)
  1130. return err_sys("PKCS7signed test failed!\n", ret);
  1131. else
  1132. test_pass("PKCS7signed test passed!\n");
  1133. if ( (ret = pkcs7enveloped_test()) != 0)
  1134. return err_sys("PKCS7enveloped test failed!\n", ret);
  1135. else
  1136. test_pass("PKCS7enveloped test passed!\n");
  1137. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1138. if ( (ret = pkcs7authenveloped_test()) != 0)
  1139. return err_sys("PKCS7authenveloped test failed!\n", ret);
  1140. else
  1141. test_pass("PKCS7authenveloped test passed!\n");
  1142. #endif
  1143. #endif
  1144. #ifdef HAVE_VALGRIND
  1145. if ( (ret = mp_test()) != 0)
  1146. return err_sys("mp test failed!\n", ret);
  1147. else
  1148. test_pass("mp test passed!\n");
  1149. #endif
  1150. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  1151. if ( (ret = prime_test()) != 0)
  1152. return err_sys("prime test failed!\n", ret);
  1153. else
  1154. test_pass("prime test passed!\n");
  1155. #endif
  1156. #if defined(ASN_BER_TO_DER) && \
  1157. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  1158. defined(OPENSSL_EXTRA_X509_SMALL))
  1159. if ( (ret = berder_test()) != 0)
  1160. return err_sys("ber-der test failed!\n", ret);
  1161. else
  1162. test_pass("ber-der test passed!\n");
  1163. #endif
  1164. if ( (ret = logging_test()) != 0)
  1165. return err_sys("logging test failed!\n", ret);
  1166. else
  1167. test_pass("logging test passed!\n");
  1168. if ( (ret = mutex_test()) != 0)
  1169. return err_sys("mutex test failed!\n", ret);
  1170. else
  1171. test_pass("mutex test passed!\n");
  1172. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  1173. if ( (ret = memcb_test()) != 0)
  1174. return err_sys("memcb test failed!\n", ret);
  1175. else
  1176. test_pass("memcb test passed!\n");
  1177. #endif
  1178. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  1179. if ( (ret = blob_test()) != 0)
  1180. return err_sys("blob test failed!\n", ret);
  1181. else
  1182. test_pass("blob test passed!\n");
  1183. #endif
  1184. #if defined(WOLF_CRYPTO_CB) && \
  1185. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  1186. if ( (ret = cryptocb_test()) != 0)
  1187. return err_sys("crypto callback test failed!\n", ret);
  1188. else
  1189. test_pass("crypto callback test passed!\n");
  1190. #endif
  1191. #ifdef WOLFSSL_CERT_PIV
  1192. if ( (ret = certpiv_test()) != 0)
  1193. return err_sys("cert piv test failed!\n", ret);
  1194. else
  1195. test_pass("cert piv test passed!\n");
  1196. #endif
  1197. #ifdef WOLF_CRYPTO_CB
  1198. #ifdef HAVE_INTEL_QA_SYNC
  1199. wc_CryptoCb_CleanupIntelQa(&devId);
  1200. #endif
  1201. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1202. wc_CryptoCb_CleanupOcteon(&devId);
  1203. #endif
  1204. #endif
  1205. #ifdef WOLFSSL_ASYNC_CRYPT
  1206. wolfAsync_DevClose(&devId);
  1207. #endif
  1208. /* cleanup the thread if fixed point cache is enabled and have thread local */
  1209. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  1210. wc_ecc_fp_free();
  1211. #endif
  1212. if (args)
  1213. ((func_args*)args)->return_code = ret;
  1214. test_pass("Test complete\n");
  1215. EXIT_TEST(ret);
  1216. }
  1217. #ifndef NO_MAIN_DRIVER
  1218. /* so overall tests can pull in test function */
  1219. #ifdef WOLFSSL_ESPIDF
  1220. void app_main( )
  1221. #else
  1222. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1223. int myoptind = 0;
  1224. char* myoptarg = NULL;
  1225. #endif
  1226. int main(int argc, char** argv)
  1227. #endif
  1228. {
  1229. int ret;
  1230. func_args args;
  1231. #ifdef WOLFSSL_ESPIDF
  1232. /* set dummy wallclock time. */
  1233. struct timeval utctime;
  1234. struct timezone tz;
  1235. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1236. utctime.tv_usec = 0;
  1237. tz.tz_minuteswest = 0;
  1238. tz.tz_dsttime = 0;
  1239. settimeofday(&utctime, &tz);
  1240. #endif
  1241. #ifdef WOLFSSL_APACHE_MYNEWT
  1242. #ifdef ARCH_sim
  1243. mcu_sim_parse_args(argc, argv);
  1244. #endif
  1245. sysinit();
  1246. /* set dummy wallclock time. */
  1247. struct os_timeval utctime;
  1248. struct os_timezone tz;
  1249. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1250. utctime.tv_usec = 0;
  1251. tz.tz_minuteswest = 0;
  1252. tz.tz_dsttime = 0;
  1253. os_settimeofday(&utctime, &tz);
  1254. #endif
  1255. #ifdef HAVE_WNR
  1256. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0) {
  1257. err_sys("Whitewood netRandom global config failed", -1001);
  1258. return -1002;
  1259. }
  1260. #endif
  1261. #ifndef WOLFSSL_ESPIDF
  1262. args.argc = argc;
  1263. args.argv = argv;
  1264. #endif
  1265. if ((ret = wolfCrypt_Init()) != 0) {
  1266. printf("wolfCrypt_Init failed %d\n", ret);
  1267. err_sys("Error with wolfCrypt_Init!\n", -1003);
  1268. }
  1269. #ifdef HAVE_STACK_SIZE
  1270. StackSizeCheck(&args, wolfcrypt_test);
  1271. #else
  1272. wolfcrypt_test(&args);
  1273. #endif
  1274. if ((ret = wolfCrypt_Cleanup()) != 0) {
  1275. printf("wolfCrypt_Cleanup failed %d\n", ret);
  1276. err_sys("Error with wolfCrypt_Cleanup!\n", -1004);
  1277. }
  1278. #ifdef HAVE_WNR
  1279. if (wc_FreeNetRandom() < 0)
  1280. err_sys("Failed to free netRandom context", -1005);
  1281. #endif /* HAVE_WNR */
  1282. #ifndef WOLFSSL_ESPIDF
  1283. return args.return_code;
  1284. #endif
  1285. }
  1286. #endif /* NO_MAIN_DRIVER */
  1287. /* helper to save DER, convert to PEM and save PEM */
  1288. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  1289. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN))))
  1290. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1291. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, fD, fP, pT, eB)
  1292. #else
  1293. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, NULL, NULL, pT, eB)
  1294. #endif
  1295. static int _SaveDerAndPem(const byte* der, int derSz,
  1296. const char* fileDer, const char* filePem, int pemType, int errBase)
  1297. {
  1298. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1299. int ret;
  1300. XFILE derFile;
  1301. derFile = XFOPEN(fileDer, "wb");
  1302. if (!derFile) {
  1303. return errBase + 0;
  1304. }
  1305. ret = (int)XFWRITE(der, 1, derSz, derFile);
  1306. XFCLOSE(derFile);
  1307. if (ret != derSz) {
  1308. return errBase + 1;
  1309. }
  1310. #endif
  1311. #ifdef WOLFSSL_DER_TO_PEM
  1312. if (filePem) {
  1313. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1314. XFILE pemFile;
  1315. #endif
  1316. byte* pem;
  1317. int pemSz;
  1318. /* calculate PEM size */
  1319. pemSz = wc_DerToPem(der, derSz, NULL, 0, pemType);
  1320. if (pemSz < 0) {
  1321. return pemSz;
  1322. }
  1323. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1324. if (pem == NULL) {
  1325. return MEMORY_E;
  1326. }
  1327. /* Convert to PEM */
  1328. pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
  1329. if (pemSz < 0) {
  1330. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1331. return errBase + 2;
  1332. }
  1333. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1334. pemFile = XFOPEN(filePem, "wb");
  1335. if (!pemFile) {
  1336. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1337. return errBase + 3;
  1338. }
  1339. ret = (int)XFWRITE(pem, 1, pemSz, pemFile);
  1340. XFCLOSE(pemFile);
  1341. if (ret != pemSz) {
  1342. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1343. return errBase + 4;
  1344. }
  1345. #endif
  1346. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1347. }
  1348. #endif /* WOLFSSL_DER_TO_PEM */
  1349. /* suppress unused variable warnings */
  1350. (void)der;
  1351. (void)derSz;
  1352. (void)filePem;
  1353. (void)fileDer;
  1354. (void)pemType;
  1355. (void)errBase;
  1356. return 0;
  1357. }
  1358. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  1359. WOLFSSL_TEST_SUBROUTINE int error_test(void)
  1360. {
  1361. const char* errStr;
  1362. char out[WOLFSSL_MAX_ERROR_SZ];
  1363. const char* unknownStr = wc_GetErrorString(0);
  1364. #ifdef NO_ERROR_STRINGS
  1365. /* Ensure a valid error code's string matches an invalid code's.
  1366. * The string is that error strings are not available.
  1367. */
  1368. errStr = wc_GetErrorString(OPEN_RAN_E);
  1369. wc_ErrorString(OPEN_RAN_E, out);
  1370. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  1371. return -1100;
  1372. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  1373. return -1101;
  1374. #else
  1375. int i;
  1376. int j = 0;
  1377. /* Values that are not or no longer error codes. */
  1378. int missing[] = { -122, -123, -124, -127, -128, -129,
  1379. -163, -164, -165, -166, -167, -168, -169,
  1380. -179, -233,
  1381. 0 };
  1382. /* Check that all errors have a string and it's the same through the two
  1383. * APIs. Check that the values that are not errors map to the unknown
  1384. * string.
  1385. */
  1386. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  1387. errStr = wc_GetErrorString(i);
  1388. wc_ErrorString(i, out);
  1389. if (i != missing[j]) {
  1390. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) == 0)
  1391. return -1102;
  1392. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) == 0)
  1393. return -1103;
  1394. if (XSTRNCMP(errStr, out, XSTRLEN(errStr)) != 0)
  1395. return -1104;
  1396. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ)
  1397. return -1105;
  1398. }
  1399. else {
  1400. j++;
  1401. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  1402. return -1106;
  1403. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  1404. return -1107;
  1405. }
  1406. }
  1407. /* Check if the next possible value has been given a string. */
  1408. errStr = wc_GetErrorString(i);
  1409. wc_ErrorString(i, out);
  1410. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  1411. return -1108;
  1412. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  1413. return -1109;
  1414. #endif
  1415. return 0;
  1416. }
  1417. #ifndef NO_CODING
  1418. WOLFSSL_TEST_SUBROUTINE int base64_test(void)
  1419. {
  1420. int ret;
  1421. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  1422. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  1423. byte out[128];
  1424. word32 outLen;
  1425. #ifdef WOLFSSL_BASE64_ENCODE
  1426. byte data[3];
  1427. word32 dataLen;
  1428. byte longData[79] = { 0 };
  1429. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  1430. #endif
  1431. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA Gdj=";
  1432. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  1433. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd AA";
  1434. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  1435. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  1436. byte goodChar[] =
  1437. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1438. "abcdefghijklmnopqrstuvwxyz"
  1439. "0123456789+/;";
  1440. byte charTest[] = "A+Gd\0\0\0";
  1441. int i;
  1442. /* Good Base64 encodings. */
  1443. outLen = sizeof(out);
  1444. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1445. if (ret != 0)
  1446. return -1200;
  1447. outLen = sizeof(out);
  1448. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  1449. if (ret != 0)
  1450. return -1201;
  1451. outLen = sizeof(goodChar);
  1452. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  1453. if (ret != 0)
  1454. return -1235;
  1455. if (outLen != 64 / 4 * 3)
  1456. return -1236;
  1457. /* Bad parameters. */
  1458. outLen = 1;
  1459. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1460. if (ret != BAD_FUNC_ARG)
  1461. return -1202;
  1462. outLen = sizeof(out);
  1463. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  1464. if (ret != ASN_INPUT_E)
  1465. return -1203;
  1466. outLen = sizeof(out);
  1467. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  1468. if (ret != ASN_INPUT_E)
  1469. return -1203;
  1470. /* Bad character at each offset 0-3. */
  1471. for (i = 0; i < 4; i++) {
  1472. outLen = sizeof(out);
  1473. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  1474. if (ret != ASN_INPUT_E)
  1475. return -1204 - i;
  1476. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  1477. if (ret != ASN_INPUT_E)
  1478. return -1214 - i;
  1479. }
  1480. /* Invalid character less than 0x2b */
  1481. for (i = 1; i < 0x2b; i++) {
  1482. outLen = sizeof(out);
  1483. charTest[0] = i;
  1484. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1485. if (ret != ASN_INPUT_E)
  1486. return -1240 - i;
  1487. }
  1488. /* Bad characters in range 0x2b - 0x7a. */
  1489. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  1490. outLen = sizeof(out);
  1491. charTest[0] = badChar[i];
  1492. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1493. if (ret != ASN_INPUT_E)
  1494. return -1270 - i;
  1495. }
  1496. /* Invalid character greater than 0x7a */
  1497. for (i = 0x7b; i < 0x100; i++) {
  1498. outLen = sizeof(out);
  1499. charTest[0] = i;
  1500. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1501. if (ret != ASN_INPUT_E)
  1502. return -1290 - i;
  1503. }
  1504. #ifdef WOLFSSL_BASE64_ENCODE
  1505. /* Decode and encode all symbols - non-alphanumeric. */
  1506. dataLen = sizeof(data);
  1507. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  1508. if (ret != 0)
  1509. return -1224;
  1510. outLen = sizeof(out);
  1511. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  1512. if (ret != LENGTH_ONLY_E)
  1513. return -1225;
  1514. outLen = sizeof(out);
  1515. ret = Base64_Encode(data, dataLen, out, &outLen);
  1516. if (ret != 0)
  1517. return -1226;
  1518. outLen = 7;
  1519. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1520. if (ret != BUFFER_E)
  1521. return -1227;
  1522. outLen = sizeof(out);
  1523. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  1524. if (ret != LENGTH_ONLY_E)
  1525. return -1228;
  1526. outLen = sizeof(out);
  1527. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1528. if (ret != 0)
  1529. return -1229;
  1530. outLen = sizeof(out);
  1531. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  1532. if (ret != 0)
  1533. return -1230;
  1534. /* Data that results in an encoding longer than one line. */
  1535. outLen = sizeof(out);
  1536. dataLen = sizeof(longData);
  1537. ret = Base64_Encode(longData, dataLen, out, &outLen);
  1538. if (ret != 0)
  1539. return -1231;
  1540. outLen = sizeof(out);
  1541. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  1542. if (ret != 0)
  1543. return -1232;
  1544. outLen = sizeof(out);
  1545. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  1546. if (ret != 0)
  1547. return -1233;
  1548. #endif
  1549. return 0;
  1550. }
  1551. #ifdef WOLFSSL_BASE16
  1552. WOLFSSL_TEST_SUBROUTINE int base16_test(void)
  1553. {
  1554. int ret;
  1555. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  1556. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  1557. byte encoded[40];
  1558. word32 encodedLen;
  1559. byte plain[40];
  1560. word32 len;
  1561. /* length returned includes null termination */
  1562. encodedLen = sizeof(encoded);
  1563. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  1564. if (ret != 0)
  1565. return -1300;
  1566. len = (word32)XSTRLEN((char*)encoded);
  1567. if (len != encodedLen - 1)
  1568. return -1301;
  1569. len = sizeof(plain);
  1570. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  1571. if (ret != 0)
  1572. return -1302;
  1573. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  1574. return -1303;
  1575. if (encodedLen != sizeof(encodedTestData) ||
  1576. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  1577. return -1304;
  1578. }
  1579. return 0;
  1580. }
  1581. #endif /* WOLFSSL_BASE16 */
  1582. #endif /* !NO_CODING */
  1583. #ifndef NO_ASN
  1584. WOLFSSL_TEST_SUBROUTINE int asn_test(void)
  1585. {
  1586. int ret;
  1587. /* ASN1 encoded date buffer */
  1588. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  1589. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a};
  1590. byte format;
  1591. int length;
  1592. const byte* datePart;
  1593. #ifndef NO_ASN_TIME
  1594. struct tm timearg;
  1595. time_t now;
  1596. #endif
  1597. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  1598. &length);
  1599. if (ret != 0)
  1600. return -1400;
  1601. #ifndef NO_ASN_TIME
  1602. /* Parameter Validation tests. */
  1603. if (wc_GetTime(NULL, sizeof(now)) != BAD_FUNC_ARG)
  1604. return -1401;
  1605. if (wc_GetTime(&now, 0) != BUFFER_E)
  1606. return -1402;
  1607. now = 0;
  1608. if (wc_GetTime(&now, sizeof(now)) != 0) {
  1609. return -1403;
  1610. }
  1611. if (now == 0) {
  1612. printf("RTC/Time not set!\n");
  1613. return -1404;
  1614. }
  1615. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  1616. if (ret != 0)
  1617. return -1405;
  1618. #endif /* !NO_ASN_TIME */
  1619. return 0;
  1620. }
  1621. #endif /* !NO_ASN */
  1622. #ifdef WOLFSSL_MD2
  1623. WOLFSSL_TEST_SUBROUTINE int md2_test(void)
  1624. {
  1625. Md2 md2;
  1626. byte hash[MD2_DIGEST_SIZE];
  1627. testVector a, b, c, d, e, f, g;
  1628. testVector test_md2[7];
  1629. int times = sizeof(test_md2) / sizeof(testVector), i;
  1630. a.input = "";
  1631. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  1632. "\x27\x73";
  1633. a.inLen = XSTRLEN(a.input);
  1634. a.outLen = MD2_DIGEST_SIZE;
  1635. b.input = "a";
  1636. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  1637. "\xb5\xd1";
  1638. b.inLen = XSTRLEN(b.input);
  1639. b.outLen = MD2_DIGEST_SIZE;
  1640. c.input = "abc";
  1641. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  1642. "\xd6\xbb";
  1643. c.inLen = XSTRLEN(c.input);
  1644. c.outLen = MD2_DIGEST_SIZE;
  1645. d.input = "message digest";
  1646. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  1647. "\x06\xb0";
  1648. d.inLen = XSTRLEN(d.input);
  1649. d.outLen = MD2_DIGEST_SIZE;
  1650. e.input = "abcdefghijklmnopqrstuvwxyz";
  1651. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  1652. "\x94\x0b";
  1653. e.inLen = XSTRLEN(e.input);
  1654. e.outLen = MD2_DIGEST_SIZE;
  1655. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1656. "6789";
  1657. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  1658. "\x38\xcd";
  1659. f.inLen = XSTRLEN(f.input);
  1660. f.outLen = MD2_DIGEST_SIZE;
  1661. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1662. "9012345678901234567890";
  1663. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  1664. "\xef\xd8";
  1665. g.inLen = XSTRLEN(g.input);
  1666. g.outLen = MD2_DIGEST_SIZE;
  1667. test_md2[0] = a;
  1668. test_md2[1] = b;
  1669. test_md2[2] = c;
  1670. test_md2[3] = d;
  1671. test_md2[4] = e;
  1672. test_md2[5] = f;
  1673. test_md2[6] = g;
  1674. wc_InitMd2(&md2);
  1675. for (i = 0; i < times; ++i) {
  1676. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  1677. wc_Md2Final(&md2, hash);
  1678. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  1679. return -1500 - i;
  1680. }
  1681. return 0;
  1682. }
  1683. #endif
  1684. #ifndef NO_MD5
  1685. WOLFSSL_TEST_SUBROUTINE int md5_test(void)
  1686. {
  1687. int ret = 0;
  1688. wc_Md5 md5, md5Copy;
  1689. byte hash[WC_MD5_DIGEST_SIZE];
  1690. byte hashcopy[WC_MD5_DIGEST_SIZE];
  1691. testVector a, b, c, d, e, f;
  1692. testVector test_md5[6];
  1693. int times = sizeof(test_md5) / sizeof(testVector), i;
  1694. a.input = "";
  1695. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  1696. "\x7e";
  1697. a.inLen = XSTRLEN(a.input);
  1698. a.outLen = WC_MD5_DIGEST_SIZE;
  1699. b.input = "abc";
  1700. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  1701. "\x72";
  1702. b.inLen = XSTRLEN(b.input);
  1703. b.outLen = WC_MD5_DIGEST_SIZE;
  1704. c.input = "message digest";
  1705. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  1706. "\xd0";
  1707. c.inLen = XSTRLEN(c.input);
  1708. c.outLen = WC_MD5_DIGEST_SIZE;
  1709. d.input = "abcdefghijklmnopqrstuvwxyz";
  1710. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  1711. "\x3b";
  1712. d.inLen = XSTRLEN(d.input);
  1713. d.outLen = WC_MD5_DIGEST_SIZE;
  1714. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1715. "6789";
  1716. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  1717. "\x9f";
  1718. e.inLen = XSTRLEN(e.input);
  1719. e.outLen = WC_MD5_DIGEST_SIZE;
  1720. f.input = "1234567890123456789012345678901234567890123456789012345678"
  1721. "9012345678901234567890";
  1722. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  1723. "\x7a";
  1724. f.inLen = XSTRLEN(f.input);
  1725. f.outLen = WC_MD5_DIGEST_SIZE;
  1726. test_md5[0] = a;
  1727. test_md5[1] = b;
  1728. test_md5[2] = c;
  1729. test_md5[3] = d;
  1730. test_md5[4] = e;
  1731. test_md5[5] = f;
  1732. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  1733. if (ret != 0)
  1734. return -1600;
  1735. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  1736. if (ret != 0) {
  1737. wc_Md5Free(&md5);
  1738. return -1601;
  1739. }
  1740. for (i = 0; i < times; ++i) {
  1741. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  1742. (word32)test_md5[i].inLen);
  1743. if (ret != 0)
  1744. ERROR_OUT(-1602 - i, exit);
  1745. ret = wc_Md5GetHash(&md5, hashcopy);
  1746. if (ret != 0)
  1747. ERROR_OUT(-1603 - i, exit);
  1748. ret = wc_Md5Copy(&md5, &md5Copy);
  1749. if (ret != 0)
  1750. ERROR_OUT(-1604 - i, exit);
  1751. ret = wc_Md5Final(&md5, hash);
  1752. if (ret != 0)
  1753. ERROR_OUT(-1605 - i, exit);
  1754. wc_Md5Free(&md5Copy);
  1755. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  1756. ERROR_OUT(-1606 - i, exit);
  1757. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  1758. ERROR_OUT(-1607 - i, exit);
  1759. }
  1760. /* BEGIN LARGE HASH TEST */ {
  1761. byte large_input[1024];
  1762. const char* large_digest =
  1763. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  1764. for (i = 0; i < (int)sizeof(large_input); i++) {
  1765. large_input[i] = (byte)(i & 0xFF);
  1766. }
  1767. times = 100;
  1768. #ifdef WOLFSSL_PIC32MZ_HASH
  1769. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  1770. #endif
  1771. for (i = 0; i < times; ++i) {
  1772. ret = wc_Md5Update(&md5, (byte*)large_input,
  1773. (word32)sizeof(large_input));
  1774. if (ret != 0)
  1775. ERROR_OUT(-1608, exit);
  1776. }
  1777. ret = wc_Md5Final(&md5, hash);
  1778. if (ret != 0)
  1779. ERROR_OUT(-1609, exit);
  1780. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  1781. ERROR_OUT(-1610, exit);
  1782. } /* END LARGE HASH TEST */
  1783. exit:
  1784. wc_Md5Free(&md5);
  1785. wc_Md5Free(&md5Copy);
  1786. return ret;
  1787. }
  1788. #endif /* NO_MD5 */
  1789. #ifndef NO_MD4
  1790. WOLFSSL_TEST_SUBROUTINE int md4_test(void)
  1791. {
  1792. Md4 md4;
  1793. byte hash[MD4_DIGEST_SIZE];
  1794. testVector a, b, c, d, e, f, g;
  1795. testVector test_md4[7];
  1796. int times = sizeof(test_md4) / sizeof(testVector), i;
  1797. a.input = "";
  1798. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  1799. "\xc0";
  1800. a.inLen = XSTRLEN(a.input);
  1801. a.outLen = MD4_DIGEST_SIZE;
  1802. b.input = "a";
  1803. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  1804. "\x24";
  1805. b.inLen = XSTRLEN(b.input);
  1806. b.outLen = MD4_DIGEST_SIZE;
  1807. c.input = "abc";
  1808. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  1809. "\x9d";
  1810. c.inLen = XSTRLEN(c.input);
  1811. c.outLen = MD4_DIGEST_SIZE;
  1812. d.input = "message digest";
  1813. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  1814. "\x4b";
  1815. d.inLen = XSTRLEN(d.input);
  1816. d.outLen = MD4_DIGEST_SIZE;
  1817. e.input = "abcdefghijklmnopqrstuvwxyz";
  1818. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  1819. "\xa9";
  1820. e.inLen = XSTRLEN(e.input);
  1821. e.outLen = MD4_DIGEST_SIZE;
  1822. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1823. "6789";
  1824. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  1825. "\xe4";
  1826. f.inLen = XSTRLEN(f.input);
  1827. f.outLen = MD4_DIGEST_SIZE;
  1828. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1829. "9012345678901234567890";
  1830. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  1831. "\x36";
  1832. g.inLen = XSTRLEN(g.input);
  1833. g.outLen = MD4_DIGEST_SIZE;
  1834. test_md4[0] = a;
  1835. test_md4[1] = b;
  1836. test_md4[2] = c;
  1837. test_md4[3] = d;
  1838. test_md4[4] = e;
  1839. test_md4[5] = f;
  1840. test_md4[6] = g;
  1841. wc_InitMd4(&md4);
  1842. for (i = 0; i < times; ++i) {
  1843. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  1844. wc_Md4Final(&md4, hash);
  1845. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  1846. return -1700 - i;
  1847. }
  1848. return 0;
  1849. }
  1850. #endif /* NO_MD4 */
  1851. #ifndef NO_SHA
  1852. WOLFSSL_TEST_SUBROUTINE int sha_test(void)
  1853. {
  1854. int ret = 0;
  1855. wc_Sha sha, shaCopy;
  1856. byte hash[WC_SHA_DIGEST_SIZE];
  1857. byte hashcopy[WC_SHA_DIGEST_SIZE];
  1858. testVector a, b, c, d, e;
  1859. testVector test_sha[5];
  1860. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1861. a.input = "";
  1862. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  1863. "\x90\xaf\xd8\x07\x09";
  1864. a.inLen = XSTRLEN(a.input);
  1865. a.outLen = WC_SHA_DIGEST_SIZE;
  1866. b.input = "abc";
  1867. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  1868. "\x6C\x9C\xD0\xD8\x9D";
  1869. b.inLen = XSTRLEN(b.input);
  1870. b.outLen = WC_SHA_DIGEST_SIZE;
  1871. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1872. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  1873. "\xE5\xE5\x46\x70\xF1";
  1874. c.inLen = XSTRLEN(c.input);
  1875. c.outLen = WC_SHA_DIGEST_SIZE;
  1876. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1877. "aaaaaa";
  1878. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  1879. "\x2A\x25\xEC\x64\x4D";
  1880. d.inLen = XSTRLEN(d.input);
  1881. d.outLen = WC_SHA_DIGEST_SIZE;
  1882. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1883. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1884. "aaaaaaaaaa";
  1885. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  1886. "\x53\x99\x5E\x26\xA0";
  1887. e.inLen = XSTRLEN(e.input);
  1888. e.outLen = WC_SHA_DIGEST_SIZE;
  1889. test_sha[0] = a;
  1890. test_sha[1] = b;
  1891. test_sha[2] = c;
  1892. test_sha[3] = d;
  1893. test_sha[4] = e;
  1894. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  1895. if (ret != 0)
  1896. return -1800;
  1897. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  1898. if (ret != 0) {
  1899. wc_ShaFree(&sha);
  1900. return -1801;
  1901. }
  1902. for (i = 0; i < times; ++i) {
  1903. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  1904. (word32)test_sha[i].inLen);
  1905. if (ret != 0)
  1906. ERROR_OUT(-1802 - i, exit);
  1907. ret = wc_ShaGetHash(&sha, hashcopy);
  1908. if (ret != 0)
  1909. ERROR_OUT(-1803 - i, exit);
  1910. ret = wc_ShaCopy(&sha, &shaCopy);
  1911. if (ret != 0)
  1912. ERROR_OUT(-1804 - i, exit);
  1913. ret = wc_ShaFinal(&sha, hash);
  1914. if (ret != 0)
  1915. ERROR_OUT(-1805 - i, exit);
  1916. wc_ShaFree(&shaCopy);
  1917. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  1918. ERROR_OUT(-1806 - i, exit);
  1919. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  1920. ERROR_OUT(-1807 - i, exit);
  1921. }
  1922. /* BEGIN LARGE HASH TEST */ {
  1923. byte large_input[1024];
  1924. #ifdef WOLFSSL_RENESAS_TSIP
  1925. const char* large_digest =
  1926. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  1927. "\x15\x8c\x6d\xb6";
  1928. #else
  1929. const char* large_digest =
  1930. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  1931. "\xc5\xd9\x0a\xac";
  1932. #endif
  1933. for (i = 0; i < (int)sizeof(large_input); i++) {
  1934. large_input[i] = (byte)(i & 0xFF);
  1935. }
  1936. #ifdef WOLFSSL_RENESAS_TSIP
  1937. times = 20;
  1938. #else
  1939. times = 100;
  1940. #endif
  1941. #ifdef WOLFSSL_PIC32MZ_HASH
  1942. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  1943. #endif
  1944. for (i = 0; i < times; ++i) {
  1945. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  1946. (word32)sizeof(large_input));
  1947. if (ret != 0)
  1948. ERROR_OUT(-1808, exit);
  1949. }
  1950. ret = wc_ShaFinal(&sha, hash);
  1951. if (ret != 0)
  1952. ERROR_OUT(-1809, exit);
  1953. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  1954. ERROR_OUT(-1810, exit);
  1955. } /* END LARGE HASH TEST */
  1956. exit:
  1957. wc_ShaFree(&sha);
  1958. wc_ShaFree(&shaCopy);
  1959. return ret;
  1960. }
  1961. #endif /* NO_SHA */
  1962. #ifdef WOLFSSL_RIPEMD
  1963. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void)
  1964. {
  1965. RipeMd ripemd;
  1966. int ret;
  1967. byte hash[RIPEMD_DIGEST_SIZE];
  1968. testVector a, b, c, d;
  1969. testVector test_ripemd[4];
  1970. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  1971. a.input = "abc";
  1972. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  1973. "\xb0\x87\xf1\x5a\x0b\xfc";
  1974. a.inLen = XSTRLEN(a.input);
  1975. a.outLen = RIPEMD_DIGEST_SIZE;
  1976. b.input = "message digest";
  1977. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  1978. "\x5f\xfa\x21\x59\x5f\x36";
  1979. b.inLen = XSTRLEN(b.input);
  1980. b.outLen = RIPEMD_DIGEST_SIZE;
  1981. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1982. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  1983. "\xf4\x9a\xda\x62\xeb\x2b";
  1984. c.inLen = XSTRLEN(c.input);
  1985. c.outLen = RIPEMD_DIGEST_SIZE;
  1986. d.input = "12345678901234567890123456789012345678901234567890123456"
  1987. "789012345678901234567890";
  1988. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  1989. "\x82\xbf\x63\x32\x6b\xfb";
  1990. d.inLen = XSTRLEN(d.input);
  1991. d.outLen = RIPEMD_DIGEST_SIZE;
  1992. test_ripemd[0] = a;
  1993. test_ripemd[1] = b;
  1994. test_ripemd[2] = c;
  1995. test_ripemd[3] = d;
  1996. ret = wc_InitRipeMd(&ripemd);
  1997. if (ret != 0) {
  1998. return -1900;
  1999. }
  2000. for (i = 0; i < times; ++i) {
  2001. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  2002. (word32)test_ripemd[i].inLen);
  2003. if (ret != 0) {
  2004. return -1901 - i;
  2005. }
  2006. ret = wc_RipeMdFinal(&ripemd, hash);
  2007. if (ret != 0) {
  2008. return -1911 - i;
  2009. }
  2010. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  2011. return -1921 - i;
  2012. }
  2013. return 0;
  2014. }
  2015. #endif /* WOLFSSL_RIPEMD */
  2016. #ifdef HAVE_BLAKE2
  2017. #define BLAKE2B_TESTS 3
  2018. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  2019. {
  2020. {
  2021. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  2022. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  2023. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  2024. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  2025. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  2026. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  2027. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  2028. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  2029. },
  2030. {
  2031. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  2032. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  2033. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  2034. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  2035. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  2036. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  2037. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  2038. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  2039. },
  2040. {
  2041. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  2042. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  2043. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  2044. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  2045. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  2046. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  2047. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  2048. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  2049. }
  2050. };
  2051. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void)
  2052. {
  2053. Blake2b b2b;
  2054. byte digest[64];
  2055. byte input[64];
  2056. int i, ret;
  2057. for (i = 0; i < (int)sizeof(input); i++)
  2058. input[i] = (byte)i;
  2059. for (i = 0; i < BLAKE2B_TESTS; i++) {
  2060. ret = wc_InitBlake2b(&b2b, 64);
  2061. if (ret != 0)
  2062. return -2000 - i;
  2063. ret = wc_Blake2bUpdate(&b2b, input, i);
  2064. if (ret != 0)
  2065. return -2010 - 1;
  2066. ret = wc_Blake2bFinal(&b2b, digest, 64);
  2067. if (ret != 0)
  2068. return -2020 - i;
  2069. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  2070. return -2030 - i;
  2071. }
  2072. }
  2073. return 0;
  2074. }
  2075. #endif /* HAVE_BLAKE2 */
  2076. #ifdef HAVE_BLAKE2S
  2077. #define BLAKE2S_TESTS 3
  2078. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  2079. {
  2080. {
  2081. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  2082. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  2083. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  2084. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  2085. },
  2086. {
  2087. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  2088. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  2089. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  2090. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  2091. },
  2092. {
  2093. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  2094. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  2095. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  2096. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  2097. }
  2098. };
  2099. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void)
  2100. {
  2101. Blake2s b2s;
  2102. byte digest[32];
  2103. byte input[64];
  2104. int i, ret;
  2105. for (i = 0; i < (int)sizeof(input); i++)
  2106. input[i] = (byte)i;
  2107. for (i = 0; i < BLAKE2S_TESTS; i++) {
  2108. ret = wc_InitBlake2s(&b2s, 32);
  2109. if (ret != 0)
  2110. return -2100 - i;
  2111. ret = wc_Blake2sUpdate(&b2s, input, i);
  2112. if (ret != 0)
  2113. return -2110 - 1;
  2114. ret = wc_Blake2sFinal(&b2s, digest, 32);
  2115. if (ret != 0)
  2116. return -2120 - i;
  2117. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  2118. return -2130 - i;
  2119. }
  2120. }
  2121. return 0;
  2122. }
  2123. #endif /* HAVE_BLAKE2S */
  2124. #ifdef WOLFSSL_SHA224
  2125. WOLFSSL_TEST_SUBROUTINE int sha224_test(void)
  2126. {
  2127. wc_Sha224 sha, shaCopy;
  2128. byte hash[WC_SHA224_DIGEST_SIZE];
  2129. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  2130. int ret = 0;
  2131. testVector a, b, c;
  2132. testVector test_sha[3];
  2133. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2134. a.input = "";
  2135. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  2136. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  2137. a.inLen = XSTRLEN(a.input);
  2138. a.outLen = WC_SHA224_DIGEST_SIZE;
  2139. b.input = "abc";
  2140. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  2141. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  2142. b.inLen = XSTRLEN(b.input);
  2143. b.outLen = WC_SHA224_DIGEST_SIZE;
  2144. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2145. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  2146. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  2147. c.inLen = XSTRLEN(c.input);
  2148. c.outLen = WC_SHA224_DIGEST_SIZE;
  2149. test_sha[0] = a;
  2150. test_sha[1] = b;
  2151. test_sha[2] = c;
  2152. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  2153. if (ret != 0)
  2154. return -2200;
  2155. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  2156. if (ret != 0) {
  2157. wc_Sha224Free(&sha);
  2158. return -2201;
  2159. }
  2160. for (i = 0; i < times; ++i) {
  2161. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  2162. (word32)test_sha[i].inLen);
  2163. if (ret != 0)
  2164. ERROR_OUT(-2202 - i, exit);
  2165. ret = wc_Sha224GetHash(&sha, hashcopy);
  2166. if (ret != 0)
  2167. ERROR_OUT(-2203 - i, exit);
  2168. ret = wc_Sha224Copy(&sha, &shaCopy);
  2169. if (ret != 0)
  2170. ERROR_OUT(-2204 - i, exit);
  2171. ret = wc_Sha224Final(&sha, hash);
  2172. if (ret != 0)
  2173. ERROR_OUT(-2205 - i, exit);
  2174. wc_Sha224Free(&shaCopy);
  2175. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  2176. ERROR_OUT(-2206 - i, exit);
  2177. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  2178. ERROR_OUT(-2207 - i, exit);
  2179. }
  2180. exit:
  2181. wc_Sha224Free(&sha);
  2182. wc_Sha224Free(&shaCopy);
  2183. return ret;
  2184. }
  2185. #endif
  2186. #ifndef NO_SHA256
  2187. WOLFSSL_TEST_SUBROUTINE int sha256_test(void)
  2188. {
  2189. wc_Sha256 sha, shaCopy;
  2190. byte hash[WC_SHA256_DIGEST_SIZE];
  2191. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  2192. int ret = 0;
  2193. testVector a, b, c;
  2194. testVector test_sha[3];
  2195. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2196. a.input = "";
  2197. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  2198. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  2199. "\xb8\x55";
  2200. a.inLen = XSTRLEN(a.input);
  2201. a.outLen = WC_SHA256_DIGEST_SIZE;
  2202. b.input = "abc";
  2203. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  2204. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  2205. "\x15\xAD";
  2206. b.inLen = XSTRLEN(b.input);
  2207. b.outLen = WC_SHA256_DIGEST_SIZE;
  2208. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2209. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  2210. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  2211. "\x06\xC1";
  2212. c.inLen = XSTRLEN(c.input);
  2213. c.outLen = WC_SHA256_DIGEST_SIZE;
  2214. test_sha[0] = a;
  2215. test_sha[1] = b;
  2216. test_sha[2] = c;
  2217. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  2218. if (ret != 0)
  2219. return -2300;
  2220. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  2221. if (ret != 0) {
  2222. wc_Sha256Free(&sha);
  2223. return -2301;
  2224. }
  2225. for (i = 0; i < times; ++i) {
  2226. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  2227. (word32)test_sha[i].inLen);
  2228. if (ret != 0) {
  2229. ERROR_OUT(-2302 - i, exit);
  2230. }
  2231. ret = wc_Sha256GetHash(&sha, hashcopy);
  2232. if (ret != 0)
  2233. ERROR_OUT(-2303 - i, exit);
  2234. ret = wc_Sha256Copy(&sha, &shaCopy);
  2235. if (ret != 0)
  2236. ERROR_OUT(-2304 - i, exit);
  2237. ret = wc_Sha256Final(&sha, hash);
  2238. if (ret != 0)
  2239. ERROR_OUT(-2305 - i, exit);
  2240. wc_Sha256Free(&shaCopy);
  2241. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  2242. ERROR_OUT(-2306 - i, exit);
  2243. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0)
  2244. ERROR_OUT(-2307 - i, exit);
  2245. }
  2246. /* BEGIN LARGE HASH TEST */ {
  2247. byte large_input[1024];
  2248. #ifdef WOLFSSL_RENESAS_TSIP_CRYPT
  2249. const char* large_digest =
  2250. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  2251. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  2252. #else
  2253. const char* large_digest =
  2254. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  2255. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  2256. #endif
  2257. for (i = 0; i < (int)sizeof(large_input); i++) {
  2258. large_input[i] = (byte)(i & 0xFF);
  2259. }
  2260. #ifdef WOLFSSL_RENESAS_TSIP
  2261. times = 20;
  2262. #else
  2263. times = 100;
  2264. #endif
  2265. #ifdef WOLFSSL_PIC32MZ_HASH
  2266. wc_Sha256SizeSet(&sha, times * sizeof(large_input));
  2267. #endif
  2268. for (i = 0; i < times; ++i) {
  2269. ret = wc_Sha256Update(&sha, (byte*)large_input,
  2270. (word32)sizeof(large_input));
  2271. if (ret != 0)
  2272. ERROR_OUT(-2308, exit);
  2273. }
  2274. ret = wc_Sha256Final(&sha, hash);
  2275. if (ret != 0)
  2276. ERROR_OUT(-2309, exit);
  2277. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  2278. ERROR_OUT(-2310, exit);
  2279. } /* END LARGE HASH TEST */
  2280. exit:
  2281. wc_Sha256Free(&sha);
  2282. wc_Sha256Free(&shaCopy);
  2283. return ret;
  2284. }
  2285. #endif
  2286. #ifdef WOLFSSL_SHA512
  2287. WOLFSSL_TEST_SUBROUTINE int sha512_test(void)
  2288. {
  2289. wc_Sha512 sha, shaCopy;
  2290. byte hash[WC_SHA512_DIGEST_SIZE];
  2291. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  2292. int ret = 0;
  2293. testVector a, b, c;
  2294. testVector test_sha[3];
  2295. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2296. a.input = "";
  2297. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  2298. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  2299. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  2300. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  2301. "\xf9\x27\xda\x3e";
  2302. a.inLen = XSTRLEN(a.input);
  2303. a.outLen = WC_SHA512_DIGEST_SIZE;
  2304. b.input = "abc";
  2305. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  2306. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  2307. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  2308. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  2309. "\xa5\x4c\xa4\x9f";
  2310. b.inLen = XSTRLEN(b.input);
  2311. b.outLen = WC_SHA512_DIGEST_SIZE;
  2312. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2313. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2314. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  2315. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  2316. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  2317. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  2318. "\x87\x4b\xe9\x09";
  2319. c.inLen = XSTRLEN(c.input);
  2320. c.outLen = WC_SHA512_DIGEST_SIZE;
  2321. test_sha[0] = a;
  2322. test_sha[1] = b;
  2323. test_sha[2] = c;
  2324. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  2325. if (ret != 0)
  2326. return -2400;
  2327. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  2328. if (ret != 0) {
  2329. wc_Sha512Free(&sha);
  2330. return -2401;
  2331. }
  2332. for (i = 0; i < times; ++i) {
  2333. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  2334. (word32)test_sha[i].inLen);
  2335. if (ret != 0)
  2336. ERROR_OUT(-2402 - i, exit);
  2337. ret = wc_Sha512GetHash(&sha, hashcopy);
  2338. if (ret != 0)
  2339. ERROR_OUT(-2403 - i, exit);
  2340. ret = wc_Sha512Copy(&sha, &shaCopy);
  2341. if (ret != 0)
  2342. ERROR_OUT(-2404 - i, exit);
  2343. ret = wc_Sha512Final(&sha, hash);
  2344. if (ret != 0)
  2345. ERROR_OUT(-2405 - i, exit);
  2346. wc_Sha512Free(&shaCopy);
  2347. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  2348. ERROR_OUT(-2406 - i, exit);
  2349. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  2350. ERROR_OUT(-2407 - i, exit);
  2351. }
  2352. /* BEGIN LARGE HASH TEST */ {
  2353. byte large_input[1024];
  2354. const char* large_digest =
  2355. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  2356. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  2357. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  2358. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  2359. for (i = 0; i < (int)sizeof(large_input); i++) {
  2360. large_input[i] = (byte)(i & 0xFF);
  2361. }
  2362. times = 100;
  2363. for (i = 0; i < times; ++i) {
  2364. ret = wc_Sha512Update(&sha, (byte*)large_input,
  2365. (word32)sizeof(large_input));
  2366. if (ret != 0)
  2367. ERROR_OUT(-2408, exit);
  2368. }
  2369. ret = wc_Sha512Final(&sha, hash);
  2370. if (ret != 0)
  2371. ERROR_OUT(-2409, exit);
  2372. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  2373. ERROR_OUT(-2410, exit);
  2374. } /* END LARGE HASH TEST */
  2375. exit:
  2376. wc_Sha512Free(&sha);
  2377. wc_Sha512Free(&shaCopy);
  2378. return ret;
  2379. }
  2380. #endif
  2381. #ifdef WOLFSSL_SHA384
  2382. WOLFSSL_TEST_SUBROUTINE int sha384_test(void)
  2383. {
  2384. wc_Sha384 sha, shaCopy;
  2385. byte hash[WC_SHA384_DIGEST_SIZE];
  2386. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  2387. int ret = 0;
  2388. testVector a, b, c;
  2389. testVector test_sha[3];
  2390. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2391. a.input = "";
  2392. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  2393. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  2394. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  2395. "\x98\xb9\x5b";
  2396. a.inLen = XSTRLEN(a.input);
  2397. a.outLen = WC_SHA384_DIGEST_SIZE;
  2398. b.input = "abc";
  2399. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  2400. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  2401. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  2402. "\xc8\x25\xa7";
  2403. b.inLen = XSTRLEN(b.input);
  2404. b.outLen = WC_SHA384_DIGEST_SIZE;
  2405. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2406. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2407. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  2408. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  2409. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  2410. "\x74\x60\x39";
  2411. c.inLen = XSTRLEN(c.input);
  2412. c.outLen = WC_SHA384_DIGEST_SIZE;
  2413. test_sha[0] = a;
  2414. test_sha[1] = b;
  2415. test_sha[2] = c;
  2416. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  2417. if (ret != 0)
  2418. return -2500;
  2419. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  2420. if (ret != 0) {
  2421. wc_Sha384Free(&sha);
  2422. return -2501;
  2423. }
  2424. for (i = 0; i < times; ++i) {
  2425. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  2426. (word32)test_sha[i].inLen);
  2427. if (ret != 0)
  2428. ERROR_OUT(-2502 - i, exit);
  2429. ret = wc_Sha384GetHash(&sha, hashcopy);
  2430. if (ret != 0)
  2431. ERROR_OUT(-2503 - i, exit);
  2432. ret = wc_Sha384Copy(&sha, &shaCopy);
  2433. if (ret != 0)
  2434. ERROR_OUT(-2504 - i, exit);
  2435. ret = wc_Sha384Final(&sha, hash);
  2436. if (ret != 0)
  2437. ERROR_OUT(-2505 - i, exit);
  2438. wc_Sha384Free(&shaCopy);
  2439. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  2440. ERROR_OUT(-2506 - i, exit);
  2441. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  2442. ERROR_OUT(-2507 - i, exit);
  2443. }
  2444. /* BEGIN LARGE HASH TEST */ {
  2445. byte large_input[1024];
  2446. const char* large_digest =
  2447. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  2448. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  2449. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  2450. for (i = 0; i < (int)sizeof(large_input); i++) {
  2451. large_input[i] = (byte)(i & 0xFF);
  2452. }
  2453. times = 100;
  2454. for (i = 0; i < times; ++i) {
  2455. ret = wc_Sha384Update(&sha, (byte*)large_input,
  2456. (word32)sizeof(large_input));
  2457. if (ret != 0)
  2458. ERROR_OUT(-2508, exit);
  2459. }
  2460. ret = wc_Sha384Final(&sha, hash);
  2461. if (ret != 0)
  2462. ERROR_OUT(-2509, exit);
  2463. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  2464. ERROR_OUT(-2510, exit);
  2465. } /* END LARGE HASH TEST */
  2466. exit:
  2467. wc_Sha384Free(&sha);
  2468. wc_Sha384Free(&shaCopy);
  2469. return ret;
  2470. }
  2471. #endif /* WOLFSSL_SHA384 */
  2472. #ifdef WOLFSSL_SHA3
  2473. #ifndef WOLFSSL_NOSHA3_224
  2474. static int sha3_224_test(void)
  2475. {
  2476. wc_Sha3 sha;
  2477. byte hash[WC_SHA3_224_DIGEST_SIZE];
  2478. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  2479. testVector a, b, c;
  2480. testVector test_sha[3];
  2481. int ret = 0;
  2482. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2483. a.input = "";
  2484. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  2485. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  2486. a.inLen = XSTRLEN(a.input);
  2487. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  2488. b.input = "abc";
  2489. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  2490. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  2491. b.inLen = XSTRLEN(b.input);
  2492. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  2493. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2494. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  2495. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  2496. c.inLen = XSTRLEN(c.input);
  2497. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  2498. test_sha[0] = a;
  2499. test_sha[1] = b;
  2500. test_sha[2] = c;
  2501. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  2502. if (ret != 0)
  2503. return -2600;
  2504. for (i = 0; i < times; ++i) {
  2505. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  2506. (word32)test_sha[i].inLen);
  2507. if (ret != 0)
  2508. ERROR_OUT(-2601 - i, exit);
  2509. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  2510. if (ret != 0)
  2511. ERROR_OUT(-2602 - i, exit);
  2512. ret = wc_Sha3_224_Final(&sha, hash);
  2513. if (ret != 0)
  2514. ERROR_OUT(-2603 - i, exit);
  2515. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  2516. ERROR_OUT(-2604 - i, exit);
  2517. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  2518. ERROR_OUT(-2605 - i, exit);
  2519. }
  2520. /* BEGIN LARGE HASH TEST */ {
  2521. byte large_input[1024];
  2522. const char* large_digest =
  2523. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  2524. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  2525. for (i = 0; i < (int)sizeof(large_input); i++) {
  2526. large_input[i] = (byte)(i & 0xFF);
  2527. }
  2528. times = 100;
  2529. for (i = 0; i < times; ++i) {
  2530. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  2531. (word32)sizeof(large_input));
  2532. if (ret != 0)
  2533. ERROR_OUT(-2606, exit);
  2534. }
  2535. ret = wc_Sha3_224_Final(&sha, hash);
  2536. if (ret != 0)
  2537. ERROR_OUT(-2607, exit);
  2538. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  2539. ERROR_OUT(-2608, exit);
  2540. } /* END LARGE HASH TEST */
  2541. exit:
  2542. wc_Sha3_224_Free(&sha);
  2543. return ret;
  2544. }
  2545. #endif /* WOLFSSL_NOSHA3_224 */
  2546. #ifndef WOLFSSL_NOSHA3_256
  2547. static int sha3_256_test(void)
  2548. {
  2549. wc_Sha3 sha;
  2550. byte hash[WC_SHA3_256_DIGEST_SIZE];
  2551. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  2552. testVector a, b, c;
  2553. testVector test_sha[3];
  2554. int ret = 0;
  2555. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2556. byte large_input[1024];
  2557. const char* large_digest =
  2558. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  2559. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  2560. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2561. /* test vector with hash of empty string */
  2562. const char* Keccak256EmptyOut =
  2563. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  2564. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  2565. #endif
  2566. a.input = "";
  2567. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  2568. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  2569. "\x43\x4a";
  2570. a.inLen = XSTRLEN(a.input);
  2571. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  2572. b.input = "abc";
  2573. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  2574. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  2575. "\x15\x32";
  2576. b.inLen = XSTRLEN(b.input);
  2577. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  2578. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2579. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  2580. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  2581. "\x33\x76";
  2582. c.inLen = XSTRLEN(c.input);
  2583. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  2584. test_sha[0] = a;
  2585. test_sha[1] = b;
  2586. test_sha[2] = c;
  2587. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  2588. if (ret != 0)
  2589. return -2700;
  2590. for (i = 0; i < times; ++i) {
  2591. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  2592. (word32)test_sha[i].inLen);
  2593. if (ret != 0)
  2594. ERROR_OUT(-2701 - i, exit);
  2595. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  2596. if (ret != 0)
  2597. ERROR_OUT(-2702 - i, exit);
  2598. ret = wc_Sha3_256_Final(&sha, hash);
  2599. if (ret != 0)
  2600. ERROR_OUT(-2703 - i, exit);
  2601. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  2602. ERROR_OUT(-2704 - i, exit);
  2603. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  2604. ERROR_OUT(-2705 - i, exit);
  2605. }
  2606. /* BEGIN LARGE HASH TEST */ {
  2607. for (i = 0; i < (int)sizeof(large_input); i++) {
  2608. large_input[i] = (byte)(i & 0xFF);
  2609. }
  2610. times = 100;
  2611. for (i = 0; i < times; ++i) {
  2612. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  2613. (word32)sizeof(large_input));
  2614. if (ret != 0)
  2615. ERROR_OUT(-2706, exit);
  2616. }
  2617. ret = wc_Sha3_256_Final(&sha, hash);
  2618. if (ret != 0)
  2619. ERROR_OUT(-2707, exit);
  2620. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  2621. ERROR_OUT(-2708, exit);
  2622. } /* END LARGE HASH TEST */
  2623. /* this is a software only variant of SHA3 not supported by external hardware devices */
  2624. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2625. /* Test for Keccak256 */
  2626. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  2627. if (ret != 0) {
  2628. ERROR_OUT(-2709, exit);
  2629. }
  2630. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  2631. if (ret != 0) {
  2632. ERROR_OUT(-2710, exit);
  2633. }
  2634. ret = wc_Sha3_256_Final(&sha, hash);
  2635. if (ret != 0) {
  2636. ERROR_OUT(-2711, exit);
  2637. }
  2638. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  2639. ERROR_OUT(-2712, exit);
  2640. }
  2641. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  2642. exit:
  2643. wc_Sha3_256_Free(&sha);
  2644. return ret;
  2645. }
  2646. #endif /* WOLFSSL_NOSHA3_256 */
  2647. #ifndef WOLFSSL_NOSHA3_384
  2648. static int sha3_384_test(void)
  2649. {
  2650. wc_Sha3 sha;
  2651. byte hash[WC_SHA3_384_DIGEST_SIZE];
  2652. #ifndef NO_INTM_HASH_TEST
  2653. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  2654. #endif
  2655. testVector a, b, c;
  2656. testVector test_sha[3];
  2657. int ret;
  2658. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2659. a.input = "";
  2660. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  2661. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  2662. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  2663. "\xd5\xf0\x04";
  2664. a.inLen = XSTRLEN(a.input);
  2665. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  2666. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT)
  2667. /* NIST test vector with a length that is a multiple of 4 */
  2668. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  2669. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  2670. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  2671. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  2672. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  2673. "\x19\x87\x22";
  2674. b.inLen = XSTRLEN(b.input);
  2675. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2676. #else
  2677. b.input = "abc";
  2678. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  2679. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  2680. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  2681. "\x37\x6d\x25";
  2682. b.inLen = XSTRLEN(b.input);
  2683. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2684. #endif
  2685. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2686. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  2687. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  2688. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  2689. "\x65\x7c\x22";
  2690. c.inLen = XSTRLEN(c.input);
  2691. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  2692. #ifdef WOLFSSL_XILINX_CRYPT
  2693. test_sha[0] = b; /* hardware acc. can not handle "" string */
  2694. #else
  2695. test_sha[0] = a;
  2696. #endif
  2697. test_sha[1] = b;
  2698. test_sha[2] = c;
  2699. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  2700. if (ret != 0)
  2701. return -2800;
  2702. for (i = 0; i < times; ++i) {
  2703. ret = wc_Sha3_384_Update(&sha, (byte*)test_sha[i].input,
  2704. (word32)test_sha[i].inLen);
  2705. if (ret != 0)
  2706. ERROR_OUT(-2801 - i, exit);
  2707. #ifndef NO_INTM_HASH_TEST
  2708. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  2709. if (ret != 0)
  2710. ERROR_OUT(-2802 - i, exit);
  2711. #endif
  2712. ret = wc_Sha3_384_Final(&sha, hash);
  2713. if (ret != 0)
  2714. ERROR_OUT(-2803 - i, exit);
  2715. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  2716. ERROR_OUT(-2804 - i, exit);
  2717. #ifndef NO_INTM_HASH_TEST
  2718. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  2719. ERROR_OUT(-2805 - i, exit);
  2720. #endif
  2721. }
  2722. /* BEGIN LARGE HASH TEST */ {
  2723. byte large_input[1024];
  2724. const char* large_digest =
  2725. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  2726. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  2727. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  2728. for (i = 0; i < (int)sizeof(large_input); i++) {
  2729. large_input[i] = (byte)(i & 0xFF);
  2730. }
  2731. times = 100;
  2732. for (i = 0; i < times; ++i) {
  2733. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  2734. (word32)sizeof(large_input));
  2735. if (ret != 0)
  2736. ERROR_OUT(-2806, exit);
  2737. }
  2738. ret = wc_Sha3_384_Final(&sha, hash);
  2739. if (ret != 0)
  2740. ERROR_OUT(-2807, exit);
  2741. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  2742. ERROR_OUT(-2808, exit);
  2743. } /* END LARGE HASH TEST */
  2744. exit:
  2745. wc_Sha3_384_Free(&sha);
  2746. return ret;
  2747. }
  2748. #endif /* WOLFSSL_NOSHA3_384 */
  2749. #ifndef WOLFSSL_NOSHA3_512
  2750. static int sha3_512_test(void)
  2751. {
  2752. wc_Sha3 sha;
  2753. byte hash[WC_SHA3_512_DIGEST_SIZE];
  2754. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  2755. testVector a, b, c;
  2756. testVector test_sha[3];
  2757. int ret;
  2758. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2759. a.input = "";
  2760. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  2761. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  2762. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  2763. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  2764. "\x28\x1d\xcd\x26";
  2765. a.inLen = XSTRLEN(a.input);
  2766. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  2767. b.input = "abc";
  2768. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  2769. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  2770. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  2771. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  2772. "\x4e\xec\x53\xf0";
  2773. b.inLen = XSTRLEN(b.input);
  2774. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  2775. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2776. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  2777. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  2778. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  2779. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  2780. "\x39\xd6\xe7\x5e";
  2781. c.inLen = XSTRLEN(c.input);
  2782. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  2783. test_sha[0] = a;
  2784. test_sha[1] = b;
  2785. test_sha[2] = c;
  2786. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  2787. if (ret != 0)
  2788. return -2900;
  2789. for (i = 0; i < times; ++i) {
  2790. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  2791. (word32)test_sha[i].inLen);
  2792. if (ret != 0)
  2793. ERROR_OUT(-2901 - i, exit);
  2794. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  2795. if (ret != 0)
  2796. ERROR_OUT(-2902 - i, exit);
  2797. ret = wc_Sha3_512_Final(&sha, hash);
  2798. if (ret != 0)
  2799. ERROR_OUT(-2903 - i, exit);
  2800. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  2801. ERROR_OUT(-2904 - i, exit);
  2802. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  2803. ERROR_OUT(-2905 - i, exit);
  2804. }
  2805. /* BEGIN LARGE HASH TEST */ {
  2806. byte large_input[1024];
  2807. const char* large_digest =
  2808. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  2809. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  2810. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  2811. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  2812. for (i = 0; i < (int)sizeof(large_input); i++) {
  2813. large_input[i] = (byte)(i & 0xFF);
  2814. }
  2815. times = 100;
  2816. for (i = 0; i < times; ++i) {
  2817. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  2818. (word32)sizeof(large_input));
  2819. if (ret != 0)
  2820. ERROR_OUT(-2906, exit);
  2821. }
  2822. ret = wc_Sha3_512_Final(&sha, hash);
  2823. if (ret != 0)
  2824. ERROR_OUT(-2907, exit);
  2825. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  2826. ERROR_OUT(-2908, exit);
  2827. } /* END LARGE HASH TEST */
  2828. exit:
  2829. wc_Sha3_512_Free(&sha);
  2830. return ret;
  2831. }
  2832. #endif /* WOLFSSL_NOSHA3_512 */
  2833. WOLFSSL_TEST_SUBROUTINE int sha3_test(void)
  2834. {
  2835. int ret;
  2836. (void)ret;
  2837. #ifndef WOLFSSL_NOSHA3_224
  2838. if ((ret = sha3_224_test()) != 0)
  2839. return ret;
  2840. #endif
  2841. #ifndef WOLFSSL_NOSHA3_256
  2842. if ((ret = sha3_256_test()) != 0)
  2843. return ret;
  2844. #endif
  2845. #ifndef WOLFSSL_NOSHA3_384
  2846. if ((ret = sha3_384_test()) != 0)
  2847. return ret;
  2848. #endif
  2849. #ifndef WOLFSSL_NOSHA3_512
  2850. if ((ret = sha3_512_test()) != 0)
  2851. return ret;
  2852. #endif
  2853. return 0;
  2854. }
  2855. #endif /* WOLFSSL_SHA3 */
  2856. #ifdef WOLFSSL_SHAKE256
  2857. WOLFSSL_TEST_SUBROUTINE int shake256_test(void)
  2858. {
  2859. #ifndef WOLFSSL_NO_SHAKE256
  2860. wc_Shake sha;
  2861. byte hash[114];
  2862. testVector a, b, c;
  2863. testVector test_sha[3];
  2864. int ret = 0;
  2865. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2866. byte large_input[1024];
  2867. const char* large_digest =
  2868. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  2869. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  2870. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  2871. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  2872. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  2873. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  2874. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  2875. "\xea\x26";
  2876. a.input = "";
  2877. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  2878. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  2879. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  2880. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  2881. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  2882. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  2883. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  2884. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  2885. a.inLen = XSTRLEN(a.input);
  2886. a.outLen = sizeof(hash);
  2887. b.input = "abc";
  2888. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  2889. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  2890. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  2891. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  2892. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  2893. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  2894. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  2895. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  2896. b.inLen = XSTRLEN(b.input);
  2897. b.outLen = sizeof(hash);
  2898. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2899. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  2900. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  2901. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  2902. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  2903. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  2904. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  2905. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  2906. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  2907. c.inLen = XSTRLEN(c.input);
  2908. c.outLen = sizeof(hash);
  2909. test_sha[0] = a;
  2910. test_sha[1] = b;
  2911. test_sha[2] = c;
  2912. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  2913. if (ret != 0)
  2914. return -3100;
  2915. for (i = 0; i < times; ++i) {
  2916. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  2917. (word32)test_sha[i].inLen);
  2918. if (ret != 0)
  2919. ERROR_OUT(-3101 - i, exit);
  2920. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  2921. if (ret != 0)
  2922. ERROR_OUT(-3102 - i, exit);
  2923. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  2924. ERROR_OUT(-3103 - i, exit);
  2925. }
  2926. /* BEGIN LARGE HASH TEST */ {
  2927. for (i = 0; i < (int)sizeof(large_input); i++) {
  2928. large_input[i] = (byte)(i & 0xFF);
  2929. }
  2930. times = 100;
  2931. for (i = 0; i < times; ++i) {
  2932. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  2933. (word32)sizeof(large_input));
  2934. if (ret != 0)
  2935. ERROR_OUT(-3104, exit);
  2936. }
  2937. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  2938. if (ret != 0)
  2939. ERROR_OUT(-3105, exit);
  2940. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  2941. ERROR_OUT(-3106, exit);
  2942. } /* END LARGE HASH TEST */
  2943. exit:
  2944. wc_Shake256_Free(&sha);
  2945. return ret;
  2946. #else
  2947. return 0;
  2948. #endif
  2949. }
  2950. #endif
  2951. WOLFSSL_TEST_SUBROUTINE int hash_test(void)
  2952. {
  2953. wc_HashAlg hash;
  2954. int ret, exp_ret;
  2955. int i, j;
  2956. int digestSz;
  2957. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  2958. byte out[WC_MAX_DIGEST_SIZE];
  2959. byte hashOut[WC_MAX_DIGEST_SIZE];
  2960. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  2961. enum wc_HashType hashType;
  2962. #endif
  2963. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  2964. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  2965. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  2966. WC_HASH_TYPE_SHA3_224,
  2967. WC_HASH_TYPE_SHA3_256,
  2968. WC_HASH_TYPE_SHA3_384,
  2969. WC_HASH_TYPE_SHA3_512 };
  2970. enum wc_HashType typesNoImpl[] = {
  2971. #ifdef NO_MD5
  2972. WC_HASH_TYPE_MD5,
  2973. #endif
  2974. #ifdef NO_SHA
  2975. WC_HASH_TYPE_SHA,
  2976. #endif
  2977. #ifndef WOLFSSL_SHA224
  2978. WC_HASH_TYPE_SHA224,
  2979. #endif
  2980. #ifdef NO_SHA256
  2981. WC_HASH_TYPE_SHA256,
  2982. #endif
  2983. #ifndef WOLFSSL_SHA384
  2984. WC_HASH_TYPE_SHA384,
  2985. #endif
  2986. #ifndef WOLFSSL_SHA512
  2987. WC_HASH_TYPE_SHA512,
  2988. #endif
  2989. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  2990. WC_HASH_TYPE_SHA3_224,
  2991. #endif
  2992. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  2993. WC_HASH_TYPE_SHA3_256,
  2994. #endif
  2995. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  2996. WC_HASH_TYPE_SHA3_384,
  2997. #endif
  2998. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  2999. WC_HASH_TYPE_SHA3_512,
  3000. #endif
  3001. WC_HASH_TYPE_NONE
  3002. };
  3003. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  3004. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  3005. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  3006. WC_HASH_TYPE_BLAKE2B,
  3007. WC_HASH_TYPE_NONE };
  3008. /* Parameter Validation testing. */
  3009. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  3010. if (ret != BAD_FUNC_ARG)
  3011. return -3200;
  3012. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3013. if (ret != BAD_FUNC_ARG)
  3014. return -3201;
  3015. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3016. if (ret != BAD_FUNC_ARG)
  3017. return -3202;
  3018. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  3019. if (ret != BAD_FUNC_ARG)
  3020. return -3203;
  3021. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  3022. if (ret != BAD_FUNC_ARG)
  3023. return -3204;
  3024. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  3025. if (ret != BAD_FUNC_ARG)
  3026. return -3205;
  3027. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  3028. if (ret != BAD_FUNC_ARG)
  3029. return -3206;
  3030. /* Try invalid hash algorithms. */
  3031. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  3032. ret = wc_HashInit(&hash, typesBad[i]);
  3033. if (ret != BAD_FUNC_ARG)
  3034. return -3207 - i;
  3035. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  3036. if (ret != BAD_FUNC_ARG)
  3037. return -3217 - i;
  3038. ret = wc_HashFinal(&hash, typesBad[i], out);
  3039. if (ret != BAD_FUNC_ARG)
  3040. return -3227 - i;
  3041. wc_HashFree(&hash, typesBad[i]);
  3042. }
  3043. /* Try valid hash algorithms. */
  3044. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  3045. exp_ret = 0;
  3046. if (typesGood[i] == typesNoImpl[j]) {
  3047. /* Recognized but no implementation compiled in. */
  3048. exp_ret = HASH_TYPE_E;
  3049. j++;
  3050. }
  3051. ret = wc_HashInit(&hash, typesGood[i]);
  3052. if (ret != exp_ret)
  3053. return -3237 - i;
  3054. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  3055. if (ret != exp_ret)
  3056. return -3247 - i;
  3057. ret = wc_HashFinal(&hash, typesGood[i], out);
  3058. if (ret != exp_ret)
  3059. return -3257 - i;
  3060. wc_HashFree(&hash, typesGood[i]);
  3061. digestSz = wc_HashGetDigestSize(typesGood[i]);
  3062. if (exp_ret < 0 && digestSz != exp_ret)
  3063. return -3267 - i;
  3064. if (exp_ret == 0 && digestSz < 0)
  3065. return -3277 - i;
  3066. if (exp_ret == 0) {
  3067. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  3068. digestSz - 1);
  3069. if (ret != BUFFER_E)
  3070. return -3287 - i;
  3071. }
  3072. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, digestSz);
  3073. if (ret != exp_ret)
  3074. return -3297 - i;
  3075. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  3076. return -3307 -i;
  3077. ret = wc_HashGetBlockSize(typesGood[i]);
  3078. if (exp_ret < 0 && ret != exp_ret)
  3079. return -3308 - i;
  3080. if (exp_ret == 0 && ret < 0)
  3081. return -3318 - i;
  3082. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3083. ret = wc_HashGetOID(typesGood[i]);
  3084. if (ret == BAD_FUNC_ARG ||
  3085. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  3086. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  3087. return -3328 - i;
  3088. }
  3089. hashType = wc_OidGetHash(ret);
  3090. if (exp_ret == 0 && hashType != typesGood[i])
  3091. return -3338 - i;
  3092. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3093. }
  3094. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  3095. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  3096. if (ret != BAD_FUNC_ARG && ret != BUFFER_E)
  3097. return -3348 - i;
  3098. }
  3099. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3100. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  3101. #ifdef WOLFSSL_MD2
  3102. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3103. return -3358;
  3104. #else
  3105. if (ret != HASH_TYPE_E)
  3106. return -3359;
  3107. #endif
  3108. hashType = wc_OidGetHash(646); /* Md2h */
  3109. #ifdef WOLFSSL_MD2
  3110. if (hashType != WC_HASH_TYPE_MD2)
  3111. return -3360;
  3112. #else
  3113. if (hashType != WC_HASH_TYPE_NONE)
  3114. return -3361;
  3115. #endif
  3116. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  3117. #ifndef NO_MD5
  3118. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3119. return -3362;
  3120. #else
  3121. if (ret != HASH_TYPE_E)
  3122. return -3363;
  3123. #endif
  3124. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  3125. if (ret != BAD_FUNC_ARG)
  3126. return -3364;
  3127. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  3128. if (ret != BAD_FUNC_ARG)
  3129. return -3365;
  3130. hashType = wc_OidGetHash(0);
  3131. if (hashType != WC_HASH_TYPE_NONE)
  3132. return -3366;
  3133. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3134. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  3135. #ifdef WOLFSSL_MD2
  3136. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3137. return -3367;
  3138. #else
  3139. if (ret != HASH_TYPE_E)
  3140. return -3368;
  3141. #endif
  3142. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  3143. #ifdef WOLFSSL_MD2
  3144. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3145. return -3369;
  3146. #else
  3147. if (ret != HASH_TYPE_E)
  3148. return -3370;
  3149. #endif
  3150. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  3151. #ifndef NO_MD4
  3152. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3153. return -3371;
  3154. #else
  3155. if (ret != HASH_TYPE_E)
  3156. return -3372;
  3157. #endif
  3158. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  3159. #ifndef NO_MD4
  3160. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3161. return -3373;
  3162. #else
  3163. if (ret != HASH_TYPE_E)
  3164. return -3374;
  3165. #endif
  3166. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  3167. #if !defined(NO_MD5) && !defined(NO_SHA)
  3168. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3169. return -3375;
  3170. #else
  3171. if (ret != HASH_TYPE_E)
  3172. return -3376;
  3173. #endif
  3174. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  3175. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3176. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3177. return -3377;
  3178. #else
  3179. if (ret != HASH_TYPE_E)
  3180. return -3378;
  3181. #endif
  3182. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  3183. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3184. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3185. return -3379;
  3186. #else
  3187. if (ret != HASH_TYPE_E)
  3188. return -3380;
  3189. #endif
  3190. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  3191. if (ret != BAD_FUNC_ARG)
  3192. return -3381;
  3193. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  3194. if (ret != BAD_FUNC_ARG)
  3195. return -3382;
  3196. #if !defined(NO_CERTS) && !defined(NO_ASN)
  3197. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  3198. ret = wc_GetCTC_HashOID(MD2);
  3199. if (ret == 0)
  3200. return -3383;
  3201. #endif
  3202. #ifndef NO_MD5
  3203. ret = wc_GetCTC_HashOID(WC_MD5);
  3204. if (ret == 0)
  3205. return -3384;
  3206. #endif
  3207. #ifndef NO_SHA
  3208. ret = wc_GetCTC_HashOID(WC_SHA);
  3209. if (ret == 0)
  3210. return -3385;
  3211. #endif
  3212. #ifdef WOLFSSL_SHA224
  3213. ret = wc_GetCTC_HashOID(WC_SHA224);
  3214. if (ret == 0)
  3215. return -3386;
  3216. #endif
  3217. #ifndef NO_SHA256
  3218. ret = wc_GetCTC_HashOID(WC_SHA256);
  3219. if (ret == 0)
  3220. return -3387;
  3221. #endif
  3222. #ifdef WOLFSSL_SHA384
  3223. ret = wc_GetCTC_HashOID(WC_SHA384);
  3224. if (ret == 0)
  3225. return -3388;
  3226. #endif
  3227. #ifdef WOLFSSL_SHA512
  3228. ret = wc_GetCTC_HashOID(WC_SHA512);
  3229. if (ret == 0)
  3230. return -3389;
  3231. #endif
  3232. ret = wc_GetCTC_HashOID(-1);
  3233. if (ret != 0)
  3234. return -3390;
  3235. #endif
  3236. return 0;
  3237. }
  3238. #if !defined(NO_HMAC) && !defined(NO_MD5)
  3239. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void)
  3240. {
  3241. Hmac hmac;
  3242. byte hash[WC_MD5_DIGEST_SIZE];
  3243. const char* keys[]=
  3244. {
  3245. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  3246. "Jefe",
  3247. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3248. };
  3249. testVector a, b, c;
  3250. testVector test_hmac[3];
  3251. int ret;
  3252. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3253. a.input = "Hi There";
  3254. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  3255. "\x9d";
  3256. a.inLen = XSTRLEN(a.input);
  3257. a.outLen = WC_MD5_DIGEST_SIZE;
  3258. b.input = "what do ya want for nothing?";
  3259. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  3260. "\x38";
  3261. b.inLen = XSTRLEN(b.input);
  3262. b.outLen = WC_MD5_DIGEST_SIZE;
  3263. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3264. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3265. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3266. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3267. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  3268. "\xf6";
  3269. c.inLen = XSTRLEN(c.input);
  3270. c.outLen = WC_MD5_DIGEST_SIZE;
  3271. test_hmac[0] = a;
  3272. test_hmac[1] = b;
  3273. test_hmac[2] = c;
  3274. for (i = 0; i < times; ++i) {
  3275. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3276. if (i == 1) {
  3277. continue; /* cavium can't handle short keys, fips not allowed */
  3278. }
  3279. #endif
  3280. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0) {
  3281. return -3400;
  3282. }
  3283. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  3284. (word32)XSTRLEN(keys[i]));
  3285. if (ret != 0)
  3286. return -3401;
  3287. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3288. (word32)test_hmac[i].inLen);
  3289. if (ret != 0)
  3290. return -3402;
  3291. ret = wc_HmacFinal(&hmac, hash);
  3292. if (ret != 0)
  3293. return -3403;
  3294. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  3295. return -3404 - i;
  3296. wc_HmacFree(&hmac);
  3297. }
  3298. #ifndef HAVE_FIPS
  3299. if (wc_HmacSizeByType(WC_MD5) != WC_MD5_DIGEST_SIZE)
  3300. return -3414;
  3301. #endif
  3302. return 0;
  3303. }
  3304. #endif /* NO_HMAC && NO_MD5 */
  3305. #if !defined(NO_HMAC) && !defined(NO_SHA)
  3306. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void)
  3307. {
  3308. Hmac hmac;
  3309. byte hash[WC_SHA_DIGEST_SIZE];
  3310. const char* keys[]=
  3311. {
  3312. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3313. "\x0b\x0b\x0b",
  3314. "Jefe",
  3315. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3316. "\xAA\xAA\xAA"
  3317. };
  3318. testVector a, b, c;
  3319. testVector test_hmac[3];
  3320. int ret;
  3321. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3322. a.input = "Hi There";
  3323. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  3324. "\x8e\xf1\x46\xbe\x00";
  3325. a.inLen = XSTRLEN(a.input);
  3326. a.outLen = WC_SHA_DIGEST_SIZE;
  3327. b.input = "what do ya want for nothing?";
  3328. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  3329. "\x9c\x25\x9a\x7c\x79";
  3330. b.inLen = XSTRLEN(b.input);
  3331. b.outLen = WC_SHA_DIGEST_SIZE;
  3332. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3333. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3334. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3335. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3336. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  3337. "\x4f\x63\xf1\x75\xd3";
  3338. c.inLen = XSTRLEN(c.input);
  3339. c.outLen = WC_SHA_DIGEST_SIZE;
  3340. test_hmac[0] = a;
  3341. test_hmac[1] = b;
  3342. test_hmac[2] = c;
  3343. for (i = 0; i < times; ++i) {
  3344. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3345. if (i == 1)
  3346. continue; /* cavium can't handle short keys, fips not allowed */
  3347. #endif
  3348. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3349. return -3500;
  3350. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  3351. (word32)XSTRLEN(keys[i]));
  3352. if (ret != 0)
  3353. return -3501;
  3354. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3355. (word32)test_hmac[i].inLen);
  3356. if (ret != 0)
  3357. return -3502;
  3358. ret = wc_HmacFinal(&hmac, hash);
  3359. if (ret != 0)
  3360. return -3503;
  3361. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  3362. return -3504 - i;
  3363. wc_HmacFree(&hmac);
  3364. }
  3365. #ifndef HAVE_FIPS
  3366. if (wc_HmacSizeByType(WC_SHA) != WC_SHA_DIGEST_SIZE)
  3367. return -3514;
  3368. #endif
  3369. return 0;
  3370. }
  3371. #endif
  3372. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  3373. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void)
  3374. {
  3375. Hmac hmac;
  3376. byte hash[WC_SHA224_DIGEST_SIZE];
  3377. const char* keys[]=
  3378. {
  3379. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3380. "\x0b\x0b\x0b",
  3381. "Jefe",
  3382. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3383. "\xAA\xAA\xAA",
  3384. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3385. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3386. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3387. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3388. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3389. };
  3390. testVector a, b, c, d;
  3391. testVector test_hmac[4];
  3392. int ret;
  3393. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3394. a.input = "Hi There";
  3395. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  3396. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  3397. a.inLen = XSTRLEN(a.input);
  3398. a.outLen = WC_SHA224_DIGEST_SIZE;
  3399. b.input = "what do ya want for nothing?";
  3400. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  3401. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  3402. b.inLen = XSTRLEN(b.input);
  3403. b.outLen = WC_SHA224_DIGEST_SIZE;
  3404. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3405. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3406. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3407. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3408. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  3409. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  3410. c.inLen = XSTRLEN(c.input);
  3411. c.outLen = WC_SHA224_DIGEST_SIZE;
  3412. d.input = "Big Key Input";
  3413. d.output = "\xe7\x4e\x2b\x8a\xa9\xf0\x37\x2f\xed\xae\x70\x0c\x49\x47\xf1"
  3414. "\x46\x54\xa7\x32\x6b\x55\x01\x87\xd2\xc8\x02\x0e\x3a";
  3415. d.inLen = XSTRLEN(d.input);
  3416. d.outLen = WC_SHA224_DIGEST_SIZE;
  3417. test_hmac[0] = a;
  3418. test_hmac[1] = b;
  3419. test_hmac[2] = c;
  3420. test_hmac[3] = d;
  3421. for (i = 0; i < times; ++i) {
  3422. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3423. if (i == 1)
  3424. continue; /* cavium can't handle short keys, fips not allowed */
  3425. #endif
  3426. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3427. return -3600;
  3428. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  3429. (word32)XSTRLEN(keys[i]));
  3430. if (ret != 0)
  3431. return -3601;
  3432. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3433. (word32)test_hmac[i].inLen);
  3434. if (ret != 0)
  3435. return -3602;
  3436. ret = wc_HmacFinal(&hmac, hash);
  3437. if (ret != 0)
  3438. return -3603;
  3439. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  3440. return -3604 - i;
  3441. wc_HmacFree(&hmac);
  3442. }
  3443. #ifndef HAVE_FIPS
  3444. if (wc_HmacSizeByType(WC_SHA224) != WC_SHA224_DIGEST_SIZE)
  3445. return -3614;
  3446. #endif
  3447. return 0;
  3448. }
  3449. #endif
  3450. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  3451. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void)
  3452. {
  3453. Hmac hmac;
  3454. byte hash[WC_SHA256_DIGEST_SIZE];
  3455. const char* keys[]=
  3456. {
  3457. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3458. "\x0b\x0b\x0b",
  3459. "Jefe",
  3460. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3461. "\xAA\xAA\xAA",
  3462. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3463. "\xAA\xAA\xAA",
  3464. };
  3465. testVector a, b, c, d;
  3466. testVector test_hmac[4];
  3467. int ret;
  3468. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3469. a.input = "Hi There";
  3470. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  3471. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  3472. "\xcf\xf7";
  3473. a.inLen = XSTRLEN(a.input);
  3474. a.outLen = WC_SHA256_DIGEST_SIZE;
  3475. b.input = "what do ya want for nothing?";
  3476. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  3477. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  3478. "\x38\x43";
  3479. b.inLen = XSTRLEN(b.input);
  3480. b.outLen = WC_SHA256_DIGEST_SIZE;
  3481. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3482. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3483. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3484. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3485. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  3486. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  3487. "\x65\xfe";
  3488. c.inLen = XSTRLEN(c.input);
  3489. c.outLen = WC_SHA256_DIGEST_SIZE;
  3490. d.input = 0;
  3491. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  3492. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  3493. "\x3e\x46";
  3494. d.inLen = 0;
  3495. d.outLen = WC_SHA256_DIGEST_SIZE;
  3496. test_hmac[0] = a;
  3497. test_hmac[1] = b;
  3498. test_hmac[2] = c;
  3499. test_hmac[3] = d;
  3500. for (i = 0; i < times; ++i) {
  3501. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3502. if (i == 1)
  3503. continue; /* cavium can't handle short keys, fips not allowed */
  3504. #endif
  3505. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  3506. if (i == 3)
  3507. continue; /* QuickAssist can't handle empty HMAC */
  3508. #endif
  3509. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3510. return -3700 - i;
  3511. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  3512. (word32)XSTRLEN(keys[i]));
  3513. if (ret != 0)
  3514. return -3710 - i;
  3515. if (test_hmac[i].input != NULL) {
  3516. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3517. (word32)test_hmac[i].inLen);
  3518. if (ret != 0)
  3519. return -3720 - i;
  3520. }
  3521. ret = wc_HmacFinal(&hmac, hash);
  3522. if (ret != 0)
  3523. return -3730 - i;
  3524. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  3525. return -3740 - i;
  3526. wc_HmacFree(&hmac);
  3527. }
  3528. #ifndef HAVE_FIPS
  3529. if (wc_HmacSizeByType(WC_SHA256) != WC_SHA256_DIGEST_SIZE)
  3530. return -3750;
  3531. if (wc_HmacSizeByType(20) != BAD_FUNC_ARG)
  3532. return -3751;
  3533. #endif
  3534. if (wolfSSL_GetHmacMaxSize() != WC_MAX_DIGEST_SIZE)
  3535. return -3752;
  3536. return 0;
  3537. }
  3538. #endif
  3539. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  3540. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void)
  3541. {
  3542. Hmac hmac;
  3543. byte hash[WC_SHA384_DIGEST_SIZE];
  3544. const char* keys[]=
  3545. {
  3546. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3547. "\x0b\x0b\x0b",
  3548. "Jefe",
  3549. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3550. "\xAA\xAA\xAA",
  3551. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3552. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3553. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3554. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3555. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3556. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3557. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3558. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3559. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3560. };
  3561. testVector a, b, c, d;
  3562. testVector test_hmac[4];
  3563. int ret;
  3564. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3565. a.input = "Hi There";
  3566. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  3567. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  3568. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  3569. "\xfa\x9c\xb6";
  3570. a.inLen = XSTRLEN(a.input);
  3571. a.outLen = WC_SHA384_DIGEST_SIZE;
  3572. b.input = "what do ya want for nothing?";
  3573. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  3574. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  3575. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  3576. "\xb2\x16\x49";
  3577. b.inLen = XSTRLEN(b.input);
  3578. b.outLen = WC_SHA384_DIGEST_SIZE;
  3579. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3580. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3581. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3582. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3583. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  3584. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  3585. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  3586. "\xa3\x4f\x27";
  3587. c.inLen = XSTRLEN(c.input);
  3588. c.outLen = WC_SHA384_DIGEST_SIZE;
  3589. d.input = "Big Key Input";
  3590. d.output = "\xd2\x3d\x29\x6e\xf5\x1e\x23\x23\x49\x18\xb3\xbf\x4c\x38\x7b"
  3591. "\x31\x21\x17\xbb\x09\x73\x27\xf8\x12\x9d\xe9\xc6\x5d\xf9\x54"
  3592. "\xd6\x38\x5a\x68\x53\x14\xee\xe0\xa6\x4f\x36\x7e\xb2\xf3\x1a"
  3593. "\x57\x41\x69";
  3594. d.inLen = XSTRLEN(d.input);
  3595. d.outLen = WC_SHA384_DIGEST_SIZE;
  3596. test_hmac[0] = a;
  3597. test_hmac[1] = b;
  3598. test_hmac[2] = c;
  3599. test_hmac[3] = d;
  3600. for (i = 0; i < times; ++i) {
  3601. #if defined(HAVE_FIPS)
  3602. if (i == 1)
  3603. continue; /* fips not allowed */
  3604. #endif
  3605. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3606. return -3800;
  3607. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  3608. (word32)XSTRLEN(keys[i]));
  3609. if (ret != 0)
  3610. return -3801;
  3611. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3612. (word32)test_hmac[i].inLen);
  3613. if (ret != 0)
  3614. return -3802;
  3615. ret = wc_HmacFinal(&hmac, hash);
  3616. if (ret != 0)
  3617. return -3803;
  3618. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  3619. return -3804 - i;
  3620. wc_HmacFree(&hmac);
  3621. }
  3622. #ifndef HAVE_FIPS
  3623. if (wc_HmacSizeByType(WC_SHA384) != WC_SHA384_DIGEST_SIZE)
  3624. return -3814;
  3625. #endif
  3626. return 0;
  3627. }
  3628. #endif
  3629. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  3630. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void)
  3631. {
  3632. Hmac hmac;
  3633. byte hash[WC_SHA512_DIGEST_SIZE];
  3634. const char* keys[]=
  3635. {
  3636. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3637. "\x0b\x0b\x0b",
  3638. "Jefe",
  3639. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3640. "\xAA\xAA\xAA",
  3641. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3642. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3643. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3644. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3645. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3646. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3647. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3648. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3649. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3650. };
  3651. testVector a, b, c, d;
  3652. testVector test_hmac[4];
  3653. int ret;
  3654. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3655. a.input = "Hi There";
  3656. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  3657. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  3658. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  3659. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  3660. "\x3a\x12\x68\x54";
  3661. a.inLen = XSTRLEN(a.input);
  3662. a.outLen = WC_SHA512_DIGEST_SIZE;
  3663. b.input = "what do ya want for nothing?";
  3664. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  3665. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  3666. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  3667. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  3668. "\x38\xbc\xe7\x37";
  3669. b.inLen = XSTRLEN(b.input);
  3670. b.outLen = WC_SHA512_DIGEST_SIZE;
  3671. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3672. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3673. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3674. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3675. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  3676. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  3677. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  3678. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  3679. "\xe1\x32\x92\xfb";
  3680. c.inLen = XSTRLEN(c.input);
  3681. c.outLen = WC_SHA512_DIGEST_SIZE;
  3682. d.input = "Big Key Input";
  3683. d.output = "\x3f\xa9\xc9\xe1\xbd\xbb\x04\x55\x1f\xef\xcc\x92\x33\x08\xeb"
  3684. "\xcf\xc1\x9a\x5b\x5b\xc0\x7c\x86\x84\xae\x8c\x40\xaf\xb1\x27"
  3685. "\x87\x38\x92\x04\xa8\xed\xd7\xd7\x07\xa9\x85\xa0\xc2\xcd\x30"
  3686. "\xc0\x56\x14\x49\xbc\x2f\x69\x15\x6a\x97\xd8\x79\x2f\xb3\x3b"
  3687. "\x1e\x18\xfe\xfa";
  3688. d.inLen = XSTRLEN(d.input);
  3689. d.outLen = WC_SHA512_DIGEST_SIZE;
  3690. test_hmac[0] = a;
  3691. test_hmac[1] = b;
  3692. test_hmac[2] = c;
  3693. test_hmac[3] = d;
  3694. for (i = 0; i < times; ++i) {
  3695. #if defined(HAVE_FIPS)
  3696. if (i == 1)
  3697. continue; /* fips not allowed */
  3698. #endif
  3699. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3700. return -3900;
  3701. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  3702. (word32)XSTRLEN(keys[i]));
  3703. if (ret != 0)
  3704. return -3901;
  3705. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3706. (word32)test_hmac[i].inLen);
  3707. if (ret != 0)
  3708. return -3902;
  3709. ret = wc_HmacFinal(&hmac, hash);
  3710. if (ret != 0)
  3711. return -3903;
  3712. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  3713. return -3904 - i;
  3714. wc_HmacFree(&hmac);
  3715. }
  3716. #ifndef HAVE_FIPS
  3717. if (wc_HmacSizeByType(WC_SHA512) != WC_SHA512_DIGEST_SIZE)
  3718. return -3914;
  3719. #endif
  3720. return 0;
  3721. }
  3722. #endif
  3723. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  3724. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  3725. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  3726. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void)
  3727. {
  3728. Hmac hmac;
  3729. byte hash[WC_SHA3_512_DIGEST_SIZE];
  3730. const char* key[4] =
  3731. {
  3732. "Jefe",
  3733. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3734. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  3735. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  3736. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  3737. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3738. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3739. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3740. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3741. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3742. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3743. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3744. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3745. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3746. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3747. };
  3748. const char* input[4] =
  3749. {
  3750. "what do ya want for nothing?",
  3751. "Hi There",
  3752. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3753. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3754. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3755. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3756. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  3757. "Big Key Input"
  3758. };
  3759. const int hashType[4] =
  3760. {
  3761. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  3762. };
  3763. const int hashSz[4] =
  3764. {
  3765. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  3766. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  3767. };
  3768. const char* output[16] =
  3769. {
  3770. /* key = jefe, input = what do ya want for nothing? */
  3771. /* HMAC-SHA3-224 */
  3772. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  3773. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  3774. /* HMAC-SHA3-256 */
  3775. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  3776. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  3777. /* HMAC-SHA3-384 */
  3778. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  3779. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  3780. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  3781. /* HMAC-SHA3-512 */
  3782. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  3783. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  3784. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  3785. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  3786. /* key = 0b..., input = Hi There */
  3787. /* HMAC-SHA3-224 */
  3788. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  3789. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  3790. /* HMAC-SHA3-256 */
  3791. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  3792. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  3793. /* HMAC-SHA3-384 */
  3794. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  3795. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  3796. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  3797. /* HMAC-SHA3-512 */
  3798. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  3799. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  3800. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  3801. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  3802. /* key = aa..., output = dd... */
  3803. /* HMAC-SHA3-224 */
  3804. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  3805. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  3806. /* HMAC-SHA3-256 */
  3807. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  3808. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  3809. /* HMAC-SHA3-384 */
  3810. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  3811. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  3812. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  3813. /* HMAC-SHA3-512 */
  3814. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  3815. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  3816. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  3817. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  3818. /* key = big key, input = Big Key Input */
  3819. /* HMAC-SHA3-224 */
  3820. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  3821. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  3822. /* HMAC-SHA3-256 */
  3823. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  3824. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  3825. /* HMAC-SHA3-384 */
  3826. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  3827. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  3828. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  3829. /* HMAC-SHA3-512 */
  3830. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  3831. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  3832. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  3833. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  3834. };
  3835. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  3836. j, jMax = sizeof(hashType) / sizeof(hashType[0]),
  3837. ret;
  3838. #ifdef HAVE_FIPS
  3839. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  3840. * short. Skip it in FIPS builds. */
  3841. i = 1;
  3842. #endif
  3843. for (; i < iMax; i++) {
  3844. for (j = 0; j < jMax; j++) {
  3845. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3846. return -4000;
  3847. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  3848. (word32)XSTRLEN(key[i]));
  3849. if (ret != 0)
  3850. return -4001;
  3851. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  3852. (word32)XSTRLEN(input[i]));
  3853. if (ret != 0)
  3854. return -4002;
  3855. ret = wc_HmacFinal(&hmac, hash);
  3856. if (ret != 0)
  3857. return -4003;
  3858. if (XMEMCMP(hash, output[(i*jMax) + j], hashSz[j]) != 0)
  3859. return -4004;
  3860. wc_HmacFree(&hmac);
  3861. if (i > 0)
  3862. continue;
  3863. #ifndef HAVE_FIPS
  3864. ret = wc_HmacSizeByType(hashType[j]);
  3865. if (ret != hashSz[j])
  3866. return -4005;
  3867. #endif
  3868. }
  3869. }
  3870. return 0;
  3871. }
  3872. #endif
  3873. #ifdef WC_RC2
  3874. typedef struct rc2TestVector {
  3875. const char* input;
  3876. const char* output;
  3877. const char* key; /* Key, variable up to 128 bytes */
  3878. const char* iv; /* IV, 8-bytes */
  3879. int inLen;
  3880. int outLen;
  3881. int keyLen;
  3882. int effectiveKeyBits; /* Up to 1024 bits supported */
  3883. } rc2TestVector;
  3884. static int rc2_ecb_test(void)
  3885. {
  3886. int ret = 0;
  3887. byte cipher[RC2_BLOCK_SIZE];
  3888. byte plain[RC2_BLOCK_SIZE];
  3889. rc2TestVector a, b, c, d, e, f, g, h;
  3890. rc2TestVector test_rc2[8];
  3891. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  3892. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3893. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  3894. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3895. a.inLen = RC2_BLOCK_SIZE;
  3896. a.outLen = RC2_BLOCK_SIZE;
  3897. a.keyLen = 8;
  3898. a.effectiveKeyBits = 63;
  3899. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  3900. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  3901. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  3902. b.inLen = RC2_BLOCK_SIZE;
  3903. b.outLen = RC2_BLOCK_SIZE;
  3904. b.keyLen = 8;
  3905. b.effectiveKeyBits = 64;
  3906. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  3907. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  3908. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  3909. c.inLen = RC2_BLOCK_SIZE;
  3910. c.outLen = RC2_BLOCK_SIZE;
  3911. c.keyLen = 8;
  3912. c.effectiveKeyBits = 64;
  3913. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3914. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  3915. d.key = "\x88";
  3916. d.inLen = RC2_BLOCK_SIZE;
  3917. d.outLen = RC2_BLOCK_SIZE;
  3918. d.keyLen = 1;
  3919. d.effectiveKeyBits = 64;
  3920. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3921. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  3922. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  3923. e.inLen = RC2_BLOCK_SIZE;
  3924. e.outLen = RC2_BLOCK_SIZE;
  3925. e.keyLen = 7;
  3926. e.effectiveKeyBits = 64;
  3927. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3928. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  3929. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  3930. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  3931. f.inLen = RC2_BLOCK_SIZE;
  3932. f.outLen = RC2_BLOCK_SIZE;
  3933. f.keyLen = 16;
  3934. f.effectiveKeyBits = 64;
  3935. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3936. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  3937. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  3938. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  3939. g.inLen = RC2_BLOCK_SIZE;
  3940. g.outLen = RC2_BLOCK_SIZE;
  3941. g.keyLen = 16;
  3942. g.effectiveKeyBits = 128;
  3943. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3944. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  3945. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  3946. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  3947. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  3948. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  3949. "\x1e";
  3950. h.inLen = RC2_BLOCK_SIZE;
  3951. h.outLen = RC2_BLOCK_SIZE;
  3952. h.keyLen = 33;
  3953. h.effectiveKeyBits = 129;
  3954. test_rc2[0] = a;
  3955. test_rc2[1] = b;
  3956. test_rc2[2] = c;
  3957. test_rc2[3] = d;
  3958. test_rc2[4] = e;
  3959. test_rc2[5] = f;
  3960. test_rc2[6] = g;
  3961. test_rc2[7] = h;
  3962. for (i = 0; i < times; ++i) {
  3963. Rc2 enc;
  3964. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  3965. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  3966. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  3967. NULL, test_rc2[i].effectiveKeyBits);
  3968. if (ret != 0) {
  3969. return -4100;
  3970. }
  3971. /* ECB encrypt */
  3972. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  3973. (word32)test_rc2[i].outLen);
  3974. if (ret != 0) {
  3975. return -4101;
  3976. }
  3977. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  3978. return -4102;
  3979. }
  3980. /* ECB decrypt */
  3981. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  3982. if (ret != 0) {
  3983. return -4103;
  3984. }
  3985. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  3986. return -4104;
  3987. }
  3988. }
  3989. return 0;
  3990. }
  3991. static int rc2_cbc_test(void)
  3992. {
  3993. int ret = 0;
  3994. byte cipher[128];
  3995. byte plain[128];
  3996. rc2TestVector a, b, c, d, e, f, g, h, i;
  3997. rc2TestVector test_rc2[9];
  3998. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  3999. /* key length = 7, effective key bits = 63 */
  4000. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4001. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4002. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  4003. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  4004. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4005. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4006. a.inLen = RC2_BLOCK_SIZE*2;
  4007. a.outLen = RC2_BLOCK_SIZE*2;
  4008. a.keyLen = 8;
  4009. a.effectiveKeyBits = 63;
  4010. /* key length = 8, effective key bits = 64, all 0xFF */
  4011. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  4012. "\xff\xff\xff\xff\xff\xff\xff\xff";
  4013. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  4014. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  4015. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4016. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4017. b.inLen = RC2_BLOCK_SIZE*2;
  4018. b.outLen = RC2_BLOCK_SIZE*2;
  4019. b.keyLen = 8;
  4020. b.effectiveKeyBits = 64;
  4021. /* key length = 8, effective key bits = 64 */
  4022. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  4023. "\x10\x00\x00\x00\x00\x00\x00\x01";
  4024. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  4025. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  4026. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  4027. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4028. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4029. c.inLen = RC2_BLOCK_SIZE*2;
  4030. c.outLen = RC2_BLOCK_SIZE*2;
  4031. c.keyLen = 8;
  4032. c.effectiveKeyBits = 64;
  4033. /* key length = 1, effective key bits = 64 */
  4034. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4035. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4036. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  4037. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  4038. d.key = "\x88";
  4039. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4040. d.inLen = RC2_BLOCK_SIZE*2;
  4041. d.outLen = RC2_BLOCK_SIZE*2;
  4042. d.keyLen = 1;
  4043. d.effectiveKeyBits = 64;
  4044. /* key length = 7, effective key bits = 64 */
  4045. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4046. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4047. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  4048. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  4049. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4050. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4051. e.inLen = RC2_BLOCK_SIZE*2;
  4052. e.outLen = RC2_BLOCK_SIZE*2;
  4053. e.keyLen = 7;
  4054. e.effectiveKeyBits = 64;
  4055. /* key length = 16, effective key bits = 64 */
  4056. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4057. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4058. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  4059. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  4060. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4061. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4062. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4063. f.inLen = RC2_BLOCK_SIZE*2;
  4064. f.outLen = RC2_BLOCK_SIZE*2;
  4065. f.keyLen = 16;
  4066. f.effectiveKeyBits = 64;
  4067. /* key length = 16, effective bits = 128 */
  4068. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4069. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4070. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  4071. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  4072. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4073. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4074. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4075. g.inLen = RC2_BLOCK_SIZE*2;
  4076. g.outLen = RC2_BLOCK_SIZE*2;
  4077. g.keyLen = 16;
  4078. g.effectiveKeyBits = 128;
  4079. /* key length = 33, effective bits = 129 */
  4080. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4081. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4082. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  4083. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  4084. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4085. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  4086. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  4087. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  4088. "\x1e";
  4089. h.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4090. h.inLen = RC2_BLOCK_SIZE*2;
  4091. h.outLen = RC2_BLOCK_SIZE*2;
  4092. h.keyLen = 33;
  4093. h.effectiveKeyBits = 129;
  4094. /* key length = 10, effective bits = 40 */
  4095. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  4096. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  4097. "\x11\x22\x33\x44\x55\x66\x77\x88"
  4098. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  4099. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  4100. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  4101. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  4102. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  4103. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  4104. "\x3E\x96";
  4105. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  4106. i.inLen = RC2_BLOCK_SIZE*4;
  4107. i.outLen = RC2_BLOCK_SIZE*4;
  4108. i.keyLen = 10;
  4109. i.effectiveKeyBits = 40;
  4110. test_rc2[0] = a;
  4111. test_rc2[1] = b;
  4112. test_rc2[2] = c;
  4113. test_rc2[3] = d;
  4114. test_rc2[4] = e;
  4115. test_rc2[5] = f;
  4116. test_rc2[6] = g;
  4117. test_rc2[7] = h;
  4118. test_rc2[8] = i;
  4119. for (j = 0; j < times; ++j) {
  4120. Rc2 rc2;
  4121. XMEMSET(cipher, 0, sizeof(cipher));
  4122. XMEMSET(plain, 0, sizeof(plain));
  4123. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  4124. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  4125. if (ret != 0) {
  4126. return -4200;
  4127. }
  4128. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  4129. test_rc2[j].inLen);
  4130. if (ret != 0) {
  4131. return -4201;
  4132. }
  4133. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  4134. return -4202;
  4135. }
  4136. /* reset IV for decrypt, since overriden by encrypt operation */
  4137. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  4138. if (ret != 0) {
  4139. return -4203;
  4140. }
  4141. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  4142. if (ret != 0) {
  4143. return -4204;
  4144. }
  4145. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  4146. return -4205;
  4147. }
  4148. }
  4149. return 0;
  4150. }
  4151. WOLFSSL_TEST_SUBROUTINE int rc2_test(void)
  4152. {
  4153. int ret = 0;
  4154. ret = rc2_ecb_test();
  4155. if (ret != 0) {
  4156. return ret;
  4157. }
  4158. return rc2_cbc_test();
  4159. }
  4160. #endif
  4161. #ifndef NO_RC4
  4162. WOLFSSL_TEST_SUBROUTINE int arc4_test(void)
  4163. {
  4164. byte cipher[16];
  4165. byte plain[16];
  4166. const char* keys[] =
  4167. {
  4168. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4169. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4170. "\x00\x00\x00\x00\x00\x00\x00\x00",
  4171. "\xef\x01\x23\x45"
  4172. };
  4173. testVector a, b, c, d;
  4174. testVector test_arc4[4];
  4175. int times = sizeof(test_arc4) / sizeof(testVector), i;
  4176. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  4177. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  4178. a.inLen = 8;
  4179. a.outLen = 8;
  4180. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4181. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  4182. b.inLen = 8;
  4183. b.outLen = 8;
  4184. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4185. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  4186. c.inLen = 8;
  4187. c.outLen = 8;
  4188. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  4189. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  4190. d.inLen = 10;
  4191. d.outLen = 10;
  4192. test_arc4[0] = a;
  4193. test_arc4[1] = b;
  4194. test_arc4[2] = c;
  4195. test_arc4[3] = d;
  4196. for (i = 0; i < times; ++i) {
  4197. Arc4 enc;
  4198. Arc4 dec;
  4199. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  4200. if (i == 3)
  4201. keylen = 4;
  4202. if (wc_Arc4Init(&enc, HEAP_HINT, devId) != 0)
  4203. return -4400;
  4204. if (wc_Arc4Init(&dec, HEAP_HINT, devId) != 0)
  4205. return -4401;
  4206. wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  4207. wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  4208. wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  4209. (word32)test_arc4[i].outLen);
  4210. wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  4211. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  4212. return -4402 - i;
  4213. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  4214. return -4412 - i;
  4215. wc_Arc4Free(&enc);
  4216. wc_Arc4Free(&dec);
  4217. }
  4218. return 0;
  4219. }
  4220. #endif
  4221. WOLFSSL_TEST_SUBROUTINE int hc128_test(void)
  4222. {
  4223. #ifdef HAVE_HC128
  4224. byte cipher[16];
  4225. byte plain[16];
  4226. const char* keys[] =
  4227. {
  4228. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4229. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4230. "\x00\x53\xA6\xF9\x4C\x9F\xF2\x45\x98\xEB\x3E\x91\xE4\x37\x8A\xDD",
  4231. "\x0F\x62\xB5\x08\x5B\xAE\x01\x54\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC"
  4232. };
  4233. const char* ivs[] =
  4234. {
  4235. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4236. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4237. "\x0D\x74\xDB\x42\xA9\x10\x77\xDE\x45\xAC\x13\x7A\xE1\x48\xAF\x16",
  4238. "\x28\x8F\xF6\x5D\xC4\x2B\x92\xF9\x60\xC7\x2E\x95\xFC\x63\xCA\x31"
  4239. };
  4240. testVector a, b, c, d;
  4241. testVector test_hc128[4];
  4242. int times = sizeof(test_hc128) / sizeof(testVector), i;
  4243. int ret = 0;
  4244. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
  4245. HC128 enc[1], dec[1];
  4246. #else
  4247. HC128 *enc = (HC128 *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4248. HC128 *dec = (HC128 *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4249. if ((! enc) || (! dec)) {
  4250. ERROR_OUT(-4500, out);
  4251. }
  4252. #endif
  4253. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4254. a.output = "\x37\x86\x02\xB9\x8F\x32\xA7\x48";
  4255. a.inLen = 8;
  4256. a.outLen = 8;
  4257. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4258. b.output = "\x33\x7F\x86\x11\xC6\xED\x61\x5F";
  4259. b.inLen = 8;
  4260. b.outLen = 8;
  4261. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4262. c.output = "\x2E\x1E\xD1\x2A\x85\x51\xC0\x5A";
  4263. c.inLen = 8;
  4264. c.outLen = 8;
  4265. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  4266. d.output = "\x1C\xD8\xAE\xDD\xFE\x52\xE2\x17\xE8\x35\xD0\xB7\xE8\x4E\x29";
  4267. d.inLen = 15;
  4268. d.outLen = 15;
  4269. test_hc128[0] = a;
  4270. test_hc128[1] = b;
  4271. test_hc128[2] = c;
  4272. test_hc128[3] = d;
  4273. for (i = 0; i < times; ++i) {
  4274. /* align keys/ivs in plain/cipher buffers */
  4275. XMEMCPY(plain, keys[i], 16);
  4276. XMEMCPY(cipher, ivs[i], 16);
  4277. wc_Hc128_SetKey(enc, plain, cipher);
  4278. wc_Hc128_SetKey(dec, plain, cipher);
  4279. /* align input */
  4280. XMEMCPY(plain, test_hc128[i].input, test_hc128[i].outLen);
  4281. if (wc_Hc128_Process(enc, cipher, plain,
  4282. (word32)test_hc128[i].outLen) != 0) {
  4283. ret = -4501;
  4284. goto out;
  4285. }
  4286. if (wc_Hc128_Process(dec, plain, cipher,
  4287. (word32)test_hc128[i].outLen) != 0) {
  4288. ret = -4502;
  4289. goto out;
  4290. }
  4291. if (XMEMCMP(plain, test_hc128[i].input, test_hc128[i].outLen)) {
  4292. ret = -4503 - i;
  4293. goto out;
  4294. }
  4295. if (XMEMCMP(cipher, test_hc128[i].output, test_hc128[i].outLen)) {
  4296. ret = -4513 - i;
  4297. goto out;
  4298. }
  4299. }
  4300. out:
  4301. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4302. if (enc)
  4303. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4304. if (dec)
  4305. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4306. #endif
  4307. return ret;
  4308. #else
  4309. return 0;
  4310. #endif /* HAVE_HC128 */
  4311. }
  4312. #ifndef NO_RABBIT
  4313. WOLFSSL_TEST_SUBROUTINE int rabbit_test(void)
  4314. {
  4315. byte cipher[16];
  4316. byte plain[16];
  4317. const char* keys[] =
  4318. {
  4319. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4320. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4321. "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B\xFE\x36\x3D\x2E\x29\x13\x28\x91"
  4322. };
  4323. const char* ivs[] =
  4324. {
  4325. "\x00\x00\x00\x00\x00\x00\x00\x00",
  4326. "\x59\x7E\x26\xC1\x75\xF5\x73\xC3",
  4327. 0
  4328. };
  4329. testVector a, b, c;
  4330. testVector test_rabbit[3];
  4331. int times = sizeof(test_rabbit) / sizeof(testVector), i;
  4332. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4333. a.output = "\xED\xB7\x05\x67\x37\x5D\xCD\x7C";
  4334. a.inLen = 8;
  4335. a.outLen = 8;
  4336. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4337. b.output = "\x6D\x7D\x01\x22\x92\xCC\xDC\xE0";
  4338. b.inLen = 8;
  4339. b.outLen = 8;
  4340. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4341. c.output = "\x04\xCE\xCA\x7A\x1A\x86\x6E\x77";
  4342. c.inLen = 8;
  4343. c.outLen = 8;
  4344. test_rabbit[0] = a;
  4345. test_rabbit[1] = b;
  4346. test_rabbit[2] = c;
  4347. for (i = 0; i < times; ++i) {
  4348. Rabbit enc;
  4349. Rabbit dec;
  4350. byte* iv;
  4351. /* align keys/ivs in plain/cipher buffers */
  4352. XMEMCPY(plain, keys[i], 16);
  4353. if (ivs[i]) {
  4354. XMEMCPY(cipher, ivs[i], 8);
  4355. iv = cipher;
  4356. } else
  4357. iv = NULL;
  4358. wc_RabbitSetKey(&enc, plain, iv);
  4359. wc_RabbitSetKey(&dec, plain, iv);
  4360. /* align input */
  4361. XMEMCPY(plain, test_rabbit[i].input, test_rabbit[i].outLen);
  4362. wc_RabbitProcess(&enc, cipher, plain, (word32)test_rabbit[i].outLen);
  4363. wc_RabbitProcess(&dec, plain, cipher, (word32)test_rabbit[i].outLen);
  4364. if (XMEMCMP(plain, test_rabbit[i].input, test_rabbit[i].outLen))
  4365. return -4600 - i;
  4366. if (XMEMCMP(cipher, test_rabbit[i].output, test_rabbit[i].outLen))
  4367. return -4610 - i;
  4368. }
  4369. return 0;
  4370. }
  4371. #endif /* NO_RABBIT */
  4372. #ifdef HAVE_CHACHA
  4373. WOLFSSL_TEST_SUBROUTINE int chacha_test(void)
  4374. {
  4375. ChaCha enc;
  4376. ChaCha dec;
  4377. byte cipher[128];
  4378. byte plain[128];
  4379. byte sliver[64];
  4380. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  4381. word32 keySz = 32;
  4382. int ret = 0;
  4383. int i;
  4384. int times = 4;
  4385. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  4386. {
  4387. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4388. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4389. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4390. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4391. };
  4392. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  4393. {
  4394. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4395. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4396. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4397. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  4398. };
  4399. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  4400. {
  4401. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4402. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4403. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4404. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4405. };
  4406. /* 128 bit key */
  4407. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  4408. {
  4409. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4410. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4411. };
  4412. const byte* keys[] = {key1, key2, key3, key4};
  4413. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  4414. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  4415. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  4416. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  4417. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  4418. #ifndef BENCH_EMBEDDED
  4419. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  4420. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  4421. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  4422. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  4423. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  4424. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  4425. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  4426. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  4427. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  4428. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  4429. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  4430. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  4431. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  4432. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  4433. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  4434. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  4435. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  4436. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  4437. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  4438. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  4439. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  4440. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  4441. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  4442. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  4443. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  4444. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  4445. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  4446. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  4447. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  4448. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  4449. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  4450. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  4451. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  4452. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  4453. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  4454. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  4455. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  4456. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  4457. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  4458. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  4459. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  4460. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  4461. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  4462. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  4463. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  4464. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  4465. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  4466. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  4467. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  4468. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  4469. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  4470. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  4471. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  4472. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  4473. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  4474. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  4475. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  4476. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  4477. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  4478. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  4479. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  4480. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  4481. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  4482. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  4483. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  4484. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  4485. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  4486. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  4487. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  4488. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  4489. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  4490. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  4491. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  4492. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  4493. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  4494. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  4495. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  4496. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  4497. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  4498. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  4499. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  4500. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  4501. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  4502. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  4503. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  4504. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  4505. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  4506. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  4507. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  4508. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  4509. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  4510. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  4511. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  4512. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  4513. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  4514. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  4515. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  4516. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  4517. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  4518. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  4519. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  4520. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  4521. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  4522. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  4523. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  4524. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  4525. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  4526. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  4527. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  4528. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  4529. };
  4530. #define CHACHA_BIG_TEST_SIZE 1305
  4531. #ifndef WOLFSSL_SMALL_STACK
  4532. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  4533. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  4534. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  4535. #else
  4536. byte* cipher_big;
  4537. byte* plain_big;
  4538. byte* input_big;
  4539. #endif /* WOLFSSL_SMALL_STACK */
  4540. int block_size;
  4541. #endif /* BENCH_EMBEDDED */
  4542. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  4543. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  4544. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  4545. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  4546. byte* test_chacha[4];
  4547. test_chacha[0] = a;
  4548. test_chacha[1] = b;
  4549. test_chacha[2] = c;
  4550. test_chacha[3] = d;
  4551. #ifndef BENCH_EMBEDDED
  4552. #ifdef WOLFSSL_SMALL_STACK
  4553. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  4554. DYNAMIC_TYPE_TMP_BUFFER);
  4555. if (cipher_big == NULL) {
  4556. return MEMORY_E;
  4557. }
  4558. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  4559. DYNAMIC_TYPE_TMP_BUFFER);
  4560. if (plain_big == NULL) {
  4561. return MEMORY_E;
  4562. }
  4563. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  4564. DYNAMIC_TYPE_TMP_BUFFER);
  4565. if (input_big == NULL) {
  4566. return MEMORY_E;
  4567. }
  4568. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  4569. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  4570. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  4571. #endif /* WOLFSSL_SMALL_STACK */
  4572. #endif /* BENCH_EMBEDDED */
  4573. for (i = 0; i < times; ++i) {
  4574. if (i < 3) {
  4575. keySz = 32;
  4576. }
  4577. else {
  4578. keySz = 16;
  4579. }
  4580. XMEMCPY(plain, keys[i], keySz);
  4581. XMEMSET(cipher, 0, 32);
  4582. XMEMCPY(cipher + 4, ivs[i], 8);
  4583. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  4584. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  4585. if (ret != 0)
  4586. return ret;
  4587. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  4588. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  4589. if (ret != 0)
  4590. return ret;
  4591. XMEMCPY(plain, input, 8);
  4592. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  4593. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  4594. if (ret != 0)
  4595. return ret;
  4596. if (XMEMCMP(test_chacha[i], cipher, 8))
  4597. return -4700 - i;
  4598. if (XMEMCMP(plain, input, 8))
  4599. return -4710 - i;
  4600. }
  4601. /* test of starting at a different counter
  4602. encrypts all of the information and decrypts starting at 2nd chunk */
  4603. XMEMSET(plain, 0, sizeof(plain));
  4604. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  4605. XMEMSET(cipher, 0, sizeof(cipher));
  4606. XMEMCPY(cipher + 4, ivs[0], 8);
  4607. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  4608. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  4609. if (ret != 0)
  4610. return ret;
  4611. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  4612. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  4613. if (ret != 0)
  4614. return ret;
  4615. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  4616. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  4617. if (ret != 0)
  4618. return ret;
  4619. if (XMEMCMP(plain + 64, sliver, 64))
  4620. return -4720;
  4621. #ifndef BENCH_EMBEDDED
  4622. /* test of encrypting more data */
  4623. keySz = 32;
  4624. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  4625. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  4626. if (ret != 0)
  4627. return ret;
  4628. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  4629. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  4630. if (ret != 0)
  4631. return ret;
  4632. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  4633. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  4634. CHACHA_BIG_TEST_SIZE);
  4635. if (ret != 0)
  4636. return ret;
  4637. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  4638. return -4721;
  4639. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  4640. return -4722;
  4641. for (i = 0; i < 18; ++i) {
  4642. /* this will test all paths */
  4643. // block sizes: 1 2 3 4 7 8 15 16 31 32 63 64 127 128 255 256 511 512
  4644. block_size = (2 << (i%9)) - (i<9?1:0);
  4645. keySz = 32;
  4646. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  4647. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  4648. if (ret != 0)
  4649. return ret;
  4650. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  4651. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  4652. if (ret != 0)
  4653. return ret;
  4654. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, block_size);
  4655. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big, block_size);
  4656. if (ret != 0)
  4657. return ret;
  4658. if (XMEMCMP(plain_big, input_big, block_size))
  4659. return -4723-i;
  4660. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  4661. return -4724-i;
  4662. }
  4663. /* Streaming test */
  4664. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  4665. int j, rem;
  4666. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  4667. if (ret != 0)
  4668. return -4725;
  4669. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  4670. if (ret != 0)
  4671. return -4726;
  4672. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  4673. if (ret != 0)
  4674. return -4727;
  4675. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  4676. if (ret != 0)
  4677. return -4728;
  4678. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  4679. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, i);
  4680. if (ret != 0)
  4681. return -4729;
  4682. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, i);
  4683. if (ret != 0)
  4684. return -4730;
  4685. }
  4686. rem = CHACHA_BIG_TEST_SIZE - j;
  4687. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, rem);
  4688. if (ret != 0)
  4689. return -4731;
  4690. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, rem);
  4691. if (ret != 0)
  4692. return -4732;
  4693. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  4694. return -4733;
  4695. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  4696. return -4734;
  4697. }
  4698. #ifdef WOLFSSL_SMALL_STACK
  4699. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4700. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4701. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4702. #endif /* WOLFSSL_SMALL_STACK */
  4703. #endif /* BENCH_EMBEDDED */
  4704. return 0;
  4705. }
  4706. #endif /* HAVE_CHACHA */
  4707. #ifdef HAVE_POLY1305
  4708. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void)
  4709. {
  4710. int ret = 0;
  4711. int i;
  4712. byte tag[16];
  4713. Poly1305 enc;
  4714. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  4715. {
  4716. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  4717. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  4718. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  4719. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  4720. 0x75,0x70
  4721. };
  4722. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  4723. {
  4724. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  4725. 0x6c,0x64,0x21
  4726. };
  4727. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  4728. {
  4729. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4730. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4731. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4732. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4733. };
  4734. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  4735. {
  4736. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  4737. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  4738. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  4739. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  4740. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  4741. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  4742. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  4743. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  4744. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  4745. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  4746. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4747. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  4748. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  4749. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  4750. 0x61,0x16
  4751. };
  4752. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  4753. {
  4754. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  4755. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  4756. };
  4757. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  4758. {
  4759. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  4760. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  4761. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  4762. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  4763. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  4764. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  4765. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4766. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  4767. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  4768. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  4769. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4770. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  4771. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  4772. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  4773. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  4774. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4775. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  4776. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  4777. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  4778. 0x61,0x16
  4779. };
  4780. byte additional[] =
  4781. {
  4782. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  4783. 0xc4,0xc5,0xc6,0xc7
  4784. };
  4785. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  4786. {
  4787. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  4788. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  4789. };
  4790. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  4791. {
  4792. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  4793. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  4794. };
  4795. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  4796. {
  4797. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  4798. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  4799. };
  4800. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  4801. {
  4802. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  4803. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  4804. };
  4805. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  4806. {
  4807. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  4808. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  4809. };
  4810. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  4811. {
  4812. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4813. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4814. };
  4815. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  4816. {
  4817. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  4818. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  4819. };
  4820. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  4821. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  4822. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  4823. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  4824. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  4825. };
  4826. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  4827. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  4828. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  4829. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  4830. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  4831. };
  4832. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  4833. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  4834. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  4835. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  4836. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  4837. };
  4838. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  4839. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4840. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4841. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4842. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4843. };
  4844. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  4845. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  4846. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  4847. const byte* keys[] = {key, key, key2, key2, key5, key};
  4848. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  4849. correct6};
  4850. for (i = 0; i < 6; i++) {
  4851. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  4852. if (ret != 0)
  4853. return -4800 - i;
  4854. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  4855. if (ret != 0)
  4856. return -4810 - i;
  4857. ret = wc_Poly1305Final(&enc, tag);
  4858. if (ret != 0)
  4859. return -4820 - i;
  4860. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  4861. return -4830 - i;
  4862. }
  4863. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  4864. XMEMSET(tag, 0, sizeof(tag));
  4865. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  4866. if (ret != 0)
  4867. return -4840;
  4868. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  4869. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  4870. if (ret != 0)
  4871. return -4841;
  4872. if (XMEMCMP(tag, correct4, sizeof(tag)))
  4873. return -4842;
  4874. /* Check fail of TLS MAC function if altering additional data */
  4875. XMEMSET(tag, 0, sizeof(tag));
  4876. additional[0]++;
  4877. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  4878. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  4879. if (ret != 0)
  4880. return -4843;
  4881. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  4882. return -4844;
  4883. return 0;
  4884. }
  4885. #endif /* HAVE_POLY1305 */
  4886. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4887. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void)
  4888. {
  4889. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  4890. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  4891. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  4892. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  4893. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  4894. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  4895. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  4896. };
  4897. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  4898. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  4899. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  4900. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  4901. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  4902. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  4903. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  4904. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  4905. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  4906. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  4907. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  4908. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  4909. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  4910. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  4911. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  4912. 0x74, 0x2e
  4913. };
  4914. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  4915. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  4916. 0x44, 0x45, 0x46, 0x47
  4917. };
  4918. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  4919. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  4920. 0xc4, 0xc5, 0xc6, 0xc7
  4921. };
  4922. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  4923. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  4924. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  4925. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  4926. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  4927. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  4928. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  4929. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  4930. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  4931. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  4932. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  4933. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  4934. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  4935. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  4936. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  4937. 0x61, 0x16
  4938. };
  4939. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  4940. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  4941. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  4942. };
  4943. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  4944. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  4945. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  4946. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  4947. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  4948. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  4949. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  4950. };
  4951. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  4952. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  4953. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  4954. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  4955. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  4956. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  4957. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  4958. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  4959. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  4960. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  4961. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  4962. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  4963. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  4964. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  4965. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  4966. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  4967. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  4968. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  4969. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  4970. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  4971. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  4972. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  4973. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  4974. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  4975. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  4976. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  4977. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  4978. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  4979. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  4980. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  4981. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  4982. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  4983. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  4984. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  4985. 0x9d
  4986. };
  4987. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  4988. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  4989. 0x05, 0x06, 0x07, 0x08
  4990. };
  4991. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  4992. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  4993. 0x00, 0x00, 0x4e, 0x91
  4994. };
  4995. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  4996. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  4997. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  4998. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  4999. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  5000. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  5001. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  5002. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  5003. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  5004. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  5005. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  5006. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  5007. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  5008. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  5009. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  5010. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  5011. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  5012. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  5013. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  5014. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  5015. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  5016. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  5017. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  5018. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  5019. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  5020. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  5021. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  5022. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  5023. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  5024. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  5025. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  5026. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  5027. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  5028. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  5029. 0x9b
  5030. };
  5031. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  5032. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  5033. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  5034. };
  5035. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  5036. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  5037. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  5038. int err;
  5039. ChaChaPoly_Aead aead;
  5040. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  5041. #define TEST_SMALL_CHACHA_CHUNKS 32
  5042. #else
  5043. #define TEST_SMALL_CHACHA_CHUNKS 64
  5044. #endif
  5045. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5046. word32 testLen;
  5047. #endif
  5048. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5049. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5050. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5051. /* Parameter Validation testing */
  5052. /* Encrypt */
  5053. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  5054. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5055. if (err != BAD_FUNC_ARG)
  5056. return -4900;
  5057. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  5058. plaintext1, sizeof(plaintext1), generatedCiphertext,
  5059. generatedAuthTag);
  5060. if (err != BAD_FUNC_ARG)
  5061. return -4901;
  5062. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  5063. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5064. if (err != BAD_FUNC_ARG)
  5065. return -4902;
  5066. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5067. sizeof(plaintext1), NULL, generatedAuthTag);
  5068. if (err != BAD_FUNC_ARG)
  5069. return -4903;
  5070. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5071. sizeof(plaintext1), generatedCiphertext, NULL);
  5072. if (err != BAD_FUNC_ARG)
  5073. return -4904;
  5074. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5075. 0, generatedCiphertext, generatedAuthTag);
  5076. if (err != BAD_FUNC_ARG)
  5077. return -4905;
  5078. /* Decrypt */
  5079. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  5080. sizeof(cipher2), authTag2, generatedPlaintext);
  5081. if (err != BAD_FUNC_ARG)
  5082. return -4906;
  5083. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  5084. sizeof(cipher2), authTag2, generatedPlaintext);
  5085. if (err != BAD_FUNC_ARG)
  5086. return -4907;
  5087. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  5088. sizeof(cipher2), authTag2, generatedPlaintext);
  5089. if (err != BAD_FUNC_ARG)
  5090. return -4908;
  5091. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5092. sizeof(cipher2), NULL, generatedPlaintext);
  5093. if (err != BAD_FUNC_ARG)
  5094. return -4909;
  5095. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5096. sizeof(cipher2), authTag2, NULL);
  5097. if (err != BAD_FUNC_ARG)
  5098. return -4910;
  5099. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5100. 0, authTag2, generatedPlaintext);
  5101. if (err != BAD_FUNC_ARG)
  5102. return -4911;
  5103. /* Test #1 */
  5104. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  5105. aad1, sizeof(aad1),
  5106. plaintext1, sizeof(plaintext1),
  5107. generatedCiphertext, generatedAuthTag);
  5108. if (err) {
  5109. return err;
  5110. }
  5111. /* -- Check the ciphertext and authtag */
  5112. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5113. return -4912;
  5114. }
  5115. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  5116. return -4913;
  5117. }
  5118. /* -- Verify decryption works */
  5119. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  5120. aad1, sizeof(aad1),
  5121. cipher1, sizeof(cipher1),
  5122. authTag1, generatedPlaintext);
  5123. if (err) {
  5124. return err;
  5125. }
  5126. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5127. return -4914;
  5128. }
  5129. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5130. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5131. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5132. /* Test #2 */
  5133. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  5134. aad2, sizeof(aad2),
  5135. plaintext2, sizeof(plaintext2),
  5136. generatedCiphertext, generatedAuthTag);
  5137. if (err) {
  5138. return err;
  5139. }
  5140. /* -- Check the ciphertext and authtag */
  5141. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5142. return -4915;
  5143. }
  5144. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  5145. return -4916;
  5146. }
  5147. /* -- Verify decryption works */
  5148. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  5149. aad2, sizeof(aad2),
  5150. cipher2, sizeof(cipher2),
  5151. authTag2, generatedPlaintext);
  5152. if (err) {
  5153. return err;
  5154. }
  5155. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  5156. return -4917;
  5157. }
  5158. /* AEAD init/update/final - bad argument tests */
  5159. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  5160. CHACHA20_POLY1305_AEAD_DECRYPT);
  5161. if (err != BAD_FUNC_ARG)
  5162. return -4918;
  5163. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  5164. CHACHA20_POLY1305_AEAD_DECRYPT);
  5165. if (err != BAD_FUNC_ARG)
  5166. return -4919;
  5167. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  5168. CHACHA20_POLY1305_AEAD_DECRYPT);
  5169. if (err != BAD_FUNC_ARG)
  5170. return -4920;
  5171. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  5172. if (err != BAD_FUNC_ARG)
  5173. return -4921;
  5174. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  5175. if (err != BAD_FUNC_ARG)
  5176. return -4922;
  5177. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  5178. generatedPlaintext, sizeof(plaintext1));
  5179. if (err != BAD_FUNC_ARG)
  5180. return -4923;
  5181. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  5182. sizeof(plaintext1));
  5183. if (err != BAD_FUNC_ARG)
  5184. return -4924;
  5185. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  5186. sizeof(plaintext1));
  5187. if (err != BAD_FUNC_ARG)
  5188. return -4925;
  5189. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  5190. if (err != BAD_FUNC_ARG)
  5191. return -4926;
  5192. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  5193. if (err != BAD_FUNC_ARG)
  5194. return -4927;
  5195. /* AEAD init/update/final - bad state tests */
  5196. /* clear struct - make valgrind happy to resolve
  5197. "Conditional jump or move depends on uninitialised value(s)".
  5198. The enum is "int" size and aead.state is "byte" */
  5199. /* The wc_ChaCha20Poly1305_Init function does this normally */
  5200. XMEMSET(&aead, 0, sizeof(aead));
  5201. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5202. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5203. if (err != BAD_STATE_E)
  5204. return -4928;
  5205. aead.state = CHACHA20_POLY1305_STATE_DATA;
  5206. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5207. if (err != BAD_STATE_E)
  5208. return -4929;
  5209. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5210. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  5211. generatedPlaintext, sizeof(plaintext1));
  5212. if (err != BAD_STATE_E)
  5213. return -4930;
  5214. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5215. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5216. if (err != BAD_STATE_E)
  5217. return -4931;
  5218. aead.state = CHACHA20_POLY1305_STATE_READY;
  5219. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5220. if (err != BAD_STATE_E)
  5221. return -4932;
  5222. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5223. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5224. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5225. /* Test 1 - Encrypt */
  5226. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5227. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5228. if (err != 0)
  5229. return -4933;
  5230. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5231. if (err != 0)
  5232. return -4934;
  5233. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5234. /* test doing data in smaller chunks */
  5235. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5236. word32 dataLen = sizeof(plaintext1) - testLen;
  5237. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5238. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5239. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  5240. &generatedCiphertext[testLen], dataLen);
  5241. if (err != 0)
  5242. return -4935;
  5243. testLen += dataLen;
  5244. }
  5245. #else
  5246. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  5247. generatedCiphertext, sizeof(plaintext1));
  5248. #endif
  5249. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5250. if (err != 0)
  5251. return -4936;
  5252. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5253. if (err != 0)
  5254. return -4937;
  5255. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5256. return -4938;
  5257. }
  5258. /* Test 1 - Decrypt */
  5259. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5260. CHACHA20_POLY1305_AEAD_DECRYPT);
  5261. if (err != 0)
  5262. return -4939;
  5263. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5264. if (err != 0)
  5265. return -4940;
  5266. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5267. /* test doing data in smaller chunks */
  5268. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5269. word32 dataLen = sizeof(plaintext1) - testLen;
  5270. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5271. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5272. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5273. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5274. dataLen);
  5275. if (err != 0)
  5276. return -4941;
  5277. testLen += dataLen;
  5278. }
  5279. #else
  5280. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5281. generatedPlaintext, sizeof(cipher1));
  5282. #endif
  5283. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5284. if (err != 0)
  5285. return -4942;
  5286. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5287. if (err != 0)
  5288. return -4943;
  5289. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5290. return -4944;
  5291. }
  5292. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5293. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5294. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5295. /* Test 2 - Encrypt */
  5296. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5297. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5298. if (err != 0)
  5299. return -4945;
  5300. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5301. if (err != 0)
  5302. return -4946;
  5303. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5304. /* test doing data in smaller chunks */
  5305. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5306. word32 dataLen = sizeof(plaintext2) - testLen;
  5307. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5308. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5309. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  5310. &generatedCiphertext[testLen], dataLen);
  5311. if (err != 0)
  5312. return -4947;
  5313. testLen += dataLen;
  5314. }
  5315. #else
  5316. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  5317. sizeof(plaintext2));
  5318. #endif
  5319. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5320. if (err != 0)
  5321. return -4948;
  5322. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5323. if (err != 0)
  5324. return -4949;
  5325. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5326. return -4950;
  5327. }
  5328. /* Test 2 - Decrypt */
  5329. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5330. CHACHA20_POLY1305_AEAD_DECRYPT);
  5331. if (err != 0)
  5332. return -4951;
  5333. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5334. if (err != 0)
  5335. return -4952;
  5336. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5337. /* test doing data in smaller chunks */
  5338. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5339. word32 dataLen = sizeof(plaintext2) - testLen;
  5340. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5341. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5342. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5343. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5344. dataLen);
  5345. if (err != 0)
  5346. return -4953;
  5347. testLen += dataLen;
  5348. }
  5349. #else
  5350. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5351. generatedPlaintext, sizeof(cipher2));
  5352. #endif
  5353. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5354. if (err != 0)
  5355. return -4954;
  5356. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5357. if (err != 0)
  5358. return -4955;
  5359. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  5360. return -4956;
  5361. }
  5362. return err;
  5363. }
  5364. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  5365. #ifndef NO_DES3
  5366. WOLFSSL_TEST_SUBROUTINE int des_test(void)
  5367. {
  5368. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  5369. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  5370. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  5371. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  5372. };
  5373. byte plain[24];
  5374. byte cipher[24];
  5375. Des enc;
  5376. Des dec;
  5377. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  5378. {
  5379. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  5380. };
  5381. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  5382. {
  5383. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  5384. };
  5385. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  5386. {
  5387. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  5388. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  5389. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  5390. };
  5391. int ret;
  5392. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  5393. if (ret != 0)
  5394. return -5000;
  5395. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  5396. if (ret != 0)
  5397. return -5001;
  5398. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  5399. if (ret != 0)
  5400. return -5002;
  5401. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  5402. if (ret != 0)
  5403. return -5003;
  5404. if (XMEMCMP(plain, vector, sizeof(plain)))
  5405. return -5004;
  5406. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  5407. return -5005;
  5408. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  5409. if (ret != 0)
  5410. return -5006;
  5411. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  5412. {
  5413. EncryptedInfo info;
  5414. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  5415. XMEMCPY(info.iv, iv, sizeof(iv));
  5416. info.ivSz = sizeof(iv);
  5417. info.keySz = sizeof(key);
  5418. info.cipherType = WC_CIPHER_DES;
  5419. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  5420. sizeof(key), WC_HASH_TYPE_SHA);
  5421. if (ret != 0)
  5422. return -5007;
  5423. /* Test invalid info ptr */
  5424. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  5425. sizeof(key), WC_HASH_TYPE_SHA);
  5426. if (ret != BAD_FUNC_ARG)
  5427. return -5008;
  5428. /* Test invalid hash type */
  5429. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  5430. sizeof(key), WC_HASH_TYPE_NONE);
  5431. if (ret == 0)
  5432. return -5009;
  5433. }
  5434. #endif
  5435. return 0;
  5436. }
  5437. #endif /* NO_DES3 */
  5438. #ifndef NO_DES3
  5439. WOLFSSL_TEST_SUBROUTINE int des3_test(void)
  5440. {
  5441. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  5442. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  5443. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  5444. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  5445. };
  5446. byte plain[24];
  5447. byte cipher[24];
  5448. Des3 enc;
  5449. Des3 dec;
  5450. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  5451. {
  5452. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  5453. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  5454. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  5455. };
  5456. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  5457. {
  5458. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  5459. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  5460. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  5461. };
  5462. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  5463. {
  5464. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  5465. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  5466. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  5467. };
  5468. int ret;
  5469. if (wc_Des3Init(&enc, HEAP_HINT, devId) != 0)
  5470. return -5100;
  5471. if (wc_Des3Init(&dec, HEAP_HINT, devId) != 0)
  5472. return -5101;
  5473. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  5474. if (ret != 0)
  5475. return -5102;
  5476. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  5477. if (ret != 0)
  5478. return -5103;
  5479. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  5480. #if defined(WOLFSSL_ASYNC_CRYPT)
  5481. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  5482. #endif
  5483. if (ret != 0)
  5484. return -5104;
  5485. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  5486. #if defined(WOLFSSL_ASYNC_CRYPT)
  5487. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  5488. #endif
  5489. if (ret != 0)
  5490. return -5105;
  5491. if (XMEMCMP(plain, vector, sizeof(plain)))
  5492. return -5106;
  5493. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  5494. return -5107;
  5495. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  5496. /* test the same vectors with using compatibility layer */
  5497. {
  5498. DES_key_schedule ks1;
  5499. DES_key_schedule ks2;
  5500. DES_key_schedule ks3;
  5501. DES_cblock iv4;
  5502. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  5503. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  5504. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  5505. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  5506. XMEMSET(plain, 0, sizeof(plain));
  5507. XMEMSET(cipher, 0, sizeof(cipher));
  5508. DES_ede3_cbc_encrypt(vector, cipher, sizeof(vector), &ks1, &ks2, &ks3,
  5509. &iv4, DES_ENCRYPT);
  5510. DES_ede3_cbc_encrypt(cipher, plain, sizeof(cipher), &ks1, &ks2, &ks3,
  5511. &iv4, DES_DECRYPT);
  5512. if (XMEMCMP(plain, vector, sizeof(plain)))
  5513. return -5108;
  5514. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  5515. return -5109;
  5516. }
  5517. #endif /* OPENSSL_EXTRA */
  5518. wc_Des3Free(&enc);
  5519. wc_Des3Free(&dec);
  5520. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  5521. {
  5522. EncryptedInfo info;
  5523. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  5524. XMEMCPY(info.iv, iv3, sizeof(iv3));
  5525. info.ivSz = sizeof(iv3);
  5526. info.keySz = sizeof(key3);
  5527. info.cipherType = WC_CIPHER_DES3;
  5528. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  5529. sizeof(key3), WC_HASH_TYPE_SHA);
  5530. if (ret != 0)
  5531. return -5110;
  5532. }
  5533. #endif
  5534. return 0;
  5535. }
  5536. #endif /* NO_DES */
  5537. #ifndef NO_AES
  5538. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  5539. defined(WOLFSSL_AES_XTS)
  5540. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5541. /* pass in the function, key, iv, plain text and expected and this function
  5542. * tests that the encryption and decryption is successful */
  5543. static int EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  5544. const byte* iv, const byte* plain, int plainSz,
  5545. const byte* expected, int expectedSz)
  5546. {
  5547. #ifdef WOLFSSL_SMALL_STACK
  5548. EVP_CIPHER_CTX *ctx = NULL;
  5549. #else
  5550. EVP_CIPHER_CTX ctx[1];
  5551. #endif
  5552. int idx, ret = 0, cipherSz;
  5553. byte* cipher;
  5554. #ifdef WOLFSSL_SMALL_STACK
  5555. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  5556. return MEMORY_E;
  5557. #endif
  5558. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5559. if (cipher == NULL) {
  5560. ret = -5120;
  5561. goto EVP_TEST_END;
  5562. }
  5563. /* test encrypt */
  5564. EVP_CIPHER_CTX_init(ctx);
  5565. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  5566. ret = -5121;
  5567. goto EVP_TEST_END;
  5568. }
  5569. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  5570. ret = -5122;
  5571. goto EVP_TEST_END;
  5572. }
  5573. cipherSz = idx;
  5574. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  5575. ret = -5123;
  5576. goto EVP_TEST_END;
  5577. }
  5578. cipherSz += idx;
  5579. if (XMEMCMP(cipher, expected, plainSz)) {
  5580. ret = -5124;
  5581. goto EVP_TEST_END;
  5582. }
  5583. /* test decrypt */
  5584. EVP_CIPHER_CTX_init(ctx);
  5585. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  5586. ret = -5125;
  5587. goto EVP_TEST_END;
  5588. }
  5589. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  5590. ret = -5126;
  5591. goto EVP_TEST_END;
  5592. }
  5593. cipherSz = idx;
  5594. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  5595. ret = -5127;
  5596. goto EVP_TEST_END;
  5597. }
  5598. cipherSz += idx;
  5599. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  5600. ret = -5128;
  5601. goto EVP_TEST_END;
  5602. }
  5603. EVP_TEST_END:
  5604. if (cipher)
  5605. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5606. (void)cipherSz;
  5607. #ifdef WOLFSSL_SMALL_STACK
  5608. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  5609. #endif
  5610. return ret;
  5611. }
  5612. #endif /* OPENSSL_EXTRA */
  5613. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  5614. #ifdef WOLFSSL_AES_OFB
  5615. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  5616. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void)
  5617. {
  5618. #ifdef WOLFSSL_AES_256
  5619. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  5620. {
  5621. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  5622. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  5623. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  5624. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  5625. };
  5626. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  5627. {
  5628. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  5629. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  5630. };
  5631. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  5632. {
  5633. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  5634. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  5635. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  5636. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  5637. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  5638. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  5639. };
  5640. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  5641. {
  5642. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  5643. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  5644. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  5645. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  5646. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  5647. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  5648. };
  5649. #endif /* WOLFSSL_AES_256 */
  5650. #ifdef WOLFSSL_AES_128
  5651. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  5652. {
  5653. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  5654. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  5655. };
  5656. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  5657. {
  5658. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5659. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5660. };
  5661. WOLFSSL_SMALL_STACK_STATIC const byte plain2[] =
  5662. {
  5663. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5664. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5665. };
  5666. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  5667. {
  5668. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  5669. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  5670. };
  5671. #endif /* WOLFSSL_AES_128 */
  5672. #ifdef WOLFSSL_AES_192
  5673. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  5674. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  5675. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  5676. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  5677. };
  5678. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  5679. {
  5680. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5681. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5682. };
  5683. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  5684. {
  5685. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  5686. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  5687. };
  5688. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  5689. {
  5690. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5691. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5692. };
  5693. #endif /* WOLFSSL_AES_192 */
  5694. #ifdef WOLFSSL_SMALL_STACK
  5695. Aes *enc = NULL;
  5696. #else
  5697. Aes enc[1];
  5698. #endif
  5699. byte cipher[AES_BLOCK_SIZE * 4];
  5700. #ifdef HAVE_AES_DECRYPT
  5701. #ifdef WOLFSSL_SMALL_STACK
  5702. Aes *dec = NULL;
  5703. #else
  5704. Aes dec[1];
  5705. #endif
  5706. byte plain [AES_BLOCK_SIZE * 4];
  5707. #endif
  5708. int ret = 0;
  5709. #ifdef WOLFSSL_SMALL_STACK
  5710. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  5711. ERROR_OUT(-1, out);
  5712. #ifdef HAVE_AES_DECRYPT
  5713. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  5714. ERROR_OUT(-1, out);
  5715. #endif
  5716. #endif
  5717. XMEMSET(enc, 0, sizeof *enc);
  5718. #ifdef HAVE_AES_DECRYPT
  5719. XMEMSET(dec, 0, sizeof *dec);
  5720. #endif
  5721. #ifdef WOLFSSL_AES_128
  5722. /* 128 key size test */
  5723. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5724. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  5725. cipher2, sizeof(cipher2));
  5726. if (ret != 0) {
  5727. goto out;
  5728. }
  5729. #endif
  5730. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  5731. if (ret != 0)
  5732. ERROR_OUT(-5129, out);
  5733. #ifdef HAVE_AES_DECRYPT
  5734. /* decrypt uses AES_ENCRYPTION */
  5735. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  5736. if (ret != 0)
  5737. ERROR_OUT(-5130, out);
  5738. #endif
  5739. XMEMSET(cipher, 0, sizeof(cipher));
  5740. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  5741. if (ret != 0)
  5742. ERROR_OUT(-5131, out);
  5743. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  5744. ERROR_OUT(-5132, out);
  5745. #ifdef HAVE_AES_DECRYPT
  5746. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  5747. if (ret != 0)
  5748. ERROR_OUT(-5133, out);
  5749. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  5750. ERROR_OUT(-5134, out);
  5751. #endif /* HAVE_AES_DECRYPT */
  5752. #endif /* WOLFSSL_AES_128 */
  5753. #ifdef WOLFSSL_AES_192
  5754. /* 192 key size test */
  5755. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5756. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  5757. cipher3, sizeof(cipher3));
  5758. if (ret != 0) {
  5759. goto out;
  5760. }
  5761. #endif
  5762. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  5763. if (ret != 0)
  5764. ERROR_OUT(-5135, out);
  5765. #ifdef HAVE_AES_DECRYPT
  5766. /* decrypt uses AES_ENCRYPTION */
  5767. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  5768. if (ret != 0)
  5769. ERROR_OUT(-5136, out);
  5770. #endif
  5771. XMEMSET(cipher, 0, sizeof(cipher));
  5772. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  5773. if (ret != 0)
  5774. ERROR_OUT(-5137, out);
  5775. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  5776. ERROR_OUT(-5138, out);
  5777. #ifdef HAVE_AES_DECRYPT
  5778. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  5779. if (ret != 0)
  5780. ERROR_OUT(-5139, out);
  5781. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  5782. ERROR_OUT(-5140, out);
  5783. #endif /* HAVE_AES_DECRYPT */
  5784. #endif /* WOLFSSL_AES_192 */
  5785. #ifdef WOLFSSL_AES_256
  5786. /* 256 key size test */
  5787. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5788. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  5789. cipher1, sizeof(cipher1));
  5790. if (ret != 0) {
  5791. goto out;
  5792. }
  5793. #endif
  5794. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5795. if (ret != 0)
  5796. ERROR_OUT(-5141, out);
  5797. #ifdef HAVE_AES_DECRYPT
  5798. /* decrypt uses AES_ENCRYPTION */
  5799. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5800. if (ret != 0)
  5801. ERROR_OUT(-5142, out);
  5802. #endif
  5803. XMEMSET(cipher, 0, sizeof(cipher));
  5804. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  5805. if (ret != 0)
  5806. ERROR_OUT(-5143, out);
  5807. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  5808. ERROR_OUT(-5144, out);
  5809. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  5810. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  5811. if (ret != 0)
  5812. ERROR_OUT(-5145, out);
  5813. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  5814. AES_BLOCK_SIZE))
  5815. ERROR_OUT(-5146, out);
  5816. #ifdef HAVE_AES_DECRYPT
  5817. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  5818. if (ret != 0)
  5819. ERROR_OUT(-5147, out);
  5820. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  5821. ERROR_OUT(-5148, out);
  5822. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  5823. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  5824. if (ret != 0)
  5825. ERROR_OUT(-5149, out);
  5826. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  5827. AES_BLOCK_SIZE))
  5828. ERROR_OUT(-5150, out);
  5829. #endif /* HAVE_AES_DECRYPT */
  5830. /* multiple blocks at once */
  5831. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5832. if (ret != 0)
  5833. ERROR_OUT(-5151, out);
  5834. #ifdef HAVE_AES_DECRYPT
  5835. /* decrypt uses AES_ENCRYPTION */
  5836. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5837. if (ret != 0)
  5838. ERROR_OUT(-5152, out);
  5839. #endif
  5840. XMEMSET(cipher, 0, sizeof(cipher));
  5841. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  5842. if (ret != 0)
  5843. ERROR_OUT(-5153, out);
  5844. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  5845. ERROR_OUT(-5154, out);
  5846. #ifdef HAVE_AES_DECRYPT
  5847. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  5848. if (ret != 0)
  5849. ERROR_OUT(-5155, out);
  5850. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  5851. ERROR_OUT(-5156, out);
  5852. #endif /* HAVE_AES_DECRYPT */
  5853. /* inline decrypt/encrypt*/
  5854. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5855. if (ret != 0)
  5856. ERROR_OUT(-5157, out);
  5857. #ifdef HAVE_AES_DECRYPT
  5858. /* decrypt uses AES_ENCRYPTION */
  5859. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5860. if (ret != 0)
  5861. ERROR_OUT(-5158, out);
  5862. #endif
  5863. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  5864. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  5865. if (ret != 0)
  5866. ERROR_OUT(-5159, out);
  5867. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  5868. ERROR_OUT(-5160, out);
  5869. #ifdef HAVE_AES_DECRYPT
  5870. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  5871. if (ret != 0)
  5872. ERROR_OUT(-5161, out);
  5873. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  5874. ERROR_OUT(-5162, out);
  5875. #endif /* HAVE_AES_DECRYPT */
  5876. /* 256 key size test leftover support */
  5877. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5878. if (ret != 0)
  5879. ERROR_OUT(-5163, out);
  5880. #ifdef HAVE_AES_DECRYPT
  5881. /* decrypt uses AES_ENCRYPTION */
  5882. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5883. if (ret != 0)
  5884. ERROR_OUT(-5164, out);
  5885. #endif
  5886. XMEMSET(cipher, 0, sizeof(cipher));
  5887. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  5888. if (ret != 0)
  5889. ERROR_OUT(-5165, out);
  5890. if (XMEMCMP(cipher, cipher1, 3))
  5891. ERROR_OUT(-5166, out);
  5892. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  5893. if (ret != 0)
  5894. ERROR_OUT(-5167, out);
  5895. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  5896. ERROR_OUT(-5168, out);
  5897. #ifdef HAVE_AES_DECRYPT
  5898. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  5899. if (ret != 0)
  5900. ERROR_OUT(-5169, out);
  5901. if (XMEMCMP(plain, plain1, 6))
  5902. ERROR_OUT(-5170, out);
  5903. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  5904. if (ret != 0)
  5905. ERROR_OUT(-5171, out);
  5906. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  5907. ERROR_OUT(-5172, out);
  5908. #endif /* HAVE_AES_DECRYPT */
  5909. out:
  5910. #ifdef WOLFSSL_SMALL_STACK
  5911. if (enc)
  5912. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  5913. #ifdef HAVE_AES_DECRYPT
  5914. if (dec)
  5915. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  5916. #endif
  5917. #endif
  5918. #endif /* WOLFSSL_AES_256 */
  5919. return ret;
  5920. }
  5921. #endif /* WOLFSSL_AES_OFB */
  5922. #if defined(WOLFSSL_AES_CFB)
  5923. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of Operation Methods an*/
  5924. static int aescfb_test(void)
  5925. {
  5926. #ifdef WOLFSSL_SMALL_STACK
  5927. Aes *enc = NULL;
  5928. #else
  5929. Aes enc[1];
  5930. #endif
  5931. int enc_inited = 0;
  5932. byte cipher[AES_BLOCK_SIZE * 4];
  5933. #ifdef HAVE_AES_DECRYPT
  5934. #ifdef WOLFSSL_SMALL_STACK
  5935. Aes *dec = NULL;
  5936. #else
  5937. Aes dec[1];
  5938. #endif
  5939. int dec_inited = 0;
  5940. byte plain [AES_BLOCK_SIZE * 4];
  5941. #endif
  5942. int ret = 0;
  5943. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  5944. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  5945. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  5946. };
  5947. #ifdef WOLFSSL_AES_128
  5948. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  5949. {
  5950. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  5951. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  5952. };
  5953. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  5954. {
  5955. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  5956. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  5957. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  5958. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  5959. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  5960. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  5961. };
  5962. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  5963. {
  5964. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  5965. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  5966. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  5967. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  5968. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  5969. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  5970. };
  5971. #endif /* WOLFSSL_AES_128 */
  5972. #ifdef WOLFSSL_AES_192
  5973. /* 192 size key test */
  5974. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  5975. {
  5976. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  5977. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  5978. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  5979. };
  5980. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  5981. {
  5982. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  5983. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  5984. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  5985. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  5986. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  5987. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  5988. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  5989. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  5990. };
  5991. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  5992. {
  5993. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  5994. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  5995. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  5996. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  5997. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  5998. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  5999. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6000. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6001. };
  6002. #endif /* WOLFSSL_AES_192 */
  6003. #ifdef WOLFSSL_AES_256
  6004. /* 256 size key simple test */
  6005. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6006. {
  6007. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  6008. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  6009. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  6010. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  6011. };
  6012. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6013. {
  6014. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  6015. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  6016. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  6017. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  6018. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  6019. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  6020. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  6021. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  6022. };
  6023. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6024. {
  6025. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6026. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6027. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6028. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6029. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6030. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6031. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6032. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6033. };
  6034. #endif /* WOLFSSL_AES_256 */
  6035. #ifdef WOLFSSL_SMALL_STACK
  6036. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6037. ERROR_OUT(-1, out);
  6038. #ifdef HAVE_AES_DECRYPT
  6039. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6040. ERROR_OUT(-1, out);
  6041. #endif
  6042. #endif
  6043. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6044. ERROR_OUT(-5173, out);
  6045. else
  6046. enc_inited = 1;
  6047. #ifdef HAVE_AES_DECRYPT
  6048. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6049. ERROR_OUT(-5174, out);
  6050. else
  6051. dec_inited = 1;
  6052. #endif
  6053. #ifdef WOLFSSL_AES_128
  6054. /* 128 key tests */
  6055. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6056. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  6057. cipher1, sizeof(cipher1));
  6058. if (ret != 0) {
  6059. return ret;
  6060. }
  6061. #endif
  6062. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6063. if (ret != 0)
  6064. ERROR_OUT(-5175, out);
  6065. #ifdef HAVE_AES_DECRYPT
  6066. /* decrypt uses AES_ENCRYPTION */
  6067. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6068. if (ret != 0)
  6069. ERROR_OUT(-5176, out);
  6070. #endif
  6071. XMEMSET(cipher, 0, sizeof(cipher));
  6072. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  6073. if (ret != 0)
  6074. ERROR_OUT(-5177, out);
  6075. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6076. ERROR_OUT(-5178, out);
  6077. /* test restarting encryption process */
  6078. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  6079. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  6080. if (ret != 0)
  6081. ERROR_OUT(-5179, out);
  6082. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  6083. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  6084. ERROR_OUT(-5180, out);
  6085. #ifdef HAVE_AES_DECRYPT
  6086. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  6087. if (ret != 0)
  6088. ERROR_OUT(-5181, out);
  6089. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  6090. ERROR_OUT(-5182, out);
  6091. #endif /* HAVE_AES_DECRYPT */
  6092. #endif /* WOLFSSL_AES_128 */
  6093. #ifdef WOLFSSL_AES_192
  6094. /* 192 key size test */
  6095. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6096. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  6097. cipher2, sizeof(cipher2));
  6098. if (ret != 0) {
  6099. return ret;
  6100. }
  6101. #endif
  6102. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6103. if (ret != 0)
  6104. ERROR_OUT(-5183, out);
  6105. #ifdef HAVE_AES_DECRYPT
  6106. /* decrypt uses AES_ENCRYPTION */
  6107. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6108. if (ret != 0)
  6109. ERROR_OUT(-5184, out);
  6110. #endif
  6111. XMEMSET(cipher, 0, sizeof(cipher));
  6112. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  6113. if (ret != 0)
  6114. ERROR_OUT(-5185, out);
  6115. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  6116. ERROR_OUT(-5186, out);
  6117. #ifdef HAVE_AES_DECRYPT
  6118. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  6119. if (ret != 0)
  6120. ERROR_OUT(-5187, out);
  6121. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  6122. ERROR_OUT(-5188, out);
  6123. #endif /* HAVE_AES_DECRYPT */
  6124. #endif /* WOLFSSL_AES_192 */
  6125. #ifdef WOLFSSL_AES_256
  6126. /* 256 key size test */
  6127. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6128. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  6129. cipher3, sizeof(cipher3));
  6130. if (ret != 0) {
  6131. return ret;
  6132. }
  6133. #endif
  6134. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6135. if (ret != 0)
  6136. ERROR_OUT(-5189, out);
  6137. #ifdef HAVE_AES_DECRYPT
  6138. /* decrypt uses AES_ENCRYPTION */
  6139. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6140. if (ret != 0)
  6141. ERROR_OUT(-5190, out);
  6142. #endif
  6143. /* test with data left overs, magic lengths are checking near edges */
  6144. XMEMSET(cipher, 0, sizeof(cipher));
  6145. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  6146. if (ret != 0)
  6147. ERROR_OUT(-5191, out);
  6148. if (XMEMCMP(cipher, cipher3, 4))
  6149. ERROR_OUT(-5192, out);
  6150. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  6151. if (ret != 0)
  6152. ERROR_OUT(-5193, out);
  6153. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  6154. ERROR_OUT(-5194, out);
  6155. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  6156. (AES_BLOCK_SIZE * 4) - 31);
  6157. if (ret != 0)
  6158. ERROR_OUT(-5195, out);
  6159. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  6160. ERROR_OUT(-5196, out);
  6161. #ifdef HAVE_AES_DECRYPT
  6162. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  6163. if (ret != 0)
  6164. ERROR_OUT(-5197, out);
  6165. if (XMEMCMP(plain, msg3, 4))
  6166. ERROR_OUT(-5198, out);
  6167. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  6168. if (ret != 0)
  6169. ERROR_OUT(-5199, out);
  6170. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  6171. if (ret != 0)
  6172. ERROR_OUT(-5200, out);
  6173. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  6174. ERROR_OUT(-5201, out);
  6175. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  6176. (AES_BLOCK_SIZE * 4) - 31);
  6177. if (ret != 0)
  6178. ERROR_OUT(-5202, out);
  6179. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  6180. ERROR_OUT(-5203, out);
  6181. #endif /* HAVE_AES_DECRYPT */
  6182. #endif /* WOLFSSL_AES_256 */
  6183. out:
  6184. if (enc_inited)
  6185. wc_AesFree(enc);
  6186. if (dec_inited)
  6187. wc_AesFree(dec);
  6188. #ifdef WOLFSSL_SMALL_STACK
  6189. if (enc)
  6190. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6191. #ifdef HAVE_AES_DECRYPT
  6192. if (dec)
  6193. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6194. #endif
  6195. #endif
  6196. return ret;
  6197. }
  6198. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6199. static int aescfb1_test(void)
  6200. {
  6201. #ifdef WOLFSSL_SMALL_STACK
  6202. Aes *enc = NULL;
  6203. #else
  6204. Aes enc[1];
  6205. #endif
  6206. int enc_inited = 0;
  6207. byte cipher[AES_BLOCK_SIZE];
  6208. #ifdef HAVE_AES_DECRYPT
  6209. #ifdef WOLFSSL_SMALL_STACK
  6210. Aes *dec = NULL;
  6211. #else
  6212. Aes dec[1];
  6213. #endif
  6214. int dec_inited = 0;
  6215. byte plain [AES_BLOCK_SIZE];
  6216. #endif
  6217. int ret = 0;
  6218. #ifdef WOLFSSL_AES_128
  6219. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6220. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  6221. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  6222. };
  6223. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6224. {
  6225. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  6226. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  6227. };
  6228. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6229. {
  6230. 0x00
  6231. };
  6232. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6233. {
  6234. 0xC0
  6235. };
  6236. #endif /* WOLFSSL_AES_128 */
  6237. #ifdef WOLFSSL_AES_192
  6238. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6239. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  6240. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  6241. };
  6242. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6243. {
  6244. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  6245. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  6246. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  6247. };
  6248. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6249. {
  6250. 0x30
  6251. };
  6252. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6253. {
  6254. 0x80
  6255. };
  6256. #endif /* WOLFSSL_AES_192 */
  6257. #ifdef WOLFSSL_AES_256
  6258. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  6259. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  6260. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  6261. };
  6262. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6263. {
  6264. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  6265. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  6266. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  6267. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  6268. };
  6269. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6270. {
  6271. 0xF7, 0x00
  6272. };
  6273. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6274. {
  6275. 0x41, 0xC0
  6276. };
  6277. #endif /* WOLFSSL_AES_256 */
  6278. #ifdef WOLFSSL_SMALL_STACK
  6279. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6280. ERROR_OUT(-1, out);
  6281. #ifdef HAVE_AES_DECRYPT
  6282. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6283. ERROR_OUT(-1, out);
  6284. #endif
  6285. #endif
  6286. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6287. ERROR_OUT(-5204, out);
  6288. else
  6289. enc_inited = 1;
  6290. #ifdef HAVE_AES_DECRYPT
  6291. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6292. ERROR_OUT(-5205, out);
  6293. else
  6294. dec_inited = 1;
  6295. #endif
  6296. #ifdef WOLFSSL_AES_128
  6297. /* 128 key tests */
  6298. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6299. if (ret != 0)
  6300. ERROR_OUT(-5206, out);
  6301. #ifdef HAVE_AES_DECRYPT
  6302. /* decrypt uses AES_ENCRYPTION */
  6303. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6304. if (ret != 0)
  6305. ERROR_OUT(-5207, out);
  6306. #endif
  6307. XMEMSET(cipher, 0, sizeof(cipher));
  6308. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  6309. if (ret != 0)
  6310. ERROR_OUT(-5208, out);
  6311. if (cipher[0] != cipher1[0])
  6312. ERROR_OUT(-5209, out);
  6313. #ifdef HAVE_AES_DECRYPT
  6314. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  6315. if (ret != 0)
  6316. ERROR_OUT(-5210, out);
  6317. if (plain[0] != msg1[0])
  6318. ERROR_OUT(-5211, out);
  6319. #endif /* HAVE_AES_DECRYPT */
  6320. #ifdef OPENSSL_EXTRA
  6321. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6322. if (ret != 0)
  6323. ERROR_OUT(-5212, out);
  6324. XMEMSET(cipher, 0, sizeof(cipher));
  6325. ret = wc_AesCfb1Encrypt(enc, cipher, msg1,
  6326. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  6327. if (ret != 0)
  6328. ERROR_OUT(-5213, out);
  6329. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  6330. cipher, sizeof(msg1));
  6331. if (ret != 0) {
  6332. goto out;
  6333. }
  6334. #endif
  6335. #endif /* WOLFSSL_AES_128 */
  6336. #ifdef WOLFSSL_AES_192
  6337. /* 192 key tests */
  6338. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6339. if (ret != 0)
  6340. ERROR_OUT(-5214, out);
  6341. XMEMSET(cipher, 0, sizeof(cipher));
  6342. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  6343. if (ret != 0)
  6344. ERROR_OUT(-5215, out);
  6345. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  6346. ERROR_OUT(-5216, out);
  6347. #ifdef OPENSSL_EXTRA
  6348. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6349. if (ret != 0)
  6350. ERROR_OUT(-5217, out);
  6351. XMEMSET(cipher, 0, sizeof(cipher));
  6352. ret = wc_AesCfb1Encrypt(enc, cipher, msg2,
  6353. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  6354. if (ret != 0)
  6355. ERROR_OUT(-5218, out);
  6356. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  6357. cipher, sizeof(msg2));
  6358. if (ret != 0) {
  6359. goto out;
  6360. }
  6361. #endif
  6362. #endif /* WOLFSSL_AES_192 */
  6363. #ifdef WOLFSSL_AES_256
  6364. /* 256 key tests */
  6365. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6366. if (ret != 0)
  6367. ERROR_OUT(-5219, out);
  6368. XMEMSET(cipher, 0, sizeof(cipher));
  6369. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  6370. if (ret != 0)
  6371. ERROR_OUT(-5220, out);
  6372. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  6373. ERROR_OUT(-5221, out);
  6374. #ifdef OPENSSL_EXTRA
  6375. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6376. if (ret != 0)
  6377. ERROR_OUT(-5222, out);
  6378. XMEMSET(cipher, 0, sizeof(cipher));
  6379. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  6380. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  6381. if (ret != 0)
  6382. ERROR_OUT(-5223, out);
  6383. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  6384. cipher, sizeof(msg3));
  6385. if (ret != 0) {
  6386. goto out;
  6387. }
  6388. #endif
  6389. out:
  6390. if (enc_inited)
  6391. wc_AesFree(enc);
  6392. #ifdef HAVE_AES_DECRYPT
  6393. if (dec_inited)
  6394. wc_AesFree(dec);
  6395. #endif
  6396. #ifdef WOLFSSL_SMALL_STACK
  6397. if (enc)
  6398. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6399. #ifdef HAVE_AES_DECRYPT
  6400. if (dec)
  6401. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6402. #endif
  6403. #endif
  6404. #endif /* WOLFSSL_AES_256 */
  6405. return ret;
  6406. }
  6407. static int aescfb8_test(void)
  6408. {
  6409. #ifdef WOLFSSL_SMALL_STACK
  6410. Aes *enc = NULL;
  6411. #else
  6412. Aes enc[1];
  6413. #endif
  6414. int enc_inited = 0;
  6415. byte cipher[AES_BLOCK_SIZE];
  6416. #ifdef HAVE_AES_DECRYPT
  6417. #ifdef WOLFSSL_SMALL_STACK
  6418. Aes *dec = NULL;
  6419. #else
  6420. Aes dec[1];
  6421. #endif
  6422. int dec_inited = 0;
  6423. byte plain [AES_BLOCK_SIZE];
  6424. #endif
  6425. int ret = 0;
  6426. #ifdef WOLFSSL_AES_128
  6427. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6428. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  6429. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  6430. };
  6431. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6432. {
  6433. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  6434. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  6435. };
  6436. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6437. {
  6438. 0xd2,0x76,0x91
  6439. };
  6440. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6441. {
  6442. 0xc9,0x06,0x35
  6443. };
  6444. #endif /* WOLFSSL_AES_128 */
  6445. #ifdef WOLFSSL_AES_192
  6446. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6447. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  6448. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  6449. };
  6450. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6451. {
  6452. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  6453. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  6454. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  6455. };
  6456. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6457. {
  6458. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  6459. 0xb5,0xe8
  6460. };
  6461. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6462. {
  6463. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  6464. 0x3a,0x60
  6465. };
  6466. #endif
  6467. #ifdef WOLFSSL_AES_256
  6468. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  6469. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  6470. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  6471. };
  6472. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6473. {
  6474. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  6475. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  6476. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  6477. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  6478. };
  6479. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6480. {
  6481. 0x1c,0xff,0x95
  6482. };
  6483. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6484. {
  6485. 0xb9,0x74,0xfa
  6486. };
  6487. #endif
  6488. #ifdef WOLFSSL_SMALL_STACK
  6489. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6490. ERROR_OUT(-5238, out);
  6491. #ifdef HAVE_AES_DECRYPT
  6492. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6493. ERROR_OUT(-5239, out);
  6494. #endif
  6495. #endif
  6496. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6497. ERROR_OUT(-5224, out);
  6498. else
  6499. enc_inited = 1;
  6500. #ifdef HAVE_AES_DECRYPT
  6501. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6502. ERROR_OUT(-5225, out);
  6503. else
  6504. dec_inited = 1;
  6505. #endif
  6506. #ifdef WOLFSSL_AES_128
  6507. /* 128 key tests */
  6508. #ifdef OPENSSL_EXTRA
  6509. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  6510. cipher1, sizeof(cipher1));
  6511. if (ret != 0) {
  6512. return ret;
  6513. }
  6514. #endif
  6515. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6516. if (ret != 0)
  6517. ERROR_OUT(-5226, out);
  6518. #ifdef HAVE_AES_DECRYPT
  6519. /* decrypt uses AES_ENCRYPTION */
  6520. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6521. if (ret != 0)
  6522. ERROR_OUT(-5227, out);
  6523. #endif
  6524. XMEMSET(cipher, 0, sizeof(cipher));
  6525. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  6526. if (ret != 0)
  6527. ERROR_OUT(-5228, out);
  6528. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  6529. ERROR_OUT(-5229, out);
  6530. #ifdef HAVE_AES_DECRYPT
  6531. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  6532. if (ret != 0)
  6533. ERROR_OUT(-5230, out);
  6534. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  6535. ERROR_OUT(-5231, out);
  6536. #endif /* HAVE_AES_DECRYPT */
  6537. #endif /* WOLFSSL_AES_128 */
  6538. #ifdef WOLFSSL_AES_192
  6539. /* 192 key tests */
  6540. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6541. if (ret != 0)
  6542. ERROR_OUT(-5232, out);
  6543. XMEMSET(cipher, 0, sizeof(cipher));
  6544. ret = wc_AesCfb8Encrypt(enc, cipher, msg2, sizeof(msg2));
  6545. if (ret != 0)
  6546. ERROR_OUT(-5233, out);
  6547. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  6548. ERROR_OUT(-5234, out);
  6549. #ifdef OPENSSL_EXTRA
  6550. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  6551. cipher2, sizeof(msg2));
  6552. if (ret != 0) {
  6553. return ret;
  6554. }
  6555. #endif
  6556. #endif /* WOLFSSL_AES_192 */
  6557. #ifdef WOLFSSL_AES_256
  6558. /* 256 key tests */
  6559. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6560. if (ret != 0)
  6561. ERROR_OUT(-5235, out);
  6562. XMEMSET(cipher, 0, sizeof(cipher));
  6563. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  6564. if (ret != 0)
  6565. ERROR_OUT(-5236, out);
  6566. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  6567. ERROR_OUT(-5237, out);
  6568. #ifdef OPENSSL_EXTRA
  6569. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  6570. cipher3, sizeof(msg3));
  6571. if (ret != 0) {
  6572. goto out;
  6573. }
  6574. #endif
  6575. out:
  6576. if (enc_inited)
  6577. wc_AesFree(enc);
  6578. #ifdef HAVE_AES_DECRYPT
  6579. if (dec_inited)
  6580. wc_AesFree(dec);
  6581. #endif
  6582. #ifdef WOLFSSL_SMALL_STACK
  6583. if (enc)
  6584. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6585. #ifdef HAVE_AES_DECRYPT
  6586. if (dec)
  6587. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6588. #endif
  6589. #endif
  6590. #endif /* WOLFSSL_AES_256 */
  6591. return ret;
  6592. }
  6593. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  6594. #endif /* WOLFSSL_AES_CFB */
  6595. static int aes_key_size_test(void)
  6596. {
  6597. int ret;
  6598. #ifdef WOLFSSL_SMALL_STACK
  6599. Aes *aes;
  6600. #else
  6601. Aes aes[1];
  6602. #endif
  6603. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6604. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  6605. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6606. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  6607. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  6608. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6609. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  6610. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6611. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  6612. byte iv[] = "1234567890abcdef";
  6613. #ifndef HAVE_FIPS
  6614. word32 keySize;
  6615. #endif
  6616. #ifdef WOLFSSL_SMALL_STACK
  6617. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6618. return -5315;
  6619. #endif
  6620. #if !defined(HAVE_FIPS) || \
  6621. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  6622. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  6623. * supported with that FIPS version */
  6624. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  6625. if (ret != BAD_FUNC_ARG)
  6626. ERROR_OUT(-5300, out);
  6627. #endif
  6628. ret = wc_AesInit(aes, HEAP_HINT, devId);
  6629. /* 0 check OK for FIPSv1 */
  6630. if (ret != 0)
  6631. ERROR_OUT(-5301, out);
  6632. #ifndef HAVE_FIPS
  6633. /* Parameter Validation testing. */
  6634. ret = wc_AesGetKeySize(NULL, NULL);
  6635. if (ret != BAD_FUNC_ARG)
  6636. ERROR_OUT(-5302, out);
  6637. ret = wc_AesGetKeySize(aes, NULL);
  6638. if (ret != BAD_FUNC_ARG)
  6639. ERROR_OUT(-5303, out);
  6640. ret = wc_AesGetKeySize(NULL, &keySize);
  6641. if (ret != BAD_FUNC_ARG)
  6642. ERROR_OUT(-5304, out);
  6643. /* Crashes in FIPS */
  6644. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  6645. if (ret != BAD_FUNC_ARG)
  6646. ERROR_OUT(-5305, out);
  6647. #endif
  6648. /* NULL IV indicates to use all zeros IV. */
  6649. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  6650. #ifdef WOLFSSL_AES_128
  6651. if (ret != 0)
  6652. #else
  6653. if (ret != BAD_FUNC_ARG)
  6654. #endif
  6655. ERROR_OUT(-5306, out);
  6656. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  6657. if (ret != BAD_FUNC_ARG)
  6658. ERROR_OUT(-5307, out);
  6659. /* CryptoCell handles rounds internally */
  6660. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  6661. /* Force invalid rounds */
  6662. aes->rounds = 16;
  6663. ret = wc_AesGetKeySize(aes, &keySize);
  6664. if (ret != BAD_FUNC_ARG)
  6665. ERROR_OUT(-5308, out);
  6666. #endif
  6667. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  6668. #ifdef WOLFSSL_AES_128
  6669. if (ret != 0)
  6670. #else
  6671. if (ret != BAD_FUNC_ARG)
  6672. #endif
  6673. ERROR_OUT(-5309, out);
  6674. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  6675. ret = wc_AesGetKeySize(aes, &keySize);
  6676. if (ret != 0 || keySize != sizeof(key16))
  6677. ERROR_OUT(-5310, out);
  6678. #endif
  6679. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  6680. #ifdef WOLFSSL_AES_192
  6681. if (ret != 0)
  6682. #else
  6683. if (ret != BAD_FUNC_ARG)
  6684. #endif
  6685. ERROR_OUT(-5311, out);
  6686. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  6687. ret = wc_AesGetKeySize(aes, &keySize);
  6688. if (ret != 0 || keySize != sizeof(key24))
  6689. ERROR_OUT(-5312, out);
  6690. #endif
  6691. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  6692. #ifdef WOLFSSL_AES_256
  6693. if (ret != 0)
  6694. #else
  6695. if (ret != BAD_FUNC_ARG)
  6696. #endif
  6697. ERROR_OUT(-5313, out);
  6698. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  6699. ret = wc_AesGetKeySize(aes, &keySize);
  6700. if (ret != 0 || keySize != sizeof(key32))
  6701. ERROR_OUT(-5314, out);
  6702. #endif
  6703. out:
  6704. #ifdef WOLFSSL_SMALL_STACK
  6705. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  6706. #endif
  6707. return ret;
  6708. }
  6709. #if defined(WOLFSSL_AES_XTS)
  6710. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  6711. #ifdef WOLFSSL_AES_128
  6712. static int aes_xts_128_test(void)
  6713. {
  6714. #ifdef WOLFSSL_SMALL_STACK
  6715. XtsAes *aes = NULL;
  6716. #else
  6717. XtsAes aes[1];
  6718. #endif
  6719. int aes_inited = 0;
  6720. int ret = 0;
  6721. unsigned char buf[AES_BLOCK_SIZE * 2];
  6722. unsigned char cipher[AES_BLOCK_SIZE * 2];
  6723. /* 128 key tests */
  6724. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  6725. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  6726. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  6727. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  6728. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  6729. };
  6730. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  6731. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  6732. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  6733. };
  6734. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  6735. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  6736. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  6737. };
  6738. /* plain text test of partial block is not from NIST test vector list */
  6739. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  6740. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  6741. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  6742. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  6743. };
  6744. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  6745. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  6746. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  6747. };
  6748. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  6749. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  6750. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  6751. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  6752. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  6753. };
  6754. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  6755. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  6756. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  6757. };
  6758. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  6759. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  6760. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  6761. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  6762. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  6763. };
  6764. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  6765. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  6766. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  6767. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  6768. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  6769. };
  6770. #ifdef WOLFSSL_SMALL_STACK
  6771. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6772. ERROR_OUT(-5417, out);
  6773. #endif
  6774. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6775. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  6776. if (ret != 0) {
  6777. printf("EVP_aes_128_xts failed!\n");
  6778. goto out;
  6779. }
  6780. #endif
  6781. XMEMSET(buf, 0, sizeof(buf));
  6782. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  6783. HEAP_HINT, devId) != 0)
  6784. ERROR_OUT(-5400, out);
  6785. else
  6786. aes_inited = 1;
  6787. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  6788. #if defined(WOLFSSL_ASYNC_CRYPT)
  6789. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6790. #endif
  6791. if (ret != 0)
  6792. ERROR_OUT(-5401, out);
  6793. if (XMEMCMP(c2, buf, sizeof(c2)))
  6794. ERROR_OUT(-5402, out);
  6795. XMEMSET(buf, 0, sizeof(buf));
  6796. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  6797. HEAP_HINT, devId) != 0)
  6798. ERROR_OUT(-5403, out);
  6799. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  6800. #if defined(WOLFSSL_ASYNC_CRYPT)
  6801. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6802. #endif
  6803. if (ret != 0)
  6804. ERROR_OUT(-5404, out);
  6805. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  6806. ERROR_OUT(-5405, out);
  6807. /* partial block encryption test */
  6808. XMEMSET(cipher, 0, sizeof(cipher));
  6809. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  6810. #if defined(WOLFSSL_ASYNC_CRYPT)
  6811. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6812. #endif
  6813. if (ret != 0)
  6814. ERROR_OUT(-5406, out);
  6815. wc_AesXtsFree(aes);
  6816. /* partial block decrypt test */
  6817. XMEMSET(buf, 0, sizeof(buf));
  6818. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  6819. HEAP_HINT, devId) != 0)
  6820. ERROR_OUT(-5407, out);
  6821. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  6822. #if defined(WOLFSSL_ASYNC_CRYPT)
  6823. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6824. #endif
  6825. if (ret != 0)
  6826. ERROR_OUT(-5408, out);
  6827. if (XMEMCMP(pp, buf, sizeof(pp)))
  6828. ERROR_OUT(-5409, out);
  6829. /* NIST decrypt test vector */
  6830. XMEMSET(buf, 0, sizeof(buf));
  6831. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  6832. #if defined(WOLFSSL_ASYNC_CRYPT)
  6833. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6834. #endif
  6835. if (ret != 0)
  6836. ERROR_OUT(-5410, out);
  6837. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  6838. ERROR_OUT(-5411, out);
  6839. /* fail case with decrypting using wrong key */
  6840. XMEMSET(buf, 0, sizeof(buf));
  6841. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  6842. #if defined(WOLFSSL_ASYNC_CRYPT)
  6843. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6844. #endif
  6845. if (ret != 0)
  6846. ERROR_OUT(-5412, out);
  6847. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  6848. ERROR_OUT(-5413, out);
  6849. /* set correct key and retest */
  6850. XMEMSET(buf, 0, sizeof(buf));
  6851. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  6852. HEAP_HINT, devId) != 0)
  6853. ERROR_OUT(-5414, out);
  6854. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  6855. #if defined(WOLFSSL_ASYNC_CRYPT)
  6856. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6857. #endif
  6858. if (ret != 0)
  6859. ERROR_OUT(-5415, out);
  6860. if (XMEMCMP(p2, buf, sizeof(p2)))
  6861. ERROR_OUT(-5416, out);
  6862. out:
  6863. if (aes_inited)
  6864. wc_AesXtsFree(aes);
  6865. #ifdef WOLFSSL_SMALL_STACK
  6866. if (aes)
  6867. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  6868. #endif
  6869. return ret;
  6870. }
  6871. #endif /* WOLFSSL_AES_128 */
  6872. #ifdef WOLFSSL_AES_256
  6873. static int aes_xts_256_test(void)
  6874. {
  6875. #ifdef WOLFSSL_SMALL_STACK
  6876. XtsAes *aes = NULL;
  6877. #else
  6878. XtsAes aes[1];
  6879. #endif
  6880. int aes_inited = 0;
  6881. int ret = 0;
  6882. unsigned char buf[AES_BLOCK_SIZE * 3];
  6883. unsigned char cipher[AES_BLOCK_SIZE * 3];
  6884. /* 256 key tests */
  6885. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  6886. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  6887. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  6888. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  6889. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  6890. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  6891. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  6892. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  6893. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  6894. };
  6895. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  6896. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  6897. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  6898. };
  6899. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  6900. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  6901. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  6902. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  6903. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  6904. };
  6905. /* plain text test of partial block is not from NIST test vector list */
  6906. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  6907. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  6908. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  6909. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  6910. };
  6911. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  6912. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  6913. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  6914. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  6915. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  6916. };
  6917. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  6918. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  6919. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  6920. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  6921. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  6922. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  6923. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  6924. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  6925. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  6926. };
  6927. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  6928. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  6929. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  6930. };
  6931. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  6932. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  6933. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  6934. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  6935. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  6936. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  6937. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  6938. };
  6939. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  6940. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  6941. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  6942. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  6943. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  6944. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  6945. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  6946. };
  6947. #ifdef WOLFSSL_SMALL_STACK
  6948. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6949. ERROR_OUT(-5515, out);
  6950. #endif
  6951. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6952. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  6953. if (ret != 0) {
  6954. printf("EVP_aes_256_xts failed\n");
  6955. goto out;
  6956. }
  6957. #endif
  6958. XMEMSET(buf, 0, sizeof(buf));
  6959. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  6960. HEAP_HINT, devId) != 0)
  6961. ERROR_OUT(-5500, out);
  6962. else
  6963. aes_inited = 1;
  6964. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  6965. #if defined(WOLFSSL_ASYNC_CRYPT)
  6966. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6967. #endif
  6968. if (ret != 0)
  6969. ERROR_OUT(-5501, out);
  6970. if (XMEMCMP(c2, buf, sizeof(c2)))
  6971. ERROR_OUT(-5502, out);
  6972. XMEMSET(buf, 0, sizeof(buf));
  6973. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  6974. HEAP_HINT, devId) != 0)
  6975. ERROR_OUT(-5503, out);
  6976. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  6977. #if defined(WOLFSSL_ASYNC_CRYPT)
  6978. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6979. #endif
  6980. if (ret != 0)
  6981. ERROR_OUT(-5504, out);
  6982. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  6983. ERROR_OUT(-5505, out);
  6984. /* partial block encryption test */
  6985. XMEMSET(cipher, 0, sizeof(cipher));
  6986. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  6987. #if defined(WOLFSSL_ASYNC_CRYPT)
  6988. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6989. #endif
  6990. if (ret != 0)
  6991. ERROR_OUT(-5506, out);
  6992. wc_AesXtsFree(aes);
  6993. /* partial block decrypt test */
  6994. XMEMSET(buf, 0, sizeof(buf));
  6995. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  6996. HEAP_HINT, devId) != 0)
  6997. ERROR_OUT(-5507, out);
  6998. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  6999. #if defined(WOLFSSL_ASYNC_CRYPT)
  7000. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7001. #endif
  7002. if (ret != 0)
  7003. ERROR_OUT(-5508, out);
  7004. if (XMEMCMP(pp, buf, sizeof(pp)))
  7005. ERROR_OUT(-5509, out);
  7006. /* NIST decrypt test vector */
  7007. XMEMSET(buf, 0, sizeof(buf));
  7008. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  7009. #if defined(WOLFSSL_ASYNC_CRYPT)
  7010. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7011. #endif
  7012. if (ret != 0)
  7013. ERROR_OUT(-5510, out);
  7014. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7015. ERROR_OUT(-5511, out);
  7016. XMEMSET(buf, 0, sizeof(buf));
  7017. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7018. HEAP_HINT, devId) != 0)
  7019. ERROR_OUT(-5512, out);
  7020. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7021. #if defined(WOLFSSL_ASYNC_CRYPT)
  7022. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7023. #endif
  7024. if (ret != 0)
  7025. ERROR_OUT(-5513, out);
  7026. if (XMEMCMP(p2, buf, sizeof(p2)))
  7027. ERROR_OUT(-5514, out);
  7028. out:
  7029. if (aes_inited)
  7030. wc_AesXtsFree(aes);
  7031. #ifdef WOLFSSL_SMALL_STACK
  7032. if (aes)
  7033. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7034. #endif
  7035. return ret;
  7036. }
  7037. #endif /* WOLFSSL_AES_256 */
  7038. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  7039. /* both 128 and 256 bit key test */
  7040. static int aes_xts_sector_test(void)
  7041. {
  7042. #ifdef WOLFSSL_SMALL_STACK
  7043. XtsAes *aes = NULL;
  7044. #else
  7045. XtsAes aes[1];
  7046. #endif
  7047. int aes_inited = 0;
  7048. int ret = 0;
  7049. unsigned char buf[AES_BLOCK_SIZE * 2];
  7050. /* 128 key tests */
  7051. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7052. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7053. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7054. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7055. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7056. };
  7057. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7058. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7059. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7060. };
  7061. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7062. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7063. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7064. };
  7065. word64 s1 = 141;
  7066. /* 256 key tests */
  7067. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7068. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  7069. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  7070. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  7071. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  7072. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  7073. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  7074. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  7075. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  7076. };
  7077. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7078. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  7079. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  7080. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  7081. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  7082. };
  7083. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7084. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  7085. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  7086. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  7087. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  7088. };
  7089. word64 s2 = 187;
  7090. #ifdef WOLFSSL_SMALL_STACK
  7091. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7092. ERROR_OUT(-5612, out);
  7093. #endif
  7094. XMEMSET(buf, 0, sizeof(buf));
  7095. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7096. HEAP_HINT, devId) != 0)
  7097. ERROR_OUT(-5600, out);
  7098. else
  7099. aes_inited = 1;
  7100. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  7101. #if defined(WOLFSSL_ASYNC_CRYPT)
  7102. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7103. #endif
  7104. if (ret != 0)
  7105. ERROR_OUT(-5601, out);
  7106. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7107. ERROR_OUT(-5602, out);
  7108. /* decrypt test */
  7109. XMEMSET(buf, 0, sizeof(buf));
  7110. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7111. HEAP_HINT, devId) != 0)
  7112. ERROR_OUT(-5603, out);
  7113. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  7114. #if defined(WOLFSSL_ASYNC_CRYPT)
  7115. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7116. #endif
  7117. if (ret != 0)
  7118. ERROR_OUT(-5604, out);
  7119. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7120. ERROR_OUT(-5605, out);
  7121. wc_AesXtsFree(aes);
  7122. /* 256 bit key tests */
  7123. XMEMSET(buf, 0, sizeof(buf));
  7124. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7125. HEAP_HINT, devId) != 0)
  7126. ERROR_OUT(-5606, out);
  7127. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  7128. #if defined(WOLFSSL_ASYNC_CRYPT)
  7129. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7130. #endif
  7131. if (ret != 0)
  7132. ERROR_OUT(-5607, out);
  7133. if (XMEMCMP(c2, buf, sizeof(c2)))
  7134. ERROR_OUT(-5608, out);
  7135. /* decrypt test */
  7136. XMEMSET(buf, 0, sizeof(buf));
  7137. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7138. HEAP_HINT, devId) != 0)
  7139. ERROR_OUT(-5609, out);
  7140. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  7141. #if defined(WOLFSSL_ASYNC_CRYPT)
  7142. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7143. #endif
  7144. if (ret != 0)
  7145. ERROR_OUT(-5610, out);
  7146. if (XMEMCMP(p2, buf, sizeof(p2)))
  7147. ERROR_OUT(-5611, out);
  7148. out:
  7149. if (aes_inited)
  7150. wc_AesXtsFree(aes);
  7151. #ifdef WOLFSSL_SMALL_STACK
  7152. if (aes)
  7153. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7154. #endif
  7155. return ret;
  7156. }
  7157. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  7158. #ifdef WOLFSSL_AES_128
  7159. /* testing of bad arguments */
  7160. static int aes_xts_args_test(void)
  7161. {
  7162. #ifdef WOLFSSL_SMALL_STACK
  7163. XtsAes *aes = NULL;
  7164. #else
  7165. XtsAes aes[1];
  7166. #endif
  7167. int aes_inited = 0;
  7168. int ret;
  7169. unsigned char buf[AES_BLOCK_SIZE * 2];
  7170. /* 128 key tests */
  7171. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7172. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7173. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7174. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7175. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7176. };
  7177. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7178. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7179. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7180. };
  7181. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7182. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7183. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7184. };
  7185. word64 s1 = 141;
  7186. #ifdef WOLFSSL_SMALL_STACK
  7187. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7188. ERROR_OUT(-5708, out);
  7189. #endif
  7190. if (wc_AesXtsSetKey(NULL, k1, sizeof(k1), AES_ENCRYPTION,
  7191. HEAP_HINT, devId) == 0)
  7192. ERROR_OUT(-5700, out);
  7193. if (wc_AesXtsSetKey(aes, NULL, sizeof(k1), AES_ENCRYPTION,
  7194. HEAP_HINT, devId) == 0)
  7195. ERROR_OUT(-5701, out);
  7196. /* encryption operations */
  7197. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7198. HEAP_HINT, devId) != 0)
  7199. ERROR_OUT(-5702, out);
  7200. else
  7201. aes_inited = 1;
  7202. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  7203. #if defined(WOLFSSL_ASYNC_CRYPT)
  7204. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7205. #endif
  7206. if (ret == 0)
  7207. ERROR_OUT(-5703, out);
  7208. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), s1);
  7209. #if defined(WOLFSSL_ASYNC_CRYPT)
  7210. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7211. #endif
  7212. if (ret == 0)
  7213. ERROR_OUT(-5704, out);
  7214. wc_AesXtsFree(aes);
  7215. /* decryption operations */
  7216. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7217. HEAP_HINT, devId) != 0)
  7218. ERROR_OUT(-5705, out);
  7219. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  7220. #if defined(WOLFSSL_ASYNC_CRYPT)
  7221. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7222. #endif
  7223. if (ret == 0)
  7224. ERROR_OUT(-5706, out);
  7225. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  7226. #if defined(WOLFSSL_ASYNC_CRYPT)
  7227. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7228. #endif
  7229. if (ret == 0)
  7230. ERROR_OUT(-5707, out);
  7231. ret = 0;
  7232. out:
  7233. if (aes_inited)
  7234. wc_AesXtsFree(aes);
  7235. #ifdef WOLFSSL_SMALL_STACK
  7236. if (aes)
  7237. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7238. #endif
  7239. return ret;
  7240. }
  7241. #endif /* WOLFSSL_AES_128 */
  7242. #endif /* WOLFSSL_AES_XTS */
  7243. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  7244. static int aes_cbc_test(void)
  7245. {
  7246. byte cipher[AES_BLOCK_SIZE];
  7247. byte plain[AES_BLOCK_SIZE];
  7248. int ret;
  7249. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  7250. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7251. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7252. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7253. };
  7254. byte key[] = "0123456789abcdef "; /* align */
  7255. byte iv[] = "1234567890abcdef "; /* align */
  7256. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7257. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7258. /* Parameter Validation testing. */
  7259. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  7260. if (ret != BAD_FUNC_ARG)
  7261. return -5800;
  7262. #ifdef HAVE_AES_DECRYPT
  7263. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  7264. if (ret != BAD_FUNC_ARG)
  7265. return -5801;
  7266. #endif
  7267. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  7268. AES_BLOCK_SIZE, iv);
  7269. if (ret != 0)
  7270. return -5802;
  7271. #ifdef HAVE_AES_DECRYPT
  7272. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  7273. AES_BLOCK_SIZE, iv);
  7274. if (ret != 0)
  7275. return -5803;
  7276. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  7277. return -5804;
  7278. #endif /* HAVE_AES_DECRYPT */
  7279. (void)plain;
  7280. return 0;
  7281. }
  7282. #endif
  7283. WOLFSSL_TEST_SUBROUTINE int aes_test(void)
  7284. {
  7285. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  7286. #ifdef WOLFSSL_SMALL_STACK
  7287. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7288. #else
  7289. Aes enc[1];
  7290. #endif
  7291. byte cipher[AES_BLOCK_SIZE * 4];
  7292. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  7293. #ifdef WOLFSSL_SMALL_STACK
  7294. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7295. #else
  7296. Aes dec[1];
  7297. #endif
  7298. byte plain [AES_BLOCK_SIZE * 4];
  7299. #endif
  7300. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER */
  7301. int ret = 0;
  7302. #ifdef HAVE_AES_CBC
  7303. #ifdef WOLFSSL_AES_128
  7304. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  7305. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7306. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7307. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7308. };
  7309. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  7310. {
  7311. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  7312. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  7313. };
  7314. WOLFSSL_SMALL_STACK_STATIC const byte key[] = "0123456789abcdef "; /* align */
  7315. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  7316. #ifdef WOLFSSL_SMALL_STACK
  7317. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  7318. if (enc == NULL)
  7319. ERROR_OUT(-5948, out);
  7320. #endif
  7321. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  7322. if (dec == NULL)
  7323. ERROR_OUT(-5949, out);
  7324. #endif
  7325. #endif
  7326. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7327. ERROR_OUT(-5900, out); /* note this error code is used programmatically in cleanup. */
  7328. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  7329. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  7330. ERROR_OUT(-5901, out); /* note this error code is used programmatically in cleanup. */
  7331. #endif
  7332. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7333. if (ret != 0)
  7334. ERROR_OUT(-5902, out);
  7335. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  7336. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  7337. if (ret != 0)
  7338. ERROR_OUT(-5903, out);
  7339. #endif
  7340. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  7341. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  7342. #if defined(WOLFSSL_ASYNC_CRYPT)
  7343. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7344. #endif
  7345. if (ret != 0)
  7346. ERROR_OUT(-5904, out);
  7347. #ifdef HAVE_AES_DECRYPT
  7348. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  7349. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  7350. #if defined(WOLFSSL_ASYNC_CRYPT)
  7351. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7352. #endif
  7353. if (ret != 0)
  7354. ERROR_OUT(-5905, out);
  7355. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  7356. ERROR_OUT(-5906, out);
  7357. #endif /* HAVE_AES_DECRYPT */
  7358. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  7359. ERROR_OUT(-5907, out);
  7360. #endif /* WOLFSSL_AES_128 */
  7361. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  7362. {
  7363. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  7364. /* "All work and no play makes Jack a dull boy. " */
  7365. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7366. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7367. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7368. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  7369. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  7370. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  7371. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  7372. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  7373. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  7374. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  7375. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  7376. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7377. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7378. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7379. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  7380. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  7381. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  7382. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  7383. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  7384. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  7385. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  7386. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  7387. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7388. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7389. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7390. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  7391. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  7392. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  7393. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  7394. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  7395. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  7396. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  7397. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  7398. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7399. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7400. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7401. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  7402. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  7403. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  7404. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  7405. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  7406. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  7407. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  7408. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  7409. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7410. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7411. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7412. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  7413. };
  7414. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  7415. word32 keySz, msgSz;
  7416. #ifdef WOLFSSL_SMALL_STACK
  7417. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7418. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7419. if ((bigCipher == NULL) ||
  7420. (bigPlain == NULL)) {
  7421. if (bigCipher != NULL)
  7422. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7423. ERROR_OUT(-5947, out);
  7424. }
  7425. #else
  7426. byte bigCipher[sizeof(bigMsg)];
  7427. byte bigPlain[sizeof(bigMsg)];
  7428. #endif
  7429. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  7430. * message by AES_BLOCK_SIZE for each size of AES key. */
  7431. for (keySz = 16; keySz <= 32; keySz += 8) {
  7432. for (msgSz = AES_BLOCK_SIZE;
  7433. msgSz <= sizeof(bigMsg);
  7434. msgSz += AES_BLOCK_SIZE) {
  7435. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  7436. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  7437. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  7438. if (ret != 0) {
  7439. ret = -5908;
  7440. break;
  7441. }
  7442. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  7443. if (ret != 0) {
  7444. ret = -5909;
  7445. break;
  7446. }
  7447. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  7448. #if defined(WOLFSSL_ASYNC_CRYPT)
  7449. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7450. #endif
  7451. if (ret != 0) {
  7452. ret = -5910;
  7453. break;
  7454. }
  7455. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  7456. #if defined(WOLFSSL_ASYNC_CRYPT)
  7457. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7458. #endif
  7459. if (ret != 0) {
  7460. ret = -5911;
  7461. break;
  7462. }
  7463. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  7464. ret = -5912;
  7465. break;
  7466. }
  7467. }
  7468. if (ret != 0)
  7469. break;
  7470. }
  7471. #ifdef WOLFSSL_SMALL_STACK
  7472. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7473. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7474. #endif
  7475. if (ret != 0)
  7476. goto out;
  7477. }
  7478. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  7479. /* Test of AES IV state with encrypt/decrypt */
  7480. #ifdef WOLFSSL_AES_128
  7481. {
  7482. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  7483. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  7484. */
  7485. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7486. {
  7487. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  7488. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  7489. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  7490. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  7491. };
  7492. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  7493. {
  7494. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  7495. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  7496. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  7497. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  7498. };
  7499. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  7500. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  7501. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  7502. };
  7503. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7504. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  7505. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  7506. };
  7507. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7508. if (ret != 0)
  7509. ERROR_OUT(-5913, out);
  7510. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  7511. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  7512. #if defined(WOLFSSL_ASYNC_CRYPT)
  7513. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7514. #endif
  7515. if (ret != 0)
  7516. ERROR_OUT(-5914, out);
  7517. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  7518. ERROR_OUT(-5915, out);
  7519. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  7520. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  7521. #if defined(WOLFSSL_ASYNC_CRYPT)
  7522. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7523. #endif
  7524. if (ret != 0)
  7525. ERROR_OUT(-5916, out);
  7526. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  7527. AES_BLOCK_SIZE))
  7528. ERROR_OUT(-5917, out);
  7529. #if defined(HAVE_AES_DECRYPT)
  7530. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  7531. if (ret != 0)
  7532. ERROR_OUT(-5918, out);
  7533. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  7534. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  7535. #if defined(WOLFSSL_ASYNC_CRYPT)
  7536. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7537. #endif
  7538. if (ret != 0)
  7539. ERROR_OUT(-5919, out);
  7540. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  7541. ERROR_OUT(-5920, out);
  7542. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  7543. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  7544. #if defined(WOLFSSL_ASYNC_CRYPT)
  7545. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7546. #endif
  7547. if (ret != 0)
  7548. ERROR_OUT(-5921, out);
  7549. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  7550. AES_BLOCK_SIZE))
  7551. ERROR_OUT(-5922, out);
  7552. #endif /* HAVE_AES_DECRYPT */
  7553. }
  7554. #endif /* WOLFSSL_AES_128 */
  7555. #endif /* HAVE_AES_CBC */
  7556. #ifdef WOLFSSL_AES_COUNTER
  7557. {
  7558. /* test vectors from "Recommendation for Block Cipher Modes of
  7559. * Operation" NIST Special Publication 800-38A */
  7560. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  7561. {
  7562. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  7563. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  7564. };
  7565. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  7566. {
  7567. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7568. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  7569. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  7570. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  7571. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  7572. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  7573. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  7574. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  7575. };
  7576. #ifdef WOLFSSL_AES_128
  7577. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  7578. {
  7579. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  7580. 0xc2
  7581. };
  7582. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  7583. {
  7584. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  7585. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  7586. };
  7587. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  7588. {
  7589. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  7590. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  7591. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  7592. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  7593. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  7594. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  7595. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  7596. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  7597. };
  7598. #endif /* WOLFSSL_AES_128 */
  7599. #ifdef WOLFSSL_AES_192
  7600. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  7601. {
  7602. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  7603. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  7604. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  7605. };
  7606. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  7607. {
  7608. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  7609. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  7610. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  7611. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  7612. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  7613. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  7614. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  7615. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  7616. };
  7617. #endif
  7618. #ifdef WOLFSSL_AES_256
  7619. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  7620. {
  7621. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7622. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7623. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7624. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7625. };
  7626. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  7627. {
  7628. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  7629. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  7630. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  7631. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  7632. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  7633. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  7634. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  7635. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  7636. };
  7637. #endif
  7638. #ifdef WOLFSSL_AES_128
  7639. wc_AesSetKeyDirect(enc, ctr128Key, sizeof(ctr128Key),
  7640. ctrIv, AES_ENCRYPTION);
  7641. /* Ctr only uses encrypt, even on key setup */
  7642. wc_AesSetKeyDirect(dec, ctr128Key, sizeof(ctr128Key),
  7643. ctrIv, AES_ENCRYPTION);
  7644. ret = wc_AesCtrEncrypt(enc, cipher, ctrPlain, sizeof(ctrPlain));
  7645. if (ret != 0) {
  7646. ERROR_OUT(-5923, out);
  7647. }
  7648. ret = wc_AesCtrEncrypt(dec, plain, cipher, sizeof(ctrPlain));
  7649. if (ret != 0) {
  7650. ERROR_OUT(-5924, out);
  7651. }
  7652. if (XMEMCMP(plain, ctrPlain, sizeof(ctrPlain)))
  7653. ERROR_OUT(-5925, out);
  7654. if (XMEMCMP(cipher, ctr128Cipher, sizeof(ctr128Cipher)))
  7655. ERROR_OUT(-5926, out);
  7656. /* let's try with just 9 bytes, non block size test */
  7657. wc_AesSetKeyDirect(enc, ctr128Key, AES_BLOCK_SIZE,
  7658. ctrIv, AES_ENCRYPTION);
  7659. /* Ctr only uses encrypt, even on key setup */
  7660. wc_AesSetKeyDirect(dec, ctr128Key, AES_BLOCK_SIZE,
  7661. ctrIv, AES_ENCRYPTION);
  7662. ret = wc_AesCtrEncrypt(enc, cipher, ctrPlain, sizeof(oddCipher));
  7663. if (ret != 0) {
  7664. ERROR_OUT(-5927, out);
  7665. }
  7666. ret = wc_AesCtrEncrypt(dec, plain, cipher, sizeof(oddCipher));
  7667. if (ret != 0) {
  7668. ERROR_OUT(-5928, out);
  7669. }
  7670. if (XMEMCMP(plain, ctrPlain, sizeof(oddCipher)))
  7671. ERROR_OUT(-5929, out);
  7672. if (XMEMCMP(cipher, ctr128Cipher, sizeof(oddCipher)))
  7673. ERROR_OUT(-5930, out);
  7674. /* and an additional 9 bytes to reuse tmp left buffer */
  7675. ret = wc_AesCtrEncrypt(enc, cipher, ctrPlain, sizeof(oddCipher));
  7676. if (ret != 0) {
  7677. ERROR_OUT(-5931, out);
  7678. }
  7679. ret = wc_AesCtrEncrypt(dec, plain, cipher, sizeof(oddCipher));
  7680. if (ret != 0) {
  7681. ERROR_OUT(-5932, out);
  7682. }
  7683. if (XMEMCMP(plain, ctrPlain, sizeof(oddCipher)))
  7684. ERROR_OUT(-5933, out);
  7685. if (XMEMCMP(cipher, oddCipher, sizeof(oddCipher)))
  7686. ERROR_OUT(-5934, out);
  7687. #endif /* WOLFSSL_AES_128 */
  7688. #ifdef WOLFSSL_AES_192
  7689. /* 192 bit key */
  7690. wc_AesSetKeyDirect(enc, ctr192Key, sizeof(ctr192Key),
  7691. ctrIv, AES_ENCRYPTION);
  7692. /* Ctr only uses encrypt, even on key setup */
  7693. wc_AesSetKeyDirect(dec, ctr192Key, sizeof(ctr192Key),
  7694. ctrIv, AES_ENCRYPTION);
  7695. XMEMSET(plain, 0, sizeof(plain));
  7696. ret = wc_AesCtrEncrypt(enc, plain, ctr192Cipher, sizeof(ctr192Cipher));
  7697. if (ret != 0) {
  7698. ERROR_OUT(-5935, out);
  7699. }
  7700. if (XMEMCMP(plain, ctrPlain, sizeof(ctr192Cipher)))
  7701. ERROR_OUT(-5936, out);
  7702. ret = wc_AesCtrEncrypt(dec, cipher, ctrPlain, sizeof(ctrPlain));
  7703. if (ret != 0) {
  7704. ERROR_OUT(-5937, out);
  7705. }
  7706. if (XMEMCMP(ctr192Cipher, cipher, sizeof(ctr192Cipher)))
  7707. ERROR_OUT(-5938, out);
  7708. #endif /* WOLFSSL_AES_192 */
  7709. #ifdef WOLFSSL_AES_256
  7710. /* 256 bit key */
  7711. wc_AesSetKeyDirect(enc, ctr256Key, sizeof(ctr256Key),
  7712. ctrIv, AES_ENCRYPTION);
  7713. /* Ctr only uses encrypt, even on key setup */
  7714. wc_AesSetKeyDirect(dec, ctr256Key, sizeof(ctr256Key),
  7715. ctrIv, AES_ENCRYPTION);
  7716. XMEMSET(plain, 0, sizeof(plain));
  7717. ret = wc_AesCtrEncrypt(enc, plain, ctr256Cipher, sizeof(ctr256Cipher));
  7718. if (ret != 0) {
  7719. ERROR_OUT(-5939, out);
  7720. }
  7721. if (XMEMCMP(plain, ctrPlain, sizeof(ctrPlain)))
  7722. ERROR_OUT(-5940, out);
  7723. ret = wc_AesCtrEncrypt(dec, cipher, ctrPlain, sizeof(ctrPlain));
  7724. if (ret != 0) {
  7725. ERROR_OUT(-5941, out);
  7726. }
  7727. if (XMEMCMP(ctr256Cipher, cipher, sizeof(ctr256Cipher)))
  7728. ERROR_OUT(-5942, out);
  7729. #endif /* WOLFSSL_AES_256 */
  7730. }
  7731. #endif /* WOLFSSL_AES_COUNTER */
  7732. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  7733. {
  7734. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  7735. {
  7736. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7737. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  7738. };
  7739. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  7740. {
  7741. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  7742. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  7743. };
  7744. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  7745. {
  7746. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7747. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7748. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7749. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7750. };
  7751. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7752. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  7753. if (ret != 0)
  7754. ERROR_OUT(-5943, out);
  7755. wc_AesEncryptDirect(enc, cipher, niPlain);
  7756. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  7757. ERROR_OUT(-5944, out);
  7758. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7759. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  7760. if (ret != 0)
  7761. ERROR_OUT(-5945, out);
  7762. wc_AesDecryptDirect(dec, plain, niCipher);
  7763. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  7764. ERROR_OUT(-5946, out);
  7765. }
  7766. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  7767. ret = aes_key_size_test();
  7768. if (ret != 0)
  7769. goto out;
  7770. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  7771. ret = aes_cbc_test();
  7772. if (ret != 0)
  7773. goto out;
  7774. #endif
  7775. #if defined(WOLFSSL_AES_XTS)
  7776. #ifdef WOLFSSL_AES_128
  7777. ret = aes_xts_128_test();
  7778. if (ret != 0)
  7779. goto out;
  7780. #endif
  7781. #ifdef WOLFSSL_AES_256
  7782. ret = aes_xts_256_test();
  7783. if (ret != 0)
  7784. goto out;
  7785. #endif
  7786. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  7787. ret = aes_xts_sector_test();
  7788. if (ret != 0)
  7789. goto out;
  7790. #endif
  7791. #ifdef WOLFSSL_AES_128
  7792. ret = aes_xts_args_test();
  7793. if (ret != 0)
  7794. goto out;
  7795. #endif
  7796. #endif
  7797. #if defined(WOLFSSL_AES_CFB)
  7798. ret = aescfb_test();
  7799. if (ret != 0)
  7800. goto out;
  7801. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7802. ret = aescfb1_test();
  7803. if (ret != 0)
  7804. goto out;
  7805. ret = aescfb8_test();
  7806. if (ret != 0)
  7807. goto out;
  7808. #endif
  7809. #endif
  7810. out:
  7811. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER)
  7812. #ifdef WOLFSSL_SMALL_STACK
  7813. if (enc) {
  7814. if (ret != -5900) /* note this must match ERRROR_OUT() code
  7815. * for wc_AesInit(enc, ...) failure above.
  7816. */
  7817. wc_AesFree(enc);
  7818. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7819. }
  7820. #else
  7821. if (ret != -5900)
  7822. wc_AesFree(enc);
  7823. #endif
  7824. (void)cipher;
  7825. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  7826. #ifdef WOLFSSL_SMALL_STACK
  7827. if (dec) {
  7828. if ((ret != -5900) && (ret != -5901))
  7829. /* note these codes must match the ERRROR_OUT() codes for
  7830. * wc_AesInit() failures above.
  7831. */
  7832. wc_AesFree(dec);
  7833. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7834. }
  7835. #else
  7836. if ((ret != -5900) && (ret != -5901))
  7837. wc_AesFree(dec);
  7838. #endif
  7839. (void)plain;
  7840. #endif
  7841. #endif
  7842. return ret;
  7843. }
  7844. #ifdef WOLFSSL_AES_192
  7845. WOLFSSL_TEST_SUBROUTINE int aes192_test(void)
  7846. {
  7847. #ifdef HAVE_AES_CBC
  7848. #ifdef WOLFSSL_SMALL_STACK
  7849. Aes *enc = NULL;
  7850. #else
  7851. Aes enc[1];
  7852. #endif
  7853. byte cipher[AES_BLOCK_SIZE];
  7854. #ifdef HAVE_AES_DECRYPT
  7855. #ifdef WOLFSSL_SMALL_STACK
  7856. Aes *dec = NULL;
  7857. #else
  7858. Aes dec[1];
  7859. #endif
  7860. byte plain[AES_BLOCK_SIZE];
  7861. #endif
  7862. #endif /* HAVE_AES_CBC */
  7863. int ret = 0;
  7864. #ifdef HAVE_AES_CBC
  7865. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  7866. * Appendix F.2.3 */
  7867. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  7868. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7869. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  7870. };
  7871. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  7872. {
  7873. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  7874. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  7875. };
  7876. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  7877. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  7878. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  7879. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  7880. };
  7881. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  7882. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  7883. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  7884. };
  7885. #ifdef WOLFSSL_SMALL_STACK
  7886. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7887. ERROR_OUT(-6008, out);
  7888. #ifdef HAVE_AES_DECRYPT
  7889. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7890. ERROR_OUT(-6009, out);
  7891. #endif
  7892. #endif
  7893. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7894. ERROR_OUT(-6000, out);
  7895. #ifdef HAVE_AES_DECRYPT
  7896. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  7897. ERROR_OUT(-6001, out);
  7898. #endif
  7899. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  7900. if (ret != 0)
  7901. ERROR_OUT(-6002, out);
  7902. #ifdef HAVE_AES_DECRYPT
  7903. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  7904. if (ret != 0)
  7905. ERROR_OUT(-6003, out);
  7906. #endif
  7907. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7908. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  7909. #if defined(WOLFSSL_ASYNC_CRYPT)
  7910. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7911. #endif
  7912. if (ret != 0)
  7913. ERROR_OUT(-6004, out);
  7914. #ifdef HAVE_AES_DECRYPT
  7915. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7916. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  7917. #if defined(WOLFSSL_ASYNC_CRYPT)
  7918. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7919. #endif
  7920. if (ret != 0)
  7921. ERROR_OUT(-6005, out);
  7922. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  7923. ERROR_OUT(-6006, out);
  7924. }
  7925. #endif
  7926. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  7927. ERROR_OUT(-6007, out);
  7928. wc_AesFree(enc);
  7929. #ifdef HAVE_AES_DECRYPT
  7930. wc_AesFree(dec);
  7931. #endif
  7932. out:
  7933. #ifdef WOLFSSL_SMALL_STACK
  7934. if (enc)
  7935. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7936. #ifdef HAVE_AES_DECRYPT
  7937. if (dec)
  7938. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7939. #endif
  7940. #endif
  7941. #endif /* HAVE_AES_CBC */
  7942. return ret;
  7943. }
  7944. #endif /* WOLFSSL_AES_192 */
  7945. #ifdef WOLFSSL_AES_256
  7946. WOLFSSL_TEST_SUBROUTINE int aes256_test(void)
  7947. {
  7948. #ifdef HAVE_AES_CBC
  7949. #ifdef WOLFSSL_SMALL_STACK
  7950. Aes *enc = NULL;
  7951. #else
  7952. Aes enc[1];
  7953. #endif
  7954. byte cipher[AES_BLOCK_SIZE];
  7955. #ifdef HAVE_AES_DECRYPT
  7956. #ifdef WOLFSSL_SMALL_STACK
  7957. Aes *dec = NULL;
  7958. #else
  7959. Aes dec[1];
  7960. #endif
  7961. byte plain[AES_BLOCK_SIZE];
  7962. #endif
  7963. #endif /* HAVE_AES_CBC */
  7964. int ret = 0;
  7965. #ifdef HAVE_AES_CBC
  7966. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  7967. * Appendix F.2.5 */
  7968. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  7969. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7970. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  7971. };
  7972. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  7973. {
  7974. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  7975. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  7976. };
  7977. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  7978. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7979. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7980. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7981. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7982. };
  7983. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  7984. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  7985. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  7986. };
  7987. #ifdef WOLFSSL_SMALL_STACK
  7988. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7989. ERROR_OUT(-6108, out);
  7990. #ifdef HAVE_AES_DECRYPT
  7991. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7992. ERROR_OUT(-6109, out);
  7993. #endif
  7994. #endif
  7995. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7996. ERROR_OUT(-6100, out);
  7997. #ifdef HAVE_AES_DECRYPT
  7998. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  7999. ERROR_OUT(-6101, out);
  8000. #endif
  8001. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  8002. if (ret != 0)
  8003. ERROR_OUT(-6102, out);
  8004. #ifdef HAVE_AES_DECRYPT
  8005. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  8006. if (ret != 0)
  8007. ERROR_OUT(-6103, out);
  8008. #endif
  8009. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8010. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  8011. #if defined(WOLFSSL_ASYNC_CRYPT)
  8012. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8013. #endif
  8014. if (ret != 0)
  8015. ERROR_OUT(-6104, out);
  8016. #ifdef HAVE_AES_DECRYPT
  8017. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8018. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  8019. #if defined(WOLFSSL_ASYNC_CRYPT)
  8020. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8021. #endif
  8022. if (ret != 0)
  8023. ERROR_OUT(-6105, out);
  8024. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  8025. ERROR_OUT(-6106, out);
  8026. }
  8027. #endif
  8028. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  8029. ERROR_OUT(-6107, out);
  8030. wc_AesFree(enc);
  8031. #ifdef HAVE_AES_DECRYPT
  8032. wc_AesFree(dec);
  8033. #endif
  8034. out:
  8035. #ifdef WOLFSSL_SMALL_STACK
  8036. if (enc)
  8037. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8038. #ifdef HAVE_AES_DECRYPT
  8039. if (dec)
  8040. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8041. #endif
  8042. #endif
  8043. #endif /* HAVE_AES_CBC */
  8044. return ret;
  8045. }
  8046. #endif /* WOLFSSL_AES_256 */
  8047. #ifdef HAVE_AESGCM
  8048. static int aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  8049. byte* plain, int plainSz, byte* cipher, int cipherSz,
  8050. byte* aad, int aadSz, byte* tag, int tagSz)
  8051. {
  8052. int ret, enc_inited = 0, dec_inited = 0;
  8053. #ifdef WOLFSSL_SMALL_STACK
  8054. Aes *enc = NULL;
  8055. Aes *dec = NULL;
  8056. #else
  8057. Aes enc[1];
  8058. Aes dec[1];
  8059. #endif
  8060. byte resultT[AES_BLOCK_SIZE];
  8061. byte resultP[AES_BLOCK_SIZE * 3];
  8062. byte resultC[AES_BLOCK_SIZE * 3];
  8063. int result;
  8064. #ifdef WOLFSSL_SMALL_STACK
  8065. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8066. ERROR_OUT(-6118, out);
  8067. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8068. ERROR_OUT(-6119, out);
  8069. #endif
  8070. XMEMSET(resultT, 0, sizeof(resultT));
  8071. XMEMSET(resultC, 0, sizeof(resultC));
  8072. XMEMSET(resultP, 0, sizeof(resultP));
  8073. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  8074. ERROR_OUT(-6110, out);
  8075. else
  8076. enc_inited = 1;
  8077. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8078. ERROR_OUT(-6111, out);
  8079. else
  8080. dec_inited = 1;
  8081. result = wc_AesGcmSetKey(enc, key, keySz);
  8082. if (result != 0)
  8083. ERROR_OUT(-6112, out);
  8084. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8085. result = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  8086. resultT, tagSz, aad, aadSz);
  8087. #if defined(WOLFSSL_ASYNC_CRYPT)
  8088. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8089. #endif
  8090. if (result != 0)
  8091. ERROR_OUT(-6113, out);
  8092. if (cipher != NULL) {
  8093. if (XMEMCMP(cipher, resultC, cipherSz))
  8094. ERROR_OUT(-6114, out);
  8095. }
  8096. if (XMEMCMP(tag, resultT, tagSz))
  8097. ERROR_OUT(-6115, out);
  8098. #ifdef HAVE_AES_DECRYPT
  8099. result = wc_AesGcmSetKey(dec, key, keySz);
  8100. if (result != 0)
  8101. ERROR_OUT(-6116, out);
  8102. result = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  8103. iv, ivSz, resultT, tagSz, aad, aadSz);
  8104. #if defined(WOLFSSL_ASYNC_CRYPT)
  8105. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8106. #endif
  8107. if (result != 0)
  8108. ERROR_OUT(-6117, out);
  8109. if (plain != NULL) {
  8110. if (XMEMCMP(plain, resultP, plainSz))
  8111. ERROR_OUT(-6118, out);
  8112. }
  8113. #endif /* HAVE_AES_DECRYPT */
  8114. ret = 0;
  8115. out:
  8116. if (enc_inited)
  8117. wc_AesFree(enc);
  8118. if (dec_inited)
  8119. wc_AesFree(dec);
  8120. #ifdef WOLFSSL_SMALL_STACK
  8121. if (enc)
  8122. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8123. if (dec)
  8124. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8125. #endif
  8126. return ret;
  8127. }
  8128. /* tests that only use 12 byte IV and 16 or less byte AAD
  8129. * test vectors are from NIST SP 800-38D
  8130. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  8131. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void)
  8132. {
  8133. byte key1[] = {
  8134. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  8135. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  8136. };
  8137. byte iv1[] = {
  8138. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  8139. 0xe4, 0xed, 0x2f, 0x6d
  8140. };
  8141. ALIGN64 byte plain1[] = {
  8142. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  8143. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  8144. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  8145. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  8146. };
  8147. byte aad1[] = {
  8148. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  8149. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  8150. };
  8151. ALIGN64 byte cipher1[] = {
  8152. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  8153. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  8154. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  8155. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  8156. };
  8157. byte tag1[] = {
  8158. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  8159. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  8160. };
  8161. byte key2[] = {
  8162. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  8163. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  8164. };
  8165. byte iv2[] = {
  8166. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  8167. 0xc9, 0x8a, 0xff, 0xe3
  8168. };
  8169. ALIGN64 byte plain2[] = {
  8170. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  8171. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  8172. };
  8173. ALIGN64 byte cipher2[] = {
  8174. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  8175. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  8176. };
  8177. byte tag2[] = {
  8178. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  8179. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  8180. };
  8181. byte key3[] = {
  8182. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  8183. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  8184. };
  8185. byte iv3[] = {
  8186. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  8187. 0x6a, 0x4b, 0xd5, 0xe1
  8188. };
  8189. byte tag3[] = {
  8190. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  8191. 0x11, 0x64, 0xb2, 0xff
  8192. };
  8193. int ret;
  8194. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  8195. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  8196. aad1, sizeof(aad1), tag1, sizeof(tag1));
  8197. if (ret != 0) {
  8198. return ret;
  8199. }
  8200. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  8201. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  8202. NULL, 0, tag2, sizeof(tag2));
  8203. if (ret != 0) {
  8204. return ret;
  8205. }
  8206. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  8207. NULL, 0, NULL, 0,
  8208. NULL, 0, tag3, sizeof(tag3));
  8209. if (ret != 0) {
  8210. return ret;
  8211. }
  8212. return 0;
  8213. }
  8214. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void)
  8215. {
  8216. #ifdef WOLFSSL_SMALL_STACK
  8217. Aes *enc = NULL;
  8218. Aes *dec = NULL;
  8219. #else
  8220. Aes enc[1];
  8221. Aes dec[1];
  8222. #endif
  8223. /*
  8224. * This is Test Case 16 from the document Galois/
  8225. * Counter Mode of Operation (GCM) by McGrew and
  8226. * Viega.
  8227. */
  8228. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  8229. {
  8230. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  8231. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  8232. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  8233. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  8234. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  8235. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  8236. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  8237. 0xba, 0x63, 0x7b, 0x39
  8238. };
  8239. #if defined(WOLFSSL_AES_256)
  8240. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  8241. {
  8242. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  8243. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  8244. 0xab, 0xad, 0xda, 0xd2
  8245. };
  8246. #endif
  8247. #ifdef WOLFSSL_AES_256
  8248. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  8249. {
  8250. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  8251. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  8252. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  8253. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  8254. };
  8255. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  8256. {
  8257. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  8258. 0xde, 0xca, 0xf8, 0x88
  8259. };
  8260. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  8261. {
  8262. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  8263. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  8264. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  8265. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  8266. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  8267. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  8268. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  8269. 0xbc, 0xc9, 0xf6, 0x62
  8270. };
  8271. #endif /* WOLFSSL_AES_256 */
  8272. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  8273. {
  8274. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  8275. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  8276. };
  8277. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  8278. #if !defined(HAVE_FIPS) && \
  8279. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  8280. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  8281. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  8282. !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  8283. !(defined(WOLF_CRYPTO_CB) && \
  8284. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  8285. #define ENABLE_NON_12BYTE_IV_TEST
  8286. #ifdef WOLFSSL_AES_192
  8287. /* Test Case 12, uses same plaintext and AAD data. */
  8288. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  8289. {
  8290. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  8291. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  8292. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  8293. };
  8294. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  8295. {
  8296. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  8297. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  8298. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  8299. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  8300. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  8301. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  8302. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  8303. 0xa6, 0x37, 0xb3, 0x9b
  8304. };
  8305. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  8306. {
  8307. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  8308. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  8309. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  8310. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  8311. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  8312. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  8313. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  8314. 0xe9, 0xb7, 0x37, 0x3b
  8315. };
  8316. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  8317. {
  8318. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  8319. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  8320. };
  8321. #endif /* WOLFSSL_AES_192 */
  8322. #ifdef WOLFSSL_AES_128
  8323. /* The following is an interesting test case from the example
  8324. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  8325. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  8326. {
  8327. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  8328. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  8329. };
  8330. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  8331. {
  8332. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  8333. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  8334. };
  8335. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  8336. {
  8337. 0xca
  8338. };
  8339. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  8340. {
  8341. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  8342. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  8343. };
  8344. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  8345. {
  8346. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  8347. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  8348. };
  8349. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  8350. {
  8351. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  8352. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  8353. };
  8354. #endif /* WOLFSSL_AES_128 */
  8355. #ifdef WOLFSSL_AES_256
  8356. int ivlen;
  8357. #endif
  8358. #endif
  8359. byte resultT[sizeof(t1)];
  8360. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  8361. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  8362. int result = 0;
  8363. int ret;
  8364. #ifdef WOLFSSL_AES_256
  8365. int alen;
  8366. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  8367. int plen;
  8368. #endif
  8369. #endif
  8370. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  8371. #if !defined(BENCH_AESGCM_LARGE)
  8372. #define BENCH_AESGCM_LARGE 1024
  8373. #endif
  8374. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8375. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8376. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8377. if ((! large_input) || (! large_output) || (! large_outdec))
  8378. ERROR_OUT(MEMORY_E, out);
  8379. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  8380. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  8381. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  8382. #endif
  8383. #ifdef WOLFSSL_SMALL_STACK
  8384. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8385. ERROR_OUT(-6342, out);
  8386. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8387. ERROR_OUT(-6343, out);
  8388. #endif
  8389. (void)result;
  8390. XMEMSET(resultT, 0, sizeof(resultT));
  8391. XMEMSET(resultC, 0, sizeof(resultC));
  8392. XMEMSET(resultP, 0, sizeof(resultP));
  8393. if (wc_AesInit(enc, HEAP_HINT, devId) != 0) {
  8394. ERROR_OUT(-6300, out);
  8395. }
  8396. if (wc_AesInit(dec, HEAP_HINT, devId) != 0) {
  8397. ERROR_OUT(-6301, out);
  8398. }
  8399. #ifdef WOLFSSL_AES_256
  8400. result = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  8401. if (result != 0)
  8402. ERROR_OUT(-6302, out);
  8403. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8404. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  8405. resultT, sizeof(resultT), a, sizeof(a));
  8406. #if defined(WOLFSSL_ASYNC_CRYPT)
  8407. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8408. #endif
  8409. if (result != 0)
  8410. ERROR_OUT(-6303, out);
  8411. if (XMEMCMP(c1, resultC, sizeof(c1)))
  8412. ERROR_OUT(-6304, out);
  8413. if (XMEMCMP(t1, resultT, sizeof(resultT)))
  8414. ERROR_OUT(-6305, out);
  8415. #ifdef HAVE_AES_DECRYPT
  8416. result = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  8417. if (result != 0)
  8418. ERROR_OUT(-6306, out);
  8419. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  8420. iv1, sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  8421. #if defined(WOLFSSL_ASYNC_CRYPT)
  8422. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8423. #endif
  8424. if (result != 0)
  8425. ERROR_OUT(-6307, out);
  8426. if (XMEMCMP(p, resultP, sizeof(p)))
  8427. ERROR_OUT(-6308, out);
  8428. #endif /* HAVE_AES_DECRYPT */
  8429. /* Large buffer test */
  8430. #ifdef BENCH_AESGCM_LARGE
  8431. /* setup test buffer */
  8432. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  8433. large_input[alen] = (byte)alen;
  8434. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8435. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  8436. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  8437. resultT, sizeof(resultT), a, sizeof(a));
  8438. #if defined(WOLFSSL_ASYNC_CRYPT)
  8439. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8440. #endif
  8441. if (result != 0)
  8442. ERROR_OUT(-6309, out);
  8443. #ifdef HAVE_AES_DECRYPT
  8444. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  8445. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  8446. sizeof(resultT), a, sizeof(a));
  8447. #if defined(WOLFSSL_ASYNC_CRYPT)
  8448. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8449. #endif
  8450. if (result != 0)
  8451. ERROR_OUT(-6310, out);
  8452. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  8453. ERROR_OUT(-6311, out);
  8454. #endif /* HAVE_AES_DECRYPT */
  8455. #endif /* BENCH_AESGCM_LARGE */
  8456. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  8457. /* Variable IV length test */
  8458. for (ivlen=1; ivlen<(int)sizeof(k1); ivlen++) {
  8459. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8460. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  8461. (word32)ivlen, resultT, sizeof(resultT), a, sizeof(a));
  8462. #if defined(WOLFSSL_ASYNC_CRYPT)
  8463. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8464. #endif
  8465. if (result != 0)
  8466. ERROR_OUT(-6312, out);
  8467. #ifdef HAVE_AES_DECRYPT
  8468. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  8469. (word32)ivlen, resultT, sizeof(resultT), a, sizeof(a));
  8470. #if defined(WOLFSSL_ASYNC_CRYPT)
  8471. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8472. #endif
  8473. if (result != 0)
  8474. ERROR_OUT(-6313, out);
  8475. #endif /* HAVE_AES_DECRYPT */
  8476. }
  8477. #endif
  8478. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  8479. /* Variable authenticated data length test */
  8480. for (alen=0; alen<(int)sizeof(p); alen++) {
  8481. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8482. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  8483. sizeof(iv1), resultT, sizeof(resultT), p, (word32)alen);
  8484. #if defined(WOLFSSL_ASYNC_CRYPT)
  8485. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8486. #endif
  8487. if (result != 0)
  8488. ERROR_OUT(-6314, out);
  8489. #ifdef HAVE_AES_DECRYPT
  8490. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  8491. sizeof(iv1), resultT, sizeof(resultT), p, (word32)alen);
  8492. #if defined(WOLFSSL_ASYNC_CRYPT)
  8493. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8494. #endif
  8495. if (result != 0)
  8496. ERROR_OUT(-6315, out);
  8497. #endif /* HAVE_AES_DECRYPT */
  8498. }
  8499. #endif
  8500. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  8501. #ifdef BENCH_AESGCM_LARGE
  8502. /* Variable plain text length test */
  8503. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  8504. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8505. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  8506. plen, iv1, sizeof(iv1), resultT,
  8507. sizeof(resultT), a, sizeof(a));
  8508. #if defined(WOLFSSL_ASYNC_CRYPT)
  8509. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8510. #endif
  8511. if (result != 0)
  8512. ERROR_OUT(-6316, out);
  8513. #ifdef HAVE_AES_DECRYPT
  8514. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  8515. plen, iv1, sizeof(iv1), resultT,
  8516. sizeof(resultT), a, sizeof(a));
  8517. #if defined(WOLFSSL_ASYNC_CRYPT)
  8518. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8519. #endif
  8520. if (result != 0)
  8521. ERROR_OUT(-6317, out);
  8522. #endif /* HAVE_AES_DECRYPT */
  8523. }
  8524. #else /* BENCH_AESGCM_LARGE */
  8525. /* Variable plain text length test */
  8526. for (plen=1; plen<(int)sizeof(p); plen++) {
  8527. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8528. result = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  8529. sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  8530. #if defined(WOLFSSL_ASYNC_CRYPT)
  8531. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8532. #endif
  8533. if (result != 0)
  8534. ERROR_OUT(-6318, out);
  8535. #ifdef HAVE_AES_DECRYPT
  8536. result = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  8537. sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  8538. #if defined(WOLFSSL_ASYNC_CRYPT)
  8539. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8540. #endif
  8541. if (result != 0)
  8542. ERROR_OUT(-6319, out);
  8543. #endif /* HAVE_AES_DECRYPT */
  8544. }
  8545. #endif /* BENCH_AESGCM_LARGE */
  8546. #endif
  8547. #endif /* WOLFSSL_AES_256 */
  8548. /* test with IV != 12 bytes */
  8549. #ifdef ENABLE_NON_12BYTE_IV_TEST
  8550. XMEMSET(resultT, 0, sizeof(resultT));
  8551. XMEMSET(resultC, 0, sizeof(resultC));
  8552. XMEMSET(resultP, 0, sizeof(resultP));
  8553. #ifdef WOLFSSL_AES_192
  8554. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  8555. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8556. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  8557. resultT, sizeof(resultT), a, sizeof(a));
  8558. #if defined(WOLFSSL_ASYNC_CRYPT)
  8559. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8560. #endif
  8561. if (result != 0)
  8562. ERROR_OUT(-6320, out);
  8563. if (XMEMCMP(c2, resultC, sizeof(c2)))
  8564. ERROR_OUT(-6321, out);
  8565. if (XMEMCMP(t2, resultT, sizeof(resultT)))
  8566. ERROR_OUT(-6322, out);
  8567. #ifdef HAVE_AES_DECRYPT
  8568. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  8569. iv2, sizeof(iv2), resultT, sizeof(resultT), a, sizeof(a));
  8570. #if defined(WOLFSSL_ASYNC_CRYPT)
  8571. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8572. #endif
  8573. if (result != 0)
  8574. ERROR_OUT(-6323, out);
  8575. if (XMEMCMP(p, resultP, sizeof(p)))
  8576. ERROR_OUT(-6324, out);
  8577. #endif /* HAVE_AES_DECRYPT */
  8578. XMEMSET(resultT, 0, sizeof(resultT));
  8579. XMEMSET(resultC, 0, sizeof(resultC));
  8580. XMEMSET(resultP, 0, sizeof(resultP));
  8581. #endif /* WOLFSSL_AES_192 */
  8582. #ifdef WOLFSSL_AES_128
  8583. wc_AesGcmSetKey(enc, k3, sizeof(k3));
  8584. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8585. result = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  8586. resultT, sizeof(t3), a3, sizeof(a3));
  8587. #if defined(WOLFSSL_ASYNC_CRYPT)
  8588. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8589. #endif
  8590. if (result != 0)
  8591. ERROR_OUT(-6325, out);
  8592. if (XMEMCMP(c3, resultC, sizeof(c3)))
  8593. ERROR_OUT(-6326, out);
  8594. if (XMEMCMP(t3, resultT, sizeof(t3)))
  8595. ERROR_OUT(-6327, out);
  8596. #ifdef HAVE_AES_DECRYPT
  8597. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  8598. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  8599. #if defined(WOLFSSL_ASYNC_CRYPT)
  8600. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8601. #endif
  8602. if (result != 0)
  8603. ERROR_OUT(-6328, out);
  8604. if (XMEMCMP(p3, resultP, sizeof(p3)))
  8605. ERROR_OUT(-6329, out);
  8606. #endif /* HAVE_AES_DECRYPT */
  8607. #endif /* WOLFSSL_AES_128 */
  8608. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  8609. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  8610. !defined(WOLFSSL_XILINX_CRYPT) && \
  8611. !(defined(WOLF_CRYPTO_CB) && \
  8612. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  8613. XMEMSET(resultT, 0, sizeof(resultT));
  8614. XMEMSET(resultC, 0, sizeof(resultC));
  8615. XMEMSET(resultP, 0, sizeof(resultP));
  8616. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  8617. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8618. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  8619. resultT + 1, sizeof(resultT) - 1, a, sizeof(a));
  8620. #if defined(WOLFSSL_ASYNC_CRYPT)
  8621. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8622. #endif
  8623. if (result != 0)
  8624. ERROR_OUT(-6330, out);
  8625. if (XMEMCMP(c1, resultC, sizeof(c1)))
  8626. ERROR_OUT(-6331, out);
  8627. if (XMEMCMP(t1, resultT + 1, sizeof(resultT) - 1))
  8628. ERROR_OUT(-6332, out);
  8629. #ifdef HAVE_AES_DECRYPT
  8630. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  8631. iv1, sizeof(iv1), resultT + 1, sizeof(resultT) - 1, a, sizeof(a));
  8632. #if defined(WOLFSSL_ASYNC_CRYPT)
  8633. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8634. #endif
  8635. if (result != 0)
  8636. ERROR_OUT(-6333, out);
  8637. if (XMEMCMP(p, resultP, sizeof(p)))
  8638. ERROR_OUT(-6334, out);
  8639. #endif /* HAVE_AES_DECRYPT */
  8640. #endif /* WOLFSSL_AES_256 */
  8641. #if !defined(HAVE_FIPS) || \
  8642. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  8643. /* Test encrypt with internally generated IV */
  8644. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  8645. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  8646. {
  8647. WC_RNG rng;
  8648. byte randIV[12];
  8649. result = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  8650. if (result != 0)
  8651. ERROR_OUT(-6335, out);
  8652. XMEMSET(randIV, 0, sizeof(randIV));
  8653. XMEMSET(resultT, 0, sizeof(resultT));
  8654. XMEMSET(resultC, 0, sizeof(resultC));
  8655. XMEMSET(resultP, 0, sizeof(resultP));
  8656. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  8657. result = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  8658. if (result != 0)
  8659. ERROR_OUT(-6336, out);
  8660. result = wc_AesGcmEncrypt_ex(enc,
  8661. resultC, p, sizeof(p),
  8662. randIV, sizeof(randIV),
  8663. resultT, sizeof(resultT),
  8664. a, sizeof(a));
  8665. #if defined(WOLFSSL_ASYNC_CRYPT)
  8666. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8667. #endif
  8668. if (result != 0)
  8669. ERROR_OUT(-6337, out);
  8670. /* Check the IV has been set. */
  8671. {
  8672. word32 i, ivSum = 0;
  8673. for (i = 0; i < sizeof(randIV); i++)
  8674. ivSum += randIV[i];
  8675. if (ivSum == 0)
  8676. ERROR_OUT(-6338, out);
  8677. }
  8678. #ifdef HAVE_AES_DECRYPT
  8679. wc_AesGcmSetKey(dec, k1, sizeof(k1));
  8680. result = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  8681. if (result != 0)
  8682. ERROR_OUT(-6339, out);
  8683. result = wc_AesGcmDecrypt(dec,
  8684. resultP, resultC, sizeof(c1),
  8685. randIV, sizeof(randIV),
  8686. resultT, sizeof(resultT),
  8687. a, sizeof(a));
  8688. #if defined(WOLFSSL_ASYNC_CRYPT)
  8689. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8690. #endif
  8691. if (result != 0)
  8692. ERROR_OUT(-6340, out);
  8693. if (XMEMCMP(p, resultP, sizeof(p)))
  8694. ERROR_OUT(-6341, out);
  8695. #endif /* HAVE_AES_DECRYPT */
  8696. wc_FreeRng(&rng);
  8697. }
  8698. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  8699. #endif /* HAVE_FIPS_VERSION >= 2 */
  8700. wc_AesFree(enc);
  8701. wc_AesFree(dec);
  8702. ret = 0;
  8703. out:
  8704. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  8705. if (large_input)
  8706. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8707. if (large_output)
  8708. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8709. if (large_outdec)
  8710. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8711. #endif
  8712. #ifdef WOLFSSL_SMALL_STACK
  8713. if (enc)
  8714. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8715. if (dec)
  8716. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8717. #endif
  8718. return ret;
  8719. }
  8720. #ifdef WOLFSSL_AES_128
  8721. WOLFSSL_TEST_SUBROUTINE int gmac_test(void)
  8722. {
  8723. int ret;
  8724. #ifdef WOLFSSL_SMALL_STACK
  8725. Gmac *gmac;
  8726. #else
  8727. Gmac gmac[1];
  8728. #endif
  8729. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  8730. {
  8731. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  8732. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  8733. };
  8734. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  8735. {
  8736. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  8737. 0xe2, 0x8c, 0x8f, 0x16
  8738. };
  8739. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  8740. {
  8741. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  8742. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  8743. };
  8744. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  8745. {
  8746. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  8747. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  8748. };
  8749. #if (!defined(HAVE_FIPS) || \
  8750. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  8751. /* FIPS builds only allow 16-byte auth tags. */
  8752. /* This sample uses a 15-byte auth tag. */
  8753. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  8754. {
  8755. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  8756. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  8757. };
  8758. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  8759. {
  8760. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  8761. 0x1a, 0x60, 0x24, 0xa7
  8762. };
  8763. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  8764. {
  8765. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  8766. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  8767. };
  8768. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  8769. {
  8770. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  8771. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  8772. };
  8773. #endif
  8774. byte tag[16];
  8775. #ifdef WOLFSSL_SMALL_STACK
  8776. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8777. return -6409;
  8778. #endif
  8779. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  8780. (void)wc_AesInit((Aes*)gmac, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  8781. XMEMSET(tag, 0, sizeof(tag));
  8782. wc_GmacSetKey(gmac, k1, sizeof(k1));
  8783. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  8784. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  8785. ERROR_OUT(-6400, out);
  8786. #if (!defined(HAVE_FIPS) || \
  8787. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  8788. XMEMSET(tag, 0, sizeof(tag));
  8789. wc_GmacSetKey(gmac, k2, sizeof(k2));
  8790. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  8791. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  8792. ERROR_OUT(-6401, out);
  8793. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  8794. {
  8795. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  8796. {
  8797. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  8798. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  8799. };
  8800. WC_RNG rng;
  8801. byte iv[12];
  8802. #ifndef HAVE_FIPS
  8803. if (wc_InitRng_ex(&rng, HEAP_HINT, devId) != 0)
  8804. ERROR_OUT(-6402, out);
  8805. #else
  8806. if (wc_InitRng(&rng) != 0)
  8807. ERROR_OUT(-6403, out);
  8808. #endif
  8809. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  8810. t1, sizeof(t1)) != 0)
  8811. ERROR_OUT(-6404, out);
  8812. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  8813. badT, sizeof(badT)) != AES_GCM_AUTH_E)
  8814. ERROR_OUT(-6405, out);
  8815. if (wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  8816. t2, sizeof(t2)) != 0)
  8817. ERROR_OUT(-6406, out);
  8818. XMEMSET(tag, 0, sizeof(tag));
  8819. XMEMSET(iv, 0, sizeof(iv));
  8820. if (wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  8821. tag, sizeof(tag), &rng) != 0)
  8822. ERROR_OUT(-6407, out);
  8823. if (wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  8824. tag, sizeof(tag)) != 0)
  8825. ERROR_OUT(-6408, out);
  8826. wc_FreeRng(&rng);
  8827. }
  8828. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  8829. #endif /* HAVE_FIPS */
  8830. ret = 0;
  8831. out:
  8832. #ifdef WOLFSSL_SMALL_STACK
  8833. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  8834. #endif
  8835. return ret;
  8836. }
  8837. #endif /* WOLFSSL_AES_128 */
  8838. #endif /* HAVE_AESGCM */
  8839. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  8840. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void)
  8841. {
  8842. int ret;
  8843. #ifdef WOLFSSL_SMALL_STACK
  8844. Aes *enc;
  8845. #else
  8846. Aes enc[1];
  8847. #endif
  8848. /* key */
  8849. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  8850. {
  8851. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  8852. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  8853. };
  8854. /* nonce */
  8855. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  8856. {
  8857. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  8858. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  8859. };
  8860. /* plaintext */
  8861. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  8862. {
  8863. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  8864. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  8865. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  8866. };
  8867. /* plaintext - long */
  8868. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  8869. {
  8870. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  8871. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  8872. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  8873. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  8874. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  8875. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8876. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  8877. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  8878. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  8879. 0x50
  8880. };
  8881. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  8882. {
  8883. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  8884. };
  8885. /* ciphertext */
  8886. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  8887. {
  8888. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  8889. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  8890. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  8891. };
  8892. /* tag - authentication */
  8893. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  8894. {
  8895. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  8896. };
  8897. /* ciphertext - long */
  8898. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  8899. {
  8900. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  8901. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  8902. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  8903. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  8904. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  8905. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  8906. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  8907. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  8908. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  8909. 0x0b
  8910. };
  8911. /* tag - authentication - long */
  8912. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  8913. {
  8914. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  8915. };
  8916. byte t2[sizeof(t)];
  8917. byte p2[sizeof(p)];
  8918. byte c2[sizeof(c)];
  8919. byte iv2[sizeof(iv)];
  8920. byte pl2[sizeof(pl)];
  8921. byte cl2[sizeof(cl)];
  8922. byte tl2[sizeof(tl)];
  8923. int result;
  8924. #ifdef WOLFSSL_SMALL_STACK
  8925. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8926. return -6521;
  8927. #endif
  8928. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  8929. XMEMSET(t2, 0, sizeof(t2));
  8930. XMEMSET(c2, 0, sizeof(c2));
  8931. XMEMSET(p2, 0, sizeof(p2));
  8932. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  8933. if (result != 0)
  8934. ERROR_OUT(-6500, out);
  8935. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  8936. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  8937. t2, sizeof(t2), a, sizeof(a));
  8938. if (result != 0)
  8939. ERROR_OUT(-6501, out);
  8940. if (XMEMCMP(c, c2, sizeof(c2)))
  8941. ERROR_OUT(-6502, out);
  8942. if (XMEMCMP(t, t2, sizeof(t2)))
  8943. ERROR_OUT(-6503, out);
  8944. result = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  8945. t2, sizeof(t2), a, sizeof(a));
  8946. if (result != 0)
  8947. ERROR_OUT(-6504, out);
  8948. if (XMEMCMP(p, p2, sizeof(p2)))
  8949. ERROR_OUT(-6505, out);
  8950. /* Test the authentication failure */
  8951. t2[0]++; /* Corrupt the authentication tag. */
  8952. result = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  8953. t2, sizeof(t2), a, sizeof(a));
  8954. if (result == 0)
  8955. ERROR_OUT(-6506, out);
  8956. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  8957. * authentication fail. */
  8958. XMEMSET(c2, 0, sizeof(c2));
  8959. if (XMEMCMP(p2, c2, sizeof(p2)))
  8960. ERROR_OUT(-6507, out);
  8961. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  8962. XMEMSET(t2, 0, sizeof(t2));
  8963. XMEMSET(c2, 0, sizeof(c2));
  8964. XMEMSET(p2, 0, sizeof(p2));
  8965. XMEMSET(iv2, 0, sizeof(iv2));
  8966. #ifndef HAVE_SELFTEST
  8967. /* selftest build does not have wc_AesCcmSetNonce() or
  8968. * wc_AesCcmEncrypt_ex() */
  8969. if (wc_AesCcmSetKey(enc, k, sizeof(k)) != 0)
  8970. ERROR_OUT(-6508, out);
  8971. if (wc_AesCcmSetNonce(enc, iv, sizeof(iv)) != 0)
  8972. ERROR_OUT(-6509, out);
  8973. if (wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  8974. t2, sizeof(t2), a, sizeof(a)) != 0)
  8975. ERROR_OUT(-6510, out);
  8976. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  8977. ERROR_OUT(-6511, out);
  8978. if (XMEMCMP(c, c2, sizeof(c2)))
  8979. ERROR_OUT(-6512, out);
  8980. if (XMEMCMP(t, t2, sizeof(t2)))
  8981. ERROR_OUT(-6513, out);
  8982. #endif
  8983. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8984. /* test fail on invalid IV sizes */
  8985. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  8986. if (result != 0)
  8987. ERROR_OUT(-6514, out);
  8988. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  8989. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  8990. t2, 1, a, sizeof(a));
  8991. if (result == 0) {
  8992. ERROR_OUT(-6515, out);
  8993. }
  8994. #endif
  8995. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  8996. result = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  8997. tl2, sizeof(tl2), a, sizeof(a));
  8998. if (result != 0)
  8999. ERROR_OUT(-6516, out);
  9000. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  9001. ERROR_OUT(-6517, out);
  9002. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  9003. ERROR_OUT(-6518, out);
  9004. result = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  9005. tl2, sizeof(tl2), a, sizeof(a));
  9006. if (result != 0)
  9007. ERROR_OUT(-6519, out);
  9008. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  9009. ERROR_OUT(-6520, out);
  9010. ret = 0;
  9011. out:
  9012. #ifdef WOLFSSL_SMALL_STACK
  9013. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9014. #endif
  9015. return ret;
  9016. }
  9017. #endif /* HAVE_AESCCM WOLFSSL_AES_128 */
  9018. #ifdef HAVE_AES_KEYWRAP
  9019. #define MAX_KEYWRAP_TEST_OUTLEN 40
  9020. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  9021. typedef struct keywrapVector {
  9022. const byte* kek;
  9023. const byte* data;
  9024. const byte* verify;
  9025. word32 kekLen;
  9026. word32 dataLen;
  9027. word32 verifyLen;
  9028. } keywrapVector;
  9029. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void)
  9030. {
  9031. int wrapSz, plainSz, testSz, i;
  9032. /* test vectors from RFC 3394 (kek, data, verify) */
  9033. #ifdef WOLFSSL_AES_128
  9034. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  9035. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  9036. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9037. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  9038. };
  9039. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  9040. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9041. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  9042. };
  9043. WOLFSSL_SMALL_STACK_STATIC const byte v1[] = {
  9044. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  9045. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  9046. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  9047. };
  9048. #endif /* WOLFSSL_AES_128 */
  9049. #ifdef WOLFSSL_AES_192
  9050. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  9051. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  9052. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9053. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9054. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  9055. };
  9056. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  9057. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9058. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  9059. };
  9060. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  9061. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  9062. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  9063. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  9064. };
  9065. #endif
  9066. #ifdef WOLFSSL_AES_256
  9067. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  9068. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  9069. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9070. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9071. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  9072. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  9073. };
  9074. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  9075. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9076. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  9077. };
  9078. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  9079. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  9080. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  9081. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  9082. };
  9083. #endif
  9084. #ifdef WOLFSSL_AES_192
  9085. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  9086. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  9087. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9088. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9089. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  9090. };
  9091. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  9092. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9093. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  9094. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  9095. };
  9096. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  9097. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  9098. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  9099. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  9100. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  9101. };
  9102. #endif
  9103. #ifdef WOLFSSL_AES_256
  9104. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  9105. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  9106. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9107. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9108. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  9109. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  9110. };
  9111. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  9112. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9113. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  9114. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  9115. };
  9116. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  9117. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  9118. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  9119. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  9120. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  9121. };
  9122. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  9123. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  9124. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9125. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9126. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  9127. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  9128. };
  9129. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  9130. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9131. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  9132. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9133. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  9134. };
  9135. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  9136. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  9137. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  9138. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  9139. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  9140. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  9141. };
  9142. #endif /* WOLFSSL_AES_256 */
  9143. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  9144. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  9145. const keywrapVector test_wrap[] =
  9146. {
  9147. #ifdef WOLFSSL_AES_128
  9148. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  9149. #endif
  9150. #ifdef WOLFSSL_AES_192
  9151. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  9152. #endif
  9153. #ifdef WOLFSSL_AES_256
  9154. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  9155. #endif
  9156. #ifdef WOLFSSL_AES_192
  9157. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  9158. #endif
  9159. #ifdef WOLFSSL_AES_256
  9160. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  9161. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  9162. #endif
  9163. };
  9164. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  9165. XMEMSET(output, 0, sizeof(output));
  9166. XMEMSET(plain, 0, sizeof(plain));
  9167. for (i = 0; i < testSz; i++) {
  9168. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  9169. test_wrap[i].data, test_wrap[i].dataLen,
  9170. output, sizeof(output), NULL);
  9171. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  9172. return -6600;
  9173. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  9174. return -6601;
  9175. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  9176. output, wrapSz,
  9177. plain, sizeof(plain), NULL);
  9178. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  9179. return -6602;
  9180. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  9181. return -6603 - i;
  9182. }
  9183. return 0;
  9184. }
  9185. #endif /* HAVE_AES_KEYWRAP */
  9186. #endif /* NO_AES */
  9187. #ifdef HAVE_CAMELLIA
  9188. enum {
  9189. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  9190. };
  9191. typedef struct {
  9192. int type;
  9193. const byte* plaintext;
  9194. const byte* iv;
  9195. const byte* ciphertext;
  9196. const byte* key;
  9197. word32 keySz;
  9198. int errorCode;
  9199. } test_vector_t;
  9200. WOLFSSL_TEST_SUBROUTINE int camellia_test(void)
  9201. {
  9202. /* Camellia ECB Test Plaintext */
  9203. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  9204. {
  9205. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  9206. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  9207. };
  9208. /* Camellia ECB Test Initialization Vector */
  9209. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  9210. /* Test 1: Camellia ECB 128-bit key */
  9211. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  9212. {
  9213. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  9214. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  9215. };
  9216. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  9217. {
  9218. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  9219. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  9220. };
  9221. /* Test 2: Camellia ECB 192-bit key */
  9222. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  9223. {
  9224. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  9225. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  9226. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  9227. };
  9228. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  9229. {
  9230. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  9231. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  9232. };
  9233. /* Test 3: Camellia ECB 256-bit key */
  9234. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  9235. {
  9236. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  9237. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  9238. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9239. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  9240. };
  9241. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  9242. {
  9243. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  9244. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  9245. };
  9246. /* Camellia CBC Test Plaintext */
  9247. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  9248. {
  9249. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  9250. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  9251. };
  9252. /* Camellia CBC Test Initialization Vector */
  9253. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  9254. {
  9255. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9256. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  9257. };
  9258. /* Test 4: Camellia-CBC 128-bit key */
  9259. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  9260. {
  9261. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  9262. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  9263. };
  9264. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  9265. {
  9266. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  9267. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  9268. };
  9269. /* Test 5: Camellia-CBC 192-bit key */
  9270. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  9271. {
  9272. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  9273. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  9274. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  9275. };
  9276. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  9277. {
  9278. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  9279. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  9280. };
  9281. /* Test 6: CBC 256-bit key */
  9282. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  9283. {
  9284. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  9285. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  9286. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  9287. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  9288. };
  9289. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  9290. {
  9291. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  9292. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  9293. };
  9294. byte out[CAMELLIA_BLOCK_SIZE];
  9295. Camellia cam;
  9296. int i, testsSz, ret;
  9297. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  9298. {
  9299. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  9300. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  9301. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  9302. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  9303. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  9304. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  9305. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  9306. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  9307. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  9308. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  9309. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  9310. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  9311. };
  9312. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  9313. for (i = 0; i < testsSz; i++) {
  9314. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  9315. testVectors[i].iv) != 0)
  9316. return testVectors[i].errorCode;
  9317. switch (testVectors[i].type) {
  9318. case CAM_ECB_ENC:
  9319. ret = wc_CamelliaEncryptDirect(&cam, out,
  9320. testVectors[i].plaintext);
  9321. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  9322. CAMELLIA_BLOCK_SIZE))
  9323. return testVectors[i].errorCode;
  9324. break;
  9325. case CAM_ECB_DEC:
  9326. ret = wc_CamelliaDecryptDirect(&cam, out,
  9327. testVectors[i].ciphertext);
  9328. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  9329. CAMELLIA_BLOCK_SIZE))
  9330. return testVectors[i].errorCode;
  9331. break;
  9332. case CAM_CBC_ENC:
  9333. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  9334. CAMELLIA_BLOCK_SIZE);
  9335. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  9336. CAMELLIA_BLOCK_SIZE))
  9337. return testVectors[i].errorCode;
  9338. break;
  9339. case CAM_CBC_DEC:
  9340. ret = wc_CamelliaCbcDecrypt(&cam, out,
  9341. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  9342. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  9343. CAMELLIA_BLOCK_SIZE))
  9344. return testVectors[i].errorCode;
  9345. break;
  9346. default:
  9347. break;
  9348. }
  9349. }
  9350. /* Setting the IV and checking it was actually set. */
  9351. ret = wc_CamelliaSetIV(&cam, ivc);
  9352. if (ret != 0 || XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
  9353. return -6700;
  9354. /* Setting the IV to NULL should be same as all zeros IV */
  9355. if (wc_CamelliaSetIV(&cam, NULL) != 0 ||
  9356. XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE))
  9357. return -6701;
  9358. /* First parameter should never be null */
  9359. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  9360. return -6702;
  9361. /* First parameter should never be null, check it fails */
  9362. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  9363. return -6703;
  9364. /* Key should have a size of 16, 24, or 32 */
  9365. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  9366. return -6704;
  9367. return 0;
  9368. }
  9369. #endif /* HAVE_CAMELLIA */
  9370. #ifdef HAVE_IDEA
  9371. WOLFSSL_TEST_SUBROUTINE int idea_test(void)
  9372. {
  9373. int ret;
  9374. word16 i, j;
  9375. Idea idea;
  9376. byte data[IDEA_BLOCK_SIZE];
  9377. /* Project NESSIE test vectors */
  9378. #define IDEA_NB_TESTS 6
  9379. #define IDEA_NB_TESTS_EXTRA 4
  9380. WOLFSSL_SMALL_STACK_STATIC const byte v_key[IDEA_NB_TESTS][IDEA_KEY_SIZE] = {
  9381. { 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
  9382. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37 },
  9383. { 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57,
  9384. 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57 },
  9385. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9386. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F },
  9387. { 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  9388. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48 },
  9389. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9390. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F },
  9391. { 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  9392. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48 },
  9393. };
  9394. WOLFSSL_SMALL_STACK_STATIC const byte v1_plain[IDEA_NB_TESTS][IDEA_BLOCK_SIZE] = {
  9395. { 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37 },
  9396. { 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57 },
  9397. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 },
  9398. { 0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84 },
  9399. { 0xDB, 0x2D, 0x4A, 0x92, 0xAA, 0x68, 0x27, 0x3F },
  9400. { 0xF1, 0x29, 0xA6, 0x60, 0x1E, 0xF6, 0x2A, 0x47 },
  9401. };
  9402. WOLFSSL_SMALL_STACK_STATIC const byte v1_cipher[IDEA_NB_TESTS][IDEA_BLOCK_SIZE] = {
  9403. { 0x54, 0xCF, 0x21, 0xE3, 0x89, 0xD8, 0x73, 0xEC },
  9404. { 0x85, 0x52, 0x4D, 0x41, 0x0E, 0xB4, 0x28, 0xAE },
  9405. { 0xF5, 0x26, 0xAB, 0x9A, 0x62, 0xC0, 0xD2, 0x58 },
  9406. { 0xC8, 0xFB, 0x51, 0xD3, 0x51, 0x66, 0x27, 0xA8 },
  9407. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 },
  9408. { 0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84 },
  9409. };
  9410. WOLFSSL_SMALL_STACK_STATIC const byte v1_cipher_100[IDEA_NB_TESTS_EXTRA][IDEA_BLOCK_SIZE] = {
  9411. { 0x12, 0x46, 0x2F, 0xD0, 0xFB, 0x3A, 0x63, 0x39 },
  9412. { 0x15, 0x61, 0xE8, 0xC9, 0x04, 0x54, 0x8B, 0xE9 },
  9413. { 0x42, 0x12, 0x2A, 0x94, 0xB0, 0xF6, 0xD2, 0x43 },
  9414. { 0x53, 0x4D, 0xCD, 0x48, 0xDD, 0xD5, 0xF5, 0x9C },
  9415. };
  9416. WOLFSSL_SMALL_STACK_STATIC const byte v1_cipher_1000[IDEA_NB_TESTS_EXTRA][IDEA_BLOCK_SIZE] = {
  9417. { 0x44, 0x1B, 0x38, 0x5C, 0x77, 0x29, 0x75, 0x34 },
  9418. { 0xF0, 0x4E, 0x58, 0x88, 0x44, 0x99, 0x22, 0x2D },
  9419. { 0xB3, 0x5F, 0x93, 0x7F, 0x6A, 0xA0, 0xCD, 0x1F },
  9420. { 0x9A, 0xEA, 0x46, 0x8F, 0x42, 0x9B, 0xBA, 0x15 },
  9421. };
  9422. /* CBC test */
  9423. const char *message = "International Data Encryption Algorithm";
  9424. byte msg_enc[40], msg_dec[40];
  9425. for (i = 0; i < IDEA_NB_TESTS; i++) {
  9426. /* Set encryption key */
  9427. XMEMSET(&idea, 0, sizeof(Idea));
  9428. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9429. NULL, IDEA_ENCRYPTION);
  9430. if (ret != 0) {
  9431. printf("wc_IdeaSetKey (enc) failed\n");
  9432. return -6800;
  9433. }
  9434. /* Data encryption */
  9435. ret = wc_IdeaCipher(&idea, data, v1_plain[i]);
  9436. if (ret != 0 || XMEMCMP(&v1_cipher[i], data, IDEA_BLOCK_SIZE)) {
  9437. printf("Bad encryption\n");
  9438. return -6801;
  9439. }
  9440. /* Set decryption key */
  9441. XMEMSET(&idea, 0, sizeof(Idea));
  9442. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9443. NULL, IDEA_DECRYPTION);
  9444. if (ret != 0) {
  9445. printf("wc_IdeaSetKey (dec) failed\n");
  9446. return -6802;
  9447. }
  9448. /* Data decryption */
  9449. ret = wc_IdeaCipher(&idea, data, data);
  9450. if (ret != 0 || XMEMCMP(v1_plain[i], data, IDEA_BLOCK_SIZE)) {
  9451. printf("Bad decryption\n");
  9452. return -6803;
  9453. }
  9454. /* Set encryption key */
  9455. XMEMSET(&idea, 0, sizeof(Idea));
  9456. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9457. v_key[i], IDEA_ENCRYPTION);
  9458. if (ret != 0) {
  9459. printf("wc_IdeaSetKey (enc) failed\n");
  9460. return -6804;
  9461. }
  9462. XMEMSET(msg_enc, 0, sizeof(msg_enc));
  9463. ret = wc_IdeaCbcEncrypt(&idea, msg_enc, (byte *)message,
  9464. (word32)XSTRLEN(message)+1);
  9465. if (ret != 0) {
  9466. printf("wc_IdeaCbcEncrypt failed\n");
  9467. return -6805;
  9468. }
  9469. /* Set decryption key */
  9470. XMEMSET(&idea, 0, sizeof(Idea));
  9471. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9472. v_key[i], IDEA_DECRYPTION);
  9473. if (ret != 0) {
  9474. printf("wc_IdeaSetKey (dec) failed\n");
  9475. return -6806;
  9476. }
  9477. XMEMSET(msg_dec, 0, sizeof(msg_dec));
  9478. ret = wc_IdeaCbcDecrypt(&idea, msg_dec, msg_enc,
  9479. (word32)XSTRLEN(message)+1);
  9480. if (ret != 0) {
  9481. printf("wc_IdeaCbcDecrypt failed\n");
  9482. return -6807;
  9483. }
  9484. if (XMEMCMP(message, msg_dec, (word32)XSTRLEN(message))) {
  9485. printf("Bad CBC decryption\n");
  9486. return -6808;
  9487. }
  9488. }
  9489. for (i = 0; i < IDEA_NB_TESTS_EXTRA; i++) {
  9490. /* Set encryption key */
  9491. XMEMSET(&idea, 0, sizeof(Idea));
  9492. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9493. NULL, IDEA_ENCRYPTION);
  9494. if (ret != 0) {
  9495. printf("wc_IdeaSetKey (enc) failed\n");
  9496. return -6809;
  9497. }
  9498. /* 100 times data encryption */
  9499. XMEMCPY(data, v1_plain[i], IDEA_BLOCK_SIZE);
  9500. for (j = 0; j < 100; j++) {
  9501. ret = wc_IdeaCipher(&idea, data, data);
  9502. if (ret != 0) {
  9503. return -6810;
  9504. }
  9505. }
  9506. if (XMEMCMP(v1_cipher_100[i], data, IDEA_BLOCK_SIZE)) {
  9507. printf("Bad encryption (100 times)\n");
  9508. return -6811;
  9509. }
  9510. /* 1000 times data encryption */
  9511. XMEMCPY(data, v1_plain[i], IDEA_BLOCK_SIZE);
  9512. for (j = 0; j < 1000; j++) {
  9513. ret = wc_IdeaCipher(&idea, data, data);
  9514. if (ret != 0) {
  9515. return -6812;
  9516. }
  9517. }
  9518. if (XMEMCMP(v1_cipher_1000[i], data, IDEA_BLOCK_SIZE)) {
  9519. printf("Bad encryption (100 times)\n");
  9520. return -6813;
  9521. }
  9522. }
  9523. #ifndef WC_NO_RNG
  9524. /* random test for CBC */
  9525. {
  9526. WC_RNG rng;
  9527. byte key[IDEA_KEY_SIZE], iv[IDEA_BLOCK_SIZE],
  9528. rnd[1000], enc[1000], dec[1000];
  9529. /* random values */
  9530. #ifndef HAVE_FIPS
  9531. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  9532. #else
  9533. ret = wc_InitRng(&rng);
  9534. #endif
  9535. if (ret != 0)
  9536. return -6814;
  9537. for (i = 0; i < 1000; i++) {
  9538. /* random key */
  9539. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  9540. if (ret != 0)
  9541. return -6815;
  9542. /* random iv */
  9543. ret = wc_RNG_GenerateBlock(&rng, iv, sizeof(iv));
  9544. if (ret != 0)
  9545. return -6816;
  9546. /* random data */
  9547. ret = wc_RNG_GenerateBlock(&rng, rnd, sizeof(rnd));
  9548. if (ret != 0)
  9549. return -6817;
  9550. /* Set encryption key */
  9551. XMEMSET(&idea, 0, sizeof(Idea));
  9552. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, iv, IDEA_ENCRYPTION);
  9553. if (ret != 0) {
  9554. printf("wc_IdeaSetKey (enc) failed\n");
  9555. return -6818;
  9556. }
  9557. /* Data encryption */
  9558. XMEMSET(enc, 0, sizeof(enc));
  9559. ret = wc_IdeaCbcEncrypt(&idea, enc, rnd, sizeof(rnd));
  9560. if (ret != 0) {
  9561. printf("wc_IdeaCbcEncrypt failed\n");
  9562. return -6819;
  9563. }
  9564. /* Set decryption key */
  9565. XMEMSET(&idea, 0, sizeof(Idea));
  9566. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, iv, IDEA_DECRYPTION);
  9567. if (ret != 0) {
  9568. printf("wc_IdeaSetKey (enc) failed\n");
  9569. return -6820;
  9570. }
  9571. /* Data decryption */
  9572. XMEMSET(dec, 0, sizeof(dec));
  9573. ret = wc_IdeaCbcDecrypt(&idea, dec, enc, sizeof(enc));
  9574. if (ret != 0) {
  9575. printf("wc_IdeaCbcDecrypt failed\n");
  9576. return -6821;
  9577. }
  9578. if (XMEMCMP(rnd, dec, sizeof(rnd))) {
  9579. printf("Bad CBC decryption\n");
  9580. return -6822;
  9581. }
  9582. }
  9583. wc_FreeRng(&rng);
  9584. }
  9585. #endif /* WC_NO_RNG */
  9586. return 0;
  9587. }
  9588. #endif /* HAVE_IDEA */
  9589. #ifdef HAVE_XCHACHA
  9590. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void) {
  9591. int ret = -6830;
  9592. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  9593. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  9594. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  9595. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  9596. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  9597. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  9598. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  9599. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  9600. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  9601. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  9602. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  9603. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  9604. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  9605. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  9606. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  9607. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  9608. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  9609. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  9610. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  9611. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  9612. };
  9613. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  9614. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  9615. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  9616. };
  9617. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  9618. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  9619. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  9620. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  9621. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  9622. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  9623. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  9624. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  9625. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  9626. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  9627. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  9628. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  9629. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  9630. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  9631. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  9632. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  9633. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  9634. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  9635. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  9636. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  9637. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  9638. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  9639. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  9640. };
  9641. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9642. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  9643. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9644. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9645. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  9646. ERROR_OUT(MEMORY_E, out);
  9647. #else
  9648. struct ChaCha chacha[1];
  9649. byte buf1[sizeof Plaintext];
  9650. byte buf2[sizeof Plaintext];
  9651. #endif
  9652. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  9653. if (ret < 0)
  9654. ERROR_OUT(-6831, out);
  9655. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  9656. if (ret < 0)
  9657. ERROR_OUT(-6832, out);
  9658. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  9659. ERROR_OUT(-6833, out);
  9660. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  9661. if (ret < 0)
  9662. ERROR_OUT(-6834, out);
  9663. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  9664. if (ret < 0)
  9665. ERROR_OUT(-6835, out);
  9666. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  9667. ERROR_OUT(-6836, out);
  9668. out:
  9669. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9670. if (chacha)
  9671. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  9672. if (buf1)
  9673. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9674. if (buf2)
  9675. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9676. #endif
  9677. return ret;
  9678. }
  9679. #endif /* HAVE_XCHACHA */
  9680. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  9681. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void) {
  9682. int ret;
  9683. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  9684. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  9685. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  9686. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  9687. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  9688. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  9689. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  9690. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  9691. 0x74, 0x2e }; /* t. */
  9692. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  9693. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  9694. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  9695. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  9696. };
  9697. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  9698. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  9699. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  9700. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  9701. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  9702. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  9703. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  9704. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  9705. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  9706. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  9707. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  9708. 0xb5, 0x2e };
  9709. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  9710. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  9711. };
  9712. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9713. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9714. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9715. #else
  9716. byte buf1[sizeof Ciphertext + sizeof Tag];
  9717. byte buf2[sizeof Plaintext];
  9718. #endif
  9719. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  9720. Plaintext, sizeof Plaintext,
  9721. AAD, sizeof AAD,
  9722. IV, sizeof IV,
  9723. Key, sizeof Key);
  9724. if (ret < 0)
  9725. ERROR_OUT(-6840, out);
  9726. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  9727. ERROR_OUT(-6841, out);
  9728. if (XMEMCMP(buf1 + sizeof Plaintext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  9729. ERROR_OUT(-6842, out);
  9730. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  9731. buf1, sizeof Plaintext + sizeof Tag,
  9732. AAD, sizeof AAD,
  9733. IV, sizeof IV,
  9734. Key, sizeof Key);
  9735. if (ret < 0)
  9736. ERROR_OUT(-6843, out);
  9737. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  9738. ERROR_OUT(-6844, out);
  9739. out:
  9740. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9741. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9742. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9743. #endif
  9744. return ret;
  9745. }
  9746. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  9747. #ifndef WC_NO_RNG
  9748. static int _rng_test(WC_RNG* rng, int errorOffset)
  9749. {
  9750. byte block[32];
  9751. int ret, i;
  9752. XMEMSET(block, 0, sizeof(block));
  9753. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  9754. if (ret != 0) {
  9755. ret = -6850;
  9756. goto exit;
  9757. }
  9758. /* Check for 0's */
  9759. for (i=0; i<(int)sizeof(block); i++) {
  9760. if (block[i] == 0) {
  9761. ret++;
  9762. }
  9763. }
  9764. /* All zeros count check */
  9765. if (ret >= (int)sizeof(block)) {
  9766. ret = -6851;
  9767. goto exit;
  9768. }
  9769. ret = wc_RNG_GenerateByte(rng, block);
  9770. if (ret != 0) {
  9771. ret = -6852;
  9772. goto exit;
  9773. }
  9774. /* Parameter validation testing. */
  9775. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  9776. if (ret != BAD_FUNC_ARG) {
  9777. ret = -6853;
  9778. goto exit;
  9779. }
  9780. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  9781. if (ret != BAD_FUNC_ARG) {
  9782. ret = -6854;
  9783. goto exit;
  9784. }
  9785. ret = wc_RNG_GenerateByte(NULL, block);
  9786. if (ret != BAD_FUNC_ARG) {
  9787. ret = -6855;
  9788. goto exit;
  9789. }
  9790. ret = wc_RNG_GenerateByte(rng, NULL);
  9791. if (ret != BAD_FUNC_ARG) {
  9792. ret = -6856;
  9793. goto exit;
  9794. }
  9795. ret = 0;
  9796. exit:
  9797. if (ret != 0)
  9798. ret += errorOffset;
  9799. return ret;
  9800. }
  9801. static int random_rng_test(void)
  9802. {
  9803. WC_RNG localRng;
  9804. WC_RNG* rng;
  9805. int ret;
  9806. rng = &localRng;
  9807. /* Test stack based RNG. */
  9808. #ifndef HAVE_FIPS
  9809. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  9810. #else
  9811. ret = wc_InitRng(rng);
  9812. #endif
  9813. if (ret != 0) return -6900;
  9814. ret = _rng_test(rng, -6300);
  9815. /* Make sure and free RNG */
  9816. wc_FreeRng(rng);
  9817. if (ret != 0) return ret;
  9818. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  9819. {
  9820. byte nonce[8] = { 0 };
  9821. /* Test dynamic RNG. */
  9822. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  9823. if (rng == NULL) return -6901;
  9824. ret = _rng_test(rng, -6310);
  9825. wc_rng_free(rng);
  9826. }
  9827. #endif
  9828. return ret;
  9829. }
  9830. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  9831. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  9832. {
  9833. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  9834. {
  9835. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  9836. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  9837. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  9838. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  9839. };
  9840. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  9841. {
  9842. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  9843. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  9844. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  9845. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  9846. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  9847. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  9848. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  9849. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  9850. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  9851. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  9852. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  9853. };
  9854. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  9855. {
  9856. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  9857. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  9858. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  9859. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  9860. };
  9861. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  9862. {
  9863. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  9864. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  9865. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  9866. };
  9867. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  9868. {
  9869. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  9870. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  9871. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  9872. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  9873. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  9874. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  9875. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  9876. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  9877. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  9878. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  9879. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  9880. };
  9881. byte output[WC_SHA256_DIGEST_SIZE * 4];
  9882. int ret;
  9883. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  9884. output, sizeof(output));
  9885. if (ret != 0)
  9886. return -7000;
  9887. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  9888. return -7001;
  9889. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  9890. test2EntropyB, sizeof(test2EntropyB),
  9891. output, sizeof(output));
  9892. if (ret != 0)
  9893. return -7002;
  9894. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  9895. return -7003;
  9896. /* Basic RNG generate block test */
  9897. if ((ret = random_rng_test()) != 0)
  9898. return ret;
  9899. /* Test the seed check function. */
  9900. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  9901. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  9902. {
  9903. word32 i, outputSz;
  9904. /* Repeat the same byte over and over. Should fail. */
  9905. outputSz = sizeof(output);
  9906. XMEMSET(output, 1, outputSz);
  9907. ret = wc_RNG_TestSeed(output, outputSz);
  9908. if (ret == 0)
  9909. return -7004;
  9910. /* Every byte of the entropy scratch is different,
  9911. * entropy is a single byte that shouldn't match. */
  9912. outputSz = (sizeof(word32) * 2) + 1;
  9913. for (i = 0; i < outputSz; i++)
  9914. output[i] = (byte)i;
  9915. ret = wc_RNG_TestSeed(output, outputSz);
  9916. if (ret != 0)
  9917. return -7005;
  9918. outputSz = sizeof(output);
  9919. for (i = 0; i < outputSz; i++)
  9920. output[i] = (byte)i;
  9921. ret = wc_RNG_TestSeed(output, outputSz);
  9922. if (ret != 0)
  9923. return -7006;
  9924. }
  9925. #endif
  9926. return 0;
  9927. }
  9928. #else
  9929. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  9930. {
  9931. /* Basic RNG generate block test */
  9932. return random_rng_test();
  9933. }
  9934. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  9935. #endif /* WC_NO_RNG */
  9936. #ifndef MEM_TEST_SZ
  9937. #define MEM_TEST_SZ 1024
  9938. #endif
  9939. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  9940. static int simple_mem_test(int sz)
  9941. {
  9942. int ret = 0;
  9943. byte* b;
  9944. int i;
  9945. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9946. if (b == NULL) {
  9947. return -7110;
  9948. }
  9949. /* utilize memory */
  9950. for (i = 0; i < sz; i++) {
  9951. b[i] = (byte)i;
  9952. }
  9953. /* read back and verify */
  9954. for (i = 0; i < sz; i++) {
  9955. if (b[i] != (byte)i) {
  9956. ret = -7111;
  9957. break;
  9958. }
  9959. }
  9960. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9961. return ret;
  9962. }
  9963. #endif
  9964. WOLFSSL_TEST_SUBROUTINE int memory_test(void)
  9965. {
  9966. int ret = 0;
  9967. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC)
  9968. byte* b = NULL;
  9969. #endif
  9970. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  9971. int i;
  9972. #endif
  9973. #ifdef WOLFSSL_STATIC_MEMORY
  9974. word32 size[] = { WOLFMEM_BUCKETS };
  9975. word32 dist[] = { WOLFMEM_DIST };
  9976. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  9977. int pad = -(int)((wolfssl_word)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  9978. /* pad to account for if head of buffer is not at set memory
  9979. * alignment when tests are ran */
  9980. #endif
  9981. #ifdef WOLFSSL_STATIC_MEMORY
  9982. /* check macro settings */
  9983. if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  9984. return -7200;
  9985. }
  9986. if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  9987. return -7201;
  9988. }
  9989. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  9990. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  9991. /* each element in array should be divisible by alignment size */
  9992. return -7202;
  9993. }
  9994. }
  9995. for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
  9996. if (size[i - 1] >= size[i]) {
  9997. return -7203; /* sizes should be in increasing order */
  9998. }
  9999. }
  10000. /* check that padding size returned is possible */
  10001. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  10002. return -7204; /* no room for wc_Memory struct */
  10003. }
  10004. if (wolfSSL_MemoryPaddingSz() < 0) {
  10005. return -7205;
  10006. }
  10007. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  10008. return -7206; /* not aligned! */
  10009. }
  10010. /* check function to return optimum buffer size (rounded down) */
  10011. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  10012. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  10013. return -7207; /* not aligned! */
  10014. }
  10015. if (ret < 0) {
  10016. return -7208;
  10017. }
  10018. if ((unsigned int)ret > sizeof(buffer)) {
  10019. return -7209; /* did not round down as expected */
  10020. }
  10021. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  10022. return -7210; /* return value changed when using suggested value */
  10023. }
  10024. ret = wolfSSL_MemoryPaddingSz();
  10025. ret += pad; /* add space that is going to be needed if buffer not aligned */
  10026. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  10027. (ret + (int)size[0])) {
  10028. return -7211; /* did not round down to nearest bucket value */
  10029. }
  10030. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  10031. if ((ret - pad) < 0) {
  10032. return -7212;
  10033. }
  10034. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  10035. return -7213; /* not even chunks of memory for IO size */
  10036. }
  10037. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  10038. return -7214; /* memory not aligned */
  10039. }
  10040. /* check for passing bad or unknown arguments to functions */
  10041. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  10042. return -7215;
  10043. }
  10044. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  10045. return -7216; /* should round to 0 since struct + bucket will not fit */
  10046. }
  10047. (void)dist; /* avoid static analysis warning of variable not used */
  10048. #endif
  10049. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  10050. /* simple test */
  10051. ret = simple_mem_test(MEM_TEST_SZ);
  10052. if (ret != 0)
  10053. return ret;
  10054. #endif
  10055. #ifdef COMPLEX_MEM_TEST
  10056. /* test various size blocks */
  10057. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  10058. ret = simple_mem_test(i);
  10059. if (ret != 0)
  10060. return ret;
  10061. }
  10062. #endif
  10063. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC)
  10064. /* realloc test */
  10065. b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10066. if (b) {
  10067. b = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  10068. DYNAMIC_TYPE_TMP_BUFFER);
  10069. }
  10070. if (b == NULL) {
  10071. return -7217;
  10072. }
  10073. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10074. #endif
  10075. return ret;
  10076. }
  10077. #ifdef HAVE_NTRU
  10078. byte GetEntropy(ENTROPY_CMD cmd, byte* out);
  10079. byte GetEntropy(ENTROPY_CMD cmd, byte* out)
  10080. {
  10081. static WC_RNG rng;
  10082. if (cmd == INIT)
  10083. return (wc_InitRng(&rng) == 0) ? 1 : 0;
  10084. if (out == NULL)
  10085. return 0;
  10086. if (cmd == GET_BYTE_OF_ENTROPY)
  10087. return (wc_RNG_GenerateBlock(&rng, out, 1) == 0) ? 1 : 0;
  10088. if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) {
  10089. *out = 1;
  10090. return 1;
  10091. }
  10092. return 0;
  10093. }
  10094. #endif /* HAVE_NTRU */
  10095. #ifndef NO_FILESYSTEM
  10096. /* Cert Paths */
  10097. #ifdef FREESCALE_MQX
  10098. #define CERT_PREFIX "a:\\"
  10099. #define CERT_PATH_SEP "\\"
  10100. #elif defined(WOLFSSL_uTKERNEL2)
  10101. #define CERT_PREFIX "/uda/"
  10102. #define CERT_PATH_SEP "/"
  10103. #else
  10104. #define CERT_PREFIX "./"
  10105. #define CERT_PATH_SEP "/"
  10106. #endif
  10107. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  10108. #ifdef DISTCHECK_BUILD
  10109. /* This is the path used during distcheck by autotools.
  10110. * Using a temp directory passed in would be more flexible */
  10111. #define CERT_TEMPDIR "./_build/sub/"
  10112. #else
  10113. #define CERT_TEMPDIR CERT_PREFIX
  10114. #endif
  10115. /* Generated Test Certs */
  10116. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  10117. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  10118. #if !defined(NO_RSA) && !defined(NO_ASN)
  10119. static const char* clientKey = CERT_ROOT "client-key.der";
  10120. static const char* clientCert = CERT_ROOT "client-cert.der";
  10121. #ifdef WOLFSSL_CERT_EXT
  10122. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  10123. #endif
  10124. #endif /* !NO_RSA && !NO_ASN */
  10125. #endif
  10126. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  10127. #if !defined(NO_RSA) && !defined(NO_ASN)
  10128. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  10129. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  10130. #ifdef WOLFSSL_CERT_GEN
  10131. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  10132. #endif
  10133. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  10134. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  10135. #endif
  10136. #ifdef HAVE_PKCS7
  10137. static const char* rsaServerCertDerFile =
  10138. CERT_ROOT "server-cert.der";
  10139. static const char* rsaServerKeyDerFile =
  10140. CERT_ROOT "server-key.der";
  10141. #endif
  10142. #endif
  10143. #endif /* !NO_RSA && !NO_ASN */
  10144. #endif /* !USE_CERT_BUFFER_* */
  10145. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  10146. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  10147. !defined(NO_ASN)
  10148. #ifndef NO_DH
  10149. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  10150. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  10151. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  10152. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  10153. #endif
  10154. #endif
  10155. #ifndef NO_DSA
  10156. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  10157. #endif
  10158. #endif /* !USE_CERT_BUFFER_* */
  10159. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  10160. #ifdef HAVE_ECC
  10161. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  10162. #ifdef HAVE_ECC_KEY_IMPORT
  10163. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  10164. #endif
  10165. #endif
  10166. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  10167. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  10168. #ifndef NO_RSA
  10169. /* eccKeyPubFile is used in a test that requires RSA. */
  10170. static const char* eccKeyPubFile = CERT_ROOT "ecc-keyPub.der";
  10171. #endif
  10172. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  10173. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  10174. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  10175. static const char* eccCaKey384File =
  10176. CERT_ROOT "ca-ecc384-key.der";
  10177. static const char* eccCaCert384File =
  10178. CERT_ROOT "ca-ecc384-cert.pem";
  10179. #endif
  10180. #endif
  10181. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  10182. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  10183. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  10184. #endif
  10185. #endif /* HAVE_ECC */
  10186. #ifdef HAVE_ED25519
  10187. #ifdef WOLFSSL_TEST_CERT
  10188. static const char* serverEd25519Cert =
  10189. CERT_ROOT "ed25519/server-ed25519.der";
  10190. static const char* caEd25519Cert =
  10191. CERT_ROOT "ed25519/ca-ed25519.der";
  10192. #endif
  10193. #endif
  10194. #ifdef HAVE_ED448
  10195. #ifdef WOLFSSL_TEST_CERT
  10196. static const char* serverEd448Cert =
  10197. CERT_ROOT "ed448/server-ed448.der";
  10198. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  10199. #endif
  10200. #endif
  10201. #endif /* !USE_CERT_BUFFER_* */
  10202. #ifndef NO_WRITE_TEMP_FILES
  10203. #ifdef HAVE_ECC
  10204. #ifdef WOLFSSL_CERT_GEN
  10205. static const char* certEccPemFile = CERT_TEMPDIR "certecc.pem";
  10206. #endif
  10207. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  10208. static const char* certEccRsaPemFile = CERT_TEMPDIR "certeccrsa.pem";
  10209. static const char* certEccRsaDerFile = CERT_TEMPDIR "certeccrsa.der";
  10210. #endif
  10211. static const char* eccCaKeyPemFile = CERT_TEMPDIR "ecc-key.pem";
  10212. static const char* eccPubKeyDerFile = CERT_TEMPDIR "ecc-public-key.der";
  10213. static const char* eccCaKeyTempFile = CERT_TEMPDIR "ecc-key.der";
  10214. #ifdef HAVE_PKCS8
  10215. static const char* eccPkcs8KeyDerFile = CERT_TEMPDIR "ecc-key-pkcs8.der";
  10216. #endif
  10217. #if defined(WOLFSSL_CERT_GEN) || \
  10218. (defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT))
  10219. static const char* certEccDerFile = CERT_TEMPDIR "certecc.der";
  10220. #endif
  10221. #endif /* HAVE_ECC */
  10222. #ifndef NO_RSA
  10223. #if defined(WOLFSSL_CERT_GEN) || \
  10224. (defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT))
  10225. static const char* otherCertDerFile = CERT_TEMPDIR "othercert.der";
  10226. static const char* certDerFile = CERT_TEMPDIR "cert.der";
  10227. #endif
  10228. #ifdef WOLFSSL_CERT_GEN
  10229. static const char* otherCertPemFile = CERT_TEMPDIR "othercert.pem";
  10230. static const char* certPemFile = CERT_TEMPDIR "cert.pem";
  10231. #endif
  10232. #ifdef WOLFSSL_CERT_REQ
  10233. static const char* certReqDerFile = CERT_TEMPDIR "certreq.der";
  10234. static const char* certReqPemFile = CERT_TEMPDIR "certreq.pem";
  10235. #endif
  10236. #endif /* !NO_RSA */
  10237. #if !defined(NO_RSA) || !defined(NO_DSA)
  10238. #ifdef WOLFSSL_KEY_GEN
  10239. static const char* keyDerFile = CERT_TEMPDIR "key.der";
  10240. static const char* keyPemFile = CERT_TEMPDIR "key.pem";
  10241. #endif
  10242. #endif
  10243. #endif /* !NO_WRITE_TEMP_FILES */
  10244. #endif /* !NO_FILESYSTEM */
  10245. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  10246. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  10247. #ifdef WOLFSSL_MULTI_ATTRIB
  10248. static CertName certDefaultName;
  10249. static void initDefaultName(void)
  10250. {
  10251. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  10252. certDefaultName.countryEnc = CTC_PRINTABLE;
  10253. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  10254. certDefaultName.stateEnc = CTC_UTF8;
  10255. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  10256. certDefaultName.localityEnc = CTC_UTF8;
  10257. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  10258. certDefaultName.surEnc = CTC_UTF8;
  10259. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  10260. certDefaultName.orgEnc = CTC_UTF8;
  10261. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  10262. certDefaultName.unitEnc = CTC_UTF8;
  10263. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  10264. certDefaultName.commonNameEnc = CTC_UTF8;
  10265. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  10266. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  10267. #ifdef WOLFSSL_CERT_EXT
  10268. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  10269. certDefaultName.busCatEnc = CTC_UTF8;
  10270. #endif
  10271. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  10272. #ifdef WOLFSSL_TEST_CERT
  10273. {
  10274. NameAttrib* n;
  10275. /* test having additional OUs and setting DC */
  10276. n = &certDefaultName.name[0];
  10277. n->id = ASN_ORGUNIT_NAME;
  10278. n->type = CTC_UTF8;
  10279. n->sz = sizeof("Development-2");
  10280. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  10281. #if CTC_MAX_ATTRIB > 3
  10282. n = &certDefaultName.name[1];
  10283. n->id = ASN_DOMAIN_COMPONENT;
  10284. n->type = CTC_UTF8;
  10285. n->sz = sizeof("com");
  10286. XMEMCPY(n->value, "com", sizeof("com"));
  10287. n = &certDefaultName.name[2];
  10288. n->id = ASN_DOMAIN_COMPONENT;
  10289. n->type = CTC_UTF8;
  10290. n->sz = sizeof("wolfssl");
  10291. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  10292. #endif
  10293. }
  10294. #endif /* WOLFSSL_TEST_CERT */
  10295. }
  10296. #else
  10297. static const CertName certDefaultName = {
  10298. "US", CTC_PRINTABLE, /* country */
  10299. "Oregon", CTC_UTF8, /* state */
  10300. "Portland", CTC_UTF8, /* locality */
  10301. "Test", CTC_UTF8, /* sur */
  10302. "wolfSSL", CTC_UTF8, /* org */
  10303. "Development", CTC_UTF8, /* unit */
  10304. "www.wolfssl.com", CTC_UTF8, /* commonName */
  10305. "wolfSSL12345", CTC_PRINTABLE, /* serial number of device */
  10306. #ifdef WOLFSSL_CERT_EXT
  10307. "Private Organization", CTC_UTF8, /* businessCategory */
  10308. "US", CTC_PRINTABLE, /* jurisdiction country */
  10309. "Oregon", CTC_PRINTABLE, /* jurisdiction state */
  10310. #endif
  10311. "info@wolfssl.com" /* email */
  10312. };
  10313. #endif /* WOLFSSL_MULTI_ATTRIB */
  10314. #ifdef WOLFSSL_CERT_EXT
  10315. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  10316. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  10317. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  10318. "digitalSignature,nonRepudiation";
  10319. #endif
  10320. #if (defined(WOLFSSL_CERT_REQ) || defined(HAVE_NTRU)) && !defined(NO_RSA)
  10321. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  10322. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  10323. #endif
  10324. #endif /* WOLFSSL_CERT_EXT */
  10325. #endif /* WOLFSSL_CERT_GEN */
  10326. #ifndef NO_RSA
  10327. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  10328. !defined(NO_FILESYSTEM)
  10329. static byte minSerial[] = { 0x02, 0x01, 0x01 };
  10330. static byte minName[] = { 0x30, 0x00 };
  10331. static byte nameBad[] = {
  10332. 0x30, 0x08,
  10333. 0x31, 0x06,
  10334. 0x30, 0x04,
  10335. 0x06, 0x02,
  10336. 0x55, 0x04,
  10337. };
  10338. static byte minDates[] = {
  10339. 0x30, 0x1e,
  10340. 0x17, 0x0d,
  10341. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  10342. 0x32, 0x33, 0x31, 0x30, 0x5a,
  10343. 0x17, 0x0d,
  10344. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  10345. 0x32, 0x33, 0x31, 0x30, 0x5a
  10346. };
  10347. static byte minPubKey[] = {
  10348. 0x30, 0x1b,
  10349. 0x30, 0x0d,
  10350. 0x06, 0x09,
  10351. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  10352. 0x01,
  10353. 0x05, 0x00,
  10354. 0x03, 0x0b,
  10355. 0x00, 0x30, 0x08,
  10356. 0x02, 0x01,
  10357. 0x03,
  10358. 0x02, 0x03,
  10359. 0x01, 0x00, 0x01
  10360. };
  10361. static byte minSigAlg[] = {
  10362. 0x30, 0x0d,
  10363. 0x06, 0x09,
  10364. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  10365. 0x0b,
  10366. 0x05, 0x00
  10367. };
  10368. static byte minSig[] = {
  10369. 0x03, 0x01,
  10370. 0x00
  10371. };
  10372. static int add_seq(byte* certData, int offset, byte* data, byte length)
  10373. {
  10374. XMEMMOVE(certData + offset + 2, data, length);
  10375. certData[offset++] = 0x30;
  10376. certData[offset++] = length;
  10377. return offset + length;
  10378. }
  10379. static int add_data(byte* certData, int offset, byte* data, byte length)
  10380. {
  10381. XMEMCPY(certData + offset, data, length);
  10382. return offset + length;
  10383. }
  10384. static int cert_asn1_test(void)
  10385. {
  10386. int ret;
  10387. int len[3];
  10388. DecodedCert cert;
  10389. byte certData[106];
  10390. byte* badCert = NULL;
  10391. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  10392. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  10393. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  10394. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  10395. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  10396. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  10397. len[1] = add_seq(certData, 0, certData, len[2]);
  10398. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  10399. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  10400. len[0] = add_seq(certData, 0, certData, len[1]);
  10401. /* Minimal good certificate */
  10402. InitDecodedCert(&cert, certData, len[0], 0);
  10403. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10404. FreeDecodedCert(&cert);
  10405. if (ret != 0) {
  10406. ERROR_OUT(-7300, done);
  10407. }
  10408. /* Bad issuer name */
  10409. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  10410. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  10411. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  10412. len[1] = add_seq(certData, 0, certData, len[2]);
  10413. len[0] = add_seq(certData, 0, certData, len[1]);
  10414. /* Put data into allocated buffer to allow access error checking. */
  10415. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10416. XMEMCPY(badCert, certData, len[0]);
  10417. InitDecodedCert(&cert, badCert, len[0], 0);
  10418. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10419. FreeDecodedCert(&cert);
  10420. if (ret != ASN_PARSE_E) {
  10421. ERROR_OUT(-7301, done);
  10422. }
  10423. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10424. badCert = NULL;
  10425. ret = 0;
  10426. done:
  10427. if (badCert != NULL)
  10428. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10429. return ret;
  10430. }
  10431. WOLFSSL_TEST_SUBROUTINE int cert_test(void)
  10432. {
  10433. #if !defined(NO_FILESYSTEM)
  10434. DecodedCert cert;
  10435. byte* tmp;
  10436. size_t bytes;
  10437. XFILE file;
  10438. int ret;
  10439. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10440. if (tmp == NULL)
  10441. return -7400;
  10442. /* Certificate with Name Constraints extension. */
  10443. #ifdef FREESCALE_MQX
  10444. file = XFOPEN(".\\certs\\test\\cert-ext-nc.der", "rb");
  10445. #else
  10446. file = XFOPEN("./certs/test/cert-ext-nc.der", "rb");
  10447. #endif
  10448. if (!file) {
  10449. ERROR_OUT(-7401, done);
  10450. }
  10451. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10452. XFCLOSE(file);
  10453. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10454. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10455. if (ret != 0) {
  10456. ERROR_OUT(-7402, done);
  10457. }
  10458. FreeDecodedCert(&cert);
  10459. /* Certificate with Inhibit Any Policy extension. */
  10460. #ifdef FREESCALE_MQX
  10461. file = XFOPEN(".\\certs\\test\\cert-ext-ia.der", "rb");
  10462. #else
  10463. file = XFOPEN("./certs/test/cert-ext-ia.der", "rb");
  10464. #endif
  10465. if (!file) {
  10466. ERROR_OUT(-7403, done);
  10467. }
  10468. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10469. XFCLOSE(file);
  10470. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10471. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10472. if (ret != 0) {
  10473. ERROR_OUT(-7404, done);
  10474. }
  10475. FreeDecodedCert(&cert);
  10476. /* Certificate with Netscape Certificate Type extension. */
  10477. #ifdef FREESCALE_MQX
  10478. file = XFOPEN(".\\certs\\test\\cert-ext-nct.der", "rb");
  10479. #else
  10480. file = XFOPEN("./certs/test/cert-ext-nct.der", "rb");
  10481. #endif
  10482. if (!file) {
  10483. ERROR_OUT(-7405, done);
  10484. }
  10485. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10486. XFCLOSE(file);
  10487. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10488. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10489. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  10490. if (ret != 0) {
  10491. ERROR_OUT(-7406, done);
  10492. }
  10493. #else
  10494. if (ret != ASN_CRIT_EXT_E) {
  10495. ERROR_OUT(-7407, done);
  10496. }
  10497. ret = 0;
  10498. #endif
  10499. done:
  10500. FreeDecodedCert(&cert);
  10501. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10502. #endif /* !NO_FILESYSTEM */
  10503. if (ret == 0)
  10504. ret = cert_asn1_test();
  10505. return ret;
  10506. }
  10507. #endif /* WOLFSSL_TEST_CERT */
  10508. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  10509. !defined(NO_FILESYSTEM)
  10510. WOLFSSL_TEST_SUBROUTINE int certext_test(void)
  10511. {
  10512. DecodedCert cert;
  10513. byte* tmp;
  10514. size_t bytes;
  10515. XFILE file;
  10516. int ret;
  10517. /* created from rsa_test : othercert.der */
  10518. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  10519. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  10520. /* created from rsa_test : othercert.der */
  10521. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  10522. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  10523. #ifdef HAVE_ECC
  10524. /* created from ecc_test_cert_gen : certecc.der */
  10525. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  10526. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  10527. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  10528. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  10529. #else
  10530. /* Authority key id from ./certs/ca-ecc-cert.pem */
  10531. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  10532. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  10533. #endif
  10534. #endif /* HAVE_ECC */
  10535. /* created from rsa_test : cert.der */
  10536. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  10537. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  10538. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10539. if (tmp == NULL)
  10540. return -7500;
  10541. /* load othercert.der (Cert signed by an authority) */
  10542. file = XFOPEN(otherCertDerFile, "rb");
  10543. if (!file) {
  10544. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10545. return -7501;
  10546. }
  10547. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10548. XFCLOSE(file);
  10549. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10550. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  10551. if (ret != 0)
  10552. return -7502;
  10553. /* check the SKID from a RSA certificate */
  10554. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  10555. return -7503;
  10556. /* check the AKID from an RSA certificate */
  10557. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  10558. return -7504;
  10559. /* check the Key Usage from an RSA certificate */
  10560. if (!cert.extKeyUsageSet)
  10561. return -7505;
  10562. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  10563. return -7506;
  10564. /* check the CA Basic Constraints from an RSA certificate */
  10565. if (cert.isCA)
  10566. return -7507;
  10567. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  10568. /* check the Certificate Policies Id */
  10569. if (cert.extCertPoliciesNb != 1)
  10570. return -7508;
  10571. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  10572. return -7509;
  10573. #endif
  10574. FreeDecodedCert(&cert);
  10575. #ifdef HAVE_ECC
  10576. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  10577. file = XFOPEN(certEccDerFile, "rb");
  10578. if (!file) {
  10579. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10580. return -7510;
  10581. }
  10582. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10583. XFCLOSE(file);
  10584. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10585. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  10586. if (ret != 0)
  10587. return -7511;
  10588. /* check the SKID from a ECC certificate - generated dynamically */
  10589. /* check the AKID from an ECC certificate */
  10590. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  10591. return -7512;
  10592. /* check the Key Usage from an ECC certificate */
  10593. if (!cert.extKeyUsageSet)
  10594. return -7513;
  10595. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  10596. return -7514;
  10597. /* check the CA Basic Constraints from an ECC certificate */
  10598. if (cert.isCA)
  10599. return -7515;
  10600. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  10601. /* check the Certificate Policies Id */
  10602. if (cert.extCertPoliciesNb != 2)
  10603. return -7516;
  10604. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  10605. return -7517;
  10606. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  10607. return -7518;
  10608. #endif
  10609. FreeDecodedCert(&cert);
  10610. #endif /* HAVE_ECC */
  10611. /* load cert.der (self signed certificate) */
  10612. file = XFOPEN(certDerFile, "rb");
  10613. if (!file) {
  10614. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10615. return -7519;
  10616. }
  10617. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10618. XFCLOSE(file);
  10619. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10620. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  10621. if (ret != 0)
  10622. return -7520;
  10623. /* check the SKID from a CA certificate */
  10624. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  10625. return -7521;
  10626. /* check the AKID from an CA certificate */
  10627. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  10628. return -7522;
  10629. /* check the Key Usage from CA certificate */
  10630. if (!cert.extKeyUsageSet)
  10631. return -7523;
  10632. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  10633. return -7524;
  10634. /* check the CA Basic Constraints CA certificate */
  10635. if (!cert.isCA)
  10636. return -7525;
  10637. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  10638. /* check the Certificate Policies Id */
  10639. if (cert.extCertPoliciesNb != 2)
  10640. return -7526;
  10641. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  10642. return -7527;
  10643. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  10644. return -7528;
  10645. #endif
  10646. FreeDecodedCert(&cert);
  10647. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10648. return 0;
  10649. }
  10650. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT */
  10651. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  10652. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  10653. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void)
  10654. {
  10655. int ret = 0;
  10656. Cert cert;
  10657. FILE* file;
  10658. byte* der;
  10659. word32 derSz;
  10660. derSz = FOURK_BUF;
  10661. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10662. if (der == NULL)
  10663. ret = -7600;
  10664. if (ret == 0) {
  10665. /* load cert.der */
  10666. file = XFOPEN(certDerFile, "rb");
  10667. if (file != NULL) {
  10668. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  10669. XFCLOSE(file);
  10670. }
  10671. else
  10672. ret = -7601;
  10673. }
  10674. if (ret == 0) {
  10675. if (wc_InitCert(&cert)) {
  10676. ret = -7602;
  10677. }
  10678. }
  10679. if (ret == 0) {
  10680. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  10681. }
  10682. if (ret == 0) {
  10683. if(wc_SetSubjectBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  10684. ret = -7603;
  10685. }
  10686. if (ret == 0) {
  10687. if (wc_SetSubjectRaw(&cert, der, derSz) != 0)
  10688. ret = -7604;
  10689. }
  10690. if (ret == 0) {
  10691. if(wc_SetSubjectRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  10692. ret = -7605;
  10693. }
  10694. if (ret == 0) {
  10695. if(wc_SetIssuerBuffer(&cert, der, derSz) != 0)
  10696. ret = -7606;
  10697. }
  10698. if (ret == 0) {
  10699. if(wc_SetIssuerBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  10700. ret = -7607;
  10701. }
  10702. if (ret == 0) {
  10703. if(wc_SetIssuerRaw(&cert, der, derSz) != 0)
  10704. ret = -7608;
  10705. }
  10706. if (ret == 0) {
  10707. if(wc_SetIssuerRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  10708. ret = -7609;
  10709. }
  10710. #ifdef WOLFSSL_ALT_NAMES
  10711. if (ret == 0) {
  10712. if(wc_SetAltNamesBuffer(&cert, der, derSz) != 0)
  10713. ret = -7610;
  10714. }
  10715. if (ret == 0) {
  10716. if(wc_SetAltNamesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  10717. ret = -7611;
  10718. }
  10719. if (ret == 0) {
  10720. if(wc_SetDatesBuffer(&cert, der, derSz) != 0)
  10721. ret = -7612;
  10722. }
  10723. if (ret == 0) {
  10724. if(wc_SetDatesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  10725. ret = -7613;
  10726. }
  10727. #endif
  10728. if (ret == 0) {
  10729. if(wc_SetAuthKeyIdFromCert(&cert, der, derSz) != 0)
  10730. ret = -7614;
  10731. }
  10732. if (ret == 0) {
  10733. if(wc_SetAuthKeyIdFromCert(NULL, der, derSz) != BAD_FUNC_ARG)
  10734. ret = -7615;
  10735. }
  10736. wc_SetCert_Free(&cert);
  10737. if (ret == 0) {
  10738. if(cert.decodedCert != NULL)
  10739. ret = -7616;
  10740. }
  10741. XFREE(der, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10742. return ret;
  10743. }
  10744. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  10745. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  10746. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  10747. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  10748. static int rsa_flatten_test(RsaKey* key)
  10749. {
  10750. int ret;
  10751. byte e[RSA_TEST_BYTES];
  10752. byte n[RSA_TEST_BYTES];
  10753. word32 eSz = sizeof(e);
  10754. word32 nSz = sizeof(n);
  10755. /* Parameter Validation testing. */
  10756. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  10757. #ifdef HAVE_USER_RSA
  10758. /* Implementation using IPP Libraries returns:
  10759. * -101 = USER_CRYPTO_ERROR
  10760. */
  10761. if (ret == 0)
  10762. #else
  10763. if (ret != BAD_FUNC_ARG)
  10764. #endif
  10765. return -7620;
  10766. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  10767. #ifdef HAVE_USER_RSA
  10768. /* Implementation using IPP Libraries returns:
  10769. * -101 = USER_CRYPTO_ERROR
  10770. */
  10771. if (ret == 0)
  10772. #else
  10773. if (ret != BAD_FUNC_ARG)
  10774. #endif
  10775. return -7621;
  10776. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  10777. #ifdef HAVE_USER_RSA
  10778. /* Implementation using IPP Libraries returns:
  10779. * -101 = USER_CRYPTO_ERROR
  10780. */
  10781. if (ret == 0)
  10782. #else
  10783. if (ret != BAD_FUNC_ARG)
  10784. #endif
  10785. return -7622;
  10786. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  10787. #ifdef HAVE_USER_RSA
  10788. /* Implementation using IPP Libraries returns:
  10789. * -101 = USER_CRYPTO_ERROR
  10790. */
  10791. if (ret == 0)
  10792. #else
  10793. if (ret != BAD_FUNC_ARG)
  10794. #endif
  10795. return -7623;
  10796. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  10797. #ifdef HAVE_USER_RSA
  10798. /* Implementation using IPP Libraries returns:
  10799. * -101 = USER_CRYPTO_ERROR
  10800. */
  10801. if (ret == 0)
  10802. #else
  10803. if (ret != BAD_FUNC_ARG)
  10804. #endif
  10805. return -7624;
  10806. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  10807. if (ret != 0)
  10808. return -7625;
  10809. eSz = 0;
  10810. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  10811. #ifdef HAVE_USER_RSA
  10812. /* Implementation using IPP Libraries returns:
  10813. * -101 = USER_CRYPTO_ERROR
  10814. */
  10815. if (ret == 0)
  10816. #elif defined(HAVE_FIPS) && \
  10817. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  10818. if (ret != 0)
  10819. #else
  10820. if (ret != RSA_BUFFER_E)
  10821. #endif
  10822. return -7626;
  10823. eSz = sizeof(e);
  10824. nSz = 0;
  10825. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  10826. #ifdef HAVE_USER_RSA
  10827. /* Implementation using IPP Libraries returns:
  10828. * -101 = USER_CRYPTO_ERROR
  10829. */
  10830. if (ret == 0)
  10831. #else
  10832. if (ret != RSA_BUFFER_E)
  10833. #endif
  10834. return -7627;
  10835. return 0;
  10836. }
  10837. #endif /* NO_ASN */
  10838. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  10839. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  10840. static int rsa_export_key_test(RsaKey* key)
  10841. {
  10842. int ret;
  10843. byte e[3];
  10844. word32 eSz = sizeof(e);
  10845. byte n[RSA_TEST_BYTES];
  10846. word32 nSz = sizeof(n);
  10847. byte d[RSA_TEST_BYTES];
  10848. word32 dSz = sizeof(d);
  10849. byte p[RSA_TEST_BYTES/2];
  10850. word32 pSz = sizeof(p);
  10851. byte q[RSA_TEST_BYTES/2];
  10852. word32 qSz = sizeof(q);
  10853. word32 zero = 0;
  10854. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  10855. if (ret != BAD_FUNC_ARG)
  10856. return -7630;
  10857. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  10858. if (ret != BAD_FUNC_ARG)
  10859. return -7631;
  10860. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  10861. if (ret != BAD_FUNC_ARG)
  10862. return -7632;
  10863. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  10864. if (ret != BAD_FUNC_ARG)
  10865. return -7633;
  10866. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  10867. if (ret != BAD_FUNC_ARG)
  10868. return -7634;
  10869. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  10870. if (ret != BAD_FUNC_ARG)
  10871. return -7635;
  10872. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  10873. if (ret != BAD_FUNC_ARG)
  10874. return -7636;
  10875. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  10876. if (ret != BAD_FUNC_ARG)
  10877. return -7637;
  10878. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  10879. if (ret != BAD_FUNC_ARG)
  10880. return -7638;
  10881. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  10882. if (ret != BAD_FUNC_ARG)
  10883. return -7639;
  10884. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  10885. if (ret != BAD_FUNC_ARG)
  10886. return -7640;
  10887. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  10888. if (ret != RSA_BUFFER_E)
  10889. return -7641;
  10890. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  10891. if (ret != RSA_BUFFER_E)
  10892. return -7642;
  10893. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  10894. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  10895. if (ret != RSA_BUFFER_E)
  10896. return -7643;
  10897. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  10898. if (ret != RSA_BUFFER_E)
  10899. return -7644;
  10900. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  10901. if (ret != RSA_BUFFER_E)
  10902. return -7645;
  10903. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  10904. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  10905. if (ret != 0)
  10906. return -7646;
  10907. return 0;
  10908. }
  10909. #endif /* !HAVE_FIPS && !USER_RSA && !NO_ASN */
  10910. #ifndef NO_SIG_WRAPPER
  10911. static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  10912. {
  10913. int ret;
  10914. word32 sigSz;
  10915. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  10916. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  10917. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  10918. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  10919. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  10920. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  10921. };
  10922. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  10923. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  10924. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  10925. 0x00, 0x04, 0x20,
  10926. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  10927. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  10928. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  10929. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  10930. };
  10931. word32 inLen = (word32)XSTRLEN((char*)in);
  10932. byte out[RSA_TEST_BYTES];
  10933. /* Parameter Validation testing. */
  10934. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  10935. if (ret != BAD_FUNC_ARG)
  10936. return -7650;
  10937. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  10938. if (ret != BAD_FUNC_ARG)
  10939. return -7651;
  10940. sigSz = (word32)modLen;
  10941. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  10942. inLen, out, &sigSz, key, keyLen, rng);
  10943. if (ret != BAD_FUNC_ARG)
  10944. return -7652;
  10945. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  10946. 0, out, &sigSz, key, keyLen, rng);
  10947. if (ret != BAD_FUNC_ARG)
  10948. return -7653;
  10949. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  10950. inLen, NULL, &sigSz, key, keyLen, rng);
  10951. if (ret != BAD_FUNC_ARG)
  10952. return -7654;
  10953. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  10954. inLen, out, NULL, key, keyLen, rng);
  10955. if (ret != BAD_FUNC_ARG)
  10956. return -7655;
  10957. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  10958. inLen, out, &sigSz, NULL, keyLen, rng);
  10959. if (ret != BAD_FUNC_ARG)
  10960. return -7656;
  10961. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  10962. inLen, out, &sigSz, key, 0, rng);
  10963. if (ret != BAD_FUNC_ARG)
  10964. return -7657;
  10965. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  10966. inLen, out, &sigSz, key, keyLen, NULL);
  10967. #ifdef HAVE_USER_RSA
  10968. /* Implementation using IPP Libraries returns:
  10969. * -101 = USER_CRYPTO_ERROR
  10970. */
  10971. if (ret == 0)
  10972. #elif defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  10973. /* blinding / rng handled with hardware acceleration */
  10974. if (ret != 0)
  10975. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  10976. /* async may not require RNG */
  10977. if (ret != 0 && ret != MISSING_RNG_E)
  10978. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  10979. /* FIPS140 implementation does not do blinding */
  10980. if (ret != 0)
  10981. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY)
  10982. if (ret != SIG_TYPE_E)
  10983. #elif defined(WOLFSSL_CRYPTOCELL)
  10984. /* RNG is handled with the cryptocell */
  10985. if (ret != 0)
  10986. #else
  10987. if (ret != MISSING_RNG_E)
  10988. #endif
  10989. return -7658;
  10990. sigSz = 0;
  10991. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  10992. inLen, out, &sigSz, key, keyLen, rng);
  10993. if (ret != BAD_FUNC_ARG)
  10994. return -7659;
  10995. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  10996. inLen, out, (word32)modLen, key, keyLen);
  10997. if (ret != BAD_FUNC_ARG)
  10998. return -7660;
  10999. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11000. 0, out, (word32)modLen, key, keyLen);
  11001. if (ret != BAD_FUNC_ARG)
  11002. return -7661;
  11003. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11004. inLen, NULL, (word32)modLen, key, keyLen);
  11005. if (ret != BAD_FUNC_ARG)
  11006. return -7662;
  11007. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11008. inLen, out, 0, key, keyLen);
  11009. if (ret != BAD_FUNC_ARG)
  11010. return -7663;
  11011. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11012. inLen, out, (word32)modLen, NULL, keyLen);
  11013. if (ret != BAD_FUNC_ARG)
  11014. return -7664;
  11015. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11016. inLen, out, (word32)modLen, key, 0);
  11017. if (ret != BAD_FUNC_ARG)
  11018. return -7665;
  11019. #ifndef HAVE_ECC
  11020. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  11021. if (ret != SIG_TYPE_E)
  11022. return -7666;
  11023. #endif
  11024. /* Use APIs. */
  11025. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  11026. if (ret != modLen)
  11027. return -7667;
  11028. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  11029. if (ret != modLen)
  11030. return -7668;
  11031. sigSz = (word32)ret;
  11032. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  11033. XMEMSET(out, 0, sizeof(out));
  11034. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11035. inLen, out, &sigSz, key, keyLen, rng);
  11036. if (ret != 0)
  11037. return -7669;
  11038. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11039. inLen, out, (word32)modLen, key, keyLen);
  11040. if (ret != 0)
  11041. return -7670;
  11042. sigSz = (word32)sizeof(out);
  11043. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  11044. in, inLen, out, &sigSz, key, keyLen, rng);
  11045. if (ret != 0)
  11046. return -7671;
  11047. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  11048. in, inLen, out, (word32)modLen, key, keyLen);
  11049. if (ret != 0)
  11050. return -7672;
  11051. /* Wrong signature type. */
  11052. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11053. inLen, out, (word32)modLen, key, keyLen);
  11054. if (ret == 0)
  11055. return -7673;
  11056. /* check hash functions */
  11057. sigSz = (word32)sizeof(out);
  11058. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  11059. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  11060. if (ret != 0)
  11061. return -7674;
  11062. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  11063. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  11064. if (ret != 0)
  11065. return -7675;
  11066. sigSz = (word32)sizeof(out);
  11067. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  11068. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  11069. if (ret != 0)
  11070. return -7676;
  11071. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  11072. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  11073. if (ret != 0)
  11074. return -7677;
  11075. #else
  11076. (void)hash;
  11077. (void)hashEnc;
  11078. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  11079. return 0;
  11080. }
  11081. #endif /* !NO_SIG_WRAPPER */
  11082. #ifdef WC_RSA_NONBLOCK
  11083. static int rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  11084. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  11085. {
  11086. int ret = 0, count;
  11087. int signSz = 0;
  11088. RsaNb nb;
  11089. byte* inlinePlain = NULL;
  11090. /* Enable non-blocking RSA mode - provide context */
  11091. ret = wc_RsaSetNonBlock(key, &nb);
  11092. if (ret != 0)
  11093. return ret;
  11094. #ifdef WC_RSA_NONBLOCK_TIME
  11095. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  11096. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  11097. if (ret != 0)
  11098. return ret;
  11099. #endif
  11100. count = 0;
  11101. do {
  11102. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  11103. count++; /* track number of would blocks */
  11104. if (ret == FP_WOULDBLOCK) {
  11105. /* do "other" work here */
  11106. }
  11107. } while (ret == FP_WOULDBLOCK);
  11108. if (ret < 0) {
  11109. return ret;
  11110. }
  11111. #ifdef DEBUG_WOLFSSL
  11112. printf("RSA non-block sign: %d times\n", count);
  11113. #endif
  11114. signSz = ret;
  11115. /* Test non-blocking verify */
  11116. XMEMSET(plain, 0, plainSz);
  11117. count = 0;
  11118. do {
  11119. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  11120. count++; /* track number of would blocks */
  11121. if (ret == FP_WOULDBLOCK) {
  11122. /* do "other" work here */
  11123. }
  11124. } while (ret == FP_WOULDBLOCK);
  11125. if (ret < 0) {
  11126. return ret;
  11127. }
  11128. #ifdef DEBUG_WOLFSSL
  11129. printf("RSA non-block verify: %d times\n", count);
  11130. #endif
  11131. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  11132. return SIG_VERIFY_E;
  11133. }
  11134. /* Test inline non-blocking verify */
  11135. count = 0;
  11136. do {
  11137. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  11138. count++; /* track number of would blocks */
  11139. if (ret == FP_WOULDBLOCK) {
  11140. /* do "other" work here */
  11141. }
  11142. } while (ret == FP_WOULDBLOCK);
  11143. if (ret < 0) {
  11144. return ret;
  11145. }
  11146. #ifdef DEBUG_WOLFSSL
  11147. printf("RSA non-block inline verify: %d times\n", count);
  11148. #endif
  11149. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  11150. return SIG_VERIFY_E;
  11151. }
  11152. /* Disabling non-block RSA mode */
  11153. ret = wc_RsaSetNonBlock(key, NULL);
  11154. (void)count;
  11155. return 0;
  11156. }
  11157. #endif
  11158. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  11159. static int rsa_decode_test(RsaKey* keyPub)
  11160. {
  11161. int ret;
  11162. word32 inSz;
  11163. word32 inOutIdx;
  11164. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  11165. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  11166. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  11167. 0x03 };
  11168. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00,
  11169. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11170. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = { 0x30, 0x11, 0x30, 0x0f, 0x06, 0x00,
  11171. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  11172. 0x02, 0x1, 0x03 };
  11173. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = { 0x30, 0x12, 0x30, 0x10, 0x06, 0x00,
  11174. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  11175. 0x02, 0x1, 0x03 };
  11176. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00,
  11177. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11178. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00,
  11179. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11180. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = { 0x30, 0x0d, 0x30, 0x0b, 0x06, 0x00, 0x03,
  11181. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11182. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  11183. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  11184. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11185. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = { 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1,
  11186. 0x03 };
  11187. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1,
  11188. 0x03 };
  11189. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = { 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1,
  11190. 0x03 };
  11191. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = { 0x30, 0x0e, 0x30, 0x0c, 0x06, 0x00,
  11192. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11193. ret = wc_InitRsaKey(keyPub, NULL);
  11194. if (ret != 0)
  11195. return -7690;
  11196. /* Parameter Validation testing. */
  11197. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  11198. if (ret != BAD_FUNC_ARG) {
  11199. ret = -7691;
  11200. goto done;
  11201. }
  11202. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  11203. if (ret != BAD_FUNC_ARG) {
  11204. ret = -7692;
  11205. goto done;
  11206. }
  11207. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  11208. if (ret != BAD_FUNC_ARG) {
  11209. ret = -7693;
  11210. goto done;
  11211. }
  11212. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  11213. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  11214. if (ret != 0) {
  11215. #else
  11216. if (ret != ASN_GETINT_E) {
  11217. #endif
  11218. ret = -7694;
  11219. goto done;
  11220. }
  11221. wc_FreeRsaKey(keyPub);
  11222. ret = wc_InitRsaKey(keyPub, NULL);
  11223. if (ret != 0)
  11224. return -7695;
  11225. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  11226. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  11227. if (ret != 0) {
  11228. #else
  11229. if (ret != ASN_GETINT_E) {
  11230. #endif
  11231. ret = -7696;
  11232. goto done;
  11233. }
  11234. wc_FreeRsaKey(keyPub);
  11235. ret = wc_InitRsaKey(keyPub, NULL);
  11236. if (ret != 0)
  11237. return -7697;
  11238. /* Use API. */
  11239. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  11240. if (ret != 0) {
  11241. ret = -7698;
  11242. goto done;
  11243. }
  11244. wc_FreeRsaKey(keyPub);
  11245. ret = wc_InitRsaKey(keyPub, NULL);
  11246. if (ret != 0)
  11247. return -7699;
  11248. /* Parameter Validation testing. */
  11249. inSz = sizeof(good);
  11250. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  11251. if (ret != BAD_FUNC_ARG) {
  11252. ret = -7700;
  11253. goto done;
  11254. }
  11255. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  11256. if (ret != BAD_FUNC_ARG) {
  11257. ret = -7701;
  11258. goto done;
  11259. }
  11260. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  11261. if (ret != BAD_FUNC_ARG) {
  11262. ret = -7702;
  11263. goto done;
  11264. }
  11265. /* Use good data and offset to bad data. */
  11266. inOutIdx = 2;
  11267. inSz = sizeof(good) - inOutIdx;
  11268. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  11269. if (ret != ASN_PARSE_E) {
  11270. ret = -7703;
  11271. goto done;
  11272. }
  11273. inOutIdx = 2;
  11274. inSz = sizeof(goodAlgId) - inOutIdx;
  11275. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  11276. if (ret != ASN_PARSE_E) {
  11277. ret = -7704;
  11278. goto done;
  11279. }
  11280. inOutIdx = 2;
  11281. inSz = sizeof(goodAlgId);
  11282. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  11283. #ifndef WOLFSSL_NO_DECODE_EXTRA
  11284. if (ret != ASN_PARSE_E)
  11285. #else
  11286. if (ret != ASN_RSA_KEY_E)
  11287. #endif
  11288. {
  11289. ret = -7705;
  11290. goto done;
  11291. }
  11292. /* Try different bad data. */
  11293. inSz = sizeof(badAlgIdNull);
  11294. inOutIdx = 0;
  11295. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  11296. if (ret != ASN_EXPECT_0_E) {
  11297. ret = -7706;
  11298. goto done;
  11299. }
  11300. inSz = sizeof(badNotBitString);
  11301. inOutIdx = 0;
  11302. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  11303. if (ret != ASN_BITSTR_E) {
  11304. ret = -7707;
  11305. goto done;
  11306. }
  11307. inSz = sizeof(badBitStringLen);
  11308. inOutIdx = 0;
  11309. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  11310. if (ret != ASN_PARSE_E) {
  11311. ret = -7708;
  11312. goto done;
  11313. }
  11314. inSz = sizeof(badNoSeq);
  11315. inOutIdx = 0;
  11316. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  11317. if (ret != ASN_PARSE_E) {
  11318. ret = -7709;
  11319. goto done;
  11320. }
  11321. inSz = sizeof(badNoObj);
  11322. inOutIdx = 0;
  11323. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  11324. if (ret != ASN_PARSE_E) {
  11325. ret = -7710;
  11326. goto done;
  11327. }
  11328. inSz = sizeof(badIntN);
  11329. inOutIdx = 0;
  11330. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  11331. if (ret != ASN_RSA_KEY_E) {
  11332. ret = -7711;
  11333. goto done;
  11334. }
  11335. inSz = sizeof(badNotIntE);
  11336. inOutIdx = 0;
  11337. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  11338. if (ret != ASN_RSA_KEY_E) {
  11339. ret = -7712;
  11340. goto done;
  11341. }
  11342. /* TODO: Shouldn't pass as the sequence length is too small. */
  11343. inSz = sizeof(badLength);
  11344. inOutIdx = 0;
  11345. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  11346. if (ret != 0) {
  11347. ret = -7713;
  11348. goto done;
  11349. }
  11350. /* TODO: Shouldn't ignore object id's data. */
  11351. wc_FreeRsaKey(keyPub);
  11352. ret = wc_InitRsaKey(keyPub, NULL);
  11353. if (ret != 0)
  11354. return -7714;
  11355. inSz = sizeof(badBitStrNoZero);
  11356. inOutIdx = 0;
  11357. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  11358. if (ret != ASN_EXPECT_0_E) {
  11359. ret = -7715;
  11360. goto done;
  11361. }
  11362. wc_FreeRsaKey(keyPub);
  11363. ret = wc_InitRsaKey(keyPub, NULL);
  11364. if (ret != 0)
  11365. return -7716;
  11366. /* Valid data cases. */
  11367. inSz = sizeof(good);
  11368. inOutIdx = 0;
  11369. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  11370. if (ret != 0) {
  11371. ret = -7717;
  11372. goto done;
  11373. }
  11374. if (inOutIdx != inSz) {
  11375. ret = -7718;
  11376. goto done;
  11377. }
  11378. wc_FreeRsaKey(keyPub);
  11379. ret = wc_InitRsaKey(keyPub, NULL);
  11380. if (ret != 0)
  11381. return -7719;
  11382. inSz = sizeof(goodAlgId);
  11383. inOutIdx = 0;
  11384. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  11385. if (ret != 0) {
  11386. ret = -7720;
  11387. goto done;
  11388. }
  11389. if (inOutIdx != inSz) {
  11390. ret = -7721;
  11391. goto done;
  11392. }
  11393. wc_FreeRsaKey(keyPub);
  11394. ret = wc_InitRsaKey(keyPub, NULL);
  11395. if (ret != 0)
  11396. return -7722;
  11397. inSz = sizeof(goodAlgIdNull);
  11398. inOutIdx = 0;
  11399. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  11400. if (ret != 0) {
  11401. ret = -7723;
  11402. goto done;
  11403. }
  11404. if (inOutIdx != inSz) {
  11405. ret = -7724;
  11406. goto done;
  11407. }
  11408. done:
  11409. wc_FreeRsaKey(keyPub);
  11410. return ret;
  11411. }
  11412. #endif
  11413. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  11414. /* Need to create known good signatures to test with this. */
  11415. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  11416. static int rsa_pss_test(WC_RNG* rng, RsaKey* key)
  11417. {
  11418. byte digest[WC_MAX_DIGEST_SIZE];
  11419. int ret = 0;
  11420. const char inStr[] = TEST_STRING;
  11421. word32 inLen = (word32)TEST_STRING_SZ;
  11422. word32 outSz;
  11423. word32 plainSz;
  11424. word32 digestSz;
  11425. int i, j;
  11426. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  11427. int k, l;
  11428. #endif
  11429. int len;
  11430. byte* plain;
  11431. int mgf[] = {
  11432. #ifndef NO_SHA
  11433. WC_MGF1SHA1,
  11434. #endif
  11435. #ifdef WOLFSSL_SHA224
  11436. WC_MGF1SHA224,
  11437. #endif
  11438. WC_MGF1SHA256,
  11439. #ifdef WOLFSSL_SHA384
  11440. WC_MGF1SHA384,
  11441. #endif
  11442. #ifdef WOLFSSL_SHA512
  11443. WC_MGF1SHA512
  11444. #endif
  11445. };
  11446. enum wc_HashType hash[] = {
  11447. #ifndef NO_SHA
  11448. WC_HASH_TYPE_SHA,
  11449. #endif
  11450. #ifdef WOLFSSL_SHA224
  11451. WC_HASH_TYPE_SHA224,
  11452. #endif
  11453. WC_HASH_TYPE_SHA256,
  11454. #ifdef WOLFSSL_SHA384
  11455. WC_HASH_TYPE_SHA384,
  11456. #endif
  11457. #ifdef WOLFSSL_SHA512
  11458. WC_HASH_TYPE_SHA512,
  11459. #endif
  11460. };
  11461. DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  11462. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  11463. DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  11464. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  11465. if (in == NULL || out == NULL || sig == NULL)
  11466. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  11467. #endif
  11468. XMEMCPY(in, inStr, inLen);
  11469. /* Test all combinations of hash and MGF. */
  11470. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  11471. /* Calculate hash of message. */
  11472. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  11473. if (ret != 0)
  11474. ERROR_OUT(-7730, exit_rsa_pss);
  11475. digestSz = wc_HashGetDigestSize(hash[j]);
  11476. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  11477. outSz = RSA_TEST_BYTES;
  11478. do {
  11479. #if defined(WOLFSSL_ASYNC_CRYPT)
  11480. ret = wc_AsyncWait(ret, &key->asyncDev,
  11481. WC_ASYNC_FLAG_CALL_AGAIN);
  11482. #endif
  11483. if (ret >= 0) {
  11484. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  11485. hash[j], mgf[i], -1, key, rng);
  11486. }
  11487. } while (ret == WC_PENDING_E);
  11488. if (ret <= 0)
  11489. ERROR_OUT(-7731, exit_rsa_pss);
  11490. outSz = ret;
  11491. XMEMCPY(sig, out, outSz);
  11492. plain = NULL;
  11493. TEST_SLEEP();
  11494. do {
  11495. #if defined(WOLFSSL_ASYNC_CRYPT)
  11496. ret = wc_AsyncWait(ret, &key->asyncDev,
  11497. WC_ASYNC_FLAG_CALL_AGAIN);
  11498. #endif
  11499. if (ret >= 0) {
  11500. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  11501. mgf[i], -1, key);
  11502. }
  11503. } while (ret == WC_PENDING_E);
  11504. if (ret <= 0)
  11505. ERROR_OUT(-7732, exit_rsa_pss);
  11506. plainSz = ret;
  11507. TEST_SLEEP();
  11508. #if defined(HAVE_SELFTEST) && \
  11509. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  11510. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  11511. hash[j], -1);
  11512. #else
  11513. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  11514. hash[j], -1, wc_RsaEncryptSize(key)*8);
  11515. #endif
  11516. if (ret != 0)
  11517. ERROR_OUT(-7733, exit_rsa_pss);
  11518. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  11519. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  11520. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  11521. if (i == k && j == l)
  11522. continue;
  11523. XMEMCPY(sig, out, outSz);
  11524. do {
  11525. #if defined(WOLFSSL_ASYNC_CRYPT)
  11526. ret = wc_AsyncWait(ret, &key->asyncDev,
  11527. WC_ASYNC_FLAG_CALL_AGAIN);
  11528. #endif
  11529. if (ret >= 0) {
  11530. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  11531. (byte**)&plain, hash[l], mgf[k], -1, key);
  11532. }
  11533. } while (ret == WC_PENDING_E);
  11534. if (ret >= 0)
  11535. ERROR_OUT(-7734, exit_rsa_pss);
  11536. }
  11537. }
  11538. #endif
  11539. }
  11540. }
  11541. /* Test that a salt length of zero works. */
  11542. digestSz = wc_HashGetDigestSize(hash[0]);
  11543. outSz = RSA_TEST_BYTES;
  11544. do {
  11545. #if defined(WOLFSSL_ASYNC_CRYPT)
  11546. ret = wc_AsyncWait(ret, &key->asyncDev,
  11547. WC_ASYNC_FLAG_CALL_AGAIN);
  11548. #endif
  11549. if (ret >= 0) {
  11550. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  11551. mgf[0], 0, key, rng);
  11552. }
  11553. } while (ret == WC_PENDING_E);
  11554. if (ret <= 0)
  11555. ERROR_OUT(-7735, exit_rsa_pss);
  11556. outSz = ret;
  11557. TEST_SLEEP();
  11558. do {
  11559. #if defined(WOLFSSL_ASYNC_CRYPT)
  11560. ret = wc_AsyncWait(ret, &key->asyncDev,
  11561. WC_ASYNC_FLAG_CALL_AGAIN);
  11562. #endif
  11563. if (ret >= 0) {
  11564. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  11565. 0, key);
  11566. }
  11567. } while (ret == WC_PENDING_E);
  11568. if (ret <= 0)
  11569. ERROR_OUT(-7736, exit_rsa_pss);
  11570. plainSz = ret;
  11571. TEST_SLEEP();
  11572. do {
  11573. #if defined(WOLFSSL_ASYNC_CRYPT)
  11574. ret = wc_AsyncWait(ret, &key->asyncDev,
  11575. WC_ASYNC_FLAG_CALL_AGAIN);
  11576. #endif
  11577. if (ret >= 0) {
  11578. #if defined(HAVE_SELFTEST) && \
  11579. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  11580. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  11581. hash[0], 0);
  11582. #else
  11583. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  11584. hash[0], 0, 0);
  11585. #endif
  11586. }
  11587. } while (ret == WC_PENDING_E);
  11588. if (ret != 0)
  11589. ERROR_OUT(-7737, exit_rsa_pss);
  11590. XMEMCPY(sig, out, outSz);
  11591. plain = NULL;
  11592. do {
  11593. #if defined(WOLFSSL_ASYNC_CRYPT)
  11594. ret = wc_AsyncWait(ret, &key->asyncDev,
  11595. WC_ASYNC_FLAG_CALL_AGAIN);
  11596. #endif
  11597. if (ret >= 0) {
  11598. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  11599. 0, key);
  11600. }
  11601. } while (ret == WC_PENDING_E);
  11602. if (ret <= 0)
  11603. ERROR_OUT(-7738, exit_rsa_pss);
  11604. plainSz = ret;
  11605. TEST_SLEEP();
  11606. #if defined(HAVE_SELFTEST) && \
  11607. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  11608. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11609. 0);
  11610. #else
  11611. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11612. 0, 0);
  11613. #endif
  11614. if (ret != 0)
  11615. ERROR_OUT(-7739, exit_rsa_pss);
  11616. /* Test bad salt lengths in various APIs. */
  11617. digestSz = wc_HashGetDigestSize(hash[0]);
  11618. outSz = RSA_TEST_BYTES;
  11619. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  11620. len = -2;
  11621. #else
  11622. len = -3;
  11623. #endif
  11624. do {
  11625. #if defined(WOLFSSL_ASYNC_CRYPT)
  11626. ret = wc_AsyncWait(ret, &key->asyncDev,
  11627. WC_ASYNC_FLAG_CALL_AGAIN);
  11628. #endif
  11629. if (ret >= 0) {
  11630. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  11631. mgf[0], len, key, rng);
  11632. }
  11633. } while (ret == WC_PENDING_E);
  11634. if (ret != PSS_SALTLEN_E)
  11635. ERROR_OUT(-7740, exit_rsa_pss);
  11636. do {
  11637. #if defined(WOLFSSL_ASYNC_CRYPT)
  11638. ret = wc_AsyncWait(ret, &key->asyncDev,
  11639. WC_ASYNC_FLAG_CALL_AGAIN);
  11640. #endif
  11641. if (ret >= 0) {
  11642. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  11643. mgf[0], digestSz + 1, key, rng);
  11644. }
  11645. } while (ret == WC_PENDING_E);
  11646. if (ret != PSS_SALTLEN_E)
  11647. ERROR_OUT(-7741, exit_rsa_pss);
  11648. TEST_SLEEP();
  11649. do {
  11650. #if defined(WOLFSSL_ASYNC_CRYPT)
  11651. ret = wc_AsyncWait(ret, &key->asyncDev,
  11652. WC_ASYNC_FLAG_CALL_AGAIN);
  11653. #endif
  11654. if (ret >= 0) {
  11655. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  11656. mgf[0], -2, key);
  11657. }
  11658. } while (ret == WC_PENDING_E);
  11659. if (ret != PSS_SALTLEN_E)
  11660. ERROR_OUT(-7742, exit_rsa_pss);
  11661. TEST_SLEEP();
  11662. do {
  11663. #if defined(WOLFSSL_ASYNC_CRYPT)
  11664. ret = wc_AsyncWait(ret, &key->asyncDev,
  11665. WC_ASYNC_FLAG_CALL_AGAIN);
  11666. #endif
  11667. if (ret >= 0) {
  11668. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  11669. digestSz + 1, key);
  11670. }
  11671. } while (ret == WC_PENDING_E);
  11672. if (ret != PSS_SALTLEN_E)
  11673. ERROR_OUT(-7743, exit_rsa_pss);
  11674. TEST_SLEEP();
  11675. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  11676. len = -2;
  11677. #else
  11678. len = -3;
  11679. #endif
  11680. #if defined(HAVE_SELFTEST) && \
  11681. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  11682. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11683. len);
  11684. #else
  11685. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11686. len, 0);
  11687. #endif
  11688. if (ret != PSS_SALTLEN_E)
  11689. ERROR_OUT(-7744, exit_rsa_pss);
  11690. #ifndef WOLFSSL_PSS_LONG_SALT
  11691. len = digestSz + 1;
  11692. #else
  11693. len = plainSz - digestSz - 1;
  11694. #endif
  11695. #if defined(HAVE_SELFTEST) && \
  11696. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  11697. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11698. len);
  11699. #else
  11700. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11701. len, 0);
  11702. #endif
  11703. if (ret != PSS_SALTLEN_E)
  11704. ERROR_OUT(-7745, exit_rsa_pss);
  11705. ret = 0;
  11706. exit_rsa_pss:
  11707. FREE_VAR(sig, HEAP_HINT);
  11708. FREE_VAR(in, HEAP_HINT);
  11709. FREE_VAR(out, HEAP_HINT);
  11710. return ret;
  11711. }
  11712. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  11713. #endif
  11714. #ifdef WC_RSA_NO_PADDING
  11715. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void)
  11716. {
  11717. WC_RNG rng;
  11718. byte* tmp = NULL;
  11719. size_t bytes;
  11720. int ret;
  11721. word32 inLen = 0;
  11722. word32 idx = 0;
  11723. word32 outSz = RSA_TEST_BYTES;
  11724. word32 plainSz = RSA_TEST_BYTES;
  11725. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  11726. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  11727. !defined(NO_FILESYSTEM)
  11728. XFILE file;
  11729. #endif
  11730. DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  11731. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  11732. DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  11733. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  11734. if (key == NULL || out == NULL || plain == NULL)
  11735. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  11736. #endif
  11737. /* initialize stack structures */
  11738. XMEMSET(&rng, 0, sizeof(rng));
  11739. XMEMSET(key, 0, sizeof(RsaKey));
  11740. #ifdef USE_CERT_BUFFERS_1024
  11741. bytes = (size_t)sizeof_client_key_der_1024;
  11742. if (bytes < (size_t)sizeof_client_cert_der_1024)
  11743. bytes = (size_t)sizeof_client_cert_der_1024;
  11744. #elif defined(USE_CERT_BUFFERS_2048)
  11745. bytes = (size_t)sizeof_client_key_der_2048;
  11746. if (bytes < (size_t)sizeof_client_cert_der_2048)
  11747. bytes = (size_t)sizeof_client_cert_der_2048;
  11748. #else
  11749. bytes = FOURK_BUF;
  11750. #endif
  11751. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11752. if (tmp == NULL
  11753. #ifdef WOLFSSL_ASYNC_CRYPT
  11754. || out == NULL || plain == NULL
  11755. #endif
  11756. ) {
  11757. ERROR_OUT(-7800, exit_rsa_nopadding);
  11758. }
  11759. #ifdef USE_CERT_BUFFERS_1024
  11760. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  11761. #elif defined(USE_CERT_BUFFERS_2048)
  11762. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  11763. #elif defined(USE_CERT_BUFFERS_3072)
  11764. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  11765. #elif defined(USE_CERT_BUFFERS_4096)
  11766. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  11767. #elif !defined(NO_FILESYSTEM)
  11768. file = XFOPEN(clientKey, "rb");
  11769. if (!file) {
  11770. err_sys("can't open ./certs/client-key.der, "
  11771. "Please run from wolfSSL home dir", -40);
  11772. ERROR_OUT(-7801, exit_rsa_nopadding);
  11773. }
  11774. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11775. XFCLOSE(file);
  11776. #else
  11777. /* No key to use. */
  11778. ERROR_OUT(-7802, exit_rsa_nopadding);
  11779. #endif /* USE_CERT_BUFFERS */
  11780. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  11781. if (ret != 0) {
  11782. ERROR_OUT(-7803, exit_rsa_nopadding);
  11783. }
  11784. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  11785. if (ret != 0) {
  11786. ERROR_OUT(-7804, exit_rsa_nopadding);
  11787. }
  11788. /* after loading in key use tmp as the test buffer */
  11789. #ifndef HAVE_FIPS
  11790. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  11791. #else
  11792. ret = wc_InitRng(&rng);
  11793. #endif
  11794. if (ret != 0) {
  11795. ERROR_OUT(-7805, exit_rsa_nopadding);
  11796. }
  11797. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  11798. inLen = wc_RsaEncryptSize(key);
  11799. outSz = inLen;
  11800. plainSz = inLen;
  11801. XMEMSET(tmp, 7, inLen);
  11802. do {
  11803. #if defined(WOLFSSL_ASYNC_CRYPT)
  11804. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11805. #endif
  11806. if (ret >= 0) {
  11807. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  11808. RSA_PRIVATE_ENCRYPT, &rng);
  11809. }
  11810. } while (ret == WC_PENDING_E);
  11811. if (ret <= 0) {
  11812. ERROR_OUT(-7806, exit_rsa_nopadding);
  11813. }
  11814. /* encrypted result should not be the same as input */
  11815. if (XMEMCMP(out, tmp, inLen) == 0) {
  11816. ERROR_OUT(-7807, exit_rsa_nopadding);
  11817. }
  11818. TEST_SLEEP();
  11819. /* decrypt with public key and compare result */
  11820. do {
  11821. #if defined(WOLFSSL_ASYNC_CRYPT)
  11822. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11823. #endif
  11824. if (ret >= 0) {
  11825. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  11826. RSA_PUBLIC_DECRYPT, &rng);
  11827. }
  11828. } while (ret == WC_PENDING_E);
  11829. if (ret <= 0) {
  11830. ERROR_OUT(-7808, exit_rsa_nopadding);
  11831. }
  11832. if (XMEMCMP(plain, tmp, inLen) != 0) {
  11833. ERROR_OUT(-7809, exit_rsa_nopadding);
  11834. }
  11835. TEST_SLEEP();
  11836. #endif
  11837. #ifdef WC_RSA_BLINDING
  11838. ret = wc_RsaSetRNG(NULL, &rng);
  11839. if (ret != BAD_FUNC_ARG) {
  11840. ERROR_OUT(-7810, exit_rsa_nopadding);
  11841. }
  11842. ret = wc_RsaSetRNG(key, &rng);
  11843. if (ret < 0) {
  11844. ERROR_OUT(-7811, exit_rsa_nopadding);
  11845. }
  11846. #endif
  11847. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  11848. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  11849. do {
  11850. #if defined(WOLFSSL_ASYNC_CRYPT)
  11851. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11852. #endif
  11853. if (ret >= 0) {
  11854. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, (int)outSz, key, &rng,
  11855. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  11856. }
  11857. } while (ret == WC_PENDING_E);
  11858. if (ret < 0) {
  11859. ERROR_OUT(-7812, exit_rsa_nopadding);
  11860. }
  11861. TEST_SLEEP();
  11862. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  11863. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  11864. do {
  11865. #if defined(WOLFSSL_ASYNC_CRYPT)
  11866. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11867. #endif
  11868. if (ret >= 0) {
  11869. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, (int)plainSz, key,
  11870. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  11871. }
  11872. } while (ret == WC_PENDING_E);
  11873. if (ret < 0) {
  11874. ERROR_OUT(-7813, exit_rsa_nopadding);
  11875. }
  11876. if (XMEMCMP(plain, tmp, inLen) != 0) {
  11877. ERROR_OUT(-7814, exit_rsa_nopadding);
  11878. }
  11879. TEST_SLEEP();
  11880. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  11881. /* test some bad arguments */
  11882. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  11883. &rng);
  11884. if (ret != BAD_FUNC_ARG) {
  11885. ERROR_OUT(-7815, exit_rsa_nopadding);
  11886. }
  11887. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  11888. &rng);
  11889. if (ret != BAD_FUNC_ARG) {
  11890. ERROR_OUT(-7816, exit_rsa_nopadding);
  11891. }
  11892. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  11893. &rng);
  11894. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  11895. ERROR_OUT(-7817, exit_rsa_nopadding);
  11896. }
  11897. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  11898. RSA_PUBLIC_DECRYPT, &rng);
  11899. if (ret != BAD_FUNC_ARG) {
  11900. ERROR_OUT(-7818, exit_rsa_nopadding);
  11901. }
  11902. /* if making it to this point of code without hitting an ERROR_OUT then
  11903. * all tests have passed */
  11904. ret = 0;
  11905. exit_rsa_nopadding:
  11906. wc_FreeRsaKey(key);
  11907. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11908. FREE_VAR(key, HEAP_HINT);
  11909. FREE_VAR(out, HEAP_HINT);
  11910. FREE_VAR(plain, HEAP_HINT);
  11911. wc_FreeRng(&rng);
  11912. return ret;
  11913. }
  11914. #endif /* WC_RSA_NO_PADDING */
  11915. #ifdef WOLFSSL_HAVE_SP_RSA
  11916. static int rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  11917. {
  11918. byte* tmp = NULL;
  11919. size_t bytes;
  11920. int ret;
  11921. word32 inLen = 0;
  11922. #ifndef NO_ASN
  11923. word32 idx = 0;
  11924. #endif
  11925. word32 outSz = RSA_TEST_BYTES;
  11926. word32 plainSz = RSA_TEST_BYTES;
  11927. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  11928. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  11929. XFILE file;
  11930. #endif
  11931. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  11932. DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  11933. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  11934. if (out == NULL || plain == NULL)
  11935. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  11936. #endif
  11937. #if defined(USE_CERT_BUFFERS_2048)
  11938. bytes = (size_t)sizeof_client_key_der_2048;
  11939. if (bytes < (size_t)sizeof_client_cert_der_2048)
  11940. bytes = (size_t)sizeof_client_cert_der_2048;
  11941. #else
  11942. bytes = FOURK_BUF;
  11943. #endif
  11944. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11945. if (tmp == NULL
  11946. #ifdef WOLFSSL_ASYNC_CRYPT
  11947. || out == NULL || plain == NULL
  11948. #endif
  11949. ) {
  11950. ERROR_OUT(-7800, exit_rsa_even_mod);
  11951. }
  11952. #if defined(USE_CERT_BUFFERS_2048)
  11953. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  11954. #elif defined(USE_CERT_BUFFERS_3072)
  11955. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  11956. #elif defined(USE_CERT_BUFFERS_4096)
  11957. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  11958. #elif !defined(NO_FILESYSTEM)
  11959. file = XFOPEN(clientKey, "rb");
  11960. if (!file) {
  11961. err_sys("can't open ./certs/client-key.der, "
  11962. "Please run from wolfSSL home dir", -40);
  11963. ERROR_OUT(-7801, exit_rsa_even_mod);
  11964. }
  11965. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11966. XFCLOSE(file);
  11967. #else
  11968. /* No key to use. */
  11969. ERROR_OUT(-7802, exit_rsa_even_mod);
  11970. #endif /* USE_CERT_BUFFERS */
  11971. #ifndef NO_ASN
  11972. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  11973. if (ret != 0) {
  11974. ERROR_OUT(-7804, exit_rsa_even_mod);
  11975. }
  11976. #else
  11977. #ifdef USE_CERT_BUFFERS_2048
  11978. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  11979. if (ret != 0) {
  11980. ERROR_OUT(-7804, exit_rsa_even_mod);
  11981. }
  11982. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  11983. if (ret != 0) {
  11984. ERROR_OUT(-7804, exit_rsa_even_mod);
  11985. }
  11986. #ifndef NO_SIG_WRAPPER
  11987. modLen = 2048;
  11988. #endif
  11989. #else
  11990. #error Not supported yet!
  11991. #endif
  11992. #endif
  11993. key->n.dp[0] &= (mp_digit)-2;
  11994. if (ret != 0) {
  11995. ERROR_OUT(-7804, exit_rsa_even_mod);
  11996. }
  11997. /* after loading in key use tmp as the test buffer */
  11998. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  11999. inLen = 32;
  12000. outSz = wc_RsaEncryptSize(key);
  12001. XMEMSET(tmp, 7, plainSz);
  12002. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  12003. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  12004. ERROR_OUT(-7806, exit_rsa_even_mod);
  12005. }
  12006. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  12007. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  12008. ERROR_OUT(-7808, exit_rsa_even_mod);
  12009. }
  12010. #endif
  12011. #ifdef WC_RSA_BLINDING
  12012. ret = wc_RsaSetRNG(key, rng);
  12013. if (ret < 0) {
  12014. ERROR_OUT(-7811, exit_rsa_even_mod);
  12015. }
  12016. #endif
  12017. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  12018. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  12019. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  12020. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  12021. ERROR_OUT(-7812, exit_rsa_even_mod);
  12022. }
  12023. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  12024. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12025. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  12026. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  12027. ERROR_OUT(-7813, exit_rsa_even_mod);
  12028. }
  12029. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  12030. /* if making it to this point of code without hitting an ERROR_OUT then
  12031. * all tests have passed */
  12032. ret = 0;
  12033. exit_rsa_even_mod:
  12034. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12035. FREE_VAR(out, HEAP_HINT);
  12036. FREE_VAR(plain, HEAP_HINT);
  12037. (void)out;
  12038. (void)outSz;
  12039. (void)plain;
  12040. (void)plainSz;
  12041. (void)inLen;
  12042. (void)rng;
  12043. return ret;
  12044. }
  12045. #endif /* WOLFSSL_HAVE_SP_RSA */
  12046. #ifdef WOLFSSL_CERT_GEN
  12047. static int rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  12048. {
  12049. #ifdef WOLFSSL_SMALL_STACK
  12050. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12051. #ifdef WOLFSSL_TEST_CERT
  12052. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12053. #endif
  12054. #else
  12055. RsaKey caKey[1];
  12056. #ifdef WOLFSSL_TEST_CERT
  12057. DecodedCert decode[1];
  12058. #endif
  12059. #endif
  12060. byte* der = NULL;
  12061. int ret;
  12062. Cert* myCert = NULL;
  12063. int certSz;
  12064. size_t bytes3;
  12065. word32 idx3 = 0;
  12066. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  12067. XFILE file3;
  12068. #endif
  12069. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  12070. struct tm beforeTime;
  12071. struct tm afterTime;
  12072. #endif
  12073. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  12074. (void)keypub;
  12075. #ifdef WOLFSSL_SMALL_STACK
  12076. if (caKey == NULL)
  12077. ERROR_OUT(MEMORY_E, exit_rsa);
  12078. #ifdef WOLFSSL_TEST_CERT
  12079. if (decode == NULL)
  12080. ERROR_OUT(MEMORY_E, exit_rsa);
  12081. #endif
  12082. #endif
  12083. XMEMSET(caKey, 0, sizeof *caKey);
  12084. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12085. if (der == NULL) {
  12086. ERROR_OUT(-7820, exit_rsa);
  12087. }
  12088. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12089. if (myCert == NULL) {
  12090. ERROR_OUT(-7821, exit_rsa);
  12091. }
  12092. /* self signed */
  12093. if (wc_InitCert(myCert)) {
  12094. ERROR_OUT(-7822, exit_rsa);
  12095. }
  12096. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  12097. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  12098. myCert->serialSz = (int)sizeof(mySerial);
  12099. myCert->isCA = 1;
  12100. #ifndef NO_SHA256
  12101. myCert->sigType = CTC_SHA256wRSA;
  12102. #else
  12103. myCert->sigType = CTC_SHAwRSA;
  12104. #endif
  12105. #ifdef WOLFSSL_CERT_EXT
  12106. /* add Policies */
  12107. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  12108. CTC_MAX_CERTPOL_SZ);
  12109. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  12110. CTC_MAX_CERTPOL_SZ);
  12111. myCert->certPoliciesNb = 2;
  12112. /* add SKID from the Public Key */
  12113. if (wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  12114. ERROR_OUT(-7823, exit_rsa);
  12115. }
  12116. /* add AKID from the Public Key */
  12117. if (wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  12118. ERROR_OUT(-7824, exit_rsa);
  12119. }
  12120. /* add Key Usage */
  12121. if (wc_SetKeyUsage(myCert,"cRLSign,keyCertSign") != 0) {
  12122. ERROR_OUT(-7825, exit_rsa);
  12123. }
  12124. #ifdef WOLFSSL_EKU_OID
  12125. {
  12126. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  12127. if (wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  12128. HEAP_HINT) != 0) {
  12129. ERROR_OUT(-7826, exit_rsa);
  12130. }
  12131. }
  12132. #endif /* WOLFSSL_EKU_OID */
  12133. #endif /* WOLFSSL_CERT_EXT */
  12134. ret = 0;
  12135. do {
  12136. #if defined(WOLFSSL_ASYNC_CRYPT)
  12137. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12138. #endif
  12139. if (ret >= 0) {
  12140. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  12141. }
  12142. } while (ret == WC_PENDING_E);
  12143. if (ret < 0) {
  12144. ERROR_OUT(-7827, exit_rsa);
  12145. }
  12146. certSz = ret;
  12147. #ifdef WOLFSSL_TEST_CERT
  12148. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  12149. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  12150. if (ret != 0) {
  12151. FreeDecodedCert(decode);
  12152. ERROR_OUT(-7828, exit_rsa);
  12153. }
  12154. FreeDecodedCert(decode);
  12155. #endif
  12156. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  12157. CERT_TYPE, -5578);
  12158. if (ret != 0) {
  12159. goto exit_rsa;
  12160. }
  12161. /* Setup Certificate */
  12162. if (wc_InitCert(myCert)) {
  12163. ERROR_OUT(-7829, exit_rsa);
  12164. }
  12165. #ifdef WOLFSSL_ALT_NAMES
  12166. /* Get CA Cert for testing */
  12167. #ifdef USE_CERT_BUFFERS_1024
  12168. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  12169. bytes3 = sizeof_ca_cert_der_1024;
  12170. #elif defined(USE_CERT_BUFFERS_2048)
  12171. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  12172. bytes3 = sizeof_ca_cert_der_2048;
  12173. #else
  12174. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  12175. if (!file3) {
  12176. ERROR_OUT(-7830, exit_rsa);
  12177. }
  12178. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  12179. XFCLOSE(file3);
  12180. #endif /* USE_CERT_BUFFERS */
  12181. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  12182. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  12183. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  12184. if (ret != 0) {
  12185. ERROR_OUT(-7831, exit_rsa);
  12186. }
  12187. #endif
  12188. /* get alt names from der */
  12189. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  12190. if (ret != 0) {
  12191. ERROR_OUT(-7832, exit_rsa);
  12192. }
  12193. /* get dates from der */
  12194. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  12195. if (ret != 0) {
  12196. ERROR_OUT(-7833, exit_rsa);
  12197. }
  12198. #ifndef NO_ASN_TIME
  12199. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  12200. if (ret < 0) {
  12201. ERROR_OUT(-7834, exit_rsa);
  12202. }
  12203. #endif
  12204. #endif /* WOLFSSL_ALT_NAMES */
  12205. /* Get CA Key */
  12206. #ifdef USE_CERT_BUFFERS_1024
  12207. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  12208. bytes3 = sizeof_ca_key_der_1024;
  12209. #elif defined(USE_CERT_BUFFERS_2048)
  12210. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  12211. bytes3 = sizeof_ca_key_der_2048;
  12212. #else
  12213. file3 = XFOPEN(rsaCaKeyFile, "rb");
  12214. if (!file3) {
  12215. ERROR_OUT(-7835, exit_rsa);
  12216. }
  12217. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  12218. XFCLOSE(file3);
  12219. #endif /* USE_CERT_BUFFERS */
  12220. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  12221. if (ret != 0) {
  12222. ERROR_OUT(-7836, exit_rsa);
  12223. }
  12224. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  12225. if (ret != 0) {
  12226. ERROR_OUT(-7837, exit_rsa);
  12227. }
  12228. #ifndef NO_SHA256
  12229. myCert->sigType = CTC_SHA256wRSA;
  12230. #else
  12231. myCert->sigType = CTC_SHAwRSA;
  12232. #endif
  12233. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  12234. #ifdef WOLFSSL_CERT_EXT
  12235. /* add Policies */
  12236. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  12237. CTC_MAX_CERTPOL_SZ);
  12238. myCert->certPoliciesNb =1;
  12239. /* add SKID from the Public Key */
  12240. if (wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL) != 0) {
  12241. ERROR_OUT(-7838, exit_rsa);
  12242. }
  12243. /* add AKID from the CA certificate */
  12244. #if defined(USE_CERT_BUFFERS_2048)
  12245. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  12246. sizeof_ca_cert_der_2048);
  12247. #elif defined(USE_CERT_BUFFERS_1024)
  12248. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  12249. sizeof_ca_cert_der_1024);
  12250. #else
  12251. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  12252. #endif
  12253. if (ret != 0) {
  12254. ERROR_OUT(-7839, exit_rsa);
  12255. }
  12256. /* add Key Usage */
  12257. if (wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement") != 0) {
  12258. ERROR_OUT(-7840, exit_rsa);
  12259. }
  12260. #endif /* WOLFSSL_CERT_EXT */
  12261. #if defined(USE_CERT_BUFFERS_2048)
  12262. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  12263. sizeof_ca_cert_der_2048);
  12264. #elif defined(USE_CERT_BUFFERS_1024)
  12265. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  12266. sizeof_ca_cert_der_1024);
  12267. #else
  12268. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  12269. #endif
  12270. if (ret < 0) {
  12271. ERROR_OUT(-7841, exit_rsa);
  12272. }
  12273. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  12274. if (certSz < 0) {
  12275. ERROR_OUT(-7842, exit_rsa);
  12276. }
  12277. ret = 0;
  12278. do {
  12279. #if defined(WOLFSSL_ASYNC_CRYPT)
  12280. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12281. #endif
  12282. if (ret >= 0) {
  12283. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  12284. caKey, NULL, rng);
  12285. }
  12286. } while (ret == WC_PENDING_E);
  12287. if (ret < 0) {
  12288. ERROR_OUT(-7843, exit_rsa);
  12289. }
  12290. certSz = ret;
  12291. #ifdef WOLFSSL_TEST_CERT
  12292. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  12293. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  12294. if (ret != 0) {
  12295. FreeDecodedCert(decode);
  12296. ERROR_OUT(-7844, exit_rsa);
  12297. }
  12298. FreeDecodedCert(decode);
  12299. #endif
  12300. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  12301. CERT_TYPE, -5598);
  12302. if (ret != 0) {
  12303. goto exit_rsa;
  12304. }
  12305. exit_rsa:
  12306. #ifdef WOLFSSL_SMALL_STACK
  12307. if (caKey != NULL) {
  12308. wc_FreeRsaKey(caKey);
  12309. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12310. }
  12311. #ifdef WOLFSSL_TEST_CERT
  12312. if (decode != NULL)
  12313. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12314. #endif
  12315. #else
  12316. wc_FreeRsaKey(caKey);
  12317. #endif
  12318. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12319. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12320. return ret;
  12321. }
  12322. #endif
  12323. #if !defined(NO_RSA) && defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  12324. /* Make Cert / Sign example for ECC cert and RSA CA */
  12325. static int rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  12326. {
  12327. #ifdef WOLFSSL_SMALL_STACK
  12328. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12329. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12330. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12331. #ifdef WOLFSSL_TEST_CERT
  12332. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12333. #endif
  12334. #else
  12335. RsaKey caKey[1];
  12336. ecc_key caEccKey[1];
  12337. ecc_key caEccKeyPub[1];
  12338. #ifdef WOLFSSL_TEST_CERT
  12339. DecodedCert decode[1];
  12340. #endif
  12341. #endif
  12342. byte* der = NULL;
  12343. Cert* myCert = NULL;
  12344. int certSz;
  12345. size_t bytes3;
  12346. word32 idx3 = 0;
  12347. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  12348. || !defined(USE_CERT_BUFFERS_256)
  12349. XFILE file3;
  12350. #endif
  12351. int ret;
  12352. #ifdef WOLFSSL_SMALL_STACK
  12353. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  12354. #ifdef WOLFSSL_TEST_CERT
  12355. || (decode == NULL)
  12356. #endif
  12357. )
  12358. ERROR_OUT(MEMORY_E, exit_rsa);
  12359. #endif
  12360. XMEMSET(caKey, 0, sizeof *caKey);
  12361. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  12362. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  12363. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12364. if (der == NULL) {
  12365. ERROR_OUT(-7850, exit_rsa);
  12366. }
  12367. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12368. if (myCert == NULL) {
  12369. ERROR_OUT(-7851, exit_rsa);
  12370. }
  12371. /* Get CA Key */
  12372. #ifdef USE_CERT_BUFFERS_1024
  12373. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  12374. bytes3 = sizeof_ca_key_der_1024;
  12375. #elif defined(USE_CERT_BUFFERS_2048)
  12376. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  12377. bytes3 = sizeof_ca_key_der_2048;
  12378. #else
  12379. file3 = XFOPEN(rsaCaKeyFile, "rb");
  12380. if (!file3) {
  12381. ERROR_OUT(-7852, exit_rsa);
  12382. }
  12383. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  12384. XFCLOSE(file3);
  12385. #endif /* USE_CERT_BUFFERS */
  12386. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  12387. if (ret != 0) {
  12388. ERROR_OUT(-7853, exit_rsa);
  12389. }
  12390. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  12391. if (ret != 0) {
  12392. ERROR_OUT(-7854, exit_rsa);
  12393. }
  12394. /* Get Cert Key */
  12395. #ifdef USE_CERT_BUFFERS_256
  12396. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  12397. bytes3 = sizeof_ecc_key_pub_der_256;
  12398. #else
  12399. file3 = XFOPEN(eccKeyPubFile, "rb");
  12400. if (!file3) {
  12401. ERROR_OUT(-7855, exit_rsa);
  12402. }
  12403. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  12404. XFCLOSE(file3);
  12405. #endif
  12406. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  12407. if (ret != 0) {
  12408. ERROR_OUT(-7856, exit_rsa);
  12409. }
  12410. idx3 = 0;
  12411. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  12412. if (ret != 0) {
  12413. ERROR_OUT(-7857, exit_rsa);
  12414. }
  12415. /* Setup Certificate */
  12416. if (wc_InitCert(myCert)) {
  12417. ERROR_OUT(-7858, exit_rsa);
  12418. }
  12419. #ifndef NO_SHA256
  12420. myCert->sigType = CTC_SHA256wRSA;
  12421. #else
  12422. myCert->sigType = CTC_SHAwRSA;
  12423. #endif
  12424. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  12425. #ifdef WOLFSSL_CERT_EXT
  12426. /* add Policies */
  12427. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  12428. CTC_MAX_CERTPOL_SZ);
  12429. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  12430. CTC_MAX_CERTPOL_SZ);
  12431. myCert->certPoliciesNb = 2;
  12432. /* add SKID from the Public Key */
  12433. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub) != 0) {
  12434. ERROR_OUT(-7859, exit_rsa);
  12435. }
  12436. /* add AKID from the CA certificate */
  12437. #if defined(USE_CERT_BUFFERS_2048)
  12438. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  12439. sizeof_ca_cert_der_2048);
  12440. #elif defined(USE_CERT_BUFFERS_1024)
  12441. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  12442. sizeof_ca_cert_der_1024);
  12443. #else
  12444. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  12445. #endif
  12446. if (ret != 0) {
  12447. ERROR_OUT(-7860, exit_rsa);
  12448. }
  12449. /* add Key Usage */
  12450. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  12451. ERROR_OUT(-7861, exit_rsa);
  12452. }
  12453. #endif /* WOLFSSL_CERT_EXT */
  12454. #if defined(USE_CERT_BUFFERS_2048)
  12455. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  12456. sizeof_ca_cert_der_2048);
  12457. #elif defined(USE_CERT_BUFFERS_1024)
  12458. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  12459. sizeof_ca_cert_der_1024);
  12460. #else
  12461. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  12462. #endif
  12463. if (ret < 0) {
  12464. ERROR_OUT(-7862, exit_rsa);
  12465. }
  12466. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  12467. if (certSz < 0) {
  12468. ERROR_OUT(-7863, exit_rsa);
  12469. }
  12470. ret = 0;
  12471. do {
  12472. #if defined(WOLFSSL_ASYNC_CRYPT)
  12473. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12474. #endif
  12475. if (ret >= 0) {
  12476. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  12477. FOURK_BUF, caKey, NULL, rng);
  12478. }
  12479. } while (ret == WC_PENDING_E);
  12480. if (ret < 0) {
  12481. ERROR_OUT(-7864, exit_rsa);
  12482. }
  12483. certSz = ret;
  12484. #ifdef WOLFSSL_TEST_CERT
  12485. InitDecodedCert(decode, der, certSz, 0);
  12486. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  12487. if (ret != 0) {
  12488. FreeDecodedCert(decode);
  12489. ERROR_OUT(-7865, exit_rsa);
  12490. }
  12491. FreeDecodedCert(decode);
  12492. #endif
  12493. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  12494. CERT_TYPE, -5616);
  12495. if (ret != 0) {
  12496. goto exit_rsa;
  12497. }
  12498. exit_rsa:
  12499. #ifdef WOLFSSL_SMALL_STACK
  12500. if (caKey != NULL) {
  12501. wc_FreeRsaKey(caKey);
  12502. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12503. }
  12504. if (caEccKey != NULL) {
  12505. wc_ecc_free(caEccKey);
  12506. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12507. }
  12508. if (caEccKeyPub != NULL) {
  12509. wc_ecc_free(caEccKeyPub);
  12510. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12511. }
  12512. #ifdef WOLFSSL_TEST_CERT
  12513. if (decode != NULL)
  12514. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12515. #endif
  12516. #else
  12517. wc_FreeRsaKey(caKey);
  12518. wc_ecc_free(caEccKey);
  12519. wc_ecc_free(caEccKeyPub);
  12520. #endif
  12521. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12522. myCert = NULL;
  12523. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12524. der = NULL;
  12525. if (ret >= 0)
  12526. ret = 0;
  12527. return ret;
  12528. }
  12529. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  12530. #ifdef WOLFSSL_KEY_GEN
  12531. static int rsa_keygen_test(WC_RNG* rng)
  12532. {
  12533. #ifdef WOLFSSL_SMALL_STACK
  12534. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12535. #else
  12536. RsaKey genKey[1];
  12537. #endif
  12538. int ret;
  12539. byte* der = NULL;
  12540. word32 idx = 0;
  12541. int derSz = 0;
  12542. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  12543. int keySz = 1024;
  12544. #else
  12545. int keySz = 2048;
  12546. #endif
  12547. #ifdef WOLFSSL_SMALL_STACK
  12548. if (! genKey)
  12549. ERROR_OUT(MEMORY_E, exit_rsa);
  12550. #endif
  12551. XMEMSET(genKey, 0, sizeof *genKey);
  12552. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  12553. if (ret != 0) {
  12554. ERROR_OUT(-7870, exit_rsa);
  12555. }
  12556. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  12557. #if defined(WOLFSSL_ASYNC_CRYPT)
  12558. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  12559. #endif
  12560. if (ret != 0) {
  12561. ERROR_OUT(-7871, exit_rsa);
  12562. }
  12563. TEST_SLEEP();
  12564. /* If not using old FIPS, or not using FAST or USER RSA... */
  12565. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  12566. (!defined(HAVE_FIPS) || \
  12567. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  12568. !defined(HAVE_SELFTEST) && !defined(HAVE_INTEL_QA)
  12569. ret = wc_CheckRsaKey(genKey);
  12570. if (ret != 0) {
  12571. ERROR_OUT(-7872, exit_rsa);
  12572. }
  12573. #endif
  12574. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12575. if (der == NULL) {
  12576. ERROR_OUT(-7873, exit_rsa);
  12577. }
  12578. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  12579. if (derSz < 0) {
  12580. ERROR_OUT(-7874, exit_rsa);
  12581. }
  12582. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  12583. PRIVATEKEY_TYPE, -5555);
  12584. if (ret != 0) {
  12585. goto exit_rsa;
  12586. }
  12587. wc_FreeRsaKey(genKey);
  12588. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  12589. if (ret != 0) {
  12590. ERROR_OUT(-7875, exit_rsa);
  12591. }
  12592. idx = 0;
  12593. #if !defined(WOLFSSL_CRYPTOCELL)
  12594. /* The private key part of the key gen pairs from cryptocell can't be exported */
  12595. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, derSz);
  12596. if (ret != 0) {
  12597. ERROR_OUT(-7876, exit_rsa);
  12598. }
  12599. #endif /* WOLFSSL_CRYPTOCELL */
  12600. exit_rsa:
  12601. #ifdef WOLFSSL_SMALL_STACK
  12602. if (genKey) {
  12603. wc_FreeRsaKey(genKey);
  12604. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12605. }
  12606. #else
  12607. wc_FreeRsaKey(genKey);
  12608. #endif
  12609. if (der != NULL) {
  12610. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12611. der = NULL;
  12612. }
  12613. return ret;
  12614. }
  12615. #endif
  12616. WOLFSSL_TEST_SUBROUTINE int rsa_test(void)
  12617. {
  12618. int ret;
  12619. byte* tmp = NULL;
  12620. byte* der = NULL;
  12621. size_t bytes;
  12622. WC_RNG rng;
  12623. #ifdef WOLFSSL_SMALL_STACK
  12624. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12625. #else
  12626. RsaKey key[1];
  12627. #endif
  12628. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  12629. #ifdef WOLFSSL_SMALL_STACK
  12630. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12631. #else
  12632. RsaKey keypub[1];
  12633. #endif
  12634. #endif
  12635. #if defined(HAVE_NTRU)
  12636. #ifdef WOLFSSL_SMALL_STACK
  12637. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12638. #else
  12639. RsaKey caKey[1];
  12640. #endif
  12641. #endif
  12642. #if !defined(NO_ASN) || !defined(WOLFSSL_RSA_PUBLIC_ONLY) \
  12643. || defined(WOLFSSL_PUBLIC_MP)
  12644. word32 idx = 0;
  12645. #endif
  12646. #if (!defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_PUBLIC_MP)) && \
  12647. !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  12648. const char inStr[] = TEST_STRING;
  12649. const word32 inLen = (word32)TEST_STRING_SZ;
  12650. const word32 outSz = RSA_TEST_BYTES;
  12651. const word32 plainSz = RSA_TEST_BYTES;
  12652. #endif
  12653. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_PUBLIC_MP)
  12654. byte* res;
  12655. #endif
  12656. #ifndef NO_SIG_WRAPPER
  12657. int modLen;
  12658. #endif
  12659. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  12660. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  12661. !defined(NO_FILESYSTEM)
  12662. XFILE file;
  12663. XFILE file2;
  12664. #endif
  12665. #ifdef WOLFSSL_TEST_CERT
  12666. #ifdef WOLFSSL_SMALL_STACK
  12667. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12668. #else
  12669. DecodedCert cert[1];
  12670. #endif
  12671. #endif
  12672. #if (!defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_PUBLIC_MP)) && \
  12673. !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  12674. DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  12675. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  12676. DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  12677. #endif
  12678. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  12679. if (in == NULL || out == NULL || plain == NULL)
  12680. ERROR_OUT(MEMORY_E, exit_rsa);
  12681. #endif
  12682. XMEMCPY(in, inStr, inLen);
  12683. #ifdef WOLFSSL_SMALL_STACK
  12684. if (key == NULL)
  12685. ERROR_OUT(MEMORY_E, exit_rsa);
  12686. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  12687. if (keypub == NULL)
  12688. ERROR_OUT(MEMORY_E, exit_rsa);
  12689. #endif
  12690. #if defined(HAVE_NTRU)
  12691. if (caKey == NULL)
  12692. ERROR_OUT(MEMORY_E, exit_rsa);
  12693. #endif
  12694. #ifdef WOLFSSL_TEST_CERT
  12695. if (cert == NULL)
  12696. ERROR_OUT(MEMORY_E, exit_rsa);
  12697. #endif
  12698. #endif /* WOLFSSL_SMALL_STACK */
  12699. /* initialize stack structures */
  12700. XMEMSET(&rng, 0, sizeof(rng));
  12701. XMEMSET(key, 0, sizeof *key);
  12702. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  12703. XMEMSET(keypub, 0, sizeof *keypub);
  12704. #endif
  12705. #if defined(HAVE_NTRU)
  12706. XMEMSET(caKey, 0, sizeof *caKey);
  12707. #endif
  12708. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  12709. ret = rsa_decode_test(key);
  12710. if (ret != 0)
  12711. ERROR_OUT(ret, exit_rsa);
  12712. #endif
  12713. #ifdef USE_CERT_BUFFERS_1024
  12714. bytes = (size_t)sizeof_client_key_der_1024;
  12715. if (bytes < (size_t)sizeof_client_cert_der_1024)
  12716. bytes = (size_t)sizeof_client_cert_der_1024;
  12717. #elif defined(USE_CERT_BUFFERS_2048)
  12718. bytes = (size_t)sizeof_client_key_der_2048;
  12719. if (bytes < (size_t)sizeof_client_cert_der_2048)
  12720. bytes = (size_t)sizeof_client_cert_der_2048;
  12721. #elif defined(USE_CERT_BUFFERS_3072)
  12722. bytes = (size_t)sizeof_client_key_der_3072;
  12723. if (bytes < (size_t)sizeof_client_cert_der_3072)
  12724. bytes = (size_t)sizeof_client_cert_der_3072;
  12725. #elif defined(USE_CERT_BUFFERS_4096)
  12726. bytes = (size_t)sizeof_client_key_der_4096;
  12727. if (bytes < (size_t)sizeof_client_cert_der_4096)
  12728. bytes = (size_t)sizeof_client_cert_der_4096;
  12729. #else
  12730. bytes = FOURK_BUF;
  12731. #endif
  12732. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12733. if (tmp == NULL)
  12734. ERROR_OUT(-7900, exit_rsa);
  12735. #ifdef USE_CERT_BUFFERS_1024
  12736. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  12737. #elif defined(USE_CERT_BUFFERS_2048)
  12738. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  12739. #elif defined(USE_CERT_BUFFERS_3072)
  12740. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  12741. #elif defined(USE_CERT_BUFFERS_4096)
  12742. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  12743. #elif !defined(NO_FILESYSTEM)
  12744. file = XFOPEN(clientKey, "rb");
  12745. if (!file) {
  12746. err_sys("can't open ./certs/client-key.der, "
  12747. "Please run from wolfSSL home dir", -40);
  12748. ERROR_OUT(-7901, exit_rsa);
  12749. }
  12750. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  12751. XFCLOSE(file);
  12752. #else
  12753. /* No key to use. */
  12754. ERROR_OUT(-7902, exit_rsa);
  12755. #endif /* USE_CERT_BUFFERS */
  12756. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  12757. if (ret != 0) {
  12758. ERROR_OUT(-7903, exit_rsa);
  12759. }
  12760. #ifndef NO_ASN
  12761. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  12762. if (ret != 0) {
  12763. ERROR_OUT(-7904, exit_rsa);
  12764. }
  12765. #ifndef NO_SIG_WRAPPER
  12766. modLen = wc_RsaEncryptSize(key);
  12767. #endif
  12768. #else
  12769. #ifdef USE_CERT_BUFFERS_2048
  12770. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  12771. if (ret != 0) {
  12772. ERROR_OUT(-7905, exit_rsa);
  12773. }
  12774. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  12775. if (ret != 0) {
  12776. ERROR_OUT(-7906, exit_rsa);
  12777. }
  12778. #ifndef NO_SIG_WRAPPER
  12779. modLen = 2048;
  12780. #endif
  12781. #else
  12782. #error Not supported yet!
  12783. #endif
  12784. #endif
  12785. #ifndef WC_NO_RNG
  12786. #ifndef HAVE_FIPS
  12787. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  12788. #else
  12789. ret = wc_InitRng(&rng);
  12790. #endif
  12791. if (ret != 0) {
  12792. ERROR_OUT(-7907, exit_rsa);
  12793. }
  12794. #endif
  12795. #ifndef NO_SIG_WRAPPER
  12796. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  12797. if (ret != 0)
  12798. goto exit_rsa;
  12799. #endif
  12800. #ifdef WC_RSA_NONBLOCK
  12801. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  12802. if (ret != 0)
  12803. goto exit_rsa;
  12804. #endif
  12805. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  12806. do {
  12807. #if defined(WOLFSSL_ASYNC_CRYPT)
  12808. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12809. #endif
  12810. if (ret >= 0) {
  12811. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  12812. }
  12813. } while (ret == WC_PENDING_E);
  12814. if (ret < 0) {
  12815. ERROR_OUT(-7908, exit_rsa);
  12816. }
  12817. TEST_SLEEP();
  12818. #ifdef WC_RSA_BLINDING
  12819. {
  12820. int tmpret = ret;
  12821. ret = wc_RsaSetRNG(key, &rng);
  12822. if (ret < 0) {
  12823. ERROR_OUT(-7909, exit_rsa);
  12824. }
  12825. ret = tmpret;
  12826. }
  12827. #endif
  12828. idx = (word32)ret; /* save off encrypted length */
  12829. do {
  12830. #if defined(WOLFSSL_ASYNC_CRYPT)
  12831. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12832. #endif
  12833. if (ret >= 0) {
  12834. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  12835. }
  12836. } while (ret == WC_PENDING_E);
  12837. if (ret < 0) {
  12838. ERROR_OUT(-7910, exit_rsa);
  12839. }
  12840. if (XMEMCMP(plain, in, inLen)) {
  12841. ERROR_OUT(-7911, exit_rsa);
  12842. }
  12843. TEST_SLEEP();
  12844. do {
  12845. #if defined(WOLFSSL_ASYNC_CRYPT)
  12846. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12847. #endif
  12848. if (ret >= 0) {
  12849. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  12850. }
  12851. } while (ret == WC_PENDING_E);
  12852. if (ret < 0) {
  12853. ERROR_OUT(-7912, exit_rsa);
  12854. }
  12855. if (ret != (int)inLen) {
  12856. ERROR_OUT(-7913, exit_rsa);
  12857. }
  12858. if (XMEMCMP(res, in, inLen)) {
  12859. ERROR_OUT(-7914, exit_rsa);
  12860. }
  12861. TEST_SLEEP();
  12862. do {
  12863. #if defined(WOLFSSL_ASYNC_CRYPT)
  12864. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12865. #endif
  12866. if (ret >= 0) {
  12867. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  12868. }
  12869. } while (ret == WC_PENDING_E);
  12870. if (ret < 0) {
  12871. ERROR_OUT(-7915, exit_rsa);
  12872. }
  12873. TEST_SLEEP();
  12874. #elif defined(WOLFSSL_PUBLIC_MP)
  12875. (void)outSz;
  12876. (void)inLen;
  12877. (void)res;
  12878. {
  12879. static byte signature_2048[] = {
  12880. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  12881. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  12882. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  12883. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  12884. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  12885. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  12886. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  12887. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  12888. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  12889. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  12890. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  12891. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  12892. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  12893. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  12894. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  12895. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  12896. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  12897. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  12898. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  12899. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  12900. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  12901. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  12902. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  12903. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  12904. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  12905. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  12906. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  12907. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  12908. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  12909. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  12910. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  12911. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  12912. };
  12913. ret = sizeof(signature_2048);
  12914. XMEMCPY(out, signature_2048, ret);
  12915. }
  12916. #endif
  12917. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_PUBLIC_MP)
  12918. idx = (word32)ret;
  12919. XMEMSET(plain, 0, plainSz);
  12920. do {
  12921. #if defined(WOLFSSL_ASYNC_CRYPT)
  12922. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12923. #endif
  12924. if (ret >= 0) {
  12925. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  12926. #if defined(WOLFSSL_CRYPTOCELL)
  12927. /*
  12928. Cryptocell requires the input data and signature byte array to verify.
  12929. first argument must be the input data
  12930. second argument must be the length of input data
  12931. third argument must be the signature byte array or the output from
  12932. wc_RsaSSL_Sign()
  12933. fourth argument must be the length of the signature byte array
  12934. */
  12935. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  12936. #else
  12937. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  12938. #endif /* WOLFSSL_CRYPTOCELL */
  12939. #else
  12940. byte* dec = NULL;
  12941. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  12942. if (ret > 0) {
  12943. XMEMCPY(plain, dec, ret);
  12944. }
  12945. #endif
  12946. }
  12947. } while (ret == WC_PENDING_E);
  12948. if (ret < 0) {
  12949. ERROR_OUT(-7916, exit_rsa);
  12950. }
  12951. if (XMEMCMP(plain, in, (size_t)ret)) {
  12952. ERROR_OUT(-7917, exit_rsa);
  12953. }
  12954. TEST_SLEEP();
  12955. #endif
  12956. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  12957. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  12958. /* OAEP padding testing */
  12959. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  12960. (!defined(HAVE_FIPS) || \
  12961. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  12962. #ifndef NO_SHA
  12963. XMEMSET(plain, 0, plainSz);
  12964. do {
  12965. #if defined(WOLFSSL_ASYNC_CRYPT)
  12966. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12967. #endif
  12968. if (ret >= 0) {
  12969. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  12970. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  12971. }
  12972. } while (ret == WC_PENDING_E);
  12973. if (ret < 0) {
  12974. ERROR_OUT(-7918, exit_rsa);
  12975. }
  12976. TEST_SLEEP();
  12977. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12978. idx = (word32)ret;
  12979. do {
  12980. #if defined(WOLFSSL_ASYNC_CRYPT)
  12981. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12982. #endif
  12983. if (ret >= 0) {
  12984. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  12985. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  12986. }
  12987. } while (ret == WC_PENDING_E);
  12988. if (ret < 0) {
  12989. ERROR_OUT(-7919, exit_rsa);
  12990. }
  12991. if (XMEMCMP(plain, in, inLen)) {
  12992. ERROR_OUT(-7920, exit_rsa);
  12993. }
  12994. TEST_SLEEP();
  12995. #endif /* NO_SHA */
  12996. #endif
  12997. #ifndef NO_SHA256
  12998. XMEMSET(plain, 0, plainSz);
  12999. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13000. do {
  13001. #if defined(WOLFSSL_ASYNC_CRYPT)
  13002. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13003. #endif
  13004. if (ret >= 0) {
  13005. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13006. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13007. }
  13008. } while (ret == WC_PENDING_E);
  13009. if (ret < 0) {
  13010. ERROR_OUT(-7921, exit_rsa);
  13011. }
  13012. TEST_SLEEP();
  13013. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13014. idx = (word32)ret;
  13015. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13016. do {
  13017. #if defined(WOLFSSL_ASYNC_CRYPT)
  13018. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13019. #endif
  13020. if (ret >= 0) {
  13021. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13022. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13023. }
  13024. } while (ret == WC_PENDING_E);
  13025. if (ret < 0) {
  13026. ERROR_OUT(-7922, exit_rsa);
  13027. }
  13028. if (XMEMCMP(plain, in, inLen)) {
  13029. ERROR_OUT(-7923, exit_rsa);
  13030. }
  13031. TEST_SLEEP();
  13032. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13033. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13034. do {
  13035. #if defined(WOLFSSL_ASYNC_CRYPT)
  13036. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13037. #endif
  13038. if (ret >= 0) {
  13039. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  13040. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13041. }
  13042. } while (ret == WC_PENDING_E);
  13043. if (ret < 0) {
  13044. ERROR_OUT(-7924, exit_rsa);
  13045. }
  13046. if (ret != (int)inLen) {
  13047. ERROR_OUT(-7925, exit_rsa);
  13048. }
  13049. if (XMEMCMP(res, in, inLen)) {
  13050. ERROR_OUT(-7926, exit_rsa);
  13051. }
  13052. TEST_SLEEP();
  13053. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13054. /* check fails if not using the same optional label */
  13055. XMEMSET(plain, 0, plainSz);
  13056. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13057. do {
  13058. #if defined(WOLFSSL_ASYNC_CRYPT)
  13059. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13060. #endif
  13061. if (ret >= 0) {
  13062. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13063. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13064. }
  13065. } while (ret == WC_PENDING_E);
  13066. if (ret < 0) {
  13067. ERROR_OUT(-7927, exit_rsa);
  13068. }
  13069. TEST_SLEEP();
  13070. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13071. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  13072. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  13073. !defined(WOLFSSL_CRYPTOCELL)
  13074. /* label is unused in cryptocell so it won't detect decrypt error due to label */
  13075. idx = (word32)ret;
  13076. do {
  13077. #if defined(WOLFSSL_ASYNC_CRYPT)
  13078. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13079. #endif
  13080. if (ret >= 0) {
  13081. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13082. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  13083. }
  13084. } while (ret == WC_PENDING_E);
  13085. if (ret > 0) { /* in this case decrypt should fail */
  13086. ERROR_OUT(-7928, exit_rsa);
  13087. }
  13088. ret = 0;
  13089. TEST_SLEEP();
  13090. #endif /* !HAVE_CAVIUM */
  13091. /* check using optional label with encrypt/decrypt */
  13092. XMEMSET(plain, 0, plainSz);
  13093. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13094. do {
  13095. #if defined(WOLFSSL_ASYNC_CRYPT)
  13096. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13097. #endif
  13098. if (ret >= 0) {
  13099. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13100. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  13101. }
  13102. } while (ret == WC_PENDING_E);
  13103. if (ret < 0) {
  13104. ERROR_OUT(-7929, exit_rsa);
  13105. }
  13106. TEST_SLEEP();
  13107. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13108. idx = (word32)ret;
  13109. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13110. do {
  13111. #if defined(WOLFSSL_ASYNC_CRYPT)
  13112. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13113. #endif
  13114. if (ret >= 0) {
  13115. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13116. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  13117. }
  13118. } while (ret == WC_PENDING_E);
  13119. if (ret < 0) {
  13120. ERROR_OUT(-7930, exit_rsa);
  13121. }
  13122. if (XMEMCMP(plain, in, inLen)) {
  13123. ERROR_OUT(-7931, exit_rsa);
  13124. }
  13125. TEST_SLEEP();
  13126. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13127. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13128. #ifndef NO_SHA
  13129. /* check fail using mismatch hash algorithms */
  13130. XMEMSET(plain, 0, plainSz);
  13131. do {
  13132. #if defined(WOLFSSL_ASYNC_CRYPT)
  13133. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13134. #endif
  13135. if (ret >= 0) {
  13136. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13137. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  13138. }
  13139. } while (ret == WC_PENDING_E);
  13140. if (ret < 0) {
  13141. ERROR_OUT(-7932, exit_rsa);
  13142. }
  13143. TEST_SLEEP();
  13144. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  13145. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  13146. !defined(WOLFSSL_CRYPTOCELL)
  13147. idx = (word32)ret;
  13148. do {
  13149. #if defined(WOLFSSL_ASYNC_CRYPT)
  13150. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13151. #endif
  13152. if (ret >= 0) {
  13153. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13154. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  13155. in, inLen);
  13156. }
  13157. } while (ret == WC_PENDING_E);
  13158. if (ret > 0) { /* should fail */
  13159. ERROR_OUT(-7933, exit_rsa);
  13160. }
  13161. ret = 0;
  13162. TEST_SLEEP();
  13163. #endif /* !HAVE_CAVIUM */
  13164. #endif /* NO_SHA */
  13165. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13166. #endif /* NO_SHA256 */
  13167. #ifdef WOLFSSL_SHA512
  13168. /* Check valid RSA key size is used while using hash length of SHA512
  13169. If key size is less than (hash length * 2) + 2 then is invalid use
  13170. and test, since OAEP padding requires this.
  13171. BAD_FUNC_ARG is returned when this case is not met */
  13172. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  13173. XMEMSET(plain, 0, plainSz);
  13174. do {
  13175. #if defined(WOLFSSL_ASYNC_CRYPT)
  13176. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13177. #endif
  13178. if (ret >= 0) {
  13179. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13180. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  13181. }
  13182. } while (ret == WC_PENDING_E);
  13183. if (ret < 0) {
  13184. ERROR_OUT(-7934, exit_rsa);
  13185. }
  13186. TEST_SLEEP();
  13187. idx = ret;
  13188. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13189. do {
  13190. #if defined(WOLFSSL_ASYNC_CRYPT)
  13191. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13192. #endif
  13193. if (ret >= 0) {
  13194. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13195. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  13196. }
  13197. } while (ret == WC_PENDING_E);
  13198. if (ret < 0) {
  13199. ERROR_OUT(-7935, exit_rsa);
  13200. }
  13201. if (XMEMCMP(plain, in, inLen)) {
  13202. ERROR_OUT(-7936, exit_rsa);
  13203. }
  13204. TEST_SLEEP();
  13205. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13206. }
  13207. #endif /* WOLFSSL_SHA512 */
  13208. /* check using pkcsv15 padding with _ex API */
  13209. XMEMSET(plain, 0, plainSz);
  13210. do {
  13211. #if defined(WOLFSSL_ASYNC_CRYPT)
  13212. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13213. #endif
  13214. if (ret >= 0) {
  13215. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13216. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  13217. }
  13218. } while (ret == WC_PENDING_E);
  13219. if (ret < 0) {
  13220. ERROR_OUT(-7937, exit_rsa);
  13221. }
  13222. TEST_SLEEP();
  13223. idx = (word32)ret;
  13224. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13225. do {
  13226. #if defined(WOLFSSL_ASYNC_CRYPT)
  13227. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13228. #endif
  13229. if (ret >= 0) {
  13230. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13231. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  13232. }
  13233. } while (ret == WC_PENDING_E);
  13234. if (ret < 0) {
  13235. ERROR_OUT(-7938, exit_rsa);
  13236. }
  13237. if (XMEMCMP(plain, in, inLen)) {
  13238. ERROR_OUT(-7939, exit_rsa);
  13239. }
  13240. TEST_SLEEP();
  13241. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13242. #endif /* !HAVE_FAST_RSA && !HAVE_FIPS */
  13243. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  13244. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13245. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  13246. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  13247. ret = rsa_export_key_test(key);
  13248. if (ret != 0)
  13249. return ret;
  13250. #endif
  13251. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13252. ret = rsa_flatten_test(key);
  13253. if (ret != 0)
  13254. return ret;
  13255. #endif
  13256. #if defined(WOLFSSL_MDK_ARM)
  13257. #define sizeof(s) XSTRLEN((char *)(s))
  13258. #endif
  13259. #ifdef USE_CERT_BUFFERS_1024
  13260. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  13261. bytes = (size_t)sizeof_client_cert_der_1024;
  13262. #elif defined(USE_CERT_BUFFERS_2048)
  13263. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  13264. bytes = (size_t)sizeof_client_cert_der_2048;
  13265. #elif defined(USE_CERT_BUFFERS_3072)
  13266. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  13267. bytes = (size_t)sizeof_client_cert_der_3072;
  13268. #elif defined(USE_CERT_BUFFERS_4096)
  13269. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  13270. bytes = (size_t)sizeof_client_cert_der_4096;
  13271. #elif !defined(NO_FILESYSTEM)
  13272. file2 = XFOPEN(clientCert, "rb");
  13273. if (!file2) {
  13274. ERROR_OUT(-7940, exit_rsa);
  13275. }
  13276. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  13277. XFCLOSE(file2);
  13278. #else
  13279. /* No certificate to use. */
  13280. ERROR_OUT(-7941, exit_rsa);
  13281. #endif
  13282. #ifdef sizeof
  13283. #undef sizeof
  13284. #endif
  13285. #ifdef WOLFSSL_TEST_CERT
  13286. InitDecodedCert(cert, tmp, (word32)bytes, 0);
  13287. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, 0);
  13288. if (ret != 0) {
  13289. FreeDecodedCert(cert);
  13290. ERROR_OUT(-7942, exit_rsa);
  13291. }
  13292. FreeDecodedCert(cert);
  13293. #else
  13294. (void)bytes;
  13295. #endif
  13296. #ifdef WOLFSSL_CERT_EXT
  13297. #ifdef USE_CERT_BUFFERS_1024
  13298. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  13299. bytes = sizeof_client_keypub_der_1024;
  13300. #elif defined(USE_CERT_BUFFERS_2048)
  13301. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  13302. bytes = sizeof_client_keypub_der_2048;
  13303. #elif defined(USE_CERT_BUFFERS_3072)
  13304. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  13305. bytes = sizeof_client_keypub_der_3072;
  13306. #elif defined(USE_CERT_BUFFERS_4096)
  13307. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  13308. bytes = sizeof_client_keypub_der_4096;
  13309. #else
  13310. file = XFOPEN(clientKeyPub, "rb");
  13311. if (!file) {
  13312. err_sys("can't open ./certs/client-keyPub.der, "
  13313. "Please run from wolfSSL home dir", -40);
  13314. ERROR_OUT(-7943, exit_rsa);
  13315. }
  13316. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13317. XFCLOSE(file);
  13318. #endif /* USE_CERT_BUFFERS */
  13319. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  13320. if (ret != 0) {
  13321. ERROR_OUT(-7944, exit_rsa);
  13322. }
  13323. idx = 0;
  13324. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  13325. if (ret != 0) {
  13326. ERROR_OUT(-7945, exit_rsa);
  13327. }
  13328. #endif /* WOLFSSL_CERT_EXT */
  13329. #ifdef WOLFSSL_KEY_GEN
  13330. ret = rsa_keygen_test(&rng);
  13331. if (ret != 0)
  13332. goto exit_rsa;
  13333. #endif
  13334. #ifdef WOLFSSL_CERT_GEN
  13335. /* Make Cert / Sign example for RSA cert and RSA CA */
  13336. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  13337. if (ret != 0)
  13338. goto exit_rsa;
  13339. #if !defined(NO_RSA) && defined(HAVE_ECC)
  13340. ret = rsa_ecc_certgen_test(&rng, tmp);
  13341. if (ret != 0)
  13342. goto exit_rsa;
  13343. #endif
  13344. #ifdef HAVE_NTRU
  13345. {
  13346. Cert myCert;
  13347. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  13348. XFILE caFile;
  13349. #endif
  13350. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  13351. XFILE ntruPrivFile;
  13352. #endif
  13353. int certSz;
  13354. word32 idx3 = 0;
  13355. #ifdef WOLFSSL_TEST_CERT
  13356. DecodedCert decode;
  13357. #endif
  13358. byte public_key[557]; /* sized for EES401EP2 */
  13359. word16 public_key_len; /* no. of octets in public key */
  13360. byte private_key[607]; /* sized for EES401EP2 */
  13361. word16 private_key_len; /* no. of octets in private key */
  13362. DRBG_HANDLE drbg;
  13363. static uint8_t const pers_str[] = {
  13364. 'C', 'y', 'a', 'S', 'S', 'L', ' ', 't', 'e', 's', 't'
  13365. };
  13366. word32 rc = ntru_crypto_drbg_instantiate(112, pers_str,
  13367. sizeof(pers_str), GetEntropy, &drbg);
  13368. if (rc != DRBG_OK) {
  13369. ERROR_OUT(-7946, exit_rsa);
  13370. }
  13371. rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2,
  13372. &public_key_len, NULL,
  13373. &private_key_len, NULL);
  13374. if (rc != NTRU_OK) {
  13375. ERROR_OUT(-7947, exit_rsa);
  13376. }
  13377. rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2,
  13378. &public_key_len, public_key,
  13379. &private_key_len, private_key);
  13380. if (rc != NTRU_OK) {
  13381. ERROR_OUT(-7948, exit_rsa);
  13382. }
  13383. rc = ntru_crypto_drbg_uninstantiate(drbg);
  13384. if (rc != NTRU_OK) {
  13385. ERROR_OUT(-7949, exit_rsa);
  13386. }
  13387. #ifdef USE_CERT_BUFFERS_1024
  13388. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  13389. bytes = sizeof_ca_key_der_1024;
  13390. #elif defined(USE_CERT_BUFFERS_2048)
  13391. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  13392. bytes = sizeof_ca_key_der_2048;
  13393. #else
  13394. caFile = XFOPEN(rsaCaKeyFile, "rb");
  13395. if (!caFile) {
  13396. ERROR_OUT(-7950, exit_rsa);
  13397. }
  13398. bytes = XFREAD(tmp, 1, FOURK_BUF, caFile);
  13399. XFCLOSE(caFile);
  13400. #endif /* USE_CERT_BUFFERS */
  13401. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  13402. if (ret != 0) {
  13403. ERROR_OUT(-7951, exit_rsa);
  13404. }
  13405. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes);
  13406. if (ret != 0) {
  13407. ERROR_OUT(-7952, exit_rsa);
  13408. }
  13409. if (wc_InitCert(&myCert)) {
  13410. ERROR_OUT(-7953, exit_rsa);
  13411. }
  13412. XMEMCPY(&myCert.subject, &certDefaultName, sizeof(CertName));
  13413. myCert.daysValid = 1000;
  13414. #ifdef WOLFSSL_CERT_EXT
  13415. /* add SKID from the Public Key */
  13416. if (wc_SetSubjectKeyIdFromNtruPublicKey(&myCert, public_key,
  13417. public_key_len) != 0) {
  13418. ERROR_OUT(-7954, exit_rsa);
  13419. }
  13420. /* add AKID from the CA certificate */
  13421. #if defined(USE_CERT_BUFFERS_2048)
  13422. ret = wc_SetAuthKeyIdFromCert(&myCert, ca_cert_der_2048,
  13423. sizeof_ca_cert_der_2048);
  13424. #elif defined(USE_CERT_BUFFERS_1024)
  13425. ret = wc_SetAuthKeyIdFromCert(&myCert, ca_cert_der_1024,
  13426. sizeof_ca_cert_der_1024);
  13427. #else
  13428. ret = wc_SetAuthKeyId(&myCert, rsaCaCertFile);
  13429. #endif
  13430. if (ret != 0) {
  13431. ERROR_OUT(-7955, exit_rsa);
  13432. }
  13433. /* add Key Usage */
  13434. if (wc_SetKeyUsage(&myCert, certKeyUsage2) != 0) {
  13435. ERROR_OUT(-7956, exit_rsa);
  13436. }
  13437. #endif /* WOLFSSL_CERT_EXT */
  13438. #if defined(USE_CERT_BUFFERS_2048)
  13439. ret = wc_SetIssuerBuffer(&myCert, ca_cert_der_2048,
  13440. sizeof_ca_cert_der_2048);
  13441. #elif defined(USE_CERT_BUFFERS_1024)
  13442. ret = wc_SetIssuerBuffer(&myCert, ca_cert_der_1024,
  13443. sizeof_ca_cert_der_1024);
  13444. #else
  13445. ret = wc_SetIssuer(&myCert, rsaCaCertFile);
  13446. #endif
  13447. if (ret < 0) {
  13448. ERROR_OUT(-7957, exit_rsa);
  13449. }
  13450. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13451. if (der == NULL) {
  13452. ERROR_OUT(-7958, exit_rsa);
  13453. }
  13454. certSz = wc_MakeNtruCert(&myCert, der, FOURK_BUF, public_key,
  13455. public_key_len, &rng);
  13456. if (certSz < 0) {
  13457. ERROR_OUT(-7959, exit_rsa);
  13458. }
  13459. ret = 0;
  13460. do {
  13461. #if defined(WOLFSSL_ASYNC_CRYPT)
  13462. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13463. #endif
  13464. if (ret >= 0) {
  13465. ret = wc_SignCert(myCert.bodySz, myCert.sigType, der, FOURK_BUF,
  13466. caKey, NULL, &rng);
  13467. }
  13468. } while (ret == WC_PENDING_E);
  13469. wc_FreeRsaKey(caKey);
  13470. if (ret < 0) {
  13471. ERROR_OUT(-7960, exit_rsa);
  13472. }
  13473. certSz = ret;
  13474. #ifdef WOLFSSL_TEST_CERT
  13475. InitDecodedCert(&decode, der, certSz, HEAP_HINT);
  13476. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  13477. if (ret != 0) {
  13478. FreeDecodedCert(&decode);
  13479. ERROR_OUT(-7961, exit_rsa);
  13480. }
  13481. FreeDecodedCert(&decode);
  13482. #endif
  13483. ret = SaveDerAndPem(der, certSz, "./ntru-cert.der", "./ntru-cert.pem",
  13484. CERT_TYPE, -5637);
  13485. if (ret != 0) {
  13486. goto exit_rsa;
  13487. }
  13488. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  13489. ntruPrivFile = XFOPEN("./ntru-key.raw", "wb");
  13490. if (!ntruPrivFile) {
  13491. ERROR_OUT(-7962, exit_rsa);
  13492. }
  13493. ret = (int)XFWRITE(private_key, 1, private_key_len, ntruPrivFile);
  13494. XFCLOSE(ntruPrivFile);
  13495. if (ret != private_key_len) {
  13496. ERROR_OUT(-7963, exit_rsa);
  13497. }
  13498. #endif
  13499. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13500. der = NULL;
  13501. }
  13502. #endif /* HAVE_NTRU */
  13503. #ifdef WOLFSSL_CERT_REQ
  13504. {
  13505. Cert *req;
  13506. int derSz;
  13507. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13508. if (! req)
  13509. ERROR_OUT(MEMORY_E, exit_rsa);
  13510. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  13511. if (der == NULL) {
  13512. ERROR_OUT(-7964, exit_rsa);
  13513. }
  13514. if (wc_InitCert(req)) {
  13515. ERROR_OUT(-7965, exit_rsa);
  13516. }
  13517. req->version = 0;
  13518. req->isCA = 1;
  13519. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  13520. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  13521. #ifndef NO_SHA256
  13522. req->sigType = CTC_SHA256wRSA;
  13523. #else
  13524. req->sigType = CTC_SHAwRSA;
  13525. #endif
  13526. #ifdef WOLFSSL_CERT_EXT
  13527. /* add SKID from the Public Key */
  13528. if (wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL) != 0) {
  13529. ERROR_OUT(-7966, exit_rsa);
  13530. }
  13531. /* add Key Usage */
  13532. if (wc_SetKeyUsage(req, certKeyUsage2) != 0) {
  13533. ERROR_OUT(-7967, exit_rsa);
  13534. }
  13535. /* add Extended Key Usage */
  13536. if (wc_SetExtKeyUsage(req, "serverAuth,clientAuth,codeSigning,"
  13537. "emailProtection,timeStamping,OCSPSigning") != 0) {
  13538. ERROR_OUT(-7968, exit_rsa);
  13539. }
  13540. #ifdef WOLFSSL_EKU_OID
  13541. {
  13542. WOLFSSL_SMALL_STACK_STATIC const char unique[] = "2.16.840.1.111111.100.1.10.1";
  13543. if (wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  13544. HEAP_HINT) != 0) {
  13545. ERROR_OUT(-7969, exit_rsa);
  13546. }
  13547. }
  13548. #endif /* WOLFSSL_EKU_OID */
  13549. #endif /* WOLFSSL_CERT_EXT */
  13550. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  13551. if (derSz < 0) {
  13552. ERROR_OUT(-7970, exit_rsa);
  13553. }
  13554. #ifdef WOLFSSL_CERT_EXT
  13555. /* Try again with "any" flag set, will override all others */
  13556. if (wc_SetExtKeyUsage(req, "any") != 0) {
  13557. ERROR_OUT(-7971, exit_rsa);
  13558. }
  13559. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  13560. if (derSz < 0) {
  13561. ERROR_OUT(-7972, exit_rsa);
  13562. }
  13563. #endif /* WOLFSSL_CERT_EXT */
  13564. ret = 0;
  13565. do {
  13566. #if defined(WOLFSSL_ASYNC_CRYPT)
  13567. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13568. #endif
  13569. if (ret >= 0) {
  13570. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  13571. key, NULL, &rng);
  13572. }
  13573. } while (ret == WC_PENDING_E);
  13574. if (ret < 0) {
  13575. ERROR_OUT(-7973, exit_rsa);
  13576. }
  13577. derSz = ret;
  13578. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  13579. CERTREQ_TYPE, -5650);
  13580. if (ret != 0) {
  13581. goto exit_rsa;
  13582. }
  13583. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  13584. if (derSz < 0) {
  13585. ERROR_OUT(-7974, exit_rsa);
  13586. }
  13587. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13588. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13589. der = NULL;
  13590. }
  13591. #endif /* WOLFSSL_CERT_REQ */
  13592. #endif /* WOLFSSL_CERT_GEN */
  13593. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  13594. /* Need to create known good signatures to test with this. */
  13595. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13596. ret = rsa_pss_test(&rng, key);
  13597. if (ret != 0)
  13598. goto exit_rsa;
  13599. #endif
  13600. #endif
  13601. #ifdef WOLFSSL_HAVE_SP_RSA
  13602. /* New key to be loaded in rsa_even_mod_test(). */
  13603. if (key != NULL)
  13604. wc_FreeRsaKey(key);
  13605. ret = rsa_even_mod_test(&rng, key);
  13606. #endif
  13607. exit_rsa:
  13608. #ifdef WOLFSSL_SMALL_STACK
  13609. if (key != NULL) {
  13610. wc_FreeRsaKey(key);
  13611. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13612. }
  13613. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  13614. if (keypub != NULL) {
  13615. wc_FreeRsaKey(keypub);
  13616. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13617. }
  13618. #endif
  13619. #if defined(HAVE_NTRU)
  13620. if (caKey != NULL) {
  13621. wc_FreeRsaKey(caKey);
  13622. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13623. }
  13624. #endif
  13625. #ifdef WOLFSSL_TEST_CERT
  13626. if (cert != NULL)
  13627. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13628. #endif
  13629. #else
  13630. wc_FreeRsaKey(key);
  13631. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  13632. wc_FreeRsaKey(keypub);
  13633. #endif
  13634. #if defined(HAVE_NTRU)
  13635. wc_FreeRsaKey(caKey);
  13636. #endif
  13637. #endif
  13638. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13639. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13640. wc_FreeRng(&rng);
  13641. FREE_VAR(in, HEAP_HINT);
  13642. FREE_VAR(out, HEAP_HINT);
  13643. FREE_VAR(plain, HEAP_HINT);
  13644. /* ret can be greater then 0 with certgen but all negative values should
  13645. * be returned and treated as an error */
  13646. if (ret >= 0) {
  13647. return 0;
  13648. }
  13649. else {
  13650. return ret;
  13651. }
  13652. }
  13653. #endif /* !NO_RSA */
  13654. #ifndef NO_DH
  13655. static int dh_fips_generate_test(WC_RNG *rng)
  13656. {
  13657. int ret = 0;
  13658. #ifdef WOLFSSL_SMALL_STACK
  13659. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);;
  13660. #else
  13661. DhKey key[1];
  13662. #endif
  13663. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  13664. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  13665. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  13666. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  13667. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  13668. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  13669. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  13670. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  13671. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  13672. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  13673. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  13674. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  13675. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  13676. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  13677. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  13678. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  13679. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  13680. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  13681. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  13682. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  13683. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  13684. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  13685. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  13686. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  13687. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  13688. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  13689. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  13690. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  13691. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  13692. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  13693. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  13694. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  13695. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  13696. };
  13697. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  13698. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  13699. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  13700. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  13701. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  13702. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  13703. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  13704. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  13705. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  13706. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  13707. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  13708. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  13709. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  13710. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  13711. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  13712. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  13713. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  13714. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  13715. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  13716. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  13717. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  13718. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  13719. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  13720. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  13721. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  13722. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  13723. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  13724. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  13725. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  13726. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  13727. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  13728. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  13729. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  13730. };
  13731. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  13732. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  13733. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  13734. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  13735. 0x40, 0x52, 0xed, 0x41
  13736. };
  13737. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  13738. 0x00,
  13739. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  13740. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  13741. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  13742. 0x40, 0x52, 0xed, 0x41
  13743. };
  13744. byte priv[256];
  13745. byte pub[256];
  13746. word32 privSz = sizeof(priv);
  13747. word32 pubSz = sizeof(pub);
  13748. #ifdef WOLFSSL_SMALL_STACK
  13749. if (key == NULL)
  13750. ERROR_OUT(MEMORY_E, exit_gen_test);
  13751. #endif
  13752. /* Parameter Validation testing. */
  13753. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  13754. if (ret != BAD_FUNC_ARG)
  13755. ERROR_OUT(-7980, exit_gen_test);
  13756. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  13757. if (ret != BAD_FUNC_ARG)
  13758. ERROR_OUT(-7981, exit_gen_test);
  13759. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  13760. if (ret != BAD_FUNC_ARG)
  13761. ERROR_OUT(-7982, exit_gen_test);
  13762. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  13763. if (ret != BAD_FUNC_ARG)
  13764. ERROR_OUT(-7983, exit_gen_test);
  13765. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  13766. if (ret != BAD_FUNC_ARG)
  13767. ERROR_OUT(-7984, exit_gen_test);
  13768. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  13769. if (ret != BAD_FUNC_ARG)
  13770. ERROR_OUT(-7985, exit_gen_test);
  13771. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  13772. if (ret != 0)
  13773. ERROR_OUT(-7986, exit_gen_test);
  13774. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  13775. if (ret != 0) {
  13776. ERROR_OUT(-7987, exit_gen_test);
  13777. }
  13778. wc_FreeDhKey(key);
  13779. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  13780. if (ret != 0)
  13781. ERROR_OUT(-7988, exit_gen_test);
  13782. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  13783. if (ret != 0) {
  13784. ERROR_OUT(-7989, exit_gen_test);
  13785. }
  13786. /* Use API. */
  13787. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  13788. #if defined(WOLFSSL_ASYNC_CRYPT)
  13789. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  13790. #endif
  13791. if (ret != 0) {
  13792. ERROR_OUT(-7990, exit_gen_test);
  13793. }
  13794. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  13795. if (ret != 0) {
  13796. ERROR_OUT(-7991, exit_gen_test);
  13797. }
  13798. wc_FreeDhKey(key);
  13799. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  13800. if (ret != 0)
  13801. ERROR_OUT(-7992, exit_gen_test);
  13802. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  13803. if (ret != 0) {
  13804. ERROR_OUT(-7993, exit_gen_test);
  13805. }
  13806. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  13807. if (ret != 0) {
  13808. ERROR_OUT(-7994, exit_gen_test);
  13809. }
  13810. #ifndef HAVE_SELFTEST
  13811. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  13812. if (ret != 0) {
  13813. ERROR_OUT(-7995, exit_gen_test);
  13814. }
  13815. /* Taint the public key so the check fails. */
  13816. pub[0]++;
  13817. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  13818. if (ret != MP_CMP_E) {
  13819. ERROR_OUT(-7996, exit_gen_test);
  13820. }
  13821. #ifdef WOLFSSL_KEY_GEN
  13822. wc_FreeDhKey(key);
  13823. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  13824. if (ret != 0)
  13825. ERROR_OUT(-7997, exit_gen_test);
  13826. ret = wc_DhGenerateParams(rng, 2048, key);
  13827. if (ret != 0) {
  13828. ERROR_OUT(-7998, exit_gen_test);
  13829. }
  13830. privSz = sizeof(priv);
  13831. pubSz = sizeof(pub);
  13832. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  13833. #if defined(WOLFSSL_ASYNC_CRYPT)
  13834. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  13835. #endif
  13836. if (ret != 0) {
  13837. ERROR_OUT(-7999, exit_gen_test);
  13838. }
  13839. #endif /* WOLFSSL_KEY_GEN */
  13840. #endif /* HAVE_SELFTEST */
  13841. ret = 0;
  13842. exit_gen_test:
  13843. #ifdef WOLFSSL_SMALL_STACK
  13844. if (key) {
  13845. wc_FreeDhKey(key);
  13846. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13847. }
  13848. #else
  13849. wc_FreeDhKey(key);
  13850. #endif
  13851. return ret;
  13852. }
  13853. static int dh_generate_test(WC_RNG *rng)
  13854. {
  13855. int ret = 0;
  13856. DhKey smallKey;
  13857. byte p[2] = { 0, 5 };
  13858. byte g[2] = { 0, 2 };
  13859. #if !defined(WOLFSSL_SP_MATH)
  13860. #ifdef WOLFSSL_DH_CONST
  13861. /* the table for constant DH lookup will round to the lowest byte size 21 */
  13862. byte priv[21];
  13863. byte pub[21];
  13864. #else
  13865. byte priv[2];
  13866. byte pub[2];
  13867. #endif
  13868. word32 privSz = sizeof(priv);
  13869. word32 pubSz = sizeof(pub);
  13870. #endif
  13871. ret = wc_InitDhKey_ex(&smallKey, HEAP_HINT, devId);
  13872. if (ret != 0)
  13873. return -8010;
  13874. /* Parameter Validation testing. */
  13875. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  13876. if (ret != BAD_FUNC_ARG)
  13877. return -8011;
  13878. wc_FreeDhKey(NULL);
  13879. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  13880. if (ret != BAD_FUNC_ARG) {
  13881. ERROR_OUT(-8012, exit_gen_test);
  13882. }
  13883. ret = wc_DhSetKey(&smallKey, NULL, sizeof(p), g, sizeof(g));
  13884. if (ret != BAD_FUNC_ARG) {
  13885. ERROR_OUT(-8013, exit_gen_test);
  13886. }
  13887. ret = wc_DhSetKey(&smallKey, p, 0, g, sizeof(g));
  13888. if (ret != BAD_FUNC_ARG) {
  13889. ERROR_OUT(-8014, exit_gen_test);
  13890. }
  13891. ret = wc_DhSetKey(&smallKey, p, sizeof(p), NULL, sizeof(g));
  13892. if (ret != BAD_FUNC_ARG) {
  13893. ERROR_OUT(-8015, exit_gen_test);
  13894. }
  13895. ret = wc_DhSetKey(&smallKey, p, sizeof(p), g, 0);
  13896. if (ret != BAD_FUNC_ARG) {
  13897. ERROR_OUT(-8016, exit_gen_test);
  13898. }
  13899. ret = wc_DhSetKey(&smallKey, p, sizeof(p), g, sizeof(g));
  13900. if (ret != 0) {
  13901. ERROR_OUT(-8017, exit_gen_test);
  13902. }
  13903. #if !defined(WOLFSSL_SP_MATH)
  13904. /* Use API. */
  13905. ret = wc_DhGenerateKeyPair(&smallKey, rng, priv, &privSz, pub, &pubSz);
  13906. #if defined(WOLFSSL_ASYNC_CRYPT)
  13907. ret = wc_AsyncWait(ret, &smallKey.asyncDev, WC_ASYNC_FLAG_NONE);
  13908. #endif
  13909. if (ret != 0) {
  13910. ret = -8018;
  13911. }
  13912. #else
  13913. (void)rng;
  13914. ret = 0;
  13915. #endif
  13916. exit_gen_test:
  13917. wc_FreeDhKey(&smallKey);
  13918. return ret;
  13919. }
  13920. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  13921. typedef struct dh_pubvalue_test {
  13922. const byte* data;
  13923. word32 len;
  13924. } dh_pubvalue_test;
  13925. static int dh_test_check_pubvalue(void)
  13926. {
  13927. int ret;
  13928. word32 i;
  13929. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  13930. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  13931. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  13932. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  13933. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  13934. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  13935. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  13936. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  13937. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  13938. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  13939. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  13940. const dh_pubvalue_test dh_pubval_fail[] = {
  13941. { prime, sizeof(prime) },
  13942. { pubValZero, sizeof(pubValZero) },
  13943. { pubValZeroLong, sizeof(pubValZeroLong) },
  13944. { pubValOne, sizeof(pubValOne) },
  13945. { pubValOneLong, sizeof(pubValOneLong) },
  13946. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  13947. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  13948. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  13949. { pubValTooBig0, sizeof(pubValTooBig0) },
  13950. { pubValTooBig1, sizeof(pubValTooBig1) },
  13951. { pubValTooLong, sizeof(pubValTooLong) },
  13952. };
  13953. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  13954. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  13955. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  13956. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  13957. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  13958. const dh_pubvalue_test dh_pubval_pass[] = {
  13959. { pubValTwo, sizeof(pubValTwo) },
  13960. { pubValTwoLong, sizeof(pubValTwoLong) },
  13961. { pubValGood, sizeof(pubValGood) },
  13962. { pubValGoodLen, sizeof(pubValGoodLen) },
  13963. { pubValGoodLong, sizeof(pubValGoodLong) },
  13964. };
  13965. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  13966. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  13967. dh_pubval_fail[i].len);
  13968. if (ret != MP_VAL)
  13969. return -8020 - (int)i;
  13970. }
  13971. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  13972. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  13973. dh_pubval_pass[i].len);
  13974. if (ret != 0)
  13975. return -8030 - (int)i;
  13976. }
  13977. return 0;
  13978. }
  13979. #endif
  13980. #if defined(HAVE_FFDHE)
  13981. #ifdef HAVE_FFDHE_3072
  13982. #define FFDHE_KEY_SIZE (3072/8)
  13983. #else
  13984. #define FFDHE_KEY_SIZE (2048/8)
  13985. #endif
  13986. static int dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  13987. {
  13988. int ret;
  13989. word32 privSz, pubSz, privSz2, pubSz2;
  13990. #ifdef WOLFSSL_SMALL_STACK
  13991. byte *priv = (byte *)XMALLOC(FFDHE_KEY_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13992. byte *pub = (byte *)XMALLOC(FFDHE_KEY_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13993. byte *priv2 = (byte *)XMALLOC(FFDHE_KEY_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13994. byte *pub2 = (byte *)XMALLOC(FFDHE_KEY_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13995. byte *agree = (byte *)XMALLOC(FFDHE_KEY_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13996. byte *agree2 = (byte *)XMALLOC(FFDHE_KEY_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13997. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13998. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13999. #else
  14000. byte priv[FFDHE_KEY_SIZE];
  14001. byte pub[FFDHE_KEY_SIZE];
  14002. byte priv2[FFDHE_KEY_SIZE];
  14003. byte pub2[FFDHE_KEY_SIZE];
  14004. byte agree[FFDHE_KEY_SIZE];
  14005. byte agree2[FFDHE_KEY_SIZE];
  14006. DhKey key[1];
  14007. DhKey key2[1];
  14008. #endif
  14009. word32 agreeSz = FFDHE_KEY_SIZE;
  14010. word32 agreeSz2 = FFDHE_KEY_SIZE;
  14011. #ifdef WOLFSSL_SMALL_STACK
  14012. if ((priv == NULL) ||
  14013. (pub == NULL) ||
  14014. (priv2 == NULL) ||
  14015. (pub2 == NULL) ||
  14016. (agree == NULL) ||
  14017. (agree2 == NULL) ||
  14018. (key == NULL) ||
  14019. (key2 == NULL))
  14020. ERROR_OUT(-8050, done);
  14021. #endif
  14022. pubSz = FFDHE_KEY_SIZE;
  14023. pubSz2 = FFDHE_KEY_SIZE;
  14024. privSz = FFDHE_KEY_SIZE;
  14025. privSz2 = FFDHE_KEY_SIZE;
  14026. XMEMSET(key, 0, sizeof *key);
  14027. XMEMSET(key2, 0, sizeof *key2);
  14028. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14029. if (ret != 0) {
  14030. ERROR_OUT(-8051, done);
  14031. }
  14032. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  14033. if (ret != 0) {
  14034. ERROR_OUT(-8052, done);
  14035. }
  14036. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  14037. if (ret != 0) {
  14038. ERROR_OUT(-8053, done);
  14039. }
  14040. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  14041. params->g_len);
  14042. if (ret != 0) {
  14043. ERROR_OUT(-8054, done);
  14044. }
  14045. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  14046. #if defined(WOLFSSL_ASYNC_CRYPT)
  14047. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14048. #endif
  14049. if (ret != 0) {
  14050. ERROR_OUT(-8055, done);
  14051. }
  14052. ret = wc_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  14053. #if defined(WOLFSSL_ASYNC_CRYPT)
  14054. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  14055. #endif
  14056. if (ret != 0) {
  14057. ERROR_OUT(-8056, done);
  14058. }
  14059. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  14060. #if defined(WOLFSSL_ASYNC_CRYPT)
  14061. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14062. #endif
  14063. if (ret != 0) {
  14064. ERROR_OUT(-8057, done);
  14065. }
  14066. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  14067. #if defined(WOLFSSL_ASYNC_CRYPT)
  14068. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  14069. #endif
  14070. if (ret != 0) {
  14071. ERROR_OUT(-8058, done);
  14072. }
  14073. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  14074. ERROR_OUT(-8059, done);
  14075. }
  14076. #ifdef WOLFSSL_HAVE_SP_DH
  14077. /* Make p even */
  14078. key->p.dp[0] &= (mp_digit)-2;
  14079. if (ret != 0) {
  14080. ERROR_OUT(-8058, done);
  14081. }
  14082. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  14083. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  14084. ERROR_OUT(-8058, done);
  14085. }
  14086. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  14087. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  14088. ERROR_OUT(-8057, done);
  14089. }
  14090. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  14091. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  14092. ERROR_OUT(-8057, done);
  14093. }
  14094. /* Getting here means success - set ret to 0. */
  14095. ret = 0;
  14096. #endif
  14097. done:
  14098. #ifdef WOLFSSL_SMALL_STACK
  14099. if (priv)
  14100. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14101. if (pub)
  14102. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14103. if (priv2)
  14104. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14105. if (pub2)
  14106. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14107. if (agree)
  14108. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14109. if (agree2)
  14110. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14111. if (key) {
  14112. wc_FreeDhKey(key);
  14113. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14114. }
  14115. if (key2) {
  14116. wc_FreeDhKey(key2);
  14117. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14118. }
  14119. #else
  14120. wc_FreeDhKey(key);
  14121. wc_FreeDhKey(key2);
  14122. #endif
  14123. return ret;
  14124. }
  14125. #endif /* HAVE_FFDHE */
  14126. WOLFSSL_TEST_SUBROUTINE int dh_test(void)
  14127. {
  14128. int ret;
  14129. word32 bytes;
  14130. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  14131. WC_RNG rng;
  14132. int keyInit = 0;
  14133. #define DH_TEST_TMP_SIZE 1024
  14134. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  14135. #define DH_TEST_BUF_SIZE 256
  14136. #else
  14137. #define DH_TEST_BUF_SIZE 512
  14138. #endif
  14139. word32 agreeSz = DH_TEST_BUF_SIZE;
  14140. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  14141. #ifdef WOLFSSL_SMALL_STACK
  14142. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14143. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14144. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14145. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14146. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14147. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14148. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14149. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14150. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14151. if ((tmp == NULL) || (priv == NULL) || (pub == NULL) ||
  14152. (priv2 == NULL) || (pub2 == NULL) || (agree == NULL) ||
  14153. (agree2 == NULL))
  14154. ERROR_OUT(-8100, done);
  14155. #else
  14156. DhKey key[1];
  14157. DhKey key2[1];
  14158. byte tmp[DH_TEST_TMP_SIZE];
  14159. byte priv[DH_TEST_BUF_SIZE];
  14160. byte pub[DH_TEST_BUF_SIZE];
  14161. byte priv2[DH_TEST_BUF_SIZE];
  14162. byte pub2[DH_TEST_BUF_SIZE];
  14163. byte agree[DH_TEST_BUF_SIZE];
  14164. byte agree2[DH_TEST_BUF_SIZE];
  14165. #endif
  14166. #ifdef USE_CERT_BUFFERS_1024
  14167. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  14168. bytes = (size_t)sizeof_dh_key_der_1024;
  14169. #elif defined(USE_CERT_BUFFERS_2048)
  14170. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  14171. bytes = (size_t)sizeof_dh_key_der_2048;
  14172. #elif defined(USE_CERT_BUFFERS_3072)
  14173. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  14174. bytes = (size_t)sizeof_dh_key_der_3072;
  14175. #elif defined(USE_CERT_BUFFERS_4096)
  14176. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  14177. bytes = (size_t)sizeof_dh_key_der_4096;
  14178. #elif defined(NO_ASN)
  14179. /* don't use file, no DER parsing */
  14180. #elif !defined(NO_FILESYSTEM)
  14181. {
  14182. XFILE file = XFOPEN(dhParamsFile, "rb");
  14183. if (! file)
  14184. ERROR_OUT(-8101, done);
  14185. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  14186. XFCLOSE(file);
  14187. }
  14188. #else
  14189. /* No DH key to use. */
  14190. ERROR_OUT(-8102, done);
  14191. #endif /* USE_CERT_BUFFERS */
  14192. (void)idx;
  14193. (void)tmp;
  14194. (void)bytes;
  14195. pubSz = DH_TEST_BUF_SIZE;
  14196. pubSz2 = DH_TEST_BUF_SIZE;
  14197. privSz = DH_TEST_BUF_SIZE;
  14198. privSz2 = DH_TEST_BUF_SIZE;
  14199. XMEMSET(&rng, 0, sizeof(rng));
  14200. /* Use API for coverage. */
  14201. ret = wc_InitDhKey(key);
  14202. if (ret != 0) {
  14203. ERROR_OUT(-8103, done);
  14204. }
  14205. wc_FreeDhKey(key);
  14206. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14207. if (ret != 0) {
  14208. ERROR_OUT(-8104, done);
  14209. }
  14210. keyInit = 1;
  14211. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  14212. if (ret != 0) {
  14213. ERROR_OUT(-8105, done);
  14214. }
  14215. #ifdef NO_ASN
  14216. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  14217. if (ret != 0) {
  14218. ERROR_OUT(-8106, done);
  14219. }
  14220. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  14221. if (ret != 0) {
  14222. ERROR_OUT(-8107, done);
  14223. }
  14224. #else
  14225. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  14226. if (ret != 0) {
  14227. ERROR_OUT(-8108, done);
  14228. }
  14229. idx = 0;
  14230. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  14231. if (ret != 0) {
  14232. ERROR_OUT(-8109, done);
  14233. }
  14234. #endif
  14235. #ifndef HAVE_FIPS
  14236. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14237. #else
  14238. ret = wc_InitRng(&rng);
  14239. #endif
  14240. if (ret != 0) {
  14241. ERROR_OUT(-8110, done);
  14242. }
  14243. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  14244. #if defined(WOLFSSL_ASYNC_CRYPT)
  14245. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14246. #endif
  14247. if (ret != 0) {
  14248. ERROR_OUT(-8111, done);
  14249. }
  14250. ret = wc_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  14251. #if defined(WOLFSSL_ASYNC_CRYPT)
  14252. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  14253. #endif
  14254. if (ret != 0) {
  14255. ERROR_OUT(-8112, done);
  14256. }
  14257. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  14258. #if defined(WOLFSSL_ASYNC_CRYPT)
  14259. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14260. #endif
  14261. if (ret != 0) {
  14262. ERROR_OUT(-8113, done);
  14263. }
  14264. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  14265. #if defined(WOLFSSL_ASYNC_CRYPT)
  14266. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  14267. #endif
  14268. if (ret != 0) {
  14269. ERROR_OUT(-8114, done);
  14270. }
  14271. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  14272. ERROR_OUT(-8115, done);
  14273. }
  14274. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  14275. if (wc_DhCheckPrivKey(NULL, NULL, 0) != BAD_FUNC_ARG)
  14276. ERROR_OUT(-8116, done);
  14277. if (wc_DhCheckPrivKey(key, priv, privSz) != 0)
  14278. ERROR_OUT(-8117, done);
  14279. if (wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL) != BAD_FUNC_ARG)
  14280. ERROR_OUT(-8118, done);
  14281. {
  14282. word32 pSz, qSz, gSz;
  14283. if (wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz) != LENGTH_ONLY_E)
  14284. ERROR_OUT(-8119, done);
  14285. }
  14286. #endif
  14287. /* Test DH key import / export */
  14288. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  14289. (!defined(HAVE_FIPS) || \
  14290. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  14291. wc_FreeDhKey(key);
  14292. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14293. if (ret != 0) {
  14294. ERROR_OUT(-8120, done);
  14295. }
  14296. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM)
  14297. {
  14298. XFILE file = XFOPEN(dhKeyFile, "rb");
  14299. if (!file)
  14300. ERROR_OUT(-8121, done);
  14301. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  14302. XFCLOSE(file);
  14303. }
  14304. idx = 0;
  14305. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  14306. if (ret != 0) {
  14307. ERROR_OUT(-8122, done);
  14308. }
  14309. #else
  14310. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  14311. if (ret != 0) {
  14312. ERROR_OUT(-8123, done);
  14313. }
  14314. #endif
  14315. privSz = DH_TEST_BUF_SIZE;
  14316. pubSz = DH_TEST_BUF_SIZE;
  14317. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  14318. if (ret != 0) {
  14319. ERROR_OUT(-8124, done);
  14320. }
  14321. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  14322. if (ret != 0) {
  14323. ERROR_OUT(-8125, done);
  14324. }
  14325. #endif /* WOLFSSL_DH_EXTRA */
  14326. ret = dh_generate_test(&rng);
  14327. if (ret != 0)
  14328. ERROR_OUT(-8126, done);
  14329. ret = dh_fips_generate_test(&rng);
  14330. if (ret != 0)
  14331. ERROR_OUT(-8127, done);
  14332. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  14333. ret = dh_test_check_pubvalue();
  14334. if (ret != 0)
  14335. ERROR_OUT(-8128, done);
  14336. #endif
  14337. /* Specialized code for key gen when using FFDHE-2048 and FFDHE-3072. */
  14338. #ifdef HAVE_FFDHE_2048
  14339. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  14340. if (ret != 0)
  14341. ERROR_OUT(-8129, done);
  14342. #endif
  14343. #ifdef HAVE_FFDHE_3072
  14344. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  14345. if (ret != 0)
  14346. ERROR_OUT(-8130, done);
  14347. #endif
  14348. wc_FreeDhKey(key);
  14349. keyInit = 0;
  14350. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  14351. !defined(WOLFSSL_OLD_PRIME_CHECK)
  14352. /* Test Check Key */
  14353. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  14354. NULL, 0, 0, &rng);
  14355. if (ret != 0)
  14356. ERROR_OUT(-8131, done);
  14357. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  14358. #endif
  14359. done:
  14360. wc_FreeRng(&rng);
  14361. #ifdef WOLFSSL_SMALL_STACK
  14362. if (key) {
  14363. if (keyInit)
  14364. wc_FreeDhKey(key);
  14365. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14366. }
  14367. if (key2) {
  14368. wc_FreeDhKey(key2);
  14369. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14370. }
  14371. if (tmp)
  14372. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14373. if (priv)
  14374. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14375. if (pub)
  14376. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14377. if (priv2)
  14378. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14379. if (pub2)
  14380. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14381. if (agree)
  14382. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14383. if (agree2)
  14384. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14385. #else
  14386. if (keyInit)
  14387. wc_FreeDhKey(key);
  14388. wc_FreeDhKey(key2);
  14389. #endif
  14390. return ret;
  14391. #undef DH_TEST_BUF_SIZE
  14392. #undef DH_TEST_TMP_SIZE
  14393. }
  14394. #endif /* NO_DH */
  14395. #ifndef NO_DSA
  14396. WOLFSSL_TEST_SUBROUTINE int dsa_test(void)
  14397. {
  14398. int ret = 0, answer;
  14399. word32 bytes;
  14400. word32 idx = 0;
  14401. WC_RNG rng;
  14402. wc_Sha sha;
  14403. byte hash[WC_SHA_DIGEST_SIZE];
  14404. byte signature[40];
  14405. #ifdef WOLFSSL_KEY_GEN
  14406. byte* der = 0;
  14407. #endif
  14408. #define DSA_TEST_TMP_SIZE 1024
  14409. #ifdef WOLFSSL_SMALL_STACK
  14410. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14411. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14412. #ifdef WOLFSSL_KEY_GEN
  14413. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14414. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14415. #endif
  14416. if ((tmp == NULL) ||
  14417. (key == NULL)
  14418. #ifdef WOLFSSL_KEY_GEN
  14419. || (derIn == NULL)
  14420. || (genKey == NULL)
  14421. #endif
  14422. ) {
  14423. ret = -8216;
  14424. goto out;
  14425. }
  14426. #else
  14427. byte tmp[1024];
  14428. DsaKey key[1];
  14429. #ifdef WOLFSSL_KEY_GEN
  14430. DsaKey derIn[1];
  14431. DsaKey genKey[1];
  14432. #endif
  14433. #endif
  14434. #ifdef USE_CERT_BUFFERS_1024
  14435. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  14436. bytes = sizeof_dsa_key_der_1024;
  14437. #elif defined(USE_CERT_BUFFERS_2048)
  14438. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  14439. bytes = sizeof_dsa_key_der_2048;
  14440. #else
  14441. {
  14442. XFILE file = XFOPEN(dsaKey, "rb");
  14443. if (!file)
  14444. ERROR_OUT(-8200, out);
  14445. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  14446. XFCLOSE(file);
  14447. }
  14448. #endif /* USE_CERT_BUFFERS */
  14449. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  14450. if (ret != 0)
  14451. ERROR_OUT(-8201, out);
  14452. wc_ShaUpdate(&sha, tmp, bytes);
  14453. wc_ShaFinal(&sha, hash);
  14454. wc_ShaFree(&sha);
  14455. ret = wc_InitDsaKey(key);
  14456. if (ret != 0)
  14457. ERROR_OUT(-8202, out);
  14458. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  14459. if (ret != 0)
  14460. ERROR_OUT(-8203, out);
  14461. #ifndef HAVE_FIPS
  14462. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14463. #else
  14464. ret = wc_InitRng(&rng);
  14465. #endif
  14466. if (ret != 0)
  14467. ERROR_OUT(-8204, out);
  14468. ret = wc_DsaSign(hash, signature, key, &rng);
  14469. if (ret != 0)
  14470. ERROR_OUT(-8205, out);
  14471. ret = wc_DsaVerify(hash, signature, key, &answer);
  14472. if (ret != 0)
  14473. ERROR_OUT(-8206, out);
  14474. if (answer != 1)
  14475. ERROR_OUT(-8207, out);
  14476. wc_FreeDsaKey(key);
  14477. #ifdef WOLFSSL_KEY_GEN
  14478. {
  14479. int derSz = 0;
  14480. ret = wc_InitDsaKey(genKey);
  14481. if (ret != 0)
  14482. ERROR_OUT(-8208, out);
  14483. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  14484. if (ret != 0) {
  14485. wc_FreeDsaKey(genKey);
  14486. ERROR_OUT(-8209, out);
  14487. }
  14488. ret = wc_MakeDsaKey(&rng, genKey);
  14489. if (ret != 0) {
  14490. wc_FreeDsaKey(genKey);
  14491. ERROR_OUT(-8210, out);
  14492. }
  14493. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14494. if (der == NULL) {
  14495. wc_FreeDsaKey(genKey);
  14496. ERROR_OUT(-8211, out);
  14497. }
  14498. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  14499. if (derSz < 0) {
  14500. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14501. ERROR_OUT(-8212, out);
  14502. }
  14503. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  14504. DSA_PRIVATEKEY_TYPE, -5814);
  14505. if (ret != 0) {
  14506. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14507. wc_FreeDsaKey(genKey);
  14508. goto out;
  14509. }
  14510. ret = wc_InitDsaKey(derIn);
  14511. if (ret != 0) {
  14512. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14513. wc_FreeDsaKey(genKey);
  14514. ERROR_OUT(-8213, out);
  14515. }
  14516. idx = 0;
  14517. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, derSz);
  14518. if (ret != 0) {
  14519. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14520. wc_FreeDsaKey(derIn);
  14521. wc_FreeDsaKey(genKey);
  14522. ERROR_OUT(-8214, out);
  14523. }
  14524. }
  14525. #endif /* WOLFSSL_KEY_GEN */
  14526. out:
  14527. #ifdef WOLFSSL_SMALL_STACK
  14528. if (key) {
  14529. #endif
  14530. if (wc_InitDsaKey_h(key, NULL) != 0)
  14531. ret = -8215;
  14532. #ifdef WOLFSSL_SMALL_STACK
  14533. }
  14534. #endif
  14535. #ifdef WOLFSSL_KEY_GEN
  14536. if (der)
  14537. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14538. #endif
  14539. #ifdef WOLFSSL_SMALL_STACK
  14540. if (tmp)
  14541. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14542. if (key)
  14543. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14544. #ifdef WOLFSSL_KEY_GEN
  14545. if (derIn) {
  14546. wc_FreeDsaKey(derIn);
  14547. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14548. }
  14549. if (genKey) {
  14550. wc_FreeDsaKey(genKey);
  14551. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14552. }
  14553. #endif
  14554. #else /* !WOLFSSL_SMALL_STACK */
  14555. #ifdef WOLFSSL_KEY_GEN
  14556. wc_FreeDsaKey(derIn);
  14557. wc_FreeDsaKey(genKey);
  14558. #endif
  14559. #endif
  14560. wc_FreeRng(&rng);
  14561. return ret;
  14562. }
  14563. #endif /* NO_DSA */
  14564. #ifdef WOLFCRYPT_HAVE_SRP
  14565. static int generate_random_salt(byte *buf, word32 size)
  14566. {
  14567. int ret = -8220;
  14568. WC_RNG rng;
  14569. if(NULL == buf || !size)
  14570. return -8221;
  14571. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  14572. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  14573. wc_FreeRng(&rng);
  14574. }
  14575. return ret;
  14576. }
  14577. WOLFSSL_TEST_SUBROUTINE int srp_test(void)
  14578. {
  14579. int r;
  14580. byte clientPubKey[80]; /* A */
  14581. byte serverPubKey[80]; /* B */
  14582. word32 clientPubKeySz = 80;
  14583. word32 serverPubKeySz = 80;
  14584. byte username[] = "user";
  14585. word32 usernameSz = 4;
  14586. byte password[] = "password";
  14587. word32 passwordSz = 8;
  14588. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  14589. 0xC9, 0x4D, 0x67, 0xEB, 0x5B, 0x1A, 0x23, 0x46, 0xE8, 0xAB, 0x42, 0x2F,
  14590. 0xC6, 0xA0, 0xED, 0xAE, 0xDA, 0x8C, 0x7F, 0x89, 0x4C, 0x9E, 0xEE, 0xC4,
  14591. 0x2F, 0x9E, 0xD2, 0x50, 0xFD, 0x7F, 0x00, 0x46, 0xE5, 0xAF, 0x2C, 0xF7,
  14592. 0x3D, 0x6B, 0x2F, 0xA2, 0x6B, 0xB0, 0x80, 0x33, 0xDA, 0x4D, 0xE3, 0x22,
  14593. 0xE1, 0x44, 0xE7, 0xA8, 0xE9, 0xB1, 0x2A, 0x0E, 0x46, 0x37, 0xF6, 0x37,
  14594. 0x1F, 0x34, 0xA2, 0x07, 0x1C, 0x4B, 0x38, 0x36, 0xCB, 0xEE, 0xAB, 0x15,
  14595. 0x03, 0x44, 0x60, 0xFA, 0xA7, 0xAD, 0xF4, 0x83
  14596. };
  14597. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  14598. 0x02
  14599. };
  14600. byte salt[10];
  14601. byte verifier[80];
  14602. word32 v_size = sizeof(verifier);
  14603. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  14604. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  14605. #ifdef WOLFSSL_SMALL_STACK
  14606. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14607. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14608. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  14609. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  14610. if ((cli == NULL) ||
  14611. (srv == NULL) ||
  14612. (clientProof == NULL) ||
  14613. (serverProof == NULL)) {
  14614. r = -8222;
  14615. goto out;
  14616. }
  14617. #else
  14618. Srp cli[1], srv[1];
  14619. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  14620. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  14621. #endif
  14622. /* set as 0's so if second init on srv not called SrpTerm is not on
  14623. * garbage values */
  14624. XMEMSET(srv, 0, sizeof *srv);
  14625. XMEMSET(cli, 0, sizeof *cli);
  14626. /* generating random salt */
  14627. r = generate_random_salt(salt, sizeof(salt));
  14628. /* client knows username and password. */
  14629. /* server knows N, g, salt and verifier. */
  14630. if (!r) r = wc_SrpInit(cli, SRP_TYPE_SHA, SRP_CLIENT_SIDE);
  14631. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  14632. /* loading N, g and salt in advance to generate the verifier. */
  14633. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  14634. g, sizeof(g),
  14635. salt, sizeof(salt));
  14636. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  14637. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  14638. /* client sends username to server */
  14639. if (!r) r = wc_SrpInit(srv, SRP_TYPE_SHA, SRP_SERVER_SIDE);
  14640. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  14641. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  14642. g, sizeof(g),
  14643. salt, sizeof(salt));
  14644. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  14645. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  14646. /* server sends N, g, salt and B to client */
  14647. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  14648. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  14649. serverPubKey, serverPubKeySz);
  14650. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  14651. /* client sends A and M1 to server */
  14652. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  14653. serverPubKey, serverPubKeySz);
  14654. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  14655. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  14656. /* server sends M2 to client */
  14657. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  14658. wc_SrpTerm(cli);
  14659. wc_SrpTerm(srv);
  14660. #ifdef WOLFSSL_SMALL_STACK
  14661. out:
  14662. if (cli)
  14663. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14664. if (srv)
  14665. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14666. if (clientProof)
  14667. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14668. if (serverProof)
  14669. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14670. #endif
  14671. return r;
  14672. }
  14673. #endif /* WOLFCRYPT_HAVE_SRP */
  14674. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  14675. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  14676. static int openssl_aes_test(void)
  14677. {
  14678. #ifdef HAVE_AES_CBC
  14679. #ifdef WOLFSSL_AES_128
  14680. {
  14681. /* EVP_CipherUpdate test */
  14682. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  14683. {
  14684. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  14685. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  14686. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  14687. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  14688. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  14689. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  14690. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  14691. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  14692. };
  14693. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  14694. "0123456789abcdef "; /* align */
  14695. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  14696. "1234567890abcdef "; /* align */
  14697. byte cipher[AES_BLOCK_SIZE * 4];
  14698. byte plain [AES_BLOCK_SIZE * 4];
  14699. #ifdef WOLFSSL_SMALL_STACK
  14700. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  14701. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  14702. #else
  14703. EVP_CIPHER_CTX en[1];
  14704. EVP_CIPHER_CTX de[1];
  14705. #endif
  14706. int outlen ;
  14707. int total = 0;
  14708. int i;
  14709. #ifdef WOLFSSL_SMALL_STACK
  14710. if ((en == NULL) || (de == NULL))
  14711. return MEMORY_E;
  14712. #endif
  14713. EVP_CIPHER_CTX_init(en);
  14714. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  14715. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  14716. return -8400;
  14717. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  14718. (byte*)cbcPlain, 9) == 0)
  14719. return -8401;
  14720. if (outlen != 0)
  14721. return -8402;
  14722. total += outlen;
  14723. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  14724. (byte*)&cbcPlain[9] , 9) == 0)
  14725. return -8403;
  14726. if (outlen != 16)
  14727. return -8404;
  14728. total += outlen;
  14729. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  14730. return -8405;
  14731. if (outlen != 16)
  14732. return -8406;
  14733. total += outlen;
  14734. if (total != 32)
  14735. return 3408;
  14736. total = 0;
  14737. EVP_CIPHER_CTX_init(de);
  14738. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  14739. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  14740. return -8407;
  14741. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  14742. return -8408;
  14743. if (outlen != 0)
  14744. return -8409;
  14745. total += outlen;
  14746. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  14747. (byte*)&cipher[6], 12) == 0)
  14748. return -8410;
  14749. if (outlen != 0)
  14750. total += outlen;
  14751. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  14752. (byte*)&cipher[6+12], 14) == 0)
  14753. return -8411;
  14754. if (outlen != 16)
  14755. return -8412;
  14756. total += outlen;
  14757. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  14758. return -8413;
  14759. if (outlen != 2)
  14760. return -8414;
  14761. total += outlen;
  14762. if (total != 18)
  14763. return 3427;
  14764. if (XMEMCMP(plain, cbcPlain, 18))
  14765. return -8415;
  14766. /* test with encrypting/decrypting more than 16 bytes at once */
  14767. total = 0;
  14768. EVP_CIPHER_CTX_init(en);
  14769. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  14770. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  14771. return -8416;
  14772. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  14773. (byte*)cbcPlain, 17) == 0)
  14774. return -8417;
  14775. if (outlen != 16)
  14776. return -8418;
  14777. total += outlen;
  14778. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  14779. (byte*)&cbcPlain[17] , 1) == 0)
  14780. return -8419;
  14781. if (outlen != 0)
  14782. return -8420;
  14783. total += outlen;
  14784. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  14785. return -8421;
  14786. if (outlen != 16)
  14787. return -8422;
  14788. total += outlen;
  14789. if (total != 32)
  14790. return -8423;
  14791. total = 0;
  14792. EVP_CIPHER_CTX_init(de);
  14793. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  14794. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  14795. return -8424;
  14796. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  14797. return -8425;
  14798. if (outlen != 16)
  14799. return -8426;
  14800. total += outlen;
  14801. /* final call on non block size should fail */
  14802. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  14803. return -8427;
  14804. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  14805. (byte*)&cipher[17], 1) == 0)
  14806. return -8428;
  14807. if (outlen != 0)
  14808. total += outlen;
  14809. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  14810. (byte*)&cipher[17+1], 14) == 0)
  14811. return -8429;
  14812. if (outlen != 0)
  14813. return -8430;
  14814. total += outlen;
  14815. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  14816. return -8431;
  14817. if (outlen != 2)
  14818. return -8432;
  14819. total += outlen;
  14820. if (total != 18)
  14821. return -8433;
  14822. if (XMEMCMP(plain, cbcPlain, 18))
  14823. return -8434;
  14824. /* test byte by byte decrypt */
  14825. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  14826. plain[i] = i;
  14827. }
  14828. total = 0;
  14829. EVP_CIPHER_CTX_init(en);
  14830. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  14831. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  14832. return -8435;
  14833. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  14834. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  14835. return -8436;
  14836. if (outlen != AES_BLOCK_SIZE * 3)
  14837. return -8437;
  14838. total += outlen;
  14839. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  14840. return -8438;
  14841. if (outlen != AES_BLOCK_SIZE)
  14842. return -8439;
  14843. total += outlen;
  14844. if (total != sizeof(plain))
  14845. return -8440;
  14846. total = 0;
  14847. EVP_CIPHER_CTX_init(de);
  14848. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  14849. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  14850. return -8441;
  14851. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  14852. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  14853. (byte*)cipher + i, 1) == 0)
  14854. return -8442;
  14855. if (outlen > 0) {
  14856. int j;
  14857. total += outlen;
  14858. for (j = 0; j < total; j++) {
  14859. if (plain[j] != j) {
  14860. return -8443;
  14861. }
  14862. }
  14863. }
  14864. }
  14865. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  14866. return -8444;
  14867. total += outlen;
  14868. if (total != AES_BLOCK_SIZE * 3) {
  14869. return -8445;
  14870. }
  14871. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  14872. if (plain[i] != i) {
  14873. return -8446;
  14874. }
  14875. }
  14876. #ifdef WOLFSSL_SMALL_STACK
  14877. wolfSSL_EVP_CIPHER_CTX_free(en);
  14878. wolfSSL_EVP_CIPHER_CTX_free(de);
  14879. #endif
  14880. }
  14881. /* set buffers to be exact size to catch potential over read/write */
  14882. {
  14883. /* EVP_CipherUpdate test */
  14884. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  14885. {
  14886. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  14887. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  14888. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  14889. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  14890. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  14891. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  14892. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  14893. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  14894. };
  14895. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  14896. "0123456789abcdef "; /* align */
  14897. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  14898. "1234567890abcdef "; /* align */
  14899. #define EVP_TEST_BUF_SZ 18
  14900. #define EVP_TEST_BUF_PAD 32
  14901. byte cipher[EVP_TEST_BUF_SZ];
  14902. byte plain [EVP_TEST_BUF_SZ];
  14903. byte padded[EVP_TEST_BUF_PAD];
  14904. #ifdef WOLFSSL_SMALL_STACK
  14905. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  14906. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  14907. #else
  14908. EVP_CIPHER_CTX en[1];
  14909. EVP_CIPHER_CTX de[1];
  14910. #endif
  14911. int outlen ;
  14912. int total = 0;
  14913. #ifdef WOLFSSL_SMALL_STACK
  14914. if ((en == NULL) || (de == NULL))
  14915. return MEMORY_E;
  14916. #endif
  14917. EVP_CIPHER_CTX_init(en);
  14918. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  14919. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  14920. return -8447;
  14921. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  14922. return -8448;
  14923. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  14924. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  14925. return -8449;
  14926. if (outlen != 16)
  14927. return -8450;
  14928. total += outlen;
  14929. /* should fail here */
  14930. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  14931. return -8451;
  14932. /* turn padding back on and do successful encrypt */
  14933. total = 0;
  14934. EVP_CIPHER_CTX_init(en);
  14935. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  14936. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  14937. return -8452;
  14938. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  14939. return -8453;
  14940. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  14941. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  14942. return -8454;
  14943. if (outlen != 16)
  14944. return -8455;
  14945. total += outlen;
  14946. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  14947. return -8456;
  14948. total += outlen;
  14949. if (total != 32)
  14950. return -8457;
  14951. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  14952. /* test out of bounds read on buffers w/o padding during decryption */
  14953. total = 0;
  14954. EVP_CIPHER_CTX_init(de);
  14955. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  14956. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  14957. return -8458;
  14958. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  14959. return -8459;
  14960. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  14961. EVP_TEST_BUF_SZ) == 0)
  14962. return -8460;
  14963. if (outlen != 16)
  14964. return -8461;
  14965. total += outlen;
  14966. /* should fail since not using padding */
  14967. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  14968. return -8462;
  14969. total = 0;
  14970. EVP_CIPHER_CTX_init(de);
  14971. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  14972. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  14973. return -8463;
  14974. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  14975. return -8464;
  14976. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  14977. EVP_TEST_BUF_PAD) == 0)
  14978. return -8465;
  14979. if (outlen != 16)
  14980. return -8466;
  14981. total += outlen;
  14982. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  14983. return -8467;
  14984. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  14985. return -8468;
  14986. #ifdef WOLFSSL_SMALL_STACK
  14987. wolfSSL_EVP_CIPHER_CTX_free(en);
  14988. wolfSSL_EVP_CIPHER_CTX_free(de);
  14989. #endif
  14990. }
  14991. { /* evp_cipher test: EVP_aes_128_cbc */
  14992. #ifdef WOLFSSL_SMALL_STACK
  14993. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  14994. #else
  14995. EVP_CIPHER_CTX ctx[1];
  14996. #endif
  14997. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  14998. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14999. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15000. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15001. };
  15002. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  15003. {
  15004. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  15005. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  15006. };
  15007. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15008. "0123456789abcdef "; /* align */
  15009. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  15010. "1234567890abcdef "; /* align */
  15011. byte cipher[AES_BLOCK_SIZE * 4];
  15012. byte plain [AES_BLOCK_SIZE * 4];
  15013. #ifdef WOLFSSL_SMALL_STACK
  15014. if (ctx == NULL)
  15015. return MEMORY_E;
  15016. #endif
  15017. EVP_CIPHER_CTX_init(ctx);
  15018. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  15019. return -8469;
  15020. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  15021. return -8470;
  15022. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  15023. return -8471;
  15024. EVP_CIPHER_CTX_init(ctx);
  15025. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  15026. return -8472;
  15027. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  15028. return -8473;
  15029. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  15030. return -8474;
  15031. #ifdef WOLFSSL_SMALL_STACK
  15032. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  15033. #endif
  15034. } /* end evp_cipher test: EVP_aes_128_cbc*/
  15035. #endif /* WOLFSSL_AES_128 */
  15036. #endif /* HAVE_AES_CBC */
  15037. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  15038. { /* evp_cipher test: EVP_aes_256_ecb*/
  15039. #ifdef WOLFSSL_SMALL_STACK
  15040. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  15041. #else
  15042. EVP_CIPHER_CTX ctx[1];
  15043. #endif
  15044. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  15045. {
  15046. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15047. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15048. };
  15049. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  15050. {
  15051. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  15052. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  15053. };
  15054. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15055. {
  15056. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15057. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15058. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15059. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15060. };
  15061. byte cipher[AES_BLOCK_SIZE * 4];
  15062. byte plain [AES_BLOCK_SIZE * 4];
  15063. #ifdef WOLFSSL_SMALL_STACK
  15064. if (ctx == NULL)
  15065. return MEMORY_E;
  15066. #endif
  15067. EVP_CIPHER_CTX_init(ctx);
  15068. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  15069. return -8475;
  15070. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  15071. return -8476;
  15072. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  15073. return -8477;
  15074. EVP_CIPHER_CTX_init(ctx);
  15075. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  15076. return -8478;
  15077. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  15078. return -8479;
  15079. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  15080. return -8480;
  15081. #ifdef WOLFSSL_SMALL_STACK
  15082. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  15083. #endif
  15084. } /* end evp_cipher test */
  15085. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  15086. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  15087. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  15088. {
  15089. /* Test: AES_encrypt/decrypt/set Key */
  15090. #ifdef WOLFSSL_SMALL_STACK
  15091. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15092. #ifdef HAVE_AES_DECRYPT
  15093. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15094. #endif
  15095. #else
  15096. AES_KEY enc[1];
  15097. #ifdef HAVE_AES_DECRYPT
  15098. AES_KEY dec[1];
  15099. #endif
  15100. #endif
  15101. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  15102. {
  15103. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15104. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15105. };
  15106. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  15107. {
  15108. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  15109. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  15110. };
  15111. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15112. {
  15113. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15114. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15115. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15116. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15117. };
  15118. byte plain[sizeof(msg)];
  15119. byte cipher[sizeof(msg)];
  15120. #ifdef WOLFSSL_SMALL_STACK
  15121. if (enc == NULL)
  15122. return MEMORY_E;
  15123. #ifdef HAVE_AES_DECRYPT
  15124. if (dec == NULL)
  15125. return MEMORY_E;
  15126. #endif
  15127. #endif
  15128. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  15129. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  15130. AES_encrypt(msg, cipher, enc);
  15131. #ifdef HAVE_AES_DECRYPT
  15132. AES_decrypt(cipher, plain, dec);
  15133. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  15134. return -8481;
  15135. #endif /* HAVE_AES_DECRYPT */
  15136. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  15137. return -8482;
  15138. #ifdef WOLFSSL_SMALL_STACK
  15139. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15140. #ifdef HAVE_AES_DECRYPT
  15141. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15142. #endif
  15143. #endif
  15144. }
  15145. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  15146. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  15147. #ifdef WOLFSSL_AES_COUNTER
  15148. {
  15149. byte plainBuff [64];
  15150. byte cipherBuff[64];
  15151. #ifdef WOLFSSL_AES_128
  15152. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  15153. {
  15154. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  15155. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  15156. };
  15157. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  15158. {
  15159. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  15160. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  15161. };
  15162. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  15163. {
  15164. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15165. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  15166. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  15167. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  15168. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  15169. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  15170. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  15171. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  15172. };
  15173. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  15174. {
  15175. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  15176. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  15177. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  15178. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  15179. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  15180. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  15181. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  15182. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  15183. };
  15184. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  15185. {
  15186. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  15187. 0xc2
  15188. };
  15189. #endif
  15190. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  15191. * NIST Special Publication 800-38A */
  15192. #ifdef WOLFSSL_AES_192
  15193. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  15194. {
  15195. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  15196. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  15197. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  15198. };
  15199. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  15200. {
  15201. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  15202. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  15203. };
  15204. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  15205. {
  15206. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15207. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15208. };
  15209. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  15210. {
  15211. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  15212. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  15213. };
  15214. #endif /* WOLFSSL_AES_192 */
  15215. #ifdef WOLFSSL_AES_256
  15216. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  15217. * NIST Special Publication 800-38A */
  15218. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  15219. {
  15220. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15221. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15222. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15223. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15224. };
  15225. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  15226. {
  15227. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  15228. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  15229. };
  15230. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  15231. {
  15232. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15233. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15234. };
  15235. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  15236. {
  15237. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  15238. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  15239. };
  15240. #endif /* WOLFSSL_AES_256 */
  15241. #ifdef WOLFSSL_SMALL_STACK
  15242. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  15243. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  15244. #else
  15245. EVP_CIPHER_CTX en[1];
  15246. EVP_CIPHER_CTX de[1];
  15247. #endif
  15248. #ifdef WOLFSSL_AES_128
  15249. #ifndef WOLFSSL_SMALL_STACK
  15250. EVP_CIPHER_CTX *p_en;
  15251. EVP_CIPHER_CTX *p_de;
  15252. #endif
  15253. #ifdef WOLFSSL_SMALL_STACK
  15254. if ((en == NULL) || (de == NULL))
  15255. return MEMORY_E;
  15256. #endif
  15257. EVP_CIPHER_CTX_init(en);
  15258. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  15259. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15260. return -8483;
  15261. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  15262. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  15263. return -8484;
  15264. EVP_CIPHER_CTX_init(de);
  15265. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  15266. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15267. return -8485;
  15268. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  15269. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  15270. return -8486;
  15271. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  15272. return -8487;
  15273. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  15274. return -8488;
  15275. #ifndef WOLFSSL_SMALL_STACK
  15276. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  15277. if (p_en == NULL)
  15278. return -8489;
  15279. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  15280. if (p_de == NULL)
  15281. return -8490;
  15282. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  15283. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15284. return -8491;
  15285. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  15286. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  15287. return -8492;
  15288. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  15289. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15290. return -8493;
  15291. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  15292. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  15293. return -8494;
  15294. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  15295. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  15296. #endif /* !WOLFSSL_SMALL_STACK */
  15297. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  15298. return -8495;
  15299. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  15300. return -8496;
  15301. EVP_CIPHER_CTX_init(en);
  15302. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  15303. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15304. return -8497;
  15305. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  15306. return -8498;
  15307. EVP_CIPHER_CTX_init(de);
  15308. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  15309. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15310. return -8499;
  15311. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  15312. return -8500;
  15313. if (XMEMCMP(plainBuff, ctrPlain, 9))
  15314. return -8501;
  15315. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  15316. return -8502;
  15317. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  15318. return -8503;
  15319. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  15320. return -8504;
  15321. if (XMEMCMP(plainBuff, ctrPlain, 9))
  15322. return -8505;
  15323. if (XMEMCMP(cipherBuff, oddCipher, 9))
  15324. return -8506;
  15325. #endif /* WOLFSSL_AES_128 */
  15326. #ifdef WOLFSSL_AES_192
  15327. EVP_CIPHER_CTX_init(en);
  15328. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  15329. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  15330. return -8507;
  15331. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  15332. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  15333. return -8508;
  15334. EVP_CIPHER_CTX_init(de);
  15335. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  15336. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  15337. return -8509;
  15338. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  15339. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  15340. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  15341. return -8510;
  15342. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  15343. return -8511;
  15344. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  15345. return -8512;
  15346. #endif /* WOLFSSL_AES_192 */
  15347. #ifdef WOLFSSL_AES_256
  15348. EVP_CIPHER_CTX_init(en);
  15349. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  15350. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  15351. return -8513;
  15352. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  15353. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  15354. return -8514;
  15355. EVP_CIPHER_CTX_init(de);
  15356. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  15357. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  15358. return -8515;
  15359. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  15360. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  15361. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  15362. return -8516;
  15363. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  15364. return -8517;
  15365. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  15366. return -8518;
  15367. #ifdef WOLFSSL_SMALL_STACK
  15368. wolfSSL_EVP_CIPHER_CTX_free(en);
  15369. wolfSSL_EVP_CIPHER_CTX_free(de);
  15370. #endif
  15371. #endif /* WOLFSSL_AES_256 */
  15372. }
  15373. #endif /* HAVE_AES_COUNTER */
  15374. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  15375. {
  15376. #ifdef WOLFSSL_SMALL_STACK
  15377. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15378. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15379. #else
  15380. AES_KEY enc[1];
  15381. AES_KEY dec[1];
  15382. #endif
  15383. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  15384. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  15385. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  15386. };
  15387. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15388. {
  15389. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  15390. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  15391. };
  15392. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  15393. {
  15394. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  15395. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  15396. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  15397. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  15398. };
  15399. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  15400. {
  15401. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15402. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  15403. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  15404. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  15405. };
  15406. byte cipher[AES_BLOCK_SIZE * 2];
  15407. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  15408. int num = 0;
  15409. #ifdef WOLFSSL_SMALL_STACK
  15410. if ((enc == NULL) || (dec == NULL))
  15411. return MEMORY_E;
  15412. #endif
  15413. XMEMCPY(iv, setIv, sizeof(setIv));
  15414. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  15415. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  15416. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  15417. &num, AES_ENCRYPT);
  15418. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  15419. return -8519;
  15420. if (num != 15) /* should have used 15 of the 16 bytes */
  15421. return -8520;
  15422. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  15423. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  15424. &num, AES_ENCRYPT);
  15425. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  15426. return -8521;
  15427. if (num != 0)
  15428. return -8522;
  15429. #ifdef WOLFSSL_SMALL_STACK
  15430. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15431. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15432. #endif
  15433. }
  15434. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  15435. return 0;
  15436. }
  15437. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  15438. WOLFSSL_TEST_SUBROUTINE int openssl_test(void)
  15439. {
  15440. int ret;
  15441. EVP_MD_CTX md_ctx;
  15442. testVector a, b, c, d, e, f;
  15443. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  15444. a.inLen = 0;
  15445. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  15446. (void)a;
  15447. (void)b;
  15448. (void)c;
  15449. (void)d;
  15450. (void)e;
  15451. (void)f;
  15452. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  15453. {
  15454. byte* p;
  15455. p = (byte*)CRYPTO_malloc(10);
  15456. if (p == NULL) {
  15457. return -8600;
  15458. }
  15459. XMEMSET(p, 0, 10);
  15460. CRYPTO_free(p);
  15461. }
  15462. #ifndef NO_MD5
  15463. a.input = "1234567890123456789012345678901234567890123456789012345678"
  15464. "9012345678901234567890";
  15465. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  15466. "\x7a";
  15467. a.inLen = XSTRLEN(a.input);
  15468. a.outLen = WC_MD5_DIGEST_SIZE;
  15469. EVP_MD_CTX_init(&md_ctx);
  15470. ret = EVP_DigestInit(&md_ctx, EVP_md5());
  15471. if (ret == WOLFSSL_SUCCESS) {
  15472. ret = EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  15473. if (ret == WOLFSSL_SUCCESS)
  15474. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15475. }
  15476. EVP_MD_CTX_cleanup(&md_ctx);
  15477. if (ret != WOLFSSL_SUCCESS ||
  15478. XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  15479. return -8601;
  15480. }
  15481. #endif /* NO_MD5 */
  15482. #ifndef NO_SHA
  15483. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  15484. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  15485. "aaaaaaaaaa";
  15486. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  15487. "\x53\x99\x5E\x26\xA0";
  15488. b.inLen = XSTRLEN(b.input);
  15489. b.outLen = WC_SHA_DIGEST_SIZE;
  15490. EVP_MD_CTX_init(&md_ctx);
  15491. ret = EVP_DigestInit(&md_ctx, EVP_sha1());
  15492. if (ret == WOLFSSL_SUCCESS) {
  15493. ret = EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  15494. if (ret == WOLFSSL_SUCCESS)
  15495. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15496. }
  15497. EVP_MD_CTX_cleanup(&md_ctx);
  15498. if (ret != WOLFSSL_SUCCESS ||
  15499. XMEMCMP(hash, b.output, WC_SHA_DIGEST_SIZE) != 0) {
  15500. return -8602;
  15501. }
  15502. #endif /* NO_SHA */
  15503. #ifdef WOLFSSL_SHA224
  15504. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15505. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15506. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  15507. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  15508. e.inLen = XSTRLEN(e.input);
  15509. e.outLen = WC_SHA224_DIGEST_SIZE;
  15510. EVP_MD_CTX_init(&md_ctx);
  15511. ret = EVP_DigestInit(&md_ctx, EVP_sha224());
  15512. if (ret == WOLFSSL_SUCCESS) {
  15513. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  15514. if (ret == WOLFSSL_SUCCESS)
  15515. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15516. }
  15517. EVP_MD_CTX_cleanup(&md_ctx);
  15518. if (ret != WOLFSSL_SUCCESS ||
  15519. XMEMCMP(hash, e.output, WC_SHA224_DIGEST_SIZE) != 0) {
  15520. return -8603;
  15521. }
  15522. #endif /* WOLFSSL_SHA224 */
  15523. #ifndef NO_SHA256
  15524. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  15525. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  15526. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  15527. "\x06\xC1";
  15528. d.inLen = XSTRLEN(d.input);
  15529. d.outLen = WC_SHA256_DIGEST_SIZE;
  15530. EVP_MD_CTX_init(&md_ctx);
  15531. ret = EVP_DigestInit(&md_ctx, EVP_sha256());
  15532. if (ret == WOLFSSL_SUCCESS) {
  15533. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  15534. if (ret == WOLFSSL_SUCCESS)
  15535. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15536. }
  15537. EVP_MD_CTX_cleanup(&md_ctx);
  15538. if (ret != WOLFSSL_SUCCESS ||
  15539. XMEMCMP(hash, d.output, WC_SHA256_DIGEST_SIZE) != 0) {
  15540. return -8604;
  15541. }
  15542. #endif /* !NO_SHA256 */
  15543. #ifdef WOLFSSL_SHA384
  15544. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15545. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15546. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  15547. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  15548. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  15549. "\x74\x60\x39";
  15550. e.inLen = XSTRLEN(e.input);
  15551. e.outLen = WC_SHA384_DIGEST_SIZE;
  15552. EVP_MD_CTX_init(&md_ctx);
  15553. ret = EVP_DigestInit(&md_ctx, EVP_sha384());
  15554. if (ret == WOLFSSL_SUCCESS) {
  15555. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  15556. if (ret == WOLFSSL_SUCCESS)
  15557. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15558. }
  15559. EVP_MD_CTX_cleanup(&md_ctx);
  15560. if (ret != WOLFSSL_SUCCESS ||
  15561. XMEMCMP(hash, e.output, WC_SHA384_DIGEST_SIZE) != 0) {
  15562. return -8605;
  15563. }
  15564. #endif /* WOLFSSL_SHA384 */
  15565. #ifdef WOLFSSL_SHA512
  15566. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15567. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15568. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  15569. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  15570. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  15571. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  15572. "\x87\x4b\xe9\x09";
  15573. f.inLen = XSTRLEN(f.input);
  15574. f.outLen = WC_SHA512_DIGEST_SIZE;
  15575. EVP_MD_CTX_init(&md_ctx);
  15576. ret = EVP_DigestInit(&md_ctx, EVP_sha512());
  15577. if (ret == WOLFSSL_SUCCESS) {
  15578. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  15579. if (ret == WOLFSSL_SUCCESS)
  15580. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15581. }
  15582. EVP_MD_CTX_cleanup(&md_ctx);
  15583. if (ret != WOLFSSL_SUCCESS ||
  15584. XMEMCMP(hash, f.output, WC_SHA512_DIGEST_SIZE) != 0) {
  15585. return -8606;
  15586. }
  15587. #endif /* WOLFSSL_SHA512 */
  15588. #ifdef WOLFSSL_SHA3
  15589. #ifndef WOLFSSL_NOSHA3_224
  15590. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15591. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15592. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  15593. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  15594. e.inLen = XSTRLEN(e.input);
  15595. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  15596. EVP_MD_CTX_init(&md_ctx);
  15597. ret = EVP_DigestInit(&md_ctx, EVP_sha3_224());
  15598. if (ret == WOLFSSL_SUCCESS) {
  15599. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  15600. if (ret == WOLFSSL_SUCCESS)
  15601. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15602. }
  15603. EVP_MD_CTX_cleanup(&md_ctx);
  15604. if (ret != WOLFSSL_SUCCESS ||
  15605. XMEMCMP(hash, e.output, WC_SHA3_224_DIGEST_SIZE) != 0) {
  15606. return -8607;
  15607. }
  15608. #endif /* WOLFSSL_NOSHA3_224 */
  15609. #ifndef WOLFSSL_NOSHA3_256
  15610. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15611. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15612. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  15613. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  15614. "\x1d\x18";
  15615. d.inLen = XSTRLEN(d.input);
  15616. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  15617. EVP_MD_CTX_init(&md_ctx);
  15618. ret = EVP_DigestInit(&md_ctx, EVP_sha3_256());
  15619. if (ret == WOLFSSL_SUCCESS) {
  15620. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  15621. if (ret == WOLFSSL_SUCCESS)
  15622. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15623. }
  15624. EVP_MD_CTX_cleanup(&md_ctx);
  15625. if (ret != WOLFSSL_SUCCESS ||
  15626. XMEMCMP(hash, d.output, WC_SHA3_256_DIGEST_SIZE) != 0) {
  15627. return -8608;
  15628. }
  15629. #endif /* WOLFSSL_NOSHA3_256 */
  15630. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15631. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15632. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  15633. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  15634. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  15635. "\xaa\x7f\xc7";
  15636. e.inLen = XSTRLEN(e.input);
  15637. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  15638. EVP_MD_CTX_init(&md_ctx);
  15639. ret = EVP_DigestInit(&md_ctx, EVP_sha3_384());
  15640. if (ret == WOLFSSL_SUCCESS) {
  15641. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  15642. if (ret == WOLFSSL_SUCCESS)
  15643. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15644. }
  15645. EVP_MD_CTX_cleanup(&md_ctx);
  15646. if (ret != WOLFSSL_SUCCESS ||
  15647. XMEMCMP(hash, e.output, WC_SHA3_384_DIGEST_SIZE) != 0) {
  15648. return -8609;
  15649. }
  15650. #ifndef WOLFSSL_NOSHA3_512
  15651. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15652. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15653. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  15654. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  15655. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  15656. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  15657. "\xf2\x1d\xd1\x85";
  15658. f.inLen = XSTRLEN(f.input);
  15659. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  15660. EVP_MD_CTX_init(&md_ctx);
  15661. ret = EVP_DigestInit(&md_ctx, EVP_sha3_512());
  15662. if (ret == WOLFSSL_SUCCESS) {
  15663. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  15664. if (ret == WOLFSSL_SUCCESS)
  15665. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15666. }
  15667. EVP_MD_CTX_cleanup(&md_ctx);
  15668. if (ret != WOLFSSL_SUCCESS ||
  15669. XMEMCMP(hash, f.output, WC_SHA3_512_DIGEST_SIZE) != 0) {
  15670. return -8610;
  15671. }
  15672. #endif /* WOLFSSL_NOSHA3_512 */
  15673. #endif /* WOLFSSL_SHA3 */
  15674. #ifndef WC_NO_RNG
  15675. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  15676. return -8611;
  15677. #endif
  15678. #ifndef NO_MD5
  15679. c.input = "what do ya want for nothing?";
  15680. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  15681. "\x76";
  15682. c.inLen = XSTRLEN(c.input);
  15683. c.outLen = WC_MD5_DIGEST_SIZE;
  15684. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  15685. hash, 0) == NULL ||
  15686. XMEMCMP(hash, c.output, WC_MD5_DIGEST_SIZE) != 0)
  15687. {
  15688. return -8612;
  15689. }
  15690. #endif /* NO_MD5 */
  15691. #ifndef NO_DES3
  15692. { /* des test */
  15693. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  15694. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15695. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15696. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15697. };
  15698. byte plain[24];
  15699. byte cipher[24];
  15700. const_DES_cblock key = {
  15701. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  15702. };
  15703. DES_cblock iv = {
  15704. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  15705. };
  15706. DES_key_schedule sched;
  15707. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  15708. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  15709. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  15710. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  15711. };
  15712. DES_key_sched(&key, &sched);
  15713. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  15714. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  15715. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  15716. return -8613;
  15717. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  15718. return -8614;
  15719. /* test changing iv */
  15720. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  15721. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  15722. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  15723. return -8615;
  15724. } /* end des test */
  15725. #endif /* NO_DES3 */
  15726. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  15727. if (openssl_aes_test() != 0) {
  15728. return -8616;
  15729. }
  15730. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  15731. { /* evp_cipher test: EVP_aes_128_cbc */
  15732. #ifdef WOLFSSL_SMALL_STACK
  15733. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  15734. #else
  15735. EVP_CIPHER_CTX ctx[1];
  15736. #endif
  15737. int idx, cipherSz, plainSz;
  15738. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  15739. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15740. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15741. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15742. };
  15743. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  15744. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  15745. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  15746. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  15747. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  15748. };
  15749. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  15750. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  15751. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  15752. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  15753. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  15754. };
  15755. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15756. "0123456789abcdef "; /* align */
  15757. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  15758. "1234567890abcdef "; /* align */
  15759. byte cipher[AES_BLOCK_SIZE * 4];
  15760. byte plain [AES_BLOCK_SIZE * 4];
  15761. #ifdef WOLFSSL_SMALL_STACK
  15762. if (ctx == NULL)
  15763. return MEMORY_E;
  15764. #endif
  15765. cipherSz = 0;
  15766. EVP_CIPHER_CTX_init(ctx);
  15767. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  15768. if (ret == WOLFSSL_SUCCESS) {
  15769. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  15770. if (ret == WOLFSSL_SUCCESS)
  15771. cipherSz += idx;
  15772. }
  15773. if (ret == WOLFSSL_SUCCESS) {
  15774. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  15775. if (ret == WOLFSSL_SUCCESS)
  15776. cipherSz += idx;
  15777. }
  15778. EVP_CIPHER_CTX_cleanup(ctx);
  15779. if (ret != WOLFSSL_SUCCESS)
  15780. return -8617;
  15781. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  15782. return -8618;
  15783. /* check partial decrypt (not enough padding for full block) */
  15784. plainSz = 0;
  15785. EVP_CIPHER_CTX_init(ctx);
  15786. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  15787. if (ret == WOLFSSL_SUCCESS) {
  15788. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  15789. if (ret == WOLFSSL_SUCCESS)
  15790. plainSz += idx;
  15791. }
  15792. if (ret == WOLFSSL_SUCCESS) {
  15793. /* this test should fail... not enough padding for full block */
  15794. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  15795. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  15796. ret = WOLFSSL_SUCCESS;
  15797. else
  15798. ret = -8619;
  15799. }
  15800. else
  15801. ret = -8620;
  15802. EVP_CIPHER_CTX_cleanup(ctx);
  15803. if (ret != WOLFSSL_SUCCESS)
  15804. return ret;
  15805. plainSz = 0;
  15806. EVP_CIPHER_CTX_init(ctx);
  15807. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  15808. if (ret == WOLFSSL_SUCCESS) {
  15809. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  15810. if (ret == WOLFSSL_SUCCESS)
  15811. plainSz += idx;
  15812. }
  15813. if (ret == WOLFSSL_SUCCESS) {
  15814. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  15815. if (ret == WOLFSSL_SUCCESS)
  15816. plainSz += idx;
  15817. }
  15818. EVP_CIPHER_CTX_cleanup(ctx);
  15819. if (ret != WOLFSSL_SUCCESS)
  15820. return -8621;
  15821. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  15822. return -8622;
  15823. cipherSz = 0;
  15824. EVP_CIPHER_CTX_init(ctx);
  15825. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  15826. if (ret == WOLFSSL_SUCCESS) {
  15827. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  15828. if (ret == WOLFSSL_SUCCESS)
  15829. cipherSz += idx;
  15830. }
  15831. if (ret == WOLFSSL_SUCCESS) {
  15832. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  15833. if (ret == WOLFSSL_SUCCESS)
  15834. cipherSz += idx;
  15835. }
  15836. EVP_CIPHER_CTX_cleanup(ctx);
  15837. if (ret != WOLFSSL_SUCCESS)
  15838. return -8623;
  15839. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  15840. return -8624;
  15841. #ifdef WOLFSSL_SMALL_STACK
  15842. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  15843. #endif
  15844. } /* end evp_cipher test: EVP_aes_128_cbc*/
  15845. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  15846. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  15847. { /* evp_cipher test: EVP_aes_256_ecb*/
  15848. #ifdef WOLFSSL_SMALL_STACK
  15849. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  15850. #else
  15851. EVP_CIPHER_CTX ctx[1];
  15852. #endif
  15853. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  15854. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15855. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15856. };
  15857. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  15858. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  15859. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  15860. };
  15861. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  15862. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15863. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15864. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15865. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15866. };
  15867. byte cipher[AES_BLOCK_SIZE * 4];
  15868. byte plain [AES_BLOCK_SIZE * 4];
  15869. #ifdef WOLFSSL_SMALL_STACK
  15870. if (ctx == NULL)
  15871. return MEMORY_E;
  15872. #endif
  15873. EVP_CIPHER_CTX_init(ctx);
  15874. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  15875. if (ret == WOLFSSL_SUCCESS)
  15876. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  15877. EVP_CIPHER_CTX_cleanup(ctx);
  15878. if (ret != 16)
  15879. return -8625;
  15880. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  15881. return -8626;
  15882. EVP_CIPHER_CTX_init(ctx);
  15883. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  15884. if (ret == WOLFSSL_SUCCESS)
  15885. ret = EVP_Cipher(ctx, plain, cipher, 16);
  15886. EVP_CIPHER_CTX_cleanup(ctx);
  15887. if (ret != 16)
  15888. return -8627;
  15889. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  15890. return -8628;
  15891. #ifdef WOLFSSL_SMALL_STACK
  15892. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  15893. #endif
  15894. } /* end evp_cipher test */
  15895. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  15896. #define OPENSSL_TEST_ERROR (-10000)
  15897. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  15898. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  15899. {
  15900. /* Test: AES_encrypt/decrypt/set Key */
  15901. #ifdef WOLFSSL_SMALL_STACK
  15902. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15903. #ifdef HAVE_AES_DECRYPT
  15904. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15905. #endif
  15906. #else
  15907. AES_KEY enc[1];
  15908. #ifdef HAVE_AES_DECRYPT
  15909. AES_KEY dec[1];
  15910. #endif
  15911. #endif
  15912. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  15913. {
  15914. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15915. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15916. };
  15917. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  15918. {
  15919. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  15920. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  15921. };
  15922. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15923. {
  15924. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15925. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15926. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15927. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15928. };
  15929. byte plain[sizeof(msg)];
  15930. byte cipher[sizeof(msg)];
  15931. printf("openSSL extra test\n") ;
  15932. #ifdef WOLFSSL_SMALL_STACK
  15933. if (enc == NULL)
  15934. return MEMORY_E;
  15935. #ifdef HAVE_AES_DECRYPT
  15936. if (dec == NULL)
  15937. return MEMORY_E;
  15938. #endif
  15939. #endif
  15940. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  15941. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  15942. AES_encrypt(msg, cipher, enc);
  15943. #ifdef HAVE_AES_DECRYPT
  15944. AES_decrypt(cipher, plain, dec);
  15945. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  15946. return OPENSSL_TEST_ERROR-60;
  15947. #endif /* HAVE_AES_DECRYPT */
  15948. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  15949. return OPENSSL_TEST_ERROR-61;
  15950. #ifdef WOLFSSL_SMALL_STACK
  15951. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15952. #ifdef HAVE_AES_DECRYPT
  15953. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15954. #endif
  15955. #endif
  15956. }
  15957. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  15958. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  15959. #ifdef WOLFSSL_AES_COUNTER
  15960. {
  15961. byte plainBuff [64];
  15962. byte cipherBuff[64];
  15963. #ifdef WOLFSSL_AES_128
  15964. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  15965. {
  15966. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  15967. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  15968. };
  15969. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  15970. {
  15971. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  15972. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  15973. };
  15974. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  15975. {
  15976. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15977. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  15978. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  15979. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  15980. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  15981. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  15982. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  15983. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  15984. };
  15985. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  15986. {
  15987. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  15988. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  15989. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  15990. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  15991. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  15992. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  15993. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  15994. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  15995. };
  15996. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  15997. {
  15998. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  15999. 0xc2
  16000. };
  16001. #endif /* WOLFSSL_AES_128 */
  16002. #ifdef WOLFSSL_AES_192
  16003. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16004. * NIST Special Publication 800-38A */
  16005. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  16006. {
  16007. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  16008. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  16009. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  16010. };
  16011. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  16012. {
  16013. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16014. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16015. };
  16016. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  16017. {
  16018. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16019. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16020. };
  16021. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  16022. {
  16023. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  16024. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  16025. };
  16026. #endif /* WOLFSSL_AES_192 */
  16027. #ifdef WOLFSSL_AES_256
  16028. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16029. * NIST Special Publication 800-38A */
  16030. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  16031. {
  16032. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16033. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16034. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16035. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16036. };
  16037. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  16038. {
  16039. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16040. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16041. };
  16042. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  16043. {
  16044. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16045. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16046. };
  16047. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  16048. {
  16049. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  16050. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  16051. };
  16052. #endif /* WOLFSSL_AES_256 */
  16053. #ifdef WOLFSSL_SMALL_STACK
  16054. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16055. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16056. if ((en == NULL) || (de == NULL))
  16057. return MEMORY_E;
  16058. #else
  16059. EVP_CIPHER_CTX en[1];
  16060. EVP_CIPHER_CTX de[1];
  16061. #endif
  16062. #ifdef WOLFSSL_AES_128
  16063. #ifndef WOLFSSL_SMALL_STACK
  16064. EVP_CIPHER_CTX *p_en;
  16065. EVP_CIPHER_CTX *p_de;
  16066. #endif
  16067. EVP_CIPHER_CTX_init(en);
  16068. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16069. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16070. return -8629;
  16071. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  16072. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16073. return -8630;
  16074. EVP_CIPHER_CTX_init(de);
  16075. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16076. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16077. return -8631;
  16078. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16079. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16080. return -8632;
  16081. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16082. return -8633;
  16083. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16084. return -8634;
  16085. #ifndef WOLFSSL_SMALL_STACK
  16086. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  16087. if(p_en == NULL)return -8635;
  16088. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  16089. if(p_de == NULL)return -8636;
  16090. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  16091. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16092. return -8637;
  16093. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  16094. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16095. return -8638;
  16096. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  16097. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16098. return -8639;
  16099. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  16100. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16101. return -8640;
  16102. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  16103. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  16104. #endif /* !WOLFSSL_SMALL_STACK */
  16105. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16106. return -8641;
  16107. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16108. return -8642;
  16109. EVP_CIPHER_CTX_init(en);
  16110. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16111. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16112. return -8643;
  16113. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16114. return -8644;
  16115. EVP_CIPHER_CTX_init(de);
  16116. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16117. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16118. return -8645;
  16119. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16120. return -8646;
  16121. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16122. return -8647;
  16123. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  16124. return -8648;
  16125. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16126. return -8649;
  16127. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16128. return -8650;
  16129. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16130. return -8651;
  16131. if (XMEMCMP(cipherBuff, oddCipher, 9))
  16132. return -8652;
  16133. #endif /* WOLFSSL_AES_128 */
  16134. #ifdef WOLFSSL_AES_192
  16135. EVP_CIPHER_CTX_init(en);
  16136. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  16137. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16138. return -8653;
  16139. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  16140. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16141. return -8654;
  16142. EVP_CIPHER_CTX_init(de);
  16143. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  16144. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16145. return -8655;
  16146. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16147. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16148. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16149. return -8656;
  16150. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  16151. return -8657;
  16152. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  16153. return -8658;
  16154. #endif /* WOLFSSL_AES_192 */
  16155. #ifdef WOLFSSL_AES_256
  16156. EVP_CIPHER_CTX_init(en);
  16157. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  16158. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16159. return -8659;
  16160. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  16161. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16162. return -8660;
  16163. EVP_CIPHER_CTX_init(de);
  16164. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  16165. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16166. return -8661;
  16167. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16168. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16169. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16170. return -8662;
  16171. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  16172. return -8663;
  16173. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  16174. return -8664;
  16175. #endif /* WOLFSSL_AES_256 */
  16176. #ifdef WOLFSSL_SMALL_STACK
  16177. wolfSSL_EVP_CIPHER_CTX_free(en);
  16178. wolfSSL_EVP_CIPHER_CTX_free(de);
  16179. #endif
  16180. }
  16181. #endif /* HAVE_AES_COUNTER */
  16182. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  16183. {
  16184. /* EVP_CipherUpdate test */
  16185. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  16186. {
  16187. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16188. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16189. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16190. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16191. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16192. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16193. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16194. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16195. };
  16196. byte key[] = "0123456789abcdef "; /* align */
  16197. byte iv[] = "1234567890abcdef "; /* align */
  16198. byte cipher[AES_BLOCK_SIZE * 4];
  16199. byte plain [AES_BLOCK_SIZE * 4];
  16200. #ifdef WOLFSSL_SMALL_STACK
  16201. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16202. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16203. #else
  16204. EVP_CIPHER_CTX en[1];
  16205. EVP_CIPHER_CTX de[1];
  16206. #endif
  16207. int outlen ;
  16208. int total = 0;
  16209. #ifdef WOLFSSL_SMALL_STACK
  16210. if ((en == NULL) || (de == NULL))
  16211. return MEMORY_E;
  16212. #endif
  16213. EVP_CIPHER_CTX_init(en);
  16214. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16215. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16216. return -8665;
  16217. /* openSSL compatibility, if(inlen == 0)return 1; */
  16218. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16219. (byte*)cbcPlain, 0) != 1)
  16220. return -8666;
  16221. EVP_CIPHER_CTX_init(en);
  16222. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16223. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16224. return -8667;
  16225. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16226. (byte*)cbcPlain, 9) == 0)
  16227. return -8668;
  16228. if(outlen != 0)
  16229. return -8669;
  16230. total += outlen;
  16231. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  16232. (byte*)&cbcPlain[9] , 9) == 0)
  16233. return -8670;
  16234. if(outlen != 16)
  16235. return -8671;
  16236. total += outlen;
  16237. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16238. return -8672;
  16239. if(outlen != 16)
  16240. return -8673;
  16241. total += outlen;
  16242. if(total != 32)
  16243. return -8674;
  16244. total = 0;
  16245. EVP_CIPHER_CTX_init(de);
  16246. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16247. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16248. return -8675;
  16249. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  16250. return -8676;
  16251. if(outlen != 0)
  16252. return -8677;
  16253. total += outlen;
  16254. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16255. (byte*)&cipher[6], 12) == 0)
  16256. return -8678;
  16257. if(outlen != 0)
  16258. total += outlen;
  16259. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16260. (byte*)&cipher[6+12], 14) == 0)
  16261. return -8679;
  16262. if(outlen != 16)
  16263. return -8680;
  16264. total += outlen;
  16265. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16266. return -8681;
  16267. if(outlen != 2)
  16268. return -8682;
  16269. total += outlen;
  16270. if(total != 18)
  16271. return -8683;
  16272. if (XMEMCMP(plain, cbcPlain, 18))
  16273. return -8684;
  16274. total = 0;
  16275. EVP_CIPHER_CTX_init(en);
  16276. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  16277. (unsigned char*)key, (unsigned char*)iv) == 0)
  16278. return -8685;
  16279. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  16280. return -8686;
  16281. if(outlen != 0)
  16282. return -8687;
  16283. total += outlen;
  16284. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  16285. return -8688;
  16286. if(outlen != 16)
  16287. return -8689;
  16288. total += outlen;
  16289. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16290. return -8690;
  16291. if(outlen != 16)
  16292. return -8691;
  16293. total += outlen;
  16294. if(total != 32)
  16295. return 3438;
  16296. total = 0;
  16297. EVP_CIPHER_CTX_init(de);
  16298. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  16299. (unsigned char*)key, (unsigned char*)iv) == 0)
  16300. return -8692;
  16301. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  16302. return -8693;
  16303. if(outlen != 0)
  16304. return -8694;
  16305. total += outlen;
  16306. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  16307. return -8695;
  16308. if(outlen != 0)
  16309. total += outlen;
  16310. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  16311. return -8696;
  16312. if(outlen != 16)
  16313. return -8697;
  16314. total += outlen;
  16315. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  16316. return -8698;
  16317. if(outlen != 2)
  16318. return -8699;
  16319. total += outlen;
  16320. if(total != 18)
  16321. return 3447;
  16322. if (XMEMCMP(plain, cbcPlain, 18))
  16323. return -8700;
  16324. if (EVP_CIPHER_key_length(NULL) != 0)
  16325. return -8701;
  16326. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  16327. return -8702;
  16328. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  16329. return -8703;
  16330. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  16331. return -8704;
  16332. EVP_CIPHER_CTX_init(en);
  16333. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  16334. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16335. return -8705;
  16336. EVP_CIPHER_CTX_init(en);
  16337. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  16338. (unsigned char*)key, (unsigned char*)iv) == 0)
  16339. return -8706;
  16340. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  16341. return -8707;
  16342. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  16343. return -8708;
  16344. EVP_CIPHER_CTX_init(de);
  16345. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  16346. (unsigned char*)key, (unsigned char*)iv) == 0)
  16347. return -8709;
  16348. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  16349. return -8710;
  16350. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  16351. return -8711;
  16352. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  16353. return -8712;
  16354. EVP_CIPHER_CTX_init(en);
  16355. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  16356. (unsigned char*)key, (unsigned char*)iv);
  16357. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  16358. return -8713;
  16359. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  16360. return -8714;
  16361. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  16362. return -8715;
  16363. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  16364. return -8716;
  16365. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  16366. return -8717;
  16367. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  16368. EVP_CIPHER_CTX_set_flags(en, 42);
  16369. if (en->flags != 42)
  16370. return -8718;
  16371. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  16372. return -8719;
  16373. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  16374. return -8720;
  16375. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  16376. return -8721;
  16377. #ifdef WOLFSSL_SMALL_STACK
  16378. wolfSSL_EVP_CIPHER_CTX_free(en);
  16379. wolfSSL_EVP_CIPHER_CTX_free(de);
  16380. #endif
  16381. }
  16382. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  16383. #endif /* ifndef NO_AES */
  16384. return 0;
  16385. }
  16386. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void)
  16387. {
  16388. int ret = 0;
  16389. #if !defined(NO_SHA256) && !defined(NO_SHA)
  16390. WOLFSSL_EVP_MD_CTX* ctx;
  16391. WOLFSSL_EVP_MD_CTX* ctx2;
  16392. ctx = EVP_MD_CTX_create();
  16393. ctx2 = EVP_MD_CTX_create();
  16394. ret = EVP_DigestInit(ctx, EVP_sha256());
  16395. if (ret != SSL_SUCCESS) {
  16396. ret = -8800;
  16397. goto openSSL_evpMD_test_done;
  16398. }
  16399. ret = EVP_MD_CTX_copy(ctx2, ctx);
  16400. if (ret != SSL_SUCCESS) {
  16401. ret = -8801;
  16402. goto openSSL_evpMD_test_done;
  16403. }
  16404. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  16405. ret = -8802;
  16406. goto openSSL_evpMD_test_done;
  16407. }
  16408. ret = EVP_DigestInit(ctx, EVP_sha1());
  16409. if (ret != SSL_SUCCESS) {
  16410. ret = -8803;
  16411. goto openSSL_evpMD_test_done;
  16412. }
  16413. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  16414. ret = -8804;
  16415. goto openSSL_evpMD_test_done;
  16416. }
  16417. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  16418. if (ret != SSL_SUCCESS) {
  16419. ret = -8805;
  16420. goto openSSL_evpMD_test_done;
  16421. }
  16422. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  16423. ret = -8806;
  16424. goto openSSL_evpMD_test_done;
  16425. }
  16426. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  16427. ret = -8807;
  16428. goto openSSL_evpMD_test_done;
  16429. }
  16430. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  16431. ret = -8808;
  16432. goto openSSL_evpMD_test_done;
  16433. }
  16434. if (EVP_add_digest(NULL) != 0) {
  16435. ret = -8809;
  16436. goto openSSL_evpMD_test_done;
  16437. }
  16438. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  16439. ret = -8810;
  16440. goto openSSL_evpMD_test_done;
  16441. }
  16442. ret = 0; /* got to success state without jumping to end with a fail */
  16443. openSSL_evpMD_test_done:
  16444. EVP_MD_CTX_destroy(ctx);
  16445. EVP_MD_CTX_destroy(ctx2);
  16446. #endif /* NO_SHA256 */
  16447. return ret;
  16448. }
  16449. #ifdef DEBUG_SIGN
  16450. static void show(const char *title, const char *p, unsigned int s) {
  16451. char* i;
  16452. printf("%s: ", title);
  16453. for (i = p;
  16454. i < p + s;
  16455. printf("%c", *i), i++);
  16456. printf("\n");
  16457. }
  16458. #else
  16459. #define show(a,b,c)
  16460. #endif
  16461. #define FOURK_BUFF 4096
  16462. #define ERR_BASE_PKEY -5000
  16463. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void)
  16464. {
  16465. int ret = 0;
  16466. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(NO_SHA)
  16467. byte* prvTmp;
  16468. byte* pubTmp;
  16469. int prvBytes;
  16470. int pubBytes;
  16471. RSA *prvRsa = NULL;
  16472. RSA *pubRsa = NULL;
  16473. EVP_PKEY *prvPkey = NULL;
  16474. EVP_PKEY *pubPkey = NULL;
  16475. EVP_PKEY_CTX *enc = NULL;
  16476. EVP_PKEY_CTX *dec = NULL;
  16477. byte in[] = TEST_STRING;
  16478. byte out[256];
  16479. size_t outlen;
  16480. size_t keySz;
  16481. byte plain[256];
  16482. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  16483. XFILE keyFile;
  16484. XFILE keypubFile;
  16485. char cliKey[] = "./certs/client-key.der";
  16486. char cliKeypub[] = "./certs/client-keyPub.der";
  16487. #endif
  16488. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16489. if (prvTmp == NULL)
  16490. return ERR_BASE_PKEY-1;
  16491. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16492. if (pubTmp == NULL) {
  16493. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16494. return ERR_BASE_PKEY-2;
  16495. }
  16496. #ifdef USE_CERT_BUFFERS_1024
  16497. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  16498. prvBytes = sizeof_client_key_der_1024;
  16499. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16500. pubBytes = sizeof_client_keypub_der_1024;
  16501. #elif defined(USE_CERT_BUFFERS_2048)
  16502. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  16503. prvBytes = sizeof_client_key_der_2048;
  16504. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16505. pubBytes = sizeof_client_keypub_der_2048;
  16506. #else
  16507. keyFile = XFOPEN(cliKey, "rb");
  16508. if (!keyFile) {
  16509. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16510. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16511. err_sys("can't open ./certs/client-key.der, "
  16512. "Please run from wolfSSL home dir", ERR_BASE_PKEY-3);
  16513. return ERR_BASE_PKEY-3;
  16514. }
  16515. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  16516. XFCLOSE(keyFile);
  16517. keypubFile = XFOPEN(cliKeypub, "rb");
  16518. if (!keypubFile) {
  16519. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16520. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16521. err_sys("can't open ./certs/client-cert.der, "
  16522. "Please run from wolfSSL home dir", -4);
  16523. return ERR_BASE_PKEY-4;
  16524. }
  16525. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  16526. XFCLOSE(keypubFile);
  16527. #endif /* USE_CERT_BUFFERS */
  16528. prvRsa = wolfSSL_RSA_new();
  16529. pubRsa = wolfSSL_RSA_new();
  16530. if((prvRsa == NULL) || (pubRsa == NULL)){
  16531. printf("error with RSA_new\n");
  16532. ret = ERR_BASE_PKEY-10;
  16533. goto openssl_pkey0_test_done;
  16534. }
  16535. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  16536. if(ret != SSL_SUCCESS){
  16537. printf("error with RSA_LoadDer_ex\n");
  16538. ret = ERR_BASE_PKEY-11;
  16539. goto openssl_pkey0_test_done;
  16540. }
  16541. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  16542. if(ret != SSL_SUCCESS){
  16543. printf("error with RSA_LoadDer_ex\n");
  16544. ret = ERR_BASE_PKEY-12;
  16545. goto openssl_pkey0_test_done;
  16546. }
  16547. keySz = (size_t)RSA_size(pubRsa);
  16548. prvPkey = wolfSSL_EVP_PKEY_new();
  16549. pubPkey = wolfSSL_EVP_PKEY_new();
  16550. if((prvPkey == NULL) || (pubPkey == NULL)){
  16551. printf("error with PKEY_new\n");
  16552. ret = ERR_BASE_PKEY-13;
  16553. goto openssl_pkey0_test_done;
  16554. }
  16555. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  16556. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  16557. if(ret != 2){
  16558. printf("error with PKEY_set1_RSA\n");
  16559. ret = ERR_BASE_PKEY-14;
  16560. goto openssl_pkey0_test_done;
  16561. }
  16562. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  16563. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  16564. if((dec == NULL)||(enc==NULL)){
  16565. printf("error with EVP_PKEY_CTX_new\n");
  16566. ret = ERR_BASE_PKEY-15;
  16567. goto openssl_pkey0_test_done;
  16568. }
  16569. ret = EVP_PKEY_decrypt_init(dec);
  16570. if (ret != 1) {
  16571. printf("error with decrypt init\n");
  16572. ret = ERR_BASE_PKEY-16;
  16573. goto openssl_pkey0_test_done;
  16574. }
  16575. ret = EVP_PKEY_encrypt_init(enc);
  16576. if (ret != 1) {
  16577. printf("error with encrypt init\n");
  16578. ret = ERR_BASE_PKEY-17;
  16579. goto openssl_pkey0_test_done;
  16580. }
  16581. XMEMSET(out, 0, sizeof(out));
  16582. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  16583. if (ret != 1) {
  16584. printf("error encrypting msg\n");
  16585. ret = ERR_BASE_PKEY-18;
  16586. goto openssl_pkey0_test_done;
  16587. }
  16588. show("encrypted msg", out, outlen);
  16589. XMEMSET(plain, 0, sizeof(plain));
  16590. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  16591. if (ret != 1) {
  16592. printf("error decrypting msg\n");
  16593. ret = ERR_BASE_PKEY-19;
  16594. goto openssl_pkey0_test_done;
  16595. }
  16596. show("decrypted msg", plain, outlen);
  16597. /* RSA_PKCS1_OAEP_PADDING test */
  16598. ret = EVP_PKEY_decrypt_init(dec);
  16599. if (ret != 1) {
  16600. printf("error with decrypt init\n");
  16601. ret = ERR_BASE_PKEY-30;
  16602. goto openssl_pkey0_test_done;
  16603. }
  16604. ret = EVP_PKEY_encrypt_init(enc);
  16605. if (ret != 1) {
  16606. printf("error with encrypt init\n");
  16607. ret = ERR_BASE_PKEY-31;
  16608. goto openssl_pkey0_test_done;
  16609. }
  16610. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  16611. printf("first set rsa padding error\n");
  16612. ret = ERR_BASE_PKEY-32;
  16613. goto openssl_pkey0_test_done;
  16614. }
  16615. #ifndef HAVE_FIPS
  16616. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  16617. printf("second set rsa padding error\n");
  16618. ret = ERR_BASE_PKEY-33;
  16619. goto openssl_pkey0_test_done;
  16620. }
  16621. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  16622. printf("third set rsa padding error\n");
  16623. ret = ERR_BASE_PKEY-34;
  16624. goto openssl_pkey0_test_done;
  16625. }
  16626. #endif
  16627. XMEMSET(out, 0, sizeof(out));
  16628. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  16629. if (ret != 1) {
  16630. printf("error encrypting msg\n");
  16631. ret = ERR_BASE_PKEY-35;
  16632. goto openssl_pkey0_test_done;
  16633. }
  16634. show("encrypted msg", out, outlen);
  16635. XMEMSET(plain, 0, sizeof(plain));
  16636. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  16637. if (ret != 1) {
  16638. printf("error decrypting msg\n");
  16639. ret = ERR_BASE_PKEY-36;
  16640. goto openssl_pkey0_test_done;
  16641. }
  16642. show("decrypted msg", plain, outlen);
  16643. ret = 0; /* made it to this point without error then set success */
  16644. openssl_pkey0_test_done:
  16645. wolfSSL_RSA_free(prvRsa);
  16646. wolfSSL_RSA_free(pubRsa);
  16647. EVP_PKEY_free(pubPkey);
  16648. EVP_PKEY_free(prvPkey);
  16649. EVP_PKEY_CTX_free(dec);
  16650. EVP_PKEY_CTX_free(enc);
  16651. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16652. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16653. #endif /* NO_RSA */
  16654. return ret;
  16655. }
  16656. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void)
  16657. {
  16658. int ret = 0;
  16659. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  16660. !defined(NO_SHA)
  16661. EVP_PKEY_CTX* dec = NULL;
  16662. EVP_PKEY_CTX* enc = NULL;
  16663. EVP_PKEY* pubKey = NULL;
  16664. EVP_PKEY* prvKey = NULL;
  16665. X509* x509 = NULL;
  16666. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  16667. const unsigned char* clikey;
  16668. long cliKeySz;
  16669. size_t outlen;
  16670. int keyLenBits = 2048;
  16671. #ifdef WOLFSSL_SMALL_STACK
  16672. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16673. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16674. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16675. if ((tmp == NULL) ||
  16676. (cipher == NULL) ||
  16677. (plain == NULL)) {
  16678. ret = -9015;
  16679. goto openssl_pkey1_test_done;
  16680. }
  16681. #else
  16682. unsigned char tmp[FOURK_BUF];
  16683. unsigned char cipher[RSA_TEST_BYTES];
  16684. unsigned char plain[RSA_TEST_BYTES];
  16685. #endif
  16686. #if defined(USE_CERT_BUFFERS_1024)
  16687. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  16688. cliKeySz = (long)sizeof_client_key_der_1024;
  16689. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  16690. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  16691. keyLenBits = 1024;
  16692. #elif defined(USE_CERT_BUFFERS_2048)
  16693. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  16694. cliKeySz = (long)sizeof_client_key_der_2048;
  16695. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  16696. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  16697. #elif defined(USE_CERT_BUFFERS_3072)
  16698. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  16699. cliKeySz = (long)sizeof_client_key_der_3072;
  16700. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  16701. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  16702. keyLenBits = 3072;
  16703. #elif defined(USE_CERT_BUFFERS_4096)
  16704. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  16705. cliKeySz = (long)sizeof_client_key_der_4096;
  16706. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  16707. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  16708. keyLenBits = 4096;
  16709. #else
  16710. XFILE f;
  16711. f = XFOPEN(clientKey, "rb");
  16712. if (!f) {
  16713. err_sys("can't open ./certs/client-key.der, "
  16714. "Please run from wolfSSL home dir", -41);
  16715. ret = -9000;
  16716. goto openssl_pkey1_test_done;
  16717. }
  16718. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  16719. XFCLOSE(f);
  16720. /* using existing wolfSSL api to get public and private key */
  16721. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  16722. #endif /* USE_CERT_BUFFERS */
  16723. clikey = tmp;
  16724. if ((prvKey = EVP_PKEY_new()) == NULL) {
  16725. ret = -9001;
  16726. goto openssl_pkey1_test_done;
  16727. }
  16728. EVP_PKEY_free(prvKey);
  16729. prvKey = NULL;
  16730. if (x509 == NULL) {
  16731. ret = -9002;
  16732. goto openssl_pkey1_test_done;
  16733. }
  16734. pubKey = X509_get_pubkey(x509);
  16735. if (pubKey == NULL) {
  16736. ret = -9003;
  16737. goto openssl_pkey1_test_done;
  16738. }
  16739. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  16740. if (prvKey == NULL) {
  16741. ret = -9004;
  16742. goto openssl_pkey1_test_done;
  16743. }
  16744. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  16745. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  16746. ret = -9005;
  16747. goto openssl_pkey1_test_done;
  16748. }
  16749. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  16750. ret = -9006;
  16751. goto openssl_pkey1_test_done;
  16752. }
  16753. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  16754. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  16755. if (dec == NULL || enc == NULL) {
  16756. ret = -9007;
  16757. goto openssl_pkey1_test_done;
  16758. }
  16759. if (EVP_PKEY_decrypt_init(dec) != 1) {
  16760. ret = -9008;
  16761. goto openssl_pkey1_test_done;
  16762. }
  16763. if (EVP_PKEY_encrypt_init(enc) != 1) {
  16764. ret = -9009;
  16765. goto openssl_pkey1_test_done;
  16766. }
  16767. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  16768. ret = -9010;
  16769. goto openssl_pkey1_test_done;
  16770. }
  16771. #ifndef HAVE_FIPS
  16772. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  16773. ret = -9011;
  16774. goto openssl_pkey1_test_done;
  16775. }
  16776. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  16777. ret = -9012;
  16778. goto openssl_pkey1_test_done;
  16779. }
  16780. #endif
  16781. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  16782. outlen = keyLenBits/8;
  16783. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  16784. ret = -9013;
  16785. goto openssl_pkey1_test_done;
  16786. }
  16787. XMEMSET(plain, 0, RSA_TEST_BYTES);
  16788. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  16789. ret = -9014;
  16790. goto openssl_pkey1_test_done;
  16791. }
  16792. openssl_pkey1_test_done:
  16793. if (pubKey != NULL) {
  16794. EVP_PKEY_free(pubKey);
  16795. }
  16796. if (prvKey != NULL) {
  16797. EVP_PKEY_free(prvKey);
  16798. }
  16799. if (dec != NULL) {
  16800. EVP_PKEY_CTX_free(dec);
  16801. }
  16802. if (enc != NULL) {
  16803. EVP_PKEY_CTX_free(enc);
  16804. }
  16805. if (x509 != NULL) {
  16806. X509_free(x509);
  16807. }
  16808. #ifdef WOLFSSL_SMALL_STACK
  16809. if (tmp != NULL)
  16810. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16811. if (cipher != NULL)
  16812. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16813. if (plain != NULL)
  16814. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16815. #endif
  16816. #endif
  16817. return ret;
  16818. }
  16819. #define ERR_BASE_EVPSIG -5100
  16820. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void)
  16821. {
  16822. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(HAVE_USER_RSA)
  16823. byte* prvTmp;
  16824. byte* pubTmp;
  16825. int prvBytes;
  16826. int pubBytes;
  16827. RSA *prvRsa;
  16828. RSA *pubRsa;
  16829. EVP_PKEY *prvPkey;
  16830. EVP_PKEY *pubPkey;
  16831. EVP_MD_CTX* sign;
  16832. EVP_MD_CTX* verf;
  16833. char msg[] = "see spot run";
  16834. unsigned char sig[256];
  16835. unsigned int sigSz;
  16836. const void* pt;
  16837. unsigned int count;
  16838. int ret, ret1, ret2;
  16839. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  16840. XFILE keyFile;
  16841. XFILE keypubFile;
  16842. char cliKey[] = "./certs/client-key.der";
  16843. char cliKeypub[] = "./certs/client-keyPub.der";
  16844. #endif
  16845. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16846. if (prvTmp == NULL)
  16847. return ERR_BASE_EVPSIG-1;
  16848. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16849. if (pubTmp == NULL) {
  16850. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16851. return ERR_BASE_EVPSIG-2;
  16852. }
  16853. #ifdef USE_CERT_BUFFERS_1024
  16854. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  16855. prvBytes = sizeof_client_key_der_1024;
  16856. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16857. pubBytes = sizeof_client_keypub_der_1024;
  16858. #elif defined(USE_CERT_BUFFERS_2048)
  16859. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  16860. prvBytes = sizeof_client_key_der_2048;
  16861. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16862. pubBytes = sizeof_client_keypub_der_2048;
  16863. #else
  16864. keyFile = XFOPEN(cliKey, "rb");
  16865. if (!keyFile) {
  16866. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16867. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16868. err_sys("can't open ./certs/client-key.der, "
  16869. "Please run from wolfSSL home dir", -40);
  16870. return ERR_BASE_EVPSIG-3;
  16871. }
  16872. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  16873. XFCLOSE(keyFile);
  16874. keypubFile = XFOPEN(cliKeypub, "rb");
  16875. if (!keypubFile) {
  16876. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16877. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16878. err_sys("can't open ./certs/client-cert.der, "
  16879. "Please run from wolfSSL home dir", -41);
  16880. return ERR_BASE_EVPSIG-4;
  16881. }
  16882. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  16883. XFCLOSE(keypubFile);
  16884. #endif /* USE_CERT_BUFFERS */
  16885. prvRsa = wolfSSL_RSA_new();
  16886. pubRsa = wolfSSL_RSA_new();
  16887. if((prvRsa == NULL) || (pubRsa == NULL)){
  16888. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16889. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16890. err_sys("ERROR with RSA_new", -9100);
  16891. return ERR_BASE_EVPSIG-5;
  16892. }
  16893. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  16894. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  16895. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  16896. printf("error with RSA_LoadDer_ex\n");
  16897. return ERR_BASE_EVPSIG-6;
  16898. }
  16899. prvPkey = wolfSSL_EVP_PKEY_new();
  16900. pubPkey = wolfSSL_EVP_PKEY_new();
  16901. if((prvPkey == NULL) || (pubPkey == NULL)){
  16902. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16903. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16904. printf("error with KEY_new\n");
  16905. return ERR_BASE_EVPSIG-7;
  16906. }
  16907. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  16908. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  16909. if((ret1 != 1) || (ret2 != 1)){
  16910. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16911. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16912. printf("error with EVP_PKEY_set1_RSA\n");
  16913. return ERR_BASE_EVPSIG-8;
  16914. }
  16915. /****************** sign and verify *******************/
  16916. sign = EVP_MD_CTX_create();
  16917. verf = EVP_MD_CTX_create();
  16918. if((sign == NULL)||(verf == NULL)){
  16919. printf("error with EVP_MD_CTX_create\n");
  16920. EVP_MD_CTX_destroy(sign);
  16921. EVP_MD_CTX_destroy(verf);
  16922. return ERR_BASE_EVPSIG-10;
  16923. }
  16924. ret = EVP_SignInit(sign, EVP_sha1());
  16925. if (ret != SSL_SUCCESS){
  16926. printf("error with EVP_SignInit\n");
  16927. EVP_MD_CTX_destroy(sign);
  16928. EVP_MD_CTX_destroy(verf);
  16929. return ERR_BASE_EVPSIG-11;
  16930. }
  16931. count = sizeof(msg);
  16932. show("message = ", (char *)msg, count);
  16933. /* sign */
  16934. XMEMSET(sig, 0, sizeof(sig));
  16935. pt = (const void*)msg;
  16936. ret1 = EVP_SignUpdate(sign, pt, count);
  16937. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  16938. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  16939. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16940. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16941. EVP_MD_CTX_destroy(sign);
  16942. EVP_MD_CTX_destroy(verf);
  16943. printf("error with EVP_MD_CTX_create\n");
  16944. return ERR_BASE_EVPSIG-12;
  16945. }
  16946. show("signature = ", (char *)sig, sigSz);
  16947. /* verify */
  16948. pt = (const void*)msg;
  16949. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  16950. ret2 = EVP_VerifyUpdate(verf, pt, count);
  16951. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  16952. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16953. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16954. EVP_MD_CTX_destroy(sign);
  16955. EVP_MD_CTX_destroy(verf);
  16956. printf("error with EVP_Verify\n");
  16957. return ERR_BASE_EVPSIG-13;
  16958. }
  16959. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  16960. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16961. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16962. EVP_MD_CTX_destroy(sign);
  16963. EVP_MD_CTX_destroy(verf);
  16964. printf("error with EVP_VerifyFinal\n");
  16965. return ERR_BASE_EVPSIG-14;
  16966. }
  16967. /* expect fail without update */
  16968. EVP_VerifyInit(verf, EVP_sha1());
  16969. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  16970. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16971. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16972. EVP_MD_CTX_destroy(sign);
  16973. EVP_MD_CTX_destroy(verf);
  16974. printf("EVP_VerifyInit without update not detected\n");
  16975. return ERR_BASE_EVPSIG-15;
  16976. }
  16977. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16978. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16979. EVP_MD_CTX_destroy(sign);
  16980. EVP_MD_CTX_destroy(verf);
  16981. wolfSSL_RSA_free(prvRsa);
  16982. wolfSSL_RSA_free(pubRsa);
  16983. EVP_PKEY_free(pubPkey);
  16984. EVP_PKEY_free(prvPkey);
  16985. #endif /* NO_RSA */
  16986. return 0;
  16987. }
  16988. #endif /* OPENSSL_EXTRA */
  16989. #ifndef NO_PWDBASED
  16990. #ifdef HAVE_SCRYPT
  16991. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  16992. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void)
  16993. {
  16994. #ifdef HAVE_FIPS
  16995. /* RFC 7914 test vector keys are too short for FIPS. */
  16996. #else
  16997. int ret;
  16998. byte derived[64];
  16999. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  17000. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  17001. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  17002. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  17003. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  17004. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  17005. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  17006. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  17007. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  17008. };
  17009. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17010. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  17011. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  17012. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  17013. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  17014. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  17015. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  17016. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  17017. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  17018. };
  17019. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  17020. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  17021. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  17022. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  17023. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  17024. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  17025. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  17026. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  17027. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  17028. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  17029. };
  17030. #endif
  17031. #ifdef SCRYPT_TEST_ALL
  17032. /* Test case is very slow.
  17033. * Use for confirmation after code change or new platform.
  17034. */
  17035. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  17036. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  17037. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  17038. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  17039. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  17040. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  17041. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  17042. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  17043. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  17044. };
  17045. #endif
  17046. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  17047. if (ret != 0)
  17048. return -9200;
  17049. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  17050. return -9201;
  17051. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  17052. sizeof(verify2));
  17053. if (ret != 0)
  17054. return -9202;
  17055. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  17056. return -9203;
  17057. /* Don't run these test on embedded, since they use large mallocs */
  17058. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  17059. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  17060. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  17061. if (ret != 0)
  17062. return -9204;
  17063. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  17064. return -9205;
  17065. #ifdef SCRYPT_TEST_ALL
  17066. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  17067. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  17068. if (ret != 0)
  17069. return -9206;
  17070. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  17071. return -9207;
  17072. #endif
  17073. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  17074. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  17075. 8, 16, sizeof(verify2));
  17076. if (ret != 0)
  17077. return -9208;
  17078. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  17079. return -9209;
  17080. #endif /* !HAVE_FIPS */
  17081. return 0;
  17082. }
  17083. #endif
  17084. #ifdef HAVE_PKCS12
  17085. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void)
  17086. {
  17087. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  17088. 0x00, 0x00 };
  17089. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  17090. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  17091. 0x00, 0x67, 0x00, 0x00 };
  17092. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  17093. byte derived[64];
  17094. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17095. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  17096. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  17097. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  17098. };
  17099. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17100. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  17101. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  17102. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  17103. };
  17104. int id = 1;
  17105. int kLen = 24;
  17106. int iterations = 1;
  17107. int ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  17108. iterations, kLen, WC_SHA256, id);
  17109. if (ret < 0)
  17110. return -9300;
  17111. if (XMEMCMP(derived, verify, kLen) != 0)
  17112. return -9301;
  17113. iterations = 1000;
  17114. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  17115. iterations, kLen, WC_SHA256, id);
  17116. if (ret < 0)
  17117. return -9302;
  17118. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  17119. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  17120. if (ret < 0)
  17121. return -9303;
  17122. if (XMEMCMP(derived, verify2, 24) != 0)
  17123. return -9304;
  17124. return 0;
  17125. }
  17126. #endif /* HAVE_PKCS12 */
  17127. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  17128. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void)
  17129. {
  17130. char passwd[] = "passwordpassword";
  17131. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  17132. int iterations = 2048;
  17133. int kLen = 24;
  17134. byte derived[64];
  17135. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17136. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  17137. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  17138. };
  17139. int ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt,
  17140. (int)sizeof(salt), iterations, kLen, WC_SHA256, HEAP_HINT, devId);
  17141. if (ret != 0)
  17142. return ret;
  17143. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  17144. return -9400;
  17145. return 0;
  17146. }
  17147. #endif /* HAVE_PBKDF2 && !NO_SHA256 */
  17148. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  17149. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void)
  17150. {
  17151. char passwd[] = "password";
  17152. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  17153. int iterations = 1000;
  17154. int kLen = 16;
  17155. byte derived[16];
  17156. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17157. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  17158. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  17159. };
  17160. int ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  17161. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  17162. HEAP_HINT);
  17163. if (ret != 0)
  17164. return ret;
  17165. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  17166. return -9500;
  17167. return 0;
  17168. }
  17169. #endif /* HAVE_PBKDF2 && !NO_SHA */
  17170. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void)
  17171. {
  17172. int ret = 0;
  17173. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  17174. ret = pbkdf1_test();
  17175. if (ret != 0)
  17176. return ret;
  17177. #endif
  17178. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  17179. ret = pbkdf2_test();
  17180. if (ret != 0)
  17181. return ret;
  17182. #endif
  17183. #ifdef HAVE_PKCS12
  17184. ret = pkcs12_test();
  17185. if (ret != 0)
  17186. return ret;
  17187. #endif
  17188. #ifdef HAVE_SCRYPT
  17189. ret = scrypt_test();
  17190. if (ret != 0)
  17191. return ret;
  17192. #endif
  17193. return ret;
  17194. }
  17195. #endif /* NO_PWDBASED */
  17196. #if defined(HAVE_HKDF) && (!defined(NO_SHA) || !defined(NO_SHA256))
  17197. /* WOLFSSL_TEST_SUBROUTINE */ static int hkdf_test(void)
  17198. {
  17199. int ret;
  17200. int L = 42;
  17201. byte okm1[42];
  17202. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  17203. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  17204. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  17205. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17206. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  17207. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  17208. 0xf8, 0xf9 };
  17209. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  17210. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  17211. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  17212. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  17213. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  17214. 0x49, 0x18 };
  17215. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  17216. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  17217. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  17218. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  17219. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  17220. 0xf8, 0x96 };
  17221. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  17222. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  17223. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  17224. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  17225. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  17226. 0x96, 0xc8 };
  17227. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  17228. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  17229. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  17230. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  17231. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  17232. 0x58, 0x65 };
  17233. (void)res1;
  17234. (void)res2;
  17235. (void)res3;
  17236. (void)res4;
  17237. (void)salt1;
  17238. (void)info1;
  17239. #ifndef NO_SHA
  17240. ret = wc_HKDF(WC_SHA, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
  17241. if (ret != 0)
  17242. return -9700;
  17243. if (XMEMCMP(okm1, res1, L) != 0)
  17244. return -9701;
  17245. #ifndef HAVE_FIPS
  17246. /* fips can't have key size under 14 bytes, salt is key too */
  17247. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, 13, info1, 10, okm1, L);
  17248. if (ret != 0)
  17249. return -9702;
  17250. if (XMEMCMP(okm1, res2, L) != 0)
  17251. return -9703;
  17252. #endif /* HAVE_FIPS */
  17253. #endif /* NO_SHA */
  17254. #ifndef NO_SHA256
  17255. ret = wc_HKDF(WC_SHA256, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
  17256. if (ret != 0)
  17257. return -9704;
  17258. if (XMEMCMP(okm1, res3, L) != 0)
  17259. return -9705;
  17260. #ifndef HAVE_FIPS
  17261. /* fips can't have key size under 14 bytes, salt is key too */
  17262. ret = wc_HKDF(WC_SHA256, ikm1, 22, salt1, 13, info1, 10, okm1, L);
  17263. if (ret != 0)
  17264. return -9706;
  17265. if (XMEMCMP(okm1, res4, L) != 0)
  17266. return -9707;
  17267. #endif /* HAVE_FIPS */
  17268. #endif /* NO_SHA256 */
  17269. return 0;
  17270. }
  17271. #endif /* HAVE_HKDF */
  17272. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  17273. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void)
  17274. {
  17275. int ret;
  17276. byte kek[128];
  17277. #ifndef NO_SHA
  17278. /* SHA-1, COUNT = 0
  17279. * shared secret length: 192
  17280. * SharedInfo length: 0
  17281. * key data length: 128
  17282. */
  17283. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  17284. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  17285. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  17286. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  17287. };
  17288. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17289. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  17290. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  17291. };
  17292. #endif
  17293. #ifndef NO_SHA256
  17294. /* SHA-256, COUNT = 3
  17295. * shared secret length: 192
  17296. * SharedInfo length: 0
  17297. * key data length: 128
  17298. */
  17299. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  17300. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  17301. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  17302. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  17303. };
  17304. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17305. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  17306. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  17307. };
  17308. #endif
  17309. #ifdef WOLFSSL_SHA512
  17310. /* SHA-512, COUNT = 0
  17311. * shared secret length: 192
  17312. * SharedInfo length: 0
  17313. * key data length: 128
  17314. */
  17315. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  17316. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  17317. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  17318. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  17319. };
  17320. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  17321. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  17322. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  17323. };
  17324. /* SHA-512, COUNT = 0
  17325. * shared secret length: 521
  17326. * SharedInfo length: 128
  17327. * key data length: 1024
  17328. */
  17329. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  17330. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  17331. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  17332. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  17333. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  17334. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  17335. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  17336. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  17337. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  17338. 0x5a, 0x2d
  17339. };
  17340. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  17341. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  17342. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  17343. };
  17344. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  17345. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  17346. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  17347. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  17348. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  17349. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  17350. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  17351. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  17352. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  17353. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  17354. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  17355. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  17356. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  17357. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  17358. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  17359. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  17360. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  17361. };
  17362. #endif
  17363. #ifndef NO_SHA
  17364. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  17365. kek, sizeof(verify));
  17366. if (ret != 0)
  17367. return -9800;
  17368. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  17369. return -9801;
  17370. #endif
  17371. #ifndef NO_SHA256
  17372. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  17373. kek, sizeof(verify2));
  17374. if (ret != 0)
  17375. return -9802;
  17376. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  17377. return -9803;
  17378. #endif
  17379. #ifdef WOLFSSL_SHA512
  17380. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  17381. kek, sizeof(verify3));
  17382. if (ret != 0)
  17383. return -9804;
  17384. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  17385. return -9805;
  17386. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  17387. sizeof(info4), kek, sizeof(verify4));
  17388. if (ret != 0)
  17389. return -9806;
  17390. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  17391. return -9807;
  17392. #endif
  17393. return 0;
  17394. }
  17395. #endif /* HAVE_X963_KDF */
  17396. #ifdef HAVE_ECC
  17397. /* size to use for ECC key gen tests */
  17398. #ifndef ECC_KEYGEN_SIZE
  17399. #ifndef NO_ECC256
  17400. #define ECC_KEYGEN_SIZE 32
  17401. #elif defined(HAVE_ECC384)
  17402. #define ECC_KEYGEN_SIZE 48
  17403. #elif defined(HAVE_ECC224)
  17404. #define ECC_KEYGEN_SIZE 28
  17405. #elif defined(HAVE_ECC521)
  17406. #define ECC_KEYGEN_SIZE 66
  17407. #else
  17408. #error No ECC keygen size defined for test
  17409. #endif
  17410. #endif
  17411. #ifdef BENCH_EMBEDDED
  17412. #define ECC_SHARED_SIZE 128
  17413. #else
  17414. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  17415. #endif
  17416. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  17417. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  17418. #ifndef NO_ECC_VECTOR_TEST
  17419. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  17420. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  17421. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  17422. #define HAVE_ECC_VECTOR_TEST
  17423. #endif
  17424. #endif
  17425. #ifdef HAVE_ECC_VECTOR_TEST
  17426. typedef struct eccVector {
  17427. const char* msg; /* SHA-1 Encoded Message */
  17428. const char* Qx;
  17429. const char* Qy;
  17430. const char* d; /* Private Key */
  17431. const char* R;
  17432. const char* S;
  17433. const char* curveName;
  17434. word32 msgLen;
  17435. word32 keySize;
  17436. #ifndef NO_ASN
  17437. const byte* r;
  17438. word32 rSz;
  17439. const byte* s;
  17440. word32 sSz;
  17441. #endif
  17442. } eccVector;
  17443. static int ecc_test_vector_item(const eccVector* vector)
  17444. {
  17445. int ret = 0, verify = 0;
  17446. word32 sigSz;
  17447. #ifdef WOLFSSL_SMALL_STACK
  17448. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17449. #else
  17450. ecc_key userA[1];
  17451. #endif
  17452. DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  17453. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  17454. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  17455. DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  17456. DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  17457. DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  17458. #endif
  17459. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  17460. if (sig == NULL)
  17461. ERROR_OUT(MEMORY_E, done);
  17462. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  17463. if (sigRaw == NULL || r == NULL || s == NULL)
  17464. ERROR_OUT(MEMORY_E, done);
  17465. #endif
  17466. #endif
  17467. #ifdef WOLFSSL_SMALL_STACK
  17468. if (userA == NULL)
  17469. ERROR_OUT(MEMORY_E, done);
  17470. #endif
  17471. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  17472. if (ret != 0)
  17473. goto done;
  17474. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  17475. vector->d, vector->curveName);
  17476. if (ret != 0)
  17477. goto done;
  17478. XMEMSET(sig, 0, ECC_SIG_SIZE);
  17479. sigSz = ECC_SIG_SIZE;
  17480. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  17481. if (ret != 0)
  17482. goto done;
  17483. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  17484. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  17485. sigRawSz = ECC_SIG_SIZE;
  17486. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  17487. sigRaw, &sigRawSz);
  17488. if (ret != 0)
  17489. goto done;
  17490. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  17491. ret = -9810;
  17492. goto done;
  17493. }
  17494. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  17495. if (ret != 0)
  17496. goto done;
  17497. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  17498. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  17499. ret = -9811;
  17500. goto done;
  17501. }
  17502. #endif
  17503. do {
  17504. #if defined(WOLFSSL_ASYNC_CRYPT)
  17505. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17506. #endif
  17507. if (ret == 0)
  17508. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  17509. vector->msgLen, &verify, userA);
  17510. } while (ret == WC_PENDING_E);
  17511. if (ret != 0)
  17512. goto done;
  17513. TEST_SLEEP();
  17514. if (verify != 1)
  17515. ret = -9812;
  17516. done:
  17517. #ifdef WOLFSSL_SMALL_STACK
  17518. if (userA != NULL) {
  17519. wc_ecc_free(userA);
  17520. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17521. }
  17522. #else
  17523. wc_ecc_free(userA);
  17524. #endif
  17525. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  17526. FREE_VAR(sigRaw, HEAP_HINT);
  17527. FREE_VAR(r, HEAP_HINT);
  17528. FREE_VAR(s, HEAP_HINT);
  17529. #endif
  17530. FREE_VAR(sig, HEAP_HINT);
  17531. return ret;
  17532. }
  17533. static int ecc_test_vector(int keySize)
  17534. {
  17535. int ret;
  17536. eccVector vec;
  17537. XMEMSET(&vec, 0, sizeof(vec));
  17538. vec.keySize = (word32)keySize;
  17539. switch(keySize) {
  17540. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  17541. case 14:
  17542. return 0;
  17543. #endif /* HAVE_ECC112 */
  17544. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  17545. case 16:
  17546. return 0;
  17547. #endif /* HAVE_ECC128 */
  17548. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  17549. case 20:
  17550. return 0;
  17551. #endif /* HAVE_ECC160 */
  17552. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  17553. case 24:
  17554. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  17555. #if 1
  17556. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  17557. vec.msgLen = 20;
  17558. #else
  17559. /* This is the raw message prior to SHA-1 */
  17560. vec.msg =
  17561. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  17562. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  17563. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  17564. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  17565. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  17566. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  17567. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  17568. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  17569. vec.msgLen = 128;
  17570. #endif
  17571. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  17572. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  17573. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  17574. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  17575. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  17576. vec.curveName = "SECP192R1";
  17577. #ifndef NO_ASN
  17578. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  17579. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  17580. vec.rSz = 24;
  17581. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  17582. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  17583. vec.sSz = 24;
  17584. #endif
  17585. break;
  17586. #endif /* HAVE_ECC192 */
  17587. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  17588. case 28:
  17589. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  17590. #if 1
  17591. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  17592. vec.msgLen = 20;
  17593. #else
  17594. /* This is the raw message prior to SHA-1 */
  17595. vec.msg =
  17596. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  17597. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  17598. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  17599. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  17600. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  17601. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  17602. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  17603. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  17604. vec.msgLen = 128;
  17605. #endif
  17606. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  17607. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  17608. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  17609. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  17610. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  17611. vec.curveName = "SECP224R1";
  17612. #ifndef NO_ASN
  17613. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  17614. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  17615. "\xbc\x16\x71\xa7";
  17616. vec.rSz = 28;
  17617. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  17618. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  17619. "\x6a\xf3\xad\x5b";
  17620. vec.sSz = 28;
  17621. #endif
  17622. break;
  17623. #endif /* HAVE_ECC224 */
  17624. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  17625. case 30:
  17626. return 0;
  17627. #endif /* HAVE_ECC239 */
  17628. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  17629. case 32:
  17630. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  17631. #if 1
  17632. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  17633. vec.msgLen = 20;
  17634. #else
  17635. /* This is the raw message prior to SHA-1 */
  17636. vec.msg =
  17637. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  17638. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  17639. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  17640. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  17641. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  17642. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  17643. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  17644. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  17645. vec.msgLen = 128;
  17646. #endif
  17647. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  17648. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  17649. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  17650. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  17651. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  17652. #ifndef NO_ASN
  17653. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  17654. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  17655. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  17656. vec.rSz = 32;
  17657. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  17658. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  17659. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  17660. vec.sSz = 32;
  17661. #endif
  17662. vec.curveName = "SECP256R1";
  17663. break;
  17664. #endif /* !NO_ECC256 */
  17665. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  17666. case 40:
  17667. return 0;
  17668. #endif /* HAVE_ECC320 */
  17669. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  17670. case 48:
  17671. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  17672. #if 1
  17673. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  17674. vec.msgLen = 20;
  17675. #else
  17676. /* This is the raw message prior to SHA-1 */
  17677. vec.msg =
  17678. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  17679. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  17680. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  17681. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  17682. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  17683. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  17684. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  17685. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  17686. vec.msgLen = 128;
  17687. #endif
  17688. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  17689. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  17690. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  17691. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  17692. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  17693. vec.curveName = "SECP384R1";
  17694. #ifndef NO_ASN
  17695. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  17696. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  17697. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  17698. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  17699. vec.rSz = 48;
  17700. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  17701. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  17702. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  17703. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  17704. vec.sSz = 48;
  17705. #endif
  17706. break;
  17707. #endif /* HAVE_ECC384 */
  17708. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  17709. case 64:
  17710. return 0;
  17711. #endif /* HAVE_ECC512 */
  17712. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  17713. case 66:
  17714. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  17715. #if 1
  17716. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  17717. vec.msgLen = 20;
  17718. #else
  17719. /* This is the raw message prior to SHA-1 */
  17720. vec.msg =
  17721. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  17722. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  17723. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  17724. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  17725. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  17726. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  17727. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  17728. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  17729. vec.msgLen = 128;
  17730. #endif
  17731. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  17732. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  17733. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  17734. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  17735. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  17736. vec.curveName = "SECP521R1";
  17737. #ifndef NO_ASN
  17738. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  17739. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  17740. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  17741. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  17742. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  17743. "\xa3\xbb\x15\x21\xbe";
  17744. vec.rSz = 65;
  17745. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  17746. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  17747. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  17748. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  17749. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  17750. "\x3d\x22\xf2\x48\x0c";
  17751. vec.sSz = 65;
  17752. #endif
  17753. break;
  17754. #endif /* HAVE_ECC521 */
  17755. default:
  17756. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  17757. }; /* Switch */
  17758. ret = ecc_test_vector_item(&vec);
  17759. if (ret < 0) {
  17760. return ret;
  17761. }
  17762. return 0;
  17763. }
  17764. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K)
  17765. static int ecc_test_sign_vectors(WC_RNG* rng)
  17766. {
  17767. int ret;
  17768. ecc_key key;
  17769. byte sig[72];
  17770. word32 sigSz;
  17771. unsigned char hash[32] = "test wolfSSL deterministic sign";
  17772. const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  17773. const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  17774. const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  17775. const byte k[1] = { 0x02 };
  17776. const byte expSig[71] = {
  17777. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  17778. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  17779. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  17780. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  17781. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  17782. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  17783. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  17784. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  17785. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  17786. };
  17787. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  17788. if (ret != 0) {
  17789. return ret;
  17790. }
  17791. ret = wc_ecc_import_raw(&key, QIUTx, QIUTy, dIUT, "SECP256R1");
  17792. if (ret != 0) {
  17793. goto done;
  17794. }
  17795. ret = wc_ecc_sign_set_k(k, sizeof(k), &key);
  17796. if (ret != 0) {
  17797. goto done;
  17798. }
  17799. sigSz = sizeof(sig);
  17800. do {
  17801. #if defined(WOLFSSL_ASYNC_CRYPT)
  17802. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17803. #endif
  17804. if (ret == 0)
  17805. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, &key);
  17806. } while (ret == WC_PENDING_E);
  17807. if (ret != 0) {
  17808. goto done;
  17809. }
  17810. TEST_SLEEP();
  17811. if (sigSz != sizeof(expSig)) {
  17812. ret = -9830;
  17813. goto done;
  17814. }
  17815. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  17816. ret = -9831;
  17817. goto done;
  17818. }
  17819. sigSz = sizeof(sig);
  17820. do {
  17821. #if defined(WOLFSSL_ASYNC_CRYPT)
  17822. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17823. #endif
  17824. if (ret == 0)
  17825. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, &key);
  17826. } while (ret == WC_PENDING_E);
  17827. if (ret != 0) {
  17828. goto done;
  17829. }
  17830. TEST_SLEEP();
  17831. done:
  17832. wc_ecc_free(&key);
  17833. return ret;
  17834. }
  17835. #endif
  17836. #ifdef HAVE_ECC_CDH
  17837. static int ecc_test_cdh_vectors(WC_RNG* rng)
  17838. {
  17839. int ret;
  17840. #ifdef WOLFSSL_SMALL_STACK
  17841. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17842. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17843. #else
  17844. ecc_key pub_key[1], priv_key[1];
  17845. #endif
  17846. byte sharedA[32] = {0}, sharedB[32] = {0};
  17847. word32 x, z;
  17848. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  17849. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  17850. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  17851. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  17852. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  17853. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  17854. #ifdef WOLFSSL_SMALL_STACK
  17855. if ((pub_key == NULL) ||
  17856. (priv_key == NULL)) {
  17857. ret = MEMORY_E;
  17858. goto done;
  17859. }
  17860. #endif
  17861. XMEMSET(pub_key, 0, sizeof *pub_key);
  17862. XMEMSET(priv_key, 0, sizeof *priv_key);
  17863. /* setup private and public keys */
  17864. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  17865. if (ret != 0)
  17866. goto done;
  17867. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  17868. if (ret != 0)
  17869. goto done;
  17870. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  17871. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  17872. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  17873. if (ret != 0)
  17874. goto done;
  17875. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  17876. if (ret != 0)
  17877. goto done;
  17878. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  17879. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  17880. !defined(HAVE_SELFTEST)
  17881. ret = wc_ecc_set_rng(priv_key, rng);
  17882. if (ret != 0)
  17883. goto done;
  17884. #else
  17885. (void)rng;
  17886. #endif
  17887. /* compute ECC Cofactor shared secret */
  17888. x = sizeof(sharedA);
  17889. do {
  17890. #if defined(WOLFSSL_ASYNC_CRYPT)
  17891. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17892. #endif
  17893. if (ret == 0)
  17894. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  17895. } while (ret == WC_PENDING_E);
  17896. if (ret != 0) {
  17897. goto done;
  17898. }
  17899. TEST_SLEEP();
  17900. /* read in expected Z */
  17901. z = sizeof(sharedB);
  17902. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  17903. if (ret != 0)
  17904. goto done;
  17905. /* compare results */
  17906. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  17907. ERROR_OUT(-9840, done);
  17908. }
  17909. done:
  17910. #ifdef WOLFSSL_SMALL_STACK
  17911. if (priv_key) {
  17912. wc_ecc_free(priv_key);
  17913. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17914. }
  17915. if (pub_key) {
  17916. wc_ecc_free(pub_key);
  17917. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17918. }
  17919. #else
  17920. wc_ecc_free(priv_key);
  17921. wc_ecc_free(pub_key);
  17922. #endif
  17923. return ret;
  17924. }
  17925. #endif /* HAVE_ECC_CDH */
  17926. #endif /* HAVE_ECC_VECTOR_TEST */
  17927. #ifdef HAVE_ECC_KEY_IMPORT
  17928. /* returns 0 on success */
  17929. static int ecc_test_make_pub(WC_RNG* rng)
  17930. {
  17931. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17932. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17933. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  17934. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17935. #endif
  17936. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17937. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17938. #else
  17939. ecc_key key[1];
  17940. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  17941. ecc_key pub[1];
  17942. #endif
  17943. byte exportBuf[ECC_BUFSIZE];
  17944. byte tmp[ECC_BUFSIZE];
  17945. #endif
  17946. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  17947. word32 x, tmpSz;
  17948. int ret = 0;
  17949. ecc_point* pubPoint = NULL;
  17950. #ifdef HAVE_ECC_VERIFY
  17951. int verify = 0;
  17952. #endif
  17953. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17954. if ((key == NULL) ||
  17955. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  17956. (pub == NULL) ||
  17957. #endif
  17958. (exportBuf == NULL) ||
  17959. (tmp == NULL))
  17960. ERROR_OUT(MEMORY_E, done);
  17961. #endif
  17962. (void)msg;
  17963. (void)verify;
  17964. (void)exportBuf;
  17965. (void)rng;
  17966. wc_ecc_init_ex(key, HEAP_HINT, devId);
  17967. #ifndef NO_ECC256
  17968. #ifdef USE_CERT_BUFFERS_256
  17969. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  17970. tmpSz = (size_t)sizeof_ecc_key_der_256;
  17971. #else
  17972. {
  17973. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  17974. if (!file) {
  17975. ERROR_OUT(-9850, done);
  17976. }
  17977. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  17978. XFCLOSE(file);
  17979. }
  17980. #endif /* USE_CERT_BUFFERS_256 */
  17981. /* import private only then test with */
  17982. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  17983. if (ret == 0) {
  17984. ERROR_OUT(-9851, done);
  17985. }
  17986. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  17987. if (ret == 0) {
  17988. ERROR_OUT(-9852, done);
  17989. }
  17990. x = 0;
  17991. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  17992. if (ret != 0) {
  17993. ERROR_OUT(-9853, done);
  17994. }
  17995. #ifdef HAVE_ECC_KEY_EXPORT
  17996. x = ECC_BUFSIZE;
  17997. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  17998. if (ret != 0) {
  17999. ERROR_OUT(-9854, done);
  18000. }
  18001. /* make private only key */
  18002. wc_ecc_free(key);
  18003. wc_ecc_init_ex(key, HEAP_HINT, devId);
  18004. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  18005. if (ret != 0) {
  18006. ERROR_OUT(-9855, done);
  18007. }
  18008. x = ECC_BUFSIZE;
  18009. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  18010. if (ret == 0) {
  18011. ERROR_OUT(-9856, done);
  18012. }
  18013. #endif /* HAVE_ECC_KEY_EXPORT */
  18014. ret = wc_ecc_make_pub(NULL, NULL);
  18015. if (ret == 0) {
  18016. ERROR_OUT(-9857, done);
  18017. }
  18018. TEST_SLEEP();
  18019. #ifndef WOLFSSL_NO_MALLOC
  18020. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  18021. if (pubPoint == NULL) {
  18022. ERROR_OUT(-9858, done);
  18023. }
  18024. ret = wc_ecc_make_pub(key, pubPoint);
  18025. if (ret != 0) {
  18026. ERROR_OUT(-9859, done);
  18027. }
  18028. TEST_SLEEP();
  18029. #ifdef HAVE_ECC_KEY_EXPORT
  18030. /* export should still fail, is private only key */
  18031. x = ECC_BUFSIZE;
  18032. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  18033. if (ret == 0) {
  18034. ERROR_OUT(-9860, done);
  18035. }
  18036. #endif /* HAVE_ECC_KEY_EXPORT */
  18037. #endif /* !WOLFSSL_NO_MALLOC */
  18038. #endif /* !NO_ECC256 */
  18039. /* create a new key since above test for loading key is not supported */
  18040. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256)
  18041. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  18042. if (ret != 0) {
  18043. ERROR_OUT(-9861, done);
  18044. }
  18045. #endif
  18046. #ifdef HAVE_ECC_SIGN
  18047. tmpSz = ECC_BUFSIZE;
  18048. ret = 0;
  18049. do {
  18050. #if defined(WOLFSSL_ASYNC_CRYPT)
  18051. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18052. #endif
  18053. if (ret == 0)
  18054. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp, &tmpSz, rng, key);
  18055. } while (ret == WC_PENDING_E);
  18056. if (ret != 0) {
  18057. ERROR_OUT(-9862, done);
  18058. }
  18059. TEST_SLEEP();
  18060. #ifdef HAVE_ECC_VERIFY
  18061. /* try verify with private only key */
  18062. ret = 0;
  18063. do {
  18064. #if defined(WOLFSSL_ASYNC_CRYPT)
  18065. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18066. #endif
  18067. if (ret == 0)
  18068. ret = wc_ecc_verify_hash(tmp, tmpSz, msg, (word32)XSTRLEN((const char* )msg), &verify, key);
  18069. } while (ret == WC_PENDING_E);
  18070. if (ret != 0) {
  18071. ERROR_OUT(-9863, done);
  18072. }
  18073. if (verify != 1) {
  18074. ERROR_OUT(-9864, done);
  18075. }
  18076. TEST_SLEEP();
  18077. #ifdef HAVE_ECC_KEY_EXPORT
  18078. /* exporting the public part should now work */
  18079. x = ECC_BUFSIZE;
  18080. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  18081. if (ret != 0) {
  18082. ERROR_OUT(-9865, done);
  18083. }
  18084. #endif /* HAVE_ECC_KEY_EXPORT */
  18085. #endif /* HAVE_ECC_VERIFY */
  18086. #endif /* HAVE_ECC_SIGN */
  18087. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  18088. /* now test private only key with creating a shared secret */
  18089. x = ECC_BUFSIZE;
  18090. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  18091. if (ret != 0) {
  18092. ERROR_OUT(-9866, done);
  18093. }
  18094. /* make private only key */
  18095. wc_ecc_free(key);
  18096. wc_ecc_init_ex(key, HEAP_HINT, devId);
  18097. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  18098. if (ret != 0) {
  18099. ERROR_OUT(-9867, done);
  18100. }
  18101. /* check that public export fails with private only key */
  18102. x = ECC_BUFSIZE;
  18103. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  18104. if (ret == 0) {
  18105. ERROR_OUT(-9868, done);
  18106. }
  18107. /* make public key for shared secret */
  18108. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  18109. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  18110. #if defined(WOLFSSL_ASYNC_CRYPT)
  18111. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  18112. #endif
  18113. if (ret != 0) {
  18114. ERROR_OUT(-9869, done);
  18115. }
  18116. TEST_SLEEP();
  18117. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  18118. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  18119. !defined(HAVE_SELFTEST)
  18120. ret = wc_ecc_set_rng(key, rng);
  18121. if (ret != 0)
  18122. goto done;
  18123. #endif
  18124. x = ECC_BUFSIZE;
  18125. do {
  18126. #if defined(WOLFSSL_ASYNC_CRYPT)
  18127. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18128. #endif
  18129. if (ret == 0) {
  18130. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  18131. }
  18132. } while (ret == WC_PENDING_E);
  18133. wc_ecc_free(pub);
  18134. if (ret != 0) {
  18135. ERROR_OUT(-9870, done);
  18136. }
  18137. TEST_SLEEP();
  18138. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT */
  18139. ret = 0;
  18140. done:
  18141. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  18142. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18143. if (key != NULL) {
  18144. wc_ecc_free(key);
  18145. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18146. }
  18147. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  18148. if (pub != NULL)
  18149. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18150. #endif
  18151. if (exportBuf != NULL)
  18152. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18153. if (tmp != NULL)
  18154. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18155. #else
  18156. wc_ecc_free(key);
  18157. #endif
  18158. return ret;
  18159. }
  18160. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT)
  18161. static int ecc_test_key_decode(WC_RNG* rng, int keySize)
  18162. {
  18163. int ret;
  18164. #ifdef WOLFSSL_SMALL_STACK
  18165. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18166. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18167. #else
  18168. ecc_key eccKey[1];
  18169. byte tmpBuf[ECC_BUFSIZE];
  18170. #endif
  18171. word32 tmpSz;
  18172. word32 idx;
  18173. #ifdef WOLFSSL_SMALL_STACK
  18174. if ((eccKey == NULL) || (tmpBuf == NULL))
  18175. ERROR_OUT(MEMORY_E, done);
  18176. #endif
  18177. ret = wc_ecc_init(eccKey);
  18178. if (ret != 0) {
  18179. goto done;
  18180. }
  18181. ret = wc_ecc_make_key(rng, keySize, eccKey);
  18182. #if defined(WOLFSSL_ASYNC_CRYPT)
  18183. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  18184. #endif
  18185. if (ret != 0) {
  18186. goto done;
  18187. }
  18188. tmpSz = ECC_BUFSIZE;
  18189. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  18190. wc_ecc_free(eccKey);
  18191. if (ret < 0) {
  18192. goto done;
  18193. }
  18194. tmpSz = ret;
  18195. ret = wc_ecc_init(eccKey);
  18196. if (ret != 0) {
  18197. goto done;
  18198. }
  18199. idx = 0;
  18200. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  18201. if (ret != 0) {
  18202. goto done;
  18203. }
  18204. wc_ecc_free(eccKey);
  18205. ret = wc_ecc_init(eccKey);
  18206. if (ret != 0) {
  18207. goto done;
  18208. }
  18209. idx = 0;
  18210. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  18211. if (ret != 0) {
  18212. goto done;
  18213. }
  18214. ret = 0;
  18215. done:
  18216. #ifdef WOLFSSL_SMALL_STACK
  18217. if (eccKey != NULL) {
  18218. wc_ecc_free(eccKey);
  18219. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18220. }
  18221. if (tmpBuf != NULL)
  18222. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18223. #else
  18224. wc_ecc_free(eccKey);
  18225. #endif
  18226. return ret;
  18227. }
  18228. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  18229. #endif /* HAVE_ECC_KEY_IMPORT */
  18230. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT)
  18231. static int ecc_test_key_gen(WC_RNG* rng, int keySize)
  18232. {
  18233. int ret = 0;
  18234. int derSz;
  18235. #ifdef HAVE_PKCS8
  18236. word32 pkcs8Sz;
  18237. #endif
  18238. #ifdef WOLFSSL_SMALL_STACK
  18239. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18240. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18241. #else
  18242. byte der[ECC_BUFSIZE];
  18243. ecc_key userA[1];
  18244. #endif
  18245. #ifdef WOLFSSL_SMALL_STACK
  18246. if ((der == NULL) || (userA == NULL))
  18247. ERROR_OUT(MEMORY_E, done);
  18248. #endif
  18249. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  18250. if (ret != 0)
  18251. goto done;
  18252. ret = wc_ecc_make_key(rng, keySize, userA);
  18253. #if defined(WOLFSSL_ASYNC_CRYPT)
  18254. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  18255. #endif
  18256. if (ret != 0)
  18257. goto done;
  18258. TEST_SLEEP();
  18259. ret = wc_ecc_check_key(userA);
  18260. if (ret != 0)
  18261. goto done;
  18262. TEST_SLEEP();
  18263. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  18264. if (derSz < 0) {
  18265. ERROR_OUT(derSz, done);
  18266. }
  18267. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  18268. ECC_PRIVATEKEY_TYPE, -8347);
  18269. if (ret != 0) {
  18270. goto done;
  18271. }
  18272. /* test export of public key */
  18273. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  18274. if (derSz < 0) {
  18275. ERROR_OUT(derSz, done);
  18276. }
  18277. if (derSz == 0) {
  18278. ERROR_OUT(-9890, done);
  18279. }
  18280. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0, -8348);
  18281. if (ret != 0) {
  18282. goto done;
  18283. }
  18284. #ifdef HAVE_PKCS8
  18285. /* test export of PKCS#8 unencrypted private key */
  18286. pkcs8Sz = FOURK_BUF;
  18287. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  18288. if (derSz < 0) {
  18289. ERROR_OUT(derSz, done);
  18290. }
  18291. if (derSz == 0) {
  18292. ERROR_OUT(-9891, done);
  18293. }
  18294. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0, -8349);
  18295. if (ret != 0) {
  18296. goto done;
  18297. }
  18298. #endif /* HAVE_PKCS8 */
  18299. done:
  18300. #ifdef WOLFSSL_SMALL_STACK
  18301. if (der != NULL)
  18302. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18303. if (userA != NULL) {
  18304. wc_ecc_free(userA);
  18305. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18306. }
  18307. #else
  18308. wc_ecc_free(userA);
  18309. #endif
  18310. return ret;
  18311. }
  18312. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  18313. static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  18314. int curve_id, const ecc_set_type* dp)
  18315. {
  18316. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && \
  18317. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  18318. DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  18319. DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  18320. #endif
  18321. #ifdef HAVE_ECC_KEY_EXPORT
  18322. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  18323. DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  18324. #endif
  18325. word32 x = 0;
  18326. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && \
  18327. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  18328. word32 y;
  18329. #endif
  18330. #ifdef HAVE_ECC_SIGN
  18331. DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  18332. DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  18333. int i;
  18334. #ifdef HAVE_ECC_VERIFY
  18335. int verify;
  18336. #endif /* HAVE_ECC_VERIFY */
  18337. #endif /* HAVE_ECC_SIGN */
  18338. int ret;
  18339. #ifdef WOLFSSL_SMALL_STACK
  18340. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  18341. *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  18342. *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18343. #else
  18344. ecc_key userA[1],
  18345. userB[1],
  18346. pubKey[1];
  18347. #endif
  18348. int curveSize;
  18349. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  18350. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && \
  18351. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  18352. if (sharedA == NULL || sharedB == NULL)
  18353. ERROR_OUT(-9900, done);
  18354. #endif
  18355. #ifdef HAVE_ECC_KEY_EXPORT
  18356. if (exportBuf == NULL)
  18357. ERROR_OUT(-9901, done);
  18358. #endif
  18359. #ifdef HAVE_ECC_SIGN
  18360. if (sig == NULL || digest == NULL)
  18361. ERROR_OUT(-9902, done);
  18362. #endif
  18363. #endif /* WOLFSSL_SMALL_STACK */
  18364. (void)testVerifyCount;
  18365. (void)dp;
  18366. (void)x;
  18367. #ifdef WOLFSSL_SMALL_STACK
  18368. if ((userA == NULL) ||
  18369. (userB == NULL) ||
  18370. (pubKey == NULL))
  18371. ERROR_OUT(-9903, done);
  18372. #endif
  18373. XMEMSET(userA, 0, sizeof *userA);
  18374. XMEMSET(userB, 0, sizeof *userB);
  18375. XMEMSET(pubKey, 0, sizeof *pubKey);
  18376. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  18377. if (ret != 0)
  18378. ERROR_OUT(-9904, done);
  18379. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  18380. if (ret != 0)
  18381. ERROR_OUT(-9905, done);
  18382. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  18383. if (ret != 0)
  18384. ERROR_OUT(-9906, done);
  18385. #ifdef WOLFSSL_CUSTOM_CURVES
  18386. if (dp != NULL) {
  18387. ret = wc_ecc_set_custom_curve(userA, dp);
  18388. if (ret != 0)
  18389. ERROR_OUT(-9907, done);
  18390. ret = wc_ecc_set_custom_curve(userB, dp);
  18391. if (ret != 0)
  18392. ERROR_OUT(-9908, done);
  18393. }
  18394. #endif
  18395. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  18396. if (ret != 0)
  18397. ERROR_OUT(-9909, done);
  18398. #if defined(WOLFSSL_ASYNC_CRYPT)
  18399. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  18400. #endif
  18401. if (ret != 0)
  18402. ERROR_OUT(-9910, done);
  18403. TEST_SLEEP();
  18404. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  18405. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  18406. if (curveSize != userA->dp->size)
  18407. ERROR_OUT(-9911, done);
  18408. }
  18409. ret = wc_ecc_check_key(userA);
  18410. if (ret != 0)
  18411. ERROR_OUT(-9912, done);
  18412. TEST_SLEEP();
  18413. /* ATECC508/608 configuration may not support more than one ECDH key */
  18414. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  18415. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  18416. if (ret != 0)
  18417. ERROR_OUT(-9913, done);
  18418. #if defined(WOLFSSL_ASYNC_CRYPT)
  18419. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  18420. #endif
  18421. if (ret != 0)
  18422. ERROR_OUT(-9914, done);
  18423. TEST_SLEEP();
  18424. /* only perform the below tests if the key size matches */
  18425. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  18426. ERROR_OUT(ECC_CURVE_OID_E, done);
  18427. #ifdef HAVE_ECC_DHE
  18428. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  18429. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  18430. !defined(HAVE_SELFTEST)
  18431. ret = wc_ecc_set_rng(userA, rng);
  18432. if (ret != 0)
  18433. ERROR_OUT(-9915, done);
  18434. ret = wc_ecc_set_rng(userB, rng);
  18435. if (ret != 0)
  18436. ERROR_OUT(-9916, done);
  18437. #endif
  18438. x = ECC_SHARED_SIZE;
  18439. do {
  18440. #if defined(WOLFSSL_ASYNC_CRYPT)
  18441. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18442. #endif
  18443. if (ret == 0)
  18444. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  18445. } while (ret == WC_PENDING_E);
  18446. if (ret != 0) {
  18447. ERROR_OUT(-9917, done);
  18448. }
  18449. TEST_SLEEP();
  18450. y = ECC_SHARED_SIZE;
  18451. do {
  18452. #if defined(WOLFSSL_ASYNC_CRYPT)
  18453. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18454. #endif
  18455. if (ret == 0)
  18456. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  18457. } while (ret == WC_PENDING_E);
  18458. if (ret != 0)
  18459. ERROR_OUT(-9918, done);
  18460. if (y != x)
  18461. ERROR_OUT(-9919, done);
  18462. if (XMEMCMP(sharedA, sharedB, x))
  18463. ERROR_OUT(-9920, done);
  18464. TEST_SLEEP();
  18465. #endif /* HAVE_ECC_DHE */
  18466. #ifdef HAVE_ECC_CDH
  18467. /* add cofactor flag */
  18468. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  18469. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  18470. x = ECC_SHARED_SIZE;
  18471. do {
  18472. #if defined(WOLFSSL_ASYNC_CRYPT)
  18473. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18474. #endif
  18475. if (ret == 0)
  18476. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  18477. } while (ret == WC_PENDING_E);
  18478. if (ret != 0)
  18479. ERROR_OUT(-9921, done);
  18480. TEST_SLEEP();
  18481. y = ECC_SHARED_SIZE;
  18482. do {
  18483. #if defined(WOLFSSL_ASYNC_CRYPT)
  18484. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18485. #endif
  18486. if (ret == 0)
  18487. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  18488. } while (ret == WC_PENDING_E);
  18489. if (ret != 0)
  18490. ERROR_OUT(-9922, done);
  18491. if (y != x)
  18492. ERROR_OUT(-9923, done);
  18493. if (XMEMCMP(sharedA, sharedB, x))
  18494. ERROR_OUT(-9924, done);
  18495. TEST_SLEEP();
  18496. /* remove cofactor flag */
  18497. wc_ecc_set_flags(userA, 0);
  18498. wc_ecc_set_flags(userB, 0);
  18499. #endif /* HAVE_ECC_CDH */
  18500. #endif /* WOLFSSL_ATECC508A */
  18501. #ifdef HAVE_ECC_KEY_EXPORT
  18502. x = ECC_KEY_EXPORT_BUF_SIZE;
  18503. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  18504. if (ret != 0)
  18505. ERROR_OUT(-9925, done);
  18506. #ifdef HAVE_ECC_KEY_IMPORT
  18507. #ifdef WOLFSSL_CUSTOM_CURVES
  18508. if (dp != NULL) {
  18509. ret = wc_ecc_set_custom_curve(pubKey, dp);
  18510. if (ret != 0)
  18511. ERROR_OUT(-9926, done);
  18512. }
  18513. #endif
  18514. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  18515. if (ret != 0)
  18516. ERROR_OUT(-9927, done);
  18517. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  18518. #ifdef HAVE_ECC_DHE
  18519. y = ECC_SHARED_SIZE;
  18520. do {
  18521. #if defined(WOLFSSL_ASYNC_CRYPT)
  18522. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18523. #endif
  18524. if (ret == 0)
  18525. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  18526. } while (ret == WC_PENDING_E);
  18527. if (ret != 0)
  18528. ERROR_OUT(-9928, done);
  18529. if (XMEMCMP(sharedA, sharedB, y))
  18530. ERROR_OUT(-9929, done);
  18531. TEST_SLEEP();
  18532. #endif /* HAVE_ECC_DHE */
  18533. #ifdef HAVE_COMP_KEY
  18534. /* try compressed export / import too */
  18535. x = ECC_KEY_EXPORT_BUF_SIZE;
  18536. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  18537. if (ret != 0)
  18538. ERROR_OUT(-9930, done);
  18539. wc_ecc_free(pubKey);
  18540. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  18541. if (ret != 0)
  18542. ERROR_OUT(-9931, done);
  18543. #ifdef WOLFSSL_CUSTOM_CURVES
  18544. if (dp != NULL) {
  18545. ret = wc_ecc_set_custom_curve(pubKey, dp);
  18546. if (ret != 0)
  18547. ERROR_OUT(-9932, done);
  18548. }
  18549. #endif
  18550. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  18551. if (ret != 0)
  18552. ERROR_OUT(-9933, done);
  18553. #ifdef HAVE_ECC_DHE
  18554. y = ECC_SHARED_SIZE;
  18555. do {
  18556. #if defined(WOLFSSL_ASYNC_CRYPT)
  18557. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18558. #endif
  18559. if (ret == 0)
  18560. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  18561. } while (ret == WC_PENDING_E);
  18562. if (ret != 0)
  18563. ERROR_OUT(-9934, done);
  18564. if (XMEMCMP(sharedA, sharedB, y))
  18565. ERROR_OUT(-9935, done);
  18566. TEST_SLEEP();
  18567. #endif /* HAVE_ECC_DHE */
  18568. #endif /* HAVE_COMP_KEY */
  18569. #endif /* WOLFSSL_ATECC508A */
  18570. #endif /* HAVE_ECC_KEY_IMPORT */
  18571. #endif /* HAVE_ECC_KEY_EXPORT */
  18572. #ifdef HAVE_ECC_SIGN
  18573. /* ECC w/out Shamir has issue with all 0 digest */
  18574. /* WC_BIGINT doesn't have 0 len well on hardware */
  18575. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT)
  18576. /* test DSA sign hash with zeros */
  18577. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  18578. digest[i] = 0;
  18579. }
  18580. x = ECC_SIG_SIZE;
  18581. do {
  18582. #if defined(WOLFSSL_ASYNC_CRYPT)
  18583. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18584. #endif
  18585. if (ret == 0)
  18586. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  18587. userA);
  18588. } while (ret == WC_PENDING_E);
  18589. if (ret != 0)
  18590. ERROR_OUT(-9936, done);
  18591. TEST_SLEEP();
  18592. #ifdef HAVE_ECC_VERIFY
  18593. for (i=0; i<testVerifyCount; i++) {
  18594. verify = 0;
  18595. do {
  18596. #if defined(WOLFSSL_ASYNC_CRYPT)
  18597. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18598. #endif
  18599. if (ret == 0)
  18600. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  18601. &verify, userA);
  18602. } while (ret == WC_PENDING_E);
  18603. if (ret != 0)
  18604. ERROR_OUT(-9937, done);
  18605. if (verify != 1)
  18606. ERROR_OUT(-9938, done);
  18607. TEST_SLEEP();
  18608. }
  18609. #endif /* HAVE_ECC_VERIFY */
  18610. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT */
  18611. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  18612. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  18613. digest[i] = (byte)i;
  18614. }
  18615. x = ECC_SIG_SIZE;
  18616. do {
  18617. #if defined(WOLFSSL_ASYNC_CRYPT)
  18618. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18619. #endif
  18620. if (ret == 0)
  18621. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  18622. userA);
  18623. } while (ret == WC_PENDING_E);
  18624. if (ret != 0)
  18625. ERROR_OUT(-9939, done);
  18626. TEST_SLEEP();
  18627. #ifdef HAVE_ECC_VERIFY
  18628. for (i=0; i<testVerifyCount; i++) {
  18629. verify = 0;
  18630. do {
  18631. #if defined(WOLFSSL_ASYNC_CRYPT)
  18632. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18633. #endif
  18634. if (ret == 0)
  18635. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  18636. &verify, userA);
  18637. } while (ret == WC_PENDING_E);
  18638. if (ret != 0)
  18639. ERROR_OUT(-9940, done);
  18640. if (verify != 1)
  18641. ERROR_OUT(-9941, done);
  18642. TEST_SLEEP();
  18643. }
  18644. #endif /* HAVE_ECC_VERIFY */
  18645. #endif /* HAVE_ECC_SIGN */
  18646. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WOLFSSL_ATECC508) && \
  18647. !defined(WOLFSSL_ATECC608A)
  18648. x = ECC_KEY_EXPORT_BUF_SIZE;
  18649. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  18650. if (ret != 0)
  18651. ERROR_OUT(-9942, done);
  18652. #endif /* HAVE_ECC_KEY_EXPORT */
  18653. done:
  18654. #ifdef WOLFSSL_SMALL_STACK
  18655. if (userA != NULL) {
  18656. wc_ecc_free(userA);
  18657. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18658. }
  18659. if (userB != NULL) {
  18660. wc_ecc_free(userB);
  18661. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18662. }
  18663. if (pubKey != NULL) {
  18664. wc_ecc_free(pubKey);
  18665. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18666. }
  18667. #else
  18668. wc_ecc_free(pubKey);
  18669. wc_ecc_free(userB);
  18670. wc_ecc_free(userA);
  18671. #endif
  18672. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  18673. FREE_VAR(sharedA, HEAP_HINT);
  18674. FREE_VAR(sharedB, HEAP_HINT);
  18675. #endif
  18676. #ifdef HAVE_ECC_KEY_EXPORT
  18677. FREE_VAR(exportBuf, HEAP_HINT);
  18678. #endif
  18679. #ifdef HAVE_ECC_SIGN
  18680. FREE_VAR(sig, HEAP_HINT);
  18681. FREE_VAR(digest, HEAP_HINT);
  18682. #endif
  18683. return ret;
  18684. }
  18685. #undef ECC_TEST_VERIFY_COUNT
  18686. #define ECC_TEST_VERIFY_COUNT 2
  18687. static int ecc_test_curve(WC_RNG* rng, int keySize)
  18688. {
  18689. int ret;
  18690. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT,
  18691. ECC_CURVE_DEF, NULL);
  18692. if (ret < 0) {
  18693. if (ret == ECC_CURVE_OID_E) {
  18694. /* ignore error for curves not found */
  18695. /* some curve sizes are only available with:
  18696. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  18697. and HAVE_ECC_KOBLITZ */
  18698. }
  18699. else {
  18700. printf("ecc_test_curve_size %d failed!: %d\n", keySize, ret);
  18701. return ret;
  18702. }
  18703. }
  18704. #ifdef HAVE_ECC_VECTOR_TEST
  18705. ret = ecc_test_vector(keySize);
  18706. if (ret < 0) {
  18707. printf("ecc_test_vector %d failed!: %d\n", keySize, ret);
  18708. return ret;
  18709. }
  18710. #endif
  18711. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  18712. !defined(NO_ASN_CRYPT)
  18713. ret = ecc_test_key_decode(rng, keySize);
  18714. if (ret < 0) {
  18715. if (ret == ECC_CURVE_OID_E) {
  18716. /* ignore error for curves not found */
  18717. }
  18718. else {
  18719. printf("ecc_test_key_decode %d failed!: %d\n", keySize, ret);
  18720. return ret;
  18721. }
  18722. }
  18723. #endif
  18724. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT)
  18725. ret = ecc_test_key_gen(rng, keySize);
  18726. if (ret < 0) {
  18727. if (ret == ECC_CURVE_OID_E) {
  18728. /* ignore error for curves not found */
  18729. }
  18730. else {
  18731. printf("ecc_test_key_gen %d failed!: %d\n", keySize, ret);
  18732. return ret;
  18733. }
  18734. }
  18735. #endif
  18736. return 0;
  18737. }
  18738. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  18739. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  18740. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  18741. !defined(WOLFSSL_NO_MALLOC)
  18742. static int ecc_point_test(void)
  18743. {
  18744. int ret;
  18745. ecc_point* point;
  18746. ecc_point* point2;
  18747. #ifdef HAVE_COMP_KEY
  18748. ecc_point* point3;
  18749. ecc_point* point4;
  18750. #endif
  18751. word32 outLen;
  18752. byte out[65];
  18753. byte der[] = { 0x04, /* = Uncompressed */
  18754. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18755. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18756. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18757. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18758. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18759. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18760. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18761. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  18762. #ifdef HAVE_COMP_KEY
  18763. byte derComp0[] = { 0x02, /* = Compressed, y even */
  18764. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18765. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18766. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18767. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  18768. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  18769. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18770. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18771. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18772. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  18773. #endif
  18774. byte altDer[] = { 0x04, /* = Uncompressed */
  18775. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18776. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18777. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18778. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18779. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18780. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18781. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18782. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  18783. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  18784. /* if curve P256 is not enabled then test should not fail */
  18785. if (curve_idx == ECC_CURVE_INVALID)
  18786. return 0;
  18787. outLen = sizeof(out);
  18788. point = wc_ecc_new_point();
  18789. if (point == NULL)
  18790. return -10000;
  18791. point2 = wc_ecc_new_point();
  18792. if (point2 == NULL) {
  18793. wc_ecc_del_point(point);
  18794. return -10001;
  18795. }
  18796. #ifdef HAVE_COMP_KEY
  18797. point3 = wc_ecc_new_point();
  18798. if (point3 == NULL) {
  18799. wc_ecc_del_point(point2);
  18800. wc_ecc_del_point(point);
  18801. return -10002;
  18802. }
  18803. point4 = wc_ecc_new_point();
  18804. if (point4 == NULL) {
  18805. wc_ecc_del_point(point3);
  18806. wc_ecc_del_point(point2);
  18807. wc_ecc_del_point(point);
  18808. return -10003;
  18809. }
  18810. #endif
  18811. /* Parameter Validation testing. */
  18812. wc_ecc_del_point(NULL);
  18813. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  18814. if (ret != ECC_BAD_ARG_E) {
  18815. ret = -10004;
  18816. goto done;
  18817. }
  18818. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  18819. if (ret != ECC_BAD_ARG_E) {
  18820. ret = -10005;
  18821. goto done;
  18822. }
  18823. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  18824. if (ret != ECC_BAD_ARG_E) {
  18825. ret = -10006;
  18826. goto done;
  18827. }
  18828. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  18829. if (ret != ECC_BAD_ARG_E) {
  18830. ret = -10007;
  18831. goto done;
  18832. }
  18833. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  18834. if (ret != ECC_BAD_ARG_E) {
  18835. ret = -10008;
  18836. goto done;
  18837. }
  18838. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  18839. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  18840. ret = -10009;
  18841. goto done;
  18842. }
  18843. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  18844. if (ret != ECC_BAD_ARG_E) {
  18845. ret = -10010;
  18846. goto done;
  18847. }
  18848. outLen = 0;
  18849. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  18850. if (ret != BUFFER_E) {
  18851. ret = -10011;
  18852. goto done;
  18853. }
  18854. ret = wc_ecc_copy_point(NULL, NULL);
  18855. if (ret != ECC_BAD_ARG_E) {
  18856. ret = -10012;
  18857. goto done;
  18858. }
  18859. ret = wc_ecc_copy_point(NULL, point2);
  18860. if (ret != ECC_BAD_ARG_E) {
  18861. ret = -10013;
  18862. goto done;
  18863. }
  18864. ret = wc_ecc_copy_point(point, NULL);
  18865. if (ret != ECC_BAD_ARG_E) {
  18866. ret = -10014;
  18867. goto done;
  18868. }
  18869. ret = wc_ecc_cmp_point(NULL, NULL);
  18870. if (ret != BAD_FUNC_ARG) {
  18871. ret = -10015;
  18872. goto done;
  18873. }
  18874. ret = wc_ecc_cmp_point(NULL, point2);
  18875. if (ret != BAD_FUNC_ARG) {
  18876. ret = -10016;
  18877. goto done;
  18878. }
  18879. ret = wc_ecc_cmp_point(point, NULL);
  18880. if (ret != BAD_FUNC_ARG) {
  18881. ret = -10017;
  18882. goto done;
  18883. }
  18884. /* Use API. */
  18885. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  18886. if (ret != 0) {
  18887. ret = -10018;
  18888. goto done;
  18889. }
  18890. outLen = sizeof(out);
  18891. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  18892. if (ret != 0) {
  18893. ret = -10019;
  18894. goto done;
  18895. }
  18896. if (outLen != sizeof(der)) {
  18897. ret = -10020;
  18898. goto done;
  18899. }
  18900. if (XMEMCMP(out, der, outLen) != 0) {
  18901. ret = -10021;
  18902. goto done;
  18903. }
  18904. ret = wc_ecc_copy_point(point2, point);
  18905. if (ret != MP_OKAY) {
  18906. ret = -10022;
  18907. goto done;
  18908. }
  18909. ret = wc_ecc_cmp_point(point2, point);
  18910. if (ret != MP_EQ) {
  18911. ret = -10023;
  18912. goto done;
  18913. }
  18914. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  18915. if (ret != 0) {
  18916. ret = -10024;
  18917. goto done;
  18918. }
  18919. ret = wc_ecc_cmp_point(point2, point);
  18920. if (ret != MP_GT) {
  18921. ret = -10025;
  18922. goto done;
  18923. }
  18924. #ifdef HAVE_COMP_KEY
  18925. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  18926. if (ret != 0) {
  18927. ret = -10026;
  18928. goto done;
  18929. }
  18930. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  18931. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  18932. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  18933. if (ret != 0) {
  18934. ret = -10027;
  18935. goto done;
  18936. }
  18937. #endif
  18938. ret = wc_ecc_cmp_point(point3, point4);
  18939. if (ret != MP_EQ) {
  18940. ret = -10028;
  18941. goto done;
  18942. }
  18943. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  18944. if (ret != 0) {
  18945. ret = -10029;
  18946. goto done;
  18947. }
  18948. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  18949. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  18950. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  18951. if (ret != 0) {
  18952. ret = -10030;
  18953. goto done;
  18954. }
  18955. #endif
  18956. ret = wc_ecc_cmp_point(point3, point4);
  18957. if (ret != MP_EQ) {
  18958. ret = -10031;
  18959. goto done;
  18960. }
  18961. #endif
  18962. done:
  18963. #ifdef HAVE_COMP_KEY
  18964. wc_ecc_del_point(point4);
  18965. wc_ecc_del_point(point3);
  18966. #endif
  18967. wc_ecc_del_point(point2);
  18968. wc_ecc_del_point(point);
  18969. return ret;
  18970. }
  18971. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  18972. #ifndef NO_SIG_WRAPPER
  18973. static int ecc_sig_test(WC_RNG* rng, ecc_key* key)
  18974. {
  18975. int ret;
  18976. word32 sigSz;
  18977. int size;
  18978. byte out[ECC_MAX_SIG_SIZE];
  18979. byte in[] = TEST_STRING;
  18980. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  18981. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  18982. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  18983. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  18984. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  18985. };
  18986. word32 inLen = (word32)XSTRLEN((char*)in);
  18987. size = wc_ecc_sig_size(key);
  18988. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  18989. if (ret != size)
  18990. return -10040;
  18991. sigSz = (word32)ret;
  18992. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  18993. inLen, out, &sigSz, key, sizeof(*key), rng);
  18994. if (ret != 0)
  18995. return -10041;
  18996. TEST_SLEEP();
  18997. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  18998. inLen, out, sigSz, key, sizeof(*key));
  18999. if (ret != 0)
  19000. return -10042;
  19001. TEST_SLEEP();
  19002. sigSz = (word32)sizeof(out);
  19003. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  19004. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  19005. if (ret != 0)
  19006. return -10043;
  19007. TEST_SLEEP();
  19008. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  19009. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  19010. if (ret != 0)
  19011. return -10044;
  19012. TEST_SLEEP();
  19013. return 0;
  19014. }
  19015. #endif
  19016. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  19017. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  19018. static int ecc_exp_imp_test(ecc_key* key)
  19019. {
  19020. int ret;
  19021. int curve_id;
  19022. #ifdef WOLFSSL_SMALL_STACK
  19023. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);;
  19024. #else
  19025. ecc_key keyImp[1];
  19026. #endif
  19027. byte priv[32];
  19028. word32 privLen;
  19029. byte pub[65];
  19030. word32 pubLen, pubLenX, pubLenY;
  19031. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  19032. "ce46cbc947616d0cbaa82323818a793d";
  19033. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  19034. "922f8b30ea6e8811742ac7238fe87308";
  19035. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  19036. "2e7a493ec1153b3a95feb8a4873f8d08";
  19037. #ifdef WOLFSSL_SMALL_STACK
  19038. if (keyImp == NULL)
  19039. ERROR_OUT(-10050, done);
  19040. #endif
  19041. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19042. privLen = sizeof(priv);
  19043. ret = wc_ecc_export_private_only(key, priv, &privLen);
  19044. if (ret != 0) {
  19045. ret = -10051;
  19046. goto done;
  19047. }
  19048. pubLen = sizeof(pub);
  19049. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  19050. if (ret != 0) {
  19051. ret = -10052;
  19052. goto done;
  19053. }
  19054. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  19055. if (ret != 0) {
  19056. ret = -10053;
  19057. goto done;
  19058. }
  19059. wc_ecc_free(keyImp);
  19060. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19061. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  19062. if (ret != 0) {
  19063. ret = -10054;
  19064. goto done;
  19065. }
  19066. wc_ecc_free(keyImp);
  19067. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19068. curve_id = wc_ecc_get_curve_id(key->idx);
  19069. if (curve_id < 0) {
  19070. ret = -10055;
  19071. goto done;
  19072. }
  19073. /* test import private only */
  19074. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  19075. curve_id);
  19076. if (ret != 0) {
  19077. ret = -10056;
  19078. goto done;
  19079. }
  19080. wc_ecc_free(keyImp);
  19081. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19082. /* test export public raw */
  19083. pubLenX = pubLenY = 32;
  19084. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  19085. if (ret != 0) {
  19086. ret = -10057;
  19087. goto done;
  19088. }
  19089. #ifndef HAVE_SELFTEST
  19090. /* test import of public */
  19091. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  19092. if (ret != 0) {
  19093. ret = -10058;
  19094. goto done;
  19095. }
  19096. #endif
  19097. wc_ecc_free(keyImp);
  19098. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19099. /* test export private and public raw */
  19100. pubLenX = pubLenY = privLen = 32;
  19101. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  19102. priv, &privLen);
  19103. if (ret != 0) {
  19104. ret = -10059;
  19105. goto done;
  19106. }
  19107. #ifndef HAVE_SELFTEST
  19108. /* test import of private and public */
  19109. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  19110. if (ret != 0) {
  19111. ret = -10060;
  19112. goto done;
  19113. }
  19114. #endif
  19115. done:
  19116. #ifdef WOLFSSL_SMALL_STACK
  19117. if (keyImp != NULL) {
  19118. wc_ecc_free(keyImp);
  19119. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19120. }
  19121. #else
  19122. wc_ecc_free(keyImp);
  19123. #endif
  19124. return ret;
  19125. }
  19126. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  19127. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  19128. !defined(WOLFSSL_CRYPTOCELL)
  19129. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)
  19130. static int ecc_mulmod_test(ecc_key* key1)
  19131. {
  19132. int ret;
  19133. #ifdef WOLFSSL_SMALL_STACK
  19134. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19135. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19136. #else
  19137. ecc_key key2[1];
  19138. ecc_key key3[1];
  19139. #endif
  19140. #ifdef WOLFSSL_SMALL_STACK
  19141. if ((key2 == NULL) || (key3 == NULL))
  19142. ERROR_OUT(MEMORY_E, done);
  19143. #endif
  19144. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  19145. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  19146. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  19147. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  19148. * private key in key2.
  19149. */
  19150. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  19151. ECC_SECP256R1);
  19152. if (ret != 0)
  19153. goto done;
  19154. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  19155. * in key3.
  19156. */
  19157. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  19158. key1->dp->prime, ECC_SECP256R1);
  19159. if (ret != 0)
  19160. goto done;
  19161. ret = wc_ecc_mulmod(&key1->k, &key2->pubkey, &key3->pubkey, &key2->k, &key3->k,
  19162. 1);
  19163. if (ret != 0) {
  19164. ret = -10070;
  19165. goto done;
  19166. }
  19167. done:
  19168. #ifdef WOLFSSL_SMALL_STACK
  19169. if (key2 != NULL) {
  19170. wc_ecc_free(key2);
  19171. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19172. }
  19173. if (key3 != NULL) {
  19174. wc_ecc_free(key3);
  19175. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19176. }
  19177. #else
  19178. wc_ecc_free(key3);
  19179. wc_ecc_free(key2);
  19180. #endif
  19181. return ret;
  19182. }
  19183. #endif
  19184. #ifdef HAVE_ECC_DHE
  19185. static int ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  19186. {
  19187. int ret;
  19188. byte out[128];
  19189. word32 outLen = sizeof(out);
  19190. /* Parameter Validation testing. */
  19191. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  19192. if (ret != BAD_FUNC_ARG)
  19193. return -10080;
  19194. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  19195. if (ret != BAD_FUNC_ARG)
  19196. return -10081;
  19197. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  19198. if (ret != BAD_FUNC_ARG)
  19199. return -10082;
  19200. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  19201. if (ret != BAD_FUNC_ARG)
  19202. return -10083;
  19203. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  19204. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  19205. !defined(HAVE_SELFTEST)
  19206. ret = wc_ecc_set_rng(key, rng);
  19207. if (ret != 0)
  19208. return -10084;
  19209. #else
  19210. (void)rng;
  19211. #endif
  19212. /* Use API. */
  19213. ret = 0;
  19214. do {
  19215. #if defined(WOLFSSL_ASYNC_CRYPT)
  19216. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19217. #endif
  19218. if (ret == 0)
  19219. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  19220. } while (ret == WC_PENDING_E);
  19221. if (ret != 0)
  19222. return -10085;
  19223. TEST_SLEEP();
  19224. return 0;
  19225. }
  19226. #endif /* HAVE_ECC_DHE */
  19227. #endif
  19228. static int ecc_def_curve_test(WC_RNG *rng)
  19229. {
  19230. int ret;
  19231. #ifdef WOLFSSL_SMALL_STACK
  19232. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19233. #else
  19234. ecc_key key[1];
  19235. #endif
  19236. #ifdef WOLFSSL_SMALL_STACK
  19237. if (key == NULL)
  19238. ERROR_OUT(MEMORY_E, done);
  19239. #endif
  19240. wc_ecc_init_ex(key, HEAP_HINT, devId);
  19241. /* Use API */
  19242. ret = wc_ecc_set_flags(NULL, 0);
  19243. if (ret != BAD_FUNC_ARG) {
  19244. ret = -10090;
  19245. goto done;
  19246. }
  19247. ret = wc_ecc_set_flags(key, 0);
  19248. if (ret != 0) {
  19249. ret = -10091;
  19250. goto done;
  19251. }
  19252. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  19253. #if defined(WOLFSSL_ASYNC_CRYPT)
  19254. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19255. #endif
  19256. if (ret != 0) {
  19257. ret = -10092;
  19258. goto done;
  19259. }
  19260. TEST_SLEEP();
  19261. #ifndef NO_SIG_WRAPPER
  19262. ret = ecc_sig_test(rng, key);
  19263. if (ret < 0)
  19264. goto done;
  19265. #endif
  19266. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  19267. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  19268. ret = ecc_exp_imp_test(key);
  19269. if (ret < 0)
  19270. goto done;
  19271. #endif
  19272. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  19273. !defined(WOLFSSL_CRYPTOCELL)
  19274. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)
  19275. ret = ecc_mulmod_test(key);
  19276. if (ret < 0)
  19277. goto done;
  19278. #endif
  19279. #ifdef HAVE_ECC_DHE
  19280. ret = ecc_ssh_test(key, rng);
  19281. if (ret < 0)
  19282. goto done;
  19283. #endif
  19284. #endif /* WOLFSSL_ATECC508A */
  19285. done:
  19286. #ifdef WOLFSSL_SMALL_STACK
  19287. if (key != NULL) {
  19288. wc_ecc_free(key);
  19289. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19290. }
  19291. #else
  19292. wc_ecc_free(key);
  19293. #endif
  19294. return ret;
  19295. }
  19296. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  19297. #ifdef WOLFSSL_CERT_EXT
  19298. static int ecc_decode_test(void)
  19299. {
  19300. int ret;
  19301. word32 inSz;
  19302. word32 inOutIdx;
  19303. #ifdef WOLFSSL_SMALL_STACK
  19304. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19305. #else
  19306. ecc_key key[1];
  19307. #endif
  19308. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  19309. /* This is ecc_clikeypub_der_256. */
  19310. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  19311. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  19312. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  19313. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  19314. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  19315. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  19316. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  19317. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  19318. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  19319. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  19320. 0xb4 };
  19321. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  19322. 0x00, 0x04, 0x01, 0x01 };
  19323. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  19324. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  19325. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  19326. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  19327. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  19328. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  19329. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  19330. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  19331. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  19332. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  19333. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  19334. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  19335. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  19336. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  19337. 0x03, 0x03, 0x04, 0x01, 0x01 };
  19338. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  19339. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  19340. 0x03, 0x03, 0x00, 0x04, 0x01 };
  19341. #ifdef WOLFSSL_SMALL_STACK
  19342. if (key == NULL)
  19343. ERROR_OUT(MEMORY_E, done);
  19344. #endif
  19345. XMEMSET(key, 0, sizeof *key);
  19346. wc_ecc_init_ex(key, HEAP_HINT, devId);
  19347. inSz = sizeof(good);
  19348. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  19349. if (ret != BAD_FUNC_ARG) {
  19350. ret = -10100;
  19351. goto done;
  19352. }
  19353. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  19354. if (ret != BAD_FUNC_ARG) {
  19355. ret = -10101;
  19356. goto done;
  19357. }
  19358. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  19359. if (ret != BAD_FUNC_ARG) {
  19360. ret = -10102;
  19361. goto done;
  19362. }
  19363. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  19364. if (ret != BAD_FUNC_ARG) {
  19365. ret = -10103;
  19366. goto done;
  19367. }
  19368. /* Change offset to produce bad input data. */
  19369. inOutIdx = 2;
  19370. inSz = sizeof(good) - inOutIdx;
  19371. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  19372. if (ret != ASN_PARSE_E) {
  19373. ret = -10104;
  19374. goto done;
  19375. }
  19376. inOutIdx = 4;
  19377. inSz = sizeof(good) - inOutIdx;
  19378. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  19379. if (ret != ASN_PARSE_E) {
  19380. ret = -10105;
  19381. goto done;
  19382. }
  19383. /* Bad data. */
  19384. inSz = sizeof(badNoObjId);
  19385. inOutIdx = 0;
  19386. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  19387. if (ret != ASN_OBJECT_ID_E) {
  19388. ret = -10106;
  19389. goto done;
  19390. }
  19391. inSz = sizeof(badOneObjId);
  19392. inOutIdx = 0;
  19393. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  19394. if (ret != ASN_OBJECT_ID_E) {
  19395. ret = -10107;
  19396. goto done;
  19397. }
  19398. inSz = sizeof(badObjId1Len);
  19399. inOutIdx = 0;
  19400. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  19401. if (ret != ASN_PARSE_E) {
  19402. ret = -10108;
  19403. goto done;
  19404. }
  19405. inSz = sizeof(badObj2d1Len);
  19406. inOutIdx = 0;
  19407. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  19408. if (ret != ASN_PARSE_E) {
  19409. ret = -10109;
  19410. goto done;
  19411. }
  19412. inSz = sizeof(badNotBitStr);
  19413. inOutIdx = 0;
  19414. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  19415. if (ret != ASN_BITSTR_E) {
  19416. ret = -10110;
  19417. goto done;
  19418. }
  19419. inSz = sizeof(badBitStrLen);
  19420. inOutIdx = 0;
  19421. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  19422. if (ret != ASN_PARSE_E) {
  19423. ret = -10111;
  19424. goto done;
  19425. }
  19426. inSz = sizeof(badNoBitStrZero);
  19427. inOutIdx = 0;
  19428. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  19429. if (ret != ASN_EXPECT_0_E) {
  19430. ret = -10112;
  19431. goto done;
  19432. }
  19433. inSz = sizeof(badPoint);
  19434. inOutIdx = 0;
  19435. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  19436. if (ret != ASN_ECC_KEY_E) {
  19437. ret = -10113;
  19438. goto done;
  19439. }
  19440. inSz = sizeof(good);
  19441. inOutIdx = 0;
  19442. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  19443. if (ret != 0) {
  19444. ret = -10114;
  19445. goto done;
  19446. }
  19447. done:
  19448. #ifdef WOLFSSL_SMALL_STACK
  19449. if (key != NULL) {
  19450. wc_ecc_free(key);
  19451. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19452. }
  19453. #else
  19454. wc_ecc_free(key);
  19455. #endif
  19456. return ret;
  19457. }
  19458. #endif /* WOLFSSL_CERT_EXT */
  19459. #ifdef WOLFSSL_CUSTOM_CURVES
  19460. static const byte eccKeyExplicitCurve[] = {
  19461. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  19462. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  19463. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  19464. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  19465. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  19466. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19467. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19468. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  19469. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  19470. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  19471. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  19472. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  19473. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  19474. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  19475. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  19476. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  19477. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  19478. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  19479. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19480. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19481. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  19482. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  19483. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  19484. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  19485. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  19486. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  19487. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  19488. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  19489. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  19490. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  19491. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  19492. };
  19493. static int ecc_test_custom_curves(WC_RNG* rng)
  19494. {
  19495. int ret;
  19496. word32 inOutIdx;
  19497. #ifdef WOLFSSL_SMALL_STACK
  19498. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19499. #else
  19500. ecc_key key[1];
  19501. #endif
  19502. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  19503. #ifndef WOLFSSL_ECC_CURVE_STATIC
  19504. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  19505. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  19506. };
  19507. #define ecc_oid_brainpoolp256r1_sz \
  19508. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  19509. #else
  19510. #define ecc_oid_brainpoolp256r1 { \
  19511. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  19512. }
  19513. #define ecc_oid_brainpoolp256r1_sz 9
  19514. #endif
  19515. #define ecc_oid_brainpoolp256r1_sum 104
  19516. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  19517. 32, /* size/bytes */
  19518. ECC_CURVE_CUSTOM, /* ID */
  19519. "BRAINPOOLP256R1", /* curve name */
  19520. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  19521. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  19522. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  19523. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  19524. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  19525. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  19526. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  19527. ecc_oid_brainpoolp256r1_sz,
  19528. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  19529. 1, /* cofactor */
  19530. };
  19531. #ifdef WOLFSSL_SMALL_STACK
  19532. if (! key) {
  19533. ret = MEMORY_E;
  19534. goto done;
  19535. }
  19536. #endif
  19537. XMEMSET(key, 0, sizeof *key);
  19538. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  19539. &ecc_dp_brainpool256r1);
  19540. if (ret != 0) {
  19541. printf("ECC test for custom curve failed! %d\n", ret);
  19542. goto done;
  19543. }
  19544. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  19545. {
  19546. int curve_id;
  19547. #ifdef HAVE_ECC_BRAINPOOL
  19548. curve_id = ECC_BRAINPOOLP256R1;
  19549. #else
  19550. curve_id = ECC_SECP256K1;
  19551. #endif
  19552. /* Test and demonstrate use of non-SECP curve */
  19553. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  19554. if (ret < 0) {
  19555. printf("ECC test for curve_id %d failed! %d\n", curve_id, ret);
  19556. goto done;
  19557. }
  19558. }
  19559. #endif
  19560. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  19561. if (ret != 0) {
  19562. ret = -10120;
  19563. goto done;
  19564. }
  19565. inOutIdx = 0;
  19566. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  19567. sizeof(eccKeyExplicitCurve));
  19568. if (ret != 0)
  19569. ret = -10121;
  19570. done:
  19571. #ifdef WOLFSSL_SMALL_STACK
  19572. if (key) {
  19573. wc_ecc_free(key);
  19574. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19575. }
  19576. #else
  19577. wc_ecc_free(key);
  19578. #endif
  19579. return ret;
  19580. }
  19581. #endif /* WOLFSSL_CUSTOM_CURVES */
  19582. #ifdef WOLFSSL_CERT_GEN
  19583. /* Make Cert / Sign example for ECC cert and ECC CA */
  19584. static int ecc_test_cert_gen(WC_RNG* rng)
  19585. {
  19586. int ret;
  19587. #ifdef WOLFSSL_SMALL_STACK
  19588. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19589. #ifdef WOLFSSL_TEST_CERT
  19590. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19591. #endif
  19592. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19593. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19594. #else
  19595. Cert myCert[1];
  19596. #ifdef WOLFSSL_TEST_CERT
  19597. DecodedCert decode[1];
  19598. #endif
  19599. ecc_key caEccKey[1];
  19600. ecc_key certPubKey[1];
  19601. #endif
  19602. int certSz;
  19603. size_t bytes;
  19604. word32 idx = 0;
  19605. #ifndef USE_CERT_BUFFERS_256
  19606. XFILE file;
  19607. #endif
  19608. #ifdef WOLFSSL_SMALL_STACK
  19609. byte* der = NULL;
  19610. #else
  19611. byte der[FOURK_BUF];
  19612. #endif
  19613. #ifdef WOLFSSL_SMALL_STACK
  19614. if ((myCert == NULL)
  19615. #ifdef WOLFSSL_TEST_CERT
  19616. || (decode == NULL)
  19617. #endif
  19618. || (caEccKey == NULL) || (certPubKey == NULL))
  19619. ERROR_OUT(MEMORY_E, exit);
  19620. #endif
  19621. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  19622. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  19623. #ifdef WOLFSSL_SMALL_STACK
  19624. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19625. if (der == NULL) {
  19626. ERROR_OUT(-10130, exit);
  19627. }
  19628. #endif
  19629. /* Get cert private key */
  19630. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  19631. /* Get Cert Key 384 */
  19632. #ifdef USE_CERT_BUFFERS_256
  19633. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  19634. bytes = sizeof_ca_ecc_key_der_384;
  19635. #else
  19636. file = XFOPEN(eccCaKey384File, "rb");
  19637. if (!file) {
  19638. ERROR_OUT(-10131, exit);
  19639. }
  19640. bytes = XFREAD(der, 1, FOURK_BUF, file);
  19641. XFCLOSE(file);
  19642. (void)eccCaKeyFile;
  19643. #endif /* USE_CERT_BUFFERS_256 */
  19644. #else
  19645. #ifdef USE_CERT_BUFFERS_256
  19646. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  19647. bytes = sizeof_ca_ecc_key_der_256;
  19648. #else
  19649. file = XFOPEN(eccCaKeyFile, "rb");
  19650. if (!file) {
  19651. ERROR_OUT(-10132, exit);
  19652. }
  19653. bytes = XFREAD(der, 1, FOURK_BUF, file);
  19654. XFCLOSE(file);
  19655. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  19656. (void)eccCaKey384File;
  19657. #endif
  19658. #endif /* USE_CERT_BUFFERS_256 */
  19659. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  19660. /* Get CA Key */
  19661. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  19662. if (ret != 0) {
  19663. ERROR_OUT(-10133, exit);
  19664. }
  19665. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  19666. if (ret != 0) {
  19667. ERROR_OUT(-10134, exit);
  19668. }
  19669. /* Make a public key */
  19670. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  19671. if (ret != 0) {
  19672. ERROR_OUT(-10135, exit);
  19673. }
  19674. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  19675. #if defined(WOLFSSL_ASYNC_CRYPT)
  19676. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  19677. #endif
  19678. if (ret != 0) {
  19679. ERROR_OUT(-10136, exit);
  19680. }
  19681. TEST_SLEEP();
  19682. /* Setup Certificate */
  19683. if (wc_InitCert(myCert)) {
  19684. ERROR_OUT(-10137, exit);
  19685. }
  19686. #ifndef NO_SHA256
  19687. myCert->sigType = CTC_SHA256wECDSA;
  19688. #else
  19689. myCert->sigType = CTC_SHAwECDSA;
  19690. #endif
  19691. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  19692. #ifdef WOLFSSL_CERT_EXT
  19693. /* add Policies */
  19694. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  19695. CTC_MAX_CERTPOL_SZ);
  19696. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  19697. CTC_MAX_CERTPOL_SZ);
  19698. myCert->certPoliciesNb = 2;
  19699. /* add SKID from the Public Key */
  19700. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey) != 0) {
  19701. ERROR_OUT(-10138, exit);
  19702. }
  19703. /* add AKID from the Public Key */
  19704. if (wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey) != 0) {
  19705. ERROR_OUT(-10139, exit);
  19706. }
  19707. /* add Key Usage */
  19708. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  19709. ERROR_OUT(-10140, exit);
  19710. }
  19711. #endif /* WOLFSSL_CERT_EXT */
  19712. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  19713. #if defined(USE_CERT_BUFFERS_256)
  19714. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  19715. sizeof_ca_ecc_cert_der_384);
  19716. #else
  19717. ret = wc_SetIssuer(myCert, eccCaCert384File);
  19718. (void)eccCaCertFile;
  19719. #endif
  19720. #else
  19721. #if defined(USE_CERT_BUFFERS_256)
  19722. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  19723. sizeof_ca_ecc_cert_der_256);
  19724. #else
  19725. ret = wc_SetIssuer(myCert, eccCaCertFile);
  19726. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  19727. (void)eccCaCert384File;
  19728. #endif
  19729. #endif
  19730. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  19731. if (ret < 0) {
  19732. ERROR_OUT(-10141, exit);
  19733. }
  19734. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  19735. if (certSz < 0) {
  19736. ERROR_OUT(-10142, exit);
  19737. }
  19738. ret = 0;
  19739. do {
  19740. #if defined(WOLFSSL_ASYNC_CRYPT)
  19741. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19742. #endif
  19743. if (ret >= 0) {
  19744. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  19745. FOURK_BUF, NULL, caEccKey, rng);
  19746. }
  19747. } while (ret == WC_PENDING_E);
  19748. if (ret < 0) {
  19749. ERROR_OUT(-10143, exit);
  19750. }
  19751. certSz = ret;
  19752. TEST_SLEEP();
  19753. #ifdef WOLFSSL_TEST_CERT
  19754. InitDecodedCert(decode, der, certSz, 0);
  19755. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  19756. if (ret != 0) {
  19757. FreeDecodedCert(decode);
  19758. ERROR_OUT(-10144, exit);
  19759. }
  19760. FreeDecodedCert(decode);
  19761. #endif
  19762. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  19763. CERT_TYPE, -6735);
  19764. if (ret != 0) {
  19765. goto exit;
  19766. }
  19767. exit:
  19768. #ifdef WOLFSSL_SMALL_STACK
  19769. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19770. #endif
  19771. #ifdef WOLFSSL_SMALL_STACK
  19772. if (myCert != NULL)
  19773. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19774. #ifdef WOLFSSL_TEST_CERT
  19775. if (decode != NULL)
  19776. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19777. #endif
  19778. if (caEccKey != NULL) {
  19779. wc_ecc_free(caEccKey);
  19780. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19781. }
  19782. if (certPubKey != NULL) {
  19783. wc_ecc_free(certPubKey);
  19784. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19785. }
  19786. #else
  19787. wc_ecc_free(certPubKey);
  19788. wc_ecc_free(caEccKey);
  19789. #endif
  19790. return ret;
  19791. }
  19792. #endif /* WOLFSSL_CERT_GEN */
  19793. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  19794. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  19795. static int ecc_test_allocator(WC_RNG* rng)
  19796. {
  19797. int ret = 0;
  19798. ecc_key* key;
  19799. key = wc_ecc_key_new(HEAP_HINT);
  19800. if (key == NULL) {
  19801. ERROR_OUT(-10150, exit);
  19802. }
  19803. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  19804. if (ret != 0) {
  19805. ERROR_OUT(-10151, exit);
  19806. }
  19807. exit:
  19808. wc_ecc_key_free(key);
  19809. return ret;
  19810. }
  19811. #endif
  19812. /* ECC Non-blocking tests for Sign and Verify */
  19813. /* Requires SP math and supports P384 or P256 */
  19814. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  19815. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  19816. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  19817. /* Test Data - Random */
  19818. static const uint8_t kMsg[] = {
  19819. 0x69, 0xbc, 0x9f, 0xce, 0x68, 0x17, 0xc2, 0x10, 0xea, 0xfc, 0x10, 0x65, 0x67, 0x52, 0xed, 0x78,
  19820. 0x6e, 0xb8, 0x83, 0x9c, 0x9a, 0xb4, 0x56, 0x0d, 0xc1, 0x0d, 0x1f, 0x78, 0x6e, 0x75, 0xd7, 0xbe,
  19821. 0x92, 0x6b, 0x12, 0xf6, 0x76, 0x60, 0x8e, 0xb1, 0xf4, 0x19, 0x0c, 0x81, 0xe7, 0x54, 0x5e, 0xbc,
  19822. 0xe0, 0xae, 0xc2, 0x7d, 0x1b, 0xc4, 0x6e, 0xec, 0xb1, 0x99, 0x6c, 0xbf, 0x0e, 0x38, 0xa8, 0x01,
  19823. 0xa6, 0x9a, 0x48, 0x12, 0xe4, 0xc9, 0x3b, 0xf0, 0x63, 0x46, 0x15, 0xb4, 0x61, 0xa8, 0x1a, 0x60,
  19824. 0x71, 0x87, 0x98, 0xd7, 0x6f, 0x98, 0x7b, 0x2d, 0xb9, 0x19, 0x1b, 0x21, 0x9c, 0x70, 0x58, 0xe8,
  19825. 0x0d, 0x0f, 0xe9, 0x2d, 0x9a, 0x9a, 0xf1, 0x55, 0xa0, 0x4c, 0xd3, 0x07, 0xbd, 0x97, 0x48, 0xec,
  19826. 0x88, 0x0a, 0xaf, 0xb3, 0x80, 0x78, 0xa4, 0x59, 0x43, 0x57, 0xd3, 0xa7, 0x01, 0x66, 0x0e, 0xfc
  19827. };
  19828. /* ECC Private Key "d" */
  19829. static const uint8_t kPrivKey[] = {
  19830. #ifdef HAVE_ECC384
  19831. /* SECP384R1 */
  19832. /* d */
  19833. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  19834. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  19835. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  19836. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  19837. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  19838. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  19839. #else
  19840. /* SECP256R1 */
  19841. /* d */
  19842. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  19843. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  19844. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  19845. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  19846. #endif
  19847. };
  19848. /* ECC public key Qx/Qy */
  19849. static const uint8_t kPubKey[] = {
  19850. #ifdef HAVE_ECC384
  19851. /* SECP384R1 */
  19852. /* Qx */
  19853. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  19854. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  19855. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  19856. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  19857. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  19858. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  19859. /* Qy */
  19860. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  19861. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  19862. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  19863. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  19864. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  19865. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  19866. #else
  19867. /* SECP256R1 */
  19868. /* Qx */
  19869. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  19870. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  19871. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  19872. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  19873. /* Qy */
  19874. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  19875. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  19876. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  19877. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  19878. #endif
  19879. };
  19880. /* ECC Curve */
  19881. #ifdef HAVE_ECC384
  19882. /* SECP384R1 */
  19883. #define ECC_CURVE_SZ 48
  19884. #define ECC_CURVE_ID ECC_SECP384R1
  19885. #else
  19886. /* SECP256R1 */
  19887. #define ECC_CURVE_SZ 32
  19888. #define ECC_CURVE_ID ECC_SECP256R1
  19889. #endif
  19890. /* Hash Algorithm */
  19891. #if defined(HAVE_ECC384) && defined(WOLFSSL_SHA3)
  19892. #define HASH_DIGEST_SZ WC_SHA3_384_DIGEST_SIZE
  19893. #define HASH_SHA_VER 3
  19894. #define CRYPTO_HASH_FN crypto_sha3_384
  19895. #elif defined(HAVE_ECC384) && defined(WOLFSSL_SHA384)
  19896. #define HASH_DIGEST_SZ WC_SHA384_DIGEST_SIZE
  19897. #define HASH_SHA_VER 2
  19898. #define CRYPTO_HASH_FN crypto_sha2_384
  19899. #elif !defined(NO_SHA256)
  19900. #define HASH_DIGEST_SZ WC_SHA256_DIGEST_SIZE
  19901. #define HASH_SHA_VER 2
  19902. #define CRYPTO_HASH_FN crypto_sha2_256
  19903. #else
  19904. #error test configuration not supported
  19905. #endif
  19906. #if defined(HAVE_ECC384) && defined(WOLFSSL_SHA3)
  19907. /* helper to perform hashing block by block */
  19908. static int crypto_sha3_384(const uint8_t *buf, uint32_t len, uint8_t *hash,
  19909. uint32_t hashSz, uint32_t blkSz)
  19910. {
  19911. int ret;
  19912. uint32_t i = 0, chunk;
  19913. wc_Sha3 sha3;
  19914. /* validate arguments */
  19915. if ((buf == NULL && len > 0) || hash == NULL ||
  19916. hashSz < WC_SHA3_384_DIGEST_SIZE || blkSz == 0)
  19917. {
  19918. return BAD_FUNC_ARG;
  19919. }
  19920. /* Init Sha3_384 structure */
  19921. ret = wc_InitSha3_384(&sha3, NULL, INVALID_DEVID);
  19922. if (ret != 0) {
  19923. return ret;
  19924. }
  19925. while (i < len) {
  19926. chunk = blkSz;
  19927. if ((chunk + i) > len)
  19928. chunk = len - i;
  19929. /* Perform chunked update */
  19930. ret = wc_Sha3_384_Update(&sha3, (buf + i), chunk);
  19931. if (ret != 0) {
  19932. break;
  19933. }
  19934. i += chunk;
  19935. }
  19936. if (ret == 0) {
  19937. /* Get final digest result */
  19938. ret = wc_Sha3_384_Final(&sha3, hash);
  19939. }
  19940. return ret;
  19941. }
  19942. #elif defined(HAVE_ECC384) && defined(WOLFSSL_SHA384)
  19943. /* helper to perform hashing block by block */
  19944. static int crypto_sha2_384(const uint8_t *buf, uint32_t len, uint8_t *hash,
  19945. uint32_t hashSz, uint32_t blkSz)
  19946. {
  19947. int ret;
  19948. uint32_t i = 0, chunk;
  19949. wc_Sha384 sha384;
  19950. /* validate arguments */
  19951. if ((buf == NULL && len > 0) || hash == NULL ||
  19952. hashSz < WC_SHA384_DIGEST_SIZE || blkSz == 0)
  19953. {
  19954. return BAD_FUNC_ARG;
  19955. }
  19956. /* Init Sha384 structure */
  19957. ret = wc_InitSha384(&sha384);
  19958. if (ret != 0) {
  19959. return ret;
  19960. }
  19961. while (i < len) {
  19962. chunk = blkSz;
  19963. if ((chunk + i) > len)
  19964. chunk = len - i;
  19965. /* Perform chunked update */
  19966. ret = wc_Sha384Update(&sha384, (buf + i), chunk);
  19967. if (ret != 0) {
  19968. break;
  19969. }
  19970. i += chunk;
  19971. }
  19972. if (ret == 0) {
  19973. /* Get final digest result */
  19974. ret = wc_Sha384Final(&sha384, hash);
  19975. }
  19976. return ret;
  19977. }
  19978. #elif !defined(NO_SHA256)
  19979. /* helper to perform hashing block by block */
  19980. static int crypto_sha2_256(const uint8_t *buf, uint32_t len, uint8_t *hash,
  19981. uint32_t hashSz, uint32_t blkSz)
  19982. {
  19983. int ret;
  19984. uint32_t i = 0, chunk;
  19985. wc_Sha256 sha256;
  19986. /* validate arguments */
  19987. if ((buf == NULL && len > 0) || hash == NULL ||
  19988. hashSz < WC_SHA256_DIGEST_SIZE || blkSz == 0)
  19989. {
  19990. return BAD_FUNC_ARG;
  19991. }
  19992. /* Init Sha256 structure */
  19993. ret = wc_InitSha256(&sha256);
  19994. if (ret != 0) {
  19995. return ret;
  19996. }
  19997. while (i < len) {
  19998. chunk = blkSz;
  19999. if ((chunk + i) > len)
  20000. chunk = len - i;
  20001. /* Perform chunked update */
  20002. ret = wc_Sha256Update(&sha256, (buf + i), chunk);
  20003. if (ret != 0) {
  20004. break;
  20005. }
  20006. i += chunk;
  20007. }
  20008. if (ret == 0) {
  20009. /* Get final digest result */
  20010. ret = wc_Sha256Final(&sha256, hash);
  20011. }
  20012. return ret;
  20013. }
  20014. #endif
  20015. /* perform verify of signature and hash using public key */
  20016. /* key is public Qx + public Qy */
  20017. /* sig is r + s */
  20018. static int crypto_ecc_verify(const uint8_t *key, uint32_t keySz,
  20019. const uint8_t *hash, uint32_t hashSz, const uint8_t *sig, uint32_t sigSz,
  20020. uint32_t curveSz, int curveId)
  20021. {
  20022. int ret, verify_res = 0, count = 0;
  20023. mp_int r, s;
  20024. ecc_key ecc;
  20025. ecc_nb_ctx_t nb_ctx;
  20026. /* validate arguments */
  20027. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  20028. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  20029. {
  20030. return BAD_FUNC_ARG;
  20031. }
  20032. /* Setup the ECC key */
  20033. ret = wc_ecc_init(&ecc);
  20034. if (ret < 0) {
  20035. return ret;
  20036. }
  20037. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  20038. if (ret != MP_OKAY) {
  20039. wc_ecc_free(&ecc);
  20040. return ret;
  20041. }
  20042. /* Setup the signature r/s variables */
  20043. ret = mp_init(&r);
  20044. if (ret != MP_OKAY) {
  20045. wc_ecc_free(&ecc);
  20046. return ret;
  20047. }
  20048. ret = mp_init(&s);
  20049. if (ret != MP_OKAY) {
  20050. mp_clear(&r);
  20051. wc_ecc_free(&ecc);
  20052. return ret;
  20053. }
  20054. /* Import public key x/y */
  20055. ret = wc_ecc_import_unsigned(
  20056. &ecc,
  20057. (byte*)key, /* Public "x" Coordinate */
  20058. (byte*)(key + curveSz), /* Public "y" Coordinate */
  20059. NULL, /* Private "d" (optional) */
  20060. curveId /* ECC Curve Id */
  20061. );
  20062. /* Make sure it was a public key imported */
  20063. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  20064. ret = ECC_BAD_ARG_E;
  20065. }
  20066. /* Import signature r/s */
  20067. if (ret == 0) {
  20068. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  20069. }
  20070. if (ret == 0) {
  20071. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  20072. }
  20073. /* Verify ECC Signature */
  20074. if (ret == 0) {
  20075. do {
  20076. ret = wc_ecc_verify_hash_ex(
  20077. &r, &s, /* r/s as mp_int */
  20078. hash, hashSz, /* computed hash digest */
  20079. &verify_res, /* verification result 1=success */
  20080. &ecc
  20081. );
  20082. count++;
  20083. /* TODO: Real-time work can be called here */
  20084. } while (ret == FP_WOULDBLOCK);
  20085. #ifdef DEBUG_WOLFSSL
  20086. printf("ECC non-block verify: %d times\n", count);
  20087. #endif
  20088. }
  20089. /* check verify result */
  20090. if (ret == 0 && verify_res == 0) {
  20091. ret = SIG_VERIFY_E;
  20092. }
  20093. mp_clear(&r);
  20094. mp_clear(&s);
  20095. wc_ecc_free(&ecc);
  20096. (void)count;
  20097. return ret;
  20098. }
  20099. /* perform signature operation against hash using private key */
  20100. static int crypto_ecc_sign(const uint8_t *key, uint32_t keySz,
  20101. const uint8_t *hash, uint32_t hashSz, uint8_t *sig, uint32_t* sigSz,
  20102. uint32_t curveSz, int curveId, WC_RNG* rng)
  20103. {
  20104. int ret, count = 0;
  20105. mp_int r, s;
  20106. ecc_key ecc;
  20107. ecc_nb_ctx_t nb_ctx;
  20108. /* validate arguments */
  20109. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  20110. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  20111. {
  20112. return BAD_FUNC_ARG;
  20113. }
  20114. /* Initialize signature result */
  20115. memset(sig, 0, curveSz*2);
  20116. /* Setup the ECC key */
  20117. ret = wc_ecc_init(&ecc);
  20118. if (ret < 0) {
  20119. return ret;
  20120. }
  20121. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  20122. if (ret != MP_OKAY) {
  20123. wc_ecc_free(&ecc);
  20124. return ret;
  20125. }
  20126. /* Setup the signature r/s variables */
  20127. ret = mp_init(&r);
  20128. if (ret != MP_OKAY) {
  20129. wc_ecc_free(&ecc);
  20130. return ret;
  20131. }
  20132. ret = mp_init(&s);
  20133. if (ret != MP_OKAY) {
  20134. mp_clear(&r);
  20135. wc_ecc_free(&ecc);
  20136. return ret;
  20137. }
  20138. /* Import private key "k" */
  20139. ret = wc_ecc_import_private_key_ex(
  20140. key, keySz, /* private key "d" */
  20141. NULL, 0, /* public (optional) */
  20142. &ecc,
  20143. curveId /* ECC Curve Id */
  20144. );
  20145. if (ret == 0) {
  20146. do {
  20147. /* Verify ECC Signature */
  20148. ret = wc_ecc_sign_hash_ex(
  20149. hash, hashSz, /* computed hash digest */
  20150. rng, &ecc, /* random and key context */
  20151. &r, &s /* r/s as mp_int */
  20152. );
  20153. count++;
  20154. /* TODO: Real-time work can be called here */
  20155. } while (ret == FP_WOULDBLOCK);
  20156. #ifdef DEBUG_WOLFSSL
  20157. printf("ECC non-block sign: %d times\n", count);
  20158. #endif
  20159. }
  20160. if (ret == 0) {
  20161. /* export r/s */
  20162. mp_to_unsigned_bin(&r, sig);
  20163. mp_to_unsigned_bin(&s, sig + curveSz);
  20164. }
  20165. mp_clear(&r);
  20166. mp_clear(&s);
  20167. wc_ecc_free(&ecc);
  20168. (void)count;
  20169. return ret;
  20170. }
  20171. static int ecc_test_nonblock(WC_RNG* rng)
  20172. {
  20173. int ret;
  20174. uint8_t hash[HASH_DIGEST_SZ];
  20175. uint8_t sig[ECC_CURVE_SZ*2];
  20176. uint32_t sigSz = sizeof(sig);
  20177. ret = CRYPTO_HASH_FN(
  20178. kMsg, sizeof(kMsg), /* input message */
  20179. hash, sizeof(hash), /* hash digest result */
  20180. 32 /* configurable block / chunk size */
  20181. );
  20182. if (ret == 0) {
  20183. /* Sign hash using private key */
  20184. /* Note: result of an ECC sign varies for each call even with same
  20185. private key and hash. This is because a new random public key is
  20186. used for each operation. */
  20187. ret = crypto_ecc_sign(
  20188. kPrivKey, sizeof(kPrivKey), /* private key */
  20189. hash, sizeof(hash), /* computed hash digest */
  20190. sig, &sigSz, /* signature r/s */
  20191. ECC_CURVE_SZ, /* curve size in bytes */
  20192. ECC_CURVE_ID, /* curve id */
  20193. rng
  20194. );
  20195. }
  20196. if (ret == 0) {
  20197. /* Verify generated signature is valid */
  20198. ret = crypto_ecc_verify(
  20199. kPubKey, sizeof(kPubKey), /* public key point x/y */
  20200. hash, sizeof(hash), /* computed hash digest */
  20201. sig, sigSz, /* signature r/s */
  20202. ECC_CURVE_SZ, /* curve size in bytes */
  20203. ECC_CURVE_ID /* curve id */
  20204. );
  20205. }
  20206. return ret;
  20207. }
  20208. #endif /* WC_ECC_NONBLOCK && WOLFSSL_PUBLIC_MP && HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  20209. WOLFSSL_TEST_SUBROUTINE int ecc_test(void)
  20210. {
  20211. int ret;
  20212. WC_RNG rng;
  20213. #ifdef WOLFSSL_CERT_EXT
  20214. ret = ecc_decode_test();
  20215. if (ret < 0)
  20216. return ret;
  20217. #endif
  20218. #ifndef HAVE_FIPS
  20219. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20220. #else
  20221. ret = wc_InitRng(&rng);
  20222. #endif
  20223. #ifndef WC_NO_RNG
  20224. if (ret != 0)
  20225. return -10300;
  20226. #endif
  20227. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  20228. ret = ecc_test_curve(&rng, 14);
  20229. if (ret < 0) {
  20230. goto done;
  20231. }
  20232. #endif /* HAVE_ECC112 */
  20233. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  20234. ret = ecc_test_curve(&rng, 16);
  20235. if (ret < 0) {
  20236. goto done;
  20237. }
  20238. #endif /* HAVE_ECC128 */
  20239. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  20240. ret = ecc_test_curve(&rng, 20);
  20241. if (ret < 0) {
  20242. goto done;
  20243. }
  20244. #endif /* HAVE_ECC160 */
  20245. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  20246. ret = ecc_test_curve(&rng, 24);
  20247. if (ret < 0) {
  20248. goto done;
  20249. }
  20250. #endif /* HAVE_ECC192 */
  20251. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  20252. ret = ecc_test_curve(&rng, 28);
  20253. if (ret < 0) {
  20254. goto done;
  20255. }
  20256. #endif /* HAVE_ECC224 */
  20257. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  20258. ret = ecc_test_curve(&rng, 30);
  20259. if (ret < 0) {
  20260. goto done;
  20261. }
  20262. #endif /* HAVE_ECC239 */
  20263. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  20264. ret = ecc_test_curve(&rng, 32);
  20265. if (ret < 0) {
  20266. goto done;
  20267. }
  20268. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  20269. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  20270. !defined(WOLFSSL_NO_MALLOC)
  20271. ret = ecc_point_test();
  20272. if (ret < 0) {
  20273. goto done;
  20274. }
  20275. #endif
  20276. ret = ecc_def_curve_test(&rng);
  20277. if (ret < 0) {
  20278. goto done;
  20279. }
  20280. #endif /* !NO_ECC256 */
  20281. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  20282. ret = ecc_test_curve(&rng, 40);
  20283. if (ret < 0) {
  20284. goto done;
  20285. }
  20286. #endif /* HAVE_ECC320 */
  20287. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  20288. ret = ecc_test_curve(&rng, 48);
  20289. if (ret < 0) {
  20290. goto done;
  20291. }
  20292. #endif /* HAVE_ECC384 */
  20293. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  20294. ret = ecc_test_curve(&rng, 64);
  20295. if (ret < 0) {
  20296. goto done;
  20297. }
  20298. #endif /* HAVE_ECC512 */
  20299. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  20300. ret = ecc_test_curve(&rng, 66);
  20301. if (ret < 0) {
  20302. goto done;
  20303. }
  20304. #endif /* HAVE_ECC521 */
  20305. #if defined(WOLFSSL_CUSTOM_CURVES)
  20306. ret = ecc_test_custom_curves(&rng);
  20307. if (ret != 0) {
  20308. goto done;
  20309. }
  20310. #endif
  20311. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K)
  20312. ret = ecc_test_sign_vectors(&rng);
  20313. if (ret != 0) {
  20314. printf("ecc_test_sign_vectors failed! %d\n", ret);
  20315. goto done;
  20316. }
  20317. #endif
  20318. #ifdef HAVE_ECC_CDH
  20319. ret = ecc_test_cdh_vectors(&rng);
  20320. if (ret != 0) {
  20321. printf("ecc_test_cdh_vectors failed! %d\n", ret);
  20322. goto done;
  20323. }
  20324. #endif
  20325. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  20326. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL)
  20327. ret = ecc_test_make_pub(&rng);
  20328. if (ret != 0) {
  20329. printf("ecc_test_make_pub failed!: %d\n", ret);
  20330. goto done;
  20331. }
  20332. #elif defined(HAVE_ECC_KEY_IMPORT)
  20333. (void) ecc_test_make_pub;/* for compiler warning */
  20334. #endif
  20335. #ifdef WOLFSSL_CERT_GEN
  20336. ret = ecc_test_cert_gen(&rng);
  20337. if (ret != 0) {
  20338. printf("ecc_test_cert_gen failed!: %d\n", ret);
  20339. goto done;
  20340. }
  20341. #endif
  20342. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  20343. ret = ecc_test_allocator(&rng);
  20344. if (ret != 0) {
  20345. printf("ecc_test_allocator failed!: %d\n", ret);
  20346. goto done;
  20347. }
  20348. #endif
  20349. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  20350. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  20351. ret = ecc_test_nonblock(&rng);
  20352. if (ret != 0) {
  20353. printf("ecc_test_nonblock failed!: %d\n", ret);
  20354. goto done;
  20355. }
  20356. #endif
  20357. done:
  20358. wc_FreeRng(&rng);
  20359. return ret;
  20360. }
  20361. #if defined(HAVE_ECC_ENCRYPT) && defined(WOLFSSL_AES_128)
  20362. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void)
  20363. {
  20364. WC_RNG rng;
  20365. int ret = 0;
  20366. #ifdef WOLFSSL_SMALL_STACK
  20367. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  20368. *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20369. #else
  20370. ecc_key userA[1], userB[1];
  20371. #endif
  20372. byte msg[48];
  20373. byte plain[48];
  20374. byte out[80];
  20375. word32 outSz = sizeof(out);
  20376. word32 plainSz = sizeof(plain);
  20377. int i;
  20378. ecEncCtx* cliCtx = NULL;
  20379. ecEncCtx* srvCtx = NULL;
  20380. byte cliSalt[EXCHANGE_SALT_SZ];
  20381. byte srvSalt[EXCHANGE_SALT_SZ];
  20382. const byte* tmpSalt;
  20383. byte msg2[48];
  20384. byte plain2[48];
  20385. byte out2[80];
  20386. word32 outSz2 = sizeof(out2);
  20387. word32 plainSz2 = sizeof(plain2);
  20388. #ifndef HAVE_FIPS
  20389. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20390. #else
  20391. ret = wc_InitRng(&rng);
  20392. #endif
  20393. if (ret != 0)
  20394. return -10400;
  20395. #ifdef WOLFSSL_SMALL_STACK
  20396. if ((userA == NULL) ||
  20397. (userB == NULL))
  20398. ERROR_OUT(MEMORY_E, done);
  20399. #endif
  20400. XMEMSET(userA, 0, sizeof *userA);
  20401. XMEMSET(userB, 0, sizeof *userB);
  20402. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  20403. if (ret != 0)
  20404. goto done;
  20405. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  20406. if (ret != 0)
  20407. goto done;
  20408. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  20409. #if defined(WOLFSSL_ASYNC_CRYPT)
  20410. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  20411. #endif
  20412. if (ret != 0){
  20413. ret = -10401; goto done;
  20414. }
  20415. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  20416. #if defined(WOLFSSL_ASYNC_CRYPT)
  20417. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  20418. #endif
  20419. if (ret != 0){
  20420. ret = -10402; goto done;
  20421. }
  20422. /* set message to incrementing 0,1,2,etc... */
  20423. for (i = 0; i < (int)sizeof(msg); i++)
  20424. msg[i] = i;
  20425. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20426. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20427. !defined(HAVE_SELFTEST)
  20428. ret = wc_ecc_set_rng(userA, &rng);
  20429. if (ret != 0) {
  20430. ret = -10403; goto done;
  20431. }
  20432. ret = wc_ecc_set_rng(userB, &rng);
  20433. if (ret != 0) {
  20434. ret = -10404; goto done;
  20435. }
  20436. #endif
  20437. /* encrypt msg to B */
  20438. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  20439. if (ret != 0) {
  20440. ret = -10405; goto done;
  20441. }
  20442. /* decrypt msg from A */
  20443. ret = wc_ecc_decrypt(userB, userA, out, outSz, plain, &plainSz, NULL);
  20444. if (ret != 0) {
  20445. ret = -10406; goto done;
  20446. }
  20447. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  20448. ret = -10407; goto done;
  20449. }
  20450. /* let's verify message exchange works, A is client, B is server */
  20451. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  20452. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  20453. if (cliCtx == NULL || srvCtx == NULL) {
  20454. ret = -10408; goto done;
  20455. }
  20456. /* get salt to send to peer */
  20457. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  20458. if (tmpSalt == NULL) {
  20459. ret = -10409; goto done;
  20460. }
  20461. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  20462. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  20463. if (tmpSalt == NULL) {
  20464. ret = -10410; goto done;
  20465. }
  20466. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  20467. /* in actual use, we'd get the peer's salt over the transport */
  20468. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  20469. if (ret != 0)
  20470. goto done;
  20471. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  20472. if (ret != 0)
  20473. goto done;
  20474. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  20475. if (ret != 0)
  20476. goto done;
  20477. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  20478. if (ret != 0)
  20479. goto done;
  20480. /* get encrypted msg (request) to send to B */
  20481. outSz = sizeof(out);
  20482. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  20483. if (ret != 0)
  20484. goto done;
  20485. /* B decrypts msg (request) from A */
  20486. plainSz = sizeof(plain);
  20487. ret = wc_ecc_decrypt(userB, userA, out, outSz, plain, &plainSz, srvCtx);
  20488. if (ret != 0)
  20489. goto done;
  20490. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  20491. ret = -10411; goto done;
  20492. }
  20493. /* msg2 (response) from B to A */
  20494. for (i = 0; i < (int)sizeof(msg2); i++)
  20495. msg2[i] = i + sizeof(msg2);
  20496. /* get encrypted msg (response) to send to B */
  20497. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  20498. &outSz2, srvCtx);
  20499. if (ret != 0)
  20500. goto done;
  20501. /* A decrypts msg (response) from B */
  20502. ret = wc_ecc_decrypt(userA, userB, out2, outSz2, plain2, &plainSz2,
  20503. cliCtx);
  20504. if (ret != 0)
  20505. goto done;
  20506. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  20507. ret = -10412; goto done;
  20508. }
  20509. done:
  20510. /* cleanup */
  20511. wc_ecc_ctx_free(srvCtx);
  20512. wc_ecc_ctx_free(cliCtx);
  20513. #ifdef WOLFSSL_SMALL_STACK
  20514. if (userA != NULL) {
  20515. wc_ecc_free(userA);
  20516. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20517. }
  20518. if (userB != NULL) {
  20519. wc_ecc_free(userB);
  20520. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20521. }
  20522. #else
  20523. wc_ecc_free(userB);
  20524. wc_ecc_free(userA);
  20525. #endif
  20526. wc_FreeRng(&rng);
  20527. return ret;
  20528. }
  20529. #endif /* HAVE_ECC_ENCRYPT */
  20530. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  20531. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  20532. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN)
  20533. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void)
  20534. {
  20535. size_t bytes;
  20536. #ifdef WOLFSSL_SMALL_STACK
  20537. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20538. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20539. #else
  20540. ecc_key cliKey[1];
  20541. ecc_key servKey[1];
  20542. #endif
  20543. WC_RNG rng;
  20544. word32 idx = 0;
  20545. int ret;
  20546. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  20547. byte in[] = "Everyone gets Friday off. ecc p";
  20548. word32 inLen = (word32)XSTRLEN((char*)in);
  20549. byte out[256];
  20550. byte plain[256];
  20551. int verify = 0;
  20552. word32 x;
  20553. #ifdef WOLFSSL_SMALL_STACK
  20554. if ((cliKey == NULL) || (servKey == NULL))
  20555. ERROR_OUT(MEMORY_E, done);
  20556. #endif
  20557. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  20558. if (ret != 0)
  20559. ERROR_OUT(-10420, done);
  20560. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  20561. if (ret != 0)
  20562. ERROR_OUT(-10421, done);
  20563. bytes = (size_t)sizeof_ecc_clikey_der_256;
  20564. /* place client key into ecc_key struct cliKey */
  20565. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  20566. (word32)bytes);
  20567. if (ret != 0)
  20568. ERROR_OUT(-10422, done);
  20569. idx = 0;
  20570. bytes = (size_t)sizeof_ecc_key_der_256;
  20571. /* place server key into ecc_key struct servKey */
  20572. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  20573. (word32)bytes);
  20574. if (ret != 0)
  20575. ERROR_OUT(-10423, done);
  20576. #ifndef HAVE_FIPS
  20577. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20578. #else
  20579. ret = wc_InitRng(&rng);
  20580. #endif
  20581. if (ret != 0)
  20582. ERROR_OUT(-10424, done);
  20583. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20584. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20585. !defined(HAVE_SELFTEST)
  20586. ret = wc_ecc_set_rng(cliKey, &rng);
  20587. if (ret != 0) {
  20588. ERROR_OUT(-10425, done);
  20589. }
  20590. #endif
  20591. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF)
  20592. {
  20593. word32 y;
  20594. /* test encrypt and decrypt if they're available */
  20595. x = sizeof(out);
  20596. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  20597. if (ret < 0)
  20598. ERROR_OUT(-10426, done);
  20599. y = sizeof(plain);
  20600. ret = wc_ecc_decrypt(cliKey, servKey, out, x, plain, &y, NULL);
  20601. if (ret < 0)
  20602. ERROR_OUT(-10427, done);
  20603. if (XMEMCMP(plain, in, inLen))
  20604. ERROR_OUT(-10428, done);
  20605. }
  20606. #endif
  20607. x = sizeof(out);
  20608. do {
  20609. #if defined(WOLFSSL_ASYNC_CRYPT)
  20610. ret = wc_AsyncWait(ret, cliKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20611. #endif
  20612. if (ret == 0)
  20613. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  20614. } while (ret == WC_PENDING_E);
  20615. if (ret < 0)
  20616. ERROR_OUT(-10429, done);
  20617. TEST_SLEEP();
  20618. XMEMSET(plain, 0, sizeof(plain));
  20619. do {
  20620. #if defined(WOLFSSL_ASYNC_CRYPT)
  20621. ret = wc_AsyncWait(ret, cliKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20622. #endif
  20623. if (ret == 0)
  20624. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  20625. cliKey);
  20626. } while (ret == WC_PENDING_E);
  20627. if (ret < 0)
  20628. ERROR_OUT(-10430, done);
  20629. if (verify != 1)
  20630. ERROR_OUT(-10431, done);
  20631. TEST_SLEEP();
  20632. #ifdef WOLFSSL_CERT_EXT
  20633. idx = 0;
  20634. bytes = sizeof_ecc_clikeypub_der_256;
  20635. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  20636. (word32) bytes);
  20637. if (ret != 0)
  20638. ERROR_OUT(-10432, done);
  20639. #endif
  20640. ret = 0;
  20641. done:
  20642. #ifdef WOLFSSL_SMALL_STACK
  20643. if (cliKey != NULL) {
  20644. wc_ecc_free(cliKey);
  20645. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20646. }
  20647. if (servKey != NULL) {
  20648. wc_ecc_free(servKey);
  20649. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20650. }
  20651. #else
  20652. wc_ecc_free(cliKey);
  20653. wc_ecc_free(servKey);
  20654. #endif
  20655. wc_FreeRng(&rng);
  20656. return ret;
  20657. }
  20658. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  20659. #endif /* HAVE_ECC */
  20660. #ifdef HAVE_CURVE25519
  20661. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  20662. defined(HAVE_CURVE25519_KEY_IMPORT)
  20663. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  20664. #define X25519_TEST_CNT 5
  20665. #else
  20666. #define X25519_TEST_CNT 1
  20667. #endif
  20668. static int curve25519_overflow_test(void)
  20669. {
  20670. /* secret key for party a */
  20671. byte sa[X25519_TEST_CNT][32] = {
  20672. {
  20673. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  20674. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  20675. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  20676. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  20677. },
  20678. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  20679. {
  20680. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  20681. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  20682. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  20683. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  20684. },
  20685. {
  20686. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  20687. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  20688. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  20689. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  20690. },
  20691. {
  20692. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  20693. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  20694. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  20695. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  20696. },
  20697. {
  20698. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  20699. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  20700. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  20701. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  20702. }
  20703. #endif
  20704. };
  20705. /* public key for party b */
  20706. byte pb[X25519_TEST_CNT][32] = {
  20707. {
  20708. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  20709. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  20710. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  20711. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  20712. },
  20713. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  20714. {
  20715. /* 0xff first byte in original - invalid! */
  20716. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  20717. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  20718. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  20719. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  20720. },
  20721. {
  20722. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  20723. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  20724. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  20725. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  20726. },
  20727. {
  20728. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  20729. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  20730. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  20731. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  20732. },
  20733. {
  20734. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  20735. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  20736. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  20737. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  20738. }
  20739. #endif
  20740. };
  20741. /* expected shared key */
  20742. byte ss[X25519_TEST_CNT][32] = {
  20743. {
  20744. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  20745. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  20746. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  20747. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  20748. },
  20749. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  20750. {
  20751. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  20752. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  20753. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  20754. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  20755. },
  20756. {
  20757. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20758. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20759. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20760. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  20761. },
  20762. {
  20763. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20764. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20765. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20766. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  20767. },
  20768. {
  20769. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20770. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20771. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20772. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  20773. }
  20774. #endif
  20775. };
  20776. int i;
  20777. word32 y;
  20778. byte shared[32];
  20779. curve25519_key userA;
  20780. wc_curve25519_init(&userA);
  20781. for (i = 0; i < X25519_TEST_CNT; i++) {
  20782. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  20783. sizeof(pb[i]), &userA) != 0)
  20784. return -10500 - i;
  20785. /* test against known test vector */
  20786. XMEMSET(shared, 0, sizeof(shared));
  20787. y = sizeof(shared);
  20788. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0)
  20789. return -10510 - i;
  20790. if (XMEMCMP(ss[i], shared, y))
  20791. return -10520 - i;
  20792. }
  20793. return 0;
  20794. }
  20795. /* Test the wc_curve25519_check_public API.
  20796. *
  20797. * returns 0 on success and -ve on failure.
  20798. */
  20799. static int curve25519_check_public_test(void)
  20800. {
  20801. /* Little-endian values that will fail */
  20802. byte fail_le[][CURVE25519_KEYSIZE] = {
  20803. {
  20804. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20805. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20806. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20807. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  20808. },
  20809. {
  20810. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20811. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20812. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20813. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  20814. },
  20815. {
  20816. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20817. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20818. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20819. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  20820. },
  20821. };
  20822. /* Big-endian values that will fail */
  20823. byte fail_be[][CURVE25519_KEYSIZE] = {
  20824. {
  20825. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20826. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20827. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20828. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  20829. },
  20830. {
  20831. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20832. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20833. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20834. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  20835. },
  20836. {
  20837. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20838. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20839. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20840. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  20841. },
  20842. };
  20843. /* Good or valid public value */
  20844. byte good[CURVE25519_KEYSIZE] = {
  20845. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20846. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20847. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20848. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  20849. };
  20850. int i;
  20851. /* Parameter checks */
  20852. /* NULL pointer */
  20853. if (wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN) !=
  20854. BAD_FUNC_ARG) {
  20855. return -10600;
  20856. }
  20857. if (wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN) !=
  20858. BAD_FUNC_ARG) {
  20859. return -10601;
  20860. }
  20861. /* Length of 0 treated differently to other invalid lengths for TLS */
  20862. if (wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN) != BUFFER_E)
  20863. return -10602;
  20864. if (wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN) != BUFFER_E)
  20865. return -10603;
  20866. /* Length not CURVE25519_KEYSIZE */
  20867. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  20868. if (i == CURVE25519_KEYSIZE)
  20869. continue;
  20870. if (wc_curve25519_check_public(good, i, EC25519_LITTLE_ENDIAN) !=
  20871. ECC_BAD_ARG_E) {
  20872. return -10604 - i;
  20873. }
  20874. if (wc_curve25519_check_public(good, i, EC25519_BIG_ENDIAN) !=
  20875. ECC_BAD_ARG_E) {
  20876. return -10614 - i;
  20877. }
  20878. }
  20879. /* Little-endian fail cases */
  20880. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  20881. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  20882. EC25519_LITTLE_ENDIAN) == 0) {
  20883. return -10624 - i;
  20884. }
  20885. }
  20886. /* Big-endian fail cases */
  20887. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  20888. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  20889. EC25519_BIG_ENDIAN) == 0) {
  20890. return -10634 - i;
  20891. }
  20892. }
  20893. /* Check a valid public value works! */
  20894. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  20895. EC25519_LITTLE_ENDIAN) != 0) {
  20896. return -10644;
  20897. }
  20898. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  20899. EC25519_BIG_ENDIAN) != 0) {
  20900. return -10645;
  20901. }
  20902. return 0;
  20903. }
  20904. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  20905. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void)
  20906. {
  20907. WC_RNG rng;
  20908. int ret;
  20909. #ifdef HAVE_CURVE25519_SHARED_SECRET
  20910. byte sharedA[32];
  20911. byte sharedB[32];
  20912. word32 y;
  20913. #endif
  20914. #ifdef HAVE_CURVE25519_KEY_EXPORT
  20915. byte exportBuf[32];
  20916. #endif
  20917. word32 x = 0;
  20918. curve25519_key userA, userB, pubKey;
  20919. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  20920. defined(HAVE_CURVE25519_KEY_IMPORT)
  20921. /* test vectors from
  20922. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  20923. */
  20924. /* secret key for party a */
  20925. byte sa[] = {
  20926. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  20927. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  20928. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  20929. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  20930. };
  20931. /* public key for party a */
  20932. byte pa[] = {
  20933. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  20934. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  20935. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  20936. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  20937. };
  20938. /* secret key for party b */
  20939. byte sb[] = {
  20940. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  20941. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  20942. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  20943. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  20944. };
  20945. /* public key for party b */
  20946. byte pb[] = {
  20947. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  20948. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  20949. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  20950. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  20951. };
  20952. /* expected shared key */
  20953. byte ss[] = {
  20954. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  20955. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  20956. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  20957. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  20958. };
  20959. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  20960. (void)x;
  20961. #ifndef HAVE_FIPS
  20962. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20963. #else
  20964. ret = wc_InitRng(&rng);
  20965. #endif
  20966. if (ret != 0)
  20967. return -10700;
  20968. wc_curve25519_init(&userA);
  20969. wc_curve25519_init(&userB);
  20970. wc_curve25519_init(&pubKey);
  20971. /* make curve25519 keys */
  20972. if (wc_curve25519_make_key(&rng, 32, &userA) != 0)
  20973. return -10701;
  20974. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  20975. return -10702;
  20976. #ifdef HAVE_CURVE25519_SHARED_SECRET
  20977. /* find shared secret key */
  20978. x = sizeof(sharedA);
  20979. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  20980. return -10703;
  20981. y = sizeof(sharedB);
  20982. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  20983. return -10704;
  20984. /* compare shared secret keys to test they are the same */
  20985. if (y != x)
  20986. return -10705;
  20987. if (XMEMCMP(sharedA, sharedB, x))
  20988. return -10706;
  20989. #endif
  20990. #ifdef HAVE_CURVE25519_KEY_EXPORT
  20991. /* export a public key and import it for another user */
  20992. x = sizeof(exportBuf);
  20993. if (wc_curve25519_export_public(&userA, exportBuf, &x) != 0)
  20994. return -10707;
  20995. #ifdef HAVE_CURVE25519_KEY_IMPORT
  20996. if (wc_curve25519_import_public(exportBuf, x, &pubKey) != 0)
  20997. return -10708;
  20998. #endif
  20999. #endif
  21000. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  21001. defined(HAVE_CURVE25519_KEY_IMPORT)
  21002. /* test shared key after importing a public key */
  21003. XMEMSET(sharedB, 0, sizeof(sharedB));
  21004. y = sizeof(sharedB);
  21005. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  21006. return -10709;
  21007. if (XMEMCMP(sharedA, sharedB, y))
  21008. return -10710;
  21009. /* import RFC test vectors and compare shared key */
  21010. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  21011. != 0)
  21012. return -10711;
  21013. if (wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  21014. != 0)
  21015. return -10712;
  21016. /* test against known test vector */
  21017. XMEMSET(sharedB, 0, sizeof(sharedB));
  21018. y = sizeof(sharedB);
  21019. if (wc_curve25519_shared_secret(&userA, &userB, sharedB, &y) != 0)
  21020. return -10713;
  21021. if (XMEMCMP(ss, sharedB, y))
  21022. return -10714;
  21023. /* test swapping roles of keys and generating same shared key */
  21024. XMEMSET(sharedB, 0, sizeof(sharedB));
  21025. y = sizeof(sharedB);
  21026. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  21027. return -10715;
  21028. if (XMEMCMP(ss, sharedB, y))
  21029. return -10716;
  21030. /* test with 1 generated key and 1 from known test vector */
  21031. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  21032. != 0)
  21033. return -10717;
  21034. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  21035. return -10718;
  21036. x = sizeof(sharedA);
  21037. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  21038. return -10719;
  21039. y = sizeof(sharedB);
  21040. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  21041. return -10720;
  21042. /* compare shared secret keys to test they are the same */
  21043. if (y != x)
  21044. return -10721;
  21045. if (XMEMCMP(sharedA, sharedB, x))
  21046. return -10722;
  21047. ret = curve25519_overflow_test();
  21048. if (ret != 0)
  21049. return ret;
  21050. ret = curve25519_check_public_test();
  21051. if (ret != 0)
  21052. return ret;
  21053. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  21054. /* clean up keys when done */
  21055. wc_curve25519_free(&pubKey);
  21056. wc_curve25519_free(&userB);
  21057. wc_curve25519_free(&userA);
  21058. wc_FreeRng(&rng);
  21059. return 0;
  21060. }
  21061. #endif /* HAVE_CURVE25519 */
  21062. #ifdef HAVE_ED25519
  21063. #ifdef WOLFSSL_TEST_CERT
  21064. static int ed25519_test_cert(void)
  21065. {
  21066. DecodedCert cert[2];
  21067. DecodedCert* serverCert = NULL;
  21068. DecodedCert* caCert = NULL;
  21069. #ifdef HAVE_ED25519_VERIFY
  21070. ed25519_key key;
  21071. ed25519_key* pubKey = NULL;
  21072. int verify;
  21073. #endif /* HAVE_ED25519_VERIFY */
  21074. int ret;
  21075. byte* tmp;
  21076. size_t bytes;
  21077. XFILE file;
  21078. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21079. if (tmp == NULL) {
  21080. ERROR_OUT(-10730, done);
  21081. }
  21082. #ifdef USE_CERT_BUFFERS_256
  21083. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  21084. bytes = sizeof_ca_ed25519_cert;
  21085. #elif !defined(NO_FILESYSTEM)
  21086. file = XFOPEN(caEd25519Cert, "rb");
  21087. if (file == NULL) {
  21088. ERROR_OUT(-10731, done);
  21089. }
  21090. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  21091. XFCLOSE(file);
  21092. #else
  21093. /* No certificate to use. */
  21094. ERROR_OUT(-10732, done);
  21095. #endif
  21096. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  21097. caCert = &cert[0];
  21098. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  21099. if (ret != 0) {
  21100. ERROR_OUT(-10733, done);
  21101. }
  21102. #ifdef USE_CERT_BUFFERS_256
  21103. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  21104. bytes = sizeof_server_ed25519_cert;
  21105. #elif !defined(NO_FILESYSTEM)
  21106. file = XFOPEN(serverEd25519Cert, "rb");
  21107. if (file == NULL) {
  21108. ERROR_OUT(-10734, done);
  21109. }
  21110. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  21111. XFCLOSE(file);
  21112. #else
  21113. /* No certificate to use. */
  21114. ERROR_OUT(-10735, done);
  21115. #endif
  21116. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  21117. serverCert = &cert[1];
  21118. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  21119. if (ret != 0) {
  21120. ERROR_OUT(-10736, done);
  21121. }
  21122. #ifdef HAVE_ED25519_VERIFY
  21123. ret = wc_ed25519_init(&key);
  21124. if (ret < 0) {
  21125. ERROR_OUT(-10737, done);
  21126. }
  21127. pubKey = &key;
  21128. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  21129. pubKey);
  21130. if (ret < 0) {
  21131. ERROR_OUT(-10738, done);
  21132. }
  21133. if (wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  21134. serverCert->source + serverCert->certBegin,
  21135. serverCert->sigIndex - serverCert->certBegin,
  21136. &verify, pubKey) < 0 || verify != 1) {
  21137. ERROR_OUT(-10739, done);
  21138. }
  21139. #endif /* HAVE_ED25519_VERIFY */
  21140. done:
  21141. if (tmp != NULL)
  21142. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21143. #ifdef HAVE_ED25519_VERIFY
  21144. wc_ed25519_free(pubKey);
  21145. #endif /* HAVE_ED25519_VERIFY */
  21146. if (caCert != NULL)
  21147. FreeDecodedCert(caCert);
  21148. if (serverCert != NULL)
  21149. FreeDecodedCert(serverCert);
  21150. return ret;
  21151. }
  21152. static int ed25519_test_make_cert(void)
  21153. {
  21154. WC_RNG rng;
  21155. Cert cert;
  21156. DecodedCert decode;
  21157. ed25519_key key;
  21158. ed25519_key* privKey = NULL;
  21159. int ret = 0;
  21160. byte* tmp = NULL;
  21161. wc_InitCert(&cert);
  21162. #ifndef HAVE_FIPS
  21163. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  21164. #else
  21165. ret = wc_InitRng(&rng);
  21166. #endif
  21167. if (ret != 0)
  21168. return -10750;
  21169. wc_ed25519_init(&key);
  21170. privKey = &key;
  21171. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  21172. cert.daysValid = 365 * 2;
  21173. cert.selfSigned = 1;
  21174. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  21175. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  21176. cert.isCA = 0;
  21177. #ifdef WOLFSSL_CERT_EXT
  21178. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  21179. if (ret < 0) {
  21180. ERROR_OUT(-10751, done);
  21181. }
  21182. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  21183. if (ret < 0) {
  21184. ERROR_OUT(-10752, done);
  21185. }
  21186. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  21187. if (ret < 0) {
  21188. ERROR_OUT(-10753, done);
  21189. }
  21190. #endif
  21191. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21192. if (tmp == NULL) {
  21193. ERROR_OUT(-10754, done);
  21194. }
  21195. cert.sigType = CTC_ED25519;
  21196. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  21197. if (ret < 0) {
  21198. ERROR_OUT(-10755, done);
  21199. }
  21200. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  21201. ED25519_TYPE, privKey, &rng);
  21202. if (ret < 0) {
  21203. ERROR_OUT(-10756, done);
  21204. }
  21205. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  21206. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  21207. FreeDecodedCert(&decode);
  21208. if (ret != 0) {
  21209. ERROR_OUT(-10757, done);
  21210. }
  21211. done:
  21212. if (tmp != NULL)
  21213. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21214. wc_ed25519_free(privKey);
  21215. wc_FreeRng(&rng);
  21216. return ret;
  21217. }
  21218. #endif /* WOLFSSL_TEST_CERT */
  21219. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  21220. defined(HAVE_ED25519_KEY_IMPORT)
  21221. static int ed25519ctx_test(void)
  21222. {
  21223. byte out[ED25519_SIG_SIZE];
  21224. word32 outlen;
  21225. #ifdef HAVE_ED25519_VERIFY
  21226. int verify;
  21227. #endif /* HAVE_ED25519_VERIFY */
  21228. ed25519_key key;
  21229. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  21230. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  21231. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  21232. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  21233. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  21234. };
  21235. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  21236. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  21237. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  21238. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  21239. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  21240. };
  21241. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  21242. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  21243. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  21244. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  21245. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  21246. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  21247. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  21248. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  21249. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  21250. };
  21251. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  21252. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  21253. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  21254. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  21255. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  21256. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  21257. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  21258. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  21259. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  21260. };
  21261. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  21262. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  21263. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  21264. };
  21265. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  21266. 0x66,0x6f,0x6f
  21267. };
  21268. outlen = sizeof(out);
  21269. XMEMSET(out, 0, sizeof(out));
  21270. if (wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  21271. sizeof(pKeyCtx), &key) != 0)
  21272. return -10800;
  21273. if (wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  21274. contextCtx, sizeof(contextCtx)) != 0)
  21275. return -10801;
  21276. if (XMEMCMP(out, sigCtx1, 64))
  21277. return -10802;
  21278. #if defined(HAVE_ED25519_VERIFY)
  21279. /* test verify on good msg */
  21280. if (wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify,
  21281. &key, contextCtx, sizeof(contextCtx)) != 0 ||
  21282. verify != 1)
  21283. return -10803;
  21284. #endif
  21285. if (wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key, NULL,
  21286. 0) != 0)
  21287. return -10804;
  21288. if (XMEMCMP(out, sigCtx2, 64))
  21289. return -10805;
  21290. #if defined(HAVE_ED25519_VERIFY)
  21291. /* test verify on good msg */
  21292. if (wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify,
  21293. &key, NULL, 0) != 0 || verify != 1)
  21294. return -10806;
  21295. #endif
  21296. wc_ed25519_free(&key);
  21297. return 0;
  21298. }
  21299. static int ed25519ph_test(void)
  21300. {
  21301. byte out[ED25519_SIG_SIZE];
  21302. word32 outlen;
  21303. #ifdef HAVE_ED25519_VERIFY
  21304. int verify;
  21305. #endif /* HAVE_ED25519_VERIFY */
  21306. ed25519_key key;
  21307. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  21308. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  21309. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  21310. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  21311. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  21312. };
  21313. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  21314. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  21315. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  21316. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  21317. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  21318. };
  21319. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  21320. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  21321. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  21322. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  21323. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  21324. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  21325. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  21326. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  21327. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  21328. };
  21329. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  21330. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  21331. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  21332. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  21333. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  21334. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  21335. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  21336. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  21337. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  21338. };
  21339. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  21340. 0x61,0x62,0x63
  21341. };
  21342. /* SHA-512 hash of msgPh */
  21343. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  21344. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  21345. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  21346. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  21347. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  21348. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  21349. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  21350. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  21351. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  21352. };
  21353. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  21354. 0x66,0x6f,0x6f
  21355. };
  21356. outlen = sizeof(out);
  21357. XMEMSET(out, 0, sizeof(out));
  21358. if (wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  21359. sizeof(pKeyPh), &key) != 0) {
  21360. return -10900;
  21361. }
  21362. if (wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  21363. 0) != 0) {
  21364. return -10901;
  21365. }
  21366. if (XMEMCMP(out, sigPh1, 64))
  21367. return -10902;
  21368. #if defined(HAVE_ED25519_VERIFY)
  21369. /* test verify on good msg */
  21370. if (wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  21371. &key, NULL, 0) != 0 ||
  21372. verify != 1) {
  21373. return -10903;
  21374. }
  21375. #endif
  21376. if (wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  21377. contextPh2, sizeof(contextPh2)) != 0) {
  21378. return -10904;
  21379. }
  21380. if (XMEMCMP(out, sigPh2, 64))
  21381. return -10905;
  21382. #if defined(HAVE_ED25519_VERIFY)
  21383. /* test verify on good msg */
  21384. if (wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  21385. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  21386. verify != 1) {
  21387. return -10906;
  21388. }
  21389. #endif
  21390. if (wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  21391. 0) != 0) {
  21392. return -10907;
  21393. }
  21394. if (XMEMCMP(out, sigPh1, 64))
  21395. return -10908;
  21396. #if defined(HAVE_ED25519_VERIFY)
  21397. if (wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  21398. &key, NULL, 0) != 0 ||
  21399. verify != 1) {
  21400. return -10909;
  21401. }
  21402. #endif
  21403. if (wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  21404. contextPh2, sizeof(contextPh2)) != 0) {
  21405. return -10910;
  21406. }
  21407. if (XMEMCMP(out, sigPh2, 64))
  21408. return -10911;
  21409. #if defined(HAVE_ED25519_VERIFY)
  21410. if (wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  21411. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  21412. verify != 1) {
  21413. return -10912;
  21414. }
  21415. #endif
  21416. wc_ed25519_free(&key);
  21417. return 0;
  21418. }
  21419. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  21420. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void)
  21421. {
  21422. int ret;
  21423. WC_RNG rng;
  21424. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  21425. defined(HAVE_ED25519_KEY_IMPORT)
  21426. byte out[ED25519_SIG_SIZE];
  21427. byte exportPKey[ED25519_KEY_SIZE];
  21428. byte exportSKey[ED25519_KEY_SIZE];
  21429. word32 exportPSz;
  21430. word32 exportSSz;
  21431. int i;
  21432. word32 outlen;
  21433. #ifdef HAVE_ED25519_VERIFY
  21434. int verify;
  21435. #endif /* HAVE_ED25519_VERIFY */
  21436. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  21437. word32 keySz, sigSz;
  21438. ed25519_key key;
  21439. ed25519_key key2;
  21440. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  21441. defined(HAVE_ED25519_KEY_IMPORT)
  21442. /* test vectors from
  21443. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  21444. */
  21445. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  21446. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  21447. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  21448. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  21449. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  21450. };
  21451. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  21452. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  21453. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  21454. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  21455. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  21456. };
  21457. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  21458. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  21459. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  21460. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  21461. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  21462. };
  21463. /* uncompressed test */
  21464. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  21465. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  21466. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  21467. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  21468. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  21469. };
  21470. /* compressed prefix test */
  21471. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  21472. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  21473. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  21474. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  21475. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  21476. };
  21477. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  21478. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  21479. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  21480. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  21481. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  21482. };
  21483. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  21484. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  21485. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  21486. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  21487. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  21488. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  21489. };
  21490. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  21491. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  21492. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  21493. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  21494. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  21495. };
  21496. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  21497. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  21498. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  21499. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  21500. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  21501. };
  21502. /* uncompressed test */
  21503. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  21504. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  21505. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  21506. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  21507. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  21508. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  21509. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  21510. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  21511. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  21512. 0xd7
  21513. };
  21514. /* compressed prefix */
  21515. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  21516. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  21517. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  21518. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  21519. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  21520. };
  21521. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  21522. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  21523. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  21524. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  21525. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  21526. };
  21527. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  21528. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  21529. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  21530. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  21531. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  21532. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  21533. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  21534. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  21535. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  21536. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  21537. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  21538. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  21539. };
  21540. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  21541. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  21542. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  21543. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  21544. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  21545. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  21546. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  21547. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  21548. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  21549. };
  21550. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  21551. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  21552. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  21553. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  21554. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  21555. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  21556. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  21557. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  21558. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  21559. };
  21560. /* uncompressed test */
  21561. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  21562. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  21563. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  21564. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  21565. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  21566. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  21567. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  21568. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  21569. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  21570. };
  21571. /* compressed prefix */
  21572. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  21573. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  21574. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  21575. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  21576. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  21577. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  21578. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  21579. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  21580. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  21581. };
  21582. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  21583. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  21584. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  21585. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  21586. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  21587. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  21588. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  21589. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  21590. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  21591. };
  21592. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  21593. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  21594. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  21595. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  21596. /* test of a 1024 byte long message */
  21597. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  21598. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  21599. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  21600. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  21601. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  21602. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  21603. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  21604. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  21605. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  21606. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  21607. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  21608. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  21609. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  21610. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  21611. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  21612. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  21613. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  21614. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  21615. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  21616. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  21617. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  21618. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  21619. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  21620. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  21621. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  21622. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  21623. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  21624. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  21625. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  21626. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  21627. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  21628. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  21629. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  21630. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  21631. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  21632. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  21633. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  21634. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  21635. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  21636. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  21637. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  21638. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  21639. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  21640. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  21641. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  21642. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  21643. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  21644. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  21645. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  21646. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  21647. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  21648. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  21649. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  21650. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  21651. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  21652. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  21653. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  21654. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  21655. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  21656. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  21657. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  21658. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  21659. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  21660. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  21661. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  21662. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  21663. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  21664. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  21665. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  21666. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  21667. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  21668. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  21669. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  21670. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  21671. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  21672. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  21673. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  21674. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  21675. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  21676. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  21677. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  21678. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  21679. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  21680. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  21681. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  21682. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  21683. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  21684. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  21685. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  21686. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  21687. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  21688. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  21689. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  21690. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  21691. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  21692. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  21693. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  21694. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  21695. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  21696. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  21697. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  21698. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  21699. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  21700. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  21701. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  21702. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  21703. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  21704. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  21705. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  21706. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  21707. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  21708. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  21709. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  21710. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  21711. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  21712. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  21713. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  21714. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  21715. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  21716. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  21717. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  21718. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  21719. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  21720. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  21721. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  21722. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  21723. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  21724. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  21725. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  21726. };
  21727. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  21728. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  21729. sizeof(msg2),
  21730. sizeof(msg3),
  21731. 0 /*sizeof(msg1)*/,
  21732. 0 /*sizeof(msg1)*/,
  21733. sizeof(msg4)
  21734. };
  21735. #ifndef NO_ASN
  21736. static byte privateEd25519[] = {
  21737. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  21738. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  21739. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  21740. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  21741. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  21742. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  21743. };
  21744. static byte publicEd25519[] = {
  21745. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  21746. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  21747. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  21748. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  21749. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  21750. 0xf7,0x07,0x51,0x1a
  21751. };
  21752. static byte privPubEd25519[] = {
  21753. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  21754. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  21755. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  21756. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  21757. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  21758. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  21759. 0xa1,0x22,0x04,0x20,0xd7,0x5a,0x98,0x01,
  21760. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  21761. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  21762. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  21763. 0xf7,0x07,0x51,0x1a
  21764. };
  21765. word32 idx;
  21766. ed25519_key key3;
  21767. #endif /* NO_ASN */
  21768. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  21769. /* create ed25519 keys */
  21770. #ifndef HAVE_FIPS
  21771. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  21772. #else
  21773. ret = wc_InitRng(&rng);
  21774. #endif
  21775. if (ret != 0)
  21776. return -11000;
  21777. wc_ed25519_init(&key);
  21778. wc_ed25519_init(&key2);
  21779. #ifndef NO_ASN
  21780. wc_ed25519_init(&key3);
  21781. #endif
  21782. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  21783. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  21784. /* helper functions for signature and key size */
  21785. keySz = wc_ed25519_size(&key);
  21786. sigSz = wc_ed25519_sig_size(&key);
  21787. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  21788. defined(HAVE_ED25519_KEY_IMPORT)
  21789. for (i = 0; i < 6; i++) {
  21790. outlen = sizeof(out);
  21791. XMEMSET(out, 0, sizeof(out));
  21792. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  21793. pKeySz[i], &key) != 0)
  21794. return -11001 - i;
  21795. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  21796. return -11011 - i;
  21797. if (XMEMCMP(out, sigs[i], 64))
  21798. return -11021 - i;
  21799. #if defined(HAVE_ED25519_VERIFY)
  21800. /* test verify on good msg */
  21801. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  21802. &key) != 0 || verify != 1)
  21803. return -11031 - i;
  21804. /* test verify on bad msg */
  21805. out[outlen-1] = out[outlen-1] + 1;
  21806. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  21807. &key) == 0 || verify == 1)
  21808. return -11041 - i;
  21809. #endif /* HAVE_ED25519_VERIFY */
  21810. /* test api for import/exporting keys */
  21811. exportPSz = sizeof(exportPKey);
  21812. exportSSz = sizeof(exportSKey);
  21813. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  21814. return -11051 - i;
  21815. if (wc_ed25519_import_public(exportPKey, exportPSz, &key2) != 0)
  21816. return -11061 - i;
  21817. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  21818. return -11071 - i;
  21819. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  21820. exportPKey, exportPSz, &key2) != 0)
  21821. return -11081 - i;
  21822. /* clear "out" buffer and test sign with imported keys */
  21823. outlen = sizeof(out);
  21824. XMEMSET(out, 0, sizeof(out));
  21825. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  21826. return -11091 - i;
  21827. #if defined(HAVE_ED25519_VERIFY)
  21828. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  21829. &key2) != 0 || verify != 1)
  21830. return -11101 - i;
  21831. if (XMEMCMP(out, sigs[i], 64))
  21832. return -11111 - i;
  21833. #endif /* HAVE_ED25519_VERIFY */
  21834. }
  21835. ret = ed25519ctx_test();
  21836. if (ret != 0)
  21837. return ret;
  21838. ret = ed25519ph_test();
  21839. if (ret != 0)
  21840. return ret;
  21841. #ifndef NO_ASN
  21842. /* Try ASN.1 encoded private-only key and public key. */
  21843. idx = 0;
  21844. if (wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  21845. sizeof(privateEd25519)) != 0)
  21846. return -11121 - i;
  21847. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3)
  21848. != BAD_FUNC_ARG)
  21849. return -11131 - i;
  21850. idx = 0;
  21851. if (wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  21852. sizeof(publicEd25519)) != 0)
  21853. return -11141 - i;
  21854. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  21855. return -11151 - i;
  21856. if (XMEMCMP(out, sigs[0], 64))
  21857. return -11161 - i;
  21858. #if defined(HAVE_ED25519_VERIFY)
  21859. /* test verify on good msg */
  21860. if (wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3)
  21861. != 0 || verify != 1)
  21862. return -11171 - i;
  21863. #endif /* HAVE_ED25519_VERIFY */
  21864. wc_ed25519_free(&key3);
  21865. wc_ed25519_init(&key3);
  21866. idx = 0;
  21867. if (wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  21868. sizeof(privPubEd25519)) != 0)
  21869. return -11181 - i;
  21870. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  21871. return -11191 - i;
  21872. if (XMEMCMP(out, sigs[0], 64))
  21873. return -11201 - i;
  21874. wc_ed25519_free(&key3);
  21875. #endif /* NO_ASN */
  21876. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  21877. /* clean up keys when done */
  21878. wc_ed25519_free(&key);
  21879. wc_ed25519_free(&key2);
  21880. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  21881. wc_FreeRng(&rng);
  21882. #endif
  21883. /* hush warnings of unused keySz and sigSz */
  21884. (void)keySz;
  21885. (void)sigSz;
  21886. #ifdef WOLFSSL_TEST_CERT
  21887. ret = ed25519_test_cert();
  21888. if (ret < 0)
  21889. return ret;
  21890. #ifdef WOLFSSL_CERT_GEN
  21891. ret = ed25519_test_make_cert();
  21892. if (ret < 0)
  21893. return ret;
  21894. #endif /* WOLFSSL_CERT_GEN */
  21895. #endif /* WOLFSSL_TEST_CERT */
  21896. return 0;
  21897. }
  21898. #endif /* HAVE_ED25519 */
  21899. #ifdef HAVE_CURVE448
  21900. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  21901. defined(HAVE_CURVE448_KEY_IMPORT)
  21902. /* Test the wc_curve448_check_public API.
  21903. *
  21904. * returns 0 on success and -ve on failure.
  21905. */
  21906. static int curve448_check_public_test(void)
  21907. {
  21908. /* Little-endian values that will fail */
  21909. byte fail_le[][CURVE448_KEY_SIZE] = {
  21910. {
  21911. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21912. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21913. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21914. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21915. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21916. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21917. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  21918. },
  21919. {
  21920. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21921. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21922. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21923. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21924. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21925. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21926. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  21927. },
  21928. };
  21929. /* Big-endian values that will fail */
  21930. byte fail_be[][CURVE448_KEY_SIZE] = {
  21931. {
  21932. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21933. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21934. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21935. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21936. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21937. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21938. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  21939. },
  21940. {
  21941. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21942. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21943. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21944. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21945. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21946. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21947. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  21948. },
  21949. };
  21950. /* Good or valid public value */
  21951. byte good[CURVE448_KEY_SIZE] = {
  21952. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21953. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21954. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21955. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21956. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21957. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21958. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  21959. };
  21960. int i;
  21961. /* Parameter checks */
  21962. /* NULL pointer */
  21963. if (wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN) !=
  21964. BAD_FUNC_ARG) {
  21965. return -11300;
  21966. }
  21967. if (wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN) != BAD_FUNC_ARG) {
  21968. return -11301;
  21969. }
  21970. /* Length of 0 treated differently to other invalid lengths for TLS */
  21971. if (wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN) != BUFFER_E)
  21972. return -11302;
  21973. if (wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN) != BUFFER_E)
  21974. return -11303;
  21975. /* Length not CURVE448_KEY_SIZE */
  21976. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  21977. if (i == CURVE448_KEY_SIZE)
  21978. continue;
  21979. if (wc_curve448_check_public(good, i, EC448_LITTLE_ENDIAN) !=
  21980. ECC_BAD_ARG_E) {
  21981. return -11304 - i;
  21982. }
  21983. if (wc_curve448_check_public(good, i, EC448_BIG_ENDIAN) !=
  21984. ECC_BAD_ARG_E) {
  21985. return -11314 - i;
  21986. }
  21987. }
  21988. /* Little-endian fail cases */
  21989. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  21990. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  21991. EC448_LITTLE_ENDIAN) == 0) {
  21992. return -11324 - i;
  21993. }
  21994. }
  21995. /* Big-endian fail cases */
  21996. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  21997. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  21998. EC448_BIG_ENDIAN) == 0) {
  21999. return -11334 - i;
  22000. }
  22001. }
  22002. /* Check a valid public value works! */
  22003. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  22004. EC448_LITTLE_ENDIAN) != 0) {
  22005. return -11344;
  22006. }
  22007. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  22008. EC448_BIG_ENDIAN) != 0) {
  22009. return -11345;
  22010. }
  22011. return 0;
  22012. }
  22013. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  22014. WOLFSSL_TEST_SUBROUTINE int curve448_test(void)
  22015. {
  22016. WC_RNG rng;
  22017. int ret;
  22018. #ifdef HAVE_CURVE448_SHARED_SECRET
  22019. byte sharedA[CURVE448_KEY_SIZE];
  22020. byte sharedB[CURVE448_KEY_SIZE];
  22021. word32 y;
  22022. #endif
  22023. #ifdef HAVE_CURVE448_KEY_EXPORT
  22024. byte exportBuf[CURVE448_KEY_SIZE];
  22025. #endif
  22026. word32 x;
  22027. curve448_key userA, userB, pubKey;
  22028. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  22029. defined(HAVE_CURVE448_KEY_IMPORT)
  22030. /* test vectors from
  22031. https://www.rfc-editor.org/rfc/rfc7748.html
  22032. */
  22033. /* secret key for party a */
  22034. byte sa[] = {
  22035. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  22036. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  22037. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  22038. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  22039. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  22040. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  22041. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  22042. };
  22043. /* public key for party a */
  22044. byte pa[] = {
  22045. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  22046. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  22047. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  22048. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  22049. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  22050. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  22051. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  22052. };
  22053. /* secret key for party b */
  22054. byte sb[] = {
  22055. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  22056. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  22057. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  22058. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  22059. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  22060. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  22061. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  22062. };
  22063. /* public key for party b */
  22064. byte pb[] = {
  22065. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  22066. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  22067. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  22068. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  22069. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  22070. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  22071. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  22072. };
  22073. /* expected shared key */
  22074. byte ss[] = {
  22075. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  22076. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  22077. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  22078. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  22079. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  22080. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  22081. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  22082. };
  22083. #endif /* HAVE_CURVE448_SHARED_SECRET */
  22084. (void)x;
  22085. #ifndef HAVE_FIPS
  22086. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  22087. #else
  22088. ret = wc_InitRng(&rng);
  22089. #endif
  22090. if (ret != 0)
  22091. return -11400;
  22092. wc_curve448_init(&userA);
  22093. wc_curve448_init(&userB);
  22094. wc_curve448_init(&pubKey);
  22095. /* make curve448 keys */
  22096. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA) != 0)
  22097. return -11401;
  22098. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB) != 0)
  22099. return -11402;
  22100. #ifdef HAVE_CURVE448_SHARED_SECRET
  22101. /* find shared secret key */
  22102. x = sizeof(sharedA);
  22103. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  22104. return -11403;
  22105. y = sizeof(sharedB);
  22106. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  22107. return -11404;
  22108. /* compare shared secret keys to test they are the same */
  22109. if (y != x)
  22110. return -11405;
  22111. if (XMEMCMP(sharedA, sharedB, x))
  22112. return -11406;
  22113. #endif
  22114. #ifdef HAVE_CURVE448_KEY_EXPORT
  22115. /* export a public key and import it for another user */
  22116. x = sizeof(exportBuf);
  22117. if (wc_curve448_export_public(&userA, exportBuf, &x) != 0)
  22118. return -11407;
  22119. #ifdef HAVE_CURVE448_KEY_IMPORT
  22120. if (wc_curve448_import_public(exportBuf, x, &pubKey) != 0)
  22121. return -11408;
  22122. #endif
  22123. #endif
  22124. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  22125. defined(HAVE_CURVE448_KEY_IMPORT)
  22126. /* test shared key after importing a public key */
  22127. XMEMSET(sharedB, 0, sizeof(sharedB));
  22128. y = sizeof(sharedB);
  22129. if (wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  22130. return -11409;
  22131. if (XMEMCMP(sharedA, sharedB, y))
  22132. return -11410;
  22133. /* import RFC test vectors and compare shared key */
  22134. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  22135. != 0)
  22136. return -11411;
  22137. if (wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  22138. != 0)
  22139. return -11412;
  22140. /* test against known test vector */
  22141. XMEMSET(sharedB, 0, sizeof(sharedB));
  22142. y = sizeof(sharedB);
  22143. if (wc_curve448_shared_secret(&userA, &userB, sharedB, &y) != 0)
  22144. return -11413;
  22145. if (XMEMCMP(ss, sharedB, y))
  22146. return -11414;
  22147. /* test swapping roles of keys and generating same shared key */
  22148. XMEMSET(sharedB, 0, sizeof(sharedB));
  22149. y = sizeof(sharedB);
  22150. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  22151. return -11415;
  22152. if (XMEMCMP(ss, sharedB, y))
  22153. return -11416;
  22154. /* test with 1 generated key and 1 from known test vector */
  22155. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  22156. != 0)
  22157. return -11417;
  22158. if (wc_curve448_make_key(&rng, 56, &userB) != 0)
  22159. return -11418;
  22160. x = sizeof(sharedA);
  22161. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  22162. return -11419;
  22163. y = sizeof(sharedB);
  22164. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  22165. return -11420;
  22166. /* compare shared secret keys to test they are the same */
  22167. if (y != x)
  22168. return -11421;
  22169. if (XMEMCMP(sharedA, sharedB, x))
  22170. return -11422;
  22171. ret = curve448_check_public_test();
  22172. if (ret != 0)
  22173. return ret;
  22174. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  22175. /* clean up keys when done */
  22176. wc_curve448_free(&pubKey);
  22177. wc_curve448_free(&userB);
  22178. wc_curve448_free(&userA);
  22179. wc_FreeRng(&rng);
  22180. return 0;
  22181. }
  22182. #endif /* HAVE_CURVE448 */
  22183. #ifdef HAVE_ED448
  22184. #ifdef WOLFSSL_TEST_CERT
  22185. static int ed448_test_cert(void)
  22186. {
  22187. DecodedCert cert[2];
  22188. DecodedCert* serverCert = NULL;
  22189. DecodedCert* caCert = NULL;
  22190. #ifdef HAVE_ED448_VERIFY
  22191. ed448_key key;
  22192. ed448_key* pubKey = NULL;
  22193. int verify;
  22194. #endif /* HAVE_ED448_VERIFY */
  22195. int ret;
  22196. byte* tmp;
  22197. size_t bytes;
  22198. XFILE file;
  22199. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22200. if (tmp == NULL) {
  22201. ERROR_OUT(-11430, done);
  22202. }
  22203. #ifdef USE_CERT_BUFFERS_256
  22204. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  22205. bytes = sizeof_ca_ed448_cert;
  22206. #elif !defined(NO_FILESYSTEM)
  22207. file = XFOPEN(caEd448Cert, "rb");
  22208. if (file == NULL) {
  22209. ERROR_OUT(-11431, done);
  22210. }
  22211. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  22212. XFCLOSE(file);
  22213. #else
  22214. /* No certificate to use. */
  22215. ERROR_OUT(-11432, done);
  22216. #endif
  22217. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  22218. caCert = &cert[0];
  22219. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  22220. if (ret != 0) {
  22221. ERROR_OUT(-11433, done);
  22222. }
  22223. #ifdef USE_CERT_BUFFERS_256
  22224. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  22225. bytes = sizeof_server_ed448_cert;
  22226. #elif !defined(NO_FILESYSTEM)
  22227. file = XFOPEN(serverEd448Cert, "rb");
  22228. if (file == NULL) {
  22229. ERROR_OUT(-11434, done);
  22230. }
  22231. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  22232. XFCLOSE(file);
  22233. #else
  22234. /* No certificate to use. */
  22235. ERROR_OUT(-11435, done);
  22236. #endif
  22237. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  22238. serverCert = &cert[1];
  22239. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  22240. if (ret != 0) {
  22241. ERROR_OUT(-11436, done);
  22242. }
  22243. #ifdef HAVE_ED448_VERIFY
  22244. ret = wc_ed448_init(&key);
  22245. if (ret < 0) {
  22246. ERROR_OUT(-11437, done);
  22247. }
  22248. pubKey = &key;
  22249. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  22250. if (ret < 0) {
  22251. ERROR_OUT(-11438, done);
  22252. }
  22253. if (wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  22254. serverCert->source + serverCert->certBegin,
  22255. serverCert->sigIndex - serverCert->certBegin,
  22256. &verify, pubKey, NULL, 0) < 0 || verify != 1) {
  22257. ERROR_OUT(-11439, done);
  22258. }
  22259. #endif /* HAVE_ED448_VERIFY */
  22260. done:
  22261. if (tmp != NULL)
  22262. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22263. #ifdef HAVE_ED448_VERIFY
  22264. wc_ed448_free(pubKey);
  22265. #endif /* HAVE_ED448_VERIFY */
  22266. if (caCert != NULL)
  22267. FreeDecodedCert(caCert);
  22268. if (serverCert != NULL)
  22269. FreeDecodedCert(serverCert);
  22270. return ret;
  22271. }
  22272. static int ed448_test_make_cert(void)
  22273. {
  22274. WC_RNG rng;
  22275. Cert cert;
  22276. DecodedCert decode;
  22277. ed448_key key;
  22278. ed448_key* privKey = NULL;
  22279. int ret = 0;
  22280. byte* tmp = NULL;
  22281. wc_InitCert(&cert);
  22282. #ifndef HAVE_FIPS
  22283. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  22284. #else
  22285. ret = wc_InitRng(&rng);
  22286. #endif
  22287. if (ret != 0)
  22288. return -11450;
  22289. wc_ed448_init(&key);
  22290. privKey = &key;
  22291. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  22292. cert.daysValid = 365 * 2;
  22293. cert.selfSigned = 1;
  22294. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  22295. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  22296. cert.isCA = 0;
  22297. #ifdef WOLFSSL_CERT_EXT
  22298. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  22299. if (ret < 0) {
  22300. ERROR_OUT(-11451, done);
  22301. }
  22302. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  22303. if (ret < 0) {
  22304. ERROR_OUT(-11452, done);
  22305. }
  22306. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  22307. if (ret < 0) {
  22308. ERROR_OUT(-11453, done);
  22309. }
  22310. #endif
  22311. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22312. if (tmp == NULL) {
  22313. ERROR_OUT(-11454, done);
  22314. }
  22315. cert.sigType = CTC_ED448;
  22316. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  22317. if (ret < 0) {
  22318. ERROR_OUT(-11455, done);
  22319. }
  22320. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  22321. privKey, &rng);
  22322. if (ret < 0) {
  22323. ERROR_OUT(-11456, done);
  22324. }
  22325. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  22326. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  22327. FreeDecodedCert(&decode);
  22328. if (ret != 0) {
  22329. ERROR_OUT(-11457, done);
  22330. }
  22331. done:
  22332. if (tmp != NULL)
  22333. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22334. wc_ed448_free(privKey);
  22335. wc_FreeRng(&rng);
  22336. return ret;
  22337. }
  22338. #endif /* WOLFSSL_TEST_CERT */
  22339. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  22340. defined(HAVE_ED448_KEY_IMPORT)
  22341. static int ed448_ctx_test(void)
  22342. {
  22343. byte out[ED448_SIG_SIZE];
  22344. word32 outlen;
  22345. #ifdef HAVE_ED448_VERIFY
  22346. int verify;
  22347. #endif /* HAVE_ED448_VERIFY */
  22348. ed448_key key;
  22349. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  22350. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  22351. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  22352. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  22353. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  22354. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  22355. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  22356. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  22357. 0x4e
  22358. };
  22359. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  22360. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  22361. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  22362. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  22363. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  22364. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  22365. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  22366. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  22367. 0x80
  22368. };
  22369. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  22370. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  22371. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  22372. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  22373. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  22374. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  22375. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  22376. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  22377. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  22378. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  22379. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  22380. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  22381. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  22382. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  22383. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  22384. 0x3c, 0x00
  22385. };
  22386. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  22387. 0x03
  22388. };
  22389. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  22390. 0x66,0x6f,0x6f
  22391. };
  22392. outlen = sizeof(out);
  22393. XMEMSET(out, 0, sizeof(out));
  22394. if (wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  22395. sizeof(pKeyCtx), &key) != 0)
  22396. return -11500;
  22397. if (wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  22398. contextCtx, sizeof(contextCtx)) != 0)
  22399. return -11501;
  22400. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  22401. return -11502;
  22402. #if defined(HAVE_ED448_VERIFY)
  22403. /* test verify on good msg */
  22404. if (wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  22405. contextCtx, sizeof(contextCtx)) != 0 || verify != 1)
  22406. return -11503;
  22407. #endif
  22408. wc_ed448_free(&key);
  22409. return 0;
  22410. }
  22411. static int ed448ph_test(void)
  22412. {
  22413. byte out[ED448_SIG_SIZE];
  22414. word32 outlen;
  22415. #ifdef HAVE_ED448_VERIFY
  22416. int verify;
  22417. #endif /* HAVE_ED448_VERIFY */
  22418. ed448_key key;
  22419. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  22420. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  22421. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  22422. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  22423. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  22424. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  22425. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  22426. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  22427. 0x49
  22428. };
  22429. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  22430. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  22431. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  22432. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  22433. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  22434. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  22435. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  22436. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  22437. 0x80
  22438. };
  22439. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  22440. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  22441. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  22442. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  22443. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  22444. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  22445. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  22446. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  22447. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  22448. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  22449. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  22450. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  22451. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  22452. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  22453. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  22454. 0x0f, 0x00
  22455. };
  22456. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  22457. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  22458. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  22459. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  22460. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  22461. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  22462. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  22463. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  22464. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  22465. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  22466. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  22467. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  22468. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  22469. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  22470. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  22471. 0x21, 0x00
  22472. };
  22473. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  22474. 0x61,0x62,0x63
  22475. };
  22476. /* SHA-512 hash of msgPh */
  22477. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  22478. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  22479. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  22480. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  22481. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  22482. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  22483. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  22484. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  22485. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  22486. };
  22487. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  22488. 0x66,0x6f,0x6f
  22489. };
  22490. outlen = sizeof(out);
  22491. XMEMSET(out, 0, sizeof(out));
  22492. if (wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  22493. sizeof(pKeyPh), &key) != 0) {
  22494. return -11600;
  22495. }
  22496. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  22497. 0) != 0) {
  22498. return -11601;
  22499. }
  22500. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  22501. return -11602;
  22502. #if defined(HAVE_ED448_VERIFY)
  22503. /* test verify on good msg */
  22504. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  22505. NULL, 0) != 0 || verify != 1) {
  22506. return -11603;
  22507. }
  22508. #endif
  22509. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  22510. contextPh2, sizeof(contextPh2)) != 0) {
  22511. return -11604;
  22512. }
  22513. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  22514. return -11605;
  22515. #if defined(HAVE_ED448_VERIFY)
  22516. /* test verify on good msg */
  22517. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  22518. contextPh2, sizeof(contextPh2)) != 0 ||
  22519. verify != 1) {
  22520. return -11606;
  22521. }
  22522. #endif
  22523. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  22524. 0) != 0) {
  22525. return -11607;
  22526. }
  22527. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  22528. return -11608;
  22529. #if defined(HAVE_ED448_VERIFY)
  22530. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  22531. &key, NULL, 0) != 0 || verify != 1) {
  22532. return -11609;
  22533. }
  22534. #endif
  22535. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  22536. contextPh2, sizeof(contextPh2)) != 0) {
  22537. return -11610;
  22538. }
  22539. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  22540. return -11611;
  22541. #if defined(HAVE_ED448_VERIFY)
  22542. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  22543. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  22544. verify != 1) {
  22545. return -11612;
  22546. }
  22547. #endif
  22548. wc_ed448_free(&key);
  22549. return 0;
  22550. }
  22551. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  22552. WOLFSSL_TEST_SUBROUTINE int ed448_test(void)
  22553. {
  22554. int ret;
  22555. WC_RNG rng;
  22556. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  22557. defined(HAVE_ED448_KEY_IMPORT)
  22558. byte out[ED448_SIG_SIZE];
  22559. byte exportPKey[ED448_KEY_SIZE];
  22560. byte exportSKey[ED448_KEY_SIZE];
  22561. word32 exportPSz;
  22562. word32 exportSSz;
  22563. int i;
  22564. word32 outlen;
  22565. #ifdef HAVE_ED448_VERIFY
  22566. int verify;
  22567. #endif /* HAVE_ED448_VERIFY */
  22568. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  22569. word32 keySz, sigSz;
  22570. ed448_key key;
  22571. ed448_key key2;
  22572. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  22573. defined(HAVE_ED448_KEY_IMPORT)
  22574. /* test vectors from
  22575. https://tools.ietf.org/html/rfc8032
  22576. */
  22577. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  22578. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  22579. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  22580. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  22581. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  22582. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  22583. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  22584. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  22585. 0x5b
  22586. };
  22587. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  22588. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  22589. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  22590. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  22591. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  22592. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  22593. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  22594. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  22595. 0x4e
  22596. };
  22597. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  22598. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  22599. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  22600. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  22601. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  22602. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  22603. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  22604. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  22605. 0x1b
  22606. };
  22607. /* uncompressed test */
  22608. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  22609. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  22610. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  22611. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  22612. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  22613. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  22614. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  22615. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  22616. 0x5b
  22617. };
  22618. /* compressed prefix test */
  22619. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  22620. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  22621. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  22622. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  22623. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  22624. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  22625. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  22626. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  22627. 0x5b
  22628. };
  22629. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  22630. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  22631. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  22632. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  22633. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  22634. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  22635. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  22636. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  22637. 0xe8
  22638. };
  22639. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  22640. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  22641. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  22642. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  22643. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  22644. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  22645. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  22646. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  22647. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  22648. 0x80
  22649. };
  22650. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  22651. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  22652. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  22653. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  22654. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  22655. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  22656. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  22657. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  22658. 0x80
  22659. };
  22660. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  22661. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  22662. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  22663. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  22664. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  22665. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  22666. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  22667. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  22668. 0x80
  22669. };
  22670. /* uncompressed test */
  22671. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  22672. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  22673. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  22674. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  22675. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  22676. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  22677. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  22678. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  22679. 0x80
  22680. };
  22681. /* compressed prefix */
  22682. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  22683. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  22684. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  22685. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  22686. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  22687. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  22688. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  22689. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  22690. 0x80
  22691. };
  22692. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  22693. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  22694. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  22695. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  22696. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  22697. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  22698. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  22699. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  22700. 0x00
  22701. };
  22702. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  22703. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  22704. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  22705. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  22706. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  22707. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  22708. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  22709. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  22710. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  22711. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  22712. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  22713. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  22714. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  22715. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  22716. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  22717. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  22718. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  22719. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  22720. 0x26, 0x00
  22721. };
  22722. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  22723. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  22724. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  22725. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  22726. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  22727. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  22728. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  22729. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  22730. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  22731. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  22732. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  22733. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  22734. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  22735. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  22736. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  22737. 0x3a, 0x00
  22738. };
  22739. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  22740. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  22741. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  22742. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  22743. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  22744. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  22745. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  22746. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  22747. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  22748. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  22749. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  22750. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  22751. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  22752. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  22753. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  22754. 0x3c, 0x00
  22755. };
  22756. /* uncompressed test */
  22757. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  22758. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  22759. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  22760. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  22761. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  22762. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  22763. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  22764. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  22765. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  22766. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  22767. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  22768. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  22769. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  22770. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  22771. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  22772. 0x26, 0x00
  22773. };
  22774. /* compressed prefix */
  22775. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  22776. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  22777. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  22778. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  22779. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  22780. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  22781. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  22782. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  22783. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  22784. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  22785. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  22786. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  22787. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  22788. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  22789. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  22790. 0x26, 0x00
  22791. };
  22792. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  22793. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  22794. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  22795. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  22796. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  22797. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  22798. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  22799. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  22800. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  22801. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  22802. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  22803. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  22804. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  22805. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  22806. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  22807. 0x1a, 0x00
  22808. };
  22809. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  22810. #define SIGSZ sizeof(sig1)
  22811. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { };
  22812. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  22813. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  22814. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  22815. /* test of a 1023 byte long message */
  22816. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  22817. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  22818. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  22819. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  22820. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  22821. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  22822. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  22823. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  22824. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  22825. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  22826. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  22827. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  22828. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  22829. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  22830. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  22831. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  22832. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  22833. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  22834. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  22835. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  22836. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  22837. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  22838. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  22839. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  22840. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  22841. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  22842. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  22843. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  22844. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  22845. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  22846. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  22847. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  22848. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  22849. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  22850. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  22851. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  22852. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  22853. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  22854. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  22855. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  22856. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  22857. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  22858. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  22859. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  22860. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  22861. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  22862. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  22863. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  22864. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  22865. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  22866. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  22867. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  22868. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  22869. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  22870. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  22871. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  22872. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  22873. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  22874. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  22875. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  22876. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  22877. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  22878. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  22879. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  22880. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  22881. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  22882. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  22883. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  22884. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  22885. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  22886. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  22887. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  22888. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  22889. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  22890. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  22891. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  22892. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  22893. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  22894. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  22895. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  22896. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  22897. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  22898. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  22899. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  22900. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  22901. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  22902. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  22903. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  22904. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  22905. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  22906. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  22907. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  22908. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  22909. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  22910. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  22911. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  22912. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  22913. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  22914. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  22915. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  22916. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  22917. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  22918. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  22919. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  22920. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  22921. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  22922. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  22923. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  22924. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  22925. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  22926. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  22927. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  22928. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  22929. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  22930. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  22931. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  22932. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  22933. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  22934. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  22935. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  22936. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  22937. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  22938. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  22939. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  22940. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  22941. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  22942. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  22943. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  22944. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  22945. };
  22946. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  22947. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  22948. sizeof(msg2),
  22949. sizeof(msg3),
  22950. 0 /*sizeof(msg1)*/,
  22951. 0 /*sizeof(msg1)*/,
  22952. sizeof(msg4)
  22953. };
  22954. #ifndef NO_ASN
  22955. static byte privateEd448[] = {
  22956. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  22957. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  22958. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  22959. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  22960. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  22961. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  22962. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  22963. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  22964. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  22965. 0x5b
  22966. };
  22967. static byte publicEd448[] = {
  22968. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  22969. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  22970. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  22971. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  22972. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  22973. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  22974. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  22975. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  22976. 0xaf, 0xe8, 0x25, 0x61, 0x80
  22977. };
  22978. static byte privPubEd448[] = {
  22979. 0x30, 0x81, 0x84, 0x02, 0x01, 0x00, 0x30, 0x05,
  22980. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  22981. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  22982. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  22983. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  22984. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  22985. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  22986. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  22987. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  22988. 0xf9, 0x5b, 0xa1, 0x3b, 0x04, 0x39, 0x5f, 0xd7,
  22989. 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7,
  22990. 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1,
  22991. 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e,
  22992. 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1,
  22993. 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6,
  22994. 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa,
  22995. 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61, 0x80
  22996. };
  22997. word32 idx;
  22998. ed448_key key3;
  22999. #endif /* NO_ASN */
  23000. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  23001. /* create ed448 keys */
  23002. #ifndef HAVE_FIPS
  23003. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23004. #else
  23005. ret = wc_InitRng(&rng);
  23006. #endif
  23007. if (ret != 0)
  23008. return -11700;
  23009. wc_ed448_init(&key);
  23010. wc_ed448_init(&key2);
  23011. #ifndef NO_ASN
  23012. wc_ed448_init(&key3);
  23013. #endif
  23014. wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  23015. wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key2);
  23016. /* helper functions for signature and key size */
  23017. keySz = wc_ed448_size(&key);
  23018. sigSz = wc_ed448_sig_size(&key);
  23019. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  23020. defined(HAVE_ED448_KEY_IMPORT)
  23021. for (i = 0; i < 6; i++) {
  23022. outlen = sizeof(out);
  23023. XMEMSET(out, 0, sizeof(out));
  23024. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  23025. pKeySz[i], &key) != 0)
  23026. return -11701 - i;
  23027. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, &key, NULL,
  23028. 0) != 0) {
  23029. return -11711 - i;
  23030. }
  23031. if (XMEMCMP(out, sigs[i], 114))
  23032. return -11721 - i;
  23033. #if defined(HAVE_ED448_VERIFY)
  23034. /* test verify on good msg */
  23035. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, &key,
  23036. NULL, 0) != 0 || verify != 1) {
  23037. return -11731 - i;
  23038. }
  23039. /* test verify on bad msg */
  23040. out[outlen-2] = out[outlen-2] + 1;
  23041. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, &key,
  23042. NULL, 0) == 0 || verify == 1) {
  23043. return -11741 - i;
  23044. }
  23045. #endif /* HAVE_ED448_VERIFY */
  23046. /* test api for import/exporting keys */
  23047. exportPSz = sizeof(exportPKey);
  23048. exportSSz = sizeof(exportSKey);
  23049. if (wc_ed448_export_public(&key, exportPKey, &exportPSz) != 0)
  23050. return -11751 - i;
  23051. if (wc_ed448_import_public(exportPKey, exportPSz, &key2) != 0)
  23052. return -11761 - i;
  23053. if (wc_ed448_export_private_only(&key, exportSKey, &exportSSz) != 0)
  23054. return -11771 - i;
  23055. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  23056. exportPKey, exportPSz, &key2) != 0)
  23057. return -11781 - i;
  23058. /* clear "out" buffer and test sign with imported keys */
  23059. outlen = sizeof(out);
  23060. XMEMSET(out, 0, sizeof(out));
  23061. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2, NULL,
  23062. 0) != 0) {
  23063. return -11791 - i;
  23064. }
  23065. #if defined(HAVE_ED448_VERIFY)
  23066. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, &key2,
  23067. NULL, 0) != 0 || verify != 1)
  23068. return -11801 - i;
  23069. if (XMEMCMP(out, sigs[i], SIGSZ))
  23070. return -11811 - i;
  23071. #endif /* HAVE_ED448_VERIFY */
  23072. }
  23073. ret = ed448_ctx_test();
  23074. if (ret != 0)
  23075. return ret;
  23076. ret = ed448ph_test();
  23077. if (ret != 0)
  23078. return ret;
  23079. #ifndef NO_ASN
  23080. /* Try ASN.1 encoded private-only key and public key. */
  23081. idx = 0;
  23082. if (wc_Ed448PrivateKeyDecode(privateEd448, &idx, &key3,
  23083. sizeof(privateEd448)) != 0)
  23084. return -11821 - i;
  23085. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3, NULL, 0)
  23086. != BAD_FUNC_ARG)
  23087. return -11831 - i;
  23088. idx = 0;
  23089. if (wc_Ed448PublicKeyDecode(publicEd448, &idx, &key3,
  23090. sizeof(publicEd448)) != 0)
  23091. return -11841 - i;
  23092. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3, NULL, 0) != 0)
  23093. return -11851 - i;
  23094. if (XMEMCMP(out, sigs[0], SIGSZ))
  23095. return -11861 - i;
  23096. #if defined(HAVE_ED448_VERIFY)
  23097. /* test verify on good msg */
  23098. if (wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3,
  23099. NULL, 0) != 0 || verify != 1)
  23100. return -11871 - i;
  23101. #endif /* HAVE_ED448_VERIFY */
  23102. wc_ed448_free(&key3);
  23103. wc_ed448_init(&key3);
  23104. idx = 0;
  23105. if (wc_Ed448PrivateKeyDecode(privPubEd448, &idx, &key3,
  23106. sizeof(privPubEd448)) != 0)
  23107. return -11881 - i;
  23108. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3, NULL, 0) != 0)
  23109. return -11891 - i;
  23110. if (XMEMCMP(out, sigs[0], SIGSZ))
  23111. return -11901 - i;
  23112. wc_ed448_free(&key3);
  23113. #endif /* NO_ASN */
  23114. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  23115. /* clean up keys when done */
  23116. wc_ed448_free(&key);
  23117. wc_ed448_free(&key2);
  23118. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  23119. wc_FreeRng(&rng);
  23120. #endif
  23121. /* hush warnings of unused keySz and sigSz */
  23122. (void)keySz;
  23123. (void)sigSz;
  23124. #ifdef WOLFSSL_TEST_CERT
  23125. ret = ed448_test_cert();
  23126. if (ret < 0)
  23127. return ret;
  23128. #ifdef WOLFSSL_CERT_GEN
  23129. ret = ed448_test_make_cert();
  23130. if (ret < 0)
  23131. return ret;
  23132. #endif /* WOLFSSL_CERT_GEN */
  23133. #endif /* WOLFSSL_TEST_CERT */
  23134. return 0;
  23135. }
  23136. #endif /* HAVE_ED448 */
  23137. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  23138. typedef struct CMAC_Test_Case {
  23139. int type;
  23140. int partial;
  23141. const byte* m;
  23142. word32 mSz;
  23143. const byte* k;
  23144. word32 kSz;
  23145. const byte* t;
  23146. word32 tSz;
  23147. } CMAC_Test_Case;
  23148. WOLFSSL_TEST_SUBROUTINE int cmac_test(void)
  23149. {
  23150. #ifdef WOLFSSL_AES_128
  23151. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  23152. {
  23153. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  23154. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  23155. };
  23156. #define KLEN_128 (sizeof(k128))
  23157. #endif
  23158. #ifdef WOLFSSL_AES_192
  23159. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  23160. {
  23161. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  23162. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  23163. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  23164. };
  23165. #define KLEN_192 (sizeof(k192))
  23166. #endif
  23167. #ifdef WOLFSSL_AES_256
  23168. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  23169. {
  23170. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  23171. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  23172. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  23173. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  23174. };
  23175. #define KLEN_256 (sizeof(k256))
  23176. #endif
  23177. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  23178. {
  23179. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  23180. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  23181. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  23182. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  23183. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  23184. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  23185. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  23186. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  23187. };
  23188. #define MLEN_0 (0)
  23189. #define MLEN_128 (128/8)
  23190. #define MLEN_320 (320/8)
  23191. #define MLEN_319 (MLEN_320 - 1)
  23192. #define MLEN_512 (512/8)
  23193. #ifdef WOLFSSL_AES_128
  23194. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  23195. {
  23196. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  23197. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  23198. };
  23199. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  23200. {
  23201. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  23202. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  23203. };
  23204. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  23205. {
  23206. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  23207. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  23208. };
  23209. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  23210. {
  23211. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  23212. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  23213. };
  23214. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  23215. {
  23216. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  23217. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  23218. };
  23219. #endif
  23220. #ifdef WOLFSSL_AES_192
  23221. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  23222. {
  23223. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  23224. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  23225. };
  23226. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  23227. {
  23228. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  23229. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  23230. };
  23231. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  23232. {
  23233. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  23234. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  23235. };
  23236. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  23237. {
  23238. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  23239. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  23240. };
  23241. #endif
  23242. #ifdef WOLFSSL_AES_256
  23243. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  23244. {
  23245. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  23246. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  23247. };
  23248. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  23249. {
  23250. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  23251. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  23252. };
  23253. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  23254. {
  23255. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  23256. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  23257. };
  23258. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  23259. {
  23260. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  23261. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  23262. };
  23263. #endif
  23264. const CMAC_Test_Case testCases[] =
  23265. {
  23266. #ifdef WOLFSSL_AES_128
  23267. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  23268. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  23269. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  23270. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  23271. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  23272. #endif
  23273. #ifdef WOLFSSL_AES_192
  23274. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  23275. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  23276. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  23277. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  23278. #endif
  23279. #ifdef WOLFSSL_AES_256
  23280. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  23281. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  23282. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  23283. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  23284. #endif
  23285. #ifdef WOLFSSL_AES_128
  23286. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  23287. #endif
  23288. };
  23289. #ifdef WOLFSSL_SMALL_STACK
  23290. Cmac *cmac;
  23291. #else
  23292. Cmac cmac[1];
  23293. #endif
  23294. byte tag[AES_BLOCK_SIZE];
  23295. const CMAC_Test_Case* tc;
  23296. word32 i, tagSz;
  23297. int ret;
  23298. #ifdef WOLFSSL_SMALL_STACK
  23299. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  23300. ERROR_OUT(-12009, out);
  23301. #endif
  23302. for (i = 0, tc = testCases;
  23303. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  23304. i++, tc++) {
  23305. XMEMSET(tag, 0, sizeof(tag));
  23306. tagSz = AES_BLOCK_SIZE;
  23307. if (wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL) != 0)
  23308. ERROR_OUT(-12000, out);
  23309. if (tc->partial) {
  23310. if (wc_CmacUpdate(cmac, tc->m,
  23311. tc->mSz/2 - tc->partial) != 0)
  23312. ERROR_OUT(-12001, out);
  23313. if (wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  23314. tc->mSz/2 + tc->partial) != 0)
  23315. ERROR_OUT(-12002, out);
  23316. }
  23317. else {
  23318. if (wc_CmacUpdate(cmac, tc->m, tc->mSz) != 0)
  23319. ERROR_OUT(-12003, out);
  23320. }
  23321. if (wc_CmacFinal(cmac, tag, &tagSz) != 0)
  23322. ERROR_OUT(-12004, out);
  23323. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  23324. ERROR_OUT(-12005, out);
  23325. XMEMSET(tag, 0, sizeof(tag));
  23326. tagSz = sizeof(tag);
  23327. if (wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  23328. tc->k, tc->kSz) != 0)
  23329. ERROR_OUT(-12006, out);
  23330. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  23331. ERROR_OUT(-12007, out);
  23332. if (wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  23333. tc->k, tc->kSz) != 0)
  23334. ERROR_OUT(-12008, out);
  23335. }
  23336. ret = 0;
  23337. out:
  23338. #ifdef WOLFSSL_SMALL_STACK
  23339. if (cmac)
  23340. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  23341. #endif
  23342. return ret;
  23343. }
  23344. #endif /* NO_AES && WOLFSSL_CMAC */
  23345. #ifdef HAVE_LIBZ
  23346. static const byte sample_text[] =
  23347. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  23348. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  23349. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  23350. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  23351. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  23352. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  23353. "\n"
  23354. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  23355. "four loko you probably haven't heard of them high life. Messenger bag\n"
  23356. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  23357. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  23358. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  23359. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  23360. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  23361. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  23362. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  23363. "four loko whatever street art yr farm-to-table.\n"
  23364. "\n"
  23365. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  23366. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  23367. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  23368. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  23369. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  23370. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  23371. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  23372. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  23373. "locavore.\n"
  23374. "\n"
  23375. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  23376. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  23377. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  23378. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  23379. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  23380. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  23381. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  23382. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  23383. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  23384. "neutra PBR selvage.\n"
  23385. "\n"
  23386. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  23387. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  23388. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  23389. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  23390. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  23391. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  23392. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  23393. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  23394. "mollit magna, sriracha sartorial helvetica.\n"
  23395. "\n"
  23396. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  23397. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  23398. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  23399. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  23400. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  23401. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  23402. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  23403. "\n"
  23404. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  23405. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  23406. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  23407. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  23408. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  23409. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  23410. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  23411. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  23412. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  23413. "\n"
  23414. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  23415. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  23416. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  23417. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  23418. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  23419. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  23420. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  23421. "bag dolor terry richardson sapiente.\n";
  23422. static const byte sample_text_gz[] = {
  23423. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  23424. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  23425. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  23426. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  23427. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  23428. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  23429. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  23430. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  23431. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  23432. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  23433. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  23434. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  23435. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  23436. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  23437. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  23438. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  23439. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  23440. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  23441. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  23442. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  23443. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  23444. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  23445. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  23446. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  23447. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  23448. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  23449. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  23450. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  23451. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  23452. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  23453. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  23454. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  23455. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  23456. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  23457. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  23458. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  23459. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  23460. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  23461. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  23462. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  23463. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  23464. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  23465. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  23466. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  23467. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  23468. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  23469. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  23470. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  23471. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  23472. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  23473. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  23474. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  23475. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  23476. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  23477. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  23478. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  23479. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  23480. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  23481. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  23482. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  23483. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  23484. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  23485. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  23486. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  23487. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  23488. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  23489. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  23490. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  23491. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  23492. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  23493. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  23494. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  23495. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  23496. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  23497. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  23498. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  23499. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  23500. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  23501. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  23502. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  23503. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  23504. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  23505. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  23506. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  23507. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  23508. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  23509. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  23510. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  23511. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  23512. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  23513. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  23514. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  23515. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  23516. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  23517. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  23518. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  23519. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  23520. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  23521. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  23522. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  23523. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  23524. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  23525. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  23526. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  23527. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  23528. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  23529. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  23530. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  23531. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  23532. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  23533. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  23534. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  23535. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  23536. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  23537. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  23538. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  23539. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  23540. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  23541. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  23542. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  23543. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  23544. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  23545. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  23546. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  23547. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  23548. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  23549. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  23550. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  23551. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  23552. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  23553. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  23554. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  23555. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  23556. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  23557. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  23558. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  23559. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  23560. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  23561. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  23562. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  23563. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  23564. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  23565. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  23566. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  23567. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  23568. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  23569. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  23570. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  23571. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  23572. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  23573. };
  23574. WOLFSSL_TEST_SUBROUTINE int compress_test(void)
  23575. {
  23576. int ret = 0;
  23577. word32 dSz = sizeof(sample_text);
  23578. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  23579. byte *c;
  23580. byte *d;
  23581. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23582. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23583. if (c == NULL || d == NULL) {
  23584. ERROR_OUT(-12100, exit);
  23585. }
  23586. /* follow calloc and initialize to 0 */
  23587. XMEMSET(c, 0, cSz);
  23588. XMEMSET(d, 0, dSz);
  23589. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  23590. ERROR_OUT(-12101, exit);
  23591. }
  23592. cSz = (word32)ret;
  23593. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  23594. ERROR_OUT(-12102, exit);
  23595. }
  23596. if (XMEMCMP(d, sample_text, dSz) != 0) {
  23597. ERROR_OUT(-12103, exit);
  23598. }
  23599. /* GZIP tests */
  23600. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  23601. XMEMSET(c, 0, cSz);
  23602. XMEMSET(d, 0, dSz);
  23603. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  23604. if (ret < 0) {
  23605. ERROR_OUT(-12104, exit);
  23606. }
  23607. cSz = (word32)ret;
  23608. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  23609. if (ret < 0) {
  23610. ERROR_OUT(-12105, exit);
  23611. }
  23612. if (XMEMCMP(d, sample_text, dSz) != 0) {
  23613. ERROR_OUT(-12106, exit);
  23614. }
  23615. /* Try with gzip generated output */
  23616. XMEMSET(d, 0, dSz);
  23617. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  23618. LIBZ_WINBITS_GZIP);
  23619. if (ret < 0) {
  23620. ERROR_OUT(-12107, exit);
  23621. }
  23622. dSz = (word32)ret;
  23623. if (XMEMCMP(d, sample_text, dSz) != 0) {
  23624. ERROR_OUT(-12108, exit);
  23625. }
  23626. ret = 0; /* success */
  23627. exit:
  23628. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23629. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23630. return ret;
  23631. }
  23632. #endif /* HAVE_LIBZ */
  23633. #ifdef HAVE_PKCS7
  23634. /* External Debugging/Testing Note:
  23635. *
  23636. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  23637. * additional testing. To dump bundles to files DER encoded files, please
  23638. * define:
  23639. *
  23640. * #define PKCS7_OUTPUT_TEST_BUNDLES
  23641. */
  23642. /* Loads certs and keys for use with PKCS7 tests, from either files
  23643. * or buffers.
  23644. *
  23645. * rsaClientCertBuf - output buffer for RSA client cert
  23646. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  23647. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  23648. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  23649. *
  23650. * rsaServerCertBuf - output buffer for RSA server cert
  23651. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  23652. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  23653. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  23654. *
  23655. * rsaCaCertBuf - output buffer for RSA CA cert
  23656. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  23657. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  23658. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  23659. *
  23660. * eccClientCertBuf - output buffer for ECC cert
  23661. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  23662. * eccClientPrivKeyBuf - output buffer for ECC private key
  23663. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  23664. *
  23665. * Returns 0 on success, negative on error
  23666. */
  23667. static int pkcs7_load_certs_keys(
  23668. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  23669. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  23670. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  23671. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  23672. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  23673. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  23674. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  23675. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  23676. {
  23677. #ifndef NO_FILESYSTEM
  23678. XFILE certFile;
  23679. XFILE keyFile;
  23680. (void)certFile;
  23681. (void)keyFile;
  23682. #endif
  23683. #ifndef NO_RSA
  23684. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  23685. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  23686. return BAD_FUNC_ARG;
  23687. #endif
  23688. #ifdef HAVE_ECC
  23689. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  23690. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  23691. return BAD_FUNC_ARG;
  23692. #endif
  23693. /* RSA */
  23694. #ifndef NO_RSA
  23695. #ifdef USE_CERT_BUFFERS_1024
  23696. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  23697. return -12110;
  23698. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  23699. sizeof_client_cert_der_1024);
  23700. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  23701. if (rsaServerCertBuf != NULL) {
  23702. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  23703. return -12111;
  23704. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  23705. sizeof_server_cert_der_1024);
  23706. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  23707. }
  23708. if (rsaCaCertBuf != NULL) {
  23709. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  23710. return -12112;
  23711. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  23712. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  23713. }
  23714. #elif defined(USE_CERT_BUFFERS_2048)
  23715. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  23716. return -12113;
  23717. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  23718. sizeof_client_cert_der_2048);
  23719. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  23720. if (rsaServerCertBuf != NULL) {
  23721. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  23722. return -12114;
  23723. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  23724. sizeof_server_cert_der_2048);
  23725. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  23726. }
  23727. if (rsaCaCertBuf != NULL) {
  23728. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  23729. return -12115;
  23730. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  23731. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  23732. }
  23733. #else
  23734. certFile = XFOPEN(clientCert, "rb");
  23735. if (!certFile)
  23736. return -12116;
  23737. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  23738. *rsaClientCertBufSz, certFile);
  23739. XFCLOSE(certFile);
  23740. if (rsaServerCertBuf != NULL) {
  23741. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  23742. if (!certFile)
  23743. return -12117;
  23744. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  23745. *rsaServerCertBufSz, certFile);
  23746. XFCLOSE(certFile);
  23747. }
  23748. if (rsaCaCertBuf != NULL) {
  23749. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  23750. if (!certFile)
  23751. return -12118;
  23752. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  23753. certFile);
  23754. XFCLOSE(certFile);
  23755. }
  23756. #endif
  23757. #ifdef USE_CERT_BUFFERS_1024
  23758. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  23759. return -12119;
  23760. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  23761. sizeof_client_key_der_1024);
  23762. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  23763. if (rsaServerPrivKeyBuf != NULL) {
  23764. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  23765. return -12120;
  23766. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  23767. sizeof_server_key_der_1024);
  23768. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  23769. }
  23770. if (rsaCaPrivKeyBuf != NULL) {
  23771. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  23772. return -12121;
  23773. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  23774. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  23775. }
  23776. #elif defined(USE_CERT_BUFFERS_2048)
  23777. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  23778. return -12122;
  23779. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  23780. sizeof_client_key_der_2048);
  23781. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  23782. if (rsaServerPrivKeyBuf != NULL) {
  23783. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  23784. return -12123;
  23785. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  23786. sizeof_server_key_der_2048);
  23787. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  23788. }
  23789. if (rsaCaPrivKeyBuf != NULL) {
  23790. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  23791. return -12124;
  23792. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  23793. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  23794. }
  23795. #else
  23796. keyFile = XFOPEN(clientKey, "rb");
  23797. if (!keyFile)
  23798. return -12125;
  23799. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  23800. *rsaClientPrivKeyBufSz, keyFile);
  23801. XFCLOSE(keyFile);
  23802. if (rsaServerPrivKeyBuf != NULL) {
  23803. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  23804. if (!keyFile)
  23805. return -12126;
  23806. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  23807. *rsaServerPrivKeyBufSz, keyFile);
  23808. XFCLOSE(keyFile);
  23809. }
  23810. if (rsaCaPrivKeyBuf != NULL) {
  23811. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  23812. if (!keyFile)
  23813. return -12127;
  23814. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  23815. *rsaCaPrivKeyBufSz, keyFile);
  23816. XFCLOSE(keyFile);
  23817. }
  23818. #endif /* USE_CERT_BUFFERS */
  23819. #endif /* NO_RSA */
  23820. /* ECC */
  23821. #ifdef HAVE_ECC
  23822. #ifdef USE_CERT_BUFFERS_256
  23823. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  23824. return -12128;
  23825. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23826. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  23827. #else
  23828. certFile = XFOPEN(eccClientCert, "rb");
  23829. if (!certFile)
  23830. return -12129;
  23831. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  23832. *eccClientCertBufSz, certFile);
  23833. XFCLOSE(certFile);
  23834. #endif /* USE_CERT_BUFFERS_256 */
  23835. #ifdef USE_CERT_BUFFERS_256
  23836. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  23837. return -12130;
  23838. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23839. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  23840. #else
  23841. keyFile = XFOPEN(eccClientKey, "rb");
  23842. if (!keyFile)
  23843. return -12131;
  23844. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  23845. *eccClientPrivKeyBufSz, keyFile);
  23846. XFCLOSE(keyFile);
  23847. #endif /* USE_CERT_BUFFERS_256 */
  23848. #endif /* HAVE_ECC */
  23849. #ifdef NO_RSA
  23850. (void)rsaClientCertBuf;
  23851. (void)rsaClientCertBufSz;
  23852. (void)rsaClientPrivKeyBuf;
  23853. (void)rsaClientPrivKeyBufSz;
  23854. (void)rsaServerCertBuf;
  23855. (void)rsaServerCertBufSz;
  23856. (void)rsaServerPrivKeyBuf;
  23857. (void)rsaServerPrivKeyBufSz;
  23858. (void)rsaCaCertBuf;
  23859. (void)rsaCaCertBufSz;
  23860. (void)rsaCaPrivKeyBuf;
  23861. (void)rsaCaPrivKeyBufSz;
  23862. #endif
  23863. #ifndef HAVE_ECC
  23864. (void)eccClientCertBuf;
  23865. (void)eccClientCertBufSz;
  23866. (void)eccClientPrivKeyBuf;
  23867. (void)eccClientPrivKeyBufSz;
  23868. #endif
  23869. #ifndef NO_FILESYSTEM
  23870. (void)certFile;
  23871. (void)keyFile;
  23872. #endif
  23873. return 0;
  23874. }
  23875. typedef struct {
  23876. const byte* content;
  23877. word32 contentSz;
  23878. int contentOID;
  23879. int encryptOID;
  23880. int keyWrapOID;
  23881. int keyAgreeOID;
  23882. byte* cert;
  23883. size_t certSz;
  23884. byte* privateKey;
  23885. word32 privateKeySz;
  23886. byte* optionalUkm;
  23887. word32 optionalUkmSz;
  23888. int ktriOptions; /* KTRI options flags */
  23889. int kariOptions; /* KARI options flags */
  23890. /* KEKRI specific */
  23891. byte* secretKey; /* key, only for kekri RecipientInfo types */
  23892. word32 secretKeySz; /* size of secretKey, bytes */
  23893. byte* secretKeyId; /* key identifier */
  23894. word32 secretKeyIdSz; /* size of key identifier, bytes */
  23895. void* timePtr; /* time_t pointer */
  23896. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  23897. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  23898. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  23899. word32 otherAttrSz; /* size of otherAttr, bytes */
  23900. int kekriOptions; /* KEKRI options flags */
  23901. /* PWRI specific */
  23902. char* password;
  23903. word32 passwordSz;
  23904. byte* salt;
  23905. word32 saltSz;
  23906. int kdfOID;
  23907. int hashOID;
  23908. int kdfIterations;
  23909. int pwriOptions; /* PWRI options flags */
  23910. /* ORI specific */
  23911. int isOri;
  23912. int oriOptions; /* ORI options flags */
  23913. const char* outFileName;
  23914. } pkcs7EnvelopedVector;
  23915. static const byte asnDataOid[] = {
  23916. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  23917. };
  23918. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  23919. * and giving wolfCrypt the value for oriOID and oriValue to place in
  23920. * OtherRecipientInfo.
  23921. *
  23922. * Returns 0 on success, negative upon error. */
  23923. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  23924. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  23925. void* ctx)
  23926. {
  23927. int i;
  23928. /* make sure buffers are large enough */
  23929. if ((*oriValueSz < (2 + cekSz)) || (*oriTypeSz < sizeof(oriType)))
  23930. return -12140;
  23931. /* our simple encryption algorithm will be take the bitwise complement */
  23932. oriValue[0] = 0x04; /*ASN OCTET STRING */
  23933. oriValue[1] = (byte)cekSz; /* length */
  23934. for (i = 0; i < (int)cekSz; i++) {
  23935. oriValue[2 + i] = ~cek[i];
  23936. }
  23937. *oriValueSz = 2 + cekSz;
  23938. /* set oriType to ASN.1 encoded data OID */
  23939. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  23940. *oriTypeSz = sizeof(asnDataOid);
  23941. (void)pkcs7;
  23942. (void)ctx;
  23943. return 0;
  23944. }
  23945. /* ORI decrypt callback, responsible for providing a decrypted content
  23946. * encryption key (CEK) placed into decryptedKey and size placed into
  23947. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  23948. * in decrypting the encrypted CEK.
  23949. *
  23950. * Returns 0 on success, negative upon error. */
  23951. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  23952. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  23953. word32* decryptedKeySz, void* ctx)
  23954. {
  23955. int i;
  23956. /* make sure oriType matches what we expect */
  23957. if (oriTypeSz != sizeof(asnDataOid))
  23958. return -12150;
  23959. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  23960. return -12151;
  23961. /* make sure decrypted buffer is large enough */
  23962. if (*decryptedKeySz < oriValueSz)
  23963. return -12152;
  23964. /* decrypt encrypted CEK using simple bitwise complement,
  23965. only for example */
  23966. for (i = 0; i < (int)oriValueSz - 2; i++) {
  23967. decryptedKey[i] = ~oriValue[2 + i];
  23968. }
  23969. *decryptedKeySz = oriValueSz - 2;
  23970. (void)pkcs7;
  23971. (void)ctx;
  23972. return 0;
  23973. }
  23974. #ifndef NO_AES
  23975. /* returns 0 on success */
  23976. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  23977. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  23978. byte* in, int inSz, byte* out, void* usrCtx)
  23979. {
  23980. int keyId = -1, ret, keySz;
  23981. word32 keyIdSz = 8;
  23982. const byte* key;
  23983. byte keyIdRaw[8];
  23984. #ifdef WOLFSSL_SMALL_STACK
  23985. Aes *aes;
  23986. #else
  23987. Aes aes[1];
  23988. #endif
  23989. /* looking for KEY ID
  23990. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  23991. */
  23992. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  23993. /* 0x06, 0x0B do not pass in tag and length */
  23994. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  23995. 0x01, 0x09, 0x10, 0x02, 0x25
  23996. };
  23997. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  23998. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23999. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24000. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24001. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24002. };
  24003. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  24004. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24005. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24006. };
  24007. /* test user context passed in */
  24008. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  24009. return -12160;
  24010. }
  24011. #ifdef WOLFSSL_SMALL_STACK
  24012. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  24013. return -12164;
  24014. #endif
  24015. /* if needing to find keyIdSz can call with NULL */
  24016. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  24017. &keyIdSz);
  24018. if (ret != LENGTH_ONLY_E) {
  24019. printf("Unexpected error %d when getting keyIdSz\n", ret);
  24020. printf("Possibly no KEY ID attribute set\n");
  24021. ERROR_OUT(-12161, out);
  24022. }
  24023. else {
  24024. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  24025. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  24026. &keyIdSz);
  24027. if (ret < 0) {
  24028. goto out;
  24029. }
  24030. if (keyIdSz < 3) {
  24031. printf("keyIdSz is smaller than expected\n");
  24032. ERROR_OUT(-12162, out);
  24033. }
  24034. if (keyIdSz > 2 + sizeof(int)) {
  24035. printf("example case was only expecting a keyId of int size\n");
  24036. ERROR_OUT(-12163, out);
  24037. }
  24038. /* keyIdRaw[0] OCTET TAG */
  24039. /* keyIdRaw[1] Length */
  24040. #ifdef BIG_ENDIAN_ORDER
  24041. if (keyIdRaw[1] == 0x01) {
  24042. keyId = 1;
  24043. }
  24044. #else
  24045. keyId = *(int*)(keyIdRaw + 2);
  24046. #endif
  24047. }
  24048. /* Use keyID here if found to select key and decrypt in HSM or in this
  24049. * example just select key and do software decryption */
  24050. if (keyId == 1) {
  24051. key = altKey;
  24052. keySz = sizeof(altKey);
  24053. }
  24054. else {
  24055. key = defKey;
  24056. keySz = sizeof(defKey);
  24057. }
  24058. switch (encryptOID) {
  24059. case AES256CBCb:
  24060. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  24061. ERROR_OUT(BAD_FUNC_ARG, out);
  24062. break;
  24063. case AES128CBCb:
  24064. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  24065. ERROR_OUT(BAD_FUNC_ARG, out);
  24066. break;
  24067. default:
  24068. printf("Unsupported content cipher type for example");
  24069. ERROR_OUT(ALGO_ID_E, out);
  24070. };
  24071. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  24072. if (ret == 0) {
  24073. ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
  24074. if (ret == 0)
  24075. ret = wc_AesCbcDecrypt(aes, out, in, inSz);
  24076. wc_AesFree(aes);
  24077. }
  24078. out:
  24079. #ifdef WOLFSSL_SMALL_STACK
  24080. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24081. #endif
  24082. (void)aad;
  24083. (void)aadSz;
  24084. (void)authTag;
  24085. (void)authTagSz;
  24086. return ret;
  24087. }
  24088. #endif /* NO_AES */
  24089. #define PKCS7_BUF_SIZE 2048
  24090. static int pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  24091. byte* rsaPrivKey, word32 rsaPrivKeySz,
  24092. byte* eccCert, word32 eccCertSz,
  24093. byte* eccPrivKey, word32 eccPrivKeySz)
  24094. {
  24095. int ret = 0, testSz, i;
  24096. int envelopedSz, decodedSz;
  24097. byte *enveloped;
  24098. byte *decoded;
  24099. PKCS7* pkcs7;
  24100. #ifdef ECC_TIMING_RESISTANT
  24101. WC_RNG rng;
  24102. #endif
  24103. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  24104. XFILE pkcs7File;
  24105. #endif
  24106. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  24107. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  24108. 0x72,0x6c,0x64
  24109. };
  24110. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  24111. defined(WOLFSSL_SHA512)
  24112. byte optionalUkm[] = {
  24113. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  24114. };
  24115. #endif /* NO_AES */
  24116. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24117. /* encryption key for kekri recipient types */
  24118. byte secretKey[] = {
  24119. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  24120. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  24121. };
  24122. /* encryption key identifier */
  24123. byte secretKeyId[] = {
  24124. 0x02,0x02,0x03,0x04
  24125. };
  24126. #endif
  24127. #if !defined(NO_PWDBASED) && !defined(NO_AES) && \
  24128. !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24129. #ifndef HAVE_FIPS
  24130. char password[] = "password"; /* NOTE: Password is too short for FIPS */
  24131. #else
  24132. char password[] = "passwordFIPS_MODE";
  24133. #endif
  24134. byte salt[] = {
  24135. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  24136. };
  24137. #endif
  24138. const pkcs7EnvelopedVector testVectors[] =
  24139. {
  24140. /* key transport key encryption technique */
  24141. #ifndef NO_RSA
  24142. #ifndef NO_DES3
  24143. {data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  24144. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  24145. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24146. "pkcs7envelopedDataDES3.der"},
  24147. #endif
  24148. #ifndef NO_AES
  24149. #ifdef WOLFSSL_AES_128
  24150. {data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  24151. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  24152. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24153. "pkcs7envelopedDataAES128CBC.der"},
  24154. #endif
  24155. #ifdef WOLFSSL_AES_192
  24156. {data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  24157. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  24158. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24159. "pkcs7envelopedDataAES192CBC.der"},
  24160. #endif
  24161. #ifdef WOLFSSL_AES_256
  24162. {data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  24163. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  24164. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24165. "pkcs7envelopedDataAES256CBC.der"},
  24166. /* explicitly using SKID for SubjectKeyIdentifier */
  24167. {data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  24168. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  24169. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24170. "pkcs7envelopedDataAES256CBC_SKID.der"},
  24171. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  24172. {data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  24173. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  24174. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  24175. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der"},
  24176. #endif
  24177. #endif /* NO_AES */
  24178. #endif
  24179. /* key agreement key encryption technique*/
  24180. #ifdef HAVE_ECC
  24181. #ifndef NO_AES
  24182. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24183. {data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  24184. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24185. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  24186. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24187. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der"},
  24188. #endif
  24189. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  24190. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  24191. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24192. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  24193. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24194. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der"},
  24195. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  24196. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  24197. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  24198. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24199. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  24200. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24201. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der"},
  24202. /* with optional user keying material (ukm) */
  24203. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  24204. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24205. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  24206. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24207. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der"},
  24208. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  24209. #endif /* NO_AES */
  24210. #endif
  24211. /* kekri (KEKRecipientInfo) recipient types */
  24212. #ifndef NO_AES
  24213. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24214. {data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  24215. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  24216. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  24217. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24218. "pkcs7envelopedDataAES128CBC_KEKRI.der"},
  24219. #endif
  24220. #endif
  24221. /* pwri (PasswordRecipientInfo) recipient types */
  24222. #if !defined(NO_PWDBASED) && !defined(NO_AES)
  24223. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24224. {data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  24225. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  24226. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  24227. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  24228. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der"},
  24229. #endif
  24230. #endif
  24231. #if !defined(NO_AES) && !defined(NO_AES_128)
  24232. /* ori (OtherRecipientInfo) recipient types */
  24233. {data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  24234. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  24235. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der"},
  24236. #endif
  24237. };
  24238. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24239. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24240. if ((! enveloped) || (! decoded)) {
  24241. ERROR_OUT(-12170, out);
  24242. }
  24243. testSz = sizeof(testVectors) / sizeof(pkcs7EnvelopedVector);
  24244. #ifdef ECC_TIMING_RESISTANT
  24245. #ifndef HAVE_FIPS
  24246. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24247. #else
  24248. ret = wc_InitRng(&rng);
  24249. #endif
  24250. if (ret != 0) {
  24251. ERROR_OUT(-12171, out);
  24252. }
  24253. #endif
  24254. for (i = 0; i < testSz; i++) {
  24255. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  24256. #ifdef WOLFSSL_ASYNC_CRYPT
  24257. INVALID_DEVID /* async PKCS7 is not supported */
  24258. #else
  24259. devId
  24260. #endif
  24261. );
  24262. if (pkcs7 == NULL) {
  24263. ERROR_OUT(-12172, out);
  24264. }
  24265. if (testVectors[i].secretKey != NULL) {
  24266. /* KEKRI recipient type */
  24267. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  24268. if (ret != 0) {
  24269. ERROR_OUT(-12173, out);
  24270. }
  24271. pkcs7->content = (byte*)testVectors[i].content;
  24272. pkcs7->contentSz = testVectors[i].contentSz;
  24273. pkcs7->contentOID = testVectors[i].contentOID;
  24274. pkcs7->encryptOID = testVectors[i].encryptOID;
  24275. pkcs7->ukm = testVectors[i].optionalUkm;
  24276. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  24277. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  24278. testVectors[i].secretKey, testVectors[i].secretKeySz,
  24279. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  24280. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  24281. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  24282. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  24283. if (ret < 0) {
  24284. wc_PKCS7_Free(pkcs7);
  24285. ERROR_OUT(-12174, out);
  24286. }
  24287. /* set key, for decryption */
  24288. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  24289. testVectors[i].secretKeySz);
  24290. if (ret != 0) {
  24291. wc_PKCS7_Free(pkcs7);
  24292. ERROR_OUT(-12175, out);
  24293. }
  24294. } else if (testVectors[i].password != NULL) {
  24295. #ifndef NO_PWDBASED
  24296. /* PWRI recipient type */
  24297. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  24298. if (ret != 0) {
  24299. ERROR_OUT(-12176, out);
  24300. }
  24301. pkcs7->content = (byte*)testVectors[i].content;
  24302. pkcs7->contentSz = testVectors[i].contentSz;
  24303. pkcs7->contentOID = testVectors[i].contentOID;
  24304. pkcs7->encryptOID = testVectors[i].encryptOID;
  24305. pkcs7->ukm = testVectors[i].optionalUkm;
  24306. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  24307. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  24308. (byte*)testVectors[i].password,
  24309. testVectors[i].passwordSz, testVectors[i].salt,
  24310. testVectors[i].saltSz, testVectors[i].kdfOID,
  24311. testVectors[i].hashOID, testVectors[i].kdfIterations,
  24312. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  24313. if (ret < 0) {
  24314. wc_PKCS7_Free(pkcs7);
  24315. ERROR_OUT(-12177, out);
  24316. }
  24317. /* set password, for decryption */
  24318. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  24319. testVectors[i].passwordSz);
  24320. if (ret < 0) {
  24321. wc_PKCS7_Free(pkcs7);
  24322. ERROR_OUT(-12178, out);
  24323. }
  24324. #endif /* NO_PWDBASED */
  24325. } else if (testVectors[i].isOri == 1) {
  24326. /* ORI recipient type */
  24327. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  24328. if (ret != 0) {
  24329. ERROR_OUT(-12179, out);
  24330. }
  24331. pkcs7->content = (byte*)testVectors[i].content;
  24332. pkcs7->contentSz = testVectors[i].contentSz;
  24333. pkcs7->contentOID = testVectors[i].contentOID;
  24334. pkcs7->encryptOID = testVectors[i].encryptOID;
  24335. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  24336. testVectors[i].oriOptions);
  24337. if (ret < 0) {
  24338. wc_PKCS7_Free(pkcs7);
  24339. ERROR_OUT(-12180, out);
  24340. }
  24341. /* set decrypt callback for decryption */
  24342. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  24343. if (ret < 0) {
  24344. wc_PKCS7_Free(pkcs7);
  24345. ERROR_OUT(-12181, out);
  24346. }
  24347. } else {
  24348. /* KTRI or KARI recipient types */
  24349. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  24350. if (ret != 0) {
  24351. ERROR_OUT(-12182, out);
  24352. }
  24353. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  24354. (word32)testVectors[i].certSz);
  24355. if (ret != 0) {
  24356. wc_PKCS7_Free(pkcs7);
  24357. ERROR_OUT(-12183, out);
  24358. }
  24359. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  24360. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  24361. pkcs7->privateKey = testVectors[i].privateKey;
  24362. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  24363. pkcs7->content = (byte*)testVectors[i].content;
  24364. pkcs7->contentSz = testVectors[i].contentSz;
  24365. pkcs7->contentOID = testVectors[i].contentOID;
  24366. pkcs7->encryptOID = testVectors[i].encryptOID;
  24367. pkcs7->ukm = testVectors[i].optionalUkm;
  24368. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  24369. /* set SubjectIdentifier type for KTRI types */
  24370. if (testVectors[i].ktriOptions & CMS_SKID) {
  24371. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  24372. if (ret != 0) {
  24373. wc_PKCS7_Free(pkcs7);
  24374. ERROR_OUT(-12184, out);
  24375. }
  24376. } else if (testVectors[i].ktriOptions &
  24377. CMS_ISSUER_AND_SERIAL_NUMBER) {
  24378. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  24379. CMS_ISSUER_AND_SERIAL_NUMBER);
  24380. if (ret != 0) {
  24381. wc_PKCS7_Free(pkcs7);
  24382. ERROR_OUT(-12185, out);
  24383. }
  24384. }
  24385. }
  24386. #ifdef ECC_TIMING_RESISTANT
  24387. pkcs7->rng = &rng;
  24388. #endif
  24389. /* encode envelopedData */
  24390. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  24391. PKCS7_BUF_SIZE);
  24392. if (envelopedSz <= 0) {
  24393. wc_PKCS7_Free(pkcs7);
  24394. ERROR_OUT(-12186, out);
  24395. }
  24396. /* decode envelopedData */
  24397. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, envelopedSz,
  24398. decoded, PKCS7_BUF_SIZE);
  24399. if (decodedSz <= 0) {
  24400. wc_PKCS7_Free(pkcs7);
  24401. ERROR_OUT(-12187, out);
  24402. }
  24403. /* test decode result */
  24404. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  24405. wc_PKCS7_Free(pkcs7);
  24406. ERROR_OUT(-12188, out);
  24407. }
  24408. #ifndef NO_PKCS7_STREAM
  24409. { /* test reading byte by byte */
  24410. int z;
  24411. for (z = 0; z < envelopedSz; z++) {
  24412. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  24413. decoded, PKCS7_BUF_SIZE);
  24414. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  24415. printf("unexpected error %d\n", decodedSz);
  24416. ERROR_OUT(-12189, out);
  24417. }
  24418. }
  24419. /* test decode result */
  24420. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  24421. printf("stream read compare failed\n");
  24422. wc_PKCS7_Free(pkcs7);
  24423. ERROR_OUT(-12190, out);
  24424. }
  24425. }
  24426. #endif
  24427. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  24428. /* output pkcs7 envelopedData for external testing */
  24429. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  24430. if (!pkcs7File) {
  24431. wc_PKCS7_Free(pkcs7);
  24432. ERROR_OUT(-12191, out);
  24433. }
  24434. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  24435. XFCLOSE(pkcs7File);
  24436. if (ret != envelopedSz) {
  24437. wc_PKCS7_Free(pkcs7);
  24438. ERROR_OUT(-12192, out);
  24439. }
  24440. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  24441. wc_PKCS7_Free(pkcs7);
  24442. pkcs7 = NULL;
  24443. }
  24444. #ifdef ECC_TIMING_RESISTANT
  24445. wc_FreeRng(&rng);
  24446. #endif
  24447. (void)eccCert;
  24448. (void)eccCertSz;
  24449. (void)eccPrivKey;
  24450. (void)eccPrivKeySz;
  24451. (void)rsaCert;
  24452. (void)rsaCertSz;
  24453. (void)rsaPrivKey;
  24454. (void)rsaPrivKeySz;
  24455. out:
  24456. if (enveloped)
  24457. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24458. if (decoded)
  24459. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24460. return ret;
  24461. }
  24462. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void)
  24463. {
  24464. int ret = 0;
  24465. byte* rsaCert = NULL;
  24466. byte* rsaPrivKey = NULL;
  24467. word32 rsaCertSz = 0;
  24468. word32 rsaPrivKeySz = 0;
  24469. byte* eccCert = NULL;
  24470. byte* eccPrivKey = NULL;
  24471. word32 eccCertSz = 0;
  24472. word32 eccPrivKeySz = 0;
  24473. #ifndef NO_RSA
  24474. /* read client RSA cert and key in DER format */
  24475. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24476. if (rsaCert == NULL)
  24477. return -12200;
  24478. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24479. if (rsaPrivKey == NULL) {
  24480. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24481. return -12201;
  24482. }
  24483. rsaCertSz = FOURK_BUF;
  24484. rsaPrivKeySz = FOURK_BUF;
  24485. #endif /* NO_RSA */
  24486. #ifdef HAVE_ECC
  24487. /* read client ECC cert and key in DER format */
  24488. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24489. if (eccCert == NULL) {
  24490. #ifndef NO_RSA
  24491. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24492. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24493. #endif
  24494. return -12202;
  24495. }
  24496. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24497. if (eccPrivKey == NULL) {
  24498. #ifndef NO_RSA
  24499. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24500. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24501. #endif
  24502. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24503. return -12203;
  24504. }
  24505. eccCertSz = FOURK_BUF;
  24506. eccPrivKeySz = FOURK_BUF;
  24507. #endif /* HAVE_ECC */
  24508. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  24509. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  24510. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  24511. eccPrivKey, &eccPrivKeySz);
  24512. if (ret < 0) {
  24513. #ifndef NO_RSA
  24514. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24515. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24516. #endif
  24517. #ifdef HAVE_ECC
  24518. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24519. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24520. #endif
  24521. return -12204;
  24522. }
  24523. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  24524. rsaPrivKey, (word32)rsaPrivKeySz,
  24525. eccCert, (word32)eccCertSz,
  24526. eccPrivKey, (word32)eccPrivKeySz);
  24527. #ifndef NO_RSA
  24528. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24529. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24530. #endif
  24531. #ifdef HAVE_ECC
  24532. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24533. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24534. #endif
  24535. return ret;
  24536. }
  24537. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  24538. typedef struct {
  24539. const byte* content;
  24540. word32 contentSz;
  24541. int contentOID;
  24542. int encryptOID;
  24543. int keyWrapOID;
  24544. int keyAgreeOID;
  24545. byte* cert;
  24546. size_t certSz;
  24547. byte* privateKey;
  24548. word32 privateKeySz;
  24549. PKCS7Attrib* authAttribs;
  24550. word32 authAttribsSz;
  24551. PKCS7Attrib* unauthAttribs;
  24552. word32 unauthAttribsSz;
  24553. /* KARI / KTRI specific */
  24554. byte* optionalUkm;
  24555. word32 optionalUkmSz;
  24556. int ktriOptions; /* KTRI options flags */
  24557. int kariOptions; /* KARI options flags */
  24558. /* KEKRI specific */
  24559. byte* secretKey; /* key, only for kekri RecipientInfo types */
  24560. word32 secretKeySz; /* size of secretKey, bytes */
  24561. byte* secretKeyId; /* key identifier */
  24562. word32 secretKeyIdSz; /* size of key identifier, bytes */
  24563. void* timePtr; /* time_t pointer */
  24564. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  24565. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  24566. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  24567. word32 otherAttrSz; /* size of otherAttr, bytes */
  24568. int kekriOptions; /* KEKRI options flags */
  24569. /* PWRI specific */
  24570. char* password; /* password */
  24571. word32 passwordSz; /* password size, bytes */
  24572. byte* salt; /* KDF salt */
  24573. word32 saltSz; /* KDF salt size, bytes */
  24574. int kdfOID; /* KDF OID */
  24575. int hashOID; /* KDF hash algorithm OID */
  24576. int kdfIterations; /* KDF iterations */
  24577. int kekEncryptOID; /* KEK encryption algorithm OID */
  24578. int pwriOptions; /* PWRI options flags */
  24579. /* ORI specific */
  24580. int isOri;
  24581. int oriOptions; /* ORI options flags */
  24582. const char* outFileName;
  24583. } pkcs7AuthEnvelopedVector;
  24584. static int pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  24585. byte* rsaPrivKey, word32 rsaPrivKeySz,
  24586. byte* eccCert, word32 eccCertSz,
  24587. byte* eccPrivKey, word32 eccPrivKeySz)
  24588. {
  24589. int ret = 0, testSz = 0, i;
  24590. int envelopedSz, decodedSz;
  24591. byte *enveloped = NULL;
  24592. byte *decoded = NULL;
  24593. WC_RNG rng;
  24594. PKCS7* pkcs7;
  24595. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  24596. XFILE pkcs7File;
  24597. #endif
  24598. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  24599. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  24600. 0x72,0x6c,0x64
  24601. };
  24602. byte senderNonce[PKCS7_NONCE_SZ + 2];
  24603. #ifdef HAVE_ECC
  24604. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  24605. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24606. 0x09, 0x05 };
  24607. PKCS7Attrib attribs[] =
  24608. {
  24609. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  24610. sizeof(senderNonce) }
  24611. };
  24612. #endif
  24613. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  24614. defined(WOLFSSL_SHA512)
  24615. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  24616. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  24617. };
  24618. #endif /* NO_AES */
  24619. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24620. /* encryption key for kekri recipient types */
  24621. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  24622. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  24623. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  24624. };
  24625. /* encryption key identifier */
  24626. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  24627. 0x02,0x02,0x03,0x04
  24628. };
  24629. #endif
  24630. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  24631. !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24632. #ifndef HAVE_FIPS
  24633. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  24634. #else
  24635. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  24636. #endif
  24637. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  24638. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  24639. };
  24640. #endif
  24641. pkcs7AuthEnvelopedVector *testVectors = NULL;
  24642. {
  24643. #define ADD_PKCS7_TEST_VEC(...) { \
  24644. const pkcs7AuthEnvelopedVector vec = __VA_ARGS__; \
  24645. testVectors = (pkcs7AuthEnvelopedVector *) \
  24646. XREALLOC(testVectors, \
  24647. sizeof *testVectors * (testSz + 1), \
  24648. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); \
  24649. if (testVectors == NULL) \
  24650. ERROR_OUT(-12233, out); \
  24651. XMEMCPY(&testVectors[testSz++], &vec, sizeof *testVectors); \
  24652. }
  24653. /* key transport key encryption technique */
  24654. #ifndef NO_RSA
  24655. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  24656. #ifdef WOLFSSL_AES_128
  24657. ADD_PKCS7_TEST_VEC(
  24658. {data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  24659. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  24660. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  24661. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der"});
  24662. #endif
  24663. #ifdef WOLFSSL_AES_192
  24664. ADD_PKCS7_TEST_VEC(
  24665. {data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  24666. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  24667. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  24668. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der"});
  24669. #endif
  24670. #ifdef WOLFSSL_AES_256
  24671. ADD_PKCS7_TEST_VEC(
  24672. {data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  24673. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  24674. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  24675. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der"});
  24676. /* test with contentType set to FirmwarePkgData */
  24677. ADD_PKCS7_TEST_VEC(
  24678. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  24679. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  24680. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  24681. 0, 0, 0, 0, 0, 0, 0, 0,
  24682. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der"});
  24683. /* explicitly using SKID for SubjectKeyIdentifier */
  24684. ADD_PKCS7_TEST_VEC(
  24685. {data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  24686. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  24687. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  24688. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der"});
  24689. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  24690. ADD_PKCS7_TEST_VEC(
  24691. {data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  24692. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  24693. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  24694. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  24695. "pkcs7authEnvelopedDataAES256GCM_IANDS.der"});
  24696. #endif
  24697. #endif /* NO_AES */
  24698. #endif
  24699. /* key agreement key encryption technique*/
  24700. #ifdef HAVE_ECC
  24701. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  24702. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24703. ADD_PKCS7_TEST_VEC(
  24704. {data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  24705. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24706. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  24707. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  24708. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der"});
  24709. #endif
  24710. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  24711. ADD_PKCS7_TEST_VEC(
  24712. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  24713. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24714. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  24715. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  24716. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der"});
  24717. /* with authenticated attributes */
  24718. ADD_PKCS7_TEST_VEC(
  24719. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  24720. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24721. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  24722. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  24723. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  24724. 0, 0, 0,
  24725. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der"});
  24726. /* with unauthenticated attributes */
  24727. ADD_PKCS7_TEST_VEC(
  24728. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  24729. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24730. eccPrivKeySz, NULL, 0, attribs,
  24731. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  24732. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  24733. 0, 0, 0,
  24734. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der"});
  24735. /* with authenticated AND unauthenticated attributes */
  24736. ADD_PKCS7_TEST_VEC(
  24737. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  24738. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24739. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  24740. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  24741. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  24742. 0, 0, 0, 0, 0, 0,
  24743. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der"});
  24744. /* with authenticated AND unauthenticated attributes AND
  24745. * contentType of FirmwarePkgData */
  24746. ADD_PKCS7_TEST_VEC(
  24747. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  24748. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24749. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  24750. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  24751. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  24752. 0, 0, 0, 0, 0, 0,
  24753. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der"});
  24754. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  24755. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  24756. ADD_PKCS7_TEST_VEC(
  24757. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  24758. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24759. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  24760. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  24761. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der"});
  24762. /* with optional user keying material (ukm) */
  24763. ADD_PKCS7_TEST_VEC(
  24764. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  24765. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24766. eccPrivKeySz, NULL, 0, NULL, 0, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  24767. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  24768. 0, 0, 0, 0, 0, 0,
  24769. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der"});
  24770. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  24771. #endif /* NO_AES */
  24772. #endif
  24773. /* kekri (KEKRecipientInfo) recipient types */
  24774. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  24775. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24776. ADD_PKCS7_TEST_VEC(
  24777. {data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  24778. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  24779. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  24780. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  24781. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der"});
  24782. #endif
  24783. #endif
  24784. /* pwri (PasswordRecipientInfo) recipient types */
  24785. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  24786. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24787. ADD_PKCS7_TEST_VEC(
  24788. {data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  24789. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  24790. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  24791. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  24792. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der"});
  24793. #endif
  24794. #endif
  24795. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  24796. #ifdef WOLFSSL_AES_128
  24797. /* ori (OtherRecipientInfo) recipient types */
  24798. ADD_PKCS7_TEST_VEC(
  24799. {data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  24800. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  24801. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  24802. "pkcs7authEnvelopedDataAES128GCM_ORI.der"});
  24803. #endif
  24804. #endif
  24805. }
  24806. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24807. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24808. if ((! enveloped) || (! decoded)) {
  24809. ERROR_OUT(-12210, out);
  24810. }
  24811. /* generate senderNonce */
  24812. {
  24813. #ifndef HAVE_FIPS
  24814. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24815. #else
  24816. ret = wc_InitRng(&rng);
  24817. #endif
  24818. if (ret != 0) {
  24819. ERROR_OUT(-12211, out);
  24820. }
  24821. senderNonce[0] = 0x04;
  24822. senderNonce[1] = PKCS7_NONCE_SZ;
  24823. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  24824. if (ret != 0) {
  24825. wc_FreeRng(&rng);
  24826. ERROR_OUT(-12212, out);
  24827. }
  24828. }
  24829. for (i = 0; i < testSz; i++) {
  24830. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  24831. #ifdef WOLFSSL_ASYNC_CRYPT
  24832. INVALID_DEVID /* async PKCS7 is not supported */
  24833. #else
  24834. devId
  24835. #endif
  24836. );
  24837. if (pkcs7 == NULL) {
  24838. ERROR_OUT(-12213, out);
  24839. }
  24840. if (testVectors[i].secretKey != NULL) {
  24841. /* KEKRI recipient type */
  24842. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  24843. if (ret != 0) {
  24844. ERROR_OUT(-12214, out);
  24845. }
  24846. pkcs7->content = (byte*)testVectors[i].content;
  24847. pkcs7->contentSz = testVectors[i].contentSz;
  24848. pkcs7->contentOID = testVectors[i].contentOID;
  24849. pkcs7->encryptOID = testVectors[i].encryptOID;
  24850. pkcs7->ukm = testVectors[i].optionalUkm;
  24851. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  24852. pkcs7->authAttribs = testVectors[i].authAttribs;
  24853. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  24854. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  24855. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  24856. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  24857. testVectors[i].secretKey, testVectors[i].secretKeySz,
  24858. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  24859. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  24860. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  24861. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  24862. if (ret < 0) {
  24863. wc_PKCS7_Free(pkcs7);
  24864. ERROR_OUT(-12215, out);
  24865. }
  24866. /* set key, for decryption */
  24867. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  24868. testVectors[i].secretKeySz);
  24869. if (ret != 0) {
  24870. wc_PKCS7_Free(pkcs7);
  24871. ERROR_OUT(-12216, out);
  24872. }
  24873. } else if (testVectors[i].password != NULL) {
  24874. #ifndef NO_PWDBASED
  24875. /* PWRI recipient type */
  24876. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  24877. if (ret != 0) {
  24878. ERROR_OUT(-12217, out);
  24879. }
  24880. pkcs7->content = (byte*)testVectors[i].content;
  24881. pkcs7->contentSz = testVectors[i].contentSz;
  24882. pkcs7->contentOID = testVectors[i].contentOID;
  24883. pkcs7->encryptOID = testVectors[i].encryptOID;
  24884. pkcs7->ukm = testVectors[i].optionalUkm;
  24885. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  24886. pkcs7->authAttribs = testVectors[i].authAttribs;
  24887. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  24888. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  24889. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  24890. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  24891. (byte*)testVectors[i].password,
  24892. testVectors[i].passwordSz, testVectors[i].salt,
  24893. testVectors[i].saltSz, testVectors[i].kdfOID,
  24894. testVectors[i].hashOID, testVectors[i].kdfIterations,
  24895. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  24896. if (ret < 0) {
  24897. wc_PKCS7_Free(pkcs7);
  24898. ERROR_OUT(-12218, out);
  24899. }
  24900. /* set password, for decryption */
  24901. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  24902. testVectors[i].passwordSz);
  24903. if (ret < 0) {
  24904. wc_PKCS7_Free(pkcs7);
  24905. ERROR_OUT(-12219, out);
  24906. }
  24907. #endif /* NO_PWDBASED */
  24908. } else if (testVectors[i].isOri == 1) {
  24909. /* ORI recipient type */
  24910. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  24911. if (ret != 0) {
  24912. ERROR_OUT(-12220, out);
  24913. }
  24914. pkcs7->content = (byte*)testVectors[i].content;
  24915. pkcs7->contentSz = testVectors[i].contentSz;
  24916. pkcs7->contentOID = testVectors[i].contentOID;
  24917. pkcs7->encryptOID = testVectors[i].encryptOID;
  24918. pkcs7->authAttribs = testVectors[i].authAttribs;
  24919. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  24920. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  24921. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  24922. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  24923. testVectors[i].oriOptions);
  24924. if (ret < 0) {
  24925. wc_PKCS7_Free(pkcs7);
  24926. ERROR_OUT(-12221, out);
  24927. }
  24928. /* set decrypt callback for decryption */
  24929. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  24930. if (ret < 0) {
  24931. wc_PKCS7_Free(pkcs7);
  24932. ERROR_OUT(-12222, out);
  24933. }
  24934. } else {
  24935. /* KTRI or KARI recipient types */
  24936. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  24937. (word32)testVectors[i].certSz);
  24938. if (ret != 0) {
  24939. wc_PKCS7_Free(pkcs7);
  24940. ERROR_OUT(-12223, out);
  24941. }
  24942. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  24943. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  24944. pkcs7->privateKey = testVectors[i].privateKey;
  24945. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  24946. pkcs7->content = (byte*)testVectors[i].content;
  24947. pkcs7->contentSz = testVectors[i].contentSz;
  24948. pkcs7->contentOID = testVectors[i].contentOID;
  24949. pkcs7->encryptOID = testVectors[i].encryptOID;
  24950. pkcs7->ukm = testVectors[i].optionalUkm;
  24951. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  24952. pkcs7->authAttribs = testVectors[i].authAttribs;
  24953. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  24954. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  24955. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  24956. /* set SubjectIdentifier type for KTRI types */
  24957. if (testVectors[i].ktriOptions & CMS_SKID) {
  24958. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  24959. if (ret != 0) {
  24960. wc_PKCS7_Free(pkcs7);
  24961. ERROR_OUT(-12224, out);
  24962. }
  24963. } else if (testVectors[i].ktriOptions &
  24964. CMS_ISSUER_AND_SERIAL_NUMBER) {
  24965. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  24966. CMS_ISSUER_AND_SERIAL_NUMBER);
  24967. if (ret != 0) {
  24968. wc_PKCS7_Free(pkcs7);
  24969. ERROR_OUT(-12225, out);
  24970. }
  24971. }
  24972. }
  24973. #ifdef ECC_TIMING_RESISTANT
  24974. pkcs7->rng = &rng;
  24975. #endif
  24976. /* encode envelopedData */
  24977. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  24978. PKCS7_BUF_SIZE);
  24979. if (envelopedSz <= 0) {
  24980. wc_PKCS7_Free(pkcs7);
  24981. ERROR_OUT(-12226, out);
  24982. }
  24983. #ifndef NO_PKCS7_STREAM
  24984. { /* test reading byte by byte */
  24985. int z;
  24986. for (z = 0; z < envelopedSz; z++) {
  24987. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  24988. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  24989. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  24990. printf("unexpected error %d\n", decodedSz);
  24991. ERROR_OUT(-12227, out);
  24992. }
  24993. }
  24994. /* test decode result */
  24995. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  24996. printf("stream read compare failed\n");
  24997. wc_PKCS7_Free(pkcs7);
  24998. ERROR_OUT(-12228, out);
  24999. }
  25000. }
  25001. #endif
  25002. /* decode envelopedData */
  25003. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  25004. envelopedSz, decoded,
  25005. PKCS7_BUF_SIZE);
  25006. if (decodedSz <= 0) {
  25007. wc_PKCS7_Free(pkcs7);
  25008. ERROR_OUT(-12229, out);
  25009. }
  25010. /* test decode result */
  25011. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  25012. wc_PKCS7_Free(pkcs7);
  25013. ERROR_OUT(-12230, out);
  25014. }
  25015. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  25016. /* output pkcs7 envelopedData for external testing */
  25017. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  25018. if (!pkcs7File) {
  25019. wc_PKCS7_Free(pkcs7);
  25020. ERROR_OUT(-12231, out);
  25021. }
  25022. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  25023. XFCLOSE(pkcs7File);
  25024. if (ret != envelopedSz) {
  25025. wc_PKCS7_Free(pkcs7);
  25026. ERROR_OUT(-12232, out);
  25027. }
  25028. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  25029. wc_PKCS7_Free(pkcs7);
  25030. pkcs7 = NULL;
  25031. }
  25032. wc_FreeRng(&rng);
  25033. #if !defined(HAVE_ECC) || defined(NO_AES)
  25034. (void)eccCert;
  25035. (void)eccCertSz;
  25036. (void)eccPrivKey;
  25037. (void)eccPrivKeySz;
  25038. (void)secretKey;
  25039. (void)secretKeyId;
  25040. #endif
  25041. #ifdef NO_RSA
  25042. (void)rsaCert;
  25043. (void)rsaCertSz;
  25044. (void)rsaPrivKey;
  25045. (void)rsaPrivKeySz;
  25046. #endif
  25047. out:
  25048. if (testVectors)
  25049. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25050. if (enveloped)
  25051. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25052. if (decoded)
  25053. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25054. return ret;
  25055. }
  25056. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void)
  25057. {
  25058. int ret = 0;
  25059. byte* rsaCert = NULL;
  25060. byte* rsaPrivKey = NULL;
  25061. word32 rsaCertSz = 0;
  25062. word32 rsaPrivKeySz = 0;
  25063. byte* eccCert = NULL;
  25064. byte* eccPrivKey = NULL;
  25065. word32 eccCertSz = 0;
  25066. word32 eccPrivKeySz = 0;
  25067. #ifndef NO_RSA
  25068. /* read client RSA cert and key in DER format */
  25069. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25070. if (rsaCert == NULL)
  25071. return -12300;
  25072. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25073. if (rsaPrivKey == NULL) {
  25074. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25075. return -12301;
  25076. }
  25077. rsaCertSz = FOURK_BUF;
  25078. rsaPrivKeySz = FOURK_BUF;
  25079. #endif /* NO_RSA */
  25080. #ifdef HAVE_ECC
  25081. /* read client ECC cert and key in DER format */
  25082. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25083. if (eccCert == NULL) {
  25084. #ifndef NO_RSA
  25085. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25086. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25087. #endif
  25088. return -12302;
  25089. }
  25090. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25091. if (eccPrivKey == NULL) {
  25092. #ifndef NO_RSA
  25093. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25094. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25095. #endif
  25096. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25097. return -12303;
  25098. }
  25099. eccCertSz = FOURK_BUF;
  25100. eccPrivKeySz = FOURK_BUF;
  25101. #endif /* HAVE_ECC */
  25102. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  25103. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  25104. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  25105. eccPrivKey, &eccPrivKeySz);
  25106. if (ret < 0) {
  25107. #ifndef NO_RSA
  25108. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25109. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25110. #endif
  25111. #ifdef HAVE_ECC
  25112. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25113. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25114. #endif
  25115. return -12304;
  25116. }
  25117. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  25118. rsaPrivKey, (word32)rsaPrivKeySz,
  25119. eccCert, (word32)eccCertSz,
  25120. eccPrivKey, (word32)eccPrivKeySz);
  25121. #ifndef NO_RSA
  25122. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25123. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25124. #endif
  25125. #ifdef HAVE_ECC
  25126. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25127. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25128. #endif
  25129. return ret;
  25130. }
  25131. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  25132. #ifndef NO_AES
  25133. static const byte p7DefKey[] = {
  25134. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25135. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25136. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25137. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25138. };
  25139. static const byte p7AltKey[] = {
  25140. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25141. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25142. };
  25143. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  25144. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  25145. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  25146. {
  25147. int ret;
  25148. if (cek == NULL || out == NULL)
  25149. return BAD_FUNC_ARG;
  25150. /* test case sanity checks */
  25151. if (keyIdSz != 1) {
  25152. return -12310;
  25153. }
  25154. if (keyId[0] != 0x00) {
  25155. return -12311;
  25156. }
  25157. if (type != (int)PKCS7_KEKRI) {
  25158. return -12312;
  25159. }
  25160. switch (keyWrapAlgo) {
  25161. case AES256_WRAP:
  25162. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  25163. out, outSz, NULL);
  25164. if (ret <= 0)
  25165. return ret;
  25166. break;
  25167. default:
  25168. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  25169. return BAD_KEYWRAP_ALG_E;
  25170. };
  25171. (void)pkcs7;
  25172. (void)direction;
  25173. (void)orginKey; /* used with KAKRI */
  25174. (void)orginKeySz;
  25175. return ret;
  25176. }
  25177. /* returns key size on success */
  25178. static int getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  25179. {
  25180. int ret;
  25181. word32 atrSz;
  25182. byte atr[256];
  25183. /* Additionally can look for fwWrappedFirmwareKey
  25184. * 1.2.840.113529.1.9.16.1.16 */
  25185. const unsigned char fwWrappedFirmwareKey[] = {
  25186. /* 0x06, 0x0B */
  25187. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  25188. 0x01, 0x09, 0x10, 0x02, 0x27
  25189. };
  25190. /* find keyID in fwWrappedFirmwareKey */
  25191. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  25192. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  25193. if (ret == LENGTH_ONLY_E) {
  25194. XMEMSET(atr, 0, sizeof(atr));
  25195. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  25196. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  25197. /* keyIdRaw[0] OCTET TAG */
  25198. /* keyIdRaw[1] Length */
  25199. if (ret > 0) {
  25200. PKCS7* envPkcs7;
  25201. envPkcs7 = wc_PKCS7_New(NULL, 0);
  25202. if (envPkcs7 == NULL) {
  25203. return MEMORY_E;
  25204. }
  25205. wc_PKCS7_Init(envPkcs7, NULL, 0);
  25206. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  25207. if (ret == 0) {
  25208. /* expecting FIRMWARE_PKG_DATA content */
  25209. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  25210. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  25211. key, keySz);
  25212. }
  25213. wc_PKCS7_Free(envPkcs7);
  25214. }
  25215. }
  25216. return ret;
  25217. }
  25218. /* create a KEKRI enveloped data
  25219. * return size on success */
  25220. static int envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  25221. word32 outSz)
  25222. {
  25223. int ret;
  25224. PKCS7* pkcs7;
  25225. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  25226. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  25227. if (pkcs7 == NULL)
  25228. return -12330;
  25229. pkcs7->content = in;
  25230. pkcs7->contentSz = inSz;
  25231. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  25232. pkcs7->encryptOID = AES256CBCb;
  25233. pkcs7->ukm = NULL;
  25234. pkcs7->ukmSz = 0;
  25235. /* add recipient (KEKRI type) */
  25236. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  25237. sizeof(p7DefKey), (byte*)keyId,
  25238. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  25239. if (ret < 0) {
  25240. printf("wc_PKCS7_AddRecipient_KEKRI() failed, ret = %d\n", ret);
  25241. wc_PKCS7_Free(pkcs7);
  25242. return -12331;
  25243. }
  25244. /* encode envelopedData, returns size */
  25245. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  25246. if (ret <= 0) {
  25247. printf("wc_PKCS7_EncodeEnvelopedData() failed, ret = %d\n", ret);
  25248. wc_PKCS7_Free(pkcs7);
  25249. return -12332;
  25250. }
  25251. wc_PKCS7_Free(pkcs7);
  25252. return ret;
  25253. }
  25254. /*
  25255. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  25256. * returns size of buffer output on success
  25257. */
  25258. static int generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  25259. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  25260. byte* key, word32 keySz)
  25261. {
  25262. int ret, attribNum = 1;
  25263. PKCS7* pkcs7;
  25264. /* KEY ID
  25265. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  25266. */
  25267. const unsigned char fwDecryptKeyID[] = {
  25268. 0x06, 0x0B,
  25269. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  25270. 0x01, 0x09, 0x10, 0x02, 0x25
  25271. };
  25272. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  25273. const unsigned char fwWrappedFirmwareKey[] = {
  25274. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  25275. 0x01, 0x09, 0x10, 0x02, 0x27
  25276. };
  25277. byte keyID[] = { 0x04, 0x01, 0x00 };
  25278. byte env[256];
  25279. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  25280. PKCS7Attrib attribs[] =
  25281. {
  25282. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  25283. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  25284. };
  25285. keyID[2] = keyHint;
  25286. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  25287. if (keyHint == 0) {
  25288. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  25289. sizeof(env));
  25290. if (ret <= 0) {
  25291. return ret;
  25292. }
  25293. attribs[1].valueSz = ret;
  25294. attribNum++;
  25295. }
  25296. /* init PKCS7 */
  25297. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  25298. if (pkcs7 == NULL)
  25299. return -12340;
  25300. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  25301. if (ret != 0) {
  25302. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  25303. wc_PKCS7_Free(pkcs7);
  25304. return -12341;
  25305. }
  25306. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  25307. if (ret != 0) {
  25308. wc_PKCS7_Free(pkcs7);
  25309. return -12342;
  25310. }
  25311. /* encode Signed Encrypted FirmwarePkgData */
  25312. if (encryptKeySz == 16) {
  25313. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  25314. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  25315. (byte*)data, sizeof(data), NULL, 0,
  25316. attribs, attribNum, out, *outSz);
  25317. }
  25318. else {
  25319. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  25320. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  25321. (byte*)data, sizeof(data), NULL, 0,
  25322. attribs, attribNum, out, *outSz);
  25323. }
  25324. if (ret <= 0) {
  25325. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  25326. "ret = %d\n", ret);
  25327. wc_PKCS7_Free(pkcs7);
  25328. return -12343;
  25329. } else {
  25330. *outSz = ret;
  25331. }
  25332. wc_PKCS7_Free(pkcs7);
  25333. return ret;
  25334. }
  25335. /* test verification and decryption of PKCS7 bundle
  25336. * return 0 on success
  25337. */
  25338. static int verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  25339. {
  25340. int ret = 0;
  25341. int usrCtx = 1; /* test value to pass as user context to callback */
  25342. PKCS7* pkcs7;
  25343. byte* sid;
  25344. word32 sidSz;
  25345. byte key[256];
  25346. word32 keySz = sizeof(key);
  25347. byte decoded[FOURK_BUF/2];
  25348. int decodedSz = FOURK_BUF/2;
  25349. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  25350. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  25351. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  25352. 0xD7, 0x85, 0x65, 0xC0
  25353. };
  25354. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  25355. if (pkcs7 == NULL) {
  25356. return MEMORY_E;
  25357. }
  25358. /* Test verify */
  25359. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  25360. if (ret != 0) {
  25361. wc_PKCS7_Free(pkcs7);
  25362. return ret;
  25363. }
  25364. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  25365. if (ret != 0) {
  25366. wc_PKCS7_Free(pkcs7);
  25367. return ret;
  25368. }
  25369. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  25370. if (ret != 0) {
  25371. wc_PKCS7_Free(pkcs7);
  25372. return ret;
  25373. }
  25374. /* Get size of SID and print it out */
  25375. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  25376. if (ret != LENGTH_ONLY_E) {
  25377. wc_PKCS7_Free(pkcs7);
  25378. return ret;
  25379. }
  25380. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25381. if (sid == NULL) {
  25382. wc_PKCS7_Free(pkcs7);
  25383. return ret;
  25384. }
  25385. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  25386. if (ret != 0) {
  25387. wc_PKCS7_Free(pkcs7);
  25388. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25389. return ret;
  25390. }
  25391. ret = XMEMCMP(sid, expectedSid, sidSz);
  25392. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25393. if (ret != 0) {
  25394. wc_PKCS7_Free(pkcs7);
  25395. return ret;
  25396. }
  25397. /* get expected fwWrappedFirmwareKey */
  25398. if (keyHint == 0) {
  25399. ret = getFirmwareKey(pkcs7, key, keySz);
  25400. if (ret < 0) {
  25401. wc_PKCS7_Free(pkcs7);
  25402. return ret;
  25403. }
  25404. pkcs7->encryptionKey = key;
  25405. pkcs7->encryptionKeySz = ret;
  25406. }
  25407. else {
  25408. decodedSz = PKCS7_BUF_SIZE;
  25409. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  25410. if (ret != 0) {
  25411. wc_PKCS7_Free(pkcs7);
  25412. return ret;
  25413. }
  25414. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  25415. if (ret != 0) {
  25416. wc_PKCS7_Free(pkcs7);
  25417. return ret;
  25418. }
  25419. }
  25420. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  25421. pkcs7->contentSz, decoded, decodedSz);
  25422. if (decodedSz < 0) {
  25423. ret = decodedSz;
  25424. wc_PKCS7_Free(pkcs7);
  25425. return ret;
  25426. }
  25427. wc_PKCS7_Free(pkcs7);
  25428. return 0;
  25429. }
  25430. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  25431. {
  25432. int ret = 0;
  25433. word32 derSz;
  25434. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25435. if (! derBuf)
  25436. ERROR_OUT(-12360, out);
  25437. /* Doing default generation and verify */
  25438. derSz = FOURK_BUF;
  25439. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  25440. certSz, key, keySz);
  25441. if (ret <= 0) {
  25442. ERROR_OUT(-12361, out);
  25443. }
  25444. ret = verifyBundle(derBuf, derSz, 0);
  25445. if (ret != 0) {
  25446. ERROR_OUT(-12362, out);
  25447. }
  25448. /* test choosing other key with keyID */
  25449. derSz = FOURK_BUF;
  25450. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  25451. cert, certSz, key, keySz);
  25452. if (ret <= 0) {
  25453. ERROR_OUT(-12363, out);
  25454. }
  25455. ret = verifyBundle(derBuf, derSz, 1);
  25456. if (ret != 0) {
  25457. ERROR_OUT(-12364, out);
  25458. }
  25459. /* test fail case with wrong keyID */
  25460. derSz = FOURK_BUF;
  25461. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  25462. cert, certSz, key, keySz);
  25463. if (ret <= 0) {
  25464. ERROR_OUT(-12365, out);
  25465. }
  25466. ret = verifyBundle(derBuf, derSz, 1);
  25467. if (ret == 0) {
  25468. ERROR_OUT(-12366, out);
  25469. }
  25470. ret = 0;
  25471. out:
  25472. if (derBuf)
  25473. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25474. return ret;
  25475. }
  25476. #endif /* NO_AES */
  25477. #ifndef NO_PKCS7_ENCRYPTED_DATA
  25478. typedef struct {
  25479. const byte* content;
  25480. word32 contentSz;
  25481. int contentOID;
  25482. int encryptOID;
  25483. byte* encryptionKey;
  25484. word32 encryptionKeySz;
  25485. PKCS7Attrib* attribs;
  25486. word32 attribsSz;
  25487. const char* outFileName;
  25488. } pkcs7EncryptedVector;
  25489. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void)
  25490. {
  25491. int ret = 0;
  25492. int i, testSz;
  25493. int encryptedSz, decodedSz, attribIdx;
  25494. PKCS7* pkcs7;
  25495. byte *encrypted;
  25496. byte *decoded;
  25497. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  25498. XFILE pkcs7File;
  25499. #endif
  25500. PKCS7Attrib* expectedAttrib;
  25501. PKCS7DecodedAttrib* decodedAttrib;
  25502. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  25503. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25504. 0x72,0x6c,0x64
  25505. };
  25506. #ifndef NO_DES3
  25507. byte desKey[] = {
  25508. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  25509. };
  25510. byte des3Key[] = {
  25511. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  25512. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  25513. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  25514. };
  25515. #endif
  25516. #ifndef NO_AES
  25517. #ifdef WOLFSSL_AES_128
  25518. byte aes128Key[] = {
  25519. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25520. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25521. };
  25522. #endif
  25523. #ifdef WOLFSSL_AES_192
  25524. byte aes192Key[] = {
  25525. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25526. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25527. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25528. };
  25529. #endif
  25530. #ifdef WOLFSSL_AES_256
  25531. byte aes256Key[] = {
  25532. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25533. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25534. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25535. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25536. };
  25537. #endif
  25538. #ifdef WOLFSSL_AES_256
  25539. /* Attribute example from RFC 4134, Section 7.2
  25540. * OID = 1.2.5555
  25541. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  25542. static byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  25543. static byte genAttr[] = { 0x04, 47,
  25544. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  25545. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  25546. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  25547. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  25548. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  25549. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  25550. static byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  25551. static byte genAttr2[] = { 0x04, 47,
  25552. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  25553. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  25554. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  25555. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  25556. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  25557. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  25558. PKCS7Attrib attribs[] =
  25559. {
  25560. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  25561. };
  25562. PKCS7Attrib multiAttribs[] =
  25563. {
  25564. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  25565. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  25566. };
  25567. #endif
  25568. #endif /* NO_AES */
  25569. const pkcs7EncryptedVector testVectors[] =
  25570. {
  25571. #ifndef NO_DES3
  25572. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  25573. NULL, 0, "pkcs7encryptedDataDES3.der"},
  25574. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  25575. NULL, 0, "pkcs7encryptedDataDES.der"},
  25576. #endif /* NO_DES3 */
  25577. #ifndef NO_AES
  25578. #ifdef WOLFSSL_AES_128
  25579. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  25580. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  25581. #endif
  25582. #ifdef WOLFSSL_AES_192
  25583. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  25584. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  25585. #endif
  25586. #ifdef WOLFSSL_AES_256
  25587. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25588. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  25589. /* test with optional unprotected attributes */
  25590. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25591. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25592. "pkcs7encryptedDataAES256CBC_attribs.der"},
  25593. /* test with multiple optional unprotected attributes */
  25594. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25595. sizeof(aes256Key), multiAttribs,
  25596. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  25597. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  25598. /* test with contentType set to FirmwarePkgData */
  25599. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  25600. sizeof(aes256Key), NULL, 0,
  25601. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  25602. #endif
  25603. #endif /* NO_AES */
  25604. };
  25605. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25606. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25607. if ((! encrypted) || (! decoded)) {
  25608. ERROR_OUT(MEMORY_E, out);
  25609. }
  25610. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  25611. for (i = 0; i < testSz; i++) {
  25612. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  25613. if (pkcs7 == NULL) {
  25614. ERROR_OUT(-12400, out);
  25615. }
  25616. pkcs7->content = (byte*)testVectors[i].content;
  25617. pkcs7->contentSz = testVectors[i].contentSz;
  25618. pkcs7->contentOID = testVectors[i].contentOID;
  25619. pkcs7->encryptOID = testVectors[i].encryptOID;
  25620. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  25621. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  25622. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  25623. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  25624. /* encode encryptedData */
  25625. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25626. PKCS7_BUF_SIZE);
  25627. if (encryptedSz <= 0) {
  25628. wc_PKCS7_Free(pkcs7);
  25629. ERROR_OUT(-12401, out);
  25630. }
  25631. /* decode encryptedData */
  25632. #ifndef NO_PKCS7_STREAM
  25633. { /* test reading byte by byte */
  25634. int z;
  25635. for (z = 0; z < encryptedSz; z++) {
  25636. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  25637. decoded, PKCS7_BUF_SIZE);
  25638. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  25639. printf("unexpected error %d\n", decodedSz);
  25640. ERROR_OUT(-12402, out);
  25641. }
  25642. }
  25643. /* test decode result */
  25644. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  25645. printf("stream read failed\n");
  25646. wc_PKCS7_Free(pkcs7);
  25647. ERROR_OUT(-12403, out);
  25648. }
  25649. }
  25650. #endif
  25651. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25652. decoded, PKCS7_BUF_SIZE);
  25653. if (decodedSz <= 0){
  25654. wc_PKCS7_Free(pkcs7);
  25655. ERROR_OUT(-12404, out);
  25656. }
  25657. /* test decode result */
  25658. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  25659. wc_PKCS7_Free(pkcs7);
  25660. ERROR_OUT(-12405, out);
  25661. }
  25662. /* verify decoded unprotected attributes */
  25663. if (pkcs7->decodedAttrib != NULL) {
  25664. decodedAttrib = pkcs7->decodedAttrib;
  25665. attribIdx = 1;
  25666. while (decodedAttrib != NULL) {
  25667. /* expected attribute, stored list is reversed */
  25668. expectedAttrib = &(pkcs7->unprotectedAttribs
  25669. [pkcs7->unprotectedAttribsSz - attribIdx]);
  25670. /* verify oid */
  25671. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  25672. decodedAttrib->oidSz) != 0) {
  25673. wc_PKCS7_Free(pkcs7);
  25674. ERROR_OUT(-12406, out);
  25675. }
  25676. /* verify value */
  25677. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  25678. decodedAttrib->valueSz) != 0) {
  25679. wc_PKCS7_Free(pkcs7);
  25680. ERROR_OUT(-12407, out);
  25681. }
  25682. decodedAttrib = decodedAttrib->next;
  25683. attribIdx++;
  25684. }
  25685. }
  25686. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  25687. /* output pkcs7 envelopedData for external testing */
  25688. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  25689. if (!pkcs7File) {
  25690. wc_PKCS7_Free(pkcs7);
  25691. ERROR_OUT(-12408, out);
  25692. }
  25693. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  25694. XFCLOSE(pkcs7File);
  25695. if (ret > 0)
  25696. ret = 0;
  25697. #endif
  25698. wc_PKCS7_Free(pkcs7);
  25699. }
  25700. out:
  25701. if (encrypted)
  25702. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25703. if (decoded)
  25704. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25705. return ret;
  25706. }
  25707. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  25708. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  25709. typedef struct {
  25710. const byte* content;
  25711. word32 contentSz;
  25712. int contentOID;
  25713. const char* outFileName;
  25714. } pkcs7CompressedVector;
  25715. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void)
  25716. {
  25717. int ret = 0;
  25718. int i, testSz;
  25719. int compressedSz, decodedSz;
  25720. PKCS7* pkcs7;
  25721. #ifdef WOLFSSL_SMALL_STACK
  25722. byte *compressed;
  25723. byte *decoded;
  25724. #else
  25725. byte compressed[PKCS7_BUF_SIZE];
  25726. byte decoded[PKCS7_BUF_SIZE];
  25727. #endif
  25728. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  25729. XFILE pkcs7File;
  25730. #endif
  25731. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  25732. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25733. 0x72,0x6c,0x64
  25734. };
  25735. const pkcs7CompressedVector testVectors[] =
  25736. {
  25737. {data, (word32)sizeof(data), DATA,
  25738. "pkcs7compressedData_data_zlib.der"},
  25739. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  25740. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  25741. };
  25742. #ifdef WOLFSSL_SMALL_STACK
  25743. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25744. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25745. if ((! compressed) || (! decoded)) {
  25746. ERROR_OUT(MEMORY_E, out);
  25747. }
  25748. #endif
  25749. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  25750. for (i = 0; i < testSz; i++) {
  25751. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  25752. if (pkcs7 == NULL) {
  25753. ERROR_OUT(-12500, out);
  25754. }
  25755. pkcs7->content = (byte*)testVectors[i].content;
  25756. pkcs7->contentSz = testVectors[i].contentSz;
  25757. pkcs7->contentOID = testVectors[i].contentOID;
  25758. /* encode compressedData */
  25759. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  25760. PKCS7_BUF_SIZE);
  25761. if (compressedSz <= 0) {
  25762. wc_PKCS7_Free(pkcs7);
  25763. ERROR_OUT(-12501, out);
  25764. }
  25765. /* decode compressedData */
  25766. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  25767. compressedSz, decoded,
  25768. PKCS7_BUF_SIZE);
  25769. if (decodedSz <= 0){
  25770. wc_PKCS7_Free(pkcs7);
  25771. ERROR_OUT(-12502, out);
  25772. }
  25773. /* test decode result */
  25774. if (XMEMCMP(decoded, testVectors[i].content,
  25775. testVectors[i].contentSz) != 0) {
  25776. wc_PKCS7_Free(pkcs7);
  25777. ERROR_OUT(-12503, out);
  25778. }
  25779. /* make sure content type is the same */
  25780. if (testVectors[i].contentOID != pkcs7->contentOID) {
  25781. ERROR_OUT(-12504, out);
  25782. }
  25783. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  25784. /* output pkcs7 compressedData for external testing */
  25785. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  25786. if (!pkcs7File) {
  25787. wc_PKCS7_Free(pkcs7);
  25788. ERROR_OUT(-12505, out);
  25789. }
  25790. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  25791. XFCLOSE(pkcs7File);
  25792. if (ret > 0)
  25793. ret = 0;
  25794. #endif
  25795. wc_PKCS7_Free(pkcs7);
  25796. }
  25797. out:
  25798. #ifdef WOLFSSL_SMALL_STACK
  25799. if (compressed)
  25800. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25801. if (decoded)
  25802. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25803. #endif
  25804. return ret;
  25805. } /* pkcs7compressed_test() */
  25806. #undef PKCS7_BUF_SIZE
  25807. #endif /* HAVE_LIBZ */
  25808. typedef struct {
  25809. const byte* content;
  25810. word32 contentSz;
  25811. int hashOID;
  25812. int signOID;
  25813. byte* privateKey;
  25814. word32 privateKeySz;
  25815. byte* cert;
  25816. size_t certSz;
  25817. byte* caCert;
  25818. size_t caCertSz;
  25819. PKCS7Attrib* signedAttribs;
  25820. word32 signedAttribsSz;
  25821. const char* outFileName;
  25822. int contentOID;
  25823. byte* contentType;
  25824. word32 contentTypeSz;
  25825. int sidType;
  25826. int encryptOID; /* for single-shot encrypt alg OID */
  25827. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  25828. byte* encryptKey; /* for single-shot, encryptedData */
  25829. word32 encryptKeySz; /* for single-shot, encryptedData */
  25830. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  25831. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  25832. word16 detachedSignature; /* generate detached signature (0:1) */
  25833. } pkcs7SignedVector;
  25834. static int pkcs7signed_run_vectors(
  25835. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  25836. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  25837. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  25838. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  25839. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  25840. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  25841. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  25842. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  25843. {
  25844. int ret, testSz, i;
  25845. int encodedSz;
  25846. byte* out;
  25847. word32 outSz;
  25848. WC_RNG rng;
  25849. PKCS7* pkcs7;
  25850. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  25851. XFILE file;
  25852. #endif
  25853. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  25854. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25855. 0x72,0x6c,0x64
  25856. };
  25857. static byte transIdOid[] =
  25858. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  25859. 0x09, 0x07 };
  25860. static byte messageTypeOid[] =
  25861. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  25862. 0x09, 0x02 };
  25863. static byte senderNonceOid[] =
  25864. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  25865. 0x09, 0x05 };
  25866. #ifndef NO_SHA
  25867. static byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  25868. #else
  25869. static byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  25870. #endif
  25871. static byte messageType[] = { 0x13, 2, '1', '9' };
  25872. static byte senderNonce[PKCS7_NONCE_SZ + 2];
  25873. static PKCS7Attrib attribs[] =
  25874. {
  25875. { transIdOid, sizeof(transIdOid), transId,
  25876. sizeof(transId) - 1 }, /* take off the null */
  25877. { messageTypeOid, sizeof(messageTypeOid), messageType,
  25878. sizeof(messageType) },
  25879. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  25880. sizeof(senderNonce) }
  25881. };
  25882. /* for testing custom contentType, FirmwarePkgData */
  25883. static byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  25884. 0x48, 0x86, 0xF7, 0x0D,
  25885. 0x01, 0x09, 0x10, 0x01, 0x10 };
  25886. const pkcs7SignedVector testVectors[] =
  25887. {
  25888. #ifndef NO_RSA
  25889. #ifndef NO_SHA
  25890. /* RSA with SHA */
  25891. {data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  25892. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  25893. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25894. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  25895. 0, 0},
  25896. /* RSA with SHA, no signed attributes */
  25897. {data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  25898. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  25899. NULL, 0, NULL, 0,
  25900. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  25901. NULL, 0, 0},
  25902. #endif
  25903. #ifdef WOLFSSL_SHA224
  25904. /* RSA with SHA224 */
  25905. {data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  25906. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  25907. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25908. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  25909. NULL, 0, 0},
  25910. #endif
  25911. #ifndef NO_SHA256
  25912. /* RSA with SHA256 */
  25913. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  25914. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  25915. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25916. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  25917. NULL, 0, 0},
  25918. /* RSA with SHA256, detached signature */
  25919. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  25920. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  25921. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25922. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  25923. NULL, 0, NULL, 0, 1},
  25924. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  25925. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  25926. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  25927. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25928. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  25929. NULL, 0, NULL, 0, 0},
  25930. /* RSA with SHA256 and custom contentType */
  25931. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  25932. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  25933. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25934. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  25935. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  25936. NULL, 0, 0},
  25937. /* RSA with SHA256 and FirmwarePkgData contentType */
  25938. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  25939. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  25940. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25941. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  25942. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  25943. /* RSA with SHA256 using server cert and ca cert */
  25944. {data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  25945. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  25946. rsaCaCertBuf, rsaCaCertBufSz,
  25947. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25948. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  25949. NULL, 0, NULL, 0, 0},
  25950. #endif
  25951. #if defined(WOLFSSL_SHA384)
  25952. /* RSA with SHA384 */
  25953. {data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  25954. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  25955. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25956. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  25957. NULL, 0, 0},
  25958. #endif
  25959. #if defined(WOLFSSL_SHA512)
  25960. /* RSA with SHA512 */
  25961. {data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  25962. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  25963. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25964. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  25965. NULL, 0, 0},
  25966. #endif
  25967. #endif /* NO_RSA */
  25968. #ifdef HAVE_ECC
  25969. #ifndef NO_SHA
  25970. /* ECDSA with SHA */
  25971. {data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  25972. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  25973. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25974. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  25975. NULL, 0, 0},
  25976. /* ECDSA with SHA, no signed attributes */
  25977. {data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  25978. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  25979. NULL, 0, NULL, 0,
  25980. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  25981. NULL, 0, 0},
  25982. #endif
  25983. #ifdef WOLFSSL_SHA224
  25984. /* ECDSA with SHA224 */
  25985. {data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  25986. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  25987. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25988. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  25989. NULL, 0, 0},
  25990. #endif
  25991. #ifndef NO_SHA256
  25992. /* ECDSA with SHA256 */
  25993. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  25994. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  25995. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25996. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  25997. NULL, 0, 0},
  25998. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  25999. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26000. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26001. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26002. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  26003. NULL, 0, NULL, 0, 0},
  26004. /* ECDSA with SHA256 and custom contentType */
  26005. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26006. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26007. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26008. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  26009. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  26010. NULL, 0, 0},
  26011. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  26012. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26013. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26014. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26015. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  26016. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  26017. #endif
  26018. #ifdef WOLFSSL_SHA384
  26019. /* ECDSA with SHA384 */
  26020. {data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  26021. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26022. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26023. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  26024. NULL, 0, 0},
  26025. #endif
  26026. #ifdef WOLFSSL_SHA512
  26027. /* ECDSA with SHA512 */
  26028. {data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  26029. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26030. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26031. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  26032. NULL, 0, 0},
  26033. #endif
  26034. #endif /* HAVE_ECC */
  26035. };
  26036. testSz = sizeof(testVectors) / sizeof(pkcs7SignedVector);
  26037. outSz = FOURK_BUF;
  26038. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26039. if (out == NULL)
  26040. return -12510;
  26041. XMEMSET(out, 0, outSz);
  26042. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  26043. if (ret < 0) {
  26044. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26045. return -12511;
  26046. }
  26047. #ifndef HAVE_FIPS
  26048. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26049. #else
  26050. ret = wc_InitRng(&rng);
  26051. #endif
  26052. if (ret != 0) {
  26053. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26054. return -12512;
  26055. }
  26056. for (i = 0; i < testSz; i++) {
  26057. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  26058. if (pkcs7 == NULL)
  26059. return -12513;
  26060. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  26061. (word32)testVectors[i].certSz);
  26062. if (ret != 0) {
  26063. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26064. wc_PKCS7_Free(pkcs7);
  26065. return -12514;
  26066. }
  26067. /* load CA certificate, if present */
  26068. if (testVectors[i].caCert != NULL) {
  26069. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  26070. (word32)testVectors[i].caCertSz);
  26071. if (ret != 0) {
  26072. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26073. wc_PKCS7_Free(pkcs7);
  26074. return -12515;
  26075. }
  26076. }
  26077. pkcs7->rng = &rng;
  26078. pkcs7->content = (byte*)testVectors[i].content;
  26079. pkcs7->contentSz = testVectors[i].contentSz;
  26080. pkcs7->contentOID = testVectors[i].contentOID;
  26081. pkcs7->hashOID = testVectors[i].hashOID;
  26082. pkcs7->encryptOID = testVectors[i].signOID;
  26083. pkcs7->privateKey = testVectors[i].privateKey;
  26084. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  26085. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  26086. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  26087. /* optional custom contentType, default is DATA,
  26088. overrides contentOID if set */
  26089. if (testVectors[i].contentType != NULL) {
  26090. ret = wc_PKCS7_SetContentType(pkcs7, testVectors[i].contentType,
  26091. testVectors[i].contentTypeSz);
  26092. if (ret != 0) {
  26093. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26094. wc_PKCS7_Free(pkcs7);
  26095. return -12516;
  26096. }
  26097. }
  26098. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  26099. default is IssuerAndSerialNumber */
  26100. if (testVectors[i].sidType == CMS_SKID) {
  26101. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  26102. if (ret != 0) {
  26103. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26104. wc_PKCS7_Free(pkcs7);
  26105. return -12517;
  26106. }
  26107. }
  26108. /* generate senderNonce */
  26109. {
  26110. senderNonce[0] = 0x04;
  26111. senderNonce[1] = PKCS7_NONCE_SZ;
  26112. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  26113. if (ret != 0) {
  26114. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26115. wc_PKCS7_Free(pkcs7);
  26116. return -12518;
  26117. }
  26118. }
  26119. /* generate transactionID (used with SCEP) */
  26120. {
  26121. #ifndef NO_SHA
  26122. wc_Sha sha;
  26123. byte digest[WC_SHA_DIGEST_SIZE];
  26124. #else
  26125. wc_Sha256 sha;
  26126. byte digest[WC_SHA256_DIGEST_SIZE];
  26127. #endif
  26128. int j,k;
  26129. transId[0] = 0x13;
  26130. transId[1] = sizeof(digest) * 2;
  26131. #ifndef NO_SHA
  26132. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  26133. if (ret != 0) {
  26134. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26135. wc_PKCS7_Free(pkcs7);
  26136. return -12519;
  26137. }
  26138. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  26139. wc_ShaFinal(&sha, digest);
  26140. wc_ShaFree(&sha);
  26141. #else
  26142. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  26143. if (ret != 0) {
  26144. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26145. wc_PKCS7_Free(pkcs7);
  26146. return -12520;
  26147. }
  26148. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  26149. wc_Sha256Final(&sha, digest);
  26150. wc_Sha256Free(&sha);
  26151. #endif
  26152. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  26153. XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  26154. }
  26155. }
  26156. /* enable detached signature generation, if set */
  26157. if (testVectors[i].detachedSignature == 1) {
  26158. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  26159. if (ret != 0) {
  26160. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26161. wc_PKCS7_Free(pkcs7);
  26162. return -12521;
  26163. }
  26164. }
  26165. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  26166. if (encodedSz < 0) {
  26167. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26168. wc_PKCS7_Free(pkcs7);
  26169. return -12522;
  26170. }
  26171. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  26172. /* write PKCS#7 to output file for more testing */
  26173. file = XFOPEN(testVectors[i].outFileName, "wb");
  26174. if (!file) {
  26175. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26176. wc_PKCS7_Free(pkcs7);
  26177. return -12523;
  26178. }
  26179. ret = (int)XFWRITE(out, 1, encodedSz, file);
  26180. XFCLOSE(file);
  26181. if (ret != (int)encodedSz) {
  26182. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26183. wc_PKCS7_Free(pkcs7);
  26184. return -12524;
  26185. }
  26186. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  26187. wc_PKCS7_Free(pkcs7);
  26188. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  26189. if (pkcs7 == NULL)
  26190. return -12525;
  26191. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  26192. if (testVectors[i].detachedSignature == 1) {
  26193. /* set content for verifying detached signatures */
  26194. pkcs7->content = (byte*)testVectors[i].content;
  26195. pkcs7->contentSz = testVectors[i].contentSz;
  26196. }
  26197. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  26198. if (ret < 0) {
  26199. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26200. wc_PKCS7_Free(pkcs7);
  26201. return -12526;
  26202. }
  26203. /* verify contentType extracted successfully for custom content types */
  26204. if (testVectors[i].contentTypeSz > 0) {
  26205. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  26206. return -12527;
  26207. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  26208. pkcs7->contentTypeSz) != 0) {
  26209. return -12528;
  26210. }
  26211. }
  26212. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0) {
  26213. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26214. wc_PKCS7_Free(pkcs7);
  26215. return -12529;
  26216. }
  26217. {
  26218. /* check getting signed attributes */
  26219. #ifndef NO_SHA
  26220. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  26221. #else
  26222. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  26223. #endif
  26224. byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  26225. int oidSz = (int)sizeof(transIdOid) - 2;
  26226. int bufSz = 0;
  26227. if (testVectors[i].signedAttribs != NULL &&
  26228. wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  26229. NULL, (word32*)&bufSz) != LENGTH_ONLY_E) {
  26230. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26231. wc_PKCS7_Free(pkcs7);
  26232. return -12530;
  26233. }
  26234. if (bufSz > (int)sizeof(buf)) {
  26235. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26236. wc_PKCS7_Free(pkcs7);
  26237. return -12531;
  26238. }
  26239. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  26240. buf, (word32*)&bufSz);
  26241. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  26242. (testVectors[i].signedAttribs == NULL && bufSz > 0)) {
  26243. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26244. wc_PKCS7_Free(pkcs7);
  26245. return -12532;
  26246. }
  26247. }
  26248. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  26249. file = XFOPEN("./pkcs7cert.der", "wb");
  26250. if (!file) {
  26251. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26252. wc_PKCS7_Free(pkcs7);
  26253. return -12533;
  26254. }
  26255. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  26256. XFCLOSE(file);
  26257. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  26258. wc_PKCS7_Free(pkcs7);
  26259. }
  26260. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26261. wc_FreeRng(&rng);
  26262. if (ret > 0)
  26263. return 0;
  26264. (void)rsaClientCertBuf;
  26265. (void)rsaClientCertBufSz;
  26266. (void)rsaClientPrivKeyBuf;
  26267. (void)rsaClientPrivKeyBufSz;
  26268. (void)rsaServerCertBuf;
  26269. (void)rsaServerCertBufSz;
  26270. (void)rsaServerPrivKeyBuf;
  26271. (void)rsaServerPrivKeyBufSz;
  26272. (void)rsaCaCertBuf;
  26273. (void)rsaCaCertBufSz;
  26274. (void)rsaCaPrivKeyBuf;
  26275. (void)rsaCaPrivKeyBufSz;
  26276. (void)eccClientCertBuf;
  26277. (void)eccClientCertBufSz;
  26278. (void)eccClientPrivKeyBuf;
  26279. (void)eccClientPrivKeyBufSz;
  26280. return ret;
  26281. }
  26282. static int pkcs7signed_run_SingleShotVectors(
  26283. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  26284. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  26285. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  26286. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  26287. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  26288. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  26289. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  26290. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  26291. {
  26292. int ret, testSz, i;
  26293. int encodedSz;
  26294. byte* out;
  26295. word32 outSz;
  26296. WC_RNG rng;
  26297. PKCS7* pkcs7;
  26298. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  26299. XFILE file;
  26300. #endif
  26301. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  26302. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  26303. 0x72,0x6c,0x64
  26304. };
  26305. #if defined(WOLFSSL_AES_256) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  26306. static byte aes256Key[] = {
  26307. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26308. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26309. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26310. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26311. };
  26312. #endif
  26313. static byte messageTypeOid[] =
  26314. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  26315. 0x09, 0x02 };
  26316. static byte messageType[] = { 0x13, 2, '1', '9' };
  26317. PKCS7Attrib attribs[] =
  26318. {
  26319. { messageTypeOid, sizeof(messageTypeOid), messageType,
  26320. sizeof(messageType) },
  26321. };
  26322. const pkcs7SignedVector testVectors[] =
  26323. {
  26324. #ifndef NO_RSA
  26325. #ifndef NO_SHA256
  26326. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  26327. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26328. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26329. NULL, 0,
  26330. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  26331. 0, 0, NULL, 0, NULL, 0, 0},
  26332. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  26333. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26334. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26335. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26336. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  26337. NULL, 0, NULL, 0, 0},
  26338. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  26339. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26340. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26341. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26342. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  26343. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0},
  26344. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  26345. {data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  26346. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  26347. rsaCaCertBuf, rsaCaCertBufSz,
  26348. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26349. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  26350. 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  26351. #if defined(WOLFSSL_AES_256) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  26352. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  26353. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26354. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26355. NULL, 0,
  26356. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  26357. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0},
  26358. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  26359. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26360. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26361. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26362. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  26363. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  26364. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  26365. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  26366. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  26367. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  26368. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26369. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26370. NULL, 0,
  26371. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  26372. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  26373. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  26374. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26375. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26376. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26377. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  26378. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  26379. #ifndef NO_PKCS7_ENCRYPTED_DATA
  26380. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  26381. no attribs */
  26382. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26383. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26384. NULL, 0,
  26385. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  26386. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  26387. 0, 0},
  26388. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  26389. attribs */
  26390. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26391. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26392. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26393. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  26394. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  26395. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  26396. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  26397. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  26398. #endif /* NO_SHA256 */
  26399. #endif /* NO_RSA */
  26400. #ifdef HAVE_ECC
  26401. #ifndef NO_SHA256
  26402. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  26403. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26404. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26405. NULL, 0,
  26406. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  26407. 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  26408. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  26409. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26410. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26411. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26412. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  26413. 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  26414. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  26415. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26416. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26417. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26418. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  26419. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0},
  26420. #if defined(WOLFSSL_AES_256) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  26421. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  26422. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26423. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26424. NULL, 0,
  26425. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  26426. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0},
  26427. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  26428. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26429. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26430. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26431. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  26432. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  26433. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  26434. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  26435. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  26436. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  26437. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26438. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26439. NULL, 0,
  26440. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  26441. 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  26442. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  26443. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26444. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26445. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26446. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  26447. 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  26448. #ifndef NO_PKCS7_ENCRYPTED_DATA
  26449. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  26450. no attribs */
  26451. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26452. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26453. NULL, 0,
  26454. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  26455. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  26456. 0, 0},
  26457. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  26458. attribs */
  26459. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26460. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26461. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26462. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  26463. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  26464. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  26465. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  26466. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  26467. #endif /* NO_SHA256 */
  26468. #endif /* HAVE_ECC */
  26469. };
  26470. testSz = sizeof(testVectors) / sizeof(pkcs7SignedVector);
  26471. outSz = FOURK_BUF;
  26472. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26473. if (out == NULL)
  26474. return -12540;
  26475. XMEMSET(out, 0, outSz);
  26476. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  26477. if (ret < 0) {
  26478. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26479. return -12541;
  26480. }
  26481. #ifndef HAVE_FIPS
  26482. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26483. #else
  26484. ret = wc_InitRng(&rng);
  26485. #endif
  26486. if (ret != 0) {
  26487. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26488. return -12542;
  26489. }
  26490. for (i = 0; i < testSz; i++) {
  26491. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  26492. if (pkcs7 == NULL)
  26493. return -12543;
  26494. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  26495. (word32)testVectors[i].certSz);
  26496. if (ret != 0) {
  26497. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26498. wc_PKCS7_Free(pkcs7);
  26499. return -12544;
  26500. }
  26501. /* load CA certificate, if present */
  26502. if (testVectors[i].caCert != NULL) {
  26503. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  26504. (word32)testVectors[i].caCertSz);
  26505. if (ret != 0) {
  26506. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26507. wc_PKCS7_Free(pkcs7);
  26508. return -12545;
  26509. }
  26510. }
  26511. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  26512. default is IssuerAndSerialNumber */
  26513. if (testVectors[i].sidType == CMS_SKID) {
  26514. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  26515. if (ret != 0) {
  26516. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26517. wc_PKCS7_Free(pkcs7);
  26518. return -12546;
  26519. }
  26520. }
  26521. if (testVectors[i].encCompFlag == 0) {
  26522. /* encode Signed FirmwarePkgData */
  26523. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  26524. testVectors[i].privateKey, testVectors[i].privateKeySz,
  26525. testVectors[i].signOID, testVectors[i].hashOID,
  26526. (byte*)testVectors[i].content, testVectors[i].contentSz,
  26527. testVectors[i].signedAttribs,
  26528. testVectors[i].signedAttribsSz, out, outSz);
  26529. if (encodedSz < 0) {
  26530. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26531. wc_PKCS7_Free(pkcs7);
  26532. return -12547;
  26533. }
  26534. #ifndef NO_PKCS7_ENCRYPTED_DATA
  26535. } else if (testVectors[i].encCompFlag == 1) {
  26536. /* encode Signed Encrypted FirmwarePkgData */
  26537. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  26538. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  26539. testVectors[i].privateKey, testVectors[i].privateKeySz,
  26540. testVectors[i].encryptOID, testVectors[i].signOID,
  26541. testVectors[i].hashOID, (byte*)testVectors[i].content,
  26542. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  26543. testVectors[i].unprotectedAttribsSz,
  26544. testVectors[i].signedAttribs,
  26545. testVectors[i].signedAttribsSz, out, outSz);
  26546. if (encodedSz <= 0) {
  26547. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26548. wc_PKCS7_Free(pkcs7);
  26549. return -12548;
  26550. }
  26551. #endif
  26552. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  26553. } else if (testVectors[i].encCompFlag == 2) {
  26554. /* encode Signed Compressed FirmwarePkgData */
  26555. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  26556. testVectors[i].privateKey, testVectors[i].privateKeySz,
  26557. testVectors[i].signOID, testVectors[i].hashOID,
  26558. (byte*)testVectors[i].content, testVectors[i].contentSz,
  26559. testVectors[i].signedAttribs,
  26560. testVectors[i].signedAttribsSz, out, outSz);
  26561. if (encodedSz <= 0) {
  26562. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26563. wc_PKCS7_Free(pkcs7);
  26564. return -12549;
  26565. }
  26566. #ifndef NO_PKCS7_ENCRYPTED_DATA
  26567. } else if (testVectors[i].encCompFlag == 3) {
  26568. /* encode Signed Encrypted Compressed FirmwarePkgData */
  26569. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  26570. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  26571. testVectors[i].privateKey, testVectors[i].privateKeySz,
  26572. testVectors[i].encryptOID, testVectors[i].signOID,
  26573. testVectors[i].hashOID, (byte*)testVectors[i].content,
  26574. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  26575. testVectors[i].unprotectedAttribsSz,
  26576. testVectors[i].signedAttribs,
  26577. testVectors[i].signedAttribsSz, out, outSz);
  26578. if (encodedSz <= 0) {
  26579. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26580. wc_PKCS7_Free(pkcs7);
  26581. return -12550;
  26582. }
  26583. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  26584. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  26585. } else {
  26586. /* unsupported SignedData single-shot combination */
  26587. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26588. wc_PKCS7_Free(pkcs7);
  26589. return -12551;
  26590. }
  26591. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  26592. /* write PKCS#7 to output file for more testing */
  26593. file = XFOPEN(testVectors[i].outFileName, "wb");
  26594. if (!file) {
  26595. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26596. wc_PKCS7_Free(pkcs7);
  26597. return -12552;
  26598. }
  26599. ret = (int)XFWRITE(out, 1, encodedSz, file);
  26600. XFCLOSE(file);
  26601. if (ret != (int)encodedSz) {
  26602. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26603. wc_PKCS7_Free(pkcs7);
  26604. return -12553;
  26605. }
  26606. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  26607. wc_PKCS7_Free(pkcs7);
  26608. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  26609. if (pkcs7 == NULL)
  26610. return -12554;
  26611. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  26612. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  26613. if (ret < 0) {
  26614. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26615. wc_PKCS7_Free(pkcs7);
  26616. return -12555;
  26617. }
  26618. #ifndef NO_PKCS7_STREAM
  26619. {
  26620. word32 z;
  26621. for (z = 0; z < outSz && ret != 0; z++) {
  26622. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  26623. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  26624. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26625. wc_PKCS7_Free(pkcs7);
  26626. printf("unexpected error %d\n", ret);
  26627. return -12556;
  26628. }
  26629. }
  26630. }
  26631. #endif
  26632. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0) {
  26633. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26634. wc_PKCS7_Free(pkcs7);
  26635. return -12557;
  26636. }
  26637. if (testVectors[i].encCompFlag == 0) {
  26638. /* verify decoded content matches expected */
  26639. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  26640. XMEMCMP(pkcs7->content, testVectors[i].content,
  26641. pkcs7->contentSz)) {
  26642. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26643. wc_PKCS7_Free(pkcs7);
  26644. return -12558;
  26645. }
  26646. }
  26647. #ifndef NO_PKCS7_ENCRYPTED_DATA
  26648. else if (testVectors[i].encCompFlag == 1) {
  26649. /* decrypt inner encryptedData */
  26650. pkcs7->encryptionKey = testVectors[i].encryptKey;
  26651. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  26652. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  26653. pkcs7->contentSz, out, outSz);
  26654. if (ret < 0) {
  26655. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26656. wc_PKCS7_Free(pkcs7);
  26657. return -12559;
  26658. }
  26659. /* compare decrypted to expected */
  26660. if (((word32)ret != testVectors[i].contentSz) ||
  26661. XMEMCMP(out, testVectors[i].content, ret)) {
  26662. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26663. wc_PKCS7_Free(pkcs7);
  26664. return -12560;
  26665. }
  26666. }
  26667. #endif
  26668. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  26669. else if (testVectors[i].encCompFlag == 2) {
  26670. /* decompress inner compressedData */
  26671. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  26672. pkcs7->contentSz, out, outSz);
  26673. if (ret < 0) {
  26674. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26675. wc_PKCS7_Free(pkcs7);
  26676. return -12561;
  26677. }
  26678. /* compare decompressed to expected */
  26679. if (((word32)ret != testVectors[i].contentSz) ||
  26680. XMEMCMP(out, testVectors[i].content, ret)) {
  26681. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26682. wc_PKCS7_Free(pkcs7);
  26683. return -12562;
  26684. }
  26685. }
  26686. #ifndef NO_PKCS7_ENCRYPTED_DATA
  26687. else if (testVectors[i].encCompFlag == 3) {
  26688. byte* encryptedTmp;
  26689. int encryptedTmpSz;
  26690. encryptedTmpSz = FOURK_BUF;
  26691. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  26692. DYNAMIC_TYPE_TMP_BUFFER);
  26693. if (encryptedTmp == NULL) {
  26694. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26695. wc_PKCS7_Free(pkcs7);
  26696. return -12563;
  26697. }
  26698. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  26699. /* decrypt inner encryptedData */
  26700. pkcs7->encryptionKey = testVectors[i].encryptKey;
  26701. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  26702. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  26703. pkcs7->contentSz, encryptedTmp,
  26704. encryptedTmpSz);
  26705. if (encryptedTmpSz < 0) {
  26706. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26707. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26708. wc_PKCS7_Free(pkcs7);
  26709. return -12564;
  26710. }
  26711. /* decompress inner compressedData */
  26712. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  26713. encryptedTmpSz, out, outSz);
  26714. if (ret < 0) {
  26715. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26716. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26717. wc_PKCS7_Free(pkcs7);
  26718. return -12565;
  26719. }
  26720. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26721. /* compare decompressed to expected */
  26722. if (((word32)ret != testVectors[i].contentSz) ||
  26723. XMEMCMP(out, testVectors[i].content, ret)) {
  26724. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26725. wc_PKCS7_Free(pkcs7);
  26726. return -12566;
  26727. }
  26728. }
  26729. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  26730. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  26731. wc_PKCS7_Free(pkcs7);
  26732. }
  26733. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26734. wc_FreeRng(&rng);
  26735. if (ret > 0)
  26736. return 0;
  26737. (void)eccClientCertBuf;
  26738. (void)eccClientCertBufSz;
  26739. (void)eccClientPrivKeyBuf;
  26740. (void)eccClientPrivKeyBufSz;
  26741. (void)rsaClientCertBuf;
  26742. (void)rsaClientCertBufSz;
  26743. (void)rsaClientPrivKeyBuf;
  26744. (void)rsaClientPrivKeyBufSz;
  26745. (void)rsaServerCertBuf;
  26746. (void)rsaServerCertBufSz;
  26747. (void)rsaServerPrivKeyBuf;
  26748. (void)rsaServerPrivKeyBufSz;
  26749. (void)rsaCaCertBuf;
  26750. (void)rsaCaCertBufSz;
  26751. (void)rsaCaPrivKeyBuf;
  26752. (void)rsaCaPrivKeyBufSz;
  26753. return ret;
  26754. }
  26755. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void)
  26756. {
  26757. int ret = 0;
  26758. byte* rsaClientCertBuf = NULL;
  26759. byte* rsaServerCertBuf = NULL;
  26760. byte* rsaCaCertBuf = NULL;
  26761. byte* eccClientCertBuf = NULL;
  26762. byte* rsaClientPrivKeyBuf = NULL;
  26763. byte* rsaServerPrivKeyBuf = NULL;
  26764. byte* rsaCaPrivKeyBuf = NULL;
  26765. byte* eccClientPrivKeyBuf = NULL;
  26766. word32 rsaClientCertBufSz = 0;
  26767. word32 rsaServerCertBufSz = 0;
  26768. word32 rsaCaCertBufSz = 0;
  26769. word32 eccClientCertBufSz = 0;
  26770. word32 rsaClientPrivKeyBufSz = 0;
  26771. word32 rsaServerPrivKeyBufSz = 0;
  26772. word32 rsaCaPrivKeyBufSz = 0;
  26773. word32 eccClientPrivKeyBufSz = 0;
  26774. #ifndef NO_RSA
  26775. /* read client RSA cert and key in DER format */
  26776. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26777. DYNAMIC_TYPE_TMP_BUFFER);
  26778. if (rsaClientCertBuf == NULL)
  26779. ret = -12600;
  26780. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26781. DYNAMIC_TYPE_TMP_BUFFER);
  26782. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  26783. ret = -12601;
  26784. }
  26785. rsaClientCertBufSz = FOURK_BUF;
  26786. rsaClientPrivKeyBufSz = FOURK_BUF;
  26787. /* read server RSA cert and key in DER format */
  26788. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26789. DYNAMIC_TYPE_TMP_BUFFER);
  26790. if (ret == 0 && rsaServerCertBuf == NULL)
  26791. ret = -12602;
  26792. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26793. DYNAMIC_TYPE_TMP_BUFFER);
  26794. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  26795. ret = -12603;
  26796. }
  26797. rsaServerCertBufSz = FOURK_BUF;
  26798. rsaServerPrivKeyBufSz = FOURK_BUF;
  26799. /* read CA RSA cert and key in DER format, for use with server cert */
  26800. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26801. DYNAMIC_TYPE_TMP_BUFFER);
  26802. if (ret == 0 && rsaCaCertBuf == NULL)
  26803. ret = -12604;
  26804. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26805. DYNAMIC_TYPE_TMP_BUFFER);
  26806. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  26807. ret = -12605;
  26808. }
  26809. rsaCaCertBufSz = FOURK_BUF;
  26810. rsaCaPrivKeyBufSz = FOURK_BUF;
  26811. #endif /* NO_RSA */
  26812. #ifdef HAVE_ECC
  26813. /* read client ECC cert and key in DER format */
  26814. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26815. DYNAMIC_TYPE_TMP_BUFFER);
  26816. if (ret == 0 && eccClientCertBuf == NULL) {
  26817. ret = -12606;
  26818. }
  26819. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26820. DYNAMIC_TYPE_TMP_BUFFER);
  26821. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  26822. ret = -12607;
  26823. }
  26824. eccClientCertBufSz = FOURK_BUF;
  26825. eccClientPrivKeyBufSz = FOURK_BUF;
  26826. #endif /* HAVE_ECC */
  26827. if (ret >= 0)
  26828. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  26829. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  26830. rsaServerCertBuf, &rsaServerCertBufSz,
  26831. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  26832. rsaCaCertBuf, &rsaCaCertBufSz,
  26833. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  26834. eccClientCertBuf, &eccClientCertBufSz,
  26835. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  26836. if (ret < 0) {
  26837. ret = -12608;
  26838. }
  26839. if (ret >= 0)
  26840. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  26841. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  26842. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  26843. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  26844. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  26845. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  26846. eccClientCertBuf, (word32)eccClientCertBufSz,
  26847. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  26848. if (ret >= 0)
  26849. ret = pkcs7signed_run_SingleShotVectors(
  26850. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  26851. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  26852. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  26853. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  26854. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  26855. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  26856. eccClientCertBuf, (word32)eccClientCertBufSz,
  26857. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  26858. #ifndef NO_AES
  26859. if (ret >= 0)
  26860. ret = pkcs7callback_test(
  26861. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  26862. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  26863. #endif
  26864. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26865. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26866. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26867. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26868. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26869. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26870. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26871. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26872. return ret;
  26873. }
  26874. #endif /* HAVE_PKCS7 */
  26875. #ifdef HAVE_VALGRIND
  26876. /* Need a static build to have access to symbols. */
  26877. /* Maximum number of bytes in a number to test. */
  26878. #define MP_MAX_TEST_BYTE_LEN 32
  26879. static int randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  26880. {
  26881. byte d[MP_MAX_TEST_BYTE_LEN];
  26882. int ret;
  26883. (void)heap;
  26884. do {
  26885. ret = wc_RNG_GenerateBlock(rng, d, len);
  26886. if (ret != 0)
  26887. return ret;
  26888. ret = mp_read_unsigned_bin(n, d, len);
  26889. if (ret != 0)
  26890. return ret;
  26891. } while (mp_iszero(n));
  26892. return 0;
  26893. }
  26894. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  26895. static int mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  26896. {
  26897. int i, j;
  26898. mp_digit rem;
  26899. mp_digit rem2;
  26900. for (i = 0; i < 10; i++) {
  26901. for (j = 1; j < 10; j++) {
  26902. if (randNum(a, j, rng, NULL) != 0)
  26903. return -12620;
  26904. if (mp_div_3(a, r, &rem) != 0)
  26905. return -12621;
  26906. if (mp_mul_d(r, 3, r) != 0)
  26907. return -12622;
  26908. if (mp_add_d(r, rem, r) != 0)
  26909. return -12623;
  26910. if (mp_cmp(r, a) != MP_EQ)
  26911. return -12624;
  26912. }
  26913. }
  26914. if (mp_div_3(a, r, &rem) != 0)
  26915. return -12625;
  26916. if (mp_div_3(a, a, NULL) != 0)
  26917. return -12626;
  26918. if (mp_cmp(r, a) != MP_EQ)
  26919. return -12627;
  26920. #if defined(WOLFSSL_SP_MATH_ALL)
  26921. if (mp_div_d(a, 10, r, &rem) != 0)
  26922. return -12628;
  26923. if (mp_div_d(a, 10, a, NULL) != 0)
  26924. return -12629;
  26925. if (mp_cmp(r, a) != MP_EQ)
  26926. return -12630;
  26927. if (mp_div_d(a, 12, r, &rem) != 0)
  26928. return -12631;
  26929. if (mp_div_d(a, 12, a, NULL) != 0)
  26930. return -12632;
  26931. if (mp_cmp(r, a) != MP_EQ)
  26932. return -12633;
  26933. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem) != 0)
  26934. return -12634;
  26935. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2) != 0)
  26936. return -12635;
  26937. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL) != 0)
  26938. return -12636;
  26939. if (mp_cmp(r, a) != MP_EQ)
  26940. return -12637;
  26941. if (rem != rem2)
  26942. return -12638;
  26943. #else
  26944. (void)rem2;
  26945. #endif
  26946. return 0;
  26947. }
  26948. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  26949. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  26950. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  26951. static int mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  26952. {
  26953. int ret;
  26954. int i, j;
  26955. int size;
  26956. char str[30];
  26957. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  26958. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  26959. WOLFSSL_SMALL_STACK_STATIC const char* badStr3 = " ";
  26960. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  26961. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  26962. for (i = 0; i < 10; i++) {
  26963. for (j = 2; j < 12; j++) {
  26964. if (randNum(a, j, rng, NULL) != 0)
  26965. return -12640;
  26966. if (mp_radix_size(a, MP_RADIX_DEC, &size) != MP_OKAY)
  26967. return -12641;
  26968. mp_toradix(a, str, MP_RADIX_DEC);
  26969. if ((int)XSTRLEN(str) != size - 1)
  26970. return -12642;
  26971. mp_read_radix(r, str, MP_RADIX_DEC);
  26972. if (mp_cmp(a, r) != MP_EQ)
  26973. return -12643;
  26974. }
  26975. }
  26976. if (mp_read_radix(r, badStr1, MP_RADIX_DEC) != MP_VAL)
  26977. return -12644;
  26978. if (mp_read_radix(r, badStr2, MP_RADIX_DEC) != MP_VAL)
  26979. return -12645;
  26980. if (mp_read_radix(r, badStr3, MP_RADIX_DEC) != MP_VAL)
  26981. return -12646;
  26982. if (mp_read_radix(r, zeros, MP_RADIX_DEC) != MP_OKAY)
  26983. return -12647;
  26984. if (!mp_iszero(r))
  26985. return -12648;
  26986. mp_set(r, 1);
  26987. if (mp_read_radix(r, empty, MP_RADIX_DEC) != MP_OKAY)
  26988. return -12649;
  26989. if (!mp_iszero(r))
  26990. return -12650;
  26991. mp_zero(a);
  26992. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  26993. if (ret != 0)
  26994. return -12651;
  26995. if (size != 2)
  26996. return -12652;
  26997. ret = mp_toradix(a, str, MP_RADIX_DEC);
  26998. if (ret != 0)
  26999. return -12653;
  27000. if ((int)XSTRLEN(str) != size - 1)
  27001. return -12654;
  27002. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  27003. if (ret != 0)
  27004. return -12655;
  27005. if (!mp_iszero(r))
  27006. return -12656;
  27007. return 0;
  27008. }
  27009. #endif
  27010. #if defined(WOLFSSL_SP_MATH_ALL) || defined(HAVE_ECC)
  27011. static int mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  27012. {
  27013. int ret;
  27014. int i, j;
  27015. int size;
  27016. char str[30];
  27017. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  27018. static char longStr[2 * sizeof(a->dp) + 2];
  27019. #endif
  27020. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = " ";
  27021. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  27022. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  27023. for (i = 0; i < 10; i++) {
  27024. for (j = 2; j < 12; j++) {
  27025. if (randNum(a, j, rng, NULL) != 0)
  27026. return -12660;
  27027. mp_radix_size(a, MP_RADIX_HEX, &size);
  27028. mp_toradix(a, str, MP_RADIX_HEX);
  27029. if ((int)XSTRLEN(str) != size - 1)
  27030. return -12661;
  27031. mp_read_radix(r, str, MP_RADIX_HEX);
  27032. if (mp_cmp(a, r) != MP_EQ)
  27033. return -12662;
  27034. }
  27035. }
  27036. if (mp_read_radix(r, badStr1, MP_RADIX_HEX) != MP_VAL)
  27037. return -12663;
  27038. if (mp_read_radix(r, badStr2, MP_RADIX_HEX) != MP_VAL)
  27039. return -12664;
  27040. mp_set(r, 1);
  27041. if (mp_read_radix(r, empty, MP_RADIX_HEX) != MP_OKAY)
  27042. return -12665;
  27043. if (!mp_iszero(r))
  27044. return -12666;
  27045. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  27046. /* Fixed MP data size - string can be too long. */
  27047. longStr[0] = '8';
  27048. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  27049. longStr[sizeof(longStr)-1] = '\0';
  27050. if (mp_read_radix(r, longStr, MP_RADIX_HEX) != MP_VAL)
  27051. return -12667;
  27052. #endif
  27053. mp_zero(a);
  27054. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  27055. if (ret != 0)
  27056. return -12668;
  27057. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  27058. if (size != 3)
  27059. #else
  27060. if (size != 2)
  27061. #endif
  27062. return -12669;
  27063. ret = mp_toradix(a, str, MP_RADIX_HEX);
  27064. if (ret != 0)
  27065. return -12670;
  27066. if ((int)XSTRLEN(str) != size - 1)
  27067. return -12671;
  27068. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  27069. if (ret != 0)
  27070. return -12672;
  27071. if (!mp_iszero(r))
  27072. return -12673;
  27073. #ifdef WOLFSSL_SP_MATH
  27074. ret = mp_toradix(a, str, 8);
  27075. if (ret != MP_VAL)
  27076. return -12674;
  27077. ret = mp_radix_size(a, 8, &size);
  27078. if (ret != MP_VAL)
  27079. return -12675;
  27080. #endif
  27081. return 0;
  27082. }
  27083. #endif
  27084. static int mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  27085. {
  27086. int i;
  27087. if (randNum(a, 4, rng, NULL) != 0)
  27088. return -12680;
  27089. for (i = 0; i < 4; i++) {
  27090. mp_copy(r1, a);
  27091. if (mp_lshd(r1, i) != MP_OKAY)
  27092. return -12681;
  27093. mp_rshd(r1, i);
  27094. if (mp_cmp(a, r1) != MP_EQ)
  27095. return -12682;
  27096. }
  27097. for (i = 0; i < DIGIT_BIT+1; i++) {
  27098. if (mp_mul_2d(a, i, r1) != MP_OKAY)
  27099. return -12683;
  27100. mp_rshb(r1, i);
  27101. if (mp_cmp(a, r1) != MP_EQ)
  27102. return -12684;
  27103. }
  27104. return 0;
  27105. }
  27106. static int mp_test_add_sub_d(mp_int* a, mp_int* r1)
  27107. {
  27108. int i, j;
  27109. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  27110. mp_zero(a);
  27111. mp_set_bit(a, i);
  27112. if (a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  27113. return -12690;
  27114. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  27115. mp_zero(r1);
  27116. mp_set_bit(r1, i);
  27117. if (mp_sub_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  27118. return -12691;
  27119. if (mp_add_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  27120. return -12692;
  27121. if (mp_cmp(a, r1) != MP_EQ)
  27122. return -12693;
  27123. }
  27124. }
  27125. mp_zero(r1);
  27126. if (mp_add_d(r1, 1, r1) != MP_OKAY)
  27127. return -12694;
  27128. if (r1->used != 1)
  27129. return -12695;
  27130. if (mp_sub_d(r1, 1, r1) != MP_OKAY)
  27131. return -12696;
  27132. if (r1->used != 0)
  27133. return -12697;
  27134. #ifdef WOLFSSL_SP_MATH
  27135. if (mp_set(r1, 1) != MP_OKAY)
  27136. return -12698;
  27137. if (mp_mul_2d(r1, SP_INT_MAX_BITS - 1, r1) != MP_OKAY)
  27138. return -12699;
  27139. if (mp_sub_d(r1, 1, r1) != MP_OKAY)
  27140. return -12700;
  27141. if (mp_mul_2d(r1, 1, r1) != MP_OKAY)
  27142. return -12701;
  27143. if (mp_add_d(r1, 1, r1) != MP_OKAY)
  27144. return -12702;
  27145. if (mp_add_d(r1, 1, r1) == MP_OKAY)
  27146. return -12703;
  27147. #endif
  27148. return 0;
  27149. }
  27150. static int mp_test_read_to_bin(mp_int* a)
  27151. {
  27152. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  27153. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  27154. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  27155. };
  27156. byte out[24];
  27157. int i, j, k;
  27158. const byte* p;
  27159. int ret;
  27160. for (i = 0; i < (int)sizeof(in); i++) {
  27161. p = in + sizeof(in) - i;
  27162. ret = mp_read_unsigned_bin(a, p, i);
  27163. if (ret != 0)
  27164. return -12710;
  27165. for (j = i; j < (int)sizeof(out); j++) {
  27166. XMEMSET(out, 0xff, sizeof(out));
  27167. ret = mp_to_unsigned_bin_len(a, out, j);
  27168. if (ret != 0)
  27169. return -12711;
  27170. for (k = 0; k < j - i; k++) {
  27171. if (out[k] != 0)
  27172. return -12712;
  27173. }
  27174. for (; k < j; k++) {
  27175. if (out[k] != p[k - (j - i)])
  27176. return -12713;
  27177. }
  27178. }
  27179. }
  27180. ret = mp_read_unsigned_bin(a, NULL, 0);
  27181. if (ret != 0)
  27182. return -12714;
  27183. if (!mp_iszero(a))
  27184. return -12715;
  27185. return 0;
  27186. }
  27187. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  27188. static int mp_test_set_int(mp_int* a)
  27189. {
  27190. #if SP_ULONG_BITS == 64
  27191. unsigned long n = 0xfedcba9876543210UL;
  27192. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  27193. byte out[8] = { 0 };
  27194. #elif SP_ULONG_BITS == 32
  27195. unsigned long n = 0xfedcba98UL;
  27196. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  27197. byte out[4] = { 0 };
  27198. #elif SP_ULONG_BITS == 16
  27199. unsigned long n = 0xfedc;
  27200. byte exp[2] = { 0xfe, 0xdc };
  27201. byte out[2] = { 0 };
  27202. #elif SP_ULONG_BITS == 8
  27203. unsigned long n = 0xfe;
  27204. byte exp[1] = { 0xfe };
  27205. byte out[1] = { 0 };
  27206. #endif
  27207. int ret;
  27208. ret = mp_set_int(a, n);
  27209. if (ret != 0)
  27210. return -12720;
  27211. ret = mp_unsigned_bin_size(a);
  27212. if (ret != sizeof(exp))
  27213. return -12721;
  27214. ret = mp_to_unsigned_bin(a, out);
  27215. if (ret != 0)
  27216. return -12722;
  27217. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  27218. return -12723;
  27219. return 0;
  27220. }
  27221. #endif
  27222. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  27223. static int mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  27224. {
  27225. byte buffer[16];
  27226. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  27227. char hexStr[] = "abcdef0123456789";
  27228. #ifndef WOLFSSL_SP_INT_NEGATIVE
  27229. char negStr[] = "-1234";
  27230. #endif
  27231. #endif
  27232. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  27233. defined(HAVE_COMP_KEY)
  27234. char decStr[] = "0987654321";
  27235. #endif
  27236. int ret;
  27237. #ifdef WOLFSSL_SP_MATH_ALL
  27238. mp_digit rho;
  27239. int size;
  27240. #endif
  27241. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  27242. int result;
  27243. #endif
  27244. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  27245. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  27246. mp_digit rd;
  27247. #endif
  27248. (void)rng;
  27249. (void)r;
  27250. ret = mp_init(NULL);
  27251. if (ret != MP_VAL)
  27252. return -12730;
  27253. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  27254. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  27255. if (ret != MP_OKAY)
  27256. return -12731;
  27257. #endif
  27258. mp_free(NULL);
  27259. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  27260. ret = mp_grow(NULL, 1);
  27261. if (ret != MP_VAL)
  27262. return -12732;
  27263. #ifdef WOLFSSL_SP_MATH
  27264. ret = mp_grow(a, SP_INT_DIGITS + 1);
  27265. if (ret != MP_MEM)
  27266. return -12733;
  27267. #endif
  27268. #endif
  27269. mp_clear(NULL);
  27270. ret = mp_abs(NULL, NULL);
  27271. if (ret != MP_VAL)
  27272. return -12734;
  27273. ret = mp_abs(a, NULL);
  27274. if (ret != MP_VAL)
  27275. return -12735;
  27276. ret = mp_abs(NULL, b);
  27277. if (ret != MP_VAL)
  27278. return -12736;
  27279. ret = mp_unsigned_bin_size(NULL);
  27280. if (ret != 0)
  27281. return -12737;
  27282. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  27283. if (ret != MP_VAL)
  27284. return -12738;
  27285. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  27286. if (ret != MP_VAL)
  27287. return -12739;
  27288. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  27289. if (ret != MP_VAL)
  27290. return -12740;
  27291. ret = mp_read_unsigned_bin(a, buffer,
  27292. (SP_INT_DIGITS - 1) * SP_WORD_SIZEOF + 1);
  27293. if (ret != MP_VAL)
  27294. return -12741;
  27295. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  27296. ret = mp_read_radix(NULL, NULL, 16);
  27297. if (ret != MP_VAL)
  27298. return -12742;
  27299. ret = mp_read_radix(a, NULL, 16);
  27300. if (ret != MP_VAL)
  27301. return -12743;
  27302. ret = mp_read_radix(NULL, hexStr, 16);
  27303. if (ret != MP_VAL)
  27304. return -12744;
  27305. #ifndef WOLFSSL_SP_INT_NEGATIVE
  27306. ret = mp_read_radix(a, negStr, 16);
  27307. if (ret != MP_VAL)
  27308. return -12745;
  27309. #ifdef WOLFSSL_SP_MATH_ALL
  27310. ret = mp_read_radix(a, negStr, 10);
  27311. if (ret != MP_VAL)
  27312. return -12746;
  27313. #endif /* WOLFSSL_SP_MATH_ALL */
  27314. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  27315. #endif
  27316. #ifndef WOLFSSL_SP_MATH_ALL
  27317. /* Radix 10 only supported with ALL. */
  27318. ret = mp_read_radix(a, decStr, 10);
  27319. if (ret != MP_VAL)
  27320. return -12747;
  27321. #endif
  27322. /* Radix 8 not supported SP_INT. */
  27323. ret = mp_read_radix(a, "0123", 8);
  27324. if (ret != MP_VAL)
  27325. return -12748;
  27326. ret = mp_count_bits(NULL);
  27327. if (ret != 0)
  27328. return -12749;
  27329. ret = mp_is_bit_set(NULL, 0);
  27330. if (ret != 0)
  27331. return -12750;
  27332. ret = mp_leading_bit(NULL);
  27333. if (ret != 0)
  27334. return -12751;
  27335. mp_zero(a);
  27336. ret = mp_leading_bit(a);
  27337. if (ret != 0)
  27338. return -12752;
  27339. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  27340. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  27341. !defined(NO_RSA)
  27342. ret = mp_set_bit(NULL, 1);
  27343. if (ret != MP_VAL)
  27344. return -12753;
  27345. #endif
  27346. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  27347. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  27348. ret = mp_to_unsigned_bin(NULL, NULL);
  27349. if (ret != MP_VAL)
  27350. return -12754;
  27351. ret = mp_to_unsigned_bin(a, NULL);
  27352. if (ret != MP_VAL)
  27353. return -12755;
  27354. ret = mp_to_unsigned_bin(NULL, buffer);
  27355. if (ret != MP_VAL)
  27356. return -12756;
  27357. #endif
  27358. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  27359. if (ret != MP_VAL)
  27360. return -12757;
  27361. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  27362. if (ret != MP_VAL)
  27363. return -12758;
  27364. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  27365. if (ret != MP_VAL)
  27366. return -12759;
  27367. #ifdef WOLFSSL_SP_MATH_ALL
  27368. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  27369. if (ret != MP_VAL)
  27370. return -12760;
  27371. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  27372. if (ret != MP_VAL)
  27373. return -12761;
  27374. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  27375. if (ret != MP_VAL)
  27376. return -12762;
  27377. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  27378. if (ret != MP_OKAY)
  27379. return -12763;
  27380. #endif
  27381. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  27382. ret = mp_copy(NULL, NULL);
  27383. if (ret != MP_VAL)
  27384. return -12764;
  27385. ret = mp_copy(a, NULL);
  27386. if (ret != MP_VAL)
  27387. return -12765;
  27388. ret = mp_copy(NULL, b);
  27389. if (ret != MP_VAL)
  27390. return -12766;
  27391. #endif
  27392. #if defined(WOLFSSL_KEY_GEN)
  27393. ret = sp_2expt(NULL, 1);
  27394. if (ret != MP_VAL)
  27395. return -12767;
  27396. #endif
  27397. ret = mp_set(NULL, 0);
  27398. if (ret != MP_VAL)
  27399. return -12768;
  27400. ret = mp_cmp_d(NULL, 0);
  27401. if (ret != MP_LT)
  27402. return -12769;
  27403. ret = mp_cmp(NULL, NULL);
  27404. if (ret != MP_EQ)
  27405. return -12770;
  27406. ret = mp_cmp(a, NULL);
  27407. if (ret != MP_GT)
  27408. return -12771;
  27409. ret = mp_cmp(NULL, b);
  27410. if (ret != MP_LT)
  27411. return -12772;
  27412. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  27413. mp_rshd(NULL, 1);
  27414. #endif
  27415. mp_zero(NULL);
  27416. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  27417. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  27418. ret = mp_lshd(NULL, 0);
  27419. if (ret != MP_VAL)
  27420. return -12773;
  27421. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  27422. if (ret != MP_VAL)
  27423. return -12774;
  27424. #endif
  27425. #if defined(WOLFSSL_SP_MATH_ALL)
  27426. ret = mp_div(NULL, NULL, a, b);
  27427. if (ret != MP_VAL)
  27428. return -12775;
  27429. ret = mp_div(a, NULL, a, b);
  27430. if (ret != MP_VAL)
  27431. return -12776;
  27432. ret = mp_div(NULL, b, a, b);
  27433. if (ret != MP_VAL)
  27434. return -12777;
  27435. ret = mp_div(a, b, NULL, NULL);
  27436. if (ret != MP_VAL)
  27437. return -12778;
  27438. #endif
  27439. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  27440. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  27441. ret = mp_mod(NULL, NULL, NULL);
  27442. if (ret != MP_VAL)
  27443. return -12779;
  27444. ret = mp_mod(a, NULL, NULL);
  27445. if (ret != MP_VAL)
  27446. return -12780;
  27447. ret = mp_mod(NULL, b, NULL);
  27448. if (ret != MP_VAL)
  27449. return -12781;
  27450. ret = mp_mod(NULL, NULL, r);
  27451. if (ret != MP_VAL)
  27452. return -12782;
  27453. ret = mp_mod(a, b, NULL);
  27454. if (ret != MP_VAL)
  27455. return -12783;
  27456. ret = mp_mod(a, NULL, r);
  27457. if (ret != MP_VAL)
  27458. return -12784;
  27459. ret = mp_mod(NULL, b, r);
  27460. if (ret != MP_VAL)
  27461. return -12785;
  27462. #endif
  27463. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  27464. ret = mp_set_int(NULL, 0);
  27465. if (ret != MP_VAL)
  27466. return -12786;
  27467. #endif
  27468. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  27469. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  27470. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  27471. if (ret != MP_VAL)
  27472. return 9950;
  27473. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  27474. if (ret != MP_VAL)
  27475. return 9951;
  27476. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  27477. if (ret != MP_VAL)
  27478. return 9952;
  27479. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  27480. if (ret != MP_VAL)
  27481. return 9953;
  27482. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  27483. if (ret != MP_VAL)
  27484. return 9954;
  27485. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  27486. if (ret != MP_VAL)
  27487. return 9955;
  27488. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  27489. if (ret != MP_VAL)
  27490. return 9956;
  27491. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  27492. if (ret != MP_VAL)
  27493. return 9957;
  27494. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  27495. if (ret != MP_VAL)
  27496. return 9958;
  27497. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  27498. if (ret != MP_VAL)
  27499. return 9960;
  27500. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  27501. if (ret != MP_VAL)
  27502. return 9961;
  27503. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  27504. if (ret != MP_VAL)
  27505. return 9962;
  27506. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  27507. if (ret != MP_VAL)
  27508. return 9963;
  27509. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  27510. if (ret != MP_VAL)
  27511. return 9964;
  27512. ret = mp_exptmod_nct(a, a, a, NULL);
  27513. if (ret != MP_VAL)
  27514. return 9965;
  27515. ret = mp_exptmod_nct(a, a, NULL, a);
  27516. if (ret != MP_VAL)
  27517. return 9966;
  27518. ret = mp_exptmod_nct(a, NULL, a, a);
  27519. if (ret != MP_VAL)
  27520. return 9967;
  27521. ret = mp_exptmod_nct(NULL, a, a, a);
  27522. if (ret != MP_VAL)
  27523. return 9968;
  27524. #endif
  27525. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  27526. !defined(WC_NO_RNG)
  27527. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  27528. if (ret != MP_VAL)
  27529. return -12787;
  27530. ret = mp_rand_prime(a, 32, NULL, NULL);
  27531. if (ret != MP_VAL)
  27532. return -12788;
  27533. ret = mp_rand_prime(NULL, 32, rng, NULL);
  27534. if (ret != MP_VAL)
  27535. return -12789;
  27536. ret = mp_rand_prime(a, 0, rng, NULL);
  27537. if (ret != MP_VAL)
  27538. return -9969;
  27539. #endif
  27540. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  27541. ret = mp_mul(NULL, NULL, NULL);
  27542. if (ret != MP_VAL)
  27543. return -12790;
  27544. ret = mp_mul(a, NULL, NULL);
  27545. if (ret != MP_VAL)
  27546. return -12791;
  27547. ret = mp_mul(NULL, b, NULL);
  27548. if (ret != MP_VAL)
  27549. return -12792;
  27550. ret = mp_mul(NULL, NULL, r);
  27551. if (ret != MP_VAL)
  27552. return -12793;
  27553. ret = mp_mul(a, b, NULL);
  27554. if (ret != MP_VAL)
  27555. return -12794;
  27556. ret = mp_mul(a, NULL, r);
  27557. if (ret != MP_VAL)
  27558. return -12795;
  27559. ret = mp_mul(NULL, b, r);
  27560. if (ret != MP_VAL)
  27561. return -12796;
  27562. #endif
  27563. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  27564. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  27565. ret = mp_sqr(NULL, NULL);
  27566. if (ret != MP_VAL)
  27567. return -12797;
  27568. ret = mp_sqr(a, NULL);
  27569. if (ret != MP_VAL)
  27570. return -12798;
  27571. ret = mp_sqr(NULL, r);
  27572. if (ret != MP_VAL)
  27573. return -12799;
  27574. #endif
  27575. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  27576. ret = mp_sqrmod(NULL, NULL, NULL);
  27577. if (ret != MP_VAL)
  27578. return -12800;
  27579. ret = mp_sqrmod(a, NULL, NULL);
  27580. if (ret != MP_VAL)
  27581. return -12801;
  27582. ret = mp_sqrmod(NULL, a, NULL);
  27583. if (ret != MP_VAL)
  27584. return -12802;
  27585. ret = mp_sqrmod(NULL, NULL, a);
  27586. if (ret != MP_VAL)
  27587. return -12803;
  27588. ret = mp_sqrmod(a, b, NULL);
  27589. if (ret != MP_VAL)
  27590. return -12804;
  27591. ret = mp_sqrmod(a, NULL, b);
  27592. if (ret != MP_VAL)
  27593. return -12805;
  27594. ret = mp_sqrmod(NULL, a, b);
  27595. if (ret != MP_VAL)
  27596. return -12806;
  27597. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  27598. if (ret != MP_VAL)
  27599. return -12807;
  27600. ret = mp_mulmod(a, NULL, NULL, NULL);
  27601. if (ret != MP_VAL)
  27602. return -12808;
  27603. ret = mp_mulmod(NULL, a, NULL, NULL);
  27604. if (ret != MP_VAL)
  27605. return -12809;
  27606. ret = mp_mulmod(NULL, NULL, a, NULL);
  27607. if (ret != MP_VAL)
  27608. return -12810;
  27609. ret = mp_mulmod(NULL, NULL, NULL, a);
  27610. if (ret != MP_VAL)
  27611. return -12811;
  27612. ret = mp_mulmod(a, b, b, NULL);
  27613. if (ret != MP_VAL)
  27614. return -12812;
  27615. ret = mp_mulmod(a, b, NULL, a);
  27616. if (ret != MP_VAL)
  27617. return -12813;
  27618. ret = mp_mulmod(a, NULL, b, a);
  27619. if (ret != MP_VAL)
  27620. return -12814;
  27621. ret = mp_mulmod(NULL, b, b, a);
  27622. if (ret != MP_VAL)
  27623. return -12815;
  27624. #endif
  27625. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  27626. !defined(NO_RSA) || !defined(NO_DSA)
  27627. ret = mp_add_d(NULL, 1, NULL);
  27628. if (ret != MP_VAL)
  27629. return -12816;
  27630. ret = mp_add_d(a, 1, NULL);
  27631. if (ret != MP_VAL)
  27632. return -12817;
  27633. ret = mp_add_d(NULL, 1, b);
  27634. if (ret != MP_VAL)
  27635. return -12818;
  27636. #endif
  27637. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  27638. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  27639. ret = mp_sub_d(NULL, 1, NULL);
  27640. if (ret != MP_VAL)
  27641. return -12819;
  27642. ret = mp_sub_d(a, 1, NULL);
  27643. if (ret != MP_VAL)
  27644. return -12820;
  27645. ret = mp_sub_d(NULL, 1, b);
  27646. if (ret != MP_VAL)
  27647. return -12821;
  27648. #endif
  27649. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  27650. (defined(HAVE_ECC) && defined(FP_ECC))
  27651. ret = mp_div_d(NULL, 0, NULL, NULL);
  27652. if (ret != MP_VAL)
  27653. return -12822;
  27654. ret = mp_div_d(a, 0, NULL, NULL);
  27655. if (ret != MP_VAL)
  27656. return -12823;
  27657. ret = mp_div_d(NULL, 1, NULL, NULL);
  27658. if (ret != MP_VAL)
  27659. return -12824;
  27660. #endif
  27661. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  27662. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  27663. ret = mp_mod_d(NULL, 0, NULL);
  27664. if (ret != MP_VAL)
  27665. return -12825;
  27666. ret = mp_mod_d(a, 0, NULL);
  27667. if (ret != MP_VAL)
  27668. return -12826;
  27669. ret = mp_mod_d(NULL, 0, &rd);
  27670. if (ret != MP_VAL)
  27671. return -12827;
  27672. #endif
  27673. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  27674. (defined(HAVE_ECC) && defined(FP_ECC))
  27675. ret = mp_gcd(NULL, NULL, NULL);
  27676. if (ret != MP_VAL)
  27677. return -12828;
  27678. ret = mp_gcd(a, NULL, NULL);
  27679. if (ret != MP_VAL)
  27680. return -12829;
  27681. ret = mp_gcd(NULL, a, NULL);
  27682. if (ret != MP_VAL)
  27683. return -12830;
  27684. ret = mp_gcd(NULL, NULL, a);
  27685. if (ret != MP_VAL)
  27686. return -12831;
  27687. ret = mp_gcd(a, b, NULL);
  27688. if (ret != MP_VAL)
  27689. return -12832;
  27690. ret = mp_gcd(a, NULL, b);
  27691. if (ret != MP_VAL)
  27692. return -12833;
  27693. ret = mp_gcd(NULL, a, b);
  27694. if (ret != MP_VAL)
  27695. return -12834;
  27696. #endif
  27697. #ifdef HAVE_ECC
  27698. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  27699. if (ret != MP_VAL)
  27700. return -12835;
  27701. ret = mp_div_2_mod_ct(a, NULL, NULL);
  27702. if (ret != MP_VAL)
  27703. return -12836;
  27704. ret = mp_div_2_mod_ct(NULL, b, NULL);
  27705. if (ret != MP_VAL)
  27706. return -12837;
  27707. ret = mp_div_2_mod_ct(NULL, NULL, a);
  27708. if (ret != MP_VAL)
  27709. return -12838;
  27710. ret = mp_div_2_mod_ct(a, b, NULL);
  27711. if (ret != MP_VAL)
  27712. return -12839;
  27713. ret = mp_div_2_mod_ct(a, b, NULL);
  27714. if (ret != MP_VAL)
  27715. return -12840;
  27716. ret = mp_div_2_mod_ct(NULL, b, a);
  27717. if (ret != MP_VAL)
  27718. return -12841;
  27719. ret = mp_div_2(NULL, NULL);
  27720. if (ret != MP_VAL)
  27721. return -12842;
  27722. ret = mp_div_2(a, NULL);
  27723. if (ret != MP_VAL)
  27724. return -12843;
  27725. ret = mp_div_2(NULL, a);
  27726. if (ret != MP_VAL)
  27727. return -12844;
  27728. #endif
  27729. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  27730. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  27731. ret = mp_invmod(NULL, NULL, NULL);
  27732. if (ret != MP_VAL)
  27733. return -12845;
  27734. ret = mp_invmod(a, NULL, NULL);
  27735. if (ret != MP_VAL)
  27736. return -12846;
  27737. ret = mp_invmod(NULL, b, NULL);
  27738. if (ret != MP_VAL)
  27739. return -12847;
  27740. ret = mp_invmod(NULL, NULL, a);
  27741. if (ret != MP_VAL)
  27742. return -12848;
  27743. ret = mp_invmod(a, b, NULL);
  27744. if (ret != MP_VAL)
  27745. return -12849;
  27746. ret = mp_invmod(a, NULL, a);
  27747. if (ret != MP_VAL)
  27748. return -12850;
  27749. ret = mp_invmod(NULL, b, a);
  27750. if (ret != MP_VAL)
  27751. return -12851;
  27752. #endif
  27753. #ifdef HAVE_ECC
  27754. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  27755. if (ret != MP_VAL)
  27756. return -12852;
  27757. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  27758. if (ret != MP_VAL)
  27759. return -12853;
  27760. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  27761. if (ret != MP_VAL)
  27762. return -12854;
  27763. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  27764. if (ret != MP_VAL)
  27765. return -12855;
  27766. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  27767. if (ret != MP_VAL)
  27768. return -12856;
  27769. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  27770. if (ret != MP_VAL)
  27771. return -12857;
  27772. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  27773. if (ret != MP_VAL)
  27774. return -12858;
  27775. #endif
  27776. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  27777. ret = mp_lcm(NULL, NULL, NULL);
  27778. if (ret != MP_VAL)
  27779. return -12859;
  27780. ret = mp_lcm(a, NULL, NULL);
  27781. if (ret != MP_VAL)
  27782. return -12860;
  27783. ret = mp_lcm(NULL, b, NULL);
  27784. if (ret != MP_VAL)
  27785. return -12861;
  27786. ret = mp_lcm(NULL, NULL, a);
  27787. if (ret != MP_VAL)
  27788. return -12862;
  27789. ret = mp_lcm(a, b, NULL);
  27790. if (ret != MP_VAL)
  27791. return -12863;
  27792. ret = mp_lcm(a, NULL, a);
  27793. if (ret != MP_VAL)
  27794. return -12864;
  27795. ret = mp_lcm(NULL, b, a);
  27796. if (ret != MP_VAL)
  27797. return -12865;
  27798. #endif
  27799. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  27800. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  27801. if (ret != MP_VAL)
  27802. return -12866;
  27803. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  27804. if (ret != MP_VAL)
  27805. return -12867;
  27806. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  27807. if (ret != MP_VAL)
  27808. return -12868;
  27809. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  27810. if (ret != MP_VAL)
  27811. return -12869;
  27812. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  27813. if (ret != MP_VAL)
  27814. return -12870;
  27815. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  27816. if (ret != MP_VAL)
  27817. return -12871;
  27818. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  27819. if (ret != MP_VAL)
  27820. return -12872;
  27821. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  27822. if (ret != MP_VAL)
  27823. return -12873;
  27824. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  27825. if (ret != MP_VAL)
  27826. return -12874;
  27827. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  27828. if (ret != MP_VAL)
  27829. return -12875;
  27830. ret = mp_exptmod(a, NULL, NULL, NULL);
  27831. if (ret != MP_VAL)
  27832. return -12876;
  27833. ret = mp_exptmod(NULL, b, NULL, NULL);
  27834. if (ret != MP_VAL)
  27835. return -12877;
  27836. ret = mp_exptmod(NULL, NULL, b, NULL);
  27837. if (ret != MP_VAL)
  27838. return -12878;
  27839. ret = mp_exptmod(NULL, NULL, NULL, a);
  27840. if (ret != MP_VAL)
  27841. return -12879;
  27842. ret = mp_exptmod(a, b, b, NULL);
  27843. if (ret != MP_VAL)
  27844. return -12880;
  27845. ret = mp_exptmod(a, b, NULL, a);
  27846. if (ret != MP_VAL)
  27847. return -12881;
  27848. ret = mp_exptmod(a, NULL, b, a);
  27849. if (ret != MP_VAL)
  27850. return -12882;
  27851. ret = mp_exptmod(NULL, b, b, a);
  27852. if (ret != MP_VAL)
  27853. return -12883;
  27854. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  27855. if (ret != MP_VAL)
  27856. return -12884;
  27857. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  27858. if (ret != MP_VAL)
  27859. return -12885;
  27860. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  27861. if (ret != MP_VAL)
  27862. return -12886;
  27863. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  27864. if (ret != MP_VAL)
  27865. return -12887;
  27866. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  27867. if (ret != MP_VAL)
  27868. return -12888;
  27869. ret = mp_exptmod_nct(a, b, b, NULL);
  27870. if (ret != MP_VAL)
  27871. return -12889;
  27872. ret = mp_exptmod_nct(a, b, NULL, a);
  27873. if (ret != MP_VAL)
  27874. return -12890;
  27875. ret = mp_exptmod_nct(a, NULL, b, a);
  27876. if (ret != MP_VAL)
  27877. return -12891;
  27878. ret = mp_exptmod_nct(NULL, b, b, a);
  27879. if (ret != MP_VAL)
  27880. return -12892;
  27881. #endif
  27882. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  27883. ret = mp_cnt_lsb(NULL);
  27884. if (ret != 0)
  27885. return -12893;
  27886. #endif
  27887. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  27888. ret = mp_prime_is_prime(NULL, 1, NULL);
  27889. if (ret != MP_VAL)
  27890. return -12894;
  27891. ret = mp_prime_is_prime(a, 1, NULL);
  27892. if (ret != MP_VAL)
  27893. return -12895;
  27894. ret = mp_prime_is_prime(NULL, 1, &result);
  27895. if (ret != MP_VAL)
  27896. return -12896;
  27897. ret = mp_prime_is_prime(a, 0, &result);
  27898. if (ret != MP_VAL)
  27899. return -12897;
  27900. ret = mp_prime_is_prime(a, 1024, &result);
  27901. if (ret != MP_VAL)
  27902. return -12898;
  27903. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  27904. if (ret != MP_VAL)
  27905. return -12899;
  27906. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  27907. if (ret != MP_VAL)
  27908. return -12900;
  27909. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  27910. if (ret != MP_VAL)
  27911. return -12901;
  27912. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  27913. if (ret != MP_VAL)
  27914. return -12902;
  27915. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  27916. if (ret != MP_VAL)
  27917. return -12903;
  27918. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  27919. if (ret != MP_VAL)
  27920. return -12904;
  27921. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  27922. if (ret != MP_VAL)
  27923. return -12905;
  27924. #endif
  27925. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  27926. ret = mp_exch(NULL, NULL);
  27927. if (ret != MP_VAL)
  27928. return -12906;
  27929. ret = mp_exch(a, NULL);
  27930. if (ret != MP_VAL)
  27931. return -12907;
  27932. ret = mp_exch(NULL, b);
  27933. if (ret != MP_VAL)
  27934. return -12908;
  27935. #endif
  27936. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  27937. defined(WOLFSSL_SP_MATH_ALL)
  27938. ret = mp_mul_d(NULL, 1, NULL);
  27939. if (ret != MP_VAL)
  27940. return -12909;
  27941. ret = mp_mul_d(a, 1, NULL);
  27942. if (ret != MP_VAL)
  27943. return -12910;
  27944. ret = mp_mul_d(NULL, 1, b);
  27945. if (ret != MP_VAL)
  27946. return -12911;
  27947. #endif
  27948. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  27949. ret = mp_add(NULL, NULL, NULL);
  27950. if (ret != MP_VAL)
  27951. return -12912;
  27952. ret = mp_add(a, NULL, NULL);
  27953. if (ret != MP_VAL)
  27954. return -12913;
  27955. ret = mp_add(NULL, b, NULL);
  27956. if (ret != MP_VAL)
  27957. return -12914;
  27958. ret = mp_add(NULL, NULL, r);
  27959. if (ret != MP_VAL)
  27960. return -12915;
  27961. ret = mp_add(a, b, NULL);
  27962. if (ret != MP_VAL)
  27963. return -12916;
  27964. ret = mp_add(a, NULL, r);
  27965. if (ret != MP_VAL)
  27966. return -12917;
  27967. ret = mp_add(NULL, b, r);
  27968. if (ret != MP_VAL)
  27969. return -12918;
  27970. #endif
  27971. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  27972. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  27973. ret = mp_sub(NULL, NULL, NULL);
  27974. if (ret != MP_VAL)
  27975. return -12919;
  27976. ret = mp_sub(a, NULL, NULL);
  27977. if (ret != MP_VAL)
  27978. return -12920;
  27979. ret = mp_sub(NULL, b, NULL);
  27980. if (ret != MP_VAL)
  27981. return -12921;
  27982. ret = mp_sub(NULL, NULL, r);
  27983. if (ret != MP_VAL)
  27984. return -12922;
  27985. ret = mp_sub(a, b, NULL);
  27986. if (ret != MP_VAL)
  27987. return -12923;
  27988. ret = mp_sub(a, NULL, r);
  27989. if (ret != MP_VAL)
  27990. return -12924;
  27991. ret = mp_sub(NULL, b, r);
  27992. if (ret != MP_VAL)
  27993. return -12925;
  27994. #endif
  27995. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  27996. defined(WOLFSSL_CUSTOM_CURVES))
  27997. ret = mp_addmod(NULL, NULL, NULL, NULL);
  27998. if (ret != MP_VAL)
  27999. return -12926;
  28000. ret = mp_addmod(a, NULL, NULL, NULL);
  28001. if (ret != MP_VAL)
  28002. return -12927;
  28003. ret = mp_addmod(NULL, b, NULL, NULL);
  28004. if (ret != MP_VAL)
  28005. return -12928;
  28006. ret = mp_addmod(NULL, NULL, b, NULL);
  28007. if (ret != MP_VAL)
  28008. return -12929;
  28009. ret = mp_addmod(NULL, NULL, NULL, a);
  28010. if (ret != MP_VAL)
  28011. return -12930;
  28012. ret = mp_addmod(a, b, b, NULL);
  28013. if (ret != MP_VAL)
  28014. return -12931;
  28015. ret = mp_addmod(a, b, NULL, a);
  28016. if (ret != MP_VAL)
  28017. return -12932;
  28018. ret = mp_addmod(a, NULL, b, a);
  28019. if (ret != MP_VAL)
  28020. return -12933;
  28021. ret = mp_addmod(NULL, b, b, a);
  28022. if (ret != MP_VAL)
  28023. return -12934;
  28024. #endif
  28025. #ifdef WOLFSSL_SP_MATH_ALL
  28026. ret = mp_submod(NULL, NULL, NULL, NULL);
  28027. if (ret != MP_VAL)
  28028. return -12935;
  28029. ret = mp_submod(a, NULL, NULL, NULL);
  28030. if (ret != MP_VAL)
  28031. return -12936;
  28032. ret = mp_submod(NULL, b, NULL, NULL);
  28033. if (ret != MP_VAL)
  28034. return -12937;
  28035. ret = mp_submod(NULL, NULL, b, NULL);
  28036. if (ret != MP_VAL)
  28037. return -12938;
  28038. ret = mp_submod(NULL, NULL, NULL, a);
  28039. if (ret != MP_VAL)
  28040. return -12939;
  28041. ret = mp_submod(a, b, b, NULL);
  28042. if (ret != MP_VAL)
  28043. return -12940;
  28044. ret = mp_submod(a, b, NULL, a);
  28045. if (ret != MP_VAL)
  28046. return -12941;
  28047. ret = mp_submod(a, NULL, b, a);
  28048. if (ret != MP_VAL)
  28049. return -12942;
  28050. ret = mp_submod(NULL, b, b, a);
  28051. if (ret != MP_VAL)
  28052. return -12943;
  28053. #endif
  28054. #ifdef WOLFSSL_SP_MATH_ALL
  28055. ret = mp_div_2d(NULL, 1, a, b);
  28056. if (ret != MP_VAL)
  28057. return -12944;
  28058. ret = mp_mod_2d(NULL, 1, NULL);
  28059. if (ret != MP_VAL)
  28060. return -12945;
  28061. ret = mp_mod_2d(a, 1, NULL);
  28062. if (ret != MP_VAL)
  28063. return -12946;
  28064. ret = mp_mod_2d(NULL, 1, b);
  28065. if (ret != MP_VAL)
  28066. return -12947;
  28067. ret = mp_mul_2d(NULL, 1, NULL);
  28068. if (ret != MP_VAL)
  28069. return -12948;
  28070. ret = mp_mul_2d(a, 1, NULL);
  28071. if (ret != MP_VAL)
  28072. return -12949;
  28073. ret = mp_mul_2d(NULL, 1, b);
  28074. if (ret != MP_VAL)
  28075. return -12950;
  28076. #endif
  28077. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  28078. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  28079. ret = mp_montgomery_reduce(NULL, NULL, 1);
  28080. if (ret != MP_VAL)
  28081. return -12951;
  28082. ret = mp_montgomery_reduce(a, NULL, 1);
  28083. if (ret != MP_VAL)
  28084. return -12952;
  28085. ret = mp_montgomery_reduce(NULL, b, 1);
  28086. if (ret != MP_VAL)
  28087. return -12953;
  28088. mp_zero(b);
  28089. ret = mp_montgomery_reduce(a, b, 1);
  28090. if (ret != MP_VAL)
  28091. return -12954;
  28092. #endif
  28093. #ifdef WOLFSSL_SP_MATH_ALL
  28094. ret = mp_montgomery_setup(NULL, NULL);
  28095. if (ret != MP_VAL)
  28096. return -12955;
  28097. ret = mp_montgomery_setup(a, NULL);
  28098. if (ret != MP_VAL)
  28099. return -12956;
  28100. ret = mp_montgomery_setup(NULL, &rho);
  28101. if (ret != MP_VAL)
  28102. return -12957;
  28103. ret = mp_montgomery_calc_normalization(NULL, NULL);
  28104. if (ret != MP_VAL)
  28105. return -12958;
  28106. ret = mp_montgomery_calc_normalization(a, NULL);
  28107. if (ret != MP_VAL)
  28108. return -12959;
  28109. ret = mp_montgomery_calc_normalization(NULL, b);
  28110. if (ret != MP_VAL)
  28111. return -12960;
  28112. #endif
  28113. ret = mp_unsigned_bin_size(NULL);
  28114. if (ret != 0)
  28115. return -12961;
  28116. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  28117. ret = mp_tohex(NULL, NULL);
  28118. if (ret != MP_VAL)
  28119. return -12962;
  28120. ret = mp_tohex(a, NULL);
  28121. if (ret != MP_VAL)
  28122. return -12963;
  28123. ret = mp_tohex(NULL, hexStr);
  28124. if (ret != MP_VAL)
  28125. return -12964;
  28126. #endif
  28127. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  28128. ret = mp_todecimal(NULL, NULL);
  28129. if (ret != MP_VAL)
  28130. return -12965;
  28131. ret = mp_todecimal(a, NULL);
  28132. if (ret != MP_VAL)
  28133. return -12966;
  28134. ret = mp_todecimal(NULL, decStr);
  28135. if (ret != MP_VAL)
  28136. return -12967;
  28137. #endif
  28138. #ifdef WOLFSSL_SP_MATH_ALL
  28139. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  28140. if (ret != MP_VAL)
  28141. return -12968;
  28142. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  28143. if (ret != MP_VAL)
  28144. return -12969;
  28145. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  28146. if (ret != MP_VAL)
  28147. return -12970;
  28148. ret = mp_toradix(a, hexStr, 3);
  28149. if (ret != MP_VAL)
  28150. return -12971;
  28151. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  28152. if (ret != MP_VAL)
  28153. return -12972;
  28154. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  28155. if (ret != MP_VAL)
  28156. return -12973;
  28157. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  28158. if (ret != MP_VAL)
  28159. return -12974;
  28160. ret = mp_radix_size(a, 3, &size);
  28161. if (ret != MP_VAL)
  28162. return -12975;
  28163. #endif
  28164. return 0;
  28165. }
  28166. #endif
  28167. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  28168. static int mp_test_set_is_bit(mp_int* a)
  28169. {
  28170. int i, j;
  28171. mp_zero(a);
  28172. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  28173. if (mp_is_bit_set(a, i))
  28174. return -12980;
  28175. for (j = 0; j < i; j++) {
  28176. if (!mp_is_bit_set(a, j))
  28177. return -12981;
  28178. }
  28179. if (mp_set_bit(a, i) != 0)
  28180. return -12982;
  28181. if (!mp_is_bit_set(a, i))
  28182. return -12983;
  28183. }
  28184. mp_zero(a);
  28185. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  28186. if (mp_is_bit_set(a, i))
  28187. return -12984;
  28188. }
  28189. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  28190. mp_zero(a);
  28191. if (mp_set_bit(a, i) != 0)
  28192. return -12985;
  28193. for (j = 0; j < i; j++) {
  28194. if (mp_is_bit_set(a, j))
  28195. return -12986;
  28196. }
  28197. if (!mp_is_bit_set(a, i))
  28198. return -12987;
  28199. }
  28200. #ifdef WOLFSSL_KEY_GEN
  28201. for (i = 0; i < DIGIT_BIT * 2; i++) {
  28202. mp_set(a, 1);
  28203. if (mp_2expt(a, i) != 0)
  28204. return -12988;
  28205. for (j = 0; j < i; j++) {
  28206. if (mp_is_bit_set(a, j))
  28207. return -12989;
  28208. }
  28209. if (!mp_is_bit_set(a, i))
  28210. return -12990;
  28211. }
  28212. #endif
  28213. #ifdef WOLFSSL_SP_MATH
  28214. mp_zero(a);
  28215. for (j = 1; j <= 3; j++) {
  28216. i = SP_INT_MAX_BITS - j;
  28217. if (mp_is_bit_set(a, i))
  28218. return -12991;
  28219. if (mp_set_bit(a, i) != 0)
  28220. return -12992;
  28221. if (!mp_is_bit_set(a, i))
  28222. return -12993;
  28223. #ifdef WOLFSSL_KEY_GEN
  28224. if (mp_2expt(a, i) != 0)
  28225. return -12994;
  28226. if (!mp_is_bit_set(a, i))
  28227. return -12995;
  28228. #endif
  28229. }
  28230. mp_zero(a);
  28231. for (j = 0; j <= 3; j++) {
  28232. i = SP_INT_MAX_BITS + j;
  28233. if (mp_is_bit_set(a, i))
  28234. return -12996;
  28235. if (mp_set_bit(a, i) != MP_VAL)
  28236. return -12997;
  28237. #ifdef WOLFSSL_KEY_GEN
  28238. if (mp_2expt(a, i) != MP_VAL)
  28239. return -12998;
  28240. #endif
  28241. }
  28242. #endif
  28243. return 0;
  28244. }
  28245. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  28246. static int mp_test_cmp(mp_int* a, mp_int* b)
  28247. {
  28248. int ret;
  28249. mp_zero(a);
  28250. mp_zero(b);
  28251. ret = mp_cmp_d(a, 0);
  28252. if (ret != MP_EQ)
  28253. return -13000;
  28254. ret = mp_cmp_d(a, 1);
  28255. if (ret != MP_LT)
  28256. return -13001;
  28257. ret = mp_cmp(a, b);
  28258. if (ret != MP_EQ)
  28259. return -13002;
  28260. mp_set(a, 1);
  28261. ret = mp_cmp_d(a, 0);
  28262. if (ret != MP_GT)
  28263. return -13003;
  28264. ret = mp_cmp_d(a, 1);
  28265. if (ret != MP_EQ)
  28266. return -13004;
  28267. ret = mp_cmp_d(a, 2);
  28268. if (ret != MP_LT)
  28269. return -13005;
  28270. ret = mp_cmp(a, b);
  28271. if (ret != MP_GT)
  28272. return -13006;
  28273. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  28274. ret = mp_cmp_d(b, -1);
  28275. if (ret != MP_GT)
  28276. return -13007;
  28277. ret = mp_cmp(a, b);
  28278. if (ret != MP_LT)
  28279. return -13008;
  28280. ret = mp_cmp(b, a);
  28281. if (ret != MP_GT)
  28282. return -13009;
  28283. ret = mp_cmp(b, b);
  28284. if (ret != MP_EQ)
  28285. return -13010;
  28286. return 0;
  28287. }
  28288. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  28289. static int mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  28290. {
  28291. int ret;
  28292. int i, j, k;
  28293. for (i = 0; i < 10; i++) {
  28294. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  28295. ret = randNum(a, j, rng, NULL);
  28296. if (ret != MP_OKAY)
  28297. return -13020;
  28298. mp_copy(a, b);
  28299. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  28300. ret = mp_mul_2d(a, k, a);
  28301. if (ret != MP_OKAY)
  28302. return -13021;
  28303. mp_rshb(a, k);
  28304. if (mp_cmp(a, b) != MP_EQ)
  28305. return -13022;
  28306. }
  28307. }
  28308. }
  28309. for (i = 0; i < 10; i++) {
  28310. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  28311. ret = randNum(a, j, rng, NULL);
  28312. if (ret != MP_OKAY)
  28313. return -13023;
  28314. mp_copy(a, b);
  28315. for (k = 0; k < 10; k++) {
  28316. ret = mp_lshd(a, k);
  28317. if (ret != MP_OKAY)
  28318. return -13024;
  28319. mp_rshd(a, k);
  28320. if (mp_cmp(a, b) != MP_EQ)
  28321. return -13025;
  28322. }
  28323. }
  28324. }
  28325. mp_zero(a);
  28326. mp_rshd(a, 1);
  28327. if (!mp_iszero(a))
  28328. return -13026;
  28329. mp_set(a, 1);
  28330. mp_rshd(a, 1);
  28331. if (!mp_iszero(a))
  28332. return -13027;
  28333. mp_set(a, 1);
  28334. mp_rshd(a, 2);
  28335. if (!mp_iszero(a))
  28336. return -13028;
  28337. return 0;
  28338. }
  28339. #endif
  28340. static int mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  28341. WC_RNG* rng)
  28342. {
  28343. int ret;
  28344. int i, j, k;
  28345. mp_zero(a);
  28346. mp_zero(d);
  28347. ret = mp_div(a, d, r, rem);
  28348. if (ret != MP_VAL)
  28349. return -13030;
  28350. mp_set(d, 1);
  28351. ret = mp_div(a, d, r, rem);
  28352. if (ret != MP_OKAY)
  28353. return -13031;
  28354. if (!mp_iszero(r))
  28355. return -13032;
  28356. if (!mp_iszero(rem))
  28357. return -13033;
  28358. mp_set(a, 1);
  28359. ret = mp_div(a, d, r, rem);
  28360. if (ret != MP_OKAY)
  28361. return -13034;
  28362. if (!mp_isone(r))
  28363. return -13035;
  28364. if (!mp_iszero(rem))
  28365. return -13036;
  28366. for (i = 0; i < 100; i++) {
  28367. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  28368. ret = randNum(d, j, rng, NULL);
  28369. if (ret != MP_OKAY)
  28370. return -13037;
  28371. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  28372. ret = randNum(a, k, rng, NULL);
  28373. if (ret != MP_OKAY)
  28374. return -13038;
  28375. ret = mp_div(a, d, NULL, rem);
  28376. if (ret != MP_OKAY)
  28377. return -13039;
  28378. ret = mp_div(a, d, r, NULL);
  28379. if (ret != MP_OKAY)
  28380. return -13040;
  28381. ret = mp_div(a, d, r, rem);
  28382. if (ret != MP_OKAY)
  28383. return -13041;
  28384. mp_mul(r, d, r);
  28385. mp_add(r, rem, r);
  28386. if (mp_cmp(r, a) != MP_EQ)
  28387. return -13042;
  28388. }
  28389. }
  28390. }
  28391. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  28392. if (ret != MP_OKAY)
  28393. return -13043;
  28394. mp_add(d, d, a);
  28395. mp_set(rem, 1);
  28396. mp_div(a, d, NULL, rem);
  28397. if (ret != MP_OKAY)
  28398. return -13044;
  28399. if (!mp_iszero(rem))
  28400. return -13045;
  28401. mp_set(r, 1);
  28402. mp_div(a, d, r, NULL);
  28403. if (ret != MP_OKAY)
  28404. return -13046;
  28405. if (mp_cmp_d(r, 2) != MP_EQ)
  28406. return -13047;
  28407. mp_set(r, 1);
  28408. mp_set(rem, 1);
  28409. mp_div(a, d, r, rem);
  28410. if (ret != MP_OKAY)
  28411. return -13048;
  28412. if (mp_cmp_d(r, 2) != MP_EQ)
  28413. return -13049;
  28414. if (!mp_iszero(rem))
  28415. return -13050;
  28416. mp_set(a, 0xfe);
  28417. mp_lshd(a, 3);
  28418. mp_add_d(a, 0xff, a);
  28419. mp_set(d, 0xfe);
  28420. mp_lshd(d, 2);
  28421. ret = mp_div(a, d, r, rem);
  28422. if (ret != MP_OKAY)
  28423. return -13051;
  28424. mp_mul(r, d, d);
  28425. mp_add(rem, d, d);
  28426. if (mp_cmp(a, d) != MP_EQ)
  28427. return -13052;
  28428. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  28429. * a digit. So mp_div must detect and handle.
  28430. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  28431. */
  28432. mp_set(a, 1);
  28433. mp_mul_2d(a, DIGIT_BIT * 3 - 1, a);
  28434. mp_set(d, 1);
  28435. mp_mul_2d(d, DIGIT_BIT * 2 - 1, d);
  28436. mp_add_d(d, 1, d);
  28437. ret = mp_div(a, d, r, rem);
  28438. if (ret != MP_OKAY)
  28439. return -13053;
  28440. return 0;
  28441. }
  28442. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  28443. !defined(WC_NO_RNG)
  28444. static int mp_test_prime(mp_int* a, WC_RNG* rng)
  28445. {
  28446. int ret;
  28447. int res;
  28448. ret = mp_rand_prime(a, 1, rng, NULL);
  28449. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  28450. if (ret != 0)
  28451. #else
  28452. if (ret != MP_VAL)
  28453. #endif
  28454. return -13060;
  28455. ret = mp_rand_prime(a, -5, rng, NULL);
  28456. if (ret != 0)
  28457. return -13061;
  28458. ret = mp_prime_is_prime(a, 1, &res);
  28459. if (ret != MP_OKAY)
  28460. return -13062;
  28461. if (res != MP_YES)
  28462. return -13063;
  28463. ret = mp_prime_is_prime(a, 0, &res);
  28464. if (ret != MP_VAL)
  28465. return -13064;
  28466. ret = mp_prime_is_prime(a, -1, &res);
  28467. if (ret != MP_VAL)
  28468. return -13065;
  28469. ret = mp_prime_is_prime(a, 257, &res);
  28470. if (ret != MP_VAL)
  28471. return -13066;
  28472. mp_set(a, 1);
  28473. ret = mp_prime_is_prime(a, 1, &res);
  28474. if (ret != MP_OKAY)
  28475. return -13067;
  28476. if (res != MP_NO)
  28477. return -13068;
  28478. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  28479. if (ret != MP_OKAY)
  28480. return -13069;
  28481. if (res != MP_NO)
  28482. return -13070;
  28483. mp_set(a, 2);
  28484. ret = mp_prime_is_prime(a, 1, &res);
  28485. if (ret != MP_OKAY)
  28486. return -13071;
  28487. if (res != MP_YES)
  28488. return -13072;
  28489. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  28490. if (ret != MP_OKAY)
  28491. return -13073;
  28492. if (res != MP_YES)
  28493. return -13074;
  28494. mp_set(a, 0xfb);
  28495. ret = mp_prime_is_prime(a, 1, &res);
  28496. if (ret != MP_OKAY)
  28497. return -13075;
  28498. if (res != MP_YES)
  28499. return -13076;
  28500. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  28501. if (ret != MP_OKAY)
  28502. return -13077;
  28503. if (res != MP_YES)
  28504. return -13078;
  28505. mp_set(a, 0x6);
  28506. ret = mp_prime_is_prime(a, 1, &res);
  28507. if (ret != MP_OKAY)
  28508. return -13079;
  28509. if (res != MP_NO)
  28510. return -13080;
  28511. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  28512. if (ret != MP_OKAY)
  28513. return -13081;
  28514. if (res != MP_NO)
  28515. return -13082;
  28516. mp_set_int(a, 0x655 * 0x65b);
  28517. ret = mp_prime_is_prime(a, 10, &res);
  28518. if (ret != MP_OKAY)
  28519. return -13083;
  28520. if (res != MP_NO)
  28521. return -13084;
  28522. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  28523. if (ret != MP_OKAY)
  28524. return -13085;
  28525. if (res != MP_NO)
  28526. return -13086;
  28527. return 0;
  28528. }
  28529. #endif
  28530. #if defined(WOLFSSL_SP_MATH_ALL) || (defined(WOLFSSL_SP_MATH) && \
  28531. defined(WOLFSSL_HAVE_SP_DH) || (defined(HAVE_ECC) && defined(FP_ECC)))
  28532. static int mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  28533. WC_RNG* rng)
  28534. {
  28535. int ret;
  28536. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  28537. int i;
  28538. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  28539. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  28540. { 12, 56, 168 }, { 56, 12, 168 }
  28541. };
  28542. #endif
  28543. (void)exp;
  28544. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  28545. mp_set(a, 0);
  28546. mp_set(b, 1);
  28547. ret = mp_lcm(a, a, r);
  28548. if (ret != MP_VAL)
  28549. return -13090;
  28550. ret = mp_lcm(a, b, r);
  28551. if (ret != MP_VAL)
  28552. return -13091;
  28553. ret = mp_lcm(b, a, r);
  28554. if (ret != MP_VAL)
  28555. return -13092;
  28556. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  28557. mp_set(a, kat[i][0]);
  28558. mp_set(b, kat[i][1]);
  28559. ret = mp_lcm(a, b, r);
  28560. if (ret != MP_OKAY)
  28561. return -13093;
  28562. mp_set(exp, kat[i][2]);
  28563. if (mp_cmp(r, exp) != MP_EQ)
  28564. return -13094;
  28565. }
  28566. #endif
  28567. (void)rng;
  28568. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  28569. !defined(WC_NO_RNG)
  28570. if (mp_rand_prime(a, 20, rng, NULL) != MP_OKAY)
  28571. return -13095;
  28572. if (mp_rand_prime(b, 20, rng, NULL) != MP_OKAY)
  28573. return -13096;
  28574. if (mp_mul(a, b, exp) != MP_OKAY)
  28575. return -13097;
  28576. ret = mp_lcm(a, b, r);
  28577. if (ret != MP_OKAY)
  28578. return -13098;
  28579. if (mp_cmp(r, exp) != MP_EQ)
  28580. return -13099;
  28581. ret = mp_lcm(b, a, r);
  28582. if (ret != MP_OKAY)
  28583. return -13100;
  28584. if (mp_cmp(r, exp) != MP_EQ)
  28585. return -13101;
  28586. #endif
  28587. mp_set(a, 11);
  28588. mp_zero(b);
  28589. ret = mp_gcd(a, b, r);
  28590. if (ret != MP_OKAY)
  28591. return -13102;
  28592. if (mp_cmp_d(r, 11) != MP_EQ)
  28593. return -13103;
  28594. ret = mp_gcd(b, a, r);
  28595. if (ret != MP_OKAY)
  28596. return -13104;
  28597. if (mp_cmp_d(r, 11) != MP_EQ)
  28598. return -13105;
  28599. ret = mp_gcd(b, b, r);
  28600. if (ret != MP_VAL)
  28601. return -13106;
  28602. return 0;
  28603. }
  28604. #endif
  28605. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  28606. defined(WOLFSSL_SP_MATH_ALL)
  28607. static int mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  28608. {
  28609. int ret;
  28610. int i;
  28611. int j;
  28612. mp_set(a, 10);
  28613. ret = mp_mod_2d(a, 0, r);
  28614. if (ret != MP_OKAY)
  28615. return -13110;
  28616. if (!mp_iszero(r))
  28617. return -13111;
  28618. ret = mp_mod_2d(a, 1, r);
  28619. if (ret != MP_OKAY)
  28620. return -13112;
  28621. if (!mp_iszero(r))
  28622. return -13113;
  28623. ret = mp_mod_2d(a, 2, r);
  28624. if (ret != MP_OKAY)
  28625. return -13114;
  28626. if (mp_cmp_d(r, 2))
  28627. return -13115;
  28628. for (i = 2; i < 20; i++) {
  28629. ret = randNum(a, i, rng, NULL);
  28630. if (ret != 0)
  28631. return -13116;
  28632. for (j = 1; j <= mp_count_bits(a); j++) {
  28633. /* Get top part */
  28634. ret = mp_div_2d(a, j, t, NULL);
  28635. if (ret != 0)
  28636. return -13117;
  28637. ret = mp_mul_2d(t, j, t);
  28638. if (ret != 0)
  28639. return -13118;
  28640. /* Get bottom part */
  28641. ret = mp_mod_2d(a, j, r);
  28642. if (ret != 0)
  28643. return -13119;
  28644. /* Reassemble */
  28645. ret = mp_add(t, r, r);
  28646. if (ret != 0)
  28647. return -13120;
  28648. if (mp_cmp(a, r) != MP_EQ)
  28649. return -13121;
  28650. }
  28651. }
  28652. return 0;
  28653. }
  28654. #endif
  28655. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  28656. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  28657. static int mp_test_mod_d(mp_int* a)
  28658. {
  28659. int ret;
  28660. mp_digit r;
  28661. if (mp_set(a, 1) != MP_OKAY)
  28662. return -13130;
  28663. ret = mp_mod_d(a, 0, &r);
  28664. if (ret != MP_VAL)
  28665. return -13131;
  28666. mp_zero(a);
  28667. ret = mp_mod_d(a, 1, &r);
  28668. if (ret != MP_OKAY)
  28669. return -13132;
  28670. ret = mp_mod_d(a, 3, &r);
  28671. if (ret != MP_OKAY)
  28672. return -13133;
  28673. ret = mp_mod_d(a, 5, &r);
  28674. if (ret != MP_OKAY)
  28675. return -13134;
  28676. return 0;
  28677. }
  28678. #endif
  28679. static int mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  28680. WC_RNG* rng)
  28681. {
  28682. int ret;
  28683. int i;
  28684. for (i = 1; i < 16; i++) {
  28685. ret = randNum(a, i, rng, NULL);
  28686. if (ret != 0)
  28687. return -13140;
  28688. ret = mp_mul(a, a, r1);
  28689. if (ret != 0)
  28690. return -13141;
  28691. ret = mp_sqr(a, r2);
  28692. if (ret != 0)
  28693. return -13142;
  28694. if (mp_cmp(r1, r2) != MP_EQ)
  28695. return -13143;
  28696. }
  28697. ret = mp_set(b, 0);
  28698. if (ret != MP_OKAY)
  28699. return -13144;
  28700. ret = mp_mul(a, b, r1);
  28701. if (ret != MP_OKAY)
  28702. return -13145;
  28703. if (!mp_iszero(r1))
  28704. return -13146;
  28705. ret = mp_sqr(b, r1);
  28706. if (ret != MP_OKAY)
  28707. return -13147;
  28708. if (!mp_iszero(r1))
  28709. return -13148;
  28710. #ifdef WOLFSSL_SP_MATH
  28711. ret = mp_set(a, 1);
  28712. if (ret != MP_OKAY)
  28713. return -13149;
  28714. i = (SP_INT_DIGITS + 1) / 2;
  28715. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  28716. if (ret != MP_OKAY)
  28717. return -13150;
  28718. ret = mp_set(b, 1);
  28719. if (ret != MP_OKAY)
  28720. return -13151;
  28721. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  28722. if (ret != MP_OKAY)
  28723. return -13152;
  28724. ret = mp_mul(a, b, r1);
  28725. if (ret != MP_OKAY)
  28726. return -13153;
  28727. ret = mp_mul(a, a, r1);
  28728. if (ret == MP_OKAY)
  28729. return -13154;
  28730. ret = mp_sqr(a, r1);
  28731. if (ret == MP_OKAY)
  28732. return -13155;
  28733. ret = mp_sqr(b, r1);
  28734. if (ret != MP_OKAY)
  28735. return -13156;
  28736. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  28737. (defined(HAVE_ECC) && defined(FP_ECC))
  28738. ret = mp_mulmod(a, b, b, r1);
  28739. if (ret != MP_OKAY)
  28740. return -13157;
  28741. ret = mp_mulmod(a, a, b, r1);
  28742. if (ret == MP_OKAY)
  28743. return -13158;
  28744. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  28745. ret = mp_sqrmod(a, b, r1);
  28746. if (ret == MP_OKAY)
  28747. return -13159;
  28748. ret = mp_sqrmod(b, a, r1);
  28749. if (ret != MP_OKAY)
  28750. return -13160;
  28751. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  28752. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  28753. #endif /* WOLFSSL_SP_MATH */
  28754. return 0;
  28755. }
  28756. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  28757. defined(OPENSSL_EXTRA)
  28758. static int mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  28759. {
  28760. int ret;
  28761. mp_set(a, 0);
  28762. mp_set(m, 1);
  28763. ret = mp_invmod(a, m, r);
  28764. if (ret != MP_VAL)
  28765. return -13170;
  28766. ret = mp_invmod(m, a, r);
  28767. if (ret != MP_VAL)
  28768. return -13171;
  28769. mp_set(a, 2);
  28770. mp_set(m, 4);
  28771. ret = mp_invmod(a, m, r);
  28772. if (ret != MP_VAL)
  28773. return -13172;
  28774. mp_set(a, 1);
  28775. mp_set(m, 4);
  28776. ret = mp_invmod(a, m, r);
  28777. if (ret != MP_OKAY)
  28778. return -13173;
  28779. if (!mp_isone(r))
  28780. return -13174;
  28781. mp_set(a, 3);
  28782. mp_set(m, 4);
  28783. ret = mp_invmod(a, m, r);
  28784. if (ret != MP_OKAY)
  28785. return -13175;
  28786. mp_set(a, 3);
  28787. mp_set(m, 5);
  28788. ret = mp_invmod(a, m, r);
  28789. if (ret != MP_OKAY)
  28790. return -13176;
  28791. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  28792. mp_read_radix(a, "-3", 16);
  28793. ret = mp_invmod(a, m, r);
  28794. if (ret != MP_OKAY)
  28795. return -13177;
  28796. #endif
  28797. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  28798. #ifdef HAVE_ECC
  28799. mp_set(a, 0);
  28800. mp_set(m, 3);
  28801. ret = mp_invmod_mont_ct(a, m, r, 1);
  28802. if (ret != MP_VAL)
  28803. return -13178;
  28804. mp_set(a, 1);
  28805. mp_set(m, 0);
  28806. ret = mp_invmod_mont_ct(a, m, r, 1);
  28807. if (ret != MP_VAL)
  28808. return -13179;
  28809. mp_set(a, 1);
  28810. mp_set(m, 1);
  28811. ret = mp_invmod_mont_ct(a, m, r, 1);
  28812. if (ret != MP_VAL)
  28813. return -13180;
  28814. mp_set(a, 1);
  28815. mp_set(m, 2);
  28816. ret = mp_invmod_mont_ct(a, m, r, 1);
  28817. if (ret != MP_VAL)
  28818. return -13181;
  28819. mp_set(a, 1);
  28820. mp_set(m, 3);
  28821. ret = mp_invmod_mont_ct(a, m, r, 1);
  28822. if (ret != MP_OKAY)
  28823. return -13182;
  28824. #endif
  28825. #endif
  28826. return 0;
  28827. }
  28828. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  28829. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  28830. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  28831. static int mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  28832. {
  28833. int ret;
  28834. mp_set(b, 0x2);
  28835. mp_set(e, 0x3);
  28836. mp_set(m, 0x0);
  28837. ret = mp_exptmod_ex(b, e, 1, m, r);
  28838. if (ret != MP_VAL)
  28839. return -13190;
  28840. ret = mp_exptmod_nct(b, e, m, r);
  28841. if (ret != MP_VAL)
  28842. return -13191;
  28843. mp_set(b, 0x2);
  28844. mp_set(e, 0x3);
  28845. mp_set(m, 0x1);
  28846. ret = mp_exptmod_ex(b, e, 1, m, r);
  28847. if (ret != MP_OKAY)
  28848. return -13192;
  28849. if (!mp_iszero(r))
  28850. return -13193;
  28851. ret = mp_exptmod_nct(b, e, m, r);
  28852. if (ret != MP_OKAY)
  28853. return -13194;
  28854. if (!mp_iszero(r))
  28855. return -13195;
  28856. mp_set(b, 0x2);
  28857. mp_set(e, 0x0);
  28858. mp_set(m, 0x7);
  28859. ret = mp_exptmod_ex(b, e, 1, m, r);
  28860. if (ret != MP_OKAY)
  28861. return -13196;
  28862. if (!mp_isone(r))
  28863. return -13197;
  28864. ret = mp_exptmod_nct(b, e, m, r);
  28865. if (ret != MP_OKAY)
  28866. return -13198;
  28867. if (!mp_isone(r))
  28868. return -13199;
  28869. mp_set(b, 0x0);
  28870. mp_set(e, 0x3);
  28871. mp_set(m, 0x7);
  28872. ret = mp_exptmod_ex(b, e, 1, m, r);
  28873. if (ret != MP_OKAY)
  28874. return -13200;
  28875. if (!mp_iszero(r))
  28876. return -13201;
  28877. ret = mp_exptmod_nct(b, e, m, r);
  28878. if (ret != MP_OKAY)
  28879. return -13202;
  28880. if (!mp_iszero(r))
  28881. return -13203;
  28882. mp_set(b, 0x10);
  28883. mp_set(e, 0x3);
  28884. mp_set(m, 0x7);
  28885. ret = mp_exptmod_ex(b, e, 1, m, r);
  28886. if (ret != MP_OKAY)
  28887. return -13204;
  28888. ret = mp_exptmod_nct(b, e, m, r);
  28889. if (ret != MP_OKAY)
  28890. return -13205;
  28891. mp_set(b, 0x7);
  28892. mp_set(e, 0x3);
  28893. mp_set(m, 0x7);
  28894. ret = mp_exptmod_ex(b, e, 1, m, r);
  28895. if (ret != MP_OKAY)
  28896. return -13206;
  28897. if (!mp_iszero(r))
  28898. return -13207;
  28899. ret = mp_exptmod_nct(b, e, m, r);
  28900. if (ret != MP_OKAY)
  28901. return -13208;
  28902. if (!mp_iszero(r))
  28903. return -13209;
  28904. mp_set(b, 0x01);
  28905. mp_mul_2d(b, DIGIT_BIT, b);
  28906. mp_add_d(b, 1, b);
  28907. mp_set(e, 0x3);
  28908. mp_copy(b, m);
  28909. ret = mp_exptmod_ex(b, e, 1, m, r);
  28910. if (ret != MP_OKAY)
  28911. return -13210;
  28912. if (!mp_iszero(r))
  28913. return -13211;
  28914. ret = mp_exptmod_nct(b, e, m, r);
  28915. if (ret != MP_OKAY)
  28916. return -13212;
  28917. if (!mp_iszero(r))
  28918. return -13213;
  28919. mp_set(b, 0x2);
  28920. mp_set(e, 0x3);
  28921. mp_set(m, 0x7);
  28922. ret = mp_exptmod_ex(b, e, 1, m, r);
  28923. if (ret != MP_OKAY)
  28924. return -13214;
  28925. ret = mp_exptmod_nct(b, e, m, r);
  28926. if (ret != MP_OKAY)
  28927. return -13215;
  28928. #ifdef WOLFSSL_SP_MATH
  28929. mp_set(b, 0x2);
  28930. mp_set(e, 0x3);
  28931. mp_set(m, 0x01);
  28932. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  28933. mp_add_d(m, 0x01, m);
  28934. ret = mp_exptmod_ex(b, e, 1, m, r);
  28935. if (ret != MP_VAL)
  28936. return -13216;
  28937. ret = mp_exptmod_nct(b, e, m, r);
  28938. if (ret != MP_VAL)
  28939. return -13217;
  28940. #endif
  28941. return 0;
  28942. }
  28943. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  28944. * OPENSSL_EXTRA */
  28945. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  28946. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  28947. static int mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  28948. {
  28949. int ret;
  28950. mp_digit mp;
  28951. static int exp[] = { 7, 8, 16, 27, 32, 64,
  28952. 127, 128, 255, 256,
  28953. 383, 384, 2033, 2048
  28954. };
  28955. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  28956. 0x01, 0x9f, 0x13, 0xbd,
  28957. 0x1f, 0x13d, 0x45, 0x615
  28958. };
  28959. int i;
  28960. int j;
  28961. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  28962. if (exp[i] >= DIGIT_BIT)
  28963. continue;
  28964. mp_zero(m);
  28965. ret = mp_set_bit(m, exp[i]);
  28966. if (ret != MP_OKAY)
  28967. return -13220;
  28968. ret = mp_sub_d(m, sub[i], m);
  28969. if (ret != MP_OKAY)
  28970. return -13221;
  28971. ret = mp_montgomery_setup(m, &mp);
  28972. if (ret != MP_OKAY)
  28973. return -13222;
  28974. ret = mp_montgomery_calc_normalization(n, m);
  28975. if (ret != MP_OKAY)
  28976. return -13223;
  28977. for (j = 0; j < 10; j++) {
  28978. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  28979. if (ret != 0)
  28980. return -13224;
  28981. ret = mp_mod(a, m, a);
  28982. if (ret != 0)
  28983. return -13225;
  28984. /* r = a * a */
  28985. ret = mp_sqrmod(a, m, r);
  28986. if (ret != MP_OKAY)
  28987. return -13226;
  28988. /* Convert to Montgomery form = a*n */
  28989. ret = mp_mulmod(a, n, m, a);
  28990. if (ret != MP_OKAY)
  28991. return -13227;
  28992. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  28993. ret = mp_sqr(a, a);
  28994. if (ret != MP_OKAY)
  28995. return -13228;
  28996. ret = mp_montgomery_reduce(a, m, mp);
  28997. if (ret != MP_OKAY)
  28998. return -13229;
  28999. ret = mp_montgomery_reduce(a, m, mp);
  29000. if (ret != MP_OKAY)
  29001. return -13230;
  29002. if (mp_cmp(a, r) != MP_EQ)
  29003. return -13231;
  29004. }
  29005. }
  29006. return 0;
  29007. }
  29008. #endif
  29009. WOLFSSL_TEST_SUBROUTINE int mp_test(void)
  29010. {
  29011. WC_RNG rng;
  29012. int ret;
  29013. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  29014. int i, j, k;
  29015. mp_digit d;
  29016. #endif
  29017. mp_int a, b, r1, r2, p;
  29018. ret = mp_init_multi(&a, &b, &r1, &r2, NULL, NULL);
  29019. if (ret != 0)
  29020. return -13300;
  29021. #ifdef WOLSSL_SP_MATH_ALL
  29022. mp_init_copy(&p, &a);
  29023. #else
  29024. ret = mp_init(&p);
  29025. if (ret != 0)
  29026. return -13301;
  29027. #endif
  29028. #ifndef HAVE_FIPS
  29029. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  29030. #else
  29031. ret = wc_InitRng(&rng);
  29032. #endif
  29033. if (ret != 0)
  29034. goto done;
  29035. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  29036. mp_set_int(&a, 0);
  29037. if (a.used != 0 || a.dp[0] != 0)
  29038. return -13302;
  29039. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  29040. for (i = 0; i < 4 * j; i++) {
  29041. /* New values to use. */
  29042. ret = randNum(&p, j, &rng, NULL);
  29043. if (ret != 0)
  29044. return -13303;
  29045. ret = randNum(&a, j, &rng, NULL);
  29046. if (ret != 0)
  29047. return -13304;
  29048. ret = randNum(&b, j, &rng, NULL);
  29049. if (ret != 0)
  29050. return -13305;
  29051. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  29052. if (ret != 0)
  29053. return -13306;
  29054. d &= MP_MASK;
  29055. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  29056. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  29057. /* Ensure sqrmod produce same result as mulmod. */
  29058. ret = mp_sqrmod(&a, &p, &r1);
  29059. if (ret != 0)
  29060. return -13307;
  29061. ret = mp_mulmod(&a, &a, &p, &r2);
  29062. if (ret != 0)
  29063. return -13308;
  29064. if (mp_cmp(&r1, &r2) != 0)
  29065. return -13309;
  29066. #endif
  29067. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  29068. !defined(WOLFSSL_SP_INT_NEGATIVE))
  29069. ret = mp_addmod(&a, &b, &p, &r1);
  29070. if (ret != 0)
  29071. return -13310;
  29072. ret = mp_submod(&r1, &b, &p, &r2);
  29073. if (ret != 0)
  29074. return -13311;
  29075. ret = mp_mod(&a, &p, &r1);
  29076. if (ret != 0)
  29077. return -13312;
  29078. if (mp_cmp(&r1, &r2) != MP_EQ)
  29079. return -13313;
  29080. #else
  29081. /* Ensure add with mod produce same result as sub with mod. */
  29082. ret = mp_addmod(&a, &b, &p, &r1);
  29083. if (ret != 0)
  29084. return -13314;
  29085. b.sign ^= 1;
  29086. ret = mp_submod(&a, &b, &p, &r2);
  29087. if (ret != 0)
  29088. return -13315;
  29089. if (mp_cmp(&r1, &r2) != 0)
  29090. return -13316;
  29091. #endif
  29092. /* Ensure add digit produce same result as sub digit. */
  29093. ret = mp_add_d(&a, d, &r1);
  29094. if (ret != 0)
  29095. return -13317;
  29096. ret = mp_sub_d(&r1, d, &r2);
  29097. if (ret != 0)
  29098. return -13318;
  29099. if (mp_cmp(&a, &r2) != 0)
  29100. return -13319;
  29101. /* Invert - if p is even it will use the slow impl.
  29102. * - if p and a are even it will fail.
  29103. */
  29104. ret = mp_invmod(&a, &p, &r1);
  29105. if (ret != 0 && ret != MP_VAL)
  29106. return -13320;
  29107. ret = 0;
  29108. /* Shift up and down number all bits in a digit. */
  29109. for (k = 0; k < DIGIT_BIT; k++) {
  29110. mp_mul_2d(&a, k, &r1);
  29111. mp_div_2d(&r1, k, &r2, &p);
  29112. if (mp_cmp(&a, &r2) != 0)
  29113. return -13321;
  29114. if (!mp_iszero(&p))
  29115. return -13322;
  29116. mp_rshb(&r1, k);
  29117. if (mp_cmp(&a, &r1) != 0)
  29118. return -13323;
  29119. }
  29120. }
  29121. }
  29122. #if DIGIT_BIT >= 32
  29123. /* Check that setting a 32-bit digit works. */
  29124. d &= 0xffffffffU;
  29125. mp_set_int(&a, d);
  29126. if (a.used != 1 || a.dp[0] != d)
  29127. return -13324;
  29128. #endif
  29129. /* Check setting a bit and testing a bit works. */
  29130. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  29131. mp_zero(&a);
  29132. mp_set_bit(&a, i);
  29133. if (!mp_is_bit_set(&a, i))
  29134. return -13325;
  29135. }
  29136. #endif
  29137. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  29138. mp_zero(&a);
  29139. i = mp_cnt_lsb(&a);
  29140. if (i != 0)
  29141. return -13326;
  29142. mp_set(&a, 1);
  29143. i = mp_cnt_lsb(&a);
  29144. if (i != 0)
  29145. return -13327;
  29146. #endif
  29147. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  29148. if ((ret = mp_test_param(&a, &b, &r1, &rng)) != 0)
  29149. return ret;
  29150. #endif
  29151. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  29152. if ((ret = mp_test_div_3(&a, &r1, &rng)) != 0)
  29153. return ret;
  29154. #endif
  29155. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  29156. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  29157. if ((ret = mp_test_radix_10(&a, &r1, &rng)) != 0)
  29158. return ret;
  29159. #endif
  29160. #if defined(WOLFSSL_SP_MATH_ALL) || defined(HAVE_ECC)
  29161. if ((ret = mp_test_radix_16(&a, &r1, &rng)) != 0)
  29162. return ret;
  29163. #endif
  29164. if ((ret = mp_test_shift(&a, &r1, &rng)) != 0)
  29165. return ret;
  29166. if ((ret = mp_test_add_sub_d(&a, &r1)) != 0)
  29167. return ret;
  29168. if ((ret = mp_test_read_to_bin(&a)) != 0)
  29169. return ret;
  29170. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  29171. if ((ret = mp_test_set_int(&a)) != 0)
  29172. return ret;
  29173. #endif
  29174. if ((ret = mp_test_cmp(&a, &r1)) != 0)
  29175. return ret;
  29176. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  29177. if ((ret = mp_test_shbd(&a, &b, &rng)) != 0)
  29178. return ret;
  29179. #endif
  29180. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  29181. if ((ret = mp_test_set_is_bit(&a)) != 0)
  29182. return ret;
  29183. #endif
  29184. if ((ret = mp_test_div(&a, &b, &r1, &r2, &rng)) != 0)
  29185. return ret;
  29186. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  29187. !defined(WC_NO_RNG)
  29188. if ((ret = mp_test_prime(&a, &rng)) != 0)
  29189. return ret;
  29190. #endif
  29191. #if defined(WOLFSSL_SP_MATH_ALL) || (defined(WOLFSSL_SP_MATH) && \
  29192. defined(WOLFSSL_HAVE_SP_DH) || (defined(HAVE_ECC) && defined(FP_ECC)))
  29193. if ((ret = mp_test_lcm_gcd(&a, &b, &r1, &r2, &rng)) != 0)
  29194. return ret;
  29195. #endif
  29196. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  29197. defined(WOLFSSL_SP_MATH_ALL)
  29198. if ((ret = mp_test_mod_2d(&a, &r1, &p, &rng)) != 0)
  29199. return ret;
  29200. #endif
  29201. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  29202. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  29203. if ((ret = mp_test_mod_d(&a)) != 0)
  29204. return ret;
  29205. #endif
  29206. if ((ret = mp_test_mul_sqr(&a, &b, &r1, &r2, &rng)) != 0)
  29207. return ret;
  29208. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  29209. defined(OPENSSL_EXTRA)
  29210. if ((ret = mp_test_invmod(&a, &b, &r1)) != 0)
  29211. return ret;
  29212. #endif
  29213. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  29214. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  29215. if ((ret = mp_test_exptmod(&a, &b, &r1, &r2)) != 0)
  29216. return ret;
  29217. #endif
  29218. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  29219. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  29220. if ((ret = mp_test_mont(&a, &b, &r1, &r2, &rng)) != 0)
  29221. return ret;
  29222. #endif
  29223. done:
  29224. mp_clear(&p);
  29225. mp_clear(&r2);
  29226. mp_clear(&r1);
  29227. mp_clear(&b);
  29228. mp_clear(&a);
  29229. wc_FreeRng(&rng);
  29230. return ret;
  29231. }
  29232. #endif
  29233. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  29234. typedef struct pairs_t {
  29235. const unsigned char* coeff;
  29236. int coeffSz;
  29237. int exp;
  29238. } pairs_t;
  29239. /*
  29240. n =p1p2p3, where pi = ki(p1−1)+1 with (k2,k3) = (173,293)
  29241. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  29242. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  29243. */
  29244. static const unsigned char c192a[] =
  29245. {
  29246. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  29247. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  29248. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  29249. };
  29250. static const unsigned char c0a[] =
  29251. {
  29252. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  29253. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  29254. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  29255. };
  29256. static const pairs_t ecPairsA[] =
  29257. {
  29258. {c192a, sizeof(c192a), 192},
  29259. {c0a, sizeof(c0a), 0}
  29260. };
  29261. static const int kA[] = {173, 293};
  29262. static const unsigned char controlPrime[] = {
  29263. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  29264. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  29265. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  29266. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  29267. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  29268. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  29269. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  29270. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  29271. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  29272. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  29273. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  29274. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  29275. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  29276. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  29277. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  29278. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  29279. };
  29280. static const unsigned char testOne[] = { 1 };
  29281. static int GenerateNextP(mp_int* p1, mp_int* p2, int k)
  29282. {
  29283. int ret;
  29284. mp_int ki;
  29285. ret = mp_init(&ki);
  29286. if (ret == 0)
  29287. ret = mp_set(&ki, k);
  29288. if (ret == 0)
  29289. ret = mp_sub_d(p1, 1, p2);
  29290. if (ret == 0)
  29291. ret = mp_mul(p2, &ki, p2);
  29292. if (ret == 0)
  29293. ret = mp_add_d(p2, 1, p2);
  29294. mp_clear(&ki);
  29295. return ret;
  29296. }
  29297. static int GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  29298. const pairs_t* ecPairs, int ecPairsSz,
  29299. const int* k)
  29300. {
  29301. mp_int x,y;
  29302. int ret, i;
  29303. ret = mp_init(&x);
  29304. if (ret == 0) {
  29305. ret = mp_init(&y);
  29306. if (ret != 0) {
  29307. mp_clear(&x);
  29308. return MP_MEM;
  29309. }
  29310. }
  29311. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  29312. ret = mp_read_unsigned_bin(&x, ecPairs[i].coeff, ecPairs[i].coeffSz);
  29313. /* p1 = 2^exp */
  29314. if (ret == 0)
  29315. ret = mp_2expt(&y, ecPairs[i].exp);
  29316. /* p1 = p1 * m */
  29317. if (ret == 0)
  29318. ret = mp_mul(&x, &y, &x);
  29319. /* p1 += */
  29320. if (ret == 0)
  29321. ret = mp_add(p1, &x, p1);
  29322. mp_zero(&x);
  29323. mp_zero(&y);
  29324. }
  29325. mp_clear(&x);
  29326. mp_clear(&y);
  29327. if (ret == 0)
  29328. ret = GenerateNextP(p1, p2, k[0]);
  29329. if (ret == 0)
  29330. ret = GenerateNextP(p1, p3, k[1]);
  29331. return ret;
  29332. }
  29333. WOLFSSL_TEST_SUBROUTINE int prime_test(void)
  29334. {
  29335. #ifdef WOLFSSL_SMALL_STACK
  29336. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  29337. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  29338. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  29339. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29340. #else
  29341. mp_int n[1],
  29342. p1[1],
  29343. p2[1],
  29344. p3[1];
  29345. #endif
  29346. int ret, isPrime = 0;
  29347. WC_RNG rng;
  29348. #ifdef WOLFSSL_SMALL_STACK
  29349. if ((n == NULL) ||
  29350. (p1 == NULL) ||
  29351. (p2 == NULL) ||
  29352. (p3 == NULL))
  29353. ERROR_OUT(MEMORY_E, out);
  29354. #endif
  29355. ret = wc_InitRng(&rng);
  29356. if (ret == 0)
  29357. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  29358. if (ret == 0)
  29359. ret = GenerateP(p1, p2, p3,
  29360. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  29361. if (ret == 0)
  29362. ret = mp_mul(p1, p2, n);
  29363. if (ret == 0)
  29364. ret = mp_mul(n, p3, n);
  29365. if (ret != 0)
  29366. ERROR_OUT(-13400, out);
  29367. /* Check the old prime test using the number that false positives.
  29368. * This test result should indicate as not prime. */
  29369. ret = mp_prime_is_prime(n, 40, &isPrime);
  29370. if (ret != 0)
  29371. ERROR_OUT(-13401, out);
  29372. if (isPrime)
  29373. ERROR_OUT(-13402, out);
  29374. /* This test result should fail. It should indicate the value as prime. */
  29375. ret = mp_prime_is_prime(n, 8, &isPrime);
  29376. if (ret != 0)
  29377. ERROR_OUT(-13403, out);
  29378. if (!isPrime)
  29379. ERROR_OUT(-13404, out);
  29380. /* This test result should indicate the value as not prime. */
  29381. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  29382. if (ret != 0)
  29383. ERROR_OUT(-13405, out);
  29384. if (isPrime)
  29385. ERROR_OUT(-13406, out);
  29386. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  29387. if (ret != 0)
  29388. ERROR_OUT(-13407, out);
  29389. /* This test result should indicate the value as prime. */
  29390. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  29391. if (ret != 0)
  29392. ERROR_OUT(-13408, out);
  29393. if (!isPrime)
  29394. ERROR_OUT(-13409, out);
  29395. /* This test result should indicate the value as prime. */
  29396. isPrime = -1;
  29397. ret = mp_prime_is_prime(n, 8, &isPrime);
  29398. if (ret != 0)
  29399. ERROR_OUT(-13410, out);
  29400. if (!isPrime)
  29401. ERROR_OUT(-13411, out);
  29402. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  29403. if (ret != 0)
  29404. ERROR_OUT(-13412, out);
  29405. /* This test result should indicate the value as not prime. */
  29406. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  29407. if (ret != 0)
  29408. ERROR_OUT(-13413, out);
  29409. if (isPrime)
  29410. ERROR_OUT(-13414, out);
  29411. ret = mp_prime_is_prime(n, 8, &isPrime);
  29412. if (ret != 0)
  29413. ERROR_OUT(-13415, out);
  29414. if (isPrime)
  29415. ERROR_OUT(-13416, out);
  29416. ret = 0;
  29417. out:
  29418. #ifdef WOLFSSL_SMALL_STACK
  29419. if (n != NULL) {
  29420. mp_clear(n);
  29421. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29422. }
  29423. if (p1 != NULL) {
  29424. mp_clear(p1);
  29425. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29426. }
  29427. if (p2 != NULL) {
  29428. mp_clear(p2);
  29429. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29430. }
  29431. if (p3 != NULL) {
  29432. mp_clear(p3);
  29433. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29434. }
  29435. #else
  29436. mp_clear(p3);
  29437. mp_clear(p2);
  29438. mp_clear(p1);
  29439. mp_clear(n);
  29440. #endif
  29441. wc_FreeRng(&rng);
  29442. return ret;
  29443. }
  29444. #endif /* WOLFSSL_PUBLIC_MP */
  29445. #if defined(ASN_BER_TO_DER) && \
  29446. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  29447. defined(OPENSSL_EXTRA_X509_SMALL))
  29448. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  29449. typedef struct berDerTestData {
  29450. const byte *in;
  29451. word32 inSz;
  29452. const byte *out;
  29453. word32 outSz;
  29454. } berDerTestData;
  29455. WOLFSSL_TEST_SUBROUTINE int berder_test(void)
  29456. {
  29457. int ret;
  29458. int i;
  29459. word32 len = 0, l;
  29460. byte out[32];
  29461. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  29462. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  29463. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  29464. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  29465. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  29466. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  29467. };
  29468. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  29469. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  29470. 0x30, 0x80,
  29471. 0x02, 0x01, 0x01,
  29472. 0x30, 0x80,
  29473. 0x24, 0x80,
  29474. 0x04, 0x01, 0x01,
  29475. 0x04, 0x02, 0x02, 0x03,
  29476. 0x00, 0x00,
  29477. 0x06, 0x01, 0x01,
  29478. 0x00, 0x00,
  29479. 0x31, 0x80,
  29480. 0x06, 0x01, 0x01,
  29481. 0x00, 0x00,
  29482. 0x00, 0x00,
  29483. };
  29484. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  29485. 0x30, 0x12,
  29486. 0x02, 0x01, 0x01,
  29487. 0x30, 0x08,
  29488. 0x04, 0x03, 0x01, 0x02, 0x03,
  29489. 0x06, 0x01, 0x01,
  29490. 0x31, 0x03,
  29491. 0x06, 0x01, 0x01
  29492. };
  29493. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  29494. berDerTestData testData[] = {
  29495. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  29496. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  29497. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  29498. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  29499. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  29500. };
  29501. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  29502. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  29503. if (ret != LENGTH_ONLY_E)
  29504. return -13500 - i;
  29505. if (len != testData[i].outSz)
  29506. return -13510 - i;
  29507. len = testData[i].outSz;
  29508. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  29509. if (ret != 0)
  29510. return -13520 - i;
  29511. if (XMEMCMP(out, testData[i].out, len) != 0)
  29512. return -13530 - i;
  29513. for (l = 1; l < testData[i].inSz; l++) {
  29514. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  29515. if (ret != ASN_PARSE_E)
  29516. return -13540;
  29517. len = testData[i].outSz;
  29518. ret = wc_BerToDer(testData[i].in, l, out, &len);
  29519. if (ret != ASN_PARSE_E)
  29520. return -13541;
  29521. }
  29522. for (l = 0; l < testData[i].outSz-1; l++) {
  29523. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  29524. if (ret != BUFFER_E)
  29525. return -13542;
  29526. }
  29527. }
  29528. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  29529. if (ret != BAD_FUNC_ARG)
  29530. return -13543;
  29531. ret = wc_BerToDer(out, 4, NULL, NULL);
  29532. if (ret != BAD_FUNC_ARG)
  29533. return -13544;
  29534. ret = wc_BerToDer(NULL, 4, NULL, &len);
  29535. if (ret != BAD_FUNC_ARG)
  29536. return -13545;
  29537. ret = wc_BerToDer(NULL, 4, out, NULL);
  29538. if (ret != BAD_FUNC_ARG)
  29539. return -13546;
  29540. ret = wc_BerToDer(out, 4, out, NULL);
  29541. if (ret != BAD_FUNC_ARG)
  29542. return -13547;
  29543. ret = wc_BerToDer(NULL, 4, out, &len);
  29544. if (ret != BAD_FUNC_ARG)
  29545. return -13548;
  29546. for (l = 1; l < sizeof(good4_out); l++) {
  29547. len = l;
  29548. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  29549. if (ret != BUFFER_E)
  29550. return -13549;
  29551. }
  29552. return 0;
  29553. }
  29554. #endif
  29555. #ifdef DEBUG_WOLFSSL
  29556. static THREAD_LS_T int log_cnt = 0;
  29557. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  29558. {
  29559. (void)logLevel;
  29560. (void)logMessage;
  29561. log_cnt++;
  29562. }
  29563. #endif /* DEBUG_WOLFSSL */
  29564. WOLFSSL_TEST_SUBROUTINE int logging_test(void)
  29565. {
  29566. #ifdef DEBUG_WOLFSSL
  29567. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  29568. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  29569. byte b[256];
  29570. int i;
  29571. for (i = 0; i < (int)sizeof(b); i++)
  29572. b[i] = i;
  29573. if (wolfSSL_Debugging_ON() != 0)
  29574. return -13600;
  29575. if (wolfSSL_SetLoggingCb(my_Logging_cb) != 0)
  29576. return -13601;
  29577. WOLFSSL_MSG(msg);
  29578. WOLFSSL_BUFFER(a, sizeof(a));
  29579. WOLFSSL_BUFFER(b, sizeof(b));
  29580. WOLFSSL_BUFFER(NULL, 0);
  29581. WOLFSSL_ERROR(MEMORY_E);
  29582. WOLFSSL_ERROR_MSG(msg);
  29583. /* turn off logs */
  29584. wolfSSL_Debugging_OFF();
  29585. /* capture log count */
  29586. i = log_cnt;
  29587. /* validate no logs are output when disabled */
  29588. WOLFSSL_MSG(msg);
  29589. WOLFSSL_BUFFER(a, sizeof(a));
  29590. WOLFSSL_BUFFER(b, sizeof(b));
  29591. WOLFSSL_BUFFER(NULL, 0);
  29592. WOLFSSL_ERROR(MEMORY_E);
  29593. WOLFSSL_ERROR_MSG(msg);
  29594. /* check the logs were disabled */
  29595. if (i != log_cnt)
  29596. return -13602;
  29597. /* restore callback and leave logging enabled */
  29598. wolfSSL_SetLoggingCb(NULL);
  29599. wolfSSL_Debugging_ON();
  29600. /* suppress unused args */
  29601. (void)a;
  29602. (void)b;
  29603. #else
  29604. if (wolfSSL_Debugging_ON() != NOT_COMPILED_IN)
  29605. return -13603;
  29606. wolfSSL_Debugging_OFF();
  29607. if (wolfSSL_SetLoggingCb(NULL) != NOT_COMPILED_IN)
  29608. return -13604;
  29609. #endif /* DEBUG_WOLFSSL */
  29610. return 0;
  29611. }
  29612. WOLFSSL_TEST_SUBROUTINE int mutex_test(void)
  29613. {
  29614. #ifdef WOLFSSL_PTHREADS
  29615. wolfSSL_Mutex m;
  29616. #endif
  29617. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  29618. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  29619. if (mm == NULL)
  29620. return -13700;
  29621. wc_FreeMutex(mm);
  29622. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  29623. #endif
  29624. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  29625. #ifdef WOLFSSL_PTHREADS
  29626. if (wc_InitMutex(&m) != 0)
  29627. return -13701;
  29628. if (wc_LockMutex(&m) != 0)
  29629. return -13702;
  29630. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  29631. /* trying to free a locked mutex is not portable behavior with pthread */
  29632. /* Attempting to destroy a locked mutex results in undefined behavior */
  29633. if (wc_FreeMutex(&m) != BAD_MUTEX_E)
  29634. return -13703;
  29635. #endif
  29636. if (wc_UnLockMutex(&m) != 0)
  29637. return -13704;
  29638. if (wc_FreeMutex(&m) != 0)
  29639. return -13705;
  29640. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  29641. /* Trying to use a pthread after free'ing is not portable behavior */
  29642. if (wc_LockMutex(&m) != BAD_MUTEX_E)
  29643. return -13706;
  29644. if (wc_UnLockMutex(&m) != BAD_MUTEX_E)
  29645. return -13707;
  29646. #endif
  29647. #endif
  29648. return 0;
  29649. }
  29650. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  29651. #ifndef WOLFSSL_NO_MALLOC
  29652. static int malloc_cnt = 0;
  29653. static int realloc_cnt = 0;
  29654. static int free_cnt = 0;
  29655. static void *my_Malloc_cb(size_t size)
  29656. {
  29657. malloc_cnt++;
  29658. #ifndef WOLFSSL_NO_MALLOC
  29659. return malloc(size);
  29660. #else
  29661. WOLFSSL_MSG("No malloc available");
  29662. (void)size;
  29663. return NULL;
  29664. #endif
  29665. }
  29666. static void my_Free_cb(void *ptr)
  29667. {
  29668. free_cnt++;
  29669. #ifndef WOLFSSL_NO_MALLOC
  29670. free(ptr);
  29671. #else
  29672. WOLFSSL_MSG("No free available");
  29673. (void)ptr;
  29674. #endif
  29675. }
  29676. static void *my_Realloc_cb(void *ptr, size_t size)
  29677. {
  29678. realloc_cnt++;
  29679. #ifndef WOLFSSL_NO_MALLOC
  29680. return realloc(ptr, size);
  29681. #else
  29682. WOLFSSL_MSG("No realloc available");
  29683. (void)ptr;
  29684. (void)size;
  29685. return NULL;
  29686. #endif
  29687. }
  29688. #endif /* !WOLFSSL_NO_MALLOC */
  29689. WOLFSSL_TEST_SUBROUTINE int memcb_test(void)
  29690. {
  29691. int ret = 0;
  29692. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM)
  29693. byte* b = NULL;
  29694. #endif
  29695. wolfSSL_Malloc_cb mc;
  29696. wolfSSL_Free_cb fc;
  29697. wolfSSL_Realloc_cb rc;
  29698. /* Save existing memory callbacks */
  29699. if (wolfSSL_GetAllocators(&mc, &fc, &rc) != 0)
  29700. return -13800;
  29701. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM)
  29702. /* test realloc */
  29703. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29704. if (b == NULL) {
  29705. ERROR_OUT(-13801, exit_memcb);
  29706. }
  29707. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29708. b = NULL;
  29709. /* Use API. */
  29710. if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)(void*)&my_Malloc_cb,
  29711. (wolfSSL_Free_cb)(void*)&my_Free_cb,
  29712. (wolfSSL_Realloc_cb)(void*)&my_Realloc_cb) != 0) {
  29713. ERROR_OUT(-13802, exit_memcb);
  29714. }
  29715. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29716. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29717. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29718. #ifndef WOLFSSL_STATIC_MEMORY
  29719. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  29720. #else
  29721. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  29722. #endif
  29723. ret = -13803;
  29724. #endif /* !WOLFSSL_NO_MALLOC */
  29725. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM)
  29726. exit_memcb:
  29727. #endif
  29728. /* restore memory callbacks */
  29729. wolfSSL_SetAllocators(mc, fc, rc);
  29730. return ret;
  29731. }
  29732. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  29733. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  29734. WOLFSSL_TEST_SUBROUTINE int blob_test(void)
  29735. {
  29736. int ret = 0;
  29737. byte out[112];
  29738. byte blob[112];
  29739. word32 outSz;
  29740. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  29741. {
  29742. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  29743. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  29744. };
  29745. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  29746. {
  29747. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  29748. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  29749. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  29750. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  29751. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  29752. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  29753. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  29754. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  29755. };
  29756. XMEMSET(blob, 0, sizeof(blob));
  29757. outSz = sizeof(blob);
  29758. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  29759. if (ret != 0) {
  29760. ERROR_OUT(-13900, exit_blob);
  29761. }
  29762. blob[outSz - 2] += 1;
  29763. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  29764. if (ret == 0) { /* should fail with altered blob */
  29765. ERROR_OUT(-13901, exit_blob);
  29766. }
  29767. XMEMSET(blob, 0, sizeof(blob));
  29768. outSz = sizeof(blob);
  29769. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  29770. if (ret != 0) {
  29771. ERROR_OUT(-13902, exit_blob);
  29772. }
  29773. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  29774. if (ret != 0) {
  29775. ERROR_OUT(-13903, exit_blob);
  29776. }
  29777. if (XMEMCMP(out, iv, sizeof(iv))) {
  29778. ERROR_OUT(-13904, exit_blob);
  29779. }
  29780. XMEMSET(blob, 0, sizeof(blob));
  29781. outSz = sizeof(blob);
  29782. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  29783. if (ret != 0) {
  29784. ERROR_OUT(-13905, exit_blob);
  29785. }
  29786. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  29787. if (ret != 0) {
  29788. ERROR_OUT(-13906, exit_blob);
  29789. }
  29790. if (XMEMCMP(out, text, sizeof(text))) {
  29791. ERROR_OUT(-13907, exit_blob);
  29792. }
  29793. exit_blob:
  29794. return ret;
  29795. }
  29796. #endif /* WOLFSSL_IMX6_CAAM_BLOB */
  29797. #ifdef WOLF_CRYPTO_CB
  29798. /* Example custom context for crypto callback */
  29799. typedef struct {
  29800. int exampleVar; /* example, not used */
  29801. } myCryptoDevCtx;
  29802. /* Example crypto dev callback function that calls software version */
  29803. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  29804. {
  29805. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  29806. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  29807. if (info == NULL)
  29808. return BAD_FUNC_ARG;
  29809. #ifdef DEBUG_WOLFSSL
  29810. printf("CryptoDevCb: Algo Type %d\n", info->algo_type);
  29811. #endif
  29812. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  29813. #ifndef WC_NO_RNG
  29814. /* set devId to invalid, so software is used */
  29815. info->rng.rng->devId = INVALID_DEVID;
  29816. ret = wc_RNG_GenerateBlock(info->rng.rng,
  29817. info->rng.out, info->rng.sz);
  29818. /* reset devId */
  29819. info->rng.rng->devId = devIdArg;
  29820. #endif
  29821. }
  29822. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  29823. #ifndef WC_NO_RNG
  29824. static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  29825. word32* seedWord32 = (word32*)seed;
  29826. word32 len;
  29827. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  29828. while (info->seed.sz > 0) {
  29829. len = (word32)sizeof(seed);
  29830. if (info->seed.sz < len)
  29831. len = info->seed.sz;
  29832. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  29833. info->seed.seed += len;
  29834. info->seed.sz -= len;
  29835. (*seedWord32)++;
  29836. }
  29837. ret = 0;
  29838. #endif
  29839. }
  29840. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  29841. #ifdef DEBUG_WOLFSSL
  29842. printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
  29843. #endif
  29844. #ifndef NO_RSA
  29845. if (info->pk.type == WC_PK_TYPE_RSA) {
  29846. /* set devId to invalid, so software is used */
  29847. info->pk.rsa.key->devId = INVALID_DEVID;
  29848. switch (info->pk.rsa.type) {
  29849. case RSA_PUBLIC_ENCRYPT:
  29850. case RSA_PUBLIC_DECRYPT:
  29851. /* perform software based RSA public op */
  29852. ret = wc_RsaFunction(
  29853. info->pk.rsa.in, info->pk.rsa.inLen,
  29854. info->pk.rsa.out, info->pk.rsa.outLen,
  29855. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  29856. break;
  29857. case RSA_PRIVATE_ENCRYPT:
  29858. case RSA_PRIVATE_DECRYPT:
  29859. /* perform software based RSA private op */
  29860. ret = wc_RsaFunction(
  29861. info->pk.rsa.in, info->pk.rsa.inLen,
  29862. info->pk.rsa.out, info->pk.rsa.outLen,
  29863. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  29864. break;
  29865. }
  29866. /* reset devId */
  29867. info->pk.rsa.key->devId = devIdArg;
  29868. }
  29869. #ifdef WOLFSSL_KEY_GEN
  29870. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  29871. info->pk.rsakg.key->devId = INVALID_DEVID;
  29872. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  29873. info->pk.rsakg.e, info->pk.rsakg.rng);
  29874. /* reset devId */
  29875. info->pk.rsakg.key->devId = devIdArg;
  29876. }
  29877. #endif
  29878. #endif /* !NO_RSA */
  29879. #ifdef HAVE_ECC
  29880. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  29881. /* set devId to invalid, so software is used */
  29882. info->pk.eckg.key->devId = INVALID_DEVID;
  29883. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  29884. info->pk.eckg.key, info->pk.eckg.curveId);
  29885. /* reset devId */
  29886. info->pk.eckg.key->devId = devIdArg;
  29887. }
  29888. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  29889. /* set devId to invalid, so software is used */
  29890. info->pk.eccsign.key->devId = INVALID_DEVID;
  29891. ret = wc_ecc_sign_hash(
  29892. info->pk.eccsign.in, info->pk.eccsign.inlen,
  29893. info->pk.eccsign.out, info->pk.eccsign.outlen,
  29894. info->pk.eccsign.rng, info->pk.eccsign.key);
  29895. /* reset devId */
  29896. info->pk.eccsign.key->devId = devIdArg;
  29897. }
  29898. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  29899. /* set devId to invalid, so software is used */
  29900. info->pk.eccverify.key->devId = INVALID_DEVID;
  29901. ret = wc_ecc_verify_hash(
  29902. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  29903. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  29904. info->pk.eccverify.res, info->pk.eccverify.key);
  29905. /* reset devId */
  29906. info->pk.eccverify.key->devId = devIdArg;
  29907. }
  29908. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  29909. /* set devId to invalid, so software is used */
  29910. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  29911. ret = wc_ecc_shared_secret(
  29912. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  29913. info->pk.ecdh.out, info->pk.ecdh.outlen);
  29914. /* reset devId */
  29915. info->pk.ecdh.private_key->devId = devIdArg;
  29916. }
  29917. #endif /* HAVE_ECC */
  29918. }
  29919. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  29920. #if !defined(NO_AES) || !defined(NO_DES3)
  29921. #ifdef HAVE_AESGCM
  29922. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  29923. if (info->cipher.enc) {
  29924. /* set devId to invalid, so software is used */
  29925. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  29926. ret = wc_AesGcmEncrypt(
  29927. info->cipher.aesgcm_enc.aes,
  29928. info->cipher.aesgcm_enc.out,
  29929. info->cipher.aesgcm_enc.in,
  29930. info->cipher.aesgcm_enc.sz,
  29931. info->cipher.aesgcm_enc.iv,
  29932. info->cipher.aesgcm_enc.ivSz,
  29933. info->cipher.aesgcm_enc.authTag,
  29934. info->cipher.aesgcm_enc.authTagSz,
  29935. info->cipher.aesgcm_enc.authIn,
  29936. info->cipher.aesgcm_enc.authInSz);
  29937. /* reset devId */
  29938. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  29939. }
  29940. else {
  29941. /* set devId to invalid, so software is used */
  29942. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  29943. ret = wc_AesGcmDecrypt(
  29944. info->cipher.aesgcm_dec.aes,
  29945. info->cipher.aesgcm_dec.out,
  29946. info->cipher.aesgcm_dec.in,
  29947. info->cipher.aesgcm_dec.sz,
  29948. info->cipher.aesgcm_dec.iv,
  29949. info->cipher.aesgcm_dec.ivSz,
  29950. info->cipher.aesgcm_dec.authTag,
  29951. info->cipher.aesgcm_dec.authTagSz,
  29952. info->cipher.aesgcm_dec.authIn,
  29953. info->cipher.aesgcm_dec.authInSz);
  29954. /* reset devId */
  29955. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  29956. }
  29957. }
  29958. #endif /* HAVE_AESGCM */
  29959. #ifdef HAVE_AES_CBC
  29960. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  29961. if (info->cipher.enc) {
  29962. /* set devId to invalid, so software is used */
  29963. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  29964. ret = wc_AesCbcEncrypt(
  29965. info->cipher.aescbc.aes,
  29966. info->cipher.aescbc.out,
  29967. info->cipher.aescbc.in,
  29968. info->cipher.aescbc.sz);
  29969. /* reset devId */
  29970. info->cipher.aescbc.aes->devId = devIdArg;
  29971. }
  29972. else {
  29973. /* set devId to invalid, so software is used */
  29974. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  29975. ret = wc_AesCbcDecrypt(
  29976. info->cipher.aescbc.aes,
  29977. info->cipher.aescbc.out,
  29978. info->cipher.aescbc.in,
  29979. info->cipher.aescbc.sz);
  29980. /* reset devId */
  29981. info->cipher.aescbc.aes->devId = devIdArg;
  29982. }
  29983. }
  29984. #endif /* HAVE_AES_CBC */
  29985. #ifndef NO_DES3
  29986. if (info->cipher.type == WC_CIPHER_DES3) {
  29987. if (info->cipher.enc) {
  29988. /* set devId to invalid, so software is used */
  29989. info->cipher.des3.des->devId = INVALID_DEVID;
  29990. ret = wc_Des3_CbcEncrypt(
  29991. info->cipher.des3.des,
  29992. info->cipher.des3.out,
  29993. info->cipher.des3.in,
  29994. info->cipher.des3.sz);
  29995. /* reset devId */
  29996. info->cipher.des3.des->devId = devIdArg;
  29997. }
  29998. else {
  29999. /* set devId to invalid, so software is used */
  30000. info->cipher.des3.des->devId = INVALID_DEVID;
  30001. ret = wc_Des3_CbcDecrypt(
  30002. info->cipher.des3.des,
  30003. info->cipher.des3.out,
  30004. info->cipher.des3.in,
  30005. info->cipher.des3.sz);
  30006. /* reset devId */
  30007. info->cipher.des3.des->devId = devIdArg;
  30008. }
  30009. }
  30010. #endif /* !NO_DES3 */
  30011. #endif /* !NO_AES || !NO_DES3 */
  30012. }
  30013. #if !defined(NO_SHA) || !defined(NO_SHA256)
  30014. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  30015. #if !defined(NO_SHA)
  30016. if (info->hash.type == WC_HASH_TYPE_SHA) {
  30017. if (info->hash.sha1 == NULL)
  30018. return NOT_COMPILED_IN;
  30019. /* set devId to invalid, so software is used */
  30020. info->hash.sha1->devId = INVALID_DEVID;
  30021. if (info->hash.in != NULL) {
  30022. ret = wc_ShaUpdate(
  30023. info->hash.sha1,
  30024. info->hash.in,
  30025. info->hash.inSz);
  30026. }
  30027. if (info->hash.digest != NULL) {
  30028. ret = wc_ShaFinal(
  30029. info->hash.sha1,
  30030. info->hash.digest);
  30031. }
  30032. /* reset devId */
  30033. info->hash.sha1->devId = devIdArg;
  30034. }
  30035. else
  30036. #endif
  30037. #if !defined(NO_SHA256)
  30038. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  30039. if (info->hash.sha256 == NULL)
  30040. return NOT_COMPILED_IN;
  30041. /* set devId to invalid, so software is used */
  30042. info->hash.sha256->devId = INVALID_DEVID;
  30043. if (info->hash.in != NULL) {
  30044. ret = wc_Sha256Update(
  30045. info->hash.sha256,
  30046. info->hash.in,
  30047. info->hash.inSz);
  30048. }
  30049. if (info->hash.digest != NULL) {
  30050. ret = wc_Sha256Final(
  30051. info->hash.sha256,
  30052. info->hash.digest);
  30053. }
  30054. /* reset devId */
  30055. info->hash.sha256->devId = devIdArg;
  30056. }
  30057. else
  30058. #endif
  30059. {
  30060. }
  30061. }
  30062. #endif /* !NO_SHA || !NO_SHA256 */
  30063. #ifndef NO_HMAC
  30064. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  30065. if (info->hmac.hmac == NULL)
  30066. return NOT_COMPILED_IN;
  30067. /* set devId to invalid, so software is used */
  30068. info->hmac.hmac->devId = INVALID_DEVID;
  30069. if (info->hash.in != NULL) {
  30070. ret = wc_HmacUpdate(
  30071. info->hmac.hmac,
  30072. info->hmac.in,
  30073. info->hmac.inSz);
  30074. }
  30075. else if (info->hash.digest != NULL) {
  30076. ret = wc_HmacFinal(
  30077. info->hmac.hmac,
  30078. info->hmac.digest);
  30079. }
  30080. /* reset devId */
  30081. info->hmac.hmac->devId = devIdArg;
  30082. }
  30083. #endif
  30084. (void)devIdArg;
  30085. (void)myCtx;
  30086. return ret;
  30087. }
  30088. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void)
  30089. {
  30090. int ret = 0;
  30091. myCryptoDevCtx myCtx;
  30092. /* example data for callback */
  30093. myCtx.exampleVar = 1;
  30094. /* set devId to something other than INVALID_DEVID */
  30095. devId = 1;
  30096. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  30097. #ifndef WC_NO_RNG
  30098. if (ret == 0)
  30099. ret = random_test();
  30100. #endif /* WC_NO_RNG */
  30101. #ifndef NO_RSA
  30102. if (ret == 0)
  30103. ret = rsa_test();
  30104. #endif
  30105. #ifdef HAVE_ECC
  30106. if (ret == 0)
  30107. ret = ecc_test();
  30108. #endif
  30109. #ifndef NO_AES
  30110. #ifdef HAVE_AESGCM
  30111. if (ret == 0)
  30112. ret = aesgcm_test();
  30113. #endif
  30114. #ifdef HAVE_AES_CBC
  30115. if (ret == 0)
  30116. ret = aes_test();
  30117. #endif
  30118. #endif /* !NO_AES */
  30119. #ifndef NO_DES3
  30120. if (ret == 0)
  30121. ret = des3_test();
  30122. #endif /* !NO_DES3 */
  30123. #if !defined(NO_SHA) || !defined(NO_SHA256)
  30124. #ifndef NO_SHA
  30125. if (ret == 0)
  30126. ret = sha_test();
  30127. #endif
  30128. #ifndef NO_SHA256
  30129. if (ret == 0)
  30130. ret = sha256_test();
  30131. #endif
  30132. #endif
  30133. #ifndef NO_HMAC
  30134. #ifndef NO_SHA
  30135. if (ret == 0)
  30136. ret = hmac_sha_test();
  30137. #endif
  30138. #ifndef NO_SHA256
  30139. if (ret == 0)
  30140. ret = hmac_sha256_test();
  30141. #endif
  30142. #endif
  30143. #ifndef NO_PWDBASED
  30144. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  30145. if (ret == 0)
  30146. ret = pbkdf2_test();
  30147. #endif
  30148. #endif
  30149. /* reset devId */
  30150. devId = INVALID_DEVID;
  30151. return ret;
  30152. }
  30153. #endif /* WOLF_CRYPTO_CB */
  30154. #ifdef WOLFSSL_CERT_PIV
  30155. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void)
  30156. {
  30157. int ret;
  30158. wc_CertPIV piv;
  30159. /* Template for Identiv PIV cert, nonce and signature */
  30160. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  30161. 0x0A, 0x0D,
  30162. 0x53, 0x04, /* NIST PIV Cert */
  30163. 0x70, 0x02, /* Certificate */
  30164. 0x30, 0x00,
  30165. 0x71, 0x01, 0x00, /* Cert Info */
  30166. 0xFE, 0x00, /* Error Detection */
  30167. 0x0B, 0x01, 0x00, /* Nonce */
  30168. 0x0C, 0x01, 0x00, /* Signed Nonce */
  30169. };
  30170. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  30171. 0x53, 0x04, /* NIST PIV Cert */
  30172. 0x70, 0x02, /* Certificate */
  30173. 0x30, 0x00,
  30174. 0x71, 0x01, 0x00, /* Cert Info */
  30175. 0xFE, 0x00, /* Error Detection */
  30176. };
  30177. /* Test with identiv 0x0A, 0x0B and 0x0C markers */
  30178. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  30179. if (ret == 0) {
  30180. /* Test with NIST PIV format */
  30181. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  30182. }
  30183. return ret;
  30184. }
  30185. #endif /* WOLFSSL_CERT_PIV */
  30186. #undef ERROR_OUT
  30187. #else
  30188. #ifndef NO_MAIN_DRIVER
  30189. int main() { return 0; }
  30190. #endif
  30191. #endif /* NO_CRYPT_TEST */