test.c 924 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268
  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. /* only for stack size check */
  32. #ifdef HAVE_STACK_SIZE
  33. #include <wolfssl/ssl.h>
  34. #define err_sys err_sys_remap /* remap err_sys */
  35. #include <wolfssl/test.h>
  36. #undef err_sys
  37. #endif
  38. #ifdef USE_FLAT_TEST_H
  39. #include "test.h"
  40. #else
  41. #include "wolfcrypt/test/test.h"
  42. #endif
  43. /* printf mappings */
  44. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  45. #include <mqx.h>
  46. #include <stdlib.h>
  47. /* see wc_port.h for fio.h and nio.h includes */
  48. #elif defined(FREESCALE_KSDK_BM)
  49. #include "fsl_debug_console.h"
  50. #undef printf
  51. #define printf PRINTF
  52. #elif defined(WOLFSSL_APACHE_MYNEWT)
  53. #include <assert.h>
  54. #include <string.h>
  55. #include "sysinit/sysinit.h"
  56. #include "os/os.h"
  57. #ifdef ARCH_sim
  58. #include "mcu/mcu_sim.h"
  59. #endif
  60. #include "os/os_time.h"
  61. #elif defined(WOLFSSL_ESPIDF)
  62. #include <time.h>
  63. #include <sys/time.h>
  64. #elif defined(WOLFSSL_ZEPHYR)
  65. #include <stdio.h>
  66. #define printf printk
  67. #elif defined(MICRIUM)
  68. #include <bsp_ser.h>
  69. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  70. #undef printf
  71. #define printf BSP_Ser_Printf
  72. #elif defined(WOLFSSL_PB)
  73. #include <stdarg.h>
  74. int wolfssl_pb_print(const char*, ...);
  75. #undef printf
  76. #define printf wolfssl_pb_print
  77. #elif defined(WOLFSSL_TELIT_M2MB)
  78. #include "wolfssl/wolfcrypt/wc_port.h" /* for m2mb headers */
  79. #include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
  80. /* remap printf */
  81. #undef printf
  82. #define printf M2M_LOG_INFO
  83. /* OS requires occasional sleep() */
  84. #ifndef TEST_SLEEP_MS
  85. #define TEST_SLEEP_MS 50
  86. #endif
  87. #define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
  88. /* don't use file system for these tests, since ./certs dir isn't loaded */
  89. #undef NO_FILESYSTEM
  90. #define NO_FILESYSTEM
  91. #elif defined(THREADX) && !defined(WOLFSSL_WICED) && !defined(THREADX_NO_DC_PRINTF)
  92. /* since just testing, use THREADX log printf instead */
  93. int dc_log_printf(char*, ...);
  94. #undef printf
  95. #define printf dc_log_printf
  96. #else
  97. #ifdef XMALLOC_USER
  98. #include <stdlib.h> /* we're using malloc / free direct here */
  99. #endif
  100. #ifndef STRING_USER
  101. #include <stdio.h>
  102. #endif
  103. /* enable way for customer to override test/bench printf */
  104. #ifdef XPRINTF
  105. #undef printf
  106. #define printf XPRINTF
  107. #endif
  108. #endif
  109. #include <wolfssl/wolfcrypt/memory.h>
  110. #include <wolfssl/wolfcrypt/wc_port.h>
  111. #include <wolfssl/wolfcrypt/logging.h>
  112. #include <wolfssl/wolfcrypt/types.h>
  113. #include <wolfssl/wolfcrypt/asn.h>
  114. #include <wolfssl/wolfcrypt/md2.h>
  115. #include <wolfssl/wolfcrypt/md5.h>
  116. #include <wolfssl/wolfcrypt/md4.h>
  117. #include <wolfssl/wolfcrypt/sha.h>
  118. #include <wolfssl/wolfcrypt/sha256.h>
  119. #include <wolfssl/wolfcrypt/sha512.h>
  120. #include <wolfssl/wolfcrypt/arc4.h>
  121. #if defined(WC_NO_RNG)
  122. #include <wolfssl/wolfcrypt/integer.h>
  123. #else
  124. #include <wolfssl/wolfcrypt/random.h>
  125. #endif
  126. #include <wolfssl/wolfcrypt/coding.h>
  127. #include <wolfssl/wolfcrypt/signature.h>
  128. #include <wolfssl/wolfcrypt/rsa.h>
  129. #include <wolfssl/wolfcrypt/des3.h>
  130. #include <wolfssl/wolfcrypt/aes.h>
  131. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  132. #include <wolfssl/wolfcrypt/cmac.h>
  133. #include <wolfssl/wolfcrypt/poly1305.h>
  134. #include <wolfssl/wolfcrypt/camellia.h>
  135. #include <wolfssl/wolfcrypt/hmac.h>
  136. #include <wolfssl/wolfcrypt/dh.h>
  137. #include <wolfssl/wolfcrypt/dsa.h>
  138. #include <wolfssl/wolfcrypt/srp.h>
  139. #include <wolfssl/wolfcrypt/idea.h>
  140. #include <wolfssl/wolfcrypt/hc128.h>
  141. #include <wolfssl/wolfcrypt/rabbit.h>
  142. #include <wolfssl/wolfcrypt/chacha.h>
  143. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  144. #include <wolfssl/wolfcrypt/pwdbased.h>
  145. #include <wolfssl/wolfcrypt/ripemd.h>
  146. #include <wolfssl/wolfcrypt/error-crypt.h>
  147. #ifdef HAVE_ECC
  148. #include <wolfssl/wolfcrypt/ecc.h>
  149. #endif
  150. #ifdef HAVE_CURVE25519
  151. #include <wolfssl/wolfcrypt/curve25519.h>
  152. #endif
  153. #ifdef HAVE_ED25519
  154. #include <wolfssl/wolfcrypt/ed25519.h>
  155. #endif
  156. #ifdef HAVE_CURVE448
  157. #include <wolfssl/wolfcrypt/curve448.h>
  158. #endif
  159. #ifdef HAVE_ED448
  160. #include <wolfssl/wolfcrypt/ed448.h>
  161. #endif
  162. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  163. #include <wolfssl/wolfcrypt/blake2.h>
  164. #endif
  165. #ifdef WOLFSSL_SHA3
  166. #include <wolfssl/wolfcrypt/sha3.h>
  167. #endif
  168. #ifdef HAVE_LIBZ
  169. #include <wolfssl/wolfcrypt/compress.h>
  170. #endif
  171. #ifdef HAVE_PKCS7
  172. #include <wolfssl/wolfcrypt/pkcs7.h>
  173. #endif
  174. #ifdef HAVE_FIPS
  175. #include <wolfssl/wolfcrypt/fips_test.h>
  176. #endif
  177. #ifdef HAVE_SELFTEST
  178. #include <wolfssl/wolfcrypt/selftest.h>
  179. #endif
  180. #ifdef WOLFSSL_ASYNC_CRYPT
  181. #include <wolfssl/wolfcrypt/async.h>
  182. #endif
  183. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  184. #include <wolfssl/wolfcrypt/logging.h>
  185. #endif
  186. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  187. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  188. #endif
  189. #ifdef WOLF_CRYPTO_CB
  190. #include <wolfssl/wolfcrypt/cryptocb.h>
  191. #ifdef HAVE_INTEL_QA_SYNC
  192. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  193. #endif
  194. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  195. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  196. #endif
  197. #endif
  198. #ifdef _MSC_VER
  199. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  200. #pragma warning(disable: 4996)
  201. #endif
  202. #ifdef OPENSSL_EXTRA
  203. #ifndef WOLFCRYPT_ONLY
  204. #include <wolfssl/openssl/evp.h>
  205. #endif
  206. #include <wolfssl/openssl/rand.h>
  207. #include <wolfssl/openssl/hmac.h>
  208. #include <wolfssl/openssl/aes.h>
  209. #include <wolfssl/openssl/des.h>
  210. #endif
  211. #if defined(NO_FILESYSTEM)
  212. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  213. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  214. #define USE_CERT_BUFFERS_2048
  215. #endif
  216. #if !defined(USE_CERT_BUFFERS_256)
  217. #define USE_CERT_BUFFERS_256
  218. #endif
  219. #endif
  220. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  221. #define ENABLE_ECC384_CERT_GEN_TEST
  222. #endif
  223. #include <wolfssl/certs_test.h>
  224. #ifdef HAVE_NTRU
  225. #include "libntruencrypt/ntru_crypto.h"
  226. #endif
  227. #ifdef WOLFSSL_STATIC_MEMORY
  228. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  229. #else
  230. #define HEAP_HINT NULL
  231. #endif /* WOLFSSL_STATIC_MEMORY */
  232. /* these cases do not have intermediate hashing support */
  233. #if (defined(WOLFSSL_AFALG_XILINX_SHA3) && !defined(WOLFSSL_AFALG_HASH_KEEP)) \
  234. && !defined(WOLFSSL_XILINX_CRYPT)
  235. #define NO_INTM_HASH_TEST
  236. #endif
  237. #if defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_MULTI_ATTRIB)
  238. static void initDefaultName(void);
  239. #endif
  240. /* for async devices */
  241. static int devId = INVALID_DEVID;
  242. #ifdef HAVE_WNR
  243. const char* wnrConfigFile = "wnr-example.conf";
  244. #endif
  245. typedef struct testVector {
  246. const char* input;
  247. const char* output;
  248. size_t inLen;
  249. size_t outLen;
  250. } testVector;
  251. int error_test(void);
  252. int base64_test(void);
  253. int base16_test(void);
  254. int asn_test(void);
  255. int md2_test(void);
  256. int md5_test(void);
  257. int md4_test(void);
  258. int sha_test(void);
  259. int sha224_test(void);
  260. int sha256_test(void);
  261. int sha512_test(void);
  262. int sha384_test(void);
  263. int sha3_test(void);
  264. int shake256_test(void);
  265. int hash_test(void);
  266. int hmac_md5_test(void);
  267. int hmac_sha_test(void);
  268. int hmac_sha224_test(void);
  269. int hmac_sha256_test(void);
  270. int hmac_sha384_test(void);
  271. int hmac_sha512_test(void);
  272. int hmac_sha3_test(void);
  273. int hkdf_test(void);
  274. int x963kdf_test(void);
  275. int arc4_test(void);
  276. int hc128_test(void);
  277. int rabbit_test(void);
  278. int chacha_test(void);
  279. int chacha20_poly1305_aead_test(void);
  280. int des_test(void);
  281. int des3_test(void);
  282. int aes_test(void);
  283. int aes192_test(void);
  284. int aes256_test(void);
  285. int aesofb_test(void);
  286. int cmac_test(void);
  287. int poly1305_test(void);
  288. int aesgcm_test(void);
  289. int aesgcm_default_test(void);
  290. int gmac_test(void);
  291. int aesccm_test(void);
  292. int aeskeywrap_test(void);
  293. int camellia_test(void);
  294. int rsa_no_pad_test(void);
  295. int rsa_test(void);
  296. int dh_test(void);
  297. int dsa_test(void);
  298. int srp_test(void);
  299. #ifndef WC_NO_RNG
  300. int random_test(void);
  301. #endif /* WC_NO_RNG */
  302. int pwdbased_test(void);
  303. int ripemd_test(void);
  304. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  305. int openssl_test(void); /* test mini api */
  306. int openssl_pkey_test(void);
  307. int openssl_pkey0_test(void);
  308. int openssl_pkey1_test(void);
  309. int openSSL_evpMD_test(void);
  310. int openssl_evpSig_test(void);
  311. #endif
  312. int pbkdf1_test(void);
  313. int pkcs12_test(void);
  314. int pbkdf2_test(void);
  315. int scrypt_test(void);
  316. #ifdef HAVE_ECC
  317. int ecc_test(void);
  318. #ifdef HAVE_ECC_ENCRYPT
  319. int ecc_encrypt_test(void);
  320. #endif
  321. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  322. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256)
  323. /* skip for ATECC508/608A, cannot import private key buffers */
  324. int ecc_test_buffers(void);
  325. #endif
  326. #endif
  327. #ifdef HAVE_CURVE25519
  328. int curve25519_test(void);
  329. #endif
  330. #ifdef HAVE_ED25519
  331. int ed25519_test(void);
  332. #endif
  333. #ifdef HAVE_CURVE448
  334. int curve448_test(void);
  335. #endif
  336. #ifdef HAVE_ED448
  337. int ed448_test(void);
  338. #endif
  339. #ifdef HAVE_BLAKE2
  340. int blake2b_test(void);
  341. #endif
  342. #ifdef HAVE_BLAKE2S
  343. int blake2s_test(void);
  344. #endif
  345. #ifdef HAVE_LIBZ
  346. int compress_test(void);
  347. #endif
  348. #ifdef HAVE_PKCS7
  349. #ifndef NO_PKCS7_ENCRYPTED_DATA
  350. int pkcs7encrypted_test(void);
  351. #endif
  352. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  353. int pkcs7compressed_test(void);
  354. #endif
  355. int pkcs7signed_test(void);
  356. int pkcs7enveloped_test(void);
  357. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  358. int pkcs7authenveloped_test(void);
  359. #endif
  360. #ifndef NO_AES
  361. int pkcs7callback_test(byte* cert, word32 certSz, byte* key,
  362. word32 keySz);
  363. #endif
  364. #endif
  365. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  366. !defined(NO_FILESYSTEM)
  367. int cert_test(void);
  368. #endif
  369. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  370. !defined(NO_FILESYSTEM)
  371. int certext_test(void);
  372. #endif
  373. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  374. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  375. int decodedCertCache_test(void);
  376. #endif
  377. #ifdef HAVE_IDEA
  378. int idea_test(void);
  379. #endif
  380. int memory_test(void);
  381. #ifdef HAVE_VALGRIND
  382. int mp_test(void);
  383. #endif
  384. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  385. int prime_test(void);
  386. #endif
  387. #ifdef ASN_BER_TO_DER
  388. int berder_test(void);
  389. #endif
  390. int logging_test(void);
  391. int mutex_test(void);
  392. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  393. int memcb_test(void);
  394. #endif
  395. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  396. int blob_test(void);
  397. #endif
  398. #ifdef WOLF_CRYPTO_CB
  399. int cryptocb_test(void);
  400. #endif
  401. #ifdef WOLFSSL_CERT_PIV
  402. int certpiv_test(void);
  403. #endif
  404. /* General big buffer size for many tests. */
  405. #define FOURK_BUF 4096
  406. #define ERROR_OUT(err, eLabel) { ret = (err); goto eLabel; }
  407. #ifdef HAVE_STACK_SIZE
  408. static THREAD_RETURN err_sys(const char* msg, int es)
  409. #else
  410. static int err_sys(const char* msg, int es)
  411. #endif
  412. {
  413. printf("%s error = %d\n", msg, es);
  414. EXIT_TEST(-1);
  415. }
  416. #ifndef HAVE_STACK_SIZE
  417. /* func_args from test.h, so don't have to pull in other stuff */
  418. typedef struct func_args {
  419. int argc;
  420. char** argv;
  421. int return_code;
  422. } func_args;
  423. #endif /* !HAVE_STACK_SIZE */
  424. #ifdef HAVE_FIPS
  425. static void myFipsCb(int ok, int err, const char* hash)
  426. {
  427. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  428. printf("message = %s\n", wc_GetErrorString(err));
  429. printf("hash = %s\n", hash);
  430. if (err == IN_CORE_FIPS_E) {
  431. printf("In core integrity hash check failure, copy above hash\n");
  432. printf("into verifyCore[] in fips_test.c and rebuild\n");
  433. }
  434. }
  435. #endif /* HAVE_FIPS */
  436. #ifdef WOLFSSL_STATIC_MEMORY
  437. #ifdef BENCH_EMBEDDED
  438. static byte gTestMemory[14000];
  439. #elif defined(WOLFSSL_CERT_EXT)
  440. static byte gTestMemory[140000];
  441. #elif defined(USE_FAST_MATH) && !defined(ALT_ECC_SIZE)
  442. static byte gTestMemory[160000];
  443. #else
  444. static byte gTestMemory[80000];
  445. #endif
  446. #endif
  447. #ifdef WOLFSSL_PB
  448. int wolfssl_pb_print(const char* msg, ...)
  449. {
  450. int ret;
  451. va_list args;
  452. char tmpBuf[80];
  453. va_start(args, msg);
  454. ret = vsprint(tmpBuf, msg, args);
  455. va_end(args);
  456. fnDumpStringToSystemLog(tmpBuf);
  457. return ret;
  458. }
  459. #endif /* WOLFSSL_PB */
  460. /* optional macro to add sleep between tests */
  461. #ifdef TEST_SLEEP
  462. #include <stdarg.h> /* for var args */
  463. static WC_INLINE void test_pass(const char* fmt, ...)
  464. {
  465. va_list args;
  466. va_start(args, fmt);
  467. printf(fmt, args);
  468. va_end(args);
  469. TEST_SLEEP();
  470. }
  471. #else
  472. /* redirect to printf */
  473. #define test_pass printf
  474. /* stub the sleep macro */
  475. #define TEST_SLEEP()
  476. #endif
  477. #ifdef HAVE_STACK_SIZE
  478. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  479. #else
  480. int wolfcrypt_test(void* args)
  481. #endif
  482. {
  483. int ret;
  484. printf("------------------------------------------------------------------------------\n");
  485. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  486. printf("------------------------------------------------------------------------------\n");
  487. if (args)
  488. ((func_args*)args)->return_code = -1; /* error state */
  489. #ifdef WOLFSSL_STATIC_MEMORY
  490. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  491. WOLFMEM_GENERAL, 1) != 0) {
  492. printf("unable to load static memory");
  493. return(EXIT_FAILURE);
  494. }
  495. #endif
  496. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  497. wolfSSL_Debugging_ON();
  498. #endif
  499. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  500. wc_SetLoggingHeap(HEAP_HINT);
  501. #endif
  502. #ifdef HAVE_FIPS
  503. wolfCrypt_SetCb_fips(myFipsCb);
  504. #endif
  505. #if !defined(NO_BIG_INT)
  506. if (CheckCtcSettings() != 1) {
  507. printf("Sizeof mismatch (build) %x != (run) %x\n",
  508. CTC_SETTINGS, CheckRunTimeSettings());
  509. return err_sys("Build vs runtime math mismatch\n", -1000);
  510. }
  511. #if defined(USE_FAST_MATH) && \
  512. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  513. if (CheckFastMathSettings() != 1)
  514. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  515. -1001);
  516. #endif /* USE_FAST_MATH */
  517. #endif /* !NO_BIG_INT */
  518. #if defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_MULTI_ATTRIB)
  519. initDefaultName();
  520. #endif
  521. #ifdef WOLFSSL_ASYNC_CRYPT
  522. ret = wolfAsync_DevOpen(&devId);
  523. if (ret < 0) {
  524. printf("Async device open failed\nRunning without async\n");
  525. }
  526. #else
  527. (void)devId;
  528. #endif /* WOLFSSL_ASYNC_CRYPT */
  529. #ifdef WOLF_CRYPTO_CB
  530. #ifdef HAVE_INTEL_QA_SYNC
  531. devId = wc_CryptoCb_InitIntelQa();
  532. if (INVALID_DEVID == devId) {
  533. printf("Couldn't init the Intel QA\n");
  534. }
  535. #endif
  536. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  537. devId = wc_CryptoCb_InitOcteon();
  538. if (INVALID_DEVID == devId) {
  539. printf("Couldn't init the Cavium Octeon\n");
  540. }
  541. #endif
  542. #endif
  543. #ifdef HAVE_SELFTEST
  544. if ( (ret = wolfCrypt_SelfTest()) != 0)
  545. return err_sys("CAVP selftest failed!\n", ret);
  546. else
  547. test_pass("CAVP selftest passed!\n");
  548. #endif
  549. if ( (ret = error_test()) != 0)
  550. return err_sys("error test failed!\n", ret);
  551. else
  552. test_pass("error test passed!\n");
  553. if ( (ret = memory_test()) != 0)
  554. return err_sys("MEMORY test failed!\n", ret);
  555. else
  556. test_pass("MEMORY test passed!\n");
  557. #ifndef NO_CODING
  558. if ( (ret = base64_test()) != 0)
  559. return err_sys("base64 test failed!\n", ret);
  560. else
  561. test_pass("base64 test passed!\n");
  562. #ifdef WOLFSSL_BASE16
  563. if ( (ret = base16_test()) != 0)
  564. return err_sys("base16 test failed!\n", ret);
  565. else
  566. test_pass("base16 test passed!\n");
  567. #endif
  568. #endif /* !NO_CODING */
  569. #ifndef NO_ASN
  570. if ( (ret = asn_test()) != 0)
  571. return err_sys("asn test failed!\n", ret);
  572. else
  573. test_pass("asn test passed!\n");
  574. #endif
  575. #ifndef WC_NO_RNG
  576. if ( (ret = random_test()) != 0)
  577. return err_sys("RANDOM test failed!\n", ret);
  578. else
  579. test_pass("RANDOM test passed!\n");
  580. #endif /* WC_NO_RNG */
  581. #ifndef NO_MD5
  582. if ( (ret = md5_test()) != 0)
  583. return err_sys("MD5 test failed!\n", ret);
  584. else
  585. test_pass("MD5 test passed!\n");
  586. #endif
  587. #ifdef WOLFSSL_MD2
  588. if ( (ret = md2_test()) != 0)
  589. return err_sys("MD2 test failed!\n", ret);
  590. else
  591. test_pass("MD2 test passed!\n");
  592. #endif
  593. #ifndef NO_MD4
  594. if ( (ret = md4_test()) != 0)
  595. return err_sys("MD4 test failed!\n", ret);
  596. else
  597. test_pass("MD4 test passed!\n");
  598. #endif
  599. #ifndef NO_SHA
  600. if ( (ret = sha_test()) != 0)
  601. return err_sys("SHA test failed!\n", ret);
  602. else
  603. test_pass("SHA test passed!\n");
  604. #endif
  605. #ifdef WOLFSSL_SHA224
  606. if ( (ret = sha224_test()) != 0)
  607. return err_sys("SHA-224 test failed!\n", ret);
  608. else
  609. test_pass("SHA-224 test passed!\n");
  610. #endif
  611. #ifndef NO_SHA256
  612. if ( (ret = sha256_test()) != 0)
  613. return err_sys("SHA-256 test failed!\n", ret);
  614. else
  615. test_pass("SHA-256 test passed!\n");
  616. #endif
  617. #ifdef WOLFSSL_SHA384
  618. if ( (ret = sha384_test()) != 0)
  619. return err_sys("SHA-384 test failed!\n", ret);
  620. else
  621. test_pass("SHA-384 test passed!\n");
  622. #endif
  623. #ifdef WOLFSSL_SHA512
  624. if ( (ret = sha512_test()) != 0)
  625. return err_sys("SHA-512 test failed!\n", ret);
  626. else
  627. test_pass("SHA-512 test passed!\n");
  628. #endif
  629. #ifdef WOLFSSL_SHA3
  630. if ( (ret = sha3_test()) != 0)
  631. return err_sys("SHA-3 test failed!\n", ret);
  632. else
  633. test_pass("SHA-3 test passed!\n");
  634. #endif
  635. #ifdef WOLFSSL_SHAKE256
  636. if ( (ret = shake256_test()) != 0)
  637. return err_sys("SHAKE256 test failed!\n", ret);
  638. else
  639. test_pass("SHAKE256 test passed!\n");
  640. #endif
  641. if ( (ret = hash_test()) != 0)
  642. return err_sys("Hash test failed!\n", ret);
  643. else
  644. test_pass("Hash test passed!\n");
  645. #ifdef WOLFSSL_RIPEMD
  646. if ( (ret = ripemd_test()) != 0)
  647. return err_sys("RIPEMD test failed!\n", ret);
  648. else
  649. test_pass("RIPEMD test passed!\n");
  650. #endif
  651. #ifdef HAVE_BLAKE2
  652. if ( (ret = blake2b_test()) != 0)
  653. return err_sys("BLAKE2b test failed!\n", ret);
  654. else
  655. test_pass("BLAKE2b test passed!\n");
  656. #endif
  657. #ifdef HAVE_BLAKE2S
  658. if ( (ret = blake2s_test()) != 0)
  659. return err_sys("BLAKE2s test failed!\n", ret);
  660. else
  661. test_pass("BLAKE2s test passed!\n");
  662. #endif
  663. #ifndef NO_HMAC
  664. #ifndef NO_MD5
  665. if ( (ret = hmac_md5_test()) != 0)
  666. return err_sys("HMAC-MD5 test failed!\n", ret);
  667. else
  668. test_pass("HMAC-MD5 test passed!\n");
  669. #endif
  670. #ifndef NO_SHA
  671. if ( (ret = hmac_sha_test()) != 0)
  672. return err_sys("HMAC-SHA test failed!\n", ret);
  673. else
  674. test_pass("HMAC-SHA test passed!\n");
  675. #endif
  676. #ifdef WOLFSSL_SHA224
  677. if ( (ret = hmac_sha224_test()) != 0)
  678. return err_sys("HMAC-SHA224 test failed!\n", ret);
  679. else
  680. test_pass("HMAC-SHA224 test passed!\n");
  681. #endif
  682. #ifndef NO_SHA256
  683. if ( (ret = hmac_sha256_test()) != 0)
  684. return err_sys("HMAC-SHA256 test failed!\n", ret);
  685. else
  686. test_pass("HMAC-SHA256 test passed!\n");
  687. #endif
  688. #ifdef WOLFSSL_SHA384
  689. if ( (ret = hmac_sha384_test()) != 0)
  690. return err_sys("HMAC-SHA384 test failed!\n", ret);
  691. else
  692. test_pass("HMAC-SHA384 test passed!\n");
  693. #endif
  694. #ifdef WOLFSSL_SHA512
  695. if ( (ret = hmac_sha512_test()) != 0)
  696. return err_sys("HMAC-SHA512 test failed!\n", ret);
  697. else
  698. test_pass("HMAC-SHA512 test passed!\n");
  699. #endif
  700. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  701. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  702. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  703. if ( (ret = hmac_sha3_test()) != 0)
  704. return err_sys("HMAC-SHA3 test failed!\n", ret);
  705. else
  706. test_pass("HMAC-SHA3 test passed!\n");
  707. #endif
  708. #ifdef HAVE_HKDF
  709. if ( (ret = hkdf_test()) != 0)
  710. return err_sys("HMAC-KDF test failed!\n", ret);
  711. else
  712. test_pass("HMAC-KDF test passed!\n");
  713. #endif
  714. #endif /* !NO_HMAC */
  715. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  716. if ( (ret = x963kdf_test()) != 0)
  717. return err_sys("X963-KDF test failed!\n", ret);
  718. else
  719. test_pass("X963-KDF test passed!\n");
  720. #endif
  721. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  722. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  723. if ( (ret = gmac_test()) != 0)
  724. return err_sys("GMAC test failed!\n", ret);
  725. else
  726. test_pass("GMAC test passed!\n");
  727. #endif
  728. #ifndef NO_RC4
  729. if ( (ret = arc4_test()) != 0)
  730. return err_sys("ARC4 test failed!\n", ret);
  731. else
  732. test_pass("ARC4 test passed!\n");
  733. #endif
  734. #ifndef NO_HC128
  735. if ( (ret = hc128_test()) != 0)
  736. return err_sys("HC-128 test failed!\n", ret);
  737. else
  738. test_pass("HC-128 test passed!\n");
  739. #endif
  740. #ifndef NO_RABBIT
  741. if ( (ret = rabbit_test()) != 0)
  742. return err_sys("Rabbit test failed!\n", ret);
  743. else
  744. test_pass("Rabbit test passed!\n");
  745. #endif
  746. #ifdef HAVE_CHACHA
  747. if ( (ret = chacha_test()) != 0)
  748. return err_sys("Chacha test failed!\n", ret);
  749. else
  750. test_pass("Chacha test passed!\n");
  751. #endif
  752. #ifdef HAVE_POLY1305
  753. if ( (ret = poly1305_test()) != 0)
  754. return err_sys("POLY1305 test failed!\n", ret);
  755. else
  756. test_pass("POLY1305 test passed!\n");
  757. #endif
  758. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  759. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  760. return err_sys("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  761. else
  762. test_pass("ChaCha20-Poly1305 AEAD test passed!\n");
  763. #endif
  764. #ifndef NO_DES3
  765. if ( (ret = des_test()) != 0)
  766. return err_sys("DES test failed!\n", ret);
  767. else
  768. test_pass("DES test passed!\n");
  769. #endif
  770. #ifndef NO_DES3
  771. if ( (ret = des3_test()) != 0)
  772. return err_sys("DES3 test failed!\n", ret);
  773. else
  774. test_pass("DES3 test passed!\n");
  775. #endif
  776. #ifndef NO_AES
  777. if ( (ret = aes_test()) != 0)
  778. return err_sys("AES test failed!\n", ret);
  779. else
  780. test_pass("AES test passed!\n");
  781. #ifdef WOLFSSL_AES_192
  782. if ( (ret = aes192_test()) != 0)
  783. return err_sys("AES192 test failed!\n", ret);
  784. else
  785. test_pass("AES192 test passed!\n");
  786. #endif
  787. #ifdef WOLFSSL_AES_256
  788. if ( (ret = aes256_test()) != 0)
  789. return err_sys("AES256 test failed!\n", ret);
  790. else
  791. test_pass("AES256 test passed!\n");
  792. #endif
  793. #ifdef WOLFSSL_AES_OFB
  794. if ( (ret = aesofb_test()) != 0)
  795. return err_sys("AES-OFB test failed!\n", ret);
  796. else
  797. test_pass("AESOFB test passed!\n");
  798. #endif
  799. #ifdef HAVE_AESGCM
  800. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  801. if ( (ret = aesgcm_test()) != 0)
  802. return err_sys("AES-GCM test failed!\n", ret);
  803. #endif
  804. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  805. !(defined(WOLF_CRYPTO_CB) && \
  806. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  807. if ((ret = aesgcm_default_test()) != 0) {
  808. return err_sys("AES-GCM test failed!\n", ret);
  809. }
  810. #endif
  811. test_pass("AES-GCM test passed!\n");
  812. #endif
  813. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  814. if ( (ret = aesccm_test()) != 0)
  815. return err_sys("AES-CCM test failed!\n", ret);
  816. else
  817. test_pass("AES-CCM test passed!\n");
  818. #endif
  819. #ifdef HAVE_AES_KEYWRAP
  820. if ( (ret = aeskeywrap_test()) != 0)
  821. return err_sys("AES Key Wrap test failed!\n", ret);
  822. else
  823. test_pass("AES Key Wrap test passed!\n");
  824. #endif
  825. #endif
  826. #ifdef HAVE_CAMELLIA
  827. if ( (ret = camellia_test()) != 0)
  828. return err_sys("CAMELLIA test failed!\n", ret);
  829. else
  830. test_pass("CAMELLIA test passed!\n");
  831. #endif
  832. #ifdef HAVE_IDEA
  833. if ( (ret = idea_test()) != 0)
  834. return err_sys("IDEA test failed!\n", ret);
  835. else
  836. test_pass("IDEA test passed!\n");
  837. #endif
  838. #ifndef NO_RSA
  839. #ifdef WC_RSA_NO_PADDING
  840. if ( (ret = rsa_no_pad_test()) != 0)
  841. return err_sys("RSA NOPAD test failed!\n", ret);
  842. else
  843. test_pass("RSA NOPAD test passed!\n");
  844. #endif
  845. if ( (ret = rsa_test()) != 0)
  846. return err_sys("RSA test failed!\n", ret);
  847. else
  848. test_pass("RSA test passed!\n");
  849. #endif
  850. #ifndef NO_DH
  851. if ( (ret = dh_test()) != 0)
  852. return err_sys("DH test failed!\n", ret);
  853. else
  854. test_pass("DH test passed!\n");
  855. #endif
  856. #ifndef NO_DSA
  857. if ( (ret = dsa_test()) != 0)
  858. return err_sys("DSA test failed!\n", ret);
  859. else
  860. test_pass("DSA test passed!\n");
  861. #endif
  862. #ifdef WOLFCRYPT_HAVE_SRP
  863. if ( (ret = srp_test()) != 0)
  864. return err_sys("SRP test failed!\n", ret);
  865. else
  866. test_pass("SRP test passed!\n");
  867. #endif
  868. #ifndef NO_PWDBASED
  869. if ( (ret = pwdbased_test()) != 0)
  870. return err_sys("PWDBASED test failed!\n", ret);
  871. else
  872. test_pass("PWDBASED test passed!\n");
  873. #endif
  874. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  875. if ( (ret = openssl_test()) != 0)
  876. return err_sys("OPENSSL test failed!\n", ret);
  877. else
  878. test_pass("OPENSSL test passed!\n");
  879. if ( (ret = openSSL_evpMD_test()) != 0)
  880. return err_sys("OPENSSL (EVP MD) test failed!\n", ret);
  881. else
  882. test_pass("OPENSSL (EVP MD) passed!\n");
  883. if ( (ret = openssl_pkey0_test()) != 0)
  884. return err_sys("OPENSSL (PKEY0) test failed!\n", ret);
  885. else
  886. test_pass("OPENSSL (PKEY0) passed!\n");
  887. if ( (ret = openssl_pkey1_test()) != 0)
  888. return err_sys("OPENSSL (PKEY1) test failed!\n", ret);
  889. else
  890. test_pass("OPENSSL (PKEY1) passed!\n");
  891. if ( (ret = openssl_evpSig_test()) != 0)
  892. return err_sys("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  893. else
  894. test_pass("OPENSSL (EVP Sign/Verify) passed!\n");
  895. #endif
  896. #ifdef HAVE_ECC
  897. if ( (ret = ecc_test()) != 0)
  898. return err_sys("ECC test failed!\n", ret);
  899. else
  900. test_pass("ECC test passed!\n");
  901. #if defined(HAVE_ECC_ENCRYPT) && defined(WOLFSSL_AES_128)
  902. if ( (ret = ecc_encrypt_test()) != 0)
  903. return err_sys("ECC Enc test failed!\n", ret);
  904. else
  905. test_pass("ECC Enc test passed!\n");
  906. #endif
  907. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  908. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256)
  909. /* skip for ATECC508/608A, cannot import private key buffers */
  910. if ( (ret = ecc_test_buffers()) != 0)
  911. return err_sys("ECC buffer test failed!\n", ret);
  912. else
  913. test_pass("ECC buffer test passed!\n");
  914. #endif
  915. #endif
  916. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  917. !defined(NO_FILESYSTEM)
  918. if ( (ret = cert_test()) != 0)
  919. return err_sys("CERT test failed!\n", ret);
  920. else
  921. test_pass("CERT test passed!\n");
  922. #endif
  923. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  924. !defined(NO_FILESYSTEM)
  925. if ( (ret = certext_test()) != 0)
  926. return err_sys("CERT EXT test failed!\n", ret);
  927. else
  928. test_pass("CERT EXT test passed!\n");
  929. #endif
  930. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  931. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  932. if ( (ret = decodedCertCache_test()) != 0)
  933. return err_sys("DECODED CERT CACHE test failed!\n", ret);
  934. else
  935. test_pass("DECODED CERT CACHE test passed!\n");
  936. #endif
  937. #ifdef HAVE_CURVE25519
  938. if ( (ret = curve25519_test()) != 0)
  939. return err_sys("CURVE25519 test failed!\n", ret);
  940. else
  941. test_pass("CURVE25519 test passed!\n");
  942. #endif
  943. #ifdef HAVE_ED25519
  944. if ( (ret = ed25519_test()) != 0)
  945. return err_sys("ED25519 test failed!\n", ret);
  946. else
  947. test_pass("ED25519 test passed!\n");
  948. #endif
  949. #ifdef HAVE_CURVE448
  950. if ( (ret = curve448_test()) != 0)
  951. return err_sys("CURVE448 test failed!\n", ret);
  952. else
  953. test_pass("CURVE448 test passed!\n");
  954. #endif
  955. #ifdef HAVE_ED448
  956. if ( (ret = ed448_test()) != 0)
  957. return err_sys("ED448 test failed!\n", ret);
  958. else
  959. test_pass("ED448 test passed!\n");
  960. #endif
  961. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  962. if ( (ret = cmac_test()) != 0)
  963. return err_sys("CMAC test failed!\n", ret);
  964. else
  965. test_pass("CMAC test passed!\n");
  966. #endif
  967. #ifdef HAVE_LIBZ
  968. if ( (ret = compress_test()) != 0)
  969. return err_sys("COMPRESS test failed!\n", ret);
  970. else
  971. test_pass("COMPRESS test passed!\n");
  972. #endif
  973. #ifdef HAVE_PKCS7
  974. #ifndef NO_PKCS7_ENCRYPTED_DATA
  975. if ( (ret = pkcs7encrypted_test()) != 0)
  976. return err_sys("PKCS7encrypted test failed!\n", ret);
  977. else
  978. test_pass("PKCS7encrypted test passed!\n");
  979. #endif
  980. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  981. if ( (ret = pkcs7compressed_test()) != 0)
  982. return err_sys("PKCS7compressed test failed!\n", ret);
  983. else
  984. test_pass("PKCS7compressed test passed!\n");
  985. #endif
  986. if ( (ret = pkcs7signed_test()) != 0)
  987. return err_sys("PKCS7signed test failed!\n", ret);
  988. else
  989. test_pass("PKCS7signed test passed!\n");
  990. if ( (ret = pkcs7enveloped_test()) != 0)
  991. return err_sys("PKCS7enveloped test failed!\n", ret);
  992. else
  993. test_pass("PKCS7enveloped test passed!\n");
  994. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  995. if ( (ret = pkcs7authenveloped_test()) != 0)
  996. return err_sys("PKCS7authenveloped test failed!\n", ret);
  997. else
  998. test_pass("PKCS7authenveloped test passed!\n");
  999. #endif
  1000. #endif
  1001. #ifdef HAVE_VALGRIND
  1002. if ( (ret = mp_test()) != 0)
  1003. return err_sys("mp test failed!\n", ret);
  1004. else
  1005. test_pass("mp test passed!\n");
  1006. #endif
  1007. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  1008. if ( (ret = prime_test()) != 0)
  1009. return err_sys("prime test failed!\n", ret);
  1010. else
  1011. test_pass("prime test passed!\n");
  1012. #endif
  1013. #if defined(ASN_BER_TO_DER) && \
  1014. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  1015. defined(OPENSSL_EXTRA_X509_SMALL))
  1016. if ( (ret = berder_test()) != 0)
  1017. return err_sys("ber-der test failed!\n", ret);
  1018. else
  1019. test_pass("ber-der test passed!\n");
  1020. #endif
  1021. if ( (ret = logging_test()) != 0)
  1022. return err_sys("logging test failed!\n", ret);
  1023. else
  1024. test_pass("logging test passed!\n");
  1025. if ( (ret = mutex_test()) != 0)
  1026. return err_sys("mutex test failed!\n", ret);
  1027. else
  1028. test_pass("mutex test passed!\n");
  1029. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  1030. if ( (ret = memcb_test()) != 0)
  1031. return err_sys("memcb test failed!\n", ret);
  1032. else
  1033. test_pass("memcb test passed!\n");
  1034. #endif
  1035. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  1036. if ( (ret = blob_test()) != 0)
  1037. return err_sys("blob test failed!\n", ret);
  1038. else
  1039. test_pass("blob test passed!\n");
  1040. #endif
  1041. #if defined(WOLF_CRYPTO_CB) && \
  1042. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  1043. if ( (ret = cryptocb_test()) != 0)
  1044. return err_sys("crypto callback test failed!\n", ret);
  1045. else
  1046. test_pass("crypto callback test passed!\n");
  1047. #endif
  1048. #ifdef WOLFSSL_CERT_PIV
  1049. if ( (ret = certpiv_test()) != 0)
  1050. return err_sys("cert piv test failed!\n", ret);
  1051. else
  1052. test_pass("cert piv test passed!\n");
  1053. #endif
  1054. #ifdef WOLF_CRYPTO_CB
  1055. #ifdef HAVE_INTEL_QA_SYNC
  1056. wc_CryptoCb_CleanupIntelQa(&devId);
  1057. #endif
  1058. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1059. wc_CryptoCb_CleanupOcteon(&devId);
  1060. #endif
  1061. #endif
  1062. #ifdef WOLFSSL_ASYNC_CRYPT
  1063. wolfAsync_DevClose(&devId);
  1064. #endif
  1065. /* cleanup the thread if fixed point cache is enabled and have thread local */
  1066. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  1067. wc_ecc_fp_free();
  1068. #endif
  1069. if (args)
  1070. ((func_args*)args)->return_code = ret;
  1071. test_pass("Test complete\n");
  1072. EXIT_TEST(ret);
  1073. }
  1074. #ifndef NO_MAIN_DRIVER
  1075. /* so overall tests can pull in test function */
  1076. #ifdef WOLFSSL_ESPIDF
  1077. void app_main( )
  1078. #else
  1079. int main(int argc, char** argv)
  1080. #endif
  1081. {
  1082. int ret;
  1083. func_args args;
  1084. #ifdef WOLFSSL_ESPIDF
  1085. /* set dummy wallclock time. */
  1086. struct timeval utctime;
  1087. struct timezone tz;
  1088. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1089. utctime.tv_usec = 0;
  1090. tz.tz_minuteswest = 0;
  1091. tz.tz_dsttime = 0;
  1092. settimeofday(&utctime, &tz);
  1093. #endif
  1094. #ifdef WOLFSSL_APACHE_MYNEWT
  1095. #ifdef ARCH_sim
  1096. mcu_sim_parse_args(argc, argv);
  1097. #endif
  1098. sysinit();
  1099. /* set dummy wallclock time. */
  1100. struct os_timeval utctime;
  1101. struct os_timezone tz;
  1102. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1103. utctime.tv_usec = 0;
  1104. tz.tz_minuteswest = 0;
  1105. tz.tz_dsttime = 0;
  1106. os_settimeofday(&utctime, &tz);
  1107. #endif
  1108. #ifdef HAVE_WNR
  1109. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0) {
  1110. err_sys("Whitewood netRandom global config failed", -1001);
  1111. return -1002;
  1112. }
  1113. #endif
  1114. #ifndef WOLFSSL_ESPIDF
  1115. args.argc = argc;
  1116. args.argv = argv;
  1117. #endif
  1118. if ((ret = wolfCrypt_Init()) != 0) {
  1119. printf("wolfCrypt_Init failed %d\n", ret);
  1120. err_sys("Error with wolfCrypt_Init!\n", -1003);
  1121. }
  1122. #ifdef HAVE_STACK_SIZE
  1123. StackSizeCheck(&args, wolfcrypt_test);
  1124. #else
  1125. wolfcrypt_test(&args);
  1126. #endif
  1127. if ((ret = wolfCrypt_Cleanup()) != 0) {
  1128. printf("wolfCrypt_Cleanup failed %d\n", ret);
  1129. err_sys("Error with wolfCrypt_Cleanup!\n", -1004);
  1130. }
  1131. #ifdef HAVE_WNR
  1132. if (wc_FreeNetRandom() < 0)
  1133. err_sys("Failed to free netRandom context", -1005);
  1134. #endif /* HAVE_WNR */
  1135. #ifndef WOLFSSL_ESPIDF
  1136. return args.return_code;
  1137. #endif
  1138. }
  1139. #endif /* NO_MAIN_DRIVER */
  1140. /* helper to save DER, convert to PEM and save PEM */
  1141. #if !defined(NO_ASN) && (defined(HAVE_ECC) || \
  1142. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN))))
  1143. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1144. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, fD, fP, pT, eB)
  1145. #else
  1146. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, NULL, NULL, pT, eB)
  1147. #endif
  1148. static int _SaveDerAndPem(const byte* der, int derSz,
  1149. const char* fileDer, const char* filePem, int pemType, int errBase)
  1150. {
  1151. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1152. int ret;
  1153. XFILE derFile;
  1154. derFile = XFOPEN(fileDer, "wb");
  1155. if (!derFile) {
  1156. return errBase + 0;
  1157. }
  1158. ret = (int)XFWRITE(der, 1, derSz, derFile);
  1159. XFCLOSE(derFile);
  1160. if (ret != derSz) {
  1161. return errBase + 1;
  1162. }
  1163. #endif
  1164. #ifdef WOLFSSL_DER_TO_PEM
  1165. if (filePem) {
  1166. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1167. XFILE pemFile;
  1168. #endif
  1169. byte* pem;
  1170. int pemSz;
  1171. /* calculate PEM size */
  1172. pemSz = wc_DerToPem(der, derSz, NULL, 0, pemType);
  1173. if (pemSz < 0) {
  1174. return pemSz;
  1175. }
  1176. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1177. if (pem == NULL) {
  1178. return MEMORY_E;
  1179. }
  1180. /* Convert to PEM */
  1181. pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
  1182. if (pemSz < 0) {
  1183. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1184. return errBase + 2;
  1185. }
  1186. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1187. pemFile = XFOPEN(filePem, "wb");
  1188. if (!pemFile) {
  1189. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1190. return errBase + 3;
  1191. }
  1192. ret = (int)XFWRITE(pem, 1, pemSz, pemFile);
  1193. XFCLOSE(pemFile);
  1194. if (ret != pemSz) {
  1195. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1196. return errBase + 4;
  1197. }
  1198. #endif
  1199. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1200. }
  1201. #endif /* WOLFSSL_DER_TO_PEM */
  1202. /* suppress unused variable warnings */
  1203. (void)der;
  1204. (void)derSz;
  1205. (void)filePem;
  1206. (void)fileDer;
  1207. (void)pemType;
  1208. (void)errBase;
  1209. return 0;
  1210. }
  1211. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  1212. int error_test(void)
  1213. {
  1214. const char* errStr;
  1215. char out[WOLFSSL_MAX_ERROR_SZ];
  1216. const char* unknownStr = wc_GetErrorString(0);
  1217. #ifdef NO_ERROR_STRINGS
  1218. /* Ensure a valid error code's string matches an invalid code's.
  1219. * The string is that error strings are not available.
  1220. */
  1221. errStr = wc_GetErrorString(OPEN_RAN_E);
  1222. wc_ErrorString(OPEN_RAN_E, out);
  1223. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  1224. return -1100;
  1225. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  1226. return -1101;
  1227. #else
  1228. int i;
  1229. int j = 0;
  1230. /* Values that are not or no longer error codes. */
  1231. int missing[] = { -122, -123, -124, -127, -128, -129,
  1232. -163, -164, -165, -166, -167, -168, -169,
  1233. -179, -233,
  1234. 0 };
  1235. /* Check that all errors have a string and it's the same through the two
  1236. * APIs. Check that the values that are not errors map to the unknown
  1237. * string.
  1238. */
  1239. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  1240. errStr = wc_GetErrorString(i);
  1241. wc_ErrorString(i, out);
  1242. if (i != missing[j]) {
  1243. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) == 0)
  1244. return -1102;
  1245. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) == 0)
  1246. return -1103;
  1247. if (XSTRNCMP(errStr, out, XSTRLEN(errStr)) != 0)
  1248. return -1104;
  1249. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ)
  1250. return -1105;
  1251. }
  1252. else {
  1253. j++;
  1254. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  1255. return -1106;
  1256. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  1257. return -1107;
  1258. }
  1259. }
  1260. /* Check if the next possible value has been given a string. */
  1261. errStr = wc_GetErrorString(i);
  1262. wc_ErrorString(i, out);
  1263. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  1264. return -1108;
  1265. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  1266. return -1109;
  1267. #endif
  1268. return 0;
  1269. }
  1270. #ifndef NO_CODING
  1271. int base64_test(void)
  1272. {
  1273. int ret;
  1274. const byte good[] = "A+Gd\0\0\0";
  1275. const byte goodEnd[] = "A+Gd \r\n";
  1276. byte out[128];
  1277. word32 outLen;
  1278. #ifdef WOLFSSL_BASE64_ENCODE
  1279. byte data[3];
  1280. word32 dataLen;
  1281. byte longData[79] = { 0 };
  1282. const byte symbols[] = "+/A=";
  1283. #endif
  1284. const byte badSmall[] = "AAA Gdj=";
  1285. const byte badLarge[] = "AAA~Gdj=";
  1286. const byte badEOL[] = "A+Gd AA";
  1287. int i;
  1288. /* Good Base64 encodings. */
  1289. outLen = sizeof(out);
  1290. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1291. if (ret != 0)
  1292. return -1200;
  1293. outLen = sizeof(out);
  1294. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  1295. if (ret != 0)
  1296. return -1201;
  1297. /* Bad parameters. */
  1298. outLen = 1;
  1299. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1300. if (ret != BAD_FUNC_ARG)
  1301. return -1202;
  1302. outLen = sizeof(out);
  1303. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  1304. if (ret != ASN_INPUT_E)
  1305. return -1203;
  1306. /* Bad character at each offset 0-3. */
  1307. for (i = 0; i < 4; i++) {
  1308. outLen = sizeof(out);
  1309. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  1310. if (ret != ASN_INPUT_E)
  1311. return -1204 - i;
  1312. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  1313. if (ret != ASN_INPUT_E)
  1314. return -1214 - i;
  1315. }
  1316. #ifdef WOLFSSL_BASE64_ENCODE
  1317. /* Decode and encode all symbols - non-alphanumeric. */
  1318. dataLen = sizeof(data);
  1319. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  1320. if (ret != 0)
  1321. return -1224;
  1322. outLen = sizeof(out);
  1323. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  1324. if (ret != LENGTH_ONLY_E)
  1325. return -1225;
  1326. outLen = sizeof(out);
  1327. ret = Base64_Encode(data, dataLen, out, &outLen);
  1328. if (ret != 0)
  1329. return -1226;
  1330. outLen = 7;
  1331. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1332. if (ret != BUFFER_E)
  1333. return -1227;
  1334. outLen = sizeof(out);
  1335. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  1336. if (ret != LENGTH_ONLY_E)
  1337. return -1228;
  1338. outLen = sizeof(out);
  1339. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1340. if (ret != 0)
  1341. return -1229;
  1342. outLen = sizeof(out);
  1343. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  1344. if (ret != 0)
  1345. return -1230;
  1346. /* Data that results in an encoding longer than one line. */
  1347. outLen = sizeof(out);
  1348. dataLen = sizeof(longData);
  1349. ret = Base64_Encode(longData, dataLen, out, &outLen);
  1350. if (ret != 0)
  1351. return -1231;
  1352. outLen = sizeof(out);
  1353. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  1354. if (ret != 0)
  1355. return -1232;
  1356. outLen = sizeof(out);
  1357. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  1358. if (ret != 0)
  1359. return -1233;
  1360. #endif
  1361. return 0;
  1362. }
  1363. #ifdef WOLFSSL_BASE16
  1364. int base16_test(void)
  1365. {
  1366. int ret;
  1367. const byte testData[] = "SomeDataToEncode\n";
  1368. const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  1369. byte encoded[40];
  1370. word32 encodedLen;
  1371. byte plain[40];
  1372. word32 len;
  1373. /* length returned includes null termination */
  1374. encodedLen = sizeof(encoded);
  1375. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  1376. if (ret != 0)
  1377. return -1300;
  1378. len = (word32)XSTRLEN((char*)encoded);
  1379. if (len != encodedLen - 1)
  1380. return -1301;
  1381. len = sizeof(plain);
  1382. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  1383. if (ret != 0)
  1384. return -1302;
  1385. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  1386. return -1303;
  1387. if (encodedLen != sizeof(encodedTestData) ||
  1388. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  1389. return -1304;
  1390. }
  1391. return 0;
  1392. }
  1393. #endif /* WOLFSSL_BASE16 */
  1394. #endif /* !NO_CODING */
  1395. #ifndef NO_ASN
  1396. int asn_test(void)
  1397. {
  1398. int ret;
  1399. /* ASN1 encoded date buffer */
  1400. const byte dateBuf[] = {0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  1401. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a};
  1402. byte format;
  1403. int length;
  1404. const byte* datePart;
  1405. #ifndef NO_ASN_TIME
  1406. struct tm timearg;
  1407. time_t now;
  1408. #endif
  1409. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  1410. &length);
  1411. if (ret != 0)
  1412. return -1400;
  1413. #ifndef NO_ASN_TIME
  1414. /* Parameter Validation tests. */
  1415. if (wc_GetTime(NULL, sizeof(now)) != BAD_FUNC_ARG)
  1416. return -1401;
  1417. if (wc_GetTime(&now, 0) != BUFFER_E)
  1418. return -1402;
  1419. now = 0;
  1420. if (wc_GetTime(&now, sizeof(now)) != 0) {
  1421. return -1403;
  1422. }
  1423. if (now == 0) {
  1424. printf("RTC/Time not set!\n");
  1425. return -1404;
  1426. }
  1427. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  1428. if (ret != 0)
  1429. return -1405;
  1430. #endif /* !NO_ASN_TIME */
  1431. return 0;
  1432. }
  1433. #endif /* !NO_ASN */
  1434. #ifdef WOLFSSL_MD2
  1435. int md2_test(void)
  1436. {
  1437. Md2 md2;
  1438. byte hash[MD2_DIGEST_SIZE];
  1439. testVector a, b, c, d, e, f, g;
  1440. testVector test_md2[7];
  1441. int times = sizeof(test_md2) / sizeof(testVector), i;
  1442. a.input = "";
  1443. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  1444. "\x27\x73";
  1445. a.inLen = XSTRLEN(a.input);
  1446. a.outLen = MD2_DIGEST_SIZE;
  1447. b.input = "a";
  1448. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  1449. "\xb5\xd1";
  1450. b.inLen = XSTRLEN(b.input);
  1451. b.outLen = MD2_DIGEST_SIZE;
  1452. c.input = "abc";
  1453. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  1454. "\xd6\xbb";
  1455. c.inLen = XSTRLEN(c.input);
  1456. c.outLen = MD2_DIGEST_SIZE;
  1457. d.input = "message digest";
  1458. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  1459. "\x06\xb0";
  1460. d.inLen = XSTRLEN(d.input);
  1461. d.outLen = MD2_DIGEST_SIZE;
  1462. e.input = "abcdefghijklmnopqrstuvwxyz";
  1463. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  1464. "\x94\x0b";
  1465. e.inLen = XSTRLEN(e.input);
  1466. e.outLen = MD2_DIGEST_SIZE;
  1467. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1468. "6789";
  1469. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  1470. "\x38\xcd";
  1471. f.inLen = XSTRLEN(f.input);
  1472. f.outLen = MD2_DIGEST_SIZE;
  1473. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1474. "9012345678901234567890";
  1475. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  1476. "\xef\xd8";
  1477. g.inLen = XSTRLEN(g.input);
  1478. g.outLen = MD2_DIGEST_SIZE;
  1479. test_md2[0] = a;
  1480. test_md2[1] = b;
  1481. test_md2[2] = c;
  1482. test_md2[3] = d;
  1483. test_md2[4] = e;
  1484. test_md2[5] = f;
  1485. test_md2[6] = g;
  1486. wc_InitMd2(&md2);
  1487. for (i = 0; i < times; ++i) {
  1488. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  1489. wc_Md2Final(&md2, hash);
  1490. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  1491. return -1500 - i;
  1492. }
  1493. return 0;
  1494. }
  1495. #endif
  1496. #ifndef NO_MD5
  1497. int md5_test(void)
  1498. {
  1499. int ret = 0;
  1500. wc_Md5 md5, md5Copy;
  1501. byte hash[WC_MD5_DIGEST_SIZE];
  1502. byte hashcopy[WC_MD5_DIGEST_SIZE];
  1503. testVector a, b, c, d, e, f;
  1504. testVector test_md5[6];
  1505. int times = sizeof(test_md5) / sizeof(testVector), i;
  1506. a.input = "";
  1507. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  1508. "\x7e";
  1509. a.inLen = XSTRLEN(a.input);
  1510. a.outLen = WC_MD5_DIGEST_SIZE;
  1511. b.input = "abc";
  1512. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  1513. "\x72";
  1514. b.inLen = XSTRLEN(b.input);
  1515. b.outLen = WC_MD5_DIGEST_SIZE;
  1516. c.input = "message digest";
  1517. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  1518. "\xd0";
  1519. c.inLen = XSTRLEN(c.input);
  1520. c.outLen = WC_MD5_DIGEST_SIZE;
  1521. d.input = "abcdefghijklmnopqrstuvwxyz";
  1522. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  1523. "\x3b";
  1524. d.inLen = XSTRLEN(d.input);
  1525. d.outLen = WC_MD5_DIGEST_SIZE;
  1526. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1527. "6789";
  1528. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  1529. "\x9f";
  1530. e.inLen = XSTRLEN(e.input);
  1531. e.outLen = WC_MD5_DIGEST_SIZE;
  1532. f.input = "1234567890123456789012345678901234567890123456789012345678"
  1533. "9012345678901234567890";
  1534. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  1535. "\x7a";
  1536. f.inLen = XSTRLEN(f.input);
  1537. f.outLen = WC_MD5_DIGEST_SIZE;
  1538. test_md5[0] = a;
  1539. test_md5[1] = b;
  1540. test_md5[2] = c;
  1541. test_md5[3] = d;
  1542. test_md5[4] = e;
  1543. test_md5[5] = f;
  1544. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  1545. if (ret != 0)
  1546. return -1600;
  1547. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  1548. if (ret != 0) {
  1549. wc_Md5Free(&md5);
  1550. return -1601;
  1551. }
  1552. for (i = 0; i < times; ++i) {
  1553. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  1554. (word32)test_md5[i].inLen);
  1555. if (ret != 0)
  1556. ERROR_OUT(-1602 - i, exit);
  1557. ret = wc_Md5GetHash(&md5, hashcopy);
  1558. if (ret != 0)
  1559. ERROR_OUT(-1603 - i, exit);
  1560. ret = wc_Md5Copy(&md5, &md5Copy);
  1561. if (ret != 0)
  1562. ERROR_OUT(-1604 - i, exit);
  1563. ret = wc_Md5Final(&md5, hash);
  1564. if (ret != 0)
  1565. ERROR_OUT(-1605 - i, exit);
  1566. wc_Md5Free(&md5Copy);
  1567. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  1568. ERROR_OUT(-1606 - i, exit);
  1569. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  1570. ERROR_OUT(-1607 - i, exit);
  1571. }
  1572. /* BEGIN LARGE HASH TEST */ {
  1573. byte large_input[1024];
  1574. const char* large_digest =
  1575. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  1576. for (i = 0; i < (int)sizeof(large_input); i++) {
  1577. large_input[i] = (byte)(i & 0xFF);
  1578. }
  1579. times = 100;
  1580. #ifdef WOLFSSL_PIC32MZ_HASH
  1581. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  1582. #endif
  1583. for (i = 0; i < times; ++i) {
  1584. ret = wc_Md5Update(&md5, (byte*)large_input,
  1585. (word32)sizeof(large_input));
  1586. if (ret != 0)
  1587. ERROR_OUT(-1608, exit);
  1588. }
  1589. ret = wc_Md5Final(&md5, hash);
  1590. if (ret != 0)
  1591. ERROR_OUT(-1609, exit);
  1592. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  1593. ERROR_OUT(-1610, exit);
  1594. } /* END LARGE HASH TEST */
  1595. exit:
  1596. wc_Md5Free(&md5);
  1597. wc_Md5Free(&md5Copy);
  1598. return ret;
  1599. }
  1600. #endif /* NO_MD5 */
  1601. #ifndef NO_MD4
  1602. int md4_test(void)
  1603. {
  1604. Md4 md4;
  1605. byte hash[MD4_DIGEST_SIZE];
  1606. testVector a, b, c, d, e, f, g;
  1607. testVector test_md4[7];
  1608. int times = sizeof(test_md4) / sizeof(testVector), i;
  1609. a.input = "";
  1610. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  1611. "\xc0";
  1612. a.inLen = XSTRLEN(a.input);
  1613. a.outLen = MD4_DIGEST_SIZE;
  1614. b.input = "a";
  1615. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  1616. "\x24";
  1617. b.inLen = XSTRLEN(b.input);
  1618. b.outLen = MD4_DIGEST_SIZE;
  1619. c.input = "abc";
  1620. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  1621. "\x9d";
  1622. c.inLen = XSTRLEN(c.input);
  1623. c.outLen = MD4_DIGEST_SIZE;
  1624. d.input = "message digest";
  1625. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  1626. "\x4b";
  1627. d.inLen = XSTRLEN(d.input);
  1628. d.outLen = MD4_DIGEST_SIZE;
  1629. e.input = "abcdefghijklmnopqrstuvwxyz";
  1630. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  1631. "\xa9";
  1632. e.inLen = XSTRLEN(e.input);
  1633. e.outLen = MD4_DIGEST_SIZE;
  1634. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1635. "6789";
  1636. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  1637. "\xe4";
  1638. f.inLen = XSTRLEN(f.input);
  1639. f.outLen = MD4_DIGEST_SIZE;
  1640. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1641. "9012345678901234567890";
  1642. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  1643. "\x36";
  1644. g.inLen = XSTRLEN(g.input);
  1645. g.outLen = MD4_DIGEST_SIZE;
  1646. test_md4[0] = a;
  1647. test_md4[1] = b;
  1648. test_md4[2] = c;
  1649. test_md4[3] = d;
  1650. test_md4[4] = e;
  1651. test_md4[5] = f;
  1652. test_md4[6] = g;
  1653. wc_InitMd4(&md4);
  1654. for (i = 0; i < times; ++i) {
  1655. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  1656. wc_Md4Final(&md4, hash);
  1657. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  1658. return -1700 - i;
  1659. }
  1660. return 0;
  1661. }
  1662. #endif /* NO_MD4 */
  1663. #ifndef NO_SHA
  1664. int sha_test(void)
  1665. {
  1666. int ret = 0;
  1667. wc_Sha sha, shaCopy;
  1668. byte hash[WC_SHA_DIGEST_SIZE];
  1669. byte hashcopy[WC_SHA_DIGEST_SIZE];
  1670. testVector a, b, c, d, e;
  1671. testVector test_sha[5];
  1672. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1673. a.input = "";
  1674. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  1675. "\x90\xaf\xd8\x07\x09";
  1676. a.inLen = XSTRLEN(a.input);
  1677. a.outLen = WC_SHA_DIGEST_SIZE;
  1678. b.input = "abc";
  1679. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  1680. "\x6C\x9C\xD0\xD8\x9D";
  1681. b.inLen = XSTRLEN(b.input);
  1682. b.outLen = WC_SHA_DIGEST_SIZE;
  1683. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1684. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  1685. "\xE5\xE5\x46\x70\xF1";
  1686. c.inLen = XSTRLEN(c.input);
  1687. c.outLen = WC_SHA_DIGEST_SIZE;
  1688. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1689. "aaaaaa";
  1690. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  1691. "\x2A\x25\xEC\x64\x4D";
  1692. d.inLen = XSTRLEN(d.input);
  1693. d.outLen = WC_SHA_DIGEST_SIZE;
  1694. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1695. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1696. "aaaaaaaaaa";
  1697. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  1698. "\x53\x99\x5E\x26\xA0";
  1699. e.inLen = XSTRLEN(e.input);
  1700. e.outLen = WC_SHA_DIGEST_SIZE;
  1701. test_sha[0] = a;
  1702. test_sha[1] = b;
  1703. test_sha[2] = c;
  1704. test_sha[3] = d;
  1705. test_sha[4] = e;
  1706. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  1707. if (ret != 0)
  1708. return -1800;
  1709. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  1710. if (ret != 0) {
  1711. wc_ShaFree(&sha);
  1712. return -1801;
  1713. }
  1714. for (i = 0; i < times; ++i) {
  1715. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  1716. (word32)test_sha[i].inLen);
  1717. if (ret != 0)
  1718. ERROR_OUT(-1802 - i, exit);
  1719. ret = wc_ShaGetHash(&sha, hashcopy);
  1720. if (ret != 0)
  1721. ERROR_OUT(-1803 - i, exit);
  1722. ret = wc_ShaCopy(&sha, &shaCopy);
  1723. if (ret != 0)
  1724. ERROR_OUT(-1804 - i, exit);
  1725. ret = wc_ShaFinal(&sha, hash);
  1726. if (ret != 0)
  1727. ERROR_OUT(-1805 - i, exit);
  1728. wc_ShaFree(&shaCopy);
  1729. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  1730. ERROR_OUT(-1806 - i, exit);
  1731. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  1732. ERROR_OUT(-1807 - i, exit);
  1733. }
  1734. /* BEGIN LARGE HASH TEST */ {
  1735. byte large_input[1024];
  1736. #ifdef WOLFSSL_RENESAS_TSIP
  1737. const char* large_digest =
  1738. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  1739. "\x15\x8c\x6d\xb6";
  1740. #else
  1741. const char* large_digest =
  1742. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  1743. "\xc5\xd9\x0a\xac";
  1744. #endif
  1745. for (i = 0; i < (int)sizeof(large_input); i++) {
  1746. large_input[i] = (byte)(i & 0xFF);
  1747. }
  1748. #ifdef WOLFSSL_RENESAS_TSIP
  1749. times = 20;
  1750. #else
  1751. times = 100;
  1752. #endif
  1753. #ifdef WOLFSSL_PIC32MZ_HASH
  1754. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  1755. #endif
  1756. for (i = 0; i < times; ++i) {
  1757. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  1758. (word32)sizeof(large_input));
  1759. if (ret != 0)
  1760. ERROR_OUT(-1808, exit);
  1761. }
  1762. ret = wc_ShaFinal(&sha, hash);
  1763. if (ret != 0)
  1764. ERROR_OUT(-1809, exit);
  1765. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  1766. ERROR_OUT(-1810, exit);
  1767. } /* END LARGE HASH TEST */
  1768. exit:
  1769. wc_ShaFree(&sha);
  1770. wc_ShaFree(&shaCopy);
  1771. return ret;
  1772. }
  1773. #endif /* NO_SHA */
  1774. #ifdef WOLFSSL_RIPEMD
  1775. int ripemd_test(void)
  1776. {
  1777. RipeMd ripemd;
  1778. int ret;
  1779. byte hash[RIPEMD_DIGEST_SIZE];
  1780. testVector a, b, c, d;
  1781. testVector test_ripemd[4];
  1782. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  1783. a.input = "abc";
  1784. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  1785. "\xb0\x87\xf1\x5a\x0b\xfc";
  1786. a.inLen = XSTRLEN(a.input);
  1787. a.outLen = RIPEMD_DIGEST_SIZE;
  1788. b.input = "message digest";
  1789. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  1790. "\x5f\xfa\x21\x59\x5f\x36";
  1791. b.inLen = XSTRLEN(b.input);
  1792. b.outLen = RIPEMD_DIGEST_SIZE;
  1793. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1794. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  1795. "\xf4\x9a\xda\x62\xeb\x2b";
  1796. c.inLen = XSTRLEN(c.input);
  1797. c.outLen = RIPEMD_DIGEST_SIZE;
  1798. d.input = "12345678901234567890123456789012345678901234567890123456"
  1799. "789012345678901234567890";
  1800. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  1801. "\x82\xbf\x63\x32\x6b\xfb";
  1802. d.inLen = XSTRLEN(d.input);
  1803. d.outLen = RIPEMD_DIGEST_SIZE;
  1804. test_ripemd[0] = a;
  1805. test_ripemd[1] = b;
  1806. test_ripemd[2] = c;
  1807. test_ripemd[3] = d;
  1808. ret = wc_InitRipeMd(&ripemd);
  1809. if (ret != 0) {
  1810. return -1900;
  1811. }
  1812. for (i = 0; i < times; ++i) {
  1813. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  1814. (word32)test_ripemd[i].inLen);
  1815. if (ret != 0) {
  1816. return -1901 - i;
  1817. }
  1818. ret = wc_RipeMdFinal(&ripemd, hash);
  1819. if (ret != 0) {
  1820. return -1911 - i;
  1821. }
  1822. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  1823. return -1921 - i;
  1824. }
  1825. return 0;
  1826. }
  1827. #endif /* WOLFSSL_RIPEMD */
  1828. #ifdef HAVE_BLAKE2
  1829. #define BLAKE2B_TESTS 3
  1830. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  1831. {
  1832. {
  1833. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  1834. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  1835. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  1836. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  1837. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  1838. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  1839. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  1840. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  1841. },
  1842. {
  1843. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  1844. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  1845. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  1846. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  1847. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  1848. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  1849. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  1850. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  1851. },
  1852. {
  1853. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  1854. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  1855. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  1856. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  1857. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  1858. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  1859. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  1860. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  1861. }
  1862. };
  1863. int blake2b_test(void)
  1864. {
  1865. Blake2b b2b;
  1866. byte digest[64];
  1867. byte input[64];
  1868. int i, ret;
  1869. for (i = 0; i < (int)sizeof(input); i++)
  1870. input[i] = (byte)i;
  1871. for (i = 0; i < BLAKE2B_TESTS; i++) {
  1872. ret = wc_InitBlake2b(&b2b, 64);
  1873. if (ret != 0)
  1874. return -2000 - i;
  1875. ret = wc_Blake2bUpdate(&b2b, input, i);
  1876. if (ret != 0)
  1877. return -2010 - 1;
  1878. ret = wc_Blake2bFinal(&b2b, digest, 64);
  1879. if (ret != 0)
  1880. return -2020 - i;
  1881. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  1882. return -2030 - i;
  1883. }
  1884. }
  1885. return 0;
  1886. }
  1887. #endif /* HAVE_BLAKE2 */
  1888. #ifdef HAVE_BLAKE2S
  1889. #define BLAKE2S_TESTS 3
  1890. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  1891. {
  1892. {
  1893. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  1894. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  1895. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  1896. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  1897. },
  1898. {
  1899. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  1900. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  1901. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  1902. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  1903. },
  1904. {
  1905. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  1906. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  1907. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  1908. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  1909. }
  1910. };
  1911. int blake2s_test(void)
  1912. {
  1913. Blake2s b2s;
  1914. byte digest[32];
  1915. byte input[64];
  1916. int i, ret;
  1917. for (i = 0; i < (int)sizeof(input); i++)
  1918. input[i] = (byte)i;
  1919. for (i = 0; i < BLAKE2S_TESTS; i++) {
  1920. ret = wc_InitBlake2s(&b2s, 32);
  1921. if (ret != 0)
  1922. return -2100 - i;
  1923. ret = wc_Blake2sUpdate(&b2s, input, i);
  1924. if (ret != 0)
  1925. return -2110 - 1;
  1926. ret = wc_Blake2sFinal(&b2s, digest, 32);
  1927. if (ret != 0)
  1928. return -2120 - i;
  1929. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  1930. return -2130 - i;
  1931. }
  1932. }
  1933. return 0;
  1934. }
  1935. #endif /* HAVE_BLAKE2S */
  1936. #ifdef WOLFSSL_SHA224
  1937. int sha224_test(void)
  1938. {
  1939. wc_Sha224 sha, shaCopy;
  1940. byte hash[WC_SHA224_DIGEST_SIZE];
  1941. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  1942. int ret = 0;
  1943. testVector a, b, c;
  1944. testVector test_sha[3];
  1945. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1946. a.input = "";
  1947. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  1948. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  1949. a.inLen = XSTRLEN(a.input);
  1950. a.outLen = WC_SHA224_DIGEST_SIZE;
  1951. b.input = "abc";
  1952. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  1953. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  1954. b.inLen = XSTRLEN(b.input);
  1955. b.outLen = WC_SHA224_DIGEST_SIZE;
  1956. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1957. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  1958. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  1959. c.inLen = XSTRLEN(c.input);
  1960. c.outLen = WC_SHA224_DIGEST_SIZE;
  1961. test_sha[0] = a;
  1962. test_sha[1] = b;
  1963. test_sha[2] = c;
  1964. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  1965. if (ret != 0)
  1966. return -2200;
  1967. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  1968. if (ret != 0) {
  1969. wc_Sha224Free(&sha);
  1970. return -2201;
  1971. }
  1972. for (i = 0; i < times; ++i) {
  1973. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  1974. (word32)test_sha[i].inLen);
  1975. if (ret != 0)
  1976. ERROR_OUT(-2202 - i, exit);
  1977. ret = wc_Sha224GetHash(&sha, hashcopy);
  1978. if (ret != 0)
  1979. ERROR_OUT(-2203 - i, exit);
  1980. ret = wc_Sha224Copy(&sha, &shaCopy);
  1981. if (ret != 0)
  1982. ERROR_OUT(-2204 - i, exit);
  1983. ret = wc_Sha224Final(&sha, hash);
  1984. if (ret != 0)
  1985. ERROR_OUT(-2205 - i, exit);
  1986. wc_Sha224Free(&shaCopy);
  1987. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  1988. ERROR_OUT(-2206 - i, exit);
  1989. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  1990. ERROR_OUT(-2207 - i, exit);
  1991. }
  1992. exit:
  1993. wc_Sha224Free(&sha);
  1994. wc_Sha224Free(&shaCopy);
  1995. return ret;
  1996. }
  1997. #endif
  1998. #ifndef NO_SHA256
  1999. int sha256_test(void)
  2000. {
  2001. wc_Sha256 sha, shaCopy;
  2002. byte hash[WC_SHA256_DIGEST_SIZE];
  2003. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  2004. int ret = 0;
  2005. testVector a, b, c;
  2006. testVector test_sha[3];
  2007. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2008. a.input = "";
  2009. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  2010. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  2011. "\xb8\x55";
  2012. a.inLen = XSTRLEN(a.input);
  2013. a.outLen = WC_SHA256_DIGEST_SIZE;
  2014. b.input = "abc";
  2015. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  2016. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  2017. "\x15\xAD";
  2018. b.inLen = XSTRLEN(b.input);
  2019. b.outLen = WC_SHA256_DIGEST_SIZE;
  2020. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2021. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  2022. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  2023. "\x06\xC1";
  2024. c.inLen = XSTRLEN(c.input);
  2025. c.outLen = WC_SHA256_DIGEST_SIZE;
  2026. test_sha[0] = a;
  2027. test_sha[1] = b;
  2028. test_sha[2] = c;
  2029. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  2030. if (ret != 0)
  2031. return -2300;
  2032. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  2033. if (ret != 0) {
  2034. wc_Sha256Free(&sha);
  2035. return -2301;
  2036. }
  2037. for (i = 0; i < times; ++i) {
  2038. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  2039. (word32)test_sha[i].inLen);
  2040. if (ret != 0) {
  2041. ERROR_OUT(-2302 - i, exit);
  2042. }
  2043. ret = wc_Sha256GetHash(&sha, hashcopy);
  2044. if (ret != 0)
  2045. ERROR_OUT(-2303 - i, exit);
  2046. ret = wc_Sha256Copy(&sha, &shaCopy);
  2047. if (ret != 0)
  2048. ERROR_OUT(-2304 - i, exit);
  2049. ret = wc_Sha256Final(&sha, hash);
  2050. if (ret != 0)
  2051. ERROR_OUT(-2305 - i, exit);
  2052. wc_Sha256Free(&shaCopy);
  2053. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  2054. ERROR_OUT(-2306 - i, exit);
  2055. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0)
  2056. ERROR_OUT(-2307 - i, exit);
  2057. }
  2058. /* BEGIN LARGE HASH TEST */ {
  2059. byte large_input[1024];
  2060. #ifdef WOLFSSL_RENESAS_TSIP_CRYPT
  2061. const char* large_digest =
  2062. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  2063. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  2064. #else
  2065. const char* large_digest =
  2066. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  2067. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  2068. #endif
  2069. for (i = 0; i < (int)sizeof(large_input); i++) {
  2070. large_input[i] = (byte)(i & 0xFF);
  2071. }
  2072. #ifdef WOLFSSL_RENESAS_TSIP
  2073. times = 20;
  2074. #else
  2075. times = 100;
  2076. #endif
  2077. #ifdef WOLFSSL_PIC32MZ_HASH
  2078. wc_Sha256SizeSet(&sha, times * sizeof(large_input));
  2079. #endif
  2080. for (i = 0; i < times; ++i) {
  2081. ret = wc_Sha256Update(&sha, (byte*)large_input,
  2082. (word32)sizeof(large_input));
  2083. if (ret != 0)
  2084. ERROR_OUT(-2308, exit);
  2085. }
  2086. ret = wc_Sha256Final(&sha, hash);
  2087. if (ret != 0)
  2088. ERROR_OUT(-2309, exit);
  2089. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  2090. ERROR_OUT(-2310, exit);
  2091. } /* END LARGE HASH TEST */
  2092. exit:
  2093. wc_Sha256Free(&sha);
  2094. wc_Sha256Free(&shaCopy);
  2095. return ret;
  2096. }
  2097. #endif
  2098. #ifdef WOLFSSL_SHA512
  2099. int sha512_test(void)
  2100. {
  2101. wc_Sha512 sha, shaCopy;
  2102. byte hash[WC_SHA512_DIGEST_SIZE];
  2103. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  2104. int ret = 0;
  2105. testVector a, b, c;
  2106. testVector test_sha[3];
  2107. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2108. a.input = "";
  2109. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  2110. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  2111. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  2112. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  2113. "\xf9\x27\xda\x3e";
  2114. a.inLen = XSTRLEN(a.input);
  2115. a.outLen = WC_SHA512_DIGEST_SIZE;
  2116. b.input = "abc";
  2117. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  2118. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  2119. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  2120. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  2121. "\xa5\x4c\xa4\x9f";
  2122. b.inLen = XSTRLEN(b.input);
  2123. b.outLen = WC_SHA512_DIGEST_SIZE;
  2124. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2125. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2126. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  2127. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  2128. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  2129. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  2130. "\x87\x4b\xe9\x09";
  2131. c.inLen = XSTRLEN(c.input);
  2132. c.outLen = WC_SHA512_DIGEST_SIZE;
  2133. test_sha[0] = a;
  2134. test_sha[1] = b;
  2135. test_sha[2] = c;
  2136. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  2137. if (ret != 0)
  2138. return -2400;
  2139. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  2140. if (ret != 0) {
  2141. wc_Sha512Free(&sha);
  2142. return -2401;
  2143. }
  2144. for (i = 0; i < times; ++i) {
  2145. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  2146. (word32)test_sha[i].inLen);
  2147. if (ret != 0)
  2148. ERROR_OUT(-2402 - i, exit);
  2149. ret = wc_Sha512GetHash(&sha, hashcopy);
  2150. if (ret != 0)
  2151. ERROR_OUT(-2403 - i, exit);
  2152. ret = wc_Sha512Copy(&sha, &shaCopy);
  2153. if (ret != 0)
  2154. ERROR_OUT(-2404 - i, exit);
  2155. ret = wc_Sha512Final(&sha, hash);
  2156. if (ret != 0)
  2157. ERROR_OUT(-2405 - i, exit);
  2158. wc_Sha512Free(&shaCopy);
  2159. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  2160. ERROR_OUT(-2406 - i, exit);
  2161. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  2162. ERROR_OUT(-2407 - i, exit);
  2163. }
  2164. /* BEGIN LARGE HASH TEST */ {
  2165. byte large_input[1024];
  2166. const char* large_digest =
  2167. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  2168. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  2169. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  2170. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  2171. for (i = 0; i < (int)sizeof(large_input); i++) {
  2172. large_input[i] = (byte)(i & 0xFF);
  2173. }
  2174. times = 100;
  2175. for (i = 0; i < times; ++i) {
  2176. ret = wc_Sha512Update(&sha, (byte*)large_input,
  2177. (word32)sizeof(large_input));
  2178. if (ret != 0)
  2179. ERROR_OUT(-2408, exit);
  2180. }
  2181. ret = wc_Sha512Final(&sha, hash);
  2182. if (ret != 0)
  2183. ERROR_OUT(-2409, exit);
  2184. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  2185. ERROR_OUT(-2410, exit);
  2186. } /* END LARGE HASH TEST */
  2187. exit:
  2188. wc_Sha512Free(&sha);
  2189. wc_Sha512Free(&shaCopy);
  2190. return ret;
  2191. }
  2192. #endif
  2193. #ifdef WOLFSSL_SHA384
  2194. int sha384_test(void)
  2195. {
  2196. wc_Sha384 sha, shaCopy;
  2197. byte hash[WC_SHA384_DIGEST_SIZE];
  2198. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  2199. int ret = 0;
  2200. testVector a, b, c;
  2201. testVector test_sha[3];
  2202. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2203. a.input = "";
  2204. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  2205. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  2206. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  2207. "\x98\xb9\x5b";
  2208. a.inLen = XSTRLEN(a.input);
  2209. a.outLen = WC_SHA384_DIGEST_SIZE;
  2210. b.input = "abc";
  2211. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  2212. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  2213. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  2214. "\xc8\x25\xa7";
  2215. b.inLen = XSTRLEN(b.input);
  2216. b.outLen = WC_SHA384_DIGEST_SIZE;
  2217. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2218. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2219. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  2220. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  2221. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  2222. "\x74\x60\x39";
  2223. c.inLen = XSTRLEN(c.input);
  2224. c.outLen = WC_SHA384_DIGEST_SIZE;
  2225. test_sha[0] = a;
  2226. test_sha[1] = b;
  2227. test_sha[2] = c;
  2228. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  2229. if (ret != 0)
  2230. return -2500;
  2231. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  2232. if (ret != 0) {
  2233. wc_Sha384Free(&sha);
  2234. return -2501;
  2235. }
  2236. for (i = 0; i < times; ++i) {
  2237. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  2238. (word32)test_sha[i].inLen);
  2239. if (ret != 0)
  2240. ERROR_OUT(-2502 - i, exit);
  2241. ret = wc_Sha384GetHash(&sha, hashcopy);
  2242. if (ret != 0)
  2243. ERROR_OUT(-2503 - i, exit);
  2244. ret = wc_Sha384Copy(&sha, &shaCopy);
  2245. if (ret != 0)
  2246. ERROR_OUT(-2504 - i, exit);
  2247. ret = wc_Sha384Final(&sha, hash);
  2248. if (ret != 0)
  2249. ERROR_OUT(-2505 - i, exit);
  2250. wc_Sha384Free(&shaCopy);
  2251. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  2252. ERROR_OUT(-2506 - i, exit);
  2253. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  2254. ERROR_OUT(-2507 - i, exit);
  2255. }
  2256. /* BEGIN LARGE HASH TEST */ {
  2257. byte large_input[1024];
  2258. const char* large_digest =
  2259. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  2260. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  2261. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  2262. for (i = 0; i < (int)sizeof(large_input); i++) {
  2263. large_input[i] = (byte)(i & 0xFF);
  2264. }
  2265. times = 100;
  2266. for (i = 0; i < times; ++i) {
  2267. ret = wc_Sha384Update(&sha, (byte*)large_input,
  2268. (word32)sizeof(large_input));
  2269. if (ret != 0)
  2270. ERROR_OUT(-2508, exit);
  2271. }
  2272. ret = wc_Sha384Final(&sha, hash);
  2273. if (ret != 0)
  2274. ERROR_OUT(-2509, exit);
  2275. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  2276. ERROR_OUT(-2510, exit);
  2277. } /* END LARGE HASH TEST */
  2278. exit:
  2279. wc_Sha384Free(&sha);
  2280. wc_Sha384Free(&shaCopy);
  2281. return ret;
  2282. }
  2283. #endif /* WOLFSSL_SHA384 */
  2284. #ifdef WOLFSSL_SHA3
  2285. #ifndef WOLFSSL_NOSHA3_224
  2286. static int sha3_224_test(void)
  2287. {
  2288. wc_Sha3 sha;
  2289. byte hash[WC_SHA3_224_DIGEST_SIZE];
  2290. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  2291. testVector a, b, c;
  2292. testVector test_sha[3];
  2293. int ret = 0;
  2294. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2295. a.input = "";
  2296. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  2297. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  2298. a.inLen = XSTRLEN(a.input);
  2299. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  2300. b.input = "abc";
  2301. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  2302. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  2303. b.inLen = XSTRLEN(b.input);
  2304. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  2305. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2306. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  2307. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  2308. c.inLen = XSTRLEN(c.input);
  2309. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  2310. test_sha[0] = a;
  2311. test_sha[1] = b;
  2312. test_sha[2] = c;
  2313. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  2314. if (ret != 0)
  2315. return -2600;
  2316. for (i = 0; i < times; ++i) {
  2317. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  2318. (word32)test_sha[i].inLen);
  2319. if (ret != 0)
  2320. ERROR_OUT(-2601 - i, exit);
  2321. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  2322. if (ret != 0)
  2323. ERROR_OUT(-2602 - i, exit);
  2324. ret = wc_Sha3_224_Final(&sha, hash);
  2325. if (ret != 0)
  2326. ERROR_OUT(-2603 - i, exit);
  2327. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  2328. ERROR_OUT(-2604 - i, exit);
  2329. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  2330. ERROR_OUT(-2605 - i, exit);
  2331. }
  2332. /* BEGIN LARGE HASH TEST */ {
  2333. byte large_input[1024];
  2334. const char* large_digest =
  2335. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  2336. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  2337. for (i = 0; i < (int)sizeof(large_input); i++) {
  2338. large_input[i] = (byte)(i & 0xFF);
  2339. }
  2340. times = 100;
  2341. for (i = 0; i < times; ++i) {
  2342. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  2343. (word32)sizeof(large_input));
  2344. if (ret != 0)
  2345. ERROR_OUT(-2606, exit);
  2346. }
  2347. ret = wc_Sha3_224_Final(&sha, hash);
  2348. if (ret != 0)
  2349. ERROR_OUT(-2607, exit);
  2350. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  2351. ERROR_OUT(-2608, exit);
  2352. } /* END LARGE HASH TEST */
  2353. exit:
  2354. wc_Sha3_224_Free(&sha);
  2355. return ret;
  2356. }
  2357. #endif /* WOLFSSL_NOSHA3_224 */
  2358. #ifndef WOLFSSL_NOSHA3_256
  2359. static int sha3_256_test(void)
  2360. {
  2361. wc_Sha3 sha;
  2362. byte hash[WC_SHA3_256_DIGEST_SIZE];
  2363. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  2364. testVector a, b, c;
  2365. testVector test_sha[3];
  2366. int ret = 0;
  2367. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2368. byte large_input[1024];
  2369. const char* large_digest =
  2370. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  2371. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  2372. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2373. /* test vector with hash of empty string */
  2374. const char* Keccak256EmptyOut =
  2375. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  2376. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  2377. #endif
  2378. a.input = "";
  2379. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  2380. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  2381. "\x43\x4a";
  2382. a.inLen = XSTRLEN(a.input);
  2383. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  2384. b.input = "abc";
  2385. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  2386. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  2387. "\x15\x32";
  2388. b.inLen = XSTRLEN(b.input);
  2389. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  2390. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2391. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  2392. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  2393. "\x33\x76";
  2394. c.inLen = XSTRLEN(c.input);
  2395. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  2396. test_sha[0] = a;
  2397. test_sha[1] = b;
  2398. test_sha[2] = c;
  2399. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  2400. if (ret != 0)
  2401. return -2700;
  2402. for (i = 0; i < times; ++i) {
  2403. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  2404. (word32)test_sha[i].inLen);
  2405. if (ret != 0)
  2406. ERROR_OUT(-2701 - i, exit);
  2407. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  2408. if (ret != 0)
  2409. ERROR_OUT(-2702 - i, exit);
  2410. ret = wc_Sha3_256_Final(&sha, hash);
  2411. if (ret != 0)
  2412. ERROR_OUT(-2703 - i, exit);
  2413. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  2414. ERROR_OUT(-2704 - i, exit);
  2415. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  2416. ERROR_OUT(-2705 - i, exit);
  2417. }
  2418. /* BEGIN LARGE HASH TEST */ {
  2419. for (i = 0; i < (int)sizeof(large_input); i++) {
  2420. large_input[i] = (byte)(i & 0xFF);
  2421. }
  2422. times = 100;
  2423. for (i = 0; i < times; ++i) {
  2424. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  2425. (word32)sizeof(large_input));
  2426. if (ret != 0)
  2427. ERROR_OUT(-2706, exit);
  2428. }
  2429. ret = wc_Sha3_256_Final(&sha, hash);
  2430. if (ret != 0)
  2431. ERROR_OUT(-2707, exit);
  2432. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  2433. ERROR_OUT(-2708, exit);
  2434. } /* END LARGE HASH TEST */
  2435. /* this is a software only variant of SHA3 not supported by external hardware devices */
  2436. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2437. /* Test for Keccak256 */
  2438. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  2439. if (ret != 0) {
  2440. ERROR_OUT(-2709, exit);
  2441. }
  2442. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  2443. if (ret != 0) {
  2444. ERROR_OUT(-2710, exit);
  2445. }
  2446. ret = wc_Sha3_256_Final(&sha, hash);
  2447. if (ret != 0) {
  2448. ERROR_OUT(-2711, exit);
  2449. }
  2450. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  2451. ERROR_OUT(-2712, exit);
  2452. }
  2453. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  2454. exit:
  2455. wc_Sha3_256_Free(&sha);
  2456. return ret;
  2457. }
  2458. #endif /* WOLFSSL_NOSHA3_256 */
  2459. #ifndef WOLFSSL_NOSHA3_384
  2460. static int sha3_384_test(void)
  2461. {
  2462. wc_Sha3 sha;
  2463. byte hash[WC_SHA3_384_DIGEST_SIZE];
  2464. #ifndef NO_INTM_HASH_TEST
  2465. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  2466. #endif
  2467. testVector a, b, c;
  2468. testVector test_sha[3];
  2469. int ret;
  2470. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2471. a.input = "";
  2472. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  2473. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  2474. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  2475. "\xd5\xf0\x04";
  2476. a.inLen = XSTRLEN(a.input);
  2477. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  2478. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT)
  2479. /* NIST test vector with a length that is a multiple of 4 */
  2480. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  2481. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  2482. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  2483. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  2484. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  2485. "\x19\x87\x22";
  2486. b.inLen = XSTRLEN(b.input);
  2487. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2488. #else
  2489. b.input = "abc";
  2490. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  2491. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  2492. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  2493. "\x37\x6d\x25";
  2494. b.inLen = XSTRLEN(b.input);
  2495. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2496. #endif
  2497. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2498. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  2499. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  2500. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  2501. "\x65\x7c\x22";
  2502. c.inLen = XSTRLEN(c.input);
  2503. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  2504. #ifdef WOLFSSL_XILINX_CRYPT
  2505. test_sha[0] = b; /* hardware acc. can not handle "" string */
  2506. #else
  2507. test_sha[0] = a;
  2508. #endif
  2509. test_sha[1] = b;
  2510. test_sha[2] = c;
  2511. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  2512. if (ret != 0)
  2513. return -2800;
  2514. for (i = 0; i < times; ++i) {
  2515. ret = wc_Sha3_384_Update(&sha, (byte*)test_sha[i].input,
  2516. (word32)test_sha[i].inLen);
  2517. if (ret != 0)
  2518. ERROR_OUT(-2801 - i, exit);
  2519. #ifndef NO_INTM_HASH_TEST
  2520. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  2521. if (ret != 0)
  2522. ERROR_OUT(-2802 - i, exit);
  2523. #endif
  2524. ret = wc_Sha3_384_Final(&sha, hash);
  2525. if (ret != 0)
  2526. ERROR_OUT(-2803 - i, exit);
  2527. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  2528. ERROR_OUT(-2804 - i, exit);
  2529. #ifndef NO_INTM_HASH_TEST
  2530. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  2531. ERROR_OUT(-2805 - i, exit);
  2532. #endif
  2533. }
  2534. /* BEGIN LARGE HASH TEST */ {
  2535. byte large_input[1024];
  2536. const char* large_digest =
  2537. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  2538. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  2539. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  2540. for (i = 0; i < (int)sizeof(large_input); i++) {
  2541. large_input[i] = (byte)(i & 0xFF);
  2542. }
  2543. times = 100;
  2544. for (i = 0; i < times; ++i) {
  2545. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  2546. (word32)sizeof(large_input));
  2547. if (ret != 0)
  2548. ERROR_OUT(-2806, exit);
  2549. }
  2550. ret = wc_Sha3_384_Final(&sha, hash);
  2551. if (ret != 0)
  2552. ERROR_OUT(-2807, exit);
  2553. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  2554. ERROR_OUT(-2808, exit);
  2555. } /* END LARGE HASH TEST */
  2556. exit:
  2557. wc_Sha3_384_Free(&sha);
  2558. return ret;
  2559. }
  2560. #endif /* WOLFSSL_NOSHA3_384 */
  2561. #ifndef WOLFSSL_NOSHA3_512
  2562. static int sha3_512_test(void)
  2563. {
  2564. wc_Sha3 sha;
  2565. byte hash[WC_SHA3_512_DIGEST_SIZE];
  2566. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  2567. testVector a, b, c;
  2568. testVector test_sha[3];
  2569. int ret;
  2570. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2571. a.input = "";
  2572. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  2573. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  2574. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  2575. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  2576. "\x28\x1d\xcd\x26";
  2577. a.inLen = XSTRLEN(a.input);
  2578. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  2579. b.input = "abc";
  2580. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  2581. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  2582. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  2583. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  2584. "\x4e\xec\x53\xf0";
  2585. b.inLen = XSTRLEN(b.input);
  2586. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  2587. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2588. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  2589. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  2590. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  2591. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  2592. "\x39\xd6\xe7\x5e";
  2593. c.inLen = XSTRLEN(c.input);
  2594. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  2595. test_sha[0] = a;
  2596. test_sha[1] = b;
  2597. test_sha[2] = c;
  2598. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  2599. if (ret != 0)
  2600. return -2900;
  2601. for (i = 0; i < times; ++i) {
  2602. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  2603. (word32)test_sha[i].inLen);
  2604. if (ret != 0)
  2605. ERROR_OUT(-2901 - i, exit);
  2606. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  2607. if (ret != 0)
  2608. ERROR_OUT(-2902 - i, exit);
  2609. ret = wc_Sha3_512_Final(&sha, hash);
  2610. if (ret != 0)
  2611. ERROR_OUT(-2903 - i, exit);
  2612. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  2613. ERROR_OUT(-2904 - i, exit);
  2614. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  2615. ERROR_OUT(-2905 - i, exit);
  2616. }
  2617. /* BEGIN LARGE HASH TEST */ {
  2618. byte large_input[1024];
  2619. const char* large_digest =
  2620. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  2621. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  2622. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  2623. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  2624. for (i = 0; i < (int)sizeof(large_input); i++) {
  2625. large_input[i] = (byte)(i & 0xFF);
  2626. }
  2627. times = 100;
  2628. for (i = 0; i < times; ++i) {
  2629. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  2630. (word32)sizeof(large_input));
  2631. if (ret != 0)
  2632. ERROR_OUT(-2906, exit);
  2633. }
  2634. ret = wc_Sha3_512_Final(&sha, hash);
  2635. if (ret != 0)
  2636. ERROR_OUT(-2907, exit);
  2637. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  2638. ERROR_OUT(-2908, exit);
  2639. } /* END LARGE HASH TEST */
  2640. exit:
  2641. wc_Sha3_512_Free(&sha);
  2642. return ret;
  2643. }
  2644. #endif /* WOLFSSL_NOSHA3_512 */
  2645. int sha3_test(void)
  2646. {
  2647. int ret;
  2648. (void)ret;
  2649. #ifndef WOLFSSL_NOSHA3_224
  2650. if ((ret = sha3_224_test()) != 0)
  2651. return ret;
  2652. #endif
  2653. #ifndef WOLFSSL_NOSHA3_256
  2654. if ((ret = sha3_256_test()) != 0)
  2655. return ret;
  2656. #endif
  2657. #ifndef WOLFSSL_NOSHA3_384
  2658. if ((ret = sha3_384_test()) != 0)
  2659. return ret;
  2660. #endif
  2661. #ifndef WOLFSSL_NOSHA3_512
  2662. if ((ret = sha3_512_test()) != 0)
  2663. return ret;
  2664. #endif
  2665. return 0;
  2666. }
  2667. #endif /* WOLFSSL_SHA3 */
  2668. #ifdef WOLFSSL_SHAKE256
  2669. int shake256_test(void)
  2670. {
  2671. #ifndef WOLFSSL_NO_SHAKE256
  2672. wc_Shake sha;
  2673. byte hash[114];
  2674. testVector a, b, c;
  2675. testVector test_sha[3];
  2676. int ret = 0;
  2677. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2678. byte large_input[1024];
  2679. const char* large_digest =
  2680. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  2681. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  2682. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  2683. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  2684. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  2685. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  2686. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  2687. "\xea\x26";
  2688. a.input = "";
  2689. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  2690. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  2691. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  2692. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  2693. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  2694. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  2695. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  2696. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  2697. a.inLen = XSTRLEN(a.input);
  2698. a.outLen = sizeof(hash);
  2699. b.input = "abc";
  2700. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  2701. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  2702. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  2703. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  2704. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  2705. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  2706. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  2707. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  2708. b.inLen = XSTRLEN(b.input);
  2709. b.outLen = sizeof(hash);
  2710. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2711. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  2712. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  2713. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  2714. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  2715. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  2716. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  2717. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  2718. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  2719. c.inLen = XSTRLEN(c.input);
  2720. c.outLen = sizeof(hash);
  2721. test_sha[0] = a;
  2722. test_sha[1] = b;
  2723. test_sha[2] = c;
  2724. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  2725. if (ret != 0)
  2726. return -3100;
  2727. for (i = 0; i < times; ++i) {
  2728. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  2729. (word32)test_sha[i].inLen);
  2730. if (ret != 0)
  2731. ERROR_OUT(-3101 - i, exit);
  2732. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  2733. if (ret != 0)
  2734. ERROR_OUT(-3102 - i, exit);
  2735. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  2736. ERROR_OUT(-3103 - i, exit);
  2737. }
  2738. /* BEGIN LARGE HASH TEST */ {
  2739. for (i = 0; i < (int)sizeof(large_input); i++) {
  2740. large_input[i] = (byte)(i & 0xFF);
  2741. }
  2742. times = 100;
  2743. for (i = 0; i < times; ++i) {
  2744. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  2745. (word32)sizeof(large_input));
  2746. if (ret != 0)
  2747. ERROR_OUT(-3104, exit);
  2748. }
  2749. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  2750. if (ret != 0)
  2751. ERROR_OUT(-3105, exit);
  2752. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  2753. ERROR_OUT(-3106, exit);
  2754. } /* END LARGE HASH TEST */
  2755. exit:
  2756. wc_Shake256_Free(&sha);
  2757. return ret;
  2758. #else
  2759. return 0;
  2760. #endif
  2761. }
  2762. #endif
  2763. int hash_test(void)
  2764. {
  2765. wc_HashAlg hash;
  2766. int ret, exp_ret;
  2767. int i, j;
  2768. int digestSz;
  2769. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  2770. byte out[WC_MAX_DIGEST_SIZE];
  2771. byte hashOut[WC_MAX_DIGEST_SIZE];
  2772. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  2773. enum wc_HashType hashType;
  2774. #endif
  2775. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  2776. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  2777. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  2778. WC_HASH_TYPE_SHA3_224,
  2779. WC_HASH_TYPE_SHA3_256,
  2780. WC_HASH_TYPE_SHA3_384,
  2781. WC_HASH_TYPE_SHA3_512 };
  2782. enum wc_HashType typesNoImpl[] = {
  2783. #ifdef NO_MD5
  2784. WC_HASH_TYPE_MD5,
  2785. #endif
  2786. #ifdef NO_SHA
  2787. WC_HASH_TYPE_SHA,
  2788. #endif
  2789. #ifndef WOLFSSL_SHA224
  2790. WC_HASH_TYPE_SHA224,
  2791. #endif
  2792. #ifdef NO_SHA256
  2793. WC_HASH_TYPE_SHA256,
  2794. #endif
  2795. #ifndef WOLFSSL_SHA384
  2796. WC_HASH_TYPE_SHA384,
  2797. #endif
  2798. #ifndef WOLFSSL_SHA512
  2799. WC_HASH_TYPE_SHA512,
  2800. #endif
  2801. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  2802. WC_HASH_TYPE_SHA3_224,
  2803. #endif
  2804. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  2805. WC_HASH_TYPE_SHA3_256,
  2806. #endif
  2807. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  2808. WC_HASH_TYPE_SHA3_384,
  2809. #endif
  2810. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  2811. WC_HASH_TYPE_SHA3_512,
  2812. #endif
  2813. WC_HASH_TYPE_NONE
  2814. };
  2815. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  2816. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  2817. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  2818. WC_HASH_TYPE_BLAKE2B,
  2819. WC_HASH_TYPE_NONE };
  2820. /* Parameter Validation testing. */
  2821. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  2822. if (ret != BAD_FUNC_ARG)
  2823. return -3200;
  2824. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  2825. if (ret != BAD_FUNC_ARG)
  2826. return -3201;
  2827. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  2828. if (ret != BAD_FUNC_ARG)
  2829. return -3202;
  2830. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  2831. if (ret != BAD_FUNC_ARG)
  2832. return -3203;
  2833. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  2834. if (ret != BAD_FUNC_ARG)
  2835. return -3204;
  2836. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  2837. if (ret != BAD_FUNC_ARG)
  2838. return -3205;
  2839. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  2840. if (ret != BAD_FUNC_ARG)
  2841. return -3206;
  2842. /* Try invalid hash algorithms. */
  2843. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  2844. ret = wc_HashInit(&hash, typesBad[i]);
  2845. if (ret != BAD_FUNC_ARG)
  2846. return -3207 - i;
  2847. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  2848. if (ret != BAD_FUNC_ARG)
  2849. return -3217 - i;
  2850. ret = wc_HashFinal(&hash, typesBad[i], out);
  2851. if (ret != BAD_FUNC_ARG)
  2852. return -3227 - i;
  2853. wc_HashFree(&hash, typesBad[i]);
  2854. }
  2855. /* Try valid hash algorithms. */
  2856. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  2857. exp_ret = 0;
  2858. if (typesGood[i] == typesNoImpl[j]) {
  2859. /* Recognized but no implementation compiled in. */
  2860. exp_ret = HASH_TYPE_E;
  2861. j++;
  2862. }
  2863. ret = wc_HashInit(&hash, typesGood[i]);
  2864. if (ret != exp_ret)
  2865. return -3237 - i;
  2866. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  2867. if (ret != exp_ret)
  2868. return -3247 - i;
  2869. ret = wc_HashFinal(&hash, typesGood[i], out);
  2870. if (ret != exp_ret)
  2871. return -3257 - i;
  2872. wc_HashFree(&hash, typesGood[i]);
  2873. digestSz = wc_HashGetDigestSize(typesGood[i]);
  2874. if (exp_ret < 0 && digestSz != exp_ret)
  2875. return -3267 - i;
  2876. if (exp_ret == 0 && digestSz < 0)
  2877. return -3277 - i;
  2878. if (exp_ret == 0) {
  2879. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  2880. digestSz - 1);
  2881. if (ret != BUFFER_E)
  2882. return -3287 - i;
  2883. }
  2884. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, digestSz);
  2885. if (ret != exp_ret)
  2886. return -3297 - i;
  2887. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  2888. return -3307 -i;
  2889. ret = wc_HashGetBlockSize(typesGood[i]);
  2890. if (exp_ret < 0 && ret != exp_ret)
  2891. return -3308 - i;
  2892. if (exp_ret == 0 && ret < 0)
  2893. return -3318 - i;
  2894. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  2895. ret = wc_HashGetOID(typesGood[i]);
  2896. if (ret == BAD_FUNC_ARG ||
  2897. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  2898. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  2899. return -3328 - i;
  2900. }
  2901. hashType = wc_OidGetHash(ret);
  2902. if (exp_ret == 0 && hashType != typesGood[i])
  2903. return -3348 - i;
  2904. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  2905. }
  2906. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  2907. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  2908. if (ret != BAD_FUNC_ARG && ret != BUFFER_E)
  2909. return -3358 - i;
  2910. }
  2911. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  2912. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  2913. #ifdef WOLFSSL_MD2
  2914. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2915. return -3368;
  2916. #else
  2917. if (ret != HASH_TYPE_E)
  2918. return -3369;
  2919. #endif
  2920. hashType = wc_OidGetHash(646); /* Md2h */
  2921. #ifdef WOLFSSL_MD2
  2922. if (hashType != WC_HASH_TYPE_MD2)
  2923. return -3370;
  2924. #else
  2925. if (hashType != WC_HASH_TYPE_NONE)
  2926. return -3371;
  2927. #endif
  2928. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  2929. #ifndef NO_MD5
  2930. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2931. return -3372;
  2932. #else
  2933. if (ret != HASH_TYPE_E)
  2934. return -3373;
  2935. #endif
  2936. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  2937. if (ret != BAD_FUNC_ARG)
  2938. return -3374;
  2939. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  2940. if (ret != BAD_FUNC_ARG)
  2941. return -3375;
  2942. hashType = wc_OidGetHash(0);
  2943. if (hashType != WC_HASH_TYPE_NONE)
  2944. return -3376;
  2945. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  2946. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  2947. #ifdef WOLFSSL_MD2
  2948. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2949. return -3377;
  2950. #else
  2951. if (ret != HASH_TYPE_E)
  2952. return -3378;
  2953. #endif
  2954. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  2955. #ifdef WOLFSSL_MD2
  2956. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2957. return -3379;
  2958. #else
  2959. if (ret != HASH_TYPE_E)
  2960. return -3380;
  2961. #endif
  2962. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  2963. #ifndef NO_MD4
  2964. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2965. return -3381;
  2966. #else
  2967. if (ret != HASH_TYPE_E)
  2968. return -3382;
  2969. #endif
  2970. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  2971. #ifndef NO_MD4
  2972. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2973. return -3383;
  2974. #else
  2975. if (ret != HASH_TYPE_E)
  2976. return -3384;
  2977. #endif
  2978. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  2979. #if !defined(NO_MD5) && !defined(NO_SHA)
  2980. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2981. return -3385;
  2982. #else
  2983. if (ret != HASH_TYPE_E)
  2984. return -3386;
  2985. #endif
  2986. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  2987. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  2988. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2989. return -3387;
  2990. #else
  2991. if (ret != HASH_TYPE_E)
  2992. return -3388;
  2993. #endif
  2994. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  2995. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  2996. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2997. return -3389;
  2998. #else
  2999. if (ret != HASH_TYPE_E)
  3000. return -3390;
  3001. #endif
  3002. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  3003. if (ret != BAD_FUNC_ARG)
  3004. return -3391;
  3005. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  3006. if (ret != BAD_FUNC_ARG)
  3007. return -3392;
  3008. #ifndef NO_CERTS
  3009. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST)
  3010. ret = wc_GetCTC_HashOID(MD2);
  3011. if (ret == 0)
  3012. return -3393;
  3013. #endif
  3014. #ifndef NO_MD5
  3015. ret = wc_GetCTC_HashOID(WC_MD5);
  3016. if (ret == 0)
  3017. return -3394;
  3018. #endif
  3019. #ifndef NO_SHA
  3020. ret = wc_GetCTC_HashOID(WC_SHA);
  3021. if (ret == 0)
  3022. return -3395;
  3023. #endif
  3024. #ifdef WOLFSSL_SHA224
  3025. ret = wc_GetCTC_HashOID(WC_SHA224);
  3026. if (ret == 0)
  3027. return -3396;
  3028. #endif
  3029. #ifndef NO_SHA256
  3030. ret = wc_GetCTC_HashOID(WC_SHA256);
  3031. if (ret == 0)
  3032. return -3397;
  3033. #endif
  3034. #ifdef WOLFSSL_SHA384
  3035. ret = wc_GetCTC_HashOID(WC_SHA384);
  3036. if (ret == 0)
  3037. return -3398;
  3038. #endif
  3039. #ifdef WOLFSSL_SHA512
  3040. ret = wc_GetCTC_HashOID(WC_SHA512);
  3041. if (ret == 0)
  3042. return -3399;
  3043. #endif
  3044. ret = wc_GetCTC_HashOID(-1);
  3045. if (ret != 0)
  3046. return -3400;
  3047. #endif
  3048. return 0;
  3049. }
  3050. #if !defined(NO_HMAC) && !defined(NO_MD5)
  3051. int hmac_md5_test(void)
  3052. {
  3053. Hmac hmac;
  3054. byte hash[WC_MD5_DIGEST_SIZE];
  3055. const char* keys[]=
  3056. {
  3057. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  3058. "Jefe",
  3059. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3060. };
  3061. testVector a, b, c;
  3062. testVector test_hmac[3];
  3063. int ret;
  3064. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3065. a.input = "Hi There";
  3066. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  3067. "\x9d";
  3068. a.inLen = XSTRLEN(a.input);
  3069. a.outLen = WC_MD5_DIGEST_SIZE;
  3070. b.input = "what do ya want for nothing?";
  3071. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  3072. "\x38";
  3073. b.inLen = XSTRLEN(b.input);
  3074. b.outLen = WC_MD5_DIGEST_SIZE;
  3075. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3076. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3077. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3078. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3079. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  3080. "\xf6";
  3081. c.inLen = XSTRLEN(c.input);
  3082. c.outLen = WC_MD5_DIGEST_SIZE;
  3083. test_hmac[0] = a;
  3084. test_hmac[1] = b;
  3085. test_hmac[2] = c;
  3086. for (i = 0; i < times; ++i) {
  3087. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3088. if (i == 1) {
  3089. continue; /* cavium can't handle short keys, fips not allowed */
  3090. }
  3091. #endif
  3092. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0) {
  3093. return -3500;
  3094. }
  3095. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  3096. (word32)XSTRLEN(keys[i]));
  3097. if (ret != 0)
  3098. return -3501;
  3099. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3100. (word32)test_hmac[i].inLen);
  3101. if (ret != 0)
  3102. return -3502;
  3103. ret = wc_HmacFinal(&hmac, hash);
  3104. if (ret != 0)
  3105. return -3503;
  3106. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  3107. return -3504 - i;
  3108. wc_HmacFree(&hmac);
  3109. }
  3110. #ifndef HAVE_FIPS
  3111. if (wc_HmacSizeByType(WC_MD5) != WC_MD5_DIGEST_SIZE)
  3112. return -3514;
  3113. #endif
  3114. return 0;
  3115. }
  3116. #endif /* NO_HMAC && NO_MD5 */
  3117. #if !defined(NO_HMAC) && !defined(NO_SHA)
  3118. int hmac_sha_test(void)
  3119. {
  3120. Hmac hmac;
  3121. byte hash[WC_SHA_DIGEST_SIZE];
  3122. const char* keys[]=
  3123. {
  3124. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3125. "\x0b\x0b\x0b",
  3126. "Jefe",
  3127. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3128. "\xAA\xAA\xAA"
  3129. };
  3130. testVector a, b, c;
  3131. testVector test_hmac[3];
  3132. int ret;
  3133. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3134. a.input = "Hi There";
  3135. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  3136. "\x8e\xf1\x46\xbe\x00";
  3137. a.inLen = XSTRLEN(a.input);
  3138. a.outLen = WC_SHA_DIGEST_SIZE;
  3139. b.input = "what do ya want for nothing?";
  3140. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  3141. "\x9c\x25\x9a\x7c\x79";
  3142. b.inLen = XSTRLEN(b.input);
  3143. b.outLen = WC_SHA_DIGEST_SIZE;
  3144. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3145. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3146. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3147. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3148. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  3149. "\x4f\x63\xf1\x75\xd3";
  3150. c.inLen = XSTRLEN(c.input);
  3151. c.outLen = WC_SHA_DIGEST_SIZE;
  3152. test_hmac[0] = a;
  3153. test_hmac[1] = b;
  3154. test_hmac[2] = c;
  3155. for (i = 0; i < times; ++i) {
  3156. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3157. if (i == 1)
  3158. continue; /* cavium can't handle short keys, fips not allowed */
  3159. #endif
  3160. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3161. return -3600;
  3162. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  3163. (word32)XSTRLEN(keys[i]));
  3164. if (ret != 0)
  3165. return -3601;
  3166. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3167. (word32)test_hmac[i].inLen);
  3168. if (ret != 0)
  3169. return -3602;
  3170. ret = wc_HmacFinal(&hmac, hash);
  3171. if (ret != 0)
  3172. return -3603;
  3173. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  3174. return -3604 - i;
  3175. wc_HmacFree(&hmac);
  3176. }
  3177. #ifndef HAVE_FIPS
  3178. if (wc_HmacSizeByType(WC_SHA) != WC_SHA_DIGEST_SIZE)
  3179. return -3614;
  3180. #endif
  3181. return 0;
  3182. }
  3183. #endif
  3184. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  3185. int hmac_sha224_test(void)
  3186. {
  3187. Hmac hmac;
  3188. byte hash[WC_SHA224_DIGEST_SIZE];
  3189. const char* keys[]=
  3190. {
  3191. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3192. "\x0b\x0b\x0b",
  3193. "Jefe",
  3194. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3195. "\xAA\xAA\xAA",
  3196. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3197. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3198. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3199. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3200. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3201. };
  3202. testVector a, b, c, d;
  3203. testVector test_hmac[4];
  3204. int ret;
  3205. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3206. a.input = "Hi There";
  3207. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  3208. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  3209. a.inLen = XSTRLEN(a.input);
  3210. a.outLen = WC_SHA224_DIGEST_SIZE;
  3211. b.input = "what do ya want for nothing?";
  3212. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  3213. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  3214. b.inLen = XSTRLEN(b.input);
  3215. b.outLen = WC_SHA224_DIGEST_SIZE;
  3216. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3217. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3218. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3219. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3220. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  3221. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  3222. c.inLen = XSTRLEN(c.input);
  3223. c.outLen = WC_SHA224_DIGEST_SIZE;
  3224. d.input = "Big Key Input";
  3225. d.output = "\xe7\x4e\x2b\x8a\xa9\xf0\x37\x2f\xed\xae\x70\x0c\x49\x47\xf1"
  3226. "\x46\x54\xa7\x32\x6b\x55\x01\x87\xd2\xc8\x02\x0e\x3a";
  3227. d.inLen = XSTRLEN(d.input);
  3228. d.outLen = WC_SHA224_DIGEST_SIZE;
  3229. test_hmac[0] = a;
  3230. test_hmac[1] = b;
  3231. test_hmac[2] = c;
  3232. test_hmac[3] = d;
  3233. for (i = 0; i < times; ++i) {
  3234. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3235. if (i == 1)
  3236. continue; /* cavium can't handle short keys, fips not allowed */
  3237. #endif
  3238. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3239. return -3700;
  3240. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  3241. (word32)XSTRLEN(keys[i]));
  3242. if (ret != 0)
  3243. return -3701;
  3244. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3245. (word32)test_hmac[i].inLen);
  3246. if (ret != 0)
  3247. return -3702;
  3248. ret = wc_HmacFinal(&hmac, hash);
  3249. if (ret != 0)
  3250. return -3703;
  3251. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  3252. return -3704 - i;
  3253. wc_HmacFree(&hmac);
  3254. }
  3255. #ifndef HAVE_FIPS
  3256. if (wc_HmacSizeByType(WC_SHA224) != WC_SHA224_DIGEST_SIZE)
  3257. return -3714;
  3258. #endif
  3259. return 0;
  3260. }
  3261. #endif
  3262. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  3263. int hmac_sha256_test(void)
  3264. {
  3265. Hmac hmac;
  3266. byte hash[WC_SHA256_DIGEST_SIZE];
  3267. const char* keys[]=
  3268. {
  3269. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3270. "\x0b\x0b\x0b",
  3271. "Jefe",
  3272. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3273. "\xAA\xAA\xAA",
  3274. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3275. "\xAA\xAA\xAA",
  3276. };
  3277. testVector a, b, c, d;
  3278. testVector test_hmac[4];
  3279. int ret;
  3280. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3281. a.input = "Hi There";
  3282. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  3283. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  3284. "\xcf\xf7";
  3285. a.inLen = XSTRLEN(a.input);
  3286. a.outLen = WC_SHA256_DIGEST_SIZE;
  3287. b.input = "what do ya want for nothing?";
  3288. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  3289. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  3290. "\x38\x43";
  3291. b.inLen = XSTRLEN(b.input);
  3292. b.outLen = WC_SHA256_DIGEST_SIZE;
  3293. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3294. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3295. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3296. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3297. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  3298. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  3299. "\x65\xfe";
  3300. c.inLen = XSTRLEN(c.input);
  3301. c.outLen = WC_SHA256_DIGEST_SIZE;
  3302. d.input = 0;
  3303. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  3304. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  3305. "\x3e\x46";
  3306. d.inLen = 0;
  3307. d.outLen = WC_SHA256_DIGEST_SIZE;
  3308. test_hmac[0] = a;
  3309. test_hmac[1] = b;
  3310. test_hmac[2] = c;
  3311. test_hmac[3] = d;
  3312. for (i = 0; i < times; ++i) {
  3313. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3314. if (i == 1)
  3315. continue; /* cavium can't handle short keys, fips not allowed */
  3316. #endif
  3317. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  3318. if (i == 3)
  3319. continue; /* QuickAssist can't handle empty HMAC */
  3320. #endif
  3321. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3322. return -3800 - i;
  3323. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  3324. (word32)XSTRLEN(keys[i]));
  3325. if (ret != 0)
  3326. return -3810 - i;
  3327. if (test_hmac[i].input != NULL) {
  3328. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3329. (word32)test_hmac[i].inLen);
  3330. if (ret != 0)
  3331. return -3820 - i;
  3332. }
  3333. ret = wc_HmacFinal(&hmac, hash);
  3334. if (ret != 0)
  3335. return -3830 - i;
  3336. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  3337. return -3840 - i;
  3338. wc_HmacFree(&hmac);
  3339. }
  3340. #ifndef HAVE_FIPS
  3341. if (wc_HmacSizeByType(WC_SHA256) != WC_SHA256_DIGEST_SIZE)
  3342. return -3850;
  3343. if (wc_HmacSizeByType(20) != BAD_FUNC_ARG)
  3344. return -3851;
  3345. #endif
  3346. if (wolfSSL_GetHmacMaxSize() != WC_MAX_DIGEST_SIZE)
  3347. return -3852;
  3348. return 0;
  3349. }
  3350. #endif
  3351. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  3352. int hmac_sha384_test(void)
  3353. {
  3354. Hmac hmac;
  3355. byte hash[WC_SHA384_DIGEST_SIZE];
  3356. const char* keys[]=
  3357. {
  3358. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3359. "\x0b\x0b\x0b",
  3360. "Jefe",
  3361. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3362. "\xAA\xAA\xAA",
  3363. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3364. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3365. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3366. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3367. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3368. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3369. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3370. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3371. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3372. };
  3373. testVector a, b, c, d;
  3374. testVector test_hmac[4];
  3375. int ret;
  3376. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3377. a.input = "Hi There";
  3378. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  3379. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  3380. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  3381. "\xfa\x9c\xb6";
  3382. a.inLen = XSTRLEN(a.input);
  3383. a.outLen = WC_SHA384_DIGEST_SIZE;
  3384. b.input = "what do ya want for nothing?";
  3385. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  3386. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  3387. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  3388. "\xb2\x16\x49";
  3389. b.inLen = XSTRLEN(b.input);
  3390. b.outLen = WC_SHA384_DIGEST_SIZE;
  3391. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3392. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3393. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3394. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3395. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  3396. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  3397. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  3398. "\xa3\x4f\x27";
  3399. c.inLen = XSTRLEN(c.input);
  3400. c.outLen = WC_SHA384_DIGEST_SIZE;
  3401. d.input = "Big Key Input";
  3402. d.output = "\xd2\x3d\x29\x6e\xf5\x1e\x23\x23\x49\x18\xb3\xbf\x4c\x38\x7b"
  3403. "\x31\x21\x17\xbb\x09\x73\x27\xf8\x12\x9d\xe9\xc6\x5d\xf9\x54"
  3404. "\xd6\x38\x5a\x68\x53\x14\xee\xe0\xa6\x4f\x36\x7e\xb2\xf3\x1a"
  3405. "\x57\x41\x69";
  3406. d.inLen = XSTRLEN(d.input);
  3407. d.outLen = WC_SHA384_DIGEST_SIZE;
  3408. test_hmac[0] = a;
  3409. test_hmac[1] = b;
  3410. test_hmac[2] = c;
  3411. test_hmac[3] = d;
  3412. for (i = 0; i < times; ++i) {
  3413. #if defined(HAVE_FIPS)
  3414. if (i == 1)
  3415. continue; /* fips not allowed */
  3416. #endif
  3417. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3418. return -3900;
  3419. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  3420. (word32)XSTRLEN(keys[i]));
  3421. if (ret != 0)
  3422. return -3901;
  3423. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3424. (word32)test_hmac[i].inLen);
  3425. if (ret != 0)
  3426. return -3902;
  3427. ret = wc_HmacFinal(&hmac, hash);
  3428. if (ret != 0)
  3429. return -3903;
  3430. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  3431. return -3904 - i;
  3432. wc_HmacFree(&hmac);
  3433. }
  3434. #ifndef HAVE_FIPS
  3435. if (wc_HmacSizeByType(WC_SHA384) != WC_SHA384_DIGEST_SIZE)
  3436. return -3914;
  3437. #endif
  3438. return 0;
  3439. }
  3440. #endif
  3441. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  3442. int hmac_sha512_test(void)
  3443. {
  3444. Hmac hmac;
  3445. byte hash[WC_SHA512_DIGEST_SIZE];
  3446. const char* keys[]=
  3447. {
  3448. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3449. "\x0b\x0b\x0b",
  3450. "Jefe",
  3451. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3452. "\xAA\xAA\xAA",
  3453. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3454. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3455. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3456. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3457. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3458. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3459. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3460. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3461. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3462. };
  3463. testVector a, b, c, d;
  3464. testVector test_hmac[4];
  3465. int ret;
  3466. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3467. a.input = "Hi There";
  3468. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  3469. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  3470. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  3471. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  3472. "\x3a\x12\x68\x54";
  3473. a.inLen = XSTRLEN(a.input);
  3474. a.outLen = WC_SHA512_DIGEST_SIZE;
  3475. b.input = "what do ya want for nothing?";
  3476. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  3477. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  3478. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  3479. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  3480. "\x38\xbc\xe7\x37";
  3481. b.inLen = XSTRLEN(b.input);
  3482. b.outLen = WC_SHA512_DIGEST_SIZE;
  3483. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3484. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3485. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3486. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3487. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  3488. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  3489. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  3490. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  3491. "\xe1\x32\x92\xfb";
  3492. c.inLen = XSTRLEN(c.input);
  3493. c.outLen = WC_SHA512_DIGEST_SIZE;
  3494. d.input = "Big Key Input";
  3495. d.output = "\x3f\xa9\xc9\xe1\xbd\xbb\x04\x55\x1f\xef\xcc\x92\x33\x08\xeb"
  3496. "\xcf\xc1\x9a\x5b\x5b\xc0\x7c\x86\x84\xae\x8c\x40\xaf\xb1\x27"
  3497. "\x87\x38\x92\x04\xa8\xed\xd7\xd7\x07\xa9\x85\xa0\xc2\xcd\x30"
  3498. "\xc0\x56\x14\x49\xbc\x2f\x69\x15\x6a\x97\xd8\x79\x2f\xb3\x3b"
  3499. "\x1e\x18\xfe\xfa";
  3500. d.inLen = XSTRLEN(d.input);
  3501. d.outLen = WC_SHA512_DIGEST_SIZE;
  3502. test_hmac[0] = a;
  3503. test_hmac[1] = b;
  3504. test_hmac[2] = c;
  3505. test_hmac[3] = d;
  3506. for (i = 0; i < times; ++i) {
  3507. #if defined(HAVE_FIPS)
  3508. if (i == 1)
  3509. continue; /* fips not allowed */
  3510. #endif
  3511. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3512. return -4000;
  3513. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  3514. (word32)XSTRLEN(keys[i]));
  3515. if (ret != 0)
  3516. return -4001;
  3517. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3518. (word32)test_hmac[i].inLen);
  3519. if (ret != 0)
  3520. return -4002;
  3521. ret = wc_HmacFinal(&hmac, hash);
  3522. if (ret != 0)
  3523. return -4003;
  3524. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  3525. return -4004 - i;
  3526. wc_HmacFree(&hmac);
  3527. }
  3528. #ifndef HAVE_FIPS
  3529. if (wc_HmacSizeByType(WC_SHA512) != WC_SHA512_DIGEST_SIZE)
  3530. return -4014;
  3531. #endif
  3532. return 0;
  3533. }
  3534. #endif
  3535. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  3536. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  3537. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  3538. int hmac_sha3_test(void)
  3539. {
  3540. Hmac hmac;
  3541. byte hash[WC_SHA3_512_DIGEST_SIZE];
  3542. const char* key[4] =
  3543. {
  3544. "Jefe",
  3545. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3546. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  3547. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  3548. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  3549. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3550. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  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. };
  3560. const char* input[4] =
  3561. {
  3562. "what do ya want for nothing?",
  3563. "Hi There",
  3564. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3565. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3566. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3567. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3568. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  3569. "Big Key Input"
  3570. };
  3571. const int hashType[4] =
  3572. {
  3573. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  3574. };
  3575. const int hashSz[4] =
  3576. {
  3577. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  3578. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  3579. };
  3580. const char* output[16] =
  3581. {
  3582. /* key = jefe, input = what do ya want for nothing? */
  3583. /* HMAC-SHA3-224 */
  3584. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  3585. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  3586. /* HMAC-SHA3-256 */
  3587. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  3588. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  3589. /* HMAC-SHA3-384 */
  3590. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  3591. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  3592. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  3593. /* HMAC-SHA3-512 */
  3594. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  3595. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  3596. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  3597. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  3598. /* key = 0b..., input = Hi There */
  3599. /* HMAC-SHA3-224 */
  3600. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  3601. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  3602. /* HMAC-SHA3-256 */
  3603. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  3604. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  3605. /* HMAC-SHA3-384 */
  3606. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  3607. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  3608. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  3609. /* HMAC-SHA3-512 */
  3610. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  3611. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  3612. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  3613. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  3614. /* key = aa..., output = dd... */
  3615. /* HMAC-SHA3-224 */
  3616. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  3617. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  3618. /* HMAC-SHA3-256 */
  3619. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  3620. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  3621. /* HMAC-SHA3-384 */
  3622. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  3623. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  3624. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  3625. /* HMAC-SHA3-512 */
  3626. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  3627. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  3628. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  3629. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  3630. /* key = big key, input = Big Key Input */
  3631. /* HMAC-SHA3-224 */
  3632. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  3633. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  3634. /* HMAC-SHA3-256 */
  3635. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  3636. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  3637. /* HMAC-SHA3-384 */
  3638. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  3639. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  3640. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  3641. /* HMAC-SHA3-512 */
  3642. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  3643. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  3644. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  3645. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  3646. };
  3647. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  3648. j, jMax = sizeof(hashType) / sizeof(hashType[0]),
  3649. ret;
  3650. #ifdef HAVE_FIPS
  3651. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  3652. * short. Skip it in FIPS builds. */
  3653. i = 1;
  3654. #endif
  3655. for (; i < iMax; i++) {
  3656. for (j = 0; j < jMax; j++) {
  3657. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3658. return -4100;
  3659. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  3660. (word32)XSTRLEN(key[i]));
  3661. if (ret != 0)
  3662. return -4101;
  3663. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  3664. (word32)XSTRLEN(input[i]));
  3665. if (ret != 0)
  3666. return -4102;
  3667. ret = wc_HmacFinal(&hmac, hash);
  3668. if (ret != 0)
  3669. return -4103;
  3670. if (XMEMCMP(hash, output[(i*jMax) + j], hashSz[j]) != 0)
  3671. return -4104;
  3672. wc_HmacFree(&hmac);
  3673. if (i > 0)
  3674. continue;
  3675. #ifndef HAVE_FIPS
  3676. ret = wc_HmacSizeByType(hashType[j]);
  3677. if (ret != hashSz[j])
  3678. return -4105;
  3679. #endif
  3680. }
  3681. }
  3682. return 0;
  3683. }
  3684. #endif
  3685. #ifndef NO_RC4
  3686. int arc4_test(void)
  3687. {
  3688. byte cipher[16];
  3689. byte plain[16];
  3690. const char* keys[] =
  3691. {
  3692. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  3693. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  3694. "\x00\x00\x00\x00\x00\x00\x00\x00",
  3695. "\xef\x01\x23\x45"
  3696. };
  3697. testVector a, b, c, d;
  3698. testVector test_arc4[4];
  3699. int times = sizeof(test_arc4) / sizeof(testVector), i;
  3700. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  3701. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  3702. a.inLen = 8;
  3703. a.outLen = 8;
  3704. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3705. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  3706. b.inLen = 8;
  3707. b.outLen = 8;
  3708. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3709. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  3710. c.inLen = 8;
  3711. c.outLen = 8;
  3712. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  3713. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  3714. d.inLen = 10;
  3715. d.outLen = 10;
  3716. test_arc4[0] = a;
  3717. test_arc4[1] = b;
  3718. test_arc4[2] = c;
  3719. test_arc4[3] = d;
  3720. for (i = 0; i < times; ++i) {
  3721. Arc4 enc;
  3722. Arc4 dec;
  3723. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  3724. if (i == 3)
  3725. keylen = 4;
  3726. if (wc_Arc4Init(&enc, HEAP_HINT, devId) != 0)
  3727. return -4200;
  3728. if (wc_Arc4Init(&dec, HEAP_HINT, devId) != 0)
  3729. return -4201;
  3730. wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  3731. wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  3732. wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  3733. (word32)test_arc4[i].outLen);
  3734. wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  3735. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  3736. return -4202 - i;
  3737. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  3738. return -4212 - i;
  3739. wc_Arc4Free(&enc);
  3740. wc_Arc4Free(&dec);
  3741. }
  3742. return 0;
  3743. }
  3744. #endif
  3745. int hc128_test(void)
  3746. {
  3747. #ifdef HAVE_HC128
  3748. byte cipher[16];
  3749. byte plain[16];
  3750. const char* keys[] =
  3751. {
  3752. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  3753. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  3754. "\x00\x53\xA6\xF9\x4C\x9F\xF2\x45\x98\xEB\x3E\x91\xE4\x37\x8A\xDD",
  3755. "\x0F\x62\xB5\x08\x5B\xAE\x01\x54\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC"
  3756. };
  3757. const char* ivs[] =
  3758. {
  3759. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  3760. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  3761. "\x0D\x74\xDB\x42\xA9\x10\x77\xDE\x45\xAC\x13\x7A\xE1\x48\xAF\x16",
  3762. "\x28\x8F\xF6\x5D\xC4\x2B\x92\xF9\x60\xC7\x2E\x95\xFC\x63\xCA\x31"
  3763. };
  3764. testVector a, b, c, d;
  3765. testVector test_hc128[4];
  3766. int times = sizeof(test_hc128) / sizeof(testVector), i;
  3767. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3768. a.output = "\x37\x86\x02\xB9\x8F\x32\xA7\x48";
  3769. a.inLen = 8;
  3770. a.outLen = 8;
  3771. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3772. b.output = "\x33\x7F\x86\x11\xC6\xED\x61\x5F";
  3773. b.inLen = 8;
  3774. b.outLen = 8;
  3775. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3776. c.output = "\x2E\x1E\xD1\x2A\x85\x51\xC0\x5A";
  3777. c.inLen = 8;
  3778. c.outLen = 8;
  3779. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  3780. d.output = "\x1C\xD8\xAE\xDD\xFE\x52\xE2\x17\xE8\x35\xD0\xB7\xE8\x4E\x29";
  3781. d.inLen = 15;
  3782. d.outLen = 15;
  3783. test_hc128[0] = a;
  3784. test_hc128[1] = b;
  3785. test_hc128[2] = c;
  3786. test_hc128[3] = d;
  3787. for (i = 0; i < times; ++i) {
  3788. HC128 enc;
  3789. HC128 dec;
  3790. /* align keys/ivs in plain/cipher buffers */
  3791. XMEMCPY(plain, keys[i], 16);
  3792. XMEMCPY(cipher, ivs[i], 16);
  3793. wc_Hc128_SetKey(&enc, plain, cipher);
  3794. wc_Hc128_SetKey(&dec, plain, cipher);
  3795. /* align input */
  3796. XMEMCPY(plain, test_hc128[i].input, test_hc128[i].outLen);
  3797. if (wc_Hc128_Process(&enc, cipher, plain,
  3798. (word32)test_hc128[i].outLen) != 0) {
  3799. return -4300;
  3800. }
  3801. if (wc_Hc128_Process(&dec, plain, cipher,
  3802. (word32)test_hc128[i].outLen) != 0) {
  3803. return -4301;
  3804. }
  3805. if (XMEMCMP(plain, test_hc128[i].input, test_hc128[i].outLen))
  3806. return -4302 - i;
  3807. if (XMEMCMP(cipher, test_hc128[i].output, test_hc128[i].outLen))
  3808. return -4312 - i;
  3809. }
  3810. #endif /* HAVE_HC128 */
  3811. return 0;
  3812. }
  3813. #ifndef NO_RABBIT
  3814. int rabbit_test(void)
  3815. {
  3816. byte cipher[16];
  3817. byte plain[16];
  3818. const char* keys[] =
  3819. {
  3820. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  3821. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  3822. "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B\xFE\x36\x3D\x2E\x29\x13\x28\x91"
  3823. };
  3824. const char* ivs[] =
  3825. {
  3826. "\x00\x00\x00\x00\x00\x00\x00\x00",
  3827. "\x59\x7E\x26\xC1\x75\xF5\x73\xC3",
  3828. 0
  3829. };
  3830. testVector a, b, c;
  3831. testVector test_rabbit[3];
  3832. int times = sizeof(test_rabbit) / sizeof(testVector), i;
  3833. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3834. a.output = "\xED\xB7\x05\x67\x37\x5D\xCD\x7C";
  3835. a.inLen = 8;
  3836. a.outLen = 8;
  3837. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3838. b.output = "\x6D\x7D\x01\x22\x92\xCC\xDC\xE0";
  3839. b.inLen = 8;
  3840. b.outLen = 8;
  3841. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3842. c.output = "\x04\xCE\xCA\x7A\x1A\x86\x6E\x77";
  3843. c.inLen = 8;
  3844. c.outLen = 8;
  3845. test_rabbit[0] = a;
  3846. test_rabbit[1] = b;
  3847. test_rabbit[2] = c;
  3848. for (i = 0; i < times; ++i) {
  3849. Rabbit enc;
  3850. Rabbit dec;
  3851. byte* iv;
  3852. /* align keys/ivs in plain/cipher buffers */
  3853. XMEMCPY(plain, keys[i], 16);
  3854. if (ivs[i]) {
  3855. XMEMCPY(cipher, ivs[i], 8);
  3856. iv = cipher;
  3857. } else
  3858. iv = NULL;
  3859. wc_RabbitSetKey(&enc, plain, iv);
  3860. wc_RabbitSetKey(&dec, plain, iv);
  3861. /* align input */
  3862. XMEMCPY(plain, test_rabbit[i].input, test_rabbit[i].outLen);
  3863. wc_RabbitProcess(&enc, cipher, plain, (word32)test_rabbit[i].outLen);
  3864. wc_RabbitProcess(&dec, plain, cipher, (word32)test_rabbit[i].outLen);
  3865. if (XMEMCMP(plain, test_rabbit[i].input, test_rabbit[i].outLen))
  3866. return -4400 - i;
  3867. if (XMEMCMP(cipher, test_rabbit[i].output, test_rabbit[i].outLen))
  3868. return -4410 - i;
  3869. }
  3870. return 0;
  3871. }
  3872. #endif /* NO_RABBIT */
  3873. #ifdef HAVE_CHACHA
  3874. int chacha_test(void)
  3875. {
  3876. ChaCha enc;
  3877. ChaCha dec;
  3878. byte cipher[128];
  3879. byte plain[128];
  3880. byte sliver[64];
  3881. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  3882. word32 keySz = 32;
  3883. int ret = 0;
  3884. int i;
  3885. int times = 4;
  3886. static const byte key1[] =
  3887. {
  3888. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3889. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3890. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3891. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  3892. };
  3893. static const byte key2[] =
  3894. {
  3895. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3896. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3897. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3898. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  3899. };
  3900. static const byte key3[] =
  3901. {
  3902. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3903. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3904. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3905. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  3906. };
  3907. /* 128 bit key */
  3908. static const byte key4[] =
  3909. {
  3910. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3911. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  3912. };
  3913. const byte* keys[] = {key1, key2, key3, key4};
  3914. static const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  3915. static const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  3916. static const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  3917. static const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  3918. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  3919. #ifndef BENCH_EMBEDDED
  3920. static const byte cipher_big_result[] = {
  3921. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  3922. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  3923. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  3924. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  3925. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  3926. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  3927. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  3928. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  3929. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  3930. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  3931. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  3932. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  3933. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  3934. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  3935. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  3936. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  3937. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  3938. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  3939. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  3940. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  3941. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  3942. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  3943. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  3944. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  3945. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  3946. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  3947. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  3948. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  3949. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  3950. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  3951. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  3952. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  3953. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  3954. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  3955. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  3956. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  3957. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  3958. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  3959. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  3960. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  3961. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  3962. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  3963. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  3964. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  3965. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  3966. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  3967. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  3968. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  3969. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  3970. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  3971. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  3972. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  3973. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  3974. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  3975. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  3976. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  3977. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  3978. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  3979. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  3980. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  3981. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  3982. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  3983. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  3984. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  3985. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  3986. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  3987. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  3988. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  3989. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  3990. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  3991. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  3992. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  3993. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  3994. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  3995. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  3996. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  3997. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  3998. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  3999. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  4000. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  4001. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  4002. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  4003. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  4004. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  4005. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  4006. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  4007. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  4008. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  4009. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  4010. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  4011. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  4012. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  4013. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  4014. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  4015. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  4016. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  4017. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  4018. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  4019. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  4020. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  4021. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  4022. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  4023. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  4024. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  4025. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  4026. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  4027. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  4028. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  4029. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  4030. };
  4031. #define CHACHA_BIG_TEST_SIZE 1305
  4032. #ifndef WOLFSSL_SMALL_STACK
  4033. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  4034. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  4035. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  4036. #else
  4037. byte* cipher_big;
  4038. byte* plain_big;
  4039. byte* input_big;
  4040. #endif /* WOLFSSL_SMALL_STACK */
  4041. int block_size;
  4042. #endif /* BENCH_EMBEDDED */
  4043. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  4044. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  4045. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  4046. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  4047. byte* test_chacha[4];
  4048. test_chacha[0] = a;
  4049. test_chacha[1] = b;
  4050. test_chacha[2] = c;
  4051. test_chacha[3] = d;
  4052. #ifndef BENCH_EMBEDDED
  4053. #ifdef WOLFSSL_SMALL_STACK
  4054. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, NULL,
  4055. DYNAMIC_TYPE_TMP_BUFFER);
  4056. if (cipher_big == NULL) {
  4057. return MEMORY_E;
  4058. }
  4059. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, NULL,
  4060. DYNAMIC_TYPE_TMP_BUFFER);
  4061. if (plain_big == NULL) {
  4062. return MEMORY_E;
  4063. }
  4064. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, NULL,
  4065. DYNAMIC_TYPE_TMP_BUFFER);
  4066. if (input_big == NULL) {
  4067. return MEMORY_E;
  4068. }
  4069. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  4070. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  4071. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  4072. #endif /* WOLFSSL_SMALL_STACK */
  4073. #endif /* BENCH_EMBEDDED */
  4074. for (i = 0; i < times; ++i) {
  4075. if (i < 3) {
  4076. keySz = 32;
  4077. }
  4078. else {
  4079. keySz = 16;
  4080. }
  4081. XMEMCPY(plain, keys[i], keySz);
  4082. XMEMSET(cipher, 0, 32);
  4083. XMEMCPY(cipher + 4, ivs[i], 8);
  4084. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  4085. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  4086. if (ret != 0)
  4087. return ret;
  4088. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  4089. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  4090. if (ret != 0)
  4091. return ret;
  4092. XMEMCPY(plain, input, 8);
  4093. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  4094. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  4095. if (ret != 0)
  4096. return ret;
  4097. if (XMEMCMP(test_chacha[i], cipher, 8))
  4098. return -4500 - i;
  4099. if (XMEMCMP(plain, input, 8))
  4100. return -4510 - i;
  4101. }
  4102. /* test of starting at a different counter
  4103. encrypts all of the information and decrypts starting at 2nd chunk */
  4104. XMEMSET(plain, 0, sizeof(plain));
  4105. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  4106. XMEMSET(cipher, 0, sizeof(cipher));
  4107. XMEMCPY(cipher + 4, ivs[0], 8);
  4108. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  4109. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  4110. if (ret != 0)
  4111. return ret;
  4112. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  4113. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  4114. if (ret != 0)
  4115. return ret;
  4116. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  4117. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  4118. if (ret != 0)
  4119. return ret;
  4120. if (XMEMCMP(plain + 64, sliver, 64))
  4121. return -4520;
  4122. #ifndef BENCH_EMBEDDED
  4123. /* test of encrypting more data */
  4124. keySz = 32;
  4125. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  4126. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  4127. if (ret != 0)
  4128. return ret;
  4129. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  4130. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  4131. if (ret != 0)
  4132. return ret;
  4133. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  4134. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  4135. CHACHA_BIG_TEST_SIZE);
  4136. if (ret != 0)
  4137. return ret;
  4138. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  4139. return -4521;
  4140. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  4141. return -4522;
  4142. for (i = 0; i < 18; ++i) {
  4143. /* this will test all paths */
  4144. // block sizes: 1 2 3 4 7 8 15 16 31 32 63 64 127 128 255 256 511 512
  4145. block_size = (2 << (i%9)) - (i<9?1:0);
  4146. keySz = 32;
  4147. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  4148. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  4149. if (ret != 0)
  4150. return ret;
  4151. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  4152. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  4153. if (ret != 0)
  4154. return ret;
  4155. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, block_size);
  4156. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big, block_size);
  4157. if (ret != 0)
  4158. return ret;
  4159. if (XMEMCMP(plain_big, input_big, block_size))
  4160. return -4523-i;
  4161. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  4162. return -4524-i;
  4163. }
  4164. #ifdef WOLFSSL_SMALL_STACK
  4165. XFREE(cipher_big, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4166. XFREE(plain_big, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4167. XFREE(input_big, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4168. #endif /* WOLFSSL_SMALL_STACK */
  4169. #endif /* BENCH_EMBEDDED */
  4170. return 0;
  4171. }
  4172. #endif /* HAVE_CHACHA */
  4173. #ifdef HAVE_POLY1305
  4174. int poly1305_test(void)
  4175. {
  4176. int ret = 0;
  4177. int i;
  4178. byte tag[16];
  4179. Poly1305 enc;
  4180. static const byte msg1[] =
  4181. {
  4182. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  4183. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  4184. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  4185. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  4186. 0x75,0x70
  4187. };
  4188. static const byte msg2[] =
  4189. {
  4190. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  4191. 0x6c,0x64,0x21
  4192. };
  4193. static const byte msg3[] =
  4194. {
  4195. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4196. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4197. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4198. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4199. };
  4200. static const byte msg4[] =
  4201. {
  4202. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  4203. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  4204. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  4205. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  4206. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  4207. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  4208. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  4209. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  4210. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  4211. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  4212. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4213. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  4214. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  4215. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  4216. 0x61,0x16
  4217. };
  4218. static const byte msg5[] =
  4219. {
  4220. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  4221. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  4222. };
  4223. static const byte msg6[] =
  4224. {
  4225. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  4226. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  4227. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  4228. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  4229. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  4230. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  4231. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4232. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  4233. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  4234. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  4235. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4236. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  4237. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  4238. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  4239. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  4240. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4241. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  4242. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  4243. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  4244. 0x61,0x16
  4245. };
  4246. byte additional[] =
  4247. {
  4248. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  4249. 0xc4,0xc5,0xc6,0xc7
  4250. };
  4251. static const byte correct0[] =
  4252. {
  4253. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  4254. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  4255. };
  4256. static const byte correct1[] =
  4257. {
  4258. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  4259. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  4260. };
  4261. static const byte correct2[] =
  4262. {
  4263. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  4264. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  4265. };
  4266. static const byte correct3[] =
  4267. {
  4268. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  4269. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  4270. };
  4271. static const byte correct4[] =
  4272. {
  4273. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  4274. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  4275. };
  4276. static const byte correct5[] =
  4277. {
  4278. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4279. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4280. };
  4281. static const byte correct6[] =
  4282. {
  4283. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  4284. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  4285. };
  4286. static const byte key[] = {
  4287. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  4288. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  4289. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  4290. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  4291. };
  4292. static const byte key2[] = {
  4293. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  4294. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  4295. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  4296. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  4297. };
  4298. static const byte key4[] = {
  4299. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  4300. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  4301. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  4302. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  4303. };
  4304. static const byte key5[] = {
  4305. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4306. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4307. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4308. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4309. };
  4310. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  4311. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  4312. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  4313. const byte* keys[] = {key, key, key2, key2, key5, key};
  4314. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  4315. correct6};
  4316. for (i = 0; i < 6; i++) {
  4317. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  4318. if (ret != 0)
  4319. return -4600 - i;
  4320. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  4321. if (ret != 0)
  4322. return -4610 - i;
  4323. ret = wc_Poly1305Final(&enc, tag);
  4324. if (ret != 0)
  4325. return -4620 - i;
  4326. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  4327. return -4630 - i;
  4328. }
  4329. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  4330. XMEMSET(tag, 0, sizeof(tag));
  4331. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  4332. if (ret != 0)
  4333. return -4640;
  4334. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  4335. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  4336. if (ret != 0)
  4337. return -4641;
  4338. if (XMEMCMP(tag, correct4, sizeof(tag)))
  4339. return -4642;
  4340. /* Check fail of TLS MAC function if altering additional data */
  4341. XMEMSET(tag, 0, sizeof(tag));
  4342. additional[0]++;
  4343. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  4344. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  4345. if (ret != 0)
  4346. return -4643;
  4347. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  4348. return -4644;
  4349. return 0;
  4350. }
  4351. #endif /* HAVE_POLY1305 */
  4352. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4353. int chacha20_poly1305_aead_test(void)
  4354. {
  4355. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  4356. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  4357. const byte key1[] = {
  4358. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  4359. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  4360. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  4361. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  4362. };
  4363. const byte plaintext1[] = {
  4364. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  4365. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  4366. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  4367. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  4368. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  4369. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  4370. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  4371. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  4372. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  4373. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  4374. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  4375. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  4376. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  4377. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  4378. 0x74, 0x2e
  4379. };
  4380. const byte iv1[] = {
  4381. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  4382. 0x44, 0x45, 0x46, 0x47
  4383. };
  4384. const byte aad1[] = { /* additional data */
  4385. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  4386. 0xc4, 0xc5, 0xc6, 0xc7
  4387. };
  4388. const byte cipher1[] = { /* expected output from operation */
  4389. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  4390. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  4391. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  4392. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  4393. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  4394. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  4395. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  4396. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  4397. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  4398. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  4399. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  4400. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  4401. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  4402. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  4403. 0x61, 0x16
  4404. };
  4405. const byte authTag1[] = { /* expected output from operation */
  4406. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  4407. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  4408. };
  4409. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  4410. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  4411. const byte key2[] = {
  4412. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  4413. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  4414. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  4415. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  4416. };
  4417. const byte plaintext2[] = {
  4418. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  4419. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  4420. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  4421. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  4422. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  4423. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  4424. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  4425. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  4426. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  4427. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  4428. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  4429. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  4430. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  4431. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  4432. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  4433. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  4434. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  4435. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  4436. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  4437. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  4438. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  4439. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  4440. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  4441. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  4442. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  4443. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  4444. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  4445. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  4446. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  4447. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  4448. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  4449. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  4450. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  4451. 0x9d
  4452. };
  4453. const byte iv2[] = {
  4454. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  4455. 0x05, 0x06, 0x07, 0x08
  4456. };
  4457. const byte aad2[] = { /* additional data */
  4458. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  4459. 0x00, 0x00, 0x4e, 0x91
  4460. };
  4461. const byte cipher2[] = { /* expected output from operation */
  4462. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  4463. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  4464. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  4465. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  4466. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  4467. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  4468. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  4469. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  4470. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  4471. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  4472. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  4473. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  4474. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  4475. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  4476. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  4477. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  4478. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  4479. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  4480. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  4481. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  4482. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  4483. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  4484. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  4485. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  4486. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  4487. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  4488. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  4489. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  4490. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  4491. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  4492. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  4493. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  4494. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  4495. 0x9b
  4496. };
  4497. const byte authTag2[] = { /* expected output from operation */
  4498. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  4499. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  4500. };
  4501. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  4502. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  4503. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  4504. int err;
  4505. ChaChaPoly_Aead aead;
  4506. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  4507. #define TEST_SMALL_CHACHA_CHUNKS 32
  4508. #else
  4509. #define TEST_SMALL_CHACHA_CHUNKS 64
  4510. #endif
  4511. #ifdef TEST_SMALL_CHACHA_CHUNKS
  4512. word32 testLen;
  4513. #endif
  4514. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  4515. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  4516. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  4517. /* Parameter Validation testing */
  4518. /* Encrypt */
  4519. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  4520. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  4521. if (err != BAD_FUNC_ARG)
  4522. return -4700;
  4523. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  4524. plaintext1, sizeof(plaintext1), generatedCiphertext,
  4525. generatedAuthTag);
  4526. if (err != BAD_FUNC_ARG)
  4527. return -4701;
  4528. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  4529. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  4530. if (err != BAD_FUNC_ARG)
  4531. return -4702;
  4532. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  4533. sizeof(plaintext1), NULL, generatedAuthTag);
  4534. if (err != BAD_FUNC_ARG)
  4535. return -4703;
  4536. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  4537. sizeof(plaintext1), generatedCiphertext, NULL);
  4538. if (err != BAD_FUNC_ARG)
  4539. return -4704;
  4540. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  4541. 0, generatedCiphertext, generatedAuthTag);
  4542. if (err != BAD_FUNC_ARG)
  4543. return -4705;
  4544. /* Decrypt */
  4545. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  4546. sizeof(cipher2), authTag2, generatedPlaintext);
  4547. if (err != BAD_FUNC_ARG)
  4548. return -4706;
  4549. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  4550. sizeof(cipher2), authTag2, generatedPlaintext);
  4551. if (err != BAD_FUNC_ARG)
  4552. return -4707;
  4553. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  4554. sizeof(cipher2), authTag2, generatedPlaintext);
  4555. if (err != BAD_FUNC_ARG)
  4556. return -4708;
  4557. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  4558. sizeof(cipher2), NULL, generatedPlaintext);
  4559. if (err != BAD_FUNC_ARG)
  4560. return -4709;
  4561. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  4562. sizeof(cipher2), authTag2, NULL);
  4563. if (err != BAD_FUNC_ARG)
  4564. return -4710;
  4565. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  4566. 0, authTag2, generatedPlaintext);
  4567. if (err != BAD_FUNC_ARG)
  4568. return -4711;
  4569. /* Test #1 */
  4570. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  4571. aad1, sizeof(aad1),
  4572. plaintext1, sizeof(plaintext1),
  4573. generatedCiphertext, generatedAuthTag);
  4574. if (err) {
  4575. return err;
  4576. }
  4577. /* -- Check the ciphertext and authtag */
  4578. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  4579. return -4712;
  4580. }
  4581. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  4582. return -4713;
  4583. }
  4584. /* -- Verify decryption works */
  4585. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  4586. aad1, sizeof(aad1),
  4587. cipher1, sizeof(cipher1),
  4588. authTag1, generatedPlaintext);
  4589. if (err) {
  4590. return err;
  4591. }
  4592. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  4593. return -4714;
  4594. }
  4595. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  4596. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  4597. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  4598. /* Test #2 */
  4599. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  4600. aad2, sizeof(aad2),
  4601. plaintext2, sizeof(plaintext2),
  4602. generatedCiphertext, generatedAuthTag);
  4603. if (err) {
  4604. return err;
  4605. }
  4606. /* -- Check the ciphertext and authtag */
  4607. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  4608. return -4715;
  4609. }
  4610. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  4611. return -4716;
  4612. }
  4613. /* -- Verify decryption works */
  4614. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  4615. aad2, sizeof(aad2),
  4616. cipher2, sizeof(cipher2),
  4617. authTag2, generatedPlaintext);
  4618. if (err) {
  4619. return err;
  4620. }
  4621. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  4622. return -4717;
  4623. }
  4624. /* AEAD init/update/final */
  4625. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  4626. CHACHA20_POLY1305_AEAD_DECRYPT);
  4627. if (err != BAD_FUNC_ARG)
  4628. return -4718;
  4629. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  4630. CHACHA20_POLY1305_AEAD_DECRYPT);
  4631. if (err != BAD_FUNC_ARG)
  4632. return -4719;
  4633. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  4634. CHACHA20_POLY1305_AEAD_DECRYPT);
  4635. if (err != BAD_FUNC_ARG)
  4636. return -4720;
  4637. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  4638. if (err != BAD_FUNC_ARG)
  4639. return -4721;
  4640. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  4641. if (err != BAD_FUNC_ARG)
  4642. return -4722;
  4643. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  4644. generatedPlaintext, sizeof(plaintext1));
  4645. if (err != BAD_FUNC_ARG)
  4646. return -4723;
  4647. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  4648. sizeof(plaintext1));
  4649. if (err != BAD_FUNC_ARG)
  4650. return -4724;
  4651. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  4652. sizeof(plaintext1));
  4653. if (err != BAD_FUNC_ARG)
  4654. return -4725;
  4655. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  4656. if (err != BAD_FUNC_ARG)
  4657. return -4726;
  4658. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  4659. if (err != BAD_FUNC_ARG)
  4660. return -4727;
  4661. /* AEAD init/update/final - state tests */
  4662. aead.state = CHACHA20_POLY1305_STATE_INIT;
  4663. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  4664. if (err != BAD_STATE_E)
  4665. return -4728;
  4666. aead.state = CHACHA20_POLY1305_STATE_DATA;
  4667. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  4668. if (err != BAD_STATE_E)
  4669. return -4729;
  4670. aead.state = CHACHA20_POLY1305_STATE_INIT;
  4671. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  4672. generatedPlaintext, sizeof(plaintext1));
  4673. if (err != BAD_STATE_E)
  4674. return -4730;
  4675. aead.state = CHACHA20_POLY1305_STATE_INIT;
  4676. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  4677. if (err != BAD_STATE_E)
  4678. return -4731;
  4679. aead.state = CHACHA20_POLY1305_STATE_READY;
  4680. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  4681. if (err != BAD_STATE_E)
  4682. return -4732;
  4683. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  4684. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  4685. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  4686. /* Test 1 - Encrypt */
  4687. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  4688. CHACHA20_POLY1305_AEAD_ENCRYPT);
  4689. if (err != 0)
  4690. return -4733;
  4691. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  4692. if (err != 0)
  4693. return -4734;
  4694. #ifdef TEST_SMALL_CHACHA_CHUNKS
  4695. /* test doing data in smaller chunks */
  4696. for (testLen=0; testLen<sizeof(plaintext1); ) {
  4697. word32 dataLen = sizeof(plaintext1) - testLen;
  4698. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  4699. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  4700. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  4701. &generatedCiphertext[testLen], dataLen);
  4702. if (err != 0)
  4703. return -4735;
  4704. testLen += dataLen;
  4705. }
  4706. #else
  4707. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  4708. generatedCiphertext, sizeof(plaintext1));
  4709. #endif
  4710. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  4711. if (err != 0)
  4712. return -4736;
  4713. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  4714. if (err != 0)
  4715. return -4737;
  4716. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  4717. return -4738;
  4718. }
  4719. /* Test 1 - Decrypt */
  4720. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  4721. CHACHA20_POLY1305_AEAD_DECRYPT);
  4722. if (err != 0)
  4723. return -4739;
  4724. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  4725. if (err != 0)
  4726. return -4740;
  4727. #ifdef TEST_SMALL_CHACHA_CHUNKS
  4728. /* test doing data in smaller chunks */
  4729. for (testLen=0; testLen<sizeof(plaintext1); ) {
  4730. word32 dataLen = sizeof(plaintext1) - testLen;
  4731. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  4732. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  4733. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  4734. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  4735. dataLen);
  4736. if (err != 0)
  4737. return -4741;
  4738. testLen += dataLen;
  4739. }
  4740. #else
  4741. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  4742. generatedPlaintext, sizeof(cipher1));
  4743. #endif
  4744. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  4745. if (err != 0)
  4746. return -4742;
  4747. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  4748. if (err != 0)
  4749. return -4743;
  4750. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  4751. return -4744;
  4752. }
  4753. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  4754. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  4755. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  4756. /* Test 2 - Encrypt */
  4757. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  4758. CHACHA20_POLY1305_AEAD_ENCRYPT);
  4759. if (err != 0)
  4760. return -4745;
  4761. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  4762. if (err != 0)
  4763. return -4746;
  4764. #ifdef TEST_SMALL_CHACHA_CHUNKS
  4765. /* test doing data in smaller chunks */
  4766. for (testLen=0; testLen<sizeof(plaintext2); ) {
  4767. word32 dataLen = sizeof(plaintext2) - testLen;
  4768. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  4769. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  4770. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  4771. &generatedCiphertext[testLen], dataLen);
  4772. if (err != 0)
  4773. return -4747;
  4774. testLen += dataLen;
  4775. }
  4776. #else
  4777. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  4778. sizeof(plaintext2));
  4779. #endif
  4780. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  4781. if (err != 0)
  4782. return -4748;
  4783. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  4784. if (err != 0)
  4785. return -4749;
  4786. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  4787. return -4750;
  4788. }
  4789. /* Test 2 - Decrypt */
  4790. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  4791. CHACHA20_POLY1305_AEAD_DECRYPT);
  4792. if (err != 0)
  4793. return -4751;
  4794. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  4795. if (err != 0)
  4796. return -4752;
  4797. #ifdef TEST_SMALL_CHACHA_CHUNKS
  4798. /* test doing data in smaller chunks */
  4799. for (testLen=0; testLen<sizeof(plaintext2); ) {
  4800. word32 dataLen = sizeof(plaintext2) - testLen;
  4801. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  4802. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  4803. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  4804. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  4805. dataLen);
  4806. if (err != 0)
  4807. return -4753;
  4808. testLen += dataLen;
  4809. }
  4810. #else
  4811. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  4812. generatedPlaintext, sizeof(cipher2));
  4813. #endif
  4814. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  4815. if (err != 0)
  4816. return -4754;
  4817. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  4818. if (err != 0)
  4819. return -4755;
  4820. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  4821. return -4756;
  4822. }
  4823. return err;
  4824. }
  4825. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  4826. #ifndef NO_DES3
  4827. int des_test(void)
  4828. {
  4829. const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  4830. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  4831. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  4832. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  4833. };
  4834. byte plain[24];
  4835. byte cipher[24];
  4836. Des enc;
  4837. Des dec;
  4838. const byte key[] =
  4839. {
  4840. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  4841. };
  4842. const byte iv[] =
  4843. {
  4844. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  4845. };
  4846. const byte verify[] =
  4847. {
  4848. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  4849. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  4850. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  4851. };
  4852. int ret;
  4853. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  4854. if (ret != 0)
  4855. return -4800;
  4856. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  4857. if (ret != 0)
  4858. return -4801;
  4859. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  4860. if (ret != 0)
  4861. return -4802;
  4862. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  4863. if (ret != 0)
  4864. return -4803;
  4865. if (XMEMCMP(plain, vector, sizeof(plain)))
  4866. return -4804;
  4867. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  4868. return -4805;
  4869. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  4870. if (ret != 0)
  4871. return -4806;
  4872. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  4873. {
  4874. EncryptedInfo info;
  4875. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  4876. XMEMCPY(info.iv, iv, sizeof(iv));
  4877. info.ivSz = sizeof(iv);
  4878. info.keySz = sizeof(key);
  4879. info.cipherType = WC_CIPHER_DES;
  4880. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  4881. sizeof(key), WC_HASH_TYPE_SHA);
  4882. if (ret != 0)
  4883. return -4807;
  4884. /* Test invalid info ptr */
  4885. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  4886. sizeof(key), WC_HASH_TYPE_SHA);
  4887. if (ret != BAD_FUNC_ARG)
  4888. return -4808;
  4889. /* Test invalid hash type */
  4890. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  4891. sizeof(key), WC_HASH_TYPE_NONE);
  4892. if (ret == 0)
  4893. return -4809;
  4894. }
  4895. #endif
  4896. return 0;
  4897. }
  4898. #endif /* NO_DES3 */
  4899. #ifndef NO_DES3
  4900. int des3_test(void)
  4901. {
  4902. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  4903. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  4904. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  4905. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  4906. };
  4907. byte plain[24];
  4908. byte cipher[24];
  4909. Des3 enc;
  4910. Des3 dec;
  4911. const byte key3[] =
  4912. {
  4913. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  4914. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  4915. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  4916. };
  4917. const byte iv3[] =
  4918. {
  4919. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  4920. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  4921. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  4922. };
  4923. const byte verify3[] =
  4924. {
  4925. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  4926. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  4927. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  4928. };
  4929. int ret;
  4930. if (wc_Des3Init(&enc, HEAP_HINT, devId) != 0)
  4931. return -4900;
  4932. if (wc_Des3Init(&dec, HEAP_HINT, devId) != 0)
  4933. return -4901;
  4934. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  4935. if (ret != 0)
  4936. return -4902;
  4937. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  4938. if (ret != 0)
  4939. return -4903;
  4940. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  4941. #if defined(WOLFSSL_ASYNC_CRYPT)
  4942. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  4943. #endif
  4944. if (ret != 0)
  4945. return -4904;
  4946. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  4947. #if defined(WOLFSSL_ASYNC_CRYPT)
  4948. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  4949. #endif
  4950. if (ret != 0)
  4951. return -4905;
  4952. if (XMEMCMP(plain, vector, sizeof(plain)))
  4953. return -4906;
  4954. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  4955. return -4907;
  4956. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  4957. /* test the same vectors with using compatibility layer */
  4958. {
  4959. DES_key_schedule ks1;
  4960. DES_key_schedule ks2;
  4961. DES_key_schedule ks3;
  4962. DES_cblock iv4;
  4963. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  4964. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  4965. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  4966. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  4967. XMEMSET(plain, 0, sizeof(plain));
  4968. XMEMSET(cipher, 0, sizeof(cipher));
  4969. DES_ede3_cbc_encrypt(vector, cipher, sizeof(vector), &ks1, &ks2, &ks3,
  4970. &iv4, DES_ENCRYPT);
  4971. DES_ede3_cbc_encrypt(cipher, plain, sizeof(cipher), &ks1, &ks2, &ks3,
  4972. &iv4, DES_DECRYPT);
  4973. if (XMEMCMP(plain, vector, sizeof(plain)))
  4974. return -4908;
  4975. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  4976. return -4909;
  4977. }
  4978. #endif /* OPENSSL_EXTRA */
  4979. wc_Des3Free(&enc);
  4980. wc_Des3Free(&dec);
  4981. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  4982. {
  4983. EncryptedInfo info;
  4984. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  4985. XMEMCPY(info.iv, iv3, sizeof(iv3));
  4986. info.ivSz = sizeof(iv3);
  4987. info.keySz = sizeof(key3);
  4988. info.cipherType = WC_CIPHER_DES3;
  4989. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  4990. sizeof(key3), WC_HASH_TYPE_SHA);
  4991. if (ret != 0)
  4992. return -4910;
  4993. }
  4994. #endif
  4995. return 0;
  4996. }
  4997. #endif /* NO_DES */
  4998. #ifndef NO_AES
  4999. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  5000. defined(WOLFSSL_AES_XTS)
  5001. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5002. /* pass in the function, key, iv, plain text and expected and this function
  5003. * tests that the encryption and decryption is successful */
  5004. static int EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  5005. const byte* iv, const byte* plain, int plainSz,
  5006. const byte* expected, int expectedSz)
  5007. {
  5008. EVP_CIPHER_CTX ctx;
  5009. int idx, ret = 0, cipherSz;
  5010. byte* cipher;
  5011. cipher = (byte*)XMALLOC(plainSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5012. if (cipher == NULL) {
  5013. return -4911;
  5014. }
  5015. /* test encrypt */
  5016. EVP_CIPHER_CTX_init(&ctx);
  5017. if (EVP_CipherInit(&ctx, type, key, iv, 1) == 0) {
  5018. ret = -4912;
  5019. goto EVP_TEST_END;
  5020. }
  5021. if (EVP_CipherUpdate(&ctx, cipher, &idx, plain, expectedSz) == 0) {
  5022. ret = -4913;
  5023. goto EVP_TEST_END;
  5024. }
  5025. cipherSz = idx;
  5026. if (EVP_CipherFinal(&ctx, cipher + cipherSz, &idx) == 0) {
  5027. ret = -4914;
  5028. goto EVP_TEST_END;
  5029. }
  5030. cipherSz += idx;
  5031. if (XMEMCMP(cipher, expected, plainSz)) {
  5032. ret = -4915;
  5033. goto EVP_TEST_END;
  5034. }
  5035. /* test decrypt */
  5036. EVP_CIPHER_CTX_init(&ctx);
  5037. if (EVP_CipherInit(&ctx, type, key, iv, 0) == 0) {
  5038. ret = -4916;
  5039. goto EVP_TEST_END;
  5040. }
  5041. if (EVP_CipherUpdate(&ctx, cipher, &idx, cipher, expectedSz) == 0) {
  5042. ret = -4917;
  5043. goto EVP_TEST_END;
  5044. }
  5045. cipherSz = idx;
  5046. if (EVP_CipherFinal(&ctx, cipher + cipherSz, &idx) == 0) {
  5047. ret = -4918;
  5048. goto EVP_TEST_END;
  5049. }
  5050. cipherSz += idx;
  5051. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  5052. ret = -4919;
  5053. goto EVP_TEST_END;
  5054. }
  5055. EVP_TEST_END:
  5056. XFREE(cipher, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5057. (void)cipherSz;
  5058. return ret;
  5059. }
  5060. #endif /* OPENSSL_EXTRA */
  5061. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  5062. #ifdef WOLFSSL_AES_OFB
  5063. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  5064. int aesofb_test(void)
  5065. {
  5066. #ifdef WOLFSSL_AES_256
  5067. const byte key1[] =
  5068. {
  5069. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  5070. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  5071. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  5072. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  5073. };
  5074. const byte iv1[] =
  5075. {
  5076. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  5077. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  5078. };
  5079. const byte plain1[] =
  5080. {
  5081. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  5082. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  5083. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  5084. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  5085. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  5086. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  5087. };
  5088. const byte cipher1[] =
  5089. {
  5090. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  5091. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  5092. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  5093. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  5094. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  5095. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  5096. };
  5097. #endif /* WOLFSSL_AES_256 */
  5098. #ifdef WOLFSSL_AES_128
  5099. const byte key2[] =
  5100. {
  5101. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  5102. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  5103. };
  5104. const byte iv2[] =
  5105. {
  5106. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5107. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5108. };
  5109. const byte plain2[] =
  5110. {
  5111. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5112. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5113. };
  5114. const byte cipher2[] =
  5115. {
  5116. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  5117. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  5118. };
  5119. #endif /* WOLFSSL_AES_128 */
  5120. #ifdef WOLFSSL_AES_192
  5121. const byte key3[] = {
  5122. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  5123. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  5124. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  5125. };
  5126. const byte iv3[] =
  5127. {
  5128. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5129. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5130. };
  5131. const byte cipher3[] =
  5132. {
  5133. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  5134. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  5135. };
  5136. const byte plain3[] =
  5137. {
  5138. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5139. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5140. };
  5141. #endif /* WOLFSSL_AES_192 */
  5142. Aes enc;
  5143. byte cipher[AES_BLOCK_SIZE * 4];
  5144. #ifdef HAVE_AES_DECRYPT
  5145. Aes dec;
  5146. byte plain [AES_BLOCK_SIZE * 4];
  5147. #endif
  5148. int ret = 0;
  5149. (void)enc;
  5150. #ifdef HAVE_AES_DECRYPT
  5151. (void)dec;
  5152. #endif
  5153. #ifdef WOLFSSL_AES_128
  5154. /* 128 key size test */
  5155. #ifdef OPENSSL_EXTRA
  5156. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  5157. cipher2, sizeof(cipher2));
  5158. if (ret != 0) {
  5159. return ret;
  5160. }
  5161. #endif
  5162. ret = wc_AesSetKey(&enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  5163. if (ret != 0)
  5164. return -4920;
  5165. #ifdef HAVE_AES_DECRYPT
  5166. /* decrypt uses AES_ENCRYPTION */
  5167. ret = wc_AesSetKey(&dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  5168. if (ret != 0)
  5169. return -4921;
  5170. #endif
  5171. XMEMSET(cipher, 0, sizeof(cipher));
  5172. ret = wc_AesOfbEncrypt(&enc, cipher, plain2, AES_BLOCK_SIZE);
  5173. if (ret != 0)
  5174. return -4922;
  5175. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  5176. return -4923;
  5177. #ifdef HAVE_AES_DECRYPT
  5178. ret = wc_AesOfbDecrypt(&dec, plain, cipher2, AES_BLOCK_SIZE);
  5179. if (ret != 0)
  5180. return -4924;
  5181. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  5182. return -4925;
  5183. #endif /* HAVE_AES_DECRYPT */
  5184. #endif /* WOLFSSL_AES_128 */
  5185. #ifdef WOLFSSL_AES_192
  5186. /* 192 key size test */
  5187. #ifdef OPENSSL_EXTRA
  5188. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  5189. cipher3, sizeof(cipher3));
  5190. if (ret != 0) {
  5191. return ret;
  5192. }
  5193. #endif
  5194. ret = wc_AesSetKey(&enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  5195. if (ret != 0)
  5196. return -4926;
  5197. #ifdef HAVE_AES_DECRYPT
  5198. /* decrypt uses AES_ENCRYPTION */
  5199. ret = wc_AesSetKey(&dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  5200. if (ret != 0)
  5201. return -4927;
  5202. #endif
  5203. XMEMSET(cipher, 0, sizeof(cipher));
  5204. ret = wc_AesOfbEncrypt(&enc, cipher, plain3, AES_BLOCK_SIZE);
  5205. if (ret != 0)
  5206. return -4928;
  5207. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  5208. return -4929;
  5209. #ifdef HAVE_AES_DECRYPT
  5210. ret = wc_AesOfbDecrypt(&dec, plain, cipher3, AES_BLOCK_SIZE);
  5211. if (ret != 0)
  5212. return -4930;
  5213. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  5214. return -4931;
  5215. #endif /* HAVE_AES_DECRYPT */
  5216. #endif /* WOLFSSL_AES_192 */
  5217. #ifdef WOLFSSL_AES_256
  5218. /* 256 key size test */
  5219. #ifdef OPENSSL_EXTRA
  5220. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  5221. cipher1, sizeof(cipher1));
  5222. if (ret != 0) {
  5223. return ret;
  5224. }
  5225. #endif
  5226. ret = wc_AesSetKey(&enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5227. if (ret != 0)
  5228. return -4932;
  5229. #ifdef HAVE_AES_DECRYPT
  5230. /* decrypt uses AES_ENCRYPTION */
  5231. ret = wc_AesSetKey(&dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5232. if (ret != 0)
  5233. return -4933;
  5234. #endif
  5235. XMEMSET(cipher, 0, sizeof(cipher));
  5236. ret = wc_AesOfbEncrypt(&enc, cipher, plain1, AES_BLOCK_SIZE);
  5237. if (ret != 0)
  5238. return -4934;
  5239. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  5240. return -4935;
  5241. ret = wc_AesOfbEncrypt(&enc, cipher + AES_BLOCK_SIZE,
  5242. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  5243. if (ret != 0)
  5244. return -4936;
  5245. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  5246. AES_BLOCK_SIZE))
  5247. return -4937;
  5248. #ifdef HAVE_AES_DECRYPT
  5249. ret = wc_AesOfbDecrypt(&dec, plain, cipher1, AES_BLOCK_SIZE);
  5250. if (ret != 0)
  5251. return -4938;
  5252. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  5253. return -4939;
  5254. ret = wc_AesOfbDecrypt(&dec, plain + AES_BLOCK_SIZE,
  5255. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  5256. if (ret != 0)
  5257. return -4940;
  5258. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  5259. AES_BLOCK_SIZE))
  5260. return -4941;
  5261. #endif /* HAVE_AES_DECRYPT */
  5262. /* multiple blocks at once */
  5263. ret = wc_AesSetKey(&enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5264. if (ret != 0)
  5265. return -4942;
  5266. #ifdef HAVE_AES_DECRYPT
  5267. /* decrypt uses AES_ENCRYPTION */
  5268. ret = wc_AesSetKey(&dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5269. if (ret != 0)
  5270. return -4943;
  5271. #endif
  5272. XMEMSET(cipher, 0, sizeof(cipher));
  5273. ret = wc_AesOfbEncrypt(&enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  5274. if (ret != 0)
  5275. return -4944;
  5276. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  5277. return -4945;
  5278. #ifdef HAVE_AES_DECRYPT
  5279. ret = wc_AesOfbDecrypt(&dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  5280. if (ret != 0)
  5281. return -4946;
  5282. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  5283. return -4947;
  5284. #endif /* HAVE_AES_DECRYPT */
  5285. /* inline decrypt/encrypt*/
  5286. ret = wc_AesSetKey(&enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5287. if (ret != 0)
  5288. return -4948;
  5289. #ifdef HAVE_AES_DECRYPT
  5290. /* decrypt uses AES_ENCRYPTION */
  5291. ret = wc_AesSetKey(&dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5292. if (ret != 0)
  5293. return -4949;
  5294. #endif
  5295. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  5296. ret = wc_AesOfbEncrypt(&enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  5297. if (ret != 0)
  5298. return -4950;
  5299. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  5300. return -4951;
  5301. #ifdef HAVE_AES_DECRYPT
  5302. ret = wc_AesOfbDecrypt(&dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  5303. if (ret != 0)
  5304. return -4952;
  5305. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  5306. return -4953;
  5307. #endif /* HAVE_AES_DECRYPT */
  5308. /* 256 key size test leftover support */
  5309. ret = wc_AesSetKey(&enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5310. if (ret != 0)
  5311. return -4954;
  5312. #ifdef HAVE_AES_DECRYPT
  5313. /* decrypt uses AES_ENCRYPTION */
  5314. ret = wc_AesSetKey(&dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5315. if (ret != 0)
  5316. return -4955;
  5317. #endif
  5318. XMEMSET(cipher, 0, sizeof(cipher));
  5319. ret = wc_AesOfbEncrypt(&enc, cipher, plain1, 3);
  5320. if (ret != 0)
  5321. return -4956;
  5322. if (XMEMCMP(cipher, cipher1, 3))
  5323. return -4957;
  5324. ret = wc_AesOfbEncrypt(&enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  5325. if (ret != 0)
  5326. return -4958;
  5327. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  5328. return -4959;
  5329. #ifdef HAVE_AES_DECRYPT
  5330. ret = wc_AesOfbDecrypt(&dec, plain, cipher1, 6);
  5331. if (ret != 0)
  5332. return -4960;
  5333. if (XMEMCMP(plain, plain1, 6))
  5334. return -4961;
  5335. ret = wc_AesOfbDecrypt(&dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  5336. if (ret != 0)
  5337. return -4962;
  5338. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  5339. return -4963;
  5340. #endif /* HAVE_AES_DECRYPT */
  5341. #endif /* WOLFSSL_AES_256 */
  5342. return 0;
  5343. }
  5344. #endif /* WOLFSSL_AES_OFB */
  5345. #if defined(WOLFSSL_AES_CFB)
  5346. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of Operation Methods an*/
  5347. static int aescfb_test(void)
  5348. {
  5349. Aes enc;
  5350. byte cipher[AES_BLOCK_SIZE * 4];
  5351. #ifdef HAVE_AES_DECRYPT
  5352. Aes dec;
  5353. byte plain [AES_BLOCK_SIZE * 4];
  5354. #endif
  5355. int ret = 0;
  5356. const byte iv[] = {
  5357. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  5358. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  5359. };
  5360. #ifdef WOLFSSL_AES_128
  5361. const byte key1[] =
  5362. {
  5363. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  5364. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  5365. };
  5366. const byte cipher1[] =
  5367. {
  5368. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  5369. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  5370. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  5371. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  5372. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  5373. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  5374. };
  5375. const byte msg1[] =
  5376. {
  5377. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  5378. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  5379. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  5380. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  5381. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  5382. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  5383. };
  5384. #endif /* WOLFSSL_AES_128 */
  5385. #ifdef WOLFSSL_AES_192
  5386. /* 192 size key test */
  5387. const byte key2[] =
  5388. {
  5389. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  5390. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  5391. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  5392. };
  5393. const byte cipher2[] =
  5394. {
  5395. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  5396. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  5397. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  5398. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  5399. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  5400. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  5401. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  5402. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  5403. };
  5404. const byte msg2[] =
  5405. {
  5406. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  5407. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  5408. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  5409. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  5410. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  5411. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  5412. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  5413. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  5414. };
  5415. #endif /* WOLFSSL_AES_192 */
  5416. #ifdef WOLFSSL_AES_256
  5417. /* 256 size key simple test */
  5418. const byte key3[] =
  5419. {
  5420. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  5421. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  5422. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  5423. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  5424. };
  5425. const byte cipher3[] =
  5426. {
  5427. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  5428. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  5429. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  5430. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  5431. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  5432. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  5433. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  5434. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  5435. };
  5436. const byte msg3[] =
  5437. {
  5438. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  5439. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  5440. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  5441. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  5442. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  5443. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  5444. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  5445. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  5446. };
  5447. #endif /* WOLFSSL_AES_256 */
  5448. if (wc_AesInit(&enc, HEAP_HINT, devId) != 0)
  5449. return -4964;
  5450. #ifdef HAVE_AES_DECRYPT
  5451. if (wc_AesInit(&dec, HEAP_HINT, devId) != 0)
  5452. return -4965;
  5453. #endif
  5454. #ifdef WOLFSSL_AES_128
  5455. /* 128 key tests */
  5456. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5457. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  5458. cipher1, sizeof(cipher1));
  5459. if (ret != 0) {
  5460. return ret;
  5461. }
  5462. #endif
  5463. ret = wc_AesSetKey(&enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  5464. if (ret != 0)
  5465. return -4966;
  5466. #ifdef HAVE_AES_DECRYPT
  5467. /* decrypt uses AES_ENCRYPTION */
  5468. ret = wc_AesSetKey(&dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  5469. if (ret != 0)
  5470. return -4967;
  5471. #endif
  5472. XMEMSET(cipher, 0, sizeof(cipher));
  5473. ret = wc_AesCfbEncrypt(&enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  5474. if (ret != 0)
  5475. return -4968;
  5476. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  5477. return -4969;
  5478. /* test restarting encryption process */
  5479. ret = wc_AesCfbEncrypt(&enc, cipher + (AES_BLOCK_SIZE * 2),
  5480. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  5481. if (ret != 0)
  5482. return -4970;
  5483. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  5484. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  5485. return -4971;
  5486. #ifdef HAVE_AES_DECRYPT
  5487. ret = wc_AesCfbDecrypt(&dec, plain, cipher, AES_BLOCK_SIZE * 3);
  5488. if (ret != 0)
  5489. return -4972;
  5490. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  5491. return -4973;
  5492. #endif /* HAVE_AES_DECRYPT */
  5493. #endif /* WOLFSSL_AES_128 */
  5494. #ifdef WOLFSSL_AES_192
  5495. /* 192 key size test */
  5496. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5497. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  5498. cipher2, sizeof(cipher2));
  5499. if (ret != 0) {
  5500. return ret;
  5501. }
  5502. #endif
  5503. ret = wc_AesSetKey(&enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  5504. if (ret != 0)
  5505. return -4974;
  5506. #ifdef HAVE_AES_DECRYPT
  5507. /* decrypt uses AES_ENCRYPTION */
  5508. ret = wc_AesSetKey(&dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  5509. if (ret != 0)
  5510. return -4975;
  5511. #endif
  5512. XMEMSET(cipher, 0, sizeof(cipher));
  5513. ret = wc_AesCfbEncrypt(&enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  5514. if (ret != 0)
  5515. return -4976;
  5516. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  5517. return -4977;
  5518. #ifdef HAVE_AES_DECRYPT
  5519. ret = wc_AesCfbDecrypt(&dec, plain, cipher, AES_BLOCK_SIZE * 4);
  5520. if (ret != 0)
  5521. return -4978;
  5522. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  5523. return -4979;
  5524. #endif /* HAVE_AES_DECRYPT */
  5525. #endif /* WOLFSSL_AES_192 */
  5526. #ifdef WOLFSSL_AES_256
  5527. /* 256 key size test */
  5528. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5529. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  5530. cipher3, sizeof(cipher3));
  5531. if (ret != 0) {
  5532. return ret;
  5533. }
  5534. #endif
  5535. ret = wc_AesSetKey(&enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  5536. if (ret != 0)
  5537. return -4980;
  5538. #ifdef HAVE_AES_DECRYPT
  5539. /* decrypt uses AES_ENCRYPTION */
  5540. ret = wc_AesSetKey(&dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  5541. if (ret != 0)
  5542. return -4981;
  5543. #endif
  5544. /* test with data left overs, magic lengths are checking near edges */
  5545. XMEMSET(cipher, 0, sizeof(cipher));
  5546. ret = wc_AesCfbEncrypt(&enc, cipher, msg3, 4);
  5547. if (ret != 0)
  5548. return -4982;
  5549. if (XMEMCMP(cipher, cipher3, 4))
  5550. return -4983;
  5551. ret = wc_AesCfbEncrypt(&enc, cipher + 4, msg3 + 4, 27);
  5552. if (ret != 0)
  5553. return -4984;
  5554. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  5555. return -4985;
  5556. ret = wc_AesCfbEncrypt(&enc, cipher + 31, msg3 + 31,
  5557. (AES_BLOCK_SIZE * 4) - 31);
  5558. if (ret != 0)
  5559. return -4986;
  5560. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  5561. return -4987;
  5562. #ifdef HAVE_AES_DECRYPT
  5563. ret = wc_AesCfbDecrypt(&dec, plain, cipher, 4);
  5564. if (ret != 0)
  5565. return -4988;
  5566. if (XMEMCMP(plain, msg3, 4))
  5567. return -4989;
  5568. ret = wc_AesCfbDecrypt(&dec, plain + 4, cipher + 4, 4);
  5569. if (ret != 0)
  5570. return -4990;
  5571. ret = wc_AesCfbDecrypt(&dec, plain + 8, cipher + 8, 23);
  5572. if (ret != 0)
  5573. return -4991;
  5574. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  5575. return -4992;
  5576. ret = wc_AesCfbDecrypt(&dec, plain + 31, cipher + 31,
  5577. (AES_BLOCK_SIZE * 4) - 31);
  5578. if (ret != 0)
  5579. return -4993;
  5580. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  5581. return -4994;
  5582. #endif /* HAVE_AES_DECRYPT */
  5583. #endif /* WOLFSSL_AES_256 */
  5584. return ret;
  5585. }
  5586. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5587. static int aescfb1_test(void)
  5588. {
  5589. Aes enc;
  5590. byte cipher[AES_BLOCK_SIZE];
  5591. #ifdef HAVE_AES_DECRYPT
  5592. Aes dec;
  5593. byte plain [AES_BLOCK_SIZE];
  5594. #endif
  5595. int ret = 0;
  5596. #ifdef WOLFSSL_AES_128
  5597. const byte iv[] = {
  5598. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  5599. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  5600. };
  5601. const byte key1[] =
  5602. {
  5603. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  5604. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  5605. };
  5606. const byte cipher1[] =
  5607. {
  5608. 0x00
  5609. };
  5610. const byte msg1[] =
  5611. {
  5612. 0xC0
  5613. };
  5614. #endif /* WOLFSSL_AES_128 */
  5615. #ifdef WOLFSSL_AES_192
  5616. const byte iv2[] = {
  5617. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  5618. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  5619. };
  5620. const byte key2[] =
  5621. {
  5622. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  5623. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  5624. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  5625. };
  5626. const byte cipher2[] =
  5627. {
  5628. 0x30
  5629. };
  5630. const byte msg2[] =
  5631. {
  5632. 0x80
  5633. };
  5634. #endif /* WOLFSSL_AES_192 */
  5635. #ifdef WOLFSSL_AES_256
  5636. const byte iv3[] = {
  5637. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  5638. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  5639. };
  5640. const byte key3[] =
  5641. {
  5642. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  5643. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  5644. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  5645. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  5646. };
  5647. const byte cipher3[] =
  5648. {
  5649. 0xF7, 0x00
  5650. };
  5651. const byte msg3[] =
  5652. {
  5653. 0x41, 0xC0
  5654. };
  5655. #endif /* WOLFSSL_AES_256 */
  5656. if (wc_AesInit(&enc, HEAP_HINT, devId) != 0)
  5657. return -4995;
  5658. #ifdef HAVE_AES_DECRYPT
  5659. if (wc_AesInit(&dec, HEAP_HINT, devId) != 0)
  5660. return -4996;
  5661. #endif
  5662. #ifdef WOLFSSL_AES_128
  5663. /* 128 key tests */
  5664. ret = wc_AesSetKey(&enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  5665. if (ret != 0)
  5666. return -4997;
  5667. #ifdef HAVE_AES_DECRYPT
  5668. /* decrypt uses AES_ENCRYPTION */
  5669. ret = wc_AesSetKey(&dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  5670. if (ret != 0)
  5671. return -4998;
  5672. #endif
  5673. XMEMSET(cipher, 0, sizeof(cipher));
  5674. ret = wc_AesCfb1Encrypt(&enc, cipher, msg1, 2);
  5675. if (ret != 0)
  5676. return -4999;
  5677. if (cipher[0] != cipher1[0])
  5678. return -5000;
  5679. #ifdef HAVE_AES_DECRYPT
  5680. ret = wc_AesCfb1Decrypt(&dec, plain, cipher, 2);
  5681. if (ret != 0)
  5682. return -5001;
  5683. if (plain[0] != msg1[0])
  5684. return -5002;
  5685. #endif /* HAVE_AES_DECRYPT */
  5686. #ifdef OPENSSL_EXTRA
  5687. ret = wc_AesSetKey(&enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  5688. if (ret != 0)
  5689. return -5003;
  5690. XMEMSET(cipher, 0, sizeof(cipher));
  5691. ret = wc_AesCfb1Encrypt(&enc, cipher, msg1,
  5692. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  5693. if (ret != 0)
  5694. return -5004;
  5695. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  5696. cipher, sizeof(msg1));
  5697. if (ret != 0) {
  5698. return ret;
  5699. }
  5700. #endif
  5701. #endif /* WOLFSSL_AES_128 */
  5702. #ifdef WOLFSSL_AES_192
  5703. /* 192 key tests */
  5704. ret = wc_AesSetKey(&enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  5705. if (ret != 0)
  5706. return -5005;
  5707. XMEMSET(cipher, 0, sizeof(cipher));
  5708. ret = wc_AesCfb1Encrypt(&enc, cipher, msg2, 4);
  5709. if (ret != 0)
  5710. return -5006;
  5711. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  5712. return -5007;
  5713. #ifdef OPENSSL_EXTRA
  5714. ret = wc_AesSetKey(&enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  5715. if (ret != 0)
  5716. return -5008;
  5717. XMEMSET(cipher, 0, sizeof(cipher));
  5718. ret = wc_AesCfb1Encrypt(&enc, cipher, msg2,
  5719. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  5720. if (ret != 0)
  5721. return -5009;
  5722. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  5723. cipher, sizeof(msg2));
  5724. if (ret != 0) {
  5725. return ret;
  5726. }
  5727. #endif
  5728. #endif /* WOLFSSL_AES_192 */
  5729. #ifdef WOLFSSL_AES_256
  5730. /* 256 key tests */
  5731. ret = wc_AesSetKey(&enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  5732. if (ret != 0)
  5733. return -5010;
  5734. XMEMSET(cipher, 0, sizeof(cipher));
  5735. ret = wc_AesCfb1Encrypt(&enc, cipher, msg3, 10);
  5736. if (ret != 0)
  5737. return -5011;
  5738. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  5739. return -5012;
  5740. #ifdef OPENSSL_EXTRA
  5741. ret = wc_AesSetKey(&enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  5742. if (ret != 0)
  5743. return -5013;
  5744. XMEMSET(cipher, 0, sizeof(cipher));
  5745. ret = wc_AesCfb1Encrypt(&enc, cipher, msg3,
  5746. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  5747. if (ret != 0)
  5748. return -5014;
  5749. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  5750. cipher, sizeof(msg3));
  5751. if (ret != 0) {
  5752. return ret;
  5753. }
  5754. #endif
  5755. #endif /* WOLFSSL_AES_256 */
  5756. return ret;
  5757. }
  5758. static int aescfb8_test(void)
  5759. {
  5760. Aes enc;
  5761. byte cipher[AES_BLOCK_SIZE];
  5762. #ifdef HAVE_AES_DECRYPT
  5763. Aes dec;
  5764. byte plain [AES_BLOCK_SIZE];
  5765. #endif
  5766. int ret = 0;
  5767. #ifdef WOLFSSL_AES_128
  5768. const byte iv[] = {
  5769. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  5770. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  5771. };
  5772. const byte key1[] =
  5773. {
  5774. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  5775. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  5776. };
  5777. const byte cipher1[] =
  5778. {
  5779. 0xd2,0x76,0x91
  5780. };
  5781. const byte msg1[] =
  5782. {
  5783. 0xc9,0x06,0x35
  5784. };
  5785. #endif /* WOLFSSL_AES_128 */
  5786. #ifdef WOLFSSL_AES_192
  5787. const byte iv2[] = {
  5788. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  5789. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  5790. };
  5791. const byte key2[] =
  5792. {
  5793. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  5794. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  5795. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  5796. };
  5797. const byte cipher2[] =
  5798. {
  5799. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  5800. 0xb5,0xe8
  5801. };
  5802. const byte msg2[] =
  5803. {
  5804. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  5805. 0x3a,0x60
  5806. };
  5807. #endif
  5808. #ifdef WOLFSSL_AES_256
  5809. const byte iv3[] = {
  5810. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  5811. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  5812. };
  5813. const byte key3[] =
  5814. {
  5815. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  5816. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  5817. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  5818. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  5819. };
  5820. const byte cipher3[] =
  5821. {
  5822. 0x1c,0xff,0x95
  5823. };
  5824. const byte msg3[] =
  5825. {
  5826. 0xb9,0x74,0xfa
  5827. };
  5828. #endif
  5829. if (wc_AesInit(&enc, HEAP_HINT, devId) != 0)
  5830. return -5015;
  5831. #ifdef HAVE_AES_DECRYPT
  5832. if (wc_AesInit(&dec, HEAP_HINT, devId) != 0)
  5833. return -5016;
  5834. #endif
  5835. #ifdef WOLFSSL_AES_128
  5836. /* 128 key tests */
  5837. #ifdef OPENSSL_EXTRA
  5838. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  5839. cipher1, sizeof(cipher1));
  5840. if (ret != 0) {
  5841. return ret;
  5842. }
  5843. #endif
  5844. ret = wc_AesSetKey(&enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  5845. if (ret != 0)
  5846. return -5017;
  5847. #ifdef HAVE_AES_DECRYPT
  5848. /* decrypt uses AES_ENCRYPTION */
  5849. ret = wc_AesSetKey(&dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  5850. if (ret != 0)
  5851. return -5018;
  5852. #endif
  5853. XMEMSET(cipher, 0, sizeof(cipher));
  5854. ret = wc_AesCfb8Encrypt(&enc, cipher, msg1, sizeof(msg1));
  5855. if (ret != 0)
  5856. return -5019;
  5857. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  5858. return -5020;
  5859. #ifdef HAVE_AES_DECRYPT
  5860. ret = wc_AesCfb8Decrypt(&dec, plain, cipher, sizeof(msg1));
  5861. if (ret != 0)
  5862. return -5021;
  5863. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  5864. return -5022;
  5865. #endif /* HAVE_AES_DECRYPT */
  5866. #endif /* WOLFSSL_AES_128 */
  5867. #ifdef WOLFSSL_AES_192
  5868. /* 192 key tests */
  5869. ret = wc_AesSetKey(&enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  5870. if (ret != 0)
  5871. return -5023;
  5872. XMEMSET(cipher, 0, sizeof(cipher));
  5873. ret = wc_AesCfb8Encrypt(&enc, cipher, msg2, sizeof(msg2));
  5874. if (ret != 0)
  5875. return -5024;
  5876. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  5877. return -5025;
  5878. #ifdef OPENSSL_EXTRA
  5879. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  5880. cipher2, sizeof(msg2));
  5881. if (ret != 0) {
  5882. return ret;
  5883. }
  5884. #endif
  5885. #endif /* WOLFSSL_AES_192 */
  5886. #ifdef WOLFSSL_AES_256
  5887. /* 256 key tests */
  5888. ret = wc_AesSetKey(&enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  5889. if (ret != 0)
  5890. return -5026;
  5891. XMEMSET(cipher, 0, sizeof(cipher));
  5892. ret = wc_AesCfb8Encrypt(&enc, cipher, msg3, sizeof(msg3));
  5893. if (ret != 0)
  5894. return -5027;
  5895. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  5896. return -5028;
  5897. #ifdef OPENSSL_EXTRA
  5898. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  5899. cipher3, sizeof(msg3));
  5900. if (ret != 0) {
  5901. return ret;
  5902. }
  5903. #endif
  5904. #endif /* WOLFSSL_AES_256 */
  5905. return ret;
  5906. }
  5907. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  5908. #endif /* WOLFSSL_AES_CFB */
  5909. static int aes_key_size_test(void)
  5910. {
  5911. int ret;
  5912. Aes aes;
  5913. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  5914. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  5915. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  5916. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  5917. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  5918. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  5919. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  5920. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  5921. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  5922. byte iv[] = "1234567890abcdef";
  5923. #ifndef HAVE_FIPS
  5924. word32 keySize;
  5925. #endif
  5926. #if !defined(HAVE_FIPS) || \
  5927. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  5928. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  5929. * supported with that FIPS version */
  5930. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  5931. if (ret != BAD_FUNC_ARG)
  5932. return -5100;
  5933. #endif
  5934. ret = wc_AesInit(&aes, HEAP_HINT, devId);
  5935. /* 0 check OK for FIPSv1 */
  5936. if (ret != 0)
  5937. return -5101;
  5938. #ifndef HAVE_FIPS
  5939. /* Parameter Validation testing. */
  5940. ret = wc_AesGetKeySize(NULL, NULL);
  5941. if (ret != BAD_FUNC_ARG)
  5942. return -5102;
  5943. ret = wc_AesGetKeySize(&aes, NULL);
  5944. if (ret != BAD_FUNC_ARG)
  5945. return -5103;
  5946. ret = wc_AesGetKeySize(NULL, &keySize);
  5947. if (ret != BAD_FUNC_ARG)
  5948. return -5104;
  5949. /* Crashes in FIPS */
  5950. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  5951. if (ret != BAD_FUNC_ARG)
  5952. return -5105;
  5953. #endif
  5954. /* NULL IV indicates to use all zeros IV. */
  5955. ret = wc_AesSetKey(&aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  5956. #ifdef WOLFSSL_AES_128
  5957. if (ret != 0)
  5958. #else
  5959. if (ret != BAD_FUNC_ARG)
  5960. #endif
  5961. return -5106;
  5962. ret = wc_AesSetKey(&aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  5963. if (ret != BAD_FUNC_ARG)
  5964. return -5107;
  5965. /* CryptoCell handles rounds internally */
  5966. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  5967. /* Force invalid rounds */
  5968. aes.rounds = 16;
  5969. ret = wc_AesGetKeySize(&aes, &keySize);
  5970. if (ret != BAD_FUNC_ARG)
  5971. return -5108;
  5972. #endif
  5973. ret = wc_AesSetKey(&aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  5974. #ifdef WOLFSSL_AES_128
  5975. if (ret != 0)
  5976. #else
  5977. if (ret != BAD_FUNC_ARG)
  5978. #endif
  5979. return -5109;
  5980. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  5981. ret = wc_AesGetKeySize(&aes, &keySize);
  5982. if (ret != 0 || keySize != sizeof(key16))
  5983. return -5110;
  5984. #endif
  5985. ret = wc_AesSetKey(&aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  5986. #ifdef WOLFSSL_AES_192
  5987. if (ret != 0)
  5988. #else
  5989. if (ret != BAD_FUNC_ARG)
  5990. #endif
  5991. return -5111;
  5992. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  5993. ret = wc_AesGetKeySize(&aes, &keySize);
  5994. if (ret != 0 || keySize != sizeof(key24))
  5995. return -5112;
  5996. #endif
  5997. ret = wc_AesSetKey(&aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  5998. #ifdef WOLFSSL_AES_256
  5999. if (ret != 0)
  6000. #else
  6001. if (ret != BAD_FUNC_ARG)
  6002. #endif
  6003. return -5113;
  6004. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  6005. ret = wc_AesGetKeySize(&aes, &keySize);
  6006. if (ret != 0 || keySize != sizeof(key32))
  6007. return -5114;
  6008. #endif
  6009. return 0;
  6010. }
  6011. #if defined(WOLFSSL_AES_XTS)
  6012. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  6013. #ifdef WOLFSSL_AES_128
  6014. static int aes_xts_128_test(void)
  6015. {
  6016. XtsAes aes;
  6017. int ret = 0;
  6018. unsigned char buf[AES_BLOCK_SIZE * 2];
  6019. unsigned char cipher[AES_BLOCK_SIZE * 2];
  6020. /* 128 key tests */
  6021. static unsigned char k1[] = {
  6022. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  6023. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  6024. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  6025. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  6026. };
  6027. static unsigned char i1[] = {
  6028. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  6029. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  6030. };
  6031. static unsigned char p1[] = {
  6032. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  6033. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  6034. };
  6035. /* plain text test of partial block is not from NIST test vector list */
  6036. static unsigned char pp[] = {
  6037. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  6038. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  6039. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  6040. };
  6041. static unsigned char c1[] = {
  6042. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  6043. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  6044. };
  6045. static unsigned char k2[] = {
  6046. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  6047. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  6048. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  6049. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  6050. };
  6051. static unsigned char i2[] = {
  6052. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  6053. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  6054. };
  6055. static unsigned char p2[] = {
  6056. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  6057. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  6058. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  6059. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  6060. };
  6061. static unsigned char c2[] = {
  6062. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  6063. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  6064. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  6065. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  6066. };
  6067. #ifdef OPENSSL_EXTRA
  6068. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  6069. if (ret != 0) {
  6070. printf("EVP_aes_128_xts failed!\n");
  6071. return ret;
  6072. }
  6073. #endif
  6074. XMEMSET(buf, 0, sizeof(buf));
  6075. if (wc_AesXtsSetKey(&aes, k2, sizeof(k2), AES_ENCRYPTION,
  6076. HEAP_HINT, devId) != 0)
  6077. return -5200;
  6078. ret = wc_AesXtsEncrypt(&aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  6079. #if defined(WOLFSSL_ASYNC_CRYPT)
  6080. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6081. #endif
  6082. if (ret != 0)
  6083. return -5201;
  6084. if (XMEMCMP(c2, buf, sizeof(c2)))
  6085. return -5202;
  6086. XMEMSET(buf, 0, sizeof(buf));
  6087. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_ENCRYPTION,
  6088. HEAP_HINT, devId) != 0)
  6089. return -5203;
  6090. ret = wc_AesXtsEncrypt(&aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  6091. #if defined(WOLFSSL_ASYNC_CRYPT)
  6092. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6093. #endif
  6094. if (ret != 0)
  6095. return -5204;
  6096. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  6097. return -5205;
  6098. /* partial block encryption test */
  6099. XMEMSET(cipher, 0, sizeof(cipher));
  6100. ret = wc_AesXtsEncrypt(&aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  6101. #if defined(WOLFSSL_ASYNC_CRYPT)
  6102. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6103. #endif
  6104. if (ret != 0)
  6105. return -5206;
  6106. wc_AesXtsFree(&aes);
  6107. /* partial block decrypt test */
  6108. XMEMSET(buf, 0, sizeof(buf));
  6109. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_DECRYPTION,
  6110. HEAP_HINT, devId) != 0)
  6111. return -5207;
  6112. ret = wc_AesXtsDecrypt(&aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  6113. #if defined(WOLFSSL_ASYNC_CRYPT)
  6114. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6115. #endif
  6116. if (ret != 0)
  6117. return -5208;
  6118. if (XMEMCMP(pp, buf, sizeof(pp)))
  6119. return -5209;
  6120. /* NIST decrypt test vector */
  6121. XMEMSET(buf, 0, sizeof(buf));
  6122. ret = wc_AesXtsDecrypt(&aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  6123. #if defined(WOLFSSL_ASYNC_CRYPT)
  6124. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6125. #endif
  6126. if (ret != 0)
  6127. return -5210;
  6128. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  6129. return -5211;
  6130. /* fail case with decrypting using wrong key */
  6131. XMEMSET(buf, 0, sizeof(buf));
  6132. ret = wc_AesXtsDecrypt(&aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  6133. #if defined(WOLFSSL_ASYNC_CRYPT)
  6134. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6135. #endif
  6136. if (ret != 0)
  6137. return -5212;
  6138. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  6139. return -5213;
  6140. /* set correct key and retest */
  6141. XMEMSET(buf, 0, sizeof(buf));
  6142. if (wc_AesXtsSetKey(&aes, k2, sizeof(k2), AES_DECRYPTION,
  6143. HEAP_HINT, devId) != 0)
  6144. return -5214;
  6145. ret = wc_AesXtsDecrypt(&aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  6146. #if defined(WOLFSSL_ASYNC_CRYPT)
  6147. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6148. #endif
  6149. if (ret != 0)
  6150. return -5215;
  6151. if (XMEMCMP(p2, buf, sizeof(p2)))
  6152. return -5216;
  6153. wc_AesXtsFree(&aes);
  6154. return ret;
  6155. }
  6156. #endif /* WOLFSSL_AES_128 */
  6157. #ifdef WOLFSSL_AES_256
  6158. static int aes_xts_256_test(void)
  6159. {
  6160. XtsAes aes;
  6161. int ret = 0;
  6162. unsigned char buf[AES_BLOCK_SIZE * 3];
  6163. unsigned char cipher[AES_BLOCK_SIZE * 3];
  6164. /* 256 key tests */
  6165. static unsigned char k1[] = {
  6166. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  6167. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  6168. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  6169. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  6170. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  6171. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  6172. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  6173. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  6174. };
  6175. static unsigned char i1[] = {
  6176. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  6177. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  6178. };
  6179. static unsigned char p1[] = {
  6180. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  6181. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  6182. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  6183. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  6184. };
  6185. /* plain text test of partial block is not from NIST test vector list */
  6186. static unsigned char pp[] = {
  6187. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  6188. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  6189. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  6190. };
  6191. static unsigned char c1[] = {
  6192. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  6193. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  6194. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  6195. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  6196. };
  6197. static unsigned char k2[] = {
  6198. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  6199. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  6200. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  6201. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  6202. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  6203. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  6204. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  6205. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  6206. };
  6207. static unsigned char i2[] = {
  6208. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  6209. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  6210. };
  6211. static unsigned char p2[] = {
  6212. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  6213. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  6214. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  6215. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  6216. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  6217. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  6218. };
  6219. static unsigned char c2[] = {
  6220. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  6221. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  6222. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  6223. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  6224. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  6225. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  6226. };
  6227. #ifdef OPENSSL_EXTRA
  6228. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  6229. if (ret != 0) {
  6230. printf("EVP_aes_256_xts failed\n");
  6231. return ret;
  6232. }
  6233. #endif
  6234. XMEMSET(buf, 0, sizeof(buf));
  6235. if (wc_AesXtsSetKey(&aes, k2, sizeof(k2), AES_ENCRYPTION,
  6236. HEAP_HINT, devId) != 0)
  6237. return -5300;
  6238. ret = wc_AesXtsEncrypt(&aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  6239. #if defined(WOLFSSL_ASYNC_CRYPT)
  6240. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6241. #endif
  6242. if (ret != 0)
  6243. return -5301;
  6244. if (XMEMCMP(c2, buf, sizeof(c2)))
  6245. return -5302;
  6246. XMEMSET(buf, 0, sizeof(buf));
  6247. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_ENCRYPTION,
  6248. HEAP_HINT, devId) != 0)
  6249. return -5303;
  6250. ret = wc_AesXtsEncrypt(&aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  6251. #if defined(WOLFSSL_ASYNC_CRYPT)
  6252. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6253. #endif
  6254. if (ret != 0)
  6255. return -5304;
  6256. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  6257. return -5305;
  6258. /* partial block encryption test */
  6259. XMEMSET(cipher, 0, sizeof(cipher));
  6260. ret = wc_AesXtsEncrypt(&aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  6261. #if defined(WOLFSSL_ASYNC_CRYPT)
  6262. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6263. #endif
  6264. if (ret != 0)
  6265. return -5306;
  6266. wc_AesXtsFree(&aes);
  6267. /* partial block decrypt test */
  6268. XMEMSET(buf, 0, sizeof(buf));
  6269. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_DECRYPTION,
  6270. HEAP_HINT, devId) != 0)
  6271. return -5307;
  6272. ret = wc_AesXtsDecrypt(&aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  6273. #if defined(WOLFSSL_ASYNC_CRYPT)
  6274. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6275. #endif
  6276. if (ret != 0)
  6277. return -5308;
  6278. if (XMEMCMP(pp, buf, sizeof(pp)))
  6279. return -5309;
  6280. /* NIST decrypt test vector */
  6281. XMEMSET(buf, 0, sizeof(buf));
  6282. ret = wc_AesXtsDecrypt(&aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  6283. #if defined(WOLFSSL_ASYNC_CRYPT)
  6284. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6285. #endif
  6286. if (ret != 0)
  6287. return -5310;
  6288. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  6289. return -5311;
  6290. XMEMSET(buf, 0, sizeof(buf));
  6291. if (wc_AesXtsSetKey(&aes, k2, sizeof(k2), AES_DECRYPTION,
  6292. HEAP_HINT, devId) != 0)
  6293. return -5312;
  6294. ret = wc_AesXtsDecrypt(&aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  6295. #if defined(WOLFSSL_ASYNC_CRYPT)
  6296. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6297. #endif
  6298. if (ret != 0)
  6299. return -5313;
  6300. if (XMEMCMP(p2, buf, sizeof(p2)))
  6301. return -5314;
  6302. wc_AesXtsFree(&aes);
  6303. return ret;
  6304. }
  6305. #endif /* WOLFSSL_AES_256 */
  6306. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  6307. /* both 128 and 256 bit key test */
  6308. static int aes_xts_sector_test(void)
  6309. {
  6310. XtsAes aes;
  6311. int ret = 0;
  6312. unsigned char buf[AES_BLOCK_SIZE * 2];
  6313. /* 128 key tests */
  6314. static unsigned char k1[] = {
  6315. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  6316. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  6317. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  6318. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  6319. };
  6320. static unsigned char p1[] = {
  6321. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  6322. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  6323. };
  6324. static unsigned char c1[] = {
  6325. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  6326. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  6327. };
  6328. word64 s1 = 141;
  6329. /* 256 key tests */
  6330. static unsigned char k2[] = {
  6331. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  6332. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  6333. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  6334. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  6335. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  6336. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  6337. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  6338. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  6339. };
  6340. static unsigned char p2[] = {
  6341. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  6342. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  6343. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  6344. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  6345. };
  6346. static unsigned char c2[] = {
  6347. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  6348. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  6349. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  6350. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  6351. };
  6352. word64 s2 = 187;
  6353. XMEMSET(buf, 0, sizeof(buf));
  6354. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_ENCRYPTION,
  6355. HEAP_HINT, devId) != 0)
  6356. return -5400;
  6357. ret = wc_AesXtsEncryptSector(&aes, buf, p1, sizeof(p1), s1);
  6358. #if defined(WOLFSSL_ASYNC_CRYPT)
  6359. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6360. #endif
  6361. if (ret != 0)
  6362. return -5401;
  6363. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  6364. return -5402;
  6365. /* decrypt test */
  6366. XMEMSET(buf, 0, sizeof(buf));
  6367. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_DECRYPTION,
  6368. HEAP_HINT, devId) != 0)
  6369. return -5403;
  6370. ret = wc_AesXtsDecryptSector(&aes, buf, c1, sizeof(c1), s1);
  6371. #if defined(WOLFSSL_ASYNC_CRYPT)
  6372. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6373. #endif
  6374. if (ret != 0)
  6375. return -5404;
  6376. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  6377. return -5405;
  6378. wc_AesXtsFree(&aes);
  6379. /* 256 bit key tests */
  6380. XMEMSET(buf, 0, sizeof(buf));
  6381. if (wc_AesXtsSetKey(&aes, k2, sizeof(k2), AES_ENCRYPTION,
  6382. HEAP_HINT, devId) != 0)
  6383. return -5406;
  6384. ret = wc_AesXtsEncryptSector(&aes, buf, p2, sizeof(p2), s2);
  6385. #if defined(WOLFSSL_ASYNC_CRYPT)
  6386. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6387. #endif
  6388. if (ret != 0)
  6389. return -5407;
  6390. if (XMEMCMP(c2, buf, sizeof(c2)))
  6391. return -5408;
  6392. /* decrypt test */
  6393. XMEMSET(buf, 0, sizeof(buf));
  6394. if (wc_AesXtsSetKey(&aes, k2, sizeof(k2), AES_DECRYPTION,
  6395. HEAP_HINT, devId) != 0)
  6396. return -5409;
  6397. ret = wc_AesXtsDecryptSector(&aes, buf, c2, sizeof(c2), s2);
  6398. #if defined(WOLFSSL_ASYNC_CRYPT)
  6399. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6400. #endif
  6401. if (ret != 0)
  6402. return -5410;
  6403. if (XMEMCMP(p2, buf, sizeof(p2)))
  6404. return -5411;
  6405. wc_AesXtsFree(&aes);
  6406. return ret;
  6407. }
  6408. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  6409. #ifdef WOLFSSL_AES_128
  6410. /* testing of bad arguments */
  6411. static int aes_xts_args_test(void)
  6412. {
  6413. XtsAes aes;
  6414. int ret = 0;
  6415. unsigned char buf[AES_BLOCK_SIZE * 2];
  6416. /* 128 key tests */
  6417. static unsigned char k1[] = {
  6418. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  6419. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  6420. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  6421. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  6422. };
  6423. static unsigned char p1[] = {
  6424. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  6425. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  6426. };
  6427. static unsigned char c1[] = {
  6428. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  6429. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  6430. };
  6431. word64 s1 = 141;
  6432. if (wc_AesXtsSetKey(NULL, k1, sizeof(k1), AES_ENCRYPTION,
  6433. HEAP_HINT, devId) == 0)
  6434. return -5500;
  6435. if (wc_AesXtsSetKey(&aes, NULL, sizeof(k1), AES_ENCRYPTION,
  6436. HEAP_HINT, devId) == 0)
  6437. return -5501;
  6438. /* encryption operations */
  6439. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_ENCRYPTION,
  6440. HEAP_HINT, devId) != 0)
  6441. return -5502;
  6442. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  6443. #if defined(WOLFSSL_ASYNC_CRYPT)
  6444. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6445. #endif
  6446. if (ret == 0)
  6447. return -5503;
  6448. ret = wc_AesXtsEncryptSector(&aes, NULL, p1, sizeof(p1), s1);
  6449. #if defined(WOLFSSL_ASYNC_CRYPT)
  6450. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6451. #endif
  6452. if (ret == 0)
  6453. return -5504;
  6454. wc_AesXtsFree(&aes);
  6455. /* decryption operations */
  6456. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_DECRYPTION,
  6457. HEAP_HINT, devId) != 0)
  6458. return -5505;
  6459. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  6460. #if defined(WOLFSSL_ASYNC_CRYPT)
  6461. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6462. #endif
  6463. if (ret == 0)
  6464. return -5506;
  6465. ret = wc_AesXtsDecryptSector(&aes, NULL, c1, sizeof(c1), s1);
  6466. #if defined(WOLFSSL_ASYNC_CRYPT)
  6467. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6468. #endif
  6469. if (ret == 0)
  6470. return -5507;
  6471. wc_AesXtsFree(&aes);
  6472. return 0;
  6473. }
  6474. #endif /* WOLFSSL_AES_128 */
  6475. #endif /* WOLFSSL_AES_XTS */
  6476. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  6477. static int aes_cbc_test(void)
  6478. {
  6479. byte cipher[AES_BLOCK_SIZE];
  6480. byte plain[AES_BLOCK_SIZE];
  6481. int ret;
  6482. const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  6483. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6484. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6485. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6486. };
  6487. byte key[] = "0123456789abcdef "; /* align */
  6488. byte iv[] = "1234567890abcdef "; /* align */
  6489. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  6490. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  6491. /* Parameter Validation testing. */
  6492. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  6493. if (ret != BAD_FUNC_ARG)
  6494. return -5600;
  6495. #ifdef HAVE_AES_DECRYPT
  6496. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  6497. if (ret != BAD_FUNC_ARG)
  6498. return -5601;
  6499. #endif
  6500. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  6501. AES_BLOCK_SIZE, iv);
  6502. if (ret != 0)
  6503. return -5602;
  6504. #ifdef HAVE_AES_DECRYPT
  6505. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  6506. AES_BLOCK_SIZE, iv);
  6507. if (ret != 0)
  6508. return -5603;
  6509. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  6510. return -5604;
  6511. #endif /* HAVE_AES_DECRYPT */
  6512. (void)plain;
  6513. return 0;
  6514. }
  6515. #endif
  6516. int aes_test(void)
  6517. {
  6518. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  6519. Aes enc;
  6520. byte cipher[AES_BLOCK_SIZE * 4];
  6521. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  6522. Aes dec;
  6523. byte plain [AES_BLOCK_SIZE * 4];
  6524. #endif
  6525. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER */
  6526. int ret = 0;
  6527. #ifdef HAVE_AES_CBC
  6528. #ifdef WOLFSSL_AES_128
  6529. const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  6530. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6531. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6532. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6533. };
  6534. const byte verify[] =
  6535. {
  6536. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  6537. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  6538. };
  6539. byte key[] = "0123456789abcdef "; /* align */
  6540. byte iv[] = "1234567890abcdef "; /* align */
  6541. if (wc_AesInit(&enc, HEAP_HINT, devId) != 0)
  6542. return -5700;
  6543. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  6544. if (wc_AesInit(&dec, HEAP_HINT, devId) != 0)
  6545. return -5701;
  6546. #endif
  6547. ret = wc_AesSetKey(&enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6548. if (ret != 0)
  6549. return -5702;
  6550. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  6551. ret = wc_AesSetKey(&dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  6552. if (ret != 0)
  6553. return -5703;
  6554. #endif
  6555. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  6556. ret = wc_AesCbcEncrypt(&enc, cipher, msg, AES_BLOCK_SIZE);
  6557. #if defined(WOLFSSL_ASYNC_CRYPT)
  6558. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6559. #endif
  6560. if (ret != 0)
  6561. return -5704;
  6562. #ifdef HAVE_AES_DECRYPT
  6563. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  6564. ret = wc_AesCbcDecrypt(&dec, plain, cipher, AES_BLOCK_SIZE);
  6565. #if defined(WOLFSSL_ASYNC_CRYPT)
  6566. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  6567. #endif
  6568. if (ret != 0)
  6569. return -5705;
  6570. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  6571. return -5706;
  6572. #endif /* HAVE_AES_DECRYPT */
  6573. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  6574. return -5707;
  6575. #endif /* WOLFSSL_AES_128 */
  6576. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  6577. {
  6578. const byte bigMsg[] = {
  6579. /* "All work and no play makes Jack a dull boy. " */
  6580. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  6581. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  6582. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  6583. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  6584. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  6585. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  6586. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  6587. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  6588. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  6589. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  6590. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  6591. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  6592. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  6593. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  6594. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  6595. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  6596. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  6597. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  6598. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  6599. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  6600. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  6601. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  6602. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  6603. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  6604. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  6605. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  6606. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  6607. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  6608. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  6609. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  6610. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  6611. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  6612. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  6613. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  6614. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  6615. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  6616. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  6617. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  6618. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  6619. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  6620. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  6621. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  6622. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  6623. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  6624. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  6625. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  6626. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  6627. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  6628. };
  6629. const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  6630. byte bigCipher[sizeof(bigMsg)];
  6631. byte bigPlain[sizeof(bigMsg)];
  6632. word32 keySz, msgSz;
  6633. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  6634. * message by AES_BLOCK_SIZE for each size of AES key. */
  6635. for (keySz = 16; keySz <= 32; keySz += 8) {
  6636. for (msgSz = AES_BLOCK_SIZE;
  6637. msgSz <= sizeof(bigMsg);
  6638. msgSz += AES_BLOCK_SIZE) {
  6639. XMEMSET(bigCipher, 0, sizeof(bigCipher));
  6640. XMEMSET(bigPlain, 0, sizeof(bigPlain));
  6641. ret = wc_AesSetKey(&enc, bigKey, keySz, iv, AES_ENCRYPTION);
  6642. if (ret != 0)
  6643. return -5708;
  6644. ret = wc_AesSetKey(&dec, bigKey, keySz, iv, AES_DECRYPTION);
  6645. if (ret != 0)
  6646. return -5709;
  6647. ret = wc_AesCbcEncrypt(&enc, bigCipher, bigMsg, msgSz);
  6648. #if defined(WOLFSSL_ASYNC_CRYPT)
  6649. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6650. #endif
  6651. if (ret != 0)
  6652. return -5710;
  6653. ret = wc_AesCbcDecrypt(&dec, bigPlain, bigCipher, msgSz);
  6654. #if defined(WOLFSSL_ASYNC_CRYPT)
  6655. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  6656. #endif
  6657. if (ret != 0)
  6658. return -5711;
  6659. if (XMEMCMP(bigPlain, bigMsg, msgSz))
  6660. return -5712;
  6661. }
  6662. }
  6663. }
  6664. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  6665. /* Test of AES IV state with encrypt/decrypt */
  6666. #ifdef WOLFSSL_AES_128
  6667. {
  6668. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  6669. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  6670. */
  6671. const byte msg2[] =
  6672. {
  6673. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  6674. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  6675. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  6676. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  6677. };
  6678. const byte verify2[] =
  6679. {
  6680. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  6681. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  6682. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  6683. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  6684. };
  6685. byte key2[] = {
  6686. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  6687. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  6688. };
  6689. byte iv2[] = {
  6690. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  6691. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  6692. };
  6693. ret = wc_AesSetKey(&enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6694. if (ret != 0)
  6695. return -5713;
  6696. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  6697. ret = wc_AesCbcEncrypt(&enc, cipher, msg2, AES_BLOCK_SIZE);
  6698. #if defined(WOLFSSL_ASYNC_CRYPT)
  6699. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6700. #endif
  6701. if (ret != 0)
  6702. return -5714;
  6703. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  6704. return -5715;
  6705. ret = wc_AesCbcEncrypt(&enc, cipher + AES_BLOCK_SIZE,
  6706. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  6707. #if defined(WOLFSSL_ASYNC_CRYPT)
  6708. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6709. #endif
  6710. if (ret != 0)
  6711. return -5716;
  6712. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  6713. AES_BLOCK_SIZE))
  6714. return -5717;
  6715. #if defined(HAVE_AES_DECRYPT)
  6716. ret = wc_AesSetKey(&dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  6717. if (ret != 0)
  6718. return -5718;
  6719. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  6720. ret = wc_AesCbcDecrypt(&dec, plain, verify2, AES_BLOCK_SIZE);
  6721. #if defined(WOLFSSL_ASYNC_CRYPT)
  6722. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  6723. #endif
  6724. if (ret != 0)
  6725. return -5719;
  6726. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  6727. return -5720;
  6728. ret = wc_AesCbcDecrypt(&dec, plain + AES_BLOCK_SIZE,
  6729. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  6730. #if defined(WOLFSSL_ASYNC_CRYPT)
  6731. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  6732. #endif
  6733. if (ret != 0)
  6734. return -5721;
  6735. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  6736. AES_BLOCK_SIZE))
  6737. return -5722;
  6738. #endif /* HAVE_AES_DECRYPT */
  6739. }
  6740. #endif /* WOLFSSL_AES_128 */
  6741. #endif /* HAVE_AES_CBC */
  6742. #ifdef WOLFSSL_AES_COUNTER
  6743. {
  6744. /* test vectors from "Recommendation for Block Cipher Modes of
  6745. * Operation" NIST Special Publication 800-38A */
  6746. const byte ctrIv[] =
  6747. {
  6748. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  6749. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  6750. };
  6751. const byte ctrPlain[] =
  6752. {
  6753. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6754. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6755. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6756. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6757. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6758. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6759. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6760. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6761. };
  6762. #ifdef WOLFSSL_AES_128
  6763. const byte oddCipher[] =
  6764. {
  6765. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  6766. 0xc2
  6767. };
  6768. const byte ctr128Key[] =
  6769. {
  6770. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  6771. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  6772. };
  6773. const byte ctr128Cipher[] =
  6774. {
  6775. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  6776. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  6777. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  6778. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  6779. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  6780. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  6781. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  6782. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  6783. };
  6784. #endif /* WOLFSSL_AES_128 */
  6785. #ifdef WOLFSSL_AES_192
  6786. const byte ctr192Key[] =
  6787. {
  6788. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  6789. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  6790. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  6791. };
  6792. const byte ctr192Cipher[] =
  6793. {
  6794. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  6795. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  6796. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  6797. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  6798. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  6799. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  6800. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  6801. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  6802. };
  6803. #endif
  6804. #ifdef WOLFSSL_AES_256
  6805. const byte ctr256Key[] =
  6806. {
  6807. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  6808. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  6809. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  6810. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  6811. };
  6812. const byte ctr256Cipher[] =
  6813. {
  6814. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  6815. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  6816. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  6817. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  6818. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  6819. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  6820. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  6821. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  6822. };
  6823. #endif
  6824. #ifdef WOLFSSL_AES_128
  6825. wc_AesSetKeyDirect(&enc, ctr128Key, sizeof(ctr128Key),
  6826. ctrIv, AES_ENCRYPTION);
  6827. /* Ctr only uses encrypt, even on key setup */
  6828. wc_AesSetKeyDirect(&dec, ctr128Key, sizeof(ctr128Key),
  6829. ctrIv, AES_ENCRYPTION);
  6830. ret = wc_AesCtrEncrypt(&enc, cipher, ctrPlain, sizeof(ctrPlain));
  6831. if (ret != 0) {
  6832. return -5723;
  6833. }
  6834. ret = wc_AesCtrEncrypt(&dec, plain, cipher, sizeof(ctrPlain));
  6835. if (ret != 0) {
  6836. return -5724;
  6837. }
  6838. if (XMEMCMP(plain, ctrPlain, sizeof(ctrPlain)))
  6839. return -5725;
  6840. if (XMEMCMP(cipher, ctr128Cipher, sizeof(ctr128Cipher)))
  6841. return -5726;
  6842. /* let's try with just 9 bytes, non block size test */
  6843. wc_AesSetKeyDirect(&enc, ctr128Key, AES_BLOCK_SIZE,
  6844. ctrIv, AES_ENCRYPTION);
  6845. /* Ctr only uses encrypt, even on key setup */
  6846. wc_AesSetKeyDirect(&dec, ctr128Key, AES_BLOCK_SIZE,
  6847. ctrIv, AES_ENCRYPTION);
  6848. ret = wc_AesCtrEncrypt(&enc, cipher, ctrPlain, sizeof(oddCipher));
  6849. if (ret != 0) {
  6850. return -5727;
  6851. }
  6852. ret = wc_AesCtrEncrypt(&dec, plain, cipher, sizeof(oddCipher));
  6853. if (ret != 0) {
  6854. return -5728;
  6855. }
  6856. if (XMEMCMP(plain, ctrPlain, sizeof(oddCipher)))
  6857. return -5729;
  6858. if (XMEMCMP(cipher, ctr128Cipher, sizeof(oddCipher)))
  6859. return -5730;
  6860. /* and an additional 9 bytes to reuse tmp left buffer */
  6861. ret = wc_AesCtrEncrypt(&enc, cipher, ctrPlain, sizeof(oddCipher));
  6862. if (ret != 0) {
  6863. return -5731;
  6864. }
  6865. ret = wc_AesCtrEncrypt(&dec, plain, cipher, sizeof(oddCipher));
  6866. if (ret != 0) {
  6867. return -5732;
  6868. }
  6869. if (XMEMCMP(plain, ctrPlain, sizeof(oddCipher)))
  6870. return -5733;
  6871. if (XMEMCMP(cipher, oddCipher, sizeof(oddCipher)))
  6872. return -5734;
  6873. #endif /* WOLFSSL_AES_128 */
  6874. #ifdef WOLFSSL_AES_192
  6875. /* 192 bit key */
  6876. wc_AesSetKeyDirect(&enc, ctr192Key, sizeof(ctr192Key),
  6877. ctrIv, AES_ENCRYPTION);
  6878. /* Ctr only uses encrypt, even on key setup */
  6879. wc_AesSetKeyDirect(&dec, ctr192Key, sizeof(ctr192Key),
  6880. ctrIv, AES_ENCRYPTION);
  6881. XMEMSET(plain, 0, sizeof(plain));
  6882. ret = wc_AesCtrEncrypt(&enc, plain, ctr192Cipher, sizeof(ctr192Cipher));
  6883. if (ret != 0) {
  6884. return -5735;
  6885. }
  6886. if (XMEMCMP(plain, ctrPlain, sizeof(ctr192Cipher)))
  6887. return -5736;
  6888. ret = wc_AesCtrEncrypt(&dec, cipher, ctrPlain, sizeof(ctrPlain));
  6889. if (ret != 0) {
  6890. return -5737;
  6891. }
  6892. if (XMEMCMP(ctr192Cipher, cipher, sizeof(ctr192Cipher)))
  6893. return -5738;
  6894. #endif /* WOLFSSL_AES_192 */
  6895. #ifdef WOLFSSL_AES_256
  6896. /* 256 bit key */
  6897. wc_AesSetKeyDirect(&enc, ctr256Key, sizeof(ctr256Key),
  6898. ctrIv, AES_ENCRYPTION);
  6899. /* Ctr only uses encrypt, even on key setup */
  6900. wc_AesSetKeyDirect(&dec, ctr256Key, sizeof(ctr256Key),
  6901. ctrIv, AES_ENCRYPTION);
  6902. XMEMSET(plain, 0, sizeof(plain));
  6903. ret = wc_AesCtrEncrypt(&enc, plain, ctr256Cipher, sizeof(ctr256Cipher));
  6904. if (ret != 0) {
  6905. return -5739;
  6906. }
  6907. if (XMEMCMP(plain, ctrPlain, sizeof(ctrPlain)))
  6908. return -5740;
  6909. ret = wc_AesCtrEncrypt(&dec, cipher, ctrPlain, sizeof(ctrPlain));
  6910. if (ret != 0) {
  6911. return -5741;
  6912. }
  6913. if (XMEMCMP(ctr256Cipher, cipher, sizeof(ctr256Cipher)))
  6914. return -5742;
  6915. #endif /* WOLFSSL_AES_256 */
  6916. }
  6917. #endif /* WOLFSSL_AES_COUNTER */
  6918. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  6919. {
  6920. const byte niPlain[] =
  6921. {
  6922. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6923. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  6924. };
  6925. const byte niCipher[] =
  6926. {
  6927. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  6928. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  6929. };
  6930. const byte niKey[] =
  6931. {
  6932. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  6933. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  6934. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  6935. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  6936. };
  6937. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  6938. ret = wc_AesSetKey(&enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  6939. if (ret != 0)
  6940. return -5743;
  6941. wc_AesEncryptDirect(&enc, cipher, niPlain);
  6942. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  6943. return -5744;
  6944. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  6945. ret = wc_AesSetKey(&dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  6946. if (ret != 0)
  6947. return -5745;
  6948. wc_AesDecryptDirect(&dec, plain, niCipher);
  6949. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  6950. return -5746;
  6951. }
  6952. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  6953. ret = aes_key_size_test();
  6954. if (ret != 0)
  6955. return ret;
  6956. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  6957. ret = aes_cbc_test();
  6958. if (ret != 0)
  6959. return ret;
  6960. #endif
  6961. #if defined(WOLFSSL_AES_XTS)
  6962. #ifdef WOLFSSL_AES_128
  6963. ret = aes_xts_128_test();
  6964. if (ret != 0)
  6965. return ret;
  6966. #endif
  6967. #ifdef WOLFSSL_AES_256
  6968. ret = aes_xts_256_test();
  6969. if (ret != 0)
  6970. return ret;
  6971. #endif
  6972. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  6973. ret = aes_xts_sector_test();
  6974. if (ret != 0)
  6975. return ret;
  6976. #endif
  6977. #ifdef WOLFSSL_AES_128
  6978. ret = aes_xts_args_test();
  6979. if (ret != 0)
  6980. return ret;
  6981. #endif
  6982. #endif
  6983. #if defined(WOLFSSL_AES_CFB)
  6984. ret = aescfb_test();
  6985. if (ret != 0)
  6986. return ret;
  6987. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6988. ret = aescfb1_test();
  6989. if (ret != 0)
  6990. return ret;
  6991. ret = aescfb8_test();
  6992. if (ret != 0)
  6993. return ret;
  6994. #endif
  6995. #endif
  6996. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER)
  6997. wc_AesFree(&enc);
  6998. (void)cipher;
  6999. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  7000. wc_AesFree(&dec);
  7001. (void)plain;
  7002. #endif
  7003. #endif
  7004. return ret;
  7005. }
  7006. #ifdef WOLFSSL_AES_192
  7007. int aes192_test(void)
  7008. {
  7009. #ifdef HAVE_AES_CBC
  7010. Aes enc;
  7011. byte cipher[AES_BLOCK_SIZE];
  7012. #ifdef HAVE_AES_DECRYPT
  7013. Aes dec;
  7014. byte plain[AES_BLOCK_SIZE];
  7015. #endif
  7016. #endif /* HAVE_AES_CBC */
  7017. int ret = 0;
  7018. #ifdef HAVE_AES_CBC
  7019. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  7020. * Appendix F.2.3 */
  7021. const byte msg[] = {
  7022. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7023. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  7024. };
  7025. const byte verify[] =
  7026. {
  7027. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  7028. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  7029. };
  7030. byte key[] = {
  7031. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  7032. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  7033. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  7034. };
  7035. byte iv[] = {
  7036. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  7037. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  7038. };
  7039. if (wc_AesInit(&enc, HEAP_HINT, devId) != 0)
  7040. return -5800;
  7041. #ifdef HAVE_AES_DECRYPT
  7042. if (wc_AesInit(&dec, HEAP_HINT, devId) != 0)
  7043. return -5801;
  7044. #endif
  7045. ret = wc_AesSetKey(&enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  7046. if (ret != 0)
  7047. return -5802;
  7048. #ifdef HAVE_AES_DECRYPT
  7049. ret = wc_AesSetKey(&dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  7050. if (ret != 0)
  7051. return -5803;
  7052. #endif
  7053. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7054. ret = wc_AesCbcEncrypt(&enc, cipher, msg, (int) sizeof(msg));
  7055. #if defined(WOLFSSL_ASYNC_CRYPT)
  7056. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7057. #endif
  7058. if (ret != 0)
  7059. return -5804;
  7060. #ifdef HAVE_AES_DECRYPT
  7061. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7062. ret = wc_AesCbcDecrypt(&dec, plain, cipher, (int) sizeof(cipher));
  7063. #if defined(WOLFSSL_ASYNC_CRYPT)
  7064. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  7065. #endif
  7066. if (ret != 0)
  7067. return -5805;
  7068. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  7069. return -5806;
  7070. }
  7071. #endif
  7072. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  7073. return -5807;
  7074. wc_AesFree(&enc);
  7075. #ifdef HAVE_AES_DECRYPT
  7076. wc_AesFree(&dec);
  7077. #endif
  7078. #endif /* HAVE_AES_CBC */
  7079. return ret;
  7080. }
  7081. #endif /* WOLFSSL_AES_192 */
  7082. #ifdef WOLFSSL_AES_256
  7083. int aes256_test(void)
  7084. {
  7085. #ifdef HAVE_AES_CBC
  7086. Aes enc;
  7087. byte cipher[AES_BLOCK_SIZE];
  7088. #ifdef HAVE_AES_DECRYPT
  7089. Aes dec;
  7090. byte plain[AES_BLOCK_SIZE];
  7091. #endif
  7092. #endif /* HAVE_AES_CBC */
  7093. int ret = 0;
  7094. #ifdef HAVE_AES_CBC
  7095. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  7096. * Appendix F.2.5 */
  7097. const byte msg[] = {
  7098. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7099. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  7100. };
  7101. const byte verify[] =
  7102. {
  7103. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  7104. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  7105. };
  7106. byte key[] = {
  7107. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7108. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7109. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7110. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7111. };
  7112. byte iv[] = {
  7113. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  7114. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  7115. };
  7116. if (wc_AesInit(&enc, HEAP_HINT, devId) != 0)
  7117. return -5900;
  7118. #ifdef HAVE_AES_DECRYPT
  7119. if (wc_AesInit(&dec, HEAP_HINT, devId) != 0)
  7120. return -5901;
  7121. #endif
  7122. ret = wc_AesSetKey(&enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  7123. if (ret != 0)
  7124. return -5902;
  7125. #ifdef HAVE_AES_DECRYPT
  7126. ret = wc_AesSetKey(&dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  7127. if (ret != 0)
  7128. return -5903;
  7129. #endif
  7130. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7131. ret = wc_AesCbcEncrypt(&enc, cipher, msg, (int) sizeof(msg));
  7132. #if defined(WOLFSSL_ASYNC_CRYPT)
  7133. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7134. #endif
  7135. if (ret != 0)
  7136. return -5904;
  7137. #ifdef HAVE_AES_DECRYPT
  7138. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7139. ret = wc_AesCbcDecrypt(&dec, plain, cipher, (int) sizeof(cipher));
  7140. #if defined(WOLFSSL_ASYNC_CRYPT)
  7141. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  7142. #endif
  7143. if (ret != 0)
  7144. return -5905;
  7145. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  7146. return -5906;
  7147. }
  7148. #endif
  7149. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  7150. return -5907;
  7151. wc_AesFree(&enc);
  7152. #ifdef HAVE_AES_DECRYPT
  7153. wc_AesFree(&dec);
  7154. #endif
  7155. #endif /* HAVE_AES_CBC */
  7156. return ret;
  7157. }
  7158. #endif /* WOLFSSL_AES_256 */
  7159. #ifdef HAVE_AESGCM
  7160. static int aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  7161. byte* plain, int plainSz, byte* cipher, int cipherSz,
  7162. byte* aad, int aadSz, byte* tag, int tagSz)
  7163. {
  7164. Aes enc;
  7165. Aes dec;
  7166. byte resultT[AES_BLOCK_SIZE];
  7167. byte resultP[AES_BLOCK_SIZE * 3];
  7168. byte resultC[AES_BLOCK_SIZE * 3];
  7169. int result;
  7170. XMEMSET(resultT, 0, sizeof(resultT));
  7171. XMEMSET(resultC, 0, sizeof(resultC));
  7172. XMEMSET(resultP, 0, sizeof(resultP));
  7173. if (wc_AesInit(&enc, HEAP_HINT, devId) != 0) {
  7174. return -5908;
  7175. }
  7176. if (wc_AesInit(&dec, HEAP_HINT, devId) != 0) {
  7177. return -5909;
  7178. }
  7179. result = wc_AesGcmSetKey(&enc, key, keySz);
  7180. if (result != 0)
  7181. return -5910;
  7182. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  7183. result = wc_AesGcmEncrypt(&enc, resultC, plain, plainSz, iv, ivSz,
  7184. resultT, tagSz, aad, aadSz);
  7185. #if defined(WOLFSSL_ASYNC_CRYPT)
  7186. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7187. #endif
  7188. if (result != 0)
  7189. return -5911;
  7190. if (cipher != NULL) {
  7191. if (XMEMCMP(cipher, resultC, cipherSz))
  7192. return -5912;
  7193. }
  7194. if (XMEMCMP(tag, resultT, tagSz))
  7195. return -5913;
  7196. wc_AesFree(&enc);
  7197. #ifdef HAVE_AES_DECRYPT
  7198. result = wc_AesGcmSetKey(&dec, key, keySz);
  7199. if (result != 0)
  7200. return -5914;
  7201. result = wc_AesGcmDecrypt(&dec, resultP, resultC, cipherSz,
  7202. iv, ivSz, resultT, tagSz, aad, aadSz);
  7203. #if defined(WOLFSSL_ASYNC_CRYPT)
  7204. result = wc_AsyncWait(result, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  7205. #endif
  7206. if (result != 0)
  7207. return -5915;
  7208. if (plain != NULL) {
  7209. if (XMEMCMP(plain, resultP, plainSz))
  7210. return -5916;
  7211. }
  7212. wc_AesFree(&dec);
  7213. #endif /* HAVE_AES_DECRYPT */
  7214. return 0;
  7215. }
  7216. /* tests that only use 12 byte IV and 16 or less byte AAD
  7217. * test vectors are from NIST SP 800-38D
  7218. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  7219. int aesgcm_default_test(void)
  7220. {
  7221. byte key1[] = {
  7222. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  7223. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  7224. };
  7225. byte iv1[] = {
  7226. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  7227. 0xe4, 0xed, 0x2f, 0x6d
  7228. };
  7229. ALIGN64 byte plain1[] = {
  7230. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  7231. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  7232. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  7233. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  7234. };
  7235. byte aad1[] = {
  7236. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  7237. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  7238. };
  7239. ALIGN64 byte cipher1[] = {
  7240. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  7241. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  7242. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  7243. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  7244. };
  7245. byte tag1[] = {
  7246. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  7247. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  7248. };
  7249. byte key2[] = {
  7250. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  7251. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  7252. };
  7253. byte iv2[] = {
  7254. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  7255. 0xc9, 0x8a, 0xff, 0xe3
  7256. };
  7257. ALIGN64 byte plain2[] = {
  7258. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  7259. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  7260. };
  7261. ALIGN64 byte cipher2[] = {
  7262. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  7263. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  7264. };
  7265. byte tag2[] = {
  7266. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  7267. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  7268. };
  7269. byte key3[] = {
  7270. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  7271. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  7272. };
  7273. byte iv3[] = {
  7274. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  7275. 0x6a, 0x4b, 0xd5, 0xe1
  7276. };
  7277. byte tag3[] = {
  7278. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  7279. 0x11, 0x64, 0xb2, 0xff
  7280. };
  7281. int ret;
  7282. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  7283. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  7284. aad1, sizeof(aad1), tag1, sizeof(tag1));
  7285. if (ret != 0) {
  7286. return ret;
  7287. }
  7288. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  7289. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  7290. NULL, 0, tag2, sizeof(tag2));
  7291. if (ret != 0) {
  7292. return ret;
  7293. }
  7294. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  7295. NULL, 0, NULL, 0,
  7296. NULL, 0, tag3, sizeof(tag3));
  7297. if (ret != 0) {
  7298. return ret;
  7299. }
  7300. return 0;
  7301. }
  7302. int aesgcm_test(void)
  7303. {
  7304. Aes enc;
  7305. Aes dec;
  7306. /*
  7307. * This is Test Case 16 from the document Galois/
  7308. * Counter Mode of Operation (GCM) by McGrew and
  7309. * Viega.
  7310. */
  7311. const byte p[] =
  7312. {
  7313. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  7314. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  7315. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  7316. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  7317. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  7318. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  7319. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  7320. 0xba, 0x63, 0x7b, 0x39
  7321. };
  7322. #if defined(WOLFSSL_AES_256)
  7323. const byte a[] =
  7324. {
  7325. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  7326. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  7327. 0xab, 0xad, 0xda, 0xd2
  7328. };
  7329. #endif
  7330. #ifdef WOLFSSL_AES_256
  7331. const byte k1[] =
  7332. {
  7333. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  7334. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  7335. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  7336. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  7337. };
  7338. const byte iv1[] =
  7339. {
  7340. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  7341. 0xde, 0xca, 0xf8, 0x88
  7342. };
  7343. const byte c1[] =
  7344. {
  7345. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  7346. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  7347. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  7348. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  7349. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  7350. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  7351. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  7352. 0xbc, 0xc9, 0xf6, 0x62
  7353. };
  7354. #endif /* WOLFSSL_AES_256 */
  7355. const byte t1[] =
  7356. {
  7357. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  7358. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  7359. };
  7360. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  7361. #if !defined(HAVE_FIPS) && \
  7362. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  7363. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  7364. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  7365. !(defined(WOLF_CRYPTO_CB) && \
  7366. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  7367. #define ENABLE_NON_12BYTE_IV_TEST
  7368. #ifdef WOLFSSL_AES_192
  7369. /* Test Case 12, uses same plaintext and AAD data. */
  7370. const byte k2[] =
  7371. {
  7372. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  7373. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  7374. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  7375. };
  7376. const byte iv2[] =
  7377. {
  7378. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  7379. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  7380. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  7381. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  7382. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  7383. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  7384. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  7385. 0xa6, 0x37, 0xb3, 0x9b
  7386. };
  7387. const byte c2[] =
  7388. {
  7389. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  7390. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  7391. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  7392. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  7393. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  7394. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  7395. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  7396. 0xe9, 0xb7, 0x37, 0x3b
  7397. };
  7398. const byte t2[] =
  7399. {
  7400. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  7401. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  7402. };
  7403. #endif /* WOLFSSL_AES_192 */
  7404. #ifdef WOLFSSL_AES_128
  7405. /* The following is an interesting test case from the example
  7406. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  7407. const byte p3[] =
  7408. {
  7409. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  7410. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  7411. };
  7412. const byte k3[] =
  7413. {
  7414. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  7415. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  7416. };
  7417. const byte iv3[] =
  7418. {
  7419. 0xca
  7420. };
  7421. const byte c3[] =
  7422. {
  7423. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  7424. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  7425. };
  7426. const byte a3[] =
  7427. {
  7428. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  7429. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  7430. };
  7431. const byte t3[] =
  7432. {
  7433. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  7434. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  7435. };
  7436. #endif /* WOLFSSL_AES_128 */
  7437. #ifdef WOLFSSL_AES_256
  7438. int ivlen;
  7439. #endif
  7440. #endif
  7441. byte resultT[sizeof(t1)];
  7442. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  7443. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  7444. int result = 0;
  7445. #ifdef WOLFSSL_AES_256
  7446. int alen;
  7447. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  7448. int plen;
  7449. #endif
  7450. #endif
  7451. #if !defined(BENCH_EMBEDDED)
  7452. #ifndef BENCH_AESGCM_LARGE
  7453. #define BENCH_AESGCM_LARGE 1024
  7454. #endif
  7455. byte large_input[BENCH_AESGCM_LARGE];
  7456. byte large_output[BENCH_AESGCM_LARGE + AES_BLOCK_SIZE];
  7457. byte large_outdec[BENCH_AESGCM_LARGE];
  7458. XMEMSET(large_input, 0, sizeof(large_input));
  7459. XMEMSET(large_output, 0, sizeof(large_output));
  7460. XMEMSET(large_outdec, 0, sizeof(large_outdec));
  7461. #endif
  7462. (void)result;
  7463. XMEMSET(resultT, 0, sizeof(resultT));
  7464. XMEMSET(resultC, 0, sizeof(resultC));
  7465. XMEMSET(resultP, 0, sizeof(resultP));
  7466. if (wc_AesInit(&enc, HEAP_HINT, devId) != 0) {
  7467. return -6100;
  7468. }
  7469. if (wc_AesInit(&dec, HEAP_HINT, devId) != 0) {
  7470. return -6101;
  7471. }
  7472. #ifdef WOLFSSL_AES_256
  7473. result = wc_AesGcmSetKey(&enc, k1, sizeof(k1));
  7474. if (result != 0)
  7475. return -6102;
  7476. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  7477. result = wc_AesGcmEncrypt(&enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  7478. resultT, sizeof(resultT), a, sizeof(a));
  7479. #if defined(WOLFSSL_ASYNC_CRYPT)
  7480. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7481. #endif
  7482. if (result != 0)
  7483. return -6103;
  7484. if (XMEMCMP(c1, resultC, sizeof(c1)))
  7485. return -6104;
  7486. if (XMEMCMP(t1, resultT, sizeof(resultT)))
  7487. return -6105;
  7488. #ifdef HAVE_AES_DECRYPT
  7489. result = wc_AesGcmSetKey(&dec, k1, sizeof(k1));
  7490. if (result != 0)
  7491. return -6106;
  7492. result = wc_AesGcmDecrypt(&dec, resultP, resultC, sizeof(c1),
  7493. iv1, sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  7494. #if defined(WOLFSSL_ASYNC_CRYPT)
  7495. result = wc_AsyncWait(result, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  7496. #endif
  7497. if (result != 0)
  7498. return -6107;
  7499. if (XMEMCMP(p, resultP, sizeof(p)))
  7500. return -6108;
  7501. #endif /* HAVE_AES_DECRYPT */
  7502. /* Large buffer test */
  7503. #ifdef BENCH_AESGCM_LARGE
  7504. /* setup test buffer */
  7505. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  7506. large_input[alen] = (byte)alen;
  7507. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  7508. result = wc_AesGcmEncrypt(&enc, large_output, large_input,
  7509. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  7510. resultT, sizeof(resultT), a, sizeof(a));
  7511. #if defined(WOLFSSL_ASYNC_CRYPT)
  7512. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7513. #endif
  7514. if (result != 0)
  7515. return -6109;
  7516. #ifdef HAVE_AES_DECRYPT
  7517. result = wc_AesGcmDecrypt(&dec, large_outdec, large_output,
  7518. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  7519. sizeof(resultT), a, sizeof(a));
  7520. #if defined(WOLFSSL_ASYNC_CRYPT)
  7521. result = wc_AsyncWait(result, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  7522. #endif
  7523. if (result != 0)
  7524. return -6110;
  7525. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  7526. return -6111;
  7527. #endif /* HAVE_AES_DECRYPT */
  7528. #endif /* BENCH_AESGCM_LARGE */
  7529. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  7530. /* Variable IV length test */
  7531. for (ivlen=1; ivlen<(int)sizeof(k1); ivlen++) {
  7532. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  7533. result = wc_AesGcmEncrypt(&enc, resultC, p, sizeof(p), k1,
  7534. (word32)ivlen, resultT, sizeof(resultT), a, sizeof(a));
  7535. #if defined(WOLFSSL_ASYNC_CRYPT)
  7536. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7537. #endif
  7538. if (result != 0)
  7539. return -6112;
  7540. #ifdef HAVE_AES_DECRYPT
  7541. result = wc_AesGcmDecrypt(&dec, resultP, resultC, sizeof(c1), k1,
  7542. (word32)ivlen, resultT, sizeof(resultT), a, sizeof(a));
  7543. #if defined(WOLFSSL_ASYNC_CRYPT)
  7544. result = wc_AsyncWait(result, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  7545. #endif
  7546. if (result != 0)
  7547. return -6113;
  7548. #endif /* HAVE_AES_DECRYPT */
  7549. }
  7550. #endif
  7551. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  7552. /* Variable authenticated data length test */
  7553. for (alen=0; alen<(int)sizeof(p); alen++) {
  7554. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  7555. result = wc_AesGcmEncrypt(&enc, resultC, p, sizeof(p), iv1,
  7556. sizeof(iv1), resultT, sizeof(resultT), p, (word32)alen);
  7557. #if defined(WOLFSSL_ASYNC_CRYPT)
  7558. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7559. #endif
  7560. if (result != 0)
  7561. return -6114;
  7562. #ifdef HAVE_AES_DECRYPT
  7563. result = wc_AesGcmDecrypt(&dec, resultP, resultC, sizeof(c1), iv1,
  7564. sizeof(iv1), resultT, sizeof(resultT), p, (word32)alen);
  7565. #if defined(WOLFSSL_ASYNC_CRYPT)
  7566. result = wc_AsyncWait(result, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  7567. #endif
  7568. if (result != 0)
  7569. return -6115;
  7570. #endif /* HAVE_AES_DECRYPT */
  7571. }
  7572. #endif
  7573. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  7574. #ifdef BENCH_AESGCM_LARGE
  7575. /* Variable plain text length test */
  7576. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  7577. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  7578. result = wc_AesGcmEncrypt(&enc, large_output, large_input,
  7579. plen, iv1, sizeof(iv1), resultT,
  7580. sizeof(resultT), a, sizeof(a));
  7581. #if defined(WOLFSSL_ASYNC_CRYPT)
  7582. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7583. #endif
  7584. if (result != 0)
  7585. return -6116;
  7586. #ifdef HAVE_AES_DECRYPT
  7587. result = wc_AesGcmDecrypt(&dec, large_outdec, large_output,
  7588. plen, iv1, sizeof(iv1), resultT,
  7589. sizeof(resultT), a, sizeof(a));
  7590. #if defined(WOLFSSL_ASYNC_CRYPT)
  7591. result = wc_AsyncWait(result, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  7592. #endif
  7593. if (result != 0)
  7594. return -6117;
  7595. #endif /* HAVE_AES_DECRYPT */
  7596. }
  7597. #else /* BENCH_AESGCM_LARGE */
  7598. /* Variable plain text length test */
  7599. for (plen=1; plen<(int)sizeof(p); plen++) {
  7600. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  7601. result = wc_AesGcmEncrypt(&enc, resultC, p, (word32)plen, iv1,
  7602. sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  7603. #if defined(WOLFSSL_ASYNC_CRYPT)
  7604. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7605. #endif
  7606. if (result != 0)
  7607. return -6118;
  7608. #ifdef HAVE_AES_DECRYPT
  7609. result = wc_AesGcmDecrypt(&dec, resultP, resultC, (word32)plen, iv1,
  7610. sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  7611. #if defined(WOLFSSL_ASYNC_CRYPT)
  7612. result = wc_AsyncWait(result, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  7613. #endif
  7614. if (result != 0)
  7615. return -6119;
  7616. #endif /* HAVE_AES_DECRYPT */
  7617. }
  7618. #endif /* BENCH_AESGCM_LARGE */
  7619. #endif
  7620. #endif /* WOLFSSL_AES_256 */
  7621. /* test with IV != 12 bytes */
  7622. #ifdef ENABLE_NON_12BYTE_IV_TEST
  7623. XMEMSET(resultT, 0, sizeof(resultT));
  7624. XMEMSET(resultC, 0, sizeof(resultC));
  7625. XMEMSET(resultP, 0, sizeof(resultP));
  7626. #ifdef WOLFSSL_AES_192
  7627. wc_AesGcmSetKey(&enc, k2, sizeof(k2));
  7628. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  7629. result = wc_AesGcmEncrypt(&enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  7630. resultT, sizeof(resultT), a, sizeof(a));
  7631. #if defined(WOLFSSL_ASYNC_CRYPT)
  7632. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7633. #endif
  7634. if (result != 0)
  7635. return -6120;
  7636. if (XMEMCMP(c2, resultC, sizeof(c2)))
  7637. return -6121;
  7638. if (XMEMCMP(t2, resultT, sizeof(resultT)))
  7639. return -6122;
  7640. #ifdef HAVE_AES_DECRYPT
  7641. result = wc_AesGcmDecrypt(&enc, resultP, resultC, sizeof(c1),
  7642. iv2, sizeof(iv2), resultT, sizeof(resultT), a, sizeof(a));
  7643. #if defined(WOLFSSL_ASYNC_CRYPT)
  7644. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7645. #endif
  7646. if (result != 0)
  7647. return -6123;
  7648. if (XMEMCMP(p, resultP, sizeof(p)))
  7649. return -6124;
  7650. #endif /* HAVE_AES_DECRYPT */
  7651. XMEMSET(resultT, 0, sizeof(resultT));
  7652. XMEMSET(resultC, 0, sizeof(resultC));
  7653. XMEMSET(resultP, 0, sizeof(resultP));
  7654. #endif /* WOLFSSL_AES_192 */
  7655. #ifdef WOLFSSL_AES_128
  7656. wc_AesGcmSetKey(&enc, k3, sizeof(k3));
  7657. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  7658. result = wc_AesGcmEncrypt(&enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  7659. resultT, sizeof(t3), a3, sizeof(a3));
  7660. #if defined(WOLFSSL_ASYNC_CRYPT)
  7661. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7662. #endif
  7663. if (result != 0)
  7664. return -6125;
  7665. if (XMEMCMP(c3, resultC, sizeof(c3)))
  7666. return -6126;
  7667. if (XMEMCMP(t3, resultT, sizeof(t3)))
  7668. return -6127;
  7669. #ifdef HAVE_AES_DECRYPT
  7670. result = wc_AesGcmDecrypt(&enc, resultP, resultC, sizeof(c3),
  7671. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  7672. #if defined(WOLFSSL_ASYNC_CRYPT)
  7673. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7674. #endif
  7675. if (result != 0)
  7676. return -6128;
  7677. if (XMEMCMP(p3, resultP, sizeof(p3)))
  7678. return -6129;
  7679. #endif /* HAVE_AES_DECRYPT */
  7680. #endif /* WOLFSSL_AES_128 */
  7681. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  7682. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  7683. !defined(WOLFSSL_XILINX_CRYPT) && \
  7684. !(defined(WOLF_CRYPTO_CB) && \
  7685. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  7686. XMEMSET(resultT, 0, sizeof(resultT));
  7687. XMEMSET(resultC, 0, sizeof(resultC));
  7688. XMEMSET(resultP, 0, sizeof(resultP));
  7689. wc_AesGcmSetKey(&enc, k1, sizeof(k1));
  7690. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  7691. result = wc_AesGcmEncrypt(&enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  7692. resultT + 1, sizeof(resultT) - 1, a, sizeof(a));
  7693. #if defined(WOLFSSL_ASYNC_CRYPT)
  7694. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7695. #endif
  7696. if (result != 0)
  7697. return -6130;
  7698. if (XMEMCMP(c1, resultC, sizeof(c1)))
  7699. return -6131;
  7700. if (XMEMCMP(t1, resultT + 1, sizeof(resultT) - 1))
  7701. return -6132;
  7702. #ifdef HAVE_AES_DECRYPT
  7703. result = wc_AesGcmDecrypt(&enc, resultP, resultC, sizeof(p),
  7704. iv1, sizeof(iv1), resultT + 1, sizeof(resultT) - 1, a, sizeof(a));
  7705. #if defined(WOLFSSL_ASYNC_CRYPT)
  7706. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7707. #endif
  7708. if (result != 0)
  7709. return -6133;
  7710. if (XMEMCMP(p, resultP, sizeof(p)))
  7711. return -6134;
  7712. #endif /* HAVE_AES_DECRYPT */
  7713. #endif /* WOLFSSL_AES_256 */
  7714. #if !defined(HAVE_FIPS) || \
  7715. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  7716. /* Test encrypt with internally generated IV */
  7717. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  7718. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  7719. {
  7720. WC_RNG rng;
  7721. byte randIV[12];
  7722. result = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  7723. if (result != 0)
  7724. return -6135;
  7725. XMEMSET(randIV, 0, sizeof(randIV));
  7726. XMEMSET(resultT, 0, sizeof(resultT));
  7727. XMEMSET(resultC, 0, sizeof(resultC));
  7728. XMEMSET(resultP, 0, sizeof(resultP));
  7729. wc_AesGcmSetKey(&enc, k1, sizeof(k1));
  7730. result = wc_AesGcmSetIV(&enc, sizeof(randIV), NULL, 0, &rng);
  7731. if (result != 0)
  7732. return -6136;
  7733. result = wc_AesGcmEncrypt_ex(&enc,
  7734. resultC, p, sizeof(p),
  7735. randIV, sizeof(randIV),
  7736. resultT, sizeof(resultT),
  7737. a, sizeof(a));
  7738. #if defined(WOLFSSL_ASYNC_CRYPT)
  7739. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  7740. #endif
  7741. if (result != 0)
  7742. return -6137;
  7743. /* Check the IV has been set. */
  7744. {
  7745. word32 i, ivSum = 0;
  7746. for (i = 0; i < sizeof(randIV); i++)
  7747. ivSum += randIV[i];
  7748. if (ivSum == 0)
  7749. return -6138;
  7750. }
  7751. #ifdef HAVE_AES_DECRYPT
  7752. wc_AesGcmSetKey(&dec, k1, sizeof(k1));
  7753. result = wc_AesGcmSetIV(&dec, sizeof(randIV), NULL, 0, &rng);
  7754. if (result != 0)
  7755. return -6139;
  7756. result = wc_AesGcmDecrypt(&dec,
  7757. resultP, resultC, sizeof(c1),
  7758. randIV, sizeof(randIV),
  7759. resultT, sizeof(resultT),
  7760. a, sizeof(a));
  7761. #if defined(WOLFSSL_ASYNC_CRYPT)
  7762. result = wc_AsyncWait(result, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  7763. #endif
  7764. if (result != 0)
  7765. return -6140;
  7766. if (XMEMCMP(p, resultP, sizeof(p)))
  7767. return -6141;
  7768. #endif /* HAVE_AES_DECRYPT */
  7769. wc_FreeRng(&rng);
  7770. }
  7771. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  7772. #endif /* HAVE_FIPS_VERSION >= 2 */
  7773. wc_AesFree(&enc);
  7774. wc_AesFree(&dec);
  7775. return 0;
  7776. }
  7777. #ifdef WOLFSSL_AES_128
  7778. int gmac_test(void)
  7779. {
  7780. Gmac gmac;
  7781. const byte k1[] =
  7782. {
  7783. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  7784. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  7785. };
  7786. const byte iv1[] =
  7787. {
  7788. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  7789. 0xe2, 0x8c, 0x8f, 0x16
  7790. };
  7791. const byte a1[] =
  7792. {
  7793. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  7794. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  7795. };
  7796. const byte t1[] =
  7797. {
  7798. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  7799. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  7800. };
  7801. #if !defined(HAVE_FIPS) || \
  7802. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  7803. /* FIPS builds only allow 16-byte auth tags. */
  7804. /* This sample uses a 15-byte auth tag. */
  7805. const byte k2[] =
  7806. {
  7807. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  7808. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  7809. };
  7810. const byte iv2[] =
  7811. {
  7812. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  7813. 0x1a, 0x60, 0x24, 0xa7
  7814. };
  7815. const byte a2[] =
  7816. {
  7817. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  7818. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  7819. };
  7820. const byte t2[] =
  7821. {
  7822. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  7823. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  7824. };
  7825. #endif
  7826. byte tag[16];
  7827. XMEMSET(&gmac, 0, sizeof(Gmac)); /* clear context */
  7828. (void)wc_AesInit((Aes*)&gmac, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  7829. XMEMSET(tag, 0, sizeof(tag));
  7830. wc_GmacSetKey(&gmac, k1, sizeof(k1));
  7831. wc_GmacUpdate(&gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  7832. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  7833. return -6200;
  7834. #if !defined(HAVE_FIPS) || \
  7835. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  7836. XMEMSET(tag, 0, sizeof(tag));
  7837. wc_GmacSetKey(&gmac, k2, sizeof(k2));
  7838. wc_GmacUpdate(&gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  7839. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  7840. return -6201;
  7841. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  7842. {
  7843. const byte badT[] =
  7844. {
  7845. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  7846. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  7847. };
  7848. WC_RNG rng;
  7849. byte iv[12];
  7850. #ifndef HAVE_FIPS
  7851. if (wc_InitRng_ex(&rng, HEAP_HINT, devId) != 0)
  7852. return -6202;
  7853. #else
  7854. if (wc_InitRng(&rng) != 0)
  7855. return -6203;
  7856. #endif
  7857. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  7858. t1, sizeof(t1)) != 0)
  7859. return -6204;
  7860. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  7861. badT, sizeof(badT)) != AES_GCM_AUTH_E)
  7862. return -6205;
  7863. if (wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  7864. t2, sizeof(t2)) != 0)
  7865. return -6206;
  7866. XMEMSET(tag, 0, sizeof(tag));
  7867. XMEMSET(iv, 0, sizeof(iv));
  7868. if (wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  7869. tag, sizeof(tag), &rng) != 0)
  7870. return -6207;
  7871. if (wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  7872. tag, sizeof(tag)) != 0)
  7873. return -6208;
  7874. wc_FreeRng(&rng);
  7875. }
  7876. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  7877. #endif /* HAVE_FIPS */
  7878. return 0;
  7879. }
  7880. #endif /* WOLFSSL_AES_128 */
  7881. #endif /* HAVE_AESGCM */
  7882. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  7883. int aesccm_test(void)
  7884. {
  7885. Aes enc;
  7886. /* key */
  7887. const byte k[] =
  7888. {
  7889. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  7890. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  7891. };
  7892. /* nonce */
  7893. const byte iv[] =
  7894. {
  7895. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  7896. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  7897. };
  7898. /* plaintext */
  7899. const byte p[] =
  7900. {
  7901. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  7902. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  7903. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  7904. };
  7905. /* plaintext - long */
  7906. const byte pl[] =
  7907. {
  7908. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  7909. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  7910. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  7911. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  7912. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  7913. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7914. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  7915. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  7916. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  7917. 0x50
  7918. };
  7919. const byte a[] =
  7920. {
  7921. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  7922. };
  7923. /* ciphertext */
  7924. const byte c[] =
  7925. {
  7926. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  7927. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  7928. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  7929. };
  7930. /* tag - authentication */
  7931. const byte t[] =
  7932. {
  7933. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  7934. };
  7935. /* ciphertext - long */
  7936. const byte cl[] =
  7937. {
  7938. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  7939. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  7940. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  7941. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  7942. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  7943. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  7944. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  7945. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  7946. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  7947. 0x0b
  7948. };
  7949. /* tag - authentication - long */
  7950. const byte tl[] =
  7951. {
  7952. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  7953. };
  7954. byte t2[sizeof(t)];
  7955. byte p2[sizeof(p)];
  7956. byte c2[sizeof(c)];
  7957. byte iv2[sizeof(iv)];
  7958. byte pl2[sizeof(pl)];
  7959. byte cl2[sizeof(cl)];
  7960. byte tl2[sizeof(tl)];
  7961. int result;
  7962. XMEMSET(&enc, 0, sizeof(Aes)); /* clear context */
  7963. XMEMSET(t2, 0, sizeof(t2));
  7964. XMEMSET(c2, 0, sizeof(c2));
  7965. XMEMSET(p2, 0, sizeof(p2));
  7966. result = wc_AesCcmSetKey(&enc, k, sizeof(k));
  7967. if (result != 0)
  7968. return -6300;
  7969. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  7970. result = wc_AesCcmEncrypt(&enc, c2, p, sizeof(c2), iv, sizeof(iv),
  7971. t2, sizeof(t2), a, sizeof(a));
  7972. if (result != 0)
  7973. return -6301;
  7974. if (XMEMCMP(c, c2, sizeof(c2)))
  7975. return -6302;
  7976. if (XMEMCMP(t, t2, sizeof(t2)))
  7977. return -6303;
  7978. result = wc_AesCcmDecrypt(&enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  7979. t2, sizeof(t2), a, sizeof(a));
  7980. if (result != 0)
  7981. return -6304;
  7982. if (XMEMCMP(p, p2, sizeof(p2)))
  7983. return -6305;
  7984. /* Test the authentication failure */
  7985. t2[0]++; /* Corrupt the authentication tag. */
  7986. result = wc_AesCcmDecrypt(&enc, p2, c, sizeof(p2), iv, sizeof(iv),
  7987. t2, sizeof(t2), a, sizeof(a));
  7988. if (result == 0)
  7989. return -6306;
  7990. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  7991. * authentication fail. */
  7992. XMEMSET(c2, 0, sizeof(c2));
  7993. if (XMEMCMP(p2, c2, sizeof(p2)))
  7994. return -6307;
  7995. XMEMSET(&enc, 0, sizeof(Aes)); /* clear context */
  7996. XMEMSET(t2, 0, sizeof(t2));
  7997. XMEMSET(c2, 0, sizeof(c2));
  7998. XMEMSET(p2, 0, sizeof(p2));
  7999. XMEMSET(iv2, 0, sizeof(iv2));
  8000. #ifndef HAVE_SELFTEST
  8001. /* selftest build does not have wc_AesCcmSetNonce() or
  8002. * wc_AesCcmEncrypt_ex() */
  8003. if (wc_AesCcmSetKey(&enc, k, sizeof(k)) != 0)
  8004. return -6308;
  8005. if (wc_AesCcmSetNonce(&enc, iv, sizeof(iv)) != 0)
  8006. return -6309;
  8007. if (wc_AesCcmEncrypt_ex(&enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  8008. t2, sizeof(t2), a, sizeof(a)) != 0)
  8009. return -6310;
  8010. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  8011. return -6311;
  8012. if (XMEMCMP(c, c2, sizeof(c2)))
  8013. return -6312;
  8014. if (XMEMCMP(t, t2, sizeof(t2)))
  8015. return -6313;
  8016. #endif
  8017. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8018. /* test fail on invalid IV sizes */
  8019. result = wc_AesCcmSetKey(&enc, k, sizeof(k));
  8020. if (result != 0)
  8021. return -6314;
  8022. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  8023. result = wc_AesCcmEncrypt(&enc, c2, p, sizeof(c2), iv, sizeof(iv),
  8024. t2, 1, a, sizeof(a));
  8025. if (result == 0) {
  8026. return -6315;
  8027. }
  8028. #endif
  8029. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  8030. result = wc_AesCcmEncrypt(&enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  8031. tl2, sizeof(tl2), a, sizeof(a));
  8032. if (result != 0)
  8033. return -6301;
  8034. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  8035. return -6302;
  8036. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  8037. return -6303;
  8038. result = wc_AesCcmDecrypt(&enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  8039. tl2, sizeof(tl2), a, sizeof(a));
  8040. if (result != 0)
  8041. return -6304;
  8042. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  8043. return -6305;
  8044. return 0;
  8045. }
  8046. #endif /* HAVE_AESCCM WOLFSSL_AES_128 */
  8047. #ifdef HAVE_AES_KEYWRAP
  8048. #define MAX_KEYWRAP_TEST_OUTLEN 40
  8049. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  8050. typedef struct keywrapVector {
  8051. const byte* kek;
  8052. const byte* data;
  8053. const byte* verify;
  8054. word32 kekLen;
  8055. word32 dataLen;
  8056. word32 verifyLen;
  8057. } keywrapVector;
  8058. int aeskeywrap_test(void)
  8059. {
  8060. int wrapSz, plainSz, testSz, i;
  8061. /* test vectors from RFC 3394 (kek, data, verify) */
  8062. #ifdef WOLFSSL_AES_128
  8063. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  8064. const byte k1[] = {
  8065. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8066. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  8067. };
  8068. const byte d1[] = {
  8069. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  8070. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  8071. };
  8072. const byte v1[] = {
  8073. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  8074. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  8075. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  8076. };
  8077. #endif /* WOLFSSL_AES_128 */
  8078. #ifdef WOLFSSL_AES_192
  8079. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  8080. const byte k2[] = {
  8081. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8082. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  8083. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  8084. };
  8085. const byte d2[] = {
  8086. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  8087. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  8088. };
  8089. const byte v2[] = {
  8090. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  8091. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  8092. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  8093. };
  8094. #endif
  8095. #ifdef WOLFSSL_AES_256
  8096. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  8097. const byte k3[] = {
  8098. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8099. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  8100. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  8101. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  8102. };
  8103. const byte d3[] = {
  8104. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  8105. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  8106. };
  8107. const byte v3[] = {
  8108. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  8109. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  8110. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  8111. };
  8112. #endif
  8113. #ifdef WOLFSSL_AES_192
  8114. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  8115. const byte k4[] = {
  8116. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8117. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  8118. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  8119. };
  8120. const byte d4[] = {
  8121. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  8122. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  8123. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  8124. };
  8125. const byte v4[] = {
  8126. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  8127. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  8128. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  8129. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  8130. };
  8131. #endif
  8132. #ifdef WOLFSSL_AES_256
  8133. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  8134. const byte k5[] = {
  8135. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8136. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  8137. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  8138. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  8139. };
  8140. const byte d5[] = {
  8141. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  8142. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  8143. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  8144. };
  8145. const byte v5[] = {
  8146. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  8147. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  8148. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  8149. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  8150. };
  8151. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  8152. const byte k6[] = {
  8153. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8154. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  8155. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  8156. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  8157. };
  8158. const byte d6[] = {
  8159. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  8160. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  8161. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8162. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  8163. };
  8164. const byte v6[] = {
  8165. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  8166. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  8167. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  8168. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  8169. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  8170. };
  8171. #endif /* WOLFSSL_AES_256 */
  8172. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  8173. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  8174. const keywrapVector test_wrap[] =
  8175. {
  8176. #ifdef WOLFSSL_AES_128
  8177. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  8178. #endif
  8179. #ifdef WOLFSSL_AES_192
  8180. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  8181. #endif
  8182. #ifdef WOLFSSL_AES_256
  8183. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  8184. #endif
  8185. #ifdef WOLFSSL_AES_192
  8186. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  8187. #endif
  8188. #ifdef WOLFSSL_AES_256
  8189. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  8190. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  8191. #endif
  8192. };
  8193. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  8194. XMEMSET(output, 0, sizeof(output));
  8195. XMEMSET(plain, 0, sizeof(plain));
  8196. for (i = 0; i < testSz; i++) {
  8197. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  8198. test_wrap[i].data, test_wrap[i].dataLen,
  8199. output, sizeof(output), NULL);
  8200. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  8201. return -6400;
  8202. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  8203. return -6401;
  8204. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  8205. output, wrapSz,
  8206. plain, sizeof(plain), NULL);
  8207. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  8208. return -6402;
  8209. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  8210. return -6403 - i;
  8211. }
  8212. return 0;
  8213. }
  8214. #endif /* HAVE_AES_KEYWRAP */
  8215. #endif /* NO_AES */
  8216. #ifdef HAVE_CAMELLIA
  8217. enum {
  8218. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  8219. };
  8220. typedef struct {
  8221. int type;
  8222. const byte* plaintext;
  8223. const byte* iv;
  8224. const byte* ciphertext;
  8225. const byte* key;
  8226. word32 keySz;
  8227. int errorCode;
  8228. } test_vector_t;
  8229. int camellia_test(void)
  8230. {
  8231. /* Camellia ECB Test Plaintext */
  8232. static const byte pte[] =
  8233. {
  8234. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  8235. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  8236. };
  8237. /* Camellia ECB Test Initialization Vector */
  8238. static const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  8239. /* Test 1: Camellia ECB 128-bit key */
  8240. static const byte k1[] =
  8241. {
  8242. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  8243. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  8244. };
  8245. static const byte c1[] =
  8246. {
  8247. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  8248. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  8249. };
  8250. /* Test 2: Camellia ECB 192-bit key */
  8251. static const byte k2[] =
  8252. {
  8253. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  8254. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  8255. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  8256. };
  8257. static const byte c2[] =
  8258. {
  8259. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  8260. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  8261. };
  8262. /* Test 3: Camellia ECB 256-bit key */
  8263. static const byte k3[] =
  8264. {
  8265. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  8266. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  8267. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  8268. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  8269. };
  8270. static const byte c3[] =
  8271. {
  8272. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  8273. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  8274. };
  8275. /* Camellia CBC Test Plaintext */
  8276. static const byte ptc[] =
  8277. {
  8278. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  8279. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  8280. };
  8281. /* Camellia CBC Test Initialization Vector */
  8282. static const byte ivc[] =
  8283. {
  8284. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8285. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  8286. };
  8287. /* Test 4: Camellia-CBC 128-bit key */
  8288. static const byte k4[] =
  8289. {
  8290. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  8291. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  8292. };
  8293. static const byte c4[] =
  8294. {
  8295. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  8296. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  8297. };
  8298. /* Test 5: Camellia-CBC 192-bit key */
  8299. static const byte k5[] =
  8300. {
  8301. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  8302. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  8303. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  8304. };
  8305. static const byte c5[] =
  8306. {
  8307. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  8308. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  8309. };
  8310. /* Test 6: CBC 256-bit key */
  8311. static const byte k6[] =
  8312. {
  8313. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  8314. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  8315. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  8316. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  8317. };
  8318. static const byte c6[] =
  8319. {
  8320. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  8321. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  8322. };
  8323. byte out[CAMELLIA_BLOCK_SIZE];
  8324. Camellia cam;
  8325. int i, testsSz, ret;
  8326. const test_vector_t testVectors[] =
  8327. {
  8328. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  8329. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  8330. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  8331. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  8332. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  8333. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  8334. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  8335. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  8336. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  8337. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  8338. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  8339. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  8340. };
  8341. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  8342. for (i = 0; i < testsSz; i++) {
  8343. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  8344. testVectors[i].iv) != 0)
  8345. return testVectors[i].errorCode;
  8346. switch (testVectors[i].type) {
  8347. case CAM_ECB_ENC:
  8348. ret = wc_CamelliaEncryptDirect(&cam, out,
  8349. testVectors[i].plaintext);
  8350. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  8351. CAMELLIA_BLOCK_SIZE))
  8352. return testVectors[i].errorCode;
  8353. break;
  8354. case CAM_ECB_DEC:
  8355. ret = wc_CamelliaDecryptDirect(&cam, out,
  8356. testVectors[i].ciphertext);
  8357. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  8358. CAMELLIA_BLOCK_SIZE))
  8359. return testVectors[i].errorCode;
  8360. break;
  8361. case CAM_CBC_ENC:
  8362. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  8363. CAMELLIA_BLOCK_SIZE);
  8364. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  8365. CAMELLIA_BLOCK_SIZE))
  8366. return testVectors[i].errorCode;
  8367. break;
  8368. case CAM_CBC_DEC:
  8369. ret = wc_CamelliaCbcDecrypt(&cam, out,
  8370. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  8371. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  8372. CAMELLIA_BLOCK_SIZE))
  8373. return testVectors[i].errorCode;
  8374. break;
  8375. default:
  8376. break;
  8377. }
  8378. }
  8379. /* Setting the IV and checking it was actually set. */
  8380. ret = wc_CamelliaSetIV(&cam, ivc);
  8381. if (ret != 0 || XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
  8382. return -6500;
  8383. /* Setting the IV to NULL should be same as all zeros IV */
  8384. if (wc_CamelliaSetIV(&cam, NULL) != 0 ||
  8385. XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE))
  8386. return -6501;
  8387. /* First parameter should never be null */
  8388. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  8389. return -6502;
  8390. /* First parameter should never be null, check it fails */
  8391. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  8392. return -6503;
  8393. /* Key should have a size of 16, 24, or 32 */
  8394. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  8395. return -6504;
  8396. return 0;
  8397. }
  8398. #endif /* HAVE_CAMELLIA */
  8399. #ifdef HAVE_IDEA
  8400. int idea_test(void)
  8401. {
  8402. int ret;
  8403. word16 i, j;
  8404. Idea idea;
  8405. byte data[IDEA_BLOCK_SIZE];
  8406. /* Project NESSIE test vectors */
  8407. #define IDEA_NB_TESTS 6
  8408. #define IDEA_NB_TESTS_EXTRA 4
  8409. const byte v_key[IDEA_NB_TESTS][IDEA_KEY_SIZE] = {
  8410. { 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
  8411. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37 },
  8412. { 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57,
  8413. 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57 },
  8414. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8415. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F },
  8416. { 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  8417. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48 },
  8418. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8419. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F },
  8420. { 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  8421. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48 },
  8422. };
  8423. const byte v1_plain[IDEA_NB_TESTS][IDEA_BLOCK_SIZE] = {
  8424. { 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37 },
  8425. { 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57 },
  8426. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 },
  8427. { 0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84 },
  8428. { 0xDB, 0x2D, 0x4A, 0x92, 0xAA, 0x68, 0x27, 0x3F },
  8429. { 0xF1, 0x29, 0xA6, 0x60, 0x1E, 0xF6, 0x2A, 0x47 },
  8430. };
  8431. byte v1_cipher[IDEA_NB_TESTS][IDEA_BLOCK_SIZE] = {
  8432. { 0x54, 0xCF, 0x21, 0xE3, 0x89, 0xD8, 0x73, 0xEC },
  8433. { 0x85, 0x52, 0x4D, 0x41, 0x0E, 0xB4, 0x28, 0xAE },
  8434. { 0xF5, 0x26, 0xAB, 0x9A, 0x62, 0xC0, 0xD2, 0x58 },
  8435. { 0xC8, 0xFB, 0x51, 0xD3, 0x51, 0x66, 0x27, 0xA8 },
  8436. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 },
  8437. { 0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84 },
  8438. };
  8439. byte v1_cipher_100[IDEA_NB_TESTS_EXTRA][IDEA_BLOCK_SIZE] = {
  8440. { 0x12, 0x46, 0x2F, 0xD0, 0xFB, 0x3A, 0x63, 0x39 },
  8441. { 0x15, 0x61, 0xE8, 0xC9, 0x04, 0x54, 0x8B, 0xE9 },
  8442. { 0x42, 0x12, 0x2A, 0x94, 0xB0, 0xF6, 0xD2, 0x43 },
  8443. { 0x53, 0x4D, 0xCD, 0x48, 0xDD, 0xD5, 0xF5, 0x9C },
  8444. };
  8445. byte v1_cipher_1000[IDEA_NB_TESTS_EXTRA][IDEA_BLOCK_SIZE] = {
  8446. { 0x44, 0x1B, 0x38, 0x5C, 0x77, 0x29, 0x75, 0x34 },
  8447. { 0xF0, 0x4E, 0x58, 0x88, 0x44, 0x99, 0x22, 0x2D },
  8448. { 0xB3, 0x5F, 0x93, 0x7F, 0x6A, 0xA0, 0xCD, 0x1F },
  8449. { 0x9A, 0xEA, 0x46, 0x8F, 0x42, 0x9B, 0xBA, 0x15 },
  8450. };
  8451. /* CBC test */
  8452. const char *message = "International Data Encryption Algorithm";
  8453. byte msg_enc[40], msg_dec[40];
  8454. for (i = 0; i < IDEA_NB_TESTS; i++) {
  8455. /* Set encryption key */
  8456. XMEMSET(&idea, 0, sizeof(Idea));
  8457. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  8458. NULL, IDEA_ENCRYPTION);
  8459. if (ret != 0) {
  8460. printf("wc_IdeaSetKey (enc) failed\n");
  8461. return -6600;
  8462. }
  8463. /* Data encryption */
  8464. ret = wc_IdeaCipher(&idea, data, v1_plain[i]);
  8465. if (ret != 0 || XMEMCMP(&v1_cipher[i], data, IDEA_BLOCK_SIZE)) {
  8466. printf("Bad encryption\n");
  8467. return -6601;
  8468. }
  8469. /* Set decryption key */
  8470. XMEMSET(&idea, 0, sizeof(Idea));
  8471. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  8472. NULL, IDEA_DECRYPTION);
  8473. if (ret != 0) {
  8474. printf("wc_IdeaSetKey (dec) failed\n");
  8475. return -6602;
  8476. }
  8477. /* Data decryption */
  8478. ret = wc_IdeaCipher(&idea, data, data);
  8479. if (ret != 0 || XMEMCMP(v1_plain[i], data, IDEA_BLOCK_SIZE)) {
  8480. printf("Bad decryption\n");
  8481. return -6603;
  8482. }
  8483. /* Set encryption key */
  8484. XMEMSET(&idea, 0, sizeof(Idea));
  8485. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  8486. v_key[i], IDEA_ENCRYPTION);
  8487. if (ret != 0) {
  8488. printf("wc_IdeaSetKey (enc) failed\n");
  8489. return -6604;
  8490. }
  8491. XMEMSET(msg_enc, 0, sizeof(msg_enc));
  8492. ret = wc_IdeaCbcEncrypt(&idea, msg_enc, (byte *)message,
  8493. (word32)XSTRLEN(message)+1);
  8494. if (ret != 0) {
  8495. printf("wc_IdeaCbcEncrypt failed\n");
  8496. return -6605;
  8497. }
  8498. /* Set decryption key */
  8499. XMEMSET(&idea, 0, sizeof(Idea));
  8500. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  8501. v_key[i], IDEA_DECRYPTION);
  8502. if (ret != 0) {
  8503. printf("wc_IdeaSetKey (dec) failed\n");
  8504. return -6606;
  8505. }
  8506. XMEMSET(msg_dec, 0, sizeof(msg_dec));
  8507. ret = wc_IdeaCbcDecrypt(&idea, msg_dec, msg_enc,
  8508. (word32)XSTRLEN(message)+1);
  8509. if (ret != 0) {
  8510. printf("wc_IdeaCbcDecrypt failed\n");
  8511. return -6607;
  8512. }
  8513. if (XMEMCMP(message, msg_dec, (word32)XSTRLEN(message))) {
  8514. printf("Bad CBC decryption\n");
  8515. return -6608;
  8516. }
  8517. }
  8518. for (i = 0; i < IDEA_NB_TESTS_EXTRA; i++) {
  8519. /* Set encryption key */
  8520. XMEMSET(&idea, 0, sizeof(Idea));
  8521. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  8522. NULL, IDEA_ENCRYPTION);
  8523. if (ret != 0) {
  8524. printf("wc_IdeaSetKey (enc) failed\n");
  8525. return -6609;
  8526. }
  8527. /* 100 times data encryption */
  8528. XMEMCPY(data, v1_plain[i], IDEA_BLOCK_SIZE);
  8529. for (j = 0; j < 100; j++) {
  8530. ret = wc_IdeaCipher(&idea, data, data);
  8531. if (ret != 0) {
  8532. return -6610;
  8533. }
  8534. }
  8535. if (XMEMCMP(v1_cipher_100[i], data, IDEA_BLOCK_SIZE)) {
  8536. printf("Bad encryption (100 times)\n");
  8537. return -6611;
  8538. }
  8539. /* 1000 times data encryption */
  8540. XMEMCPY(data, v1_plain[i], IDEA_BLOCK_SIZE);
  8541. for (j = 0; j < 1000; j++) {
  8542. ret = wc_IdeaCipher(&idea, data, data);
  8543. if (ret != 0) {
  8544. return -6612;
  8545. }
  8546. }
  8547. if (XMEMCMP(v1_cipher_1000[i], data, IDEA_BLOCK_SIZE)) {
  8548. printf("Bad encryption (100 times)\n");
  8549. return -6613;
  8550. }
  8551. }
  8552. #ifndef WC_NO_RNG
  8553. /* random test for CBC */
  8554. {
  8555. WC_RNG rng;
  8556. byte key[IDEA_KEY_SIZE], iv[IDEA_BLOCK_SIZE],
  8557. rnd[1000], enc[1000], dec[1000];
  8558. /* random values */
  8559. #ifndef HAVE_FIPS
  8560. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  8561. #else
  8562. ret = wc_InitRng(&rng);
  8563. #endif
  8564. if (ret != 0)
  8565. return -6614;
  8566. for (i = 0; i < 1000; i++) {
  8567. /* random key */
  8568. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  8569. if (ret != 0)
  8570. return -6615;
  8571. /* random iv */
  8572. ret = wc_RNG_GenerateBlock(&rng, iv, sizeof(iv));
  8573. if (ret != 0)
  8574. return -6616;
  8575. /* random data */
  8576. ret = wc_RNG_GenerateBlock(&rng, rnd, sizeof(rnd));
  8577. if (ret != 0)
  8578. return -6617;
  8579. /* Set encryption key */
  8580. XMEMSET(&idea, 0, sizeof(Idea));
  8581. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, iv, IDEA_ENCRYPTION);
  8582. if (ret != 0) {
  8583. printf("wc_IdeaSetKey (enc) failed\n");
  8584. return -6618;
  8585. }
  8586. /* Data encryption */
  8587. XMEMSET(enc, 0, sizeof(enc));
  8588. ret = wc_IdeaCbcEncrypt(&idea, enc, rnd, sizeof(rnd));
  8589. if (ret != 0) {
  8590. printf("wc_IdeaCbcEncrypt failed\n");
  8591. return -6619;
  8592. }
  8593. /* Set decryption key */
  8594. XMEMSET(&idea, 0, sizeof(Idea));
  8595. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, iv, IDEA_DECRYPTION);
  8596. if (ret != 0) {
  8597. printf("wc_IdeaSetKey (enc) failed\n");
  8598. return -6620;
  8599. }
  8600. /* Data decryption */
  8601. XMEMSET(dec, 0, sizeof(dec));
  8602. ret = wc_IdeaCbcDecrypt(&idea, dec, enc, sizeof(enc));
  8603. if (ret != 0) {
  8604. printf("wc_IdeaCbcDecrypt failed\n");
  8605. return -6621;
  8606. }
  8607. if (XMEMCMP(rnd, dec, sizeof(rnd))) {
  8608. printf("Bad CBC decryption\n");
  8609. return -6622;
  8610. }
  8611. }
  8612. wc_FreeRng(&rng);
  8613. }
  8614. #endif /* WC_NO_RNG */
  8615. return 0;
  8616. }
  8617. #endif /* HAVE_IDEA */
  8618. #ifndef WC_NO_RNG
  8619. static int _rng_test(WC_RNG* rng, int errorOffset)
  8620. {
  8621. byte block[32];
  8622. int ret, i;
  8623. XMEMSET(block, 0, sizeof(block));
  8624. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  8625. if (ret != 0) {
  8626. ret = -6623;
  8627. goto exit;
  8628. }
  8629. /* Check for 0's */
  8630. for (i=0; i<(int)sizeof(block); i++) {
  8631. if (block[i] == 0) {
  8632. ret++;
  8633. }
  8634. }
  8635. /* All zeros count check */
  8636. if (ret >= (int)sizeof(block)) {
  8637. ret = -6624;
  8638. goto exit;
  8639. }
  8640. ret = wc_RNG_GenerateByte(rng, block);
  8641. if (ret != 0) {
  8642. ret = -6625;
  8643. goto exit;
  8644. }
  8645. /* Parameter validation testing. */
  8646. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  8647. if (ret != BAD_FUNC_ARG) {
  8648. ret = -6626;
  8649. goto exit;
  8650. }
  8651. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  8652. if (ret != BAD_FUNC_ARG) {
  8653. ret = -6627;
  8654. goto exit;
  8655. }
  8656. ret = wc_RNG_GenerateByte(NULL, block);
  8657. if (ret != BAD_FUNC_ARG) {
  8658. ret = -6628;
  8659. goto exit;
  8660. }
  8661. ret = wc_RNG_GenerateByte(rng, NULL);
  8662. if (ret != BAD_FUNC_ARG) {
  8663. ret = -6629;
  8664. goto exit;
  8665. }
  8666. ret = 0;
  8667. exit:
  8668. if (ret != 0)
  8669. ret += errorOffset;
  8670. return ret;
  8671. }
  8672. static int random_rng_test(void)
  8673. {
  8674. WC_RNG localRng;
  8675. WC_RNG* rng;
  8676. int ret;
  8677. rng = &localRng;
  8678. /* Test stack based RNG. */
  8679. #ifndef HAVE_FIPS
  8680. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  8681. #else
  8682. ret = wc_InitRng(rng);
  8683. #endif
  8684. if (ret != 0) return -6700;
  8685. ret = _rng_test(rng, -6300);
  8686. /* Make sure and free RNG */
  8687. wc_FreeRng(rng);
  8688. if (ret != 0) return ret;
  8689. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8690. {
  8691. byte nonce[8] = { 0 };
  8692. /* Test dynamic RNG. */
  8693. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  8694. if (rng == NULL) return -6701;
  8695. ret = _rng_test(rng, -6310);
  8696. wc_rng_free(rng);
  8697. }
  8698. #endif
  8699. return ret;
  8700. }
  8701. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  8702. int random_test(void)
  8703. {
  8704. const byte test1Entropy[] =
  8705. {
  8706. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  8707. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  8708. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  8709. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  8710. };
  8711. const byte test1Output[] =
  8712. {
  8713. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  8714. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  8715. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  8716. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  8717. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  8718. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  8719. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  8720. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  8721. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  8722. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  8723. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  8724. };
  8725. const byte test2EntropyA[] =
  8726. {
  8727. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  8728. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  8729. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  8730. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  8731. };
  8732. const byte test2EntropyB[] =
  8733. {
  8734. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  8735. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  8736. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  8737. };
  8738. const byte test2Output[] =
  8739. {
  8740. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  8741. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  8742. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  8743. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  8744. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  8745. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  8746. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  8747. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  8748. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  8749. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  8750. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  8751. };
  8752. byte output[WC_SHA256_DIGEST_SIZE * 4];
  8753. int ret;
  8754. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  8755. output, sizeof(output));
  8756. if (ret != 0)
  8757. return -6800;
  8758. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  8759. return -6801;
  8760. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  8761. test2EntropyB, sizeof(test2EntropyB),
  8762. output, sizeof(output));
  8763. if (ret != 0)
  8764. return -6802;
  8765. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  8766. return -6803;
  8767. /* Basic RNG generate block test */
  8768. if ((ret = random_rng_test()) != 0)
  8769. return ret;
  8770. /* Test the seed check function. */
  8771. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  8772. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  8773. {
  8774. word32 i, outputSz;
  8775. /* Repeat the same byte over and over. Should fail. */
  8776. outputSz = sizeof(output);
  8777. XMEMSET(output, 1, outputSz);
  8778. ret = wc_RNG_TestSeed(output, outputSz);
  8779. if (ret == 0)
  8780. return -6804;
  8781. /* Every byte of the entropy scratch is different,
  8782. * entropy is a single byte that shouldn't match. */
  8783. outputSz = (sizeof(word32) * 2) + 1;
  8784. for (i = 0; i < outputSz; i++)
  8785. output[i] = (byte)i;
  8786. ret = wc_RNG_TestSeed(output, outputSz);
  8787. if (ret != 0)
  8788. return -6805;
  8789. outputSz = sizeof(output);
  8790. for (i = 0; i < outputSz; i++)
  8791. output[i] = (byte)i;
  8792. ret = wc_RNG_TestSeed(output, outputSz);
  8793. if (ret != 0)
  8794. return -6806;
  8795. }
  8796. #endif
  8797. return 0;
  8798. }
  8799. #else
  8800. int random_test(void)
  8801. {
  8802. /* Basic RNG generate block test */
  8803. return random_rng_test();
  8804. }
  8805. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  8806. #endif /* WC_NO_RNG */
  8807. #ifndef MEM_TEST_SZ
  8808. #define MEM_TEST_SZ 1024
  8809. #endif
  8810. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  8811. static int simple_mem_test(int sz)
  8812. {
  8813. int ret = 0;
  8814. byte* b;
  8815. int i;
  8816. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8817. if (b == NULL) {
  8818. return -6900;
  8819. }
  8820. /* utilize memory */
  8821. for (i = 0; i < sz; i++) {
  8822. b[i] = (byte)i;
  8823. }
  8824. /* read back and verify */
  8825. for (i = 0; i < sz; i++) {
  8826. if (b[i] != (byte)i) {
  8827. ret = -6901;
  8828. break;
  8829. }
  8830. }
  8831. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8832. return ret;
  8833. }
  8834. #endif
  8835. int memory_test(void)
  8836. {
  8837. int ret = 0;
  8838. #ifndef USE_FAST_MATH
  8839. byte* b = NULL;
  8840. #endif
  8841. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  8842. int i;
  8843. #endif
  8844. #ifdef WOLFSSL_STATIC_MEMORY
  8845. word32 size[] = { WOLFMEM_BUCKETS };
  8846. word32 dist[] = { WOLFMEM_DIST };
  8847. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  8848. int pad = -(int)((wolfssl_word)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  8849. /* pad to account for if head of buffer is not at set memory
  8850. * alignment when tests are ran */
  8851. #endif
  8852. #ifdef WOLFSSL_STATIC_MEMORY
  8853. /* check macro settings */
  8854. if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  8855. return -7000;
  8856. }
  8857. if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  8858. return -7001;
  8859. }
  8860. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  8861. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  8862. /* each element in array should be divisible by alignment size */
  8863. return -7002;
  8864. }
  8865. }
  8866. for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
  8867. if (size[i - 1] >= size[i]) {
  8868. return -7003; /* sizes should be in increasing order */
  8869. }
  8870. }
  8871. /* check that padding size returned is possible */
  8872. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  8873. return -7004; /* no room for wc_Memory struct */
  8874. }
  8875. if (wolfSSL_MemoryPaddingSz() < 0) {
  8876. return -7005;
  8877. }
  8878. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  8879. return -7006; /* not aligned! */
  8880. }
  8881. /* check function to return optimum buffer size (rounded down) */
  8882. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  8883. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  8884. return -7007; /* not aligned! */
  8885. }
  8886. if (ret < 0) {
  8887. return -7008;
  8888. }
  8889. if ((unsigned int)ret > sizeof(buffer)) {
  8890. return -7009; /* did not round down as expected */
  8891. }
  8892. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  8893. return -7010; /* return value changed when using suggested value */
  8894. }
  8895. ret = wolfSSL_MemoryPaddingSz();
  8896. ret += pad; /* add space that is going to be needed if buffer not aligned */
  8897. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  8898. (ret + (int)size[0])) {
  8899. return -7011; /* did not round down to nearest bucket value */
  8900. }
  8901. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  8902. if ((ret - pad) < 0) {
  8903. return -7012;
  8904. }
  8905. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  8906. return -7013; /* not even chunks of memory for IO size */
  8907. }
  8908. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  8909. return -7014; /* memory not aligned */
  8910. }
  8911. /* check for passing bad or unknown arguments to functions */
  8912. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  8913. return -7015;
  8914. }
  8915. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  8916. return -7016; /* should round to 0 since struct + bucket will not fit */
  8917. }
  8918. (void)dist; /* avoid static analysis warning of variable not used */
  8919. #endif
  8920. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  8921. /* simple test */
  8922. ret = simple_mem_test(MEM_TEST_SZ);
  8923. if (ret != 0)
  8924. return ret;
  8925. #endif
  8926. #ifdef COMPLEX_MEM_TEST
  8927. /* test various size blocks */
  8928. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  8929. ret = simple_mem_test(i);
  8930. if (ret != 0)
  8931. return ret;
  8932. }
  8933. #endif
  8934. #ifndef USE_FAST_MATH
  8935. /* realloc test */
  8936. b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8937. if (b) {
  8938. b = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  8939. DYNAMIC_TYPE_TMP_BUFFER);
  8940. }
  8941. if (b == NULL) {
  8942. return -7017;
  8943. }
  8944. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8945. #endif
  8946. return ret;
  8947. }
  8948. #ifdef HAVE_NTRU
  8949. byte GetEntropy(ENTROPY_CMD cmd, byte* out);
  8950. byte GetEntropy(ENTROPY_CMD cmd, byte* out)
  8951. {
  8952. static WC_RNG rng;
  8953. if (cmd == INIT)
  8954. return (wc_InitRng(&rng) == 0) ? 1 : 0;
  8955. if (out == NULL)
  8956. return 0;
  8957. if (cmd == GET_BYTE_OF_ENTROPY)
  8958. return (wc_RNG_GenerateBlock(&rng, out, 1) == 0) ? 1 : 0;
  8959. if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) {
  8960. *out = 1;
  8961. return 1;
  8962. }
  8963. return 0;
  8964. }
  8965. #endif /* HAVE_NTRU */
  8966. #ifndef NO_FILESYSTEM
  8967. /* Cert Paths */
  8968. #ifdef FREESCALE_MQX
  8969. #define CERT_PREFIX "a:\\"
  8970. #define CERT_PATH_SEP "\\"
  8971. #elif defined(WOLFSSL_uTKERNEL2)
  8972. #define CERT_PREFIX "/uda/"
  8973. #define CERT_PATH_SEP "/"
  8974. #else
  8975. #define CERT_PREFIX "./"
  8976. #define CERT_PATH_SEP "/"
  8977. #endif
  8978. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  8979. /* Generated Test Certs */
  8980. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  8981. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  8982. #if !defined(NO_RSA) && !defined(NO_ASN)
  8983. static const char* clientKey = CERT_ROOT "client-key.der";
  8984. static const char* clientCert = CERT_ROOT "client-cert.der";
  8985. #ifdef WOLFSSL_CERT_EXT
  8986. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  8987. #endif
  8988. #endif /* !NO_RSA && !NO_ASN */
  8989. #endif
  8990. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  8991. #if !defined(NO_RSA) && !defined(NO_ASN)
  8992. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  8993. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  8994. #ifdef WOLFSSL_CERT_GEN
  8995. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  8996. #endif
  8997. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  8998. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  8999. #endif
  9000. #ifdef HAVE_PKCS7
  9001. static const char* rsaServerCertDerFile =
  9002. CERT_ROOT "server-cert.der";
  9003. static const char* rsaServerKeyDerFile =
  9004. CERT_ROOT "server-key.der";
  9005. #endif
  9006. #endif
  9007. #endif /* !NO_RSA && !NO_ASN */
  9008. #endif /* !USE_CERT_BUFFER_* */
  9009. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  9010. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  9011. !defined(NO_ASN)
  9012. #ifndef NO_DH
  9013. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  9014. #ifdef WOLFSSL_DH_EXTRA
  9015. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  9016. #endif
  9017. #endif
  9018. #ifndef NO_DSA
  9019. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  9020. #endif
  9021. #endif /* !USE_CERT_BUFFER_* */
  9022. #if !defined(USE_CERT_BUFFERS_256)
  9023. #ifdef HAVE_ECC
  9024. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  9025. #ifdef HAVE_ECC_KEY_IMPORT
  9026. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  9027. #endif
  9028. #endif
  9029. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  9030. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  9031. #ifndef NO_RSA
  9032. /* eccKeyPubFile is used in a test that requires RSA. */
  9033. static const char* eccKeyPubFile = CERT_ROOT "ecc-keyPub.der";
  9034. #endif
  9035. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  9036. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  9037. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  9038. static const char* eccCaKey384File =
  9039. CERT_ROOT "ca-ecc384-key.der";
  9040. static const char* eccCaCert384File =
  9041. CERT_ROOT "ca-ecc384-cert.pem";
  9042. #endif
  9043. #endif
  9044. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  9045. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  9046. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  9047. #endif
  9048. #endif /* HAVE_ECC */
  9049. #ifdef HAVE_ED25519
  9050. #ifdef WOLFSSL_TEST_CERT
  9051. static const char* serverEd25519Cert =
  9052. CERT_ROOT "ed25519/server-ed25519.der";
  9053. static const char* caEd25519Cert =
  9054. CERT_ROOT "ed25519/ca-ed25519.der";
  9055. #endif
  9056. #endif
  9057. #ifdef HAVE_ED448
  9058. #ifdef WOLFSSL_TEST_CERT
  9059. static const char* serverEd448Cert =
  9060. CERT_ROOT "ed448/server-ed448.der";
  9061. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  9062. #endif
  9063. #endif
  9064. #endif /* !USE_CERT_BUFFER_* */
  9065. #ifndef NO_WRITE_TEMP_FILES
  9066. #ifdef HAVE_ECC
  9067. #ifdef WOLFSSL_CERT_GEN
  9068. static const char* certEccPemFile = CERT_PREFIX "certecc.pem";
  9069. #endif
  9070. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  9071. static const char* certEccRsaPemFile = CERT_PREFIX "certeccrsa.pem";
  9072. static const char* certEccRsaDerFile = CERT_PREFIX "certeccrsa.der";
  9073. #endif
  9074. static const char* eccCaKeyPemFile = CERT_PREFIX "ecc-key.pem";
  9075. static const char* eccPubKeyDerFile = CERT_PREFIX "ecc-public-key.der";
  9076. static const char* eccCaKeyTempFile = CERT_PREFIX "ecc-key.der";
  9077. #ifdef HAVE_PKCS8
  9078. static const char* eccPkcs8KeyDerFile = CERT_PREFIX "ecc-key-pkcs8.der";
  9079. #endif
  9080. #if defined(WOLFSSL_CERT_GEN) || \
  9081. (defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT))
  9082. static const char* certEccDerFile = CERT_PREFIX "certecc.der";
  9083. #endif
  9084. #endif /* HAVE_ECC */
  9085. #ifndef NO_RSA
  9086. #if defined(WOLFSSL_CERT_GEN) || \
  9087. (defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT))
  9088. static const char* otherCertDerFile = CERT_PREFIX "othercert.der";
  9089. static const char* certDerFile = CERT_PREFIX "cert.der";
  9090. #endif
  9091. #ifdef WOLFSSL_CERT_GEN
  9092. static const char* otherCertPemFile = CERT_PREFIX "othercert.pem";
  9093. static const char* certPemFile = CERT_PREFIX "cert.pem";
  9094. #endif
  9095. #ifdef WOLFSSL_CERT_REQ
  9096. static const char* certReqDerFile = CERT_PREFIX "certreq.der";
  9097. static const char* certReqPemFile = CERT_PREFIX "certreq.pem";
  9098. #endif
  9099. #endif /* !NO_RSA */
  9100. #if !defined(NO_RSA) || !defined(NO_DSA)
  9101. #ifdef WOLFSSL_KEY_GEN
  9102. static const char* keyDerFile = CERT_PREFIX "key.der";
  9103. static const char* keyPemFile = CERT_PREFIX "key.pem";
  9104. #endif
  9105. #endif
  9106. #endif /* !NO_WRITE_TEMP_FILES */
  9107. #endif /* !NO_FILESYSTEM */
  9108. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  9109. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  9110. #ifdef WOLFSSL_MULTI_ATTRIB
  9111. static CertName certDefaultName;
  9112. static void initDefaultName(void)
  9113. {
  9114. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  9115. certDefaultName.countryEnc = CTC_PRINTABLE;
  9116. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  9117. certDefaultName.stateEnc = CTC_UTF8;
  9118. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  9119. certDefaultName.localityEnc = CTC_UTF8;
  9120. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  9121. certDefaultName.surEnc = CTC_UTF8;
  9122. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  9123. certDefaultName.orgEnc = CTC_UTF8;
  9124. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  9125. certDefaultName.unitEnc = CTC_UTF8;
  9126. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  9127. certDefaultName.commonNameEnc = CTC_UTF8;
  9128. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  9129. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  9130. #ifdef WOLFSSL_CERT_EXT
  9131. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  9132. certDefaultName.busCatEnc = CTC_UTF8;
  9133. #endif
  9134. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  9135. #ifdef WOLFSSL_TEST_CERT
  9136. {
  9137. NameAttrib* n;
  9138. /* test having additional OUs and setting DC */
  9139. n = &certDefaultName.name[0];
  9140. n->id = ASN_ORGUNIT_NAME;
  9141. n->type = CTC_UTF8;
  9142. n->sz = sizeof("Development-2");
  9143. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  9144. #if CTC_MAX_ATTRIB > 3
  9145. n = &certDefaultName.name[1];
  9146. n->id = ASN_DOMAIN_COMPONENT;
  9147. n->type = CTC_UTF8;
  9148. n->sz = sizeof("com");
  9149. XMEMCPY(n->value, "com", sizeof("com"));
  9150. n = &certDefaultName.name[2];
  9151. n->id = ASN_DOMAIN_COMPONENT;
  9152. n->type = CTC_UTF8;
  9153. n->sz = sizeof("wolfssl");
  9154. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  9155. #endif
  9156. }
  9157. #endif /* WOLFSSL_TEST_CERT */
  9158. }
  9159. #else
  9160. static const CertName certDefaultName = {
  9161. "US", CTC_PRINTABLE, /* country */
  9162. "Oregon", CTC_UTF8, /* state */
  9163. "Portland", CTC_UTF8, /* locality */
  9164. "Test", CTC_UTF8, /* sur */
  9165. "wolfSSL", CTC_UTF8, /* org */
  9166. "Development", CTC_UTF8, /* unit */
  9167. "www.wolfssl.com", CTC_UTF8, /* commonName */
  9168. "wolfSSL12345", CTC_PRINTABLE, /* serial number of device */
  9169. #ifdef WOLFSSL_CERT_EXT
  9170. "Private Organization", CTC_UTF8, /* businessCategory */
  9171. "US", CTC_PRINTABLE, /* jurisdiction country */
  9172. "Oregon", CTC_PRINTABLE, /* jurisdiction state */
  9173. #endif
  9174. "info@wolfssl.com" /* email */
  9175. };
  9176. #endif /* WOLFSSL_MULTI_ATTRIB */
  9177. #ifdef WOLFSSL_CERT_EXT
  9178. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  9179. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  9180. static const char certKeyUsage[] =
  9181. "digitalSignature,nonRepudiation";
  9182. #endif
  9183. #if (defined(WOLFSSL_CERT_REQ) || defined(HAVE_NTRU)) && !defined(NO_RSA)
  9184. static const char certKeyUsage2[] =
  9185. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  9186. #endif
  9187. #endif /* WOLFSSL_CERT_EXT */
  9188. #endif /* WOLFSSL_CERT_GEN */
  9189. #ifndef NO_RSA
  9190. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  9191. !defined(NO_FILESYSTEM)
  9192. static byte minSerial[] = { 0x02, 0x01, 0x01 };
  9193. static byte minName[] = { 0x30, 0x00 };
  9194. static byte nameBad[] = {
  9195. 0x30, 0x08,
  9196. 0x31, 0x06,
  9197. 0x30, 0x04,
  9198. 0x06, 0x02,
  9199. 0x55, 0x04,
  9200. };
  9201. static byte minDates[] = {
  9202. 0x30, 0x1e,
  9203. 0x17, 0x0d,
  9204. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  9205. 0x32, 0x33, 0x31, 0x30, 0x5a,
  9206. 0x17, 0x0d,
  9207. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  9208. 0x32, 0x33, 0x31, 0x30, 0x5a
  9209. };
  9210. static byte minPubKey[] = {
  9211. 0x30, 0x1b,
  9212. 0x30, 0x0d,
  9213. 0x06, 0x09,
  9214. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  9215. 0x01,
  9216. 0x05, 0x00,
  9217. 0x03, 0x0b,
  9218. 0x00, 0x30, 0x08,
  9219. 0x02, 0x01,
  9220. 0x03,
  9221. 0x02, 0x03,
  9222. 0x01, 0x00, 0x01
  9223. };
  9224. static byte minSigAlg[] = {
  9225. 0x30, 0x0d,
  9226. 0x06, 0x09,
  9227. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  9228. 0x0b,
  9229. 0x05, 0x00
  9230. };
  9231. static byte minSig[] = {
  9232. 0x03, 0x01,
  9233. 0x00
  9234. };
  9235. static int add_seq(byte* certData, int offset, byte* data, byte length)
  9236. {
  9237. XMEMMOVE(certData + offset + 2, data, length);
  9238. certData[offset++] = 0x30;
  9239. certData[offset++] = length;
  9240. return offset + length;
  9241. }
  9242. static int add_data(byte* certData, int offset, byte* data, byte length)
  9243. {
  9244. XMEMCPY(certData + offset, data, length);
  9245. return offset + length;
  9246. }
  9247. static int cert_asn1_test(void)
  9248. {
  9249. int ret;
  9250. int len[3];
  9251. DecodedCert cert;
  9252. byte certData[106];
  9253. byte* badCert = NULL;
  9254. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  9255. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  9256. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  9257. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  9258. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  9259. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  9260. len[1] = add_seq(certData, 0, certData, len[2]);
  9261. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  9262. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  9263. len[0] = add_seq(certData, 0, certData, len[1]);
  9264. /* Minimal good certificate */
  9265. InitDecodedCert(&cert, certData, len[0], 0);
  9266. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  9267. FreeDecodedCert(&cert);
  9268. if (ret != 0) {
  9269. ERROR_OUT(-7100, done);
  9270. }
  9271. /* Bad issuer name */
  9272. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  9273. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  9274. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  9275. len[1] = add_seq(certData, 0, certData, len[2]);
  9276. len[0] = add_seq(certData, 0, certData, len[1]);
  9277. /* Put data into allocated buffer to allow access error checking. */
  9278. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9279. XMEMCPY(badCert, certData, len[0]);
  9280. InitDecodedCert(&cert, badCert, len[0], 0);
  9281. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  9282. FreeDecodedCert(&cert);
  9283. if (ret != ASN_PARSE_E) {
  9284. ERROR_OUT(-7101, done);
  9285. }
  9286. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9287. badCert = NULL;
  9288. ret = 0;
  9289. done:
  9290. if (badCert != NULL)
  9291. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9292. return ret;
  9293. }
  9294. int cert_test(void)
  9295. {
  9296. #if !defined(NO_FILESYSTEM)
  9297. DecodedCert cert;
  9298. byte* tmp;
  9299. size_t bytes;
  9300. XFILE file;
  9301. int ret;
  9302. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9303. if (tmp == NULL)
  9304. return -7200;
  9305. /* Certificate with Name Constraints extension. */
  9306. #ifdef FREESCALE_MQX
  9307. file = XFOPEN(".\\certs\\test\\cert-ext-nc.der", "rb");
  9308. #else
  9309. file = XFOPEN("./certs/test/cert-ext-nc.der", "rb");
  9310. #endif
  9311. if (!file) {
  9312. ERROR_OUT(-7201, done);
  9313. }
  9314. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  9315. XFCLOSE(file);
  9316. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  9317. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  9318. if (ret != 0) {
  9319. ERROR_OUT(-7202, done);
  9320. }
  9321. FreeDecodedCert(&cert);
  9322. /* Certificate with Inhibit Any Policy extension. */
  9323. #ifdef FREESCALE_MQX
  9324. file = XFOPEN(".\\certs\\test\\cert-ext-ia.der", "rb");
  9325. #else
  9326. file = XFOPEN("./certs/test/cert-ext-ia.der", "rb");
  9327. #endif
  9328. if (!file) {
  9329. ERROR_OUT(-7203, done);
  9330. }
  9331. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  9332. XFCLOSE(file);
  9333. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  9334. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  9335. if (ret != 0) {
  9336. ERROR_OUT(-7204, done);
  9337. }
  9338. FreeDecodedCert(&cert);
  9339. /* Certificate with Netscape Certificate Type extension. */
  9340. #ifdef FREESCALE_MQX
  9341. file = XFOPEN(".\\certs\\test\\cert-ext-nct.der", "rb");
  9342. #else
  9343. file = XFOPEN("./certs/test/cert-ext-nct.der", "rb");
  9344. #endif
  9345. if (!file) {
  9346. ERROR_OUT(-7203, done);
  9347. }
  9348. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  9349. XFCLOSE(file);
  9350. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  9351. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  9352. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  9353. if (ret != 0) {
  9354. ERROR_OUT(-7204, done);
  9355. }
  9356. #else
  9357. if (ret != ASN_CRIT_EXT_E) {
  9358. ERROR_OUT(-7205, done);
  9359. }
  9360. ret = 0;
  9361. #endif
  9362. done:
  9363. FreeDecodedCert(&cert);
  9364. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9365. #endif /* !NO_FILESYSTEM */
  9366. if (ret == 0)
  9367. ret = cert_asn1_test();
  9368. return ret;
  9369. }
  9370. #endif /* WOLFSSL_TEST_CERT */
  9371. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  9372. !defined(NO_FILESYSTEM)
  9373. int certext_test(void)
  9374. {
  9375. DecodedCert cert;
  9376. byte* tmp;
  9377. size_t bytes;
  9378. XFILE file;
  9379. int ret;
  9380. /* created from rsa_test : othercert.der */
  9381. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  9382. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  9383. /* created from rsa_test : othercert.der */
  9384. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  9385. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  9386. #ifdef HAVE_ECC
  9387. /* created from ecc_test_cert_gen : certecc.der */
  9388. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  9389. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  9390. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  9391. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  9392. #else
  9393. /* Authority key id from ./certs/ca-ecc-cert.pem */
  9394. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  9395. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  9396. #endif
  9397. #endif /* HAVE_ECC */
  9398. /* created from rsa_test : cert.der */
  9399. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  9400. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  9401. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9402. if (tmp == NULL)
  9403. return -7300;
  9404. /* load othercert.der (Cert signed by an authority) */
  9405. file = XFOPEN(otherCertDerFile, "rb");
  9406. if (!file) {
  9407. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  9408. return -7301;
  9409. }
  9410. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  9411. XFCLOSE(file);
  9412. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  9413. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  9414. if (ret != 0)
  9415. return -7302;
  9416. /* check the SKID from a RSA certificate */
  9417. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  9418. return -7303;
  9419. /* check the AKID from an RSA certificate */
  9420. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  9421. return -7304;
  9422. /* check the Key Usage from an RSA certificate */
  9423. if (!cert.extKeyUsageSet)
  9424. return -7305;
  9425. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  9426. return -7306;
  9427. /* check the CA Basic Constraints from an RSA certificate */
  9428. if (cert.isCA)
  9429. return -7307;
  9430. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  9431. /* check the Certificate Policies Id */
  9432. if (cert.extCertPoliciesNb != 1)
  9433. return -7308;
  9434. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  9435. return -7309;
  9436. #endif
  9437. FreeDecodedCert(&cert);
  9438. #ifdef HAVE_ECC
  9439. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  9440. file = XFOPEN(certEccDerFile, "rb");
  9441. if (!file) {
  9442. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  9443. return -7310;
  9444. }
  9445. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  9446. XFCLOSE(file);
  9447. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  9448. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  9449. if (ret != 0)
  9450. return -7311;
  9451. /* check the SKID from a ECC certificate - generated dynamically */
  9452. /* check the AKID from an ECC certificate */
  9453. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  9454. return -7312;
  9455. /* check the Key Usage from an ECC certificate */
  9456. if (!cert.extKeyUsageSet)
  9457. return -7313;
  9458. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  9459. return -7314;
  9460. /* check the CA Basic Constraints from an ECC certificate */
  9461. if (cert.isCA)
  9462. return -7315;
  9463. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  9464. /* check the Certificate Policies Id */
  9465. if (cert.extCertPoliciesNb != 2)
  9466. return -7316;
  9467. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  9468. return -7317;
  9469. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  9470. return -7318;
  9471. #endif
  9472. FreeDecodedCert(&cert);
  9473. #endif /* HAVE_ECC */
  9474. /* load cert.der (self signed certificate) */
  9475. file = XFOPEN(certDerFile, "rb");
  9476. if (!file) {
  9477. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  9478. return -7319;
  9479. }
  9480. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  9481. XFCLOSE(file);
  9482. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  9483. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  9484. if (ret != 0)
  9485. return -7320;
  9486. /* check the SKID from a CA certificate */
  9487. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  9488. return -7321;
  9489. /* check the AKID from an CA certificate */
  9490. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  9491. return -7322;
  9492. /* check the Key Usage from CA certificate */
  9493. if (!cert.extKeyUsageSet)
  9494. return -7323;
  9495. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  9496. return -7324;
  9497. /* check the CA Basic Constraints CA certificate */
  9498. if (!cert.isCA)
  9499. return -7325;
  9500. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  9501. /* check the Certificate Policies Id */
  9502. if (cert.extCertPoliciesNb != 2)
  9503. return -7326;
  9504. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  9505. return -7327;
  9506. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  9507. return -7328;
  9508. #endif
  9509. FreeDecodedCert(&cert);
  9510. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  9511. return 0;
  9512. }
  9513. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT */
  9514. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  9515. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  9516. int decodedCertCache_test(void)
  9517. {
  9518. int ret = 0;
  9519. Cert cert;
  9520. FILE* file;
  9521. byte* der;
  9522. word32 derSz;
  9523. derSz = FOURK_BUF;
  9524. der = XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9525. if (der == NULL)
  9526. ret = -7400;
  9527. if (ret == 0) {
  9528. /* load cert.der */
  9529. file = XFOPEN(certDerFile, "rb");
  9530. if (file != NULL) {
  9531. derSz = XFREAD(der, 1, FOURK_BUF, file);
  9532. XFCLOSE(file);
  9533. }
  9534. else
  9535. ret = -7401;
  9536. }
  9537. if (ret == 0) {
  9538. if (wc_InitCert(&cert)) {
  9539. ret = -7402;
  9540. }
  9541. }
  9542. if (ret == 0) {
  9543. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  9544. }
  9545. if (ret == 0) {
  9546. if(wc_SetSubjectBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  9547. ret = -7403;
  9548. }
  9549. if (ret == 0) {
  9550. if (wc_SetSubjectRaw(&cert, der, derSz) != 0)
  9551. ret = -7404;
  9552. }
  9553. if (ret == 0) {
  9554. if(wc_SetSubjectRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  9555. ret = -7405;
  9556. }
  9557. if (ret == 0) {
  9558. if(wc_SetIssuerBuffer(&cert, der, derSz) != 0)
  9559. ret = -7406;
  9560. }
  9561. if (ret == 0) {
  9562. if(wc_SetIssuerBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  9563. ret = -7407;
  9564. }
  9565. if (ret == 0) {
  9566. if(wc_SetIssuerRaw(&cert, der, derSz) != 0)
  9567. ret = -7408;
  9568. }
  9569. if (ret == 0) {
  9570. if(wc_SetIssuerRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  9571. ret = -7409;
  9572. }
  9573. #ifdef WOLFSSL_ALT_NAMES
  9574. if (ret == 0) {
  9575. if(wc_SetAltNamesBuffer(&cert, der, derSz) != 0)
  9576. ret = -7410;
  9577. }
  9578. if (ret == 0) {
  9579. if(wc_SetAltNamesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  9580. ret = -7411;
  9581. }
  9582. if (ret == 0) {
  9583. if(wc_SetDatesBuffer(&cert, der, derSz) != 0)
  9584. ret = -7412;
  9585. }
  9586. if (ret == 0) {
  9587. if(wc_SetDatesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  9588. ret = -7413;
  9589. }
  9590. #endif
  9591. if (ret == 0) {
  9592. if(wc_SetAuthKeyIdFromCert(&cert, der, derSz) != 0)
  9593. ret = -7414;
  9594. }
  9595. if (ret == 0) {
  9596. if(wc_SetAuthKeyIdFromCert(NULL, der, derSz) != BAD_FUNC_ARG)
  9597. ret = -7415;
  9598. }
  9599. wc_SetCert_Free(&cert);
  9600. if (ret == 0) {
  9601. if(cert.decodedCert != NULL)
  9602. ret = -7416;
  9603. }
  9604. XFREE(der, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  9605. return ret;
  9606. }
  9607. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  9608. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  9609. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  9610. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  9611. static int rsa_flatten_test(RsaKey* key)
  9612. {
  9613. int ret;
  9614. byte e[RSA_TEST_BYTES];
  9615. byte n[RSA_TEST_BYTES];
  9616. word32 eSz = sizeof(e);
  9617. word32 nSz = sizeof(n);
  9618. /* Parameter Validation testing. */
  9619. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  9620. #ifdef HAVE_USER_RSA
  9621. /* Implementation using IPP Libraries returns:
  9622. * -101 = USER_CRYPTO_ERROR
  9623. */
  9624. if (ret == 0)
  9625. #else
  9626. if (ret != BAD_FUNC_ARG)
  9627. #endif
  9628. return -7417;
  9629. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  9630. #ifdef HAVE_USER_RSA
  9631. /* Implementation using IPP Libraries returns:
  9632. * -101 = USER_CRYPTO_ERROR
  9633. */
  9634. if (ret == 0)
  9635. #else
  9636. if (ret != BAD_FUNC_ARG)
  9637. #endif
  9638. return -7418;
  9639. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  9640. #ifdef HAVE_USER_RSA
  9641. /* Implementation using IPP Libraries returns:
  9642. * -101 = USER_CRYPTO_ERROR
  9643. */
  9644. if (ret == 0)
  9645. #else
  9646. if (ret != BAD_FUNC_ARG)
  9647. #endif
  9648. return -7419;
  9649. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  9650. #ifdef HAVE_USER_RSA
  9651. /* Implementation using IPP Libraries returns:
  9652. * -101 = USER_CRYPTO_ERROR
  9653. */
  9654. if (ret == 0)
  9655. #else
  9656. if (ret != BAD_FUNC_ARG)
  9657. #endif
  9658. return -7420;
  9659. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  9660. #ifdef HAVE_USER_RSA
  9661. /* Implementation using IPP Libraries returns:
  9662. * -101 = USER_CRYPTO_ERROR
  9663. */
  9664. if (ret == 0)
  9665. #else
  9666. if (ret != BAD_FUNC_ARG)
  9667. #endif
  9668. return -7421;
  9669. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  9670. if (ret != 0)
  9671. return -7422;
  9672. eSz = 0;
  9673. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  9674. #ifdef HAVE_USER_RSA
  9675. /* Implementation using IPP Libraries returns:
  9676. * -101 = USER_CRYPTO_ERROR
  9677. */
  9678. if (ret == 0)
  9679. #elif defined(HAVE_FIPS) && \
  9680. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  9681. if (ret != 0)
  9682. #else
  9683. if (ret != RSA_BUFFER_E)
  9684. #endif
  9685. return -7423;
  9686. eSz = sizeof(e);
  9687. nSz = 0;
  9688. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  9689. #ifdef HAVE_USER_RSA
  9690. /* Implementation using IPP Libraries returns:
  9691. * -101 = USER_CRYPTO_ERROR
  9692. */
  9693. if (ret == 0)
  9694. #else
  9695. if (ret != RSA_BUFFER_E)
  9696. #endif
  9697. return -7424;
  9698. return 0;
  9699. }
  9700. #endif /* NO_ASN */
  9701. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  9702. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  9703. static int rsa_export_key_test(RsaKey* key)
  9704. {
  9705. int ret;
  9706. byte e[3];
  9707. word32 eSz = sizeof(e);
  9708. byte n[RSA_TEST_BYTES];
  9709. word32 nSz = sizeof(n);
  9710. byte d[RSA_TEST_BYTES];
  9711. word32 dSz = sizeof(d);
  9712. byte p[RSA_TEST_BYTES/2];
  9713. word32 pSz = sizeof(p);
  9714. byte q[RSA_TEST_BYTES/2];
  9715. word32 qSz = sizeof(q);
  9716. word32 zero = 0;
  9717. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  9718. if (ret != BAD_FUNC_ARG)
  9719. return -7425;
  9720. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  9721. if (ret != BAD_FUNC_ARG)
  9722. return -7426;
  9723. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  9724. if (ret != BAD_FUNC_ARG)
  9725. return -7427;
  9726. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  9727. if (ret != BAD_FUNC_ARG)
  9728. return -7428;
  9729. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  9730. if (ret != BAD_FUNC_ARG)
  9731. return -7429;
  9732. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  9733. if (ret != BAD_FUNC_ARG)
  9734. return -7430;
  9735. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  9736. if (ret != BAD_FUNC_ARG)
  9737. return -7431;
  9738. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  9739. if (ret != BAD_FUNC_ARG)
  9740. return -7432;
  9741. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  9742. if (ret != BAD_FUNC_ARG)
  9743. return -7433;
  9744. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  9745. if (ret != BAD_FUNC_ARG)
  9746. return -7434;
  9747. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  9748. if (ret != BAD_FUNC_ARG)
  9749. return -7435;
  9750. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  9751. if (ret != RSA_BUFFER_E)
  9752. return -7436;
  9753. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  9754. if (ret != RSA_BUFFER_E)
  9755. return -7437;
  9756. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  9757. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  9758. if (ret != RSA_BUFFER_E)
  9759. return -7438;
  9760. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  9761. if (ret != RSA_BUFFER_E)
  9762. return -7439;
  9763. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  9764. if (ret != RSA_BUFFER_E)
  9765. return -7440;
  9766. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  9767. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  9768. if (ret != 0)
  9769. return -7441;
  9770. return 0;
  9771. }
  9772. #endif /* !HAVE_FIPS && !USER_RSA && !NO_ASN */
  9773. #ifndef NO_SIG_WRAPPER
  9774. static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  9775. {
  9776. int ret;
  9777. word32 sigSz;
  9778. const byte in[] = "Everyone gets Friday off.";
  9779. const byte hash[] = {
  9780. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  9781. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  9782. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  9783. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  9784. };
  9785. const byte hashEnc[] = {
  9786. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  9787. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  9788. 0x00, 0x04, 0x20,
  9789. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  9790. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  9791. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  9792. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  9793. };
  9794. word32 inLen = (word32)XSTRLEN((char*)in);
  9795. byte out[RSA_TEST_BYTES];
  9796. /* Parameter Validation testing. */
  9797. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  9798. if (ret != BAD_FUNC_ARG)
  9799. return -7442;
  9800. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  9801. if (ret != BAD_FUNC_ARG)
  9802. return -7443;
  9803. sigSz = (word32)modLen;
  9804. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  9805. inLen, out, &sigSz, key, keyLen, rng);
  9806. if (ret != BAD_FUNC_ARG)
  9807. return -7444;
  9808. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  9809. 0, out, &sigSz, key, keyLen, rng);
  9810. if (ret != BAD_FUNC_ARG)
  9811. return -7445;
  9812. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  9813. inLen, NULL, &sigSz, key, keyLen, rng);
  9814. if (ret != BAD_FUNC_ARG)
  9815. return -7446;
  9816. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  9817. inLen, out, NULL, key, keyLen, rng);
  9818. if (ret != BAD_FUNC_ARG)
  9819. return -7447;
  9820. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  9821. inLen, out, &sigSz, NULL, keyLen, rng);
  9822. if (ret != BAD_FUNC_ARG)
  9823. return -7448;
  9824. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  9825. inLen, out, &sigSz, key, 0, rng);
  9826. if (ret != BAD_FUNC_ARG)
  9827. return -7449;
  9828. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  9829. inLen, out, &sigSz, key, keyLen, NULL);
  9830. #ifdef HAVE_USER_RSA
  9831. /* Implementation using IPP Libraries returns:
  9832. * -101 = USER_CRYPTO_ERROR
  9833. */
  9834. if (ret == 0)
  9835. #elif defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  9836. /* blinding / rng handled with hardware acceleration */
  9837. if (ret != 0)
  9838. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  9839. /* async may not require RNG */
  9840. if (ret != 0 && ret != MISSING_RNG_E)
  9841. #elif defined(HAVE_FIPS) || defined(WOLFSSL_ASYNC_CRYPT) || \
  9842. !defined(WC_RSA_BLINDING)
  9843. /* FIPS140 implementation does not do blinding */
  9844. if (ret != 0)
  9845. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY)
  9846. if (ret != SIG_TYPE_E)
  9847. #elif defined(WOLFSSL_CRYPTOCELL)
  9848. /* RNG is handled with the cryptocell */
  9849. if (ret != 0)
  9850. #else
  9851. if (ret != MISSING_RNG_E)
  9852. #endif
  9853. return -7450;
  9854. sigSz = 0;
  9855. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  9856. inLen, out, &sigSz, key, keyLen, rng);
  9857. if (ret != BAD_FUNC_ARG)
  9858. return -7451;
  9859. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  9860. inLen, out, (word32)modLen, key, keyLen);
  9861. if (ret != BAD_FUNC_ARG)
  9862. return -7452;
  9863. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  9864. 0, out, (word32)modLen, key, keyLen);
  9865. if (ret != BAD_FUNC_ARG)
  9866. return -7453;
  9867. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  9868. inLen, NULL, (word32)modLen, key, keyLen);
  9869. if (ret != BAD_FUNC_ARG)
  9870. return -7454;
  9871. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  9872. inLen, out, 0, key, keyLen);
  9873. if (ret != BAD_FUNC_ARG)
  9874. return -7455;
  9875. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  9876. inLen, out, (word32)modLen, NULL, keyLen);
  9877. if (ret != BAD_FUNC_ARG)
  9878. return -7456;
  9879. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  9880. inLen, out, (word32)modLen, key, 0);
  9881. if (ret != BAD_FUNC_ARG)
  9882. return -7457;
  9883. #ifndef HAVE_ECC
  9884. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  9885. if (ret != SIG_TYPE_E)
  9886. return -7458;
  9887. #endif
  9888. /* Use APIs. */
  9889. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  9890. if (ret != modLen)
  9891. return -7459;
  9892. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  9893. if (ret != modLen)
  9894. return -7460;
  9895. sigSz = (word32)ret;
  9896. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  9897. XMEMSET(out, 0, sizeof(out));
  9898. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  9899. inLen, out, &sigSz, key, keyLen, rng);
  9900. if (ret != 0)
  9901. return -7461;
  9902. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  9903. inLen, out, (word32)modLen, key, keyLen);
  9904. if (ret != 0)
  9905. return -7462;
  9906. sigSz = (word32)sizeof(out);
  9907. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  9908. in, inLen, out, &sigSz, key, keyLen, rng);
  9909. if (ret != 0)
  9910. return -7463;
  9911. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  9912. in, inLen, out, (word32)modLen, key, keyLen);
  9913. if (ret != 0)
  9914. return -7464;
  9915. /* Wrong signature type. */
  9916. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  9917. inLen, out, (word32)modLen, key, keyLen);
  9918. if (ret == 0)
  9919. return -7465;
  9920. /* check hash functions */
  9921. sigSz = (word32)sizeof(out);
  9922. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  9923. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  9924. if (ret != 0)
  9925. return -7466;
  9926. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  9927. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  9928. if (ret != 0)
  9929. return -7467;
  9930. sigSz = (word32)sizeof(out);
  9931. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  9932. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  9933. if (ret != 0)
  9934. return -7468;
  9935. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  9936. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  9937. if (ret != 0)
  9938. return -7469;
  9939. #else
  9940. (void)hash;
  9941. (void)hashEnc;
  9942. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  9943. return 0;
  9944. }
  9945. #endif /* !NO_SIG_WRAPPER */
  9946. #ifdef WC_RSA_NONBLOCK
  9947. static int rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  9948. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  9949. {
  9950. int ret = 0, count;
  9951. int signSz = 0;
  9952. RsaNb nb;
  9953. byte* inlinePlain = NULL;
  9954. /* Enable non-blocking RSA mode - provide context */
  9955. ret = wc_RsaSetNonBlock(key, &nb);
  9956. if (ret != 0)
  9957. return ret;
  9958. #ifdef WC_RSA_NONBLOCK_TIME
  9959. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  9960. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  9961. if (ret != 0)
  9962. return ret;
  9963. #endif
  9964. count = 0;
  9965. do {
  9966. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  9967. count++; /* track number of would blocks */
  9968. if (ret == FP_WOULDBLOCK) {
  9969. /* do "other" work here */
  9970. }
  9971. } while (ret == FP_WOULDBLOCK);
  9972. if (ret < 0) {
  9973. return ret;
  9974. }
  9975. #ifdef DEBUG_WOLFSSL
  9976. printf("RSA non-block sign: %d times\n", count);
  9977. #endif
  9978. signSz = ret;
  9979. /* Test non-blocking verify */
  9980. XMEMSET(plain, 0, plainSz);
  9981. count = 0;
  9982. do {
  9983. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  9984. count++; /* track number of would blocks */
  9985. if (ret == FP_WOULDBLOCK) {
  9986. /* do "other" work here */
  9987. }
  9988. } while (ret == FP_WOULDBLOCK);
  9989. if (ret < 0) {
  9990. return ret;
  9991. }
  9992. #ifdef DEBUG_WOLFSSL
  9993. printf("RSA non-block verify: %d times\n", count);
  9994. #endif
  9995. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  9996. return SIG_VERIFY_E;
  9997. }
  9998. /* Test inline non-blocking verify */
  9999. count = 0;
  10000. do {
  10001. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  10002. count++; /* track number of would blocks */
  10003. if (ret == FP_WOULDBLOCK) {
  10004. /* do "other" work here */
  10005. }
  10006. } while (ret == FP_WOULDBLOCK);
  10007. if (ret < 0) {
  10008. return ret;
  10009. }
  10010. #ifdef DEBUG_WOLFSSL
  10011. printf("RSA non-block inline verify: %d times\n", count);
  10012. #endif
  10013. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  10014. return SIG_VERIFY_E;
  10015. }
  10016. /* Disabling non-block RSA mode */
  10017. ret = wc_RsaSetNonBlock(key, NULL);
  10018. (void)count;
  10019. return 0;
  10020. }
  10021. #endif
  10022. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  10023. static int rsa_decode_test(RsaKey* keyPub)
  10024. {
  10025. int ret;
  10026. word32 inSz;
  10027. word32 inOutIdx;
  10028. static const byte n[2] = { 0x00, 0x23 };
  10029. static const byte e[2] = { 0x00, 0x03 };
  10030. static const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  10031. 0x03 };
  10032. static const byte goodAlgId[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00,
  10033. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  10034. static const byte goodAlgIdNull[] = { 0x30, 0x11, 0x30, 0x0f, 0x06, 0x00,
  10035. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  10036. 0x02, 0x1, 0x03 };
  10037. static const byte badAlgIdNull[] = { 0x30, 0x12, 0x30, 0x10, 0x06, 0x00,
  10038. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  10039. 0x02, 0x1, 0x03 };
  10040. static const byte badNotBitString[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00,
  10041. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  10042. static const byte badBitStringLen[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00,
  10043. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  10044. static const byte badNoSeq[] = { 0x30, 0x0d, 0x30, 0x0b, 0x06, 0x00, 0x03,
  10045. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  10046. static const byte badNoObj[] = {
  10047. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  10048. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  10049. static const byte badIntN[] = { 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1,
  10050. 0x03 };
  10051. static const byte badNotIntE[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1,
  10052. 0x03 };
  10053. static const byte badLength[] = { 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1,
  10054. 0x03 };
  10055. static const byte badBitStrNoZero[] = { 0x30, 0x0e, 0x30, 0x0c, 0x06, 0x00,
  10056. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  10057. ret = wc_InitRsaKey(keyPub, NULL);
  10058. if (ret != 0)
  10059. return -7470;
  10060. /* Parameter Validation testing. */
  10061. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  10062. if (ret != BAD_FUNC_ARG) {
  10063. ret = -7471;
  10064. goto done;
  10065. }
  10066. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  10067. if (ret != BAD_FUNC_ARG) {
  10068. ret = -7472;
  10069. goto done;
  10070. }
  10071. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  10072. if (ret != BAD_FUNC_ARG) {
  10073. ret = -7473;
  10074. goto done;
  10075. }
  10076. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  10077. #ifndef WOLFSSL_SP_MATH
  10078. if (ret != 0) {
  10079. #else
  10080. if (ret != ASN_GETINT_E) {
  10081. #endif
  10082. ret = -7474;
  10083. goto done;
  10084. }
  10085. wc_FreeRsaKey(keyPub);
  10086. ret = wc_InitRsaKey(keyPub, NULL);
  10087. if (ret != 0)
  10088. return -7475;
  10089. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  10090. #ifndef WOLFSSL_SP_MATH
  10091. if (ret != 0) {
  10092. #else
  10093. if (ret != ASN_GETINT_E) {
  10094. #endif
  10095. ret = -7476;
  10096. goto done;
  10097. }
  10098. wc_FreeRsaKey(keyPub);
  10099. ret = wc_InitRsaKey(keyPub, NULL);
  10100. if (ret != 0)
  10101. return -7477;
  10102. /* Use API. */
  10103. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  10104. if (ret != 0) {
  10105. ret = -7478;
  10106. goto done;
  10107. }
  10108. wc_FreeRsaKey(keyPub);
  10109. ret = wc_InitRsaKey(keyPub, NULL);
  10110. if (ret != 0)
  10111. return -7479;
  10112. /* Parameter Validation testing. */
  10113. inSz = sizeof(good);
  10114. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  10115. if (ret != BAD_FUNC_ARG) {
  10116. ret = -7480;
  10117. goto done;
  10118. }
  10119. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  10120. if (ret != BAD_FUNC_ARG) {
  10121. ret = -7481;
  10122. goto done;
  10123. }
  10124. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  10125. if (ret != BAD_FUNC_ARG) {
  10126. ret = -7482;
  10127. goto done;
  10128. }
  10129. /* Use good data and offset to bad data. */
  10130. inOutIdx = 2;
  10131. inSz = sizeof(good) - inOutIdx;
  10132. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  10133. if (ret != ASN_PARSE_E) {
  10134. ret = -7483;
  10135. goto done;
  10136. }
  10137. inOutIdx = 2;
  10138. inSz = sizeof(goodAlgId) - inOutIdx;
  10139. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  10140. if (ret != ASN_PARSE_E) {
  10141. ret = -7484;
  10142. goto done;
  10143. }
  10144. inOutIdx = 2;
  10145. inSz = sizeof(goodAlgId);
  10146. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  10147. #ifndef WOLFSSL_NO_DECODE_EXTRA
  10148. if (ret != ASN_PARSE_E)
  10149. #else
  10150. if (ret != ASN_RSA_KEY_E)
  10151. #endif
  10152. {
  10153. ret = -7485;
  10154. goto done;
  10155. }
  10156. /* Try different bad data. */
  10157. inSz = sizeof(badAlgIdNull);
  10158. inOutIdx = 0;
  10159. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  10160. if (ret != ASN_EXPECT_0_E) {
  10161. ret = -7486;
  10162. goto done;
  10163. }
  10164. inSz = sizeof(badNotBitString);
  10165. inOutIdx = 0;
  10166. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  10167. if (ret != ASN_BITSTR_E) {
  10168. ret = -7487;
  10169. goto done;
  10170. }
  10171. inSz = sizeof(badBitStringLen);
  10172. inOutIdx = 0;
  10173. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  10174. if (ret != ASN_PARSE_E) {
  10175. ret = -7488;
  10176. goto done;
  10177. }
  10178. inSz = sizeof(badNoSeq);
  10179. inOutIdx = 0;
  10180. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  10181. if (ret != ASN_PARSE_E) {
  10182. ret = -7489;
  10183. goto done;
  10184. }
  10185. inSz = sizeof(badNoObj);
  10186. inOutIdx = 0;
  10187. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  10188. if (ret != ASN_PARSE_E) {
  10189. ret = -7490;
  10190. goto done;
  10191. }
  10192. inSz = sizeof(badIntN);
  10193. inOutIdx = 0;
  10194. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  10195. if (ret != ASN_RSA_KEY_E) {
  10196. ret = -7491;
  10197. goto done;
  10198. }
  10199. inSz = sizeof(badNotIntE);
  10200. inOutIdx = 0;
  10201. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  10202. if (ret != ASN_RSA_KEY_E) {
  10203. ret = -7492;
  10204. goto done;
  10205. }
  10206. /* TODO: Shouldn't pass as the sequence length is too small. */
  10207. inSz = sizeof(badLength);
  10208. inOutIdx = 0;
  10209. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  10210. if (ret != 0) {
  10211. ret = -7493;
  10212. goto done;
  10213. }
  10214. /* TODO: Shouldn't ignore object id's data. */
  10215. wc_FreeRsaKey(keyPub);
  10216. ret = wc_InitRsaKey(keyPub, NULL);
  10217. if (ret != 0)
  10218. return -7494;
  10219. inSz = sizeof(badBitStrNoZero);
  10220. inOutIdx = 0;
  10221. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  10222. if (ret != ASN_EXPECT_0_E) {
  10223. ret = -7495;
  10224. goto done;
  10225. }
  10226. wc_FreeRsaKey(keyPub);
  10227. ret = wc_InitRsaKey(keyPub, NULL);
  10228. if (ret != 0)
  10229. return -7496;
  10230. /* Valid data cases. */
  10231. inSz = sizeof(good);
  10232. inOutIdx = 0;
  10233. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  10234. if (ret != 0) {
  10235. ret = -7497;
  10236. goto done;
  10237. }
  10238. if (inOutIdx != inSz) {
  10239. ret = -7498;
  10240. goto done;
  10241. }
  10242. wc_FreeRsaKey(keyPub);
  10243. ret = wc_InitRsaKey(keyPub, NULL);
  10244. if (ret != 0)
  10245. return -7499;
  10246. inSz = sizeof(goodAlgId);
  10247. inOutIdx = 0;
  10248. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  10249. if (ret != 0) {
  10250. ret = -7500;
  10251. goto done;
  10252. }
  10253. if (inOutIdx != inSz) {
  10254. ret = -7501;
  10255. goto done;
  10256. }
  10257. wc_FreeRsaKey(keyPub);
  10258. ret = wc_InitRsaKey(keyPub, NULL);
  10259. if (ret != 0)
  10260. return -7502;
  10261. inSz = sizeof(goodAlgIdNull);
  10262. inOutIdx = 0;
  10263. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  10264. if (ret != 0) {
  10265. ret = -7503;
  10266. goto done;
  10267. }
  10268. if (inOutIdx != inSz) {
  10269. ret = -7504;
  10270. goto done;
  10271. }
  10272. done:
  10273. wc_FreeRsaKey(keyPub);
  10274. return ret;
  10275. }
  10276. #endif
  10277. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  10278. /* Need to create known good signatures to test with this. */
  10279. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  10280. static int rsa_pss_test(WC_RNG* rng, RsaKey* key)
  10281. {
  10282. byte digest[WC_MAX_DIGEST_SIZE];
  10283. int ret = 0;
  10284. const char* inStr = "Everyone gets Friday off.";
  10285. word32 inLen = (word32)XSTRLEN((char*)inStr);
  10286. word32 outSz;
  10287. word32 plainSz;
  10288. word32 digestSz;
  10289. int i, j;
  10290. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  10291. int k, l;
  10292. #endif
  10293. int len;
  10294. byte* plain;
  10295. int mgf[] = {
  10296. #ifndef NO_SHA
  10297. WC_MGF1SHA1,
  10298. #endif
  10299. #ifdef WOLFSSL_SHA224
  10300. WC_MGF1SHA224,
  10301. #endif
  10302. WC_MGF1SHA256,
  10303. #ifdef WOLFSSL_SHA384
  10304. WC_MGF1SHA384,
  10305. #endif
  10306. #ifdef WOLFSSL_SHA512
  10307. WC_MGF1SHA512
  10308. #endif
  10309. };
  10310. enum wc_HashType hash[] = {
  10311. #ifndef NO_SHA
  10312. WC_HASH_TYPE_SHA,
  10313. #endif
  10314. #ifdef WOLFSSL_SHA224
  10315. WC_HASH_TYPE_SHA224,
  10316. #endif
  10317. WC_HASH_TYPE_SHA256,
  10318. #ifdef WOLFSSL_SHA384
  10319. WC_HASH_TYPE_SHA384,
  10320. #endif
  10321. #ifdef WOLFSSL_SHA512
  10322. WC_HASH_TYPE_SHA512,
  10323. #endif
  10324. };
  10325. DECLARE_VAR_INIT(in, byte, inLen, inStr, HEAP_HINT);
  10326. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  10327. DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  10328. /* Test all combinations of hash and MGF. */
  10329. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  10330. /* Calculate hash of message. */
  10331. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  10332. if (ret != 0)
  10333. ERROR_OUT(-7505, exit_rsa_pss);
  10334. digestSz = wc_HashGetDigestSize(hash[j]);
  10335. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  10336. outSz = RSA_TEST_BYTES;
  10337. do {
  10338. #if defined(WOLFSSL_ASYNC_CRYPT)
  10339. ret = wc_AsyncWait(ret, &key->asyncDev,
  10340. WC_ASYNC_FLAG_CALL_AGAIN);
  10341. #endif
  10342. if (ret >= 0) {
  10343. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  10344. hash[j], mgf[i], -1, key, rng);
  10345. }
  10346. } while (ret == WC_PENDING_E);
  10347. if (ret <= 0)
  10348. ERROR_OUT(-7506, exit_rsa_pss);
  10349. outSz = ret;
  10350. XMEMCPY(sig, out, outSz);
  10351. plain = NULL;
  10352. TEST_SLEEP();
  10353. do {
  10354. #if defined(WOLFSSL_ASYNC_CRYPT)
  10355. ret = wc_AsyncWait(ret, &key->asyncDev,
  10356. WC_ASYNC_FLAG_CALL_AGAIN);
  10357. #endif
  10358. if (ret >= 0) {
  10359. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  10360. mgf[i], -1, key);
  10361. }
  10362. } while (ret == WC_PENDING_E);
  10363. if (ret <= 0)
  10364. ERROR_OUT(-7507, exit_rsa_pss);
  10365. plainSz = ret;
  10366. TEST_SLEEP();
  10367. #if defined(HAVE_SELFTEST) && \
  10368. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  10369. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  10370. hash[j], -1);
  10371. #else
  10372. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  10373. hash[j], -1, wc_RsaEncryptSize(key)*8);
  10374. #endif
  10375. if (ret != 0)
  10376. ERROR_OUT(-7508, exit_rsa_pss);
  10377. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  10378. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  10379. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  10380. if (i == k && j == l)
  10381. continue;
  10382. XMEMCPY(sig, out, outSz);
  10383. do {
  10384. #if defined(WOLFSSL_ASYNC_CRYPT)
  10385. ret = wc_AsyncWait(ret, &key->asyncDev,
  10386. WC_ASYNC_FLAG_CALL_AGAIN);
  10387. #endif
  10388. if (ret >= 0) {
  10389. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  10390. (byte**)&plain, hash[l], mgf[k], -1, key);
  10391. }
  10392. } while (ret == WC_PENDING_E);
  10393. if (ret >= 0)
  10394. ERROR_OUT(-7509, exit_rsa_pss);
  10395. }
  10396. }
  10397. #endif
  10398. }
  10399. }
  10400. /* Test that a salt length of zero works. */
  10401. digestSz = wc_HashGetDigestSize(hash[0]);
  10402. outSz = RSA_TEST_BYTES;
  10403. do {
  10404. #if defined(WOLFSSL_ASYNC_CRYPT)
  10405. ret = wc_AsyncWait(ret, &key->asyncDev,
  10406. WC_ASYNC_FLAG_CALL_AGAIN);
  10407. #endif
  10408. if (ret >= 0) {
  10409. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  10410. mgf[0], 0, key, rng);
  10411. }
  10412. } while (ret == WC_PENDING_E);
  10413. if (ret <= 0)
  10414. ERROR_OUT(-7510, exit_rsa_pss);
  10415. outSz = ret;
  10416. TEST_SLEEP();
  10417. do {
  10418. #if defined(WOLFSSL_ASYNC_CRYPT)
  10419. ret = wc_AsyncWait(ret, &key->asyncDev,
  10420. WC_ASYNC_FLAG_CALL_AGAIN);
  10421. #endif
  10422. if (ret >= 0) {
  10423. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  10424. 0, key);
  10425. }
  10426. } while (ret == WC_PENDING_E);
  10427. if (ret <= 0)
  10428. ERROR_OUT(-7511, exit_rsa_pss);
  10429. plainSz = ret;
  10430. TEST_SLEEP();
  10431. do {
  10432. #if defined(WOLFSSL_ASYNC_CRYPT)
  10433. ret = wc_AsyncWait(ret, &key->asyncDev,
  10434. WC_ASYNC_FLAG_CALL_AGAIN);
  10435. #endif
  10436. if (ret >= 0) {
  10437. #if defined(HAVE_SELFTEST) && \
  10438. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  10439. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  10440. hash[0], 0);
  10441. #else
  10442. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  10443. hash[0], 0, 0);
  10444. #endif
  10445. }
  10446. } while (ret == WC_PENDING_E);
  10447. if (ret != 0)
  10448. ERROR_OUT(-7512, exit_rsa_pss);
  10449. XMEMCPY(sig, out, outSz);
  10450. plain = NULL;
  10451. do {
  10452. #if defined(WOLFSSL_ASYNC_CRYPT)
  10453. ret = wc_AsyncWait(ret, &key->asyncDev,
  10454. WC_ASYNC_FLAG_CALL_AGAIN);
  10455. #endif
  10456. if (ret >= 0) {
  10457. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  10458. 0, key);
  10459. }
  10460. } while (ret == WC_PENDING_E);
  10461. if (ret <= 0)
  10462. ERROR_OUT(-7513, exit_rsa_pss);
  10463. plainSz = ret;
  10464. TEST_SLEEP();
  10465. #if defined(HAVE_SELFTEST) && \
  10466. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  10467. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  10468. 0);
  10469. #else
  10470. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  10471. 0, 0);
  10472. #endif
  10473. if (ret != 0)
  10474. ERROR_OUT(-7514, exit_rsa_pss);
  10475. /* Test bad salt lengths in various APIs. */
  10476. digestSz = wc_HashGetDigestSize(hash[0]);
  10477. outSz = RSA_TEST_BYTES;
  10478. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  10479. len = -2;
  10480. #else
  10481. len = -3;
  10482. #endif
  10483. do {
  10484. #if defined(WOLFSSL_ASYNC_CRYPT)
  10485. ret = wc_AsyncWait(ret, &key->asyncDev,
  10486. WC_ASYNC_FLAG_CALL_AGAIN);
  10487. #endif
  10488. if (ret >= 0) {
  10489. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  10490. mgf[0], len, key, rng);
  10491. }
  10492. } while (ret == WC_PENDING_E);
  10493. if (ret != PSS_SALTLEN_E)
  10494. ERROR_OUT(-7515, exit_rsa_pss);
  10495. do {
  10496. #if defined(WOLFSSL_ASYNC_CRYPT)
  10497. ret = wc_AsyncWait(ret, &key->asyncDev,
  10498. WC_ASYNC_FLAG_CALL_AGAIN);
  10499. #endif
  10500. if (ret >= 0) {
  10501. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  10502. mgf[0], digestSz + 1, key, rng);
  10503. }
  10504. } while (ret == WC_PENDING_E);
  10505. if (ret != PSS_SALTLEN_E)
  10506. ERROR_OUT(-7516, exit_rsa_pss);
  10507. TEST_SLEEP();
  10508. do {
  10509. #if defined(WOLFSSL_ASYNC_CRYPT)
  10510. ret = wc_AsyncWait(ret, &key->asyncDev,
  10511. WC_ASYNC_FLAG_CALL_AGAIN);
  10512. #endif
  10513. if (ret >= 0) {
  10514. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  10515. mgf[0], -2, key);
  10516. }
  10517. } while (ret == WC_PENDING_E);
  10518. if (ret != PSS_SALTLEN_E)
  10519. ERROR_OUT(-7517, exit_rsa_pss);
  10520. TEST_SLEEP();
  10521. do {
  10522. #if defined(WOLFSSL_ASYNC_CRYPT)
  10523. ret = wc_AsyncWait(ret, &key->asyncDev,
  10524. WC_ASYNC_FLAG_CALL_AGAIN);
  10525. #endif
  10526. if (ret >= 0) {
  10527. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  10528. digestSz + 1, key);
  10529. }
  10530. } while (ret == WC_PENDING_E);
  10531. if (ret != PSS_SALTLEN_E)
  10532. ERROR_OUT(-7518, exit_rsa_pss);
  10533. TEST_SLEEP();
  10534. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  10535. len = -2;
  10536. #else
  10537. len = -3;
  10538. #endif
  10539. #if defined(HAVE_SELFTEST) && \
  10540. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  10541. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  10542. len);
  10543. #else
  10544. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  10545. len, 0);
  10546. #endif
  10547. if (ret != PSS_SALTLEN_E)
  10548. ERROR_OUT(-7519, exit_rsa_pss);
  10549. #ifndef WOLFSSL_PSS_LONG_SALT
  10550. len = digestSz + 1;
  10551. #else
  10552. len = plainSz - digestSz - 1;
  10553. #endif
  10554. #if defined(HAVE_SELFTEST) && \
  10555. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  10556. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  10557. len);
  10558. #else
  10559. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  10560. len, 0);
  10561. #endif
  10562. if (ret != PSS_SALTLEN_E)
  10563. ERROR_OUT(-7520, exit_rsa_pss);
  10564. ret = 0;
  10565. exit_rsa_pss:
  10566. FREE_VAR(sig, HEAP_HINT);
  10567. FREE_VAR(in, HEAP_HINT);
  10568. FREE_VAR(out, HEAP_HINT);
  10569. return ret;
  10570. }
  10571. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  10572. #endif
  10573. #ifdef WC_RSA_NO_PADDING
  10574. int rsa_no_pad_test(void)
  10575. {
  10576. WC_RNG rng;
  10577. RsaKey key;
  10578. byte* tmp;
  10579. size_t bytes;
  10580. int ret;
  10581. word32 inLen = 0;
  10582. word32 idx = 0;
  10583. word32 outSz = RSA_TEST_BYTES;
  10584. word32 plainSz = RSA_TEST_BYTES;
  10585. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  10586. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  10587. !defined(NO_FILESYSTEM)
  10588. XFILE file;
  10589. #endif
  10590. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  10591. DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  10592. /* initialize stack structures */
  10593. XMEMSET(&rng, 0, sizeof(rng));
  10594. XMEMSET(&key, 0, sizeof(key));
  10595. #ifdef USE_CERT_BUFFERS_1024
  10596. bytes = (size_t)sizeof_client_key_der_1024;
  10597. if (bytes < (size_t)sizeof_client_cert_der_1024)
  10598. bytes = (size_t)sizeof_client_cert_der_1024;
  10599. #elif defined(USE_CERT_BUFFERS_2048)
  10600. bytes = (size_t)sizeof_client_key_der_2048;
  10601. if (bytes < (size_t)sizeof_client_cert_der_2048)
  10602. bytes = (size_t)sizeof_client_cert_der_2048;
  10603. #else
  10604. bytes = FOURK_BUF;
  10605. #endif
  10606. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10607. if (tmp == NULL
  10608. #ifdef WOLFSSL_ASYNC_CRYPT
  10609. || out == NULL || plain == NULL
  10610. #endif
  10611. ) {
  10612. ERROR_OUT(-7600, exit_rsa_nopadding);
  10613. }
  10614. #ifdef USE_CERT_BUFFERS_1024
  10615. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  10616. #elif defined(USE_CERT_BUFFERS_2048)
  10617. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  10618. #elif defined(USE_CERT_BUFFERS_3072)
  10619. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  10620. #elif defined(USE_CERT_BUFFERS_4096)
  10621. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  10622. #elif !defined(NO_FILESYSTEM)
  10623. file = XFOPEN(clientKey, "rb");
  10624. if (!file) {
  10625. err_sys("can't open ./certs/client-key.der, "
  10626. "Please run from wolfSSL home dir", -40);
  10627. ERROR_OUT(-7601, exit_rsa_nopadding);
  10628. }
  10629. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10630. XFCLOSE(file);
  10631. #else
  10632. /* No key to use. */
  10633. ERROR_OUT(-7602, exit_rsa_nopadding);
  10634. #endif /* USE_CERT_BUFFERS */
  10635. ret = wc_InitRsaKey_ex(&key, HEAP_HINT, devId);
  10636. if (ret != 0) {
  10637. ERROR_OUT(-7603, exit_rsa_nopadding);
  10638. }
  10639. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  10640. if (ret != 0) {
  10641. ERROR_OUT(-7604, exit_rsa_nopadding);
  10642. }
  10643. /* after loading in key use tmp as the test buffer */
  10644. #ifndef HAVE_FIPS
  10645. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  10646. #else
  10647. ret = wc_InitRng(&rng);
  10648. #endif
  10649. if (ret != 0) {
  10650. ERROR_OUT(-7605, exit_rsa_nopadding);
  10651. }
  10652. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  10653. inLen = wc_RsaEncryptSize(&key);
  10654. outSz = inLen;
  10655. plainSz = inLen;
  10656. XMEMSET(tmp, 7, inLen);
  10657. do {
  10658. #if defined(WOLFSSL_ASYNC_CRYPT)
  10659. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  10660. #endif
  10661. if (ret >= 0) {
  10662. ret = wc_RsaDirect(tmp, inLen, out, &outSz, &key,
  10663. RSA_PRIVATE_ENCRYPT, &rng);
  10664. }
  10665. } while (ret == WC_PENDING_E);
  10666. if (ret <= 0) {
  10667. ERROR_OUT(-7606, exit_rsa_nopadding);
  10668. }
  10669. /* encrypted result should not be the same as input */
  10670. if (XMEMCMP(out, tmp, inLen) == 0) {
  10671. ERROR_OUT(-7607, exit_rsa_nopadding);
  10672. }
  10673. TEST_SLEEP();
  10674. /* decrypt with public key and compare result */
  10675. do {
  10676. #if defined(WOLFSSL_ASYNC_CRYPT)
  10677. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  10678. #endif
  10679. if (ret >= 0) {
  10680. ret = wc_RsaDirect(out, outSz, plain, &plainSz, &key,
  10681. RSA_PUBLIC_DECRYPT, &rng);
  10682. }
  10683. } while (ret == WC_PENDING_E);
  10684. if (ret <= 0) {
  10685. ERROR_OUT(-7608, exit_rsa_nopadding);
  10686. }
  10687. if (XMEMCMP(plain, tmp, inLen) != 0) {
  10688. ERROR_OUT(-7609, exit_rsa_nopadding);
  10689. }
  10690. TEST_SLEEP();
  10691. #endif
  10692. #ifdef WC_RSA_BLINDING
  10693. ret = wc_RsaSetRNG(NULL, &rng);
  10694. if (ret != BAD_FUNC_ARG) {
  10695. ERROR_OUT(-7610, exit_rsa_nopadding);
  10696. }
  10697. ret = wc_RsaSetRNG(&key, &rng);
  10698. if (ret < 0) {
  10699. ERROR_OUT(-7611, exit_rsa_nopadding);
  10700. }
  10701. #endif
  10702. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  10703. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  10704. do {
  10705. #if defined(WOLFSSL_ASYNC_CRYPT)
  10706. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  10707. #endif
  10708. if (ret >= 0) {
  10709. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, (int)outSz, &key, &rng,
  10710. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  10711. }
  10712. } while (ret == WC_PENDING_E);
  10713. if (ret < 0) {
  10714. ERROR_OUT(-7612, exit_rsa_nopadding);
  10715. }
  10716. TEST_SLEEP();
  10717. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  10718. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  10719. do {
  10720. #if defined(WOLFSSL_ASYNC_CRYPT)
  10721. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  10722. #endif
  10723. if (ret >= 0) {
  10724. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, (int)plainSz, &key,
  10725. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  10726. }
  10727. } while (ret == WC_PENDING_E);
  10728. if (ret < 0) {
  10729. ERROR_OUT(-7613, exit_rsa_nopadding);
  10730. }
  10731. if (XMEMCMP(plain, tmp, inLen) != 0) {
  10732. ERROR_OUT(-7614, exit_rsa_nopadding);
  10733. }
  10734. TEST_SLEEP();
  10735. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  10736. /* test some bad arguments */
  10737. ret = wc_RsaDirect(out, outSz, plain, &plainSz, &key, -1,
  10738. &rng);
  10739. if (ret != BAD_FUNC_ARG) {
  10740. ERROR_OUT(-7615, exit_rsa_nopadding);
  10741. }
  10742. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  10743. &rng);
  10744. if (ret != BAD_FUNC_ARG) {
  10745. ERROR_OUT(-7616, exit_rsa_nopadding);
  10746. }
  10747. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, &key, RSA_PUBLIC_DECRYPT,
  10748. &rng);
  10749. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  10750. ERROR_OUT(-7617, exit_rsa_nopadding);
  10751. }
  10752. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, &key,
  10753. RSA_PUBLIC_DECRYPT, &rng);
  10754. if (ret != BAD_FUNC_ARG) {
  10755. ERROR_OUT(-7618, exit_rsa_nopadding);
  10756. }
  10757. /* if making it to this point of code without hitting an ERROR_OUT then
  10758. * all tests have passed */
  10759. ret = 0;
  10760. exit_rsa_nopadding:
  10761. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10762. FREE_VAR(out, HEAP_HINT);
  10763. FREE_VAR(plain, HEAP_HINT);
  10764. wc_FreeRsaKey(&key);
  10765. wc_FreeRng(&rng);
  10766. return ret;
  10767. }
  10768. #endif /* WC_RSA_NO_PADDING */
  10769. #ifdef WOLFSSL_CERT_GEN
  10770. static int rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  10771. {
  10772. RsaKey caKey;
  10773. byte* der;
  10774. int ret;
  10775. Cert* myCert = NULL;
  10776. int certSz;
  10777. size_t bytes3;
  10778. word32 idx3 = 0;
  10779. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  10780. XFILE file3;
  10781. #endif
  10782. #ifdef WOLFSSL_TEST_CERT
  10783. DecodedCert decode;
  10784. #endif
  10785. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  10786. struct tm beforeTime;
  10787. struct tm afterTime;
  10788. #endif
  10789. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  10790. (void)keypub;
  10791. XMEMSET(&caKey, 0, sizeof(caKey));
  10792. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10793. if (der == NULL) {
  10794. ERROR_OUT(-7619, exit_rsa);
  10795. }
  10796. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10797. if (myCert == NULL) {
  10798. ERROR_OUT(-7621, exit_rsa);
  10799. }
  10800. /* self signed */
  10801. if (wc_InitCert(myCert)) {
  10802. ERROR_OUT(-7622, exit_rsa);
  10803. }
  10804. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  10805. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  10806. myCert->serialSz = (int)sizeof(mySerial);
  10807. myCert->isCA = 1;
  10808. #ifndef NO_SHA256
  10809. myCert->sigType = CTC_SHA256wRSA;
  10810. #else
  10811. myCert->sigType = CTC_SHAwRSA;
  10812. #endif
  10813. #ifdef WOLFSSL_CERT_EXT
  10814. /* add Policies */
  10815. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  10816. CTC_MAX_CERTPOL_SZ);
  10817. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  10818. CTC_MAX_CERTPOL_SZ);
  10819. myCert->certPoliciesNb = 2;
  10820. /* add SKID from the Public Key */
  10821. if (wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  10822. ERROR_OUT(-7623, exit_rsa);
  10823. }
  10824. /* add AKID from the Public Key */
  10825. if (wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  10826. ERROR_OUT(-7624, exit_rsa);
  10827. }
  10828. /* add Key Usage */
  10829. if (wc_SetKeyUsage(myCert,"cRLSign,keyCertSign") != 0) {
  10830. ERROR_OUT(-7625, exit_rsa);
  10831. }
  10832. #ifdef WOLFSSL_EKU_OID
  10833. {
  10834. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  10835. if (wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  10836. HEAP_HINT) != 0) {
  10837. ERROR_OUT(-7626, exit_rsa);
  10838. }
  10839. }
  10840. #endif /* WOLFSSL_EKU_OID */
  10841. #endif /* WOLFSSL_CERT_EXT */
  10842. ret = 0;
  10843. do {
  10844. #if defined(WOLFSSL_ASYNC_CRYPT)
  10845. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  10846. #endif
  10847. if (ret >= 0) {
  10848. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  10849. }
  10850. } while (ret == WC_PENDING_E);
  10851. if (ret < 0) {
  10852. ERROR_OUT(-7627, exit_rsa);
  10853. }
  10854. certSz = ret;
  10855. #ifdef WOLFSSL_TEST_CERT
  10856. InitDecodedCert(&decode, der, certSz, HEAP_HINT);
  10857. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  10858. if (ret != 0) {
  10859. FreeDecodedCert(&decode);
  10860. ERROR_OUT(-7628, exit_rsa);
  10861. }
  10862. FreeDecodedCert(&decode);
  10863. #endif
  10864. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  10865. CERT_TYPE, -5578);
  10866. if (ret != 0) {
  10867. goto exit_rsa;
  10868. }
  10869. /* Setup Certificate */
  10870. if (wc_InitCert(myCert)) {
  10871. ERROR_OUT(-7629, exit_rsa);
  10872. }
  10873. #ifdef WOLFSSL_ALT_NAMES
  10874. /* Get CA Cert for testing */
  10875. #ifdef USE_CERT_BUFFERS_1024
  10876. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  10877. bytes3 = sizeof_ca_cert_der_1024;
  10878. #elif defined(USE_CERT_BUFFERS_2048)
  10879. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  10880. bytes3 = sizeof_ca_cert_der_2048;
  10881. #else
  10882. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  10883. if (!file3) {
  10884. ERROR_OUT(-7630, exit_rsa);
  10885. }
  10886. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  10887. XFCLOSE(file3);
  10888. #endif /* USE_CERT_BUFFERS */
  10889. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  10890. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  10891. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  10892. if (ret != 0) {
  10893. ERROR_OUT(-7631, exit_rsa);
  10894. }
  10895. #endif
  10896. /* get alt names from der */
  10897. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  10898. if (ret != 0) {
  10899. ERROR_OUT(-7632, exit_rsa);
  10900. }
  10901. /* get dates from der */
  10902. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  10903. if (ret != 0) {
  10904. ERROR_OUT(-7633, exit_rsa);
  10905. }
  10906. #ifndef NO_ASN_TIME
  10907. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  10908. if (ret < 0) {
  10909. ERROR_OUT(-7634, exit_rsa);
  10910. }
  10911. #endif
  10912. #endif /* WOLFSSL_ALT_NAMES */
  10913. /* Get CA Key */
  10914. #ifdef USE_CERT_BUFFERS_1024
  10915. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  10916. bytes3 = sizeof_ca_key_der_1024;
  10917. #elif defined(USE_CERT_BUFFERS_2048)
  10918. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  10919. bytes3 = sizeof_ca_key_der_2048;
  10920. #else
  10921. file3 = XFOPEN(rsaCaKeyFile, "rb");
  10922. if (!file3) {
  10923. ERROR_OUT(-7635, exit_rsa);
  10924. }
  10925. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  10926. XFCLOSE(file3);
  10927. #endif /* USE_CERT_BUFFERS */
  10928. ret = wc_InitRsaKey(&caKey, HEAP_HINT);
  10929. if (ret != 0) {
  10930. ERROR_OUT(-7636, exit_rsa);
  10931. }
  10932. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes3);
  10933. if (ret != 0) {
  10934. ERROR_OUT(-7637, exit_rsa);
  10935. }
  10936. #ifndef NO_SHA256
  10937. myCert->sigType = CTC_SHA256wRSA;
  10938. #else
  10939. myCert->sigType = CTC_SHAwRSA;
  10940. #endif
  10941. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  10942. #ifdef WOLFSSL_CERT_EXT
  10943. /* add Policies */
  10944. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  10945. CTC_MAX_CERTPOL_SZ);
  10946. myCert->certPoliciesNb =1;
  10947. /* add SKID from the Public Key */
  10948. if (wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL) != 0) {
  10949. ERROR_OUT(-7638, exit_rsa);
  10950. }
  10951. /* add AKID from the CA certificate */
  10952. #if defined(USE_CERT_BUFFERS_2048)
  10953. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  10954. sizeof_ca_cert_der_2048);
  10955. #elif defined(USE_CERT_BUFFERS_1024)
  10956. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  10957. sizeof_ca_cert_der_1024);
  10958. #else
  10959. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  10960. #endif
  10961. if (ret != 0) {
  10962. ERROR_OUT(-7639, exit_rsa);
  10963. }
  10964. /* add Key Usage */
  10965. if (wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement") != 0) {
  10966. ERROR_OUT(-7640, exit_rsa);
  10967. }
  10968. #endif /* WOLFSSL_CERT_EXT */
  10969. #if defined(USE_CERT_BUFFERS_2048)
  10970. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  10971. sizeof_ca_cert_der_2048);
  10972. #elif defined(USE_CERT_BUFFERS_1024)
  10973. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  10974. sizeof_ca_cert_der_1024);
  10975. #else
  10976. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  10977. #endif
  10978. if (ret < 0) {
  10979. ERROR_OUT(-7641, exit_rsa);
  10980. }
  10981. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  10982. if (certSz < 0) {
  10983. ERROR_OUT(-7642, exit_rsa);
  10984. }
  10985. ret = 0;
  10986. do {
  10987. #if defined(WOLFSSL_ASYNC_CRYPT)
  10988. ret = wc_AsyncWait(ret, &caKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  10989. #endif
  10990. if (ret >= 0) {
  10991. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  10992. &caKey, NULL, rng);
  10993. }
  10994. } while (ret == WC_PENDING_E);
  10995. if (ret < 0) {
  10996. ERROR_OUT(-7643, exit_rsa);
  10997. }
  10998. certSz = ret;
  10999. #ifdef WOLFSSL_TEST_CERT
  11000. InitDecodedCert(&decode, der, certSz, HEAP_HINT);
  11001. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  11002. if (ret != 0) {
  11003. FreeDecodedCert(&decode);
  11004. ERROR_OUT(-7644, exit_rsa);
  11005. }
  11006. FreeDecodedCert(&decode);
  11007. #endif
  11008. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  11009. CERT_TYPE, -5598);
  11010. if (ret != 0) {
  11011. goto exit_rsa;
  11012. }
  11013. exit_rsa:
  11014. wc_FreeRsaKey(&caKey);
  11015. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11016. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11017. return ret;
  11018. }
  11019. #endif
  11020. #if !defined(NO_RSA) && defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  11021. /* Make Cert / Sign example for ECC cert and RSA CA */
  11022. static int rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  11023. {
  11024. RsaKey caKey;
  11025. ecc_key caEccKey;
  11026. ecc_key caEccKeyPub;
  11027. byte* der;
  11028. Cert* myCert = NULL;
  11029. int certSz;
  11030. size_t bytes3;
  11031. word32 idx3 = 0;
  11032. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  11033. || !defined(USE_CERT_BUFFERS_256)
  11034. XFILE file3;
  11035. #endif
  11036. #ifdef WOLFSSL_TEST_CERT
  11037. DecodedCert decode;
  11038. #endif
  11039. int ret;
  11040. XMEMSET(&caKey, 0, sizeof(caKey));
  11041. XMEMSET(&caEccKey, 0, sizeof(caEccKey));
  11042. XMEMSET(&caEccKeyPub, 0, sizeof(caEccKeyPub));
  11043. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11044. if (der == NULL) {
  11045. ERROR_OUT(-7645, exit_rsa);
  11046. }
  11047. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11048. if (myCert == NULL) {
  11049. ERROR_OUT(-7647, exit_rsa);
  11050. }
  11051. /* Get CA Key */
  11052. #ifdef USE_CERT_BUFFERS_1024
  11053. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  11054. bytes3 = sizeof_ca_key_der_1024;
  11055. #elif defined(USE_CERT_BUFFERS_2048)
  11056. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  11057. bytes3 = sizeof_ca_key_der_2048;
  11058. #else
  11059. file3 = XFOPEN(rsaCaKeyFile, "rb");
  11060. if (!file3) {
  11061. ERROR_OUT(-7648, exit_rsa);
  11062. }
  11063. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  11064. XFCLOSE(file3);
  11065. #endif /* USE_CERT_BUFFERS */
  11066. ret = wc_InitRsaKey(&caKey, HEAP_HINT);
  11067. if (ret != 0) {
  11068. ERROR_OUT(-7649, exit_rsa);
  11069. }
  11070. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes3);
  11071. if (ret != 0) {
  11072. ERROR_OUT(-7650, exit_rsa);
  11073. }
  11074. /* Get Cert Key */
  11075. #ifdef USE_CERT_BUFFERS_256
  11076. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  11077. bytes3 = sizeof_ecc_key_pub_der_256;
  11078. #else
  11079. file3 = XFOPEN(eccKeyPubFile, "rb");
  11080. if (!file3) {
  11081. ERROR_OUT(-7651, exit_rsa);
  11082. }
  11083. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  11084. XFCLOSE(file3);
  11085. #endif
  11086. ret = wc_ecc_init_ex(&caEccKeyPub, HEAP_HINT, devId);
  11087. if (ret != 0) {
  11088. ERROR_OUT(-7652, exit_rsa);
  11089. }
  11090. idx3 = 0;
  11091. ret = wc_EccPublicKeyDecode(tmp, &idx3, &caEccKeyPub, (word32)bytes3);
  11092. if (ret != 0) {
  11093. ERROR_OUT(-7653, exit_rsa);
  11094. }
  11095. /* Setup Certificate */
  11096. if (wc_InitCert(myCert)) {
  11097. ERROR_OUT(-7654, exit_rsa);
  11098. }
  11099. #ifndef NO_SHA256
  11100. myCert->sigType = CTC_SHA256wRSA;
  11101. #else
  11102. myCert->sigType = CTC_SHAwRSA;
  11103. #endif
  11104. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  11105. #ifdef WOLFSSL_CERT_EXT
  11106. /* add Policies */
  11107. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  11108. CTC_MAX_CERTPOL_SZ);
  11109. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  11110. CTC_MAX_CERTPOL_SZ);
  11111. myCert->certPoliciesNb = 2;
  11112. /* add SKID from the Public Key */
  11113. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, &caEccKeyPub) != 0) {
  11114. ERROR_OUT(-7655, exit_rsa);
  11115. }
  11116. /* add AKID from the CA certificate */
  11117. #if defined(USE_CERT_BUFFERS_2048)
  11118. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  11119. sizeof_ca_cert_der_2048);
  11120. #elif defined(USE_CERT_BUFFERS_1024)
  11121. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  11122. sizeof_ca_cert_der_1024);
  11123. #else
  11124. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  11125. #endif
  11126. if (ret != 0) {
  11127. ERROR_OUT(-7656, exit_rsa);
  11128. }
  11129. /* add Key Usage */
  11130. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  11131. ERROR_OUT(-7657, exit_rsa);
  11132. }
  11133. #endif /* WOLFSSL_CERT_EXT */
  11134. #if defined(USE_CERT_BUFFERS_2048)
  11135. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  11136. sizeof_ca_cert_der_2048);
  11137. #elif defined(USE_CERT_BUFFERS_1024)
  11138. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  11139. sizeof_ca_cert_der_1024);
  11140. #else
  11141. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  11142. #endif
  11143. if (ret < 0) {
  11144. ERROR_OUT(-7658, exit_rsa);
  11145. }
  11146. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, &caEccKeyPub, rng);
  11147. if (certSz < 0) {
  11148. ERROR_OUT(-7659, exit_rsa);
  11149. }
  11150. ret = 0;
  11151. do {
  11152. #if defined(WOLFSSL_ASYNC_CRYPT)
  11153. ret = wc_AsyncWait(ret, &caEccKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11154. #endif
  11155. if (ret >= 0) {
  11156. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  11157. FOURK_BUF, &caKey, NULL, rng);
  11158. }
  11159. } while (ret == WC_PENDING_E);
  11160. if (ret < 0) {
  11161. ERROR_OUT(-7660, exit_rsa);
  11162. }
  11163. certSz = ret;
  11164. #ifdef WOLFSSL_TEST_CERT
  11165. InitDecodedCert(&decode, der, certSz, 0);
  11166. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  11167. if (ret != 0) {
  11168. FreeDecodedCert(&decode);
  11169. ERROR_OUT(-7661, exit_rsa);
  11170. }
  11171. FreeDecodedCert(&decode);
  11172. #endif
  11173. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  11174. CERT_TYPE, -5616);
  11175. if (ret != 0) {
  11176. goto exit_rsa;
  11177. }
  11178. exit_rsa:
  11179. wc_FreeRsaKey(&caKey);
  11180. wc_ecc_free(&caEccKey);
  11181. wc_ecc_free(&caEccKeyPub);
  11182. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11183. myCert = NULL;
  11184. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11185. der = NULL;
  11186. if (ret >= 0)
  11187. ret = 0;
  11188. return ret;
  11189. }
  11190. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  11191. #ifdef WOLFSSL_KEY_GEN
  11192. static int rsa_keygen_test(WC_RNG* rng)
  11193. {
  11194. RsaKey genKey;
  11195. int ret;
  11196. byte* der = NULL;
  11197. word32 idx = 0;
  11198. int derSz = 0;
  11199. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  11200. int keySz = 1024;
  11201. #else
  11202. int keySz = 2048;
  11203. #endif
  11204. XMEMSET(&genKey, 0, sizeof(genKey));
  11205. ret = wc_InitRsaKey_ex(&genKey, HEAP_HINT, devId);
  11206. if (ret != 0) {
  11207. ERROR_OUT(-7662, exit_rsa);
  11208. }
  11209. ret = wc_MakeRsaKey(&genKey, keySz, WC_RSA_EXPONENT, rng);
  11210. #if defined(WOLFSSL_ASYNC_CRYPT)
  11211. ret = wc_AsyncWait(ret, &genKey.asyncDev, WC_ASYNC_FLAG_NONE);
  11212. #endif
  11213. if (ret != 0) {
  11214. ERROR_OUT(-7663, exit_rsa);
  11215. }
  11216. TEST_SLEEP();
  11217. /* If not using old FIPS, or not using FAST or USER RSA... */
  11218. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  11219. (!defined(HAVE_FIPS) || \
  11220. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  11221. !defined(HAVE_SELFTEST) && !defined(HAVE_INTEL_QA)
  11222. ret = wc_CheckRsaKey(&genKey);
  11223. if (ret != 0) {
  11224. ERROR_OUT(-7664, exit_rsa);
  11225. }
  11226. #endif
  11227. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11228. if (der == NULL) {
  11229. ERROR_OUT(-7665, exit_rsa);
  11230. }
  11231. derSz = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  11232. if (derSz < 0) {
  11233. ERROR_OUT(-7667, exit_rsa);
  11234. }
  11235. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  11236. PRIVATEKEY_TYPE, -5555);
  11237. if (ret != 0) {
  11238. goto exit_rsa;
  11239. }
  11240. wc_FreeRsaKey(&genKey);
  11241. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  11242. if (ret != 0) {
  11243. ERROR_OUT(-7668, exit_rsa);
  11244. }
  11245. idx = 0;
  11246. #if !defined(WOLFSSL_CRYPTOCELL)
  11247. /* The private key part of the key gen pairs from cryptocell can't be exported */
  11248. ret = wc_RsaPrivateKeyDecode(der, &idx, &genKey, derSz);
  11249. if (ret != 0) {
  11250. ERROR_OUT(-7669, exit_rsa);
  11251. }
  11252. #endif /* WOLFSSL_CRYPTOCELL */
  11253. exit_rsa:
  11254. wc_FreeRsaKey(&genKey);
  11255. if (der != NULL) {
  11256. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11257. der = NULL;
  11258. }
  11259. return ret;
  11260. }
  11261. #endif
  11262. int rsa_test(void)
  11263. {
  11264. int ret;
  11265. byte* tmp;
  11266. byte* der = NULL;
  11267. size_t bytes;
  11268. WC_RNG rng;
  11269. RsaKey key;
  11270. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  11271. RsaKey keypub;
  11272. #endif
  11273. #if defined(HAVE_NTRU)
  11274. RsaKey caKey;
  11275. #endif
  11276. #if !defined(NO_ASN) || !defined(WOLFSSL_RSA_PUBLIC_ONLY) \
  11277. || defined(WOLFSSL_PUBLIC_MP)
  11278. word32 idx = 0;
  11279. #endif
  11280. #if (!defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_PUBLIC_MP)) && \
  11281. !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  11282. const char* inStr = "Everyone gets Friday off.";
  11283. word32 inLen = (word32)XSTRLEN((char*)inStr);
  11284. const word32 outSz = RSA_TEST_BYTES;
  11285. const word32 plainSz = RSA_TEST_BYTES;
  11286. #endif
  11287. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_PUBLIC_MP)
  11288. byte* res;
  11289. #endif
  11290. #ifndef NO_SIG_WRAPPER
  11291. int modLen;
  11292. #endif
  11293. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  11294. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  11295. !defined(NO_FILESYSTEM)
  11296. XFILE file;
  11297. XFILE file2;
  11298. #endif
  11299. #ifdef WOLFSSL_TEST_CERT
  11300. DecodedCert cert;
  11301. #endif
  11302. #if (!defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_PUBLIC_MP)) && \
  11303. !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  11304. DECLARE_VAR_INIT(in, byte, inLen, inStr, HEAP_HINT);
  11305. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  11306. DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  11307. #endif
  11308. #ifdef WOLFSSL_ASYNC_CRYPT
  11309. if (in == NULL)
  11310. return MEMORY_E;
  11311. #endif
  11312. /* initialize stack structures */
  11313. XMEMSET(&rng, 0, sizeof(rng));
  11314. XMEMSET(&key, 0, sizeof(key));
  11315. #ifdef WOLFSSL_CERT_EXT
  11316. XMEMSET(&keypub, 0, sizeof(keypub));
  11317. #endif
  11318. #if defined(HAVE_NTRU)
  11319. XMEMSET(&caKey, 0, sizeof(caKey));
  11320. #endif
  11321. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  11322. ret = rsa_decode_test(&key);
  11323. if (ret != 0)
  11324. return ret;
  11325. #endif
  11326. #ifdef USE_CERT_BUFFERS_1024
  11327. bytes = (size_t)sizeof_client_key_der_1024;
  11328. if (bytes < (size_t)sizeof_client_cert_der_1024)
  11329. bytes = (size_t)sizeof_client_cert_der_1024;
  11330. #elif defined(USE_CERT_BUFFERS_2048)
  11331. bytes = (size_t)sizeof_client_key_der_2048;
  11332. if (bytes < (size_t)sizeof_client_cert_der_2048)
  11333. bytes = (size_t)sizeof_client_cert_der_2048;
  11334. #elif defined(USE_CERT_BUFFERS_3072)
  11335. bytes = (size_t)sizeof_client_key_der_3072;
  11336. if (bytes < (size_t)sizeof_client_cert_der_3072)
  11337. bytes = (size_t)sizeof_client_cert_der_3072;
  11338. #elif defined(USE_CERT_BUFFERS_4096)
  11339. bytes = (size_t)sizeof_client_key_der_4096;
  11340. if (bytes < (size_t)sizeof_client_cert_der_4096)
  11341. bytes = (size_t)sizeof_client_cert_der_4096;
  11342. #else
  11343. bytes = FOURK_BUF;
  11344. #endif
  11345. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11346. if (tmp == NULL
  11347. #ifdef WOLFSSL_ASYNC_CRYPT
  11348. || out == NULL || plain == NULL
  11349. #endif
  11350. ) {
  11351. return -7700;
  11352. }
  11353. #ifdef USE_CERT_BUFFERS_1024
  11354. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  11355. #elif defined(USE_CERT_BUFFERS_2048)
  11356. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  11357. #elif defined(USE_CERT_BUFFERS_3072)
  11358. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  11359. #elif defined(USE_CERT_BUFFERS_4096)
  11360. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  11361. #elif !defined(NO_FILESYSTEM)
  11362. file = XFOPEN(clientKey, "rb");
  11363. if (!file) {
  11364. err_sys("can't open ./certs/client-key.der, "
  11365. "Please run from wolfSSL home dir", -40);
  11366. ERROR_OUT(-7701, exit_rsa);
  11367. }
  11368. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11369. XFCLOSE(file);
  11370. #else
  11371. /* No key to use. */
  11372. ERROR_OUT(-7702, exit_rsa);
  11373. #endif /* USE_CERT_BUFFERS */
  11374. ret = wc_InitRsaKey_ex(&key, HEAP_HINT, devId);
  11375. if (ret != 0) {
  11376. ERROR_OUT(-7703, exit_rsa);
  11377. }
  11378. #ifndef NO_ASN
  11379. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  11380. if (ret != 0) {
  11381. ERROR_OUT(-7704, exit_rsa);
  11382. }
  11383. #ifndef NO_SIG_WRAPPER
  11384. modLen = wc_RsaEncryptSize(&key);
  11385. #endif
  11386. #else
  11387. #ifdef USE_CERT_BUFFERS_2048
  11388. ret = mp_read_unsigned_bin(&key.n, &tmp[12], 256);
  11389. if (ret != 0) {
  11390. ERROR_OUT(-7705, exit_rsa);
  11391. }
  11392. ret = mp_set_int(&key.e, WC_RSA_EXPONENT);
  11393. if (ret != 0) {
  11394. ERROR_OUT(-7706, exit_rsa);
  11395. }
  11396. #ifndef NO_SIG_WRAPPER
  11397. modLen = 2048;
  11398. #endif
  11399. #else
  11400. #error Not supported yet!
  11401. #endif
  11402. #endif
  11403. #ifndef WC_NO_RNG
  11404. #ifndef HAVE_FIPS
  11405. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  11406. #else
  11407. ret = wc_InitRng(&rng);
  11408. #endif
  11409. if (ret != 0) {
  11410. ERROR_OUT(-7707, exit_rsa);
  11411. }
  11412. #endif
  11413. #ifndef NO_SIG_WRAPPER
  11414. ret = rsa_sig_test(&key, sizeof(RsaKey), modLen, &rng);
  11415. if (ret != 0)
  11416. goto exit_rsa;
  11417. #endif
  11418. #ifdef WC_RSA_NONBLOCK
  11419. ret = rsa_nb_test(&key, in, inLen, out, outSz, plain, plainSz, &rng);
  11420. if (ret != 0)
  11421. goto exit_rsa;
  11422. #endif
  11423. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  11424. do {
  11425. #if defined(WOLFSSL_ASYNC_CRYPT)
  11426. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11427. #endif
  11428. if (ret >= 0) {
  11429. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, &key, &rng);
  11430. }
  11431. } while (ret == WC_PENDING_E);
  11432. if (ret < 0) {
  11433. ERROR_OUT(-7708, exit_rsa);
  11434. }
  11435. TEST_SLEEP();
  11436. #ifdef WC_RSA_BLINDING
  11437. {
  11438. int tmpret = ret;
  11439. ret = wc_RsaSetRNG(&key, &rng);
  11440. if (ret < 0) {
  11441. ERROR_OUT(-7709, exit_rsa);
  11442. }
  11443. ret = tmpret;
  11444. }
  11445. #endif
  11446. idx = (word32)ret; /* save off encrypted length */
  11447. do {
  11448. #if defined(WOLFSSL_ASYNC_CRYPT)
  11449. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11450. #endif
  11451. if (ret >= 0) {
  11452. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, &key);
  11453. }
  11454. } while (ret == WC_PENDING_E);
  11455. if (ret < 0) {
  11456. ERROR_OUT(-7710, exit_rsa);
  11457. }
  11458. if (XMEMCMP(plain, in, inLen)) {
  11459. ERROR_OUT(-7711, exit_rsa);
  11460. }
  11461. TEST_SLEEP();
  11462. do {
  11463. #if defined(WOLFSSL_ASYNC_CRYPT)
  11464. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11465. #endif
  11466. if (ret >= 0) {
  11467. ret = wc_RsaPrivateDecryptInline(out, idx, &res, &key);
  11468. }
  11469. } while (ret == WC_PENDING_E);
  11470. if (ret < 0) {
  11471. ERROR_OUT(-7712, exit_rsa);
  11472. }
  11473. if (ret != (int)inLen) {
  11474. ERROR_OUT(-7713, exit_rsa);
  11475. }
  11476. if (XMEMCMP(res, in, inLen)) {
  11477. ERROR_OUT(-7714, exit_rsa);
  11478. }
  11479. TEST_SLEEP();
  11480. do {
  11481. #if defined(WOLFSSL_ASYNC_CRYPT)
  11482. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11483. #endif
  11484. if (ret >= 0) {
  11485. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
  11486. }
  11487. } while (ret == WC_PENDING_E);
  11488. if (ret < 0) {
  11489. ERROR_OUT(-7715, exit_rsa);
  11490. }
  11491. TEST_SLEEP();
  11492. #elif defined(WOLFSSL_PUBLIC_MP)
  11493. (void)outSz;
  11494. (void)inLen;
  11495. (void)res;
  11496. {
  11497. byte signature_2048[] = {
  11498. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  11499. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  11500. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  11501. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  11502. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  11503. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  11504. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  11505. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  11506. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  11507. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  11508. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  11509. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  11510. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  11511. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  11512. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  11513. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  11514. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  11515. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  11516. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  11517. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  11518. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  11519. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  11520. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  11521. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  11522. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  11523. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  11524. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  11525. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  11526. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  11527. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  11528. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  11529. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  11530. };
  11531. ret = sizeof(signature_2048);
  11532. XMEMCPY(out, signature_2048, ret);
  11533. }
  11534. #endif
  11535. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_PUBLIC_MP)
  11536. idx = (word32)ret;
  11537. XMEMSET(plain, 0, plainSz);
  11538. do {
  11539. #if defined(WOLFSSL_ASYNC_CRYPT)
  11540. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11541. #endif
  11542. if (ret >= 0) {
  11543. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  11544. #if defined(WOLFSSL_CRYPTOCELL)
  11545. /*
  11546. Cryptocell requires the input data and signature byte array to verify.
  11547. first argument must be the input data
  11548. second argument must be the length of input data
  11549. third argument must be the signature byte array or the output from
  11550. wc_RsaSSL_Sign()
  11551. fourth argument must be the length of the signature byte array
  11552. */
  11553. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, &key);
  11554. #else
  11555. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
  11556. #endif /* WOLFSSL_CRYPTOCELL */
  11557. #else
  11558. byte* dec = NULL;
  11559. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, &key);
  11560. if (ret > 0) {
  11561. XMEMCPY(plain, dec, ret);
  11562. }
  11563. #endif
  11564. }
  11565. } while (ret == WC_PENDING_E);
  11566. if (ret < 0) {
  11567. ERROR_OUT(-7716, exit_rsa);
  11568. }
  11569. if (XMEMCMP(plain, in, (size_t)ret)) {
  11570. ERROR_OUT(-7717, exit_rsa);
  11571. }
  11572. TEST_SLEEP();
  11573. #endif
  11574. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  11575. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  11576. /* OAEP padding testing */
  11577. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  11578. (!defined(HAVE_FIPS) || \
  11579. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  11580. #ifndef NO_SHA
  11581. XMEMSET(plain, 0, plainSz);
  11582. do {
  11583. #if defined(WOLFSSL_ASYNC_CRYPT)
  11584. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11585. #endif
  11586. if (ret >= 0) {
  11587. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  11588. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  11589. }
  11590. } while (ret == WC_PENDING_E);
  11591. if (ret < 0) {
  11592. ERROR_OUT(-7718, exit_rsa);
  11593. }
  11594. TEST_SLEEP();
  11595. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  11596. idx = (word32)ret;
  11597. do {
  11598. #if defined(WOLFSSL_ASYNC_CRYPT)
  11599. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11600. #endif
  11601. if (ret >= 0) {
  11602. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  11603. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  11604. }
  11605. } while (ret == WC_PENDING_E);
  11606. if (ret < 0) {
  11607. ERROR_OUT(-7719, exit_rsa);
  11608. }
  11609. if (XMEMCMP(plain, in, inLen)) {
  11610. ERROR_OUT(-7720, exit_rsa);
  11611. }
  11612. TEST_SLEEP();
  11613. #endif /* NO_SHA */
  11614. #endif
  11615. #ifndef NO_SHA256
  11616. XMEMSET(plain, 0, plainSz);
  11617. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  11618. do {
  11619. #if defined(WOLFSSL_ASYNC_CRYPT)
  11620. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11621. #endif
  11622. if (ret >= 0) {
  11623. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  11624. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  11625. }
  11626. } while (ret == WC_PENDING_E);
  11627. if (ret < 0) {
  11628. ERROR_OUT(-7721, exit_rsa);
  11629. }
  11630. TEST_SLEEP();
  11631. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  11632. idx = (word32)ret;
  11633. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  11634. do {
  11635. #if defined(WOLFSSL_ASYNC_CRYPT)
  11636. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11637. #endif
  11638. if (ret >= 0) {
  11639. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  11640. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  11641. }
  11642. } while (ret == WC_PENDING_E);
  11643. if (ret < 0) {
  11644. ERROR_OUT(-7722, exit_rsa);
  11645. }
  11646. if (XMEMCMP(plain, in, inLen)) {
  11647. ERROR_OUT(-7723, exit_rsa);
  11648. }
  11649. TEST_SLEEP();
  11650. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  11651. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  11652. do {
  11653. #if defined(WOLFSSL_ASYNC_CRYPT)
  11654. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11655. #endif
  11656. if (ret >= 0) {
  11657. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, &key,
  11658. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  11659. }
  11660. } while (ret == WC_PENDING_E);
  11661. if (ret < 0) {
  11662. ERROR_OUT(-7724, exit_rsa);
  11663. }
  11664. if (ret != (int)inLen) {
  11665. ERROR_OUT(-7725, exit_rsa);
  11666. }
  11667. if (XMEMCMP(res, in, inLen)) {
  11668. ERROR_OUT(-7726, exit_rsa);
  11669. }
  11670. TEST_SLEEP();
  11671. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  11672. /* check fails if not using the same optional label */
  11673. XMEMSET(plain, 0, plainSz);
  11674. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  11675. do {
  11676. #if defined(WOLFSSL_ASYNC_CRYPT)
  11677. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11678. #endif
  11679. if (ret >= 0) {
  11680. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  11681. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  11682. }
  11683. } while (ret == WC_PENDING_E);
  11684. if (ret < 0) {
  11685. ERROR_OUT(-7727, exit_rsa);
  11686. }
  11687. TEST_SLEEP();
  11688. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  11689. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  11690. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  11691. !defined(WOLFSSL_CRYPTOCELL)
  11692. /* label is unused in cryptocell so it won't detect decrypt error due to label */
  11693. idx = (word32)ret;
  11694. do {
  11695. #if defined(WOLFSSL_ASYNC_CRYPT)
  11696. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11697. #endif
  11698. if (ret >= 0) {
  11699. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  11700. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  11701. }
  11702. } while (ret == WC_PENDING_E);
  11703. if (ret > 0) { /* in this case decrypt should fail */
  11704. ERROR_OUT(-7728, exit_rsa);
  11705. }
  11706. ret = 0;
  11707. TEST_SLEEP();
  11708. #endif /* !HAVE_CAVIUM */
  11709. /* check using optional label with encrypt/decrypt */
  11710. XMEMSET(plain, 0, plainSz);
  11711. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  11712. do {
  11713. #if defined(WOLFSSL_ASYNC_CRYPT)
  11714. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11715. #endif
  11716. if (ret >= 0) {
  11717. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  11718. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  11719. }
  11720. } while (ret == WC_PENDING_E);
  11721. if (ret < 0) {
  11722. ERROR_OUT(-7729, exit_rsa);
  11723. }
  11724. TEST_SLEEP();
  11725. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  11726. idx = (word32)ret;
  11727. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  11728. do {
  11729. #if defined(WOLFSSL_ASYNC_CRYPT)
  11730. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11731. #endif
  11732. if (ret >= 0) {
  11733. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  11734. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  11735. }
  11736. } while (ret == WC_PENDING_E);
  11737. if (ret < 0) {
  11738. ERROR_OUT(-7730, exit_rsa);
  11739. }
  11740. if (XMEMCMP(plain, in, inLen)) {
  11741. ERROR_OUT(-7731, exit_rsa);
  11742. }
  11743. TEST_SLEEP();
  11744. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  11745. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  11746. #ifndef NO_SHA
  11747. /* check fail using mismatch hash algorithms */
  11748. XMEMSET(plain, 0, plainSz);
  11749. do {
  11750. #if defined(WOLFSSL_ASYNC_CRYPT)
  11751. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11752. #endif
  11753. if (ret >= 0) {
  11754. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  11755. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  11756. }
  11757. } while (ret == WC_PENDING_E);
  11758. if (ret < 0) {
  11759. ERROR_OUT(-7732, exit_rsa);
  11760. }
  11761. TEST_SLEEP();
  11762. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  11763. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  11764. !defined(WOLFSSL_CRYPTOCELL)
  11765. idx = (word32)ret;
  11766. do {
  11767. #if defined(WOLFSSL_ASYNC_CRYPT)
  11768. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11769. #endif
  11770. if (ret >= 0) {
  11771. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  11772. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  11773. in, inLen);
  11774. }
  11775. } while (ret == WC_PENDING_E);
  11776. if (ret > 0) { /* should fail */
  11777. ERROR_OUT(-7733, exit_rsa);
  11778. }
  11779. ret = 0;
  11780. TEST_SLEEP();
  11781. #endif /* !HAVE_CAVIUM */
  11782. #endif /* NO_SHA */
  11783. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  11784. #endif /* NO_SHA256 */
  11785. #ifdef WOLFSSL_SHA512
  11786. /* Check valid RSA key size is used while using hash length of SHA512
  11787. If key size is less than (hash length * 2) + 2 then is invalid use
  11788. and test, since OAEP padding requires this.
  11789. BAD_FUNC_ARG is returned when this case is not met */
  11790. if (wc_RsaEncryptSize(&key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  11791. XMEMSET(plain, 0, plainSz);
  11792. do {
  11793. #if defined(WOLFSSL_ASYNC_CRYPT)
  11794. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11795. #endif
  11796. if (ret >= 0) {
  11797. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  11798. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  11799. }
  11800. } while (ret == WC_PENDING_E);
  11801. if (ret < 0) {
  11802. ERROR_OUT(-7734, exit_rsa);
  11803. }
  11804. TEST_SLEEP();
  11805. idx = ret;
  11806. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  11807. do {
  11808. #if defined(WOLFSSL_ASYNC_CRYPT)
  11809. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11810. #endif
  11811. if (ret >= 0) {
  11812. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  11813. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  11814. }
  11815. } while (ret == WC_PENDING_E);
  11816. if (ret < 0) {
  11817. ERROR_OUT(-7735, exit_rsa);
  11818. }
  11819. if (XMEMCMP(plain, in, inLen)) {
  11820. ERROR_OUT(-7736, exit_rsa);
  11821. }
  11822. TEST_SLEEP();
  11823. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  11824. }
  11825. #endif /* WOLFSSL_SHA512 */
  11826. /* check using pkcsv15 padding with _ex API */
  11827. XMEMSET(plain, 0, plainSz);
  11828. do {
  11829. #if defined(WOLFSSL_ASYNC_CRYPT)
  11830. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11831. #endif
  11832. if (ret >= 0) {
  11833. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  11834. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  11835. }
  11836. } while (ret == WC_PENDING_E);
  11837. if (ret < 0) {
  11838. ERROR_OUT(-7737, exit_rsa);
  11839. }
  11840. TEST_SLEEP();
  11841. idx = (word32)ret;
  11842. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  11843. do {
  11844. #if defined(WOLFSSL_ASYNC_CRYPT)
  11845. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11846. #endif
  11847. if (ret >= 0) {
  11848. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  11849. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  11850. }
  11851. } while (ret == WC_PENDING_E);
  11852. if (ret < 0) {
  11853. ERROR_OUT(-7738, exit_rsa);
  11854. }
  11855. if (XMEMCMP(plain, in, inLen)) {
  11856. ERROR_OUT(-7739, exit_rsa);
  11857. }
  11858. TEST_SLEEP();
  11859. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  11860. #endif /* !HAVE_FAST_RSA && !HAVE_FIPS */
  11861. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  11862. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  11863. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  11864. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  11865. ret = rsa_export_key_test(&key);
  11866. if (ret != 0)
  11867. return ret;
  11868. #endif
  11869. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  11870. ret = rsa_flatten_test(&key);
  11871. if (ret != 0)
  11872. return ret;
  11873. #endif
  11874. #if defined(WOLFSSL_MDK_ARM)
  11875. #define sizeof(s) XSTRLEN((char *)(s))
  11876. #endif
  11877. #ifdef USE_CERT_BUFFERS_1024
  11878. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  11879. bytes = (size_t)sizeof_client_cert_der_1024;
  11880. #elif defined(USE_CERT_BUFFERS_2048)
  11881. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  11882. bytes = (size_t)sizeof_client_cert_der_2048;
  11883. #elif defined(USE_CERT_BUFFERS_3072)
  11884. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  11885. bytes = (size_t)sizeof_client_cert_der_3072;
  11886. #elif defined(USE_CERT_BUFFERS_4096)
  11887. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  11888. bytes = (size_t)sizeof_client_cert_der_4096;
  11889. #elif !defined(NO_FILESYSTEM)
  11890. file2 = XFOPEN(clientCert, "rb");
  11891. if (!file2) {
  11892. ERROR_OUT(-7740, exit_rsa);
  11893. }
  11894. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  11895. XFCLOSE(file2);
  11896. #else
  11897. /* No certificate to use. */
  11898. ERROR_OUT(-7741, exit_rsa);
  11899. #endif
  11900. #ifdef sizeof
  11901. #undef sizeof
  11902. #endif
  11903. #ifdef WOLFSSL_TEST_CERT
  11904. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11905. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11906. if (ret != 0) {
  11907. FreeDecodedCert(&cert);
  11908. ERROR_OUT(-7742, exit_rsa);
  11909. }
  11910. FreeDecodedCert(&cert);
  11911. #else
  11912. (void)bytes;
  11913. #endif
  11914. #ifdef WOLFSSL_CERT_EXT
  11915. #ifdef USE_CERT_BUFFERS_1024
  11916. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  11917. bytes = sizeof_client_keypub_der_1024;
  11918. #elif defined(USE_CERT_BUFFERS_2048)
  11919. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  11920. bytes = sizeof_client_keypub_der_2048;
  11921. #elif defined(USE_CERT_BUFFERS_3072)
  11922. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  11923. bytes = sizeof_client_keypub_der_3072;
  11924. #elif defined(USE_CERT_BUFFERS_4096)
  11925. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  11926. bytes = sizeof_client_keypub_der_4096;
  11927. #else
  11928. file = XFOPEN(clientKeyPub, "rb");
  11929. if (!file) {
  11930. err_sys("can't open ./certs/client-keyPub.der, "
  11931. "Please run from wolfSSL home dir", -40);
  11932. ERROR_OUT(-7743, exit_rsa);
  11933. }
  11934. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11935. XFCLOSE(file);
  11936. #endif /* USE_CERT_BUFFERS */
  11937. ret = wc_InitRsaKey(&keypub, HEAP_HINT);
  11938. if (ret != 0) {
  11939. ERROR_OUT(-7744, exit_rsa);
  11940. }
  11941. idx = 0;
  11942. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keypub, (word32)bytes);
  11943. if (ret != 0) {
  11944. ERROR_OUT(-7745, exit_rsa);
  11945. }
  11946. #endif /* WOLFSSL_CERT_EXT */
  11947. #ifdef WOLFSSL_KEY_GEN
  11948. ret = rsa_keygen_test(&rng);
  11949. if (ret != 0)
  11950. goto exit_rsa;
  11951. #endif
  11952. #ifdef WOLFSSL_CERT_GEN
  11953. /* Make Cert / Sign example for RSA cert and RSA CA */
  11954. ret = rsa_certgen_test(&key, &keypub, &rng, tmp);
  11955. if (ret != 0)
  11956. goto exit_rsa;
  11957. #if !defined(NO_RSA) && defined(HAVE_ECC)
  11958. ret = rsa_ecc_certgen_test(&rng, tmp);
  11959. if (ret != 0)
  11960. goto exit_rsa;
  11961. #endif
  11962. #ifdef HAVE_NTRU
  11963. {
  11964. Cert myCert;
  11965. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  11966. XFILE caFile;
  11967. #endif
  11968. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  11969. XFILE ntruPrivFile;
  11970. #endif
  11971. int certSz;
  11972. word32 idx3 = 0;
  11973. #ifdef WOLFSSL_TEST_CERT
  11974. DecodedCert decode;
  11975. #endif
  11976. byte public_key[557]; /* sized for EES401EP2 */
  11977. word16 public_key_len; /* no. of octets in public key */
  11978. byte private_key[607]; /* sized for EES401EP2 */
  11979. word16 private_key_len; /* no. of octets in private key */
  11980. DRBG_HANDLE drbg;
  11981. static uint8_t const pers_str[] = {
  11982. 'C', 'y', 'a', 'S', 'S', 'L', ' ', 't', 'e', 's', 't'
  11983. };
  11984. word32 rc = ntru_crypto_drbg_instantiate(112, pers_str,
  11985. sizeof(pers_str), GetEntropy, &drbg);
  11986. if (rc != DRBG_OK) {
  11987. ERROR_OUT(-7746, exit_rsa);
  11988. }
  11989. rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2,
  11990. &public_key_len, NULL,
  11991. &private_key_len, NULL);
  11992. if (rc != NTRU_OK) {
  11993. ERROR_OUT(-7747, exit_rsa);
  11994. }
  11995. rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2,
  11996. &public_key_len, public_key,
  11997. &private_key_len, private_key);
  11998. if (rc != NTRU_OK) {
  11999. ERROR_OUT(-7748, exit_rsa);
  12000. }
  12001. rc = ntru_crypto_drbg_uninstantiate(drbg);
  12002. if (rc != NTRU_OK) {
  12003. ERROR_OUT(-7749, exit_rsa);
  12004. }
  12005. #ifdef USE_CERT_BUFFERS_1024
  12006. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  12007. bytes = sizeof_ca_key_der_1024;
  12008. #elif defined(USE_CERT_BUFFERS_2048)
  12009. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  12010. bytes = sizeof_ca_key_der_2048;
  12011. #else
  12012. caFile = XFOPEN(rsaCaKeyFile, "rb");
  12013. if (!caFile) {
  12014. ERROR_OUT(-7750, exit_rsa);
  12015. }
  12016. bytes = XFREAD(tmp, 1, FOURK_BUF, caFile);
  12017. XFCLOSE(caFile);
  12018. #endif /* USE_CERT_BUFFERS */
  12019. ret = wc_InitRsaKey(&caKey, HEAP_HINT);
  12020. if (ret != 0) {
  12021. ERROR_OUT(-7751, exit_rsa);
  12022. }
  12023. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes);
  12024. if (ret != 0) {
  12025. ERROR_OUT(-7752, exit_rsa);
  12026. }
  12027. if (wc_InitCert(&myCert)) {
  12028. ERROR_OUT(-7753, exit_rsa);
  12029. }
  12030. XMEMCPY(&myCert.subject, &certDefaultName, sizeof(CertName));
  12031. myCert.daysValid = 1000;
  12032. #ifdef WOLFSSL_CERT_EXT
  12033. /* add SKID from the Public Key */
  12034. if (wc_SetSubjectKeyIdFromNtruPublicKey(&myCert, public_key,
  12035. public_key_len) != 0) {
  12036. ERROR_OUT(-7754, exit_rsa);
  12037. }
  12038. /* add AKID from the CA certificate */
  12039. #if defined(USE_CERT_BUFFERS_2048)
  12040. ret = wc_SetAuthKeyIdFromCert(&myCert, ca_cert_der_2048,
  12041. sizeof_ca_cert_der_2048);
  12042. #elif defined(USE_CERT_BUFFERS_1024)
  12043. ret = wc_SetAuthKeyIdFromCert(&myCert, ca_cert_der_1024,
  12044. sizeof_ca_cert_der_1024);
  12045. #else
  12046. ret = wc_SetAuthKeyId(&myCert, rsaCaCertFile);
  12047. #endif
  12048. if (ret != 0) {
  12049. ERROR_OUT(-7755, exit_rsa);
  12050. }
  12051. /* add Key Usage */
  12052. if (wc_SetKeyUsage(&myCert, certKeyUsage2) != 0) {
  12053. ERROR_OUT(-7756, exit_rsa);
  12054. }
  12055. #endif /* WOLFSSL_CERT_EXT */
  12056. #if defined(USE_CERT_BUFFERS_2048)
  12057. ret = wc_SetIssuerBuffer(&myCert, ca_cert_der_2048,
  12058. sizeof_ca_cert_der_2048);
  12059. #elif defined(USE_CERT_BUFFERS_1024)
  12060. ret = wc_SetIssuerBuffer(&myCert, ca_cert_der_1024,
  12061. sizeof_ca_cert_der_1024);
  12062. #else
  12063. ret = wc_SetIssuer(&myCert, rsaCaCertFile);
  12064. #endif
  12065. if (ret < 0) {
  12066. ERROR_OUT(-7757, exit_rsa);
  12067. }
  12068. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12069. if (der == NULL) {
  12070. ERROR_OUT(-7758, exit_rsa);
  12071. }
  12072. certSz = wc_MakeNtruCert(&myCert, der, FOURK_BUF, public_key,
  12073. public_key_len, &rng);
  12074. if (certSz < 0) {
  12075. ERROR_OUT(-7760, exit_rsa);
  12076. }
  12077. ret = 0;
  12078. do {
  12079. #if defined(WOLFSSL_ASYNC_CRYPT)
  12080. ret = wc_AsyncWait(ret, &caKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12081. #endif
  12082. if (ret >= 0) {
  12083. ret = wc_SignCert(myCert.bodySz, myCert.sigType, der, FOURK_BUF,
  12084. &caKey, NULL, &rng);
  12085. }
  12086. } while (ret == WC_PENDING_E);
  12087. wc_FreeRsaKey(&caKey);
  12088. if (ret < 0) {
  12089. ERROR_OUT(-7761, exit_rsa);
  12090. }
  12091. certSz = ret;
  12092. #ifdef WOLFSSL_TEST_CERT
  12093. InitDecodedCert(&decode, der, certSz, HEAP_HINT);
  12094. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  12095. if (ret != 0) {
  12096. FreeDecodedCert(&decode);
  12097. ERROR_OUT(-7762, exit_rsa);
  12098. }
  12099. FreeDecodedCert(&decode);
  12100. #endif
  12101. ret = SaveDerAndPem(der, certSz, "./ntru-cert.der", "./ntru-cert.pem",
  12102. CERT_TYPE, -5637);
  12103. if (ret != 0) {
  12104. goto exit_rsa;
  12105. }
  12106. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  12107. ntruPrivFile = XFOPEN("./ntru-key.raw", "wb");
  12108. if (!ntruPrivFile) {
  12109. ERROR_OUT(-7763, exit_rsa);
  12110. }
  12111. ret = (int)XFWRITE(private_key, 1, private_key_len, ntruPrivFile);
  12112. XFCLOSE(ntruPrivFile);
  12113. if (ret != private_key_len) {
  12114. ERROR_OUT(-7764, exit_rsa);
  12115. }
  12116. #endif
  12117. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12118. der = NULL;
  12119. }
  12120. #endif /* HAVE_NTRU */
  12121. #ifdef WOLFSSL_CERT_REQ
  12122. {
  12123. Cert req;
  12124. int derSz;
  12125. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  12126. if (der == NULL) {
  12127. ERROR_OUT(-7765, exit_rsa);
  12128. }
  12129. if (wc_InitCert(&req)) {
  12130. ERROR_OUT(-7767, exit_rsa);
  12131. }
  12132. req.version = 0;
  12133. req.isCA = 1;
  12134. XSTRNCPY(req.challengePw, "wolf123", CTC_NAME_SIZE);
  12135. XMEMCPY(&req.subject, &certDefaultName, sizeof(CertName));
  12136. #ifndef NO_SHA256
  12137. req.sigType = CTC_SHA256wRSA;
  12138. #else
  12139. req.sigType = CTC_SHAwRSA;
  12140. #endif
  12141. #ifdef WOLFSSL_CERT_EXT
  12142. /* add SKID from the Public Key */
  12143. if (wc_SetSubjectKeyIdFromPublicKey(&req, &keypub, NULL) != 0) {
  12144. ERROR_OUT(-7768, exit_rsa);
  12145. }
  12146. /* add Key Usage */
  12147. if (wc_SetKeyUsage(&req, certKeyUsage2) != 0) {
  12148. ERROR_OUT(-7769, exit_rsa);
  12149. }
  12150. /* add Extended Key Usage */
  12151. if (wc_SetExtKeyUsage(&req, "serverAuth,clientAuth,codeSigning,"
  12152. "emailProtection,timeStamping,OCSPSigning") != 0) {
  12153. ERROR_OUT(-7770, exit_rsa);
  12154. }
  12155. #ifdef WOLFSSL_EKU_OID
  12156. {
  12157. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  12158. if (wc_SetExtKeyUsageOID(&req, unique, sizeof(unique), 0,
  12159. HEAP_HINT) != 0) {
  12160. ERROR_OUT(-7771, exit_rsa);
  12161. }
  12162. }
  12163. #endif /* WOLFSSL_EKU_OID */
  12164. #endif /* WOLFSSL_CERT_EXT */
  12165. derSz = wc_MakeCertReq(&req, der, FOURK_BUF, &key, NULL);
  12166. if (derSz < 0) {
  12167. ERROR_OUT(-7772, exit_rsa);
  12168. }
  12169. #ifdef WOLFSSL_CERT_EXT
  12170. /* Try again with "any" flag set, will override all others */
  12171. if (wc_SetExtKeyUsage(&req, "any") != 0) {
  12172. ERROR_OUT(-7773, exit_rsa);
  12173. }
  12174. derSz = wc_MakeCertReq(&req, der, FOURK_BUF, &key, NULL);
  12175. if (derSz < 0) {
  12176. ERROR_OUT(-7774, exit_rsa);
  12177. }
  12178. #endif /* WOLFSSL_CERT_EXT */
  12179. ret = 0;
  12180. do {
  12181. #if defined(WOLFSSL_ASYNC_CRYPT)
  12182. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12183. #endif
  12184. if (ret >= 0) {
  12185. ret = wc_SignCert(req.bodySz, req.sigType, der, FOURK_BUF,
  12186. &key, NULL, &rng);
  12187. }
  12188. } while (ret == WC_PENDING_E);
  12189. if (ret < 0) {
  12190. ERROR_OUT(-7775, exit_rsa);
  12191. }
  12192. derSz = ret;
  12193. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  12194. CERTREQ_TYPE, -5650);
  12195. if (ret != 0) {
  12196. goto exit_rsa;
  12197. }
  12198. derSz = wc_MakeCertReq_ex(&req, der, FOURK_BUF, RSA_TYPE, &key);
  12199. if (derSz < 0) {
  12200. ERROR_OUT(-7776, exit_rsa);
  12201. }
  12202. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12203. der = NULL;
  12204. }
  12205. #endif /* WOLFSSL_CERT_REQ */
  12206. #endif /* WOLFSSL_CERT_GEN */
  12207. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  12208. /* Need to create known good signatures to test with this. */
  12209. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  12210. ret = rsa_pss_test(&rng, &key);
  12211. #endif
  12212. #endif
  12213. exit_rsa:
  12214. wc_FreeRsaKey(&key);
  12215. #ifdef WOLFSSL_CERT_EXT
  12216. wc_FreeRsaKey(&keypub);
  12217. #endif
  12218. #if defined(HAVE_NTRU)
  12219. wc_FreeRsaKey(&caKey);
  12220. #endif
  12221. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12222. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12223. wc_FreeRng(&rng);
  12224. FREE_VAR(in, HEAP_HINT);
  12225. FREE_VAR(out, HEAP_HINT);
  12226. FREE_VAR(plain, HEAP_HINT);
  12227. /* ret can be greater then 0 with certgen but all negative values should
  12228. * be returned and treated as an error */
  12229. if (ret >= 0) {
  12230. return 0;
  12231. }
  12232. else {
  12233. return ret;
  12234. }
  12235. }
  12236. #endif /* !NO_RSA */
  12237. #ifndef NO_DH
  12238. static int dh_fips_generate_test(WC_RNG *rng)
  12239. {
  12240. int ret = 0;
  12241. DhKey key;
  12242. static byte p[] = {
  12243. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  12244. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  12245. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  12246. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  12247. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  12248. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  12249. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  12250. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  12251. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  12252. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  12253. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  12254. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  12255. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  12256. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  12257. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  12258. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  12259. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  12260. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  12261. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  12262. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  12263. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  12264. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  12265. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  12266. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  12267. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  12268. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  12269. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  12270. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  12271. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  12272. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  12273. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  12274. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  12275. };
  12276. static byte g[] = {
  12277. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  12278. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  12279. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  12280. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  12281. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  12282. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  12283. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  12284. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  12285. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  12286. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  12287. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  12288. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  12289. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  12290. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  12291. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  12292. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  12293. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  12294. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  12295. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  12296. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  12297. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  12298. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  12299. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  12300. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  12301. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  12302. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  12303. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  12304. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  12305. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  12306. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  12307. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  12308. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  12309. };
  12310. static byte q[] = {
  12311. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  12312. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  12313. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  12314. 0x40, 0x52, 0xed, 0x41
  12315. };
  12316. static byte q0[] = {
  12317. 0x00,
  12318. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  12319. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  12320. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  12321. 0x40, 0x52, 0xed, 0x41
  12322. };
  12323. byte priv[256];
  12324. byte pub[256];
  12325. word32 privSz = sizeof(priv);
  12326. word32 pubSz = sizeof(pub);
  12327. /* Parameter Validation testing. */
  12328. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  12329. if (ret != BAD_FUNC_ARG)
  12330. return -7777;
  12331. ret = wc_DhGenerateKeyPair(&key, NULL, priv, &privSz, pub, &pubSz);
  12332. if (ret != BAD_FUNC_ARG)
  12333. return -7778;
  12334. ret = wc_DhGenerateKeyPair(&key, rng, NULL, &privSz, pub, &pubSz);
  12335. if (ret != BAD_FUNC_ARG)
  12336. return -7779;
  12337. ret = wc_DhGenerateKeyPair(&key, rng, priv, NULL, pub, &pubSz);
  12338. if (ret != BAD_FUNC_ARG)
  12339. return -7780;
  12340. ret = wc_DhGenerateKeyPair(&key, rng, priv, &privSz, NULL, &pubSz);
  12341. if (ret != BAD_FUNC_ARG)
  12342. return -7781;
  12343. ret = wc_DhGenerateKeyPair(&key, rng, priv, &privSz, pub, NULL);
  12344. if (ret != BAD_FUNC_ARG)
  12345. return -7782;
  12346. ret = wc_InitDhKey_ex(&key, HEAP_HINT, devId);
  12347. if (ret != 0)
  12348. return -7783;
  12349. ret = wc_DhSetKey_ex(&key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  12350. if (ret != 0) {
  12351. ERROR_OUT(-7784, exit_gen_test);
  12352. }
  12353. wc_FreeDhKey(&key);
  12354. ret = wc_InitDhKey_ex(&key, HEAP_HINT, devId);
  12355. if (ret != 0)
  12356. return -7785;
  12357. ret = wc_DhSetKey_ex(&key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  12358. if (ret != 0) {
  12359. ERROR_OUT(-7786, exit_gen_test);
  12360. }
  12361. /* Use API. */
  12362. ret = wc_DhGenerateKeyPair(&key, rng, priv, &privSz, pub, &pubSz);
  12363. #if defined(WOLFSSL_ASYNC_CRYPT)
  12364. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  12365. #endif
  12366. if (ret != 0) {
  12367. ERROR_OUT(-7787, exit_gen_test);
  12368. }
  12369. ret = wc_DhCheckPubKey_ex(&key, pub, pubSz, q0, sizeof(q0));
  12370. if (ret != 0) {
  12371. ERROR_OUT(-7788, exit_gen_test);
  12372. }
  12373. wc_FreeDhKey(&key);
  12374. ret = wc_InitDhKey_ex(&key, HEAP_HINT, devId);
  12375. if (ret != 0)
  12376. return -7789;
  12377. ret = wc_DhSetKey(&key, p, sizeof(p), g, sizeof(g));
  12378. if (ret != 0) {
  12379. ERROR_OUT(-7790, exit_gen_test);
  12380. }
  12381. ret = wc_DhCheckPubKey_ex(&key, pub, pubSz, q, sizeof(q));
  12382. if (ret != 0) {
  12383. ERROR_OUT(-7791, exit_gen_test);
  12384. }
  12385. #ifndef HAVE_SELFTEST
  12386. ret = wc_DhCheckKeyPair(&key, pub, pubSz, priv, privSz);
  12387. if (ret != 0) {
  12388. ERROR_OUT(-7792, exit_gen_test);
  12389. }
  12390. /* Taint the public key so the check fails. */
  12391. pub[0]++;
  12392. ret = wc_DhCheckKeyPair(&key, pub, pubSz, priv, privSz);
  12393. if (ret != MP_CMP_E) {
  12394. ERROR_OUT(-7793, exit_gen_test);
  12395. }
  12396. #ifdef WOLFSSL_KEY_GEN
  12397. wc_FreeDhKey(&key);
  12398. ret = wc_InitDhKey_ex(&key, HEAP_HINT, devId);
  12399. if (ret != 0)
  12400. return -7794;
  12401. ret = wc_DhGenerateParams(rng, 2048, &key);
  12402. if (ret != 0) {
  12403. ERROR_OUT(-7795, exit_gen_test);
  12404. }
  12405. privSz = sizeof(priv);
  12406. pubSz = sizeof(pub);
  12407. ret = wc_DhGenerateKeyPair(&key, rng, priv, &privSz, pub, &pubSz);
  12408. #if defined(WOLFSSL_ASYNC_CRYPT)
  12409. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  12410. #endif
  12411. if (ret != 0) {
  12412. ERROR_OUT(-7796, exit_gen_test);
  12413. }
  12414. #endif /* WOLFSSL_KEY_GEN */
  12415. #endif /* HAVE_SELFTEST */
  12416. ret = 0;
  12417. exit_gen_test:
  12418. wc_FreeDhKey(&key);
  12419. return ret;
  12420. }
  12421. static int dh_generate_test(WC_RNG *rng)
  12422. {
  12423. int ret = 0;
  12424. DhKey smallKey;
  12425. byte p[2] = { 0, 5 };
  12426. byte g[2] = { 0, 2 };
  12427. #ifndef WOLFSSL_SP_MATH
  12428. #ifdef WOLFSSL_DH_CONST
  12429. /* the table for constant DH lookup will round to the lowest byte size 21 */
  12430. byte priv[21];
  12431. byte pub[21];
  12432. #else
  12433. byte priv[2];
  12434. byte pub[2];
  12435. #endif
  12436. word32 privSz = sizeof(priv);
  12437. word32 pubSz = sizeof(pub);
  12438. #endif
  12439. ret = wc_InitDhKey_ex(&smallKey, HEAP_HINT, devId);
  12440. if (ret != 0)
  12441. return -7797;
  12442. /* Parameter Validation testing. */
  12443. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  12444. if (ret != BAD_FUNC_ARG)
  12445. return -7798;
  12446. wc_FreeDhKey(NULL);
  12447. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  12448. if (ret != BAD_FUNC_ARG) {
  12449. ERROR_OUT(-7799, exit_gen_test);
  12450. }
  12451. ret = wc_DhSetKey(&smallKey, NULL, sizeof(p), g, sizeof(g));
  12452. if (ret != BAD_FUNC_ARG) {
  12453. ERROR_OUT(-7800, exit_gen_test);
  12454. }
  12455. ret = wc_DhSetKey(&smallKey, p, 0, g, sizeof(g));
  12456. if (ret != BAD_FUNC_ARG) {
  12457. ERROR_OUT(-7801, exit_gen_test);
  12458. }
  12459. ret = wc_DhSetKey(&smallKey, p, sizeof(p), NULL, sizeof(g));
  12460. if (ret != BAD_FUNC_ARG) {
  12461. ERROR_OUT(-7802, exit_gen_test);
  12462. }
  12463. ret = wc_DhSetKey(&smallKey, p, sizeof(p), g, 0);
  12464. if (ret != BAD_FUNC_ARG) {
  12465. ERROR_OUT(-7803, exit_gen_test);
  12466. }
  12467. ret = wc_DhSetKey(&smallKey, p, sizeof(p), g, sizeof(g));
  12468. if (ret != 0) {
  12469. ERROR_OUT(-7804, exit_gen_test);
  12470. }
  12471. #ifndef WOLFSSL_SP_MATH
  12472. /* Use API. */
  12473. ret = wc_DhGenerateKeyPair(&smallKey, rng, priv, &privSz, pub, &pubSz);
  12474. #if defined(WOLFSSL_ASYNC_CRYPT)
  12475. ret = wc_AsyncWait(ret, &smallKey.asyncDev, WC_ASYNC_FLAG_NONE);
  12476. #endif
  12477. if (ret != 0) {
  12478. ret = -7805;
  12479. }
  12480. #else
  12481. (void)rng;
  12482. ret = 0;
  12483. #endif
  12484. exit_gen_test:
  12485. wc_FreeDhKey(&smallKey);
  12486. return ret;
  12487. }
  12488. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12489. typedef struct dh_pubvalue_test {
  12490. const byte* data;
  12491. word32 len;
  12492. } dh_pubvalue_test;
  12493. static int dh_test_check_pubvalue(void)
  12494. {
  12495. int ret;
  12496. word32 i;
  12497. const byte prime[] = {0x01, 0x00, 0x01};
  12498. const byte pubValZero[] = { 0x00 };
  12499. const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  12500. const byte pubValOne[] = { 0x01 };
  12501. const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  12502. const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  12503. const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  12504. const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  12505. const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  12506. const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  12507. const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  12508. const dh_pubvalue_test dh_pubval_fail[] = {
  12509. { prime, sizeof(prime) },
  12510. { pubValZero, sizeof(pubValZero) },
  12511. { pubValZeroLong, sizeof(pubValZeroLong) },
  12512. { pubValOne, sizeof(pubValOne) },
  12513. { pubValOneLong, sizeof(pubValOneLong) },
  12514. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  12515. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  12516. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  12517. { pubValTooBig0, sizeof(pubValTooBig0) },
  12518. { pubValTooBig1, sizeof(pubValTooBig1) },
  12519. { pubValTooLong, sizeof(pubValTooLong) },
  12520. };
  12521. const byte pubValTwo[] = { 0x02 };
  12522. const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  12523. const byte pubValGood[] = { 0x12, 0x34 };
  12524. const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  12525. const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  12526. const dh_pubvalue_test dh_pubval_pass[] = {
  12527. { pubValTwo, sizeof(pubValTwo) },
  12528. { pubValTwoLong, sizeof(pubValTwoLong) },
  12529. { pubValGood, sizeof(pubValGood) },
  12530. { pubValGoodLen, sizeof(pubValGoodLen) },
  12531. { pubValGoodLong, sizeof(pubValGoodLong) },
  12532. };
  12533. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  12534. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  12535. dh_pubval_fail[i].len);
  12536. if (ret != MP_VAL)
  12537. return -7806 - (int)i;
  12538. }
  12539. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  12540. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  12541. dh_pubval_pass[i].len);
  12542. if (ret != 0)
  12543. return -7816 - (int)i;
  12544. }
  12545. return 0;
  12546. }
  12547. #endif
  12548. #if defined(HAVE_FFDHE)
  12549. #ifdef HAVE_FFDHE_3072
  12550. #define FFDHE_KEY_SIZE (3072/8)
  12551. #else
  12552. #define FFDHE_KEY_SIZE (2048/8)
  12553. #endif
  12554. static int dh_test_ffdhe(WC_RNG *rng, const DhParams* params)
  12555. {
  12556. int ret;
  12557. word32 privSz, pubSz, privSz2, pubSz2;
  12558. byte priv[FFDHE_KEY_SIZE];
  12559. byte pub[FFDHE_KEY_SIZE];
  12560. byte priv2[FFDHE_KEY_SIZE];
  12561. byte pub2[FFDHE_KEY_SIZE];
  12562. byte agree[FFDHE_KEY_SIZE];
  12563. byte agree2[FFDHE_KEY_SIZE];
  12564. word32 agreeSz = (word32)sizeof(agree);
  12565. word32 agreeSz2 = (word32)sizeof(agree2);
  12566. DhKey key;
  12567. DhKey key2;
  12568. XMEMSET(&key, 0, sizeof(DhKey));
  12569. XMEMSET(&key2, 0, sizeof(DhKey));
  12570. ret = wc_InitDhKey_ex(&key, HEAP_HINT, devId);
  12571. if (ret != 0) {
  12572. ERROR_OUT(-7826, done);
  12573. }
  12574. ret = wc_InitDhKey_ex(&key2, HEAP_HINT, devId);
  12575. if (ret != 0) {
  12576. ERROR_OUT(-7827, done);
  12577. }
  12578. ret = wc_DhSetKey(&key, params->p, params->p_len, params->g, params->g_len);
  12579. if (ret != 0) {
  12580. ERROR_OUT(-7828, done);
  12581. }
  12582. ret = wc_DhSetKey(&key2, params->p, params->p_len, params->g,
  12583. params->g_len);
  12584. if (ret != 0) {
  12585. ERROR_OUT(-7829, done);
  12586. }
  12587. ret = wc_DhGenerateKeyPair(&key, rng, priv, &privSz, pub, &pubSz);
  12588. #if defined(WOLFSSL_ASYNC_CRYPT)
  12589. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  12590. #endif
  12591. if (ret != 0) {
  12592. ERROR_OUT(-7830, done);
  12593. }
  12594. ret = wc_DhGenerateKeyPair(&key2, rng, priv2, &privSz2, pub2, &pubSz2);
  12595. #if defined(WOLFSSL_ASYNC_CRYPT)
  12596. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  12597. #endif
  12598. if (ret != 0) {
  12599. ERROR_OUT(-7831, done);
  12600. }
  12601. ret = wc_DhAgree(&key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  12602. #if defined(WOLFSSL_ASYNC_CRYPT)
  12603. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  12604. #endif
  12605. if (ret != 0) {
  12606. ERROR_OUT(-7832, done);
  12607. }
  12608. ret = wc_DhAgree(&key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  12609. #if defined(WOLFSSL_ASYNC_CRYPT)
  12610. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  12611. #endif
  12612. if (ret != 0) {
  12613. ERROR_OUT(-7833, done);
  12614. }
  12615. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  12616. ERROR_OUT(-7834, done);
  12617. }
  12618. done:
  12619. wc_FreeDhKey(&key);
  12620. wc_FreeDhKey(&key2);
  12621. return ret;
  12622. }
  12623. #endif /* HAVE_FFDHE */
  12624. int dh_test(void)
  12625. {
  12626. int ret;
  12627. word32 bytes;
  12628. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  12629. byte tmp[1024];
  12630. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  12631. byte priv[256];
  12632. byte pub[256];
  12633. byte priv2[256];
  12634. byte pub2[256];
  12635. byte agree[256];
  12636. byte agree2[256];
  12637. #else
  12638. byte priv[512];
  12639. byte pub[512];
  12640. byte priv2[512];
  12641. byte pub2[512];
  12642. byte agree[512];
  12643. byte agree2[512];
  12644. #endif
  12645. word32 agreeSz = (word32)sizeof(agree);
  12646. word32 agreeSz2 = (word32)sizeof(agree2);
  12647. DhKey key;
  12648. DhKey key2;
  12649. WC_RNG rng;
  12650. int keyInit = 0;
  12651. #ifdef USE_CERT_BUFFERS_1024
  12652. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  12653. bytes = (size_t)sizeof_dh_key_der_1024;
  12654. #elif defined(USE_CERT_BUFFERS_2048)
  12655. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  12656. bytes = (size_t)sizeof_dh_key_der_2048;
  12657. #elif defined(USE_CERT_BUFFERS_3072)
  12658. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  12659. bytes = (size_t)sizeof_dh_key_der_3072;
  12660. #elif defined(USE_CERT_BUFFERS_4096)
  12661. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  12662. bytes = (size_t)sizeof_dh_key_der_4096;
  12663. #elif defined(NO_ASN)
  12664. /* don't use file, no DER parsing */
  12665. #elif !defined(NO_FILESYSTEM)
  12666. XFILE file = XFOPEN(dhParamsFile, "rb");
  12667. if (!file)
  12668. return -7900;
  12669. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), file);
  12670. XFCLOSE(file);
  12671. #else
  12672. /* No DH key to use. */
  12673. return -7901;
  12674. #endif /* USE_CERT_BUFFERS */
  12675. (void)idx;
  12676. (void)tmp;
  12677. (void)bytes;
  12678. XMEMSET(&rng, 0, sizeof(rng));
  12679. /* Use API for coverage. */
  12680. ret = wc_InitDhKey(&key);
  12681. if (ret != 0) {
  12682. ERROR_OUT(-7902, done);
  12683. }
  12684. wc_FreeDhKey(&key);
  12685. ret = wc_InitDhKey_ex(&key, HEAP_HINT, devId);
  12686. if (ret != 0) {
  12687. ERROR_OUT(-7903, done);
  12688. }
  12689. keyInit = 1;
  12690. ret = wc_InitDhKey_ex(&key2, HEAP_HINT, devId);
  12691. if (ret != 0) {
  12692. ERROR_OUT(-7904, done);
  12693. }
  12694. #ifdef NO_ASN
  12695. ret = wc_DhSetKey(&key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  12696. if (ret != 0) {
  12697. ERROR_OUT(-7905, done);
  12698. }
  12699. ret = wc_DhSetKey(&key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  12700. if (ret != 0) {
  12701. ERROR_OUT(-7906, done);
  12702. }
  12703. #else
  12704. ret = wc_DhKeyDecode(tmp, &idx, &key, bytes);
  12705. if (ret != 0) {
  12706. ERROR_OUT(-7907, done);
  12707. }
  12708. idx = 0;
  12709. ret = wc_DhKeyDecode(tmp, &idx, &key2, bytes);
  12710. if (ret != 0) {
  12711. ERROR_OUT(-7908, done);
  12712. }
  12713. #endif
  12714. #ifndef HAVE_FIPS
  12715. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  12716. #else
  12717. ret = wc_InitRng(&rng);
  12718. #endif
  12719. if (ret != 0) {
  12720. ERROR_OUT(-7909, done);
  12721. }
  12722. ret = wc_DhGenerateKeyPair(&key, &rng, priv, &privSz, pub, &pubSz);
  12723. #if defined(WOLFSSL_ASYNC_CRYPT)
  12724. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  12725. #endif
  12726. if (ret != 0) {
  12727. ERROR_OUT(-7910, done);
  12728. }
  12729. ret = wc_DhGenerateKeyPair(&key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  12730. #if defined(WOLFSSL_ASYNC_CRYPT)
  12731. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  12732. #endif
  12733. if (ret != 0) {
  12734. ERROR_OUT(-7911, done);
  12735. }
  12736. ret = wc_DhAgree(&key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  12737. #if defined(WOLFSSL_ASYNC_CRYPT)
  12738. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  12739. #endif
  12740. if (ret != 0) {
  12741. ERROR_OUT(-7912, done);
  12742. }
  12743. ret = wc_DhAgree(&key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  12744. #if defined(WOLFSSL_ASYNC_CRYPT)
  12745. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  12746. #endif
  12747. if (ret != 0) {
  12748. ERROR_OUT(-7913, done);
  12749. }
  12750. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  12751. ERROR_OUT(-7914, done);
  12752. }
  12753. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12754. if (wc_DhCheckPrivKey(NULL, NULL, 0) != BAD_FUNC_ARG)
  12755. return -7915;
  12756. if (wc_DhCheckPrivKey(&key, priv, privSz) != 0)
  12757. return -7916;
  12758. if (wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL) != BAD_FUNC_ARG)
  12759. return -7917;
  12760. {
  12761. word32 pSz, qSz, gSz;
  12762. if (wc_DhExportParamsRaw(&key, NULL, &pSz, NULL, &qSz, NULL, &gSz) != LENGTH_ONLY_E)
  12763. return -7918;
  12764. }
  12765. #endif
  12766. /* Test DH key import / export */
  12767. #ifdef WOLFSSL_DH_EXTRA
  12768. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM)
  12769. file = XFOPEN(dhKeyFile, "rb");
  12770. if (!file)
  12771. return -7950;
  12772. bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), file);
  12773. XFCLOSE(file);
  12774. idx = 0;
  12775. ret = wc_DhKeyDecode(tmp, &idx, &key, bytes);
  12776. if (ret != 0) {
  12777. return -7951;
  12778. }
  12779. #endif
  12780. privSz = sizeof(priv);
  12781. pubSz = sizeof(pub);
  12782. ret = wc_DhExportKeyPair(&key, priv, &privSz, pub, &pubSz);
  12783. if (ret != 0) {
  12784. return -7952;
  12785. }
  12786. ret = wc_DhImportKeyPair(&key2, priv, privSz, pub, pubSz);
  12787. if (ret != 0) {
  12788. return -7953;
  12789. }
  12790. #endif /* WOLFSSL_DH_EXTRA */
  12791. ret = dh_generate_test(&rng);
  12792. if (ret == 0)
  12793. ret = dh_fips_generate_test(&rng);
  12794. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12795. if (ret == 0)
  12796. ret = dh_test_check_pubvalue();
  12797. #endif
  12798. /* Specialized code for key gen when using FFDHE-2048 and FFDHE-3072. */
  12799. #ifdef HAVE_FFDHE_2048
  12800. if (ret == 0) {
  12801. ret = dh_test_ffdhe(&rng, wc_Dh_ffdhe2048_Get());
  12802. if (ret != 0)
  12803. printf("error with FFDHE 2048\n");
  12804. }
  12805. #endif
  12806. #ifdef HAVE_FFDHE_3072
  12807. if (ret == 0) {
  12808. ret = dh_test_ffdhe(&rng, wc_Dh_ffdhe3072_Get());
  12809. if (ret != 0)
  12810. printf("error with FFDHE 3072\n");
  12811. }
  12812. #endif
  12813. wc_FreeDhKey(&key);
  12814. keyInit = 0;
  12815. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12816. !defined(WOLFSSL_OLD_PRIME_CHECK)
  12817. if (ret == 0) {
  12818. /* Test Check Key */
  12819. ret = wc_DhSetCheckKey(&key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  12820. NULL, 0, 0, &rng);
  12821. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  12822. }
  12823. #endif
  12824. done:
  12825. if (keyInit)
  12826. wc_FreeDhKey(&key);
  12827. wc_FreeDhKey(&key2);
  12828. wc_FreeRng(&rng);
  12829. return ret;
  12830. }
  12831. #endif /* NO_DH */
  12832. #ifndef NO_DSA
  12833. int dsa_test(void)
  12834. {
  12835. int ret, answer;
  12836. word32 bytes;
  12837. word32 idx = 0;
  12838. byte tmp[1024];
  12839. DsaKey key;
  12840. WC_RNG rng;
  12841. wc_Sha sha;
  12842. byte hash[WC_SHA_DIGEST_SIZE];
  12843. byte signature[40];
  12844. #ifdef USE_CERT_BUFFERS_1024
  12845. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  12846. bytes = sizeof_dsa_key_der_1024;
  12847. #elif defined(USE_CERT_BUFFERS_2048)
  12848. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  12849. bytes = sizeof_dsa_key_der_2048;
  12850. #else
  12851. XFILE file = XFOPEN(dsaKey, "rb");
  12852. if (!file)
  12853. return -8000;
  12854. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), file);
  12855. XFCLOSE(file);
  12856. #endif /* USE_CERT_BUFFERS */
  12857. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  12858. if (ret != 0)
  12859. return -8001;
  12860. wc_ShaUpdate(&sha, tmp, bytes);
  12861. wc_ShaFinal(&sha, hash);
  12862. wc_ShaFree(&sha);
  12863. ret = wc_InitDsaKey(&key);
  12864. if (ret != 0) return -8002;
  12865. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  12866. if (ret != 0) return -8003;
  12867. #ifndef HAVE_FIPS
  12868. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  12869. #else
  12870. ret = wc_InitRng(&rng);
  12871. #endif
  12872. if (ret != 0) return -8004;
  12873. ret = wc_DsaSign(hash, signature, &key, &rng);
  12874. if (ret != 0) return -8005;
  12875. ret = wc_DsaVerify(hash, signature, &key, &answer);
  12876. if (ret != 0) return -8006;
  12877. if (answer != 1) return -8007;
  12878. wc_FreeDsaKey(&key);
  12879. #ifdef WOLFSSL_KEY_GEN
  12880. {
  12881. byte* der;
  12882. int derSz = 0;
  12883. DsaKey derIn;
  12884. DsaKey genKey;
  12885. ret = wc_InitDsaKey(&genKey);
  12886. if (ret != 0) return -8008;
  12887. ret = wc_MakeDsaParameters(&rng, 1024, &genKey);
  12888. if (ret != 0) {
  12889. wc_FreeDsaKey(&genKey);
  12890. return -8009;
  12891. }
  12892. ret = wc_MakeDsaKey(&rng, &genKey);
  12893. if (ret != 0) {
  12894. wc_FreeDsaKey(&genKey);
  12895. return -8010;
  12896. }
  12897. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12898. if (der == NULL) {
  12899. wc_FreeDsaKey(&genKey);
  12900. return -8011;
  12901. }
  12902. derSz = wc_DsaKeyToDer(&genKey, der, FOURK_BUF);
  12903. if (derSz < 0) {
  12904. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12905. return -8013;
  12906. }
  12907. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  12908. DSA_PRIVATEKEY_TYPE, -5814);
  12909. if (ret != 0) {
  12910. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12911. wc_FreeDsaKey(&genKey);
  12912. return ret;
  12913. }
  12914. ret = wc_InitDsaKey(&derIn);
  12915. if (ret != 0) {
  12916. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12917. wc_FreeDsaKey(&genKey);
  12918. return -8014;
  12919. }
  12920. idx = 0;
  12921. ret = wc_DsaPrivateKeyDecode(der, &idx, &derIn, derSz);
  12922. if (ret != 0) {
  12923. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12924. wc_FreeDsaKey(&derIn);
  12925. wc_FreeDsaKey(&genKey);
  12926. return -8015;
  12927. }
  12928. wc_FreeDsaKey(&derIn);
  12929. wc_FreeDsaKey(&genKey);
  12930. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12931. }
  12932. #endif /* WOLFSSL_KEY_GEN */
  12933. if (wc_InitDsaKey_h(&key, NULL) != 0)
  12934. return -8016;
  12935. wc_FreeRng(&rng);
  12936. return 0;
  12937. }
  12938. #endif /* NO_DSA */
  12939. #ifdef WOLFCRYPT_HAVE_SRP
  12940. static int generate_random_salt(byte *buf, word32 size)
  12941. {
  12942. int ret = -8017;
  12943. WC_RNG rng;
  12944. if(NULL == buf || !size)
  12945. return -8018;
  12946. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  12947. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  12948. wc_FreeRng(&rng);
  12949. }
  12950. return ret;
  12951. }
  12952. int srp_test(void)
  12953. {
  12954. Srp cli, srv;
  12955. int r;
  12956. byte clientPubKey[80]; /* A */
  12957. byte serverPubKey[80]; /* B */
  12958. word32 clientPubKeySz = 80;
  12959. word32 serverPubKeySz = 80;
  12960. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  12961. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  12962. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  12963. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  12964. byte username[] = "user";
  12965. word32 usernameSz = 4;
  12966. byte password[] = "password";
  12967. word32 passwordSz = 8;
  12968. byte N[] = {
  12969. 0xC9, 0x4D, 0x67, 0xEB, 0x5B, 0x1A, 0x23, 0x46, 0xE8, 0xAB, 0x42, 0x2F,
  12970. 0xC6, 0xA0, 0xED, 0xAE, 0xDA, 0x8C, 0x7F, 0x89, 0x4C, 0x9E, 0xEE, 0xC4,
  12971. 0x2F, 0x9E, 0xD2, 0x50, 0xFD, 0x7F, 0x00, 0x46, 0xE5, 0xAF, 0x2C, 0xF7,
  12972. 0x3D, 0x6B, 0x2F, 0xA2, 0x6B, 0xB0, 0x80, 0x33, 0xDA, 0x4D, 0xE3, 0x22,
  12973. 0xE1, 0x44, 0xE7, 0xA8, 0xE9, 0xB1, 0x2A, 0x0E, 0x46, 0x37, 0xF6, 0x37,
  12974. 0x1F, 0x34, 0xA2, 0x07, 0x1C, 0x4B, 0x38, 0x36, 0xCB, 0xEE, 0xAB, 0x15,
  12975. 0x03, 0x44, 0x60, 0xFA, 0xA7, 0xAD, 0xF4, 0x83
  12976. };
  12977. byte g[] = {
  12978. 0x02
  12979. };
  12980. byte salt[10];
  12981. byte verifier[80];
  12982. word32 v_size = sizeof(verifier);
  12983. /* set as 0's so if second init on srv not called SrpTerm is not on
  12984. * garbage values */
  12985. XMEMSET(&srv, 0, sizeof(Srp));
  12986. XMEMSET(&cli, 0, sizeof(Srp));
  12987. /* generating random salt */
  12988. r = generate_random_salt(salt, sizeof(salt));
  12989. /* client knows username and password. */
  12990. /* server knows N, g, salt and verifier. */
  12991. if (!r) r = wc_SrpInit(&cli, SRP_TYPE_SHA, SRP_CLIENT_SIDE);
  12992. if (!r) r = wc_SrpSetUsername(&cli, username, usernameSz);
  12993. /* loading N, g and salt in advance to generate the verifier. */
  12994. if (!r) r = wc_SrpSetParams(&cli, N, sizeof(N),
  12995. g, sizeof(g),
  12996. salt, sizeof(salt));
  12997. if (!r) r = wc_SrpSetPassword(&cli, password, passwordSz);
  12998. if (!r) r = wc_SrpGetVerifier(&cli, verifier, &v_size);
  12999. /* client sends username to server */
  13000. if (!r) r = wc_SrpInit(&srv, SRP_TYPE_SHA, SRP_SERVER_SIDE);
  13001. if (!r) r = wc_SrpSetUsername(&srv, username, usernameSz);
  13002. if (!r) r = wc_SrpSetParams(&srv, N, sizeof(N),
  13003. g, sizeof(g),
  13004. salt, sizeof(salt));
  13005. if (!r) r = wc_SrpSetVerifier(&srv, verifier, v_size);
  13006. if (!r) r = wc_SrpGetPublic(&srv, serverPubKey, &serverPubKeySz);
  13007. /* server sends N, g, salt and B to client */
  13008. if (!r) r = wc_SrpGetPublic(&cli, clientPubKey, &clientPubKeySz);
  13009. if (!r) r = wc_SrpComputeKey(&cli, clientPubKey, clientPubKeySz,
  13010. serverPubKey, serverPubKeySz);
  13011. if (!r) r = wc_SrpGetProof(&cli, clientProof, &clientProofSz);
  13012. /* client sends A and M1 to server */
  13013. if (!r) r = wc_SrpComputeKey(&srv, clientPubKey, clientPubKeySz,
  13014. serverPubKey, serverPubKeySz);
  13015. if (!r) r = wc_SrpVerifyPeersProof(&srv, clientProof, clientProofSz);
  13016. if (!r) r = wc_SrpGetProof(&srv, serverProof, &serverProofSz);
  13017. /* server sends M2 to client */
  13018. if (!r) r = wc_SrpVerifyPeersProof(&cli, serverProof, serverProofSz);
  13019. wc_SrpTerm(&cli);
  13020. wc_SrpTerm(&srv);
  13021. return r;
  13022. }
  13023. #endif /* WOLFCRYPT_HAVE_SRP */
  13024. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  13025. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  13026. static int openssl_aes_test(void)
  13027. {
  13028. #ifdef HAVE_AES_CBC
  13029. #ifdef WOLFSSL_AES_128
  13030. {
  13031. /* EVP_CipherUpdate test */
  13032. const byte cbcPlain[] =
  13033. {
  13034. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  13035. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  13036. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  13037. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  13038. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  13039. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  13040. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  13041. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  13042. };
  13043. byte key[] = "0123456789abcdef "; /* align */
  13044. byte iv[] = "1234567890abcdef "; /* align */
  13045. byte cipher[AES_BLOCK_SIZE * 4];
  13046. byte plain [AES_BLOCK_SIZE * 4];
  13047. EVP_CIPHER_CTX en;
  13048. EVP_CIPHER_CTX de;
  13049. int outlen ;
  13050. int total = 0;
  13051. int i;
  13052. EVP_CIPHER_CTX_init(&en);
  13053. if (EVP_CipherInit(&en, EVP_aes_128_cbc(),
  13054. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  13055. return -8200;
  13056. if (EVP_CipherUpdate(&en, (byte*)cipher, &outlen,
  13057. (byte*)cbcPlain, 9) == 0)
  13058. return -8201;
  13059. if (outlen != 0)
  13060. return -8202;
  13061. total += outlen;
  13062. if (EVP_CipherUpdate(&en, (byte*)&cipher[total], &outlen,
  13063. (byte*)&cbcPlain[9] , 9) == 0)
  13064. return -8203;
  13065. if (outlen != 16)
  13066. return -8204;
  13067. total += outlen;
  13068. if (EVP_CipherFinal(&en, (byte*)&cipher[total], &outlen) == 0)
  13069. return -8205;
  13070. if (outlen != 16)
  13071. return -8206;
  13072. total += outlen;
  13073. if (total != 32)
  13074. return 3408;
  13075. total = 0;
  13076. EVP_CIPHER_CTX_init(&de);
  13077. if (EVP_CipherInit(&de, EVP_aes_128_cbc(),
  13078. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  13079. return -8207;
  13080. if (EVP_CipherUpdate(&de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  13081. return -8208;
  13082. if (outlen != 0)
  13083. return -8209;
  13084. total += outlen;
  13085. if (EVP_CipherUpdate(&de, (byte*)&plain[total], &outlen,
  13086. (byte*)&cipher[6], 12) == 0)
  13087. return -8210;
  13088. if (outlen != 0)
  13089. total += outlen;
  13090. if (EVP_CipherUpdate(&de, (byte*)&plain[total], &outlen,
  13091. (byte*)&cipher[6+12], 14) == 0)
  13092. return -8211;
  13093. if (outlen != 16)
  13094. return -8212;
  13095. total += outlen;
  13096. if (EVP_CipherFinal(&de, (byte*)&plain[total], &outlen) == 0)
  13097. return -8213;
  13098. if (outlen != 2)
  13099. return -8214;
  13100. total += outlen;
  13101. if (total != 18)
  13102. return 3427;
  13103. if (XMEMCMP(plain, cbcPlain, 18))
  13104. return -8215;
  13105. /* test with encrypting/decrypting more than 16 bytes at once */
  13106. total = 0;
  13107. EVP_CIPHER_CTX_init(&en);
  13108. if (EVP_CipherInit(&en, EVP_aes_128_cbc(),
  13109. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  13110. return -8216;
  13111. if (EVP_CipherUpdate(&en, (byte*)cipher, &outlen,
  13112. (byte*)cbcPlain, 17) == 0)
  13113. return -8217;
  13114. if (outlen != 16)
  13115. return -8218;
  13116. total += outlen;
  13117. if (EVP_CipherUpdate(&en, (byte*)&cipher[total], &outlen,
  13118. (byte*)&cbcPlain[17] , 1) == 0)
  13119. return -8219;
  13120. if (outlen != 0)
  13121. return -8220;
  13122. total += outlen;
  13123. if (EVP_CipherFinal(&en, (byte*)&cipher[total], &outlen) == 0)
  13124. return -8221;
  13125. if (outlen != 16)
  13126. return -8222;
  13127. total += outlen;
  13128. if (total != 32)
  13129. return -8223;
  13130. total = 0;
  13131. EVP_CIPHER_CTX_init(&de);
  13132. if (EVP_CipherInit(&de, EVP_aes_128_cbc(),
  13133. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  13134. return -8224;
  13135. if (EVP_CipherUpdate(&de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  13136. return -8225;
  13137. if (outlen != 16)
  13138. return -8226;
  13139. total += outlen;
  13140. /* final call on non block size should fail */
  13141. if (EVP_CipherFinal(&de, (byte*)&plain[total], &outlen) != 0)
  13142. return -8227;
  13143. if (EVP_CipherUpdate(&de, (byte*)&plain[total], &outlen,
  13144. (byte*)&cipher[17], 1) == 0)
  13145. return -8228;
  13146. if (outlen != 0)
  13147. total += outlen;
  13148. if (EVP_CipherUpdate(&de, (byte*)&plain[total], &outlen,
  13149. (byte*)&cipher[17+1], 14) == 0)
  13150. return -8229;
  13151. if (outlen != 0)
  13152. return -8230;
  13153. total += outlen;
  13154. if (EVP_CipherFinal(&de, (byte*)&plain[total], &outlen) == 0)
  13155. return -8231;
  13156. if (outlen != 2)
  13157. return -8232;
  13158. total += outlen;
  13159. if (total != 18)
  13160. return -8233;
  13161. if (XMEMCMP(plain, cbcPlain, 18))
  13162. return -8234;
  13163. /* test byte by byte decrypt */
  13164. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  13165. plain[i] = i;
  13166. }
  13167. total = 0;
  13168. EVP_CIPHER_CTX_init(&en);
  13169. if (EVP_CipherInit(&en, EVP_aes_128_cbc(),
  13170. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  13171. return -8235;
  13172. if (EVP_CipherUpdate(&en, (byte*)cipher, &outlen,
  13173. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  13174. return -8236;
  13175. if (outlen != AES_BLOCK_SIZE * 3)
  13176. return -8237;
  13177. total += outlen;
  13178. if (EVP_CipherFinal(&en, (byte*)&cipher[total], &outlen) == 0)
  13179. return -8238;
  13180. if (outlen != AES_BLOCK_SIZE)
  13181. return -8239;
  13182. total += outlen;
  13183. if (total != sizeof(plain))
  13184. return -8240;
  13185. total = 0;
  13186. EVP_CIPHER_CTX_init(&de);
  13187. if (EVP_CipherInit(&de, EVP_aes_128_cbc(),
  13188. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  13189. return -8241;
  13190. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  13191. if (EVP_CipherUpdate(&de, (byte*)plain + total, &outlen,
  13192. (byte*)cipher + i, 1) == 0)
  13193. return -8242;
  13194. if (outlen > 0) {
  13195. int j;
  13196. total += outlen;
  13197. for (j = 0; j < total; j++) {
  13198. if (plain[j] != j) {
  13199. return -8243;
  13200. }
  13201. }
  13202. }
  13203. }
  13204. if (EVP_CipherFinal(&de, (byte*)&plain[total], &outlen) == 0)
  13205. return -8244;
  13206. total += outlen;
  13207. if (total != AES_BLOCK_SIZE * 3) {
  13208. return -8245;
  13209. }
  13210. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  13211. if (plain[i] != i) {
  13212. return -8246;
  13213. }
  13214. }
  13215. }
  13216. /* set buffers to be exact size to catch potential over read/write */
  13217. {
  13218. /* EVP_CipherUpdate test */
  13219. const byte cbcPlain[] =
  13220. {
  13221. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  13222. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  13223. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  13224. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  13225. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  13226. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  13227. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  13228. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  13229. };
  13230. byte key[] = "0123456789abcdef "; /* align */
  13231. byte iv[] = "1234567890abcdef "; /* align */
  13232. #define EVP_TEST_BUF_SZ 18
  13233. #define EVP_TEST_BUF_PAD 32
  13234. byte cipher[EVP_TEST_BUF_SZ];
  13235. byte plain [EVP_TEST_BUF_SZ];
  13236. byte padded[EVP_TEST_BUF_PAD];
  13237. EVP_CIPHER_CTX en;
  13238. EVP_CIPHER_CTX de;
  13239. int outlen ;
  13240. int total = 0;
  13241. EVP_CIPHER_CTX_init(&en);
  13242. if (EVP_CipherInit(&en, EVP_aes_128_cbc(),
  13243. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  13244. return -8247;
  13245. if (EVP_CIPHER_CTX_set_padding(&en, 0) != 1)
  13246. return -8248;
  13247. if (EVP_CipherUpdate(&en, (byte*)cipher, &outlen,
  13248. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  13249. return -8249;
  13250. if (outlen != 16)
  13251. return -8250;
  13252. total += outlen;
  13253. /* should fail here */
  13254. if (EVP_CipherFinal(&en, (byte*)&cipher[total], &outlen) != 0)
  13255. return -8251;
  13256. /* turn padding back on and do successful encrypt */
  13257. total = 0;
  13258. EVP_CIPHER_CTX_init(&en);
  13259. if (EVP_CipherInit(&en, EVP_aes_128_cbc(),
  13260. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  13261. return -8252;
  13262. if (EVP_CIPHER_CTX_set_padding(&en, 1) != 1)
  13263. return -8253;
  13264. if (EVP_CipherUpdate(&en, (byte*)padded, &outlen,
  13265. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  13266. return -8254;
  13267. if (outlen != 16)
  13268. return -8255;
  13269. total += outlen;
  13270. if (EVP_CipherFinal(&en, (byte*)&padded[total], &outlen) == 0)
  13271. return -8256;
  13272. total += outlen;
  13273. if (total != 32)
  13274. return -8257;
  13275. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  13276. /* test out of bounds read on buffers w/o padding during decryption */
  13277. total = 0;
  13278. EVP_CIPHER_CTX_init(&de);
  13279. if (EVP_CipherInit(&de, EVP_aes_128_cbc(),
  13280. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  13281. return -8258;
  13282. if (EVP_CIPHER_CTX_set_padding(&de, 0) != 1)
  13283. return -8259;
  13284. if (EVP_CipherUpdate(&de, (byte*)plain, &outlen, (byte*)cipher,
  13285. EVP_TEST_BUF_SZ) == 0)
  13286. return -8260;
  13287. if (outlen != 16)
  13288. return -8261;
  13289. total += outlen;
  13290. /* should fail since not using padding */
  13291. if (EVP_CipherFinal(&de, (byte*)&plain[total], &outlen) != 0)
  13292. return -8262;
  13293. total = 0;
  13294. EVP_CIPHER_CTX_init(&de);
  13295. if (EVP_CipherInit(&de, EVP_aes_128_cbc(),
  13296. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  13297. return -8263;
  13298. if (EVP_CIPHER_CTX_set_padding(&de, 1) != 1)
  13299. return -8264;
  13300. if (EVP_CipherUpdate(&de, (byte*)padded, &outlen, (byte*)padded,
  13301. EVP_TEST_BUF_PAD) == 0)
  13302. return -8265;
  13303. if (outlen != 16)
  13304. return -8266;
  13305. total += outlen;
  13306. if (EVP_CipherFinal(&de, (byte*)&padded[total], &outlen) == 0)
  13307. return -8267;
  13308. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  13309. return -8268;
  13310. }
  13311. { /* evp_cipher test: EVP_aes_128_cbc */
  13312. EVP_CIPHER_CTX ctx;
  13313. const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  13314. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  13315. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  13316. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  13317. };
  13318. const byte verify[] =
  13319. {
  13320. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  13321. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  13322. };
  13323. byte key[] = "0123456789abcdef "; /* align */
  13324. byte iv[] = "1234567890abcdef "; /* align */
  13325. byte cipher[AES_BLOCK_SIZE * 4];
  13326. byte plain [AES_BLOCK_SIZE * 4];
  13327. EVP_CIPHER_CTX_init(&ctx);
  13328. if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  13329. return -8269;
  13330. if (EVP_Cipher(&ctx, cipher, (byte*)msg, 16) == 0)
  13331. return -8270;
  13332. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  13333. return -8271;
  13334. EVP_CIPHER_CTX_init(&ctx);
  13335. if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  13336. return -8272;
  13337. if (EVP_Cipher(&ctx, plain, cipher, 16) == 0)
  13338. return -8273;
  13339. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  13340. return -8274;
  13341. } /* end evp_cipher test: EVP_aes_128_cbc*/
  13342. #endif /* WOLFSSL_AES_128 */
  13343. #endif /* HAVE_AES_CBC */
  13344. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  13345. { /* evp_cipher test: EVP_aes_256_ecb*/
  13346. EVP_CIPHER_CTX ctx;
  13347. const byte msg[] =
  13348. {
  13349. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  13350. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  13351. };
  13352. const byte verify[] =
  13353. {
  13354. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  13355. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  13356. };
  13357. const byte key[] =
  13358. {
  13359. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  13360. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  13361. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  13362. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  13363. };
  13364. byte cipher[AES_BLOCK_SIZE * 4];
  13365. byte plain [AES_BLOCK_SIZE * 4];
  13366. EVP_CIPHER_CTX_init(&ctx);
  13367. if (EVP_CipherInit(&ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  13368. return -8275;
  13369. if (EVP_Cipher(&ctx, cipher, (byte*)msg, 16) == 0)
  13370. return -8276;
  13371. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  13372. return -8277;
  13373. EVP_CIPHER_CTX_init(&ctx);
  13374. if (EVP_CipherInit(&ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  13375. return -8278;
  13376. if (EVP_Cipher(&ctx, plain, cipher, 16) == 0)
  13377. return -8279;
  13378. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  13379. return -8280;
  13380. } /* end evp_cipher test */
  13381. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  13382. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  13383. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  13384. {
  13385. /* Test: AES_encrypt/decrypt/set Key */
  13386. AES_KEY enc;
  13387. #ifdef HAVE_AES_DECRYPT
  13388. AES_KEY dec;
  13389. #endif
  13390. const byte msg[] =
  13391. {
  13392. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  13393. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  13394. };
  13395. const byte verify[] =
  13396. {
  13397. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  13398. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  13399. };
  13400. const byte key[] =
  13401. {
  13402. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  13403. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  13404. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  13405. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  13406. };
  13407. byte plain[sizeof(msg)];
  13408. byte cipher[sizeof(msg)];
  13409. AES_set_encrypt_key(key, sizeof(key)*8, &enc);
  13410. AES_set_decrypt_key(key, sizeof(key)*8, &dec);
  13411. AES_encrypt(msg, cipher, &enc);
  13412. #ifdef HAVE_AES_DECRYPT
  13413. AES_decrypt(cipher, plain, &dec);
  13414. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  13415. return -8281;
  13416. #endif /* HAVE_AES_DECRYPT */
  13417. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  13418. return -8282;
  13419. }
  13420. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  13421. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  13422. #ifdef WOLFSSL_AES_COUNTER
  13423. {
  13424. byte plainBuff [64];
  13425. byte cipherBuff[64];
  13426. #ifdef WOLFSSL_AES_128
  13427. const byte ctrKey[] =
  13428. {
  13429. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  13430. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  13431. };
  13432. const byte ctrIv[] =
  13433. {
  13434. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  13435. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  13436. };
  13437. const byte ctrPlain[] =
  13438. {
  13439. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  13440. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  13441. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  13442. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  13443. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  13444. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  13445. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  13446. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  13447. };
  13448. const byte ctrCipher[] =
  13449. {
  13450. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  13451. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  13452. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  13453. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  13454. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  13455. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  13456. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  13457. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  13458. };
  13459. const byte oddCipher[] =
  13460. {
  13461. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  13462. 0xc2
  13463. };
  13464. #endif
  13465. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  13466. * NIST Special Publication 800-38A */
  13467. #ifdef WOLFSSL_AES_192
  13468. const byte ctr192Key[] =
  13469. {
  13470. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  13471. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  13472. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  13473. };
  13474. const byte ctr192Iv[] =
  13475. {
  13476. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  13477. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  13478. };
  13479. const byte ctr192Plain[] =
  13480. {
  13481. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  13482. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  13483. };
  13484. const byte ctr192Cipher[] =
  13485. {
  13486. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  13487. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  13488. };
  13489. #endif /* WOLFSSL_AES_192 */
  13490. #ifdef WOLFSSL_AES_256
  13491. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  13492. * NIST Special Publication 800-38A */
  13493. const byte ctr256Key[] =
  13494. {
  13495. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  13496. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  13497. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  13498. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  13499. };
  13500. const byte ctr256Iv[] =
  13501. {
  13502. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  13503. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  13504. };
  13505. const byte ctr256Plain[] =
  13506. {
  13507. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  13508. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  13509. };
  13510. const byte ctr256Cipher[] =
  13511. {
  13512. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  13513. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  13514. };
  13515. #endif /* WOLFSSL_AES_256 */
  13516. EVP_CIPHER_CTX en;
  13517. EVP_CIPHER_CTX de;
  13518. #ifdef WOLFSSL_AES_128
  13519. EVP_CIPHER_CTX *p_en;
  13520. EVP_CIPHER_CTX *p_de;
  13521. EVP_CIPHER_CTX_init(&en);
  13522. if (EVP_CipherInit(&en, EVP_aes_128_ctr(),
  13523. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  13524. return -8283;
  13525. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctrPlain,
  13526. AES_BLOCK_SIZE*4) == 0)
  13527. return -8284;
  13528. EVP_CIPHER_CTX_init(&de);
  13529. if (EVP_CipherInit(&de, EVP_aes_128_ctr(),
  13530. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  13531. return -8285;
  13532. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff,
  13533. AES_BLOCK_SIZE*4) == 0)
  13534. return -8286;
  13535. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  13536. return -8287;
  13537. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  13538. return -8288;
  13539. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  13540. if (p_en == NULL)
  13541. return -8289;
  13542. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  13543. if (p_de == NULL)
  13544. return -8290;
  13545. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  13546. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  13547. return -8291;
  13548. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  13549. AES_BLOCK_SIZE*4) == 0)
  13550. return -8292;
  13551. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  13552. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  13553. return -8293;
  13554. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  13555. AES_BLOCK_SIZE*4) == 0)
  13556. return -8294;
  13557. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  13558. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  13559. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  13560. return -8295;
  13561. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  13562. return -8296;
  13563. EVP_CIPHER_CTX_init(&en);
  13564. if (EVP_CipherInit(&en, EVP_aes_128_ctr(),
  13565. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  13566. return -8297;
  13567. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctrPlain, 9) == 0)
  13568. return -8298;
  13569. EVP_CIPHER_CTX_init(&de);
  13570. if (EVP_CipherInit(&de, EVP_aes_128_ctr(),
  13571. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  13572. return -8299;
  13573. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff, 9) == 0)
  13574. return -8300;
  13575. if (XMEMCMP(plainBuff, ctrPlain, 9))
  13576. return -8301;
  13577. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  13578. return -8302;
  13579. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctrPlain, 9) == 0)
  13580. return -8303;
  13581. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff, 9) == 0)
  13582. return -8304;
  13583. if (XMEMCMP(plainBuff, ctrPlain, 9))
  13584. return -8305;
  13585. if (XMEMCMP(cipherBuff, oddCipher, 9))
  13586. return -8306;
  13587. #endif /* WOLFSSL_AES_128 */
  13588. #ifdef WOLFSSL_AES_192
  13589. EVP_CIPHER_CTX_init(&en);
  13590. if (EVP_CipherInit(&en, EVP_aes_192_ctr(),
  13591. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  13592. return -8307;
  13593. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctr192Plain,
  13594. AES_BLOCK_SIZE) == 0)
  13595. return -8308;
  13596. EVP_CIPHER_CTX_init(&de);
  13597. if (EVP_CipherInit(&de, EVP_aes_192_ctr(),
  13598. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  13599. return -8309;
  13600. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  13601. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff,
  13602. AES_BLOCK_SIZE) == 0)
  13603. return -8310;
  13604. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  13605. return -8311;
  13606. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  13607. return -8312;
  13608. #endif /* WOLFSSL_AES_192 */
  13609. #ifdef WOLFSSL_AES_256
  13610. EVP_CIPHER_CTX_init(&en);
  13611. if (EVP_CipherInit(&en, EVP_aes_256_ctr(),
  13612. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  13613. return -8313;
  13614. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctr256Plain,
  13615. AES_BLOCK_SIZE) == 0)
  13616. return -8314;
  13617. EVP_CIPHER_CTX_init(&de);
  13618. if (EVP_CipherInit(&de, EVP_aes_256_ctr(),
  13619. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  13620. return -8315;
  13621. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  13622. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff,
  13623. AES_BLOCK_SIZE) == 0)
  13624. return -8316;
  13625. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  13626. return -8317;
  13627. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  13628. return -8318;
  13629. #endif /* WOLFSSL_AES_256 */
  13630. }
  13631. #endif /* HAVE_AES_COUNTER */
  13632. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  13633. {
  13634. AES_KEY enc;
  13635. AES_KEY dec;
  13636. const byte setIv[] = {
  13637. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  13638. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  13639. };
  13640. const byte key[] =
  13641. {
  13642. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  13643. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  13644. };
  13645. const byte cipher1[] =
  13646. {
  13647. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  13648. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  13649. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  13650. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  13651. };
  13652. const byte msg[] =
  13653. {
  13654. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  13655. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  13656. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  13657. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  13658. };
  13659. byte cipher[AES_BLOCK_SIZE * 2];
  13660. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  13661. int num = 0;
  13662. XMEMCPY(iv, setIv, sizeof(setIv));
  13663. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, &enc);
  13664. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, &dec);
  13665. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, &enc, iv,
  13666. &num, AES_ENCRYPT);
  13667. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  13668. return -8319;
  13669. if (num != 15) /* should have used 15 of the 16 bytes */
  13670. return -8320;
  13671. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  13672. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, &enc, iv,
  13673. &num, AES_ENCRYPT);
  13674. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  13675. return -8321;
  13676. if (num != 0)
  13677. return -8322;
  13678. }
  13679. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  13680. return 0;
  13681. }
  13682. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  13683. int openssl_test(void)
  13684. {
  13685. EVP_MD_CTX md_ctx;
  13686. testVector a, b, c, d, e, f;
  13687. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  13688. a.inLen = 0;
  13689. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  13690. (void)a;
  13691. (void)b;
  13692. (void)c;
  13693. (void)d;
  13694. (void)e;
  13695. (void)f;
  13696. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  13697. {
  13698. byte* p;
  13699. p = (byte*)CRYPTO_malloc(10);
  13700. if (p == NULL) {
  13701. return -8400;
  13702. }
  13703. XMEMSET(p, 0, 10);
  13704. CRYPTO_free(p);
  13705. }
  13706. #ifndef NO_MD5
  13707. a.input = "1234567890123456789012345678901234567890123456789012345678"
  13708. "9012345678901234567890";
  13709. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  13710. "\x7a";
  13711. a.inLen = XSTRLEN(a.input);
  13712. a.outLen = WC_MD5_DIGEST_SIZE;
  13713. EVP_MD_CTX_init(&md_ctx);
  13714. EVP_DigestInit(&md_ctx, EVP_md5());
  13715. EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  13716. EVP_DigestFinal(&md_ctx, hash, 0);
  13717. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0)
  13718. return -8401;
  13719. #endif /* NO_MD5 */
  13720. #ifndef NO_SHA
  13721. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  13722. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  13723. "aaaaaaaaaa";
  13724. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  13725. "\x53\x99\x5E\x26\xA0";
  13726. b.inLen = XSTRLEN(b.input);
  13727. b.outLen = WC_SHA_DIGEST_SIZE;
  13728. EVP_MD_CTX_init(&md_ctx);
  13729. EVP_DigestInit(&md_ctx, EVP_sha1());
  13730. EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  13731. EVP_DigestFinal(&md_ctx, hash, 0);
  13732. if (XMEMCMP(hash, b.output, WC_SHA_DIGEST_SIZE) != 0)
  13733. return -8402;
  13734. #endif /* NO_SHA */
  13735. #ifdef WOLFSSL_SHA224
  13736. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  13737. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  13738. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  13739. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  13740. e.inLen = XSTRLEN(e.input);
  13741. e.outLen = WC_SHA224_DIGEST_SIZE;
  13742. EVP_MD_CTX_init(&md_ctx);
  13743. EVP_DigestInit(&md_ctx, EVP_sha224());
  13744. EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  13745. EVP_DigestFinal(&md_ctx, hash, 0);
  13746. if (XMEMCMP(hash, e.output, WC_SHA224_DIGEST_SIZE) != 0)
  13747. return -8403;
  13748. #endif /* WOLFSSL_SHA224 */
  13749. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  13750. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  13751. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  13752. "\x06\xC1";
  13753. d.inLen = XSTRLEN(d.input);
  13754. d.outLen = WC_SHA256_DIGEST_SIZE;
  13755. EVP_MD_CTX_init(&md_ctx);
  13756. EVP_DigestInit(&md_ctx, EVP_sha256());
  13757. EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  13758. EVP_DigestFinal(&md_ctx, hash, 0);
  13759. if (XMEMCMP(hash, d.output, WC_SHA256_DIGEST_SIZE) != 0)
  13760. return -8404;
  13761. #ifdef WOLFSSL_SHA384
  13762. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  13763. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  13764. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  13765. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  13766. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  13767. "\x74\x60\x39";
  13768. e.inLen = XSTRLEN(e.input);
  13769. e.outLen = WC_SHA384_DIGEST_SIZE;
  13770. EVP_MD_CTX_init(&md_ctx);
  13771. EVP_DigestInit(&md_ctx, EVP_sha384());
  13772. EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  13773. EVP_DigestFinal(&md_ctx, hash, 0);
  13774. if (XMEMCMP(hash, e.output, WC_SHA384_DIGEST_SIZE) != 0)
  13775. return -8405;
  13776. #endif /* WOLFSSL_SHA384 */
  13777. #ifdef WOLFSSL_SHA512
  13778. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  13779. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  13780. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  13781. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  13782. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  13783. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  13784. "\x87\x4b\xe9\x09";
  13785. f.inLen = XSTRLEN(f.input);
  13786. f.outLen = WC_SHA512_DIGEST_SIZE;
  13787. EVP_MD_CTX_init(&md_ctx);
  13788. EVP_DigestInit(&md_ctx, EVP_sha512());
  13789. EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  13790. EVP_DigestFinal(&md_ctx, hash, 0);
  13791. if (XMEMCMP(hash, f.output, WC_SHA512_DIGEST_SIZE) != 0)
  13792. return -8406;
  13793. #endif /* WOLFSSL_SHA512 */
  13794. #ifdef WOLFSSL_SHA3
  13795. #ifndef WOLFSSL_NOSHA3_224
  13796. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  13797. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  13798. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  13799. e.inLen = XSTRLEN(e.input);
  13800. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  13801. EVP_MD_CTX_init(&md_ctx);
  13802. EVP_DigestInit(&md_ctx, EVP_sha3_224());
  13803. EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  13804. EVP_DigestFinal(&md_ctx, hash, 0);
  13805. if (XMEMCMP(hash, e.output, WC_SHA3_224_DIGEST_SIZE) != 0)
  13806. return -8407;
  13807. #endif /* WOLFSSL_NOSHA3_224 */
  13808. #ifndef WOLFSSL_NOSHA3_256
  13809. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  13810. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  13811. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  13812. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  13813. "\x1d\x18";
  13814. d.inLen = XSTRLEN(d.input);
  13815. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  13816. EVP_MD_CTX_init(&md_ctx);
  13817. EVP_DigestInit(&md_ctx, EVP_sha3_256());
  13818. EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  13819. EVP_DigestFinal(&md_ctx, hash, 0);
  13820. if (XMEMCMP(hash, d.output, WC_SHA3_256_DIGEST_SIZE) != 0)
  13821. return -8408;
  13822. #endif /* WOLFSSL_NOSHA3_256 */
  13823. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  13824. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  13825. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79\xaa\x7f\xc7";
  13826. e.inLen = XSTRLEN(e.input);
  13827. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  13828. EVP_MD_CTX_init(&md_ctx);
  13829. EVP_DigestInit(&md_ctx, EVP_sha3_384());
  13830. EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  13831. EVP_DigestFinal(&md_ctx, hash, 0);
  13832. if (XMEMCMP(hash, e.output, WC_SHA3_384_DIGEST_SIZE) != 0)
  13833. return -8409;
  13834. #ifndef WOLFSSL_NOSHA3_512
  13835. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  13836. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  13837. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55\xf2\x1d\xd1\x85";
  13838. f.inLen = XSTRLEN(f.input);
  13839. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  13840. EVP_MD_CTX_init(&md_ctx);
  13841. EVP_DigestInit(&md_ctx, EVP_sha3_512());
  13842. EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  13843. EVP_DigestFinal(&md_ctx, hash, 0);
  13844. if (XMEMCMP(hash, f.output, WC_SHA3_512_DIGEST_SIZE) != 0)
  13845. return -8410;
  13846. #endif /* WOLFSSL_NOSHA3_512 */
  13847. #endif /* WOLFSSL_SHA3 */
  13848. #ifndef NO_MD5
  13849. if (RAND_bytes(hash, sizeof(hash)) != 1)
  13850. return -8411;
  13851. c.input = "what do ya want for nothing?";
  13852. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  13853. "\x76";
  13854. c.inLen = XSTRLEN(c.input);
  13855. c.outLen = WC_MD5_DIGEST_SIZE;
  13856. HMAC(EVP_md5(),
  13857. "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen, hash, 0);
  13858. if (XMEMCMP(hash, c.output, WC_MD5_DIGEST_SIZE) != 0)
  13859. return -8412;
  13860. #endif /* NO_MD5 */
  13861. #ifndef NO_DES3
  13862. { /* des test */
  13863. const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  13864. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  13865. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  13866. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  13867. };
  13868. byte plain[24];
  13869. byte cipher[24];
  13870. const_DES_cblock key =
  13871. {
  13872. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  13873. };
  13874. DES_cblock iv =
  13875. {
  13876. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  13877. };
  13878. DES_key_schedule sched;
  13879. const byte verify[] =
  13880. {
  13881. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  13882. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  13883. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  13884. };
  13885. DES_key_sched(&key, &sched);
  13886. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  13887. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  13888. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  13889. return -8413;
  13890. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  13891. return -8414;
  13892. /* test changing iv */
  13893. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  13894. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  13895. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  13896. return -8415;
  13897. } /* end des test */
  13898. #endif /* NO_DES3 */
  13899. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  13900. if (openssl_aes_test() != 0) {
  13901. return -8416;
  13902. }
  13903. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  13904. { /* evp_cipher test: EVP_aes_128_cbc */
  13905. EVP_CIPHER_CTX ctx;
  13906. int idx, cipherSz, plainSz;
  13907. const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  13908. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  13909. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  13910. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  13911. };
  13912. const byte verify[] =
  13913. {
  13914. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  13915. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  13916. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  13917. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  13918. };
  13919. const byte verify2[] =
  13920. {
  13921. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  13922. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  13923. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  13924. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  13925. };
  13926. byte key[] = "0123456789abcdef "; /* align */
  13927. byte iv[] = "1234567890abcdef "; /* align */
  13928. byte cipher[AES_BLOCK_SIZE * 4];
  13929. byte plain [AES_BLOCK_SIZE * 4];
  13930. EVP_CIPHER_CTX_init(&ctx);
  13931. if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  13932. return -8417;
  13933. if (EVP_CipherUpdate(&ctx, cipher, &idx, (byte*)msg, sizeof(msg)) == 0)
  13934. return -8418;
  13935. cipherSz = idx;
  13936. if (EVP_CipherFinal(&ctx, cipher + cipherSz, &idx) == 0)
  13937. return -8419;
  13938. cipherSz += idx;
  13939. if ((cipherSz != (int)sizeof(verify)) &&
  13940. XMEMCMP(cipher, verify, cipherSz))
  13941. return -8420;
  13942. EVP_CIPHER_CTX_init(&ctx);
  13943. if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  13944. return -8421;
  13945. /* check partial decrypt (not enough padding for full block) */
  13946. if (EVP_CipherUpdate(&ctx, plain, &idx, cipher, 1) == 0)
  13947. return -8422;
  13948. plainSz = idx;
  13949. if (EVP_CipherFinal(&ctx, plain + plainSz, &idx) != 0)
  13950. return -8423;
  13951. EVP_CIPHER_CTX_init(&ctx);
  13952. if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  13953. return -8424;
  13954. if (EVP_CipherUpdate(&ctx, plain, &idx, cipher, cipherSz) == 0)
  13955. return -8425;
  13956. plainSz = idx;
  13957. if (EVP_CipherFinal(&ctx, plain + plainSz, &idx) == 0)
  13958. return -8426;
  13959. plainSz += idx;
  13960. if ((plainSz != sizeof(msg)) || XMEMCMP(plain, msg, sizeof(msg)))
  13961. return -8427;
  13962. EVP_CIPHER_CTX_init(&ctx);
  13963. if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  13964. return -8428;
  13965. if (EVP_CipherUpdate(&ctx, cipher, &idx, msg, AES_BLOCK_SIZE) == 0)
  13966. return -8429;
  13967. cipherSz = idx;
  13968. if (EVP_CipherFinal(&ctx, cipher + cipherSz, &idx) == 0)
  13969. return -8430;
  13970. cipherSz += idx;
  13971. if ((cipherSz != (int)sizeof(verify2)) ||
  13972. XMEMCMP(cipher, verify2, cipherSz))
  13973. return -8431;
  13974. } /* end evp_cipher test: EVP_aes_128_cbc*/
  13975. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  13976. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  13977. { /* evp_cipher test: EVP_aes_256_ecb*/
  13978. EVP_CIPHER_CTX ctx;
  13979. const byte msg[] =
  13980. {
  13981. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  13982. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  13983. };
  13984. const byte verify[] =
  13985. {
  13986. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  13987. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  13988. };
  13989. const byte key[] =
  13990. {
  13991. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  13992. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  13993. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  13994. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  13995. };
  13996. byte cipher[AES_BLOCK_SIZE * 4];
  13997. byte plain [AES_BLOCK_SIZE * 4];
  13998. EVP_CIPHER_CTX_init(&ctx);
  13999. if (EVP_CipherInit(&ctx, EVP_aes_256_ecb(), (unsigned char*)key,
  14000. NULL, 1) == 0)
  14001. return -8432;
  14002. if (EVP_Cipher(&ctx, cipher, (byte*)msg, 16) == 0)
  14003. return -8433;
  14004. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  14005. return -8434;
  14006. EVP_CIPHER_CTX_init(&ctx);
  14007. if (EVP_CipherInit(&ctx, EVP_aes_256_ecb(), (unsigned char*)key,
  14008. NULL, 0) == 0)
  14009. return -8435;
  14010. if (EVP_Cipher(&ctx, plain, cipher, 16) == 0)
  14011. return -8436;
  14012. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  14013. return -8437;
  14014. } /* end evp_cipher test */
  14015. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  14016. #define OPENSSL_TEST_ERROR (-10000)
  14017. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  14018. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  14019. {
  14020. /* Test: AES_encrypt/decrypt/set Key */
  14021. AES_KEY enc;
  14022. #ifdef HAVE_AES_DECRYPT
  14023. AES_KEY dec;
  14024. #endif
  14025. const byte msg[] =
  14026. {
  14027. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  14028. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  14029. };
  14030. const byte verify[] =
  14031. {
  14032. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  14033. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  14034. };
  14035. const byte key[] =
  14036. {
  14037. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  14038. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  14039. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  14040. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  14041. };
  14042. byte plain[sizeof(msg)];
  14043. byte cipher[sizeof(msg)];
  14044. printf("openSSL extra test\n") ;
  14045. AES_set_encrypt_key(key, sizeof(key)*8, &enc);
  14046. AES_set_decrypt_key(key, sizeof(key)*8, &dec);
  14047. AES_encrypt(msg, cipher, &enc);
  14048. #ifdef HAVE_AES_DECRYPT
  14049. AES_decrypt(cipher, plain, &dec);
  14050. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  14051. return OPENSSL_TEST_ERROR-60;
  14052. #endif /* HAVE_AES_DECRYPT */
  14053. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  14054. return OPENSSL_TEST_ERROR-61;
  14055. }
  14056. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  14057. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  14058. #ifdef WOLFSSL_AES_COUNTER
  14059. {
  14060. byte plainBuff [64];
  14061. byte cipherBuff[64];
  14062. #ifdef WOLFSSL_AES_128
  14063. const byte ctrKey[] =
  14064. {
  14065. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  14066. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  14067. };
  14068. const byte ctrIv[] =
  14069. {
  14070. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  14071. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  14072. };
  14073. const byte ctrPlain[] =
  14074. {
  14075. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  14076. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  14077. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  14078. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  14079. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  14080. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  14081. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  14082. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  14083. };
  14084. const byte ctrCipher[] =
  14085. {
  14086. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  14087. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  14088. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  14089. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  14090. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  14091. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  14092. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  14093. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  14094. };
  14095. const byte oddCipher[] =
  14096. {
  14097. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  14098. 0xc2
  14099. };
  14100. #endif /* WOLFSSL_AES_128 */
  14101. #ifdef WOLFSSL_AES_192
  14102. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  14103. * NIST Special Publication 800-38A */
  14104. const byte ctr192Key[] =
  14105. {
  14106. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  14107. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  14108. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  14109. };
  14110. const byte ctr192Iv[] =
  14111. {
  14112. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  14113. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  14114. };
  14115. const byte ctr192Plain[] =
  14116. {
  14117. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  14118. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  14119. };
  14120. const byte ctr192Cipher[] =
  14121. {
  14122. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  14123. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  14124. };
  14125. #endif /* WOLFSSL_AES_192 */
  14126. #ifdef WOLFSSL_AES_256
  14127. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  14128. * NIST Special Publication 800-38A */
  14129. const byte ctr256Key[] =
  14130. {
  14131. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  14132. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  14133. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  14134. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  14135. };
  14136. const byte ctr256Iv[] =
  14137. {
  14138. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  14139. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  14140. };
  14141. const byte ctr256Plain[] =
  14142. {
  14143. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  14144. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  14145. };
  14146. const byte ctr256Cipher[] =
  14147. {
  14148. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  14149. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  14150. };
  14151. #endif /* WOLFSSL_AES_256 */
  14152. EVP_CIPHER_CTX en;
  14153. EVP_CIPHER_CTX de;
  14154. #ifdef WOLFSSL_AES_128
  14155. EVP_CIPHER_CTX *p_en;
  14156. EVP_CIPHER_CTX *p_de;
  14157. EVP_CIPHER_CTX_init(&en);
  14158. if (EVP_CipherInit(&en, EVP_aes_128_ctr(),
  14159. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  14160. return -8438;
  14161. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctrPlain,
  14162. AES_BLOCK_SIZE*4) == 0)
  14163. return -8439;
  14164. EVP_CIPHER_CTX_init(&de);
  14165. if (EVP_CipherInit(&de, EVP_aes_128_ctr(),
  14166. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  14167. return -8440;
  14168. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff,
  14169. AES_BLOCK_SIZE*4) == 0)
  14170. return -8441;
  14171. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  14172. return -8442;
  14173. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  14174. return -8443;
  14175. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  14176. if(p_en == NULL)return -8444;
  14177. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  14178. if(p_de == NULL)return -8445;
  14179. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  14180. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  14181. return -8446;
  14182. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  14183. AES_BLOCK_SIZE*4) == 0)
  14184. return -8447;
  14185. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  14186. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  14187. return -8448;
  14188. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  14189. AES_BLOCK_SIZE*4) == 0)
  14190. return -8449;
  14191. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  14192. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  14193. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  14194. return -8450;
  14195. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  14196. return -8451;
  14197. EVP_CIPHER_CTX_init(&en);
  14198. if (EVP_CipherInit(&en, EVP_aes_128_ctr(),
  14199. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  14200. return -8452;
  14201. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctrPlain, 9) == 0)
  14202. return -8453;
  14203. EVP_CIPHER_CTX_init(&de);
  14204. if (EVP_CipherInit(&de, EVP_aes_128_ctr(),
  14205. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  14206. return -8454;
  14207. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff, 9) == 0)
  14208. return -8455;
  14209. if (XMEMCMP(plainBuff, ctrPlain, 9))
  14210. return -8456;
  14211. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  14212. return -8457;
  14213. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctrPlain, 9) == 0)
  14214. return -8458;
  14215. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff, 9) == 0)
  14216. return -8459;
  14217. if (XMEMCMP(plainBuff, ctrPlain, 9))
  14218. return -8460;
  14219. if (XMEMCMP(cipherBuff, oddCipher, 9))
  14220. return -8461;
  14221. #endif /* WOLFSSL_AES_128 */
  14222. #ifdef WOLFSSL_AES_192
  14223. EVP_CIPHER_CTX_init(&en);
  14224. if (EVP_CipherInit(&en, EVP_aes_192_ctr(),
  14225. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  14226. return -8462;
  14227. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctr192Plain,
  14228. AES_BLOCK_SIZE) == 0)
  14229. return -8463;
  14230. EVP_CIPHER_CTX_init(&de);
  14231. if (EVP_CipherInit(&de, EVP_aes_192_ctr(),
  14232. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  14233. return -8464;
  14234. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  14235. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff,
  14236. AES_BLOCK_SIZE) == 0)
  14237. return -8465;
  14238. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  14239. return -8466;
  14240. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  14241. return -8467;
  14242. #endif /* WOLFSSL_AES_192 */
  14243. #ifdef WOLFSSL_AES_256
  14244. EVP_CIPHER_CTX_init(&en);
  14245. if (EVP_CipherInit(&en, EVP_aes_256_ctr(),
  14246. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  14247. return -8468;
  14248. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctr256Plain,
  14249. AES_BLOCK_SIZE) == 0)
  14250. return -8469;
  14251. EVP_CIPHER_CTX_init(&de);
  14252. if (EVP_CipherInit(&de, EVP_aes_256_ctr(),
  14253. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  14254. return -8470;
  14255. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  14256. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff,
  14257. AES_BLOCK_SIZE) == 0)
  14258. return -8471;
  14259. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  14260. return -8472;
  14261. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  14262. return -8473;
  14263. #endif /* WOLFSSL_AES_256 */
  14264. }
  14265. #endif /* HAVE_AES_COUNTER */
  14266. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  14267. {
  14268. /* EVP_CipherUpdate test */
  14269. const byte cbcPlain[] =
  14270. {
  14271. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  14272. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  14273. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  14274. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  14275. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  14276. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  14277. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  14278. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  14279. };
  14280. byte key[] = "0123456789abcdef "; /* align */
  14281. byte iv[] = "1234567890abcdef "; /* align */
  14282. byte cipher[AES_BLOCK_SIZE * 4];
  14283. byte plain [AES_BLOCK_SIZE * 4];
  14284. EVP_CIPHER_CTX en;
  14285. EVP_CIPHER_CTX de;
  14286. int outlen ;
  14287. int total = 0;
  14288. EVP_CIPHER_CTX_init(&en);
  14289. if (EVP_CipherInit(&en, EVP_aes_128_cbc(),
  14290. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  14291. return -8474;
  14292. /* openSSL compatibility, if(inlen == 0)return 1; */
  14293. if (EVP_CipherUpdate(&en, (byte*)cipher, &outlen,
  14294. (byte*)cbcPlain, 0) != 1)
  14295. return -8475;
  14296. EVP_CIPHER_CTX_init(&en);
  14297. if (EVP_CipherInit(&en, EVP_aes_128_cbc(),
  14298. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  14299. return -8476;
  14300. if (EVP_CipherUpdate(&en, (byte*)cipher, &outlen,
  14301. (byte*)cbcPlain, 9) == 0)
  14302. return -8477;
  14303. if(outlen != 0)
  14304. return -8478;
  14305. total += outlen;
  14306. if (EVP_CipherUpdate(&en, (byte*)&cipher[total], &outlen,
  14307. (byte*)&cbcPlain[9] , 9) == 0)
  14308. return -8479;
  14309. if(outlen != 16)
  14310. return -8480;
  14311. total += outlen;
  14312. if (EVP_CipherFinal(&en, (byte*)&cipher[total], &outlen) == 0)
  14313. return -8481;
  14314. if(outlen != 16)
  14315. return -8482;
  14316. total += outlen;
  14317. if(total != 32)
  14318. return -8483;
  14319. total = 0;
  14320. EVP_CIPHER_CTX_init(&de);
  14321. if (EVP_CipherInit(&de, EVP_aes_128_cbc(),
  14322. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  14323. return -8484;
  14324. if (EVP_CipherUpdate(&de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  14325. return -8485;
  14326. if(outlen != 0)
  14327. return -8486;
  14328. total += outlen;
  14329. if (EVP_CipherUpdate(&de, (byte*)&plain[total], &outlen,
  14330. (byte*)&cipher[6], 12) == 0)
  14331. return -8487;
  14332. if(outlen != 0)
  14333. total += outlen;
  14334. if (EVP_CipherUpdate(&de, (byte*)&plain[total], &outlen,
  14335. (byte*)&cipher[6+12], 14) == 0)
  14336. return -8488;
  14337. if(outlen != 16)
  14338. return -8489;
  14339. total += outlen;
  14340. if (EVP_CipherFinal(&de, (byte*)&plain[total], &outlen) == 0)
  14341. return -8490;
  14342. if(outlen != 2)
  14343. return -8491;
  14344. total += outlen;
  14345. if(total != 18)
  14346. return -8492;
  14347. if (XMEMCMP(plain, cbcPlain, 18))
  14348. return -8493;
  14349. total = 0;
  14350. EVP_CIPHER_CTX_init(&en);
  14351. if (EVP_EncryptInit(&en, EVP_aes_128_cbc(),
  14352. (unsigned char*)key, (unsigned char*)iv) == 0)
  14353. return -8494;
  14354. if (EVP_CipherUpdate(&en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  14355. return -8495;
  14356. if(outlen != 0)
  14357. return -8496;
  14358. total += outlen;
  14359. if (EVP_CipherUpdate(&en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  14360. return -8497;
  14361. if(outlen != 16)
  14362. return -8498;
  14363. total += outlen;
  14364. if (EVP_EncryptFinal(&en, (byte*)&cipher[total], &outlen) == 0)
  14365. return -8499;
  14366. if(outlen != 16)
  14367. return -8500;
  14368. total += outlen;
  14369. if(total != 32)
  14370. return 3438;
  14371. total = 0;
  14372. EVP_CIPHER_CTX_init(&de);
  14373. if (EVP_DecryptInit(&de, EVP_aes_128_cbc(),
  14374. (unsigned char*)key, (unsigned char*)iv) == 0)
  14375. return -8501;
  14376. if (EVP_CipherUpdate(&de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  14377. return -8502;
  14378. if(outlen != 0)
  14379. return -8503;
  14380. total += outlen;
  14381. if (EVP_CipherUpdate(&de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  14382. return -8504;
  14383. if(outlen != 0)
  14384. total += outlen;
  14385. if (EVP_CipherUpdate(&de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  14386. return -8505;
  14387. if(outlen != 16)
  14388. return -8506;
  14389. total += outlen;
  14390. if (EVP_DecryptFinal(&de, (byte*)&plain[total], &outlen) == 0)
  14391. return -8507;
  14392. if(outlen != 2)
  14393. return -8508;
  14394. total += outlen;
  14395. if(total != 18)
  14396. return 3447;
  14397. if (XMEMCMP(plain, cbcPlain, 18))
  14398. return -8509;
  14399. if (EVP_CIPHER_key_length(NULL) != 0)
  14400. return -8510;
  14401. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  14402. return -8511;
  14403. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  14404. return -8512;
  14405. if (EVP_CIPHER_CTX_mode(&en) != (en.flags & WOLFSSL_EVP_CIPH_MODE))
  14406. return -8513;
  14407. EVP_CIPHER_CTX_init(&en);
  14408. if (EVP_CipherInit_ex(&en, EVP_aes_128_cbc(), NULL,
  14409. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  14410. return -8514;
  14411. EVP_CIPHER_CTX_init(&en);
  14412. if (EVP_EncryptInit_ex(&en, EVP_aes_128_cbc(), NULL,
  14413. (unsigned char*)key, (unsigned char*)iv) == 0)
  14414. return -8515;
  14415. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  14416. return -8516;
  14417. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  14418. return -8517;
  14419. EVP_CIPHER_CTX_init(&de);
  14420. if (EVP_DecryptInit_ex(&de, EVP_aes_128_cbc(), NULL,
  14421. (unsigned char*)key, (unsigned char*)iv) == 0)
  14422. return -8518;
  14423. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  14424. return -8519;
  14425. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  14426. return -8520;
  14427. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  14428. return -8521;
  14429. EVP_CIPHER_CTX_init(&en);
  14430. EVP_EncryptInit_ex(&en, EVP_aes_128_cbc(), NULL,
  14431. (unsigned char*)key, (unsigned char*)iv);
  14432. if (EVP_CIPHER_CTX_block_size(&en) != en.block_size)
  14433. return -8522;
  14434. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  14435. return -8523;
  14436. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  14437. return -8524;
  14438. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  14439. return -8525;
  14440. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  14441. return -8526;
  14442. EVP_CIPHER_CTX_clear_flags(&en, 0xFFFFFFFF);
  14443. EVP_CIPHER_CTX_set_flags(&en, 42);
  14444. if (en.flags != 42)
  14445. return -8527;
  14446. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  14447. return -8528;
  14448. if (EVP_CIPHER_CTX_set_padding(&en, 0) != WOLFSSL_SUCCESS)
  14449. return -8529;
  14450. if (EVP_CIPHER_CTX_set_padding(&en, 1) != WOLFSSL_SUCCESS)
  14451. return -8530;
  14452. }
  14453. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  14454. #endif /* ifndef NO_AES */
  14455. return 0;
  14456. }
  14457. int openSSL_evpMD_test(void)
  14458. {
  14459. int ret = 0;
  14460. #if !defined(NO_SHA256) && !defined(NO_SHA)
  14461. WOLFSSL_EVP_MD_CTX* ctx;
  14462. WOLFSSL_EVP_MD_CTX* ctx2;
  14463. ctx = EVP_MD_CTX_create();
  14464. ctx2 = EVP_MD_CTX_create();
  14465. ret = EVP_DigestInit(ctx, EVP_sha256());
  14466. if (ret != SSL_SUCCESS) {
  14467. ret = -8600;
  14468. goto openSSL_evpMD_test_done;
  14469. }
  14470. ret = EVP_MD_CTX_copy(ctx2, ctx);
  14471. if (ret != SSL_SUCCESS) {
  14472. ret = -8601;
  14473. goto openSSL_evpMD_test_done;
  14474. }
  14475. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  14476. ret = -8602;
  14477. goto openSSL_evpMD_test_done;
  14478. }
  14479. ret = EVP_DigestInit(ctx, EVP_sha1());
  14480. if (ret != SSL_SUCCESS) {
  14481. ret = -8603;
  14482. goto openSSL_evpMD_test_done;
  14483. }
  14484. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  14485. ret = -8604;
  14486. goto openSSL_evpMD_test_done;
  14487. }
  14488. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  14489. if (ret != SSL_SUCCESS) {
  14490. ret = -8605;
  14491. goto openSSL_evpMD_test_done;
  14492. }
  14493. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  14494. ret = -8606;
  14495. goto openSSL_evpMD_test_done;
  14496. }
  14497. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  14498. ret = -8607;
  14499. goto openSSL_evpMD_test_done;
  14500. }
  14501. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  14502. ret = -8608;
  14503. goto openSSL_evpMD_test_done;
  14504. }
  14505. if (EVP_add_digest(NULL) != 0) {
  14506. ret = -8609;
  14507. goto openSSL_evpMD_test_done;
  14508. }
  14509. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  14510. ret = -8610;
  14511. goto openSSL_evpMD_test_done;
  14512. }
  14513. ret = 0; /* got to success state without jumping to end with a fail */
  14514. openSSL_evpMD_test_done:
  14515. EVP_MD_CTX_destroy(ctx);
  14516. EVP_MD_CTX_destroy(ctx2);
  14517. #endif /* NO_SHA256 */
  14518. return ret;
  14519. }
  14520. #ifdef DEBUG_SIGN
  14521. static void show(const char *title, const char *p, unsigned int s) {
  14522. char* i;
  14523. printf("%s: ", title);
  14524. for (i = p;
  14525. i < p + s;
  14526. printf("%c", *i), i++);
  14527. printf("\n");
  14528. }
  14529. #else
  14530. #define show(a,b,c)
  14531. #endif
  14532. #define FOURK_BUFF 4096
  14533. #define ERR_BASE_PKEY -5000
  14534. int openssl_pkey0_test(void)
  14535. {
  14536. int ret = 0;
  14537. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(NO_SHA)
  14538. byte* prvTmp;
  14539. byte* pubTmp;
  14540. int prvBytes;
  14541. int pubBytes;
  14542. RSA *prvRsa = NULL;
  14543. RSA *pubRsa = NULL;
  14544. EVP_PKEY *prvPkey = NULL;
  14545. EVP_PKEY *pubPkey = NULL;
  14546. EVP_PKEY_CTX *enc = NULL;
  14547. EVP_PKEY_CTX *dec = NULL;
  14548. byte in[] = "Everyone gets Friday off.";
  14549. byte out[256];
  14550. size_t outlen;
  14551. size_t keySz;
  14552. byte plain[256];
  14553. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  14554. XFILE keyFile;
  14555. XFILE keypubFile;
  14556. char cliKey[] = "./certs/client-key.der";
  14557. char cliKeypub[] = "./certs/client-keyPub.der";
  14558. #endif
  14559. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14560. if (prvTmp == NULL)
  14561. return ERR_BASE_PKEY-1;
  14562. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14563. if (pubTmp == NULL) {
  14564. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  14565. return ERR_BASE_PKEY-2;
  14566. }
  14567. #ifdef USE_CERT_BUFFERS_1024
  14568. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  14569. prvBytes = sizeof_client_key_der_1024;
  14570. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  14571. pubBytes = sizeof_client_keypub_der_1024;
  14572. #elif defined(USE_CERT_BUFFERS_2048)
  14573. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  14574. prvBytes = sizeof_client_key_der_2048;
  14575. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  14576. pubBytes = sizeof_client_keypub_der_2048;
  14577. #else
  14578. keyFile = XFOPEN(cliKey, "rb");
  14579. if (!keyFile) {
  14580. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  14581. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  14582. err_sys("can't open ./certs/client-key.der, "
  14583. "Please run from wolfSSL home dir", ERR_BASE_PKEY-3);
  14584. return ERR_BASE_PKEY-3;
  14585. }
  14586. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  14587. XFCLOSE(keyFile);
  14588. keypubFile = XFOPEN(cliKeypub, "rb");
  14589. if (!keypubFile) {
  14590. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  14591. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  14592. err_sys("can't open ./certs/client-cert.der, "
  14593. "Please run from wolfSSL home dir", -4);
  14594. return ERR_BASE_PKEY-4;
  14595. }
  14596. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  14597. XFCLOSE(keypubFile);
  14598. #endif /* USE_CERT_BUFFERS */
  14599. prvRsa = wolfSSL_RSA_new();
  14600. pubRsa = wolfSSL_RSA_new();
  14601. if((prvRsa == NULL) || (pubRsa == NULL)){
  14602. printf("error with RSA_new\n");
  14603. ret = ERR_BASE_PKEY-10;
  14604. goto openssl_pkey0_test_done;
  14605. }
  14606. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  14607. if(ret != SSL_SUCCESS){
  14608. printf("error with RSA_LoadDer_ex\n");
  14609. ret = ERR_BASE_PKEY-11;
  14610. goto openssl_pkey0_test_done;
  14611. }
  14612. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  14613. if(ret != SSL_SUCCESS){
  14614. printf("error with RSA_LoadDer_ex\n");
  14615. ret = ERR_BASE_PKEY-12;
  14616. goto openssl_pkey0_test_done;
  14617. }
  14618. keySz = (size_t)RSA_size(pubRsa);
  14619. prvPkey = wolfSSL_EVP_PKEY_new();
  14620. pubPkey = wolfSSL_EVP_PKEY_new();
  14621. if((prvPkey == NULL) || (pubPkey == NULL)){
  14622. printf("error with PKEY_new\n");
  14623. ret = ERR_BASE_PKEY-13;
  14624. goto openssl_pkey0_test_done;
  14625. }
  14626. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  14627. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  14628. if(ret != 2){
  14629. printf("error with PKEY_set1_RSA\n");
  14630. ret = ERR_BASE_PKEY-14;
  14631. goto openssl_pkey0_test_done;
  14632. }
  14633. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  14634. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  14635. if((dec == NULL)||(enc==NULL)){
  14636. printf("error with EVP_PKEY_CTX_new\n");
  14637. ret = ERR_BASE_PKEY-15;
  14638. goto openssl_pkey0_test_done;
  14639. }
  14640. ret = EVP_PKEY_decrypt_init(dec);
  14641. if (ret != 1) {
  14642. printf("error with decrypt init\n");
  14643. ret = ERR_BASE_PKEY-16;
  14644. goto openssl_pkey0_test_done;
  14645. }
  14646. ret = EVP_PKEY_encrypt_init(enc);
  14647. if (ret != 1) {
  14648. printf("error with encrypt init\n");
  14649. ret = ERR_BASE_PKEY-17;
  14650. goto openssl_pkey0_test_done;
  14651. }
  14652. XMEMSET(out, 0, sizeof(out));
  14653. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  14654. if (ret != 1) {
  14655. printf("error encrypting msg\n");
  14656. ret = ERR_BASE_PKEY-18;
  14657. goto openssl_pkey0_test_done;
  14658. }
  14659. show("encrypted msg", out, outlen);
  14660. XMEMSET(plain, 0, sizeof(plain));
  14661. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  14662. if (ret != 1) {
  14663. printf("error decrypting msg\n");
  14664. ret = ERR_BASE_PKEY-19;
  14665. goto openssl_pkey0_test_done;
  14666. }
  14667. show("decrypted msg", plain, outlen);
  14668. /* RSA_PKCS1_OAEP_PADDING test */
  14669. ret = EVP_PKEY_decrypt_init(dec);
  14670. if (ret != 1) {
  14671. printf("error with decrypt init\n");
  14672. ret = ERR_BASE_PKEY-30;
  14673. goto openssl_pkey0_test_done;
  14674. }
  14675. ret = EVP_PKEY_encrypt_init(enc);
  14676. if (ret != 1) {
  14677. printf("error with encrypt init\n");
  14678. ret = ERR_BASE_PKEY-31;
  14679. goto openssl_pkey0_test_done;
  14680. }
  14681. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  14682. printf("first set rsa padding error\n");
  14683. ret = ERR_BASE_PKEY-32;
  14684. goto openssl_pkey0_test_done;
  14685. }
  14686. #ifndef HAVE_FIPS
  14687. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  14688. printf("second set rsa padding error\n");
  14689. ret = ERR_BASE_PKEY-33;
  14690. goto openssl_pkey0_test_done;
  14691. }
  14692. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  14693. printf("third set rsa padding error\n");
  14694. ret = ERR_BASE_PKEY-34;
  14695. goto openssl_pkey0_test_done;
  14696. }
  14697. #endif
  14698. XMEMSET(out, 0, sizeof(out));
  14699. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  14700. if (ret != 1) {
  14701. printf("error encrypting msg\n");
  14702. ret = ERR_BASE_PKEY-35;
  14703. goto openssl_pkey0_test_done;
  14704. }
  14705. show("encrypted msg", out, outlen);
  14706. XMEMSET(plain, 0, sizeof(plain));
  14707. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  14708. if (ret != 1) {
  14709. printf("error decrypting msg\n");
  14710. ret = ERR_BASE_PKEY-36;
  14711. goto openssl_pkey0_test_done;
  14712. }
  14713. show("decrypted msg", plain, outlen);
  14714. ret = 0; /* made it to this point without error then set success */
  14715. openssl_pkey0_test_done:
  14716. wolfSSL_RSA_free(prvRsa);
  14717. wolfSSL_RSA_free(pubRsa);
  14718. EVP_PKEY_free(pubPkey);
  14719. EVP_PKEY_free(prvPkey);
  14720. EVP_PKEY_CTX_free(dec);
  14721. EVP_PKEY_CTX_free(enc);
  14722. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14723. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14724. #endif /* NO_RSA */
  14725. return ret;
  14726. }
  14727. int openssl_pkey1_test(void)
  14728. {
  14729. int ret = 0;
  14730. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  14731. !defined(NO_SHA)
  14732. EVP_PKEY_CTX* dec = NULL;
  14733. EVP_PKEY_CTX* enc = NULL;
  14734. EVP_PKEY* pubKey = NULL;
  14735. EVP_PKEY* prvKey = NULL;
  14736. X509* x509;
  14737. const unsigned char msg[] = "sugar slapped";
  14738. const unsigned char* clikey;
  14739. unsigned char tmp[FOURK_BUF];
  14740. long cliKeySz;
  14741. unsigned char cipher[RSA_TEST_BYTES];
  14742. unsigned char plain[RSA_TEST_BYTES];
  14743. size_t outlen;
  14744. int keyLenBits = 2048;
  14745. #if defined(USE_CERT_BUFFERS_1024)
  14746. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  14747. cliKeySz = (long)sizeof_client_key_der_1024;
  14748. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  14749. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  14750. keyLenBits = 1024;
  14751. #elif defined(USE_CERT_BUFFERS_2048)
  14752. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  14753. cliKeySz = (long)sizeof_client_key_der_2048;
  14754. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  14755. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  14756. #elif defined(USE_CERT_BUFFERS_3072)
  14757. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  14758. cliKeySz = (long)sizeof_client_key_der_3072;
  14759. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  14760. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  14761. keyLenBits = 3072;
  14762. #elif defined(USE_CERT_BUFFERS_4096)
  14763. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  14764. cliKeySz = (long)sizeof_client_key_der_4096;
  14765. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  14766. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  14767. keyLenBits = 4096;
  14768. #else
  14769. XFILE f;
  14770. f = XFOPEN(clientKey, "rb");
  14771. if (!f) {
  14772. err_sys("can't open ./certs/client-key.der, "
  14773. "Please run from wolfSSL home dir", -41);
  14774. return -8800;
  14775. }
  14776. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  14777. XFCLOSE(f);
  14778. /* using existing wolfSSL api to get public and private key */
  14779. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  14780. #endif /* USE_CERT_BUFFERS */
  14781. clikey = tmp;
  14782. if ((prvKey = EVP_PKEY_new()) == NULL) {
  14783. return -8801;
  14784. }
  14785. EVP_PKEY_free(prvKey);
  14786. prvKey = NULL;
  14787. if (x509 == NULL) {
  14788. ret = -8802;
  14789. goto openssl_pkey1_test_done;
  14790. }
  14791. pubKey = X509_get_pubkey(x509);
  14792. if (pubKey == NULL) {
  14793. ret = -8803;
  14794. goto openssl_pkey1_test_done;
  14795. }
  14796. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  14797. if (prvKey == NULL) {
  14798. ret = -8804;
  14799. goto openssl_pkey1_test_done;
  14800. }
  14801. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  14802. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  14803. ret = -8805;
  14804. goto openssl_pkey1_test_done;
  14805. }
  14806. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  14807. ret = -8806;
  14808. goto openssl_pkey1_test_done;
  14809. }
  14810. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  14811. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  14812. if (dec == NULL || enc == NULL) {
  14813. ret = -8807;
  14814. goto openssl_pkey1_test_done;
  14815. }
  14816. if (EVP_PKEY_decrypt_init(dec) != 1) {
  14817. ret = -8808;
  14818. goto openssl_pkey1_test_done;
  14819. }
  14820. if (EVP_PKEY_encrypt_init(enc) != 1) {
  14821. ret = -8809;
  14822. goto openssl_pkey1_test_done;
  14823. }
  14824. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  14825. ret = -8810;
  14826. goto openssl_pkey1_test_done;
  14827. }
  14828. #ifndef HAVE_FIPS
  14829. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  14830. ret = -8811;
  14831. goto openssl_pkey1_test_done;
  14832. }
  14833. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  14834. ret = -8812;
  14835. goto openssl_pkey1_test_done;
  14836. }
  14837. #endif
  14838. XMEMSET(cipher, 0, sizeof(cipher));
  14839. outlen = keyLenBits/8;
  14840. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  14841. ret = -8813;
  14842. goto openssl_pkey1_test_done;
  14843. }
  14844. XMEMSET(plain, 0, sizeof(plain));
  14845. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  14846. ret = -8814;
  14847. goto openssl_pkey1_test_done;
  14848. }
  14849. openssl_pkey1_test_done:
  14850. if (pubKey != NULL) {
  14851. EVP_PKEY_free(pubKey);
  14852. }
  14853. if (prvKey != NULL) {
  14854. EVP_PKEY_free(prvKey);
  14855. }
  14856. if (dec != NULL) {
  14857. EVP_PKEY_CTX_free(dec);
  14858. }
  14859. if (enc != NULL) {
  14860. EVP_PKEY_CTX_free(enc);
  14861. }
  14862. if (x509 != NULL) {
  14863. X509_free(x509);
  14864. }
  14865. #endif
  14866. return ret;
  14867. }
  14868. #define ERR_BASE_EVPSIG -5100
  14869. int openssl_evpSig_test(void)
  14870. {
  14871. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(HAVE_USER_RSA)
  14872. byte* prvTmp;
  14873. byte* pubTmp;
  14874. int prvBytes;
  14875. int pubBytes;
  14876. RSA *prvRsa;
  14877. RSA *pubRsa;
  14878. EVP_PKEY *prvPkey;
  14879. EVP_PKEY *pubPkey;
  14880. EVP_MD_CTX* sign;
  14881. EVP_MD_CTX* verf;
  14882. char msg[] = "see spot run";
  14883. unsigned char sig[256];
  14884. unsigned int sigSz;
  14885. const void* pt;
  14886. unsigned int count;
  14887. int ret, ret1, ret2;
  14888. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  14889. XFILE keyFile;
  14890. XFILE keypubFile;
  14891. char cliKey[] = "./certs/client-key.der";
  14892. char cliKeypub[] = "./certs/client-keyPub.der";
  14893. #endif
  14894. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14895. if (prvTmp == NULL)
  14896. return ERR_BASE_EVPSIG-1;
  14897. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14898. if (pubTmp == NULL) {
  14899. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14900. return ERR_BASE_EVPSIG-2;
  14901. }
  14902. #ifdef USE_CERT_BUFFERS_1024
  14903. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  14904. prvBytes = sizeof_client_key_der_1024;
  14905. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  14906. pubBytes = sizeof_client_keypub_der_1024;
  14907. #elif defined(USE_CERT_BUFFERS_2048)
  14908. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  14909. prvBytes = sizeof_client_key_der_2048;
  14910. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  14911. pubBytes = sizeof_client_keypub_der_2048;
  14912. #else
  14913. keyFile = XFOPEN(cliKey, "rb");
  14914. if (!keyFile) {
  14915. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  14916. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  14917. err_sys("can't open ./certs/client-key.der, "
  14918. "Please run from wolfSSL home dir", -40);
  14919. return ERR_BASE_EVPSIG-3;
  14920. }
  14921. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  14922. XFCLOSE(keyFile);
  14923. keypubFile = XFOPEN(cliKeypub, "rb");
  14924. if (!keypubFile) {
  14925. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  14926. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  14927. err_sys("can't open ./certs/client-cert.der, "
  14928. "Please run from wolfSSL home dir", -41);
  14929. return ERR_BASE_EVPSIG-4;
  14930. }
  14931. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  14932. XFCLOSE(keypubFile);
  14933. #endif /* USE_CERT_BUFFERS */
  14934. prvRsa = wolfSSL_RSA_new();
  14935. pubRsa = wolfSSL_RSA_new();
  14936. if((prvRsa == NULL) || (pubRsa == NULL)){
  14937. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  14938. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  14939. err_sys("ERROR with RSA_new", -8900);
  14940. return ERR_BASE_EVPSIG-5;
  14941. }
  14942. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  14943. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  14944. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  14945. printf("error with RSA_LoadDer_ex\n");
  14946. return ERR_BASE_EVPSIG-6;
  14947. }
  14948. prvPkey = wolfSSL_EVP_PKEY_new();
  14949. pubPkey = wolfSSL_EVP_PKEY_new();
  14950. if((prvPkey == NULL) || (pubPkey == NULL)){
  14951. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  14952. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  14953. printf("error with KEY_new\n");
  14954. return ERR_BASE_EVPSIG-7;
  14955. }
  14956. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  14957. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  14958. if((ret1 != 1) || (ret2 != 1)){
  14959. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  14960. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  14961. printf("error with EVP_PKEY_set1_RSA\n");
  14962. return ERR_BASE_EVPSIG-8;
  14963. }
  14964. /****************** sign and verify *******************/
  14965. sign = EVP_MD_CTX_create();
  14966. verf = EVP_MD_CTX_create();
  14967. if((sign == NULL)||(verf == NULL)){
  14968. printf("error with EVP_MD_CTX_create\n");
  14969. EVP_MD_CTX_destroy(sign);
  14970. EVP_MD_CTX_destroy(verf);
  14971. return ERR_BASE_EVPSIG-10;
  14972. }
  14973. ret = EVP_SignInit(sign, EVP_sha1());
  14974. if (ret != SSL_SUCCESS){
  14975. printf("error with EVP_SignInit\n");
  14976. EVP_MD_CTX_destroy(sign);
  14977. EVP_MD_CTX_destroy(verf);
  14978. return ERR_BASE_EVPSIG-11;
  14979. }
  14980. count = sizeof(msg);
  14981. show("message = ", (char *)msg, count);
  14982. /* sign */
  14983. XMEMSET(sig, 0, sizeof(sig));
  14984. pt = (const void*)msg;
  14985. ret1 = EVP_SignUpdate(sign, pt, count);
  14986. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  14987. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  14988. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  14989. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  14990. EVP_MD_CTX_destroy(sign);
  14991. EVP_MD_CTX_destroy(verf);
  14992. printf("error with EVP_MD_CTX_create\n");
  14993. return ERR_BASE_EVPSIG-12;
  14994. }
  14995. show("signature = ", (char *)sig, sigSz);
  14996. /* verify */
  14997. pt = (const void*)msg;
  14998. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  14999. ret2 = EVP_VerifyUpdate(verf, pt, count);
  15000. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  15001. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  15002. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  15003. EVP_MD_CTX_destroy(sign);
  15004. EVP_MD_CTX_destroy(verf);
  15005. printf("error with EVP_Verify\n");
  15006. return ERR_BASE_EVPSIG-13;
  15007. }
  15008. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  15009. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  15010. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  15011. EVP_MD_CTX_destroy(sign);
  15012. EVP_MD_CTX_destroy(verf);
  15013. printf("error with EVP_VerifyFinal\n");
  15014. return ERR_BASE_EVPSIG-14;
  15015. }
  15016. /* expect fail without update */
  15017. EVP_VerifyInit(verf, EVP_sha1());
  15018. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  15019. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  15020. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  15021. EVP_MD_CTX_destroy(sign);
  15022. EVP_MD_CTX_destroy(verf);
  15023. printf("EVP_VerifyInit without update not detected\n");
  15024. return ERR_BASE_EVPSIG-15;
  15025. }
  15026. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  15027. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  15028. EVP_MD_CTX_destroy(sign);
  15029. EVP_MD_CTX_destroy(verf);
  15030. wolfSSL_RSA_free(prvRsa);
  15031. wolfSSL_RSA_free(pubRsa);
  15032. EVP_PKEY_free(pubPkey);
  15033. EVP_PKEY_free(prvPkey);
  15034. #endif /* NO_RSA */
  15035. return 0;
  15036. }
  15037. #endif /* OPENSSL_EXTRA */
  15038. #ifndef NO_PWDBASED
  15039. #ifdef HAVE_SCRYPT
  15040. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  15041. int scrypt_test(void)
  15042. {
  15043. int ret;
  15044. byte derived[64];
  15045. const byte verify1[] = {
  15046. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  15047. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  15048. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  15049. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  15050. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  15051. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  15052. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  15053. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  15054. };
  15055. const byte verify2[] = {
  15056. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  15057. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  15058. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  15059. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  15060. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  15061. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  15062. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  15063. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  15064. };
  15065. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_INTEL_QA)
  15066. const byte verify3[] = {
  15067. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  15068. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  15069. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  15070. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  15071. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  15072. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  15073. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  15074. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  15075. };
  15076. #endif
  15077. #ifdef SCRYPT_TEST_ALL
  15078. /* Test case is very slow.
  15079. * Use for confirmation after code change or new platform.
  15080. */
  15081. const byte verify4[] = {
  15082. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  15083. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  15084. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  15085. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  15086. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  15087. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  15088. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  15089. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  15090. };
  15091. #endif
  15092. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  15093. if (ret != 0)
  15094. return -9000;
  15095. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  15096. return -9001;
  15097. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  15098. sizeof(verify2));
  15099. if (ret != 0)
  15100. return -9002;
  15101. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  15102. return -9003;
  15103. /* Don't run these test on embedded, since they use large mallocs */
  15104. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_INTEL_QA)
  15105. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  15106. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  15107. if (ret != 0)
  15108. return -9004;
  15109. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  15110. return -9005;
  15111. #ifdef SCRYPT_TEST_ALL
  15112. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  15113. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  15114. if (ret != 0)
  15115. return -9006;
  15116. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  15117. return -9007;
  15118. #endif
  15119. #endif /* !BENCH_EMBEDDED && !HAVE_INTEL_QA */
  15120. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  15121. 8, 16, sizeof(verify2));
  15122. if (ret != 0)
  15123. return -9008;
  15124. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  15125. return -9009;
  15126. return 0;
  15127. }
  15128. #endif
  15129. #ifdef HAVE_PKCS12
  15130. int pkcs12_test(void)
  15131. {
  15132. const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  15133. 0x00, 0x00 };
  15134. const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  15135. const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  15136. 0x00, 0x67, 0x00, 0x00 };
  15137. const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  15138. byte derived[64];
  15139. const byte verify[] = {
  15140. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  15141. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  15142. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  15143. };
  15144. const byte verify2[] = {
  15145. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  15146. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  15147. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  15148. };
  15149. int id = 1;
  15150. int kLen = 24;
  15151. int iterations = 1;
  15152. int ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  15153. iterations, kLen, WC_SHA256, id);
  15154. if (ret < 0)
  15155. return -9100;
  15156. if ( (ret = XMEMCMP(derived, verify, kLen)) != 0)
  15157. return -9101;
  15158. iterations = 1000;
  15159. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  15160. iterations, kLen, WC_SHA256, id);
  15161. if (ret < 0)
  15162. return -9102;
  15163. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  15164. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  15165. if (ret < 0)
  15166. return -9103;
  15167. if ( (ret = XMEMCMP(derived, verify2, 24)) != 0)
  15168. return -9104;
  15169. return 0;
  15170. }
  15171. #endif /* HAVE_PKCS12 */
  15172. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  15173. int pbkdf2_test(void)
  15174. {
  15175. char passwd[] = "passwordpassword";
  15176. const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  15177. int iterations = 2048;
  15178. int kLen = 24;
  15179. byte derived[64];
  15180. const byte verify[] = {
  15181. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  15182. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  15183. };
  15184. int ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt,
  15185. (int)sizeof(salt), iterations, kLen, WC_SHA256, HEAP_HINT, devId);
  15186. if (ret != 0)
  15187. return ret;
  15188. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  15189. return -9200;
  15190. return 0;
  15191. }
  15192. #endif /* HAVE_PBKDF2 && !NO_SHA256 */
  15193. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  15194. int pbkdf1_test(void)
  15195. {
  15196. char passwd[] = "password";
  15197. const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  15198. int iterations = 1000;
  15199. int kLen = 16;
  15200. byte derived[16];
  15201. const byte verify[] = {
  15202. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  15203. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  15204. };
  15205. int ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  15206. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  15207. HEAP_HINT);
  15208. if (ret != 0)
  15209. return ret;
  15210. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  15211. return -9300;
  15212. return 0;
  15213. }
  15214. #endif /* HAVE_PBKDF2 && !NO_SHA */
  15215. int pwdbased_test(void)
  15216. {
  15217. int ret = 0;
  15218. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  15219. ret = pbkdf1_test();
  15220. if (ret != 0)
  15221. return ret;
  15222. #endif
  15223. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  15224. ret = pbkdf2_test();
  15225. if (ret != 0)
  15226. return ret;
  15227. #endif
  15228. #ifdef HAVE_PKCS12
  15229. ret = pkcs12_test();
  15230. if (ret != 0)
  15231. return ret;
  15232. #endif
  15233. #ifdef HAVE_SCRYPT
  15234. ret = scrypt_test();
  15235. if (ret != 0)
  15236. return ret;
  15237. #endif
  15238. return ret;
  15239. }
  15240. #endif /* NO_PWDBASED */
  15241. #if defined(HAVE_HKDF) && (!defined(NO_SHA) || !defined(NO_SHA256))
  15242. int hkdf_test(void)
  15243. {
  15244. int ret;
  15245. int L = 42;
  15246. byte okm1[42];
  15247. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  15248. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  15249. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  15250. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15251. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  15252. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  15253. 0xf8, 0xf9 };
  15254. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  15255. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  15256. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  15257. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  15258. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  15259. 0x49, 0x18 };
  15260. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  15261. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  15262. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  15263. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  15264. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  15265. 0xf8, 0x96 };
  15266. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  15267. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  15268. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  15269. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  15270. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  15271. 0x96, 0xc8 };
  15272. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  15273. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  15274. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  15275. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  15276. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  15277. 0x58, 0x65 };
  15278. (void)res1;
  15279. (void)res2;
  15280. (void)res3;
  15281. (void)res4;
  15282. (void)salt1;
  15283. (void)info1;
  15284. #ifndef NO_SHA
  15285. ret = wc_HKDF(WC_SHA, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
  15286. if (ret != 0)
  15287. return -9500;
  15288. if (XMEMCMP(okm1, res1, L) != 0)
  15289. return -9501;
  15290. #ifndef HAVE_FIPS
  15291. /* fips can't have key size under 14 bytes, salt is key too */
  15292. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, 13, info1, 10, okm1, L);
  15293. if (ret != 0)
  15294. return -9502;
  15295. if (XMEMCMP(okm1, res2, L) != 0)
  15296. return -9503;
  15297. #endif /* HAVE_FIPS */
  15298. #endif /* NO_SHA */
  15299. #ifndef NO_SHA256
  15300. ret = wc_HKDF(WC_SHA256, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
  15301. if (ret != 0)
  15302. return -9504;
  15303. if (XMEMCMP(okm1, res3, L) != 0)
  15304. return -9505;
  15305. #ifndef HAVE_FIPS
  15306. /* fips can't have key size under 14 bytes, salt is key too */
  15307. ret = wc_HKDF(WC_SHA256, ikm1, 22, salt1, 13, info1, 10, okm1, L);
  15308. if (ret != 0)
  15309. return -9506;
  15310. if (XMEMCMP(okm1, res4, L) != 0)
  15311. return -9507;
  15312. #endif /* HAVE_FIPS */
  15313. #endif /* NO_SHA256 */
  15314. return 0;
  15315. }
  15316. #endif /* HAVE_HKDF */
  15317. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  15318. int x963kdf_test(void)
  15319. {
  15320. int ret;
  15321. byte kek[128];
  15322. #ifndef NO_SHA
  15323. /* SHA-1, COUNT = 0
  15324. * shared secret length: 192
  15325. * SharedInfo length: 0
  15326. * key data length: 128
  15327. */
  15328. const byte Z[] = {
  15329. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  15330. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  15331. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  15332. };
  15333. const byte verify[] = {
  15334. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  15335. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  15336. };
  15337. #endif
  15338. #ifndef NO_SHA256
  15339. /* SHA-256, COUNT = 3
  15340. * shared secret length: 192
  15341. * SharedInfo length: 0
  15342. * key data length: 128
  15343. */
  15344. const byte Z2[] = {
  15345. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  15346. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  15347. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  15348. };
  15349. const byte verify2[] = {
  15350. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  15351. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  15352. };
  15353. #endif
  15354. #ifdef WOLFSSL_SHA512
  15355. /* SHA-512, COUNT = 0
  15356. * shared secret length: 192
  15357. * SharedInfo length: 0
  15358. * key data length: 128
  15359. */
  15360. const byte Z3[] = {
  15361. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  15362. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  15363. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  15364. };
  15365. const byte verify3[] = {
  15366. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  15367. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  15368. };
  15369. /* SHA-512, COUNT = 0
  15370. * shared secret length: 521
  15371. * SharedInfo length: 128
  15372. * key data length: 1024
  15373. */
  15374. const byte Z4[] = {
  15375. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  15376. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  15377. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  15378. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  15379. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  15380. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  15381. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  15382. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  15383. 0x5a, 0x2d
  15384. };
  15385. const byte info4[] = {
  15386. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  15387. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  15388. };
  15389. const byte verify4[] = {
  15390. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  15391. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  15392. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  15393. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  15394. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  15395. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  15396. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  15397. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  15398. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  15399. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  15400. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  15401. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  15402. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  15403. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  15404. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  15405. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  15406. };
  15407. #endif
  15408. #ifndef NO_SHA
  15409. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  15410. kek, sizeof(verify));
  15411. if (ret != 0)
  15412. return -9600;
  15413. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  15414. return -9601;
  15415. #endif
  15416. #ifndef NO_SHA256
  15417. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  15418. kek, sizeof(verify2));
  15419. if (ret != 0)
  15420. return -9602;
  15421. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  15422. return -9603;
  15423. #endif
  15424. #ifdef WOLFSSL_SHA512
  15425. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  15426. kek, sizeof(verify3));
  15427. if (ret != 0)
  15428. return -9604;
  15429. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  15430. return -9605;
  15431. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  15432. sizeof(info4), kek, sizeof(verify4));
  15433. if (ret != 0)
  15434. return -9606;
  15435. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  15436. return -9607;
  15437. #endif
  15438. return 0;
  15439. }
  15440. #endif /* HAVE_X963_KDF */
  15441. #ifdef HAVE_ECC
  15442. /* size to use for ECC key gen tests */
  15443. #ifndef ECC_KEYGEN_SIZE
  15444. #ifndef NO_ECC256
  15445. #define ECC_KEYGEN_SIZE 32
  15446. #elif defined(HAVE_ECC384)
  15447. #define ECC_KEYGEN_SIZE 48
  15448. #elif defined(HAVE_ECC224)
  15449. #define ECC_KEYGEN_SIZE 28
  15450. #else
  15451. #error No ECC keygen size defined for test
  15452. #endif
  15453. #endif
  15454. #ifdef BENCH_EMBEDDED
  15455. #define ECC_SHARED_SIZE 128
  15456. #else
  15457. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  15458. #endif
  15459. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  15460. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  15461. #ifndef NO_ECC_VECTOR_TEST
  15462. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  15463. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  15464. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  15465. #define HAVE_ECC_VECTOR_TEST
  15466. #endif
  15467. #endif
  15468. #ifdef HAVE_ECC_VECTOR_TEST
  15469. typedef struct eccVector {
  15470. const char* msg; /* SHA-1 Encoded Message */
  15471. const char* Qx;
  15472. const char* Qy;
  15473. const char* d; /* Private Key */
  15474. const char* R;
  15475. const char* S;
  15476. const char* curveName;
  15477. word32 msgLen;
  15478. word32 keySize;
  15479. #ifndef NO_ASN
  15480. const byte* r;
  15481. word32 rSz;
  15482. const byte* s;
  15483. word32 sSz;
  15484. #endif
  15485. } eccVector;
  15486. static int ecc_test_vector_item(const eccVector* vector)
  15487. {
  15488. int ret = 0, verify = 0;
  15489. word32 sigSz;
  15490. ecc_key userA;
  15491. DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  15492. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  15493. word32 sigRawSz;
  15494. DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  15495. #endif
  15496. ret = wc_ecc_init_ex(&userA, HEAP_HINT, devId);
  15497. if (ret != 0) {
  15498. FREE_VAR(sig, HEAP_HINT);
  15499. return ret;
  15500. }
  15501. ret = wc_ecc_import_raw(&userA, vector->Qx, vector->Qy,
  15502. vector->d, vector->curveName);
  15503. if (ret != 0)
  15504. goto done;
  15505. XMEMSET(sig, 0, ECC_SIG_SIZE);
  15506. sigSz = ECC_SIG_SIZE;
  15507. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  15508. if (ret != 0)
  15509. goto done;
  15510. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  15511. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  15512. sigRawSz = ECC_SIG_SIZE;
  15513. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  15514. sigRaw, &sigRawSz);
  15515. if (ret != 0)
  15516. goto done;
  15517. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  15518. ret = -9608;
  15519. goto done;
  15520. }
  15521. #endif
  15522. do {
  15523. #if defined(WOLFSSL_ASYNC_CRYPT)
  15524. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15525. #endif
  15526. if (ret == 0)
  15527. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  15528. vector->msgLen, &verify, &userA);
  15529. } while (ret == WC_PENDING_E);
  15530. if (ret != 0)
  15531. goto done;
  15532. TEST_SLEEP();
  15533. if (verify != 1)
  15534. ret = -9609;
  15535. done:
  15536. wc_ecc_free(&userA);
  15537. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  15538. FREE_VAR(sigRaw, HEAP_HINT);
  15539. #endif
  15540. FREE_VAR(sig, HEAP_HINT);
  15541. return ret;
  15542. }
  15543. static int ecc_test_vector(int keySize)
  15544. {
  15545. int ret;
  15546. eccVector vec;
  15547. XMEMSET(&vec, 0, sizeof(vec));
  15548. vec.keySize = (word32)keySize;
  15549. switch(keySize) {
  15550. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  15551. case 14:
  15552. return 0;
  15553. #endif /* HAVE_ECC112 */
  15554. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  15555. case 16:
  15556. return 0;
  15557. #endif /* HAVE_ECC128 */
  15558. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  15559. case 20:
  15560. return 0;
  15561. #endif /* HAVE_ECC160 */
  15562. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  15563. case 24:
  15564. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  15565. #if 1
  15566. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  15567. vec.msgLen = 20;
  15568. #else
  15569. /* This is the raw message prior to SHA-1 */
  15570. vec.msg =
  15571. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  15572. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  15573. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  15574. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  15575. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  15576. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  15577. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  15578. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  15579. vec.msgLen = 128;
  15580. #endif
  15581. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  15582. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  15583. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  15584. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  15585. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  15586. vec.curveName = "SECP192R1";
  15587. #ifndef NO_ASN
  15588. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  15589. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  15590. vec.rSz = 24;
  15591. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  15592. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  15593. vec.sSz = 24;
  15594. #endif
  15595. break;
  15596. #endif /* HAVE_ECC192 */
  15597. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  15598. case 28:
  15599. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  15600. #if 1
  15601. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  15602. vec.msgLen = 20;
  15603. #else
  15604. /* This is the raw message prior to SHA-1 */
  15605. vec.msg =
  15606. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  15607. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  15608. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  15609. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  15610. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  15611. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  15612. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  15613. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  15614. vec.msgLen = 128;
  15615. #endif
  15616. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  15617. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  15618. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  15619. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  15620. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  15621. vec.curveName = "SECP224R1";
  15622. #ifndef NO_ASN
  15623. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  15624. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  15625. "\xbc\x16\x71\xa7";
  15626. vec.rSz = 28;
  15627. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  15628. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  15629. "\x6a\xf3\xad\x5b";
  15630. vec.sSz = 28;
  15631. #endif
  15632. break;
  15633. #endif /* HAVE_ECC224 */
  15634. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  15635. case 30:
  15636. return 0;
  15637. #endif /* HAVE_ECC239 */
  15638. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  15639. case 32:
  15640. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  15641. #if 1
  15642. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  15643. vec.msgLen = 20;
  15644. #else
  15645. /* This is the raw message prior to SHA-1 */
  15646. vec.msg =
  15647. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  15648. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  15649. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  15650. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  15651. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  15652. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  15653. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  15654. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  15655. vec.msgLen = 128;
  15656. #endif
  15657. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  15658. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  15659. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  15660. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  15661. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  15662. #ifndef NO_ASN
  15663. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  15664. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  15665. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  15666. vec.rSz = 32;
  15667. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  15668. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  15669. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  15670. vec.sSz = 32;
  15671. #endif
  15672. vec.curveName = "SECP256R1";
  15673. break;
  15674. #endif /* !NO_ECC256 */
  15675. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  15676. case 40:
  15677. return 0;
  15678. #endif /* HAVE_ECC320 */
  15679. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  15680. case 48:
  15681. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  15682. #if 1
  15683. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  15684. vec.msgLen = 20;
  15685. #else
  15686. /* This is the raw message prior to SHA-1 */
  15687. vec.msg =
  15688. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  15689. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  15690. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  15691. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  15692. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  15693. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  15694. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  15695. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  15696. vec.msgLen = 128;
  15697. #endif
  15698. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  15699. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  15700. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  15701. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  15702. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  15703. vec.curveName = "SECP384R1";
  15704. #ifndef NO_ASN
  15705. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  15706. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  15707. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  15708. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  15709. vec.rSz = 48;
  15710. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  15711. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  15712. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  15713. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  15714. vec.sSz = 48;
  15715. #endif
  15716. break;
  15717. #endif /* HAVE_ECC384 */
  15718. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  15719. case 64:
  15720. return 0;
  15721. #endif /* HAVE_ECC512 */
  15722. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  15723. case 66:
  15724. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  15725. #if 1
  15726. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  15727. vec.msgLen = 20;
  15728. #else
  15729. /* This is the raw message prior to SHA-1 */
  15730. vec.msg =
  15731. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  15732. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  15733. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  15734. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  15735. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  15736. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  15737. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  15738. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  15739. vec.msgLen = 128;
  15740. #endif
  15741. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  15742. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  15743. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  15744. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  15745. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  15746. vec.curveName = "SECP521R1";
  15747. #ifndef NO_ASN
  15748. vec.r = (byte*)"\x00\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77"
  15749. "\x78\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf"
  15750. "\x5b\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c"
  15751. "\x7f\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac"
  15752. "\xb6\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba"
  15753. "\x0a\xa3\xbb\x15\x21\xbe";
  15754. vec.rSz = 66;
  15755. vec.s = (byte*)"\x00\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3"
  15756. "\x23\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd"
  15757. "\xc5\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13"
  15758. "\x4b\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79"
  15759. "\x11\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b"
  15760. "\x6c\x3d\x22\xf2\x48\x0c";
  15761. vec.sSz = 66;
  15762. #endif
  15763. break;
  15764. #endif /* HAVE_ECC521 */
  15765. default:
  15766. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  15767. }; /* Switch */
  15768. ret = ecc_test_vector_item(&vec);
  15769. if (ret < 0) {
  15770. return ret;
  15771. }
  15772. return 0;
  15773. }
  15774. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K)
  15775. static int ecc_test_sign_vectors(WC_RNG* rng)
  15776. {
  15777. int ret;
  15778. ecc_key key;
  15779. byte sig[72];
  15780. word32 sigSz;
  15781. unsigned char hash[32] = "test wolfSSL deterministic sign";
  15782. const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  15783. const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  15784. const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  15785. const byte k[1] = { 0x02 };
  15786. const byte expSig[71] = {
  15787. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  15788. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  15789. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  15790. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  15791. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  15792. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  15793. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  15794. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  15795. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  15796. };
  15797. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  15798. if (ret != 0) {
  15799. return ret;
  15800. }
  15801. ret = wc_ecc_import_raw(&key, QIUTx, QIUTy, dIUT, "SECP256R1");
  15802. if (ret != 0) {
  15803. goto done;
  15804. }
  15805. ret = wc_ecc_sign_set_k(k, sizeof(k), &key);
  15806. if (ret != 0) {
  15807. goto done;
  15808. }
  15809. sigSz = sizeof(sig);
  15810. do {
  15811. #if defined(WOLFSSL_ASYNC_CRYPT)
  15812. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15813. #endif
  15814. if (ret == 0)
  15815. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, &key);
  15816. } while (ret == WC_PENDING_E);
  15817. if (ret != 0) {
  15818. goto done;
  15819. }
  15820. TEST_SLEEP();
  15821. if (sigSz != sizeof(expSig)) {
  15822. ret = -9610;
  15823. goto done;
  15824. }
  15825. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  15826. ret = -9611;
  15827. goto done;
  15828. }
  15829. sigSz = sizeof(sig);
  15830. do {
  15831. #if defined(WOLFSSL_ASYNC_CRYPT)
  15832. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15833. #endif
  15834. if (ret == 0)
  15835. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, &key);
  15836. } while (ret == WC_PENDING_E);
  15837. if (ret != 0) {
  15838. goto done;
  15839. }
  15840. TEST_SLEEP();
  15841. done:
  15842. wc_ecc_free(&key);
  15843. return ret;
  15844. }
  15845. #endif
  15846. #ifdef HAVE_ECC_CDH
  15847. static int ecc_test_cdh_vectors(WC_RNG* rng)
  15848. {
  15849. int ret;
  15850. ecc_key pub_key, priv_key;
  15851. byte sharedA[32] = {0}, sharedB[32] = {0};
  15852. word32 x, z;
  15853. const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  15854. const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  15855. const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  15856. const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  15857. const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  15858. const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  15859. /* setup private and public keys */
  15860. ret = wc_ecc_init_ex(&pub_key, HEAP_HINT, devId);
  15861. if (ret != 0)
  15862. return ret;
  15863. ret = wc_ecc_init_ex(&priv_key, HEAP_HINT, devId);
  15864. if (ret != 0) {
  15865. wc_ecc_free(&pub_key);
  15866. return ret;
  15867. }
  15868. wc_ecc_set_flags(&pub_key, WC_ECC_FLAG_COFACTOR);
  15869. wc_ecc_set_flags(&priv_key, WC_ECC_FLAG_COFACTOR);
  15870. ret = wc_ecc_import_raw(&pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  15871. if (ret != 0)
  15872. goto done;
  15873. ret = wc_ecc_import_raw(&priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  15874. if (ret != 0)
  15875. goto done;
  15876. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  15877. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) && \
  15878. !defined(HAVE_SELFTEST)
  15879. ret = wc_ecc_set_rng(&priv_key, rng);
  15880. if (ret != 0)
  15881. goto done;
  15882. #else
  15883. (void)rng;
  15884. #endif
  15885. /* compute ECC Cofactor shared secret */
  15886. x = sizeof(sharedA);
  15887. do {
  15888. #if defined(WOLFSSL_ASYNC_CRYPT)
  15889. ret = wc_AsyncWait(ret, &priv_key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15890. #endif
  15891. if (ret == 0)
  15892. ret = wc_ecc_shared_secret(&priv_key, &pub_key, sharedA, &x);
  15893. } while (ret == WC_PENDING_E);
  15894. if (ret != 0) {
  15895. goto done;
  15896. }
  15897. TEST_SLEEP();
  15898. /* read in expected Z */
  15899. z = sizeof(sharedB);
  15900. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  15901. if (ret != 0)
  15902. goto done;
  15903. /* compare results */
  15904. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  15905. ERROR_OUT(-9612, done);
  15906. }
  15907. done:
  15908. wc_ecc_free(&priv_key);
  15909. wc_ecc_free(&pub_key);
  15910. return ret;
  15911. }
  15912. #endif /* HAVE_ECC_CDH */
  15913. #endif /* HAVE_ECC_VECTOR_TEST */
  15914. #ifdef HAVE_ECC_KEY_IMPORT
  15915. /* returns 0 on success */
  15916. static int ecc_test_make_pub(WC_RNG* rng)
  15917. {
  15918. ecc_key key;
  15919. byte exportBuf[ECC_BUFSIZE];
  15920. byte tmp[ECC_BUFSIZE];
  15921. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  15922. word32 x, tmpSz;
  15923. int ret = 0;
  15924. ecc_point* pubPoint = NULL;
  15925. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  15926. ecc_key pub;
  15927. #endif
  15928. #ifdef HAVE_ECC_VERIFY
  15929. int verify = 0;
  15930. #endif
  15931. #ifndef USE_CERT_BUFFERS_256
  15932. XFILE file;
  15933. #endif
  15934. wc_ecc_init_ex(&key, HEAP_HINT, devId);
  15935. #ifndef NO_ECC256
  15936. #ifdef USE_CERT_BUFFERS_256
  15937. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  15938. tmpSz = (size_t)sizeof_ecc_key_der_256;
  15939. #else
  15940. file = XFOPEN(eccKeyDerFile, "rb");
  15941. if (!file) {
  15942. ERROR_OUT(-9617, done);
  15943. }
  15944. tmpSz = (word32)XFREAD(tmp, 1, sizeof(tmp), file);
  15945. XFCLOSE(file);
  15946. #endif /* USE_CERT_BUFFERS_256 */
  15947. /* import private only then test with */
  15948. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  15949. if (ret == 0) {
  15950. ERROR_OUT(-9618, done);
  15951. }
  15952. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, &key);
  15953. if (ret == 0) {
  15954. ERROR_OUT(-9619, done);
  15955. }
  15956. x = 0;
  15957. ret = wc_EccPrivateKeyDecode(tmp, &x, &key, tmpSz);
  15958. if (ret != 0) {
  15959. ERROR_OUT(-9620, done);
  15960. }
  15961. #ifdef HAVE_ECC_KEY_EXPORT
  15962. x = sizeof(exportBuf);
  15963. ret = wc_ecc_export_private_only(&key, exportBuf, &x);
  15964. if (ret != 0) {
  15965. ERROR_OUT(-9621, done);
  15966. }
  15967. /* make private only key */
  15968. wc_ecc_free(&key);
  15969. wc_ecc_init_ex(&key, HEAP_HINT, devId);
  15970. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, &key);
  15971. if (ret != 0) {
  15972. ERROR_OUT(-9622, done);
  15973. }
  15974. x = sizeof(exportBuf);
  15975. ret = wc_ecc_export_x963_ex(&key, exportBuf, &x, 0);
  15976. if (ret == 0) {
  15977. ERROR_OUT(-9623, done);
  15978. }
  15979. #endif /* HAVE_ECC_KEY_EXPORT */
  15980. ret = wc_ecc_make_pub(NULL, NULL);
  15981. if (ret == 0) {
  15982. ERROR_OUT(-9624, done);
  15983. }
  15984. TEST_SLEEP();
  15985. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  15986. if (pubPoint == NULL) {
  15987. ERROR_OUT(-9625, done);
  15988. }
  15989. ret = wc_ecc_make_pub(&key, pubPoint);
  15990. if (ret != 0) {
  15991. ERROR_OUT(-9626, done);
  15992. }
  15993. TEST_SLEEP();
  15994. #ifdef HAVE_ECC_KEY_EXPORT
  15995. /* export should still fail, is private only key */
  15996. x = sizeof(exportBuf);
  15997. ret = wc_ecc_export_x963_ex(&key, exportBuf, &x, 0);
  15998. if (ret == 0) {
  15999. ERROR_OUT(-9627, done);
  16000. }
  16001. #endif /* HAVE_ECC_KEY_EXPORT */
  16002. #endif /* !NO_ECC256 */
  16003. /* create a new key since above test for loading key is not supported */
  16004. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256)
  16005. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, &key);
  16006. if (ret != 0) {
  16007. ERROR_OUT(-9628, done);
  16008. }
  16009. #endif
  16010. #ifdef HAVE_ECC_SIGN
  16011. tmpSz = sizeof(tmp);
  16012. ret = 0;
  16013. do {
  16014. #if defined(WOLFSSL_ASYNC_CRYPT)
  16015. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16016. #endif
  16017. if (ret == 0)
  16018. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp, &tmpSz, rng, &key);
  16019. } while (ret == WC_PENDING_E);
  16020. if (ret != 0) {
  16021. ERROR_OUT(-9629, done);
  16022. }
  16023. TEST_SLEEP();
  16024. #ifdef HAVE_ECC_VERIFY
  16025. /* try verify with private only key */
  16026. ret = 0;
  16027. do {
  16028. #if defined(WOLFSSL_ASYNC_CRYPT)
  16029. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16030. #endif
  16031. if (ret == 0)
  16032. ret = wc_ecc_verify_hash(tmp, tmpSz, msg, (word32)XSTRLEN((const char* )msg), &verify, &key);
  16033. } while (ret == WC_PENDING_E);
  16034. if (ret != 0) {
  16035. ERROR_OUT(-9630, done);
  16036. }
  16037. if (verify != 1) {
  16038. ERROR_OUT(-9631, done);
  16039. }
  16040. TEST_SLEEP();
  16041. #ifdef HAVE_ECC_KEY_EXPORT
  16042. /* exporting the public part should now work */
  16043. x = sizeof(exportBuf);
  16044. ret = wc_ecc_export_x963_ex(&key, exportBuf, &x, 0);
  16045. if (ret != 0) {
  16046. ERROR_OUT(-9632, done);
  16047. }
  16048. #endif /* HAVE_ECC_KEY_EXPORT */
  16049. #endif /* HAVE_ECC_VERIFY */
  16050. #endif /* HAVE_ECC_SIGN */
  16051. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  16052. /* now test private only key with creating a shared secret */
  16053. x = sizeof(exportBuf);
  16054. ret = wc_ecc_export_private_only(&key, exportBuf, &x);
  16055. if (ret != 0) {
  16056. ERROR_OUT(-9633, done);
  16057. }
  16058. /* make private only key */
  16059. wc_ecc_free(&key);
  16060. wc_ecc_init_ex(&key, HEAP_HINT, devId);
  16061. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, &key);
  16062. if (ret != 0) {
  16063. ERROR_OUT(-9634, done);
  16064. }
  16065. /* check that public export fails with private only key */
  16066. x = sizeof(exportBuf);
  16067. ret = wc_ecc_export_x963_ex(&key, exportBuf, &x, 0);
  16068. if (ret == 0) {
  16069. ERROR_OUT(-9635, done);
  16070. }
  16071. /* make public key for shared secret */
  16072. wc_ecc_init_ex(&pub, HEAP_HINT, devId);
  16073. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, &pub);
  16074. #if defined(WOLFSSL_ASYNC_CRYPT)
  16075. ret = wc_AsyncWait(ret, &pub.asyncDev, WC_ASYNC_FLAG_NONE);
  16076. #endif
  16077. if (ret != 0) {
  16078. ERROR_OUT(-9636, done);
  16079. }
  16080. TEST_SLEEP();
  16081. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  16082. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) && \
  16083. !defined(HAVE_SELFTEST)
  16084. ret = wc_ecc_set_rng(&key, rng);
  16085. if (ret != 0)
  16086. goto done;
  16087. #endif
  16088. x = sizeof(exportBuf);
  16089. do {
  16090. #if defined(WOLFSSL_ASYNC_CRYPT)
  16091. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16092. #endif
  16093. if (ret == 0) {
  16094. ret = wc_ecc_shared_secret(&key, &pub, exportBuf, &x);
  16095. }
  16096. } while (ret == WC_PENDING_E);
  16097. wc_ecc_free(&pub);
  16098. if (ret != 0) {
  16099. ERROR_OUT(-9637, done);
  16100. }
  16101. TEST_SLEEP();
  16102. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT */
  16103. ret = 0;
  16104. done:
  16105. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  16106. wc_ecc_free(&key);
  16107. return ret;
  16108. }
  16109. static int ecc_test_key_decode(WC_RNG* rng, int keySize)
  16110. {
  16111. int ret;
  16112. ecc_key eccKey;
  16113. byte tmpBuf[ECC_BUFSIZE];
  16114. word32 tmpSz;
  16115. word32 idx;
  16116. ret = wc_ecc_init(&eccKey);
  16117. if (ret != 0) {
  16118. return ret;
  16119. }
  16120. ret = wc_ecc_make_key(rng, keySize, &eccKey);
  16121. if (ret != 0) {
  16122. wc_ecc_free(&eccKey);
  16123. return ret;
  16124. }
  16125. tmpSz = sizeof(tmpBuf);
  16126. ret = wc_EccKeyToDer(&eccKey, tmpBuf, tmpSz);
  16127. wc_ecc_free(&eccKey);
  16128. if (ret < 0) {
  16129. return ret;
  16130. }
  16131. tmpSz = ret;
  16132. ret = wc_ecc_init(&eccKey);
  16133. if (ret != 0) {
  16134. return ret;
  16135. }
  16136. idx = 0;
  16137. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, &eccKey, tmpSz);
  16138. wc_ecc_free(&eccKey);
  16139. if (ret != 0) {
  16140. return ret;
  16141. }
  16142. ret = wc_ecc_init(&eccKey);
  16143. if (ret != 0) {
  16144. return 0;
  16145. }
  16146. idx = 0;
  16147. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, &eccKey, tmpSz);
  16148. wc_ecc_free(&eccKey);
  16149. return ret;
  16150. }
  16151. #endif /* HAVE_ECC_KEY_IMPORT */
  16152. static int ecc_test_key_gen(WC_RNG* rng, int keySize)
  16153. {
  16154. int ret = 0;
  16155. int derSz;
  16156. #ifdef HAVE_PKCS8
  16157. word32 pkcs8Sz;
  16158. #endif
  16159. byte der[ECC_BUFSIZE];
  16160. ecc_key userA;
  16161. ret = wc_ecc_init_ex(&userA, HEAP_HINT, devId);
  16162. if (ret != 0)
  16163. goto done;
  16164. ret = wc_ecc_make_key(rng, keySize, &userA);
  16165. #if defined(WOLFSSL_ASYNC_CRYPT)
  16166. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_NONE);
  16167. #endif
  16168. if (ret != 0)
  16169. goto done;
  16170. TEST_SLEEP();
  16171. ret = wc_ecc_check_key(&userA);
  16172. if (ret != 0)
  16173. goto done;
  16174. TEST_SLEEP();
  16175. derSz = wc_EccKeyToDer(&userA, der, sizeof(der));
  16176. if (derSz < 0) {
  16177. ERROR_OUT(derSz, done);
  16178. }
  16179. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  16180. ECC_PRIVATEKEY_TYPE, -8347);
  16181. if (ret != 0) {
  16182. goto done;
  16183. }
  16184. /* test export of public key */
  16185. derSz = wc_EccPublicKeyToDer(&userA, der, sizeof(der), 1);
  16186. if (derSz < 0) {
  16187. ERROR_OUT(derSz, done);
  16188. }
  16189. if (derSz == 0) {
  16190. ERROR_OUT(-9640, done);
  16191. }
  16192. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0, -8348);
  16193. if (ret != 0) {
  16194. goto done;
  16195. }
  16196. #ifdef HAVE_PKCS8
  16197. /* test export of PKCS#8 unencrypted private key */
  16198. pkcs8Sz = FOURK_BUF;
  16199. derSz = wc_EccPrivateKeyToPKCS8(&userA, der, &pkcs8Sz);
  16200. if (derSz < 0) {
  16201. ERROR_OUT(derSz, done);
  16202. }
  16203. if (derSz == 0) {
  16204. ERROR_OUT(-9641, done);
  16205. }
  16206. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0, -8349);
  16207. if (ret != 0) {
  16208. goto done;
  16209. }
  16210. #endif /* HAVE_PKCS8 */
  16211. done:
  16212. wc_ecc_free(&userA);
  16213. return ret;
  16214. }
  16215. static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  16216. int curve_id, const ecc_set_type* dp)
  16217. {
  16218. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && \
  16219. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  16220. DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  16221. DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  16222. #endif
  16223. #ifdef HAVE_ECC_KEY_EXPORT
  16224. byte exportBuf[MAX_ECC_BYTES * 2 + 32];
  16225. #endif
  16226. word32 x = 0;
  16227. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && \
  16228. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  16229. word32 y;
  16230. #endif
  16231. #ifdef HAVE_ECC_SIGN
  16232. DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  16233. DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  16234. int i;
  16235. #ifdef HAVE_ECC_VERIFY
  16236. int verify;
  16237. #endif /* HAVE_ECC_VERIFY */
  16238. #endif /* HAVE_ECC_SIGN */
  16239. int ret;
  16240. ecc_key userA, userB, pubKey;
  16241. int curveSize;
  16242. (void)testVerifyCount;
  16243. (void)dp;
  16244. (void)x;
  16245. XMEMSET(&userA, 0, sizeof(ecc_key));
  16246. XMEMSET(&userB, 0, sizeof(ecc_key));
  16247. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  16248. ret = wc_ecc_init_ex(&userA, HEAP_HINT, devId);
  16249. if (ret != 0)
  16250. goto done;
  16251. ret = wc_ecc_init_ex(&userB, HEAP_HINT, devId);
  16252. if (ret != 0)
  16253. goto done;
  16254. ret = wc_ecc_init_ex(&pubKey, HEAP_HINT, devId);
  16255. if (ret != 0)
  16256. goto done;
  16257. #ifdef WOLFSSL_CUSTOM_CURVES
  16258. if (dp != NULL) {
  16259. ret = wc_ecc_set_custom_curve(&userA, dp);
  16260. if (ret != 0)
  16261. goto done;
  16262. ret = wc_ecc_set_custom_curve(&userB, dp);
  16263. if (ret != 0)
  16264. goto done;
  16265. }
  16266. #endif
  16267. ret = wc_ecc_make_key_ex(rng, keySize, &userA, curve_id);
  16268. #if defined(WOLFSSL_ASYNC_CRYPT)
  16269. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_NONE);
  16270. #endif
  16271. if (ret != 0)
  16272. goto done;
  16273. TEST_SLEEP();
  16274. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  16275. curveSize = wc_ecc_get_curve_size_from_id(userA.dp->id);
  16276. if (curveSize != userA.dp->size) {
  16277. ret = -9642;
  16278. goto done;
  16279. }
  16280. }
  16281. ret = wc_ecc_check_key(&userA);
  16282. if (ret != 0)
  16283. goto done;
  16284. TEST_SLEEP();
  16285. /* ATECC508/608 configuration may not support more than one ECDH key */
  16286. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  16287. ret = wc_ecc_make_key_ex(rng, keySize, &userB, curve_id);
  16288. #if defined(WOLFSSL_ASYNC_CRYPT)
  16289. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_NONE);
  16290. #endif
  16291. if (ret != 0)
  16292. goto done;
  16293. TEST_SLEEP();
  16294. /* only perform the below tests if the key size matches */
  16295. if (dp == NULL && keySize > 0 && wc_ecc_size(&userA) != keySize) {
  16296. ret = ECC_CURVE_OID_E;
  16297. goto done;
  16298. }
  16299. #ifdef HAVE_ECC_DHE
  16300. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  16301. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) && \
  16302. !defined(HAVE_SELFTEST)
  16303. ret = wc_ecc_set_rng(&userA, rng);
  16304. if (ret != 0)
  16305. goto done;
  16306. ret = wc_ecc_set_rng(&userB, rng);
  16307. if (ret != 0)
  16308. goto done;
  16309. #endif
  16310. x = ECC_SHARED_SIZE;
  16311. do {
  16312. #if defined(WOLFSSL_ASYNC_CRYPT)
  16313. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16314. #endif
  16315. if (ret == 0)
  16316. ret = wc_ecc_shared_secret(&userA, &userB, sharedA, &x);
  16317. } while (ret == WC_PENDING_E);
  16318. if (ret != 0) {
  16319. goto done;
  16320. }
  16321. TEST_SLEEP();
  16322. y = ECC_SHARED_SIZE;
  16323. do {
  16324. #if defined(WOLFSSL_ASYNC_CRYPT)
  16325. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16326. #endif
  16327. if (ret == 0)
  16328. ret = wc_ecc_shared_secret(&userB, &userA, sharedB, &y);
  16329. } while (ret == WC_PENDING_E);
  16330. if (ret != 0)
  16331. goto done;
  16332. if (y != x)
  16333. ERROR_OUT(-9643, done);
  16334. if (XMEMCMP(sharedA, sharedB, x))
  16335. ERROR_OUT(-9644, done);
  16336. TEST_SLEEP();
  16337. #endif /* HAVE_ECC_DHE */
  16338. #ifdef HAVE_ECC_CDH
  16339. /* add cofactor flag */
  16340. wc_ecc_set_flags(&userA, WC_ECC_FLAG_COFACTOR);
  16341. wc_ecc_set_flags(&userB, WC_ECC_FLAG_COFACTOR);
  16342. x = ECC_SHARED_SIZE;
  16343. do {
  16344. #if defined(WOLFSSL_ASYNC_CRYPT)
  16345. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16346. #endif
  16347. if (ret == 0)
  16348. ret = wc_ecc_shared_secret(&userA, &userB, sharedA, &x);
  16349. } while (ret == WC_PENDING_E);
  16350. if (ret != 0) {
  16351. goto done;
  16352. }
  16353. TEST_SLEEP();
  16354. y = ECC_SHARED_SIZE;
  16355. do {
  16356. #if defined(WOLFSSL_ASYNC_CRYPT)
  16357. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16358. #endif
  16359. if (ret == 0)
  16360. ret = wc_ecc_shared_secret(&userB, &userA, sharedB, &y);
  16361. } while (ret == WC_PENDING_E);
  16362. if (ret != 0)
  16363. goto done;
  16364. if (y != x)
  16365. ERROR_OUT(-9645, done);
  16366. if (XMEMCMP(sharedA, sharedB, x))
  16367. ERROR_OUT(-9646, done);
  16368. TEST_SLEEP();
  16369. /* remove cofactor flag */
  16370. wc_ecc_set_flags(&userA, 0);
  16371. wc_ecc_set_flags(&userB, 0);
  16372. #endif /* HAVE_ECC_CDH */
  16373. #endif /* WOLFSSL_ATECC508A */
  16374. #ifdef HAVE_ECC_KEY_EXPORT
  16375. x = sizeof(exportBuf);
  16376. ret = wc_ecc_export_x963_ex(&userA, exportBuf, &x, 0);
  16377. if (ret != 0)
  16378. goto done;
  16379. #ifdef HAVE_ECC_KEY_IMPORT
  16380. #ifdef WOLFSSL_CUSTOM_CURVES
  16381. if (dp != NULL) {
  16382. ret = wc_ecc_set_custom_curve(&pubKey, dp);
  16383. if (ret != 0) goto done;
  16384. }
  16385. #endif
  16386. ret = wc_ecc_import_x963_ex(exportBuf, x, &pubKey, curve_id);
  16387. if (ret != 0)
  16388. goto done;
  16389. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  16390. #ifdef HAVE_ECC_DHE
  16391. y = ECC_SHARED_SIZE;
  16392. do {
  16393. #if defined(WOLFSSL_ASYNC_CRYPT)
  16394. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16395. #endif
  16396. if (ret == 0)
  16397. ret = wc_ecc_shared_secret(&userB, &pubKey, sharedB, &y);
  16398. } while (ret == WC_PENDING_E);
  16399. if (ret != 0)
  16400. goto done;
  16401. if (XMEMCMP(sharedA, sharedB, y))
  16402. ERROR_OUT(-9647, done);
  16403. TEST_SLEEP();
  16404. #endif /* HAVE_ECC_DHE */
  16405. #ifdef HAVE_COMP_KEY
  16406. /* try compressed export / import too */
  16407. x = sizeof(exportBuf);
  16408. ret = wc_ecc_export_x963_ex(&userA, exportBuf, &x, 1);
  16409. if (ret != 0)
  16410. goto done;
  16411. wc_ecc_free(&pubKey);
  16412. ret = wc_ecc_init_ex(&pubKey, HEAP_HINT, devId);
  16413. if (ret != 0)
  16414. goto done;
  16415. #ifdef WOLFSSL_CUSTOM_CURVES
  16416. if (dp != NULL) {
  16417. ret = wc_ecc_set_custom_curve(&pubKey, dp);
  16418. if (ret != 0) goto done;
  16419. }
  16420. #endif
  16421. ret = wc_ecc_import_x963_ex(exportBuf, x, &pubKey, curve_id);
  16422. if (ret != 0)
  16423. goto done;
  16424. #ifdef HAVE_ECC_DHE
  16425. y = ECC_SHARED_SIZE;
  16426. do {
  16427. #if defined(WOLFSSL_ASYNC_CRYPT)
  16428. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16429. #endif
  16430. if (ret == 0)
  16431. ret = wc_ecc_shared_secret(&userB, &pubKey, sharedB, &y);
  16432. } while (ret == WC_PENDING_E);
  16433. if (ret != 0)
  16434. goto done;
  16435. if (XMEMCMP(sharedA, sharedB, y))
  16436. ERROR_OUT(-9648, done);
  16437. TEST_SLEEP();
  16438. #endif /* HAVE_ECC_DHE */
  16439. #endif /* HAVE_COMP_KEY */
  16440. #endif /* WOLFSSL_ATECC508A */
  16441. #endif /* HAVE_ECC_KEY_IMPORT */
  16442. #endif /* HAVE_ECC_KEY_EXPORT */
  16443. #ifdef HAVE_ECC_SIGN
  16444. /* ECC w/out Shamir has issue with all 0 digest */
  16445. /* WC_BIGINT doesn't have 0 len well on hardware */
  16446. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT)
  16447. /* test DSA sign hash with zeros */
  16448. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  16449. digest[i] = 0;
  16450. }
  16451. x = ECC_SIG_SIZE;
  16452. do {
  16453. #if defined(WOLFSSL_ASYNC_CRYPT)
  16454. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16455. #endif
  16456. if (ret == 0)
  16457. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  16458. &userA);
  16459. } while (ret == WC_PENDING_E);
  16460. if (ret != 0)
  16461. goto done;
  16462. TEST_SLEEP();
  16463. #ifdef HAVE_ECC_VERIFY
  16464. for (i=0; i<testVerifyCount; i++) {
  16465. verify = 0;
  16466. do {
  16467. #if defined(WOLFSSL_ASYNC_CRYPT)
  16468. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16469. #endif
  16470. if (ret == 0)
  16471. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  16472. &verify, &userA);
  16473. } while (ret == WC_PENDING_E);
  16474. if (ret != 0)
  16475. goto done;
  16476. if (verify != 1)
  16477. ERROR_OUT(-9649, done);
  16478. TEST_SLEEP();
  16479. }
  16480. #endif /* HAVE_ECC_VERIFY */
  16481. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT */
  16482. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  16483. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  16484. digest[i] = (byte)i;
  16485. }
  16486. x = ECC_SIG_SIZE;
  16487. do {
  16488. #if defined(WOLFSSL_ASYNC_CRYPT)
  16489. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16490. #endif
  16491. if (ret == 0)
  16492. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  16493. &userA);
  16494. } while (ret == WC_PENDING_E);
  16495. if (ret != 0)
  16496. ERROR_OUT(-9650, done);
  16497. TEST_SLEEP();
  16498. #ifdef HAVE_ECC_VERIFY
  16499. for (i=0; i<testVerifyCount; i++) {
  16500. verify = 0;
  16501. do {
  16502. #if defined(WOLFSSL_ASYNC_CRYPT)
  16503. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16504. #endif
  16505. if (ret == 0)
  16506. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  16507. &verify, &userA);
  16508. } while (ret == WC_PENDING_E);
  16509. if (ret != 0)
  16510. goto done;
  16511. if (verify != 1)
  16512. ERROR_OUT(-9651, done);
  16513. TEST_SLEEP();
  16514. }
  16515. #endif /* HAVE_ECC_VERIFY */
  16516. #endif /* HAVE_ECC_SIGN */
  16517. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WOLFSSL_ATECC508) && \
  16518. !defined(WOLFSSL_ATECC608A)
  16519. x = sizeof(exportBuf);
  16520. ret = wc_ecc_export_private_only(&userA, exportBuf, &x);
  16521. if (ret != 0)
  16522. goto done;
  16523. #endif /* HAVE_ECC_KEY_EXPORT */
  16524. done:
  16525. wc_ecc_free(&pubKey);
  16526. wc_ecc_free(&userB);
  16527. wc_ecc_free(&userA);
  16528. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  16529. FREE_VAR(sharedA, HEAP_HINT);
  16530. FREE_VAR(sharedB, HEAP_HINT);
  16531. #endif
  16532. #ifdef HAVE_ECC_SIGN
  16533. FREE_VAR(sig, HEAP_HINT);
  16534. FREE_VAR(digest, HEAP_HINT);
  16535. #endif
  16536. return ret;
  16537. }
  16538. #undef ECC_TEST_VERIFY_COUNT
  16539. #define ECC_TEST_VERIFY_COUNT 2
  16540. static int ecc_test_curve(WC_RNG* rng, int keySize)
  16541. {
  16542. int ret;
  16543. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT,
  16544. ECC_CURVE_DEF, NULL);
  16545. if (ret < 0) {
  16546. if (ret == ECC_CURVE_OID_E) {
  16547. /* ignore error for curves not found */
  16548. /* some curve sizes are only available with:
  16549. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  16550. and HAVE_ECC_KOBLITZ */
  16551. }
  16552. else {
  16553. printf("ecc_test_curve_size %d failed!: %d\n", keySize, ret);
  16554. return ret;
  16555. }
  16556. }
  16557. #ifdef HAVE_ECC_VECTOR_TEST
  16558. ret = ecc_test_vector(keySize);
  16559. if (ret < 0) {
  16560. printf("ecc_test_vector %d failed!: %d\n", keySize, ret);
  16561. return ret;
  16562. }
  16563. #endif
  16564. ret = ecc_test_key_decode(rng, keySize);
  16565. if (ret < 0) {
  16566. if (ret == ECC_CURVE_OID_E) {
  16567. /* ignore error for curves not found */
  16568. }
  16569. else {
  16570. printf("ecc_test_key_decode %d failed!: %d\n", keySize, ret);
  16571. return ret;
  16572. }
  16573. }
  16574. ret = ecc_test_key_gen(rng, keySize);
  16575. if (ret < 0) {
  16576. if (ret == ECC_CURVE_OID_E) {
  16577. /* ignore error for curves not found */
  16578. }
  16579. else {
  16580. printf("ecc_test_key_gen %d failed!: %d\n", keySize, ret);
  16581. return ret;
  16582. }
  16583. }
  16584. return 0;
  16585. }
  16586. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  16587. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  16588. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  16589. static int ecc_point_test(void)
  16590. {
  16591. int ret;
  16592. ecc_point* point;
  16593. ecc_point* point2;
  16594. #ifdef HAVE_COMP_KEY
  16595. ecc_point* point3;
  16596. ecc_point* point4;
  16597. #endif
  16598. word32 outLen;
  16599. byte out[65];
  16600. byte der[] = { 0x04, /* = Uncompressed */
  16601. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  16602. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  16603. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  16604. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  16605. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  16606. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  16607. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  16608. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  16609. #ifdef HAVE_COMP_KEY
  16610. byte derComp0[] = { 0x02, /* = Compressed, y even */
  16611. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  16612. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  16613. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  16614. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  16615. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  16616. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  16617. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  16618. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  16619. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  16620. #endif
  16621. byte altDer[] = { 0x04, /* = Uncompressed */
  16622. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16623. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16624. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16625. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16626. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16627. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16628. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16629. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  16630. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  16631. /* if curve P256 is not enabled then test should not fail */
  16632. if (curve_idx == ECC_CURVE_INVALID)
  16633. return 0;
  16634. outLen = sizeof(out);
  16635. point = wc_ecc_new_point();
  16636. if (point == NULL)
  16637. return -9700;
  16638. point2 = wc_ecc_new_point();
  16639. if (point2 == NULL) {
  16640. wc_ecc_del_point(point);
  16641. return -9701;
  16642. }
  16643. #ifdef HAVE_COMP_KEY
  16644. point3 = wc_ecc_new_point();
  16645. if (point3 == NULL) {
  16646. wc_ecc_del_point(point2);
  16647. wc_ecc_del_point(point);
  16648. return -9702;
  16649. }
  16650. point4 = wc_ecc_new_point();
  16651. if (point4 == NULL) {
  16652. wc_ecc_del_point(point3);
  16653. wc_ecc_del_point(point2);
  16654. wc_ecc_del_point(point);
  16655. return -9703;
  16656. }
  16657. #endif
  16658. /* Parameter Validation testing. */
  16659. wc_ecc_del_point(NULL);
  16660. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  16661. if (ret != ECC_BAD_ARG_E) {
  16662. ret = -9704;
  16663. goto done;
  16664. }
  16665. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  16666. if (ret != ECC_BAD_ARG_E) {
  16667. ret = -9705;
  16668. goto done;
  16669. }
  16670. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  16671. if (ret != ECC_BAD_ARG_E) {
  16672. ret = -9706;
  16673. goto done;
  16674. }
  16675. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  16676. if (ret != ECC_BAD_ARG_E) {
  16677. ret = -9707;
  16678. goto done;
  16679. }
  16680. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  16681. if (ret != ECC_BAD_ARG_E) {
  16682. ret = -9708;
  16683. goto done;
  16684. }
  16685. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  16686. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  16687. ret = -9709;
  16688. goto done;
  16689. }
  16690. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  16691. if (ret != ECC_BAD_ARG_E) {
  16692. ret = -9710;
  16693. goto done;
  16694. }
  16695. outLen = 0;
  16696. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  16697. if (ret != BUFFER_E) {
  16698. ret = -9711;
  16699. goto done;
  16700. }
  16701. ret = wc_ecc_copy_point(NULL, NULL);
  16702. if (ret != ECC_BAD_ARG_E) {
  16703. ret = -9712;
  16704. goto done;
  16705. }
  16706. ret = wc_ecc_copy_point(NULL, point2);
  16707. if (ret != ECC_BAD_ARG_E) {
  16708. ret = -9713;
  16709. goto done;
  16710. }
  16711. ret = wc_ecc_copy_point(point, NULL);
  16712. if (ret != ECC_BAD_ARG_E) {
  16713. ret = -9714;
  16714. goto done;
  16715. }
  16716. ret = wc_ecc_cmp_point(NULL, NULL);
  16717. if (ret != BAD_FUNC_ARG) {
  16718. ret = -9715;
  16719. goto done;
  16720. }
  16721. ret = wc_ecc_cmp_point(NULL, point2);
  16722. if (ret != BAD_FUNC_ARG) {
  16723. ret = -9716;
  16724. goto done;
  16725. }
  16726. ret = wc_ecc_cmp_point(point, NULL);
  16727. if (ret != BAD_FUNC_ARG) {
  16728. ret = -9717;
  16729. goto done;
  16730. }
  16731. /* Use API. */
  16732. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  16733. if (ret != 0) {
  16734. ret = -9718;
  16735. goto done;
  16736. }
  16737. outLen = sizeof(out);
  16738. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  16739. if (ret != 0) {
  16740. ret = -9719;
  16741. goto done;
  16742. }
  16743. if (outLen != sizeof(der)) {
  16744. ret = -9720;
  16745. goto done;
  16746. }
  16747. if (XMEMCMP(out, der, outLen) != 0) {
  16748. ret = -9721;
  16749. goto done;
  16750. }
  16751. ret = wc_ecc_copy_point(point2, point);
  16752. if (ret != MP_OKAY) {
  16753. ret = -9722;
  16754. goto done;
  16755. }
  16756. ret = wc_ecc_cmp_point(point2, point);
  16757. if (ret != MP_EQ) {
  16758. ret = -9723;
  16759. goto done;
  16760. }
  16761. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  16762. if (ret != 0) {
  16763. ret = -9724;
  16764. goto done;
  16765. }
  16766. ret = wc_ecc_cmp_point(point2, point);
  16767. if (ret != MP_GT) {
  16768. ret = -9725;
  16769. goto done;
  16770. }
  16771. #ifdef HAVE_COMP_KEY
  16772. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  16773. if (ret != 0) {
  16774. ret = -9726;
  16775. goto done;
  16776. }
  16777. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  16778. if (ret != 0) {
  16779. ret = -9727;
  16780. goto done;
  16781. }
  16782. ret = wc_ecc_cmp_point(point3, point4);
  16783. if (ret != MP_EQ) {
  16784. ret = -9728;
  16785. goto done;
  16786. }
  16787. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  16788. if (ret != 0) {
  16789. ret = -9729;
  16790. goto done;
  16791. }
  16792. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  16793. if (ret != 0) {
  16794. ret = -9730;
  16795. goto done;
  16796. }
  16797. ret = wc_ecc_cmp_point(point3, point4);
  16798. if (ret != MP_EQ) {
  16799. ret = -9731;
  16800. goto done;
  16801. }
  16802. #endif
  16803. done:
  16804. #ifdef HAVE_COMP_KEY
  16805. wc_ecc_del_point(point4);
  16806. wc_ecc_del_point(point3);
  16807. #endif
  16808. wc_ecc_del_point(point2);
  16809. wc_ecc_del_point(point);
  16810. return ret;
  16811. }
  16812. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  16813. #ifndef NO_SIG_WRAPPER
  16814. static int ecc_sig_test(WC_RNG* rng, ecc_key* key)
  16815. {
  16816. int ret;
  16817. word32 sigSz;
  16818. int size;
  16819. byte out[ECC_MAX_SIG_SIZE];
  16820. byte in[] = "Everyone gets Friday off.";
  16821. const byte hash[] = {
  16822. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  16823. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  16824. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  16825. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  16826. };
  16827. word32 inLen = (word32)XSTRLEN((char*)in);
  16828. size = wc_ecc_sig_size(key);
  16829. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  16830. if (ret != size)
  16831. return -9728;
  16832. sigSz = (word32)ret;
  16833. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  16834. inLen, out, &sigSz, key, sizeof(*key), rng);
  16835. if (ret != 0)
  16836. return -9729;
  16837. TEST_SLEEP();
  16838. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  16839. inLen, out, sigSz, key, sizeof(*key));
  16840. if (ret != 0)
  16841. return -9730;
  16842. TEST_SLEEP();
  16843. sigSz = (word32)sizeof(out);
  16844. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  16845. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  16846. if (ret != 0)
  16847. return -9731;
  16848. TEST_SLEEP();
  16849. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  16850. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  16851. if (ret != 0)
  16852. return -9732;
  16853. TEST_SLEEP();
  16854. return 0;
  16855. }
  16856. #endif
  16857. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  16858. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  16859. static int ecc_exp_imp_test(ecc_key* key)
  16860. {
  16861. int ret;
  16862. int curve_id;
  16863. ecc_key keyImp;
  16864. byte priv[32];
  16865. word32 privLen;
  16866. byte pub[65];
  16867. word32 pubLen, pubLenX, pubLenY;
  16868. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  16869. "ce46cbc947616d0cbaa82323818a793d";
  16870. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  16871. "922f8b30ea6e8811742ac7238fe87308";
  16872. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  16873. "2e7a493ec1153b3a95feb8a4873f8d08";
  16874. wc_ecc_init_ex(&keyImp, HEAP_HINT, devId);
  16875. privLen = sizeof(priv);
  16876. ret = wc_ecc_export_private_only(key, priv, &privLen);
  16877. if (ret != 0) {
  16878. ret = -9733;
  16879. goto done;
  16880. }
  16881. pubLen = sizeof(pub);
  16882. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  16883. if (ret != 0) {
  16884. ret = -9734;
  16885. goto done;
  16886. }
  16887. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, &keyImp);
  16888. if (ret != 0) {
  16889. ret = -9735;
  16890. goto done;
  16891. }
  16892. wc_ecc_free(&keyImp);
  16893. wc_ecc_init_ex(&keyImp, HEAP_HINT, devId);
  16894. ret = wc_ecc_import_raw_ex(&keyImp, qx, qy, d, ECC_SECP256R1);
  16895. if (ret != 0) {
  16896. ret = -9736;
  16897. goto done;
  16898. }
  16899. wc_ecc_free(&keyImp);
  16900. wc_ecc_init_ex(&keyImp, HEAP_HINT, devId);
  16901. curve_id = wc_ecc_get_curve_id(key->idx);
  16902. if (curve_id < 0) {
  16903. ret = -9737;
  16904. goto done;
  16905. }
  16906. /* test import private only */
  16907. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, &keyImp,
  16908. curve_id);
  16909. if (ret != 0) {
  16910. ret = -9738;
  16911. goto done;
  16912. }
  16913. wc_ecc_free(&keyImp);
  16914. wc_ecc_init_ex(&keyImp, HEAP_HINT, devId);
  16915. /* test export public raw */
  16916. pubLenX = pubLenY = 32;
  16917. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  16918. if (ret != 0) {
  16919. ret = -9739;
  16920. goto done;
  16921. }
  16922. #ifndef HAVE_SELFTEST
  16923. /* test import of public */
  16924. ret = wc_ecc_import_unsigned(&keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  16925. if (ret != 0) {
  16926. ret = -9740;
  16927. goto done;
  16928. }
  16929. #endif
  16930. wc_ecc_free(&keyImp);
  16931. wc_ecc_init_ex(&keyImp, HEAP_HINT, devId);
  16932. /* test export private and public raw */
  16933. pubLenX = pubLenY = privLen = 32;
  16934. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  16935. priv, &privLen);
  16936. if (ret != 0) {
  16937. ret = -9741;
  16938. goto done;
  16939. }
  16940. #ifndef HAVE_SELFTEST
  16941. /* test import of private and public */
  16942. ret = wc_ecc_import_unsigned(&keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  16943. if (ret != 0) {
  16944. ret = -9742;
  16945. goto done;
  16946. }
  16947. #endif
  16948. done:
  16949. wc_ecc_free(&keyImp);
  16950. return ret;
  16951. }
  16952. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  16953. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  16954. !defined(WOLFSSL_CRYPTOCELL)
  16955. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)
  16956. static int ecc_mulmod_test(ecc_key* key1)
  16957. {
  16958. int ret;
  16959. ecc_key key2;
  16960. ecc_key key3;
  16961. wc_ecc_init_ex(&key2, HEAP_HINT, devId);
  16962. wc_ecc_init_ex(&key3, HEAP_HINT, devId);
  16963. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  16964. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  16965. * private key in key2.
  16966. */
  16967. ret = wc_ecc_import_raw_ex(&key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  16968. ECC_SECP256R1);
  16969. if (ret != 0)
  16970. goto done;
  16971. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  16972. * in key3.
  16973. */
  16974. ret = wc_ecc_import_raw_ex(&key3, key1->dp->Gx, key1->dp->Gy,
  16975. key1->dp->prime, ECC_SECP256R1);
  16976. if (ret != 0)
  16977. goto done;
  16978. ret = wc_ecc_mulmod(&key1->k, &key2.pubkey, &key3.pubkey, &key2.k, &key3.k,
  16979. 1);
  16980. if (ret != 0) {
  16981. ret = -9743;
  16982. goto done;
  16983. }
  16984. done:
  16985. wc_ecc_free(&key3);
  16986. wc_ecc_free(&key2);
  16987. return ret;
  16988. }
  16989. #endif
  16990. #ifdef HAVE_ECC_DHE
  16991. static int ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  16992. {
  16993. int ret;
  16994. byte out[128];
  16995. word32 outLen = sizeof(out);
  16996. /* Parameter Validation testing. */
  16997. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  16998. if (ret != BAD_FUNC_ARG)
  16999. return -9744;
  17000. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  17001. if (ret != BAD_FUNC_ARG)
  17002. return -9745;
  17003. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  17004. if (ret != BAD_FUNC_ARG)
  17005. return -9746;
  17006. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  17007. if (ret != BAD_FUNC_ARG)
  17008. return -9747;
  17009. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  17010. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) && \
  17011. !defined(HAVE_SELFTEST)
  17012. ret = wc_ecc_set_rng(key, rng);
  17013. if (ret != 0)
  17014. return -9748;
  17015. #else
  17016. (void)rng;
  17017. #endif
  17018. /* Use API. */
  17019. ret = 0;
  17020. do {
  17021. #if defined(WOLFSSL_ASYNC_CRYPT)
  17022. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17023. #endif
  17024. if (ret == 0)
  17025. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  17026. } while (ret == WC_PENDING_E);
  17027. if (ret != 0)
  17028. return -9749;
  17029. TEST_SLEEP();
  17030. return 0;
  17031. }
  17032. #endif /* HAVE_ECC_DHE */
  17033. #endif
  17034. static int ecc_def_curve_test(WC_RNG *rng)
  17035. {
  17036. int ret;
  17037. ecc_key key;
  17038. wc_ecc_init_ex(&key, HEAP_HINT, devId);
  17039. /* Use API */
  17040. ret = wc_ecc_set_flags(NULL, 0);
  17041. if (ret != BAD_FUNC_ARG) {
  17042. ret = -9749;
  17043. goto done;
  17044. }
  17045. ret = wc_ecc_set_flags(&key, 0);
  17046. if (ret != 0) {
  17047. ret = -9750;
  17048. goto done;
  17049. }
  17050. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, &key);
  17051. #if defined(WOLFSSL_ASYNC_CRYPT)
  17052. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  17053. #endif
  17054. if (ret != 0) {
  17055. ret = -9751;
  17056. goto done;
  17057. }
  17058. TEST_SLEEP();
  17059. #ifndef NO_SIG_WRAPPER
  17060. ret = ecc_sig_test(rng, &key);
  17061. if (ret < 0)
  17062. goto done;
  17063. #endif
  17064. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  17065. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  17066. ret = ecc_exp_imp_test(&key);
  17067. if (ret < 0)
  17068. goto done;
  17069. #endif
  17070. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  17071. !defined(WOLFSSL_CRYPTOCELL)
  17072. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)
  17073. ret = ecc_mulmod_test(&key);
  17074. if (ret < 0)
  17075. goto done;
  17076. #endif
  17077. #ifdef HAVE_ECC_DHE
  17078. ret = ecc_ssh_test(&key, rng);
  17079. if (ret < 0)
  17080. goto done;
  17081. #endif
  17082. #endif /* WOLFSSL_ATECC508A */
  17083. done:
  17084. wc_ecc_free(&key);
  17085. return ret;
  17086. }
  17087. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  17088. #ifdef WOLFSSL_CERT_EXT
  17089. static int ecc_decode_test(void)
  17090. {
  17091. int ret;
  17092. word32 inSz;
  17093. word32 inOutIdx;
  17094. ecc_key key;
  17095. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  17096. /* This is ecc_clikeypub_der_256. */
  17097. static const byte good[] = {
  17098. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  17099. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  17100. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  17101. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  17102. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  17103. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  17104. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  17105. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  17106. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  17107. 0xb4 };
  17108. static const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  17109. 0x00, 0x04, 0x01, 0x01 };
  17110. static const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  17111. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  17112. static const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  17113. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  17114. static const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  17115. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  17116. static const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  17117. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  17118. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  17119. static const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  17120. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  17121. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  17122. static const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  17123. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  17124. 0x03, 0x03, 0x04, 0x01, 0x01 };
  17125. static const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  17126. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  17127. 0x03, 0x03, 0x00, 0x04, 0x01 };
  17128. XMEMSET(&key, 0, sizeof(key));
  17129. wc_ecc_init_ex(&key, HEAP_HINT, devId);
  17130. inSz = sizeof(good);
  17131. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, &key, inSz);
  17132. if (ret != BAD_FUNC_ARG) {
  17133. ret = -9800;
  17134. goto done;
  17135. }
  17136. ret = wc_EccPublicKeyDecode(good, NULL, &key, inSz);
  17137. if (ret != BAD_FUNC_ARG) {
  17138. ret = -9801;
  17139. goto done;
  17140. }
  17141. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  17142. if (ret != BAD_FUNC_ARG) {
  17143. ret = -9802;
  17144. goto done;
  17145. }
  17146. ret = wc_EccPublicKeyDecode(good, &inOutIdx, &key, 0);
  17147. if (ret != BAD_FUNC_ARG) {
  17148. ret = -9803;
  17149. goto done;
  17150. }
  17151. /* Change offset to produce bad input data. */
  17152. inOutIdx = 2;
  17153. inSz = sizeof(good) - inOutIdx;
  17154. ret = wc_EccPublicKeyDecode(good, &inOutIdx, &key, inSz);
  17155. if (ret != ASN_PARSE_E) {
  17156. ret = -9804;
  17157. goto done;
  17158. }
  17159. inOutIdx = 4;
  17160. inSz = sizeof(good) - inOutIdx;
  17161. ret = wc_EccPublicKeyDecode(good, &inOutIdx, &key, inSz);
  17162. if (ret != ASN_PARSE_E) {
  17163. ret = -9805;
  17164. goto done;
  17165. }
  17166. /* Bad data. */
  17167. inSz = sizeof(badNoObjId);
  17168. inOutIdx = 0;
  17169. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, &key, inSz);
  17170. if (ret != ASN_OBJECT_ID_E) {
  17171. ret = -9806;
  17172. goto done;
  17173. }
  17174. inSz = sizeof(badOneObjId);
  17175. inOutIdx = 0;
  17176. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, &key, inSz);
  17177. if (ret != ASN_OBJECT_ID_E) {
  17178. ret = -9807;
  17179. goto done;
  17180. }
  17181. inSz = sizeof(badObjId1Len);
  17182. inOutIdx = 0;
  17183. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, &key, inSz);
  17184. if (ret != ASN_PARSE_E) {
  17185. ret = -9808;
  17186. goto done;
  17187. }
  17188. inSz = sizeof(badObj2d1Len);
  17189. inOutIdx = 0;
  17190. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, &key, inSz);
  17191. if (ret != ASN_PARSE_E) {
  17192. ret = -9809;
  17193. goto done;
  17194. }
  17195. inSz = sizeof(badNotBitStr);
  17196. inOutIdx = 0;
  17197. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, &key, inSz);
  17198. if (ret != ASN_BITSTR_E) {
  17199. ret = -9810;
  17200. goto done;
  17201. }
  17202. inSz = sizeof(badBitStrLen);
  17203. inOutIdx = 0;
  17204. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, &key, inSz);
  17205. if (ret != ASN_PARSE_E) {
  17206. ret = -9811;
  17207. goto done;
  17208. }
  17209. inSz = sizeof(badNoBitStrZero);
  17210. inOutIdx = 0;
  17211. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, &key, inSz);
  17212. if (ret != ASN_EXPECT_0_E) {
  17213. ret = -9812;
  17214. goto done;
  17215. }
  17216. inSz = sizeof(badPoint);
  17217. inOutIdx = 0;
  17218. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, &key, inSz);
  17219. if (ret != ASN_ECC_KEY_E) {
  17220. ret = -9813;
  17221. goto done;
  17222. }
  17223. inSz = sizeof(good);
  17224. inOutIdx = 0;
  17225. ret = wc_EccPublicKeyDecode(good, &inOutIdx, &key, inSz);
  17226. if (ret != 0) {
  17227. ret = -9814;
  17228. goto done;
  17229. }
  17230. done:
  17231. wc_ecc_free(&key);
  17232. return ret;
  17233. }
  17234. #endif /* WOLFSSL_CERT_EXT */
  17235. #ifdef WOLFSSL_CUSTOM_CURVES
  17236. static const byte eccKeyExplicitCurve[] = {
  17237. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  17238. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  17239. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  17240. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  17241. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  17242. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  17243. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  17244. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  17245. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  17246. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  17247. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  17248. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  17249. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  17250. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  17251. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  17252. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  17253. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  17254. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  17255. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  17256. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  17257. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  17258. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  17259. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  17260. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  17261. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  17262. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  17263. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  17264. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  17265. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  17266. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  17267. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  17268. };
  17269. static int ecc_test_custom_curves(WC_RNG* rng)
  17270. {
  17271. int ret;
  17272. word32 inOutIdx;
  17273. ecc_key key;
  17274. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  17275. #ifndef WOLFSSL_ECC_CURVE_STATIC
  17276. const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  17277. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  17278. };
  17279. const word32 ecc_oid_brainpoolp256r1_sz =
  17280. sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t);
  17281. #else
  17282. #define ecc_oid_brainpoolp256r1 { \
  17283. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  17284. }
  17285. #define ecc_oid_brainpoolp256r1_sz 9
  17286. #endif
  17287. const word32 ecc_oid_brainpoolp256r1_sum = 104;
  17288. const ecc_set_type ecc_dp_brainpool256r1 = {
  17289. 32, /* size/bytes */
  17290. ECC_CURVE_CUSTOM, /* ID */
  17291. "BRAINPOOLP256R1", /* curve name */
  17292. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  17293. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  17294. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  17295. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  17296. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  17297. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  17298. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  17299. ecc_oid_brainpoolp256r1_sz,
  17300. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  17301. 1, /* cofactor */
  17302. };
  17303. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  17304. &ecc_dp_brainpool256r1);
  17305. if (ret != 0) {
  17306. printf("ECC test for custom curve failed! %d\n", ret);
  17307. return ret;
  17308. }
  17309. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  17310. {
  17311. int curve_id;
  17312. #ifdef HAVE_ECC_BRAINPOOL
  17313. curve_id = ECC_BRAINPOOLP256R1;
  17314. #else
  17315. curve_id = ECC_SECP256K1;
  17316. #endif
  17317. /* Test and demonstrate use of non-SECP curve */
  17318. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  17319. if (ret < 0) {
  17320. printf("ECC test for curve_id %d failed! %d\n", curve_id, ret);
  17321. return ret;
  17322. }
  17323. }
  17324. #endif
  17325. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  17326. if (ret != 0) {
  17327. return -9815;
  17328. }
  17329. inOutIdx = 0;
  17330. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, &key,
  17331. sizeof(eccKeyExplicitCurve));
  17332. if (ret != 0)
  17333. return -9816;
  17334. wc_ecc_free(&key);
  17335. return ret;
  17336. }
  17337. #endif /* WOLFSSL_CUSTOM_CURVES */
  17338. #ifdef WOLFSSL_CERT_GEN
  17339. /* Make Cert / Sign example for ECC cert and ECC CA */
  17340. static int ecc_test_cert_gen(WC_RNG* rng)
  17341. {
  17342. int ret;
  17343. Cert myCert;
  17344. int certSz;
  17345. size_t bytes;
  17346. word32 idx = 0;
  17347. #ifndef USE_CERT_BUFFERS_256
  17348. XFILE file;
  17349. #endif
  17350. #ifdef WOLFSSL_TEST_CERT
  17351. DecodedCert decode;
  17352. #endif
  17353. #ifdef WOLFSSL_SMALL_STACK
  17354. byte* der = NULL;
  17355. #else
  17356. byte der[FOURK_BUF];
  17357. #endif
  17358. ecc_key caEccKey;
  17359. ecc_key certPubKey;
  17360. XMEMSET(&caEccKey, 0, sizeof(caEccKey));
  17361. XMEMSET(&certPubKey, 0, sizeof(certPubKey));
  17362. #ifdef WOLFSSL_SMALL_STACK
  17363. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17364. if (der == NULL) {
  17365. ERROR_OUT(-9818, exit);
  17366. }
  17367. #endif
  17368. /* Get cert private key */
  17369. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  17370. /* Get Cert Key 384 */
  17371. #ifdef USE_CERT_BUFFERS_256
  17372. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  17373. bytes = sizeof_ca_ecc_key_der_384;
  17374. #else
  17375. file = XFOPEN(eccCaKey384File, "rb");
  17376. if (!file) {
  17377. ERROR_OUT(-9819, exit);
  17378. }
  17379. bytes = XFREAD(der, 1, FOURK_BUF, file);
  17380. XFCLOSE(file);
  17381. (void)eccCaKeyFile;
  17382. #endif /* USE_CERT_BUFFERS_256 */
  17383. #else
  17384. #ifdef USE_CERT_BUFFERS_256
  17385. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  17386. bytes = sizeof_ca_ecc_key_der_256;
  17387. #else
  17388. file = XFOPEN(eccCaKeyFile, "rb");
  17389. if (!file) {
  17390. ERROR_OUT(-9820, exit);
  17391. }
  17392. bytes = XFREAD(der, 1, FOURK_BUF, file);
  17393. XFCLOSE(file);
  17394. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  17395. (void)eccCaKey384File;
  17396. #endif
  17397. #endif /* USE_CERT_BUFFERS_256 */
  17398. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  17399. /* Get CA Key */
  17400. ret = wc_ecc_init_ex(&caEccKey, HEAP_HINT, devId);
  17401. if (ret != 0) {
  17402. ERROR_OUT(-9821, exit);
  17403. }
  17404. ret = wc_EccPrivateKeyDecode(der, &idx, &caEccKey, (word32)bytes);
  17405. if (ret != 0) {
  17406. ERROR_OUT(-9822, exit);
  17407. }
  17408. /* Make a public key */
  17409. ret = wc_ecc_init_ex(&certPubKey, HEAP_HINT, devId);
  17410. if (ret != 0) {
  17411. ERROR_OUT(-9823, exit);
  17412. }
  17413. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, &certPubKey);
  17414. #if defined(WOLFSSL_ASYNC_CRYPT)
  17415. ret = wc_AsyncWait(ret, &certPubKey.asyncDev, WC_ASYNC_FLAG_NONE);
  17416. #endif
  17417. if (ret != 0) {
  17418. ERROR_OUT(-9824, exit);
  17419. }
  17420. TEST_SLEEP();
  17421. /* Setup Certificate */
  17422. if (wc_InitCert(&myCert)) {
  17423. ERROR_OUT(-9825, exit);
  17424. }
  17425. #ifndef NO_SHA256
  17426. myCert.sigType = CTC_SHA256wECDSA;
  17427. #else
  17428. myCert.sigType = CTC_SHAwECDSA;
  17429. #endif
  17430. XMEMCPY(&myCert.subject, &certDefaultName, sizeof(CertName));
  17431. #ifdef WOLFSSL_CERT_EXT
  17432. /* add Policies */
  17433. XSTRNCPY(myCert.certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  17434. CTC_MAX_CERTPOL_SZ);
  17435. XSTRNCPY(myCert.certPolicies[1], "1.2.13025.489.1.113549",
  17436. CTC_MAX_CERTPOL_SZ);
  17437. myCert.certPoliciesNb = 2;
  17438. /* add SKID from the Public Key */
  17439. if (wc_SetSubjectKeyIdFromPublicKey(&myCert, NULL, &certPubKey) != 0) {
  17440. ERROR_OUT(-9826, exit);
  17441. }
  17442. /* add AKID from the Public Key */
  17443. if (wc_SetAuthKeyIdFromPublicKey(&myCert, NULL, &caEccKey) != 0) {
  17444. ERROR_OUT(-9827, exit);
  17445. }
  17446. /* add Key Usage */
  17447. if (wc_SetKeyUsage(&myCert, certKeyUsage) != 0) {
  17448. ERROR_OUT(-9828, exit);
  17449. }
  17450. #endif /* WOLFSSL_CERT_EXT */
  17451. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  17452. #if defined(USE_CERT_BUFFERS_256)
  17453. ret = wc_SetIssuerBuffer(&myCert, ca_ecc_cert_der_384,
  17454. sizeof_ca_ecc_cert_der_384);
  17455. #else
  17456. ret = wc_SetIssuer(&myCert, eccCaCert384File);
  17457. (void)eccCaCertFile;
  17458. #endif
  17459. #else
  17460. #if defined(USE_CERT_BUFFERS_256)
  17461. ret = wc_SetIssuerBuffer(&myCert, ca_ecc_cert_der_256,
  17462. sizeof_ca_ecc_cert_der_256);
  17463. #else
  17464. ret = wc_SetIssuer(&myCert, eccCaCertFile);
  17465. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  17466. (void)eccCaCert384File;
  17467. #endif
  17468. #endif
  17469. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  17470. if (ret < 0) {
  17471. ERROR_OUT(-9829, exit);
  17472. }
  17473. certSz = wc_MakeCert(&myCert, der, FOURK_BUF, NULL, &certPubKey, rng);
  17474. if (certSz < 0) {
  17475. ERROR_OUT(-9830, exit);
  17476. }
  17477. ret = 0;
  17478. do {
  17479. #if defined(WOLFSSL_ASYNC_CRYPT)
  17480. ret = wc_AsyncWait(ret, &caEccKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17481. #endif
  17482. if (ret >= 0) {
  17483. ret = wc_SignCert(myCert.bodySz, myCert.sigType, der,
  17484. FOURK_BUF, NULL, &caEccKey, rng);
  17485. }
  17486. } while (ret == WC_PENDING_E);
  17487. if (ret < 0) {
  17488. ERROR_OUT(-9831, exit);
  17489. }
  17490. certSz = ret;
  17491. TEST_SLEEP();
  17492. #ifdef WOLFSSL_TEST_CERT
  17493. InitDecodedCert(&decode, der, certSz, 0);
  17494. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  17495. if (ret != 0) {
  17496. FreeDecodedCert(&decode);
  17497. ERROR_OUT(-9832, exit);
  17498. }
  17499. FreeDecodedCert(&decode);
  17500. #endif
  17501. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  17502. CERT_TYPE, -6735);
  17503. if (ret != 0) {
  17504. goto exit;
  17505. }
  17506. exit:
  17507. #ifdef WOLFSSL_SMALL_STACK
  17508. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17509. #endif
  17510. wc_ecc_free(&certPubKey);
  17511. wc_ecc_free(&caEccKey);
  17512. return ret;
  17513. }
  17514. #endif /* WOLFSSL_CERT_GEN */
  17515. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17516. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  17517. static int ecc_test_allocator(WC_RNG* rng)
  17518. {
  17519. int ret = 0;
  17520. ecc_key* key;
  17521. key = wc_ecc_key_new(HEAP_HINT);
  17522. if (key == NULL) {
  17523. ERROR_OUT(-9833, exit);
  17524. }
  17525. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  17526. if (ret != 0) {
  17527. ERROR_OUT(-9834, exit);
  17528. }
  17529. exit:
  17530. wc_ecc_key_free(key);
  17531. return ret;
  17532. }
  17533. #endif
  17534. /* ECC Non-blocking tests for Sign and Verify */
  17535. /* Requires SP math and supports P384 or P256 */
  17536. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  17537. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  17538. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  17539. /* Test Data - Random */
  17540. static const uint8_t kMsg[] = {
  17541. 0x69, 0xbc, 0x9f, 0xce, 0x68, 0x17, 0xc2, 0x10, 0xea, 0xfc, 0x10, 0x65, 0x67, 0x52, 0xed, 0x78,
  17542. 0x6e, 0xb8, 0x83, 0x9c, 0x9a, 0xb4, 0x56, 0x0d, 0xc1, 0x0d, 0x1f, 0x78, 0x6e, 0x75, 0xd7, 0xbe,
  17543. 0x92, 0x6b, 0x12, 0xf6, 0x76, 0x60, 0x8e, 0xb1, 0xf4, 0x19, 0x0c, 0x81, 0xe7, 0x54, 0x5e, 0xbc,
  17544. 0xe0, 0xae, 0xc2, 0x7d, 0x1b, 0xc4, 0x6e, 0xec, 0xb1, 0x99, 0x6c, 0xbf, 0x0e, 0x38, 0xa8, 0x01,
  17545. 0xa6, 0x9a, 0x48, 0x12, 0xe4, 0xc9, 0x3b, 0xf0, 0x63, 0x46, 0x15, 0xb4, 0x61, 0xa8, 0x1a, 0x60,
  17546. 0x71, 0x87, 0x98, 0xd7, 0x6f, 0x98, 0x7b, 0x2d, 0xb9, 0x19, 0x1b, 0x21, 0x9c, 0x70, 0x58, 0xe8,
  17547. 0x0d, 0x0f, 0xe9, 0x2d, 0x9a, 0x9a, 0xf1, 0x55, 0xa0, 0x4c, 0xd3, 0x07, 0xbd, 0x97, 0x48, 0xec,
  17548. 0x88, 0x0a, 0xaf, 0xb3, 0x80, 0x78, 0xa4, 0x59, 0x43, 0x57, 0xd3, 0xa7, 0x01, 0x66, 0x0e, 0xfc
  17549. };
  17550. /* ECC Private Key "d" */
  17551. static const uint8_t kPrivKey[] = {
  17552. #ifdef HAVE_ECC384
  17553. /* SECP384R1 */
  17554. /* d */
  17555. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  17556. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  17557. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  17558. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  17559. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  17560. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  17561. #else
  17562. /* SECP256R1 */
  17563. /* d */
  17564. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  17565. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  17566. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  17567. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  17568. #endif
  17569. };
  17570. /* ECC public key Qx/Qy */
  17571. static const uint8_t kPubKey[] = {
  17572. #ifdef HAVE_ECC384
  17573. /* SECP384R1 */
  17574. /* Qx */
  17575. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  17576. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  17577. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  17578. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  17579. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  17580. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  17581. /* Qy */
  17582. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  17583. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  17584. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  17585. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  17586. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  17587. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  17588. #else
  17589. /* SECP256R1 */
  17590. /* Qx */
  17591. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  17592. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  17593. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  17594. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  17595. /* Qy */
  17596. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  17597. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  17598. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  17599. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  17600. #endif
  17601. };
  17602. /* ECC Curve */
  17603. #ifdef HAVE_ECC384
  17604. /* SECP384R1 */
  17605. #define ECC_CURVE_SZ 48
  17606. #define ECC_CURVE_ID ECC_SECP384R1
  17607. #else
  17608. /* SECP256R1 */
  17609. #define ECC_CURVE_SZ 32
  17610. #define ECC_CURVE_ID ECC_SECP256R1
  17611. #endif
  17612. /* Hash Algorithm */
  17613. #if defined(HAVE_ECC384) && defined(WOLFSSL_SHA3)
  17614. #define HASH_DIGEST_SZ WC_SHA3_384_DIGEST_SIZE
  17615. #define HASH_SHA_VER 3
  17616. #define CRYPTO_HASH_FN crypto_sha3_384
  17617. #elif defined(HAVE_ECC384) && defined(WOLFSSL_SHA384)
  17618. #define HASH_DIGEST_SZ WC_SHA384_DIGEST_SIZE
  17619. #define HASH_SHA_VER 2
  17620. #define CRYPTO_HASH_FN crypto_sha2_384
  17621. #elif !defined(NO_SHA256)
  17622. #define HASH_DIGEST_SZ WC_SHA256_DIGEST_SIZE
  17623. #define HASH_SHA_VER 2
  17624. #define CRYPTO_HASH_FN crypto_sha2_256
  17625. #else
  17626. #error test configuration not supported
  17627. #endif
  17628. #if defined(HAVE_ECC384) && defined(WOLFSSL_SHA3)
  17629. /* helper to perform hashing block by block */
  17630. static int crypto_sha3_384(const uint8_t *buf, uint32_t len, uint8_t *hash,
  17631. uint32_t hashSz, uint32_t blkSz)
  17632. {
  17633. int ret;
  17634. uint32_t i = 0, chunk;
  17635. wc_Sha3 sha3;
  17636. /* validate arguments */
  17637. if ((buf == NULL && len > 0) || hash == NULL ||
  17638. hashSz < WC_SHA3_384_DIGEST_SIZE || blkSz == 0)
  17639. {
  17640. return BAD_FUNC_ARG;
  17641. }
  17642. /* Init Sha3_384 structure */
  17643. ret = wc_InitSha3_384(&sha3, NULL, INVALID_DEVID);
  17644. if (ret != 0) {
  17645. return ret;
  17646. }
  17647. while (i < len) {
  17648. chunk = blkSz;
  17649. if ((chunk + i) > len)
  17650. chunk = len - i;
  17651. /* Perform chunked update */
  17652. ret = wc_Sha3_384_Update(&sha3, (buf + i), chunk);
  17653. if (ret != 0) {
  17654. break;
  17655. }
  17656. i += chunk;
  17657. }
  17658. if (ret == 0) {
  17659. /* Get final digest result */
  17660. ret = wc_Sha3_384_Final(&sha3, hash);
  17661. }
  17662. return ret;
  17663. }
  17664. #elif defined(HAVE_ECC384) && defined(WOLFSSL_SHA384)
  17665. /* helper to perform hashing block by block */
  17666. static int crypto_sha2_384(const uint8_t *buf, uint32_t len, uint8_t *hash,
  17667. uint32_t hashSz, uint32_t blkSz)
  17668. {
  17669. int ret;
  17670. uint32_t i = 0, chunk;
  17671. wc_Sha384 sha384;
  17672. /* validate arguments */
  17673. if ((buf == NULL && len > 0) || hash == NULL ||
  17674. hashSz < WC_SHA384_DIGEST_SIZE || blkSz == 0)
  17675. {
  17676. return BAD_FUNC_ARG;
  17677. }
  17678. /* Init Sha384 structure */
  17679. ret = wc_InitSha384(&sha384);
  17680. if (ret != 0) {
  17681. return ret;
  17682. }
  17683. while (i < len) {
  17684. chunk = blkSz;
  17685. if ((chunk + i) > len)
  17686. chunk = len - i;
  17687. /* Perform chunked update */
  17688. ret = wc_Sha384Update(&sha384, (buf + i), chunk);
  17689. if (ret != 0) {
  17690. break;
  17691. }
  17692. i += chunk;
  17693. }
  17694. if (ret == 0) {
  17695. /* Get final digest result */
  17696. ret = wc_Sha384Final(&sha384, hash);
  17697. }
  17698. return ret;
  17699. }
  17700. #elif !defined(NO_SHA256)
  17701. /* helper to perform hashing block by block */
  17702. static int crypto_sha2_256(const uint8_t *buf, uint32_t len, uint8_t *hash,
  17703. uint32_t hashSz, uint32_t blkSz)
  17704. {
  17705. int ret;
  17706. uint32_t i = 0, chunk;
  17707. wc_Sha256 sha256;
  17708. /* validate arguments */
  17709. if ((buf == NULL && len > 0) || hash == NULL ||
  17710. hashSz < WC_SHA256_DIGEST_SIZE || blkSz == 0)
  17711. {
  17712. return BAD_FUNC_ARG;
  17713. }
  17714. /* Init Sha256 structure */
  17715. ret = wc_InitSha256(&sha256);
  17716. if (ret != 0) {
  17717. return ret;
  17718. }
  17719. while (i < len) {
  17720. chunk = blkSz;
  17721. if ((chunk + i) > len)
  17722. chunk = len - i;
  17723. /* Perform chunked update */
  17724. ret = wc_Sha256Update(&sha256, (buf + i), chunk);
  17725. if (ret != 0) {
  17726. break;
  17727. }
  17728. i += chunk;
  17729. }
  17730. if (ret == 0) {
  17731. /* Get final digest result */
  17732. ret = wc_Sha256Final(&sha256, hash);
  17733. }
  17734. return ret;
  17735. }
  17736. #endif
  17737. /* perform verify of signature and hash using public key */
  17738. /* key is public Qx + public Qy */
  17739. /* sig is r + s */
  17740. static int crypto_ecc_verify(const uint8_t *key, uint32_t keySz,
  17741. const uint8_t *hash, uint32_t hashSz, const uint8_t *sig, uint32_t sigSz,
  17742. uint32_t curveSz, int curveId)
  17743. {
  17744. int ret, verify_res = 0, count = 0;
  17745. mp_int r, s;
  17746. ecc_key ecc;
  17747. ecc_nb_ctx_t nb_ctx;
  17748. /* validate arguments */
  17749. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  17750. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  17751. {
  17752. return BAD_FUNC_ARG;
  17753. }
  17754. /* Setup the ECC key */
  17755. ret = wc_ecc_init(&ecc);
  17756. if (ret < 0) {
  17757. return ret;
  17758. }
  17759. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  17760. if (ret != MP_OKAY) {
  17761. wc_ecc_free(&ecc);
  17762. return ret;
  17763. }
  17764. /* Setup the signature r/s variables */
  17765. ret = mp_init(&r);
  17766. if (ret != MP_OKAY) {
  17767. wc_ecc_free(&ecc);
  17768. return ret;
  17769. }
  17770. ret = mp_init(&s);
  17771. if (ret != MP_OKAY) {
  17772. mp_clear(&r);
  17773. wc_ecc_free(&ecc);
  17774. return ret;
  17775. }
  17776. /* Import public key x/y */
  17777. ret = wc_ecc_import_unsigned(
  17778. &ecc,
  17779. (byte*)key, /* Public "x" Coordinate */
  17780. (byte*)(key + curveSz), /* Public "y" Coordinate */
  17781. NULL, /* Private "d" (optional) */
  17782. curveId /* ECC Curve Id */
  17783. );
  17784. /* Make sure it was a public key imported */
  17785. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  17786. ret = ECC_BAD_ARG_E;
  17787. }
  17788. /* Import signature r/s */
  17789. if (ret == 0) {
  17790. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  17791. }
  17792. if (ret == 0) {
  17793. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  17794. }
  17795. /* Verify ECC Signature */
  17796. if (ret == 0) {
  17797. do {
  17798. ret = wc_ecc_verify_hash_ex(
  17799. &r, &s, /* r/s as mp_int */
  17800. hash, hashSz, /* computed hash digest */
  17801. &verify_res, /* verification result 1=success */
  17802. &ecc
  17803. );
  17804. count++;
  17805. /* TODO: Real-time work can be called here */
  17806. } while (ret == FP_WOULDBLOCK);
  17807. #ifdef DEBUG_WOLFSSL
  17808. printf("ECC non-block verify: %d times\n", count);
  17809. #endif
  17810. }
  17811. /* check verify result */
  17812. if (ret == 0 && verify_res == 0) {
  17813. ret = SIG_VERIFY_E;
  17814. }
  17815. mp_clear(&r);
  17816. mp_clear(&s);
  17817. wc_ecc_free(&ecc);
  17818. (void)count;
  17819. return ret;
  17820. }
  17821. /* perform signature operation against hash using private key */
  17822. static int crypto_ecc_sign(const uint8_t *key, uint32_t keySz,
  17823. const uint8_t *hash, uint32_t hashSz, uint8_t *sig, uint32_t* sigSz,
  17824. uint32_t curveSz, int curveId, WC_RNG* rng)
  17825. {
  17826. int ret, count = 0;
  17827. mp_int r, s;
  17828. ecc_key ecc;
  17829. ecc_nb_ctx_t nb_ctx;
  17830. /* validate arguments */
  17831. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  17832. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  17833. {
  17834. return BAD_FUNC_ARG;
  17835. }
  17836. /* Initialize signature result */
  17837. memset(sig, 0, curveSz*2);
  17838. /* Setup the ECC key */
  17839. ret = wc_ecc_init(&ecc);
  17840. if (ret < 0) {
  17841. return ret;
  17842. }
  17843. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  17844. if (ret != MP_OKAY) {
  17845. wc_ecc_free(&ecc);
  17846. return ret;
  17847. }
  17848. /* Setup the signature r/s variables */
  17849. ret = mp_init(&r);
  17850. if (ret != MP_OKAY) {
  17851. wc_ecc_free(&ecc);
  17852. return ret;
  17853. }
  17854. ret = mp_init(&s);
  17855. if (ret != MP_OKAY) {
  17856. mp_clear(&r);
  17857. wc_ecc_free(&ecc);
  17858. return ret;
  17859. }
  17860. /* Import private key "k" */
  17861. ret = wc_ecc_import_private_key_ex(
  17862. key, keySz, /* private key "d" */
  17863. NULL, 0, /* public (optional) */
  17864. &ecc,
  17865. curveId /* ECC Curve Id */
  17866. );
  17867. if (ret == 0) {
  17868. do {
  17869. /* Verify ECC Signature */
  17870. ret = wc_ecc_sign_hash_ex(
  17871. hash, hashSz, /* computed hash digest */
  17872. rng, &ecc, /* random and key context */
  17873. &r, &s /* r/s as mp_int */
  17874. );
  17875. count++;
  17876. /* TODO: Real-time work can be called here */
  17877. } while (ret == FP_WOULDBLOCK);
  17878. #ifdef DEBUG_WOLFSSL
  17879. printf("ECC non-block sign: %d times\n", count);
  17880. #endif
  17881. }
  17882. if (ret == 0) {
  17883. /* export r/s */
  17884. mp_to_unsigned_bin(&r, sig);
  17885. mp_to_unsigned_bin(&s, sig + curveSz);
  17886. }
  17887. mp_clear(&r);
  17888. mp_clear(&s);
  17889. wc_ecc_free(&ecc);
  17890. (void)count;
  17891. return ret;
  17892. }
  17893. static int ecc_test_nonblock(WC_RNG* rng)
  17894. {
  17895. int ret;
  17896. uint8_t hash[HASH_DIGEST_SZ];
  17897. uint8_t sig[ECC_CURVE_SZ*2];
  17898. uint32_t sigSz = sizeof(sig);
  17899. ret = CRYPTO_HASH_FN(
  17900. kMsg, sizeof(kMsg), /* input message */
  17901. hash, sizeof(hash), /* hash digest result */
  17902. 32 /* configurable block / chunk size */
  17903. );
  17904. if (ret == 0) {
  17905. /* Sign hash using private key */
  17906. /* Note: result of an ECC sign varies for each call even with same
  17907. private key and hash. This is because a new random public key is
  17908. used for each operation. */
  17909. ret = crypto_ecc_sign(
  17910. kPrivKey, sizeof(kPrivKey), /* private key */
  17911. hash, sizeof(hash), /* computed hash digest */
  17912. sig, &sigSz, /* signature r/s */
  17913. ECC_CURVE_SZ, /* curve size in bytes */
  17914. ECC_CURVE_ID, /* curve id */
  17915. rng
  17916. );
  17917. }
  17918. if (ret == 0) {
  17919. /* Verify generated signature is valid */
  17920. ret = crypto_ecc_verify(
  17921. kPubKey, sizeof(kPubKey), /* public key point x/y */
  17922. hash, sizeof(hash), /* computed hash digest */
  17923. sig, sigSz, /* signature r/s */
  17924. ECC_CURVE_SZ, /* curve size in bytes */
  17925. ECC_CURVE_ID /* curve id */
  17926. );
  17927. }
  17928. return ret;
  17929. }
  17930. #endif /* WC_ECC_NONBLOCK && WOLFSSL_PUBLIC_MP && HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  17931. int ecc_test(void)
  17932. {
  17933. int ret;
  17934. WC_RNG rng;
  17935. #ifdef WOLFSSL_CERT_EXT
  17936. ret = ecc_decode_test();
  17937. if (ret < 0)
  17938. return ret;
  17939. #endif
  17940. #ifndef HAVE_FIPS
  17941. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  17942. #else
  17943. ret = wc_InitRng(&rng);
  17944. #endif
  17945. if (ret != 0)
  17946. return -9900;
  17947. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  17948. ret = ecc_test_curve(&rng, 14);
  17949. if (ret < 0) {
  17950. goto done;
  17951. }
  17952. #endif /* HAVE_ECC112 */
  17953. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  17954. ret = ecc_test_curve(&rng, 16);
  17955. if (ret < 0) {
  17956. goto done;
  17957. }
  17958. #endif /* HAVE_ECC128 */
  17959. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  17960. ret = ecc_test_curve(&rng, 20);
  17961. if (ret < 0) {
  17962. goto done;
  17963. }
  17964. #endif /* HAVE_ECC160 */
  17965. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  17966. ret = ecc_test_curve(&rng, 24);
  17967. if (ret < 0) {
  17968. goto done;
  17969. }
  17970. #endif /* HAVE_ECC192 */
  17971. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  17972. ret = ecc_test_curve(&rng, 28);
  17973. if (ret < 0) {
  17974. goto done;
  17975. }
  17976. #endif /* HAVE_ECC224 */
  17977. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  17978. ret = ecc_test_curve(&rng, 30);
  17979. if (ret < 0) {
  17980. goto done;
  17981. }
  17982. #endif /* HAVE_ECC239 */
  17983. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  17984. ret = ecc_test_curve(&rng, 32);
  17985. if (ret < 0) {
  17986. goto done;
  17987. }
  17988. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  17989. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  17990. ret = ecc_point_test();
  17991. if (ret < 0) {
  17992. goto done;
  17993. }
  17994. #endif
  17995. ret = ecc_def_curve_test(&rng);
  17996. if (ret < 0) {
  17997. goto done;
  17998. }
  17999. #endif /* !NO_ECC256 */
  18000. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  18001. ret = ecc_test_curve(&rng, 40);
  18002. if (ret < 0) {
  18003. goto done;
  18004. }
  18005. #endif /* HAVE_ECC320 */
  18006. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  18007. ret = ecc_test_curve(&rng, 48);
  18008. if (ret < 0) {
  18009. goto done;
  18010. }
  18011. #endif /* HAVE_ECC384 */
  18012. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  18013. ret = ecc_test_curve(&rng, 64);
  18014. if (ret < 0) {
  18015. goto done;
  18016. }
  18017. #endif /* HAVE_ECC512 */
  18018. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  18019. ret = ecc_test_curve(&rng, 66);
  18020. if (ret < 0) {
  18021. goto done;
  18022. }
  18023. #endif /* HAVE_ECC521 */
  18024. #if defined(WOLFSSL_CUSTOM_CURVES)
  18025. ret = ecc_test_custom_curves(&rng);
  18026. if (ret != 0) {
  18027. goto done;
  18028. }
  18029. #endif
  18030. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K)
  18031. ret = ecc_test_sign_vectors(&rng);
  18032. if (ret != 0) {
  18033. printf("ecc_test_sign_vectors failed! %d\n", ret);
  18034. goto done;
  18035. }
  18036. #endif
  18037. #ifdef HAVE_ECC_CDH
  18038. ret = ecc_test_cdh_vectors(&rng);
  18039. if (ret != 0) {
  18040. printf("ecc_test_cdh_vectors failed! %d\n", ret);
  18041. goto done;
  18042. }
  18043. #endif
  18044. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  18045. !defined(WOLFSSL_STM32_PKA)
  18046. ret = ecc_test_make_pub(&rng);
  18047. if (ret != 0) {
  18048. printf("ecc_test_make_pub failed!: %d\n", ret);
  18049. goto done;
  18050. }
  18051. #elif defined(HAVE_ECC_KEY_IMPORT)
  18052. (void) ecc_test_make_pub;/* for compiler warning */
  18053. #endif
  18054. #ifdef WOLFSSL_CERT_GEN
  18055. ret = ecc_test_cert_gen(&rng);
  18056. if (ret != 0) {
  18057. printf("ecc_test_cert_gen failed!: %d\n", ret);
  18058. goto done;
  18059. }
  18060. #endif
  18061. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  18062. ret = ecc_test_allocator(&rng);
  18063. if (ret != 0) {
  18064. printf("ecc_test_allocator failed!: %d\n", ret);
  18065. }
  18066. #endif
  18067. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  18068. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  18069. ret = ecc_test_nonblock(&rng);
  18070. if (ret != 0) {
  18071. printf("ecc_test_nonblock failed!: %d\n", ret);
  18072. }
  18073. #endif
  18074. done:
  18075. wc_FreeRng(&rng);
  18076. return ret;
  18077. }
  18078. #if defined(HAVE_ECC_ENCRYPT) && defined(WOLFSSL_AES_128)
  18079. int ecc_encrypt_test(void)
  18080. {
  18081. WC_RNG rng;
  18082. int ret = 0;
  18083. ecc_key userA, userB;
  18084. byte msg[48];
  18085. byte plain[48];
  18086. byte out[80];
  18087. word32 outSz = sizeof(out);
  18088. word32 plainSz = sizeof(plain);
  18089. int i;
  18090. ecEncCtx* cliCtx = NULL;
  18091. ecEncCtx* srvCtx = NULL;
  18092. byte cliSalt[EXCHANGE_SALT_SZ];
  18093. byte srvSalt[EXCHANGE_SALT_SZ];
  18094. const byte* tmpSalt;
  18095. byte msg2[48];
  18096. byte plain2[48];
  18097. byte out2[80];
  18098. word32 outSz2 = sizeof(out2);
  18099. word32 plainSz2 = sizeof(plain2);
  18100. #ifndef HAVE_FIPS
  18101. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  18102. #else
  18103. ret = wc_InitRng(&rng);
  18104. #endif
  18105. if (ret != 0)
  18106. return -10000;
  18107. XMEMSET(&userA, 0, sizeof(userA));
  18108. XMEMSET(&userB, 0, sizeof(userB));
  18109. ret = wc_ecc_init_ex(&userA, HEAP_HINT, devId);
  18110. if (ret != 0)
  18111. goto done;
  18112. ret = wc_ecc_init_ex(&userB, HEAP_HINT, devId);
  18113. if (ret != 0)
  18114. goto done;
  18115. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, &userA);
  18116. #if defined(WOLFSSL_ASYNC_CRYPT)
  18117. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_NONE);
  18118. #endif
  18119. if (ret != 0){
  18120. ret = -10001; goto done;
  18121. }
  18122. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, &userB);
  18123. #if defined(WOLFSSL_ASYNC_CRYPT)
  18124. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_NONE);
  18125. #endif
  18126. if (ret != 0){
  18127. ret = -10002; goto done;
  18128. }
  18129. /* set message to incrementing 0,1,2,etc... */
  18130. for (i = 0; i < (int)sizeof(msg); i++)
  18131. msg[i] = i;
  18132. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  18133. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) && \
  18134. !defined(HAVE_SELFTEST)
  18135. ret = wc_ecc_set_rng(&userA, &rng);
  18136. if (ret != 0) {
  18137. ret = -10011; goto done;
  18138. }
  18139. ret = wc_ecc_set_rng(&userB, &rng);
  18140. if (ret != 0) {
  18141. ret = -10012; goto done;
  18142. }
  18143. #endif
  18144. /* encrypt msg to B */
  18145. ret = wc_ecc_encrypt(&userA, &userB, msg, sizeof(msg), out, &outSz, NULL);
  18146. if (ret != 0) {
  18147. ret = -10003; goto done;
  18148. }
  18149. /* decrypt msg from A */
  18150. ret = wc_ecc_decrypt(&userB, &userA, out, outSz, plain, &plainSz, NULL);
  18151. if (ret != 0) {
  18152. ret = -10004; goto done;
  18153. }
  18154. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  18155. ret = -10005; goto done;
  18156. }
  18157. /* let's verify message exchange works, A is client, B is server */
  18158. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  18159. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  18160. if (cliCtx == NULL || srvCtx == NULL) {
  18161. ret = -10006; goto done;
  18162. }
  18163. /* get salt to send to peer */
  18164. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  18165. if (tmpSalt == NULL) {
  18166. ret = -10007; goto done;
  18167. }
  18168. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  18169. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  18170. if (tmpSalt == NULL) {
  18171. ret = -10008; goto done;
  18172. }
  18173. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  18174. /* in actual use, we'd get the peer's salt over the transport */
  18175. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  18176. if (ret != 0)
  18177. goto done;
  18178. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  18179. if (ret != 0)
  18180. goto done;
  18181. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  18182. if (ret != 0)
  18183. goto done;
  18184. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  18185. if (ret != 0)
  18186. goto done;
  18187. /* get encrypted msg (request) to send to B */
  18188. outSz = sizeof(out);
  18189. ret = wc_ecc_encrypt(&userA, &userB, msg, sizeof(msg), out, &outSz,cliCtx);
  18190. if (ret != 0)
  18191. goto done;
  18192. /* B decrypts msg (request) from A */
  18193. plainSz = sizeof(plain);
  18194. ret = wc_ecc_decrypt(&userB, &userA, out, outSz, plain, &plainSz, srvCtx);
  18195. if (ret != 0)
  18196. goto done;
  18197. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  18198. ret = -10009; goto done;
  18199. }
  18200. /* msg2 (response) from B to A */
  18201. for (i = 0; i < (int)sizeof(msg2); i++)
  18202. msg2[i] = i + sizeof(msg2);
  18203. /* get encrypted msg (response) to send to B */
  18204. ret = wc_ecc_encrypt(&userB, &userA, msg2, sizeof(msg2), out2,
  18205. &outSz2, srvCtx);
  18206. if (ret != 0)
  18207. goto done;
  18208. /* A decrypts msg (response) from B */
  18209. ret = wc_ecc_decrypt(&userA, &userB, out2, outSz2, plain2, &plainSz2,
  18210. cliCtx);
  18211. if (ret != 0)
  18212. goto done;
  18213. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  18214. ret = -10010; goto done;
  18215. }
  18216. done:
  18217. /* cleanup */
  18218. wc_ecc_ctx_free(srvCtx);
  18219. wc_ecc_ctx_free(cliCtx);
  18220. wc_ecc_free(&userB);
  18221. wc_ecc_free(&userA);
  18222. wc_FreeRng(&rng);
  18223. return ret;
  18224. }
  18225. #endif /* HAVE_ECC_ENCRYPT */
  18226. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  18227. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256)
  18228. int ecc_test_buffers(void) {
  18229. size_t bytes;
  18230. ecc_key cliKey;
  18231. ecc_key servKey;
  18232. WC_RNG rng;
  18233. word32 idx = 0;
  18234. int ret;
  18235. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  18236. byte in[] = "Everyone gets Friday off. ecc p";
  18237. word32 inLen = (word32)XSTRLEN((char*)in);
  18238. byte out[256];
  18239. byte plain[256];
  18240. int verify = 0;
  18241. word32 x;
  18242. ret = wc_ecc_init_ex(&cliKey, HEAP_HINT, devId);
  18243. if (ret != 0)
  18244. return -10011;
  18245. ret = wc_ecc_init_ex(&servKey, HEAP_HINT, devId);
  18246. if (ret != 0)
  18247. return -10012;
  18248. bytes = (size_t)sizeof_ecc_clikey_der_256;
  18249. /* place client key into ecc_key struct cliKey */
  18250. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, &cliKey,
  18251. (word32)bytes);
  18252. if (ret != 0)
  18253. return -10013;
  18254. idx = 0;
  18255. bytes = (size_t)sizeof_ecc_key_der_256;
  18256. /* place server key into ecc_key struct servKey */
  18257. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, &servKey,
  18258. (word32)bytes);
  18259. if (ret != 0)
  18260. return -10014;
  18261. #ifndef HAVE_FIPS
  18262. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  18263. #else
  18264. ret = wc_InitRng(&rng);
  18265. #endif
  18266. if (ret != 0)
  18267. return -10015;
  18268. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  18269. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) && \
  18270. !defined(HAVE_SELFTEST)
  18271. ret = wc_ecc_set_rng(&cliKey, &rng);
  18272. if (ret != 0) {
  18273. return -10023;
  18274. }
  18275. #endif
  18276. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF)
  18277. {
  18278. word32 y;
  18279. /* test encrypt and decrypt if they're available */
  18280. x = sizeof(out);
  18281. ret = wc_ecc_encrypt(&cliKey, &servKey, in, sizeof(in), out, &x, NULL);
  18282. if (ret < 0)
  18283. return -10016;
  18284. y = sizeof(plain);
  18285. ret = wc_ecc_decrypt(&cliKey, &servKey, out, x, plain, &y, NULL);
  18286. if (ret < 0)
  18287. return -10017;
  18288. if (XMEMCMP(plain, in, inLen))
  18289. return -10018;
  18290. }
  18291. #endif
  18292. x = sizeof(out);
  18293. do {
  18294. #if defined(WOLFSSL_ASYNC_CRYPT)
  18295. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18296. #endif
  18297. if (ret == 0)
  18298. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, &cliKey);
  18299. } while (ret == WC_PENDING_E);
  18300. if (ret < 0)
  18301. return -10019;
  18302. TEST_SLEEP();
  18303. XMEMSET(plain, 0, sizeof(plain));
  18304. do {
  18305. #if defined(WOLFSSL_ASYNC_CRYPT)
  18306. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18307. #endif
  18308. if (ret == 0)
  18309. ret = wc_ecc_verify_hash(out, x, plain, sizeof(plain), &verify,
  18310. &cliKey);
  18311. } while (ret == WC_PENDING_E);
  18312. if (ret < 0)
  18313. return -10020;
  18314. if (XMEMCMP(plain, in, (word32)ret))
  18315. return -10021;
  18316. TEST_SLEEP();
  18317. #ifdef WOLFSSL_CERT_EXT
  18318. idx = 0;
  18319. bytes = sizeof_ecc_clikeypub_der_256;
  18320. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, &cliKey,
  18321. (word32) bytes);
  18322. if (ret != 0)
  18323. return -10022;
  18324. #endif
  18325. wc_ecc_free(&cliKey);
  18326. wc_ecc_free(&servKey);
  18327. wc_FreeRng(&rng);
  18328. return 0;
  18329. }
  18330. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  18331. #endif /* HAVE_ECC */
  18332. #ifdef HAVE_CURVE25519
  18333. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  18334. defined(HAVE_CURVE25519_KEY_IMPORT)
  18335. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  18336. #define X25519_TEST_CNT 5
  18337. #else
  18338. #define X25519_TEST_CNT 1
  18339. #endif
  18340. static int curve25519_overflow_test(void)
  18341. {
  18342. /* secret key for party a */
  18343. byte sa[X25519_TEST_CNT][32] = {
  18344. {
  18345. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  18346. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  18347. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  18348. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  18349. },
  18350. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  18351. {
  18352. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  18353. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  18354. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  18355. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  18356. },
  18357. {
  18358. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  18359. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  18360. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  18361. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  18362. },
  18363. {
  18364. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  18365. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  18366. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  18367. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  18368. },
  18369. {
  18370. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  18371. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  18372. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  18373. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  18374. }
  18375. #endif
  18376. };
  18377. /* public key for party b */
  18378. byte pb[X25519_TEST_CNT][32] = {
  18379. {
  18380. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  18381. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  18382. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  18383. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  18384. },
  18385. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  18386. {
  18387. /* 0xff first byte in original - invalid! */
  18388. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  18389. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  18390. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  18391. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  18392. },
  18393. {
  18394. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  18395. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  18396. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  18397. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  18398. },
  18399. {
  18400. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  18401. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  18402. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  18403. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  18404. },
  18405. {
  18406. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  18407. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  18408. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  18409. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  18410. }
  18411. #endif
  18412. };
  18413. /* expected shared key */
  18414. byte ss[X25519_TEST_CNT][32] = {
  18415. {
  18416. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  18417. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  18418. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  18419. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  18420. },
  18421. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  18422. {
  18423. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  18424. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  18425. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  18426. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  18427. },
  18428. {
  18429. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18430. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18431. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18432. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  18433. },
  18434. {
  18435. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18436. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18437. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18438. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  18439. },
  18440. {
  18441. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18442. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18443. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18444. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  18445. }
  18446. #endif
  18447. };
  18448. int i;
  18449. word32 y;
  18450. byte shared[32];
  18451. curve25519_key userA;
  18452. wc_curve25519_init(&userA);
  18453. for (i = 0; i < X25519_TEST_CNT; i++) {
  18454. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  18455. sizeof(pb[i]), &userA) != 0)
  18456. return -10100 - i;
  18457. /* test against known test vector */
  18458. XMEMSET(shared, 0, sizeof(shared));
  18459. y = sizeof(shared);
  18460. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0)
  18461. return -10110 - i;
  18462. if (XMEMCMP(ss[i], shared, y))
  18463. return -10120 - i;
  18464. }
  18465. return 0;
  18466. }
  18467. /* Test the wc_curve25519_check_public API.
  18468. *
  18469. * returns 0 on success and -ve on failure.
  18470. */
  18471. static int curve25519_check_public_test(void)
  18472. {
  18473. /* Little-endian values that will fail */
  18474. byte fail_le[][CURVE25519_KEYSIZE] = {
  18475. {
  18476. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18477. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18478. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18479. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  18480. },
  18481. {
  18482. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18483. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18484. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18485. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  18486. },
  18487. {
  18488. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18489. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18490. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18491. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  18492. },
  18493. };
  18494. /* Big-endian values that will fail */
  18495. byte fail_be[][CURVE25519_KEYSIZE] = {
  18496. {
  18497. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18498. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18499. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18500. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  18501. },
  18502. {
  18503. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18504. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18505. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18506. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  18507. },
  18508. {
  18509. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18510. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18511. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18512. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  18513. },
  18514. };
  18515. /* Good or valid public value */
  18516. byte good[CURVE25519_KEYSIZE] = {
  18517. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18518. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18519. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18520. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  18521. };
  18522. int i;
  18523. /* Parameter checks */
  18524. /* NULL pointer */
  18525. if (wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN) !=
  18526. BAD_FUNC_ARG) {
  18527. return -10200;
  18528. }
  18529. if (wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN) !=
  18530. BAD_FUNC_ARG) {
  18531. return -10201;
  18532. }
  18533. /* Length of 0 treated differently to other invalid lengths for TLS */
  18534. if (wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN) != BUFFER_E)
  18535. return -10202;
  18536. if (wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN) != BUFFER_E)
  18537. return -10203;
  18538. /* Length not CURVE25519_KEYSIZE */
  18539. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  18540. if (i == CURVE25519_KEYSIZE)
  18541. continue;
  18542. if (wc_curve25519_check_public(good, i, EC25519_LITTLE_ENDIAN) !=
  18543. ECC_BAD_ARG_E) {
  18544. return -10204 - i;
  18545. }
  18546. if (wc_curve25519_check_public(good, i, EC25519_BIG_ENDIAN) !=
  18547. ECC_BAD_ARG_E) {
  18548. return -10214 - i;
  18549. }
  18550. }
  18551. /* Little-endian fail cases */
  18552. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  18553. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  18554. EC25519_LITTLE_ENDIAN) == 0) {
  18555. return -10224 - i;
  18556. }
  18557. }
  18558. /* Big-endian fail cases */
  18559. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  18560. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  18561. EC25519_BIG_ENDIAN) == 0) {
  18562. return -10234 - i;
  18563. }
  18564. }
  18565. /* Check a valid public value works! */
  18566. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  18567. EC25519_LITTLE_ENDIAN) != 0) {
  18568. return -10244;
  18569. }
  18570. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  18571. EC25519_BIG_ENDIAN) != 0) {
  18572. return -10245;
  18573. }
  18574. return 0;
  18575. }
  18576. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  18577. int curve25519_test(void)
  18578. {
  18579. WC_RNG rng;
  18580. int ret;
  18581. #ifdef HAVE_CURVE25519_SHARED_SECRET
  18582. byte sharedA[32];
  18583. byte sharedB[32];
  18584. word32 y;
  18585. #endif
  18586. #ifdef HAVE_CURVE25519_KEY_EXPORT
  18587. byte exportBuf[32];
  18588. #endif
  18589. word32 x = 0;
  18590. curve25519_key userA, userB, pubKey;
  18591. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  18592. defined(HAVE_CURVE25519_KEY_IMPORT)
  18593. /* test vectors from
  18594. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  18595. */
  18596. /* secret key for party a */
  18597. byte sa[] = {
  18598. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  18599. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  18600. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  18601. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  18602. };
  18603. /* public key for party a */
  18604. byte pa[] = {
  18605. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  18606. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  18607. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  18608. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  18609. };
  18610. /* secret key for party b */
  18611. byte sb[] = {
  18612. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  18613. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  18614. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  18615. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  18616. };
  18617. /* public key for party b */
  18618. byte pb[] = {
  18619. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  18620. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  18621. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  18622. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  18623. };
  18624. /* expected shared key */
  18625. byte ss[] = {
  18626. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  18627. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  18628. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  18629. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  18630. };
  18631. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  18632. (void)x;
  18633. #ifndef HAVE_FIPS
  18634. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  18635. #else
  18636. ret = wc_InitRng(&rng);
  18637. #endif
  18638. if (ret != 0)
  18639. return -10300;
  18640. wc_curve25519_init(&userA);
  18641. wc_curve25519_init(&userB);
  18642. wc_curve25519_init(&pubKey);
  18643. /* make curve25519 keys */
  18644. if (wc_curve25519_make_key(&rng, 32, &userA) != 0)
  18645. return -10301;
  18646. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  18647. return -10302;
  18648. #ifdef HAVE_CURVE25519_SHARED_SECRET
  18649. /* find shared secret key */
  18650. x = sizeof(sharedA);
  18651. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  18652. return -10303;
  18653. y = sizeof(sharedB);
  18654. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  18655. return -10304;
  18656. /* compare shared secret keys to test they are the same */
  18657. if (y != x)
  18658. return -10305;
  18659. if (XMEMCMP(sharedA, sharedB, x))
  18660. return -10306;
  18661. #endif
  18662. #ifdef HAVE_CURVE25519_KEY_EXPORT
  18663. /* export a public key and import it for another user */
  18664. x = sizeof(exportBuf);
  18665. if (wc_curve25519_export_public(&userA, exportBuf, &x) != 0)
  18666. return -10307;
  18667. #ifdef HAVE_CURVE25519_KEY_IMPORT
  18668. if (wc_curve25519_import_public(exportBuf, x, &pubKey) != 0)
  18669. return -10308;
  18670. #endif
  18671. #endif
  18672. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  18673. defined(HAVE_CURVE25519_KEY_IMPORT)
  18674. /* test shared key after importing a public key */
  18675. XMEMSET(sharedB, 0, sizeof(sharedB));
  18676. y = sizeof(sharedB);
  18677. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  18678. return -10309;
  18679. if (XMEMCMP(sharedA, sharedB, y))
  18680. return -10310;
  18681. /* import RFC test vectors and compare shared key */
  18682. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  18683. != 0)
  18684. return -10311;
  18685. if (wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  18686. != 0)
  18687. return -10312;
  18688. /* test against known test vector */
  18689. XMEMSET(sharedB, 0, sizeof(sharedB));
  18690. y = sizeof(sharedB);
  18691. if (wc_curve25519_shared_secret(&userA, &userB, sharedB, &y) != 0)
  18692. return -10313;
  18693. if (XMEMCMP(ss, sharedB, y))
  18694. return -10314;
  18695. /* test swapping roles of keys and generating same shared key */
  18696. XMEMSET(sharedB, 0, sizeof(sharedB));
  18697. y = sizeof(sharedB);
  18698. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  18699. return -10315;
  18700. if (XMEMCMP(ss, sharedB, y))
  18701. return -10316;
  18702. /* test with 1 generated key and 1 from known test vector */
  18703. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  18704. != 0)
  18705. return -10317;
  18706. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  18707. return -10318;
  18708. x = sizeof(sharedA);
  18709. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  18710. return -10319;
  18711. y = sizeof(sharedB);
  18712. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  18713. return -10320;
  18714. /* compare shared secret keys to test they are the same */
  18715. if (y != x)
  18716. return -10321;
  18717. if (XMEMCMP(sharedA, sharedB, x))
  18718. return -10322;
  18719. ret = curve25519_overflow_test();
  18720. if (ret != 0)
  18721. return ret;
  18722. ret = curve25519_check_public_test();
  18723. if (ret != 0)
  18724. return ret;
  18725. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  18726. /* clean up keys when done */
  18727. wc_curve25519_free(&pubKey);
  18728. wc_curve25519_free(&userB);
  18729. wc_curve25519_free(&userA);
  18730. wc_FreeRng(&rng);
  18731. return 0;
  18732. }
  18733. #endif /* HAVE_CURVE25519 */
  18734. #ifdef HAVE_ED25519
  18735. #ifdef WOLFSSL_TEST_CERT
  18736. static int ed25519_test_cert(void)
  18737. {
  18738. DecodedCert cert[2];
  18739. DecodedCert* serverCert = NULL;
  18740. DecodedCert* caCert = NULL;
  18741. #ifdef HAVE_ED25519_VERIFY
  18742. ed25519_key key;
  18743. ed25519_key* pubKey = NULL;
  18744. int verify;
  18745. #endif /* HAVE_ED25519_VERIFY */
  18746. int ret;
  18747. byte* tmp;
  18748. size_t bytes;
  18749. XFILE file;
  18750. tmp = XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18751. if (tmp == NULL) {
  18752. ERROR_OUT(-10323, done);
  18753. }
  18754. #ifdef USE_CERT_BUFFERS_256
  18755. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  18756. bytes = sizeof_ca_ed25519_cert;
  18757. #elif !defined(NO_FILESYSTEM)
  18758. file = XFOPEN(caEd25519Cert, "rb");
  18759. if (file == NULL) {
  18760. ERROR_OUT(-10324, done);
  18761. }
  18762. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  18763. XFCLOSE(file);
  18764. #else
  18765. /* No certificate to use. */
  18766. ERROR_OUT(-10325, done);
  18767. #endif
  18768. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  18769. caCert = &cert[0];
  18770. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  18771. if (ret != 0) {
  18772. ERROR_OUT(-10326, done);
  18773. }
  18774. #ifdef USE_CERT_BUFFERS_256
  18775. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  18776. bytes = sizeof_server_ed25519_cert;
  18777. #elif !defined(NO_FILESYSTEM)
  18778. file = XFOPEN(serverEd25519Cert, "rb");
  18779. if (file == NULL) {
  18780. ERROR_OUT(-10327, done);
  18781. }
  18782. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  18783. XFCLOSE(file);
  18784. #else
  18785. /* No certificate to use. */
  18786. ERROR_OUT(-10328, done);
  18787. #endif
  18788. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  18789. serverCert = &cert[1];
  18790. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  18791. if (ret != 0) {
  18792. ERROR_OUT(-10329, done);
  18793. }
  18794. #ifdef HAVE_ED25519_VERIFY
  18795. ret = wc_ed25519_init(&key);
  18796. if (ret < 0) {
  18797. ERROR_OUT(-10330, done);
  18798. }
  18799. pubKey = &key;
  18800. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  18801. pubKey);
  18802. if (ret < 0) {
  18803. ERROR_OUT(-10331, done);
  18804. }
  18805. if (wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  18806. serverCert->source + serverCert->certBegin,
  18807. serverCert->sigIndex - serverCert->certBegin,
  18808. &verify, pubKey) < 0 || verify != 1) {
  18809. ERROR_OUT(-10332, done);
  18810. }
  18811. #endif /* HAVE_ED25519_VERIFY */
  18812. done:
  18813. if (tmp != NULL)
  18814. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18815. #ifdef HAVE_ED25519_VERIFY
  18816. wc_ed25519_free(pubKey);
  18817. #endif /* HAVE_ED25519_VERIFY */
  18818. if (caCert != NULL)
  18819. FreeDecodedCert(caCert);
  18820. if (serverCert != NULL)
  18821. FreeDecodedCert(serverCert);
  18822. return ret;
  18823. }
  18824. static int ed25519_test_make_cert(void)
  18825. {
  18826. WC_RNG rng;
  18827. Cert cert;
  18828. DecodedCert decode;
  18829. ed25519_key key;
  18830. ed25519_key* privKey = NULL;
  18831. int ret = 0;
  18832. byte* tmp = NULL;
  18833. wc_InitCert(&cert);
  18834. #ifndef HAVE_FIPS
  18835. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  18836. #else
  18837. ret = wc_InitRng(&rng);
  18838. #endif
  18839. if (ret != 0)
  18840. return -10333;
  18841. wc_ed25519_init(&key);
  18842. privKey = &key;
  18843. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  18844. cert.daysValid = 365 * 2;
  18845. cert.selfSigned = 1;
  18846. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  18847. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  18848. cert.isCA = 0;
  18849. #ifdef WOLFSSL_CERT_EXT
  18850. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  18851. if (ret < 0) {
  18852. ERROR_OUT(-10334, done);
  18853. }
  18854. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  18855. if (ret < 0) {
  18856. ERROR_OUT(-10335, done);
  18857. }
  18858. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  18859. if (ret < 0) {
  18860. ERROR_OUT(-10336, done);
  18861. }
  18862. #endif
  18863. tmp = XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18864. if (tmp == NULL) {
  18865. ERROR_OUT(-10337, done);
  18866. }
  18867. cert.sigType = CTC_ED25519;
  18868. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  18869. if (ret < 0) {
  18870. ERROR_OUT(-10338, done);
  18871. }
  18872. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  18873. ED25519_TYPE, privKey, &rng);
  18874. if (ret < 0) {
  18875. ERROR_OUT(-10339, done);
  18876. }
  18877. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  18878. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  18879. FreeDecodedCert(&decode);
  18880. if (ret != 0) {
  18881. ERROR_OUT(-10340, done);
  18882. }
  18883. done:
  18884. if (tmp != NULL)
  18885. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18886. wc_ed25519_free(privKey);
  18887. wc_FreeRng(&rng);
  18888. return ret;
  18889. }
  18890. #endif /* WOLFSSL_TEST_CERT */
  18891. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  18892. defined(HAVE_ED25519_KEY_IMPORT)
  18893. static int ed25519ctx_test(void)
  18894. {
  18895. byte out[ED25519_SIG_SIZE];
  18896. word32 outlen;
  18897. #ifdef HAVE_ED25519_VERIFY
  18898. int verify;
  18899. #endif /* HAVE_ED25519_VERIFY */
  18900. ed25519_key key;
  18901. static const byte sKeyCtx[] = {
  18902. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  18903. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  18904. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  18905. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  18906. };
  18907. static const byte pKeyCtx[] = {
  18908. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  18909. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  18910. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  18911. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  18912. };
  18913. static const byte sigCtx1[] = {
  18914. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  18915. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  18916. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  18917. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  18918. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  18919. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  18920. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  18921. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  18922. };
  18923. static const byte sigCtx2[] = {
  18924. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  18925. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  18926. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  18927. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  18928. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  18929. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  18930. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  18931. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  18932. };
  18933. static const byte msgCtx[] = {
  18934. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  18935. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  18936. };
  18937. static const byte contextCtx[] = {
  18938. 0x66,0x6f,0x6f
  18939. };
  18940. outlen = sizeof(out);
  18941. XMEMSET(out, 0, sizeof(out));
  18942. if (wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  18943. sizeof(pKeyCtx), &key) != 0)
  18944. return -10400;
  18945. if (wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  18946. contextCtx, sizeof(contextCtx)) != 0)
  18947. return -10401;
  18948. if (XMEMCMP(out, sigCtx1, 64))
  18949. return -10402;
  18950. #if defined(HAVE_ED25519_VERIFY)
  18951. /* test verify on good msg */
  18952. if (wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify,
  18953. &key, contextCtx, sizeof(contextCtx)) != 0 ||
  18954. verify != 1)
  18955. return -10403;
  18956. #endif
  18957. if (wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key, NULL,
  18958. 0) != 0)
  18959. return -10404;
  18960. if (XMEMCMP(out, sigCtx2, 64))
  18961. return -10405;
  18962. #if defined(HAVE_ED25519_VERIFY)
  18963. /* test verify on good msg */
  18964. if (wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify,
  18965. &key, NULL, 0) != 0 || verify != 1)
  18966. return -10406;
  18967. #endif
  18968. wc_ed25519_free(&key);
  18969. return 0;
  18970. }
  18971. static int ed25519ph_test(void)
  18972. {
  18973. byte out[ED25519_SIG_SIZE];
  18974. word32 outlen;
  18975. #ifdef HAVE_ED25519_VERIFY
  18976. int verify;
  18977. #endif /* HAVE_ED25519_VERIFY */
  18978. ed25519_key key;
  18979. static const byte sKeyPh[] = {
  18980. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  18981. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  18982. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  18983. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  18984. };
  18985. static const byte pKeyPh[] = {
  18986. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  18987. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  18988. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  18989. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  18990. };
  18991. static const byte sigPh1[] = {
  18992. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  18993. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  18994. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  18995. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  18996. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  18997. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  18998. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  18999. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  19000. };
  19001. static const byte sigPh2[] = {
  19002. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  19003. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  19004. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  19005. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  19006. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  19007. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  19008. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  19009. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  19010. };
  19011. static const byte msgPh[] = {
  19012. 0x61,0x62,0x63
  19013. };
  19014. /* SHA-512 hash of msgPh */
  19015. static const byte hashPh[] = {
  19016. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  19017. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  19018. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  19019. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  19020. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  19021. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  19022. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  19023. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  19024. };
  19025. static const byte contextPh2[] = {
  19026. 0x66,0x6f,0x6f
  19027. };
  19028. outlen = sizeof(out);
  19029. XMEMSET(out, 0, sizeof(out));
  19030. if (wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  19031. sizeof(pKeyPh), &key) != 0) {
  19032. return -10500;
  19033. }
  19034. if (wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  19035. 0) != 0) {
  19036. return -10501;
  19037. }
  19038. if (XMEMCMP(out, sigPh1, 64))
  19039. return -10502;
  19040. #if defined(HAVE_ED25519_VERIFY)
  19041. /* test verify on good msg */
  19042. if (wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  19043. &key, NULL, 0) != 0 ||
  19044. verify != 1) {
  19045. return -10503;
  19046. }
  19047. #endif
  19048. if (wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  19049. contextPh2, sizeof(contextPh2)) != 0) {
  19050. return -10504;
  19051. }
  19052. if (XMEMCMP(out, sigPh2, 64))
  19053. return -10505;
  19054. #if defined(HAVE_ED25519_VERIFY)
  19055. /* test verify on good msg */
  19056. if (wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  19057. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  19058. verify != 1) {
  19059. return -10506;
  19060. }
  19061. #endif
  19062. if (wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  19063. 0) != 0) {
  19064. return -10507;
  19065. }
  19066. if (XMEMCMP(out, sigPh1, 64))
  19067. return -10508;
  19068. #if defined(HAVE_ED25519_VERIFY)
  19069. if (wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  19070. &key, NULL, 0) != 0 ||
  19071. verify != 1) {
  19072. return -10509;
  19073. }
  19074. #endif
  19075. if (wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  19076. contextPh2, sizeof(contextPh2)) != 0) {
  19077. return -10510;
  19078. }
  19079. if (XMEMCMP(out, sigPh2, 64))
  19080. return -10511;
  19081. #if defined(HAVE_ED25519_VERIFY)
  19082. if (wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  19083. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  19084. verify != 1) {
  19085. return -10512;
  19086. }
  19087. #endif
  19088. wc_ed25519_free(&key);
  19089. return 0;
  19090. }
  19091. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  19092. int ed25519_test(void)
  19093. {
  19094. int ret;
  19095. WC_RNG rng;
  19096. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  19097. defined(HAVE_ED25519_KEY_IMPORT)
  19098. byte out[ED25519_SIG_SIZE];
  19099. byte exportPKey[ED25519_KEY_SIZE];
  19100. byte exportSKey[ED25519_KEY_SIZE];
  19101. word32 exportPSz;
  19102. word32 exportSSz;
  19103. int i;
  19104. word32 outlen;
  19105. #ifdef HAVE_ED25519_VERIFY
  19106. int verify;
  19107. #endif /* HAVE_ED25519_VERIFY */
  19108. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  19109. word32 keySz, sigSz;
  19110. ed25519_key key;
  19111. ed25519_key key2;
  19112. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  19113. defined(HAVE_ED25519_KEY_IMPORT)
  19114. /* test vectors from
  19115. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  19116. */
  19117. static const byte sKey1[] = {
  19118. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  19119. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  19120. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  19121. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  19122. };
  19123. static const byte sKey2[] = {
  19124. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  19125. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  19126. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  19127. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  19128. };
  19129. static const byte sKey3[] = {
  19130. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  19131. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  19132. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  19133. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  19134. };
  19135. /* uncompressed test */
  19136. static const byte sKey4[] = {
  19137. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  19138. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  19139. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  19140. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  19141. };
  19142. /* compressed prefix test */
  19143. static const byte sKey5[] = {
  19144. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  19145. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  19146. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  19147. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  19148. };
  19149. static const byte sKey6[] = {
  19150. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  19151. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  19152. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  19153. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  19154. };
  19155. static const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  19156. static const byte pKey1[] = {
  19157. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  19158. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  19159. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  19160. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  19161. };
  19162. static const byte pKey2[] = {
  19163. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  19164. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  19165. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  19166. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  19167. };
  19168. static const byte pKey3[] = {
  19169. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  19170. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  19171. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  19172. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  19173. };
  19174. /* uncompressed test */
  19175. static const byte pKey4[] = {
  19176. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  19177. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  19178. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  19179. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  19180. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  19181. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  19182. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  19183. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  19184. 0xd7
  19185. };
  19186. /* compressed prefix */
  19187. static const byte pKey5[] = {
  19188. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  19189. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  19190. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  19191. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  19192. };
  19193. static const byte pKey6[] = {
  19194. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  19195. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  19196. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  19197. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  19198. };
  19199. static const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  19200. static const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  19201. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  19202. static const byte sig1[] = {
  19203. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  19204. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  19205. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  19206. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  19207. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  19208. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  19209. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  19210. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  19211. };
  19212. static const byte sig2[] = {
  19213. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  19214. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  19215. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  19216. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  19217. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  19218. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  19219. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  19220. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  19221. };
  19222. static const byte sig3[] = {
  19223. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  19224. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  19225. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  19226. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  19227. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  19228. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  19229. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  19230. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  19231. };
  19232. /* uncompressed test */
  19233. static const byte sig4[] = {
  19234. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  19235. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  19236. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  19237. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  19238. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  19239. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  19240. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  19241. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  19242. };
  19243. /* compressed prefix */
  19244. static const byte sig5[] = {
  19245. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  19246. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  19247. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  19248. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  19249. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  19250. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  19251. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  19252. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  19253. };
  19254. static const byte sig6[] = {
  19255. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  19256. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  19257. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  19258. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  19259. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  19260. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  19261. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  19262. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  19263. };
  19264. static const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  19265. static const byte msg1[] = {0x0 };
  19266. static const byte msg2[] = {0x72};
  19267. static const byte msg3[] = {0xAF,0x82};
  19268. /* test of a 1024 byte long message */
  19269. static const byte msg4[] = {
  19270. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  19271. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  19272. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  19273. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  19274. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  19275. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  19276. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  19277. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  19278. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  19279. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  19280. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  19281. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  19282. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  19283. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  19284. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  19285. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  19286. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  19287. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  19288. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  19289. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  19290. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  19291. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  19292. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  19293. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  19294. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  19295. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  19296. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  19297. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  19298. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  19299. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  19300. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  19301. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  19302. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  19303. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  19304. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  19305. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  19306. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  19307. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  19308. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  19309. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  19310. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  19311. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  19312. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  19313. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  19314. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  19315. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  19316. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  19317. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  19318. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  19319. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  19320. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  19321. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  19322. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  19323. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  19324. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  19325. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  19326. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  19327. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  19328. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  19329. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  19330. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  19331. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  19332. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  19333. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  19334. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  19335. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  19336. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  19337. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  19338. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  19339. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  19340. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  19341. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  19342. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  19343. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  19344. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  19345. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  19346. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  19347. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  19348. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  19349. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  19350. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  19351. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  19352. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  19353. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  19354. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  19355. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  19356. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  19357. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  19358. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  19359. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  19360. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  19361. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  19362. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  19363. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  19364. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  19365. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  19366. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  19367. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  19368. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  19369. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  19370. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  19371. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  19372. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  19373. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  19374. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  19375. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  19376. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  19377. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  19378. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  19379. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  19380. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  19381. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  19382. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  19383. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  19384. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  19385. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  19386. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  19387. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  19388. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  19389. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  19390. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  19391. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  19392. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  19393. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  19394. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  19395. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  19396. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  19397. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  19398. };
  19399. static const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  19400. static const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  19401. sizeof(msg2),
  19402. sizeof(msg3),
  19403. 0 /*sizeof(msg1)*/,
  19404. 0 /*sizeof(msg1)*/,
  19405. sizeof(msg4)
  19406. };
  19407. #ifndef NO_ASN
  19408. static byte privateEd25519[] = {
  19409. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  19410. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  19411. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  19412. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  19413. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  19414. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  19415. };
  19416. static byte publicEd25519[] = {
  19417. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  19418. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  19419. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  19420. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  19421. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  19422. 0xf7,0x07,0x51,0x1a
  19423. };
  19424. static byte privPubEd25519[] = {
  19425. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  19426. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  19427. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  19428. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  19429. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  19430. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  19431. 0xa1,0x22,0x04,0x20,0xd7,0x5a,0x98,0x01,
  19432. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  19433. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  19434. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  19435. 0xf7,0x07,0x51,0x1a
  19436. };
  19437. word32 idx;
  19438. ed25519_key key3;
  19439. #endif /* NO_ASN */
  19440. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  19441. /* create ed25519 keys */
  19442. #ifndef HAVE_FIPS
  19443. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  19444. #else
  19445. ret = wc_InitRng(&rng);
  19446. #endif
  19447. if (ret != 0)
  19448. return -10600;
  19449. wc_ed25519_init(&key);
  19450. wc_ed25519_init(&key2);
  19451. #ifndef NO_ASN
  19452. wc_ed25519_init(&key3);
  19453. #endif
  19454. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19455. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  19456. /* helper functions for signature and key size */
  19457. keySz = wc_ed25519_size(&key);
  19458. sigSz = wc_ed25519_sig_size(&key);
  19459. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  19460. defined(HAVE_ED25519_KEY_IMPORT)
  19461. for (i = 0; i < 6; i++) {
  19462. outlen = sizeof(out);
  19463. XMEMSET(out, 0, sizeof(out));
  19464. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  19465. pKeySz[i], &key) != 0)
  19466. return -10601 - i;
  19467. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  19468. return -10611 - i;
  19469. if (XMEMCMP(out, sigs[i], 64))
  19470. return -10621 - i;
  19471. #if defined(HAVE_ED25519_VERIFY)
  19472. /* test verify on good msg */
  19473. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  19474. &key) != 0 || verify != 1)
  19475. return -10631 - i;
  19476. /* test verify on bad msg */
  19477. out[outlen-1] = out[outlen-1] + 1;
  19478. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  19479. &key) == 0 || verify == 1)
  19480. return -10641 - i;
  19481. #endif /* HAVE_ED25519_VERIFY */
  19482. /* test api for import/exporting keys */
  19483. exportPSz = sizeof(exportPKey);
  19484. exportSSz = sizeof(exportSKey);
  19485. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  19486. return -10651 - i;
  19487. if (wc_ed25519_import_public(exportPKey, exportPSz, &key2) != 0)
  19488. return -10661 - i;
  19489. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  19490. return -10671 - i;
  19491. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  19492. exportPKey, exportPSz, &key2) != 0)
  19493. return -10681 - i;
  19494. /* clear "out" buffer and test sign with imported keys */
  19495. outlen = sizeof(out);
  19496. XMEMSET(out, 0, sizeof(out));
  19497. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  19498. return -10691 - i;
  19499. #if defined(HAVE_ED25519_VERIFY)
  19500. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  19501. &key2) != 0 || verify != 1)
  19502. return -10701 - i;
  19503. if (XMEMCMP(out, sigs[i], 64))
  19504. return -10711 - i;
  19505. #endif /* HAVE_ED25519_VERIFY */
  19506. }
  19507. ret = ed25519ctx_test();
  19508. if (ret != 0)
  19509. return ret;
  19510. ret = ed25519ph_test();
  19511. if (ret != 0)
  19512. return ret;
  19513. #ifndef NO_ASN
  19514. /* Try ASN.1 encoded private-only key and public key. */
  19515. idx = 0;
  19516. if (wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  19517. sizeof(privateEd25519)) != 0)
  19518. return -10721 - i;
  19519. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3)
  19520. != BAD_FUNC_ARG)
  19521. return -10731 - i;
  19522. idx = 0;
  19523. if (wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  19524. sizeof(publicEd25519)) != 0)
  19525. return -10741 - i;
  19526. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  19527. return -10751 - i;
  19528. if (XMEMCMP(out, sigs[0], 64))
  19529. return -10761 - i;
  19530. #if defined(HAVE_ED25519_VERIFY)
  19531. /* test verify on good msg */
  19532. if (wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3)
  19533. != 0 || verify != 1)
  19534. return -10771 - i;
  19535. #endif /* HAVE_ED25519_VERIFY */
  19536. wc_ed25519_free(&key3);
  19537. wc_ed25519_init(&key3);
  19538. idx = 0;
  19539. if (wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  19540. sizeof(privPubEd25519)) != 0)
  19541. return -10781 - i;
  19542. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  19543. return -10791 - i;
  19544. if (XMEMCMP(out, sigs[0], 64))
  19545. return -10801 - i;
  19546. wc_ed25519_free(&key3);
  19547. #endif /* NO_ASN */
  19548. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  19549. /* clean up keys when done */
  19550. wc_ed25519_free(&key);
  19551. wc_ed25519_free(&key2);
  19552. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  19553. wc_FreeRng(&rng);
  19554. #endif
  19555. /* hush warnings of unused keySz and sigSz */
  19556. (void)keySz;
  19557. (void)sigSz;
  19558. #ifdef WOLFSSL_TEST_CERT
  19559. ret = ed25519_test_cert();
  19560. if (ret < 0)
  19561. return ret;
  19562. #ifdef WOLFSSL_CERT_GEN
  19563. ret = ed25519_test_make_cert();
  19564. if (ret < 0)
  19565. return ret;
  19566. #endif /* WOLFSSL_CERT_GEN */
  19567. #endif /* WOLFSSL_TEST_CERT */
  19568. return 0;
  19569. }
  19570. #endif /* HAVE_ED25519 */
  19571. #ifdef HAVE_CURVE448
  19572. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  19573. defined(HAVE_CURVE448_KEY_IMPORT)
  19574. /* Test the wc_curve448_check_public API.
  19575. *
  19576. * returns 0 on success and -ve on failure.
  19577. */
  19578. static int curve448_check_public_test(void)
  19579. {
  19580. /* Little-endian values that will fail */
  19581. byte fail_le[][CURVE448_KEY_SIZE] = {
  19582. {
  19583. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19584. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19585. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19586. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19587. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19588. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19589. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  19590. },
  19591. {
  19592. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19593. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19594. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19595. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19596. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19597. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19598. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  19599. },
  19600. };
  19601. /* Big-endian values that will fail */
  19602. byte fail_be[][CURVE448_KEY_SIZE] = {
  19603. {
  19604. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19605. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19606. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19607. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19608. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19609. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19610. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  19611. },
  19612. {
  19613. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19614. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19615. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19616. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19617. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19618. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19619. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  19620. },
  19621. };
  19622. /* Good or valid public value */
  19623. byte good[CURVE448_KEY_SIZE] = {
  19624. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19625. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19626. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19627. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19628. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19629. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  19630. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  19631. };
  19632. int i;
  19633. /* Parameter checks */
  19634. /* NULL pointer */
  19635. if (wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN) !=
  19636. BAD_FUNC_ARG) {
  19637. return -10900;
  19638. }
  19639. if (wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN) != BAD_FUNC_ARG) {
  19640. return -10901;
  19641. }
  19642. /* Length of 0 treated differently to other invalid lengths for TLS */
  19643. if (wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN) != BUFFER_E)
  19644. return -10902;
  19645. if (wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN) != BUFFER_E)
  19646. return -10903;
  19647. /* Length not CURVE448_KEY_SIZE */
  19648. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  19649. if (i == CURVE448_KEY_SIZE)
  19650. continue;
  19651. if (wc_curve448_check_public(good, i, EC448_LITTLE_ENDIAN) !=
  19652. ECC_BAD_ARG_E) {
  19653. return -10904 - i;
  19654. }
  19655. if (wc_curve448_check_public(good, i, EC448_BIG_ENDIAN) !=
  19656. ECC_BAD_ARG_E) {
  19657. return -10914 - i;
  19658. }
  19659. }
  19660. /* Little-endian fail cases */
  19661. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  19662. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  19663. EC448_LITTLE_ENDIAN) == 0) {
  19664. return -10924 - i;
  19665. }
  19666. }
  19667. /* Big-endian fail cases */
  19668. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  19669. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  19670. EC448_BIG_ENDIAN) == 0) {
  19671. return -10934 - i;
  19672. }
  19673. }
  19674. /* Check a valid public value works! */
  19675. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  19676. EC448_LITTLE_ENDIAN) != 0) {
  19677. return -10944;
  19678. }
  19679. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  19680. EC448_BIG_ENDIAN) != 0) {
  19681. return -10945;
  19682. }
  19683. return 0;
  19684. }
  19685. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  19686. int curve448_test(void)
  19687. {
  19688. WC_RNG rng;
  19689. int ret;
  19690. #ifdef HAVE_CURVE448_SHARED_SECRET
  19691. byte sharedA[CURVE448_KEY_SIZE];
  19692. byte sharedB[CURVE448_KEY_SIZE];
  19693. word32 y;
  19694. #endif
  19695. #ifdef HAVE_CURVE448_KEY_EXPORT
  19696. byte exportBuf[CURVE448_KEY_SIZE];
  19697. #endif
  19698. word32 x;
  19699. curve448_key userA, userB, pubKey;
  19700. (void)x;
  19701. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  19702. defined(HAVE_CURVE448_KEY_IMPORT)
  19703. /* test vectors from
  19704. https://www.rfc-editor.org/rfc/rfc7748.html
  19705. */
  19706. /* secret key for party a */
  19707. byte sa[] = {
  19708. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  19709. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  19710. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  19711. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  19712. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  19713. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  19714. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  19715. };
  19716. /* public key for party a */
  19717. byte pa[] = {
  19718. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  19719. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  19720. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  19721. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  19722. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  19723. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  19724. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  19725. };
  19726. /* secret key for party b */
  19727. byte sb[] = {
  19728. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  19729. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  19730. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  19731. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  19732. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  19733. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  19734. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  19735. };
  19736. /* public key for party b */
  19737. byte pb[] = {
  19738. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  19739. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  19740. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  19741. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  19742. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  19743. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  19744. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  19745. };
  19746. /* expected shared key */
  19747. byte ss[] = {
  19748. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  19749. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  19750. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  19751. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  19752. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  19753. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  19754. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  19755. };
  19756. #endif /* HAVE_CURVE448_SHARED_SECRET */
  19757. #ifndef HAVE_FIPS
  19758. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  19759. #else
  19760. ret = wc_InitRng(&rng);
  19761. #endif
  19762. if (ret != 0)
  19763. return -11000;
  19764. wc_curve448_init(&userA);
  19765. wc_curve448_init(&userB);
  19766. wc_curve448_init(&pubKey);
  19767. /* make curve448 keys */
  19768. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA) != 0)
  19769. return -11001;
  19770. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB) != 0)
  19771. return -11002;
  19772. #ifdef HAVE_CURVE448_SHARED_SECRET
  19773. /* find shared secret key */
  19774. x = sizeof(sharedA);
  19775. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  19776. return -11003;
  19777. y = sizeof(sharedB);
  19778. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  19779. return -11004;
  19780. /* compare shared secret keys to test they are the same */
  19781. if (y != x)
  19782. return -11005;
  19783. if (XMEMCMP(sharedA, sharedB, x))
  19784. return -11006;
  19785. #endif
  19786. #ifdef HAVE_CURVE448_KEY_EXPORT
  19787. /* export a public key and import it for another user */
  19788. x = sizeof(exportBuf);
  19789. if (wc_curve448_export_public(&userA, exportBuf, &x) != 0)
  19790. return -11007;
  19791. #ifdef HAVE_CURVE448_KEY_IMPORT
  19792. if (wc_curve448_import_public(exportBuf, x, &pubKey) != 0)
  19793. return -11008;
  19794. #endif
  19795. #endif
  19796. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  19797. defined(HAVE_CURVE448_KEY_IMPORT)
  19798. /* test shared key after importing a public key */
  19799. XMEMSET(sharedB, 0, sizeof(sharedB));
  19800. y = sizeof(sharedB);
  19801. if (wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  19802. return -11009;
  19803. if (XMEMCMP(sharedA, sharedB, y))
  19804. return -11010;
  19805. /* import RFC test vectors and compare shared key */
  19806. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  19807. != 0)
  19808. return -11011;
  19809. if (wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  19810. != 0)
  19811. return -11012;
  19812. /* test against known test vector */
  19813. XMEMSET(sharedB, 0, sizeof(sharedB));
  19814. y = sizeof(sharedB);
  19815. if (wc_curve448_shared_secret(&userA, &userB, sharedB, &y) != 0)
  19816. return -11013;
  19817. if (XMEMCMP(ss, sharedB, y))
  19818. return -11014;
  19819. /* test swapping roles of keys and generating same shared key */
  19820. XMEMSET(sharedB, 0, sizeof(sharedB));
  19821. y = sizeof(sharedB);
  19822. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  19823. return -11015;
  19824. if (XMEMCMP(ss, sharedB, y))
  19825. return -11016;
  19826. /* test with 1 generated key and 1 from known test vector */
  19827. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  19828. != 0)
  19829. return -11017;
  19830. if (wc_curve448_make_key(&rng, 56, &userB) != 0)
  19831. return -11018;
  19832. x = sizeof(sharedA);
  19833. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  19834. return -11019;
  19835. y = sizeof(sharedB);
  19836. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  19837. return -11020;
  19838. /* compare shared secret keys to test they are the same */
  19839. if (y != x)
  19840. return -11021;
  19841. if (XMEMCMP(sharedA, sharedB, x))
  19842. return -11022;
  19843. ret = curve448_check_public_test();
  19844. if (ret != 0)
  19845. return ret;
  19846. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  19847. /* clean up keys when done */
  19848. wc_curve448_free(&pubKey);
  19849. wc_curve448_free(&userB);
  19850. wc_curve448_free(&userA);
  19851. wc_FreeRng(&rng);
  19852. return 0;
  19853. }
  19854. #endif /* HAVE_CURVE448 */
  19855. #ifdef HAVE_ED448
  19856. #ifdef WOLFSSL_TEST_CERT
  19857. static int ed448_test_cert(void)
  19858. {
  19859. DecodedCert cert[2];
  19860. DecodedCert* serverCert = NULL;
  19861. DecodedCert* caCert = NULL;
  19862. #ifdef HAVE_ED448_VERIFY
  19863. ed448_key key;
  19864. ed448_key* pubKey = NULL;
  19865. int verify;
  19866. #endif /* HAVE_ED448_VERIFY */
  19867. int ret;
  19868. byte* tmp;
  19869. size_t bytes;
  19870. XFILE file;
  19871. tmp = XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19872. if (tmp == NULL) {
  19873. ERROR_OUT(-11023, done);
  19874. }
  19875. #ifdef USE_CERT_BUFFERS_256
  19876. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  19877. bytes = sizeof_ca_ed448_cert;
  19878. #elif !defined(NO_FILESYSTEM)
  19879. file = XFOPEN(caEd448Cert, "rb");
  19880. if (file == NULL) {
  19881. ERROR_OUT(-11024, done);
  19882. }
  19883. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  19884. XFCLOSE(file);
  19885. #else
  19886. /* No certificate to use. */
  19887. ERROR_OUT(-11025, done);
  19888. #endif
  19889. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  19890. caCert = &cert[0];
  19891. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  19892. if (ret != 0) {
  19893. ERROR_OUT(-11026, done);
  19894. }
  19895. #ifdef USE_CERT_BUFFERS_256
  19896. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  19897. bytes = sizeof_server_ed448_cert;
  19898. #elif !defined(NO_FILESYSTEM)
  19899. file = XFOPEN(serverEd448Cert, "rb");
  19900. if (file == NULL) {
  19901. ERROR_OUT(-11027, done);
  19902. }
  19903. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  19904. XFCLOSE(file);
  19905. #else
  19906. /* No certificate to use. */
  19907. ERROR_OUT(-11028, done);
  19908. #endif
  19909. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  19910. serverCert = &cert[1];
  19911. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  19912. if (ret != 0) {
  19913. ERROR_OUT(-11029, done);
  19914. }
  19915. #ifdef HAVE_ED448_VERIFY
  19916. ret = wc_ed448_init(&key);
  19917. if (ret < 0) {
  19918. ERROR_OUT(-11030, done);
  19919. }
  19920. pubKey = &key;
  19921. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  19922. if (ret < 0) {
  19923. ERROR_OUT(-11031, done);
  19924. }
  19925. if (wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  19926. serverCert->source + serverCert->certBegin,
  19927. serverCert->sigIndex - serverCert->certBegin,
  19928. &verify, pubKey, NULL, 0) < 0 || verify != 1) {
  19929. ERROR_OUT(-11032, done);
  19930. }
  19931. #endif /* HAVE_ED448_VERIFY */
  19932. done:
  19933. if (tmp != NULL)
  19934. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19935. #ifdef HAVE_ED448_VERIFY
  19936. wc_ed448_free(pubKey);
  19937. #endif /* HAVE_ED448_VERIFY */
  19938. if (caCert != NULL)
  19939. FreeDecodedCert(caCert);
  19940. if (serverCert != NULL)
  19941. FreeDecodedCert(serverCert);
  19942. return ret;
  19943. }
  19944. static int ed448_test_make_cert(void)
  19945. {
  19946. WC_RNG rng;
  19947. Cert cert;
  19948. DecodedCert decode;
  19949. ed448_key key;
  19950. ed448_key* privKey = NULL;
  19951. int ret = 0;
  19952. byte* tmp = NULL;
  19953. wc_InitCert(&cert);
  19954. #ifndef HAVE_FIPS
  19955. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  19956. #else
  19957. ret = wc_InitRng(&rng);
  19958. #endif
  19959. if (ret != 0)
  19960. return -11033;
  19961. wc_ed448_init(&key);
  19962. privKey = &key;
  19963. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  19964. cert.daysValid = 365 * 2;
  19965. cert.selfSigned = 1;
  19966. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  19967. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  19968. cert.isCA = 0;
  19969. #ifdef WOLFSSL_CERT_EXT
  19970. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  19971. if (ret < 0) {
  19972. ERROR_OUT(-11034, done);
  19973. }
  19974. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  19975. if (ret < 0) {
  19976. ERROR_OUT(-11035, done);
  19977. }
  19978. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  19979. if (ret < 0) {
  19980. ERROR_OUT(-11036, done);
  19981. }
  19982. #endif
  19983. tmp = XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19984. if (tmp == NULL) {
  19985. ERROR_OUT(-11037, done);
  19986. }
  19987. cert.sigType = CTC_ED448;
  19988. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  19989. if (ret < 0) {
  19990. ERROR_OUT(-11038, done);
  19991. }
  19992. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  19993. privKey, &rng);
  19994. if (ret < 0) {
  19995. ERROR_OUT(-11039, done);
  19996. }
  19997. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  19998. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  19999. FreeDecodedCert(&decode);
  20000. if (ret != 0) {
  20001. ERROR_OUT(-11040, done);
  20002. }
  20003. done:
  20004. if (tmp != NULL)
  20005. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20006. wc_ed448_free(privKey);
  20007. wc_FreeRng(&rng);
  20008. return ret;
  20009. }
  20010. #endif /* WOLFSSL_TEST_CERT */
  20011. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  20012. defined(HAVE_ED448_KEY_IMPORT)
  20013. static int ed448_ctx_test(void)
  20014. {
  20015. byte out[ED448_SIG_SIZE];
  20016. word32 outlen;
  20017. #ifdef HAVE_ED448_VERIFY
  20018. int verify;
  20019. #endif /* HAVE_ED448_VERIFY */
  20020. ed448_key key;
  20021. static const byte sKeyCtx[] = {
  20022. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  20023. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  20024. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  20025. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  20026. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  20027. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  20028. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  20029. 0x4e
  20030. };
  20031. static const byte pKeyCtx[] = {
  20032. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  20033. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  20034. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  20035. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  20036. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  20037. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  20038. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  20039. 0x80
  20040. };
  20041. static const byte sigCtx[] = {
  20042. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  20043. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  20044. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  20045. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  20046. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  20047. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  20048. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  20049. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  20050. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  20051. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  20052. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  20053. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  20054. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  20055. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  20056. 0x3c, 0x00
  20057. };
  20058. static const byte msgCtx[] = {
  20059. 0x03
  20060. };
  20061. static const byte contextCtx[] = {
  20062. 0x66,0x6f,0x6f
  20063. };
  20064. outlen = sizeof(out);
  20065. XMEMSET(out, 0, sizeof(out));
  20066. if (wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  20067. sizeof(pKeyCtx), &key) != 0)
  20068. return -11100;
  20069. if (wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  20070. contextCtx, sizeof(contextCtx)) != 0)
  20071. return -11101;
  20072. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  20073. return -11102;
  20074. #if defined(HAVE_ED448_VERIFY)
  20075. /* test verify on good msg */
  20076. if (wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  20077. contextCtx, sizeof(contextCtx)) != 0 || verify != 1)
  20078. return -11103;
  20079. #endif
  20080. wc_ed448_free(&key);
  20081. return 0;
  20082. }
  20083. static int ed448ph_test(void)
  20084. {
  20085. byte out[ED448_SIG_SIZE];
  20086. word32 outlen;
  20087. #ifdef HAVE_ED448_VERIFY
  20088. int verify;
  20089. #endif /* HAVE_ED448_VERIFY */
  20090. ed448_key key;
  20091. static const byte sKeyPh[] = {
  20092. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  20093. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  20094. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  20095. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  20096. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  20097. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  20098. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  20099. 0x49
  20100. };
  20101. static const byte pKeyPh[] = {
  20102. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  20103. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  20104. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  20105. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  20106. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  20107. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  20108. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  20109. 0x80
  20110. };
  20111. static const byte sigPh1[] = {
  20112. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  20113. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  20114. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  20115. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  20116. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  20117. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  20118. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  20119. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  20120. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  20121. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  20122. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  20123. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  20124. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  20125. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  20126. 0x0f, 0x00
  20127. };
  20128. static const byte sigPh2[] = {
  20129. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  20130. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  20131. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  20132. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  20133. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  20134. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  20135. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  20136. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  20137. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  20138. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  20139. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  20140. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  20141. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  20142. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  20143. 0x21, 0x00
  20144. };
  20145. static const byte msgPh[] = {
  20146. 0x61,0x62,0x63
  20147. };
  20148. /* SHA-512 hash of msgPh */
  20149. static const byte hashPh[] = {
  20150. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  20151. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  20152. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  20153. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  20154. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  20155. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  20156. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  20157. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  20158. };
  20159. static const byte contextPh2[] = {
  20160. 0x66,0x6f,0x6f
  20161. };
  20162. outlen = sizeof(out);
  20163. XMEMSET(out, 0, sizeof(out));
  20164. if (wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  20165. sizeof(pKeyPh), &key) != 0) {
  20166. return -11200;
  20167. }
  20168. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  20169. 0) != 0) {
  20170. return -11201;
  20171. }
  20172. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  20173. return -11202;
  20174. #if defined(HAVE_ED448_VERIFY)
  20175. /* test verify on good msg */
  20176. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  20177. NULL, 0) != 0 || verify != 1) {
  20178. return -11203;
  20179. }
  20180. #endif
  20181. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  20182. contextPh2, sizeof(contextPh2)) != 0) {
  20183. return -11204;
  20184. }
  20185. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  20186. return -11205;
  20187. #if defined(HAVE_ED448_VERIFY)
  20188. /* test verify on good msg */
  20189. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  20190. contextPh2, sizeof(contextPh2)) != 0 ||
  20191. verify != 1) {
  20192. return -11206;
  20193. }
  20194. #endif
  20195. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  20196. 0) != 0) {
  20197. return -11207;
  20198. }
  20199. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  20200. return -11208;
  20201. #if defined(HAVE_ED448_VERIFY)
  20202. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  20203. &key, NULL, 0) != 0 || verify != 1) {
  20204. return -11209;
  20205. }
  20206. #endif
  20207. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  20208. contextPh2, sizeof(contextPh2)) != 0) {
  20209. return -11210;
  20210. }
  20211. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  20212. return -11211;
  20213. #if defined(HAVE_ED448_VERIFY)
  20214. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  20215. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  20216. verify != 1) {
  20217. return -11212;
  20218. }
  20219. #endif
  20220. wc_ed448_free(&key);
  20221. return 0;
  20222. }
  20223. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  20224. int ed448_test(void)
  20225. {
  20226. int ret;
  20227. WC_RNG rng;
  20228. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  20229. defined(HAVE_ED448_KEY_IMPORT)
  20230. byte out[ED448_SIG_SIZE];
  20231. byte exportPKey[ED448_KEY_SIZE];
  20232. byte exportSKey[ED448_KEY_SIZE];
  20233. word32 exportPSz;
  20234. word32 exportSSz;
  20235. int i;
  20236. word32 outlen;
  20237. #ifdef HAVE_ED448_VERIFY
  20238. int verify;
  20239. #endif /* HAVE_ED448_VERIFY */
  20240. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  20241. word32 keySz, sigSz;
  20242. ed448_key key;
  20243. ed448_key key2;
  20244. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  20245. defined(HAVE_ED448_KEY_IMPORT)
  20246. /* test vectors from
  20247. https://tools.ietf.org/html/rfc8032
  20248. */
  20249. static const byte sKey1[] = {
  20250. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  20251. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  20252. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  20253. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  20254. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  20255. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  20256. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  20257. 0x5b
  20258. };
  20259. static const byte sKey2[] = {
  20260. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  20261. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  20262. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  20263. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  20264. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  20265. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  20266. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  20267. 0x4e
  20268. };
  20269. static const byte sKey3[] = {
  20270. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  20271. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  20272. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  20273. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  20274. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  20275. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  20276. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  20277. 0x1b
  20278. };
  20279. /* uncompressed test */
  20280. static const byte sKey4[] = {
  20281. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  20282. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  20283. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  20284. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  20285. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  20286. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  20287. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  20288. 0x5b
  20289. };
  20290. /* compressed prefix test */
  20291. static const byte sKey5[] = {
  20292. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  20293. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  20294. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  20295. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  20296. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  20297. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  20298. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  20299. 0x5b
  20300. };
  20301. static const byte sKey6[] = {
  20302. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  20303. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  20304. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  20305. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  20306. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  20307. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  20308. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  20309. 0xe8
  20310. };
  20311. static const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  20312. static const byte pKey1[] = {
  20313. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  20314. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  20315. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  20316. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  20317. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  20318. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  20319. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  20320. 0x80
  20321. };
  20322. static const byte pKey2[] = {
  20323. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  20324. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  20325. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  20326. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  20327. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  20328. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  20329. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  20330. 0x80
  20331. };
  20332. static const byte pKey3[] = {
  20333. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  20334. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  20335. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  20336. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  20337. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  20338. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  20339. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  20340. 0x80
  20341. };
  20342. /* uncompressed test */
  20343. static const byte pKey4[] = {
  20344. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  20345. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  20346. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  20347. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  20348. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  20349. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  20350. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  20351. 0x80
  20352. };
  20353. /* compressed prefix */
  20354. static const byte pKey5[] = {
  20355. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  20356. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  20357. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  20358. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  20359. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  20360. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  20361. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  20362. 0x80
  20363. };
  20364. static const byte pKey6[] = {
  20365. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  20366. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  20367. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  20368. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  20369. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  20370. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  20371. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  20372. 0x00
  20373. };
  20374. static const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  20375. static const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  20376. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  20377. static const byte sig1[] = {
  20378. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  20379. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  20380. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  20381. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  20382. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  20383. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  20384. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  20385. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  20386. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  20387. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  20388. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  20389. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  20390. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  20391. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  20392. 0x26, 0x00
  20393. };
  20394. static const byte sig2[] = {
  20395. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  20396. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  20397. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  20398. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  20399. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  20400. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  20401. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  20402. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  20403. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  20404. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  20405. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  20406. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  20407. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  20408. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  20409. 0x3a, 0x00
  20410. };
  20411. static const byte sig3[] = {
  20412. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  20413. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  20414. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  20415. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  20416. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  20417. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  20418. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  20419. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  20420. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  20421. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  20422. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  20423. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  20424. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  20425. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  20426. 0x3c, 0x00
  20427. };
  20428. /* uncompressed test */
  20429. static const byte sig4[] = {
  20430. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  20431. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  20432. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  20433. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  20434. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  20435. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  20436. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  20437. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  20438. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  20439. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  20440. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  20441. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  20442. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  20443. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  20444. 0x26, 0x00
  20445. };
  20446. /* compressed prefix */
  20447. static const byte sig5[] = {
  20448. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  20449. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  20450. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  20451. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  20452. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  20453. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  20454. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  20455. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  20456. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  20457. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  20458. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  20459. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  20460. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  20461. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  20462. 0x26, 0x00
  20463. };
  20464. static const byte sig6[] = {
  20465. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  20466. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  20467. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  20468. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  20469. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  20470. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  20471. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  20472. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  20473. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  20474. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  20475. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  20476. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  20477. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  20478. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  20479. 0x1a, 0x00
  20480. };
  20481. static const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  20482. #define SIGSZ sizeof(sig1)
  20483. static const byte msg1[] = { };
  20484. static const byte msg2[] = { 0x03 };
  20485. static const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  20486. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  20487. /* test of a 1023 byte long message */
  20488. static const byte msg4[] = {
  20489. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  20490. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  20491. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  20492. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  20493. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  20494. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  20495. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  20496. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  20497. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  20498. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  20499. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  20500. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  20501. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  20502. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  20503. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  20504. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  20505. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  20506. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  20507. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  20508. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  20509. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  20510. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  20511. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  20512. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  20513. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  20514. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  20515. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  20516. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  20517. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  20518. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  20519. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  20520. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  20521. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  20522. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  20523. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  20524. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  20525. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  20526. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  20527. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  20528. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  20529. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  20530. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  20531. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  20532. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  20533. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  20534. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  20535. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  20536. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  20537. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  20538. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  20539. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  20540. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  20541. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  20542. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  20543. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  20544. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  20545. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  20546. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  20547. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  20548. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  20549. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  20550. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  20551. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  20552. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  20553. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  20554. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  20555. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  20556. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  20557. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  20558. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  20559. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  20560. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  20561. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  20562. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  20563. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  20564. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  20565. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  20566. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  20567. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  20568. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  20569. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  20570. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  20571. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  20572. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  20573. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  20574. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  20575. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  20576. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  20577. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  20578. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  20579. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  20580. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  20581. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  20582. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  20583. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  20584. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  20585. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  20586. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  20587. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  20588. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  20589. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  20590. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  20591. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  20592. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  20593. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  20594. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  20595. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  20596. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  20597. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  20598. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  20599. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  20600. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  20601. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  20602. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  20603. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  20604. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  20605. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  20606. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  20607. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  20608. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  20609. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  20610. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  20611. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  20612. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  20613. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  20614. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  20615. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  20616. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  20617. };
  20618. static const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  20619. static const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  20620. sizeof(msg2),
  20621. sizeof(msg3),
  20622. 0 /*sizeof(msg1)*/,
  20623. 0 /*sizeof(msg1)*/,
  20624. sizeof(msg4)
  20625. };
  20626. #ifndef NO_ASN
  20627. static byte privateEd448[] = {
  20628. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  20629. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  20630. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  20631. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  20632. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  20633. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  20634. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  20635. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  20636. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  20637. 0x5b
  20638. };
  20639. static byte publicEd448[] = {
  20640. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  20641. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  20642. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  20643. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  20644. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  20645. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  20646. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  20647. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  20648. 0xaf, 0xe8, 0x25, 0x61, 0x80
  20649. };
  20650. static byte privPubEd448[] = {
  20651. 0x30, 0x81, 0x84, 0x02, 0x01, 0x00, 0x30, 0x05,
  20652. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  20653. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  20654. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  20655. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  20656. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  20657. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  20658. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  20659. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  20660. 0xf9, 0x5b, 0xa1, 0x3b, 0x04, 0x39, 0x5f, 0xd7,
  20661. 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7,
  20662. 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1,
  20663. 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e,
  20664. 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1,
  20665. 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6,
  20666. 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa,
  20667. 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61, 0x80
  20668. };
  20669. word32 idx;
  20670. ed448_key key3;
  20671. #endif /* NO_ASN */
  20672. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  20673. /* create ed448 keys */
  20674. #ifndef HAVE_FIPS
  20675. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20676. #else
  20677. ret = wc_InitRng(&rng);
  20678. #endif
  20679. if (ret != 0)
  20680. return -11300;
  20681. wc_ed448_init(&key);
  20682. wc_ed448_init(&key2);
  20683. #ifndef NO_ASN
  20684. wc_ed448_init(&key3);
  20685. #endif
  20686. wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20687. wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key2);
  20688. /* helper functions for signature and key size */
  20689. keySz = wc_ed448_size(&key);
  20690. sigSz = wc_ed448_sig_size(&key);
  20691. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  20692. defined(HAVE_ED448_KEY_IMPORT)
  20693. for (i = 0; i < 6; i++) {
  20694. outlen = sizeof(out);
  20695. XMEMSET(out, 0, sizeof(out));
  20696. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  20697. pKeySz[i], &key) != 0)
  20698. return -11301 - i;
  20699. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, &key, NULL,
  20700. 0) != 0) {
  20701. return -11311 - i;
  20702. }
  20703. if (XMEMCMP(out, sigs[i], 114))
  20704. return -11321 - i;
  20705. #if defined(HAVE_ED448_VERIFY)
  20706. /* test verify on good msg */
  20707. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, &key,
  20708. NULL, 0) != 0 || verify != 1) {
  20709. return -11331 - i;
  20710. }
  20711. /* test verify on bad msg */
  20712. out[outlen-2] = out[outlen-2] + 1;
  20713. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, &key,
  20714. NULL, 0) == 0 || verify == 1) {
  20715. return -11341 - i;
  20716. }
  20717. #endif /* HAVE_ED448_VERIFY */
  20718. /* test api for import/exporting keys */
  20719. exportPSz = sizeof(exportPKey);
  20720. exportSSz = sizeof(exportSKey);
  20721. if (wc_ed448_export_public(&key, exportPKey, &exportPSz) != 0)
  20722. return -11351 - i;
  20723. if (wc_ed448_import_public(exportPKey, exportPSz, &key2) != 0)
  20724. return -11361 - i;
  20725. if (wc_ed448_export_private_only(&key, exportSKey, &exportSSz) != 0)
  20726. return -11371 - i;
  20727. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  20728. exportPKey, exportPSz, &key2) != 0)
  20729. return -11381 - i;
  20730. /* clear "out" buffer and test sign with imported keys */
  20731. outlen = sizeof(out);
  20732. XMEMSET(out, 0, sizeof(out));
  20733. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2, NULL,
  20734. 0) != 0) {
  20735. return -11391 - i;
  20736. }
  20737. #if defined(HAVE_ED448_VERIFY)
  20738. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, &key2,
  20739. NULL, 0) != 0 || verify != 1)
  20740. return -11401 - i;
  20741. if (XMEMCMP(out, sigs[i], SIGSZ))
  20742. return -11411 - i;
  20743. #endif /* HAVE_ED448_VERIFY */
  20744. }
  20745. ret = ed448_ctx_test();
  20746. if (ret != 0)
  20747. return ret;
  20748. ret = ed448ph_test();
  20749. if (ret != 0)
  20750. return ret;
  20751. #ifndef NO_ASN
  20752. /* Try ASN.1 encoded private-only key and public key. */
  20753. idx = 0;
  20754. if (wc_Ed448PrivateKeyDecode(privateEd448, &idx, &key3,
  20755. sizeof(privateEd448)) != 0)
  20756. return -11421 - i;
  20757. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3, NULL, 0)
  20758. != BAD_FUNC_ARG)
  20759. return -11431 - i;
  20760. idx = 0;
  20761. if (wc_Ed448PublicKeyDecode(publicEd448, &idx, &key3,
  20762. sizeof(publicEd448)) != 0)
  20763. return -11441 - i;
  20764. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3, NULL, 0) != 0)
  20765. return -11451 - i;
  20766. if (XMEMCMP(out, sigs[0], SIGSZ))
  20767. return -11461 - i;
  20768. #if defined(HAVE_ED448_VERIFY)
  20769. /* test verify on good msg */
  20770. if (wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3,
  20771. NULL, 0) != 0 || verify != 1)
  20772. return -11471 - i;
  20773. #endif /* HAVE_ED448_VERIFY */
  20774. wc_ed448_free(&key3);
  20775. wc_ed448_init(&key3);
  20776. idx = 0;
  20777. if (wc_Ed448PrivateKeyDecode(privPubEd448, &idx, &key3,
  20778. sizeof(privPubEd448)) != 0)
  20779. return -11481 - i;
  20780. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3, NULL, 0) != 0)
  20781. return -11491 - i;
  20782. if (XMEMCMP(out, sigs[0], SIGSZ))
  20783. return -11501 - i;
  20784. wc_ed448_free(&key3);
  20785. #endif /* NO_ASN */
  20786. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  20787. /* clean up keys when done */
  20788. wc_ed448_free(&key);
  20789. wc_ed448_free(&key2);
  20790. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  20791. wc_FreeRng(&rng);
  20792. #endif
  20793. /* hush warnings of unused keySz and sigSz */
  20794. (void)keySz;
  20795. (void)sigSz;
  20796. #ifdef WOLFSSL_TEST_CERT
  20797. ret = ed448_test_cert();
  20798. if (ret < 0)
  20799. return ret;
  20800. #ifdef WOLFSSL_CERT_GEN
  20801. ret = ed448_test_make_cert();
  20802. if (ret < 0)
  20803. return ret;
  20804. #endif /* WOLFSSL_CERT_GEN */
  20805. #endif /* WOLFSSL_TEST_CERT */
  20806. return 0;
  20807. }
  20808. #endif /* HAVE_ED448 */
  20809. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  20810. typedef struct CMAC_Test_Case {
  20811. int type;
  20812. int partial;
  20813. const byte* m;
  20814. word32 mSz;
  20815. const byte* k;
  20816. word32 kSz;
  20817. const byte* t;
  20818. word32 tSz;
  20819. } CMAC_Test_Case;
  20820. int cmac_test(void)
  20821. {
  20822. #ifdef WOLFSSL_AES_128
  20823. const byte k128[] =
  20824. {
  20825. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  20826. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  20827. };
  20828. #define KLEN_128 (sizeof(k128))
  20829. #endif
  20830. #ifdef WOLFSSL_AES_192
  20831. const byte k192[] =
  20832. {
  20833. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  20834. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  20835. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  20836. };
  20837. #define KLEN_192 (sizeof(k192))
  20838. #endif
  20839. #ifdef WOLFSSL_AES_256
  20840. const byte k256[] =
  20841. {
  20842. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  20843. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  20844. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  20845. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  20846. };
  20847. #define KLEN_256 (sizeof(k256))
  20848. #endif
  20849. const byte m[] =
  20850. {
  20851. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  20852. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  20853. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  20854. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  20855. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  20856. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  20857. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  20858. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  20859. };
  20860. #define MLEN_0 (0)
  20861. #define MLEN_128 (128/8)
  20862. #define MLEN_320 (320/8)
  20863. #define MLEN_319 (MLEN_320 - 1)
  20864. #define MLEN_512 (512/8)
  20865. #ifdef WOLFSSL_AES_128
  20866. const byte t128_0[] =
  20867. {
  20868. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  20869. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  20870. };
  20871. const byte t128_128[] =
  20872. {
  20873. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  20874. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  20875. };
  20876. const byte t128_319[] =
  20877. {
  20878. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  20879. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  20880. };
  20881. const byte t128_320[] =
  20882. {
  20883. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  20884. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  20885. };
  20886. const byte t128_512[] =
  20887. {
  20888. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  20889. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  20890. };
  20891. #endif
  20892. #ifdef WOLFSSL_AES_192
  20893. const byte t192_0[] =
  20894. {
  20895. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  20896. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  20897. };
  20898. const byte t192_128[] =
  20899. {
  20900. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  20901. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  20902. };
  20903. const byte t192_320[] =
  20904. {
  20905. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  20906. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  20907. };
  20908. const byte t192_512[] =
  20909. {
  20910. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  20911. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  20912. };
  20913. #endif
  20914. #ifdef WOLFSSL_AES_256
  20915. const byte t256_0[] =
  20916. {
  20917. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  20918. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  20919. };
  20920. const byte t256_128[] =
  20921. {
  20922. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  20923. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  20924. };
  20925. const byte t256_320[] =
  20926. {
  20927. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  20928. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  20929. };
  20930. const byte t256_512[] =
  20931. {
  20932. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  20933. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  20934. };
  20935. #endif
  20936. const CMAC_Test_Case testCases[] =
  20937. {
  20938. #ifdef WOLFSSL_AES_128
  20939. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  20940. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  20941. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  20942. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  20943. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  20944. #endif
  20945. #ifdef WOLFSSL_AES_192
  20946. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  20947. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  20948. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  20949. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  20950. #endif
  20951. #ifdef WOLFSSL_AES_256
  20952. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  20953. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  20954. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  20955. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  20956. #endif
  20957. #ifdef WOLFSSL_AES_128
  20958. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  20959. #endif
  20960. };
  20961. Cmac cmac;
  20962. byte tag[AES_BLOCK_SIZE];
  20963. const CMAC_Test_Case* tc;
  20964. word32 i, tagSz;
  20965. for (i = 0, tc = testCases;
  20966. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  20967. i++, tc++) {
  20968. XMEMSET(tag, 0, sizeof(tag));
  20969. tagSz = AES_BLOCK_SIZE;
  20970. if (wc_InitCmac(&cmac, tc->k, tc->kSz, tc->type, NULL) != 0)
  20971. return -11600;
  20972. if (tc->partial) {
  20973. if (wc_CmacUpdate(&cmac, tc->m,
  20974. tc->mSz/2 - tc->partial) != 0)
  20975. return -11601;
  20976. if (wc_CmacUpdate(&cmac, tc->m + tc->mSz/2 - tc->partial,
  20977. tc->mSz/2 + tc->partial) != 0)
  20978. return -11602;
  20979. }
  20980. else {
  20981. if (wc_CmacUpdate(&cmac, tc->m, tc->mSz) != 0)
  20982. return -11603;
  20983. }
  20984. if (wc_CmacFinal(&cmac, tag, &tagSz) != 0)
  20985. return -11604;
  20986. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  20987. return -11605;
  20988. XMEMSET(tag, 0, sizeof(tag));
  20989. tagSz = sizeof(tag);
  20990. if (wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  20991. tc->k, tc->kSz) != 0)
  20992. return -11606;
  20993. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  20994. return -11607;
  20995. if (wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  20996. tc->k, tc->kSz) != 0)
  20997. return -11608;
  20998. }
  20999. return 0;
  21000. }
  21001. #endif /* NO_AES && WOLFSSL_CMAC */
  21002. #ifdef HAVE_LIBZ
  21003. const byte sample_text[] =
  21004. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  21005. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  21006. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  21007. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  21008. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  21009. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  21010. "\n"
  21011. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  21012. "four loko you probably haven't heard of them high life. Messenger bag\n"
  21013. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  21014. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  21015. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  21016. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  21017. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  21018. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  21019. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  21020. "four loko whatever street art yr farm-to-table.\n"
  21021. "\n"
  21022. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  21023. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  21024. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  21025. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  21026. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  21027. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  21028. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  21029. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  21030. "locavore.\n"
  21031. "\n"
  21032. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  21033. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  21034. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  21035. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  21036. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  21037. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  21038. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  21039. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  21040. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  21041. "neutra PBR selvage.\n"
  21042. "\n"
  21043. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  21044. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  21045. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  21046. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  21047. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  21048. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  21049. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  21050. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  21051. "mollit magna, sriracha sartorial helvetica.\n"
  21052. "\n"
  21053. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  21054. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  21055. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  21056. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  21057. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  21058. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  21059. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  21060. "\n"
  21061. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  21062. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  21063. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  21064. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  21065. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  21066. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  21067. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  21068. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  21069. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  21070. "\n"
  21071. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  21072. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  21073. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  21074. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  21075. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  21076. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  21077. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  21078. "bag dolor terry richardson sapiente.\n";
  21079. const byte sample_text_gz[] = {
  21080. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  21081. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  21082. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  21083. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  21084. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  21085. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  21086. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  21087. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  21088. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  21089. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  21090. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  21091. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  21092. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  21093. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  21094. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  21095. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  21096. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  21097. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  21098. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  21099. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  21100. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  21101. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  21102. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  21103. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  21104. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  21105. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  21106. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  21107. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  21108. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  21109. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  21110. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  21111. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  21112. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  21113. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  21114. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  21115. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  21116. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  21117. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  21118. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  21119. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  21120. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  21121. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  21122. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  21123. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  21124. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  21125. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  21126. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  21127. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  21128. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  21129. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  21130. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  21131. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  21132. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  21133. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  21134. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  21135. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  21136. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  21137. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  21138. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  21139. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  21140. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  21141. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  21142. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  21143. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  21144. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  21145. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  21146. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  21147. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  21148. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  21149. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  21150. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  21151. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  21152. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  21153. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  21154. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  21155. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  21156. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  21157. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  21158. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  21159. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  21160. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  21161. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  21162. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  21163. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  21164. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  21165. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  21166. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  21167. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  21168. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  21169. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  21170. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  21171. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  21172. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  21173. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  21174. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  21175. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  21176. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  21177. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  21178. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  21179. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  21180. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  21181. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  21182. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  21183. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  21184. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  21185. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  21186. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  21187. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  21188. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  21189. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  21190. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  21191. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  21192. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  21193. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  21194. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  21195. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  21196. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  21197. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  21198. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  21199. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  21200. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  21201. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  21202. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  21203. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  21204. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  21205. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  21206. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  21207. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  21208. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  21209. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  21210. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  21211. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  21212. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  21213. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  21214. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  21215. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  21216. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  21217. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  21218. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  21219. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  21220. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  21221. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  21222. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  21223. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  21224. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  21225. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  21226. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  21227. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  21228. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  21229. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  21230. };
  21231. int compress_test(void)
  21232. {
  21233. int ret = 0;
  21234. word32 dSz = sizeof(sample_text);
  21235. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  21236. byte *c;
  21237. byte *d;
  21238. c = XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21239. d = XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21240. if (c == NULL || d == NULL) {
  21241. ERROR_OUT(-11700, exit);
  21242. }
  21243. /* follow calloc and initialize to 0 */
  21244. XMEMSET(c, 0, cSz);
  21245. XMEMSET(d, 0, dSz);
  21246. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  21247. ERROR_OUT(-11701, exit);
  21248. }
  21249. cSz = (word32)ret;
  21250. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  21251. ERROR_OUT(-11702, exit);
  21252. }
  21253. if (XMEMCMP(d, sample_text, dSz) != 0) {
  21254. ERROR_OUT(-11703, exit);
  21255. }
  21256. /* GZIP tests */
  21257. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  21258. XMEMSET(c, 0, cSz);
  21259. XMEMSET(d, 0, dSz);
  21260. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  21261. if (ret < 0) {
  21262. ERROR_OUT(-11704, exit);
  21263. }
  21264. cSz = (word32)ret;
  21265. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  21266. if (ret < 0) {
  21267. ERROR_OUT(-11705, exit);
  21268. }
  21269. if (XMEMCMP(d, sample_text, dSz) != 0) {
  21270. ERROR_OUT(-11706, exit);
  21271. }
  21272. /* Try with gzip generated output */
  21273. XMEMSET(d, 0, dSz);
  21274. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  21275. LIBZ_WINBITS_GZIP);
  21276. if (ret < 0) {
  21277. ERROR_OUT(-11707, exit);
  21278. }
  21279. dSz = (word32)ret;
  21280. if (XMEMCMP(d, sample_text, dSz) != 0) {
  21281. ERROR_OUT(-11708, exit);
  21282. }
  21283. ret = 0; /* success */
  21284. exit:
  21285. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21286. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21287. return ret;
  21288. }
  21289. #endif /* HAVE_LIBZ */
  21290. #ifdef HAVE_PKCS7
  21291. /* External Debugging/Testing Note:
  21292. *
  21293. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  21294. * additional testing. To dump bundles to files DER encoded files, please
  21295. * define:
  21296. *
  21297. * #define PKCS7_OUTPUT_TEST_BUNDLES
  21298. */
  21299. /* Loads certs and keys for use with PKCS7 tests, from either files
  21300. * or buffers.
  21301. *
  21302. * rsaClientCertBuf - output buffer for RSA client cert
  21303. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  21304. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  21305. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  21306. *
  21307. * rsaServerCertBuf - output buffer for RSA server cert
  21308. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  21309. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  21310. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  21311. *
  21312. * rsaCaCertBuf - output buffer for RSA CA cert
  21313. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  21314. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  21315. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  21316. *
  21317. * eccClientCertBuf - output buffer for ECC cert
  21318. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  21319. * eccClientPrivKeyBuf - output buffer for ECC private key
  21320. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  21321. *
  21322. * Returns 0 on success, negative on error
  21323. */
  21324. static int pkcs7_load_certs_keys(
  21325. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  21326. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  21327. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  21328. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  21329. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  21330. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  21331. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  21332. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  21333. {
  21334. #ifndef NO_FILESYSTEM
  21335. XFILE certFile;
  21336. XFILE keyFile;
  21337. (void)certFile;
  21338. (void)keyFile;
  21339. #endif
  21340. #ifndef NO_RSA
  21341. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  21342. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  21343. return BAD_FUNC_ARG;
  21344. #endif
  21345. #ifdef HAVE_ECC
  21346. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  21347. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  21348. return BAD_FUNC_ARG;
  21349. #endif
  21350. /* RSA */
  21351. #ifndef NO_RSA
  21352. #ifdef USE_CERT_BUFFERS_1024
  21353. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  21354. return -11709;
  21355. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  21356. sizeof_client_cert_der_1024);
  21357. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  21358. if (rsaServerCertBuf != NULL) {
  21359. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  21360. return -11710;
  21361. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  21362. sizeof_server_cert_der_1024);
  21363. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  21364. }
  21365. if (rsaCaCertBuf != NULL) {
  21366. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  21367. return -11711;
  21368. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  21369. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  21370. }
  21371. #elif defined(USE_CERT_BUFFERS_2048)
  21372. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  21373. return -11712;
  21374. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  21375. sizeof_client_cert_der_2048);
  21376. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  21377. if (rsaServerCertBuf != NULL) {
  21378. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  21379. return -11713;
  21380. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  21381. sizeof_server_cert_der_2048);
  21382. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  21383. }
  21384. if (rsaCaCertBuf != NULL) {
  21385. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  21386. return -11714;
  21387. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  21388. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  21389. }
  21390. #else
  21391. certFile = XFOPEN(clientCert, "rb");
  21392. if (!certFile)
  21393. return -11715;
  21394. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  21395. *rsaClientCertBufSz, certFile);
  21396. XFCLOSE(certFile);
  21397. if (rsaServerCertBuf != NULL) {
  21398. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  21399. if (!certFile)
  21400. return -11716;
  21401. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  21402. *rsaServerCertBufSz, certFile);
  21403. XFCLOSE(certFile);
  21404. }
  21405. if (rsaCaCertBuf != NULL) {
  21406. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  21407. if (!certFile)
  21408. return -11717;
  21409. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  21410. certFile);
  21411. XFCLOSE(certFile);
  21412. }
  21413. #endif
  21414. #ifdef USE_CERT_BUFFERS_1024
  21415. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  21416. return -11718;
  21417. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  21418. sizeof_client_key_der_1024);
  21419. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  21420. if (rsaServerPrivKeyBuf != NULL) {
  21421. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  21422. return -11719;
  21423. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  21424. sizeof_server_key_der_1024);
  21425. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  21426. }
  21427. if (rsaCaPrivKeyBuf != NULL) {
  21428. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  21429. return -11720;
  21430. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  21431. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  21432. }
  21433. #elif defined(USE_CERT_BUFFERS_2048)
  21434. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  21435. return -11721;
  21436. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  21437. sizeof_client_key_der_2048);
  21438. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  21439. if (rsaServerPrivKeyBuf != NULL) {
  21440. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  21441. return -11722;
  21442. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  21443. sizeof_server_key_der_2048);
  21444. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  21445. }
  21446. if (rsaCaPrivKeyBuf != NULL) {
  21447. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  21448. return -11723;
  21449. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  21450. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  21451. }
  21452. #else
  21453. keyFile = XFOPEN(clientKey, "rb");
  21454. if (!keyFile)
  21455. return -11724;
  21456. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  21457. *rsaClientPrivKeyBufSz, keyFile);
  21458. XFCLOSE(keyFile);
  21459. if (rsaServerPrivKeyBuf != NULL) {
  21460. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  21461. if (!keyFile)
  21462. return -11725;
  21463. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  21464. *rsaServerPrivKeyBufSz, keyFile);
  21465. XFCLOSE(keyFile);
  21466. }
  21467. if (rsaCaPrivKeyBuf != NULL) {
  21468. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  21469. if (!keyFile)
  21470. return -11726;
  21471. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  21472. *rsaCaPrivKeyBufSz, keyFile);
  21473. XFCLOSE(keyFile);
  21474. }
  21475. #endif /* USE_CERT_BUFFERS */
  21476. #endif /* NO_RSA */
  21477. /* ECC */
  21478. #ifdef HAVE_ECC
  21479. #ifdef USE_CERT_BUFFERS_256
  21480. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  21481. return -11727;
  21482. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  21483. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  21484. #else
  21485. certFile = XFOPEN(eccClientCert, "rb");
  21486. if (!certFile)
  21487. return -11728;
  21488. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  21489. *eccClientCertBufSz, certFile);
  21490. XFCLOSE(certFile);
  21491. #endif /* USE_CERT_BUFFERS_256 */
  21492. #ifdef USE_CERT_BUFFERS_256
  21493. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  21494. return -11729;
  21495. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  21496. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  21497. #else
  21498. keyFile = XFOPEN(eccClientKey, "rb");
  21499. if (!keyFile)
  21500. return -11730;
  21501. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  21502. *eccClientPrivKeyBufSz, keyFile);
  21503. XFCLOSE(keyFile);
  21504. #endif /* USE_CERT_BUFFERS_256 */
  21505. #endif /* HAVE_ECC */
  21506. #ifdef NO_RSA
  21507. (void)rsaClientCertBuf;
  21508. (void)rsaClientCertBufSz;
  21509. (void)rsaClientPrivKeyBuf;
  21510. (void)rsaClientPrivKeyBufSz;
  21511. (void)rsaServerCertBuf;
  21512. (void)rsaServerCertBufSz;
  21513. (void)rsaServerPrivKeyBuf;
  21514. (void)rsaServerPrivKeyBufSz;
  21515. (void)rsaCaCertBuf;
  21516. (void)rsaCaCertBufSz;
  21517. (void)rsaCaPrivKeyBuf;
  21518. (void)rsaCaPrivKeyBufSz;
  21519. #endif
  21520. #ifndef HAVE_ECC
  21521. (void)eccClientCertBuf;
  21522. (void)eccClientCertBufSz;
  21523. (void)eccClientPrivKeyBuf;
  21524. (void)eccClientPrivKeyBufSz;
  21525. #endif
  21526. #ifndef NO_FILESYSTEM
  21527. (void)certFile;
  21528. (void)keyFile;
  21529. #endif
  21530. return 0;
  21531. }
  21532. typedef struct {
  21533. const byte* content;
  21534. word32 contentSz;
  21535. int contentOID;
  21536. int encryptOID;
  21537. int keyWrapOID;
  21538. int keyAgreeOID;
  21539. byte* cert;
  21540. size_t certSz;
  21541. byte* privateKey;
  21542. word32 privateKeySz;
  21543. byte* optionalUkm;
  21544. word32 optionalUkmSz;
  21545. int ktriOptions; /* KTRI options flags */
  21546. int kariOptions; /* KARI options flags */
  21547. /* KEKRI specific */
  21548. byte* secretKey; /* key, only for kekri RecipientInfo types */
  21549. word32 secretKeySz; /* size of secretKey, bytes */
  21550. byte* secretKeyId; /* key identifier */
  21551. word32 secretKeyIdSz; /* size of key identifier, bytes */
  21552. void* timePtr; /* time_t pointer */
  21553. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  21554. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  21555. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  21556. word32 otherAttrSz; /* size of otherAttr, bytes */
  21557. int kekriOptions; /* KEKRI options flags */
  21558. /* PWRI specific */
  21559. char* password;
  21560. word32 passwordSz;
  21561. byte* salt;
  21562. word32 saltSz;
  21563. int kdfOID;
  21564. int hashOID;
  21565. int kdfIterations;
  21566. int pwriOptions; /* PWRI options flags */
  21567. /* ORI specific */
  21568. int isOri;
  21569. int oriOptions; /* ORI options flags */
  21570. const char* outFileName;
  21571. } pkcs7EnvelopedVector;
  21572. static const byte asnDataOid[] = {
  21573. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  21574. };
  21575. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  21576. * and giving wolfCrypt the value for oriOID and oriValue to place in
  21577. * OtherRecipientInfo.
  21578. *
  21579. * Returns 0 on success, negative upon error. */
  21580. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  21581. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  21582. void* ctx)
  21583. {
  21584. int i;
  21585. /* make sure buffers are large enough */
  21586. if ((*oriValueSz < (2 + cekSz)) || (*oriTypeSz < sizeof(oriType)))
  21587. return -11731;
  21588. /* our simple encryption algorithm will be take the bitwise complement */
  21589. oriValue[0] = 0x04; /*ASN OCTET STRING */
  21590. oriValue[1] = (byte)cekSz; /* length */
  21591. for (i = 0; i < (int)cekSz; i++) {
  21592. oriValue[2 + i] = ~cek[i];
  21593. }
  21594. *oriValueSz = 2 + cekSz;
  21595. /* set oriType to ASN.1 encoded data OID */
  21596. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  21597. *oriTypeSz = sizeof(asnDataOid);
  21598. (void)pkcs7;
  21599. (void)ctx;
  21600. return 0;
  21601. }
  21602. /* ORI decrypt callback, responsible for providing a decrypted content
  21603. * encryption key (CEK) placed into decryptedKey and size placed into
  21604. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  21605. * in decrypting the encrypted CEK.
  21606. *
  21607. * Returns 0 on success, negative upon error. */
  21608. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  21609. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  21610. word32* decryptedKeySz, void* ctx)
  21611. {
  21612. int i;
  21613. /* make sure oriType matches what we expect */
  21614. if (oriTypeSz != sizeof(asnDataOid))
  21615. return -11732;
  21616. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  21617. return -11733;
  21618. /* make sure decrypted buffer is large enough */
  21619. if (*decryptedKeySz < oriValueSz)
  21620. return -11734;
  21621. /* decrypt encrypted CEK using simple bitwise complement,
  21622. only for example */
  21623. for (i = 0; i < (int)oriValueSz - 2; i++) {
  21624. decryptedKey[i] = ~oriValue[2 + i];
  21625. }
  21626. *decryptedKeySz = oriValueSz - 2;
  21627. (void)pkcs7;
  21628. (void)ctx;
  21629. return 0;
  21630. }
  21631. #ifndef NO_AES
  21632. /* returns 0 on success */
  21633. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  21634. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  21635. byte* in, int inSz, byte* out, void* usrCtx)
  21636. {
  21637. int keyId = -1, ret, keySz;
  21638. word32 keyIdSz = 8;
  21639. const byte* key;
  21640. byte keyIdRaw[8];
  21641. Aes aes;
  21642. /* looking for KEY ID
  21643. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  21644. */
  21645. const unsigned char OID[] = {
  21646. /* 0x06, 0x0B do not pass in tag and length */
  21647. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  21648. 0x01, 0x09, 0x10, 0x02, 0x25
  21649. };
  21650. const byte defKey[] = {
  21651. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  21652. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  21653. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  21654. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  21655. };
  21656. const byte altKey[] = {
  21657. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  21658. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  21659. };
  21660. /* test user context passed in */
  21661. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  21662. return -11735;
  21663. }
  21664. /* if needing to find keyIdSz can call with NULL */
  21665. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  21666. &keyIdSz);
  21667. if (ret != LENGTH_ONLY_E) {
  21668. printf("Unexpected error %d when getting keyIdSz\n", ret);
  21669. printf("Possibly no KEY ID attribute set\n");
  21670. return -11736;
  21671. }
  21672. else {
  21673. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  21674. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  21675. &keyIdSz);
  21676. if (ret < 0) {
  21677. return ret;
  21678. }
  21679. if (keyIdSz < 3) {
  21680. printf("keyIdSz is smaller than expected\n");
  21681. return -11737;
  21682. }
  21683. if (keyIdSz > 2 + sizeof(int)) {
  21684. printf("example case was only expecting a keyId of int size\n");
  21685. return -11738;
  21686. }
  21687. /* keyIdRaw[0] OCTET TAG */
  21688. /* keyIdRaw[1] Length */
  21689. #ifdef BIG_ENDIAN_ORDER
  21690. if (keyIdRaw[1] == 0x01) {
  21691. keyId = 1;
  21692. }
  21693. #else
  21694. keyId = *(int*)(keyIdRaw + 2);
  21695. #endif
  21696. }
  21697. /* Use keyID here if found to select key and decrypt in HSM or in this
  21698. * example just select key and do software decryption */
  21699. if (keyId == 1) {
  21700. key = altKey;
  21701. keySz = sizeof(altKey);
  21702. }
  21703. else {
  21704. key = defKey;
  21705. keySz = sizeof(defKey);
  21706. }
  21707. switch (encryptOID) {
  21708. case AES256CBCb:
  21709. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  21710. return BAD_FUNC_ARG;
  21711. break;
  21712. case AES128CBCb:
  21713. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  21714. return BAD_FUNC_ARG;
  21715. break;
  21716. default:
  21717. printf("Unsupported content cipher type for example");
  21718. return ALGO_ID_E;
  21719. };
  21720. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  21721. if (ret == 0) {
  21722. ret = wc_AesSetKey(&aes, key, keySz, iv, AES_DECRYPTION);
  21723. if (ret == 0)
  21724. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  21725. wc_AesFree(&aes);
  21726. }
  21727. (void)aad;
  21728. (void)aadSz;
  21729. (void)authTag;
  21730. (void)authTagSz;
  21731. return ret;
  21732. }
  21733. #endif /* NO_AES */
  21734. static int pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  21735. byte* rsaPrivKey, word32 rsaPrivKeySz,
  21736. byte* eccCert, word32 eccCertSz,
  21737. byte* eccPrivKey, word32 eccPrivKeySz)
  21738. {
  21739. int ret, testSz, i;
  21740. int envelopedSz, decodedSz;
  21741. byte enveloped[2048];
  21742. byte decoded[2048];
  21743. PKCS7* pkcs7;
  21744. WC_RNG rng;
  21745. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  21746. XFILE pkcs7File;
  21747. #endif
  21748. const byte data[] = { /* Hello World */
  21749. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  21750. 0x72,0x6c,0x64
  21751. };
  21752. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  21753. defined(WOLFSSL_SHA512)
  21754. byte optionalUkm[] = {
  21755. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  21756. };
  21757. #endif /* NO_AES */
  21758. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  21759. /* encryption key for kekri recipient types */
  21760. byte secretKey[] = {
  21761. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  21762. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  21763. };
  21764. /* encryption key identifier */
  21765. byte secretKeyId[] = {
  21766. 0x02,0x02,0x03,0x04
  21767. };
  21768. #endif
  21769. #if !defined(NO_PWDBASED) && !defined(NO_AES) && \
  21770. !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  21771. #ifndef HAVE_FIPS
  21772. char password[] = "password"; /* NOTE: Password is too short for FIPS */
  21773. #else
  21774. char password[] = "passwordFIPS_MODE";
  21775. #endif
  21776. byte salt[] = {
  21777. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  21778. };
  21779. #endif
  21780. const pkcs7EnvelopedVector testVectors[] =
  21781. {
  21782. /* key transport key encryption technique */
  21783. #ifndef NO_RSA
  21784. #ifndef NO_DES3
  21785. {data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  21786. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  21787. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  21788. "pkcs7envelopedDataDES3.der"},
  21789. #endif
  21790. #ifndef NO_AES
  21791. #ifdef WOLFSSL_AES_128
  21792. {data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  21793. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  21794. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  21795. "pkcs7envelopedDataAES128CBC.der"},
  21796. #endif
  21797. #ifdef WOLFSSL_AES_192
  21798. {data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  21799. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  21800. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  21801. "pkcs7envelopedDataAES192CBC.der"},
  21802. #endif
  21803. #ifdef WOLFSSL_AES_256
  21804. {data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  21805. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  21806. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  21807. "pkcs7envelopedDataAES256CBC.der"},
  21808. /* explicitly using SKID for SubjectKeyIdentifier */
  21809. {data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  21810. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  21811. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  21812. "pkcs7envelopedDataAES256CBC_SKID.der"},
  21813. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  21814. {data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  21815. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  21816. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  21817. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der"},
  21818. #endif
  21819. #endif /* NO_AES */
  21820. #endif
  21821. /* key agreement key encryption technique*/
  21822. #ifdef HAVE_ECC
  21823. #ifndef NO_AES
  21824. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  21825. {data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  21826. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  21827. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  21828. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  21829. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der"},
  21830. #endif
  21831. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  21832. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  21833. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  21834. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  21835. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  21836. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der"},
  21837. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  21838. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  21839. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  21840. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  21841. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  21842. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  21843. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der"},
  21844. /* with optional user keying material (ukm) */
  21845. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  21846. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  21847. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  21848. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  21849. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der"},
  21850. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  21851. #endif /* NO_AES */
  21852. #endif
  21853. /* kekri (KEKRecipientInfo) recipient types */
  21854. #ifndef NO_AES
  21855. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  21856. {data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  21857. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  21858. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  21859. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  21860. "pkcs7envelopedDataAES128CBC_KEKRI.der"},
  21861. #endif
  21862. #endif
  21863. /* pwri (PasswordRecipientInfo) recipient types */
  21864. #if !defined(NO_PWDBASED) && !defined(NO_AES)
  21865. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  21866. {data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  21867. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  21868. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  21869. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  21870. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der"},
  21871. #endif
  21872. #endif
  21873. #if !defined(NO_AES) && !defined(NO_AES_128)
  21874. /* ori (OtherRecipientInfo) recipient types */
  21875. {data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  21876. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  21877. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der"},
  21878. #endif
  21879. };
  21880. testSz = sizeof(testVectors) / sizeof(pkcs7EnvelopedVector);
  21881. #ifdef ECC_TIMING_RESISTANT
  21882. #ifndef HAVE_FIPS
  21883. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  21884. #else
  21885. ret = wc_InitRng(&rng);
  21886. #endif
  21887. if (ret != 0) {
  21888. return -11760;
  21889. }
  21890. #endif
  21891. for (i = 0; i < testSz; i++) {
  21892. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  21893. #ifdef WOLFSSL_ASYNC_CRYPT
  21894. INVALID_DEVID /* async PKCS7 is not supported */
  21895. #else
  21896. devId
  21897. #endif
  21898. );
  21899. if (pkcs7 == NULL)
  21900. return -11739;
  21901. if (testVectors[i].secretKey != NULL) {
  21902. /* KEKRI recipient type */
  21903. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  21904. if (ret != 0) {
  21905. return -11740;
  21906. }
  21907. pkcs7->content = (byte*)testVectors[i].content;
  21908. pkcs7->contentSz = testVectors[i].contentSz;
  21909. pkcs7->contentOID = testVectors[i].contentOID;
  21910. pkcs7->encryptOID = testVectors[i].encryptOID;
  21911. pkcs7->ukm = testVectors[i].optionalUkm;
  21912. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  21913. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  21914. testVectors[i].secretKey, testVectors[i].secretKeySz,
  21915. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  21916. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  21917. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  21918. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  21919. if (ret < 0) {
  21920. wc_PKCS7_Free(pkcs7);
  21921. return -11741;
  21922. }
  21923. /* set key, for decryption */
  21924. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  21925. testVectors[i].secretKeySz);
  21926. if (ret != 0) {
  21927. wc_PKCS7_Free(pkcs7);
  21928. return -11742;
  21929. }
  21930. } else if (testVectors[i].password != NULL) {
  21931. #ifndef NO_PWDBASED
  21932. /* PWRI recipient type */
  21933. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  21934. if (ret != 0) {
  21935. return -11743;
  21936. }
  21937. pkcs7->content = (byte*)testVectors[i].content;
  21938. pkcs7->contentSz = testVectors[i].contentSz;
  21939. pkcs7->contentOID = testVectors[i].contentOID;
  21940. pkcs7->encryptOID = testVectors[i].encryptOID;
  21941. pkcs7->ukm = testVectors[i].optionalUkm;
  21942. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  21943. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  21944. (byte*)testVectors[i].password,
  21945. testVectors[i].passwordSz, testVectors[i].salt,
  21946. testVectors[i].saltSz, testVectors[i].kdfOID,
  21947. testVectors[i].hashOID, testVectors[i].kdfIterations,
  21948. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  21949. if (ret < 0) {
  21950. wc_PKCS7_Free(pkcs7);
  21951. return -11744;
  21952. }
  21953. /* set password, for decryption */
  21954. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  21955. testVectors[i].passwordSz);
  21956. if (ret < 0) {
  21957. wc_PKCS7_Free(pkcs7);
  21958. return -11745;
  21959. }
  21960. #endif /* NO_PWDBASED */
  21961. } else if (testVectors[i].isOri == 1) {
  21962. /* ORI recipient type */
  21963. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  21964. if (ret != 0) {
  21965. return -11746;
  21966. }
  21967. pkcs7->content = (byte*)testVectors[i].content;
  21968. pkcs7->contentSz = testVectors[i].contentSz;
  21969. pkcs7->contentOID = testVectors[i].contentOID;
  21970. pkcs7->encryptOID = testVectors[i].encryptOID;
  21971. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  21972. testVectors[i].oriOptions);
  21973. if (ret < 0) {
  21974. wc_PKCS7_Free(pkcs7);
  21975. return -11747;
  21976. }
  21977. /* set decrypt callback for decryption */
  21978. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  21979. if (ret < 0) {
  21980. wc_PKCS7_Free(pkcs7);
  21981. return -11748;
  21982. }
  21983. } else {
  21984. /* KTRI or KARI recipient types */
  21985. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  21986. if (ret != 0) {
  21987. return -11749;
  21988. }
  21989. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  21990. (word32)testVectors[i].certSz);
  21991. if (ret != 0) {
  21992. wc_PKCS7_Free(pkcs7);
  21993. return -11750;
  21994. }
  21995. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  21996. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  21997. pkcs7->privateKey = testVectors[i].privateKey;
  21998. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  21999. pkcs7->content = (byte*)testVectors[i].content;
  22000. pkcs7->contentSz = testVectors[i].contentSz;
  22001. pkcs7->contentOID = testVectors[i].contentOID;
  22002. pkcs7->encryptOID = testVectors[i].encryptOID;
  22003. pkcs7->ukm = testVectors[i].optionalUkm;
  22004. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  22005. /* set SubjectIdentifier type for KTRI types */
  22006. if (testVectors[i].ktriOptions & CMS_SKID) {
  22007. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  22008. if (ret != 0) {
  22009. wc_PKCS7_Free(pkcs7);
  22010. return -11751;
  22011. }
  22012. } else if (testVectors[i].ktriOptions &
  22013. CMS_ISSUER_AND_SERIAL_NUMBER) {
  22014. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  22015. CMS_ISSUER_AND_SERIAL_NUMBER);
  22016. if (ret != 0) {
  22017. wc_PKCS7_Free(pkcs7);
  22018. return -11752;
  22019. }
  22020. }
  22021. }
  22022. #ifdef ECC_TIMING_RESISTANT
  22023. pkcs7->rng = &rng;
  22024. #endif
  22025. /* encode envelopedData */
  22026. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  22027. sizeof(enveloped));
  22028. if (envelopedSz <= 0) {
  22029. wc_PKCS7_Free(pkcs7);
  22030. return -11753;
  22031. }
  22032. /* decode envelopedData */
  22033. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, envelopedSz,
  22034. decoded, sizeof(decoded));
  22035. if (decodedSz <= 0) {
  22036. wc_PKCS7_Free(pkcs7);
  22037. return -11754;
  22038. }
  22039. /* test decode result */
  22040. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  22041. wc_PKCS7_Free(pkcs7);
  22042. return -11755;
  22043. }
  22044. #ifndef NO_PKCS7_STREAM
  22045. { /* test reading byte by byte */
  22046. int z;
  22047. for (z = 0; z < envelopedSz; z++) {
  22048. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  22049. decoded, sizeof(decoded));
  22050. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  22051. printf("unexpected error %d\n", decodedSz);
  22052. return -11756;
  22053. }
  22054. }
  22055. /* test decode result */
  22056. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  22057. printf("stream read compare failed\n");
  22058. wc_PKCS7_Free(pkcs7);
  22059. return -11757;
  22060. }
  22061. }
  22062. #endif
  22063. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  22064. /* output pkcs7 envelopedData for external testing */
  22065. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  22066. if (!pkcs7File) {
  22067. wc_PKCS7_Free(pkcs7);
  22068. return -11758;
  22069. }
  22070. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  22071. XFCLOSE(pkcs7File);
  22072. if (ret != envelopedSz) {
  22073. wc_PKCS7_Free(pkcs7);
  22074. return -11759;
  22075. }
  22076. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  22077. wc_PKCS7_Free(pkcs7);
  22078. pkcs7 = NULL;
  22079. }
  22080. #ifdef ECC_TIMING_RESISTANT
  22081. wc_FreeRng(&rng);
  22082. #endif
  22083. (void)eccCert;
  22084. (void)eccCertSz;
  22085. (void)eccPrivKey;
  22086. (void)eccPrivKeySz;
  22087. (void)rsaCert;
  22088. (void)rsaCertSz;
  22089. (void)rsaPrivKey;
  22090. (void)rsaPrivKeySz;
  22091. return 0;
  22092. }
  22093. int pkcs7enveloped_test(void)
  22094. {
  22095. int ret = 0;
  22096. byte* rsaCert = NULL;
  22097. byte* rsaPrivKey = NULL;
  22098. word32 rsaCertSz = 0;
  22099. word32 rsaPrivKeySz = 0;
  22100. byte* eccCert = NULL;
  22101. byte* eccPrivKey = NULL;
  22102. word32 eccCertSz = 0;
  22103. word32 eccPrivKeySz = 0;
  22104. #ifndef NO_RSA
  22105. /* read client RSA cert and key in DER format */
  22106. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22107. if (rsaCert == NULL)
  22108. return -11800;
  22109. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22110. if (rsaPrivKey == NULL) {
  22111. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22112. return -11801;
  22113. }
  22114. rsaCertSz = FOURK_BUF;
  22115. rsaPrivKeySz = FOURK_BUF;
  22116. #endif /* NO_RSA */
  22117. #ifdef HAVE_ECC
  22118. /* read client ECC cert and key in DER format */
  22119. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22120. if (eccCert == NULL) {
  22121. #ifndef NO_RSA
  22122. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22123. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22124. #endif
  22125. return -11802;
  22126. }
  22127. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22128. if (eccPrivKey == NULL) {
  22129. #ifndef NO_RSA
  22130. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22131. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22132. #endif
  22133. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22134. return -11803;
  22135. }
  22136. eccCertSz = FOURK_BUF;
  22137. eccPrivKeySz = FOURK_BUF;
  22138. #endif /* HAVE_ECC */
  22139. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  22140. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  22141. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  22142. eccPrivKey, &eccPrivKeySz);
  22143. if (ret < 0) {
  22144. #ifndef NO_RSA
  22145. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22146. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22147. #endif
  22148. #ifdef HAVE_ECC
  22149. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22150. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22151. #endif
  22152. return -11804;
  22153. }
  22154. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  22155. rsaPrivKey, (word32)rsaPrivKeySz,
  22156. eccCert, (word32)eccCertSz,
  22157. eccPrivKey, (word32)eccPrivKeySz);
  22158. #ifndef NO_RSA
  22159. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22160. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22161. #endif
  22162. #ifdef HAVE_ECC
  22163. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22164. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22165. #endif
  22166. return ret;
  22167. }
  22168. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  22169. typedef struct {
  22170. const byte* content;
  22171. word32 contentSz;
  22172. int contentOID;
  22173. int encryptOID;
  22174. int keyWrapOID;
  22175. int keyAgreeOID;
  22176. byte* cert;
  22177. size_t certSz;
  22178. byte* privateKey;
  22179. word32 privateKeySz;
  22180. PKCS7Attrib* authAttribs;
  22181. word32 authAttribsSz;
  22182. PKCS7Attrib* unauthAttribs;
  22183. word32 unauthAttribsSz;
  22184. /* KARI / KTRI specific */
  22185. byte* optionalUkm;
  22186. word32 optionalUkmSz;
  22187. int ktriOptions; /* KTRI options flags */
  22188. int kariOptions; /* KARI options flags */
  22189. /* KEKRI specific */
  22190. byte* secretKey; /* key, only for kekri RecipientInfo types */
  22191. word32 secretKeySz; /* size of secretKey, bytes */
  22192. byte* secretKeyId; /* key identifier */
  22193. word32 secretKeyIdSz; /* size of key identifier, bytes */
  22194. void* timePtr; /* time_t pointer */
  22195. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  22196. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  22197. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  22198. word32 otherAttrSz; /* size of otherAttr, bytes */
  22199. int kekriOptions; /* KEKRI options flags */
  22200. /* PWRI specific */
  22201. char* password; /* password */
  22202. word32 passwordSz; /* password size, bytes */
  22203. byte* salt; /* KDF salt */
  22204. word32 saltSz; /* KDF salt size, bytes */
  22205. int kdfOID; /* KDF OID */
  22206. int hashOID; /* KDF hash algorithm OID */
  22207. int kdfIterations; /* KDF iterations */
  22208. int kekEncryptOID; /* KEK encryption algorithm OID */
  22209. int pwriOptions; /* PWRI options flags */
  22210. /* ORI specific */
  22211. int isOri;
  22212. int oriOptions; /* ORI options flags */
  22213. const char* outFileName;
  22214. } pkcs7AuthEnvelopedVector;
  22215. static int pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  22216. byte* rsaPrivKey, word32 rsaPrivKeySz,
  22217. byte* eccCert, word32 eccCertSz,
  22218. byte* eccPrivKey, word32 eccPrivKeySz)
  22219. {
  22220. int ret, testSz, i;
  22221. int envelopedSz, decodedSz;
  22222. byte enveloped[2048];
  22223. byte decoded[2048];
  22224. WC_RNG rng;
  22225. PKCS7* pkcs7;
  22226. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  22227. XFILE pkcs7File;
  22228. #endif
  22229. const byte data[] = { /* Hello World */
  22230. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  22231. 0x72,0x6c,0x64
  22232. };
  22233. byte senderNonce[PKCS7_NONCE_SZ + 2];
  22234. #ifdef HAVE_ECC
  22235. byte senderNonceOid[] =
  22236. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  22237. 0x09, 0x05 };
  22238. PKCS7Attrib attribs[] =
  22239. {
  22240. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  22241. sizeof(senderNonce) }
  22242. };
  22243. #endif
  22244. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  22245. defined(WOLFSSL_SHA512)
  22246. byte optionalUkm[] = {
  22247. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  22248. };
  22249. #endif /* NO_AES */
  22250. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  22251. /* encryption key for kekri recipient types */
  22252. byte secretKey[] = {
  22253. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  22254. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  22255. };
  22256. /* encryption key identifier */
  22257. byte secretKeyId[] = {
  22258. 0x02,0x02,0x03,0x04
  22259. };
  22260. #endif
  22261. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  22262. !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  22263. #ifndef HAVE_FIPS
  22264. char password[] = "password";
  22265. #else
  22266. char password[] = "passwordFIPS_MODE";
  22267. #endif
  22268. byte salt[] = {
  22269. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  22270. };
  22271. #endif
  22272. const pkcs7AuthEnvelopedVector testVectors[] =
  22273. {
  22274. /* key transport key encryption technique */
  22275. #ifndef NO_RSA
  22276. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  22277. #ifdef WOLFSSL_AES_128
  22278. {data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  22279. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  22280. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  22281. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der"},
  22282. #endif
  22283. #ifdef WOLFSSL_AES_192
  22284. {data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  22285. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  22286. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  22287. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der"},
  22288. #endif
  22289. #ifdef WOLFSSL_AES_256
  22290. {data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  22291. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  22292. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  22293. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der"},
  22294. /* test with contentType set to FirmwarePkgData */
  22295. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  22296. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  22297. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  22298. 0, 0, 0, 0, 0, 0, 0, 0,
  22299. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der"},
  22300. /* explicitly using SKID for SubjectKeyIdentifier */
  22301. {data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  22302. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  22303. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  22304. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der"},
  22305. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  22306. {data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  22307. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  22308. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  22309. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  22310. "pkcs7authEnvelopedDataAES256GCM_IANDS.der"},
  22311. #endif
  22312. #endif /* NO_AES */
  22313. #endif
  22314. /* key agreement key encryption technique*/
  22315. #ifdef HAVE_ECC
  22316. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  22317. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  22318. {data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  22319. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  22320. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  22321. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  22322. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der"},
  22323. #endif
  22324. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  22325. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  22326. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  22327. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  22328. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  22329. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der"},
  22330. /* with authenticated attributes */
  22331. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  22332. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  22333. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  22334. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  22335. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  22336. 0, 0, 0,
  22337. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der"},
  22338. /* with unauthenticated attributes */
  22339. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  22340. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  22341. eccPrivKeySz, NULL, 0, attribs,
  22342. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  22343. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  22344. 0, 0, 0,
  22345. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der"},
  22346. /* with authenticated AND unauthenticated attributes */
  22347. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  22348. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  22349. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  22350. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  22351. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  22352. 0, 0, 0, 0, 0, 0,
  22353. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der"},
  22354. /* with authenticated AND unauthenticated attributes AND
  22355. * contentType of FirmwarePkgData */
  22356. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  22357. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  22358. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  22359. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  22360. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  22361. 0, 0, 0, 0, 0, 0,
  22362. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der"},
  22363. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  22364. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  22365. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  22366. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  22367. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  22368. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  22369. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der"},
  22370. /* with optional user keying material (ukm) */
  22371. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  22372. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  22373. eccPrivKeySz, NULL, 0, NULL, 0, optionalUkm, sizeof(optionalUkm), 0,
  22374. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  22375. 0, 0, 0, 0, 0, 0,
  22376. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der"},
  22377. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  22378. #endif /* NO_AES */
  22379. #endif
  22380. /* kekri (KEKRecipientInfo) recipient types */
  22381. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  22382. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  22383. {data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  22384. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  22385. secretKey, sizeof(secretKey), secretKeyId, sizeof(secretKeyId),
  22386. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  22387. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der"},
  22388. #endif
  22389. #endif
  22390. /* pwri (PasswordRecipientInfo) recipient types */
  22391. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  22392. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  22393. {data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  22394. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  22395. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  22396. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  22397. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der"},
  22398. #endif
  22399. #endif
  22400. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  22401. #ifdef WOLFSSL_AES_128
  22402. /* ori (OtherRecipientInfo) recipient types */
  22403. {data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  22404. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  22405. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  22406. "pkcs7authEnvelopedDataAES128GCM_ORI.der"},
  22407. #endif
  22408. #endif
  22409. };
  22410. testSz = sizeof(testVectors) / sizeof(pkcs7AuthEnvelopedVector);
  22411. /* generate senderNonce */
  22412. {
  22413. #ifndef HAVE_FIPS
  22414. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  22415. #else
  22416. ret = wc_InitRng(&rng);
  22417. #endif
  22418. if (ret != 0) {
  22419. return -11805;
  22420. }
  22421. senderNonce[0] = 0x04;
  22422. senderNonce[1] = PKCS7_NONCE_SZ;
  22423. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  22424. if (ret != 0) {
  22425. wc_FreeRng(&rng);
  22426. return -11806;
  22427. }
  22428. }
  22429. for (i = 0; i < testSz; i++) {
  22430. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  22431. #ifdef WOLFSSL_ASYNC_CRYPT
  22432. INVALID_DEVID /* async PKCS7 is not supported */
  22433. #else
  22434. devId
  22435. #endif
  22436. );
  22437. if (pkcs7 == NULL)
  22438. return -11807;
  22439. if (testVectors[i].secretKey != NULL) {
  22440. /* KEKRI recipient type */
  22441. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  22442. if (ret != 0) {
  22443. return -11808;
  22444. }
  22445. pkcs7->content = (byte*)testVectors[i].content;
  22446. pkcs7->contentSz = testVectors[i].contentSz;
  22447. pkcs7->contentOID = testVectors[i].contentOID;
  22448. pkcs7->encryptOID = testVectors[i].encryptOID;
  22449. pkcs7->ukm = testVectors[i].optionalUkm;
  22450. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  22451. pkcs7->authAttribs = testVectors[i].authAttribs;
  22452. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  22453. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  22454. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  22455. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  22456. testVectors[i].secretKey, testVectors[i].secretKeySz,
  22457. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  22458. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  22459. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  22460. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  22461. if (ret < 0) {
  22462. wc_PKCS7_Free(pkcs7);
  22463. return -11809;
  22464. }
  22465. /* set key, for decryption */
  22466. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  22467. testVectors[i].secretKeySz);
  22468. if (ret != 0) {
  22469. wc_PKCS7_Free(pkcs7);
  22470. return -11810;
  22471. }
  22472. } else if (testVectors[i].password != NULL) {
  22473. #ifndef NO_PWDBASED
  22474. /* PWRI recipient type */
  22475. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  22476. if (ret != 0) {
  22477. return -11811;
  22478. }
  22479. pkcs7->content = (byte*)testVectors[i].content;
  22480. pkcs7->contentSz = testVectors[i].contentSz;
  22481. pkcs7->contentOID = testVectors[i].contentOID;
  22482. pkcs7->encryptOID = testVectors[i].encryptOID;
  22483. pkcs7->ukm = testVectors[i].optionalUkm;
  22484. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  22485. pkcs7->authAttribs = testVectors[i].authAttribs;
  22486. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  22487. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  22488. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  22489. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  22490. (byte*)testVectors[i].password,
  22491. testVectors[i].passwordSz, testVectors[i].salt,
  22492. testVectors[i].saltSz, testVectors[i].kdfOID,
  22493. testVectors[i].hashOID, testVectors[i].kdfIterations,
  22494. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  22495. if (ret < 0) {
  22496. wc_PKCS7_Free(pkcs7);
  22497. return -11812;
  22498. }
  22499. /* set password, for decryption */
  22500. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  22501. testVectors[i].passwordSz);
  22502. if (ret < 0) {
  22503. wc_PKCS7_Free(pkcs7);
  22504. return -11813;
  22505. }
  22506. #endif /* NO_PWDBASED */
  22507. } else if (testVectors[i].isOri == 1) {
  22508. /* ORI recipient type */
  22509. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  22510. if (ret != 0) {
  22511. return -11814;
  22512. }
  22513. pkcs7->content = (byte*)testVectors[i].content;
  22514. pkcs7->contentSz = testVectors[i].contentSz;
  22515. pkcs7->contentOID = testVectors[i].contentOID;
  22516. pkcs7->encryptOID = testVectors[i].encryptOID;
  22517. pkcs7->authAttribs = testVectors[i].authAttribs;
  22518. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  22519. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  22520. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  22521. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  22522. testVectors[i].oriOptions);
  22523. if (ret < 0) {
  22524. wc_PKCS7_Free(pkcs7);
  22525. return -11815;
  22526. }
  22527. /* set decrypt callback for decryption */
  22528. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  22529. if (ret < 0) {
  22530. wc_PKCS7_Free(pkcs7);
  22531. return -11816;
  22532. }
  22533. } else {
  22534. /* KTRI or KARI recipient types */
  22535. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  22536. (word32)testVectors[i].certSz);
  22537. if (ret != 0) {
  22538. wc_PKCS7_Free(pkcs7);
  22539. return -11817;
  22540. }
  22541. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  22542. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  22543. pkcs7->privateKey = testVectors[i].privateKey;
  22544. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  22545. pkcs7->content = (byte*)testVectors[i].content;
  22546. pkcs7->contentSz = testVectors[i].contentSz;
  22547. pkcs7->contentOID = testVectors[i].contentOID;
  22548. pkcs7->encryptOID = testVectors[i].encryptOID;
  22549. pkcs7->ukm = testVectors[i].optionalUkm;
  22550. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  22551. pkcs7->authAttribs = testVectors[i].authAttribs;
  22552. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  22553. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  22554. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  22555. /* set SubjectIdentifier type for KTRI types */
  22556. if (testVectors[i].ktriOptions & CMS_SKID) {
  22557. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  22558. if (ret != 0) {
  22559. wc_PKCS7_Free(pkcs7);
  22560. return -11818;
  22561. }
  22562. } else if (testVectors[i].ktriOptions &
  22563. CMS_ISSUER_AND_SERIAL_NUMBER) {
  22564. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  22565. CMS_ISSUER_AND_SERIAL_NUMBER);
  22566. if (ret != 0) {
  22567. wc_PKCS7_Free(pkcs7);
  22568. return -11819;
  22569. }
  22570. }
  22571. }
  22572. #ifdef ECC_TIMING_RESISTANT
  22573. pkcs7->rng = &rng;
  22574. #endif
  22575. /* encode envelopedData */
  22576. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  22577. sizeof(enveloped));
  22578. if (envelopedSz <= 0) {
  22579. wc_PKCS7_Free(pkcs7);
  22580. return -11820;
  22581. }
  22582. #ifndef NO_PKCS7_STREAM
  22583. { /* test reading byte by byte */
  22584. int z;
  22585. for (z = 0; z < envelopedSz; z++) {
  22586. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  22587. enveloped + z, 1, decoded, sizeof(decoded));
  22588. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  22589. printf("unexpected error %d\n", decodedSz);
  22590. return -11821;
  22591. }
  22592. }
  22593. /* test decode result */
  22594. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  22595. printf("stream read compare failed\n");
  22596. wc_PKCS7_Free(pkcs7);
  22597. return -11822;
  22598. }
  22599. }
  22600. #endif
  22601. /* decode envelopedData */
  22602. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  22603. envelopedSz, decoded,
  22604. sizeof(decoded));
  22605. if (decodedSz <= 0) {
  22606. wc_PKCS7_Free(pkcs7);
  22607. return -11823;
  22608. }
  22609. /* test decode result */
  22610. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  22611. wc_PKCS7_Free(pkcs7);
  22612. return -11824;
  22613. }
  22614. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  22615. /* output pkcs7 envelopedData for external testing */
  22616. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  22617. if (!pkcs7File) {
  22618. wc_PKCS7_Free(pkcs7);
  22619. return -11825;
  22620. }
  22621. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  22622. XFCLOSE(pkcs7File);
  22623. if (ret != envelopedSz) {
  22624. wc_PKCS7_Free(pkcs7);
  22625. return -11826;
  22626. }
  22627. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  22628. wc_PKCS7_Free(pkcs7);
  22629. pkcs7 = NULL;
  22630. }
  22631. wc_FreeRng(&rng);
  22632. #if !defined(HAVE_ECC) || defined(NO_AES)
  22633. (void)eccCert;
  22634. (void)eccCertSz;
  22635. (void)eccPrivKey;
  22636. (void)eccPrivKeySz;
  22637. (void)secretKey;
  22638. (void)secretKeyId;
  22639. #endif
  22640. #ifdef NO_RSA
  22641. (void)rsaCert;
  22642. (void)rsaCertSz;
  22643. (void)rsaPrivKey;
  22644. (void)rsaPrivKeySz;
  22645. #endif
  22646. return 0;
  22647. }
  22648. int pkcs7authenveloped_test(void)
  22649. {
  22650. int ret = 0;
  22651. byte* rsaCert = NULL;
  22652. byte* rsaPrivKey = NULL;
  22653. word32 rsaCertSz = 0;
  22654. word32 rsaPrivKeySz = 0;
  22655. byte* eccCert = NULL;
  22656. byte* eccPrivKey = NULL;
  22657. word32 eccCertSz = 0;
  22658. word32 eccPrivKeySz = 0;
  22659. #ifndef NO_RSA
  22660. /* read client RSA cert and key in DER format */
  22661. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22662. if (rsaCert == NULL)
  22663. return -11900;
  22664. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22665. if (rsaPrivKey == NULL) {
  22666. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22667. return -11901;
  22668. }
  22669. rsaCertSz = FOURK_BUF;
  22670. rsaPrivKeySz = FOURK_BUF;
  22671. #endif /* NO_RSA */
  22672. #ifdef HAVE_ECC
  22673. /* read client ECC cert and key in DER format */
  22674. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22675. if (eccCert == NULL) {
  22676. #ifndef NO_RSA
  22677. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22678. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22679. #endif
  22680. return -11902;
  22681. }
  22682. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22683. if (eccPrivKey == NULL) {
  22684. #ifndef NO_RSA
  22685. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22686. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22687. #endif
  22688. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22689. return -11903;
  22690. }
  22691. eccCertSz = FOURK_BUF;
  22692. eccPrivKeySz = FOURK_BUF;
  22693. #endif /* HAVE_ECC */
  22694. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  22695. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  22696. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  22697. eccPrivKey, &eccPrivKeySz);
  22698. if (ret < 0) {
  22699. #ifndef NO_RSA
  22700. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22701. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22702. #endif
  22703. #ifdef HAVE_ECC
  22704. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22705. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22706. #endif
  22707. return -11904;
  22708. }
  22709. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  22710. rsaPrivKey, (word32)rsaPrivKeySz,
  22711. eccCert, (word32)eccCertSz,
  22712. eccPrivKey, (word32)eccPrivKeySz);
  22713. #ifndef NO_RSA
  22714. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22715. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22716. #endif
  22717. #ifdef HAVE_ECC
  22718. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22719. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22720. #endif
  22721. return ret;
  22722. }
  22723. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  22724. #ifndef NO_AES
  22725. static const byte p7DefKey[] = {
  22726. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22727. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22728. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22729. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  22730. };
  22731. static const byte p7AltKey[] = {
  22732. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22733. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  22734. };
  22735. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  22736. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  22737. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  22738. {
  22739. int ret;
  22740. if (cek == NULL || out == NULL)
  22741. return BAD_FUNC_ARG;
  22742. /* test case sanity checks */
  22743. if (keyIdSz != 1) {
  22744. return -11905;
  22745. }
  22746. if (keyId[0] != 0x00) {
  22747. return -11906;
  22748. }
  22749. if (type != (int)PKCS7_KEKRI) {
  22750. return -11907;
  22751. }
  22752. switch (keyWrapAlgo) {
  22753. case AES256_WRAP:
  22754. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  22755. out, outSz, NULL);
  22756. if (ret <= 0)
  22757. return ret;
  22758. break;
  22759. default:
  22760. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  22761. return BAD_KEYWRAP_ALG_E;
  22762. };
  22763. (void)pkcs7;
  22764. (void)direction;
  22765. (void)orginKey; /* used with KAKRI */
  22766. (void)orginKeySz;
  22767. return ret;
  22768. }
  22769. /* returns key size on success */
  22770. static int getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  22771. {
  22772. int ret;
  22773. word32 atrSz;
  22774. byte atr[256];
  22775. /* Additionally can look for fwWrappedFirmwareKey
  22776. * 1.2.840.113529.1.9.16.1.16 */
  22777. const unsigned char fwWrappedFirmwareKey[] = {
  22778. /* 0x06, 0x0B */
  22779. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  22780. 0x01, 0x09, 0x10, 0x02, 0x27
  22781. };
  22782. /* find keyID in fwWrappedFirmwareKey */
  22783. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  22784. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  22785. if (ret == LENGTH_ONLY_E) {
  22786. XMEMSET(atr, 0, sizeof(atr));
  22787. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  22788. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  22789. /* keyIdRaw[0] OCTET TAG */
  22790. /* keyIdRaw[1] Length */
  22791. if (ret > 0) {
  22792. PKCS7* envPkcs7;
  22793. envPkcs7 = wc_PKCS7_New(NULL, 0);
  22794. if (envPkcs7 == NULL) {
  22795. return MEMORY_E;
  22796. }
  22797. wc_PKCS7_Init(envPkcs7, NULL, 0);
  22798. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  22799. if (ret == 0) {
  22800. /* expecting FIRMWARE_PKG_DATA content */
  22801. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  22802. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  22803. key, keySz);
  22804. }
  22805. wc_PKCS7_Free(envPkcs7);
  22806. }
  22807. }
  22808. return ret;
  22809. }
  22810. /* create a KEKRI enveloped data
  22811. * return size on success */
  22812. static int envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  22813. word32 outSz)
  22814. {
  22815. int ret;
  22816. PKCS7* pkcs7;
  22817. const byte keyId[] = { 0x00 };
  22818. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  22819. if (pkcs7 == NULL)
  22820. return -11908;
  22821. pkcs7->content = in;
  22822. pkcs7->contentSz = inSz;
  22823. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  22824. pkcs7->encryptOID = AES256CBCb;
  22825. pkcs7->ukm = NULL;
  22826. pkcs7->ukmSz = 0;
  22827. /* add recipient (KEKRI type) */
  22828. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  22829. sizeof(p7DefKey), (byte*)keyId,
  22830. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  22831. if (ret < 0) {
  22832. printf("wc_PKCS7_AddRecipient_KEKRI() failed, ret = %d\n", ret);
  22833. wc_PKCS7_Free(pkcs7);
  22834. return -11909;
  22835. }
  22836. /* encode envelopedData, returns size */
  22837. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  22838. if (ret <= 0) {
  22839. printf("wc_PKCS7_EncodeEnvelopedData() failed, ret = %d\n", ret);
  22840. wc_PKCS7_Free(pkcs7);
  22841. return -11910;
  22842. }
  22843. wc_PKCS7_Free(pkcs7);
  22844. return ret;
  22845. }
  22846. /*
  22847. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  22848. * returns size of buffer output on success
  22849. */
  22850. static int generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  22851. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  22852. byte* key, word32 keySz)
  22853. {
  22854. int ret, attribNum = 1;
  22855. PKCS7* pkcs7;
  22856. /* KEY ID
  22857. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  22858. */
  22859. const unsigned char fwDecryptKeyID[] = {
  22860. 0x06, 0x0B,
  22861. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  22862. 0x01, 0x09, 0x10, 0x02, 0x25
  22863. };
  22864. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  22865. const unsigned char fwWrappedFirmwareKey[] = {
  22866. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  22867. 0x01, 0x09, 0x10, 0x02, 0x27
  22868. };
  22869. byte keyID[] = { 0x04, 0x01, 0x00 };
  22870. byte env[256];
  22871. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  22872. PKCS7Attrib attribs[] =
  22873. {
  22874. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  22875. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  22876. };
  22877. keyID[2] = keyHint;
  22878. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  22879. if (keyHint == 0) {
  22880. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  22881. sizeof(env));
  22882. if (ret <= 0) {
  22883. return ret;
  22884. }
  22885. attribs[1].valueSz = ret;
  22886. attribNum++;
  22887. }
  22888. /* init PKCS7 */
  22889. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  22890. if (pkcs7 == NULL)
  22891. return -11911;
  22892. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  22893. if (ret != 0) {
  22894. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  22895. wc_PKCS7_Free(pkcs7);
  22896. return -11912;
  22897. }
  22898. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  22899. if (ret != 0) {
  22900. wc_PKCS7_Free(pkcs7);
  22901. return -11913;
  22902. }
  22903. /* encode Signed Encrypted FirmwarePkgData */
  22904. if (encryptKeySz == 16) {
  22905. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  22906. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  22907. (byte*)data, sizeof(data), NULL, 0,
  22908. attribs, attribNum, out, *outSz);
  22909. }
  22910. else {
  22911. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  22912. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  22913. (byte*)data, sizeof(data), NULL, 0,
  22914. attribs, attribNum, out, *outSz);
  22915. }
  22916. if (ret <= 0) {
  22917. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  22918. "ret = %d\n", ret);
  22919. wc_PKCS7_Free(pkcs7);
  22920. return -11914;
  22921. } else {
  22922. *outSz = ret;
  22923. }
  22924. wc_PKCS7_Free(pkcs7);
  22925. return ret;
  22926. }
  22927. /* test verification and decryption of PKCS7 bundle
  22928. * return 0 on success
  22929. */
  22930. static int verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  22931. {
  22932. int ret = 0;
  22933. int usrCtx = 1; /* test value to pass as user context to callback */
  22934. PKCS7* pkcs7;
  22935. byte* sid;
  22936. word32 sidSz;
  22937. byte key[256];
  22938. word32 keySz = sizeof(key);
  22939. byte decoded[FOURK_BUF/2];
  22940. int decodedSz = FOURK_BUF/2;
  22941. const byte expectedSid[] = {
  22942. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  22943. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  22944. 0xD7, 0x85, 0x65, 0xC0
  22945. };
  22946. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  22947. if (pkcs7 == NULL) {
  22948. return MEMORY_E;
  22949. }
  22950. /* Test verify */
  22951. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  22952. if (ret != 0) {
  22953. wc_PKCS7_Free(pkcs7);
  22954. return ret;
  22955. }
  22956. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  22957. if (ret != 0) {
  22958. wc_PKCS7_Free(pkcs7);
  22959. return ret;
  22960. }
  22961. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  22962. if (ret != 0) {
  22963. wc_PKCS7_Free(pkcs7);
  22964. return ret;
  22965. }
  22966. /* Get size of SID and print it out */
  22967. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  22968. if (ret != LENGTH_ONLY_E) {
  22969. wc_PKCS7_Free(pkcs7);
  22970. return ret;
  22971. }
  22972. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22973. if (sid == NULL) {
  22974. wc_PKCS7_Free(pkcs7);
  22975. return ret;
  22976. }
  22977. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  22978. if (ret != 0) {
  22979. wc_PKCS7_Free(pkcs7);
  22980. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22981. return ret;
  22982. }
  22983. ret = XMEMCMP(sid, expectedSid, sidSz);
  22984. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22985. if (ret != 0) {
  22986. wc_PKCS7_Free(pkcs7);
  22987. return ret;
  22988. }
  22989. /* get expected fwWrappedFirmwareKey */
  22990. if (keyHint == 0) {
  22991. ret = getFirmwareKey(pkcs7, key, keySz);
  22992. if (ret < 0) {
  22993. wc_PKCS7_Free(pkcs7);
  22994. return ret;
  22995. }
  22996. pkcs7->encryptionKey = key;
  22997. pkcs7->encryptionKeySz = ret;
  22998. }
  22999. else {
  23000. decodedSz = sizeof(decoded);
  23001. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  23002. if (ret != 0) {
  23003. wc_PKCS7_Free(pkcs7);
  23004. return ret;
  23005. }
  23006. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  23007. if (ret != 0) {
  23008. wc_PKCS7_Free(pkcs7);
  23009. return ret;
  23010. }
  23011. }
  23012. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  23013. pkcs7->contentSz, decoded, decodedSz);
  23014. if (decodedSz < 0) {
  23015. ret = decodedSz;
  23016. wc_PKCS7_Free(pkcs7);
  23017. return ret;
  23018. }
  23019. wc_PKCS7_Free(pkcs7);
  23020. return 0;
  23021. }
  23022. int pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  23023. {
  23024. int ret = 0;
  23025. byte derBuf[FOURK_BUF];
  23026. word32 derSz = sizeof(derBuf);
  23027. /* Doing default generation and verify */
  23028. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  23029. certSz, key, keySz);
  23030. if (ret <= 0) {
  23031. return -11915;
  23032. }
  23033. ret = verifyBundle(derBuf, derSz, 0);
  23034. if (ret != 0) {
  23035. return -11916;
  23036. }
  23037. /* test choosing other key with keyID */
  23038. derSz = sizeof(derBuf);
  23039. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  23040. cert, certSz, key, keySz);
  23041. if (ret <= 0) {
  23042. return -11917;
  23043. }
  23044. ret = verifyBundle(derBuf, derSz, 1);
  23045. if (ret != 0) {
  23046. return -11918;
  23047. }
  23048. /* test fail case with wrong keyID */
  23049. derSz = sizeof(derBuf);
  23050. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  23051. cert, certSz, key, keySz);
  23052. if (ret <= 0) {
  23053. return -11919;
  23054. }
  23055. ret = verifyBundle(derBuf, derSz, 1);
  23056. if (ret == 0) {
  23057. return -11920;
  23058. }
  23059. return 0;
  23060. }
  23061. #endif /* NO_AES */
  23062. #ifndef NO_PKCS7_ENCRYPTED_DATA
  23063. typedef struct {
  23064. const byte* content;
  23065. word32 contentSz;
  23066. int contentOID;
  23067. int encryptOID;
  23068. byte* encryptionKey;
  23069. word32 encryptionKeySz;
  23070. PKCS7Attrib* attribs;
  23071. word32 attribsSz;
  23072. const char* outFileName;
  23073. } pkcs7EncryptedVector;
  23074. int pkcs7encrypted_test(void)
  23075. {
  23076. int ret = 0;
  23077. int i, testSz;
  23078. int encryptedSz, decodedSz, attribIdx;
  23079. PKCS7* pkcs7;
  23080. byte encrypted[2048];
  23081. byte decoded[2048];
  23082. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  23083. XFILE pkcs7File;
  23084. #endif
  23085. PKCS7Attrib* expectedAttrib;
  23086. PKCS7DecodedAttrib* decodedAttrib;
  23087. const byte data[] = { /* Hello World */
  23088. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  23089. 0x72,0x6c,0x64
  23090. };
  23091. #ifndef NO_DES3
  23092. byte desKey[] = {
  23093. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  23094. };
  23095. byte des3Key[] = {
  23096. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  23097. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  23098. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  23099. };
  23100. #endif
  23101. #ifndef NO_AES
  23102. #ifdef WOLFSSL_AES_128
  23103. byte aes128Key[] = {
  23104. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23105. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  23106. };
  23107. #endif
  23108. #ifdef WOLFSSL_AES_192
  23109. byte aes192Key[] = {
  23110. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23111. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23112. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  23113. };
  23114. #endif
  23115. #ifdef WOLFSSL_AES_256
  23116. byte aes256Key[] = {
  23117. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23118. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23119. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23120. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  23121. };
  23122. #endif
  23123. #ifdef WOLFSSL_AES_256
  23124. /* Attribute example from RFC 4134, Section 7.2
  23125. * OID = 1.2.5555
  23126. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  23127. static byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  23128. static byte genAttr[] = { 0x04, 47,
  23129. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  23130. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  23131. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  23132. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  23133. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  23134. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  23135. static byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  23136. static byte genAttr2[] = { 0x04, 47,
  23137. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  23138. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  23139. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  23140. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  23141. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  23142. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  23143. PKCS7Attrib attribs[] =
  23144. {
  23145. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  23146. };
  23147. PKCS7Attrib multiAttribs[] =
  23148. {
  23149. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  23150. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  23151. };
  23152. #endif
  23153. #endif /* NO_AES */
  23154. const pkcs7EncryptedVector testVectors[] =
  23155. {
  23156. #ifndef NO_DES3
  23157. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  23158. NULL, 0, "pkcs7encryptedDataDES3.der"},
  23159. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  23160. NULL, 0, "pkcs7encryptedDataDES.der"},
  23161. #endif /* NO_DES3 */
  23162. #ifndef NO_AES
  23163. #ifdef WOLFSSL_AES_128
  23164. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  23165. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  23166. #endif
  23167. #ifdef WOLFSSL_AES_192
  23168. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  23169. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  23170. #endif
  23171. #ifdef WOLFSSL_AES_256
  23172. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  23173. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  23174. /* test with optional unprotected attributes */
  23175. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  23176. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23177. "pkcs7encryptedDataAES256CBC_attribs.der"},
  23178. /* test with multiple optional unprotected attributes */
  23179. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  23180. sizeof(aes256Key), multiAttribs,
  23181. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  23182. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  23183. /* test with contentType set to FirmwarePkgData */
  23184. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  23185. sizeof(aes256Key), NULL, 0,
  23186. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  23187. #endif
  23188. #endif /* NO_AES */
  23189. };
  23190. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  23191. for (i = 0; i < testSz; i++) {
  23192. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  23193. if (pkcs7 == NULL)
  23194. return -12000;
  23195. pkcs7->content = (byte*)testVectors[i].content;
  23196. pkcs7->contentSz = testVectors[i].contentSz;
  23197. pkcs7->contentOID = testVectors[i].contentOID;
  23198. pkcs7->encryptOID = testVectors[i].encryptOID;
  23199. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  23200. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  23201. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  23202. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  23203. /* encode encryptedData */
  23204. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  23205. sizeof(encrypted));
  23206. if (encryptedSz <= 0) {
  23207. wc_PKCS7_Free(pkcs7);
  23208. return -12001;
  23209. }
  23210. /* decode encryptedData */
  23211. #ifndef NO_PKCS7_STREAM
  23212. { /* test reading byte by byte */
  23213. int z;
  23214. for (z = 0; z < encryptedSz; z++) {
  23215. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  23216. decoded, sizeof(decoded));
  23217. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  23218. printf("unexpected error %d\n", decodedSz);
  23219. return -12002;
  23220. }
  23221. }
  23222. /* test decode result */
  23223. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  23224. printf("stream read failed\n");
  23225. wc_PKCS7_Free(pkcs7);
  23226. return -12003;
  23227. }
  23228. }
  23229. #endif
  23230. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  23231. decoded, sizeof(decoded));
  23232. if (decodedSz <= 0){
  23233. wc_PKCS7_Free(pkcs7);
  23234. return -12004;
  23235. }
  23236. /* test decode result */
  23237. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  23238. wc_PKCS7_Free(pkcs7);
  23239. return -12005;
  23240. }
  23241. /* verify decoded unprotected attributes */
  23242. if (pkcs7->decodedAttrib != NULL) {
  23243. decodedAttrib = pkcs7->decodedAttrib;
  23244. attribIdx = 1;
  23245. while (decodedAttrib != NULL) {
  23246. /* expected attribute, stored list is reversed */
  23247. expectedAttrib = &(pkcs7->unprotectedAttribs
  23248. [pkcs7->unprotectedAttribsSz - attribIdx]);
  23249. /* verify oid */
  23250. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  23251. decodedAttrib->oidSz) != 0) {
  23252. wc_PKCS7_Free(pkcs7);
  23253. return -12006;
  23254. }
  23255. /* verify value */
  23256. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  23257. decodedAttrib->valueSz) != 0) {
  23258. wc_PKCS7_Free(pkcs7);
  23259. return -12007;
  23260. }
  23261. decodedAttrib = decodedAttrib->next;
  23262. attribIdx++;
  23263. }
  23264. }
  23265. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  23266. /* output pkcs7 envelopedData for external testing */
  23267. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  23268. if (!pkcs7File) {
  23269. wc_PKCS7_Free(pkcs7);
  23270. return -12008;
  23271. }
  23272. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  23273. XFCLOSE(pkcs7File);
  23274. if (ret > 0)
  23275. ret = 0;
  23276. #endif
  23277. wc_PKCS7_Free(pkcs7);
  23278. }
  23279. return ret;
  23280. }
  23281. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  23282. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  23283. typedef struct {
  23284. const byte* content;
  23285. word32 contentSz;
  23286. int contentOID;
  23287. const char* outFileName;
  23288. } pkcs7CompressedVector;
  23289. int pkcs7compressed_test(void)
  23290. {
  23291. int ret = 0;
  23292. int i, testSz;
  23293. int compressedSz, decodedSz;
  23294. PKCS7* pkcs7;
  23295. byte compressed[2048];
  23296. byte decoded[2048];
  23297. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  23298. XFILE pkcs7File;
  23299. #endif
  23300. const byte data[] = { /* Hello World */
  23301. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  23302. 0x72,0x6c,0x64
  23303. };
  23304. const pkcs7CompressedVector testVectors[] =
  23305. {
  23306. {data, (word32)sizeof(data), DATA,
  23307. "pkcs7compressedData_data_zlib.der"},
  23308. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  23309. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  23310. };
  23311. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  23312. for (i = 0; i < testSz; i++) {
  23313. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  23314. if (pkcs7 == NULL)
  23315. return -12100;
  23316. pkcs7->content = (byte*)testVectors[i].content;
  23317. pkcs7->contentSz = testVectors[i].contentSz;
  23318. pkcs7->contentOID = testVectors[i].contentOID;
  23319. /* encode compressedData */
  23320. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  23321. sizeof(compressed));
  23322. if (compressedSz <= 0) {
  23323. wc_PKCS7_Free(pkcs7);
  23324. return -12101;
  23325. }
  23326. /* decode compressedData */
  23327. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  23328. compressedSz, decoded,
  23329. sizeof(decoded));
  23330. if (decodedSz <= 0){
  23331. wc_PKCS7_Free(pkcs7);
  23332. return -12102;
  23333. }
  23334. /* test decode result */
  23335. if (XMEMCMP(decoded, testVectors[i].content,
  23336. testVectors[i].contentSz) != 0) {
  23337. wc_PKCS7_Free(pkcs7);
  23338. return -12103;
  23339. }
  23340. /* make sure content type is the same */
  23341. if (testVectors[i].contentOID != pkcs7->contentOID)
  23342. return -12104;
  23343. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  23344. /* output pkcs7 compressedData for external testing */
  23345. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  23346. if (!pkcs7File) {
  23347. wc_PKCS7_Free(pkcs7);
  23348. return -12105;
  23349. }
  23350. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  23351. XFCLOSE(pkcs7File);
  23352. if (ret > 0)
  23353. ret = 0;
  23354. #endif
  23355. wc_PKCS7_Free(pkcs7);
  23356. }
  23357. return ret;
  23358. } /* pkcs7compressed_test() */
  23359. #endif /* HAVE_LIBZ */
  23360. typedef struct {
  23361. const byte* content;
  23362. word32 contentSz;
  23363. int hashOID;
  23364. int signOID;
  23365. byte* privateKey;
  23366. word32 privateKeySz;
  23367. byte* cert;
  23368. size_t certSz;
  23369. byte* caCert;
  23370. size_t caCertSz;
  23371. PKCS7Attrib* signedAttribs;
  23372. word32 signedAttribsSz;
  23373. const char* outFileName;
  23374. int contentOID;
  23375. byte* contentType;
  23376. word32 contentTypeSz;
  23377. int sidType;
  23378. int encryptOID; /* for single-shot encrypt alg OID */
  23379. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  23380. byte* encryptKey; /* for single-shot, encryptedData */
  23381. word32 encryptKeySz; /* for single-shot, encryptedData */
  23382. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  23383. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  23384. word16 detachedSignature; /* generate detached signature (0:1) */
  23385. } pkcs7SignedVector;
  23386. static int pkcs7signed_run_vectors(
  23387. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  23388. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  23389. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  23390. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  23391. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  23392. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  23393. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  23394. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  23395. {
  23396. int ret, testSz, i;
  23397. int encodedSz;
  23398. byte* out;
  23399. word32 outSz;
  23400. WC_RNG rng;
  23401. PKCS7* pkcs7;
  23402. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  23403. XFILE file;
  23404. #endif
  23405. const byte data[] = { /* Hello World */
  23406. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  23407. 0x72,0x6c,0x64
  23408. };
  23409. static byte transIdOid[] =
  23410. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  23411. 0x09, 0x07 };
  23412. static byte messageTypeOid[] =
  23413. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  23414. 0x09, 0x02 };
  23415. static byte senderNonceOid[] =
  23416. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  23417. 0x09, 0x05 };
  23418. #ifndef NO_SHA
  23419. static byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  23420. #else
  23421. static byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  23422. #endif
  23423. static byte messageType[] = { 0x13, 2, '1', '9' };
  23424. static byte senderNonce[PKCS7_NONCE_SZ + 2];
  23425. PKCS7Attrib attribs[] =
  23426. {
  23427. { transIdOid, sizeof(transIdOid), transId,
  23428. sizeof(transId) - 1 }, /* take off the null */
  23429. { messageTypeOid, sizeof(messageTypeOid), messageType,
  23430. sizeof(messageType) },
  23431. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  23432. sizeof(senderNonce) }
  23433. };
  23434. /* for testing custom contentType, FirmwarePkgData */
  23435. byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  23436. 0x48, 0x86, 0xF7, 0x0D,
  23437. 0x01, 0x09, 0x10, 0x01, 0x10 };
  23438. const pkcs7SignedVector testVectors[] =
  23439. {
  23440. #ifndef NO_RSA
  23441. #ifndef NO_SHA
  23442. /* RSA with SHA */
  23443. {data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  23444. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  23445. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23446. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  23447. 0, 0},
  23448. /* RSA with SHA, no signed attributes */
  23449. {data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  23450. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  23451. NULL, 0, NULL, 0,
  23452. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  23453. NULL, 0, 0},
  23454. #endif
  23455. #ifdef WOLFSSL_SHA224
  23456. /* RSA with SHA224 */
  23457. {data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  23458. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  23459. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23460. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  23461. NULL, 0, 0},
  23462. #endif
  23463. #ifndef NO_SHA256
  23464. /* RSA with SHA256 */
  23465. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  23466. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  23467. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23468. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  23469. NULL, 0, 0},
  23470. /* RSA with SHA256, detached signature */
  23471. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  23472. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  23473. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23474. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  23475. NULL, 0, NULL, 0, 1},
  23476. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  23477. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  23478. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  23479. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23480. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  23481. NULL, 0, NULL, 0, 0},
  23482. /* RSA with SHA256 and custom contentType */
  23483. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  23484. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  23485. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23486. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  23487. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  23488. NULL, 0, 0},
  23489. /* RSA with SHA256 and FirmwarePkgData contentType */
  23490. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  23491. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  23492. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23493. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  23494. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  23495. /* RSA with SHA256 using server cert and ca cert */
  23496. {data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  23497. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  23498. rsaCaCertBuf, rsaCaCertBufSz,
  23499. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23500. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  23501. NULL, 0, NULL, 0, 0},
  23502. #endif
  23503. #if defined(WOLFSSL_SHA384)
  23504. /* RSA with SHA384 */
  23505. {data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  23506. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  23507. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23508. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  23509. NULL, 0, 0},
  23510. #endif
  23511. #if defined(WOLFSSL_SHA512)
  23512. /* RSA with SHA512 */
  23513. {data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  23514. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  23515. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23516. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  23517. NULL, 0, 0},
  23518. #endif
  23519. #endif /* NO_RSA */
  23520. #ifdef HAVE_ECC
  23521. #ifndef NO_SHA
  23522. /* ECDSA with SHA */
  23523. {data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  23524. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  23525. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23526. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  23527. NULL, 0, 0},
  23528. /* ECDSA with SHA, no signed attributes */
  23529. {data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  23530. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  23531. NULL, 0, NULL, 0,
  23532. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  23533. NULL, 0, 0},
  23534. #endif
  23535. #ifdef WOLFSSL_SHA224
  23536. /* ECDSA with SHA224 */
  23537. {data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  23538. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  23539. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23540. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  23541. NULL, 0, 0},
  23542. #endif
  23543. #ifndef NO_SHA256
  23544. /* ECDSA with SHA256 */
  23545. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  23546. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  23547. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23548. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  23549. NULL, 0, 0},
  23550. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  23551. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  23552. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  23553. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23554. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  23555. NULL, 0, NULL, 0, 0},
  23556. /* ECDSA with SHA256 and custom contentType */
  23557. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  23558. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  23559. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23560. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  23561. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  23562. NULL, 0, 0},
  23563. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  23564. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  23565. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  23566. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23567. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  23568. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  23569. #endif
  23570. #ifdef WOLFSSL_SHA384
  23571. /* ECDSA with SHA384 */
  23572. {data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  23573. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  23574. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23575. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  23576. NULL, 0, 0},
  23577. #endif
  23578. #ifdef WOLFSSL_SHA512
  23579. /* ECDSA with SHA512 */
  23580. {data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  23581. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  23582. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23583. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  23584. NULL, 0, 0},
  23585. #endif
  23586. #endif /* HAVE_ECC */
  23587. };
  23588. testSz = sizeof(testVectors) / sizeof(pkcs7SignedVector);
  23589. outSz = FOURK_BUF;
  23590. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23591. if (out == NULL)
  23592. return -12106;
  23593. XMEMSET(out, 0, outSz);
  23594. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  23595. if (ret < 0) {
  23596. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23597. return -12107;
  23598. }
  23599. #ifndef HAVE_FIPS
  23600. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23601. #else
  23602. ret = wc_InitRng(&rng);
  23603. #endif
  23604. if (ret != 0) {
  23605. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23606. return -12108;
  23607. }
  23608. for (i = 0; i < testSz; i++) {
  23609. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  23610. if (pkcs7 == NULL)
  23611. return -12109;
  23612. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  23613. (word32)testVectors[i].certSz);
  23614. if (ret != 0) {
  23615. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23616. wc_PKCS7_Free(pkcs7);
  23617. return -12110;
  23618. }
  23619. /* load CA certificate, if present */
  23620. if (testVectors[i].caCert != NULL) {
  23621. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  23622. (word32)testVectors[i].caCertSz);
  23623. if (ret != 0) {
  23624. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23625. wc_PKCS7_Free(pkcs7);
  23626. return -12111;
  23627. }
  23628. }
  23629. pkcs7->rng = &rng;
  23630. pkcs7->content = (byte*)testVectors[i].content;
  23631. pkcs7->contentSz = testVectors[i].contentSz;
  23632. pkcs7->contentOID = testVectors[i].contentOID;
  23633. pkcs7->hashOID = testVectors[i].hashOID;
  23634. pkcs7->encryptOID = testVectors[i].signOID;
  23635. pkcs7->privateKey = testVectors[i].privateKey;
  23636. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  23637. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  23638. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  23639. /* optional custom contentType, default is DATA,
  23640. overrides contentOID if set */
  23641. if (testVectors[i].contentType != NULL) {
  23642. ret = wc_PKCS7_SetContentType(pkcs7, testVectors[i].contentType,
  23643. testVectors[i].contentTypeSz);
  23644. if (ret != 0) {
  23645. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23646. wc_PKCS7_Free(pkcs7);
  23647. return -12112;
  23648. }
  23649. }
  23650. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  23651. default is IssuerAndSerialNumber */
  23652. if (testVectors[i].sidType == CMS_SKID) {
  23653. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  23654. if (ret != 0) {
  23655. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23656. wc_PKCS7_Free(pkcs7);
  23657. return -12113;
  23658. }
  23659. }
  23660. /* generate senderNonce */
  23661. {
  23662. senderNonce[0] = 0x04;
  23663. senderNonce[1] = PKCS7_NONCE_SZ;
  23664. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  23665. if (ret != 0) {
  23666. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23667. wc_PKCS7_Free(pkcs7);
  23668. return -12114;
  23669. }
  23670. }
  23671. /* generate transactionID (used with SCEP) */
  23672. {
  23673. #ifndef NO_SHA
  23674. wc_Sha sha;
  23675. byte digest[WC_SHA_DIGEST_SIZE];
  23676. #else
  23677. wc_Sha256 sha;
  23678. byte digest[WC_SHA256_DIGEST_SIZE];
  23679. #endif
  23680. int j,k;
  23681. transId[0] = 0x13;
  23682. transId[1] = sizeof(digest) * 2;
  23683. #ifndef NO_SHA
  23684. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  23685. if (ret != 0) {
  23686. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23687. wc_PKCS7_Free(pkcs7);
  23688. return -12115;
  23689. }
  23690. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  23691. wc_ShaFinal(&sha, digest);
  23692. wc_ShaFree(&sha);
  23693. #else
  23694. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  23695. if (ret != 0) {
  23696. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23697. wc_PKCS7_Free(pkcs7);
  23698. return -12116;
  23699. }
  23700. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  23701. wc_Sha256Final(&sha, digest);
  23702. wc_Sha256Free(&sha);
  23703. #endif
  23704. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  23705. XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  23706. }
  23707. }
  23708. /* enable detached signature generation, if set */
  23709. if (testVectors[i].detachedSignature == 1) {
  23710. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  23711. if (ret != 0) {
  23712. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23713. wc_PKCS7_Free(pkcs7);
  23714. return -12117;
  23715. }
  23716. }
  23717. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  23718. if (encodedSz < 0) {
  23719. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23720. wc_PKCS7_Free(pkcs7);
  23721. return -12118;
  23722. }
  23723. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  23724. /* write PKCS#7 to output file for more testing */
  23725. file = XFOPEN(testVectors[i].outFileName, "wb");
  23726. if (!file) {
  23727. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23728. wc_PKCS7_Free(pkcs7);
  23729. return -12119;
  23730. }
  23731. ret = (int)XFWRITE(out, 1, encodedSz, file);
  23732. XFCLOSE(file);
  23733. if (ret != (int)encodedSz) {
  23734. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23735. wc_PKCS7_Free(pkcs7);
  23736. return -12120;
  23737. }
  23738. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  23739. wc_PKCS7_Free(pkcs7);
  23740. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  23741. if (pkcs7 == NULL)
  23742. return -12121;
  23743. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  23744. if (testVectors[i].detachedSignature == 1) {
  23745. /* set content for verifying detached signatures */
  23746. pkcs7->content = (byte*)testVectors[i].content;
  23747. pkcs7->contentSz = testVectors[i].contentSz;
  23748. }
  23749. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  23750. if (ret < 0) {
  23751. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23752. wc_PKCS7_Free(pkcs7);
  23753. return -12122;
  23754. }
  23755. /* verify contentType extracted successfully for custom content types */
  23756. if (testVectors[i].contentTypeSz > 0) {
  23757. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  23758. return -12123;
  23759. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  23760. pkcs7->contentTypeSz) != 0) {
  23761. return -12124;
  23762. }
  23763. }
  23764. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0) {
  23765. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23766. wc_PKCS7_Free(pkcs7);
  23767. return -12125;
  23768. }
  23769. {
  23770. /* check getting signed attributes */
  23771. #ifndef NO_SHA
  23772. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  23773. #else
  23774. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  23775. #endif
  23776. byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  23777. int oidSz = (int)sizeof(transIdOid) - 2;
  23778. int bufSz = 0;
  23779. if (testVectors[i].signedAttribs != NULL &&
  23780. wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  23781. NULL, (word32*)&bufSz) != LENGTH_ONLY_E) {
  23782. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23783. wc_PKCS7_Free(pkcs7);
  23784. return -12126;
  23785. }
  23786. if (bufSz > (int)sizeof(buf)) {
  23787. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23788. wc_PKCS7_Free(pkcs7);
  23789. return -12127;
  23790. }
  23791. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  23792. buf, (word32*)&bufSz);
  23793. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  23794. (testVectors[i].signedAttribs == NULL && bufSz > 0)) {
  23795. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23796. wc_PKCS7_Free(pkcs7);
  23797. return -12128;
  23798. }
  23799. }
  23800. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  23801. file = XFOPEN("./pkcs7cert.der", "wb");
  23802. if (!file) {
  23803. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23804. wc_PKCS7_Free(pkcs7);
  23805. return -12129;
  23806. }
  23807. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  23808. XFCLOSE(file);
  23809. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  23810. wc_PKCS7_Free(pkcs7);
  23811. }
  23812. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23813. wc_FreeRng(&rng);
  23814. if (ret > 0)
  23815. return 0;
  23816. (void)rsaClientCertBuf;
  23817. (void)rsaClientCertBufSz;
  23818. (void)rsaClientPrivKeyBuf;
  23819. (void)rsaClientPrivKeyBufSz;
  23820. (void)rsaServerCertBuf;
  23821. (void)rsaServerCertBufSz;
  23822. (void)rsaServerPrivKeyBuf;
  23823. (void)rsaServerPrivKeyBufSz;
  23824. (void)rsaCaCertBuf;
  23825. (void)rsaCaCertBufSz;
  23826. (void)rsaCaPrivKeyBuf;
  23827. (void)rsaCaPrivKeyBufSz;
  23828. (void)eccClientCertBuf;
  23829. (void)eccClientCertBufSz;
  23830. (void)eccClientPrivKeyBuf;
  23831. (void)eccClientPrivKeyBufSz;
  23832. return ret;
  23833. }
  23834. static int pkcs7signed_run_SingleShotVectors(
  23835. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  23836. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  23837. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  23838. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  23839. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  23840. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  23841. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  23842. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  23843. {
  23844. int ret, testSz, i;
  23845. int encodedSz;
  23846. byte* out;
  23847. word32 outSz;
  23848. WC_RNG rng;
  23849. PKCS7* pkcs7;
  23850. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  23851. XFILE file;
  23852. #endif
  23853. const byte data[] = { /* Hello World */
  23854. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  23855. 0x72,0x6c,0x64
  23856. };
  23857. #if defined(WOLFSSL_AES_256) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  23858. byte aes256Key[] = {
  23859. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23860. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23861. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23862. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  23863. };
  23864. #endif
  23865. static byte messageTypeOid[] =
  23866. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  23867. 0x09, 0x02 };
  23868. static byte messageType[] = { 0x13, 2, '1', '9' };
  23869. PKCS7Attrib attribs[] =
  23870. {
  23871. { messageTypeOid, sizeof(messageTypeOid), messageType,
  23872. sizeof(messageType) },
  23873. };
  23874. const pkcs7SignedVector testVectors[] =
  23875. {
  23876. #ifndef NO_RSA
  23877. #ifndef NO_SHA256
  23878. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  23879. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  23880. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  23881. NULL, 0,
  23882. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  23883. 0, 0, NULL, 0, NULL, 0, 0},
  23884. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  23885. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  23886. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  23887. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23888. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  23889. NULL, 0, NULL, 0, 0},
  23890. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  23891. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  23892. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  23893. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23894. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  23895. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0},
  23896. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  23897. {data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  23898. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  23899. rsaCaCertBuf, rsaCaCertBufSz,
  23900. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23901. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  23902. 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  23903. #if defined(WOLFSSL_AES_256) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  23904. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  23905. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  23906. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  23907. NULL, 0,
  23908. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  23909. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0},
  23910. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  23911. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  23912. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  23913. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23914. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  23915. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  23916. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  23917. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  23918. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  23919. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  23920. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  23921. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  23922. NULL, 0,
  23923. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  23924. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  23925. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  23926. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  23927. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  23928. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23929. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  23930. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  23931. #ifndef NO_PKCS7_ENCRYPTED_DATA
  23932. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  23933. no attribs */
  23934. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  23935. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  23936. NULL, 0,
  23937. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  23938. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  23939. 0, 0},
  23940. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  23941. attribs */
  23942. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  23943. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  23944. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23945. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  23946. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  23947. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  23948. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  23949. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  23950. #endif /* NO_SHA256 */
  23951. #endif /* NO_RSA */
  23952. #ifdef HAVE_ECC
  23953. #ifndef NO_SHA256
  23954. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  23955. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  23956. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  23957. NULL, 0,
  23958. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  23959. 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  23960. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  23961. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  23962. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  23963. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23964. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  23965. 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  23966. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  23967. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  23968. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  23969. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23970. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  23971. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0},
  23972. #if defined(WOLFSSL_AES_256) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  23973. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  23974. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  23975. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  23976. NULL, 0,
  23977. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  23978. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0},
  23979. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  23980. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  23981. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  23982. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23983. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  23984. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  23985. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  23986. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  23987. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  23988. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  23989. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  23990. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  23991. NULL, 0,
  23992. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  23993. 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  23994. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  23995. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  23996. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  23997. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  23998. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  23999. 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  24000. #ifndef NO_PKCS7_ENCRYPTED_DATA
  24001. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  24002. no attribs */
  24003. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  24004. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  24005. NULL, 0,
  24006. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  24007. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  24008. 0, 0},
  24009. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  24010. attribs */
  24011. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  24012. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  24013. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  24014. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  24015. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  24016. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  24017. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  24018. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  24019. #endif /* NO_SHA256 */
  24020. #endif /* HAVE_ECC */
  24021. };
  24022. testSz = sizeof(testVectors) / sizeof(pkcs7SignedVector);
  24023. outSz = FOURK_BUF;
  24024. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24025. if (out == NULL)
  24026. return -12130;
  24027. XMEMSET(out, 0, outSz);
  24028. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  24029. if (ret < 0) {
  24030. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24031. return -12131;
  24032. }
  24033. #ifndef HAVE_FIPS
  24034. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24035. #else
  24036. ret = wc_InitRng(&rng);
  24037. #endif
  24038. if (ret != 0) {
  24039. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24040. return -12132;
  24041. }
  24042. for (i = 0; i < testSz; i++) {
  24043. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  24044. if (pkcs7 == NULL)
  24045. return -12133;
  24046. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  24047. (word32)testVectors[i].certSz);
  24048. if (ret != 0) {
  24049. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24050. wc_PKCS7_Free(pkcs7);
  24051. return -12134;
  24052. }
  24053. /* load CA certificate, if present */
  24054. if (testVectors[i].caCert != NULL) {
  24055. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  24056. (word32)testVectors[i].caCertSz);
  24057. if (ret != 0) {
  24058. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24059. wc_PKCS7_Free(pkcs7);
  24060. return -12135;
  24061. }
  24062. }
  24063. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  24064. default is IssuerAndSerialNumber */
  24065. if (testVectors[i].sidType == CMS_SKID) {
  24066. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  24067. if (ret != 0) {
  24068. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24069. wc_PKCS7_Free(pkcs7);
  24070. return -12136;
  24071. }
  24072. }
  24073. if (testVectors[i].encCompFlag == 0) {
  24074. /* encode Signed FirmwarePkgData */
  24075. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  24076. testVectors[i].privateKey, testVectors[i].privateKeySz,
  24077. testVectors[i].signOID, testVectors[i].hashOID,
  24078. (byte*)testVectors[i].content, testVectors[i].contentSz,
  24079. testVectors[i].signedAttribs,
  24080. testVectors[i].signedAttribsSz, out, outSz);
  24081. if (encodedSz < 0) {
  24082. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24083. wc_PKCS7_Free(pkcs7);
  24084. return -12137;
  24085. }
  24086. #ifndef NO_PKCS7_ENCRYPTED_DATA
  24087. } else if (testVectors[i].encCompFlag == 1) {
  24088. /* encode Signed Encrypted FirmwarePkgData */
  24089. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  24090. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  24091. testVectors[i].privateKey, testVectors[i].privateKeySz,
  24092. testVectors[i].encryptOID, testVectors[i].signOID,
  24093. testVectors[i].hashOID, (byte*)testVectors[i].content,
  24094. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  24095. testVectors[i].unprotectedAttribsSz,
  24096. testVectors[i].signedAttribs,
  24097. testVectors[i].signedAttribsSz, out, outSz);
  24098. if (encodedSz <= 0) {
  24099. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24100. wc_PKCS7_Free(pkcs7);
  24101. return -12138;
  24102. }
  24103. #endif
  24104. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  24105. } else if (testVectors[i].encCompFlag == 2) {
  24106. /* encode Signed Compressed FirmwarePkgData */
  24107. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  24108. testVectors[i].privateKey, testVectors[i].privateKeySz,
  24109. testVectors[i].signOID, testVectors[i].hashOID,
  24110. (byte*)testVectors[i].content, testVectors[i].contentSz,
  24111. testVectors[i].signedAttribs,
  24112. testVectors[i].signedAttribsSz, out, outSz);
  24113. if (encodedSz <= 0) {
  24114. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24115. wc_PKCS7_Free(pkcs7);
  24116. return -12139;
  24117. }
  24118. #ifndef NO_PKCS7_ENCRYPTED_DATA
  24119. } else if (testVectors[i].encCompFlag == 3) {
  24120. /* encode Signed Encrypted Compressed FirmwarePkgData */
  24121. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  24122. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  24123. testVectors[i].privateKey, testVectors[i].privateKeySz,
  24124. testVectors[i].encryptOID, testVectors[i].signOID,
  24125. testVectors[i].hashOID, (byte*)testVectors[i].content,
  24126. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  24127. testVectors[i].unprotectedAttribsSz,
  24128. testVectors[i].signedAttribs,
  24129. testVectors[i].signedAttribsSz, out, outSz);
  24130. if (encodedSz <= 0) {
  24131. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24132. wc_PKCS7_Free(pkcs7);
  24133. return -12140;
  24134. }
  24135. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  24136. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  24137. } else {
  24138. /* unsupported SignedData single-shot combination */
  24139. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24140. wc_PKCS7_Free(pkcs7);
  24141. return -12141;
  24142. }
  24143. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  24144. /* write PKCS#7 to output file for more testing */
  24145. file = XFOPEN(testVectors[i].outFileName, "wb");
  24146. if (!file) {
  24147. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24148. wc_PKCS7_Free(pkcs7);
  24149. return -12142;
  24150. }
  24151. ret = (int)XFWRITE(out, 1, encodedSz, file);
  24152. XFCLOSE(file);
  24153. if (ret != (int)encodedSz) {
  24154. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24155. wc_PKCS7_Free(pkcs7);
  24156. return -12143;
  24157. }
  24158. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  24159. wc_PKCS7_Free(pkcs7);
  24160. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  24161. if (pkcs7 == NULL)
  24162. return -12144;
  24163. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  24164. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  24165. if (ret < 0) {
  24166. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24167. wc_PKCS7_Free(pkcs7);
  24168. return -12145;
  24169. }
  24170. #ifndef NO_PKCS7_STREAM
  24171. {
  24172. word32 z;
  24173. for (z = 0; z < outSz && ret != 0; z++) {
  24174. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  24175. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  24176. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24177. wc_PKCS7_Free(pkcs7);
  24178. printf("unexpected error %d\n", ret);
  24179. return -12146;
  24180. }
  24181. }
  24182. }
  24183. #endif
  24184. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0) {
  24185. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24186. wc_PKCS7_Free(pkcs7);
  24187. return -12147;
  24188. }
  24189. if (testVectors[i].encCompFlag == 0) {
  24190. /* verify decoded content matches expected */
  24191. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  24192. XMEMCMP(pkcs7->content, testVectors[i].content,
  24193. pkcs7->contentSz)) {
  24194. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24195. wc_PKCS7_Free(pkcs7);
  24196. return -12148;
  24197. }
  24198. }
  24199. #ifndef NO_PKCS7_ENCRYPTED_DATA
  24200. else if (testVectors[i].encCompFlag == 1) {
  24201. /* decrypt inner encryptedData */
  24202. pkcs7->encryptionKey = testVectors[i].encryptKey;
  24203. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  24204. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  24205. pkcs7->contentSz, out, outSz);
  24206. if (ret < 0) {
  24207. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24208. wc_PKCS7_Free(pkcs7);
  24209. return -12149;
  24210. }
  24211. /* compare decrypted to expected */
  24212. if (((word32)ret != testVectors[i].contentSz) ||
  24213. XMEMCMP(out, testVectors[i].content, ret)) {
  24214. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24215. wc_PKCS7_Free(pkcs7);
  24216. return -12150;
  24217. }
  24218. }
  24219. #endif
  24220. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  24221. else if (testVectors[i].encCompFlag == 2) {
  24222. /* decompress inner compressedData */
  24223. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  24224. pkcs7->contentSz, out, outSz);
  24225. if (ret < 0) {
  24226. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24227. wc_PKCS7_Free(pkcs7);
  24228. return -12151;
  24229. }
  24230. /* compare decompressed to expected */
  24231. if (((word32)ret != testVectors[i].contentSz) ||
  24232. XMEMCMP(out, testVectors[i].content, ret)) {
  24233. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24234. wc_PKCS7_Free(pkcs7);
  24235. return -12152;
  24236. }
  24237. }
  24238. #ifndef NO_PKCS7_ENCRYPTED_DATA
  24239. else if (testVectors[i].encCompFlag == 3) {
  24240. byte* encryptedTmp;
  24241. int encryptedTmpSz;
  24242. encryptedTmpSz = FOURK_BUF;
  24243. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  24244. DYNAMIC_TYPE_TMP_BUFFER);
  24245. if (encryptedTmp == NULL) {
  24246. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24247. wc_PKCS7_Free(pkcs7);
  24248. return -12153;
  24249. }
  24250. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  24251. /* decrypt inner encryptedData */
  24252. pkcs7->encryptionKey = testVectors[i].encryptKey;
  24253. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  24254. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  24255. pkcs7->contentSz, encryptedTmp,
  24256. encryptedTmpSz);
  24257. if (encryptedTmpSz < 0) {
  24258. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24259. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24260. wc_PKCS7_Free(pkcs7);
  24261. return -12154;
  24262. }
  24263. /* decompress inner compressedData */
  24264. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  24265. encryptedTmpSz, out, outSz);
  24266. if (ret < 0) {
  24267. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24268. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24269. wc_PKCS7_Free(pkcs7);
  24270. return -12155;
  24271. }
  24272. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24273. /* compare decompressed to expected */
  24274. if (((word32)ret != testVectors[i].contentSz) ||
  24275. XMEMCMP(out, testVectors[i].content, ret)) {
  24276. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24277. wc_PKCS7_Free(pkcs7);
  24278. return -12156;
  24279. }
  24280. }
  24281. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  24282. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  24283. wc_PKCS7_Free(pkcs7);
  24284. }
  24285. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24286. wc_FreeRng(&rng);
  24287. if (ret > 0)
  24288. return 0;
  24289. (void)eccClientCertBuf;
  24290. (void)eccClientCertBufSz;
  24291. (void)eccClientPrivKeyBuf;
  24292. (void)eccClientPrivKeyBufSz;
  24293. (void)rsaClientCertBuf;
  24294. (void)rsaClientCertBufSz;
  24295. (void)rsaClientPrivKeyBuf;
  24296. (void)rsaClientPrivKeyBufSz;
  24297. (void)rsaServerCertBuf;
  24298. (void)rsaServerCertBufSz;
  24299. (void)rsaServerPrivKeyBuf;
  24300. (void)rsaServerPrivKeyBufSz;
  24301. (void)rsaCaCertBuf;
  24302. (void)rsaCaCertBufSz;
  24303. (void)rsaCaPrivKeyBuf;
  24304. (void)rsaCaPrivKeyBufSz;
  24305. return ret;
  24306. }
  24307. int pkcs7signed_test(void)
  24308. {
  24309. int ret = 0;
  24310. byte* rsaClientCertBuf = NULL;
  24311. byte* rsaServerCertBuf = NULL;
  24312. byte* rsaCaCertBuf = NULL;
  24313. byte* eccClientCertBuf = NULL;
  24314. byte* rsaClientPrivKeyBuf = NULL;
  24315. byte* rsaServerPrivKeyBuf = NULL;
  24316. byte* rsaCaPrivKeyBuf = NULL;
  24317. byte* eccClientPrivKeyBuf = NULL;
  24318. word32 rsaClientCertBufSz = 0;
  24319. word32 rsaServerCertBufSz = 0;
  24320. word32 rsaCaCertBufSz = 0;
  24321. word32 eccClientCertBufSz = 0;
  24322. word32 rsaClientPrivKeyBufSz = 0;
  24323. word32 rsaServerPrivKeyBufSz = 0;
  24324. word32 rsaCaPrivKeyBufSz = 0;
  24325. word32 eccClientPrivKeyBufSz = 0;
  24326. #ifndef NO_RSA
  24327. /* read client RSA cert and key in DER format */
  24328. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24329. DYNAMIC_TYPE_TMP_BUFFER);
  24330. if (rsaClientCertBuf == NULL)
  24331. ret = -12200;
  24332. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24333. DYNAMIC_TYPE_TMP_BUFFER);
  24334. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  24335. ret = -12201;
  24336. }
  24337. rsaClientCertBufSz = FOURK_BUF;
  24338. rsaClientPrivKeyBufSz = FOURK_BUF;
  24339. /* read server RSA cert and key in DER format */
  24340. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24341. DYNAMIC_TYPE_TMP_BUFFER);
  24342. if (ret == 0 && rsaServerCertBuf == NULL)
  24343. ret = -12202;
  24344. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24345. DYNAMIC_TYPE_TMP_BUFFER);
  24346. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  24347. ret = -12203;
  24348. }
  24349. rsaServerCertBufSz = FOURK_BUF;
  24350. rsaServerPrivKeyBufSz = FOURK_BUF;
  24351. /* read CA RSA cert and key in DER format, for use with server cert */
  24352. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24353. DYNAMIC_TYPE_TMP_BUFFER);
  24354. if (ret == 0 && rsaCaCertBuf == NULL)
  24355. ret = -12204;
  24356. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24357. DYNAMIC_TYPE_TMP_BUFFER);
  24358. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  24359. ret = -12205;
  24360. }
  24361. rsaCaCertBufSz = FOURK_BUF;
  24362. rsaCaPrivKeyBufSz = FOURK_BUF;
  24363. #endif /* NO_RSA */
  24364. #ifdef HAVE_ECC
  24365. /* read client ECC cert and key in DER format */
  24366. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24367. DYNAMIC_TYPE_TMP_BUFFER);
  24368. if (ret == 0 && eccClientCertBuf == NULL) {
  24369. ret = -12206;
  24370. }
  24371. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24372. DYNAMIC_TYPE_TMP_BUFFER);
  24373. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  24374. ret = -12207;
  24375. }
  24376. eccClientCertBufSz = FOURK_BUF;
  24377. eccClientPrivKeyBufSz = FOURK_BUF;
  24378. #endif /* HAVE_ECC */
  24379. if (ret >= 0)
  24380. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  24381. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  24382. rsaServerCertBuf, &rsaServerCertBufSz,
  24383. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  24384. rsaCaCertBuf, &rsaCaCertBufSz,
  24385. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  24386. eccClientCertBuf, &eccClientCertBufSz,
  24387. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  24388. if (ret < 0) {
  24389. ret = -12208;
  24390. }
  24391. if (ret >= 0)
  24392. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  24393. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  24394. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  24395. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  24396. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  24397. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  24398. eccClientCertBuf, (word32)eccClientCertBufSz,
  24399. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  24400. if (ret >= 0)
  24401. ret = pkcs7signed_run_SingleShotVectors(
  24402. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  24403. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  24404. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  24405. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  24406. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  24407. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  24408. eccClientCertBuf, (word32)eccClientCertBufSz,
  24409. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  24410. #ifndef NO_AES
  24411. if (ret >= 0)
  24412. ret = pkcs7callback_test(
  24413. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  24414. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  24415. #endif
  24416. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24417. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24418. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24419. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24420. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24421. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24422. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24423. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24424. return ret;
  24425. }
  24426. #endif /* HAVE_PKCS7 */
  24427. #ifdef HAVE_VALGRIND
  24428. /* Need a static build to have access to symbols. */
  24429. /* Maximum number of bytes in a number to test. */
  24430. #define MP_MAX_TEST_BYTE_LEN 16
  24431. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  24432. static int randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  24433. {
  24434. byte d[MP_MAX_TEST_BYTE_LEN];
  24435. int ret;
  24436. (void)heap;
  24437. do {
  24438. ret = wc_RNG_GenerateBlock(rng, d, len);
  24439. if (ret != 0)
  24440. return ret;
  24441. ret = mp_read_unsigned_bin(n, d, len);
  24442. if (ret != 0)
  24443. return ret;
  24444. } while (mp_iszero(n));
  24445. return 0;
  24446. }
  24447. #endif
  24448. int mp_test(void)
  24449. {
  24450. WC_RNG rng;
  24451. int ret;
  24452. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  24453. int i, j, k;
  24454. mp_digit d;
  24455. #endif
  24456. mp_int a, b, r1, r2, p;
  24457. ret = mp_init_multi(&a, &b, &r1, &r2, NULL, NULL);
  24458. if (ret != 0)
  24459. return -12300;
  24460. mp_init_copy(&p, &a);
  24461. #ifndef HAVE_FIPS
  24462. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24463. #else
  24464. ret = wc_InitRng(&rng);
  24465. #endif
  24466. if (ret != 0)
  24467. goto done;
  24468. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  24469. mp_set_int(&a, 0);
  24470. if (a.used != 0 || a.dp[0] != 0)
  24471. return -12301;
  24472. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  24473. for (i = 0; i < 4 * j; i++) {
  24474. /* New values to use. */
  24475. ret = randNum(&p, j, &rng, NULL);
  24476. if (ret != 0)
  24477. return -12302;
  24478. ret = randNum(&a, j, &rng, NULL);
  24479. if (ret != 0)
  24480. return -12303;
  24481. ret = randNum(&b, j, &rng, NULL);
  24482. if (ret != 0)
  24483. return -12304;
  24484. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  24485. if (ret != 0)
  24486. return -12305;
  24487. d &= MP_MASK;
  24488. /* Ensure sqrmod produce same result as mulmod. */
  24489. ret = mp_sqrmod(&a, &p, &r1);
  24490. if (ret != 0)
  24491. return -12306;
  24492. ret = mp_mulmod(&a, &a, &p, &r2);
  24493. if (ret != 0)
  24494. return -12307;
  24495. if (mp_cmp(&r1, &r2) != 0)
  24496. return -12308;
  24497. /* Ensure add with mod produce same result as sub with mod. */
  24498. ret = mp_addmod(&a, &b, &p, &r1);
  24499. if (ret != 0)
  24500. return -12309;
  24501. b.sign ^= 1;
  24502. ret = mp_submod(&a, &b, &p, &r2);
  24503. if (ret != 0)
  24504. return -12310;
  24505. if (mp_cmp(&r1, &r2) != 0)
  24506. return -12311;
  24507. /* Ensure add digit produce same result as sub digit. */
  24508. ret = mp_add_d(&a, d, &r1);
  24509. if (ret != 0)
  24510. return -12312;
  24511. ret = mp_sub_d(&r1, d, &r2);
  24512. if (ret != 0)
  24513. return -12313;
  24514. if (mp_cmp(&a, &r2) != 0)
  24515. return -12314;
  24516. /* Invert - if p is even it will use the slow impl.
  24517. * - if p and a are even it will fail.
  24518. */
  24519. ret = mp_invmod(&a, &p, &r1);
  24520. if (ret != 0 && ret != MP_VAL)
  24521. return -12315;
  24522. ret = 0;
  24523. /* Shift up and down number all bits in a digit. */
  24524. for (k = 0; k < DIGIT_BIT; k++) {
  24525. mp_mul_2d(&a, k, &r1);
  24526. mp_div_2d(&r1, k, &r2, &p);
  24527. if (mp_cmp(&a, &r2) != 0)
  24528. return -12316;
  24529. if (!mp_iszero(&p))
  24530. return -12317;
  24531. mp_rshb(&r1, k);
  24532. if (mp_cmp(&a, &r1) != 0)
  24533. return -12318;
  24534. }
  24535. }
  24536. }
  24537. /* Check that setting a 32-bit digit works. */
  24538. d &= 0xffffffff;
  24539. mp_set_int(&a, d);
  24540. if (a.used != 1 || a.dp[0] != d)
  24541. return -12319;
  24542. /* Check setting a bit and testing a bit works. */
  24543. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  24544. mp_zero(&a);
  24545. mp_set_bit(&a, i);
  24546. if (!mp_is_bit_set(&a, i))
  24547. return -12320;
  24548. }
  24549. #endif
  24550. done:
  24551. mp_clear(&p);
  24552. mp_clear(&r2);
  24553. mp_clear(&r1);
  24554. mp_clear(&b);
  24555. mp_clear(&a);
  24556. wc_FreeRng(&rng);
  24557. return ret;
  24558. }
  24559. #endif
  24560. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  24561. typedef struct pairs_t {
  24562. const unsigned char* coeff;
  24563. int coeffSz;
  24564. int exp;
  24565. } pairs_t;
  24566. /*
  24567. n =p1p2p3, where pi = ki(p1−1)+1 with (k2,k3) = (173,293)
  24568. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  24569. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  24570. */
  24571. static const unsigned char c192a[] =
  24572. {
  24573. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  24574. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  24575. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  24576. };
  24577. static const unsigned char c0a[] =
  24578. {
  24579. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  24580. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  24581. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  24582. };
  24583. static const pairs_t ecPairsA[] =
  24584. {
  24585. {c192a, sizeof(c192a), 192},
  24586. {c0a, sizeof(c0a), 0}
  24587. };
  24588. static const int kA[] = {173, 293};
  24589. static const unsigned char controlPrime[] = {
  24590. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  24591. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  24592. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  24593. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  24594. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  24595. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  24596. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  24597. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  24598. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  24599. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  24600. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  24601. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  24602. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  24603. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  24604. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  24605. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  24606. };
  24607. static const unsigned char testOne[] = { 1 };
  24608. static int GenerateNextP(mp_int* p1, mp_int* p2, int k)
  24609. {
  24610. int ret;
  24611. mp_int ki;
  24612. ret = mp_init(&ki);
  24613. if (ret == 0)
  24614. ret = mp_set(&ki, k);
  24615. if (ret == 0)
  24616. ret = mp_sub_d(p1, 1, p2);
  24617. if (ret == 0)
  24618. ret = mp_mul(p2, &ki, p2);
  24619. if (ret == 0)
  24620. ret = mp_add_d(p2, 1, p2);
  24621. mp_clear(&ki);
  24622. return ret;
  24623. }
  24624. static int GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  24625. const pairs_t* ecPairs, int ecPairsSz,
  24626. const int* k)
  24627. {
  24628. mp_int x,y;
  24629. int ret, i;
  24630. ret = mp_init(&x);
  24631. if (ret == 0) {
  24632. ret = mp_init(&y);
  24633. if (ret != 0) {
  24634. mp_clear(&x);
  24635. return MP_MEM;
  24636. }
  24637. }
  24638. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  24639. ret = mp_read_unsigned_bin(&x, ecPairs[i].coeff, ecPairs[i].coeffSz);
  24640. /* p1 = 2^exp */
  24641. if (ret == 0)
  24642. ret = mp_2expt(&y, ecPairs[i].exp);
  24643. /* p1 = p1 * m */
  24644. if (ret == 0)
  24645. ret = mp_mul(&x, &y, &x);
  24646. /* p1 += */
  24647. if (ret == 0)
  24648. ret = mp_add(p1, &x, p1);
  24649. mp_zero(&x);
  24650. mp_zero(&y);
  24651. }
  24652. mp_clear(&x);
  24653. mp_clear(&y);
  24654. if (ret == 0)
  24655. ret = GenerateNextP(p1, p2, k[0]);
  24656. if (ret == 0)
  24657. ret = GenerateNextP(p1, p3, k[1]);
  24658. return ret;
  24659. }
  24660. int prime_test(void)
  24661. {
  24662. mp_int n, p1, p2, p3;
  24663. int ret, isPrime = 0;
  24664. WC_RNG rng;
  24665. ret = wc_InitRng(&rng);
  24666. if (ret == 0)
  24667. ret = mp_init_multi(&n, &p1, &p2, &p3, NULL, NULL);
  24668. if (ret == 0)
  24669. ret = GenerateP(&p1, &p2, &p3,
  24670. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  24671. if (ret == 0)
  24672. ret = mp_mul(&p1, &p2, &n);
  24673. if (ret == 0)
  24674. ret = mp_mul(&n, &p3, &n);
  24675. if (ret != 0)
  24676. return -12400;
  24677. /* Check the old prime test using the number that false positives.
  24678. * This test result should indicate as not prime. */
  24679. ret = mp_prime_is_prime(&n, 40, &isPrime);
  24680. if (ret != 0)
  24681. return -12401;
  24682. if (isPrime)
  24683. return -12402;
  24684. /* This test result should fail. It should indicate the value as prime. */
  24685. ret = mp_prime_is_prime(&n, 8, &isPrime);
  24686. if (ret != 0)
  24687. return -12403;
  24688. if (!isPrime)
  24689. return -12404;
  24690. /* This test result should indicate the value as not prime. */
  24691. ret = mp_prime_is_prime_ex(&n, 8, &isPrime, &rng);
  24692. if (ret != 0)
  24693. return -12405;
  24694. if (isPrime)
  24695. return -12406;
  24696. ret = mp_read_unsigned_bin(&n, controlPrime, sizeof(controlPrime));
  24697. if (ret != 0)
  24698. return -12407;
  24699. /* This test result should indicate the value as prime. */
  24700. ret = mp_prime_is_prime_ex(&n, 8, &isPrime, &rng);
  24701. if (ret != 0)
  24702. return -12408;
  24703. if (!isPrime)
  24704. return -12409;
  24705. /* This test result should indicate the value as prime. */
  24706. isPrime = -1;
  24707. ret = mp_prime_is_prime(&n, 8, &isPrime);
  24708. if (ret != 0)
  24709. return -12410;
  24710. if (!isPrime)
  24711. return -12411;
  24712. ret = mp_read_unsigned_bin(&n, testOne, sizeof(testOne));
  24713. if (ret != 0)
  24714. return -12412;
  24715. /* This test result should indicate the value as not prime. */
  24716. ret = mp_prime_is_prime_ex(&n, 8, &isPrime, &rng);
  24717. if (ret != 0)
  24718. return -12413;
  24719. if (isPrime)
  24720. return -12414;
  24721. ret = mp_prime_is_prime(&n, 8, &isPrime);
  24722. if (ret != 0)
  24723. return -12415;
  24724. if (isPrime)
  24725. return -12416;
  24726. mp_clear(&p3);
  24727. mp_clear(&p2);
  24728. mp_clear(&p1);
  24729. mp_clear(&n);
  24730. wc_FreeRng(&rng);
  24731. return 0;
  24732. }
  24733. #endif /* WOLFSSL_PUBLIC_MP */
  24734. #if defined(ASN_BER_TO_DER) && \
  24735. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  24736. defined(OPENSSL_EXTRA_X509_SMALL))
  24737. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  24738. typedef struct berDerTestData {
  24739. const byte *in;
  24740. word32 inSz;
  24741. const byte *out;
  24742. word32 outSz;
  24743. } berDerTestData;
  24744. int berder_test(void)
  24745. {
  24746. int ret;
  24747. int i;
  24748. word32 len = 0, l;
  24749. byte out[32];
  24750. static const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  24751. static const byte good1_out[] = { 0x30, 0x00 };
  24752. static const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  24753. static const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  24754. static const byte good3_in[] = {
  24755. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  24756. };
  24757. static const byte good3_out[] = { 0x04, 0x1, 0x01 };
  24758. static const byte good4_in[] = {
  24759. 0x30, 0x80,
  24760. 0x02, 0x01, 0x01,
  24761. 0x30, 0x80,
  24762. 0x24, 0x80,
  24763. 0x04, 0x01, 0x01,
  24764. 0x04, 0x02, 0x02, 0x03,
  24765. 0x00, 0x00,
  24766. 0x06, 0x01, 0x01,
  24767. 0x00, 0x00,
  24768. 0x31, 0x80,
  24769. 0x06, 0x01, 0x01,
  24770. 0x00, 0x00,
  24771. 0x00, 0x00,
  24772. };
  24773. static const byte good4_out[] = {
  24774. 0x30, 0x12,
  24775. 0x02, 0x01, 0x01,
  24776. 0x30, 0x08,
  24777. 0x04, 0x03, 0x01, 0x02, 0x03,
  24778. 0x06, 0x01, 0x01,
  24779. 0x31, 0x03,
  24780. 0x06, 0x01, 0x01
  24781. };
  24782. static const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  24783. berDerTestData testData[] = {
  24784. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  24785. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  24786. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  24787. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  24788. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  24789. };
  24790. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  24791. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  24792. if (ret != LENGTH_ONLY_E)
  24793. return -12500 - i;
  24794. if (len != testData[i].outSz)
  24795. return -12510 - i;
  24796. len = testData[i].outSz;
  24797. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  24798. if (ret != 0)
  24799. return -12520 - i;
  24800. if (XMEMCMP(out, testData[i].out, len) != 0)
  24801. return -12530 - i;
  24802. for (l = 1; l < testData[i].inSz; l++) {
  24803. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  24804. if (ret != ASN_PARSE_E)
  24805. return -12540;
  24806. len = testData[i].outSz;
  24807. ret = wc_BerToDer(testData[i].in, l, out, &len);
  24808. if (ret != ASN_PARSE_E)
  24809. return -12541;
  24810. }
  24811. for (l = 0; l < testData[i].outSz-1; l++) {
  24812. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  24813. if (ret != BUFFER_E)
  24814. return -12542;
  24815. }
  24816. }
  24817. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  24818. if (ret != BAD_FUNC_ARG)
  24819. return -12543;
  24820. ret = wc_BerToDer(out, 4, NULL, NULL);
  24821. if (ret != BAD_FUNC_ARG)
  24822. return -12544;
  24823. ret = wc_BerToDer(NULL, 4, NULL, &len);
  24824. if (ret != BAD_FUNC_ARG)
  24825. return -12545;
  24826. ret = wc_BerToDer(NULL, 4, out, NULL);
  24827. if (ret != BAD_FUNC_ARG)
  24828. return -12546;
  24829. ret = wc_BerToDer(out, 4, out, NULL);
  24830. if (ret != BAD_FUNC_ARG)
  24831. return -12547;
  24832. ret = wc_BerToDer(NULL, 4, out, &len);
  24833. if (ret != BAD_FUNC_ARG)
  24834. return -12548;
  24835. for (l = 1; l < sizeof(good4_out); l++) {
  24836. len = l;
  24837. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  24838. if (ret != BUFFER_E)
  24839. return -12549;
  24840. }
  24841. return 0;
  24842. }
  24843. #endif
  24844. #ifdef DEBUG_WOLFSSL
  24845. static THREAD_LS_T int log_cnt = 0;
  24846. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  24847. {
  24848. (void)logLevel;
  24849. (void)logMessage;
  24850. log_cnt++;
  24851. }
  24852. #endif /* DEBUG_WOLFSSL */
  24853. int logging_test(void)
  24854. {
  24855. #ifdef DEBUG_WOLFSSL
  24856. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  24857. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  24858. byte b[256];
  24859. int i;
  24860. for (i = 0; i < (int)sizeof(b); i++)
  24861. b[i] = i;
  24862. if (wolfSSL_Debugging_ON() != 0)
  24863. return -12600;
  24864. if (wolfSSL_SetLoggingCb(my_Logging_cb) != 0)
  24865. return -12601;
  24866. WOLFSSL_MSG(msg);
  24867. WOLFSSL_BUFFER(a, sizeof(a));
  24868. WOLFSSL_BUFFER(b, sizeof(b));
  24869. WOLFSSL_BUFFER(NULL, 0);
  24870. WOLFSSL_ERROR(MEMORY_E);
  24871. WOLFSSL_ERROR_MSG(msg);
  24872. /* turn off logs */
  24873. wolfSSL_Debugging_OFF();
  24874. /* capture log count */
  24875. i = log_cnt;
  24876. /* validate no logs are output when disabled */
  24877. WOLFSSL_MSG(msg);
  24878. WOLFSSL_BUFFER(a, sizeof(a));
  24879. WOLFSSL_BUFFER(b, sizeof(b));
  24880. WOLFSSL_BUFFER(NULL, 0);
  24881. WOLFSSL_ERROR(MEMORY_E);
  24882. WOLFSSL_ERROR_MSG(msg);
  24883. /* check the logs were disabled */
  24884. if (i != log_cnt)
  24885. return -12602;
  24886. /* restore callback and leave logging enabled */
  24887. wolfSSL_SetLoggingCb(NULL);
  24888. wolfSSL_Debugging_ON();
  24889. /* suppress unused args */
  24890. (void)a;
  24891. (void)b;
  24892. #else
  24893. if (wolfSSL_Debugging_ON() != NOT_COMPILED_IN)
  24894. return -12603;
  24895. wolfSSL_Debugging_OFF();
  24896. if (wolfSSL_SetLoggingCb(NULL) != NOT_COMPILED_IN)
  24897. return -12604;
  24898. #endif /* DEBUG_WOLFSSL */
  24899. return 0;
  24900. }
  24901. int mutex_test(void)
  24902. {
  24903. #ifdef WOLFSSL_PTHREADS
  24904. wolfSSL_Mutex m;
  24905. #endif
  24906. #ifndef WOLFSSL_NO_MALLOC
  24907. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  24908. if (mm == NULL)
  24909. return -12700;
  24910. wc_FreeMutex(mm);
  24911. XFREE(mm, NULL, DYNAMIC_TYPE_MUTEX);
  24912. #endif
  24913. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  24914. #ifdef WOLFSSL_PTHREADS
  24915. if (wc_InitMutex(&m) != 0)
  24916. return -12701;
  24917. if (wc_LockMutex(&m) != 0)
  24918. return -12702;
  24919. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  24920. /* trying to free a locked mutex is not portable behavior with pthread */
  24921. /* Attempting to destroy a locked mutex results in undefined behavior */
  24922. if (wc_FreeMutex(&m) != BAD_MUTEX_E)
  24923. return -12703;
  24924. #endif
  24925. if (wc_UnLockMutex(&m) != 0)
  24926. return -12704;
  24927. if (wc_FreeMutex(&m) != 0)
  24928. return -12705;
  24929. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  24930. /* Trying to use a pthread after free'ing is not portable behavior */
  24931. if (wc_LockMutex(&m) != BAD_MUTEX_E)
  24932. return -12706;
  24933. if (wc_UnLockMutex(&m) != BAD_MUTEX_E)
  24934. return -12707;
  24935. #endif
  24936. #endif
  24937. return 0;
  24938. }
  24939. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  24940. #ifndef WOLFSSL_NO_MALLOC
  24941. static int malloc_cnt = 0;
  24942. static int realloc_cnt = 0;
  24943. static int free_cnt = 0;
  24944. static void *my_Malloc_cb(size_t size)
  24945. {
  24946. malloc_cnt++;
  24947. #ifndef WOLFSSL_NO_MALLOC
  24948. return malloc(size);
  24949. #else
  24950. WOLFSSL_MSG("No malloc available");
  24951. (void)size;
  24952. return NULL;
  24953. #endif
  24954. }
  24955. static void my_Free_cb(void *ptr)
  24956. {
  24957. free_cnt++;
  24958. #ifndef WOLFSSL_NO_MALLOC
  24959. free(ptr);
  24960. #else
  24961. WOLFSSL_MSG("No free available");
  24962. (void)ptr;
  24963. #endif
  24964. }
  24965. static void *my_Realloc_cb(void *ptr, size_t size)
  24966. {
  24967. realloc_cnt++;
  24968. #ifndef WOLFSSL_NO_MALLOC
  24969. return realloc(ptr, size);
  24970. #else
  24971. WOLFSSL_MSG("No realloc available");
  24972. (void)ptr;
  24973. (void)size;
  24974. return NULL;
  24975. #endif
  24976. }
  24977. #endif /* !WOLFSSL_NO_MALLOC */
  24978. int memcb_test(void)
  24979. {
  24980. int ret = 0;
  24981. #ifndef WOLFSSL_NO_MALLOC
  24982. byte* b = NULL;
  24983. #endif
  24984. wolfSSL_Malloc_cb mc;
  24985. wolfSSL_Free_cb fc;
  24986. wolfSSL_Realloc_cb rc;
  24987. /* Save existing memory callbacks */
  24988. if (wolfSSL_GetAllocators(&mc, &fc, &rc) != 0)
  24989. return -12800;
  24990. #ifndef WOLFSSL_NO_MALLOC
  24991. /* test realloc */
  24992. b = (byte*)XREALLOC(b, 1024, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24993. if (b == NULL) {
  24994. ERROR_OUT(-12801, exit_memcb);
  24995. }
  24996. XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24997. b = NULL;
  24998. /* Use API. */
  24999. if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)(void*)&my_Malloc_cb,
  25000. (wolfSSL_Free_cb)(void*)&my_Free_cb,
  25001. (wolfSSL_Realloc_cb)(void*)&my_Realloc_cb) != 0) {
  25002. ERROR_OUT(-12802, exit_memcb);
  25003. }
  25004. b = (byte*)XMALLOC(1024, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25005. b = (byte*)XREALLOC(b, 1024, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25006. XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25007. #ifndef WOLFSSL_STATIC_MEMORY
  25008. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  25009. #else
  25010. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  25011. #endif
  25012. ret = -12803;
  25013. #endif /* !WOLFSSL_NO_MALLOC */
  25014. #ifndef WOLFSSL_NO_MALLOC
  25015. exit_memcb:
  25016. #endif
  25017. /* restore memory callbacks */
  25018. wolfSSL_SetAllocators(mc, fc, rc);
  25019. return ret;
  25020. }
  25021. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  25022. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  25023. int blob_test(void)
  25024. {
  25025. int ret = 0;
  25026. byte out[112];
  25027. byte blob[112];
  25028. word32 outSz;
  25029. const byte iv[] =
  25030. {
  25031. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  25032. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  25033. };
  25034. const byte text[] =
  25035. {
  25036. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  25037. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  25038. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  25039. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  25040. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  25041. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  25042. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  25043. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  25044. };
  25045. XMEMSET(blob, 0, sizeof(blob));
  25046. outSz = sizeof(blob);
  25047. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  25048. if (ret != 0) {
  25049. ERROR_OUT(-12900, exit_blob);
  25050. }
  25051. blob[outSz - 2] += 1;
  25052. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  25053. if (ret == 0) { /* should fail with altered blob */
  25054. ERROR_OUT(-12901, exit_blob);
  25055. }
  25056. XMEMSET(blob, 0, sizeof(blob));
  25057. outSz = sizeof(blob);
  25058. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  25059. if (ret != 0) {
  25060. ERROR_OUT(-12902, exit_blob);
  25061. }
  25062. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  25063. if (ret != 0) {
  25064. ERROR_OUT(-12903, exit_blob);
  25065. }
  25066. if (XMEMCMP(out, iv, sizeof(iv))) {
  25067. ERROR_OUT(-12904, exit_blob);
  25068. }
  25069. XMEMSET(blob, 0, sizeof(blob));
  25070. outSz = sizeof(blob);
  25071. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  25072. if (ret != 0) {
  25073. ERROR_OUT(-12905, exit_blob);
  25074. }
  25075. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  25076. if (ret != 0) {
  25077. ERROR_OUT(-12906, exit_blob);
  25078. }
  25079. if (XMEMCMP(out, text, sizeof(text))) {
  25080. ERROR_OUT(-12907, exit_blob);
  25081. }
  25082. exit_blob:
  25083. return ret;
  25084. }
  25085. #endif /* WOLFSSL_IMX6_CAAM_BLOB */
  25086. #ifdef WOLF_CRYPTO_CB
  25087. /* Example custom context for crypto callback */
  25088. typedef struct {
  25089. int exampleVar; /* example, not used */
  25090. } myCryptoDevCtx;
  25091. /* Example crypto dev callback function that calls software version */
  25092. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  25093. {
  25094. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  25095. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  25096. if (info == NULL)
  25097. return BAD_FUNC_ARG;
  25098. #ifdef DEBUG_WOLFSSL
  25099. printf("CryptoDevCb: Algo Type %d\n", info->algo_type);
  25100. #endif
  25101. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  25102. #ifndef WC_NO_RNG
  25103. /* set devId to invalid, so software is used */
  25104. info->rng.rng->devId = INVALID_DEVID;
  25105. ret = wc_RNG_GenerateBlock(info->rng.rng,
  25106. info->rng.out, info->rng.sz);
  25107. /* reset devId */
  25108. info->rng.rng->devId = devIdArg;
  25109. #endif
  25110. }
  25111. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  25112. #ifndef WC_NO_RNG
  25113. static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  25114. word32* seedWord32 = (word32*)seed;
  25115. word32 len;
  25116. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  25117. while (info->seed.sz > 0) {
  25118. len = (word32)sizeof(seed);
  25119. if (info->seed.sz < len)
  25120. len = info->seed.sz;
  25121. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  25122. info->seed.seed += len;
  25123. info->seed.sz -= len;
  25124. (*seedWord32)++;
  25125. }
  25126. ret = 0;
  25127. #endif
  25128. }
  25129. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  25130. #ifdef DEBUG_WOLFSSL
  25131. printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
  25132. #endif
  25133. #ifndef NO_RSA
  25134. if (info->pk.type == WC_PK_TYPE_RSA) {
  25135. /* set devId to invalid, so software is used */
  25136. info->pk.rsa.key->devId = INVALID_DEVID;
  25137. switch (info->pk.rsa.type) {
  25138. case RSA_PUBLIC_ENCRYPT:
  25139. case RSA_PUBLIC_DECRYPT:
  25140. /* perform software based RSA public op */
  25141. ret = wc_RsaFunction(
  25142. info->pk.rsa.in, info->pk.rsa.inLen,
  25143. info->pk.rsa.out, info->pk.rsa.outLen,
  25144. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  25145. break;
  25146. case RSA_PRIVATE_ENCRYPT:
  25147. case RSA_PRIVATE_DECRYPT:
  25148. /* perform software based RSA private op */
  25149. ret = wc_RsaFunction(
  25150. info->pk.rsa.in, info->pk.rsa.inLen,
  25151. info->pk.rsa.out, info->pk.rsa.outLen,
  25152. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  25153. break;
  25154. }
  25155. /* reset devId */
  25156. info->pk.rsa.key->devId = devIdArg;
  25157. }
  25158. #ifdef WOLFSSL_KEY_GEN
  25159. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  25160. info->pk.rsakg.key->devId = INVALID_DEVID;
  25161. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  25162. info->pk.rsakg.e, info->pk.rsakg.rng);
  25163. /* reset devId */
  25164. info->pk.rsakg.key->devId = devIdArg;
  25165. }
  25166. #endif
  25167. #endif /* !NO_RSA */
  25168. #ifdef HAVE_ECC
  25169. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  25170. /* set devId to invalid, so software is used */
  25171. info->pk.eckg.key->devId = INVALID_DEVID;
  25172. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  25173. info->pk.eckg.key, info->pk.eckg.curveId);
  25174. /* reset devId */
  25175. info->pk.eckg.key->devId = devIdArg;
  25176. }
  25177. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  25178. /* set devId to invalid, so software is used */
  25179. info->pk.eccsign.key->devId = INVALID_DEVID;
  25180. ret = wc_ecc_sign_hash(
  25181. info->pk.eccsign.in, info->pk.eccsign.inlen,
  25182. info->pk.eccsign.out, info->pk.eccsign.outlen,
  25183. info->pk.eccsign.rng, info->pk.eccsign.key);
  25184. /* reset devId */
  25185. info->pk.eccsign.key->devId = devIdArg;
  25186. }
  25187. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  25188. /* set devId to invalid, so software is used */
  25189. info->pk.eccverify.key->devId = INVALID_DEVID;
  25190. ret = wc_ecc_verify_hash(
  25191. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  25192. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  25193. info->pk.eccverify.res, info->pk.eccverify.key);
  25194. /* reset devId */
  25195. info->pk.eccverify.key->devId = devIdArg;
  25196. }
  25197. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  25198. /* set devId to invalid, so software is used */
  25199. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  25200. ret = wc_ecc_shared_secret(
  25201. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  25202. info->pk.ecdh.out, info->pk.ecdh.outlen);
  25203. /* reset devId */
  25204. info->pk.ecdh.private_key->devId = devIdArg;
  25205. }
  25206. #endif /* HAVE_ECC */
  25207. }
  25208. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  25209. #if !defined(NO_AES) || !defined(NO_DES3)
  25210. #ifdef HAVE_AESGCM
  25211. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  25212. if (info->cipher.enc) {
  25213. /* set devId to invalid, so software is used */
  25214. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  25215. ret = wc_AesGcmEncrypt(
  25216. info->cipher.aesgcm_enc.aes,
  25217. info->cipher.aesgcm_enc.out,
  25218. info->cipher.aesgcm_enc.in,
  25219. info->cipher.aesgcm_enc.sz,
  25220. info->cipher.aesgcm_enc.iv,
  25221. info->cipher.aesgcm_enc.ivSz,
  25222. info->cipher.aesgcm_enc.authTag,
  25223. info->cipher.aesgcm_enc.authTagSz,
  25224. info->cipher.aesgcm_enc.authIn,
  25225. info->cipher.aesgcm_enc.authInSz);
  25226. /* reset devId */
  25227. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  25228. }
  25229. else {
  25230. /* set devId to invalid, so software is used */
  25231. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  25232. ret = wc_AesGcmDecrypt(
  25233. info->cipher.aesgcm_dec.aes,
  25234. info->cipher.aesgcm_dec.out,
  25235. info->cipher.aesgcm_dec.in,
  25236. info->cipher.aesgcm_dec.sz,
  25237. info->cipher.aesgcm_dec.iv,
  25238. info->cipher.aesgcm_dec.ivSz,
  25239. info->cipher.aesgcm_dec.authTag,
  25240. info->cipher.aesgcm_dec.authTagSz,
  25241. info->cipher.aesgcm_dec.authIn,
  25242. info->cipher.aesgcm_dec.authInSz);
  25243. /* reset devId */
  25244. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  25245. }
  25246. }
  25247. #endif /* HAVE_AESGCM */
  25248. #ifdef HAVE_AES_CBC
  25249. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  25250. if (info->cipher.enc) {
  25251. /* set devId to invalid, so software is used */
  25252. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  25253. ret = wc_AesCbcEncrypt(
  25254. info->cipher.aescbc.aes,
  25255. info->cipher.aescbc.out,
  25256. info->cipher.aescbc.in,
  25257. info->cipher.aescbc.sz);
  25258. /* reset devId */
  25259. info->cipher.aescbc.aes->devId = devIdArg;
  25260. }
  25261. else {
  25262. /* set devId to invalid, so software is used */
  25263. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  25264. ret = wc_AesCbcDecrypt(
  25265. info->cipher.aescbc.aes,
  25266. info->cipher.aescbc.out,
  25267. info->cipher.aescbc.in,
  25268. info->cipher.aescbc.sz);
  25269. /* reset devId */
  25270. info->cipher.aescbc.aes->devId = devIdArg;
  25271. }
  25272. }
  25273. #endif /* HAVE_AES_CBC */
  25274. #ifndef NO_DES3
  25275. if (info->cipher.type == WC_CIPHER_DES3) {
  25276. if (info->cipher.enc) {
  25277. /* set devId to invalid, so software is used */
  25278. info->cipher.des3.des->devId = INVALID_DEVID;
  25279. ret = wc_Des3_CbcEncrypt(
  25280. info->cipher.des3.des,
  25281. info->cipher.des3.out,
  25282. info->cipher.des3.in,
  25283. info->cipher.des3.sz);
  25284. /* reset devId */
  25285. info->cipher.des3.des->devId = devIdArg;
  25286. }
  25287. else {
  25288. /* set devId to invalid, so software is used */
  25289. info->cipher.des3.des->devId = INVALID_DEVID;
  25290. ret = wc_Des3_CbcDecrypt(
  25291. info->cipher.des3.des,
  25292. info->cipher.des3.out,
  25293. info->cipher.des3.in,
  25294. info->cipher.des3.sz);
  25295. /* reset devId */
  25296. info->cipher.des3.des->devId = devIdArg;
  25297. }
  25298. }
  25299. #endif /* !NO_DES3 */
  25300. #endif /* !NO_AES || !NO_DES3 */
  25301. }
  25302. #if !defined(NO_SHA) || !defined(NO_SHA256)
  25303. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  25304. #if !defined(NO_SHA)
  25305. if (info->hash.type == WC_HASH_TYPE_SHA) {
  25306. if (info->hash.sha1 == NULL)
  25307. return NOT_COMPILED_IN;
  25308. /* set devId to invalid, so software is used */
  25309. info->hash.sha1->devId = INVALID_DEVID;
  25310. if (info->hash.in != NULL) {
  25311. ret = wc_ShaUpdate(
  25312. info->hash.sha1,
  25313. info->hash.in,
  25314. info->hash.inSz);
  25315. }
  25316. if (info->hash.digest != NULL) {
  25317. ret = wc_ShaFinal(
  25318. info->hash.sha1,
  25319. info->hash.digest);
  25320. }
  25321. /* reset devId */
  25322. info->hash.sha1->devId = devIdArg;
  25323. }
  25324. else
  25325. #endif
  25326. #if !defined(NO_SHA256)
  25327. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  25328. if (info->hash.sha256 == NULL)
  25329. return NOT_COMPILED_IN;
  25330. /* set devId to invalid, so software is used */
  25331. info->hash.sha256->devId = INVALID_DEVID;
  25332. if (info->hash.in != NULL) {
  25333. ret = wc_Sha256Update(
  25334. info->hash.sha256,
  25335. info->hash.in,
  25336. info->hash.inSz);
  25337. }
  25338. if (info->hash.digest != NULL) {
  25339. ret = wc_Sha256Final(
  25340. info->hash.sha256,
  25341. info->hash.digest);
  25342. }
  25343. /* reset devId */
  25344. info->hash.sha256->devId = devIdArg;
  25345. }
  25346. else
  25347. #endif
  25348. {
  25349. }
  25350. }
  25351. #endif /* !NO_SHA || !NO_SHA256 */
  25352. #ifndef NO_HMAC
  25353. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  25354. if (info->hmac.hmac == NULL)
  25355. return NOT_COMPILED_IN;
  25356. /* set devId to invalid, so software is used */
  25357. info->hmac.hmac->devId = INVALID_DEVID;
  25358. if (info->hash.in != NULL) {
  25359. ret = wc_HmacUpdate(
  25360. info->hmac.hmac,
  25361. info->hmac.in,
  25362. info->hmac.inSz);
  25363. }
  25364. else if (info->hash.digest != NULL) {
  25365. ret = wc_HmacFinal(
  25366. info->hmac.hmac,
  25367. info->hmac.digest);
  25368. }
  25369. /* reset devId */
  25370. info->hmac.hmac->devId = devIdArg;
  25371. }
  25372. #endif
  25373. (void)devIdArg;
  25374. (void)myCtx;
  25375. return ret;
  25376. }
  25377. int cryptocb_test(void)
  25378. {
  25379. int ret = 0;
  25380. myCryptoDevCtx myCtx;
  25381. /* example data for callback */
  25382. myCtx.exampleVar = 1;
  25383. /* set devId to something other than INVALID_DEVID */
  25384. devId = 1;
  25385. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  25386. #ifndef WC_NO_RNG
  25387. if (ret == 0)
  25388. ret = random_test();
  25389. #endif /* WC_NO_RNG */
  25390. #ifndef NO_RSA
  25391. if (ret == 0)
  25392. ret = rsa_test();
  25393. #endif
  25394. #ifdef HAVE_ECC
  25395. if (ret == 0)
  25396. ret = ecc_test();
  25397. #endif
  25398. #ifndef NO_AES
  25399. #ifdef HAVE_AESGCM
  25400. if (ret == 0)
  25401. ret = aesgcm_test();
  25402. #endif
  25403. #ifdef HAVE_AES_CBC
  25404. if (ret == 0)
  25405. ret = aes_test();
  25406. #endif
  25407. #endif /* !NO_AES */
  25408. #ifndef NO_DES3
  25409. if (ret == 0)
  25410. ret = des3_test();
  25411. #endif /* !NO_DES3 */
  25412. #if !defined(NO_SHA) || !defined(NO_SHA256)
  25413. #ifndef NO_SHA
  25414. if (ret == 0)
  25415. ret = sha_test();
  25416. #endif
  25417. #ifndef NO_SHA256
  25418. if (ret == 0)
  25419. ret = sha256_test();
  25420. #endif
  25421. #endif
  25422. #ifndef NO_HMAC
  25423. #ifndef NO_SHA
  25424. if (ret == 0)
  25425. ret = hmac_sha_test();
  25426. #endif
  25427. #ifndef NO_SHA256
  25428. if (ret == 0)
  25429. ret = hmac_sha256_test();
  25430. #endif
  25431. #endif
  25432. #ifndef NO_PWDBASED
  25433. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  25434. if (ret == 0)
  25435. ret = pbkdf2_test();
  25436. #endif
  25437. #endif
  25438. /* reset devId */
  25439. devId = INVALID_DEVID;
  25440. return ret;
  25441. }
  25442. #endif /* WOLF_CRYPTO_CB */
  25443. #ifdef WOLFSSL_CERT_PIV
  25444. int certpiv_test(void)
  25445. {
  25446. int ret;
  25447. wc_CertPIV piv;
  25448. /* Template for Identiv PIV cert, nonce and signature */
  25449. const byte pivCertIdentiv[] = {
  25450. 0x0A, 0x0D,
  25451. 0x53, 0x04, /* NIST PIV Cert */
  25452. 0x70, 0x02, /* Certificate */
  25453. 0x30, 0x00,
  25454. 0x71, 0x01, 0x00, /* Cert Info */
  25455. 0xFE, 0x00, /* Error Detection */
  25456. 0x0B, 0x01, 0x00, /* Nonce */
  25457. 0x0C, 0x01, 0x00, /* Signed Nonce */
  25458. };
  25459. const byte pivCert[] = {
  25460. 0x53, 0x04, /* NIST PIV Cert */
  25461. 0x70, 0x02, /* Certificate */
  25462. 0x30, 0x00,
  25463. 0x71, 0x01, 0x00, /* Cert Info */
  25464. 0xFE, 0x00, /* Error Detection */
  25465. };
  25466. /* Test with identiv 0x0A, 0x0B and 0x0C markers */
  25467. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  25468. if (ret == 0) {
  25469. /* Test with NIST PIV format */
  25470. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  25471. }
  25472. return ret;
  25473. }
  25474. #endif /* WOLFSSL_CERT_PIV */
  25475. #undef ERROR_OUT
  25476. #else
  25477. #ifndef NO_MAIN_DRIVER
  25478. int main() { return 0; }
  25479. #endif
  25480. #endif /* NO_CRYPT_TEST */