mbedtls.c 1.9 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582585835858458585585865858758588585895859058591585925859358594585955859658597585985859958600586015860258603586045860558606586075860858609586105861158612586135861458615586165861758618586195862058621586225862358624586255862658627586285862958630586315863258633586345863558636586375863858639586405864158642586435864458645586465864758648586495865058651586525865358654586555865658657586585865958660586615866258663586645866558666586675866858669586705867158672586735867458675586765867758678586795868058681586825868358684586855868658687586885868958690586915869258693586945869558696586975869858699587005870158702587035870458705587065870758708587095871058711587125871358714587155871658717587185871958720587215872258723587245872558726587275872858729587305873158732587335873458735587365873758738587395874058741587425874358744587455874658747587485874958750587515875258753587545875558756587575875858759587605876158762587635876458765587665876758768587695877058771587725877358774587755877658777587785877958780587815878258783587845878558786587875878858789587905879158792587935879458795587965879758798587995880058801588025880358804588055880658807588085880958810588115881258813588145881558816588175881858819588205882158822588235882458825588265882758828588295883058831588325883358834588355883658837588385883958840588415884258843588445884558846588475884858849588505885158852588535885458855588565885758858588595886058861588625886358864588655886658867588685886958870588715887258873588745887558876588775887858879588805888158882588835888458885588865888758888588895889058891588925889358894588955889658897588985889958900589015890258903589045890558906589075890858909589105891158912589135891458915589165891758918589195892058921589225892358924589255892658927589285892958930589315893258933589345893558936589375893858939589405894158942589435894458945589465894758948589495895058951589525895358954589555895658957589585895958960589615896258963589645896558966589675896858969589705897158972589735897458975589765897758978589795898058981589825898358984589855898658987589885898958990589915899258993589945899558996589975899858999590005900159002590035900459005590065900759008590095901059011590125901359014590155901659017590185901959020590215902259023590245902559026590275902859029590305903159032590335903459035590365903759038590395904059041590425904359044590455904659047590485904959050590515905259053590545905559056590575905859059590605906159062590635906459065590665906759068590695907059071590725907359074590755907659077590785907959080590815908259083590845908559086590875908859089590905909159092590935909459095590965909759098590995910059101591025910359104591055910659107591085910959110591115911259113591145911559116591175911859119591205912159122591235912459125591265912759128591295913059131591325913359134591355913659137591385913959140591415914259143591445914559146591475914859149591505915159152591535915459155591565915759158591595916059161591625916359164591655916659167591685916959170591715917259173591745917559176591775917859179591805918159182591835918459185591865918759188591895919059191591925919359194591955919659197591985919959200592015920259203592045920559206592075920859209592105921159212592135921459215592165921759218592195922059221592225922359224592255922659227592285922959230592315923259233592345923559236592375923859239592405924159242592435924459245592465924759248592495925059251592525925359254592555925659257592585925959260592615926259263592645926559266592675926859269592705927159272592735927459275592765927759278592795928059281592825928359284592855928659287592885928959290592915929259293592945929559296592975929859299593005930159302593035930459305593065930759308593095931059311593125931359314593155931659317593185931959320593215932259323593245932559326593275932859329593305933159332593335933459335593365933759338593395934059341593425934359344593455934659347593485934959350593515935259353593545935559356593575935859359593605936159362593635936459365593665936759368593695937059371593725937359374593755937659377593785937959380593815938259383593845938559386593875938859389593905939159392593935939459395593965939759398593995940059401594025940359404594055940659407594085940959410594115941259413594145941559416594175941859419594205942159422594235942459425594265942759428594295943059431594325943359434594355943659437594385943959440594415944259443594445944559446594475944859449594505945159452594535945459455594565945759458594595946059461594625946359464594655946659467594685946959470594715947259473594745947559476594775947859479594805948159482594835948459485594865948759488594895949059491594925949359494594955949659497594985949959500595015950259503595045950559506595075950859509595105951159512595135951459515595165951759518595195952059521595225952359524595255952659527595285952959530595315953259533595345953559536595375953859539595405954159542595435954459545595465954759548595495955059551595525955359554595555955659557595585955959560595615956259563595645956559566595675956859569595705957159572595735957459575595765957759578595795958059581595825958359584595855958659587595885958959590595915959259593595945959559596595975959859599596005960159602596035960459605596065960759608596095961059611596125961359614596155961659617596185961959620596215962259623596245962559626596275962859629596305963159632596335963459635596365963759638596395964059641596425964359644596455964659647596485964959650596515965259653596545965559656596575965859659596605966159662596635966459665596665966759668596695967059671596725967359674596755967659677596785967959680596815968259683596845968559686596875968859689596905969159692596935969459695596965969759698596995970059701597025970359704597055970659707597085970959710597115971259713597145971559716597175971859719597205972159722597235972459725597265972759728597295973059731597325973359734597355973659737597385973959740597415974259743597445974559746597475974859749597505975159752597535975459755597565975759758597595976059761597625976359764597655976659767597685976959770597715977259773597745977559776597775977859779597805978159782597835978459785597865978759788597895979059791597925979359794597955979659797597985979959800598015980259803598045980559806598075980859809598105981159812598135981459815598165981759818598195982059821598225982359824598255982659827598285982959830598315983259833598345983559836598375983859839598405984159842598435984459845598465984759848598495985059851598525985359854598555985659857598585985959860598615986259863598645986559866598675986859869598705987159872598735987459875598765987759878598795988059881598825988359884598855988659887598885988959890598915989259893598945989559896598975989859899599005990159902599035990459905599065990759908599095991059911599125991359914599155991659917599185991959920599215992259923599245992559926599275992859929599305993159932599335993459935599365993759938599395994059941599425994359944599455994659947599485994959950599515995259953599545995559956599575995859959599605996159962599635996459965599665996759968599695997059971599725997359974599755997659977599785997959980599815998259983599845998559986599875998859989599905999159992599935999459995599965999759998599996000060001600026000360004600056000660007600086000960010600116001260013600146001560016600176001860019600206002160022600236002460025600266002760028600296003060031600326003360034600356003660037600386003960040600416004260043600446004560046600476004860049600506005160052600536005460055600566005760058600596006060061600626006360064600656006660067600686006960070600716007260073600746007560076600776007860079600806008160082600836008460085600866008760088600896009060091600926009360094600956009660097600986009960100601016010260103601046010560106601076010860109601106011160112601136011460115601166011760118601196012060121601226012360124601256012660127601286012960130601316013260133601346013560136601376013860139601406014160142601436014460145601466014760148601496015060151601526015360154601556015660157601586015960160601616016260163601646016560166601676016860169601706017160172601736017460175601766017760178601796018060181601826018360184601856018660187601886018960190601916019260193601946019560196601976019860199602006020160202602036020460205602066020760208602096021060211602126021360214602156021660217602186021960220602216022260223602246022560226602276022860229602306023160232602336023460235602366023760238602396024060241602426024360244602456024660247602486024960250602516025260253602546025560256602576025860259602606026160262602636026460265602666026760268602696027060271602726027360274602756027660277602786027960280602816028260283602846028560286602876028860289602906029160292602936029460295602966029760298602996030060301603026030360304603056030660307603086030960310603116031260313603146031560316603176031860319603206032160322603236032460325603266032760328603296033060331603326033360334603356033660337603386033960340603416034260343603446034560346603476034860349603506035160352603536035460355603566035760358603596036060361603626036360364603656036660367603686036960370603716037260373603746037560376603776037860379603806038160382603836038460385603866038760388603896039060391603926039360394603956039660397603986039960400604016040260403604046040560406604076040860409604106041160412604136041460415604166041760418604196042060421604226042360424604256042660427604286042960430604316043260433604346043560436604376043860439604406044160442604436044460445604466044760448604496045060451604526045360454604556045660457604586045960460604616046260463604646046560466604676046860469604706047160472604736047460475604766047760478604796048060481604826048360484604856048660487604886048960490604916049260493604946049560496604976049860499605006050160502605036050460505605066050760508605096051060511605126051360514605156051660517605186051960520605216052260523605246052560526605276052860529605306053160532605336053460535605366053760538605396054060541605426054360544605456054660547605486054960550605516055260553605546055560556605576055860559605606056160562605636056460565605666056760568605696057060571605726057360574605756057660577605786057960580605816058260583605846058560586605876058860589605906059160592605936059460595605966059760598605996060060601606026060360604606056060660607606086060960610606116061260613606146061560616606176061860619606206062160622606236062460625606266062760628606296063060631606326063360634606356063660637606386063960640606416064260643606446064560646606476064860649606506065160652606536065460655606566065760658606596066060661606626066360664606656066660667606686066960670606716067260673606746067560676606776067860679606806068160682606836068460685606866068760688606896069060691606926069360694606956069660697606986069960700607016070260703607046070560706607076070860709607106071160712607136071460715607166071760718607196072060721607226072360724607256072660727607286072960730607316073260733607346073560736607376073860739607406074160742607436074460745607466074760748607496075060751607526075360754607556075660757607586075960760607616076260763607646076560766607676076860769607706077160772607736077460775607766077760778607796078060781607826078360784607856078660787607886078960790607916079260793607946079560796607976079860799608006080160802608036080460805608066080760808608096081060811608126081360814608156081660817608186081960820608216082260823608246082560826608276082860829608306083160832608336083460835608366083760838608396084060841608426084360844608456084660847608486084960850608516085260853608546085560856608576085860859608606086160862608636086460865608666086760868608696087060871608726087360874608756087660877608786087960880608816088260883608846088560886608876088860889608906089160892608936089460895608966089760898608996090060901609026090360904609056090660907609086090960910609116091260913609146091560916609176091860919609206092160922609236092460925609266092760928609296093060931609326093360934609356093660937609386093960940609416094260943609446094560946609476094860949609506095160952609536095460955609566095760958609596096060961609626096360964609656096660967609686096960970609716097260973609746097560976609776097860979609806098160982609836098460985609866098760988609896099060991609926099360994609956099660997609986099961000610016100261003610046100561006610076100861009610106101161012610136101461015610166101761018610196102061021610226102361024610256102661027610286102961030610316103261033610346103561036610376103861039610406104161042610436104461045610466104761048610496105061051610526105361054610556105661057610586105961060610616106261063610646106561066610676106861069610706107161072610736107461075610766107761078610796108061081610826108361084610856108661087610886108961090610916109261093610946109561096610976109861099611006110161102611036110461105611066110761108611096111061111611126111361114611156111661117611186111961120611216112261123611246112561126611276112861129611306113161132611336113461135611366113761138611396114061141611426114361144611456114661147611486114961150611516115261153611546115561156611576115861159611606116161162611636116461165611666116761168611696117061171611726117361174611756117661177611786117961180611816118261183611846118561186611876118861189611906119161192611936119461195611966119761198611996120061201612026120361204612056120661207612086120961210612116121261213612146121561216612176121861219612206122161222612236122461225612266122761228612296123061231612326123361234612356123661237612386123961240612416124261243612446124561246612476124861249612506125161252612536125461255612566125761258612596126061261612626126361264612656126661267612686126961270612716127261273612746127561276612776127861279612806128161282612836128461285612866128761288612896129061291612926129361294612956129661297612986129961300613016130261303613046130561306613076130861309613106131161312613136131461315613166131761318613196132061321613226132361324613256132661327613286132961330613316133261333613346133561336613376133861339613406134161342613436134461345613466134761348613496135061351613526135361354613556135661357613586135961360613616136261363613646136561366613676136861369613706137161372613736137461375613766137761378613796138061381613826138361384613856138661387613886138961390613916139261393613946139561396613976139861399614006140161402614036140461405614066140761408614096141061411614126141361414614156141661417614186141961420614216142261423614246142561426614276142861429614306143161432614336143461435614366143761438614396144061441614426144361444614456144661447614486144961450614516145261453614546145561456614576145861459614606146161462614636146461465614666146761468614696147061471614726147361474614756147661477614786147961480614816148261483614846148561486614876148861489614906149161492614936149461495614966149761498614996150061501615026150361504615056150661507615086150961510615116151261513615146151561516615176151861519615206152161522615236152461525615266152761528615296153061531615326153361534615356153661537615386153961540615416154261543615446154561546615476154861549615506155161552615536155461555615566155761558615596156061561615626156361564615656156661567615686156961570615716157261573615746157561576615776157861579615806158161582615836158461585615866158761588615896159061591615926159361594615956159661597615986159961600616016160261603616046160561606616076160861609616106161161612616136161461615616166161761618616196162061621616226162361624616256162661627616286162961630616316163261633616346163561636616376163861639616406164161642616436164461645616466164761648616496165061651616526165361654616556165661657616586165961660616616166261663616646166561666616676166861669616706167161672616736167461675616766167761678616796168061681616826168361684616856168661687616886168961690616916169261693616946169561696616976169861699617006170161702617036170461705617066170761708617096171061711617126171361714617156171661717617186171961720617216172261723617246172561726617276172861729617306173161732617336173461735617366173761738617396174061741617426174361744617456174661747617486174961750617516175261753617546175561756617576175861759617606176161762617636176461765617666176761768617696177061771617726177361774617756177661777617786177961780617816178261783617846178561786617876178861789617906179161792617936179461795617966179761798617996180061801618026180361804618056180661807618086180961810618116181261813618146181561816618176181861819618206182161822618236182461825618266182761828618296183061831618326183361834618356183661837618386183961840618416184261843618446184561846618476184861849618506185161852618536185461855618566185761858618596186061861618626186361864618656186661867618686186961870618716187261873618746187561876618776187861879618806188161882618836188461885618866188761888618896189061891618926189361894618956189661897618986189961900619016190261903619046190561906619076190861909619106191161912619136191461915619166191761918619196192061921619226192361924619256192661927619286192961930619316193261933619346193561936619376193861939619406194161942619436194461945619466194761948619496195061951619526195361954619556195661957619586195961960619616196261963619646196561966619676196861969619706197161972619736197461975619766197761978619796198061981619826198361984619856198661987619886198961990619916199261993619946199561996619976199861999620006200162002620036200462005620066200762008620096201062011620126201362014620156201662017620186201962020620216202262023620246202562026620276202862029620306203162032620336203462035620366203762038620396204062041620426204362044620456204662047620486204962050620516205262053620546205562056620576205862059620606206162062620636206462065620666206762068620696207062071620726207362074620756207662077620786207962080620816208262083620846208562086620876208862089620906209162092620936209462095620966209762098620996210062101621026210362104621056210662107621086210962110621116211262113621146211562116621176211862119621206212162122621236212462125621266212762128621296213062131621326213362134621356213662137621386213962140621416214262143621446214562146621476214862149621506215162152621536215462155621566215762158621596216062161621626216362164621656216662167621686216962170621716217262173621746217562176621776217862179621806218162182621836218462185621866218762188621896219062191621926219362194621956219662197621986219962200622016220262203622046220562206622076220862209622106221162212622136221462215622166221762218622196222062221622226222362224622256222662227622286222962230622316223262233622346223562236622376223862239622406224162242622436224462245622466224762248622496225062251622526225362254622556225662257622586225962260622616226262263622646226562266622676226862269622706227162272622736227462275622766227762278622796228062281622826228362284622856228662287622886228962290622916229262293622946229562296622976229862299623006230162302623036230462305623066230762308623096231062311623126231362314623156231662317623186231962320623216232262323623246232562326623276232862329623306233162332623336233462335623366233762338623396234062341623426234362344623456234662347623486234962350623516235262353623546235562356623576235862359623606236162362623636236462365623666236762368623696237062371623726237362374623756237662377623786237962380623816238262383623846238562386623876238862389623906239162392623936239462395623966239762398623996240062401624026240362404624056240662407624086240962410624116241262413624146241562416624176241862419624206242162422624236242462425624266242762428624296243062431624326243362434624356243662437624386243962440624416244262443624446244562446624476244862449624506245162452624536245462455624566245762458624596246062461624626246362464624656246662467624686246962470624716247262473624746247562476624776247862479624806248162482624836248462485624866248762488624896249062491624926249362494624956249662497624986249962500625016250262503625046250562506625076250862509625106251162512625136251462515625166251762518625196252062521625226252362524625256252662527625286252962530625316253262533625346253562536625376253862539625406254162542625436254462545625466254762548625496255062551625526255362554625556255662557625586255962560625616256262563625646256562566625676256862569625706257162572625736257462575625766257762578625796258062581625826258362584625856258662587625886258962590625916259262593625946259562596625976259862599626006260162602626036260462605626066260762608626096261062611626126261362614626156261662617626186261962620626216262262623626246262562626626276262862629626306263162632626336263462635626366263762638626396264062641626426264362644626456264662647626486264962650626516265262653626546265562656626576265862659626606266162662626636266462665626666266762668626696267062671626726267362674626756267662677626786267962680626816268262683626846268562686626876268862689626906269162692626936269462695626966269762698626996270062701627026270362704627056270662707627086270962710627116271262713627146271562716627176271862719627206272162722627236272462725627266272762728627296273062731627326273362734627356273662737627386273962740627416274262743627446274562746627476274862749627506275162752627536275462755627566275762758627596276062761627626276362764627656276662767627686276962770627716277262773627746277562776627776277862779627806278162782627836278462785627866278762788627896279062791627926279362794627956279662797627986279962800628016280262803628046280562806628076280862809628106281162812628136281462815628166281762818628196282062821628226282362824628256282662827628286282962830628316283262833628346283562836628376283862839628406284162842628436284462845628466284762848628496285062851628526285362854628556285662857628586285962860628616286262863628646286562866628676286862869628706287162872628736287462875628766287762878628796288062881628826288362884628856288662887628886288962890628916289262893628946289562896628976289862899629006290162902629036290462905629066290762908629096291062911629126291362914629156291662917629186291962920629216292262923629246292562926629276292862929629306293162932629336293462935629366293762938629396294062941629426294362944629456294662947629486294962950629516295262953629546295562956629576295862959629606296162962629636296462965629666296762968629696297062971629726297362974629756297662977629786297962980629816298262983629846298562986629876298862989629906299162992629936299462995629966299762998629996300063001630026300363004630056300663007630086300963010630116301263013630146301563016630176301863019630206302163022630236302463025630266302763028630296303063031630326303363034630356303663037630386303963040630416304263043630446304563046630476304863049630506305163052630536305463055630566305763058630596306063061630626306363064630656306663067630686306963070630716307263073630746307563076630776307863079630806308163082630836308463085630866308763088630896309063091630926309363094630956309663097630986309963100631016310263103631046310563106631076310863109631106311163112631136311463115631166311763118631196312063121631226312363124631256312663127631286312963130631316313263133631346313563136631376313863139631406314163142631436314463145631466314763148631496315063151631526315363154631556315663157631586315963160631616316263163631646316563166631676316863169631706317163172631736317463175631766317763178631796318063181631826318363184631856318663187631886318963190631916319263193631946319563196631976319863199632006320163202632036320463205632066320763208632096321063211632126321363214632156321663217632186321963220632216322263223632246322563226632276322863229632306323163232632336323463235632366323763238632396324063241632426324363244632456324663247632486324963250632516325263253632546325563256632576325863259632606326163262632636326463265632666326763268632696327063271632726327363274632756327663277632786327963280632816328263283632846328563286632876328863289632906329163292632936329463295632966329763298632996330063301633026330363304633056330663307633086330963310633116331263313633146331563316633176331863319633206332163322633236332463325633266332763328633296333063331633326333363334633356333663337633386333963340633416334263343633446334563346633476334863349633506335163352633536335463355633566335763358633596336063361633626336363364633656336663367633686336963370633716337263373633746337563376633776337863379633806338163382633836338463385633866338763388633896339063391633926339363394633956339663397633986339963400634016340263403634046340563406634076340863409634106341163412634136341463415634166341763418634196342063421634226342363424634256342663427634286342963430634316343263433634346343563436634376343863439634406344163442634436344463445634466344763448634496345063451634526345363454634556345663457634586345963460634616346263463634646346563466634676346863469634706347163472634736347463475634766347763478634796348063481634826348363484634856348663487634886348963490634916349263493634946349563496634976349863499635006350163502635036350463505635066350763508635096351063511635126351363514635156351663517635186351963520635216352263523635246352563526635276352863529635306353163532635336353463535635366353763538635396354063541635426354363544635456354663547635486354963550635516355263553635546355563556635576355863559635606356163562635636356463565635666356763568635696357063571635726357363574635756357663577635786357963580635816358263583635846358563586635876358863589635906359163592635936359463595635966359763598635996360063601636026360363604636056360663607636086360963610636116361263613636146361563616636176361863619636206362163622636236362463625636266362763628636296363063631636326363363634636356363663637636386363963640636416364263643636446364563646636476364863649636506365163652636536365463655636566365763658636596366063661636626366363664636656366663667636686366963670636716367263673636746367563676636776367863679636806368163682636836368463685636866368763688636896369063691636926369363694636956369663697636986369963700637016370263703637046370563706637076370863709637106371163712637136371463715637166371763718637196372063721637226372363724637256372663727637286372963730637316373263733637346373563736637376373863739637406374163742637436374463745637466374763748637496375063751637526375363754637556375663757637586375963760637616376263763637646376563766637676376863769637706377163772637736377463775637766377763778637796378063781637826378363784637856378663787637886378963790637916379263793637946379563796637976379863799638006380163802638036380463805638066380763808638096381063811638126381363814638156381663817638186381963820638216382263823638246382563826638276382863829638306383163832638336383463835638366383763838638396384063841638426384363844638456384663847638486384963850638516385263853638546385563856638576385863859638606386163862638636386463865638666386763868638696387063871638726387363874638756387663877638786387963880638816388263883638846388563886638876388863889638906389163892638936389463895638966389763898638996390063901639026390363904639056390663907639086390963910639116391263913639146391563916639176391863919639206392163922639236392463925639266392763928639296393063931639326393363934639356393663937639386393963940639416394263943639446394563946639476394863949639506395163952639536395463955639566395763958639596396063961639626396363964639656396663967639686396963970639716397263973639746397563976639776397863979639806398163982639836398463985639866398763988639896399063991639926399363994639956399663997639986399964000640016400264003640046400564006640076400864009640106401164012640136401464015640166401764018640196402064021640226402364024640256402664027640286402964030640316403264033640346403564036640376403864039640406404164042640436404464045640466404764048640496405064051640526405364054640556405664057640586405964060640616406264063640646406564066640676406864069640706407164072640736407464075640766407764078640796408064081640826408364084640856408664087640886408964090640916409264093640946409564096640976409864099641006410164102641036410464105641066410764108641096411064111641126411364114641156411664117641186411964120641216412264123641246412564126641276412864129641306413164132641336413464135641366413764138641396414064141641426414364144641456414664147641486414964150641516415264153641546415564156641576415864159641606416164162641636416464165641666416764168641696417064171641726417364174641756417664177641786417964180641816418264183641846418564186641876418864189641906419164192641936419464195641966419764198641996420064201642026420364204642056420664207642086420964210642116421264213642146421564216642176421864219642206422164222642236422464225642266422764228642296423064231642326423364234642356423664237642386423964240642416424264243642446424564246642476424864249642506425164252642536425464255642566425764258642596426064261642626426364264642656426664267642686426964270642716427264273642746427564276642776427864279642806428164282642836428464285642866428764288642896429064291642926429364294642956429664297642986429964300643016430264303643046430564306643076430864309643106431164312643136431464315643166431764318643196432064321643226432364324643256432664327643286432964330643316433264333643346433564336643376433864339643406434164342643436434464345643466434764348643496435064351643526435364354643556435664357643586435964360643616436264363643646436564366643676436864369643706437164372643736437464375643766437764378643796438064381643826438364384643856438664387643886438964390643916439264393643946439564396643976439864399644006440164402644036440464405644066440764408644096441064411644126441364414644156441664417644186441964420644216442264423644246442564426644276442864429644306443164432644336443464435644366443764438644396444064441644426444364444644456444664447644486444964450644516445264453644546445564456644576445864459644606446164462644636446464465644666446764468644696447064471644726447364474644756447664477644786447964480644816448264483644846448564486644876448864489644906449164492644936449464495644966449764498644996450064501645026450364504645056450664507645086450964510645116451264513645146451564516645176451864519645206452164522645236452464525645266452764528645296453064531645326453364534645356453664537645386453964540645416454264543645446454564546645476454864549645506455164552645536455464555645566455764558645596456064561645626456364564645656456664567645686456964570645716457264573645746457564576645776457864579645806458164582645836458464585645866458764588645896459064591645926459364594645956459664597645986459964600646016460264603646046460564606646076460864609646106461164612646136461464615646166461764618646196462064621646226462364624646256462664627646286462964630646316463264633646346463564636646376463864639646406464164642646436464464645646466464764648646496465064651646526465364654646556465664657646586465964660646616466264663646646466564666646676466864669646706467164672646736467464675646766467764678646796468064681646826468364684646856468664687646886468964690646916469264693646946469564696646976469864699647006470164702647036470464705647066470764708647096471064711647126471364714647156471664717647186471964720647216472264723647246472564726647276472864729647306473164732647336473464735647366473764738647396474064741647426474364744647456474664747647486474964750647516475264753647546475564756647576475864759647606476164762647636476464765647666476764768647696477064771647726477364774647756477664777647786477964780647816478264783647846478564786647876478864789647906479164792647936479464795647966479764798647996480064801648026480364804648056480664807648086480964810648116481264813648146481564816648176481864819648206482164822648236482464825648266482764828648296483064831648326483364834648356483664837648386483964840648416484264843648446484564846648476484864849648506485164852648536485464855648566485764858648596486064861648626486364864648656486664867648686486964870648716487264873648746487564876648776487864879648806488164882648836488464885648866488764888648896489064891648926489364894648956489664897648986489964900649016490264903649046490564906649076490864909649106491164912649136491464915649166491764918649196492064921649226492364924649256492664927649286492964930649316493264933649346493564936649376493864939649406494164942649436494464945649466494764948649496495064951649526495364954649556495664957649586495964960649616496264963649646496564966649676496864969649706497164972649736497464975649766497764978649796498064981649826498364984649856498664987649886498964990649916499264993649946499564996649976499864999650006500165002650036500465005650066500765008650096501065011650126501365014650156501665017650186501965020650216502265023650246502565026650276502865029650306503165032650336503465035650366503765038650396504065041650426504365044650456504665047650486504965050650516505265053650546505565056650576505865059650606506165062650636506465065650666506765068650696507065071650726507365074650756507665077650786507965080650816508265083650846508565086650876508865089650906509165092650936509465095650966509765098650996510065101651026510365104651056510665107651086510965110651116511265113651146511565116651176511865119651206512165122651236512465125651266512765128651296513065131651326513365134651356513665137651386513965140651416514265143651446514565146651476514865149651506515165152651536515465155651566515765158651596516065161651626516365164651656516665167651686516965170651716517265173651746517565176651776517865179651806518165182651836518465185651866518765188651896519065191651926519365194651956519665197651986519965200652016520265203652046520565206652076520865209652106521165212652136521465215652166521765218652196522065221652226522365224652256522665227652286522965230652316523265233652346523565236652376523865239652406524165242652436524465245652466524765248652496525065251652526525365254652556525665257652586525965260652616526265263652646526565266652676526865269652706527165272652736527465275652766527765278652796528065281652826528365284652856528665287652886528965290652916529265293652946529565296652976529865299653006530165302653036530465305653066530765308653096531065311653126531365314653156531665317653186531965320653216532265323653246532565326653276532865329653306533165332653336533465335653366533765338653396534065341653426534365344653456534665347653486534965350653516535265353653546535565356653576535865359653606536165362653636536465365653666536765368653696537065371653726537365374653756537665377653786537965380653816538265383653846538565386653876538865389653906539165392653936539465395653966539765398653996540065401654026540365404654056540665407654086540965410654116541265413654146541565416654176541865419654206542165422654236542465425654266542765428654296543065431654326543365434654356543665437654386543965440654416544265443654446544565446654476544865449654506545165452654536545465455654566545765458654596546065461654626546365464654656546665467654686546965470654716547265473654746547565476654776547865479654806548165482654836548465485654866548765488654896549065491654926549365494654956549665497654986549965500655016550265503655046550565506655076550865509655106551165512655136551465515655166551765518655196552065521655226552365524655256552665527655286552965530655316553265533655346553565536655376553865539655406554165542655436554465545655466554765548655496555065551655526555365554655556555665557655586555965560655616556265563655646556565566655676556865569655706557165572655736557465575655766557765578655796558065581655826558365584655856558665587655886558965590655916559265593655946559565596655976559865599656006560165602656036560465605656066560765608656096561065611656126561365614656156561665617656186561965620656216562265623656246562565626656276562865629656306563165632656336563465635656366563765638656396564065641656426564365644656456564665647656486564965650656516565265653656546565565656656576565865659656606566165662656636566465665656666566765668656696567065671656726567365674656756567665677656786567965680656816568265683656846568565686656876568865689656906569165692656936569465695656966569765698656996570065701657026570365704657056570665707657086570965710657116571265713657146571565716657176571865719657206572165722657236572465725657266572765728657296573065731657326573365734657356573665737657386573965740657416574265743657446574565746657476574865749657506575165752657536575465755657566575765758657596576065761657626576365764657656576665767657686576965770657716577265773657746577565776657776577865779657806578165782657836578465785657866578765788657896579065791657926579365794657956579665797657986579965800658016580265803658046580565806658076580865809658106581165812658136581465815658166581765818658196582065821658226582365824658256582665827658286582965830658316583265833658346583565836658376583865839658406584165842658436584465845658466584765848658496585065851658526585365854658556585665857658586585965860658616586265863658646586565866658676586865869658706587165872658736587465875658766587765878658796588065881658826588365884658856588665887658886588965890658916589265893658946589565896658976589865899659006590165902659036590465905659066590765908659096591065911659126591365914659156591665917659186591965920659216592265923659246592565926659276592865929659306593165932659336593465935659366593765938659396594065941659426594365944659456594665947659486594965950659516595265953659546595565956659576595865959659606596165962659636596465965659666596765968659696597065971659726597365974659756597665977659786597965980659816598265983659846598565986659876598865989659906599165992659936599465995659966599765998659996600066001660026600366004660056600666007660086600966010660116601266013660146601566016660176601866019660206602166022660236602466025660266602766028660296603066031660326603366034660356603666037660386603966040660416604266043660446604566046660476604866049660506605166052660536605466055660566605766058660596606066061660626606366064660656606666067660686606966070660716607266073660746607566076660776607866079660806608166082660836608466085660866608766088660896609066091660926609366094660956609666097660986609966100661016610266103661046610566106661076610866109661106611166112661136611466115661166611766118661196612066121661226612366124661256612666127661286612966130661316613266133661346613566136661376613866139661406614166142661436614466145661466614766148661496615066151661526615366154661556615666157661586615966160661616616266163661646616566166661676616866169661706617166172661736617466175661766617766178661796618066181661826618366184661856618666187661886618966190661916619266193661946619566196661976619866199662006620166202662036620466205662066620766208662096621066211662126621366214662156621666217662186621966220662216622266223662246622566226662276622866229662306623166232662336623466235662366623766238662396624066241662426624366244662456624666247662486624966250662516625266253662546625566256662576625866259662606626166262662636626466265662666626766268662696627066271662726627366274662756627666277662786627966280662816628266283662846628566286662876628866289662906629166292662936629466295662966629766298662996630066301663026630366304663056630666307663086630966310663116631266313663146631566316663176631866319663206632166322663236632466325663266632766328663296633066331663326633366334663356633666337663386633966340663416634266343663446634566346663476634866349663506635166352663536635466355663566635766358663596636066361663626636366364663656636666367663686636966370663716637266373663746637566376663776637866379663806638166382663836638466385663866638766388663896639066391663926639366394663956639666397663986639966400664016640266403664046640566406664076640866409664106641166412664136641466415664166641766418664196642066421664226642366424664256642666427664286642966430664316643266433664346643566436664376643866439664406644166442664436644466445664466644766448664496645066451664526645366454664556645666457664586645966460664616646266463664646646566466664676646866469664706647166472664736647466475664766647766478664796648066481664826648366484664856648666487664886648966490664916649266493664946649566496664976649866499665006650166502665036650466505665066650766508665096651066511665126651366514665156651666517665186651966520665216652266523665246652566526665276652866529665306653166532665336653466535665366653766538665396654066541665426654366544665456654666547665486654966550665516655266553665546655566556665576655866559665606656166562665636656466565665666656766568665696657066571665726657366574665756657666577665786657966580665816658266583665846658566586665876658866589665906659166592665936659466595665966659766598665996660066601666026660366604666056660666607666086660966610666116661266613666146661566616666176661866619666206662166622666236662466625666266662766628666296663066631666326663366634666356663666637666386663966640666416664266643666446664566646666476664866649666506665166652666536665466655666566665766658666596666066661666626666366664666656666666667666686666966670666716667266673666746667566676666776667866679666806668166682666836668466685666866668766688666896669066691666926669366694666956669666697666986669966700667016670266703667046670566706667076670866709667106671166712667136671466715667166671766718667196672066721667226672366724667256672666727667286672966730667316673266733667346673566736667376673866739667406674166742667436674466745667466674766748667496675066751667526675366754667556675666757667586675966760667616676266763667646676566766667676676866769667706677166772667736677466775667766677766778667796678066781667826678366784667856678666787667886678966790667916679266793667946679566796667976679866799668006680166802668036680466805668066680766808668096681066811668126681366814668156681666817668186681966820668216682266823668246682566826668276682866829668306683166832668336683466835668366683766838668396684066841668426684366844668456684666847
  1. /*
  2. * MbedTLS Source Code Library Source
  3. */
  4. #include "mbedtls.h"
  5. #if ME_COM_MBEDTLS
  6. /********* Start of file library/aes.c ************/
  7. /*
  8. * FIPS-197 compliant AES implementation
  9. *
  10. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  11. * SPDX-License-Identifier: Apache-2.0
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * This file is part of mbed TLS (https://tls.mbed.org)
  26. */
  27. /*
  28. * The AES block cipher was designed by Vincent Rijmen and Joan Daemen.
  29. *
  30. * http://csrc.nist.gov/encryption/aes/rijndael/Rijndael.pdf
  31. * http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
  32. */
  33. #if !defined(MBEDTLS_CONFIG_FILE)
  34. #else
  35. #endif
  36. #if defined(MBEDTLS_AES_C)
  37. #include <string.h>
  38. #if defined(MBEDTLS_PADLOCK_C)
  39. #endif
  40. #if defined(MBEDTLS_AESNI_C)
  41. #endif
  42. #if defined(MBEDTLS_SELF_TEST)
  43. #if defined(MBEDTLS_PLATFORM_C)
  44. #else
  45. #include <stdio.h>
  46. #define mbedtls_printf printf
  47. #endif /* MBEDTLS_PLATFORM_C */
  48. #endif /* MBEDTLS_SELF_TEST */
  49. #if !defined(MBEDTLS_AES_ALT)
  50. /* Implementation that should never be optimized out by the compiler */
  51. static void mbedtls_zeroize( void *v, size_t n ) {
  52. volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
  53. }
  54. /*
  55. * 32-bit integer manipulation macros (little endian)
  56. */
  57. #ifndef GET_UINT32_LE
  58. #define GET_UINT32_LE(n,b,i) \
  59. { \
  60. (n) = ( (uint32_t) (b)[(i) ] ) \
  61. | ( (uint32_t) (b)[(i) + 1] << 8 ) \
  62. | ( (uint32_t) (b)[(i) + 2] << 16 ) \
  63. | ( (uint32_t) (b)[(i) + 3] << 24 ); \
  64. }
  65. #endif
  66. #ifndef PUT_UINT32_LE
  67. #define PUT_UINT32_LE(n,b,i) \
  68. { \
  69. (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
  70. (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
  71. (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
  72. (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
  73. }
  74. #endif
  75. #if defined(MBEDTLS_PADLOCK_C) && \
  76. ( defined(MBEDTLS_HAVE_X86) || defined(MBEDTLS_PADLOCK_ALIGN16) )
  77. static int aes_padlock_ace = -1;
  78. #endif
  79. #if defined(MBEDTLS_AES_ROM_TABLES)
  80. /*
  81. * Forward S-box
  82. */
  83. static const unsigned char FSb[256] =
  84. {
  85. 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5,
  86. 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
  87. 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0,
  88. 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
  89. 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC,
  90. 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
  91. 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A,
  92. 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
  93. 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0,
  94. 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
  95. 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B,
  96. 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
  97. 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85,
  98. 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
  99. 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5,
  100. 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
  101. 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17,
  102. 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
  103. 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88,
  104. 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
  105. 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C,
  106. 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,
  107. 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9,
  108. 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
  109. 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6,
  110. 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
  111. 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E,
  112. 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
  113. 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94,
  114. 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
  115. 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68,
  116. 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16
  117. };
  118. /*
  119. * Forward tables
  120. */
  121. #define FT \
  122. \
  123. V(A5,63,63,C6), V(84,7C,7C,F8), V(99,77,77,EE), V(8D,7B,7B,F6), \
  124. V(0D,F2,F2,FF), V(BD,6B,6B,D6), V(B1,6F,6F,DE), V(54,C5,C5,91), \
  125. V(50,30,30,60), V(03,01,01,02), V(A9,67,67,CE), V(7D,2B,2B,56), \
  126. V(19,FE,FE,E7), V(62,D7,D7,B5), V(E6,AB,AB,4D), V(9A,76,76,EC), \
  127. V(45,CA,CA,8F), V(9D,82,82,1F), V(40,C9,C9,89), V(87,7D,7D,FA), \
  128. V(15,FA,FA,EF), V(EB,59,59,B2), V(C9,47,47,8E), V(0B,F0,F0,FB), \
  129. V(EC,AD,AD,41), V(67,D4,D4,B3), V(FD,A2,A2,5F), V(EA,AF,AF,45), \
  130. V(BF,9C,9C,23), V(F7,A4,A4,53), V(96,72,72,E4), V(5B,C0,C0,9B), \
  131. V(C2,B7,B7,75), V(1C,FD,FD,E1), V(AE,93,93,3D), V(6A,26,26,4C), \
  132. V(5A,36,36,6C), V(41,3F,3F,7E), V(02,F7,F7,F5), V(4F,CC,CC,83), \
  133. V(5C,34,34,68), V(F4,A5,A5,51), V(34,E5,E5,D1), V(08,F1,F1,F9), \
  134. V(93,71,71,E2), V(73,D8,D8,AB), V(53,31,31,62), V(3F,15,15,2A), \
  135. V(0C,04,04,08), V(52,C7,C7,95), V(65,23,23,46), V(5E,C3,C3,9D), \
  136. V(28,18,18,30), V(A1,96,96,37), V(0F,05,05,0A), V(B5,9A,9A,2F), \
  137. V(09,07,07,0E), V(36,12,12,24), V(9B,80,80,1B), V(3D,E2,E2,DF), \
  138. V(26,EB,EB,CD), V(69,27,27,4E), V(CD,B2,B2,7F), V(9F,75,75,EA), \
  139. V(1B,09,09,12), V(9E,83,83,1D), V(74,2C,2C,58), V(2E,1A,1A,34), \
  140. V(2D,1B,1B,36), V(B2,6E,6E,DC), V(EE,5A,5A,B4), V(FB,A0,A0,5B), \
  141. V(F6,52,52,A4), V(4D,3B,3B,76), V(61,D6,D6,B7), V(CE,B3,B3,7D), \
  142. V(7B,29,29,52), V(3E,E3,E3,DD), V(71,2F,2F,5E), V(97,84,84,13), \
  143. V(F5,53,53,A6), V(68,D1,D1,B9), V(00,00,00,00), V(2C,ED,ED,C1), \
  144. V(60,20,20,40), V(1F,FC,FC,E3), V(C8,B1,B1,79), V(ED,5B,5B,B6), \
  145. V(BE,6A,6A,D4), V(46,CB,CB,8D), V(D9,BE,BE,67), V(4B,39,39,72), \
  146. V(DE,4A,4A,94), V(D4,4C,4C,98), V(E8,58,58,B0), V(4A,CF,CF,85), \
  147. V(6B,D0,D0,BB), V(2A,EF,EF,C5), V(E5,AA,AA,4F), V(16,FB,FB,ED), \
  148. V(C5,43,43,86), V(D7,4D,4D,9A), V(55,33,33,66), V(94,85,85,11), \
  149. V(CF,45,45,8A), V(10,F9,F9,E9), V(06,02,02,04), V(81,7F,7F,FE), \
  150. V(F0,50,50,A0), V(44,3C,3C,78), V(BA,9F,9F,25), V(E3,A8,A8,4B), \
  151. V(F3,51,51,A2), V(FE,A3,A3,5D), V(C0,40,40,80), V(8A,8F,8F,05), \
  152. V(AD,92,92,3F), V(BC,9D,9D,21), V(48,38,38,70), V(04,F5,F5,F1), \
  153. V(DF,BC,BC,63), V(C1,B6,B6,77), V(75,DA,DA,AF), V(63,21,21,42), \
  154. V(30,10,10,20), V(1A,FF,FF,E5), V(0E,F3,F3,FD), V(6D,D2,D2,BF), \
  155. V(4C,CD,CD,81), V(14,0C,0C,18), V(35,13,13,26), V(2F,EC,EC,C3), \
  156. V(E1,5F,5F,BE), V(A2,97,97,35), V(CC,44,44,88), V(39,17,17,2E), \
  157. V(57,C4,C4,93), V(F2,A7,A7,55), V(82,7E,7E,FC), V(47,3D,3D,7A), \
  158. V(AC,64,64,C8), V(E7,5D,5D,BA), V(2B,19,19,32), V(95,73,73,E6), \
  159. V(A0,60,60,C0), V(98,81,81,19), V(D1,4F,4F,9E), V(7F,DC,DC,A3), \
  160. V(66,22,22,44), V(7E,2A,2A,54), V(AB,90,90,3B), V(83,88,88,0B), \
  161. V(CA,46,46,8C), V(29,EE,EE,C7), V(D3,B8,B8,6B), V(3C,14,14,28), \
  162. V(79,DE,DE,A7), V(E2,5E,5E,BC), V(1D,0B,0B,16), V(76,DB,DB,AD), \
  163. V(3B,E0,E0,DB), V(56,32,32,64), V(4E,3A,3A,74), V(1E,0A,0A,14), \
  164. V(DB,49,49,92), V(0A,06,06,0C), V(6C,24,24,48), V(E4,5C,5C,B8), \
  165. V(5D,C2,C2,9F), V(6E,D3,D3,BD), V(EF,AC,AC,43), V(A6,62,62,C4), \
  166. V(A8,91,91,39), V(A4,95,95,31), V(37,E4,E4,D3), V(8B,79,79,F2), \
  167. V(32,E7,E7,D5), V(43,C8,C8,8B), V(59,37,37,6E), V(B7,6D,6D,DA), \
  168. V(8C,8D,8D,01), V(64,D5,D5,B1), V(D2,4E,4E,9C), V(E0,A9,A9,49), \
  169. V(B4,6C,6C,D8), V(FA,56,56,AC), V(07,F4,F4,F3), V(25,EA,EA,CF), \
  170. V(AF,65,65,CA), V(8E,7A,7A,F4), V(E9,AE,AE,47), V(18,08,08,10), \
  171. V(D5,BA,BA,6F), V(88,78,78,F0), V(6F,25,25,4A), V(72,2E,2E,5C), \
  172. V(24,1C,1C,38), V(F1,A6,A6,57), V(C7,B4,B4,73), V(51,C6,C6,97), \
  173. V(23,E8,E8,CB), V(7C,DD,DD,A1), V(9C,74,74,E8), V(21,1F,1F,3E), \
  174. V(DD,4B,4B,96), V(DC,BD,BD,61), V(86,8B,8B,0D), V(85,8A,8A,0F), \
  175. V(90,70,70,E0), V(42,3E,3E,7C), V(C4,B5,B5,71), V(AA,66,66,CC), \
  176. V(D8,48,48,90), V(05,03,03,06), V(01,F6,F6,F7), V(12,0E,0E,1C), \
  177. V(A3,61,61,C2), V(5F,35,35,6A), V(F9,57,57,AE), V(D0,B9,B9,69), \
  178. V(91,86,86,17), V(58,C1,C1,99), V(27,1D,1D,3A), V(B9,9E,9E,27), \
  179. V(38,E1,E1,D9), V(13,F8,F8,EB), V(B3,98,98,2B), V(33,11,11,22), \
  180. V(BB,69,69,D2), V(70,D9,D9,A9), V(89,8E,8E,07), V(A7,94,94,33), \
  181. V(B6,9B,9B,2D), V(22,1E,1E,3C), V(92,87,87,15), V(20,E9,E9,C9), \
  182. V(49,CE,CE,87), V(FF,55,55,AA), V(78,28,28,50), V(7A,DF,DF,A5), \
  183. V(8F,8C,8C,03), V(F8,A1,A1,59), V(80,89,89,09), V(17,0D,0D,1A), \
  184. V(DA,BF,BF,65), V(31,E6,E6,D7), V(C6,42,42,84), V(B8,68,68,D0), \
  185. V(C3,41,41,82), V(B0,99,99,29), V(77,2D,2D,5A), V(11,0F,0F,1E), \
  186. V(CB,B0,B0,7B), V(FC,54,54,A8), V(D6,BB,BB,6D), V(3A,16,16,2C)
  187. #define V(a,b,c,d) 0x##a##b##c##d
  188. static const uint32_t FT0[256] = { FT };
  189. #undef V
  190. #define V(a,b,c,d) 0x##b##c##d##a
  191. static const uint32_t FT1[256] = { FT };
  192. #undef V
  193. #define V(a,b,c,d) 0x##c##d##a##b
  194. static const uint32_t FT2[256] = { FT };
  195. #undef V
  196. #define V(a,b,c,d) 0x##d##a##b##c
  197. static const uint32_t FT3[256] = { FT };
  198. #undef V
  199. #undef FT
  200. /*
  201. * Reverse S-box
  202. */
  203. static const unsigned char RSb[256] =
  204. {
  205. 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38,
  206. 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB,
  207. 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87,
  208. 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB,
  209. 0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D,
  210. 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E,
  211. 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2,
  212. 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25,
  213. 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16,
  214. 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92,
  215. 0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA,
  216. 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84,
  217. 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A,
  218. 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06,
  219. 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02,
  220. 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B,
  221. 0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA,
  222. 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73,
  223. 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85,
  224. 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E,
  225. 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89,
  226. 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B,
  227. 0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20,
  228. 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4,
  229. 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31,
  230. 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F,
  231. 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D,
  232. 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF,
  233. 0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0,
  234. 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
  235. 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26,
  236. 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D
  237. };
  238. /*
  239. * Reverse tables
  240. */
  241. #define RT \
  242. \
  243. V(50,A7,F4,51), V(53,65,41,7E), V(C3,A4,17,1A), V(96,5E,27,3A), \
  244. V(CB,6B,AB,3B), V(F1,45,9D,1F), V(AB,58,FA,AC), V(93,03,E3,4B), \
  245. V(55,FA,30,20), V(F6,6D,76,AD), V(91,76,CC,88), V(25,4C,02,F5), \
  246. V(FC,D7,E5,4F), V(D7,CB,2A,C5), V(80,44,35,26), V(8F,A3,62,B5), \
  247. V(49,5A,B1,DE), V(67,1B,BA,25), V(98,0E,EA,45), V(E1,C0,FE,5D), \
  248. V(02,75,2F,C3), V(12,F0,4C,81), V(A3,97,46,8D), V(C6,F9,D3,6B), \
  249. V(E7,5F,8F,03), V(95,9C,92,15), V(EB,7A,6D,BF), V(DA,59,52,95), \
  250. V(2D,83,BE,D4), V(D3,21,74,58), V(29,69,E0,49), V(44,C8,C9,8E), \
  251. V(6A,89,C2,75), V(78,79,8E,F4), V(6B,3E,58,99), V(DD,71,B9,27), \
  252. V(B6,4F,E1,BE), V(17,AD,88,F0), V(66,AC,20,C9), V(B4,3A,CE,7D), \
  253. V(18,4A,DF,63), V(82,31,1A,E5), V(60,33,51,97), V(45,7F,53,62), \
  254. V(E0,77,64,B1), V(84,AE,6B,BB), V(1C,A0,81,FE), V(94,2B,08,F9), \
  255. V(58,68,48,70), V(19,FD,45,8F), V(87,6C,DE,94), V(B7,F8,7B,52), \
  256. V(23,D3,73,AB), V(E2,02,4B,72), V(57,8F,1F,E3), V(2A,AB,55,66), \
  257. V(07,28,EB,B2), V(03,C2,B5,2F), V(9A,7B,C5,86), V(A5,08,37,D3), \
  258. V(F2,87,28,30), V(B2,A5,BF,23), V(BA,6A,03,02), V(5C,82,16,ED), \
  259. V(2B,1C,CF,8A), V(92,B4,79,A7), V(F0,F2,07,F3), V(A1,E2,69,4E), \
  260. V(CD,F4,DA,65), V(D5,BE,05,06), V(1F,62,34,D1), V(8A,FE,A6,C4), \
  261. V(9D,53,2E,34), V(A0,55,F3,A2), V(32,E1,8A,05), V(75,EB,F6,A4), \
  262. V(39,EC,83,0B), V(AA,EF,60,40), V(06,9F,71,5E), V(51,10,6E,BD), \
  263. V(F9,8A,21,3E), V(3D,06,DD,96), V(AE,05,3E,DD), V(46,BD,E6,4D), \
  264. V(B5,8D,54,91), V(05,5D,C4,71), V(6F,D4,06,04), V(FF,15,50,60), \
  265. V(24,FB,98,19), V(97,E9,BD,D6), V(CC,43,40,89), V(77,9E,D9,67), \
  266. V(BD,42,E8,B0), V(88,8B,89,07), V(38,5B,19,E7), V(DB,EE,C8,79), \
  267. V(47,0A,7C,A1), V(E9,0F,42,7C), V(C9,1E,84,F8), V(00,00,00,00), \
  268. V(83,86,80,09), V(48,ED,2B,32), V(AC,70,11,1E), V(4E,72,5A,6C), \
  269. V(FB,FF,0E,FD), V(56,38,85,0F), V(1E,D5,AE,3D), V(27,39,2D,36), \
  270. V(64,D9,0F,0A), V(21,A6,5C,68), V(D1,54,5B,9B), V(3A,2E,36,24), \
  271. V(B1,67,0A,0C), V(0F,E7,57,93), V(D2,96,EE,B4), V(9E,91,9B,1B), \
  272. V(4F,C5,C0,80), V(A2,20,DC,61), V(69,4B,77,5A), V(16,1A,12,1C), \
  273. V(0A,BA,93,E2), V(E5,2A,A0,C0), V(43,E0,22,3C), V(1D,17,1B,12), \
  274. V(0B,0D,09,0E), V(AD,C7,8B,F2), V(B9,A8,B6,2D), V(C8,A9,1E,14), \
  275. V(85,19,F1,57), V(4C,07,75,AF), V(BB,DD,99,EE), V(FD,60,7F,A3), \
  276. V(9F,26,01,F7), V(BC,F5,72,5C), V(C5,3B,66,44), V(34,7E,FB,5B), \
  277. V(76,29,43,8B), V(DC,C6,23,CB), V(68,FC,ED,B6), V(63,F1,E4,B8), \
  278. V(CA,DC,31,D7), V(10,85,63,42), V(40,22,97,13), V(20,11,C6,84), \
  279. V(7D,24,4A,85), V(F8,3D,BB,D2), V(11,32,F9,AE), V(6D,A1,29,C7), \
  280. V(4B,2F,9E,1D), V(F3,30,B2,DC), V(EC,52,86,0D), V(D0,E3,C1,77), \
  281. V(6C,16,B3,2B), V(99,B9,70,A9), V(FA,48,94,11), V(22,64,E9,47), \
  282. V(C4,8C,FC,A8), V(1A,3F,F0,A0), V(D8,2C,7D,56), V(EF,90,33,22), \
  283. V(C7,4E,49,87), V(C1,D1,38,D9), V(FE,A2,CA,8C), V(36,0B,D4,98), \
  284. V(CF,81,F5,A6), V(28,DE,7A,A5), V(26,8E,B7,DA), V(A4,BF,AD,3F), \
  285. V(E4,9D,3A,2C), V(0D,92,78,50), V(9B,CC,5F,6A), V(62,46,7E,54), \
  286. V(C2,13,8D,F6), V(E8,B8,D8,90), V(5E,F7,39,2E), V(F5,AF,C3,82), \
  287. V(BE,80,5D,9F), V(7C,93,D0,69), V(A9,2D,D5,6F), V(B3,12,25,CF), \
  288. V(3B,99,AC,C8), V(A7,7D,18,10), V(6E,63,9C,E8), V(7B,BB,3B,DB), \
  289. V(09,78,26,CD), V(F4,18,59,6E), V(01,B7,9A,EC), V(A8,9A,4F,83), \
  290. V(65,6E,95,E6), V(7E,E6,FF,AA), V(08,CF,BC,21), V(E6,E8,15,EF), \
  291. V(D9,9B,E7,BA), V(CE,36,6F,4A), V(D4,09,9F,EA), V(D6,7C,B0,29), \
  292. V(AF,B2,A4,31), V(31,23,3F,2A), V(30,94,A5,C6), V(C0,66,A2,35), \
  293. V(37,BC,4E,74), V(A6,CA,82,FC), V(B0,D0,90,E0), V(15,D8,A7,33), \
  294. V(4A,98,04,F1), V(F7,DA,EC,41), V(0E,50,CD,7F), V(2F,F6,91,17), \
  295. V(8D,D6,4D,76), V(4D,B0,EF,43), V(54,4D,AA,CC), V(DF,04,96,E4), \
  296. V(E3,B5,D1,9E), V(1B,88,6A,4C), V(B8,1F,2C,C1), V(7F,51,65,46), \
  297. V(04,EA,5E,9D), V(5D,35,8C,01), V(73,74,87,FA), V(2E,41,0B,FB), \
  298. V(5A,1D,67,B3), V(52,D2,DB,92), V(33,56,10,E9), V(13,47,D6,6D), \
  299. V(8C,61,D7,9A), V(7A,0C,A1,37), V(8E,14,F8,59), V(89,3C,13,EB), \
  300. V(EE,27,A9,CE), V(35,C9,61,B7), V(ED,E5,1C,E1), V(3C,B1,47,7A), \
  301. V(59,DF,D2,9C), V(3F,73,F2,55), V(79,CE,14,18), V(BF,37,C7,73), \
  302. V(EA,CD,F7,53), V(5B,AA,FD,5F), V(14,6F,3D,DF), V(86,DB,44,78), \
  303. V(81,F3,AF,CA), V(3E,C4,68,B9), V(2C,34,24,38), V(5F,40,A3,C2), \
  304. V(72,C3,1D,16), V(0C,25,E2,BC), V(8B,49,3C,28), V(41,95,0D,FF), \
  305. V(71,01,A8,39), V(DE,B3,0C,08), V(9C,E4,B4,D8), V(90,C1,56,64), \
  306. V(61,84,CB,7B), V(70,B6,32,D5), V(74,5C,6C,48), V(42,57,B8,D0)
  307. #define V(a,b,c,d) 0x##a##b##c##d
  308. static const uint32_t RT0[256] = { RT };
  309. #undef V
  310. #define V(a,b,c,d) 0x##b##c##d##a
  311. static const uint32_t RT1[256] = { RT };
  312. #undef V
  313. #define V(a,b,c,d) 0x##c##d##a##b
  314. static const uint32_t RT2[256] = { RT };
  315. #undef V
  316. #define V(a,b,c,d) 0x##d##a##b##c
  317. static const uint32_t RT3[256] = { RT };
  318. #undef V
  319. #undef RT
  320. /*
  321. * Round constants
  322. */
  323. static const uint32_t RCON[10] =
  324. {
  325. 0x00000001, 0x00000002, 0x00000004, 0x00000008,
  326. 0x00000010, 0x00000020, 0x00000040, 0x00000080,
  327. 0x0000001B, 0x00000036
  328. };
  329. #else /* MBEDTLS_AES_ROM_TABLES */
  330. /*
  331. * Forward S-box & tables
  332. */
  333. static unsigned char FSb[256];
  334. static uint32_t FT0[256];
  335. static uint32_t FT1[256];
  336. static uint32_t FT2[256];
  337. static uint32_t FT3[256];
  338. /*
  339. * Reverse S-box & tables
  340. */
  341. static unsigned char RSb[256];
  342. static uint32_t RT0[256];
  343. static uint32_t RT1[256];
  344. static uint32_t RT2[256];
  345. static uint32_t RT3[256];
  346. /*
  347. * Round constants
  348. */
  349. static uint32_t RCON[10];
  350. /*
  351. * Tables generation code
  352. */
  353. #define ROTL8(x) ( ( x << 8 ) & 0xFFFFFFFF ) | ( x >> 24 )
  354. #define XTIME(x) ( ( x << 1 ) ^ ( ( x & 0x80 ) ? 0x1B : 0x00 ) )
  355. #define MUL(x,y) ( ( x && y ) ? pow[(log[x]+log[y]) % 255] : 0 )
  356. static int aes_init_done = 0;
  357. static void aes_gen_tables( void )
  358. {
  359. int i, x, y, z;
  360. int pow[256];
  361. int log[256];
  362. /*
  363. * compute pow and log tables over GF(2^8)
  364. */
  365. for( i = 0, x = 1; i < 256; i++ )
  366. {
  367. pow[i] = x;
  368. log[x] = i;
  369. x = ( x ^ XTIME( x ) ) & 0xFF;
  370. }
  371. /*
  372. * calculate the round constants
  373. */
  374. for( i = 0, x = 1; i < 10; i++ )
  375. {
  376. RCON[i] = (uint32_t) x;
  377. x = XTIME( x ) & 0xFF;
  378. }
  379. /*
  380. * generate the forward and reverse S-boxes
  381. */
  382. FSb[0x00] = 0x63;
  383. RSb[0x63] = 0x00;
  384. for( i = 1; i < 256; i++ )
  385. {
  386. x = pow[255 - log[i]];
  387. y = x; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF;
  388. x ^= y; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF;
  389. x ^= y; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF;
  390. x ^= y; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF;
  391. x ^= y ^ 0x63;
  392. FSb[i] = (unsigned char) x;
  393. RSb[x] = (unsigned char) i;
  394. }
  395. /*
  396. * generate the forward and reverse tables
  397. */
  398. for( i = 0; i < 256; i++ )
  399. {
  400. x = FSb[i];
  401. y = XTIME( x ) & 0xFF;
  402. z = ( y ^ x ) & 0xFF;
  403. FT0[i] = ( (uint32_t) y ) ^
  404. ( (uint32_t) x << 8 ) ^
  405. ( (uint32_t) x << 16 ) ^
  406. ( (uint32_t) z << 24 );
  407. FT1[i] = ROTL8( FT0[i] );
  408. FT2[i] = ROTL8( FT1[i] );
  409. FT3[i] = ROTL8( FT2[i] );
  410. x = RSb[i];
  411. RT0[i] = ( (uint32_t) MUL( 0x0E, x ) ) ^
  412. ( (uint32_t) MUL( 0x09, x ) << 8 ) ^
  413. ( (uint32_t) MUL( 0x0D, x ) << 16 ) ^
  414. ( (uint32_t) MUL( 0x0B, x ) << 24 );
  415. RT1[i] = ROTL8( RT0[i] );
  416. RT2[i] = ROTL8( RT1[i] );
  417. RT3[i] = ROTL8( RT2[i] );
  418. }
  419. }
  420. #endif /* MBEDTLS_AES_ROM_TABLES */
  421. void mbedtls_aes_init( mbedtls_aes_context *ctx )
  422. {
  423. memset( ctx, 0, sizeof( mbedtls_aes_context ) );
  424. }
  425. void mbedtls_aes_free( mbedtls_aes_context *ctx )
  426. {
  427. if( ctx == NULL )
  428. return;
  429. mbedtls_zeroize( ctx, sizeof( mbedtls_aes_context ) );
  430. }
  431. /*
  432. * AES key schedule (encryption)
  433. */
  434. #if !defined(MBEDTLS_AES_SETKEY_ENC_ALT)
  435. int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
  436. unsigned int keybits )
  437. {
  438. unsigned int i;
  439. uint32_t *RK;
  440. #if !defined(MBEDTLS_AES_ROM_TABLES)
  441. if( aes_init_done == 0 )
  442. {
  443. aes_gen_tables();
  444. aes_init_done = 1;
  445. }
  446. #endif
  447. switch( keybits )
  448. {
  449. case 128: ctx->nr = 10; break;
  450. case 192: ctx->nr = 12; break;
  451. case 256: ctx->nr = 14; break;
  452. default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
  453. }
  454. #if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_PADLOCK_ALIGN16)
  455. if( aes_padlock_ace == -1 )
  456. aes_padlock_ace = mbedtls_padlock_has_support( MBEDTLS_PADLOCK_ACE );
  457. if( aes_padlock_ace )
  458. ctx->rk = RK = MBEDTLS_PADLOCK_ALIGN16( ctx->buf );
  459. else
  460. #endif
  461. ctx->rk = RK = ctx->buf;
  462. #if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64)
  463. if( mbedtls_aesni_has_support( MBEDTLS_AESNI_AES ) )
  464. return( mbedtls_aesni_setkey_enc( (unsigned char *) ctx->rk, key, keybits ) );
  465. #endif
  466. for( i = 0; i < ( keybits >> 5 ); i++ )
  467. {
  468. GET_UINT32_LE( RK[i], key, i << 2 );
  469. }
  470. switch( ctx->nr )
  471. {
  472. case 10:
  473. for( i = 0; i < 10; i++, RK += 4 )
  474. {
  475. RK[4] = RK[0] ^ RCON[i] ^
  476. ( (uint32_t) FSb[ ( RK[3] >> 8 ) & 0xFF ] ) ^
  477. ( (uint32_t) FSb[ ( RK[3] >> 16 ) & 0xFF ] << 8 ) ^
  478. ( (uint32_t) FSb[ ( RK[3] >> 24 ) & 0xFF ] << 16 ) ^
  479. ( (uint32_t) FSb[ ( RK[3] ) & 0xFF ] << 24 );
  480. RK[5] = RK[1] ^ RK[4];
  481. RK[6] = RK[2] ^ RK[5];
  482. RK[7] = RK[3] ^ RK[6];
  483. }
  484. break;
  485. case 12:
  486. for( i = 0; i < 8; i++, RK += 6 )
  487. {
  488. RK[6] = RK[0] ^ RCON[i] ^
  489. ( (uint32_t) FSb[ ( RK[5] >> 8 ) & 0xFF ] ) ^
  490. ( (uint32_t) FSb[ ( RK[5] >> 16 ) & 0xFF ] << 8 ) ^
  491. ( (uint32_t) FSb[ ( RK[5] >> 24 ) & 0xFF ] << 16 ) ^
  492. ( (uint32_t) FSb[ ( RK[5] ) & 0xFF ] << 24 );
  493. RK[7] = RK[1] ^ RK[6];
  494. RK[8] = RK[2] ^ RK[7];
  495. RK[9] = RK[3] ^ RK[8];
  496. RK[10] = RK[4] ^ RK[9];
  497. RK[11] = RK[5] ^ RK[10];
  498. }
  499. break;
  500. case 14:
  501. for( i = 0; i < 7; i++, RK += 8 )
  502. {
  503. RK[8] = RK[0] ^ RCON[i] ^
  504. ( (uint32_t) FSb[ ( RK[7] >> 8 ) & 0xFF ] ) ^
  505. ( (uint32_t) FSb[ ( RK[7] >> 16 ) & 0xFF ] << 8 ) ^
  506. ( (uint32_t) FSb[ ( RK[7] >> 24 ) & 0xFF ] << 16 ) ^
  507. ( (uint32_t) FSb[ ( RK[7] ) & 0xFF ] << 24 );
  508. RK[9] = RK[1] ^ RK[8];
  509. RK[10] = RK[2] ^ RK[9];
  510. RK[11] = RK[3] ^ RK[10];
  511. RK[12] = RK[4] ^
  512. ( (uint32_t) FSb[ ( RK[11] ) & 0xFF ] ) ^
  513. ( (uint32_t) FSb[ ( RK[11] >> 8 ) & 0xFF ] << 8 ) ^
  514. ( (uint32_t) FSb[ ( RK[11] >> 16 ) & 0xFF ] << 16 ) ^
  515. ( (uint32_t) FSb[ ( RK[11] >> 24 ) & 0xFF ] << 24 );
  516. RK[13] = RK[5] ^ RK[12];
  517. RK[14] = RK[6] ^ RK[13];
  518. RK[15] = RK[7] ^ RK[14];
  519. }
  520. break;
  521. }
  522. return( 0 );
  523. }
  524. #endif /* !MBEDTLS_AES_SETKEY_ENC_ALT */
  525. /*
  526. * AES key schedule (decryption)
  527. */
  528. #if !defined(MBEDTLS_AES_SETKEY_DEC_ALT)
  529. int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
  530. unsigned int keybits )
  531. {
  532. int i, j, ret;
  533. mbedtls_aes_context cty;
  534. uint32_t *RK;
  535. uint32_t *SK;
  536. mbedtls_aes_init( &cty );
  537. #if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_PADLOCK_ALIGN16)
  538. if( aes_padlock_ace == -1 )
  539. aes_padlock_ace = mbedtls_padlock_has_support( MBEDTLS_PADLOCK_ACE );
  540. if( aes_padlock_ace )
  541. ctx->rk = RK = MBEDTLS_PADLOCK_ALIGN16( ctx->buf );
  542. else
  543. #endif
  544. ctx->rk = RK = ctx->buf;
  545. /* Also checks keybits */
  546. if( ( ret = mbedtls_aes_setkey_enc( &cty, key, keybits ) ) != 0 )
  547. goto exit;
  548. ctx->nr = cty.nr;
  549. #if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64)
  550. if( mbedtls_aesni_has_support( MBEDTLS_AESNI_AES ) )
  551. {
  552. mbedtls_aesni_inverse_key( (unsigned char *) ctx->rk,
  553. (const unsigned char *) cty.rk, ctx->nr );
  554. goto exit;
  555. }
  556. #endif
  557. SK = cty.rk + cty.nr * 4;
  558. *RK++ = *SK++;
  559. *RK++ = *SK++;
  560. *RK++ = *SK++;
  561. *RK++ = *SK++;
  562. for( i = ctx->nr - 1, SK -= 8; i > 0; i--, SK -= 8 )
  563. {
  564. for( j = 0; j < 4; j++, SK++ )
  565. {
  566. *RK++ = RT0[ FSb[ ( *SK ) & 0xFF ] ] ^
  567. RT1[ FSb[ ( *SK >> 8 ) & 0xFF ] ] ^
  568. RT2[ FSb[ ( *SK >> 16 ) & 0xFF ] ] ^
  569. RT3[ FSb[ ( *SK >> 24 ) & 0xFF ] ];
  570. }
  571. }
  572. *RK++ = *SK++;
  573. *RK++ = *SK++;
  574. *RK++ = *SK++;
  575. *RK++ = *SK++;
  576. exit:
  577. mbedtls_aes_free( &cty );
  578. return( ret );
  579. }
  580. #endif /* !MBEDTLS_AES_SETKEY_DEC_ALT */
  581. #define AES_FROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \
  582. { \
  583. X0 = *RK++ ^ FT0[ ( Y0 ) & 0xFF ] ^ \
  584. FT1[ ( Y1 >> 8 ) & 0xFF ] ^ \
  585. FT2[ ( Y2 >> 16 ) & 0xFF ] ^ \
  586. FT3[ ( Y3 >> 24 ) & 0xFF ]; \
  587. \
  588. X1 = *RK++ ^ FT0[ ( Y1 ) & 0xFF ] ^ \
  589. FT1[ ( Y2 >> 8 ) & 0xFF ] ^ \
  590. FT2[ ( Y3 >> 16 ) & 0xFF ] ^ \
  591. FT3[ ( Y0 >> 24 ) & 0xFF ]; \
  592. \
  593. X2 = *RK++ ^ FT0[ ( Y2 ) & 0xFF ] ^ \
  594. FT1[ ( Y3 >> 8 ) & 0xFF ] ^ \
  595. FT2[ ( Y0 >> 16 ) & 0xFF ] ^ \
  596. FT3[ ( Y1 >> 24 ) & 0xFF ]; \
  597. \
  598. X3 = *RK++ ^ FT0[ ( Y3 ) & 0xFF ] ^ \
  599. FT1[ ( Y0 >> 8 ) & 0xFF ] ^ \
  600. FT2[ ( Y1 >> 16 ) & 0xFF ] ^ \
  601. FT3[ ( Y2 >> 24 ) & 0xFF ]; \
  602. }
  603. #define AES_RROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \
  604. { \
  605. X0 = *RK++ ^ RT0[ ( Y0 ) & 0xFF ] ^ \
  606. RT1[ ( Y3 >> 8 ) & 0xFF ] ^ \
  607. RT2[ ( Y2 >> 16 ) & 0xFF ] ^ \
  608. RT3[ ( Y1 >> 24 ) & 0xFF ]; \
  609. \
  610. X1 = *RK++ ^ RT0[ ( Y1 ) & 0xFF ] ^ \
  611. RT1[ ( Y0 >> 8 ) & 0xFF ] ^ \
  612. RT2[ ( Y3 >> 16 ) & 0xFF ] ^ \
  613. RT3[ ( Y2 >> 24 ) & 0xFF ]; \
  614. \
  615. X2 = *RK++ ^ RT0[ ( Y2 ) & 0xFF ] ^ \
  616. RT1[ ( Y1 >> 8 ) & 0xFF ] ^ \
  617. RT2[ ( Y0 >> 16 ) & 0xFF ] ^ \
  618. RT3[ ( Y3 >> 24 ) & 0xFF ]; \
  619. \
  620. X3 = *RK++ ^ RT0[ ( Y3 ) & 0xFF ] ^ \
  621. RT1[ ( Y2 >> 8 ) & 0xFF ] ^ \
  622. RT2[ ( Y1 >> 16 ) & 0xFF ] ^ \
  623. RT3[ ( Y0 >> 24 ) & 0xFF ]; \
  624. }
  625. /*
  626. * AES-ECB block encryption
  627. */
  628. #if !defined(MBEDTLS_AES_ENCRYPT_ALT)
  629. int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
  630. const unsigned char input[16],
  631. unsigned char output[16] )
  632. {
  633. int i;
  634. uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
  635. RK = ctx->rk;
  636. GET_UINT32_LE( X0, input, 0 ); X0 ^= *RK++;
  637. GET_UINT32_LE( X1, input, 4 ); X1 ^= *RK++;
  638. GET_UINT32_LE( X2, input, 8 ); X2 ^= *RK++;
  639. GET_UINT32_LE( X3, input, 12 ); X3 ^= *RK++;
  640. for( i = ( ctx->nr >> 1 ) - 1; i > 0; i-- )
  641. {
  642. AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
  643. AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 );
  644. }
  645. AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
  646. X0 = *RK++ ^ \
  647. ( (uint32_t) FSb[ ( Y0 ) & 0xFF ] ) ^
  648. ( (uint32_t) FSb[ ( Y1 >> 8 ) & 0xFF ] << 8 ) ^
  649. ( (uint32_t) FSb[ ( Y2 >> 16 ) & 0xFF ] << 16 ) ^
  650. ( (uint32_t) FSb[ ( Y3 >> 24 ) & 0xFF ] << 24 );
  651. X1 = *RK++ ^ \
  652. ( (uint32_t) FSb[ ( Y1 ) & 0xFF ] ) ^
  653. ( (uint32_t) FSb[ ( Y2 >> 8 ) & 0xFF ] << 8 ) ^
  654. ( (uint32_t) FSb[ ( Y3 >> 16 ) & 0xFF ] << 16 ) ^
  655. ( (uint32_t) FSb[ ( Y0 >> 24 ) & 0xFF ] << 24 );
  656. X2 = *RK++ ^ \
  657. ( (uint32_t) FSb[ ( Y2 ) & 0xFF ] ) ^
  658. ( (uint32_t) FSb[ ( Y3 >> 8 ) & 0xFF ] << 8 ) ^
  659. ( (uint32_t) FSb[ ( Y0 >> 16 ) & 0xFF ] << 16 ) ^
  660. ( (uint32_t) FSb[ ( Y1 >> 24 ) & 0xFF ] << 24 );
  661. X3 = *RK++ ^ \
  662. ( (uint32_t) FSb[ ( Y3 ) & 0xFF ] ) ^
  663. ( (uint32_t) FSb[ ( Y0 >> 8 ) & 0xFF ] << 8 ) ^
  664. ( (uint32_t) FSb[ ( Y1 >> 16 ) & 0xFF ] << 16 ) ^
  665. ( (uint32_t) FSb[ ( Y2 >> 24 ) & 0xFF ] << 24 );
  666. PUT_UINT32_LE( X0, output, 0 );
  667. PUT_UINT32_LE( X1, output, 4 );
  668. PUT_UINT32_LE( X2, output, 8 );
  669. PUT_UINT32_LE( X3, output, 12 );
  670. return( 0 );
  671. }
  672. #endif /* !MBEDTLS_AES_ENCRYPT_ALT */
  673. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  674. void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
  675. const unsigned char input[16],
  676. unsigned char output[16] )
  677. {
  678. mbedtls_internal_aes_encrypt( ctx, input, output );
  679. }
  680. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  681. /*
  682. * AES-ECB block decryption
  683. */
  684. #if !defined(MBEDTLS_AES_DECRYPT_ALT)
  685. int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
  686. const unsigned char input[16],
  687. unsigned char output[16] )
  688. {
  689. int i;
  690. uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
  691. RK = ctx->rk;
  692. GET_UINT32_LE( X0, input, 0 ); X0 ^= *RK++;
  693. GET_UINT32_LE( X1, input, 4 ); X1 ^= *RK++;
  694. GET_UINT32_LE( X2, input, 8 ); X2 ^= *RK++;
  695. GET_UINT32_LE( X3, input, 12 ); X3 ^= *RK++;
  696. for( i = ( ctx->nr >> 1 ) - 1; i > 0; i-- )
  697. {
  698. AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
  699. AES_RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 );
  700. }
  701. AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
  702. X0 = *RK++ ^ \
  703. ( (uint32_t) RSb[ ( Y0 ) & 0xFF ] ) ^
  704. ( (uint32_t) RSb[ ( Y3 >> 8 ) & 0xFF ] << 8 ) ^
  705. ( (uint32_t) RSb[ ( Y2 >> 16 ) & 0xFF ] << 16 ) ^
  706. ( (uint32_t) RSb[ ( Y1 >> 24 ) & 0xFF ] << 24 );
  707. X1 = *RK++ ^ \
  708. ( (uint32_t) RSb[ ( Y1 ) & 0xFF ] ) ^
  709. ( (uint32_t) RSb[ ( Y0 >> 8 ) & 0xFF ] << 8 ) ^
  710. ( (uint32_t) RSb[ ( Y3 >> 16 ) & 0xFF ] << 16 ) ^
  711. ( (uint32_t) RSb[ ( Y2 >> 24 ) & 0xFF ] << 24 );
  712. X2 = *RK++ ^ \
  713. ( (uint32_t) RSb[ ( Y2 ) & 0xFF ] ) ^
  714. ( (uint32_t) RSb[ ( Y1 >> 8 ) & 0xFF ] << 8 ) ^
  715. ( (uint32_t) RSb[ ( Y0 >> 16 ) & 0xFF ] << 16 ) ^
  716. ( (uint32_t) RSb[ ( Y3 >> 24 ) & 0xFF ] << 24 );
  717. X3 = *RK++ ^ \
  718. ( (uint32_t) RSb[ ( Y3 ) & 0xFF ] ) ^
  719. ( (uint32_t) RSb[ ( Y2 >> 8 ) & 0xFF ] << 8 ) ^
  720. ( (uint32_t) RSb[ ( Y1 >> 16 ) & 0xFF ] << 16 ) ^
  721. ( (uint32_t) RSb[ ( Y0 >> 24 ) & 0xFF ] << 24 );
  722. PUT_UINT32_LE( X0, output, 0 );
  723. PUT_UINT32_LE( X1, output, 4 );
  724. PUT_UINT32_LE( X2, output, 8 );
  725. PUT_UINT32_LE( X3, output, 12 );
  726. return( 0 );
  727. }
  728. #endif /* !MBEDTLS_AES_DECRYPT_ALT */
  729. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  730. void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
  731. const unsigned char input[16],
  732. unsigned char output[16] )
  733. {
  734. mbedtls_internal_aes_decrypt( ctx, input, output );
  735. }
  736. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  737. /*
  738. * AES-ECB block encryption/decryption
  739. */
  740. int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
  741. int mode,
  742. const unsigned char input[16],
  743. unsigned char output[16] )
  744. {
  745. #if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64)
  746. if( mbedtls_aesni_has_support( MBEDTLS_AESNI_AES ) )
  747. return( mbedtls_aesni_crypt_ecb( ctx, mode, input, output ) );
  748. #endif
  749. #if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_HAVE_X86)
  750. if( aes_padlock_ace )
  751. {
  752. if( mbedtls_padlock_xcryptecb( ctx, mode, input, output ) == 0 )
  753. return( 0 );
  754. // If padlock data misaligned, we just fall back to
  755. // unaccelerated mode
  756. //
  757. }
  758. #endif
  759. if( mode == MBEDTLS_AES_ENCRYPT )
  760. return( mbedtls_internal_aes_encrypt( ctx, input, output ) );
  761. else
  762. return( mbedtls_internal_aes_decrypt( ctx, input, output ) );
  763. }
  764. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  765. /*
  766. * AES-CBC buffer encryption/decryption
  767. */
  768. int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
  769. int mode,
  770. size_t length,
  771. unsigned char iv[16],
  772. const unsigned char *input,
  773. unsigned char *output )
  774. {
  775. int i;
  776. unsigned char temp[16];
  777. if( length % 16 )
  778. return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
  779. #if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_HAVE_X86)
  780. if( aes_padlock_ace )
  781. {
  782. if( mbedtls_padlock_xcryptcbc( ctx, mode, length, iv, input, output ) == 0 )
  783. return( 0 );
  784. // If padlock data misaligned, we just fall back to
  785. // unaccelerated mode
  786. //
  787. }
  788. #endif
  789. if( mode == MBEDTLS_AES_DECRYPT )
  790. {
  791. while( length > 0 )
  792. {
  793. memcpy( temp, input, 16 );
  794. mbedtls_aes_crypt_ecb( ctx, mode, input, output );
  795. for( i = 0; i < 16; i++ )
  796. output[i] = (unsigned char)( output[i] ^ iv[i] );
  797. memcpy( iv, temp, 16 );
  798. input += 16;
  799. output += 16;
  800. length -= 16;
  801. }
  802. }
  803. else
  804. {
  805. while( length > 0 )
  806. {
  807. for( i = 0; i < 16; i++ )
  808. output[i] = (unsigned char)( input[i] ^ iv[i] );
  809. mbedtls_aes_crypt_ecb( ctx, mode, output, output );
  810. memcpy( iv, output, 16 );
  811. input += 16;
  812. output += 16;
  813. length -= 16;
  814. }
  815. }
  816. return( 0 );
  817. }
  818. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  819. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  820. /*
  821. * AES-CFB128 buffer encryption/decryption
  822. */
  823. int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
  824. int mode,
  825. size_t length,
  826. size_t *iv_off,
  827. unsigned char iv[16],
  828. const unsigned char *input,
  829. unsigned char *output )
  830. {
  831. int c;
  832. size_t n = *iv_off;
  833. if( mode == MBEDTLS_AES_DECRYPT )
  834. {
  835. while( length-- )
  836. {
  837. if( n == 0 )
  838. mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
  839. c = *input++;
  840. *output++ = (unsigned char)( c ^ iv[n] );
  841. iv[n] = (unsigned char) c;
  842. n = ( n + 1 ) & 0x0F;
  843. }
  844. }
  845. else
  846. {
  847. while( length-- )
  848. {
  849. if( n == 0 )
  850. mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
  851. iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
  852. n = ( n + 1 ) & 0x0F;
  853. }
  854. }
  855. *iv_off = n;
  856. return( 0 );
  857. }
  858. /*
  859. * AES-CFB8 buffer encryption/decryption
  860. */
  861. int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
  862. int mode,
  863. size_t length,
  864. unsigned char iv[16],
  865. const unsigned char *input,
  866. unsigned char *output )
  867. {
  868. unsigned char c;
  869. unsigned char ov[17];
  870. while( length-- )
  871. {
  872. memcpy( ov, iv, 16 );
  873. mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
  874. if( mode == MBEDTLS_AES_DECRYPT )
  875. ov[16] = *input;
  876. c = *output++ = (unsigned char)( iv[0] ^ *input++ );
  877. if( mode == MBEDTLS_AES_ENCRYPT )
  878. ov[16] = c;
  879. memcpy( iv, ov + 1, 16 );
  880. }
  881. return( 0 );
  882. }
  883. #endif /*MBEDTLS_CIPHER_MODE_CFB */
  884. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  885. /*
  886. * AES-CTR buffer encryption/decryption
  887. */
  888. int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
  889. size_t length,
  890. size_t *nc_off,
  891. unsigned char nonce_counter[16],
  892. unsigned char stream_block[16],
  893. const unsigned char *input,
  894. unsigned char *output )
  895. {
  896. int c, i;
  897. size_t n = *nc_off;
  898. while( length-- )
  899. {
  900. if( n == 0 ) {
  901. mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, nonce_counter, stream_block );
  902. for( i = 16; i > 0; i-- )
  903. if( ++nonce_counter[i - 1] != 0 )
  904. break;
  905. }
  906. c = *input++;
  907. *output++ = (unsigned char)( c ^ stream_block[n] );
  908. n = ( n + 1 ) & 0x0F;
  909. }
  910. *nc_off = n;
  911. return( 0 );
  912. }
  913. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  914. #endif /* !MBEDTLS_AES_ALT */
  915. #if defined(MBEDTLS_SELF_TEST)
  916. /*
  917. * AES test vectors from:
  918. *
  919. * http://csrc.nist.gov/archive/aes/rijndael/rijndael-vals.zip
  920. */
  921. static const unsigned char aes_test_ecb_dec[3][16] =
  922. {
  923. { 0x44, 0x41, 0x6A, 0xC2, 0xD1, 0xF5, 0x3C, 0x58,
  924. 0x33, 0x03, 0x91, 0x7E, 0x6B, 0xE9, 0xEB, 0xE0 },
  925. { 0x48, 0xE3, 0x1E, 0x9E, 0x25, 0x67, 0x18, 0xF2,
  926. 0x92, 0x29, 0x31, 0x9C, 0x19, 0xF1, 0x5B, 0xA4 },
  927. { 0x05, 0x8C, 0xCF, 0xFD, 0xBB, 0xCB, 0x38, 0x2D,
  928. 0x1F, 0x6F, 0x56, 0x58, 0x5D, 0x8A, 0x4A, 0xDE }
  929. };
  930. static const unsigned char aes_test_ecb_enc[3][16] =
  931. {
  932. { 0xC3, 0x4C, 0x05, 0x2C, 0xC0, 0xDA, 0x8D, 0x73,
  933. 0x45, 0x1A, 0xFE, 0x5F, 0x03, 0xBE, 0x29, 0x7F },
  934. { 0xF3, 0xF6, 0x75, 0x2A, 0xE8, 0xD7, 0x83, 0x11,
  935. 0x38, 0xF0, 0x41, 0x56, 0x06, 0x31, 0xB1, 0x14 },
  936. { 0x8B, 0x79, 0xEE, 0xCC, 0x93, 0xA0, 0xEE, 0x5D,
  937. 0xFF, 0x30, 0xB4, 0xEA, 0x21, 0x63, 0x6D, 0xA4 }
  938. };
  939. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  940. static const unsigned char aes_test_cbc_dec[3][16] =
  941. {
  942. { 0xFA, 0xCA, 0x37, 0xE0, 0xB0, 0xC8, 0x53, 0x73,
  943. 0xDF, 0x70, 0x6E, 0x73, 0xF7, 0xC9, 0xAF, 0x86 },
  944. { 0x5D, 0xF6, 0x78, 0xDD, 0x17, 0xBA, 0x4E, 0x75,
  945. 0xB6, 0x17, 0x68, 0xC6, 0xAD, 0xEF, 0x7C, 0x7B },
  946. { 0x48, 0x04, 0xE1, 0x81, 0x8F, 0xE6, 0x29, 0x75,
  947. 0x19, 0xA3, 0xE8, 0x8C, 0x57, 0x31, 0x04, 0x13 }
  948. };
  949. static const unsigned char aes_test_cbc_enc[3][16] =
  950. {
  951. { 0x8A, 0x05, 0xFC, 0x5E, 0x09, 0x5A, 0xF4, 0x84,
  952. 0x8A, 0x08, 0xD3, 0x28, 0xD3, 0x68, 0x8E, 0x3D },
  953. { 0x7B, 0xD9, 0x66, 0xD5, 0x3A, 0xD8, 0xC1, 0xBB,
  954. 0x85, 0xD2, 0xAD, 0xFA, 0xE8, 0x7B, 0xB1, 0x04 },
  955. { 0xFE, 0x3C, 0x53, 0x65, 0x3E, 0x2F, 0x45, 0xB5,
  956. 0x6F, 0xCD, 0x88, 0xB2, 0xCC, 0x89, 0x8F, 0xF0 }
  957. };
  958. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  959. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  960. /*
  961. * AES-CFB128 test vectors from:
  962. *
  963. * http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
  964. */
  965. static const unsigned char aes_test_cfb128_key[3][32] =
  966. {
  967. { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  968. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C },
  969. { 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  970. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  971. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B },
  972. { 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  973. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  974. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  975. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4 }
  976. };
  977. static const unsigned char aes_test_cfb128_iv[16] =
  978. {
  979. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  980. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  981. };
  982. static const unsigned char aes_test_cfb128_pt[64] =
  983. {
  984. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  985. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A,
  986. 0xAE, 0x2D, 0x8A, 0x57, 0x1E, 0x03, 0xAC, 0x9C,
  987. 0x9E, 0xB7, 0x6F, 0xAC, 0x45, 0xAF, 0x8E, 0x51,
  988. 0x30, 0xC8, 0x1C, 0x46, 0xA3, 0x5C, 0xE4, 0x11,
  989. 0xE5, 0xFB, 0xC1, 0x19, 0x1A, 0x0A, 0x52, 0xEF,
  990. 0xF6, 0x9F, 0x24, 0x45, 0xDF, 0x4F, 0x9B, 0x17,
  991. 0xAD, 0x2B, 0x41, 0x7B, 0xE6, 0x6C, 0x37, 0x10
  992. };
  993. static const unsigned char aes_test_cfb128_ct[3][64] =
  994. {
  995. { 0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20,
  996. 0x33, 0x34, 0x49, 0xF8, 0xE8, 0x3C, 0xFB, 0x4A,
  997. 0xC8, 0xA6, 0x45, 0x37, 0xA0, 0xB3, 0xA9, 0x3F,
  998. 0xCD, 0xE3, 0xCD, 0xAD, 0x9F, 0x1C, 0xE5, 0x8B,
  999. 0x26, 0x75, 0x1F, 0x67, 0xA3, 0xCB, 0xB1, 0x40,
  1000. 0xB1, 0x80, 0x8C, 0xF1, 0x87, 0xA4, 0xF4, 0xDF,
  1001. 0xC0, 0x4B, 0x05, 0x35, 0x7C, 0x5D, 0x1C, 0x0E,
  1002. 0xEA, 0xC4, 0xC6, 0x6F, 0x9F, 0xF7, 0xF2, 0xE6 },
  1003. { 0xCD, 0xC8, 0x0D, 0x6F, 0xDD, 0xF1, 0x8C, 0xAB,
  1004. 0x34, 0xC2, 0x59, 0x09, 0xC9, 0x9A, 0x41, 0x74,
  1005. 0x67, 0xCE, 0x7F, 0x7F, 0x81, 0x17, 0x36, 0x21,
  1006. 0x96, 0x1A, 0x2B, 0x70, 0x17, 0x1D, 0x3D, 0x7A,
  1007. 0x2E, 0x1E, 0x8A, 0x1D, 0xD5, 0x9B, 0x88, 0xB1,
  1008. 0xC8, 0xE6, 0x0F, 0xED, 0x1E, 0xFA, 0xC4, 0xC9,
  1009. 0xC0, 0x5F, 0x9F, 0x9C, 0xA9, 0x83, 0x4F, 0xA0,
  1010. 0x42, 0xAE, 0x8F, 0xBA, 0x58, 0x4B, 0x09, 0xFF },
  1011. { 0xDC, 0x7E, 0x84, 0xBF, 0xDA, 0x79, 0x16, 0x4B,
  1012. 0x7E, 0xCD, 0x84, 0x86, 0x98, 0x5D, 0x38, 0x60,
  1013. 0x39, 0xFF, 0xED, 0x14, 0x3B, 0x28, 0xB1, 0xC8,
  1014. 0x32, 0x11, 0x3C, 0x63, 0x31, 0xE5, 0x40, 0x7B,
  1015. 0xDF, 0x10, 0x13, 0x24, 0x15, 0xE5, 0x4B, 0x92,
  1016. 0xA1, 0x3E, 0xD0, 0xA8, 0x26, 0x7A, 0xE2, 0xF9,
  1017. 0x75, 0xA3, 0x85, 0x74, 0x1A, 0xB9, 0xCE, 0xF8,
  1018. 0x20, 0x31, 0x62, 0x3D, 0x55, 0xB1, 0xE4, 0x71 }
  1019. };
  1020. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  1021. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1022. /*
  1023. * AES-CTR test vectors from:
  1024. *
  1025. * http://www.faqs.org/rfcs/rfc3686.html
  1026. */
  1027. static const unsigned char aes_test_ctr_key[3][16] =
  1028. {
  1029. { 0xAE, 0x68, 0x52, 0xF8, 0x12, 0x10, 0x67, 0xCC,
  1030. 0x4B, 0xF7, 0xA5, 0x76, 0x55, 0x77, 0xF3, 0x9E },
  1031. { 0x7E, 0x24, 0x06, 0x78, 0x17, 0xFA, 0xE0, 0xD7,
  1032. 0x43, 0xD6, 0xCE, 0x1F, 0x32, 0x53, 0x91, 0x63 },
  1033. { 0x76, 0x91, 0xBE, 0x03, 0x5E, 0x50, 0x20, 0xA8,
  1034. 0xAC, 0x6E, 0x61, 0x85, 0x29, 0xF9, 0xA0, 0xDC }
  1035. };
  1036. static const unsigned char aes_test_ctr_nonce_counter[3][16] =
  1037. {
  1038. { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00,
  1039. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
  1040. { 0x00, 0x6C, 0xB6, 0xDB, 0xC0, 0x54, 0x3B, 0x59,
  1041. 0xDA, 0x48, 0xD9, 0x0B, 0x00, 0x00, 0x00, 0x01 },
  1042. { 0x00, 0xE0, 0x01, 0x7B, 0x27, 0x77, 0x7F, 0x3F,
  1043. 0x4A, 0x17, 0x86, 0xF0, 0x00, 0x00, 0x00, 0x01 }
  1044. };
  1045. static const unsigned char aes_test_ctr_pt[3][48] =
  1046. {
  1047. { 0x53, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x62,
  1048. 0x6C, 0x6F, 0x63, 0x6B, 0x20, 0x6D, 0x73, 0x67 },
  1049. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  1050. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  1051. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  1052. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F },
  1053. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  1054. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  1055. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  1056. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
  1057. 0x20, 0x21, 0x22, 0x23 }
  1058. };
  1059. static const unsigned char aes_test_ctr_ct[3][48] =
  1060. {
  1061. { 0xE4, 0x09, 0x5D, 0x4F, 0xB7, 0xA7, 0xB3, 0x79,
  1062. 0x2D, 0x61, 0x75, 0xA3, 0x26, 0x13, 0x11, 0xB8 },
  1063. { 0x51, 0x04, 0xA1, 0x06, 0x16, 0x8A, 0x72, 0xD9,
  1064. 0x79, 0x0D, 0x41, 0xEE, 0x8E, 0xDA, 0xD3, 0x88,
  1065. 0xEB, 0x2E, 0x1E, 0xFC, 0x46, 0xDA, 0x57, 0xC8,
  1066. 0xFC, 0xE6, 0x30, 0xDF, 0x91, 0x41, 0xBE, 0x28 },
  1067. { 0xC1, 0xCF, 0x48, 0xA8, 0x9F, 0x2F, 0xFD, 0xD9,
  1068. 0xCF, 0x46, 0x52, 0xE9, 0xEF, 0xDB, 0x72, 0xD7,
  1069. 0x45, 0x40, 0xA4, 0x2B, 0xDE, 0x6D, 0x78, 0x36,
  1070. 0xD5, 0x9A, 0x5C, 0xEA, 0xAE, 0xF3, 0x10, 0x53,
  1071. 0x25, 0xB2, 0x07, 0x2F }
  1072. };
  1073. static const int aes_test_ctr_len[3] =
  1074. { 16, 32, 36 };
  1075. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  1076. /*
  1077. * Checkup routine
  1078. */
  1079. int mbedtls_aes_self_test( int verbose )
  1080. {
  1081. int ret = 0, i, j, u, mode;
  1082. unsigned int keybits;
  1083. unsigned char key[32];
  1084. unsigned char buf[64];
  1085. const unsigned char *aes_tests;
  1086. #if defined(MBEDTLS_CIPHER_MODE_CBC) || defined(MBEDTLS_CIPHER_MODE_CFB)
  1087. unsigned char iv[16];
  1088. #endif
  1089. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1090. unsigned char prv[16];
  1091. #endif
  1092. #if defined(MBEDTLS_CIPHER_MODE_CTR) || defined(MBEDTLS_CIPHER_MODE_CFB)
  1093. size_t offset;
  1094. #endif
  1095. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1096. int len;
  1097. unsigned char nonce_counter[16];
  1098. unsigned char stream_block[16];
  1099. #endif
  1100. mbedtls_aes_context ctx;
  1101. memset( key, 0, 32 );
  1102. mbedtls_aes_init( &ctx );
  1103. /*
  1104. * ECB mode
  1105. */
  1106. for( i = 0; i < 6; i++ )
  1107. {
  1108. u = i >> 1;
  1109. keybits = 128 + u * 64;
  1110. mode = i & 1;
  1111. if( verbose != 0 )
  1112. mbedtls_printf( " AES-ECB-%3d (%s): ", keybits,
  1113. ( mode == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" );
  1114. memset( buf, 0, 16 );
  1115. if( mode == MBEDTLS_AES_DECRYPT )
  1116. {
  1117. ret = mbedtls_aes_setkey_dec( &ctx, key, keybits );
  1118. aes_tests = aes_test_ecb_dec[u];
  1119. }
  1120. else
  1121. {
  1122. ret = mbedtls_aes_setkey_enc( &ctx, key, keybits );
  1123. aes_tests = aes_test_ecb_enc[u];
  1124. }
  1125. /*
  1126. * AES-192 is an optional feature that may be unavailable when
  1127. * there is an alternative underlying implementation i.e. when
  1128. * MBEDTLS_AES_ALT is defined.
  1129. */
  1130. if( ret == MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE && keybits == 192 )
  1131. {
  1132. mbedtls_printf( "skipped\n" );
  1133. continue;
  1134. }
  1135. else if( ret != 0 )
  1136. {
  1137. goto exit;
  1138. }
  1139. for( j = 0; j < 10000; j++ )
  1140. {
  1141. ret = mbedtls_aes_crypt_ecb( &ctx, mode, buf, buf );
  1142. if( ret != 0 )
  1143. goto exit;
  1144. }
  1145. if( memcmp( buf, aes_tests, 16 ) != 0 )
  1146. {
  1147. ret = 1;
  1148. goto exit;
  1149. }
  1150. if( verbose != 0 )
  1151. mbedtls_printf( "passed\n" );
  1152. }
  1153. if( verbose != 0 )
  1154. mbedtls_printf( "\n" );
  1155. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1156. /*
  1157. * CBC mode
  1158. */
  1159. for( i = 0; i < 6; i++ )
  1160. {
  1161. u = i >> 1;
  1162. keybits = 128 + u * 64;
  1163. mode = i & 1;
  1164. if( verbose != 0 )
  1165. mbedtls_printf( " AES-CBC-%3d (%s): ", keybits,
  1166. ( mode == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" );
  1167. memset( iv , 0, 16 );
  1168. memset( prv, 0, 16 );
  1169. memset( buf, 0, 16 );
  1170. if( mode == MBEDTLS_AES_DECRYPT )
  1171. {
  1172. ret = mbedtls_aes_setkey_dec( &ctx, key, keybits );
  1173. aes_tests = aes_test_cbc_dec[u];
  1174. }
  1175. else
  1176. {
  1177. ret = mbedtls_aes_setkey_enc( &ctx, key, keybits );
  1178. aes_tests = aes_test_cbc_enc[u];
  1179. }
  1180. /*
  1181. * AES-192 is an optional feature that may be unavailable when
  1182. * there is an alternative underlying implementation i.e. when
  1183. * MBEDTLS_AES_ALT is defined.
  1184. */
  1185. if( ret == MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE && keybits == 192 )
  1186. {
  1187. mbedtls_printf( "skipped\n" );
  1188. continue;
  1189. }
  1190. else if( ret != 0 )
  1191. {
  1192. goto exit;
  1193. }
  1194. for( j = 0; j < 10000; j++ )
  1195. {
  1196. if( mode == MBEDTLS_AES_ENCRYPT )
  1197. {
  1198. unsigned char tmp[16];
  1199. memcpy( tmp, prv, 16 );
  1200. memcpy( prv, buf, 16 );
  1201. memcpy( buf, tmp, 16 );
  1202. }
  1203. ret = mbedtls_aes_crypt_cbc( &ctx, mode, 16, iv, buf, buf );
  1204. if( ret != 0 )
  1205. goto exit;
  1206. }
  1207. if( memcmp( buf, aes_tests, 16 ) != 0 )
  1208. {
  1209. ret = 1;
  1210. goto exit;
  1211. }
  1212. if( verbose != 0 )
  1213. mbedtls_printf( "passed\n" );
  1214. }
  1215. if( verbose != 0 )
  1216. mbedtls_printf( "\n" );
  1217. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  1218. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1219. /*
  1220. * CFB128 mode
  1221. */
  1222. for( i = 0; i < 6; i++ )
  1223. {
  1224. u = i >> 1;
  1225. keybits = 128 + u * 64;
  1226. mode = i & 1;
  1227. if( verbose != 0 )
  1228. mbedtls_printf( " AES-CFB128-%3d (%s): ", keybits,
  1229. ( mode == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" );
  1230. memcpy( iv, aes_test_cfb128_iv, 16 );
  1231. memcpy( key, aes_test_cfb128_key[u], keybits / 8 );
  1232. offset = 0;
  1233. ret = mbedtls_aes_setkey_enc( &ctx, key, keybits );
  1234. /*
  1235. * AES-192 is an optional feature that may be unavailable when
  1236. * there is an alternative underlying implementation i.e. when
  1237. * MBEDTLS_AES_ALT is defined.
  1238. */
  1239. if( ret == MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE && keybits == 192 )
  1240. {
  1241. mbedtls_printf( "skipped\n" );
  1242. continue;
  1243. }
  1244. else if( ret != 0 )
  1245. {
  1246. goto exit;
  1247. }
  1248. if( mode == MBEDTLS_AES_DECRYPT )
  1249. {
  1250. memcpy( buf, aes_test_cfb128_ct[u], 64 );
  1251. aes_tests = aes_test_cfb128_pt;
  1252. }
  1253. else
  1254. {
  1255. memcpy( buf, aes_test_cfb128_pt, 64 );
  1256. aes_tests = aes_test_cfb128_ct[u];
  1257. }
  1258. ret = mbedtls_aes_crypt_cfb128( &ctx, mode, 64, &offset, iv, buf, buf );
  1259. if( ret != 0 )
  1260. goto exit;
  1261. if( memcmp( buf, aes_tests, 64 ) != 0 )
  1262. {
  1263. ret = 1;
  1264. goto exit;
  1265. }
  1266. if( verbose != 0 )
  1267. mbedtls_printf( "passed\n" );
  1268. }
  1269. if( verbose != 0 )
  1270. mbedtls_printf( "\n" );
  1271. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  1272. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1273. /*
  1274. * CTR mode
  1275. */
  1276. for( i = 0; i < 6; i++ )
  1277. {
  1278. u = i >> 1;
  1279. mode = i & 1;
  1280. if( verbose != 0 )
  1281. mbedtls_printf( " AES-CTR-128 (%s): ",
  1282. ( mode == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" );
  1283. memcpy( nonce_counter, aes_test_ctr_nonce_counter[u], 16 );
  1284. memcpy( key, aes_test_ctr_key[u], 16 );
  1285. offset = 0;
  1286. if( ( ret = mbedtls_aes_setkey_enc( &ctx, key, 128 ) ) != 0 )
  1287. goto exit;
  1288. len = aes_test_ctr_len[u];
  1289. if( mode == MBEDTLS_AES_DECRYPT )
  1290. {
  1291. memcpy( buf, aes_test_ctr_ct[u], len );
  1292. aes_tests = aes_test_ctr_pt[u];
  1293. }
  1294. else
  1295. {
  1296. memcpy( buf, aes_test_ctr_pt[u], len );
  1297. aes_tests = aes_test_ctr_ct[u];
  1298. }
  1299. ret = mbedtls_aes_crypt_ctr( &ctx, len, &offset, nonce_counter,
  1300. stream_block, buf, buf );
  1301. if( ret != 0 )
  1302. goto exit;
  1303. if( memcmp( buf, aes_tests, len ) != 0 )
  1304. {
  1305. ret = 1;
  1306. goto exit;
  1307. }
  1308. if( verbose != 0 )
  1309. mbedtls_printf( "passed\n" );
  1310. }
  1311. if( verbose != 0 )
  1312. mbedtls_printf( "\n" );
  1313. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  1314. ret = 0;
  1315. exit:
  1316. if( ret != 0 && verbose != 0 )
  1317. mbedtls_printf( "failed\n" );
  1318. mbedtls_aes_free( &ctx );
  1319. return( ret );
  1320. }
  1321. #endif /* MBEDTLS_SELF_TEST */
  1322. #endif /* MBEDTLS_AES_C */
  1323. /*
  1324. Amalgamated build undefines
  1325. */
  1326. #undef ADD
  1327. #undef BC
  1328. #undef BEFORE_COLON
  1329. #undef F
  1330. #undef F0
  1331. #undef F1
  1332. #undef F2
  1333. #undef F3
  1334. #undef F4
  1335. #undef F5
  1336. #undef FSb
  1337. #undef K
  1338. #undef KK
  1339. #undef P
  1340. #undef R
  1341. #undef ROTR
  1342. #undef S
  1343. #undef S0
  1344. #undef S1
  1345. #undef S2
  1346. #undef S3
  1347. #undef SAFE_SNPRINTF
  1348. #undef SHR
  1349. #undef close
  1350. #undef read
  1351. #undef supported_init
  1352. #undef write
  1353. /********* Start of file library/aesni.c ************/
  1354. /*
  1355. * AES-NI support functions
  1356. *
  1357. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  1358. * SPDX-License-Identifier: Apache-2.0
  1359. *
  1360. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  1361. * not use this file except in compliance with the License.
  1362. * You may obtain a copy of the License at
  1363. *
  1364. * http://www.apache.org/licenses/LICENSE-2.0
  1365. *
  1366. * Unless required by applicable law or agreed to in writing, software
  1367. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  1368. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1369. * See the License for the specific language governing permissions and
  1370. * limitations under the License.
  1371. *
  1372. * This file is part of mbed TLS (https://tls.mbed.org)
  1373. */
  1374. /*
  1375. * [AES-WP] http://software.intel.com/en-us/articles/intel-advanced-encryption-standard-aes-instructions-set
  1376. * [CLMUL-WP] http://software.intel.com/en-us/articles/intel-carry-less-multiplication-instruction-and-its-usage-for-computing-the-gcm-mode/
  1377. */
  1378. #if !defined(MBEDTLS_CONFIG_FILE)
  1379. #else
  1380. #endif
  1381. #if defined(MBEDTLS_AESNI_C)
  1382. #include <string.h>
  1383. #ifndef asm
  1384. #define asm __asm
  1385. #endif
  1386. #if defined(MBEDTLS_HAVE_X86_64)
  1387. /*
  1388. * AES-NI support detection routine
  1389. */
  1390. int mbedtls_aesni_has_support( unsigned int what )
  1391. {
  1392. static int done = 0;
  1393. static unsigned int c = 0;
  1394. if( ! done )
  1395. {
  1396. asm( "movl $1, %%eax \n\t"
  1397. "cpuid \n\t"
  1398. : "=c" (c)
  1399. :
  1400. : "eax", "ebx", "edx" );
  1401. done = 1;
  1402. }
  1403. return( ( c & what ) != 0 );
  1404. }
  1405. /*
  1406. * Binutils needs to be at least 2.19 to support AES-NI instructions.
  1407. * Unfortunately, a lot of users have a lower version now (2014-04).
  1408. * Emit bytecode directly in order to support "old" version of gas.
  1409. *
  1410. * Opcodes from the Intel architecture reference manual, vol. 3.
  1411. * We always use registers, so we don't need prefixes for memory operands.
  1412. * Operand macros are in gas order (src, dst) as opposed to Intel order
  1413. * (dst, src) in order to blend better into the surrounding assembly code.
  1414. */
  1415. #define AESDEC ".byte 0x66,0x0F,0x38,0xDE,"
  1416. #define AESDECLAST ".byte 0x66,0x0F,0x38,0xDF,"
  1417. #define AESENC ".byte 0x66,0x0F,0x38,0xDC,"
  1418. #define AESENCLAST ".byte 0x66,0x0F,0x38,0xDD,"
  1419. #define AESIMC ".byte 0x66,0x0F,0x38,0xDB,"
  1420. #define AESKEYGENA ".byte 0x66,0x0F,0x3A,0xDF,"
  1421. #define PCLMULQDQ ".byte 0x66,0x0F,0x3A,0x44,"
  1422. #define xmm0_xmm0 "0xC0"
  1423. #define xmm0_xmm1 "0xC8"
  1424. #define xmm0_xmm2 "0xD0"
  1425. #define xmm0_xmm3 "0xD8"
  1426. #define xmm0_xmm4 "0xE0"
  1427. #define xmm1_xmm0 "0xC1"
  1428. #define xmm1_xmm2 "0xD1"
  1429. /*
  1430. * AES-NI AES-ECB block en(de)cryption
  1431. */
  1432. int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx,
  1433. int mode,
  1434. const unsigned char input[16],
  1435. unsigned char output[16] )
  1436. {
  1437. asm( "movdqu (%3), %%xmm0 \n\t" // load input
  1438. "movdqu (%1), %%xmm1 \n\t" // load round key 0
  1439. "pxor %%xmm1, %%xmm0 \n\t" // round 0
  1440. "add $16, %1 \n\t" // point to next round key
  1441. "subl $1, %0 \n\t" // normal rounds = nr - 1
  1442. "test %2, %2 \n\t" // mode?
  1443. "jz 2f \n\t" // 0 = decrypt
  1444. "1: \n\t" // encryption loop
  1445. "movdqu (%1), %%xmm1 \n\t" // load round key
  1446. AESENC xmm1_xmm0 "\n\t" // do round
  1447. "add $16, %1 \n\t" // point to next round key
  1448. "subl $1, %0 \n\t" // loop
  1449. "jnz 1b \n\t"
  1450. "movdqu (%1), %%xmm1 \n\t" // load round key
  1451. AESENCLAST xmm1_xmm0 "\n\t" // last round
  1452. "jmp 3f \n\t"
  1453. "2: \n\t" // decryption loop
  1454. "movdqu (%1), %%xmm1 \n\t"
  1455. AESDEC xmm1_xmm0 "\n\t" // do round
  1456. "add $16, %1 \n\t"
  1457. "subl $1, %0 \n\t"
  1458. "jnz 2b \n\t"
  1459. "movdqu (%1), %%xmm1 \n\t" // load round key
  1460. AESDECLAST xmm1_xmm0 "\n\t" // last round
  1461. "3: \n\t"
  1462. "movdqu %%xmm0, (%4) \n\t" // export output
  1463. :
  1464. : "r" (ctx->nr), "r" (ctx->rk), "r" (mode), "r" (input), "r" (output)
  1465. : "memory", "cc", "xmm0", "xmm1" );
  1466. return( 0 );
  1467. }
  1468. /*
  1469. * GCM multiplication: c = a times b in GF(2^128)
  1470. * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
  1471. */
  1472. void mbedtls_aesni_gcm_mult( unsigned char c[16],
  1473. const unsigned char a[16],
  1474. const unsigned char b[16] )
  1475. {
  1476. unsigned char aa[16], bb[16], cc[16];
  1477. size_t i;
  1478. /* The inputs are in big-endian order, so byte-reverse them */
  1479. for( i = 0; i < 16; i++ )
  1480. {
  1481. aa[i] = a[15 - i];
  1482. bb[i] = b[15 - i];
  1483. }
  1484. asm( "movdqu (%0), %%xmm0 \n\t" // a1:a0
  1485. "movdqu (%1), %%xmm1 \n\t" // b1:b0
  1486. /*
  1487. * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
  1488. * using [CLMUL-WP] algorithm 1 (p. 13).
  1489. */
  1490. "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
  1491. "movdqa %%xmm1, %%xmm3 \n\t" // same
  1492. "movdqa %%xmm1, %%xmm4 \n\t" // same
  1493. PCLMULQDQ xmm0_xmm1 ",0x00 \n\t" // a0*b0 = c1:c0
  1494. PCLMULQDQ xmm0_xmm2 ",0x11 \n\t" // a1*b1 = d1:d0
  1495. PCLMULQDQ xmm0_xmm3 ",0x10 \n\t" // a0*b1 = e1:e0
  1496. PCLMULQDQ xmm0_xmm4 ",0x01 \n\t" // a1*b0 = f1:f0
  1497. "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
  1498. "movdqa %%xmm4, %%xmm3 \n\t" // same
  1499. "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
  1500. "pslldq $8, %%xmm3 \n\t" // e0+f0:0
  1501. "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
  1502. "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
  1503. /*
  1504. * Now shift the result one bit to the left,
  1505. * taking advantage of [CLMUL-WP] eq 27 (p. 20)
  1506. */
  1507. "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
  1508. "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
  1509. "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
  1510. "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
  1511. "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
  1512. "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
  1513. "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
  1514. "pslldq $8, %%xmm3 \n\t" // r0>>63:0
  1515. "pslldq $8, %%xmm4 \n\t" // r2>>63:0
  1516. "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
  1517. "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
  1518. "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
  1519. "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
  1520. /*
  1521. * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
  1522. * using [CLMUL-WP] algorithm 5 (p. 20).
  1523. * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
  1524. */
  1525. /* Step 2 (1) */
  1526. "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
  1527. "movdqa %%xmm1, %%xmm4 \n\t" // same
  1528. "movdqa %%xmm1, %%xmm5 \n\t" // same
  1529. "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
  1530. "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
  1531. "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
  1532. /* Step 2 (2) */
  1533. "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
  1534. "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
  1535. "pslldq $8, %%xmm3 \n\t" // a+b+c:0
  1536. "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
  1537. /* Steps 3 and 4 */
  1538. "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
  1539. "movdqa %%xmm1,%%xmm4 \n\t" // same
  1540. "movdqa %%xmm1,%%xmm5 \n\t" // same
  1541. "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
  1542. "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
  1543. "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
  1544. "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
  1545. "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
  1546. // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
  1547. // bits carried from d. Now get those\t bits back in.
  1548. "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
  1549. "movdqa %%xmm1,%%xmm4 \n\t" // same
  1550. "movdqa %%xmm1,%%xmm5 \n\t" // same
  1551. "psllq $63, %%xmm3 \n\t" // d<<63:stuff
  1552. "psllq $62, %%xmm4 \n\t" // d<<62:stuff
  1553. "psllq $57, %%xmm5 \n\t" // d<<57:stuff
  1554. "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
  1555. "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
  1556. "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
  1557. "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
  1558. "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
  1559. "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
  1560. "movdqu %%xmm0, (%2) \n\t" // done
  1561. :
  1562. : "r" (aa), "r" (bb), "r" (cc)
  1563. : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5" );
  1564. /* Now byte-reverse the outputs */
  1565. for( i = 0; i < 16; i++ )
  1566. c[i] = cc[15 - i];
  1567. return;
  1568. }
  1569. /*
  1570. * Compute decryption round keys from encryption round keys
  1571. */
  1572. void mbedtls_aesni_inverse_key( unsigned char *invkey,
  1573. const unsigned char *fwdkey, int nr )
  1574. {
  1575. unsigned char *ik = invkey;
  1576. const unsigned char *fk = fwdkey + 16 * nr;
  1577. memcpy( ik, fk, 16 );
  1578. for( fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16 )
  1579. asm( "movdqu (%0), %%xmm0 \n\t"
  1580. AESIMC xmm0_xmm0 "\n\t"
  1581. "movdqu %%xmm0, (%1) \n\t"
  1582. :
  1583. : "r" (fk), "r" (ik)
  1584. : "memory", "xmm0" );
  1585. memcpy( ik, fk, 16 );
  1586. }
  1587. /*
  1588. * Key expansion, 128-bit case
  1589. */
  1590. static void aesni_setkey_enc_128( unsigned char *rk,
  1591. const unsigned char *key )
  1592. {
  1593. asm( "movdqu (%1), %%xmm0 \n\t" // copy the original key
  1594. "movdqu %%xmm0, (%0) \n\t" // as round key 0
  1595. "jmp 2f \n\t" // skip auxiliary routine
  1596. /*
  1597. * Finish generating the next round key.
  1598. *
  1599. * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
  1600. * with X = rot( sub( r3 ) ) ^ RCON.
  1601. *
  1602. * On exit, xmm0 is r7:r6:r5:r4
  1603. * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
  1604. * and those are written to the round key buffer.
  1605. */
  1606. "1: \n\t"
  1607. "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
  1608. "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
  1609. "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
  1610. "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
  1611. "pslldq $4, %%xmm0 \n\t" // etc
  1612. "pxor %%xmm0, %%xmm1 \n\t"
  1613. "pslldq $4, %%xmm0 \n\t"
  1614. "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
  1615. "add $16, %0 \n\t" // point to next round key
  1616. "movdqu %%xmm0, (%0) \n\t" // write it
  1617. "ret \n\t"
  1618. /* Main "loop" */
  1619. "2: \n\t"
  1620. AESKEYGENA xmm0_xmm1 ",0x01 \n\tcall 1b \n\t"
  1621. AESKEYGENA xmm0_xmm1 ",0x02 \n\tcall 1b \n\t"
  1622. AESKEYGENA xmm0_xmm1 ",0x04 \n\tcall 1b \n\t"
  1623. AESKEYGENA xmm0_xmm1 ",0x08 \n\tcall 1b \n\t"
  1624. AESKEYGENA xmm0_xmm1 ",0x10 \n\tcall 1b \n\t"
  1625. AESKEYGENA xmm0_xmm1 ",0x20 \n\tcall 1b \n\t"
  1626. AESKEYGENA xmm0_xmm1 ",0x40 \n\tcall 1b \n\t"
  1627. AESKEYGENA xmm0_xmm1 ",0x80 \n\tcall 1b \n\t"
  1628. AESKEYGENA xmm0_xmm1 ",0x1B \n\tcall 1b \n\t"
  1629. AESKEYGENA xmm0_xmm1 ",0x36 \n\tcall 1b \n\t"
  1630. :
  1631. : "r" (rk), "r" (key)
  1632. : "memory", "cc", "0" );
  1633. }
  1634. /*
  1635. * Key expansion, 192-bit case
  1636. */
  1637. static void aesni_setkey_enc_192( unsigned char *rk,
  1638. const unsigned char *key )
  1639. {
  1640. asm( "movdqu (%1), %%xmm0 \n\t" // copy original round key
  1641. "movdqu %%xmm0, (%0) \n\t"
  1642. "add $16, %0 \n\t"
  1643. "movq 16(%1), %%xmm1 \n\t"
  1644. "movq %%xmm1, (%0) \n\t"
  1645. "add $8, %0 \n\t"
  1646. "jmp 2f \n\t" // skip auxiliary routine
  1647. /*
  1648. * Finish generating the next 6 quarter-keys.
  1649. *
  1650. * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
  1651. * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
  1652. *
  1653. * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
  1654. * and those are written to the round key buffer.
  1655. */
  1656. "1: \n\t"
  1657. "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
  1658. "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
  1659. "pslldq $4, %%xmm0 \n\t" // etc
  1660. "pxor %%xmm0, %%xmm2 \n\t"
  1661. "pslldq $4, %%xmm0 \n\t"
  1662. "pxor %%xmm0, %%xmm2 \n\t"
  1663. "pslldq $4, %%xmm0 \n\t"
  1664. "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
  1665. "movdqu %%xmm0, (%0) \n\t"
  1666. "add $16, %0 \n\t"
  1667. "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
  1668. "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
  1669. "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
  1670. "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
  1671. "movq %%xmm1, (%0) \n\t"
  1672. "add $8, %0 \n\t"
  1673. "ret \n\t"
  1674. "2: \n\t"
  1675. AESKEYGENA xmm1_xmm2 ",0x01 \n\tcall 1b \n\t"
  1676. AESKEYGENA xmm1_xmm2 ",0x02 \n\tcall 1b \n\t"
  1677. AESKEYGENA xmm1_xmm2 ",0x04 \n\tcall 1b \n\t"
  1678. AESKEYGENA xmm1_xmm2 ",0x08 \n\tcall 1b \n\t"
  1679. AESKEYGENA xmm1_xmm2 ",0x10 \n\tcall 1b \n\t"
  1680. AESKEYGENA xmm1_xmm2 ",0x20 \n\tcall 1b \n\t"
  1681. AESKEYGENA xmm1_xmm2 ",0x40 \n\tcall 1b \n\t"
  1682. AESKEYGENA xmm1_xmm2 ",0x80 \n\tcall 1b \n\t"
  1683. :
  1684. : "r" (rk), "r" (key)
  1685. : "memory", "cc", "0" );
  1686. }
  1687. /*
  1688. * Key expansion, 256-bit case
  1689. */
  1690. static void aesni_setkey_enc_256( unsigned char *rk,
  1691. const unsigned char *key )
  1692. {
  1693. asm( "movdqu (%1), %%xmm0 \n\t"
  1694. "movdqu %%xmm0, (%0) \n\t"
  1695. "add $16, %0 \n\t"
  1696. "movdqu 16(%1), %%xmm1 \n\t"
  1697. "movdqu %%xmm1, (%0) \n\t"
  1698. "jmp 2f \n\t" // skip auxiliary routine
  1699. /*
  1700. * Finish generating the next two round keys.
  1701. *
  1702. * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
  1703. * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
  1704. *
  1705. * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
  1706. * and those have been written to the output buffer.
  1707. */
  1708. "1: \n\t"
  1709. "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
  1710. "pxor %%xmm0, %%xmm2 \n\t"
  1711. "pslldq $4, %%xmm0 \n\t"
  1712. "pxor %%xmm0, %%xmm2 \n\t"
  1713. "pslldq $4, %%xmm0 \n\t"
  1714. "pxor %%xmm0, %%xmm2 \n\t"
  1715. "pslldq $4, %%xmm0 \n\t"
  1716. "pxor %%xmm2, %%xmm0 \n\t"
  1717. "add $16, %0 \n\t"
  1718. "movdqu %%xmm0, (%0) \n\t"
  1719. /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
  1720. * and proceed to generate next round key from there */
  1721. AESKEYGENA xmm0_xmm2 ",0x00 \n\t"
  1722. "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
  1723. "pxor %%xmm1, %%xmm2 \n\t"
  1724. "pslldq $4, %%xmm1 \n\t"
  1725. "pxor %%xmm1, %%xmm2 \n\t"
  1726. "pslldq $4, %%xmm1 \n\t"
  1727. "pxor %%xmm1, %%xmm2 \n\t"
  1728. "pslldq $4, %%xmm1 \n\t"
  1729. "pxor %%xmm2, %%xmm1 \n\t"
  1730. "add $16, %0 \n\t"
  1731. "movdqu %%xmm1, (%0) \n\t"
  1732. "ret \n\t"
  1733. /*
  1734. * Main "loop" - Generating one more key than necessary,
  1735. * see definition of mbedtls_aes_context.buf
  1736. */
  1737. "2: \n\t"
  1738. AESKEYGENA xmm1_xmm2 ",0x01 \n\tcall 1b \n\t"
  1739. AESKEYGENA xmm1_xmm2 ",0x02 \n\tcall 1b \n\t"
  1740. AESKEYGENA xmm1_xmm2 ",0x04 \n\tcall 1b \n\t"
  1741. AESKEYGENA xmm1_xmm2 ",0x08 \n\tcall 1b \n\t"
  1742. AESKEYGENA xmm1_xmm2 ",0x10 \n\tcall 1b \n\t"
  1743. AESKEYGENA xmm1_xmm2 ",0x20 \n\tcall 1b \n\t"
  1744. AESKEYGENA xmm1_xmm2 ",0x40 \n\tcall 1b \n\t"
  1745. :
  1746. : "r" (rk), "r" (key)
  1747. : "memory", "cc", "0" );
  1748. }
  1749. /*
  1750. * Key expansion, wrapper
  1751. */
  1752. int mbedtls_aesni_setkey_enc( unsigned char *rk,
  1753. const unsigned char *key,
  1754. size_t bits )
  1755. {
  1756. switch( bits )
  1757. {
  1758. case 128: aesni_setkey_enc_128( rk, key ); break;
  1759. case 192: aesni_setkey_enc_192( rk, key ); break;
  1760. case 256: aesni_setkey_enc_256( rk, key ); break;
  1761. default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
  1762. }
  1763. return( 0 );
  1764. }
  1765. #endif /* MBEDTLS_HAVE_X86_64 */
  1766. #endif /* MBEDTLS_AESNI_C */
  1767. /*
  1768. Amalgamated build undefines
  1769. */
  1770. #undef ADD
  1771. #undef BC
  1772. #undef BEFORE_COLON
  1773. #undef F
  1774. #undef F0
  1775. #undef F1
  1776. #undef F2
  1777. #undef F3
  1778. #undef F4
  1779. #undef F5
  1780. #undef FSb
  1781. #undef K
  1782. #undef KK
  1783. #undef P
  1784. #undef R
  1785. #undef ROTR
  1786. #undef S
  1787. #undef S0
  1788. #undef S1
  1789. #undef S2
  1790. #undef S3
  1791. #undef SAFE_SNPRINTF
  1792. #undef SHR
  1793. #undef close
  1794. #undef read
  1795. #undef supported_init
  1796. #undef write
  1797. /********* Start of file library/arc4.c ************/
  1798. /*
  1799. * An implementation of the ARCFOUR algorithm
  1800. *
  1801. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  1802. * SPDX-License-Identifier: Apache-2.0
  1803. *
  1804. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  1805. * not use this file except in compliance with the License.
  1806. * You may obtain a copy of the License at
  1807. *
  1808. * http://www.apache.org/licenses/LICENSE-2.0
  1809. *
  1810. * Unless required by applicable law or agreed to in writing, software
  1811. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  1812. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1813. * See the License for the specific language governing permissions and
  1814. * limitations under the License.
  1815. *
  1816. * This file is part of mbed TLS (https://tls.mbed.org)
  1817. */
  1818. /*
  1819. * The ARCFOUR algorithm was publicly disclosed on 94/09.
  1820. *
  1821. * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
  1822. */
  1823. #if !defined(MBEDTLS_CONFIG_FILE)
  1824. #else
  1825. #endif
  1826. #if defined(MBEDTLS_ARC4_C)
  1827. #include <string.h>
  1828. #if defined(MBEDTLS_SELF_TEST)
  1829. #if defined(MBEDTLS_PLATFORM_C)
  1830. #else
  1831. #include <stdio.h>
  1832. #define mbedtls_printf printf
  1833. #endif /* MBEDTLS_PLATFORM_C */
  1834. #endif /* MBEDTLS_SELF_TEST */
  1835. #if !defined(MBEDTLS_ARC4_ALT)
  1836. /* Implementation that should never be optimized out by the compiler */
  1837. /* zeroize was here */
  1838. void mbedtls_arc4_init( mbedtls_arc4_context *ctx )
  1839. {
  1840. memset( ctx, 0, sizeof( mbedtls_arc4_context ) );
  1841. }
  1842. void mbedtls_arc4_free( mbedtls_arc4_context *ctx )
  1843. {
  1844. if( ctx == NULL )
  1845. return;
  1846. mbedtls_zeroize( ctx, sizeof( mbedtls_arc4_context ) );
  1847. }
  1848. /*
  1849. * ARC4 key schedule
  1850. */
  1851. void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key,
  1852. unsigned int keylen )
  1853. {
  1854. int i, j, a;
  1855. unsigned int k;
  1856. unsigned char *m;
  1857. ctx->x = 0;
  1858. ctx->y = 0;
  1859. m = ctx->m;
  1860. for( i = 0; i < 256; i++ )
  1861. m[i] = (unsigned char) i;
  1862. j = k = 0;
  1863. for( i = 0; i < 256; i++, k++ )
  1864. {
  1865. if( k >= keylen ) k = 0;
  1866. a = m[i];
  1867. j = ( j + a + key[k] ) & 0xFF;
  1868. m[i] = m[j];
  1869. m[j] = (unsigned char) a;
  1870. }
  1871. }
  1872. /*
  1873. * ARC4 cipher function
  1874. */
  1875. int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned char *input,
  1876. unsigned char *output )
  1877. {
  1878. int x, y, a, b;
  1879. size_t i;
  1880. unsigned char *m;
  1881. x = ctx->x;
  1882. y = ctx->y;
  1883. m = ctx->m;
  1884. for( i = 0; i < length; i++ )
  1885. {
  1886. x = ( x + 1 ) & 0xFF; a = m[x];
  1887. y = ( y + a ) & 0xFF; b = m[y];
  1888. m[x] = (unsigned char) b;
  1889. m[y] = (unsigned char) a;
  1890. output[i] = (unsigned char)
  1891. ( input[i] ^ m[(unsigned char)( a + b )] );
  1892. }
  1893. ctx->x = x;
  1894. ctx->y = y;
  1895. return( 0 );
  1896. }
  1897. #endif /* !MBEDTLS_ARC4_ALT */
  1898. #if defined(MBEDTLS_SELF_TEST)
  1899. /*
  1900. * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
  1901. *
  1902. * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
  1903. */
  1904. static const unsigned char arc4_test_key[3][8] =
  1905. {
  1906. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
  1907. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
  1908. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  1909. };
  1910. static const unsigned char arc4_test_pt[3][8] =
  1911. {
  1912. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
  1913. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  1914. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  1915. };
  1916. static const unsigned char arc4_test_ct[3][8] =
  1917. {
  1918. { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
  1919. { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
  1920. { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
  1921. };
  1922. /*
  1923. * Checkup routine
  1924. */
  1925. int mbedtls_arc4_self_test( int verbose )
  1926. {
  1927. int i, ret = 0;
  1928. unsigned char ibuf[8];
  1929. unsigned char obuf[8];
  1930. mbedtls_arc4_context ctx;
  1931. mbedtls_arc4_init( &ctx );
  1932. for( i = 0; i < 3; i++ )
  1933. {
  1934. if( verbose != 0 )
  1935. mbedtls_printf( " ARC4 test #%d: ", i + 1 );
  1936. memcpy( ibuf, arc4_test_pt[i], 8 );
  1937. mbedtls_arc4_setup( &ctx, arc4_test_key[i], 8 );
  1938. mbedtls_arc4_crypt( &ctx, 8, ibuf, obuf );
  1939. if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
  1940. {
  1941. if( verbose != 0 )
  1942. mbedtls_printf( "failed\n" );
  1943. ret = 1;
  1944. goto exit;
  1945. }
  1946. if( verbose != 0 )
  1947. mbedtls_printf( "passed\n" );
  1948. }
  1949. if( verbose != 0 )
  1950. mbedtls_printf( "\n" );
  1951. exit:
  1952. mbedtls_arc4_free( &ctx );
  1953. return( ret );
  1954. }
  1955. #endif /* MBEDTLS_SELF_TEST */
  1956. #endif /* MBEDTLS_ARC4_C */
  1957. /*
  1958. Amalgamated build undefines
  1959. */
  1960. #undef ADD
  1961. #undef BC
  1962. #undef BEFORE_COLON
  1963. #undef F
  1964. #undef F0
  1965. #undef F1
  1966. #undef F2
  1967. #undef F3
  1968. #undef F4
  1969. #undef F5
  1970. #undef FSb
  1971. #undef K
  1972. #undef KK
  1973. #undef P
  1974. #undef R
  1975. #undef ROTR
  1976. #undef S
  1977. #undef S0
  1978. #undef S1
  1979. #undef S2
  1980. #undef S3
  1981. #undef SAFE_SNPRINTF
  1982. #undef SHR
  1983. #undef close
  1984. #undef read
  1985. #undef supported_init
  1986. #undef write
  1987. /********* Start of file library/asn1parse.c ************/
  1988. /*
  1989. * Generic ASN.1 parsing
  1990. *
  1991. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  1992. * SPDX-License-Identifier: Apache-2.0
  1993. *
  1994. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  1995. * not use this file except in compliance with the License.
  1996. * You may obtain a copy of the License at
  1997. *
  1998. * http://www.apache.org/licenses/LICENSE-2.0
  1999. *
  2000. * Unless required by applicable law or agreed to in writing, software
  2001. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  2002. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2003. * See the License for the specific language governing permissions and
  2004. * limitations under the License.
  2005. *
  2006. * This file is part of mbed TLS (https://tls.mbed.org)
  2007. */
  2008. #if !defined(MBEDTLS_CONFIG_FILE)
  2009. #else
  2010. #endif
  2011. #if defined(MBEDTLS_ASN1_PARSE_C)
  2012. #include <string.h>
  2013. #if defined(MBEDTLS_BIGNUM_C)
  2014. #endif
  2015. #if defined(MBEDTLS_PLATFORM_C)
  2016. #else
  2017. #include <stdlib.h>
  2018. #define mbedtls_calloc calloc
  2019. #define mbedtls_free free
  2020. #endif
  2021. /* Implementation that should never be optimized out by the compiler */
  2022. /* zeroize was here */
  2023. /*
  2024. * ASN.1 DER decoding routines
  2025. */
  2026. int mbedtls_asn1_get_len( unsigned char **p,
  2027. const unsigned char *end,
  2028. size_t *len )
  2029. {
  2030. if( ( end - *p ) < 1 )
  2031. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  2032. if( ( **p & 0x80 ) == 0 )
  2033. *len = *(*p)++;
  2034. else
  2035. {
  2036. switch( **p & 0x7F )
  2037. {
  2038. case 1:
  2039. if( ( end - *p ) < 2 )
  2040. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  2041. *len = (*p)[1];
  2042. (*p) += 2;
  2043. break;
  2044. case 2:
  2045. if( ( end - *p ) < 3 )
  2046. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  2047. *len = ( (size_t)(*p)[1] << 8 ) | (*p)[2];
  2048. (*p) += 3;
  2049. break;
  2050. case 3:
  2051. if( ( end - *p ) < 4 )
  2052. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  2053. *len = ( (size_t)(*p)[1] << 16 ) |
  2054. ( (size_t)(*p)[2] << 8 ) | (*p)[3];
  2055. (*p) += 4;
  2056. break;
  2057. case 4:
  2058. if( ( end - *p ) < 5 )
  2059. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  2060. *len = ( (size_t)(*p)[1] << 24 ) | ( (size_t)(*p)[2] << 16 ) |
  2061. ( (size_t)(*p)[3] << 8 ) | (*p)[4];
  2062. (*p) += 5;
  2063. break;
  2064. default:
  2065. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  2066. }
  2067. }
  2068. if( *len > (size_t) ( end - *p ) )
  2069. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  2070. return( 0 );
  2071. }
  2072. int mbedtls_asn1_get_tag( unsigned char **p,
  2073. const unsigned char *end,
  2074. size_t *len, int tag )
  2075. {
  2076. if( ( end - *p ) < 1 )
  2077. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  2078. if( **p != tag )
  2079. return( MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  2080. (*p)++;
  2081. return( mbedtls_asn1_get_len( p, end, len ) );
  2082. }
  2083. int mbedtls_asn1_get_bool( unsigned char **p,
  2084. const unsigned char *end,
  2085. int *val )
  2086. {
  2087. int ret;
  2088. size_t len;
  2089. if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_BOOLEAN ) ) != 0 )
  2090. return( ret );
  2091. if( len != 1 )
  2092. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  2093. *val = ( **p != 0 ) ? 1 : 0;
  2094. (*p)++;
  2095. return( 0 );
  2096. }
  2097. int mbedtls_asn1_get_int( unsigned char **p,
  2098. const unsigned char *end,
  2099. int *val )
  2100. {
  2101. int ret;
  2102. size_t len;
  2103. if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
  2104. return( ret );
  2105. if( len == 0 || len > sizeof( int ) || ( **p & 0x80 ) != 0 )
  2106. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  2107. *val = 0;
  2108. while( len-- > 0 )
  2109. {
  2110. *val = ( *val << 8 ) | **p;
  2111. (*p)++;
  2112. }
  2113. return( 0 );
  2114. }
  2115. #if defined(MBEDTLS_BIGNUM_C)
  2116. int mbedtls_asn1_get_mpi( unsigned char **p,
  2117. const unsigned char *end,
  2118. mbedtls_mpi *X )
  2119. {
  2120. int ret;
  2121. size_t len;
  2122. if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
  2123. return( ret );
  2124. ret = mbedtls_mpi_read_binary( X, *p, len );
  2125. *p += len;
  2126. return( ret );
  2127. }
  2128. #endif /* MBEDTLS_BIGNUM_C */
  2129. int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end,
  2130. mbedtls_asn1_bitstring *bs)
  2131. {
  2132. int ret;
  2133. /* Certificate type is a single byte bitstring */
  2134. if( ( ret = mbedtls_asn1_get_tag( p, end, &bs->len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 )
  2135. return( ret );
  2136. /* Check length, subtract one for actual bit string length */
  2137. if( bs->len < 1 )
  2138. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  2139. bs->len -= 1;
  2140. /* Get number of unused bits, ensure unused bits <= 7 */
  2141. bs->unused_bits = **p;
  2142. if( bs->unused_bits > 7 )
  2143. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  2144. (*p)++;
  2145. /* Get actual bitstring */
  2146. bs->p = *p;
  2147. *p += bs->len;
  2148. if( *p != end )
  2149. return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  2150. return( 0 );
  2151. }
  2152. /*
  2153. * Get a bit string without unused bits
  2154. */
  2155. int mbedtls_asn1_get_bitstring_null( unsigned char **p, const unsigned char *end,
  2156. size_t *len )
  2157. {
  2158. int ret;
  2159. if( ( ret = mbedtls_asn1_get_tag( p, end, len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 )
  2160. return( ret );
  2161. if( (*len)-- < 2 || *(*p)++ != 0 )
  2162. return( MBEDTLS_ERR_ASN1_INVALID_DATA );
  2163. return( 0 );
  2164. }
  2165. /*
  2166. * Parses and splits an ASN.1 "SEQUENCE OF <tag>"
  2167. */
  2168. int mbedtls_asn1_get_sequence_of( unsigned char **p,
  2169. const unsigned char *end,
  2170. mbedtls_asn1_sequence *cur,
  2171. int tag)
  2172. {
  2173. int ret;
  2174. size_t len;
  2175. mbedtls_asn1_buf *buf;
  2176. /* Get main sequence tag */
  2177. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  2178. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  2179. return( ret );
  2180. if( *p + len != end )
  2181. return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  2182. while( *p < end )
  2183. {
  2184. buf = &(cur->buf);
  2185. buf->tag = **p;
  2186. if( ( ret = mbedtls_asn1_get_tag( p, end, &buf->len, tag ) ) != 0 )
  2187. return( ret );
  2188. buf->p = *p;
  2189. *p += buf->len;
  2190. /* Allocate and assign next pointer */
  2191. if( *p < end )
  2192. {
  2193. cur->next = (mbedtls_asn1_sequence*)mbedtls_calloc( 1,
  2194. sizeof( mbedtls_asn1_sequence ) );
  2195. if( cur->next == NULL )
  2196. return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
  2197. cur = cur->next;
  2198. }
  2199. }
  2200. /* Set final sequence entry's next pointer to NULL */
  2201. cur->next = NULL;
  2202. if( *p != end )
  2203. return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  2204. return( 0 );
  2205. }
  2206. int mbedtls_asn1_get_alg( unsigned char **p,
  2207. const unsigned char *end,
  2208. mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params )
  2209. {
  2210. int ret;
  2211. size_t len;
  2212. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  2213. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  2214. return( ret );
  2215. if( ( end - *p ) < 1 )
  2216. return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  2217. alg->tag = **p;
  2218. end = *p + len;
  2219. if( ( ret = mbedtls_asn1_get_tag( p, end, &alg->len, MBEDTLS_ASN1_OID ) ) != 0 )
  2220. return( ret );
  2221. alg->p = *p;
  2222. *p += alg->len;
  2223. if( *p == end )
  2224. {
  2225. mbedtls_zeroize( params, sizeof(mbedtls_asn1_buf) );
  2226. return( 0 );
  2227. }
  2228. params->tag = **p;
  2229. (*p)++;
  2230. if( ( ret = mbedtls_asn1_get_len( p, end, &params->len ) ) != 0 )
  2231. return( ret );
  2232. params->p = *p;
  2233. *p += params->len;
  2234. if( *p != end )
  2235. return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  2236. return( 0 );
  2237. }
  2238. int mbedtls_asn1_get_alg_null( unsigned char **p,
  2239. const unsigned char *end,
  2240. mbedtls_asn1_buf *alg )
  2241. {
  2242. int ret;
  2243. mbedtls_asn1_buf params;
  2244. memset( &params, 0, sizeof(mbedtls_asn1_buf) );
  2245. if( ( ret = mbedtls_asn1_get_alg( p, end, alg, &params ) ) != 0 )
  2246. return( ret );
  2247. if( ( params.tag != MBEDTLS_ASN1_NULL && params.tag != 0 ) || params.len != 0 )
  2248. return( MBEDTLS_ERR_ASN1_INVALID_DATA );
  2249. return( 0 );
  2250. }
  2251. void mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *cur )
  2252. {
  2253. if( cur == NULL )
  2254. return;
  2255. mbedtls_free( cur->oid.p );
  2256. mbedtls_free( cur->val.p );
  2257. mbedtls_zeroize( cur, sizeof( mbedtls_asn1_named_data ) );
  2258. }
  2259. void mbedtls_asn1_free_named_data_list( mbedtls_asn1_named_data **head )
  2260. {
  2261. mbedtls_asn1_named_data *cur;
  2262. while( ( cur = *head ) != NULL )
  2263. {
  2264. *head = cur->next;
  2265. mbedtls_asn1_free_named_data( cur );
  2266. mbedtls_free( cur );
  2267. }
  2268. }
  2269. mbedtls_asn1_named_data *mbedtls_asn1_find_named_data( mbedtls_asn1_named_data *list,
  2270. const char *oid, size_t len )
  2271. {
  2272. while( list != NULL )
  2273. {
  2274. if( list->oid.len == len &&
  2275. memcmp( list->oid.p, oid, len ) == 0 )
  2276. {
  2277. break;
  2278. }
  2279. list = list->next;
  2280. }
  2281. return( list );
  2282. }
  2283. #endif /* MBEDTLS_ASN1_PARSE_C */
  2284. /*
  2285. Amalgamated build undefines
  2286. */
  2287. #undef ADD
  2288. #undef BC
  2289. #undef BEFORE_COLON
  2290. #undef F
  2291. #undef F0
  2292. #undef F1
  2293. #undef F2
  2294. #undef F3
  2295. #undef F4
  2296. #undef F5
  2297. #undef FSb
  2298. #undef K
  2299. #undef KK
  2300. #undef P
  2301. #undef R
  2302. #undef ROTR
  2303. #undef S
  2304. #undef S0
  2305. #undef S1
  2306. #undef S2
  2307. #undef S3
  2308. #undef SAFE_SNPRINTF
  2309. #undef SHR
  2310. #undef close
  2311. #undef read
  2312. #undef supported_init
  2313. #undef write
  2314. /********* Start of file library/asn1write.c ************/
  2315. /*
  2316. * ASN.1 buffer writing functionality
  2317. *
  2318. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  2319. * SPDX-License-Identifier: Apache-2.0
  2320. *
  2321. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  2322. * not use this file except in compliance with the License.
  2323. * You may obtain a copy of the License at
  2324. *
  2325. * http://www.apache.org/licenses/LICENSE-2.0
  2326. *
  2327. * Unless required by applicable law or agreed to in writing, software
  2328. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  2329. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2330. * See the License for the specific language governing permissions and
  2331. * limitations under the License.
  2332. *
  2333. * This file is part of mbed TLS (https://tls.mbed.org)
  2334. */
  2335. #if !defined(MBEDTLS_CONFIG_FILE)
  2336. #else
  2337. #endif
  2338. #if defined(MBEDTLS_ASN1_WRITE_C)
  2339. #include <string.h>
  2340. #if defined(MBEDTLS_PLATFORM_C)
  2341. #else
  2342. #include <stdlib.h>
  2343. #define mbedtls_calloc calloc
  2344. #define mbedtls_free free
  2345. #endif
  2346. int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
  2347. {
  2348. if( len < 0x80 )
  2349. {
  2350. if( *p - start < 1 )
  2351. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  2352. *--(*p) = (unsigned char) len;
  2353. return( 1 );
  2354. }
  2355. if( len <= 0xFF )
  2356. {
  2357. if( *p - start < 2 )
  2358. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  2359. *--(*p) = (unsigned char) len;
  2360. *--(*p) = 0x81;
  2361. return( 2 );
  2362. }
  2363. if( len <= 0xFFFF )
  2364. {
  2365. if( *p - start < 3 )
  2366. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  2367. *--(*p) = ( len ) & 0xFF;
  2368. *--(*p) = ( len >> 8 ) & 0xFF;
  2369. *--(*p) = 0x82;
  2370. return( 3 );
  2371. }
  2372. if( len <= 0xFFFFFF )
  2373. {
  2374. if( *p - start < 4 )
  2375. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  2376. *--(*p) = ( len ) & 0xFF;
  2377. *--(*p) = ( len >> 8 ) & 0xFF;
  2378. *--(*p) = ( len >> 16 ) & 0xFF;
  2379. *--(*p) = 0x83;
  2380. return( 4 );
  2381. }
  2382. if( len <= 0xFFFFFFFF )
  2383. {
  2384. if( *p - start < 5 )
  2385. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  2386. *--(*p) = ( len ) & 0xFF;
  2387. *--(*p) = ( len >> 8 ) & 0xFF;
  2388. *--(*p) = ( len >> 16 ) & 0xFF;
  2389. *--(*p) = ( len >> 24 ) & 0xFF;
  2390. *--(*p) = 0x84;
  2391. return( 5 );
  2392. }
  2393. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  2394. }
  2395. int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
  2396. {
  2397. if( *p - start < 1 )
  2398. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  2399. *--(*p) = tag;
  2400. return( 1 );
  2401. }
  2402. int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
  2403. const unsigned char *buf, size_t size )
  2404. {
  2405. size_t len = 0;
  2406. if( *p < start || (size_t)( *p - start ) < size )
  2407. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  2408. len = size;
  2409. (*p) -= len;
  2410. memcpy( *p, buf, len );
  2411. return( (int) len );
  2412. }
  2413. #if defined(MBEDTLS_BIGNUM_C)
  2414. int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
  2415. {
  2416. int ret;
  2417. size_t len = 0;
  2418. // Write the MPI
  2419. //
  2420. len = mbedtls_mpi_size( X );
  2421. if( *p < start || (size_t)( *p - start ) < len )
  2422. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  2423. (*p) -= len;
  2424. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
  2425. // DER format assumes 2s complement for numbers, so the leftmost bit
  2426. // should be 0 for positive numbers and 1 for negative numbers.
  2427. //
  2428. if( X->s ==1 && **p & 0x80 )
  2429. {
  2430. if( *p - start < 1 )
  2431. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  2432. *--(*p) = 0x00;
  2433. len += 1;
  2434. }
  2435. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  2436. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
  2437. ret = (int) len;
  2438. cleanup:
  2439. return( ret );
  2440. }
  2441. #endif /* MBEDTLS_BIGNUM_C */
  2442. int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
  2443. {
  2444. int ret;
  2445. size_t len = 0;
  2446. // Write NULL
  2447. //
  2448. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
  2449. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
  2450. return( (int) len );
  2451. }
  2452. int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
  2453. const char *oid, size_t oid_len )
  2454. {
  2455. int ret;
  2456. size_t len = 0;
  2457. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
  2458. (const unsigned char *) oid, oid_len ) );
  2459. MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) );
  2460. MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
  2461. return( (int) len );
  2462. }
  2463. int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
  2464. const char *oid, size_t oid_len,
  2465. size_t par_len )
  2466. {
  2467. int ret;
  2468. size_t len = 0;
  2469. if( par_len == 0 )
  2470. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
  2471. else
  2472. len += par_len;
  2473. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
  2474. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  2475. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
  2476. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  2477. return( (int) len );
  2478. }
  2479. int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
  2480. {
  2481. int ret;
  2482. size_t len = 0;
  2483. if( *p - start < 1 )
  2484. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  2485. *--(*p) = (boolean) ? 255 : 0;
  2486. len++;
  2487. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  2488. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) );
  2489. return( (int) len );
  2490. }
  2491. int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
  2492. {
  2493. int ret;
  2494. size_t len = 0;
  2495. // TODO negative values and values larger than 128
  2496. // DER format assumes 2s complement for numbers, so the leftmost bit
  2497. // should be 0 for positive numbers and 1 for negative numbers.
  2498. //
  2499. if( *p - start < 1 )
  2500. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  2501. len += 1;
  2502. *--(*p) = val;
  2503. if( val > 0 && **p & 0x80 )
  2504. {
  2505. if( *p - start < 1 )
  2506. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  2507. *--(*p) = 0x00;
  2508. len += 1;
  2509. }
  2510. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  2511. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
  2512. return( (int) len );
  2513. }
  2514. int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
  2515. const char *text, size_t text_len )
  2516. {
  2517. int ret;
  2518. size_t len = 0;
  2519. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
  2520. (const unsigned char *) text, text_len ) );
  2521. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  2522. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_PRINTABLE_STRING ) );
  2523. return( (int) len );
  2524. }
  2525. int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
  2526. const char *text, size_t text_len )
  2527. {
  2528. int ret;
  2529. size_t len = 0;
  2530. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
  2531. (const unsigned char *) text, text_len ) );
  2532. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  2533. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_IA5_STRING ) );
  2534. return( (int) len );
  2535. }
  2536. int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
  2537. const unsigned char *buf, size_t bits )
  2538. {
  2539. int ret;
  2540. size_t len = 0, size;
  2541. size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
  2542. // Calculate byte length
  2543. //
  2544. if( *p < start || (size_t)( *p - start ) < size + 1 )
  2545. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  2546. len = size + 1;
  2547. (*p) -= size;
  2548. memcpy( *p, buf, size );
  2549. // Write unused bits
  2550. //
  2551. *--(*p) = (unsigned char) (size * 8 - bits);
  2552. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  2553. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
  2554. return( (int) len );
  2555. }
  2556. int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
  2557. const unsigned char *buf, size_t size )
  2558. {
  2559. int ret;
  2560. size_t len = 0;
  2561. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
  2562. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  2563. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
  2564. return( (int) len );
  2565. }
  2566. mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **head,
  2567. const char *oid, size_t oid_len,
  2568. const unsigned char *val,
  2569. size_t val_len )
  2570. {
  2571. mbedtls_asn1_named_data *cur;
  2572. if( ( cur = mbedtls_asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
  2573. {
  2574. // Add new entry if not present yet based on OID
  2575. //
  2576. cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
  2577. sizeof(mbedtls_asn1_named_data) );
  2578. if( cur == NULL )
  2579. return( NULL );
  2580. cur->oid.len = oid_len;
  2581. cur->oid.p = mbedtls_calloc( 1, oid_len );
  2582. if( cur->oid.p == NULL )
  2583. {
  2584. mbedtls_free( cur );
  2585. return( NULL );
  2586. }
  2587. memcpy( cur->oid.p, oid, oid_len );
  2588. cur->val.len = val_len;
  2589. cur->val.p = mbedtls_calloc( 1, val_len );
  2590. if( cur->val.p == NULL )
  2591. {
  2592. mbedtls_free( cur->oid.p );
  2593. mbedtls_free( cur );
  2594. return( NULL );
  2595. }
  2596. cur->next = *head;
  2597. *head = cur;
  2598. }
  2599. else if( cur->val.len < val_len )
  2600. {
  2601. /*
  2602. * Enlarge existing value buffer if needed
  2603. * Preserve old data until the allocation succeeded, to leave list in
  2604. * a consistent state in case allocation fails.
  2605. */
  2606. void *p = mbedtls_calloc( 1, val_len );
  2607. if( p == NULL )
  2608. return( NULL );
  2609. mbedtls_free( cur->val.p );
  2610. cur->val.p = p;
  2611. cur->val.len = val_len;
  2612. }
  2613. if( val != NULL )
  2614. memcpy( cur->val.p, val, val_len );
  2615. return( cur );
  2616. }
  2617. #endif /* MBEDTLS_ASN1_WRITE_C */
  2618. /*
  2619. Amalgamated build undefines
  2620. */
  2621. #undef ADD
  2622. #undef BC
  2623. #undef BEFORE_COLON
  2624. #undef F
  2625. #undef F0
  2626. #undef F1
  2627. #undef F2
  2628. #undef F3
  2629. #undef F4
  2630. #undef F5
  2631. #undef FSb
  2632. #undef K
  2633. #undef KK
  2634. #undef P
  2635. #undef R
  2636. #undef ROTR
  2637. #undef S
  2638. #undef S0
  2639. #undef S1
  2640. #undef S2
  2641. #undef S3
  2642. #undef SAFE_SNPRINTF
  2643. #undef SHR
  2644. #undef close
  2645. #undef read
  2646. #undef supported_init
  2647. #undef write
  2648. /********* Start of file library/base64.c ************/
  2649. /*
  2650. * RFC 1521 base64 encoding/decoding
  2651. *
  2652. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  2653. * SPDX-License-Identifier: Apache-2.0
  2654. *
  2655. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  2656. * not use this file except in compliance with the License.
  2657. * You may obtain a copy of the License at
  2658. *
  2659. * http://www.apache.org/licenses/LICENSE-2.0
  2660. *
  2661. * Unless required by applicable law or agreed to in writing, software
  2662. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  2663. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2664. * See the License for the specific language governing permissions and
  2665. * limitations under the License.
  2666. *
  2667. * This file is part of mbed TLS (https://tls.mbed.org)
  2668. */
  2669. #if !defined(MBEDTLS_CONFIG_FILE)
  2670. #else
  2671. #endif
  2672. #if defined(MBEDTLS_BASE64_C)
  2673. #include <stdint.h>
  2674. #if defined(MBEDTLS_SELF_TEST)
  2675. #include <string.h>
  2676. #if defined(MBEDTLS_PLATFORM_C)
  2677. #else
  2678. #include <stdio.h>
  2679. #define mbedtls_printf printf
  2680. #endif /* MBEDTLS_PLATFORM_C */
  2681. #endif /* MBEDTLS_SELF_TEST */
  2682. static const unsigned char base64_enc_map[64] =
  2683. {
  2684. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  2685. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  2686. 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
  2687. 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  2688. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
  2689. 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
  2690. '8', '9', '+', '/'
  2691. };
  2692. static const unsigned char base64_dec_map[128] =
  2693. {
  2694. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  2695. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  2696. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  2697. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  2698. 127, 127, 127, 62, 127, 127, 127, 63, 52, 53,
  2699. 54, 55, 56, 57, 58, 59, 60, 61, 127, 127,
  2700. 127, 64, 127, 127, 127, 0, 1, 2, 3, 4,
  2701. 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  2702. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  2703. 25, 127, 127, 127, 127, 127, 127, 26, 27, 28,
  2704. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  2705. 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  2706. 49, 50, 51, 127, 127, 127, 127, 127
  2707. };
  2708. #define BASE64_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
  2709. /*
  2710. * Encode a buffer into base64 format
  2711. */
  2712. int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
  2713. const unsigned char *src, size_t slen )
  2714. {
  2715. size_t i, n;
  2716. int C1, C2, C3;
  2717. unsigned char *p;
  2718. if( slen == 0 )
  2719. {
  2720. *olen = 0;
  2721. return( 0 );
  2722. }
  2723. n = slen / 3 + ( slen % 3 != 0 );
  2724. if( n > ( BASE64_SIZE_T_MAX - 1 ) / 4 )
  2725. {
  2726. *olen = BASE64_SIZE_T_MAX;
  2727. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  2728. }
  2729. n *= 4;
  2730. if( ( dlen < n + 1 ) || ( NULL == dst ) )
  2731. {
  2732. *olen = n + 1;
  2733. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  2734. }
  2735. n = ( slen / 3 ) * 3;
  2736. for( i = 0, p = dst; i < n; i += 3 )
  2737. {
  2738. C1 = *src++;
  2739. C2 = *src++;
  2740. C3 = *src++;
  2741. *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
  2742. *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
  2743. *p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
  2744. *p++ = base64_enc_map[C3 & 0x3F];
  2745. }
  2746. if( i < slen )
  2747. {
  2748. C1 = *src++;
  2749. C2 = ( ( i + 1 ) < slen ) ? *src++ : 0;
  2750. *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
  2751. *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
  2752. if( ( i + 1 ) < slen )
  2753. *p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
  2754. else *p++ = '=';
  2755. *p++ = '=';
  2756. }
  2757. *olen = p - dst;
  2758. *p = 0;
  2759. return( 0 );
  2760. }
  2761. /*
  2762. * Decode a base64-formatted buffer
  2763. */
  2764. int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
  2765. const unsigned char *src, size_t slen )
  2766. {
  2767. size_t i, n;
  2768. uint32_t j, x;
  2769. unsigned char *p;
  2770. /* First pass: check for validity and get output length */
  2771. for( i = n = j = 0; i < slen; i++ )
  2772. {
  2773. /* Skip spaces before checking for EOL */
  2774. x = 0;
  2775. while( i < slen && src[i] == ' ' )
  2776. {
  2777. ++i;
  2778. ++x;
  2779. }
  2780. /* Spaces at end of buffer are OK */
  2781. if( i == slen )
  2782. break;
  2783. if( ( slen - i ) >= 2 &&
  2784. src[i] == '\r' && src[i + 1] == '\n' )
  2785. continue;
  2786. if( src[i] == '\n' )
  2787. continue;
  2788. /* Space inside a line is an error */
  2789. if( x != 0 )
  2790. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  2791. if( src[i] == '=' && ++j > 2 )
  2792. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  2793. if( src[i] > 127 || base64_dec_map[src[i]] == 127 )
  2794. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  2795. if( base64_dec_map[src[i]] < 64 && j != 0 )
  2796. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  2797. n++;
  2798. }
  2799. if( n == 0 )
  2800. {
  2801. *olen = 0;
  2802. return( 0 );
  2803. }
  2804. /* The following expression is to calculate the following formula without
  2805. * risk of integer overflow in n:
  2806. * n = ( ( n * 6 ) + 7 ) >> 3;
  2807. */
  2808. n = ( 6 * ( n >> 3 ) ) + ( ( 6 * ( n & 0x7 ) + 7 ) >> 3 );
  2809. n -= j;
  2810. if( dst == NULL || dlen < n )
  2811. {
  2812. *olen = n;
  2813. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  2814. }
  2815. for( j = 3, n = x = 0, p = dst; i > 0; i--, src++ )
  2816. {
  2817. if( *src == '\r' || *src == '\n' || *src == ' ' )
  2818. continue;
  2819. j -= ( base64_dec_map[*src] == 64 );
  2820. x = ( x << 6 ) | ( base64_dec_map[*src] & 0x3F );
  2821. if( ++n == 4 )
  2822. {
  2823. n = 0;
  2824. if( j > 0 ) *p++ = (unsigned char)( x >> 16 );
  2825. if( j > 1 ) *p++ = (unsigned char)( x >> 8 );
  2826. if( j > 2 ) *p++ = (unsigned char)( x );
  2827. }
  2828. }
  2829. *olen = p - dst;
  2830. return( 0 );
  2831. }
  2832. #if defined(MBEDTLS_SELF_TEST)
  2833. static const unsigned char base64_test_dec[64] =
  2834. {
  2835. 0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD,
  2836. 0xBF, 0x17, 0xD9, 0xA2, 0xC4, 0x17, 0x1A, 0x01,
  2837. 0x94, 0xED, 0x8F, 0x1E, 0x11, 0xB3, 0xD7, 0x09,
  2838. 0x0C, 0xB6, 0xE9, 0x10, 0x6F, 0x22, 0xEE, 0x13,
  2839. 0xCA, 0xB3, 0x07, 0x05, 0x76, 0xC9, 0xFA, 0x31,
  2840. 0x6C, 0x08, 0x34, 0xFF, 0x8D, 0xC2, 0x6C, 0x38,
  2841. 0x00, 0x43, 0xE9, 0x54, 0x97, 0xAF, 0x50, 0x4B,
  2842. 0xD1, 0x41, 0xBA, 0x95, 0x31, 0x5A, 0x0B, 0x97
  2843. };
  2844. static const unsigned char base64_test_enc[] =
  2845. "JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK"
  2846. "swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
  2847. /*
  2848. * Checkup routine
  2849. */
  2850. int mbedtls_base64_self_test( int verbose )
  2851. {
  2852. size_t len;
  2853. const unsigned char *src;
  2854. unsigned char buffer[128];
  2855. if( verbose != 0 )
  2856. mbedtls_printf( " Base64 encoding test: " );
  2857. src = base64_test_dec;
  2858. if( mbedtls_base64_encode( buffer, sizeof( buffer ), &len, src, 64 ) != 0 ||
  2859. memcmp( base64_test_enc, buffer, 88 ) != 0 )
  2860. {
  2861. if( verbose != 0 )
  2862. mbedtls_printf( "failed\n" );
  2863. return( 1 );
  2864. }
  2865. if( verbose != 0 )
  2866. mbedtls_printf( "passed\n Base64 decoding test: " );
  2867. src = base64_test_enc;
  2868. if( mbedtls_base64_decode( buffer, sizeof( buffer ), &len, src, 88 ) != 0 ||
  2869. memcmp( base64_test_dec, buffer, 64 ) != 0 )
  2870. {
  2871. if( verbose != 0 )
  2872. mbedtls_printf( "failed\n" );
  2873. return( 1 );
  2874. }
  2875. if( verbose != 0 )
  2876. mbedtls_printf( "passed\n\n" );
  2877. return( 0 );
  2878. }
  2879. #endif /* MBEDTLS_SELF_TEST */
  2880. #endif /* MBEDTLS_BASE64_C */
  2881. /*
  2882. Amalgamated build undefines
  2883. */
  2884. #undef ADD
  2885. #undef BC
  2886. #undef BEFORE_COLON
  2887. #undef F
  2888. #undef F0
  2889. #undef F1
  2890. #undef F2
  2891. #undef F3
  2892. #undef F4
  2893. #undef F5
  2894. #undef FSb
  2895. #undef K
  2896. #undef KK
  2897. #undef P
  2898. #undef R
  2899. #undef ROTR
  2900. #undef S
  2901. #undef S0
  2902. #undef S1
  2903. #undef S2
  2904. #undef S3
  2905. #undef SAFE_SNPRINTF
  2906. #undef SHR
  2907. #undef close
  2908. #undef read
  2909. #undef supported_init
  2910. #undef write
  2911. /********* Start of file library/bignum.c ************/
  2912. /*
  2913. * Multi-precision integer library
  2914. *
  2915. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  2916. * SPDX-License-Identifier: Apache-2.0
  2917. *
  2918. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  2919. * not use this file except in compliance with the License.
  2920. * You may obtain a copy of the License at
  2921. *
  2922. * http://www.apache.org/licenses/LICENSE-2.0
  2923. *
  2924. * Unless required by applicable law or agreed to in writing, software
  2925. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  2926. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2927. * See the License for the specific language governing permissions and
  2928. * limitations under the License.
  2929. *
  2930. * This file is part of mbed TLS (https://tls.mbed.org)
  2931. */
  2932. /*
  2933. * The following sources were referenced in the design of this Multi-precision
  2934. * Integer library:
  2935. *
  2936. * [1] Handbook of Applied Cryptography - 1997
  2937. * Menezes, van Oorschot and Vanstone
  2938. *
  2939. * [2] Multi-Precision Math
  2940. * Tom St Denis
  2941. * https://github.com/libtom/libtommath/blob/develop/tommath.pdf
  2942. *
  2943. * [3] GNU Multi-Precision Arithmetic Library
  2944. * https://gmplib.org/manual/index.html
  2945. *
  2946. */
  2947. #if !defined(MBEDTLS_CONFIG_FILE)
  2948. #else
  2949. #endif
  2950. #if defined(MBEDTLS_BIGNUM_C)
  2951. #include <string.h>
  2952. #if defined(MBEDTLS_PLATFORM_C)
  2953. #else
  2954. #include <stdio.h>
  2955. #include <stdlib.h>
  2956. #define mbedtls_printf printf
  2957. #define mbedtls_calloc calloc
  2958. #define mbedtls_free free
  2959. #endif
  2960. /* Implementation that should never be optimized out by the compiler */
  2961. static void mbedtls_mpi_zeroize( mbedtls_mpi_uint *v, size_t n ) {
  2962. volatile mbedtls_mpi_uint *p = v; while( n-- ) *p++ = 0;
  2963. }
  2964. /* Implementation that should never be optimized out by the compiler */
  2965. /* zeroize was here */
  2966. #define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
  2967. #define biL (ciL << 3) /* bits in limb */
  2968. #define biH (ciL << 2) /* half limb size */
  2969. #define MPI_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
  2970. /*
  2971. * Convert between bits/chars and number of limbs
  2972. * Divide first in order to avoid potential overflows
  2973. */
  2974. #define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
  2975. #define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
  2976. /*
  2977. * Initialize one MPI
  2978. */
  2979. void mbedtls_mpi_init( mbedtls_mpi *X )
  2980. {
  2981. if( X == NULL )
  2982. return;
  2983. X->s = 1;
  2984. X->n = 0;
  2985. X->p = NULL;
  2986. }
  2987. /*
  2988. * Unallocate one MPI
  2989. */
  2990. void mbedtls_mpi_free( mbedtls_mpi *X )
  2991. {
  2992. if( X == NULL )
  2993. return;
  2994. if( X->p != NULL )
  2995. {
  2996. mbedtls_mpi_zeroize( X->p, X->n );
  2997. mbedtls_free( X->p );
  2998. }
  2999. X->s = 1;
  3000. X->n = 0;
  3001. X->p = NULL;
  3002. }
  3003. /*
  3004. * Enlarge to the specified number of limbs
  3005. */
  3006. int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs )
  3007. {
  3008. mbedtls_mpi_uint *p;
  3009. if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
  3010. return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
  3011. if( X->n < nblimbs )
  3012. {
  3013. if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( nblimbs, ciL ) ) == NULL )
  3014. return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
  3015. if( X->p != NULL )
  3016. {
  3017. memcpy( p, X->p, X->n * ciL );
  3018. mbedtls_mpi_zeroize( X->p, X->n );
  3019. mbedtls_free( X->p );
  3020. }
  3021. X->n = nblimbs;
  3022. X->p = p;
  3023. }
  3024. return( 0 );
  3025. }
  3026. /*
  3027. * Resize down as much as possible,
  3028. * while keeping at least the specified number of limbs
  3029. */
  3030. int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs )
  3031. {
  3032. mbedtls_mpi_uint *p;
  3033. size_t i;
  3034. /* Actually resize up in this case */
  3035. if( X->n <= nblimbs )
  3036. return( mbedtls_mpi_grow( X, nblimbs ) );
  3037. for( i = X->n - 1; i > 0; i-- )
  3038. if( X->p[i] != 0 )
  3039. break;
  3040. i++;
  3041. if( i < nblimbs )
  3042. i = nblimbs;
  3043. if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( i, ciL ) ) == NULL )
  3044. return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
  3045. if( X->p != NULL )
  3046. {
  3047. memcpy( p, X->p, i * ciL );
  3048. mbedtls_mpi_zeroize( X->p, X->n );
  3049. mbedtls_free( X->p );
  3050. }
  3051. X->n = i;
  3052. X->p = p;
  3053. return( 0 );
  3054. }
  3055. /*
  3056. * Copy the contents of Y into X
  3057. */
  3058. int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y )
  3059. {
  3060. int ret;
  3061. size_t i;
  3062. if( X == Y )
  3063. return( 0 );
  3064. if( Y->p == NULL )
  3065. {
  3066. mbedtls_mpi_free( X );
  3067. return( 0 );
  3068. }
  3069. for( i = Y->n - 1; i > 0; i-- )
  3070. if( Y->p[i] != 0 )
  3071. break;
  3072. i++;
  3073. X->s = Y->s;
  3074. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) );
  3075. memset( X->p, 0, X->n * ciL );
  3076. memcpy( X->p, Y->p, i * ciL );
  3077. cleanup:
  3078. return( ret );
  3079. }
  3080. /*
  3081. * Swap the contents of X and Y
  3082. */
  3083. void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y )
  3084. {
  3085. mbedtls_mpi T;
  3086. memcpy( &T, X, sizeof( mbedtls_mpi ) );
  3087. memcpy( X, Y, sizeof( mbedtls_mpi ) );
  3088. memcpy( Y, &T, sizeof( mbedtls_mpi ) );
  3089. }
  3090. /*
  3091. * Conditionally assign X = Y, without leaking information
  3092. * about whether the assignment was made or not.
  3093. * (Leaking information about the respective sizes of X and Y is ok however.)
  3094. */
  3095. int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign )
  3096. {
  3097. int ret = 0;
  3098. size_t i;
  3099. /* make sure assign is 0 or 1 in a time-constant manner */
  3100. assign = (assign | (unsigned char)-assign) >> 7;
  3101. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
  3102. X->s = X->s * ( 1 - assign ) + Y->s * assign;
  3103. for( i = 0; i < Y->n; i++ )
  3104. X->p[i] = X->p[i] * ( 1 - assign ) + Y->p[i] * assign;
  3105. for( ; i < X->n; i++ )
  3106. X->p[i] *= ( 1 - assign );
  3107. cleanup:
  3108. return( ret );
  3109. }
  3110. /*
  3111. * Conditionally swap X and Y, without leaking information
  3112. * about whether the swap was made or not.
  3113. * Here it is not ok to simply swap the pointers, which whould lead to
  3114. * different memory access patterns when X and Y are used afterwards.
  3115. */
  3116. int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap )
  3117. {
  3118. int ret, s;
  3119. size_t i;
  3120. mbedtls_mpi_uint tmp;
  3121. if( X == Y )
  3122. return( 0 );
  3123. /* make sure swap is 0 or 1 in a time-constant manner */
  3124. swap = (swap | (unsigned char)-swap) >> 7;
  3125. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
  3126. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) );
  3127. s = X->s;
  3128. X->s = X->s * ( 1 - swap ) + Y->s * swap;
  3129. Y->s = Y->s * ( 1 - swap ) + s * swap;
  3130. for( i = 0; i < X->n; i++ )
  3131. {
  3132. tmp = X->p[i];
  3133. X->p[i] = X->p[i] * ( 1 - swap ) + Y->p[i] * swap;
  3134. Y->p[i] = Y->p[i] * ( 1 - swap ) + tmp * swap;
  3135. }
  3136. cleanup:
  3137. return( ret );
  3138. }
  3139. /*
  3140. * Set value from integer
  3141. */
  3142. int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z )
  3143. {
  3144. int ret;
  3145. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) );
  3146. memset( X->p, 0, X->n * ciL );
  3147. X->p[0] = ( z < 0 ) ? -z : z;
  3148. X->s = ( z < 0 ) ? -1 : 1;
  3149. cleanup:
  3150. return( ret );
  3151. }
  3152. /*
  3153. * Get a specific bit
  3154. */
  3155. int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos )
  3156. {
  3157. if( X->n * biL <= pos )
  3158. return( 0 );
  3159. return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 );
  3160. }
  3161. /*
  3162. * Set a bit to a specific value of 0 or 1
  3163. */
  3164. int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val )
  3165. {
  3166. int ret = 0;
  3167. size_t off = pos / biL;
  3168. size_t idx = pos % biL;
  3169. if( val != 0 && val != 1 )
  3170. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  3171. if( X->n * biL <= pos )
  3172. {
  3173. if( val == 0 )
  3174. return( 0 );
  3175. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) );
  3176. }
  3177. X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx );
  3178. X->p[off] |= (mbedtls_mpi_uint) val << idx;
  3179. cleanup:
  3180. return( ret );
  3181. }
  3182. /*
  3183. * Return the number of less significant zero-bits
  3184. */
  3185. size_t mbedtls_mpi_lsb( const mbedtls_mpi *X )
  3186. {
  3187. size_t i, j, count = 0;
  3188. for( i = 0; i < X->n; i++ )
  3189. for( j = 0; j < biL; j++, count++ )
  3190. if( ( ( X->p[i] >> j ) & 1 ) != 0 )
  3191. return( count );
  3192. return( 0 );
  3193. }
  3194. /*
  3195. * Count leading zero bits in a given integer
  3196. */
  3197. static size_t mbedtls_clz( const mbedtls_mpi_uint x )
  3198. {
  3199. size_t j;
  3200. mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
  3201. for( j = 0; j < biL; j++ )
  3202. {
  3203. if( x & mask ) break;
  3204. mask >>= 1;
  3205. }
  3206. return j;
  3207. }
  3208. /*
  3209. * Return the number of bits
  3210. */
  3211. size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X )
  3212. {
  3213. size_t i, j;
  3214. if( X->n == 0 )
  3215. return( 0 );
  3216. for( i = X->n - 1; i > 0; i-- )
  3217. if( X->p[i] != 0 )
  3218. break;
  3219. j = biL - mbedtls_clz( X->p[i] );
  3220. return( ( i * biL ) + j );
  3221. }
  3222. /*
  3223. * Return the total size in bytes
  3224. */
  3225. size_t mbedtls_mpi_size( const mbedtls_mpi *X )
  3226. {
  3227. return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 );
  3228. }
  3229. /*
  3230. * Convert an ASCII character to digit value
  3231. */
  3232. static int mpi_get_digit( mbedtls_mpi_uint *d, int radix, char c )
  3233. {
  3234. *d = 255;
  3235. if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
  3236. if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
  3237. if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
  3238. if( *d >= (mbedtls_mpi_uint) radix )
  3239. return( MBEDTLS_ERR_MPI_INVALID_CHARACTER );
  3240. return( 0 );
  3241. }
  3242. /*
  3243. * Import from an ASCII string
  3244. */
  3245. int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s )
  3246. {
  3247. int ret;
  3248. size_t i, j, slen, n;
  3249. mbedtls_mpi_uint d;
  3250. mbedtls_mpi T;
  3251. if( radix < 2 || radix > 16 )
  3252. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  3253. mbedtls_mpi_init( &T );
  3254. slen = strlen( s );
  3255. if( radix == 16 )
  3256. {
  3257. if( slen > MPI_SIZE_T_MAX >> 2 )
  3258. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  3259. n = BITS_TO_LIMBS( slen << 2 );
  3260. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) );
  3261. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
  3262. for( i = slen, j = 0; i > 0; i--, j++ )
  3263. {
  3264. if( i == 1 && s[i - 1] == '-' )
  3265. {
  3266. X->s = -1;
  3267. break;
  3268. }
  3269. MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
  3270. X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
  3271. }
  3272. }
  3273. else
  3274. {
  3275. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
  3276. for( i = 0; i < slen; i++ )
  3277. {
  3278. if( i == 0 && s[i] == '-' )
  3279. {
  3280. X->s = -1;
  3281. continue;
  3282. }
  3283. MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
  3284. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) );
  3285. if( X->s == 1 )
  3286. {
  3287. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) );
  3288. }
  3289. else
  3290. {
  3291. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( X, &T, d ) );
  3292. }
  3293. }
  3294. }
  3295. cleanup:
  3296. mbedtls_mpi_free( &T );
  3297. return( ret );
  3298. }
  3299. /*
  3300. * Helper to write the digits high-order first
  3301. */
  3302. static int mpi_write_hlp( mbedtls_mpi *X, int radix, char **p )
  3303. {
  3304. int ret;
  3305. mbedtls_mpi_uint r;
  3306. if( radix < 2 || radix > 16 )
  3307. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  3308. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
  3309. MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
  3310. if( mbedtls_mpi_cmp_int( X, 0 ) != 0 )
  3311. MBEDTLS_MPI_CHK( mpi_write_hlp( X, radix, p ) );
  3312. if( r < 10 )
  3313. *(*p)++ = (char)( r + 0x30 );
  3314. else
  3315. *(*p)++ = (char)( r + 0x37 );
  3316. cleanup:
  3317. return( ret );
  3318. }
  3319. /*
  3320. * Export into an ASCII string
  3321. */
  3322. int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
  3323. char *buf, size_t buflen, size_t *olen )
  3324. {
  3325. int ret = 0;
  3326. size_t n;
  3327. char *p;
  3328. mbedtls_mpi T;
  3329. if( radix < 2 || radix > 16 )
  3330. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  3331. n = mbedtls_mpi_bitlen( X );
  3332. if( radix >= 4 ) n >>= 1;
  3333. if( radix >= 16 ) n >>= 1;
  3334. /*
  3335. * Round up the buffer length to an even value to ensure that there is
  3336. * enough room for hexadecimal values that can be represented in an odd
  3337. * number of digits.
  3338. */
  3339. n += 3 + ( ( n + 1 ) & 1 );
  3340. if( buflen < n )
  3341. {
  3342. *olen = n;
  3343. return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
  3344. }
  3345. p = buf;
  3346. mbedtls_mpi_init( &T );
  3347. if( X->s == -1 )
  3348. *p++ = '-';
  3349. if( radix == 16 )
  3350. {
  3351. int c;
  3352. size_t i, j, k;
  3353. for( i = X->n, k = 0; i > 0; i-- )
  3354. {
  3355. for( j = ciL; j > 0; j-- )
  3356. {
  3357. c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
  3358. if( c == 0 && k == 0 && ( i + j ) != 2 )
  3359. continue;
  3360. *(p++) = "0123456789ABCDEF" [c / 16];
  3361. *(p++) = "0123456789ABCDEF" [c % 16];
  3362. k = 1;
  3363. }
  3364. }
  3365. }
  3366. else
  3367. {
  3368. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) );
  3369. if( T.s == -1 )
  3370. T.s = 1;
  3371. MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p ) );
  3372. }
  3373. *p++ = '\0';
  3374. *olen = p - buf;
  3375. cleanup:
  3376. mbedtls_mpi_free( &T );
  3377. return( ret );
  3378. }
  3379. #if defined(MBEDTLS_FS_IO)
  3380. /*
  3381. * Read X from an opened file
  3382. */
  3383. int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
  3384. {
  3385. mbedtls_mpi_uint d;
  3386. size_t slen;
  3387. char *p;
  3388. /*
  3389. * Buffer should have space for (short) label and decimal formatted MPI,
  3390. * newline characters and '\0'
  3391. */
  3392. char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
  3393. memset( s, 0, sizeof( s ) );
  3394. if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
  3395. return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
  3396. slen = strlen( s );
  3397. if( slen == sizeof( s ) - 2 )
  3398. return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
  3399. if( slen > 0 && s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
  3400. if( slen > 0 && s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
  3401. p = s + slen;
  3402. while( p-- > s )
  3403. if( mpi_get_digit( &d, radix, *p ) != 0 )
  3404. break;
  3405. return( mbedtls_mpi_read_string( X, radix, p + 1 ) );
  3406. }
  3407. /*
  3408. * Write X into an opened file (or stdout if fout == NULL)
  3409. */
  3410. int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout )
  3411. {
  3412. int ret;
  3413. size_t n, slen, plen;
  3414. /*
  3415. * Buffer should have space for (short) label and decimal formatted MPI,
  3416. * newline characters and '\0'
  3417. */
  3418. char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
  3419. memset( s, 0, sizeof( s ) );
  3420. MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) );
  3421. if( p == NULL ) p = "";
  3422. plen = strlen( p );
  3423. slen = strlen( s );
  3424. s[slen++] = '\r';
  3425. s[slen++] = '\n';
  3426. if( fout != NULL )
  3427. {
  3428. if( fwrite( p, 1, plen, fout ) != plen ||
  3429. fwrite( s, 1, slen, fout ) != slen )
  3430. return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
  3431. }
  3432. else
  3433. mbedtls_printf( "%s%s", p, s );
  3434. cleanup:
  3435. return( ret );
  3436. }
  3437. #endif /* MBEDTLS_FS_IO */
  3438. /*
  3439. * Import X from unsigned binary data, big endian
  3440. */
  3441. int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen )
  3442. {
  3443. int ret;
  3444. size_t i, j;
  3445. size_t const limbs = CHARS_TO_LIMBS( buflen );
  3446. /* Ensure that target MPI has exactly the necessary number of limbs */
  3447. if( X->n != limbs )
  3448. {
  3449. mbedtls_mpi_free( X );
  3450. mbedtls_mpi_init( X );
  3451. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
  3452. }
  3453. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
  3454. for( i = buflen, j = 0; i > 0; i--, j++ )
  3455. X->p[j / ciL] |= ((mbedtls_mpi_uint) buf[i - 1]) << ((j % ciL) << 3);
  3456. cleanup:
  3457. return( ret );
  3458. }
  3459. /*
  3460. * Export X into unsigned binary data, big endian
  3461. */
  3462. int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen )
  3463. {
  3464. size_t i, j, n;
  3465. n = mbedtls_mpi_size( X );
  3466. if( buflen < n )
  3467. return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
  3468. memset( buf, 0, buflen );
  3469. for( i = buflen - 1, j = 0; n > 0; i--, j++, n-- )
  3470. buf[i] = (unsigned char)( X->p[j / ciL] >> ((j % ciL) << 3) );
  3471. return( 0 );
  3472. }
  3473. /*
  3474. * Left-shift: X <<= count
  3475. */
  3476. int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count )
  3477. {
  3478. int ret;
  3479. size_t i, v0, t1;
  3480. mbedtls_mpi_uint r0 = 0, r1;
  3481. v0 = count / (biL );
  3482. t1 = count & (biL - 1);
  3483. i = mbedtls_mpi_bitlen( X ) + count;
  3484. if( X->n * biL < i )
  3485. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
  3486. ret = 0;
  3487. /*
  3488. * shift by count / limb_size
  3489. */
  3490. if( v0 > 0 )
  3491. {
  3492. for( i = X->n; i > v0; i-- )
  3493. X->p[i - 1] = X->p[i - v0 - 1];
  3494. for( ; i > 0; i-- )
  3495. X->p[i - 1] = 0;
  3496. }
  3497. /*
  3498. * shift by count % limb_size
  3499. */
  3500. if( t1 > 0 )
  3501. {
  3502. for( i = v0; i < X->n; i++ )
  3503. {
  3504. r1 = X->p[i] >> (biL - t1);
  3505. X->p[i] <<= t1;
  3506. X->p[i] |= r0;
  3507. r0 = r1;
  3508. }
  3509. }
  3510. cleanup:
  3511. return( ret );
  3512. }
  3513. /*
  3514. * Right-shift: X >>= count
  3515. */
  3516. int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
  3517. {
  3518. size_t i, v0, v1;
  3519. mbedtls_mpi_uint r0 = 0, r1;
  3520. v0 = count / biL;
  3521. v1 = count & (biL - 1);
  3522. if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
  3523. return mbedtls_mpi_lset( X, 0 );
  3524. /*
  3525. * shift by count / limb_size
  3526. */
  3527. if( v0 > 0 )
  3528. {
  3529. for( i = 0; i < X->n - v0; i++ )
  3530. X->p[i] = X->p[i + v0];
  3531. for( ; i < X->n; i++ )
  3532. X->p[i] = 0;
  3533. }
  3534. /*
  3535. * shift by count % limb_size
  3536. */
  3537. if( v1 > 0 )
  3538. {
  3539. for( i = X->n; i > 0; i-- )
  3540. {
  3541. r1 = X->p[i - 1] << (biL - v1);
  3542. X->p[i - 1] >>= v1;
  3543. X->p[i - 1] |= r0;
  3544. r0 = r1;
  3545. }
  3546. }
  3547. return( 0 );
  3548. }
  3549. /*
  3550. * Compare unsigned values
  3551. */
  3552. int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y )
  3553. {
  3554. size_t i, j;
  3555. for( i = X->n; i > 0; i-- )
  3556. if( X->p[i - 1] != 0 )
  3557. break;
  3558. for( j = Y->n; j > 0; j-- )
  3559. if( Y->p[j - 1] != 0 )
  3560. break;
  3561. if( i == 0 && j == 0 )
  3562. return( 0 );
  3563. if( i > j ) return( 1 );
  3564. if( j > i ) return( -1 );
  3565. for( ; i > 0; i-- )
  3566. {
  3567. if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
  3568. if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
  3569. }
  3570. return( 0 );
  3571. }
  3572. /*
  3573. * Compare signed values
  3574. */
  3575. int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
  3576. {
  3577. size_t i, j;
  3578. for( i = X->n; i > 0; i-- )
  3579. if( X->p[i - 1] != 0 )
  3580. break;
  3581. for( j = Y->n; j > 0; j-- )
  3582. if( Y->p[j - 1] != 0 )
  3583. break;
  3584. if( i == 0 && j == 0 )
  3585. return( 0 );
  3586. if( i > j ) return( X->s );
  3587. if( j > i ) return( -Y->s );
  3588. if( X->s > 0 && Y->s < 0 ) return( 1 );
  3589. if( Y->s > 0 && X->s < 0 ) return( -1 );
  3590. for( ; i > 0; i-- )
  3591. {
  3592. if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
  3593. if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
  3594. }
  3595. return( 0 );
  3596. }
  3597. /*
  3598. * Compare signed values
  3599. */
  3600. int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z )
  3601. {
  3602. mbedtls_mpi Y;
  3603. mbedtls_mpi_uint p[1];
  3604. *p = ( z < 0 ) ? -z : z;
  3605. Y.s = ( z < 0 ) ? -1 : 1;
  3606. Y.n = 1;
  3607. Y.p = p;
  3608. return( mbedtls_mpi_cmp_mpi( X, &Y ) );
  3609. }
  3610. /*
  3611. * Unsigned addition: X = |A| + |B| (HAC 14.7)
  3612. */
  3613. int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
  3614. {
  3615. int ret;
  3616. size_t i, j;
  3617. mbedtls_mpi_uint *o, *p, c, tmp;
  3618. if( X == B )
  3619. {
  3620. const mbedtls_mpi *T = A; A = X; B = T;
  3621. }
  3622. if( X != A )
  3623. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
  3624. /*
  3625. * X should always be positive as a result of unsigned additions.
  3626. */
  3627. X->s = 1;
  3628. for( j = B->n; j > 0; j-- )
  3629. if( B->p[j - 1] != 0 )
  3630. break;
  3631. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
  3632. o = B->p; p = X->p; c = 0;
  3633. /*
  3634. * tmp is used because it might happen that p == o
  3635. */
  3636. for( i = 0; i < j; i++, o++, p++ )
  3637. {
  3638. tmp= *o;
  3639. *p += c; c = ( *p < c );
  3640. *p += tmp; c += ( *p < tmp );
  3641. }
  3642. while( c != 0 )
  3643. {
  3644. if( i >= X->n )
  3645. {
  3646. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
  3647. p = X->p + i;
  3648. }
  3649. *p += c; c = ( *p < c ); i++; p++;
  3650. }
  3651. cleanup:
  3652. return( ret );
  3653. }
  3654. /*
  3655. * Helper for mbedtls_mpi subtraction
  3656. */
  3657. static void mpi_sub_hlp( size_t n, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d )
  3658. {
  3659. size_t i;
  3660. mbedtls_mpi_uint c, z;
  3661. for( i = c = 0; i < n; i++, s++, d++ )
  3662. {
  3663. z = ( *d < c ); *d -= c;
  3664. c = ( *d < *s ) + z; *d -= *s;
  3665. }
  3666. while( c != 0 )
  3667. {
  3668. z = ( *d < c ); *d -= c;
  3669. c = z; i++; d++;
  3670. }
  3671. }
  3672. /*
  3673. * Unsigned subtraction: X = |A| - |B| (HAC 14.9)
  3674. */
  3675. int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
  3676. {
  3677. mbedtls_mpi TB;
  3678. int ret;
  3679. size_t n;
  3680. if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
  3681. return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
  3682. mbedtls_mpi_init( &TB );
  3683. if( X == B )
  3684. {
  3685. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
  3686. B = &TB;
  3687. }
  3688. if( X != A )
  3689. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
  3690. /*
  3691. * X should always be positive as a result of unsigned subtractions.
  3692. */
  3693. X->s = 1;
  3694. ret = 0;
  3695. for( n = B->n; n > 0; n-- )
  3696. if( B->p[n - 1] != 0 )
  3697. break;
  3698. mpi_sub_hlp( n, B->p, X->p );
  3699. cleanup:
  3700. mbedtls_mpi_free( &TB );
  3701. return( ret );
  3702. }
  3703. /*
  3704. * Signed addition: X = A + B
  3705. */
  3706. int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
  3707. {
  3708. int ret, s = A->s;
  3709. if( A->s * B->s < 0 )
  3710. {
  3711. if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
  3712. {
  3713. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
  3714. X->s = s;
  3715. }
  3716. else
  3717. {
  3718. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
  3719. X->s = -s;
  3720. }
  3721. }
  3722. else
  3723. {
  3724. MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
  3725. X->s = s;
  3726. }
  3727. cleanup:
  3728. return( ret );
  3729. }
  3730. /*
  3731. * Signed subtraction: X = A - B
  3732. */
  3733. int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
  3734. {
  3735. int ret, s = A->s;
  3736. if( A->s * B->s > 0 )
  3737. {
  3738. if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
  3739. {
  3740. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
  3741. X->s = s;
  3742. }
  3743. else
  3744. {
  3745. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
  3746. X->s = -s;
  3747. }
  3748. }
  3749. else
  3750. {
  3751. MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
  3752. X->s = s;
  3753. }
  3754. cleanup:
  3755. return( ret );
  3756. }
  3757. /*
  3758. * Signed addition: X = A + b
  3759. */
  3760. int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
  3761. {
  3762. mbedtls_mpi _B;
  3763. mbedtls_mpi_uint p[1];
  3764. p[0] = ( b < 0 ) ? -b : b;
  3765. _B.s = ( b < 0 ) ? -1 : 1;
  3766. _B.n = 1;
  3767. _B.p = p;
  3768. return( mbedtls_mpi_add_mpi( X, A, &_B ) );
  3769. }
  3770. /*
  3771. * Signed subtraction: X = A - b
  3772. */
  3773. int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
  3774. {
  3775. mbedtls_mpi _B;
  3776. mbedtls_mpi_uint p[1];
  3777. p[0] = ( b < 0 ) ? -b : b;
  3778. _B.s = ( b < 0 ) ? -1 : 1;
  3779. _B.n = 1;
  3780. _B.p = p;
  3781. return( mbedtls_mpi_sub_mpi( X, A, &_B ) );
  3782. }
  3783. /*
  3784. * Helper for mbedtls_mpi multiplication
  3785. */
  3786. static
  3787. #if defined(__APPLE__) && defined(__arm__)
  3788. /*
  3789. * Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
  3790. * appears to need this to prevent bad ARM code generation at -O3.
  3791. */
  3792. __attribute__ ((noinline))
  3793. #endif
  3794. void mpi_mul_hlp( size_t i, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d, mbedtls_mpi_uint b )
  3795. {
  3796. mbedtls_mpi_uint c = 0, t = 0;
  3797. #if defined(MULADDC_HUIT)
  3798. for( ; i >= 8; i -= 8 )
  3799. {
  3800. MULADDC_INIT
  3801. MULADDC_HUIT
  3802. MULADDC_STOP
  3803. }
  3804. for( ; i > 0; i-- )
  3805. {
  3806. MULADDC_INIT
  3807. MULADDC_CORE
  3808. MULADDC_STOP
  3809. }
  3810. #else /* MULADDC_HUIT */
  3811. for( ; i >= 16; i -= 16 )
  3812. {
  3813. MULADDC_INIT
  3814. MULADDC_CORE MULADDC_CORE
  3815. MULADDC_CORE MULADDC_CORE
  3816. MULADDC_CORE MULADDC_CORE
  3817. MULADDC_CORE MULADDC_CORE
  3818. MULADDC_CORE MULADDC_CORE
  3819. MULADDC_CORE MULADDC_CORE
  3820. MULADDC_CORE MULADDC_CORE
  3821. MULADDC_CORE MULADDC_CORE
  3822. MULADDC_STOP
  3823. }
  3824. for( ; i >= 8; i -= 8 )
  3825. {
  3826. MULADDC_INIT
  3827. MULADDC_CORE MULADDC_CORE
  3828. MULADDC_CORE MULADDC_CORE
  3829. MULADDC_CORE MULADDC_CORE
  3830. MULADDC_CORE MULADDC_CORE
  3831. MULADDC_STOP
  3832. }
  3833. for( ; i > 0; i-- )
  3834. {
  3835. MULADDC_INIT
  3836. MULADDC_CORE
  3837. MULADDC_STOP
  3838. }
  3839. #endif /* MULADDC_HUIT */
  3840. t++;
  3841. do {
  3842. *d += c; c = ( *d < c ); d++;
  3843. }
  3844. while( c != 0 );
  3845. }
  3846. /*
  3847. * Baseline multiplication: X = A * B (HAC 14.12)
  3848. */
  3849. int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
  3850. {
  3851. int ret;
  3852. size_t i, j;
  3853. mbedtls_mpi TA, TB;
  3854. mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
  3855. if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
  3856. if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
  3857. for( i = A->n; i > 0; i-- )
  3858. if( A->p[i - 1] != 0 )
  3859. break;
  3860. for( j = B->n; j > 0; j-- )
  3861. if( B->p[j - 1] != 0 )
  3862. break;
  3863. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) );
  3864. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
  3865. for( i++; j > 0; j-- )
  3866. mpi_mul_hlp( i - 1, A->p, X->p + j - 1, B->p[j - 1] );
  3867. X->s = A->s * B->s;
  3868. cleanup:
  3869. mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
  3870. return( ret );
  3871. }
  3872. /*
  3873. * Baseline multiplication: X = A * b
  3874. */
  3875. int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
  3876. {
  3877. mbedtls_mpi _B;
  3878. mbedtls_mpi_uint p[1];
  3879. _B.s = 1;
  3880. _B.n = 1;
  3881. _B.p = p;
  3882. p[0] = b;
  3883. return( mbedtls_mpi_mul_mpi( X, A, &_B ) );
  3884. }
  3885. /*
  3886. * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
  3887. * mbedtls_mpi_uint divisor, d
  3888. */
  3889. static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
  3890. mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r )
  3891. {
  3892. #if defined(MBEDTLS_HAVE_UDBL)
  3893. mbedtls_t_udbl dividend, quotient;
  3894. #else
  3895. const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
  3896. const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1;
  3897. mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
  3898. mbedtls_mpi_uint u0_msw, u0_lsw;
  3899. size_t s;
  3900. #endif
  3901. /*
  3902. * Check for overflow
  3903. */
  3904. if( 0 == d || u1 >= d )
  3905. {
  3906. if (r != NULL) *r = ~0;
  3907. return ( ~0 );
  3908. }
  3909. #if defined(MBEDTLS_HAVE_UDBL)
  3910. dividend = (mbedtls_t_udbl) u1 << biL;
  3911. dividend |= (mbedtls_t_udbl) u0;
  3912. quotient = dividend / d;
  3913. if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
  3914. quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1;
  3915. if( r != NULL )
  3916. *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) );
  3917. return (mbedtls_mpi_uint) quotient;
  3918. #else
  3919. /*
  3920. * Algorithm D, Section 4.3.1 - The Art of Computer Programming
  3921. * Vol. 2 - Seminumerical Algorithms, Knuth
  3922. */
  3923. /*
  3924. * Normalize the divisor, d, and dividend, u0, u1
  3925. */
  3926. s = mbedtls_clz( d );
  3927. d = d << s;
  3928. u1 = u1 << s;
  3929. u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) );
  3930. u0 = u0 << s;
  3931. d1 = d >> biH;
  3932. d0 = d & uint_halfword_mask;
  3933. u0_msw = u0 >> biH;
  3934. u0_lsw = u0 & uint_halfword_mask;
  3935. /*
  3936. * Find the first quotient and remainder
  3937. */
  3938. q1 = u1 / d1;
  3939. r0 = u1 - d1 * q1;
  3940. while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) )
  3941. {
  3942. q1 -= 1;
  3943. r0 += d1;
  3944. if ( r0 >= radix ) break;
  3945. }
  3946. rAX = ( u1 * radix ) + ( u0_msw - q1 * d );
  3947. q0 = rAX / d1;
  3948. r0 = rAX - q0 * d1;
  3949. while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) )
  3950. {
  3951. q0 -= 1;
  3952. r0 += d1;
  3953. if ( r0 >= radix ) break;
  3954. }
  3955. if (r != NULL)
  3956. *r = ( rAX * radix + u0_lsw - q0 * d ) >> s;
  3957. quotient = q1 * radix + q0;
  3958. return quotient;
  3959. #endif
  3960. }
  3961. /*
  3962. * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
  3963. */
  3964. int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
  3965. {
  3966. int ret;
  3967. size_t i, n, t, k;
  3968. mbedtls_mpi X, Y, Z, T1, T2;
  3969. if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
  3970. return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
  3971. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
  3972. mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
  3973. if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
  3974. {
  3975. if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
  3976. if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
  3977. return( 0 );
  3978. }
  3979. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
  3980. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
  3981. X.s = Y.s = 1;
  3982. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
  3983. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) );
  3984. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, 2 ) );
  3985. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T2, 3 ) );
  3986. k = mbedtls_mpi_bitlen( &Y ) % biL;
  3987. if( k < biL - 1 )
  3988. {
  3989. k = biL - 1 - k;
  3990. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
  3991. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
  3992. }
  3993. else k = 0;
  3994. n = X.n - 1;
  3995. t = Y.n - 1;
  3996. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
  3997. while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
  3998. {
  3999. Z.p[n - t]++;
  4000. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
  4001. }
  4002. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
  4003. for( i = n; i > t ; i-- )
  4004. {
  4005. if( X.p[i] >= Y.p[t] )
  4006. Z.p[i - t - 1] = ~0;
  4007. else
  4008. {
  4009. Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1],
  4010. Y.p[t], NULL);
  4011. }
  4012. Z.p[i - t - 1]++;
  4013. do
  4014. {
  4015. Z.p[i - t - 1]--;
  4016. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
  4017. T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
  4018. T1.p[1] = Y.p[t];
  4019. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
  4020. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T2, 0 ) );
  4021. T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
  4022. T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
  4023. T2.p[2] = X.p[i];
  4024. }
  4025. while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
  4026. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
  4027. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
  4028. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
  4029. if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
  4030. {
  4031. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
  4032. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
  4033. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
  4034. Z.p[i - t - 1]--;
  4035. }
  4036. }
  4037. if( Q != NULL )
  4038. {
  4039. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
  4040. Q->s = A->s * B->s;
  4041. }
  4042. if( R != NULL )
  4043. {
  4044. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
  4045. X.s = A->s;
  4046. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
  4047. if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
  4048. R->s = 1;
  4049. }
  4050. cleanup:
  4051. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
  4052. mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
  4053. return( ret );
  4054. }
  4055. /*
  4056. * Division by int: A = Q * b + R
  4057. */
  4058. int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b )
  4059. {
  4060. mbedtls_mpi _B;
  4061. mbedtls_mpi_uint p[1];
  4062. p[0] = ( b < 0 ) ? -b : b;
  4063. _B.s = ( b < 0 ) ? -1 : 1;
  4064. _B.n = 1;
  4065. _B.p = p;
  4066. return( mbedtls_mpi_div_mpi( Q, R, A, &_B ) );
  4067. }
  4068. /*
  4069. * Modulo: R = A mod B
  4070. */
  4071. int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
  4072. {
  4073. int ret;
  4074. if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
  4075. return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
  4076. MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
  4077. while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
  4078. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
  4079. while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
  4080. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
  4081. cleanup:
  4082. return( ret );
  4083. }
  4084. /*
  4085. * Modulo: r = A mod b
  4086. */
  4087. int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
  4088. {
  4089. size_t i;
  4090. mbedtls_mpi_uint x, y, z;
  4091. if( b == 0 )
  4092. return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
  4093. if( b < 0 )
  4094. return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
  4095. /*
  4096. * handle trivial cases
  4097. */
  4098. if( b == 1 )
  4099. {
  4100. *r = 0;
  4101. return( 0 );
  4102. }
  4103. if( b == 2 )
  4104. {
  4105. *r = A->p[0] & 1;
  4106. return( 0 );
  4107. }
  4108. /*
  4109. * general case
  4110. */
  4111. for( i = A->n, y = 0; i > 0; i-- )
  4112. {
  4113. x = A->p[i - 1];
  4114. y = ( y << biH ) | ( x >> biH );
  4115. z = y / b;
  4116. y -= z * b;
  4117. x <<= biH;
  4118. y = ( y << biH ) | ( x >> biH );
  4119. z = y / b;
  4120. y -= z * b;
  4121. }
  4122. /*
  4123. * If A is negative, then the current y represents a negative value.
  4124. * Flipping it to the positive side.
  4125. */
  4126. if( A->s < 0 && y != 0 )
  4127. y = b - y;
  4128. *r = y;
  4129. return( 0 );
  4130. }
  4131. /*
  4132. * Fast Montgomery initialization (thanks to Tom St Denis)
  4133. */
  4134. static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
  4135. {
  4136. mbedtls_mpi_uint x, m0 = N->p[0];
  4137. unsigned int i;
  4138. x = m0;
  4139. x += ( ( m0 + 2 ) & 4 ) << 1;
  4140. for( i = biL; i >= 8; i /= 2 )
  4141. x *= ( 2 - ( m0 * x ) );
  4142. *mm = ~x + 1;
  4143. }
  4144. /*
  4145. * Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
  4146. */
  4147. static int mpi_montmul( mbedtls_mpi *A, const mbedtls_mpi *B, const mbedtls_mpi *N, mbedtls_mpi_uint mm,
  4148. const mbedtls_mpi *T )
  4149. {
  4150. size_t i, n, m;
  4151. mbedtls_mpi_uint u0, u1, *d;
  4152. if( T->n < N->n + 1 || T->p == NULL )
  4153. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  4154. memset( T->p, 0, T->n * ciL );
  4155. d = T->p;
  4156. n = N->n;
  4157. m = ( B->n < n ) ? B->n : n;
  4158. for( i = 0; i < n; i++ )
  4159. {
  4160. /*
  4161. * T = (T + u0*B + u1*N) / 2^biL
  4162. */
  4163. u0 = A->p[i];
  4164. u1 = ( d[0] + u0 * B->p[0] ) * mm;
  4165. mpi_mul_hlp( m, B->p, d, u0 );
  4166. mpi_mul_hlp( n, N->p, d, u1 );
  4167. *d++ = u0; d[n + 1] = 0;
  4168. }
  4169. memcpy( A->p, d, ( n + 1 ) * ciL );
  4170. if( mbedtls_mpi_cmp_abs( A, N ) >= 0 )
  4171. mpi_sub_hlp( n, N->p, A->p );
  4172. else
  4173. /* prevent timing attacks */
  4174. mpi_sub_hlp( n, A->p, T->p );
  4175. return( 0 );
  4176. }
  4177. /*
  4178. * Montgomery reduction: A = A * R^-1 mod N
  4179. */
  4180. static int mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N, mbedtls_mpi_uint mm, const mbedtls_mpi *T )
  4181. {
  4182. mbedtls_mpi_uint z = 1;
  4183. mbedtls_mpi U;
  4184. U.n = U.s = (int) z;
  4185. U.p = &z;
  4186. return( mpi_montmul( A, &U, N, mm, T ) );
  4187. }
  4188. /*
  4189. * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
  4190. */
  4191. int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR )
  4192. {
  4193. int ret;
  4194. size_t wbits, wsize, one = 1;
  4195. size_t i, j, nblimbs;
  4196. size_t bufsize, nbits;
  4197. mbedtls_mpi_uint ei, mm, state;
  4198. mbedtls_mpi RR, T, W[ 2 << MBEDTLS_MPI_WINDOW_SIZE ], Apos;
  4199. int neg;
  4200. if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || ( N->p[0] & 1 ) == 0 )
  4201. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  4202. if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
  4203. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  4204. /*
  4205. * Init temps and window size
  4206. */
  4207. mpi_montg_init( &mm, N );
  4208. mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
  4209. mbedtls_mpi_init( &Apos );
  4210. memset( W, 0, sizeof( W ) );
  4211. i = mbedtls_mpi_bitlen( E );
  4212. wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
  4213. ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
  4214. if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
  4215. wsize = MBEDTLS_MPI_WINDOW_SIZE;
  4216. j = N->n + 1;
  4217. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
  4218. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) );
  4219. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
  4220. /*
  4221. * Compensate for negative A (and correct at the end)
  4222. */
  4223. neg = ( A->s == -1 );
  4224. if( neg )
  4225. {
  4226. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
  4227. Apos.s = 1;
  4228. A = &Apos;
  4229. }
  4230. /*
  4231. * If 1st call, pre-compute R^2 mod N
  4232. */
  4233. if( _RR == NULL || _RR->p == NULL )
  4234. {
  4235. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
  4236. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
  4237. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
  4238. if( _RR != NULL )
  4239. memcpy( _RR, &RR, sizeof( mbedtls_mpi ) );
  4240. }
  4241. else
  4242. memcpy( &RR, _RR, sizeof( mbedtls_mpi ) );
  4243. /*
  4244. * W[1] = A * R^2 * R^-1 mod N = A * R mod N
  4245. */
  4246. if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
  4247. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
  4248. else
  4249. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
  4250. MBEDTLS_MPI_CHK( mpi_montmul( &W[1], &RR, N, mm, &T ) );
  4251. /*
  4252. * X = R^2 * R^-1 mod N = R mod N
  4253. */
  4254. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
  4255. MBEDTLS_MPI_CHK( mpi_montred( X, N, mm, &T ) );
  4256. if( wsize > 1 )
  4257. {
  4258. /*
  4259. * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
  4260. */
  4261. j = one << ( wsize - 1 );
  4262. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
  4263. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) );
  4264. for( i = 0; i < wsize - 1; i++ )
  4265. MBEDTLS_MPI_CHK( mpi_montmul( &W[j], &W[j], N, mm, &T ) );
  4266. /*
  4267. * W[i] = W[i - 1] * W[1]
  4268. */
  4269. for( i = j + 1; i < ( one << wsize ); i++ )
  4270. {
  4271. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
  4272. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
  4273. MBEDTLS_MPI_CHK( mpi_montmul( &W[i], &W[1], N, mm, &T ) );
  4274. }
  4275. }
  4276. nblimbs = E->n;
  4277. bufsize = 0;
  4278. nbits = 0;
  4279. wbits = 0;
  4280. state = 0;
  4281. while( 1 )
  4282. {
  4283. if( bufsize == 0 )
  4284. {
  4285. if( nblimbs == 0 )
  4286. break;
  4287. nblimbs--;
  4288. bufsize = sizeof( mbedtls_mpi_uint ) << 3;
  4289. }
  4290. bufsize--;
  4291. ei = (E->p[nblimbs] >> bufsize) & 1;
  4292. /*
  4293. * skip leading 0s
  4294. */
  4295. if( ei == 0 && state == 0 )
  4296. continue;
  4297. if( ei == 0 && state == 1 )
  4298. {
  4299. /*
  4300. * out of window, square X
  4301. */
  4302. MBEDTLS_MPI_CHK( mpi_montmul( X, X, N, mm, &T ) );
  4303. continue;
  4304. }
  4305. /*
  4306. * add ei to current window
  4307. */
  4308. state = 2;
  4309. nbits++;
  4310. wbits |= ( ei << ( wsize - nbits ) );
  4311. if( nbits == wsize )
  4312. {
  4313. /*
  4314. * X = X^wsize R^-1 mod N
  4315. */
  4316. for( i = 0; i < wsize; i++ )
  4317. MBEDTLS_MPI_CHK( mpi_montmul( X, X, N, mm, &T ) );
  4318. /*
  4319. * X = X * W[wbits] R^-1 mod N
  4320. */
  4321. MBEDTLS_MPI_CHK( mpi_montmul( X, &W[wbits], N, mm, &T ) );
  4322. state--;
  4323. nbits = 0;
  4324. wbits = 0;
  4325. }
  4326. }
  4327. /*
  4328. * process the remaining bits
  4329. */
  4330. for( i = 0; i < nbits; i++ )
  4331. {
  4332. MBEDTLS_MPI_CHK( mpi_montmul( X, X, N, mm, &T ) );
  4333. wbits <<= 1;
  4334. if( ( wbits & ( one << wsize ) ) != 0 )
  4335. MBEDTLS_MPI_CHK( mpi_montmul( X, &W[1], N, mm, &T ) );
  4336. }
  4337. /*
  4338. * X = A^E * R * R^-1 mod N = A^E mod N
  4339. */
  4340. MBEDTLS_MPI_CHK( mpi_montred( X, N, mm, &T ) );
  4341. if( neg && E->n != 0 && ( E->p[0] & 1 ) != 0 )
  4342. {
  4343. X->s = -1;
  4344. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
  4345. }
  4346. cleanup:
  4347. for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
  4348. mbedtls_mpi_free( &W[i] );
  4349. mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
  4350. if( _RR == NULL || _RR->p == NULL )
  4351. mbedtls_mpi_free( &RR );
  4352. return( ret );
  4353. }
  4354. /*
  4355. * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
  4356. */
  4357. int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
  4358. {
  4359. int ret;
  4360. size_t lz, lzt;
  4361. mbedtls_mpi TG, TA, TB;
  4362. mbedtls_mpi_init( &TG ); mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
  4363. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
  4364. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
  4365. lz = mbedtls_mpi_lsb( &TA );
  4366. lzt = mbedtls_mpi_lsb( &TB );
  4367. if( lzt < lz )
  4368. lz = lzt;
  4369. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, lz ) );
  4370. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, lz ) );
  4371. TA.s = TB.s = 1;
  4372. while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
  4373. {
  4374. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
  4375. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
  4376. if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
  4377. {
  4378. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
  4379. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
  4380. }
  4381. else
  4382. {
  4383. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
  4384. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
  4385. }
  4386. }
  4387. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
  4388. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
  4389. cleanup:
  4390. mbedtls_mpi_free( &TG ); mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
  4391. return( ret );
  4392. }
  4393. /*
  4394. * Fill X with size bytes of random.
  4395. *
  4396. * Use a temporary bytes representation to make sure the result is the same
  4397. * regardless of the platform endianness (useful when f_rng is actually
  4398. * deterministic, eg for tests).
  4399. */
  4400. int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
  4401. int (*f_rng)(void *, unsigned char *, size_t),
  4402. void *p_rng )
  4403. {
  4404. int ret;
  4405. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  4406. if( size > MBEDTLS_MPI_MAX_SIZE )
  4407. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  4408. MBEDTLS_MPI_CHK( f_rng( p_rng, buf, size ) );
  4409. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( X, buf, size ) );
  4410. cleanup:
  4411. mbedtls_zeroize( buf, sizeof( buf ) );
  4412. return( ret );
  4413. }
  4414. /*
  4415. * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
  4416. */
  4417. int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
  4418. {
  4419. int ret;
  4420. mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
  4421. if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 )
  4422. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  4423. mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
  4424. mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
  4425. mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
  4426. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
  4427. if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
  4428. {
  4429. ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
  4430. goto cleanup;
  4431. }
  4432. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
  4433. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
  4434. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
  4435. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
  4436. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
  4437. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
  4438. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
  4439. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
  4440. do
  4441. {
  4442. while( ( TU.p[0] & 1 ) == 0 )
  4443. {
  4444. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
  4445. if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
  4446. {
  4447. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
  4448. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
  4449. }
  4450. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
  4451. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
  4452. }
  4453. while( ( TV.p[0] & 1 ) == 0 )
  4454. {
  4455. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
  4456. if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
  4457. {
  4458. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
  4459. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
  4460. }
  4461. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
  4462. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
  4463. }
  4464. if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
  4465. {
  4466. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
  4467. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
  4468. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
  4469. }
  4470. else
  4471. {
  4472. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
  4473. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
  4474. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
  4475. }
  4476. }
  4477. while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
  4478. while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
  4479. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
  4480. while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
  4481. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
  4482. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
  4483. cleanup:
  4484. mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
  4485. mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
  4486. mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
  4487. return( ret );
  4488. }
  4489. #if defined(MBEDTLS_GENPRIME)
  4490. static const int small_prime[] =
  4491. {
  4492. 3, 5, 7, 11, 13, 17, 19, 23,
  4493. 29, 31, 37, 41, 43, 47, 53, 59,
  4494. 61, 67, 71, 73, 79, 83, 89, 97,
  4495. 101, 103, 107, 109, 113, 127, 131, 137,
  4496. 139, 149, 151, 157, 163, 167, 173, 179,
  4497. 181, 191, 193, 197, 199, 211, 223, 227,
  4498. 229, 233, 239, 241, 251, 257, 263, 269,
  4499. 271, 277, 281, 283, 293, 307, 311, 313,
  4500. 317, 331, 337, 347, 349, 353, 359, 367,
  4501. 373, 379, 383, 389, 397, 401, 409, 419,
  4502. 421, 431, 433, 439, 443, 449, 457, 461,
  4503. 463, 467, 479, 487, 491, 499, 503, 509,
  4504. 521, 523, 541, 547, 557, 563, 569, 571,
  4505. 577, 587, 593, 599, 601, 607, 613, 617,
  4506. 619, 631, 641, 643, 647, 653, 659, 661,
  4507. 673, 677, 683, 691, 701, 709, 719, 727,
  4508. 733, 739, 743, 751, 757, 761, 769, 773,
  4509. 787, 797, 809, 811, 821, 823, 827, 829,
  4510. 839, 853, 857, 859, 863, 877, 881, 883,
  4511. 887, 907, 911, 919, 929, 937, 941, 947,
  4512. 953, 967, 971, 977, 983, 991, 997, -103
  4513. };
  4514. /*
  4515. * Small divisors test (X must be positive)
  4516. *
  4517. * Return values:
  4518. * 0: no small factor (possible prime, more tests needed)
  4519. * 1: certain prime
  4520. * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
  4521. * other negative: error
  4522. */
  4523. static int mpi_check_small_factors( const mbedtls_mpi *X )
  4524. {
  4525. int ret = 0;
  4526. size_t i;
  4527. mbedtls_mpi_uint r;
  4528. if( ( X->p[0] & 1 ) == 0 )
  4529. return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
  4530. for( i = 0; small_prime[i] > 0; i++ )
  4531. {
  4532. if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
  4533. return( 1 );
  4534. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
  4535. if( r == 0 )
  4536. return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
  4537. }
  4538. cleanup:
  4539. return( ret );
  4540. }
  4541. /*
  4542. * Miller-Rabin pseudo-primality test (HAC 4.24)
  4543. */
  4544. static int mpi_miller_rabin( const mbedtls_mpi *X,
  4545. int (*f_rng)(void *, unsigned char *, size_t),
  4546. void *p_rng )
  4547. {
  4548. int ret, count;
  4549. size_t i, j, k, n, s;
  4550. mbedtls_mpi W, R, T, A, RR;
  4551. mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
  4552. mbedtls_mpi_init( &RR );
  4553. /*
  4554. * W = |X| - 1
  4555. * R = W >> lsb( W )
  4556. */
  4557. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
  4558. s = mbedtls_mpi_lsb( &W );
  4559. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
  4560. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
  4561. i = mbedtls_mpi_bitlen( X );
  4562. /*
  4563. * HAC, table 4.4
  4564. */
  4565. n = ( ( i >= 1300 ) ? 2 : ( i >= 850 ) ? 3 :
  4566. ( i >= 650 ) ? 4 : ( i >= 350 ) ? 8 :
  4567. ( i >= 250 ) ? 12 : ( i >= 150 ) ? 18 : 27 );
  4568. for( i = 0; i < n; i++ )
  4569. {
  4570. /*
  4571. * pick a random A, 1 < A < |X| - 1
  4572. */
  4573. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
  4574. if( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 )
  4575. {
  4576. j = mbedtls_mpi_bitlen( &A ) - mbedtls_mpi_bitlen( &W );
  4577. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &A, j + 1 ) );
  4578. }
  4579. A.p[0] |= 3;
  4580. count = 0;
  4581. do {
  4582. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
  4583. j = mbedtls_mpi_bitlen( &A );
  4584. k = mbedtls_mpi_bitlen( &W );
  4585. if (j > k) {
  4586. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &A, j - k ) );
  4587. }
  4588. if (count++ > 30) {
  4589. return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
  4590. }
  4591. } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ||
  4592. mbedtls_mpi_cmp_int( &A, 1 ) <= 0 );
  4593. /*
  4594. * A = A^R mod |X|
  4595. */
  4596. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
  4597. if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
  4598. mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
  4599. continue;
  4600. j = 1;
  4601. while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
  4602. {
  4603. /*
  4604. * A = A * A mod |X|
  4605. */
  4606. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
  4607. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) );
  4608. if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
  4609. break;
  4610. j++;
  4611. }
  4612. /*
  4613. * not prime if A != |X| - 1 or A == 1
  4614. */
  4615. if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
  4616. mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
  4617. {
  4618. ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
  4619. break;
  4620. }
  4621. }
  4622. cleanup:
  4623. mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
  4624. mbedtls_mpi_free( &RR );
  4625. return( ret );
  4626. }
  4627. /*
  4628. * Pseudo-primality test: small factors, then Miller-Rabin
  4629. */
  4630. int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
  4631. int (*f_rng)(void *, unsigned char *, size_t),
  4632. void *p_rng )
  4633. {
  4634. int ret;
  4635. mbedtls_mpi XX;
  4636. XX.s = 1;
  4637. XX.n = X->n;
  4638. XX.p = X->p;
  4639. if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
  4640. mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
  4641. return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
  4642. if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
  4643. return( 0 );
  4644. if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
  4645. {
  4646. if( ret == 1 )
  4647. return( 0 );
  4648. return( ret );
  4649. }
  4650. return( mpi_miller_rabin( &XX, f_rng, p_rng ) );
  4651. }
  4652. /*
  4653. * Prime number generation
  4654. */
  4655. int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
  4656. int (*f_rng)(void *, unsigned char *, size_t),
  4657. void *p_rng )
  4658. {
  4659. int ret;
  4660. size_t k, n;
  4661. mbedtls_mpi_uint r;
  4662. mbedtls_mpi Y;
  4663. if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
  4664. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  4665. mbedtls_mpi_init( &Y );
  4666. n = BITS_TO_LIMBS( nbits );
  4667. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
  4668. k = mbedtls_mpi_bitlen( X );
  4669. if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits + 1 ) );
  4670. mbedtls_mpi_set_bit( X, nbits-1, 1 );
  4671. X->p[0] |= 1;
  4672. if( dh_flag == 0 )
  4673. {
  4674. while( ( ret = mbedtls_mpi_is_prime( X, f_rng, p_rng ) ) != 0 )
  4675. {
  4676. if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
  4677. goto cleanup;
  4678. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 2 ) );
  4679. }
  4680. }
  4681. else
  4682. {
  4683. /*
  4684. * An necessary condition for Y and X = 2Y + 1 to be prime
  4685. * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
  4686. * Make sure it is satisfied, while keeping X = 3 mod 4
  4687. */
  4688. X->p[0] |= 2;
  4689. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
  4690. if( r == 0 )
  4691. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
  4692. else if( r == 1 )
  4693. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
  4694. /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
  4695. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
  4696. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
  4697. while( 1 )
  4698. {
  4699. /*
  4700. * First, check small factors for X and Y
  4701. * before doing Miller-Rabin on any of them
  4702. */
  4703. if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
  4704. ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
  4705. ( ret = mpi_miller_rabin( X, f_rng, p_rng ) ) == 0 &&
  4706. ( ret = mpi_miller_rabin( &Y, f_rng, p_rng ) ) == 0 )
  4707. {
  4708. break;
  4709. }
  4710. if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
  4711. goto cleanup;
  4712. /*
  4713. * Next candidates. We want to preserve Y = (X-1) / 2 and
  4714. * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
  4715. * so up Y by 6 and X by 12.
  4716. */
  4717. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) );
  4718. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) );
  4719. }
  4720. }
  4721. cleanup:
  4722. mbedtls_mpi_free( &Y );
  4723. return( ret );
  4724. }
  4725. #endif /* MBEDTLS_GENPRIME */
  4726. #if defined(MBEDTLS_SELF_TEST)
  4727. #define GCD_PAIR_COUNT 3
  4728. static const int gcd_pairs[GCD_PAIR_COUNT][3] =
  4729. {
  4730. { 693, 609, 21 },
  4731. { 1764, 868, 28 },
  4732. { 768454923, 542167814, 1 }
  4733. };
  4734. /*
  4735. * Checkup routine
  4736. */
  4737. int mbedtls_mpi_self_test( int verbose )
  4738. {
  4739. int ret, i;
  4740. mbedtls_mpi A, E, N, X, Y, U, V;
  4741. mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
  4742. mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
  4743. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
  4744. "EFE021C2645FD1DC586E69184AF4A31E" \
  4745. "D5F53E93B5F123FA41680867BA110131" \
  4746. "944FE7952E2517337780CB0DB80E61AA" \
  4747. "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
  4748. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
  4749. "B2E7EFD37075B9F03FF989C7C5051C20" \
  4750. "34D2A323810251127E7BF8625A4F49A5" \
  4751. "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
  4752. "5B5C25763222FEFCCFC38B832366C29E" ) );
  4753. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
  4754. "0066A198186C18C10B2F5ED9B522752A" \
  4755. "9830B69916E535C8F047518A889A43A5" \
  4756. "94B6BED27A168D31D4A52F88925AA8F5" ) );
  4757. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
  4758. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
  4759. "602AB7ECA597A3D6B56FF9829A5E8B85" \
  4760. "9E857EA95A03512E2BAE7391688D264A" \
  4761. "A5663B0341DB9CCFD2C4C5F421FEC814" \
  4762. "8001B72E848A38CAE1C65F78E56ABDEF" \
  4763. "E12D3C039B8A02D6BE593F0BBBDA56F1" \
  4764. "ECF677152EF804370C1A305CAF3B5BF1" \
  4765. "30879B56C61DE584A0F53A2447A51E" ) );
  4766. if( verbose != 0 )
  4767. mbedtls_printf( " MPI test #1 (mul_mpi): " );
  4768. if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
  4769. {
  4770. if( verbose != 0 )
  4771. mbedtls_printf( "failed\n" );
  4772. ret = 1;
  4773. goto cleanup;
  4774. }
  4775. if( verbose != 0 )
  4776. mbedtls_printf( "passed\n" );
  4777. MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
  4778. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
  4779. "256567336059E52CAE22925474705F39A94" ) );
  4780. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
  4781. "6613F26162223DF488E9CD48CC132C7A" \
  4782. "0AC93C701B001B092E4E5B9F73BCD27B" \
  4783. "9EE50D0657C77F374E903CDFA4C642" ) );
  4784. if( verbose != 0 )
  4785. mbedtls_printf( " MPI test #2 (div_mpi): " );
  4786. if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
  4787. mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
  4788. {
  4789. if( verbose != 0 )
  4790. mbedtls_printf( "failed\n" );
  4791. ret = 1;
  4792. goto cleanup;
  4793. }
  4794. if( verbose != 0 )
  4795. mbedtls_printf( "passed\n" );
  4796. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
  4797. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
  4798. "36E139AEA55215609D2816998ED020BB" \
  4799. "BD96C37890F65171D948E9BC7CBAA4D9" \
  4800. "325D24D6A3C12710F10A09FA08AB87" ) );
  4801. if( verbose != 0 )
  4802. mbedtls_printf( " MPI test #3 (exp_mod): " );
  4803. if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
  4804. {
  4805. if( verbose != 0 )
  4806. mbedtls_printf( "failed\n" );
  4807. ret = 1;
  4808. goto cleanup;
  4809. }
  4810. if( verbose != 0 )
  4811. mbedtls_printf( "passed\n" );
  4812. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
  4813. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
  4814. "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
  4815. "C3DBA76456363A10869622EAC2DD84EC" \
  4816. "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
  4817. if( verbose != 0 )
  4818. mbedtls_printf( " MPI test #4 (inv_mod): " );
  4819. if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
  4820. {
  4821. if( verbose != 0 )
  4822. mbedtls_printf( "failed\n" );
  4823. ret = 1;
  4824. goto cleanup;
  4825. }
  4826. if( verbose != 0 )
  4827. mbedtls_printf( "passed\n" );
  4828. if( verbose != 0 )
  4829. mbedtls_printf( " MPI test #5 (simple gcd): " );
  4830. for( i = 0; i < GCD_PAIR_COUNT; i++ )
  4831. {
  4832. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
  4833. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
  4834. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
  4835. if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
  4836. {
  4837. if( verbose != 0 )
  4838. mbedtls_printf( "failed at %d\n", i );
  4839. ret = 1;
  4840. goto cleanup;
  4841. }
  4842. }
  4843. if( verbose != 0 )
  4844. mbedtls_printf( "passed\n" );
  4845. cleanup:
  4846. if( ret != 0 && verbose != 0 )
  4847. mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
  4848. mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
  4849. mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
  4850. if( verbose != 0 )
  4851. mbedtls_printf( "\n" );
  4852. return( ret );
  4853. }
  4854. #endif /* MBEDTLS_SELF_TEST */
  4855. #endif /* MBEDTLS_BIGNUM_C */
  4856. /*
  4857. Amalgamated build undefines
  4858. */
  4859. #undef ADD
  4860. #undef BC
  4861. #undef BEFORE_COLON
  4862. #undef F
  4863. #undef F0
  4864. #undef F1
  4865. #undef F2
  4866. #undef F3
  4867. #undef F4
  4868. #undef F5
  4869. #undef FSb
  4870. #undef K
  4871. #undef KK
  4872. #undef P
  4873. #undef R
  4874. #undef ROTR
  4875. #undef S
  4876. #undef S0
  4877. #undef S1
  4878. #undef S2
  4879. #undef S3
  4880. #undef SAFE_SNPRINTF
  4881. #undef SHR
  4882. #undef close
  4883. #undef read
  4884. #undef supported_init
  4885. #undef write
  4886. /********* Start of file library/blowfish.c ************/
  4887. /*
  4888. * Blowfish implementation
  4889. *
  4890. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  4891. * SPDX-License-Identifier: Apache-2.0
  4892. *
  4893. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  4894. * not use this file except in compliance with the License.
  4895. * You may obtain a copy of the License at
  4896. *
  4897. * http://www.apache.org/licenses/LICENSE-2.0
  4898. *
  4899. * Unless required by applicable law or agreed to in writing, software
  4900. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  4901. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4902. * See the License for the specific language governing permissions and
  4903. * limitations under the License.
  4904. *
  4905. * This file is part of mbed TLS (https://tls.mbed.org)
  4906. */
  4907. /*
  4908. * The Blowfish block cipher was designed by Bruce Schneier in 1993.
  4909. * http://www.schneier.com/blowfish.html
  4910. * http://en.wikipedia.org/wiki/Blowfish_%28cipher%29
  4911. *
  4912. */
  4913. #if !defined(MBEDTLS_CONFIG_FILE)
  4914. #else
  4915. #endif
  4916. #if defined(MBEDTLS_BLOWFISH_C)
  4917. #include <string.h>
  4918. #if !defined(MBEDTLS_BLOWFISH_ALT)
  4919. /* Implementation that should never be optimized out by the compiler */
  4920. /* zeroize was here */
  4921. /*
  4922. * 32-bit integer manipulation macros (big endian)
  4923. */
  4924. #ifndef GET_UINT32_BE
  4925. #define GET_UINT32_BE(n,b,i) \
  4926. { \
  4927. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  4928. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  4929. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  4930. | ( (uint32_t) (b)[(i) + 3] ); \
  4931. }
  4932. #endif
  4933. #ifndef PUT_UINT32_BE
  4934. #define PUT_UINT32_BE(n,b,i) \
  4935. { \
  4936. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  4937. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  4938. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  4939. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  4940. }
  4941. #endif
  4942. static const uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2] = {
  4943. 0x243F6A88L, 0x85A308D3L, 0x13198A2EL, 0x03707344L,
  4944. 0xA4093822L, 0x299F31D0L, 0x082EFA98L, 0xEC4E6C89L,
  4945. 0x452821E6L, 0x38D01377L, 0xBE5466CFL, 0x34E90C6CL,
  4946. 0xC0AC29B7L, 0xC97C50DDL, 0x3F84D5B5L, 0xB5470917L,
  4947. 0x9216D5D9L, 0x8979FB1BL
  4948. };
  4949. /* declarations of data at the end of this file */
  4950. static const uint32_t S[4][256];
  4951. static uint32_t F( mbedtls_blowfish_context *ctx, uint32_t x )
  4952. {
  4953. unsigned short a, b, c, d;
  4954. uint32_t y;
  4955. d = (unsigned short)(x & 0xFF);
  4956. x >>= 8;
  4957. c = (unsigned short)(x & 0xFF);
  4958. x >>= 8;
  4959. b = (unsigned short)(x & 0xFF);
  4960. x >>= 8;
  4961. a = (unsigned short)(x & 0xFF);
  4962. y = ctx->S[0][a] + ctx->S[1][b];
  4963. y = y ^ ctx->S[2][c];
  4964. y = y + ctx->S[3][d];
  4965. return( y );
  4966. }
  4967. static void blowfish_enc( mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *xr )
  4968. {
  4969. uint32_t Xl, Xr, temp;
  4970. short i;
  4971. Xl = *xl;
  4972. Xr = *xr;
  4973. for( i = 0; i < MBEDTLS_BLOWFISH_ROUNDS; ++i )
  4974. {
  4975. Xl = Xl ^ ctx->P[i];
  4976. Xr = F( ctx, Xl ) ^ Xr;
  4977. temp = Xl;
  4978. Xl = Xr;
  4979. Xr = temp;
  4980. }
  4981. temp = Xl;
  4982. Xl = Xr;
  4983. Xr = temp;
  4984. Xr = Xr ^ ctx->P[MBEDTLS_BLOWFISH_ROUNDS];
  4985. Xl = Xl ^ ctx->P[MBEDTLS_BLOWFISH_ROUNDS + 1];
  4986. *xl = Xl;
  4987. *xr = Xr;
  4988. }
  4989. static void blowfish_dec( mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *xr )
  4990. {
  4991. uint32_t Xl, Xr, temp;
  4992. short i;
  4993. Xl = *xl;
  4994. Xr = *xr;
  4995. for( i = MBEDTLS_BLOWFISH_ROUNDS + 1; i > 1; --i )
  4996. {
  4997. Xl = Xl ^ ctx->P[i];
  4998. Xr = F( ctx, Xl ) ^ Xr;
  4999. temp = Xl;
  5000. Xl = Xr;
  5001. Xr = temp;
  5002. }
  5003. temp = Xl;
  5004. Xl = Xr;
  5005. Xr = temp;
  5006. Xr = Xr ^ ctx->P[1];
  5007. Xl = Xl ^ ctx->P[0];
  5008. *xl = Xl;
  5009. *xr = Xr;
  5010. }
  5011. void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx )
  5012. {
  5013. memset( ctx, 0, sizeof( mbedtls_blowfish_context ) );
  5014. }
  5015. void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx )
  5016. {
  5017. if( ctx == NULL )
  5018. return;
  5019. mbedtls_zeroize( ctx, sizeof( mbedtls_blowfish_context ) );
  5020. }
  5021. /*
  5022. * Blowfish key schedule
  5023. */
  5024. int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
  5025. unsigned int keybits )
  5026. {
  5027. unsigned int i, j, k;
  5028. uint32_t data, datal, datar;
  5029. if( keybits < MBEDTLS_BLOWFISH_MIN_KEY_BITS || keybits > MBEDTLS_BLOWFISH_MAX_KEY_BITS ||
  5030. ( keybits % 8 ) )
  5031. {
  5032. return( MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH );
  5033. }
  5034. keybits >>= 3;
  5035. for( i = 0; i < 4; i++ )
  5036. {
  5037. for( j = 0; j < 256; j++ )
  5038. ctx->S[i][j] = S[i][j];
  5039. }
  5040. j = 0;
  5041. for( i = 0; i < MBEDTLS_BLOWFISH_ROUNDS + 2; ++i )
  5042. {
  5043. data = 0x00000000;
  5044. for( k = 0; k < 4; ++k )
  5045. {
  5046. data = ( data << 8 ) | key[j++];
  5047. if( j >= keybits )
  5048. j = 0;
  5049. }
  5050. ctx->P[i] = P[i] ^ data;
  5051. }
  5052. datal = 0x00000000;
  5053. datar = 0x00000000;
  5054. for( i = 0; i < MBEDTLS_BLOWFISH_ROUNDS + 2; i += 2 )
  5055. {
  5056. blowfish_enc( ctx, &datal, &datar );
  5057. ctx->P[i] = datal;
  5058. ctx->P[i + 1] = datar;
  5059. }
  5060. for( i = 0; i < 4; i++ )
  5061. {
  5062. for( j = 0; j < 256; j += 2 )
  5063. {
  5064. blowfish_enc( ctx, &datal, &datar );
  5065. ctx->S[i][j] = datal;
  5066. ctx->S[i][j + 1] = datar;
  5067. }
  5068. }
  5069. return( 0 );
  5070. }
  5071. /*
  5072. * Blowfish-ECB block encryption/decryption
  5073. */
  5074. int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
  5075. int mode,
  5076. const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
  5077. unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] )
  5078. {
  5079. uint32_t X0, X1;
  5080. GET_UINT32_BE( X0, input, 0 );
  5081. GET_UINT32_BE( X1, input, 4 );
  5082. if( mode == MBEDTLS_BLOWFISH_DECRYPT )
  5083. {
  5084. blowfish_dec( ctx, &X0, &X1 );
  5085. }
  5086. else /* MBEDTLS_BLOWFISH_ENCRYPT */
  5087. {
  5088. blowfish_enc( ctx, &X0, &X1 );
  5089. }
  5090. PUT_UINT32_BE( X0, output, 0 );
  5091. PUT_UINT32_BE( X1, output, 4 );
  5092. return( 0 );
  5093. }
  5094. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  5095. /*
  5096. * Blowfish-CBC buffer encryption/decryption
  5097. */
  5098. int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
  5099. int mode,
  5100. size_t length,
  5101. unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
  5102. const unsigned char *input,
  5103. unsigned char *output )
  5104. {
  5105. int i;
  5106. unsigned char temp[MBEDTLS_BLOWFISH_BLOCKSIZE];
  5107. if( length % MBEDTLS_BLOWFISH_BLOCKSIZE )
  5108. return( MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH );
  5109. if( mode == MBEDTLS_BLOWFISH_DECRYPT )
  5110. {
  5111. while( length > 0 )
  5112. {
  5113. memcpy( temp, input, MBEDTLS_BLOWFISH_BLOCKSIZE );
  5114. mbedtls_blowfish_crypt_ecb( ctx, mode, input, output );
  5115. for( i = 0; i < MBEDTLS_BLOWFISH_BLOCKSIZE;i++ )
  5116. output[i] = (unsigned char)( output[i] ^ iv[i] );
  5117. memcpy( iv, temp, MBEDTLS_BLOWFISH_BLOCKSIZE );
  5118. input += MBEDTLS_BLOWFISH_BLOCKSIZE;
  5119. output += MBEDTLS_BLOWFISH_BLOCKSIZE;
  5120. length -= MBEDTLS_BLOWFISH_BLOCKSIZE;
  5121. }
  5122. }
  5123. else
  5124. {
  5125. while( length > 0 )
  5126. {
  5127. for( i = 0; i < MBEDTLS_BLOWFISH_BLOCKSIZE; i++ )
  5128. output[i] = (unsigned char)( input[i] ^ iv[i] );
  5129. mbedtls_blowfish_crypt_ecb( ctx, mode, output, output );
  5130. memcpy( iv, output, MBEDTLS_BLOWFISH_BLOCKSIZE );
  5131. input += MBEDTLS_BLOWFISH_BLOCKSIZE;
  5132. output += MBEDTLS_BLOWFISH_BLOCKSIZE;
  5133. length -= MBEDTLS_BLOWFISH_BLOCKSIZE;
  5134. }
  5135. }
  5136. return( 0 );
  5137. }
  5138. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  5139. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  5140. /*
  5141. * Blowfish CFB buffer encryption/decryption
  5142. */
  5143. int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
  5144. int mode,
  5145. size_t length,
  5146. size_t *iv_off,
  5147. unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
  5148. const unsigned char *input,
  5149. unsigned char *output )
  5150. {
  5151. int c;
  5152. size_t n = *iv_off;
  5153. if( mode == MBEDTLS_BLOWFISH_DECRYPT )
  5154. {
  5155. while( length-- )
  5156. {
  5157. if( n == 0 )
  5158. mbedtls_blowfish_crypt_ecb( ctx, MBEDTLS_BLOWFISH_ENCRYPT, iv, iv );
  5159. c = *input++;
  5160. *output++ = (unsigned char)( c ^ iv[n] );
  5161. iv[n] = (unsigned char) c;
  5162. n = ( n + 1 ) % MBEDTLS_BLOWFISH_BLOCKSIZE;
  5163. }
  5164. }
  5165. else
  5166. {
  5167. while( length-- )
  5168. {
  5169. if( n == 0 )
  5170. mbedtls_blowfish_crypt_ecb( ctx, MBEDTLS_BLOWFISH_ENCRYPT, iv, iv );
  5171. iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
  5172. n = ( n + 1 ) % MBEDTLS_BLOWFISH_BLOCKSIZE;
  5173. }
  5174. }
  5175. *iv_off = n;
  5176. return( 0 );
  5177. }
  5178. #endif /*MBEDTLS_CIPHER_MODE_CFB */
  5179. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  5180. /*
  5181. * Blowfish CTR buffer encryption/decryption
  5182. */
  5183. int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
  5184. size_t length,
  5185. size_t *nc_off,
  5186. unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
  5187. unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
  5188. const unsigned char *input,
  5189. unsigned char *output )
  5190. {
  5191. int c, i;
  5192. size_t n = *nc_off;
  5193. while( length-- )
  5194. {
  5195. if( n == 0 ) {
  5196. mbedtls_blowfish_crypt_ecb( ctx, MBEDTLS_BLOWFISH_ENCRYPT, nonce_counter,
  5197. stream_block );
  5198. for( i = MBEDTLS_BLOWFISH_BLOCKSIZE; i > 0; i-- )
  5199. if( ++nonce_counter[i - 1] != 0 )
  5200. break;
  5201. }
  5202. c = *input++;
  5203. *output++ = (unsigned char)( c ^ stream_block[n] );
  5204. n = ( n + 1 ) % MBEDTLS_BLOWFISH_BLOCKSIZE;
  5205. }
  5206. *nc_off = n;
  5207. return( 0 );
  5208. }
  5209. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  5210. static const uint32_t S[4][256] = {
  5211. { 0xD1310BA6L, 0x98DFB5ACL, 0x2FFD72DBL, 0xD01ADFB7L,
  5212. 0xB8E1AFEDL, 0x6A267E96L, 0xBA7C9045L, 0xF12C7F99L,
  5213. 0x24A19947L, 0xB3916CF7L, 0x0801F2E2L, 0x858EFC16L,
  5214. 0x636920D8L, 0x71574E69L, 0xA458FEA3L, 0xF4933D7EL,
  5215. 0x0D95748FL, 0x728EB658L, 0x718BCD58L, 0x82154AEEL,
  5216. 0x7B54A41DL, 0xC25A59B5L, 0x9C30D539L, 0x2AF26013L,
  5217. 0xC5D1B023L, 0x286085F0L, 0xCA417918L, 0xB8DB38EFL,
  5218. 0x8E79DCB0L, 0x603A180EL, 0x6C9E0E8BL, 0xB01E8A3EL,
  5219. 0xD71577C1L, 0xBD314B27L, 0x78AF2FDAL, 0x55605C60L,
  5220. 0xE65525F3L, 0xAA55AB94L, 0x57489862L, 0x63E81440L,
  5221. 0x55CA396AL, 0x2AAB10B6L, 0xB4CC5C34L, 0x1141E8CEL,
  5222. 0xA15486AFL, 0x7C72E993L, 0xB3EE1411L, 0x636FBC2AL,
  5223. 0x2BA9C55DL, 0x741831F6L, 0xCE5C3E16L, 0x9B87931EL,
  5224. 0xAFD6BA33L, 0x6C24CF5CL, 0x7A325381L, 0x28958677L,
  5225. 0x3B8F4898L, 0x6B4BB9AFL, 0xC4BFE81BL, 0x66282193L,
  5226. 0x61D809CCL, 0xFB21A991L, 0x487CAC60L, 0x5DEC8032L,
  5227. 0xEF845D5DL, 0xE98575B1L, 0xDC262302L, 0xEB651B88L,
  5228. 0x23893E81L, 0xD396ACC5L, 0x0F6D6FF3L, 0x83F44239L,
  5229. 0x2E0B4482L, 0xA4842004L, 0x69C8F04AL, 0x9E1F9B5EL,
  5230. 0x21C66842L, 0xF6E96C9AL, 0x670C9C61L, 0xABD388F0L,
  5231. 0x6A51A0D2L, 0xD8542F68L, 0x960FA728L, 0xAB5133A3L,
  5232. 0x6EEF0B6CL, 0x137A3BE4L, 0xBA3BF050L, 0x7EFB2A98L,
  5233. 0xA1F1651DL, 0x39AF0176L, 0x66CA593EL, 0x82430E88L,
  5234. 0x8CEE8619L, 0x456F9FB4L, 0x7D84A5C3L, 0x3B8B5EBEL,
  5235. 0xE06F75D8L, 0x85C12073L, 0x401A449FL, 0x56C16AA6L,
  5236. 0x4ED3AA62L, 0x363F7706L, 0x1BFEDF72L, 0x429B023DL,
  5237. 0x37D0D724L, 0xD00A1248L, 0xDB0FEAD3L, 0x49F1C09BL,
  5238. 0x075372C9L, 0x80991B7BL, 0x25D479D8L, 0xF6E8DEF7L,
  5239. 0xE3FE501AL, 0xB6794C3BL, 0x976CE0BDL, 0x04C006BAL,
  5240. 0xC1A94FB6L, 0x409F60C4L, 0x5E5C9EC2L, 0x196A2463L,
  5241. 0x68FB6FAFL, 0x3E6C53B5L, 0x1339B2EBL, 0x3B52EC6FL,
  5242. 0x6DFC511FL, 0x9B30952CL, 0xCC814544L, 0xAF5EBD09L,
  5243. 0xBEE3D004L, 0xDE334AFDL, 0x660F2807L, 0x192E4BB3L,
  5244. 0xC0CBA857L, 0x45C8740FL, 0xD20B5F39L, 0xB9D3FBDBL,
  5245. 0x5579C0BDL, 0x1A60320AL, 0xD6A100C6L, 0x402C7279L,
  5246. 0x679F25FEL, 0xFB1FA3CCL, 0x8EA5E9F8L, 0xDB3222F8L,
  5247. 0x3C7516DFL, 0xFD616B15L, 0x2F501EC8L, 0xAD0552ABL,
  5248. 0x323DB5FAL, 0xFD238760L, 0x53317B48L, 0x3E00DF82L,
  5249. 0x9E5C57BBL, 0xCA6F8CA0L, 0x1A87562EL, 0xDF1769DBL,
  5250. 0xD542A8F6L, 0x287EFFC3L, 0xAC6732C6L, 0x8C4F5573L,
  5251. 0x695B27B0L, 0xBBCA58C8L, 0xE1FFA35DL, 0xB8F011A0L,
  5252. 0x10FA3D98L, 0xFD2183B8L, 0x4AFCB56CL, 0x2DD1D35BL,
  5253. 0x9A53E479L, 0xB6F84565L, 0xD28E49BCL, 0x4BFB9790L,
  5254. 0xE1DDF2DAL, 0xA4CB7E33L, 0x62FB1341L, 0xCEE4C6E8L,
  5255. 0xEF20CADAL, 0x36774C01L, 0xD07E9EFEL, 0x2BF11FB4L,
  5256. 0x95DBDA4DL, 0xAE909198L, 0xEAAD8E71L, 0x6B93D5A0L,
  5257. 0xD08ED1D0L, 0xAFC725E0L, 0x8E3C5B2FL, 0x8E7594B7L,
  5258. 0x8FF6E2FBL, 0xF2122B64L, 0x8888B812L, 0x900DF01CL,
  5259. 0x4FAD5EA0L, 0x688FC31CL, 0xD1CFF191L, 0xB3A8C1ADL,
  5260. 0x2F2F2218L, 0xBE0E1777L, 0xEA752DFEL, 0x8B021FA1L,
  5261. 0xE5A0CC0FL, 0xB56F74E8L, 0x18ACF3D6L, 0xCE89E299L,
  5262. 0xB4A84FE0L, 0xFD13E0B7L, 0x7CC43B81L, 0xD2ADA8D9L,
  5263. 0x165FA266L, 0x80957705L, 0x93CC7314L, 0x211A1477L,
  5264. 0xE6AD2065L, 0x77B5FA86L, 0xC75442F5L, 0xFB9D35CFL,
  5265. 0xEBCDAF0CL, 0x7B3E89A0L, 0xD6411BD3L, 0xAE1E7E49L,
  5266. 0x00250E2DL, 0x2071B35EL, 0x226800BBL, 0x57B8E0AFL,
  5267. 0x2464369BL, 0xF009B91EL, 0x5563911DL, 0x59DFA6AAL,
  5268. 0x78C14389L, 0xD95A537FL, 0x207D5BA2L, 0x02E5B9C5L,
  5269. 0x83260376L, 0x6295CFA9L, 0x11C81968L, 0x4E734A41L,
  5270. 0xB3472DCAL, 0x7B14A94AL, 0x1B510052L, 0x9A532915L,
  5271. 0xD60F573FL, 0xBC9BC6E4L, 0x2B60A476L, 0x81E67400L,
  5272. 0x08BA6FB5L, 0x571BE91FL, 0xF296EC6BL, 0x2A0DD915L,
  5273. 0xB6636521L, 0xE7B9F9B6L, 0xFF34052EL, 0xC5855664L,
  5274. 0x53B02D5DL, 0xA99F8FA1L, 0x08BA4799L, 0x6E85076AL },
  5275. { 0x4B7A70E9L, 0xB5B32944L, 0xDB75092EL, 0xC4192623L,
  5276. 0xAD6EA6B0L, 0x49A7DF7DL, 0x9CEE60B8L, 0x8FEDB266L,
  5277. 0xECAA8C71L, 0x699A17FFL, 0x5664526CL, 0xC2B19EE1L,
  5278. 0x193602A5L, 0x75094C29L, 0xA0591340L, 0xE4183A3EL,
  5279. 0x3F54989AL, 0x5B429D65L, 0x6B8FE4D6L, 0x99F73FD6L,
  5280. 0xA1D29C07L, 0xEFE830F5L, 0x4D2D38E6L, 0xF0255DC1L,
  5281. 0x4CDD2086L, 0x8470EB26L, 0x6382E9C6L, 0x021ECC5EL,
  5282. 0x09686B3FL, 0x3EBAEFC9L, 0x3C971814L, 0x6B6A70A1L,
  5283. 0x687F3584L, 0x52A0E286L, 0xB79C5305L, 0xAA500737L,
  5284. 0x3E07841CL, 0x7FDEAE5CL, 0x8E7D44ECL, 0x5716F2B8L,
  5285. 0xB03ADA37L, 0xF0500C0DL, 0xF01C1F04L, 0x0200B3FFL,
  5286. 0xAE0CF51AL, 0x3CB574B2L, 0x25837A58L, 0xDC0921BDL,
  5287. 0xD19113F9L, 0x7CA92FF6L, 0x94324773L, 0x22F54701L,
  5288. 0x3AE5E581L, 0x37C2DADCL, 0xC8B57634L, 0x9AF3DDA7L,
  5289. 0xA9446146L, 0x0FD0030EL, 0xECC8C73EL, 0xA4751E41L,
  5290. 0xE238CD99L, 0x3BEA0E2FL, 0x3280BBA1L, 0x183EB331L,
  5291. 0x4E548B38L, 0x4F6DB908L, 0x6F420D03L, 0xF60A04BFL,
  5292. 0x2CB81290L, 0x24977C79L, 0x5679B072L, 0xBCAF89AFL,
  5293. 0xDE9A771FL, 0xD9930810L, 0xB38BAE12L, 0xDCCF3F2EL,
  5294. 0x5512721FL, 0x2E6B7124L, 0x501ADDE6L, 0x9F84CD87L,
  5295. 0x7A584718L, 0x7408DA17L, 0xBC9F9ABCL, 0xE94B7D8CL,
  5296. 0xEC7AEC3AL, 0xDB851DFAL, 0x63094366L, 0xC464C3D2L,
  5297. 0xEF1C1847L, 0x3215D908L, 0xDD433B37L, 0x24C2BA16L,
  5298. 0x12A14D43L, 0x2A65C451L, 0x50940002L, 0x133AE4DDL,
  5299. 0x71DFF89EL, 0x10314E55L, 0x81AC77D6L, 0x5F11199BL,
  5300. 0x043556F1L, 0xD7A3C76BL, 0x3C11183BL, 0x5924A509L,
  5301. 0xF28FE6EDL, 0x97F1FBFAL, 0x9EBABF2CL, 0x1E153C6EL,
  5302. 0x86E34570L, 0xEAE96FB1L, 0x860E5E0AL, 0x5A3E2AB3L,
  5303. 0x771FE71CL, 0x4E3D06FAL, 0x2965DCB9L, 0x99E71D0FL,
  5304. 0x803E89D6L, 0x5266C825L, 0x2E4CC978L, 0x9C10B36AL,
  5305. 0xC6150EBAL, 0x94E2EA78L, 0xA5FC3C53L, 0x1E0A2DF4L,
  5306. 0xF2F74EA7L, 0x361D2B3DL, 0x1939260FL, 0x19C27960L,
  5307. 0x5223A708L, 0xF71312B6L, 0xEBADFE6EL, 0xEAC31F66L,
  5308. 0xE3BC4595L, 0xA67BC883L, 0xB17F37D1L, 0x018CFF28L,
  5309. 0xC332DDEFL, 0xBE6C5AA5L, 0x65582185L, 0x68AB9802L,
  5310. 0xEECEA50FL, 0xDB2F953BL, 0x2AEF7DADL, 0x5B6E2F84L,
  5311. 0x1521B628L, 0x29076170L, 0xECDD4775L, 0x619F1510L,
  5312. 0x13CCA830L, 0xEB61BD96L, 0x0334FE1EL, 0xAA0363CFL,
  5313. 0xB5735C90L, 0x4C70A239L, 0xD59E9E0BL, 0xCBAADE14L,
  5314. 0xEECC86BCL, 0x60622CA7L, 0x9CAB5CABL, 0xB2F3846EL,
  5315. 0x648B1EAFL, 0x19BDF0CAL, 0xA02369B9L, 0x655ABB50L,
  5316. 0x40685A32L, 0x3C2AB4B3L, 0x319EE9D5L, 0xC021B8F7L,
  5317. 0x9B540B19L, 0x875FA099L, 0x95F7997EL, 0x623D7DA8L,
  5318. 0xF837889AL, 0x97E32D77L, 0x11ED935FL, 0x16681281L,
  5319. 0x0E358829L, 0xC7E61FD6L, 0x96DEDFA1L, 0x7858BA99L,
  5320. 0x57F584A5L, 0x1B227263L, 0x9B83C3FFL, 0x1AC24696L,
  5321. 0xCDB30AEBL, 0x532E3054L, 0x8FD948E4L, 0x6DBC3128L,
  5322. 0x58EBF2EFL, 0x34C6FFEAL, 0xFE28ED61L, 0xEE7C3C73L,
  5323. 0x5D4A14D9L, 0xE864B7E3L, 0x42105D14L, 0x203E13E0L,
  5324. 0x45EEE2B6L, 0xA3AAABEAL, 0xDB6C4F15L, 0xFACB4FD0L,
  5325. 0xC742F442L, 0xEF6ABBB5L, 0x654F3B1DL, 0x41CD2105L,
  5326. 0xD81E799EL, 0x86854DC7L, 0xE44B476AL, 0x3D816250L,
  5327. 0xCF62A1F2L, 0x5B8D2646L, 0xFC8883A0L, 0xC1C7B6A3L,
  5328. 0x7F1524C3L, 0x69CB7492L, 0x47848A0BL, 0x5692B285L,
  5329. 0x095BBF00L, 0xAD19489DL, 0x1462B174L, 0x23820E00L,
  5330. 0x58428D2AL, 0x0C55F5EAL, 0x1DADF43EL, 0x233F7061L,
  5331. 0x3372F092L, 0x8D937E41L, 0xD65FECF1L, 0x6C223BDBL,
  5332. 0x7CDE3759L, 0xCBEE7460L, 0x4085F2A7L, 0xCE77326EL,
  5333. 0xA6078084L, 0x19F8509EL, 0xE8EFD855L, 0x61D99735L,
  5334. 0xA969A7AAL, 0xC50C06C2L, 0x5A04ABFCL, 0x800BCADCL,
  5335. 0x9E447A2EL, 0xC3453484L, 0xFDD56705L, 0x0E1E9EC9L,
  5336. 0xDB73DBD3L, 0x105588CDL, 0x675FDA79L, 0xE3674340L,
  5337. 0xC5C43465L, 0x713E38D8L, 0x3D28F89EL, 0xF16DFF20L,
  5338. 0x153E21E7L, 0x8FB03D4AL, 0xE6E39F2BL, 0xDB83ADF7L },
  5339. { 0xE93D5A68L, 0x948140F7L, 0xF64C261CL, 0x94692934L,
  5340. 0x411520F7L, 0x7602D4F7L, 0xBCF46B2EL, 0xD4A20068L,
  5341. 0xD4082471L, 0x3320F46AL, 0x43B7D4B7L, 0x500061AFL,
  5342. 0x1E39F62EL, 0x97244546L, 0x14214F74L, 0xBF8B8840L,
  5343. 0x4D95FC1DL, 0x96B591AFL, 0x70F4DDD3L, 0x66A02F45L,
  5344. 0xBFBC09ECL, 0x03BD9785L, 0x7FAC6DD0L, 0x31CB8504L,
  5345. 0x96EB27B3L, 0x55FD3941L, 0xDA2547E6L, 0xABCA0A9AL,
  5346. 0x28507825L, 0x530429F4L, 0x0A2C86DAL, 0xE9B66DFBL,
  5347. 0x68DC1462L, 0xD7486900L, 0x680EC0A4L, 0x27A18DEEL,
  5348. 0x4F3FFEA2L, 0xE887AD8CL, 0xB58CE006L, 0x7AF4D6B6L,
  5349. 0xAACE1E7CL, 0xD3375FECL, 0xCE78A399L, 0x406B2A42L,
  5350. 0x20FE9E35L, 0xD9F385B9L, 0xEE39D7ABL, 0x3B124E8BL,
  5351. 0x1DC9FAF7L, 0x4B6D1856L, 0x26A36631L, 0xEAE397B2L,
  5352. 0x3A6EFA74L, 0xDD5B4332L, 0x6841E7F7L, 0xCA7820FBL,
  5353. 0xFB0AF54EL, 0xD8FEB397L, 0x454056ACL, 0xBA489527L,
  5354. 0x55533A3AL, 0x20838D87L, 0xFE6BA9B7L, 0xD096954BL,
  5355. 0x55A867BCL, 0xA1159A58L, 0xCCA92963L, 0x99E1DB33L,
  5356. 0xA62A4A56L, 0x3F3125F9L, 0x5EF47E1CL, 0x9029317CL,
  5357. 0xFDF8E802L, 0x04272F70L, 0x80BB155CL, 0x05282CE3L,
  5358. 0x95C11548L, 0xE4C66D22L, 0x48C1133FL, 0xC70F86DCL,
  5359. 0x07F9C9EEL, 0x41041F0FL, 0x404779A4L, 0x5D886E17L,
  5360. 0x325F51EBL, 0xD59BC0D1L, 0xF2BCC18FL, 0x41113564L,
  5361. 0x257B7834L, 0x602A9C60L, 0xDFF8E8A3L, 0x1F636C1BL,
  5362. 0x0E12B4C2L, 0x02E1329EL, 0xAF664FD1L, 0xCAD18115L,
  5363. 0x6B2395E0L, 0x333E92E1L, 0x3B240B62L, 0xEEBEB922L,
  5364. 0x85B2A20EL, 0xE6BA0D99L, 0xDE720C8CL, 0x2DA2F728L,
  5365. 0xD0127845L, 0x95B794FDL, 0x647D0862L, 0xE7CCF5F0L,
  5366. 0x5449A36FL, 0x877D48FAL, 0xC39DFD27L, 0xF33E8D1EL,
  5367. 0x0A476341L, 0x992EFF74L, 0x3A6F6EABL, 0xF4F8FD37L,
  5368. 0xA812DC60L, 0xA1EBDDF8L, 0x991BE14CL, 0xDB6E6B0DL,
  5369. 0xC67B5510L, 0x6D672C37L, 0x2765D43BL, 0xDCD0E804L,
  5370. 0xF1290DC7L, 0xCC00FFA3L, 0xB5390F92L, 0x690FED0BL,
  5371. 0x667B9FFBL, 0xCEDB7D9CL, 0xA091CF0BL, 0xD9155EA3L,
  5372. 0xBB132F88L, 0x515BAD24L, 0x7B9479BFL, 0x763BD6EBL,
  5373. 0x37392EB3L, 0xCC115979L, 0x8026E297L, 0xF42E312DL,
  5374. 0x6842ADA7L, 0xC66A2B3BL, 0x12754CCCL, 0x782EF11CL,
  5375. 0x6A124237L, 0xB79251E7L, 0x06A1BBE6L, 0x4BFB6350L,
  5376. 0x1A6B1018L, 0x11CAEDFAL, 0x3D25BDD8L, 0xE2E1C3C9L,
  5377. 0x44421659L, 0x0A121386L, 0xD90CEC6EL, 0xD5ABEA2AL,
  5378. 0x64AF674EL, 0xDA86A85FL, 0xBEBFE988L, 0x64E4C3FEL,
  5379. 0x9DBC8057L, 0xF0F7C086L, 0x60787BF8L, 0x6003604DL,
  5380. 0xD1FD8346L, 0xF6381FB0L, 0x7745AE04L, 0xD736FCCCL,
  5381. 0x83426B33L, 0xF01EAB71L, 0xB0804187L, 0x3C005E5FL,
  5382. 0x77A057BEL, 0xBDE8AE24L, 0x55464299L, 0xBF582E61L,
  5383. 0x4E58F48FL, 0xF2DDFDA2L, 0xF474EF38L, 0x8789BDC2L,
  5384. 0x5366F9C3L, 0xC8B38E74L, 0xB475F255L, 0x46FCD9B9L,
  5385. 0x7AEB2661L, 0x8B1DDF84L, 0x846A0E79L, 0x915F95E2L,
  5386. 0x466E598EL, 0x20B45770L, 0x8CD55591L, 0xC902DE4CL,
  5387. 0xB90BACE1L, 0xBB8205D0L, 0x11A86248L, 0x7574A99EL,
  5388. 0xB77F19B6L, 0xE0A9DC09L, 0x662D09A1L, 0xC4324633L,
  5389. 0xE85A1F02L, 0x09F0BE8CL, 0x4A99A025L, 0x1D6EFE10L,
  5390. 0x1AB93D1DL, 0x0BA5A4DFL, 0xA186F20FL, 0x2868F169L,
  5391. 0xDCB7DA83L, 0x573906FEL, 0xA1E2CE9BL, 0x4FCD7F52L,
  5392. 0x50115E01L, 0xA70683FAL, 0xA002B5C4L, 0x0DE6D027L,
  5393. 0x9AF88C27L, 0x773F8641L, 0xC3604C06L, 0x61A806B5L,
  5394. 0xF0177A28L, 0xC0F586E0L, 0x006058AAL, 0x30DC7D62L,
  5395. 0x11E69ED7L, 0x2338EA63L, 0x53C2DD94L, 0xC2C21634L,
  5396. 0xBBCBEE56L, 0x90BCB6DEL, 0xEBFC7DA1L, 0xCE591D76L,
  5397. 0x6F05E409L, 0x4B7C0188L, 0x39720A3DL, 0x7C927C24L,
  5398. 0x86E3725FL, 0x724D9DB9L, 0x1AC15BB4L, 0xD39EB8FCL,
  5399. 0xED545578L, 0x08FCA5B5L, 0xD83D7CD3L, 0x4DAD0FC4L,
  5400. 0x1E50EF5EL, 0xB161E6F8L, 0xA28514D9L, 0x6C51133CL,
  5401. 0x6FD5C7E7L, 0x56E14EC4L, 0x362ABFCEL, 0xDDC6C837L,
  5402. 0xD79A3234L, 0x92638212L, 0x670EFA8EL, 0x406000E0L },
  5403. { 0x3A39CE37L, 0xD3FAF5CFL, 0xABC27737L, 0x5AC52D1BL,
  5404. 0x5CB0679EL, 0x4FA33742L, 0xD3822740L, 0x99BC9BBEL,
  5405. 0xD5118E9DL, 0xBF0F7315L, 0xD62D1C7EL, 0xC700C47BL,
  5406. 0xB78C1B6BL, 0x21A19045L, 0xB26EB1BEL, 0x6A366EB4L,
  5407. 0x5748AB2FL, 0xBC946E79L, 0xC6A376D2L, 0x6549C2C8L,
  5408. 0x530FF8EEL, 0x468DDE7DL, 0xD5730A1DL, 0x4CD04DC6L,
  5409. 0x2939BBDBL, 0xA9BA4650L, 0xAC9526E8L, 0xBE5EE304L,
  5410. 0xA1FAD5F0L, 0x6A2D519AL, 0x63EF8CE2L, 0x9A86EE22L,
  5411. 0xC089C2B8L, 0x43242EF6L, 0xA51E03AAL, 0x9CF2D0A4L,
  5412. 0x83C061BAL, 0x9BE96A4DL, 0x8FE51550L, 0xBA645BD6L,
  5413. 0x2826A2F9L, 0xA73A3AE1L, 0x4BA99586L, 0xEF5562E9L,
  5414. 0xC72FEFD3L, 0xF752F7DAL, 0x3F046F69L, 0x77FA0A59L,
  5415. 0x80E4A915L, 0x87B08601L, 0x9B09E6ADL, 0x3B3EE593L,
  5416. 0xE990FD5AL, 0x9E34D797L, 0x2CF0B7D9L, 0x022B8B51L,
  5417. 0x96D5AC3AL, 0x017DA67DL, 0xD1CF3ED6L, 0x7C7D2D28L,
  5418. 0x1F9F25CFL, 0xADF2B89BL, 0x5AD6B472L, 0x5A88F54CL,
  5419. 0xE029AC71L, 0xE019A5E6L, 0x47B0ACFDL, 0xED93FA9BL,
  5420. 0xE8D3C48DL, 0x283B57CCL, 0xF8D56629L, 0x79132E28L,
  5421. 0x785F0191L, 0xED756055L, 0xF7960E44L, 0xE3D35E8CL,
  5422. 0x15056DD4L, 0x88F46DBAL, 0x03A16125L, 0x0564F0BDL,
  5423. 0xC3EB9E15L, 0x3C9057A2L, 0x97271AECL, 0xA93A072AL,
  5424. 0x1B3F6D9BL, 0x1E6321F5L, 0xF59C66FBL, 0x26DCF319L,
  5425. 0x7533D928L, 0xB155FDF5L, 0x03563482L, 0x8ABA3CBBL,
  5426. 0x28517711L, 0xC20AD9F8L, 0xABCC5167L, 0xCCAD925FL,
  5427. 0x4DE81751L, 0x3830DC8EL, 0x379D5862L, 0x9320F991L,
  5428. 0xEA7A90C2L, 0xFB3E7BCEL, 0x5121CE64L, 0x774FBE32L,
  5429. 0xA8B6E37EL, 0xC3293D46L, 0x48DE5369L, 0x6413E680L,
  5430. 0xA2AE0810L, 0xDD6DB224L, 0x69852DFDL, 0x09072166L,
  5431. 0xB39A460AL, 0x6445C0DDL, 0x586CDECFL, 0x1C20C8AEL,
  5432. 0x5BBEF7DDL, 0x1B588D40L, 0xCCD2017FL, 0x6BB4E3BBL,
  5433. 0xDDA26A7EL, 0x3A59FF45L, 0x3E350A44L, 0xBCB4CDD5L,
  5434. 0x72EACEA8L, 0xFA6484BBL, 0x8D6612AEL, 0xBF3C6F47L,
  5435. 0xD29BE463L, 0x542F5D9EL, 0xAEC2771BL, 0xF64E6370L,
  5436. 0x740E0D8DL, 0xE75B1357L, 0xF8721671L, 0xAF537D5DL,
  5437. 0x4040CB08L, 0x4EB4E2CCL, 0x34D2466AL, 0x0115AF84L,
  5438. 0xE1B00428L, 0x95983A1DL, 0x06B89FB4L, 0xCE6EA048L,
  5439. 0x6F3F3B82L, 0x3520AB82L, 0x011A1D4BL, 0x277227F8L,
  5440. 0x611560B1L, 0xE7933FDCL, 0xBB3A792BL, 0x344525BDL,
  5441. 0xA08839E1L, 0x51CE794BL, 0x2F32C9B7L, 0xA01FBAC9L,
  5442. 0xE01CC87EL, 0xBCC7D1F6L, 0xCF0111C3L, 0xA1E8AAC7L,
  5443. 0x1A908749L, 0xD44FBD9AL, 0xD0DADECBL, 0xD50ADA38L,
  5444. 0x0339C32AL, 0xC6913667L, 0x8DF9317CL, 0xE0B12B4FL,
  5445. 0xF79E59B7L, 0x43F5BB3AL, 0xF2D519FFL, 0x27D9459CL,
  5446. 0xBF97222CL, 0x15E6FC2AL, 0x0F91FC71L, 0x9B941525L,
  5447. 0xFAE59361L, 0xCEB69CEBL, 0xC2A86459L, 0x12BAA8D1L,
  5448. 0xB6C1075EL, 0xE3056A0CL, 0x10D25065L, 0xCB03A442L,
  5449. 0xE0EC6E0EL, 0x1698DB3BL, 0x4C98A0BEL, 0x3278E964L,
  5450. 0x9F1F9532L, 0xE0D392DFL, 0xD3A0342BL, 0x8971F21EL,
  5451. 0x1B0A7441L, 0x4BA3348CL, 0xC5BE7120L, 0xC37632D8L,
  5452. 0xDF359F8DL, 0x9B992F2EL, 0xE60B6F47L, 0x0FE3F11DL,
  5453. 0xE54CDA54L, 0x1EDAD891L, 0xCE6279CFL, 0xCD3E7E6FL,
  5454. 0x1618B166L, 0xFD2C1D05L, 0x848FD2C5L, 0xF6FB2299L,
  5455. 0xF523F357L, 0xA6327623L, 0x93A83531L, 0x56CCCD02L,
  5456. 0xACF08162L, 0x5A75EBB5L, 0x6E163697L, 0x88D273CCL,
  5457. 0xDE966292L, 0x81B949D0L, 0x4C50901BL, 0x71C65614L,
  5458. 0xE6C6C7BDL, 0x327A140AL, 0x45E1D006L, 0xC3F27B9AL,
  5459. 0xC9AA53FDL, 0x62A80F00L, 0xBB25BFE2L, 0x35BDD2F6L,
  5460. 0x71126905L, 0xB2040222L, 0xB6CBCF7CL, 0xCD769C2BL,
  5461. 0x53113EC0L, 0x1640E3D3L, 0x38ABBD60L, 0x2547ADF0L,
  5462. 0xBA38209CL, 0xF746CE76L, 0x77AFA1C5L, 0x20756060L,
  5463. 0x85CBFE4EL, 0x8AE88DD8L, 0x7AAAF9B0L, 0x4CF9AA7EL,
  5464. 0x1948C25CL, 0x02FB8A8CL, 0x01C36AE4L, 0xD6EBE1F9L,
  5465. 0x90D4F869L, 0xA65CDEA0L, 0x3F09252DL, 0xC208E69FL,
  5466. 0xB74E6132L, 0xCE77E25BL, 0x578FDFE3L, 0x3AC372E6L }
  5467. };
  5468. #endif /* !MBEDTLS_BLOWFISH_ALT */
  5469. #endif /* MBEDTLS_BLOWFISH_C */
  5470. /*
  5471. Amalgamated build undefines
  5472. */
  5473. #undef ADD
  5474. #undef BC
  5475. #undef BEFORE_COLON
  5476. #undef F
  5477. #undef F0
  5478. #undef F1
  5479. #undef F2
  5480. #undef F3
  5481. #undef F4
  5482. #undef F5
  5483. #undef FSb
  5484. #undef K
  5485. #undef KK
  5486. #undef P
  5487. #undef R
  5488. #undef ROTR
  5489. #undef S
  5490. #undef S0
  5491. #undef S1
  5492. #undef S2
  5493. #undef S3
  5494. #undef SAFE_SNPRINTF
  5495. #undef SHR
  5496. #undef close
  5497. #undef read
  5498. #undef supported_init
  5499. #undef write
  5500. /********* Start of file library/camellia.c ************/
  5501. /*
  5502. * Camellia implementation
  5503. *
  5504. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5505. * SPDX-License-Identifier: Apache-2.0
  5506. *
  5507. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  5508. * not use this file except in compliance with the License.
  5509. * You may obtain a copy of the License at
  5510. *
  5511. * http://www.apache.org/licenses/LICENSE-2.0
  5512. *
  5513. * Unless required by applicable law or agreed to in writing, software
  5514. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  5515. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5516. * See the License for the specific language governing permissions and
  5517. * limitations under the License.
  5518. *
  5519. * This file is part of mbed TLS (https://tls.mbed.org)
  5520. */
  5521. /*
  5522. * The Camellia block cipher was designed by NTT and Mitsubishi Electric
  5523. * Corporation.
  5524. *
  5525. * http://info.isl.ntt.co.jp/crypt/eng/camellia/dl/01espec.pdf
  5526. */
  5527. #if !defined(MBEDTLS_CONFIG_FILE)
  5528. #else
  5529. #endif
  5530. #if defined(MBEDTLS_CAMELLIA_C)
  5531. #include <string.h>
  5532. #if defined(MBEDTLS_SELF_TEST)
  5533. #if defined(MBEDTLS_PLATFORM_C)
  5534. #else
  5535. #include <stdio.h>
  5536. #define mbedtls_printf printf
  5537. #endif /* MBEDTLS_PLATFORM_C */
  5538. #endif /* MBEDTLS_SELF_TEST */
  5539. #if !defined(MBEDTLS_CAMELLIA_ALT)
  5540. /* Implementation that should never be optimized out by the compiler */
  5541. /* zeroize was here */
  5542. /*
  5543. * 32-bit integer manipulation macros (big endian)
  5544. */
  5545. #ifndef GET_UINT32_BE
  5546. #define GET_UINT32_BE(n,b,i) \
  5547. { \
  5548. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  5549. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  5550. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  5551. | ( (uint32_t) (b)[(i) + 3] ); \
  5552. }
  5553. #endif
  5554. #ifndef PUT_UINT32_BE
  5555. #define PUT_UINT32_BE(n,b,i) \
  5556. { \
  5557. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  5558. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  5559. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  5560. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  5561. }
  5562. #endif
  5563. static const unsigned char SIGMA_CHARS[6][8] =
  5564. {
  5565. { 0xa0, 0x9e, 0x66, 0x7f, 0x3b, 0xcc, 0x90, 0x8b },
  5566. { 0xb6, 0x7a, 0xe8, 0x58, 0x4c, 0xaa, 0x73, 0xb2 },
  5567. { 0xc6, 0xef, 0x37, 0x2f, 0xe9, 0x4f, 0x82, 0xbe },
  5568. { 0x54, 0xff, 0x53, 0xa5, 0xf1, 0xd3, 0x6f, 0x1c },
  5569. { 0x10, 0xe5, 0x27, 0xfa, 0xde, 0x68, 0x2d, 0x1d },
  5570. { 0xb0, 0x56, 0x88, 0xc2, 0xb3, 0xe6, 0xc1, 0xfd }
  5571. };
  5572. #if defined(MBEDTLS_CAMELLIA_SMALL_MEMORY)
  5573. static const unsigned char FSb[256] =
  5574. {
  5575. 112,130, 44,236,179, 39,192,229,228,133, 87, 53,234, 12,174, 65,
  5576. 35,239,107,147, 69, 25,165, 33,237, 14, 79, 78, 29,101,146,189,
  5577. 134,184,175,143,124,235, 31,206, 62, 48,220, 95, 94,197, 11, 26,
  5578. 166,225, 57,202,213, 71, 93, 61,217, 1, 90,214, 81, 86,108, 77,
  5579. 139, 13,154,102,251,204,176, 45,116, 18, 43, 32,240,177,132,153,
  5580. 223, 76,203,194, 52,126,118, 5,109,183,169, 49,209, 23, 4,215,
  5581. 20, 88, 58, 97,222, 27, 17, 28, 50, 15,156, 22, 83, 24,242, 34,
  5582. 254, 68,207,178,195,181,122,145, 36, 8,232,168, 96,252,105, 80,
  5583. 170,208,160,125,161,137, 98,151, 84, 91, 30,149,224,255,100,210,
  5584. 16,196, 0, 72,163,247,117,219,138, 3,230,218, 9, 63,221,148,
  5585. 135, 92,131, 2,205, 74,144, 51,115,103,246,243,157,127,191,226,
  5586. 82,155,216, 38,200, 55,198, 59,129,150,111, 75, 19,190, 99, 46,
  5587. 233,121,167,140,159,110,188,142, 41,245,249,182, 47,253,180, 89,
  5588. 120,152, 6,106,231, 70,113,186,212, 37,171, 66,136,162,141,250,
  5589. 114, 7,185, 85,248,238,172, 10, 54, 73, 42,104, 60, 56,241,164,
  5590. 64, 40,211,123,187,201, 67,193, 21,227,173,244,119,199,128,158
  5591. };
  5592. #define SBOX1(n) FSb[(n)]
  5593. #define SBOX2(n) (unsigned char)((FSb[(n)] >> 7 ^ FSb[(n)] << 1) & 0xff)
  5594. #define SBOX3(n) (unsigned char)((FSb[(n)] >> 1 ^ FSb[(n)] << 7) & 0xff)
  5595. #define SBOX4(n) FSb[((n) << 1 ^ (n) >> 7) &0xff]
  5596. #else /* MBEDTLS_CAMELLIA_SMALL_MEMORY */
  5597. static const unsigned char FSb[256] =
  5598. {
  5599. 112, 130, 44, 236, 179, 39, 192, 229, 228, 133, 87, 53, 234, 12, 174, 65,
  5600. 35, 239, 107, 147, 69, 25, 165, 33, 237, 14, 79, 78, 29, 101, 146, 189,
  5601. 134, 184, 175, 143, 124, 235, 31, 206, 62, 48, 220, 95, 94, 197, 11, 26,
  5602. 166, 225, 57, 202, 213, 71, 93, 61, 217, 1, 90, 214, 81, 86, 108, 77,
  5603. 139, 13, 154, 102, 251, 204, 176, 45, 116, 18, 43, 32, 240, 177, 132, 153,
  5604. 223, 76, 203, 194, 52, 126, 118, 5, 109, 183, 169, 49, 209, 23, 4, 215,
  5605. 20, 88, 58, 97, 222, 27, 17, 28, 50, 15, 156, 22, 83, 24, 242, 34,
  5606. 254, 68, 207, 178, 195, 181, 122, 145, 36, 8, 232, 168, 96, 252, 105, 80,
  5607. 170, 208, 160, 125, 161, 137, 98, 151, 84, 91, 30, 149, 224, 255, 100, 210,
  5608. 16, 196, 0, 72, 163, 247, 117, 219, 138, 3, 230, 218, 9, 63, 221, 148,
  5609. 135, 92, 131, 2, 205, 74, 144, 51, 115, 103, 246, 243, 157, 127, 191, 226,
  5610. 82, 155, 216, 38, 200, 55, 198, 59, 129, 150, 111, 75, 19, 190, 99, 46,
  5611. 233, 121, 167, 140, 159, 110, 188, 142, 41, 245, 249, 182, 47, 253, 180, 89,
  5612. 120, 152, 6, 106, 231, 70, 113, 186, 212, 37, 171, 66, 136, 162, 141, 250,
  5613. 114, 7, 185, 85, 248, 238, 172, 10, 54, 73, 42, 104, 60, 56, 241, 164,
  5614. 64, 40, 211, 123, 187, 201, 67, 193, 21, 227, 173, 244, 119, 199, 128, 158
  5615. };
  5616. static const unsigned char FSb2[256] =
  5617. {
  5618. 224, 5, 88, 217, 103, 78, 129, 203, 201, 11, 174, 106, 213, 24, 93, 130,
  5619. 70, 223, 214, 39, 138, 50, 75, 66, 219, 28, 158, 156, 58, 202, 37, 123,
  5620. 13, 113, 95, 31, 248, 215, 62, 157, 124, 96, 185, 190, 188, 139, 22, 52,
  5621. 77, 195, 114, 149, 171, 142, 186, 122, 179, 2, 180, 173, 162, 172, 216, 154,
  5622. 23, 26, 53, 204, 247, 153, 97, 90, 232, 36, 86, 64, 225, 99, 9, 51,
  5623. 191, 152, 151, 133, 104, 252, 236, 10, 218, 111, 83, 98, 163, 46, 8, 175,
  5624. 40, 176, 116, 194, 189, 54, 34, 56, 100, 30, 57, 44, 166, 48, 229, 68,
  5625. 253, 136, 159, 101, 135, 107, 244, 35, 72, 16, 209, 81, 192, 249, 210, 160,
  5626. 85, 161, 65, 250, 67, 19, 196, 47, 168, 182, 60, 43, 193, 255, 200, 165,
  5627. 32, 137, 0, 144, 71, 239, 234, 183, 21, 6, 205, 181, 18, 126, 187, 41,
  5628. 15, 184, 7, 4, 155, 148, 33, 102, 230, 206, 237, 231, 59, 254, 127, 197,
  5629. 164, 55, 177, 76, 145, 110, 141, 118, 3, 45, 222, 150, 38, 125, 198, 92,
  5630. 211, 242, 79, 25, 63, 220, 121, 29, 82, 235, 243, 109, 94, 251, 105, 178,
  5631. 240, 49, 12, 212, 207, 140, 226, 117, 169, 74, 87, 132, 17, 69, 27, 245,
  5632. 228, 14, 115, 170, 241, 221, 89, 20, 108, 146, 84, 208, 120, 112, 227, 73,
  5633. 128, 80, 167, 246, 119, 147, 134, 131, 42, 199, 91, 233, 238, 143, 1, 61
  5634. };
  5635. static const unsigned char FSb3[256] =
  5636. {
  5637. 56, 65, 22, 118, 217, 147, 96, 242, 114, 194, 171, 154, 117, 6, 87, 160,
  5638. 145, 247, 181, 201, 162, 140, 210, 144, 246, 7, 167, 39, 142, 178, 73, 222,
  5639. 67, 92, 215, 199, 62, 245, 143, 103, 31, 24, 110, 175, 47, 226, 133, 13,
  5640. 83, 240, 156, 101, 234, 163, 174, 158, 236, 128, 45, 107, 168, 43, 54, 166,
  5641. 197, 134, 77, 51, 253, 102, 88, 150, 58, 9, 149, 16, 120, 216, 66, 204,
  5642. 239, 38, 229, 97, 26, 63, 59, 130, 182, 219, 212, 152, 232, 139, 2, 235,
  5643. 10, 44, 29, 176, 111, 141, 136, 14, 25, 135, 78, 11, 169, 12, 121, 17,
  5644. 127, 34, 231, 89, 225, 218, 61, 200, 18, 4, 116, 84, 48, 126, 180, 40,
  5645. 85, 104, 80, 190, 208, 196, 49, 203, 42, 173, 15, 202, 112, 255, 50, 105,
  5646. 8, 98, 0, 36, 209, 251, 186, 237, 69, 129, 115, 109, 132, 159, 238, 74,
  5647. 195, 46, 193, 1, 230, 37, 72, 153, 185, 179, 123, 249, 206, 191, 223, 113,
  5648. 41, 205, 108, 19, 100, 155, 99, 157, 192, 75, 183, 165, 137, 95, 177, 23,
  5649. 244, 188, 211, 70, 207, 55, 94, 71, 148, 250, 252, 91, 151, 254, 90, 172,
  5650. 60, 76, 3, 53, 243, 35, 184, 93, 106, 146, 213, 33, 68, 81, 198, 125,
  5651. 57, 131, 220, 170, 124, 119, 86, 5, 27, 164, 21, 52, 30, 28, 248, 82,
  5652. 32, 20, 233, 189, 221, 228, 161, 224, 138, 241, 214, 122, 187, 227, 64, 79
  5653. };
  5654. static const unsigned char FSb4[256] =
  5655. {
  5656. 112, 44, 179, 192, 228, 87, 234, 174, 35, 107, 69, 165, 237, 79, 29, 146,
  5657. 134, 175, 124, 31, 62, 220, 94, 11, 166, 57, 213, 93, 217, 90, 81, 108,
  5658. 139, 154, 251, 176, 116, 43, 240, 132, 223, 203, 52, 118, 109, 169, 209, 4,
  5659. 20, 58, 222, 17, 50, 156, 83, 242, 254, 207, 195, 122, 36, 232, 96, 105,
  5660. 170, 160, 161, 98, 84, 30, 224, 100, 16, 0, 163, 117, 138, 230, 9, 221,
  5661. 135, 131, 205, 144, 115, 246, 157, 191, 82, 216, 200, 198, 129, 111, 19, 99,
  5662. 233, 167, 159, 188, 41, 249, 47, 180, 120, 6, 231, 113, 212, 171, 136, 141,
  5663. 114, 185, 248, 172, 54, 42, 60, 241, 64, 211, 187, 67, 21, 173, 119, 128,
  5664. 130, 236, 39, 229, 133, 53, 12, 65, 239, 147, 25, 33, 14, 78, 101, 189,
  5665. 184, 143, 235, 206, 48, 95, 197, 26, 225, 202, 71, 61, 1, 214, 86, 77,
  5666. 13, 102, 204, 45, 18, 32, 177, 153, 76, 194, 126, 5, 183, 49, 23, 215,
  5667. 88, 97, 27, 28, 15, 22, 24, 34, 68, 178, 181, 145, 8, 168, 252, 80,
  5668. 208, 125, 137, 151, 91, 149, 255, 210, 196, 72, 247, 219, 3, 218, 63, 148,
  5669. 92, 2, 74, 51, 103, 243, 127, 226, 155, 38, 55, 59, 150, 75, 190, 46,
  5670. 121, 140, 110, 142, 245, 182, 253, 89, 152, 106, 70, 186, 37, 66, 162, 250,
  5671. 7, 85, 238, 10, 73, 104, 56, 164, 40, 123, 201, 193, 227, 244, 199, 158
  5672. };
  5673. #define SBOX1(n) FSb[(n)]
  5674. #define SBOX2(n) FSb2[(n)]
  5675. #define SBOX3(n) FSb3[(n)]
  5676. #define SBOX4(n) FSb4[(n)]
  5677. #endif /* MBEDTLS_CAMELLIA_SMALL_MEMORY */
  5678. static const unsigned char shifts[2][4][4] =
  5679. {
  5680. {
  5681. { 1, 1, 1, 1 }, /* KL */
  5682. { 0, 0, 0, 0 }, /* KR */
  5683. { 1, 1, 1, 1 }, /* KA */
  5684. { 0, 0, 0, 0 } /* KB */
  5685. },
  5686. {
  5687. { 1, 0, 1, 1 }, /* KL */
  5688. { 1, 1, 0, 1 }, /* KR */
  5689. { 1, 1, 1, 0 }, /* KA */
  5690. { 1, 1, 0, 1 } /* KB */
  5691. }
  5692. };
  5693. static const signed char indexes[2][4][20] =
  5694. {
  5695. {
  5696. { 0, 1, 2, 3, 8, 9, 10, 11, 38, 39,
  5697. 36, 37, 23, 20, 21, 22, 27, -1, -1, 26 }, /* KL -> RK */
  5698. { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  5699. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, /* KR -> RK */
  5700. { 4, 5, 6, 7, 12, 13, 14, 15, 16, 17,
  5701. 18, 19, -1, 24, 25, -1, 31, 28, 29, 30 }, /* KA -> RK */
  5702. { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  5703. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } /* KB -> RK */
  5704. },
  5705. {
  5706. { 0, 1, 2, 3, 61, 62, 63, 60, -1, -1,
  5707. -1, -1, 27, 24, 25, 26, 35, 32, 33, 34 }, /* KL -> RK */
  5708. { -1, -1, -1, -1, 8, 9, 10, 11, 16, 17,
  5709. 18, 19, -1, -1, -1, -1, 39, 36, 37, 38 }, /* KR -> RK */
  5710. { -1, -1, -1, -1, 12, 13, 14, 15, 58, 59,
  5711. 56, 57, 31, 28, 29, 30, -1, -1, -1, -1 }, /* KA -> RK */
  5712. { 4, 5, 6, 7, 65, 66, 67, 64, 20, 21,
  5713. 22, 23, -1, -1, -1, -1, 43, 40, 41, 42 } /* KB -> RK */
  5714. }
  5715. };
  5716. static const signed char transposes[2][20] =
  5717. {
  5718. {
  5719. 21, 22, 23, 20,
  5720. -1, -1, -1, -1,
  5721. 18, 19, 16, 17,
  5722. 11, 8, 9, 10,
  5723. 15, 12, 13, 14
  5724. },
  5725. {
  5726. 25, 26, 27, 24,
  5727. 29, 30, 31, 28,
  5728. 18, 19, 16, 17,
  5729. -1, -1, -1, -1,
  5730. -1, -1, -1, -1
  5731. }
  5732. };
  5733. /* Shift macro for 128 bit strings with rotation smaller than 32 bits (!) */
  5734. #define ROTL(DEST, SRC, SHIFT) \
  5735. { \
  5736. (DEST)[0] = (SRC)[0] << (SHIFT) ^ (SRC)[1] >> (32 - (SHIFT)); \
  5737. (DEST)[1] = (SRC)[1] << (SHIFT) ^ (SRC)[2] >> (32 - (SHIFT)); \
  5738. (DEST)[2] = (SRC)[2] << (SHIFT) ^ (SRC)[3] >> (32 - (SHIFT)); \
  5739. (DEST)[3] = (SRC)[3] << (SHIFT) ^ (SRC)[0] >> (32 - (SHIFT)); \
  5740. }
  5741. #define FL(XL, XR, KL, KR) \
  5742. { \
  5743. (XR) = ((((XL) & (KL)) << 1) | (((XL) & (KL)) >> 31)) ^ (XR); \
  5744. (XL) = ((XR) | (KR)) ^ (XL); \
  5745. }
  5746. #define FLInv(YL, YR, KL, KR) \
  5747. { \
  5748. (YL) = ((YR) | (KR)) ^ (YL); \
  5749. (YR) = ((((YL) & (KL)) << 1) | (((YL) & (KL)) >> 31)) ^ (YR); \
  5750. }
  5751. #define SHIFT_AND_PLACE(INDEX, OFFSET) \
  5752. { \
  5753. TK[0] = KC[(OFFSET) * 4 + 0]; \
  5754. TK[1] = KC[(OFFSET) * 4 + 1]; \
  5755. TK[2] = KC[(OFFSET) * 4 + 2]; \
  5756. TK[3] = KC[(OFFSET) * 4 + 3]; \
  5757. \
  5758. for( i = 1; i <= 4; i++ ) \
  5759. if( shifts[(INDEX)][(OFFSET)][i -1] ) \
  5760. ROTL(TK + i * 4, TK, ( 15 * i ) % 32); \
  5761. \
  5762. for( i = 0; i < 20; i++ ) \
  5763. if( indexes[(INDEX)][(OFFSET)][i] != -1 ) { \
  5764. RK[indexes[(INDEX)][(OFFSET)][i]] = TK[ i ]; \
  5765. } \
  5766. }
  5767. static void camellia_feistel( const uint32_t x[2], const uint32_t k[2],
  5768. uint32_t z[2])
  5769. {
  5770. uint32_t I0, I1;
  5771. I0 = x[0] ^ k[0];
  5772. I1 = x[1] ^ k[1];
  5773. I0 = ((uint32_t) SBOX1((I0 >> 24) & 0xFF) << 24) |
  5774. ((uint32_t) SBOX2((I0 >> 16) & 0xFF) << 16) |
  5775. ((uint32_t) SBOX3((I0 >> 8) & 0xFF) << 8) |
  5776. ((uint32_t) SBOX4((I0 ) & 0xFF) );
  5777. I1 = ((uint32_t) SBOX2((I1 >> 24) & 0xFF) << 24) |
  5778. ((uint32_t) SBOX3((I1 >> 16) & 0xFF) << 16) |
  5779. ((uint32_t) SBOX4((I1 >> 8) & 0xFF) << 8) |
  5780. ((uint32_t) SBOX1((I1 ) & 0xFF) );
  5781. I0 ^= (I1 << 8) | (I1 >> 24);
  5782. I1 ^= (I0 << 16) | (I0 >> 16);
  5783. I0 ^= (I1 >> 8) | (I1 << 24);
  5784. I1 ^= (I0 >> 8) | (I0 << 24);
  5785. z[0] ^= I1;
  5786. z[1] ^= I0;
  5787. }
  5788. void mbedtls_camellia_init( mbedtls_camellia_context *ctx )
  5789. {
  5790. memset( ctx, 0, sizeof( mbedtls_camellia_context ) );
  5791. }
  5792. void mbedtls_camellia_free( mbedtls_camellia_context *ctx )
  5793. {
  5794. if( ctx == NULL )
  5795. return;
  5796. mbedtls_zeroize( ctx, sizeof( mbedtls_camellia_context ) );
  5797. }
  5798. /*
  5799. * Camellia key schedule (encryption)
  5800. */
  5801. int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned char *key,
  5802. unsigned int keybits )
  5803. {
  5804. int idx;
  5805. size_t i;
  5806. uint32_t *RK;
  5807. unsigned char t[64];
  5808. uint32_t SIGMA[6][2];
  5809. uint32_t KC[16];
  5810. uint32_t TK[20];
  5811. RK = ctx->rk;
  5812. memset( t, 0, 64 );
  5813. memset( RK, 0, sizeof(ctx->rk) );
  5814. switch( keybits )
  5815. {
  5816. case 128: ctx->nr = 3; idx = 0; break;
  5817. case 192:
  5818. case 256: ctx->nr = 4; idx = 1; break;
  5819. default : return( MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH );
  5820. }
  5821. for( i = 0; i < keybits / 8; ++i )
  5822. t[i] = key[i];
  5823. if( keybits == 192 ) {
  5824. for( i = 0; i < 8; i++ )
  5825. t[24 + i] = ~t[16 + i];
  5826. }
  5827. /*
  5828. * Prepare SIGMA values
  5829. */
  5830. for( i = 0; i < 6; i++ ) {
  5831. GET_UINT32_BE( SIGMA[i][0], SIGMA_CHARS[i], 0 );
  5832. GET_UINT32_BE( SIGMA[i][1], SIGMA_CHARS[i], 4 );
  5833. }
  5834. /*
  5835. * Key storage in KC
  5836. * Order: KL, KR, KA, KB
  5837. */
  5838. memset( KC, 0, sizeof(KC) );
  5839. /* Store KL, KR */
  5840. for( i = 0; i < 8; i++ )
  5841. GET_UINT32_BE( KC[i], t, i * 4 );
  5842. /* Generate KA */
  5843. for( i = 0; i < 4; ++i )
  5844. KC[8 + i] = KC[i] ^ KC[4 + i];
  5845. camellia_feistel( KC + 8, SIGMA[0], KC + 10 );
  5846. camellia_feistel( KC + 10, SIGMA[1], KC + 8 );
  5847. for( i = 0; i < 4; ++i )
  5848. KC[8 + i] ^= KC[i];
  5849. camellia_feistel( KC + 8, SIGMA[2], KC + 10 );
  5850. camellia_feistel( KC + 10, SIGMA[3], KC + 8 );
  5851. if( keybits > 128 ) {
  5852. /* Generate KB */
  5853. for( i = 0; i < 4; ++i )
  5854. KC[12 + i] = KC[4 + i] ^ KC[8 + i];
  5855. camellia_feistel( KC + 12, SIGMA[4], KC + 14 );
  5856. camellia_feistel( KC + 14, SIGMA[5], KC + 12 );
  5857. }
  5858. /*
  5859. * Generating subkeys
  5860. */
  5861. /* Manipulating KL */
  5862. SHIFT_AND_PLACE( idx, 0 );
  5863. /* Manipulating KR */
  5864. if( keybits > 128 ) {
  5865. SHIFT_AND_PLACE( idx, 1 );
  5866. }
  5867. /* Manipulating KA */
  5868. SHIFT_AND_PLACE( idx, 2 );
  5869. /* Manipulating KB */
  5870. if( keybits > 128 ) {
  5871. SHIFT_AND_PLACE( idx, 3 );
  5872. }
  5873. /* Do transpositions */
  5874. for( i = 0; i < 20; i++ ) {
  5875. if( transposes[idx][i] != -1 ) {
  5876. RK[32 + 12 * idx + i] = RK[transposes[idx][i]];
  5877. }
  5878. }
  5879. return( 0 );
  5880. }
  5881. /*
  5882. * Camellia key schedule (decryption)
  5883. */
  5884. int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, const unsigned char *key,
  5885. unsigned int keybits )
  5886. {
  5887. int idx, ret;
  5888. size_t i;
  5889. mbedtls_camellia_context cty;
  5890. uint32_t *RK;
  5891. uint32_t *SK;
  5892. mbedtls_camellia_init( &cty );
  5893. /* Also checks keybits */
  5894. if( ( ret = mbedtls_camellia_setkey_enc( &cty, key, keybits ) ) != 0 )
  5895. goto exit;
  5896. ctx->nr = cty.nr;
  5897. idx = ( ctx->nr == 4 );
  5898. RK = ctx->rk;
  5899. SK = cty.rk + 24 * 2 + 8 * idx * 2;
  5900. *RK++ = *SK++;
  5901. *RK++ = *SK++;
  5902. *RK++ = *SK++;
  5903. *RK++ = *SK++;
  5904. for( i = 22 + 8 * idx, SK -= 6; i > 0; i--, SK -= 4 )
  5905. {
  5906. *RK++ = *SK++;
  5907. *RK++ = *SK++;
  5908. }
  5909. SK -= 2;
  5910. *RK++ = *SK++;
  5911. *RK++ = *SK++;
  5912. *RK++ = *SK++;
  5913. *RK++ = *SK++;
  5914. exit:
  5915. mbedtls_camellia_free( &cty );
  5916. return( ret );
  5917. }
  5918. /*
  5919. * Camellia-ECB block encryption/decryption
  5920. */
  5921. int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
  5922. int mode,
  5923. const unsigned char input[16],
  5924. unsigned char output[16] )
  5925. {
  5926. int NR;
  5927. uint32_t *RK, X[4];
  5928. ( (void) mode );
  5929. NR = ctx->nr;
  5930. RK = ctx->rk;
  5931. GET_UINT32_BE( X[0], input, 0 );
  5932. GET_UINT32_BE( X[1], input, 4 );
  5933. GET_UINT32_BE( X[2], input, 8 );
  5934. GET_UINT32_BE( X[3], input, 12 );
  5935. X[0] ^= *RK++;
  5936. X[1] ^= *RK++;
  5937. X[2] ^= *RK++;
  5938. X[3] ^= *RK++;
  5939. while( NR ) {
  5940. --NR;
  5941. camellia_feistel( X, RK, X + 2 );
  5942. RK += 2;
  5943. camellia_feistel( X + 2, RK, X );
  5944. RK += 2;
  5945. camellia_feistel( X, RK, X + 2 );
  5946. RK += 2;
  5947. camellia_feistel( X + 2, RK, X );
  5948. RK += 2;
  5949. camellia_feistel( X, RK, X + 2 );
  5950. RK += 2;
  5951. camellia_feistel( X + 2, RK, X );
  5952. RK += 2;
  5953. if( NR ) {
  5954. FL(X[0], X[1], RK[0], RK[1]);
  5955. RK += 2;
  5956. FLInv(X[2], X[3], RK[0], RK[1]);
  5957. RK += 2;
  5958. }
  5959. }
  5960. X[2] ^= *RK++;
  5961. X[3] ^= *RK++;
  5962. X[0] ^= *RK++;
  5963. X[1] ^= *RK++;
  5964. PUT_UINT32_BE( X[2], output, 0 );
  5965. PUT_UINT32_BE( X[3], output, 4 );
  5966. PUT_UINT32_BE( X[0], output, 8 );
  5967. PUT_UINT32_BE( X[1], output, 12 );
  5968. return( 0 );
  5969. }
  5970. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  5971. /*
  5972. * Camellia-CBC buffer encryption/decryption
  5973. */
  5974. int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
  5975. int mode,
  5976. size_t length,
  5977. unsigned char iv[16],
  5978. const unsigned char *input,
  5979. unsigned char *output )
  5980. {
  5981. int i;
  5982. unsigned char temp[16];
  5983. if( length % 16 )
  5984. return( MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH );
  5985. if( mode == MBEDTLS_CAMELLIA_DECRYPT )
  5986. {
  5987. while( length > 0 )
  5988. {
  5989. memcpy( temp, input, 16 );
  5990. mbedtls_camellia_crypt_ecb( ctx, mode, input, output );
  5991. for( i = 0; i < 16; i++ )
  5992. output[i] = (unsigned char)( output[i] ^ iv[i] );
  5993. memcpy( iv, temp, 16 );
  5994. input += 16;
  5995. output += 16;
  5996. length -= 16;
  5997. }
  5998. }
  5999. else
  6000. {
  6001. while( length > 0 )
  6002. {
  6003. for( i = 0; i < 16; i++ )
  6004. output[i] = (unsigned char)( input[i] ^ iv[i] );
  6005. mbedtls_camellia_crypt_ecb( ctx, mode, output, output );
  6006. memcpy( iv, output, 16 );
  6007. input += 16;
  6008. output += 16;
  6009. length -= 16;
  6010. }
  6011. }
  6012. return( 0 );
  6013. }
  6014. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  6015. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  6016. /*
  6017. * Camellia-CFB128 buffer encryption/decryption
  6018. */
  6019. int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
  6020. int mode,
  6021. size_t length,
  6022. size_t *iv_off,
  6023. unsigned char iv[16],
  6024. const unsigned char *input,
  6025. unsigned char *output )
  6026. {
  6027. int c;
  6028. size_t n = *iv_off;
  6029. if( mode == MBEDTLS_CAMELLIA_DECRYPT )
  6030. {
  6031. while( length-- )
  6032. {
  6033. if( n == 0 )
  6034. mbedtls_camellia_crypt_ecb( ctx, MBEDTLS_CAMELLIA_ENCRYPT, iv, iv );
  6035. c = *input++;
  6036. *output++ = (unsigned char)( c ^ iv[n] );
  6037. iv[n] = (unsigned char) c;
  6038. n = ( n + 1 ) & 0x0F;
  6039. }
  6040. }
  6041. else
  6042. {
  6043. while( length-- )
  6044. {
  6045. if( n == 0 )
  6046. mbedtls_camellia_crypt_ecb( ctx, MBEDTLS_CAMELLIA_ENCRYPT, iv, iv );
  6047. iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
  6048. n = ( n + 1 ) & 0x0F;
  6049. }
  6050. }
  6051. *iv_off = n;
  6052. return( 0 );
  6053. }
  6054. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  6055. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  6056. /*
  6057. * Camellia-CTR buffer encryption/decryption
  6058. */
  6059. int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
  6060. size_t length,
  6061. size_t *nc_off,
  6062. unsigned char nonce_counter[16],
  6063. unsigned char stream_block[16],
  6064. const unsigned char *input,
  6065. unsigned char *output )
  6066. {
  6067. int c, i;
  6068. size_t n = *nc_off;
  6069. while( length-- )
  6070. {
  6071. if( n == 0 ) {
  6072. mbedtls_camellia_crypt_ecb( ctx, MBEDTLS_CAMELLIA_ENCRYPT, nonce_counter,
  6073. stream_block );
  6074. for( i = 16; i > 0; i-- )
  6075. if( ++nonce_counter[i - 1] != 0 )
  6076. break;
  6077. }
  6078. c = *input++;
  6079. *output++ = (unsigned char)( c ^ stream_block[n] );
  6080. n = ( n + 1 ) & 0x0F;
  6081. }
  6082. *nc_off = n;
  6083. return( 0 );
  6084. }
  6085. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  6086. #endif /* !MBEDTLS_CAMELLIA_ALT */
  6087. #if defined(MBEDTLS_SELF_TEST)
  6088. /*
  6089. * Camellia test vectors from:
  6090. *
  6091. * http://info.isl.ntt.co.jp/crypt/eng/camellia/technology.html:
  6092. * http://info.isl.ntt.co.jp/crypt/eng/camellia/dl/cryptrec/intermediate.txt
  6093. * http://info.isl.ntt.co.jp/crypt/eng/camellia/dl/cryptrec/t_camellia.txt
  6094. * (For each bitlength: Key 0, Nr 39)
  6095. */
  6096. #define CAMELLIA_TESTS_ECB 2
  6097. static const unsigned char camellia_test_ecb_key[3][CAMELLIA_TESTS_ECB][32] =
  6098. {
  6099. {
  6100. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  6101. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 },
  6102. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6103. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  6104. },
  6105. {
  6106. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  6107. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  6108. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 },
  6109. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6110. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6111. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  6112. },
  6113. {
  6114. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  6115. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  6116. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  6117. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
  6118. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6119. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6120. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6121. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  6122. },
  6123. };
  6124. static const unsigned char camellia_test_ecb_plain[CAMELLIA_TESTS_ECB][16] =
  6125. {
  6126. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  6127. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 },
  6128. { 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  6129. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  6130. };
  6131. static const unsigned char camellia_test_ecb_cipher[3][CAMELLIA_TESTS_ECB][16] =
  6132. {
  6133. {
  6134. { 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  6135. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43 },
  6136. { 0x38, 0x3C, 0x6C, 0x2A, 0xAB, 0xEF, 0x7F, 0xDE,
  6137. 0x25, 0xCD, 0x47, 0x0B, 0xF7, 0x74, 0xA3, 0x31 }
  6138. },
  6139. {
  6140. { 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  6141. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9 },
  6142. { 0xD1, 0x76, 0x3F, 0xC0, 0x19, 0xD7, 0x7C, 0xC9,
  6143. 0x30, 0xBF, 0xF2, 0xA5, 0x6F, 0x7C, 0x93, 0x64 }
  6144. },
  6145. {
  6146. { 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  6147. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09 },
  6148. { 0x05, 0x03, 0xFB, 0x10, 0xAB, 0x24, 0x1E, 0x7C,
  6149. 0xF4, 0x5D, 0x8C, 0xDE, 0xEE, 0x47, 0x43, 0x35 }
  6150. }
  6151. };
  6152. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  6153. #define CAMELLIA_TESTS_CBC 3
  6154. static const unsigned char camellia_test_cbc_key[3][32] =
  6155. {
  6156. { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  6157. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }
  6158. ,
  6159. { 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  6160. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  6161. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B }
  6162. ,
  6163. { 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  6164. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  6165. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  6166. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4 }
  6167. };
  6168. static const unsigned char camellia_test_cbc_iv[16] =
  6169. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  6170. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }
  6171. ;
  6172. static const unsigned char camellia_test_cbc_plain[CAMELLIA_TESTS_CBC][16] =
  6173. {
  6174. { 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  6175. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A },
  6176. { 0xAE, 0x2D, 0x8A, 0x57, 0x1E, 0x03, 0xAC, 0x9C,
  6177. 0x9E, 0xB7, 0x6F, 0xAC, 0x45, 0xAF, 0x8E, 0x51 },
  6178. { 0x30, 0xC8, 0x1C, 0x46, 0xA3, 0x5C, 0xE4, 0x11,
  6179. 0xE5, 0xFB, 0xC1, 0x19, 0x1A, 0x0A, 0x52, 0xEF }
  6180. };
  6181. static const unsigned char camellia_test_cbc_cipher[3][CAMELLIA_TESTS_CBC][16] =
  6182. {
  6183. {
  6184. { 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  6185. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB },
  6186. { 0xA2, 0xF2, 0xCF, 0x67, 0x16, 0x29, 0xEF, 0x78,
  6187. 0x40, 0xC5, 0xA5, 0xDF, 0xB5, 0x07, 0x48, 0x87 },
  6188. { 0x0F, 0x06, 0x16, 0x50, 0x08, 0xCF, 0x8B, 0x8B,
  6189. 0x5A, 0x63, 0x58, 0x63, 0x62, 0x54, 0x3E, 0x54 }
  6190. },
  6191. {
  6192. { 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  6193. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93 },
  6194. { 0x5D, 0x5A, 0x86, 0x9B, 0xD1, 0x4C, 0xE5, 0x42,
  6195. 0x64, 0xF8, 0x92, 0xA6, 0xDD, 0x2E, 0xC3, 0xD5 },
  6196. { 0x37, 0xD3, 0x59, 0xC3, 0x34, 0x98, 0x36, 0xD8,
  6197. 0x84, 0xE3, 0x10, 0xAD, 0xDF, 0x68, 0xC4, 0x49 }
  6198. },
  6199. {
  6200. { 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  6201. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA },
  6202. { 0x36, 0xCB, 0xEB, 0x73, 0xBD, 0x50, 0x4B, 0x40,
  6203. 0x70, 0xB1, 0xB7, 0xDE, 0x2B, 0x21, 0xEB, 0x50 },
  6204. { 0xE3, 0x1A, 0x60, 0x55, 0x29, 0x7D, 0x96, 0xCA,
  6205. 0x33, 0x30, 0xCD, 0xF1, 0xB1, 0x86, 0x0A, 0x83 }
  6206. }
  6207. };
  6208. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  6209. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  6210. /*
  6211. * Camellia-CTR test vectors from:
  6212. *
  6213. * http://www.faqs.org/rfcs/rfc5528.html
  6214. */
  6215. static const unsigned char camellia_test_ctr_key[3][16] =
  6216. {
  6217. { 0xAE, 0x68, 0x52, 0xF8, 0x12, 0x10, 0x67, 0xCC,
  6218. 0x4B, 0xF7, 0xA5, 0x76, 0x55, 0x77, 0xF3, 0x9E },
  6219. { 0x7E, 0x24, 0x06, 0x78, 0x17, 0xFA, 0xE0, 0xD7,
  6220. 0x43, 0xD6, 0xCE, 0x1F, 0x32, 0x53, 0x91, 0x63 },
  6221. { 0x76, 0x91, 0xBE, 0x03, 0x5E, 0x50, 0x20, 0xA8,
  6222. 0xAC, 0x6E, 0x61, 0x85, 0x29, 0xF9, 0xA0, 0xDC }
  6223. };
  6224. static const unsigned char camellia_test_ctr_nonce_counter[3][16] =
  6225. {
  6226. { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00,
  6227. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
  6228. { 0x00, 0x6C, 0xB6, 0xDB, 0xC0, 0x54, 0x3B, 0x59,
  6229. 0xDA, 0x48, 0xD9, 0x0B, 0x00, 0x00, 0x00, 0x01 },
  6230. { 0x00, 0xE0, 0x01, 0x7B, 0x27, 0x77, 0x7F, 0x3F,
  6231. 0x4A, 0x17, 0x86, 0xF0, 0x00, 0x00, 0x00, 0x01 }
  6232. };
  6233. static const unsigned char camellia_test_ctr_pt[3][48] =
  6234. {
  6235. { 0x53, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x62,
  6236. 0x6C, 0x6F, 0x63, 0x6B, 0x20, 0x6D, 0x73, 0x67 },
  6237. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  6238. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  6239. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  6240. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F },
  6241. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  6242. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  6243. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  6244. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
  6245. 0x20, 0x21, 0x22, 0x23 }
  6246. };
  6247. static const unsigned char camellia_test_ctr_ct[3][48] =
  6248. {
  6249. { 0xD0, 0x9D, 0xC2, 0x9A, 0x82, 0x14, 0x61, 0x9A,
  6250. 0x20, 0x87, 0x7C, 0x76, 0xDB, 0x1F, 0x0B, 0x3F },
  6251. { 0xDB, 0xF3, 0xC7, 0x8D, 0xC0, 0x83, 0x96, 0xD4,
  6252. 0xDA, 0x7C, 0x90, 0x77, 0x65, 0xBB, 0xCB, 0x44,
  6253. 0x2B, 0x8E, 0x8E, 0x0F, 0x31, 0xF0, 0xDC, 0xA7,
  6254. 0x2C, 0x74, 0x17, 0xE3, 0x53, 0x60, 0xE0, 0x48 },
  6255. { 0xB1, 0x9D, 0x1F, 0xCD, 0xCB, 0x75, 0xEB, 0x88,
  6256. 0x2F, 0x84, 0x9C, 0xE2, 0x4D, 0x85, 0xCF, 0x73,
  6257. 0x9C, 0xE6, 0x4B, 0x2B, 0x5C, 0x9D, 0x73, 0xF1,
  6258. 0x4F, 0x2D, 0x5D, 0x9D, 0xCE, 0x98, 0x89, 0xCD,
  6259. 0xDF, 0x50, 0x86, 0x96 }
  6260. };
  6261. static const int camellia_test_ctr_len[3] =
  6262. { 16, 32, 36 };
  6263. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  6264. /*
  6265. * Checkup routine
  6266. */
  6267. int mbedtls_camellia_self_test( int verbose )
  6268. {
  6269. int i, j, u, v;
  6270. unsigned char key[32];
  6271. unsigned char buf[64];
  6272. unsigned char src[16];
  6273. unsigned char dst[16];
  6274. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  6275. unsigned char iv[16];
  6276. #endif
  6277. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  6278. size_t offset, len;
  6279. unsigned char nonce_counter[16];
  6280. unsigned char stream_block[16];
  6281. #endif
  6282. mbedtls_camellia_context ctx;
  6283. memset( key, 0, 32 );
  6284. for( j = 0; j < 6; j++ ) {
  6285. u = j >> 1;
  6286. v = j & 1;
  6287. if( verbose != 0 )
  6288. mbedtls_printf( " CAMELLIA-ECB-%3d (%s): ", 128 + u * 64,
  6289. (v == MBEDTLS_CAMELLIA_DECRYPT) ? "dec" : "enc");
  6290. for( i = 0; i < CAMELLIA_TESTS_ECB; i++ ) {
  6291. memcpy( key, camellia_test_ecb_key[u][i], 16 + 8 * u );
  6292. if( v == MBEDTLS_CAMELLIA_DECRYPT ) {
  6293. mbedtls_camellia_setkey_dec( &ctx, key, 128 + u * 64 );
  6294. memcpy( src, camellia_test_ecb_cipher[u][i], 16 );
  6295. memcpy( dst, camellia_test_ecb_plain[i], 16 );
  6296. } else { /* MBEDTLS_CAMELLIA_ENCRYPT */
  6297. mbedtls_camellia_setkey_enc( &ctx, key, 128 + u * 64 );
  6298. memcpy( src, camellia_test_ecb_plain[i], 16 );
  6299. memcpy( dst, camellia_test_ecb_cipher[u][i], 16 );
  6300. }
  6301. mbedtls_camellia_crypt_ecb( &ctx, v, src, buf );
  6302. if( memcmp( buf, dst, 16 ) != 0 )
  6303. {
  6304. if( verbose != 0 )
  6305. mbedtls_printf( "failed\n" );
  6306. return( 1 );
  6307. }
  6308. }
  6309. if( verbose != 0 )
  6310. mbedtls_printf( "passed\n" );
  6311. }
  6312. if( verbose != 0 )
  6313. mbedtls_printf( "\n" );
  6314. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  6315. /*
  6316. * CBC mode
  6317. */
  6318. for( j = 0; j < 6; j++ )
  6319. {
  6320. u = j >> 1;
  6321. v = j & 1;
  6322. if( verbose != 0 )
  6323. mbedtls_printf( " CAMELLIA-CBC-%3d (%s): ", 128 + u * 64,
  6324. ( v == MBEDTLS_CAMELLIA_DECRYPT ) ? "dec" : "enc" );
  6325. memcpy( src, camellia_test_cbc_iv, 16 );
  6326. memcpy( dst, camellia_test_cbc_iv, 16 );
  6327. memcpy( key, camellia_test_cbc_key[u], 16 + 8 * u );
  6328. if( v == MBEDTLS_CAMELLIA_DECRYPT ) {
  6329. mbedtls_camellia_setkey_dec( &ctx, key, 128 + u * 64 );
  6330. } else {
  6331. mbedtls_camellia_setkey_enc( &ctx, key, 128 + u * 64 );
  6332. }
  6333. for( i = 0; i < CAMELLIA_TESTS_CBC; i++ ) {
  6334. if( v == MBEDTLS_CAMELLIA_DECRYPT ) {
  6335. memcpy( iv , src, 16 );
  6336. memcpy( src, camellia_test_cbc_cipher[u][i], 16 );
  6337. memcpy( dst, camellia_test_cbc_plain[i], 16 );
  6338. } else { /* MBEDTLS_CAMELLIA_ENCRYPT */
  6339. memcpy( iv , dst, 16 );
  6340. memcpy( src, camellia_test_cbc_plain[i], 16 );
  6341. memcpy( dst, camellia_test_cbc_cipher[u][i], 16 );
  6342. }
  6343. mbedtls_camellia_crypt_cbc( &ctx, v, 16, iv, src, buf );
  6344. if( memcmp( buf, dst, 16 ) != 0 )
  6345. {
  6346. if( verbose != 0 )
  6347. mbedtls_printf( "failed\n" );
  6348. return( 1 );
  6349. }
  6350. }
  6351. if( verbose != 0 )
  6352. mbedtls_printf( "passed\n" );
  6353. }
  6354. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  6355. if( verbose != 0 )
  6356. mbedtls_printf( "\n" );
  6357. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  6358. /*
  6359. * CTR mode
  6360. */
  6361. for( i = 0; i < 6; i++ )
  6362. {
  6363. u = i >> 1;
  6364. v = i & 1;
  6365. if( verbose != 0 )
  6366. mbedtls_printf( " CAMELLIA-CTR-128 (%s): ",
  6367. ( v == MBEDTLS_CAMELLIA_DECRYPT ) ? "dec" : "enc" );
  6368. memcpy( nonce_counter, camellia_test_ctr_nonce_counter[u], 16 );
  6369. memcpy( key, camellia_test_ctr_key[u], 16 );
  6370. offset = 0;
  6371. mbedtls_camellia_setkey_enc( &ctx, key, 128 );
  6372. if( v == MBEDTLS_CAMELLIA_DECRYPT )
  6373. {
  6374. len = camellia_test_ctr_len[u];
  6375. memcpy( buf, camellia_test_ctr_ct[u], len );
  6376. mbedtls_camellia_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block,
  6377. buf, buf );
  6378. if( memcmp( buf, camellia_test_ctr_pt[u], len ) != 0 )
  6379. {
  6380. if( verbose != 0 )
  6381. mbedtls_printf( "failed\n" );
  6382. return( 1 );
  6383. }
  6384. }
  6385. else
  6386. {
  6387. len = camellia_test_ctr_len[u];
  6388. memcpy( buf, camellia_test_ctr_pt[u], len );
  6389. mbedtls_camellia_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block,
  6390. buf, buf );
  6391. if( memcmp( buf, camellia_test_ctr_ct[u], len ) != 0 )
  6392. {
  6393. if( verbose != 0 )
  6394. mbedtls_printf( "failed\n" );
  6395. return( 1 );
  6396. }
  6397. }
  6398. if( verbose != 0 )
  6399. mbedtls_printf( "passed\n" );
  6400. }
  6401. if( verbose != 0 )
  6402. mbedtls_printf( "\n" );
  6403. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  6404. return( 0 );
  6405. }
  6406. #endif /* MBEDTLS_SELF_TEST */
  6407. #endif /* MBEDTLS_CAMELLIA_C */
  6408. /*
  6409. Amalgamated build undefines
  6410. */
  6411. #undef ADD
  6412. #undef BC
  6413. #undef BEFORE_COLON
  6414. #undef F
  6415. #undef F0
  6416. #undef F1
  6417. #undef F2
  6418. #undef F3
  6419. #undef F4
  6420. #undef F5
  6421. #undef FSb
  6422. #undef K
  6423. #undef KK
  6424. #undef P
  6425. #undef R
  6426. #undef ROTR
  6427. #undef S
  6428. #undef S0
  6429. #undef S1
  6430. #undef S2
  6431. #undef S3
  6432. #undef SAFE_SNPRINTF
  6433. #undef SHR
  6434. #undef close
  6435. #undef read
  6436. #undef supported_init
  6437. #undef write
  6438. /********* Start of file library/ccm.c ************/
  6439. /*
  6440. * NIST SP800-38C compliant CCM implementation
  6441. *
  6442. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  6443. * SPDX-License-Identifier: Apache-2.0
  6444. *
  6445. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6446. * not use this file except in compliance with the License.
  6447. * You may obtain a copy of the License at
  6448. *
  6449. * http://www.apache.org/licenses/LICENSE-2.0
  6450. *
  6451. * Unless required by applicable law or agreed to in writing, software
  6452. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  6453. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6454. * See the License for the specific language governing permissions and
  6455. * limitations under the License.
  6456. *
  6457. * This file is part of mbed TLS (https://tls.mbed.org)
  6458. */
  6459. /*
  6460. * Definition of CCM:
  6461. * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
  6462. * RFC 3610 "Counter with CBC-MAC (CCM)"
  6463. *
  6464. * Related:
  6465. * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
  6466. */
  6467. #if !defined(MBEDTLS_CONFIG_FILE)
  6468. #else
  6469. #endif
  6470. #if defined(MBEDTLS_CCM_C)
  6471. #include <string.h>
  6472. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  6473. #if defined(MBEDTLS_PLATFORM_C)
  6474. #else
  6475. #include <stdio.h>
  6476. #define mbedtls_printf printf
  6477. #endif /* MBEDTLS_PLATFORM_C */
  6478. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  6479. #if !defined(MBEDTLS_CCM_ALT)
  6480. /* Implementation that should never be optimized out by the compiler */
  6481. /* zeroize was here */
  6482. #define CCM_ENCRYPT 0
  6483. #define CCM_DECRYPT 1
  6484. /*
  6485. * Initialize context
  6486. */
  6487. void mbedtls_ccm_init( mbedtls_ccm_context *ctx )
  6488. {
  6489. memset( ctx, 0, sizeof( mbedtls_ccm_context ) );
  6490. }
  6491. int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
  6492. mbedtls_cipher_id_t cipher,
  6493. const unsigned char *key,
  6494. unsigned int keybits )
  6495. {
  6496. int ret;
  6497. const mbedtls_cipher_info_t *cipher_info;
  6498. cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, MBEDTLS_MODE_ECB );
  6499. if( cipher_info == NULL )
  6500. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  6501. if( cipher_info->block_size != 16 )
  6502. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  6503. mbedtls_cipher_free( &ctx->cipher_ctx );
  6504. if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 )
  6505. return( ret );
  6506. if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits,
  6507. MBEDTLS_ENCRYPT ) ) != 0 )
  6508. {
  6509. return( ret );
  6510. }
  6511. return( 0 );
  6512. }
  6513. /*
  6514. * Free context
  6515. */
  6516. void mbedtls_ccm_free( mbedtls_ccm_context *ctx )
  6517. {
  6518. mbedtls_cipher_free( &ctx->cipher_ctx );
  6519. mbedtls_zeroize( ctx, sizeof( mbedtls_ccm_context ) );
  6520. }
  6521. /*
  6522. * Macros for common operations.
  6523. * Results in smaller compiled code than static inline functions.
  6524. */
  6525. /*
  6526. * Update the CBC-MAC state in y using a block in b
  6527. * (Always using b as the source helps the compiler optimise a bit better.)
  6528. */
  6529. #define UPDATE_CBC_MAC \
  6530. for( i = 0; i < 16; i++ ) \
  6531. y[i] ^= b[i]; \
  6532. \
  6533. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \
  6534. return( ret );
  6535. /*
  6536. * Encrypt or decrypt a partial block with CTR
  6537. * Warning: using b for temporary storage! src and dst must not be b!
  6538. * This avoids allocating one more 16 bytes buffer while allowing src == dst.
  6539. */
  6540. #define CTR_CRYPT( dst, src, len ) \
  6541. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, 16, b, &olen ) ) != 0 ) \
  6542. return( ret ); \
  6543. \
  6544. for( i = 0; i < len; i++ ) \
  6545. dst[i] = src[i] ^ b[i];
  6546. /*
  6547. * Authenticated encryption or decryption
  6548. */
  6549. static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
  6550. const unsigned char *iv, size_t iv_len,
  6551. const unsigned char *add, size_t add_len,
  6552. const unsigned char *input, unsigned char *output,
  6553. unsigned char *tag, size_t tag_len )
  6554. {
  6555. int ret;
  6556. unsigned char i;
  6557. unsigned char q;
  6558. size_t len_left, olen;
  6559. unsigned char b[16];
  6560. unsigned char y[16];
  6561. unsigned char ctr[16];
  6562. const unsigned char *src;
  6563. unsigned char *dst;
  6564. /*
  6565. * Check length requirements: SP800-38C A.1
  6566. * Additional requirement: a < 2^16 - 2^8 to simplify the code.
  6567. * 'length' checked later (when writing it to the first block)
  6568. */
  6569. if( tag_len < 4 || tag_len > 16 || tag_len % 2 != 0 )
  6570. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  6571. /* Also implies q is within bounds */
  6572. if( iv_len < 7 || iv_len > 13 )
  6573. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  6574. if( add_len > 0xFF00 )
  6575. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  6576. q = 16 - 1 - (unsigned char) iv_len;
  6577. /*
  6578. * First block B_0:
  6579. * 0 .. 0 flags
  6580. * 1 .. iv_len nonce (aka iv)
  6581. * iv_len+1 .. 15 length
  6582. *
  6583. * With flags as (bits):
  6584. * 7 0
  6585. * 6 add present?
  6586. * 5 .. 3 (t - 2) / 2
  6587. * 2 .. 0 q - 1
  6588. */
  6589. b[0] = 0;
  6590. b[0] |= ( add_len > 0 ) << 6;
  6591. b[0] |= ( ( tag_len - 2 ) / 2 ) << 3;
  6592. b[0] |= q - 1;
  6593. memcpy( b + 1, iv, iv_len );
  6594. for( i = 0, len_left = length; i < q; i++, len_left >>= 8 )
  6595. b[15-i] = (unsigned char)( len_left & 0xFF );
  6596. if( len_left > 0 )
  6597. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  6598. /* Start CBC-MAC with first block */
  6599. memset( y, 0, 16 );
  6600. UPDATE_CBC_MAC;
  6601. /*
  6602. * If there is additional data, update CBC-MAC with
  6603. * add_len, add, 0 (padding to a block boundary)
  6604. */
  6605. if( add_len > 0 )
  6606. {
  6607. size_t use_len;
  6608. len_left = add_len;
  6609. src = add;
  6610. memset( b, 0, 16 );
  6611. b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF );
  6612. b[1] = (unsigned char)( ( add_len ) & 0xFF );
  6613. use_len = len_left < 16 - 2 ? len_left : 16 - 2;
  6614. memcpy( b + 2, src, use_len );
  6615. len_left -= use_len;
  6616. src += use_len;
  6617. UPDATE_CBC_MAC;
  6618. while( len_left > 0 )
  6619. {
  6620. use_len = len_left > 16 ? 16 : len_left;
  6621. memset( b, 0, 16 );
  6622. memcpy( b, src, use_len );
  6623. UPDATE_CBC_MAC;
  6624. len_left -= use_len;
  6625. src += use_len;
  6626. }
  6627. }
  6628. /*
  6629. * Prepare counter block for encryption:
  6630. * 0 .. 0 flags
  6631. * 1 .. iv_len nonce (aka iv)
  6632. * iv_len+1 .. 15 counter (initially 1)
  6633. *
  6634. * With flags as (bits):
  6635. * 7 .. 3 0
  6636. * 2 .. 0 q - 1
  6637. */
  6638. ctr[0] = q - 1;
  6639. memcpy( ctr + 1, iv, iv_len );
  6640. memset( ctr + 1 + iv_len, 0, q );
  6641. ctr[15] = 1;
  6642. /*
  6643. * Authenticate and {en,de}crypt the message.
  6644. *
  6645. * The only difference between encryption and decryption is
  6646. * the respective order of authentication and {en,de}cryption.
  6647. */
  6648. len_left = length;
  6649. src = input;
  6650. dst = output;
  6651. while( len_left > 0 )
  6652. {
  6653. size_t use_len = len_left > 16 ? 16 : len_left;
  6654. if( mode == CCM_ENCRYPT )
  6655. {
  6656. memset( b, 0, 16 );
  6657. memcpy( b, src, use_len );
  6658. UPDATE_CBC_MAC;
  6659. }
  6660. CTR_CRYPT( dst, src, use_len );
  6661. if( mode == CCM_DECRYPT )
  6662. {
  6663. memset( b, 0, 16 );
  6664. memcpy( b, dst, use_len );
  6665. UPDATE_CBC_MAC;
  6666. }
  6667. dst += use_len;
  6668. src += use_len;
  6669. len_left -= use_len;
  6670. /*
  6671. * Increment counter.
  6672. * No need to check for overflow thanks to the length check above.
  6673. */
  6674. for( i = 0; i < q; i++ )
  6675. if( ++ctr[15-i] != 0 )
  6676. break;
  6677. }
  6678. /*
  6679. * Authentication: reset counter and crypt/mask internal tag
  6680. */
  6681. for( i = 0; i < q; i++ )
  6682. ctr[15-i] = 0;
  6683. CTR_CRYPT( y, y, 16 );
  6684. memcpy( tag, y, tag_len );
  6685. return( 0 );
  6686. }
  6687. /*
  6688. * Authenticated encryption
  6689. */
  6690. int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
  6691. const unsigned char *iv, size_t iv_len,
  6692. const unsigned char *add, size_t add_len,
  6693. const unsigned char *input, unsigned char *output,
  6694. unsigned char *tag, size_t tag_len )
  6695. {
  6696. return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len,
  6697. add, add_len, input, output, tag, tag_len ) );
  6698. }
  6699. /*
  6700. * Authenticated decryption
  6701. */
  6702. int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
  6703. const unsigned char *iv, size_t iv_len,
  6704. const unsigned char *add, size_t add_len,
  6705. const unsigned char *input, unsigned char *output,
  6706. const unsigned char *tag, size_t tag_len )
  6707. {
  6708. int ret;
  6709. unsigned char check_tag[16];
  6710. unsigned char i;
  6711. int diff;
  6712. if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length,
  6713. iv, iv_len, add, add_len,
  6714. input, output, check_tag, tag_len ) ) != 0 )
  6715. {
  6716. return( ret );
  6717. }
  6718. /* Check tag in "constant-time" */
  6719. for( diff = 0, i = 0; i < tag_len; i++ )
  6720. diff |= tag[i] ^ check_tag[i];
  6721. if( diff != 0 )
  6722. {
  6723. mbedtls_zeroize( output, length );
  6724. return( MBEDTLS_ERR_CCM_AUTH_FAILED );
  6725. }
  6726. return( 0 );
  6727. }
  6728. #endif /* !MBEDTLS_CCM_ALT */
  6729. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  6730. /*
  6731. * Examples 1 to 3 from SP800-38C Appendix C
  6732. */
  6733. #define NB_TESTS 3
  6734. /*
  6735. * The data is the same for all tests, only the used length changes
  6736. */
  6737. static const unsigned char key[] = {
  6738. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  6739. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
  6740. };
  6741. static const unsigned char iv[] = {
  6742. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  6743. 0x18, 0x19, 0x1a, 0x1b
  6744. };
  6745. static const unsigned char ad[] = {
  6746. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  6747. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  6748. 0x10, 0x11, 0x12, 0x13
  6749. };
  6750. static const unsigned char msg[] = {
  6751. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  6752. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  6753. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6754. };
  6755. static const size_t iv_len [NB_TESTS] = { 7, 8, 12 };
  6756. static const size_t add_len[NB_TESTS] = { 8, 16, 20 };
  6757. static const size_t msg_len[NB_TESTS] = { 4, 16, 24 };
  6758. static const size_t tag_len[NB_TESTS] = { 4, 6, 8 };
  6759. static const unsigned char res[NB_TESTS][32] = {
  6760. { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
  6761. { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
  6762. 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
  6763. 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
  6764. { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
  6765. 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
  6766. 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
  6767. 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
  6768. };
  6769. int mbedtls_ccm_self_test( int verbose )
  6770. {
  6771. mbedtls_ccm_context ctx;
  6772. unsigned char out[32];
  6773. size_t i;
  6774. int ret;
  6775. mbedtls_ccm_init( &ctx );
  6776. if( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
  6777. {
  6778. if( verbose != 0 )
  6779. mbedtls_printf( " CCM: setup failed" );
  6780. return( 1 );
  6781. }
  6782. for( i = 0; i < NB_TESTS; i++ )
  6783. {
  6784. if( verbose != 0 )
  6785. mbedtls_printf( " CCM-AES #%u: ", (unsigned int) i + 1 );
  6786. ret = mbedtls_ccm_encrypt_and_tag( &ctx, msg_len[i],
  6787. iv, iv_len[i], ad, add_len[i],
  6788. msg, out,
  6789. out + msg_len[i], tag_len[i] );
  6790. if( ret != 0 ||
  6791. memcmp( out, res[i], msg_len[i] + tag_len[i] ) != 0 )
  6792. {
  6793. if( verbose != 0 )
  6794. mbedtls_printf( "failed\n" );
  6795. return( 1 );
  6796. }
  6797. ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len[i],
  6798. iv, iv_len[i], ad, add_len[i],
  6799. res[i], out,
  6800. res[i] + msg_len[i], tag_len[i] );
  6801. if( ret != 0 ||
  6802. memcmp( out, msg, msg_len[i] ) != 0 )
  6803. {
  6804. if( verbose != 0 )
  6805. mbedtls_printf( "failed\n" );
  6806. return( 1 );
  6807. }
  6808. if( verbose != 0 )
  6809. mbedtls_printf( "passed\n" );
  6810. }
  6811. mbedtls_ccm_free( &ctx );
  6812. if( verbose != 0 )
  6813. mbedtls_printf( "\n" );
  6814. return( 0 );
  6815. }
  6816. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  6817. #endif /* MBEDTLS_CCM_C */
  6818. /*
  6819. Amalgamated build undefines
  6820. */
  6821. #undef ADD
  6822. #undef BC
  6823. #undef BEFORE_COLON
  6824. #undef F
  6825. #undef F0
  6826. #undef F1
  6827. #undef F2
  6828. #undef F3
  6829. #undef F4
  6830. #undef F5
  6831. #undef FSb
  6832. #undef K
  6833. #undef KK
  6834. #undef P
  6835. #undef R
  6836. #undef ROTR
  6837. #undef S
  6838. #undef S0
  6839. #undef S1
  6840. #undef S2
  6841. #undef S3
  6842. #undef SAFE_SNPRINTF
  6843. #undef SHR
  6844. #undef close
  6845. #undef read
  6846. #undef supported_init
  6847. #undef write
  6848. /********* Start of file library/certs.c ************/
  6849. /*
  6850. * X.509 test certificates
  6851. *
  6852. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  6853. * SPDX-License-Identifier: Apache-2.0
  6854. *
  6855. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6856. * not use this file except in compliance with the License.
  6857. * You may obtain a copy of the License at
  6858. *
  6859. * http://www.apache.org/licenses/LICENSE-2.0
  6860. *
  6861. * Unless required by applicable law or agreed to in writing, software
  6862. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  6863. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6864. * See the License for the specific language governing permissions and
  6865. * limitations under the License.
  6866. *
  6867. * This file is part of mbed TLS (https://tls.mbed.org)
  6868. */
  6869. #if !defined(MBEDTLS_CONFIG_FILE)
  6870. #else
  6871. #endif
  6872. #if defined(MBEDTLS_CERTS_C)
  6873. #if defined(MBEDTLS_ECDSA_C)
  6874. #define TEST_CA_CRT_EC \
  6875. "-----BEGIN CERTIFICATE-----\r\n" \
  6876. "MIICUjCCAdegAwIBAgIJAMFD4n5iQ8zoMAoGCCqGSM49BAMCMD4xCzAJBgNVBAYT\r\n" \
  6877. "Ak5MMREwDwYDVQQKEwhQb2xhclNTTDEcMBoGA1UEAxMTUG9sYXJzc2wgVGVzdCBF\r\n" \
  6878. "QyBDQTAeFw0xMzA5MjQxNTQ5NDhaFw0yMzA5MjIxNTQ5NDhaMD4xCzAJBgNVBAYT\r\n" \
  6879. "Ak5MMREwDwYDVQQKEwhQb2xhclNTTDEcMBoGA1UEAxMTUG9sYXJzc2wgVGVzdCBF\r\n" \
  6880. "QyBDQTB2MBAGByqGSM49AgEGBSuBBAAiA2IABMPaKzRBN1gvh1b+/Im6KUNLTuBu\r\n" \
  6881. "ww5XUzM5WNRStJGVOQsj318XJGJI/BqVKc4sLYfCiFKAr9ZqqyHduNMcbli4yuiy\r\n" \
  6882. "aY7zQa0pw7RfdadHb9UZKVVpmlM7ILRmFmAzHqOBoDCBnTAdBgNVHQ4EFgQUnW0g\r\n" \
  6883. "JEkBPyvLeLUZvH4kydv7NnwwbgYDVR0jBGcwZYAUnW0gJEkBPyvLeLUZvH4kydv7\r\n" \
  6884. "NnyhQqRAMD4xCzAJBgNVBAYTAk5MMREwDwYDVQQKEwhQb2xhclNTTDEcMBoGA1UE\r\n" \
  6885. "AxMTUG9sYXJzc2wgVGVzdCBFQyBDQYIJAMFD4n5iQ8zoMAwGA1UdEwQFMAMBAf8w\r\n" \
  6886. "CgYIKoZIzj0EAwIDaQAwZgIxAMO0YnNWKJUAfXgSJtJxexn4ipg+kv4znuR50v56\r\n" \
  6887. "t4d0PCu412mUC6Nnd7izvtE2MgIxAP1nnJQjZ8BWukszFQDG48wxCCyci9qpdSMv\r\n" \
  6888. "uCjn8pwUOkABXK8Mss90fzCfCEOtIA==\r\n" \
  6889. "-----END CERTIFICATE-----\r\n"
  6890. const char mbedtls_test_ca_crt_ec[] = TEST_CA_CRT_EC;
  6891. const size_t mbedtls_test_ca_crt_ec_len = sizeof( mbedtls_test_ca_crt_ec );
  6892. const char mbedtls_test_ca_key_ec[] =
  6893. "-----BEGIN EC PRIVATE KEY-----\r\n"
  6894. "Proc-Type: 4,ENCRYPTED\r\n"
  6895. "DEK-Info: DES-EDE3-CBC,307EAB469933D64E\r\n"
  6896. "\r\n"
  6897. "IxbrRmKcAzctJqPdTQLA4SWyBYYGYJVkYEna+F7Pa5t5Yg/gKADrFKcm6B72e7DG\r\n"
  6898. "ihExtZI648s0zdYw6qSJ74vrPSuWDe5qm93BqsfVH9svtCzWHW0pm1p0KTBCFfUq\r\n"
  6899. "UsuWTITwJImcnlAs1gaRZ3sAWm7cOUidL0fo2G0fYUFNcYoCSLffCFTEHBuPnagb\r\n"
  6900. "a77x/sY1Bvii8S9/XhDTb6pTMx06wzrm\r\n"
  6901. "-----END EC PRIVATE KEY-----\r\n";
  6902. const size_t mbedtls_test_ca_key_ec_len = sizeof( mbedtls_test_ca_key_ec );
  6903. const char mbedtls_test_ca_pwd_ec[] = "PolarSSLTest";
  6904. const size_t mbedtls_test_ca_pwd_ec_len = sizeof( mbedtls_test_ca_pwd_ec ) - 1;
  6905. const char mbedtls_test_srv_crt_ec[] =
  6906. "-----BEGIN CERTIFICATE-----\r\n"
  6907. "MIICHzCCAaWgAwIBAgIBCTAKBggqhkjOPQQDAjA+MQswCQYDVQQGEwJOTDERMA8G\r\n"
  6908. "A1UEChMIUG9sYXJTU0wxHDAaBgNVBAMTE1BvbGFyc3NsIFRlc3QgRUMgQ0EwHhcN\r\n"
  6909. "MTMwOTI0MTU1MjA0WhcNMjMwOTIyMTU1MjA0WjA0MQswCQYDVQQGEwJOTDERMA8G\r\n"
  6910. "A1UEChMIUG9sYXJTU0wxEjAQBgNVBAMTCWxvY2FsaG9zdDBZMBMGByqGSM49AgEG\r\n"
  6911. "CCqGSM49AwEHA0IABDfMVtl2CR5acj7HWS3/IG7ufPkGkXTQrRS192giWWKSTuUA\r\n"
  6912. "2CMR/+ov0jRdXRa9iojCa3cNVc2KKg76Aci07f+jgZ0wgZowCQYDVR0TBAIwADAd\r\n"
  6913. "BgNVHQ4EFgQUUGGlj9QH2deCAQzlZX+MY0anE74wbgYDVR0jBGcwZYAUnW0gJEkB\r\n"
  6914. "PyvLeLUZvH4kydv7NnyhQqRAMD4xCzAJBgNVBAYTAk5MMREwDwYDVQQKEwhQb2xh\r\n"
  6915. "clNTTDEcMBoGA1UEAxMTUG9sYXJzc2wgVGVzdCBFQyBDQYIJAMFD4n5iQ8zoMAoG\r\n"
  6916. "CCqGSM49BAMCA2gAMGUCMQCaLFzXptui5WQN8LlO3ddh1hMxx6tzgLvT03MTVK2S\r\n"
  6917. "C12r0Lz3ri/moSEpNZWqPjkCMCE2f53GXcYLqyfyJR078c/xNSUU5+Xxl7VZ414V\r\n"
  6918. "fGa5kHvHARBPc8YAIVIqDvHH1Q==\r\n"
  6919. "-----END CERTIFICATE-----\r\n";
  6920. const size_t mbedtls_test_srv_crt_ec_len = sizeof( mbedtls_test_srv_crt_ec );
  6921. const char mbedtls_test_srv_key_ec[] =
  6922. "-----BEGIN EC PRIVATE KEY-----\r\n"
  6923. "MHcCAQEEIPEqEyB2AnCoPL/9U/YDHvdqXYbIogTywwyp6/UfDw6noAoGCCqGSM49\r\n"
  6924. "AwEHoUQDQgAEN8xW2XYJHlpyPsdZLf8gbu58+QaRdNCtFLX3aCJZYpJO5QDYIxH/\r\n"
  6925. "6i/SNF1dFr2KiMJrdw1VzYoqDvoByLTt/w==\r\n"
  6926. "-----END EC PRIVATE KEY-----\r\n";
  6927. const size_t mbedtls_test_srv_key_ec_len = sizeof( mbedtls_test_srv_key_ec );
  6928. const char mbedtls_test_cli_crt_ec[] =
  6929. "-----BEGIN CERTIFICATE-----\r\n"
  6930. "MIICLDCCAbKgAwIBAgIBDTAKBggqhkjOPQQDAjA+MQswCQYDVQQGEwJOTDERMA8G\r\n"
  6931. "A1UEChMIUG9sYXJTU0wxHDAaBgNVBAMTE1BvbGFyc3NsIFRlc3QgRUMgQ0EwHhcN\r\n"
  6932. "MTMwOTI0MTU1MjA0WhcNMjMwOTIyMTU1MjA0WjBBMQswCQYDVQQGEwJOTDERMA8G\r\n"
  6933. "A1UEChMIUG9sYXJTU0wxHzAdBgNVBAMTFlBvbGFyU1NMIFRlc3QgQ2xpZW50IDIw\r\n"
  6934. "WTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAARX5a6xc9/TrLuTuIH/Eq7u5lOszlVT\r\n"
  6935. "9jQOzC7jYyUL35ji81xgNpbA1RgUcOV/n9VLRRjlsGzVXPiWj4dwo+THo4GdMIGa\r\n"
  6936. "MAkGA1UdEwQCMAAwHQYDVR0OBBYEFHoAX4Zk/OBd5REQO7LmO8QmP8/iMG4GA1Ud\r\n"
  6937. "IwRnMGWAFJ1tICRJAT8ry3i1Gbx+JMnb+zZ8oUKkQDA+MQswCQYDVQQGEwJOTDER\r\n"
  6938. "MA8GA1UEChMIUG9sYXJTU0wxHDAaBgNVBAMTE1BvbGFyc3NsIFRlc3QgRUMgQ0GC\r\n"
  6939. "CQDBQ+J+YkPM6DAKBggqhkjOPQQDAgNoADBlAjBKZQ17IIOimbmoD/yN7o89u3BM\r\n"
  6940. "lgOsjnhw3fIOoLIWy2WOGsk/LGF++DzvrRzuNiACMQCd8iem1XS4JK7haj8xocpU\r\n"
  6941. "LwjQje5PDGHfd3h9tP38Qknu5bJqws0md2KOKHyeV0U=\r\n"
  6942. "-----END CERTIFICATE-----\r\n";
  6943. const size_t mbedtls_test_cli_crt_ec_len = sizeof( mbedtls_test_cli_crt_ec );
  6944. const char mbedtls_test_cli_key_ec[] =
  6945. "-----BEGIN EC PRIVATE KEY-----\r\n"
  6946. "MHcCAQEEIPb3hmTxZ3/mZI3vyk7p3U3wBf+WIop6hDhkFzJhmLcqoAoGCCqGSM49\r\n"
  6947. "AwEHoUQDQgAEV+WusXPf06y7k7iB/xKu7uZTrM5VU/Y0Dswu42MlC9+Y4vNcYDaW\r\n"
  6948. "wNUYFHDlf5/VS0UY5bBs1Vz4lo+HcKPkxw==\r\n"
  6949. "-----END EC PRIVATE KEY-----\r\n";
  6950. const size_t mbedtls_test_cli_key_ec_len = sizeof( mbedtls_test_cli_key_ec );
  6951. #endif /* MBEDTLS_ECDSA_C */
  6952. #if defined(MBEDTLS_RSA_C)
  6953. #if defined(MBEDTLS_SHA256_C)
  6954. #define TEST_CA_CRT_RSA_SHA256 \
  6955. "-----BEGIN CERTIFICATE-----\r\n" \
  6956. "MIIDhzCCAm+gAwIBAgIBADANBgkqhkiG9w0BAQsFADA7MQswCQYDVQQGEwJOTDER\r\n" \
  6957. "MA8GA1UECgwIUG9sYXJTU0wxGTAXBgNVBAMMEFBvbGFyU1NMIFRlc3QgQ0EwHhcN\r\n" \
  6958. "MTcwNTA0MTY1NzAxWhcNMjcwNTA1MTY1NzAxWjA7MQswCQYDVQQGEwJOTDERMA8G\r\n" \
  6959. "A1UECgwIUG9sYXJTU0wxGTAXBgNVBAMMEFBvbGFyU1NMIFRlc3QgQ0EwggEiMA0G\r\n" \
  6960. "CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDA3zf8F7vglp0/ht6WMn1EpRagzSHx\r\n" \
  6961. "mdTs6st8GFgIlKXsm8WL3xoemTiZhx57wI053zhdcHgH057Zk+i5clHFzqMwUqny\r\n" \
  6962. "50BwFMtEonILwuVA+T7lpg6z+exKY8C4KQB0nFc7qKUEkHHxvYPZP9al4jwqj+8n\r\n" \
  6963. "YMPGn8u67GB9t+aEMr5P+1gmIgNb1LTV+/Xjli5wwOQuvfwu7uJBVcA0Ln0kcmnL\r\n" \
  6964. "R7EUQIN9Z/SG9jGr8XmksrUuEvmEF/Bibyc+E1ixVA0hmnM3oTDPb5Lc9un8rNsu\r\n" \
  6965. "KNF+AksjoBXyOGVkCeoMbo4bF6BxyLObyavpw/LPh5aPgAIynplYb6LVAgMBAAGj\r\n" \
  6966. "gZUwgZIwHQYDVR0OBBYEFLRa5KWz3tJS9rnVppUP6z68x/3/MGMGA1UdIwRcMFqA\r\n" \
  6967. "FLRa5KWz3tJS9rnVppUP6z68x/3/oT+kPTA7MQswCQYDVQQGEwJOTDERMA8GA1UE\r\n" \
  6968. "CgwIUG9sYXJTU0wxGTAXBgNVBAMMEFBvbGFyU1NMIFRlc3QgQ0GCAQAwDAYDVR0T\r\n" \
  6969. "BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAHK/HHrTZMnnVMpde1io+voAtql7j\r\n" \
  6970. "4sRhLrjD7o3THtwRbDa2diCvpq0Sq23Ng2LMYoXsOxoL/RQK3iN7UKxV3MKPEr0w\r\n" \
  6971. "XQS+kKQqiT2bsfrjnWMVHZtUOMpm6FNqcdGm/Rss3vKda2lcKl8kUnq/ylc1+QbB\r\n" \
  6972. "G6A6tUvQcr2ZyWfVg+mM5XkhTrOOXus2OLikb4WwEtJTJRNE0f+yPODSUz0/vT57\r\n" \
  6973. "ApH0CnB80bYJshYHPHHymOtleAB8KSYtqm75g/YNobjnjB6cm4HkW3OZRVIl6fYY\r\n" \
  6974. "n20NRVA1Vjs6GAROr4NqW4k/+LofY9y0LLDE+p0oIEKXIsIvhPr39swxSA==\r\n" \
  6975. "-----END CERTIFICATE-----\r\n"
  6976. const char mbedtls_test_ca_crt_rsa[] = TEST_CA_CRT_RSA_SHA256;
  6977. const size_t mbedtls_test_ca_crt_rsa_len = sizeof( mbedtls_test_ca_crt_rsa );
  6978. #define TEST_CA_CRT_RSA_SOME
  6979. static const char mbedtls_test_ca_crt_rsa_sha256[] = TEST_CA_CRT_RSA_SHA256;
  6980. #endif
  6981. #if !defined(TEST_CA_CRT_RSA_SOME) || defined(MBEDTLS_SHA1_C)
  6982. #define TEST_CA_CRT_RSA_SHA1 \
  6983. "-----BEGIN CERTIFICATE-----\r\n" \
  6984. "MIIDhzCCAm+gAwIBAgIBADANBgkqhkiG9w0BAQUFADA7MQswCQYDVQQGEwJOTDER\r\n" \
  6985. "MA8GA1UEChMIUG9sYXJTU0wxGTAXBgNVBAMTEFBvbGFyU1NMIFRlc3QgQ0EwHhcN\r\n" \
  6986. "MTEwMjEyMTQ0NDAwWhcNMjEwMjEyMTQ0NDAwWjA7MQswCQYDVQQGEwJOTDERMA8G\r\n" \
  6987. "A1UEChMIUG9sYXJTU0wxGTAXBgNVBAMTEFBvbGFyU1NMIFRlc3QgQ0EwggEiMA0G\r\n" \
  6988. "CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDA3zf8F7vglp0/ht6WMn1EpRagzSHx\r\n" \
  6989. "mdTs6st8GFgIlKXsm8WL3xoemTiZhx57wI053zhdcHgH057Zk+i5clHFzqMwUqny\r\n" \
  6990. "50BwFMtEonILwuVA+T7lpg6z+exKY8C4KQB0nFc7qKUEkHHxvYPZP9al4jwqj+8n\r\n" \
  6991. "YMPGn8u67GB9t+aEMr5P+1gmIgNb1LTV+/Xjli5wwOQuvfwu7uJBVcA0Ln0kcmnL\r\n" \
  6992. "R7EUQIN9Z/SG9jGr8XmksrUuEvmEF/Bibyc+E1ixVA0hmnM3oTDPb5Lc9un8rNsu\r\n" \
  6993. "KNF+AksjoBXyOGVkCeoMbo4bF6BxyLObyavpw/LPh5aPgAIynplYb6LVAgMBAAGj\r\n" \
  6994. "gZUwgZIwDAYDVR0TBAUwAwEB/zAdBgNVHQ4EFgQUtFrkpbPe0lL2udWmlQ/rPrzH\r\n" \
  6995. "/f8wYwYDVR0jBFwwWoAUtFrkpbPe0lL2udWmlQ/rPrzH/f+hP6Q9MDsxCzAJBgNV\r\n" \
  6996. "BAYTAk5MMREwDwYDVQQKEwhQb2xhclNTTDEZMBcGA1UEAxMQUG9sYXJTU0wgVGVz\r\n" \
  6997. "dCBDQYIBADANBgkqhkiG9w0BAQUFAAOCAQEAuP1U2ABUkIslsCfdlc2i94QHHYeJ\r\n" \
  6998. "SsR4EdgHtdciUI5I62J6Mom+Y0dT/7a+8S6MVMCZP6C5NyNyXw1GWY/YR82XTJ8H\r\n" \
  6999. "DBJiCTok5DbZ6SzaONBzdWHXwWwmi5vg1dxn7YxrM9d0IjxM27WNKs4sDQhZBQkF\r\n" \
  7000. "pjmfs2cb4oPl4Y9T9meTx/lvdkRYEug61Jfn6cA+qHpyPYdTH+UshITnmp5/Ztkf\r\n" \
  7001. "m/UTSLBNFNHesiTZeH31NcxYGdHSme9Nc/gfidRa0FLOCfWxRlFqAI47zG9jAQCZ\r\n" \
  7002. "7Z2mCGDNMhjQc+BYcdnl0lPXjdDK6V0qCg1dVewhUBcW5gZKzV7e9+DpVA==\r\n" \
  7003. "-----END CERTIFICATE-----\r\n"
  7004. #if !defined (TEST_CA_CRT_RSA_SOME)
  7005. const char mbedtls_test_ca_crt_rsa[] = TEST_CA_CRT_RSA_SHA1;
  7006. const size_t mbedtls_test_ca_crt_rsa_len = sizeof( mbedtls_test_ca_crt_rsa );
  7007. #endif
  7008. static const char mbedtls_test_ca_crt_rsa_sha1[] = TEST_CA_CRT_RSA_SHA1;
  7009. #endif
  7010. const char mbedtls_test_ca_key_rsa[] =
  7011. "-----BEGIN RSA PRIVATE KEY-----\r\n"
  7012. "Proc-Type: 4,ENCRYPTED\r\n"
  7013. "DEK-Info: DES-EDE3-CBC,A8A95B05D5B7206B\r\n"
  7014. "\r\n"
  7015. "9Qd9GeArejl1GDVh2lLV1bHt0cPtfbh5h/5zVpAVaFpqtSPMrElp50Rntn9et+JA\r\n"
  7016. "7VOyboR+Iy2t/HU4WvA687k3Bppe9GwKHjHhtl//8xFKwZr3Xb5yO5JUP8AUctQq\r\n"
  7017. "Nb8CLlZyuUC+52REAAthdWgsX+7dJO4yabzUcQ22Tp9JSD0hiL43BlkWYUNK3dAo\r\n"
  7018. "PZlmiptjnzVTjg1MxsBSydZinWOLBV8/JQgxSPo2yD4uEfig28qbvQ2wNIn0pnAb\r\n"
  7019. "GxnSAOazkongEGfvcjIIs+LZN9gXFhxcOh6kc4Q/c99B7QWETwLLkYgZ+z1a9VY9\r\n"
  7020. "gEU7CwCxYCD+h9hY6FPmsK0/lC4O7aeRKpYq00rPPxs6i7phiexg6ax6yTMmArQq\r\n"
  7021. "QmK3TAsJm8V/J5AWpLEV6jAFgRGymGGHnof0DXzVWZidrcZJWTNuGEX90nB3ee2w\r\n"
  7022. "PXJEFWKoD3K3aFcSLdHYr3mLGxP7H9ThQai9VsycxZKS5kwvBKQ//YMrmFfwPk8x\r\n"
  7023. "vTeY4KZMaUrveEel5tWZC94RSMKgxR6cyE1nBXyTQnDOGbfpNNgBKxyKbINWoOJU\r\n"
  7024. "WJZAwlsQn+QzCDwpri7+sV1mS3gBE6UY7aQmnmiiaC2V3Hbphxct/en5QsfDOt1X\r\n"
  7025. "JczSfpRWLlbPznZg8OQh/VgCMA58N5DjOzTIK7sJJ5r+94ZBTCpgAMbF588f0NTR\r\n"
  7026. "KCe4yrxGJR7X02M4nvD4IwOlpsQ8xQxZtOSgXv4LkxvdU9XJJKWZ/XNKJeWztxSe\r\n"
  7027. "Z1vdTc2YfsDBA2SEv33vxHx2g1vqtw8SjDRT2RaQSS0QuSaMJimdOX6mTOCBKk1J\r\n"
  7028. "9Q5mXTrER+/LnK0jEmXsBXWA5bqqVZIyahXSx4VYZ7l7w/PHiUDtDgyRhMMKi4n2\r\n"
  7029. "iQvQcWSQTjrpnlJbca1/DkpRt3YwrvJwdqb8asZU2VrNETh5x0QVefDRLFiVpif/\r\n"
  7030. "tUaeAe/P1F8OkS7OIZDs1SUbv/sD2vMbhNkUoCms3/PvNtdnvgL4F0zhaDpKCmlT\r\n"
  7031. "P8vx49E7v5CyRNmED9zZg4o3wmMqrQO93PtTug3Eu9oVx1zPQM1NVMyBa2+f29DL\r\n"
  7032. "1nuTCeXdo9+ni45xx+jAI4DCwrRdhJ9uzZyC6962H37H6D+5naNvClFR1s6li1Gb\r\n"
  7033. "nqPoiy/OBsEx9CaDGcqQBp5Wme/3XW+6z1ISOx+igwNTVCT14mHdBMbya0eIKft5\r\n"
  7034. "X+GnwtgEMyCYyyWuUct8g4RzErcY9+yW9Om5Hzpx4zOuW4NPZgPDTgK+t2RSL/Yq\r\n"
  7035. "rE1njrgeGYcVeG3f+OftH4s6fPbq7t1A5ZgUscbLMBqr9tK+OqygR4EgKBPsH6Cz\r\n"
  7036. "L6zlv/2RV0qAHvVuDJcIDIgwY5rJtINEm32rhOeFNJwZS5MNIC1czXZx5//ugX7l\r\n"
  7037. "I4sy5nbVhwSjtAk8Xg5dZbdTZ6mIrb7xqH+fdakZor1khG7bC2uIwibD3cSl2XkR\r\n"
  7038. "wN48lslbHnqqagr6Xm1nNOSVl8C/6kbJEsMpLhAezfRtGwvOucoaE+WbeUNolGde\r\n"
  7039. "P/eQiddSf0brnpiLJRh7qZrl9XuqYdpUqnoEdMAfotDOID8OtV7gt8a48ad8VPW2\r\n"
  7040. "-----END RSA PRIVATE KEY-----\r\n";
  7041. const size_t mbedtls_test_ca_key_rsa_len = sizeof( mbedtls_test_ca_key_rsa );
  7042. const char mbedtls_test_ca_pwd_rsa[] = "PolarSSLTest";
  7043. const size_t mbedtls_test_ca_pwd_rsa_len = sizeof( mbedtls_test_ca_pwd_rsa ) - 1;
  7044. const char mbedtls_test_srv_crt_rsa[] =
  7045. "-----BEGIN CERTIFICATE-----\r\n"
  7046. "MIIDNzCCAh+gAwIBAgIBAjANBgkqhkiG9w0BAQUFADA7MQswCQYDVQQGEwJOTDER\r\n"
  7047. "MA8GA1UEChMIUG9sYXJTU0wxGTAXBgNVBAMTEFBvbGFyU1NMIFRlc3QgQ0EwHhcN\r\n"
  7048. "MTEwMjEyMTQ0NDA2WhcNMjEwMjEyMTQ0NDA2WjA0MQswCQYDVQQGEwJOTDERMA8G\r\n"
  7049. "A1UEChMIUG9sYXJTU0wxEjAQBgNVBAMTCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcN\r\n"
  7050. "AQEBBQADggEPADCCAQoCggEBAMFNo93nzR3RBNdJcriZrA545Do8Ss86ExbQWuTN\r\n"
  7051. "owCIp+4ea5anUrSQ7y1yej4kmvy2NKwk9XfgJmSMnLAofaHa6ozmyRyWvP7BBFKz\r\n"
  7052. "NtSj+uGxdtiQwWG0ZlI2oiZTqqt0Xgd9GYLbKtgfoNkNHC1JZvdbJXNG6AuKT2kM\r\n"
  7053. "tQCQ4dqCEGZ9rlQri2V5kaHiYcPNQEkI7mgM8YuG0ka/0LiqEQMef1aoGh5EGA8P\r\n"
  7054. "hYvai0Re4hjGYi/HZo36Xdh98yeJKQHFkA4/J/EwyEoO79bex8cna8cFPXrEAjya\r\n"
  7055. "HT4P6DSYW8tzS1KW2BGiLICIaTla0w+w3lkvEcf36hIBMJcCAwEAAaNNMEswCQYD\r\n"
  7056. "VR0TBAIwADAdBgNVHQ4EFgQUpQXoZLjc32APUBJNYKhkr02LQ5MwHwYDVR0jBBgw\r\n"
  7057. "FoAUtFrkpbPe0lL2udWmlQ/rPrzH/f8wDQYJKoZIhvcNAQEFBQADggEBAJxnXClY\r\n"
  7058. "oHkbp70cqBrsGXLybA74czbO5RdLEgFs7rHVS9r+c293luS/KdliLScZqAzYVylw\r\n"
  7059. "UfRWvKMoWhHYKp3dEIS4xTXk6/5zXxhv9Rw8SGc8qn6vITHk1S1mPevtekgasY5Y\r\n"
  7060. "iWQuM3h4YVlRH3HHEMAD1TnAexfXHHDFQGe+Bd1iAbz1/sH9H8l4StwX6egvTK3M\r\n"
  7061. "wXRwkKkvjKaEDA9ATbZx0mI8LGsxSuCqe9r9dyjmttd47J1p1Rulz3CLzaRcVIuS\r\n"
  7062. "RRQfaD8neM9c1S/iJ/amTVqJxA1KOdOS5780WhPfSArA+g4qAmSjelc3p4wWpha8\r\n"
  7063. "zhuYwjVuX6JHG0c=\r\n"
  7064. "-----END CERTIFICATE-----\r\n";
  7065. const size_t mbedtls_test_srv_crt_rsa_len = sizeof( mbedtls_test_srv_crt_rsa );
  7066. const char mbedtls_test_srv_key_rsa[] =
  7067. "-----BEGIN RSA PRIVATE KEY-----\r\n"
  7068. "MIIEpAIBAAKCAQEAwU2j3efNHdEE10lyuJmsDnjkOjxKzzoTFtBa5M2jAIin7h5r\r\n"
  7069. "lqdStJDvLXJ6PiSa/LY0rCT1d+AmZIycsCh9odrqjObJHJa8/sEEUrM21KP64bF2\r\n"
  7070. "2JDBYbRmUjaiJlOqq3ReB30Zgtsq2B+g2Q0cLUlm91slc0boC4pPaQy1AJDh2oIQ\r\n"
  7071. "Zn2uVCuLZXmRoeJhw81ASQjuaAzxi4bSRr/QuKoRAx5/VqgaHkQYDw+Fi9qLRF7i\r\n"
  7072. "GMZiL8dmjfpd2H3zJ4kpAcWQDj8n8TDISg7v1t7HxydrxwU9esQCPJodPg/oNJhb\r\n"
  7073. "y3NLUpbYEaIsgIhpOVrTD7DeWS8Rx/fqEgEwlwIDAQABAoIBAQCXR0S8EIHFGORZ\r\n"
  7074. "++AtOg6eENxD+xVs0f1IeGz57Tjo3QnXX7VBZNdj+p1ECvhCE/G7XnkgU5hLZX+G\r\n"
  7075. "Z0jkz/tqJOI0vRSdLBbipHnWouyBQ4e/A1yIJdlBtqXxJ1KE/ituHRbNc4j4kL8Z\r\n"
  7076. "/r6pvwnTI0PSx2Eqs048YdS92LT6qAv4flbNDxMn2uY7s4ycS4Q8w1JXnCeaAnYm\r\n"
  7077. "WYI5wxO+bvRELR2Mcz5DmVnL8jRyml6l6582bSv5oufReFIbyPZbQWlXgYnpu6He\r\n"
  7078. "GTc7E1zKYQGG/9+DQUl/1vQuCPqQwny0tQoX2w5tdYpdMdVm+zkLtbajzdTviJJa\r\n"
  7079. "TWzL6lt5AoGBAN86+SVeJDcmQJcv4Eq6UhtRr4QGMiQMz0Sod6ettYxYzMgxtw28\r\n"
  7080. "CIrgpozCc+UaZJLo7UxvC6an85r1b2nKPCLQFaggJ0H4Q0J/sZOhBIXaoBzWxveK\r\n"
  7081. "nupceKdVxGsFi8CDy86DBfiyFivfBj+47BbaQzPBj7C4rK7UlLjab2rDAoGBAN2u\r\n"
  7082. "AM2gchoFiu4v1HFL8D7lweEpi6ZnMJjnEu/dEgGQJFjwdpLnPbsj4c75odQ4Gz8g\r\n"
  7083. "sw9lao9VVzbusoRE/JGI4aTdO0pATXyG7eG1Qu+5Yc1YGXcCrliA2xM9xx+d7f+s\r\n"
  7084. "mPzN+WIEg5GJDYZDjAzHG5BNvi/FfM1C9dOtjv2dAoGAF0t5KmwbjWHBhcVqO4Ic\r\n"
  7085. "BVvN3BIlc1ue2YRXEDlxY5b0r8N4XceMgKmW18OHApZxfl8uPDauWZLXOgl4uepv\r\n"
  7086. "whZC3EuWrSyyICNhLY21Ah7hbIEBPF3L3ZsOwC+UErL+dXWLdB56Jgy3gZaBeW7b\r\n"
  7087. "vDrEnocJbqCm7IukhXHOBK8CgYEAwqdHB0hqyNSzIOGY7v9abzB6pUdA3BZiQvEs\r\n"
  7088. "3LjHVd4HPJ2x0N8CgrBIWOE0q8+0hSMmeE96WW/7jD3fPWwCR5zlXknxBQsfv0gP\r\n"
  7089. "3BC5PR0Qdypz+d+9zfMf625kyit4T/hzwhDveZUzHnk1Cf+IG7Q+TOEnLnWAWBED\r\n"
  7090. "ISOWmrUCgYAFEmRxgwAc/u+D6t0syCwAYh6POtscq9Y0i9GyWk89NzgC4NdwwbBH\r\n"
  7091. "4AgahOxIxXx2gxJnq3yfkJfIjwf0s2DyP0kY2y6Ua1OeomPeY9mrIS4tCuDQ6LrE\r\n"
  7092. "TB6l9VGoxJL4fyHnZb8L5gGvnB1bbD8cL6YPaDiOhcRseC9vBiEuVg==\r\n"
  7093. "-----END RSA PRIVATE KEY-----\r\n";
  7094. const size_t mbedtls_test_srv_key_rsa_len = sizeof( mbedtls_test_srv_key_rsa );
  7095. const char mbedtls_test_cli_crt_rsa[] =
  7096. "-----BEGIN CERTIFICATE-----\r\n"
  7097. "MIIDhTCCAm2gAwIBAgIBBDANBgkqhkiG9w0BAQsFADA7MQswCQYDVQQGEwJOTDER\r\n"
  7098. "MA8GA1UECgwIUG9sYXJTU0wxGTAXBgNVBAMMEFBvbGFyU1NMIFRlc3QgQ0EwHhcN\r\n"
  7099. "MTcwNTA1MTMwNzU5WhcNMjcwNTA2MTMwNzU5WjA8MQswCQYDVQQGEwJOTDERMA8G\r\n"
  7100. "A1UECgwIUG9sYXJTU0wxGjAYBgNVBAMMEVBvbGFyU1NMIENsaWVudCAyMIIBIjAN\r\n"
  7101. "BgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyHTEzLn5tXnpRdkUYLB9u5Pyax6f\r\n"
  7102. "M60Nj4o8VmXl3ETZzGaFB9X4J7BKNdBjngpuG7fa8H6r7gwQk4ZJGDTzqCrSV/Uu\r\n"
  7103. "1C93KYRhTYJQj6eVSHD1bk2y1RPD0hrt5kPqQhTrdOrA7R/UV06p86jt0uDBMHEw\r\n"
  7104. "MjDV0/YI0FZPRo7yX/k9Z5GIMC5Cst99++UMd//sMcB4j7/Cf8qtbCHWjdmLao5v\r\n"
  7105. "4Jv4EFbMs44TFeY0BGbH7vk2DmqV9gmaBmf0ZXH4yqSxJeD+PIs1BGe64E92hfx/\r\n"
  7106. "/DZrtenNLQNiTrM9AM+vdqBpVoNq0qjU51Bx5rU2BXcFbXvI5MT9TNUhXwIDAQAB\r\n"
  7107. "o4GSMIGPMB0GA1UdDgQWBBRxoQBzckAvVHZeM/xSj7zx3WtGITBjBgNVHSMEXDBa\r\n"
  7108. "gBS0WuSls97SUva51aaVD+s+vMf9/6E/pD0wOzELMAkGA1UEBhMCTkwxETAPBgNV\r\n"
  7109. "BAoMCFBvbGFyU1NMMRkwFwYDVQQDDBBQb2xhclNTTCBUZXN0IENBggEAMAkGA1Ud\r\n"
  7110. "EwQCMAAwDQYJKoZIhvcNAQELBQADggEBAC7yO786NvcHpK8UovKIG9cB32oSQQom\r\n"
  7111. "LoR0eHDRzdqEkoq7yGZufHFiRAAzbMqJfogRtxlrWAeB4y/jGaMBV25IbFOIcH2W\r\n"
  7112. "iCEaMMbG+VQLKNvuC63kmw/Zewc9ThM6Pa1Hcy0axT0faf1B/U01j0FIcw/6mTfK\r\n"
  7113. "D8w48OIwc1yr0JtutCVjig5DC0yznGMt32RyseOLcUe+lfq005v2PAiCozr5X8rE\r\n"
  7114. "ofGZpiM2NqRPePgYy+Vc75Zk28xkRQq1ncprgQb3S4vTsZdScpM9hLf+eMlrgqlj\r\n"
  7115. "c5PLSkXBeLE5+fedkyfTaLxxQlgCpuoOhKBm04/R1pWNzUHyqagjO9Q=\r\n"
  7116. "-----END CERTIFICATE-----\r\n";
  7117. const size_t mbedtls_test_cli_crt_rsa_len = sizeof( mbedtls_test_cli_crt_rsa );
  7118. const char mbedtls_test_cli_key_rsa[] =
  7119. "-----BEGIN RSA PRIVATE KEY-----\r\n"
  7120. "MIIEpAIBAAKCAQEAyHTEzLn5tXnpRdkUYLB9u5Pyax6fM60Nj4o8VmXl3ETZzGaF\r\n"
  7121. "B9X4J7BKNdBjngpuG7fa8H6r7gwQk4ZJGDTzqCrSV/Uu1C93KYRhTYJQj6eVSHD1\r\n"
  7122. "bk2y1RPD0hrt5kPqQhTrdOrA7R/UV06p86jt0uDBMHEwMjDV0/YI0FZPRo7yX/k9\r\n"
  7123. "Z5GIMC5Cst99++UMd//sMcB4j7/Cf8qtbCHWjdmLao5v4Jv4EFbMs44TFeY0BGbH\r\n"
  7124. "7vk2DmqV9gmaBmf0ZXH4yqSxJeD+PIs1BGe64E92hfx//DZrtenNLQNiTrM9AM+v\r\n"
  7125. "dqBpVoNq0qjU51Bx5rU2BXcFbXvI5MT9TNUhXwIDAQABAoIBAGdNtfYDiap6bzst\r\n"
  7126. "yhCiI8m9TtrhZw4MisaEaN/ll3XSjaOG2dvV6xMZCMV+5TeXDHOAZnY18Yi18vzz\r\n"
  7127. "4Ut2TnNFzizCECYNaA2fST3WgInnxUkV3YXAyP6CNxJaCmv2aA0yFr2kFVSeaKGt\r\n"
  7128. "ymvljNp2NVkvm7Th8fBQBO7I7AXhz43k0mR7XmPgewe8ApZOG3hstkOaMvbWAvWA\r\n"
  7129. "zCZupdDjZYjOJqlA4eEA4H8/w7F83r5CugeBE8LgEREjLPiyejrU5H1fubEY+h0d\r\n"
  7130. "l5HZBJ68ybTXfQ5U9o/QKA3dd0toBEhhdRUDGzWtjvwkEQfqF1reGWj/tod/gCpf\r\n"
  7131. "DFi6X0ECgYEA4wOv/pjSC3ty6TuOvKX2rOUiBrLXXv2JSxZnMoMiWI5ipLQt+RYT\r\n"
  7132. "VPafL/m7Dn6MbwjayOkcZhBwk5CNz5A6Q4lJ64Mq/lqHznRCQQ2Mc1G8eyDF/fYL\r\n"
  7133. "Ze2pLvwP9VD5jTc2miDfw+MnvJhywRRLcemDFP8k4hQVtm8PMp3ZmNECgYEA4gz7\r\n"
  7134. "wzObR4gn8ibe617uQPZjWzUj9dUHYd+in1gwBCIrtNnaRn9I9U/Q6tegRYpii4ys\r\n"
  7135. "c176NmU+umy6XmuSKV5qD9bSpZWG2nLFnslrN15Lm3fhZxoeMNhBaEDTnLT26yoi\r\n"
  7136. "33gp0mSSWy94ZEqipms+ULF6sY1ZtFW6tpGFoy8CgYAQHhnnvJflIs2ky4q10B60\r\n"
  7137. "ZcxFp3rtDpkp0JxhFLhiizFrujMtZSjYNm5U7KkgPVHhLELEUvCmOnKTt4ap/vZ0\r\n"
  7138. "BxJNe1GZH3pW6SAvGDQpl9sG7uu/vTFP+lCxukmzxB0DrrDcvorEkKMom7ZCCRvW\r\n"
  7139. "KZsZ6YeH2Z81BauRj218kQKBgQCUV/DgKP2985xDTT79N08jUo3hTP5MVYCCuj/+\r\n"
  7140. "UeEw1TvZcx3LJby7P6Xad6a1/BqveaGyFKIfEFIaBUBItk801sDDpDaYc4gL00Xc\r\n"
  7141. "7lFuBHOZkxJYlss5QrGpuOEl9ZwUt5IrFLBdYaKqNHzNVC1pCPfb/JyH6Dr2HUxq\r\n"
  7142. "gxUwAQKBgQCcU6G2L8AG9d9c0UpOyL1tMvFe5Ttw0KjlQVdsh1MP6yigYo9DYuwu\r\n"
  7143. "bHFVW2r0dBTqegP2/KTOxKzaHfC1qf0RGDsUoJCNJrd1cwoCLG8P2EF4w3OBrKqv\r\n"
  7144. "8u4ytY0F+Vlanj5lm3TaoHSVF1+NWPyOTiwevIECGKwSxvlki4fDAA==\r\n"
  7145. "-----END RSA PRIVATE KEY-----\r\n";
  7146. const size_t mbedtls_test_cli_key_rsa_len = sizeof( mbedtls_test_cli_key_rsa );
  7147. #endif /* MBEDTLS_RSA_C */
  7148. #if defined(MBEDTLS_PEM_PARSE_C)
  7149. /* Concatenation of all available CA certificates */
  7150. const char mbedtls_test_cas_pem[] =
  7151. #ifdef TEST_CA_CRT_RSA_SHA1
  7152. TEST_CA_CRT_RSA_SHA1
  7153. #endif
  7154. #ifdef TEST_CA_CRT_RSA_SHA256
  7155. TEST_CA_CRT_RSA_SHA256
  7156. #endif
  7157. #ifdef TEST_CA_CRT_EC
  7158. TEST_CA_CRT_EC
  7159. #endif
  7160. "";
  7161. const size_t mbedtls_test_cas_pem_len = sizeof( mbedtls_test_cas_pem );
  7162. #endif
  7163. /* List of all available CA certificates */
  7164. const char * mbedtls_test_cas[] = {
  7165. #if defined(TEST_CA_CRT_RSA_SHA1)
  7166. mbedtls_test_ca_crt_rsa_sha1,
  7167. #endif
  7168. #if defined(TEST_CA_CRT_RSA_SHA256)
  7169. mbedtls_test_ca_crt_rsa_sha256,
  7170. #endif
  7171. #if defined(MBEDTLS_ECDSA_C)
  7172. mbedtls_test_ca_crt_ec,
  7173. #endif
  7174. NULL
  7175. };
  7176. const size_t mbedtls_test_cas_len[] = {
  7177. #if defined(TEST_CA_CRT_RSA_SHA1)
  7178. sizeof( mbedtls_test_ca_crt_rsa_sha1 ),
  7179. #endif
  7180. #if defined(TEST_CA_CRT_RSA_SHA256)
  7181. sizeof( mbedtls_test_ca_crt_rsa_sha256 ),
  7182. #endif
  7183. #if defined(MBEDTLS_ECDSA_C)
  7184. sizeof( mbedtls_test_ca_crt_ec ),
  7185. #endif
  7186. 0
  7187. };
  7188. #if defined(MBEDTLS_RSA_C)
  7189. const char *mbedtls_test_ca_crt = mbedtls_test_ca_crt_rsa; /* SHA1 or SHA256 */
  7190. const char *mbedtls_test_ca_key = mbedtls_test_ca_key_rsa;
  7191. const char *mbedtls_test_ca_pwd = mbedtls_test_ca_pwd_rsa;
  7192. const char *mbedtls_test_srv_crt = mbedtls_test_srv_crt_rsa;
  7193. const char *mbedtls_test_srv_key = mbedtls_test_srv_key_rsa;
  7194. const char *mbedtls_test_cli_crt = mbedtls_test_cli_crt_rsa;
  7195. const char *mbedtls_test_cli_key = mbedtls_test_cli_key_rsa;
  7196. const size_t mbedtls_test_ca_crt_len = sizeof( mbedtls_test_ca_crt_rsa );
  7197. const size_t mbedtls_test_ca_key_len = sizeof( mbedtls_test_ca_key_rsa );
  7198. const size_t mbedtls_test_ca_pwd_len = sizeof( mbedtls_test_ca_pwd_rsa ) - 1;
  7199. const size_t mbedtls_test_srv_crt_len = sizeof( mbedtls_test_srv_crt_rsa );
  7200. const size_t mbedtls_test_srv_key_len = sizeof( mbedtls_test_srv_key_rsa );
  7201. const size_t mbedtls_test_cli_crt_len = sizeof( mbedtls_test_cli_crt_rsa );
  7202. const size_t mbedtls_test_cli_key_len = sizeof( mbedtls_test_cli_key_rsa );
  7203. #else /* ! MBEDTLS_RSA_C, so MBEDTLS_ECDSA_C */
  7204. const char *mbedtls_test_ca_crt = mbedtls_test_ca_crt_ec;
  7205. const char *mbedtls_test_ca_key = mbedtls_test_ca_key_ec;
  7206. const char *mbedtls_test_ca_pwd = mbedtls_test_ca_pwd_ec;
  7207. const char *mbedtls_test_srv_crt = mbedtls_test_srv_crt_ec;
  7208. const char *mbedtls_test_srv_key = mbedtls_test_srv_key_ec;
  7209. const char *mbedtls_test_cli_crt = mbedtls_test_cli_crt_ec;
  7210. const char *mbedtls_test_cli_key = mbedtls_test_cli_key_ec;
  7211. const size_t mbedtls_test_ca_crt_len = sizeof( mbedtls_test_ca_crt_ec );
  7212. const size_t mbedtls_test_ca_key_len = sizeof( mbedtls_test_ca_key_ec );
  7213. const size_t mbedtls_test_ca_pwd_len = sizeof( mbedtls_test_ca_pwd_ec ) - 1;
  7214. const size_t mbedtls_test_srv_crt_len = sizeof( mbedtls_test_srv_crt_ec );
  7215. const size_t mbedtls_test_srv_key_len = sizeof( mbedtls_test_srv_key_ec );
  7216. const size_t mbedtls_test_cli_crt_len = sizeof( mbedtls_test_cli_crt_ec );
  7217. const size_t mbedtls_test_cli_key_len = sizeof( mbedtls_test_cli_key_ec );
  7218. #endif /* MBEDTLS_RSA_C */
  7219. #endif /* MBEDTLS_CERTS_C */
  7220. /*
  7221. Amalgamated build undefines
  7222. */
  7223. #undef ADD
  7224. #undef BC
  7225. #undef BEFORE_COLON
  7226. #undef F
  7227. #undef F0
  7228. #undef F1
  7229. #undef F2
  7230. #undef F3
  7231. #undef F4
  7232. #undef F5
  7233. #undef FSb
  7234. #undef K
  7235. #undef KK
  7236. #undef P
  7237. #undef R
  7238. #undef ROTR
  7239. #undef S
  7240. #undef S0
  7241. #undef S1
  7242. #undef S2
  7243. #undef S3
  7244. #undef SAFE_SNPRINTF
  7245. #undef SHR
  7246. #undef close
  7247. #undef read
  7248. #undef supported_init
  7249. #undef write
  7250. /********* Start of file library/cipher.c ************/
  7251. /**
  7252. * \file cipher.c
  7253. *
  7254. * \brief Generic cipher wrapper for mbed TLS
  7255. *
  7256. * \author Adriaan de Jong <dejong@fox-it.com>
  7257. *
  7258. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7259. * SPDX-License-Identifier: Apache-2.0
  7260. *
  7261. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  7262. * not use this file except in compliance with the License.
  7263. * You may obtain a copy of the License at
  7264. *
  7265. * http://www.apache.org/licenses/LICENSE-2.0
  7266. *
  7267. * Unless required by applicable law or agreed to in writing, software
  7268. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  7269. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  7270. * See the License for the specific language governing permissions and
  7271. * limitations under the License.
  7272. *
  7273. * This file is part of mbed TLS (https://tls.mbed.org)
  7274. */
  7275. #if !defined(MBEDTLS_CONFIG_FILE)
  7276. #else
  7277. #endif
  7278. #if defined(MBEDTLS_CIPHER_C)
  7279. #include <stdlib.h>
  7280. #include <string.h>
  7281. #if defined(MBEDTLS_GCM_C)
  7282. #endif
  7283. #if defined(MBEDTLS_CCM_C)
  7284. #endif
  7285. #if defined(MBEDTLS_CMAC_C)
  7286. #endif
  7287. #if defined(MBEDTLS_PLATFORM_C)
  7288. #else
  7289. #define mbedtls_calloc calloc
  7290. #define mbedtls_free free
  7291. #endif
  7292. #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
  7293. #define MBEDTLS_CIPHER_MODE_STREAM
  7294. #endif
  7295. /* Implementation that should never be optimized out by the compiler */
  7296. /* zeroize was here */
  7297. static int supported_init = 0;
  7298. const int *mbedtls_cipher_list( void )
  7299. {
  7300. const mbedtls_cipher_definition_t *def;
  7301. int *type;
  7302. if( ! supported_init )
  7303. {
  7304. def = mbedtls_cipher_definitions;
  7305. type = mbedtls_cipher_supported;
  7306. while( def->type != 0 )
  7307. *type++ = (*def++).type;
  7308. *type = 0;
  7309. supported_init = 1;
  7310. }
  7311. return( mbedtls_cipher_supported );
  7312. }
  7313. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
  7314. {
  7315. const mbedtls_cipher_definition_t *def;
  7316. for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
  7317. if( def->type == cipher_type )
  7318. return( def->info );
  7319. return( NULL );
  7320. }
  7321. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
  7322. {
  7323. const mbedtls_cipher_definition_t *def;
  7324. if( NULL == cipher_name )
  7325. return( NULL );
  7326. for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
  7327. if( ! strcmp( def->info->name, cipher_name ) )
  7328. return( def->info );
  7329. return( NULL );
  7330. }
  7331. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
  7332. int key_bitlen,
  7333. const mbedtls_cipher_mode_t mode )
  7334. {
  7335. const mbedtls_cipher_definition_t *def;
  7336. for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
  7337. if( def->info->base->cipher == cipher_id &&
  7338. def->info->key_bitlen == (unsigned) key_bitlen &&
  7339. def->info->mode == mode )
  7340. return( def->info );
  7341. return( NULL );
  7342. }
  7343. void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
  7344. {
  7345. memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
  7346. }
  7347. void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
  7348. {
  7349. if( ctx == NULL )
  7350. return;
  7351. #if defined(MBEDTLS_CMAC_C)
  7352. if( ctx->cmac_ctx )
  7353. {
  7354. mbedtls_zeroize( ctx->cmac_ctx, sizeof( mbedtls_cmac_context_t ) );
  7355. mbedtls_free( ctx->cmac_ctx );
  7356. }
  7357. #endif
  7358. if( ctx->cipher_ctx )
  7359. ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
  7360. mbedtls_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
  7361. }
  7362. int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
  7363. {
  7364. if( NULL == cipher_info || NULL == ctx )
  7365. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7366. memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
  7367. if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
  7368. return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
  7369. ctx->cipher_info = cipher_info;
  7370. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  7371. /*
  7372. * Ignore possible errors caused by a cipher mode that doesn't use padding
  7373. */
  7374. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  7375. (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
  7376. #else
  7377. (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
  7378. #endif
  7379. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  7380. return( 0 );
  7381. }
  7382. int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
  7383. int key_bitlen, const mbedtls_operation_t operation )
  7384. {
  7385. if( NULL == ctx || NULL == ctx->cipher_info )
  7386. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7387. if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
  7388. (int) ctx->cipher_info->key_bitlen != key_bitlen )
  7389. {
  7390. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7391. }
  7392. ctx->key_bitlen = key_bitlen;
  7393. ctx->operation = operation;
  7394. /*
  7395. * For CFB and CTR mode always use the encryption key schedule
  7396. */
  7397. if( MBEDTLS_ENCRYPT == operation ||
  7398. MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
  7399. MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
  7400. {
  7401. return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
  7402. ctx->key_bitlen );
  7403. }
  7404. if( MBEDTLS_DECRYPT == operation )
  7405. return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
  7406. ctx->key_bitlen );
  7407. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7408. }
  7409. int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
  7410. const unsigned char *iv, size_t iv_len )
  7411. {
  7412. size_t actual_iv_size;
  7413. if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
  7414. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7415. /* avoid buffer overflow in ctx->iv */
  7416. if( iv_len > MBEDTLS_MAX_IV_LENGTH )
  7417. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  7418. if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
  7419. actual_iv_size = iv_len;
  7420. else
  7421. {
  7422. actual_iv_size = ctx->cipher_info->iv_size;
  7423. /* avoid reading past the end of input buffer */
  7424. if( actual_iv_size > iv_len )
  7425. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7426. }
  7427. memcpy( ctx->iv, iv, actual_iv_size );
  7428. ctx->iv_size = actual_iv_size;
  7429. return( 0 );
  7430. }
  7431. int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
  7432. {
  7433. if( NULL == ctx || NULL == ctx->cipher_info )
  7434. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7435. ctx->unprocessed_len = 0;
  7436. return( 0 );
  7437. }
  7438. #if defined(MBEDTLS_GCM_C)
  7439. int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
  7440. const unsigned char *ad, size_t ad_len )
  7441. {
  7442. if( NULL == ctx || NULL == ctx->cipher_info )
  7443. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7444. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  7445. {
  7446. return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
  7447. ctx->iv, ctx->iv_size, ad, ad_len );
  7448. }
  7449. return( 0 );
  7450. }
  7451. #endif /* MBEDTLS_GCM_C */
  7452. int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
  7453. size_t ilen, unsigned char *output, size_t *olen )
  7454. {
  7455. int ret;
  7456. size_t block_size = 0;
  7457. if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
  7458. {
  7459. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7460. }
  7461. *olen = 0;
  7462. block_size = mbedtls_cipher_get_block_size( ctx );
  7463. if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
  7464. {
  7465. if( ilen != block_size )
  7466. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  7467. *olen = ilen;
  7468. if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
  7469. ctx->operation, input, output ) ) )
  7470. {
  7471. return( ret );
  7472. }
  7473. return( 0 );
  7474. }
  7475. #if defined(MBEDTLS_GCM_C)
  7476. if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
  7477. {
  7478. *olen = ilen;
  7479. return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
  7480. output );
  7481. }
  7482. #endif
  7483. if ( 0 == block_size )
  7484. {
  7485. return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
  7486. }
  7487. if( input == output &&
  7488. ( ctx->unprocessed_len != 0 || ilen % block_size ) )
  7489. {
  7490. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7491. }
  7492. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  7493. if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
  7494. {
  7495. size_t copy_len = 0;
  7496. /*
  7497. * If there is not enough data for a full block, cache it.
  7498. */
  7499. if( ( ctx->operation == MBEDTLS_DECRYPT &&
  7500. ilen <= block_size - ctx->unprocessed_len ) ||
  7501. ( ctx->operation == MBEDTLS_ENCRYPT &&
  7502. ilen < block_size - ctx->unprocessed_len ) )
  7503. {
  7504. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
  7505. ilen );
  7506. ctx->unprocessed_len += ilen;
  7507. return( 0 );
  7508. }
  7509. /*
  7510. * Process cached data first
  7511. */
  7512. if( 0 != ctx->unprocessed_len )
  7513. {
  7514. copy_len = block_size - ctx->unprocessed_len;
  7515. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
  7516. copy_len );
  7517. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  7518. ctx->operation, block_size, ctx->iv,
  7519. ctx->unprocessed_data, output ) ) )
  7520. {
  7521. return( ret );
  7522. }
  7523. *olen += block_size;
  7524. output += block_size;
  7525. ctx->unprocessed_len = 0;
  7526. input += copy_len;
  7527. ilen -= copy_len;
  7528. }
  7529. /*
  7530. * Cache final, incomplete block
  7531. */
  7532. if( 0 != ilen )
  7533. {
  7534. if( 0 == block_size )
  7535. {
  7536. return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
  7537. }
  7538. copy_len = ilen % block_size;
  7539. if( copy_len == 0 && ctx->operation == MBEDTLS_DECRYPT )
  7540. copy_len = block_size;
  7541. memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
  7542. copy_len );
  7543. ctx->unprocessed_len += copy_len;
  7544. ilen -= copy_len;
  7545. }
  7546. /*
  7547. * Process remaining full blocks
  7548. */
  7549. if( ilen )
  7550. {
  7551. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  7552. ctx->operation, ilen, ctx->iv, input, output ) ) )
  7553. {
  7554. return( ret );
  7555. }
  7556. *olen += ilen;
  7557. }
  7558. return( 0 );
  7559. }
  7560. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  7561. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  7562. if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
  7563. {
  7564. if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
  7565. ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
  7566. input, output ) ) )
  7567. {
  7568. return( ret );
  7569. }
  7570. *olen = ilen;
  7571. return( 0 );
  7572. }
  7573. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  7574. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  7575. if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
  7576. {
  7577. if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
  7578. ilen, &ctx->unprocessed_len, ctx->iv,
  7579. ctx->unprocessed_data, input, output ) ) )
  7580. {
  7581. return( ret );
  7582. }
  7583. *olen = ilen;
  7584. return( 0 );
  7585. }
  7586. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  7587. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  7588. if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
  7589. {
  7590. if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
  7591. ilen, input, output ) ) )
  7592. {
  7593. return( ret );
  7594. }
  7595. *olen = ilen;
  7596. return( 0 );
  7597. }
  7598. #endif /* MBEDTLS_CIPHER_MODE_STREAM */
  7599. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  7600. }
  7601. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  7602. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  7603. /*
  7604. * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
  7605. */
  7606. static void add_pkcs_padding( unsigned char *output, size_t output_len,
  7607. size_t data_len )
  7608. {
  7609. size_t padding_len = output_len - data_len;
  7610. unsigned char i;
  7611. for( i = 0; i < padding_len; i++ )
  7612. output[data_len + i] = (unsigned char) padding_len;
  7613. }
  7614. static int get_pkcs_padding( unsigned char *input, size_t input_len,
  7615. size_t *data_len )
  7616. {
  7617. size_t i, pad_idx;
  7618. unsigned char padding_len, bad = 0;
  7619. if( NULL == input || NULL == data_len )
  7620. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7621. padding_len = input[input_len - 1];
  7622. *data_len = input_len - padding_len;
  7623. /* Avoid logical || since it results in a branch */
  7624. bad |= padding_len > input_len;
  7625. bad |= padding_len == 0;
  7626. /* The number of bytes checked must be independent of padding_len,
  7627. * so pick input_len, which is usually 8 or 16 (one block) */
  7628. pad_idx = input_len - padding_len;
  7629. for( i = 0; i < input_len; i++ )
  7630. bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
  7631. return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
  7632. }
  7633. #endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
  7634. #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
  7635. /*
  7636. * One and zeros padding: fill with 80 00 ... 00
  7637. */
  7638. static void add_one_and_zeros_padding( unsigned char *output,
  7639. size_t output_len, size_t data_len )
  7640. {
  7641. size_t padding_len = output_len - data_len;
  7642. unsigned char i = 0;
  7643. output[data_len] = 0x80;
  7644. for( i = 1; i < padding_len; i++ )
  7645. output[data_len + i] = 0x00;
  7646. }
  7647. static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
  7648. size_t *data_len )
  7649. {
  7650. size_t i;
  7651. unsigned char done = 0, prev_done, bad;
  7652. if( NULL == input || NULL == data_len )
  7653. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7654. bad = 0x80;
  7655. *data_len = 0;
  7656. for( i = input_len; i > 0; i-- )
  7657. {
  7658. prev_done = done;
  7659. done |= ( input[i - 1] != 0 );
  7660. *data_len |= ( i - 1 ) * ( done != prev_done );
  7661. bad ^= input[i - 1] * ( done != prev_done );
  7662. }
  7663. return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
  7664. }
  7665. #endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
  7666. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
  7667. /*
  7668. * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
  7669. */
  7670. static void add_zeros_and_len_padding( unsigned char *output,
  7671. size_t output_len, size_t data_len )
  7672. {
  7673. size_t padding_len = output_len - data_len;
  7674. unsigned char i = 0;
  7675. for( i = 1; i < padding_len; i++ )
  7676. output[data_len + i - 1] = 0x00;
  7677. output[output_len - 1] = (unsigned char) padding_len;
  7678. }
  7679. static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
  7680. size_t *data_len )
  7681. {
  7682. size_t i, pad_idx;
  7683. unsigned char padding_len, bad = 0;
  7684. if( NULL == input || NULL == data_len )
  7685. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7686. padding_len = input[input_len - 1];
  7687. *data_len = input_len - padding_len;
  7688. /* Avoid logical || since it results in a branch */
  7689. bad |= padding_len > input_len;
  7690. bad |= padding_len == 0;
  7691. /* The number of bytes checked must be independent of padding_len */
  7692. pad_idx = input_len - padding_len;
  7693. for( i = 0; i < input_len - 1; i++ )
  7694. bad |= input[i] * ( i >= pad_idx );
  7695. return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
  7696. }
  7697. #endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
  7698. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
  7699. /*
  7700. * Zero padding: fill with 00 ... 00
  7701. */
  7702. static void add_zeros_padding( unsigned char *output,
  7703. size_t output_len, size_t data_len )
  7704. {
  7705. size_t i;
  7706. for( i = data_len; i < output_len; i++ )
  7707. output[i] = 0x00;
  7708. }
  7709. static int get_zeros_padding( unsigned char *input, size_t input_len,
  7710. size_t *data_len )
  7711. {
  7712. size_t i;
  7713. unsigned char done = 0, prev_done;
  7714. if( NULL == input || NULL == data_len )
  7715. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7716. *data_len = 0;
  7717. for( i = input_len; i > 0; i-- )
  7718. {
  7719. prev_done = done;
  7720. done |= ( input[i-1] != 0 );
  7721. *data_len |= i * ( done != prev_done );
  7722. }
  7723. return( 0 );
  7724. }
  7725. #endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
  7726. /*
  7727. * No padding: don't pad :)
  7728. *
  7729. * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
  7730. * but a trivial get_padding function
  7731. */
  7732. static int get_no_padding( unsigned char *input, size_t input_len,
  7733. size_t *data_len )
  7734. {
  7735. if( NULL == input || NULL == data_len )
  7736. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7737. *data_len = input_len;
  7738. return( 0 );
  7739. }
  7740. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  7741. int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
  7742. unsigned char *output, size_t *olen )
  7743. {
  7744. if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
  7745. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7746. *olen = 0;
  7747. if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
  7748. MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
  7749. MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
  7750. MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
  7751. {
  7752. return( 0 );
  7753. }
  7754. if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
  7755. {
  7756. if( ctx->unprocessed_len != 0 )
  7757. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  7758. return( 0 );
  7759. }
  7760. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  7761. if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
  7762. {
  7763. int ret = 0;
  7764. if( MBEDTLS_ENCRYPT == ctx->operation )
  7765. {
  7766. /* check for 'no padding' mode */
  7767. if( NULL == ctx->add_padding )
  7768. {
  7769. if( 0 != ctx->unprocessed_len )
  7770. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  7771. return( 0 );
  7772. }
  7773. ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
  7774. ctx->unprocessed_len );
  7775. }
  7776. else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
  7777. {
  7778. /*
  7779. * For decrypt operations, expect a full block,
  7780. * or an empty block if no padding
  7781. */
  7782. if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
  7783. return( 0 );
  7784. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  7785. }
  7786. /* cipher block */
  7787. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  7788. ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
  7789. ctx->unprocessed_data, output ) ) )
  7790. {
  7791. return( ret );
  7792. }
  7793. /* Set output size for decryption */
  7794. if( MBEDTLS_DECRYPT == ctx->operation )
  7795. return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
  7796. olen );
  7797. /* Set output size for encryption */
  7798. *olen = mbedtls_cipher_get_block_size( ctx );
  7799. return( 0 );
  7800. }
  7801. #else
  7802. ((void) output);
  7803. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  7804. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  7805. }
  7806. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  7807. int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
  7808. {
  7809. if( NULL == ctx ||
  7810. MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
  7811. {
  7812. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7813. }
  7814. switch( mode )
  7815. {
  7816. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  7817. case MBEDTLS_PADDING_PKCS7:
  7818. ctx->add_padding = add_pkcs_padding;
  7819. ctx->get_padding = get_pkcs_padding;
  7820. break;
  7821. #endif
  7822. #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
  7823. case MBEDTLS_PADDING_ONE_AND_ZEROS:
  7824. ctx->add_padding = add_one_and_zeros_padding;
  7825. ctx->get_padding = get_one_and_zeros_padding;
  7826. break;
  7827. #endif
  7828. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
  7829. case MBEDTLS_PADDING_ZEROS_AND_LEN:
  7830. ctx->add_padding = add_zeros_and_len_padding;
  7831. ctx->get_padding = get_zeros_and_len_padding;
  7832. break;
  7833. #endif
  7834. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
  7835. case MBEDTLS_PADDING_ZEROS:
  7836. ctx->add_padding = add_zeros_padding;
  7837. ctx->get_padding = get_zeros_padding;
  7838. break;
  7839. #endif
  7840. case MBEDTLS_PADDING_NONE:
  7841. ctx->add_padding = NULL;
  7842. ctx->get_padding = get_no_padding;
  7843. break;
  7844. default:
  7845. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  7846. }
  7847. return( 0 );
  7848. }
  7849. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  7850. #if defined(MBEDTLS_GCM_C)
  7851. int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
  7852. unsigned char *tag, size_t tag_len )
  7853. {
  7854. if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
  7855. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7856. if( MBEDTLS_ENCRYPT != ctx->operation )
  7857. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7858. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  7859. return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
  7860. return( 0 );
  7861. }
  7862. int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
  7863. const unsigned char *tag, size_t tag_len )
  7864. {
  7865. int ret;
  7866. if( NULL == ctx || NULL == ctx->cipher_info ||
  7867. MBEDTLS_DECRYPT != ctx->operation )
  7868. {
  7869. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7870. }
  7871. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  7872. {
  7873. unsigned char check_tag[16];
  7874. size_t i;
  7875. int diff;
  7876. if( tag_len > sizeof( check_tag ) )
  7877. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  7878. if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
  7879. check_tag, tag_len ) ) )
  7880. {
  7881. return( ret );
  7882. }
  7883. /* Check the tag in "constant-time" */
  7884. for( diff = 0, i = 0; i < tag_len; i++ )
  7885. diff |= tag[i] ^ check_tag[i];
  7886. if( diff != 0 )
  7887. return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
  7888. return( 0 );
  7889. }
  7890. return( 0 );
  7891. }
  7892. #endif /* MBEDTLS_GCM_C */
  7893. /*
  7894. * Packet-oriented wrapper for non-AEAD modes
  7895. */
  7896. int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
  7897. const unsigned char *iv, size_t iv_len,
  7898. const unsigned char *input, size_t ilen,
  7899. unsigned char *output, size_t *olen )
  7900. {
  7901. int ret;
  7902. size_t finish_olen;
  7903. if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
  7904. return( ret );
  7905. if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
  7906. return( ret );
  7907. if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
  7908. return( ret );
  7909. if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
  7910. return( ret );
  7911. *olen += finish_olen;
  7912. return( 0 );
  7913. }
  7914. #if defined(MBEDTLS_CIPHER_MODE_AEAD)
  7915. /*
  7916. * Packet-oriented encryption for AEAD modes
  7917. */
  7918. int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
  7919. const unsigned char *iv, size_t iv_len,
  7920. const unsigned char *ad, size_t ad_len,
  7921. const unsigned char *input, size_t ilen,
  7922. unsigned char *output, size_t *olen,
  7923. unsigned char *tag, size_t tag_len )
  7924. {
  7925. #if defined(MBEDTLS_GCM_C)
  7926. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  7927. {
  7928. *olen = ilen;
  7929. return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
  7930. iv, iv_len, ad, ad_len, input, output,
  7931. tag_len, tag ) );
  7932. }
  7933. #endif /* MBEDTLS_GCM_C */
  7934. #if defined(MBEDTLS_CCM_C)
  7935. if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
  7936. {
  7937. *olen = ilen;
  7938. return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
  7939. iv, iv_len, ad, ad_len, input, output,
  7940. tag, tag_len ) );
  7941. }
  7942. #endif /* MBEDTLS_CCM_C */
  7943. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  7944. }
  7945. /*
  7946. * Packet-oriented decryption for AEAD modes
  7947. */
  7948. int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
  7949. const unsigned char *iv, size_t iv_len,
  7950. const unsigned char *ad, size_t ad_len,
  7951. const unsigned char *input, size_t ilen,
  7952. unsigned char *output, size_t *olen,
  7953. const unsigned char *tag, size_t tag_len )
  7954. {
  7955. #if defined(MBEDTLS_GCM_C)
  7956. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  7957. {
  7958. int ret;
  7959. *olen = ilen;
  7960. ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
  7961. iv, iv_len, ad, ad_len,
  7962. tag, tag_len, input, output );
  7963. if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
  7964. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  7965. return( ret );
  7966. }
  7967. #endif /* MBEDTLS_GCM_C */
  7968. #if defined(MBEDTLS_CCM_C)
  7969. if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
  7970. {
  7971. int ret;
  7972. *olen = ilen;
  7973. ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
  7974. iv, iv_len, ad, ad_len,
  7975. input, output, tag, tag_len );
  7976. if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
  7977. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  7978. return( ret );
  7979. }
  7980. #endif /* MBEDTLS_CCM_C */
  7981. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  7982. }
  7983. #endif /* MBEDTLS_CIPHER_MODE_AEAD */
  7984. #endif /* MBEDTLS_CIPHER_C */
  7985. /*
  7986. Amalgamated build undefines
  7987. */
  7988. #undef ADD
  7989. #undef BC
  7990. #undef BEFORE_COLON
  7991. #undef F
  7992. #undef F0
  7993. #undef F1
  7994. #undef F2
  7995. #undef F3
  7996. #undef F4
  7997. #undef F5
  7998. #undef FSb
  7999. #undef K
  8000. #undef KK
  8001. #undef P
  8002. #undef R
  8003. #undef ROTR
  8004. #undef S
  8005. #undef S0
  8006. #undef S1
  8007. #undef S2
  8008. #undef S3
  8009. #undef SAFE_SNPRINTF
  8010. #undef SHR
  8011. #undef close
  8012. #undef read
  8013. #undef supported_init
  8014. #undef write
  8015. /********* Start of file library/cipher_wrap.c ************/
  8016. /**
  8017. * \file cipher_wrap.c
  8018. *
  8019. * \brief Generic cipher wrapper for mbed TLS
  8020. *
  8021. * \author Adriaan de Jong <dejong@fox-it.com>
  8022. *
  8023. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  8024. * SPDX-License-Identifier: Apache-2.0
  8025. *
  8026. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8027. * not use this file except in compliance with the License.
  8028. * You may obtain a copy of the License at
  8029. *
  8030. * http://www.apache.org/licenses/LICENSE-2.0
  8031. *
  8032. * Unless required by applicable law or agreed to in writing, software
  8033. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  8034. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8035. * See the License for the specific language governing permissions and
  8036. * limitations under the License.
  8037. *
  8038. * This file is part of mbed TLS (https://tls.mbed.org)
  8039. */
  8040. #if !defined(MBEDTLS_CONFIG_FILE)
  8041. #else
  8042. #endif
  8043. #if defined(MBEDTLS_CIPHER_C)
  8044. #if defined(MBEDTLS_AES_C)
  8045. #endif
  8046. #if defined(MBEDTLS_ARC4_C)
  8047. #endif
  8048. #if defined(MBEDTLS_CAMELLIA_C)
  8049. #endif
  8050. #if defined(MBEDTLS_DES_C)
  8051. #endif
  8052. #if defined(MBEDTLS_BLOWFISH_C)
  8053. #endif
  8054. #if defined(MBEDTLS_GCM_C)
  8055. #endif
  8056. #if defined(MBEDTLS_CCM_C)
  8057. #endif
  8058. #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
  8059. #include <string.h>
  8060. #endif
  8061. #if defined(MBEDTLS_PLATFORM_C)
  8062. #else
  8063. #include <stdlib.h>
  8064. #define mbedtls_calloc calloc
  8065. #define mbedtls_free free
  8066. #endif
  8067. #if defined(MBEDTLS_GCM_C)
  8068. /* shared by all GCM ciphers */
  8069. static void *gcm_ctx_alloc( void )
  8070. {
  8071. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_gcm_context ) );
  8072. if( ctx != NULL )
  8073. mbedtls_gcm_init( (mbedtls_gcm_context *) ctx );
  8074. return( ctx );
  8075. }
  8076. static void gcm_ctx_free( void *ctx )
  8077. {
  8078. mbedtls_gcm_free( ctx );
  8079. mbedtls_free( ctx );
  8080. }
  8081. #endif /* MBEDTLS_GCM_C */
  8082. #if defined(MBEDTLS_CCM_C)
  8083. /* shared by all CCM ciphers */
  8084. static void *ccm_ctx_alloc( void )
  8085. {
  8086. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ccm_context ) );
  8087. if( ctx != NULL )
  8088. mbedtls_ccm_init( (mbedtls_ccm_context *) ctx );
  8089. return( ctx );
  8090. }
  8091. static void ccm_ctx_free( void *ctx )
  8092. {
  8093. mbedtls_ccm_free( ctx );
  8094. mbedtls_free( ctx );
  8095. }
  8096. #endif /* MBEDTLS_CCM_C */
  8097. #if defined(MBEDTLS_AES_C)
  8098. static int aes_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
  8099. const unsigned char *input, unsigned char *output )
  8100. {
  8101. return mbedtls_aes_crypt_ecb( (mbedtls_aes_context *) ctx, operation, input, output );
  8102. }
  8103. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8104. static int aes_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
  8105. unsigned char *iv, const unsigned char *input, unsigned char *output )
  8106. {
  8107. return mbedtls_aes_crypt_cbc( (mbedtls_aes_context *) ctx, operation, length, iv, input,
  8108. output );
  8109. }
  8110. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  8111. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  8112. static int aes_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
  8113. size_t length, size_t *iv_off, unsigned char *iv,
  8114. const unsigned char *input, unsigned char *output )
  8115. {
  8116. return mbedtls_aes_crypt_cfb128( (mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
  8117. input, output );
  8118. }
  8119. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  8120. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  8121. static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
  8122. unsigned char *nonce_counter, unsigned char *stream_block,
  8123. const unsigned char *input, unsigned char *output )
  8124. {
  8125. return mbedtls_aes_crypt_ctr( (mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
  8126. stream_block, input, output );
  8127. }
  8128. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  8129. static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
  8130. unsigned int key_bitlen )
  8131. {
  8132. return mbedtls_aes_setkey_dec( (mbedtls_aes_context *) ctx, key, key_bitlen );
  8133. }
  8134. static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
  8135. unsigned int key_bitlen )
  8136. {
  8137. return mbedtls_aes_setkey_enc( (mbedtls_aes_context *) ctx, key, key_bitlen );
  8138. }
  8139. static void * aes_ctx_alloc( void )
  8140. {
  8141. mbedtls_aes_context *aes = mbedtls_calloc( 1, sizeof( mbedtls_aes_context ) );
  8142. if( aes == NULL )
  8143. return( NULL );
  8144. mbedtls_aes_init( aes );
  8145. return( aes );
  8146. }
  8147. static void aes_ctx_free( void *ctx )
  8148. {
  8149. mbedtls_aes_free( (mbedtls_aes_context *) ctx );
  8150. mbedtls_free( ctx );
  8151. }
  8152. static const mbedtls_cipher_base_t aes_info = {
  8153. MBEDTLS_CIPHER_ID_AES,
  8154. aes_crypt_ecb_wrap,
  8155. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8156. aes_crypt_cbc_wrap,
  8157. #endif
  8158. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  8159. aes_crypt_cfb128_wrap,
  8160. #endif
  8161. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  8162. aes_crypt_ctr_wrap,
  8163. #endif
  8164. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  8165. NULL,
  8166. #endif
  8167. aes_setkey_enc_wrap,
  8168. aes_setkey_dec_wrap,
  8169. aes_ctx_alloc,
  8170. aes_ctx_free
  8171. };
  8172. static const mbedtls_cipher_info_t aes_128_ecb_info = {
  8173. MBEDTLS_CIPHER_AES_128_ECB,
  8174. MBEDTLS_MODE_ECB,
  8175. 128,
  8176. "AES-128-ECB",
  8177. 16,
  8178. 0,
  8179. 16,
  8180. &aes_info
  8181. };
  8182. static const mbedtls_cipher_info_t aes_192_ecb_info = {
  8183. MBEDTLS_CIPHER_AES_192_ECB,
  8184. MBEDTLS_MODE_ECB,
  8185. 192,
  8186. "AES-192-ECB",
  8187. 16,
  8188. 0,
  8189. 16,
  8190. &aes_info
  8191. };
  8192. static const mbedtls_cipher_info_t aes_256_ecb_info = {
  8193. MBEDTLS_CIPHER_AES_256_ECB,
  8194. MBEDTLS_MODE_ECB,
  8195. 256,
  8196. "AES-256-ECB",
  8197. 16,
  8198. 0,
  8199. 16,
  8200. &aes_info
  8201. };
  8202. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8203. static const mbedtls_cipher_info_t aes_128_cbc_info = {
  8204. MBEDTLS_CIPHER_AES_128_CBC,
  8205. MBEDTLS_MODE_CBC,
  8206. 128,
  8207. "AES-128-CBC",
  8208. 16,
  8209. 0,
  8210. 16,
  8211. &aes_info
  8212. };
  8213. static const mbedtls_cipher_info_t aes_192_cbc_info = {
  8214. MBEDTLS_CIPHER_AES_192_CBC,
  8215. MBEDTLS_MODE_CBC,
  8216. 192,
  8217. "AES-192-CBC",
  8218. 16,
  8219. 0,
  8220. 16,
  8221. &aes_info
  8222. };
  8223. static const mbedtls_cipher_info_t aes_256_cbc_info = {
  8224. MBEDTLS_CIPHER_AES_256_CBC,
  8225. MBEDTLS_MODE_CBC,
  8226. 256,
  8227. "AES-256-CBC",
  8228. 16,
  8229. 0,
  8230. 16,
  8231. &aes_info
  8232. };
  8233. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  8234. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  8235. static const mbedtls_cipher_info_t aes_128_cfb128_info = {
  8236. MBEDTLS_CIPHER_AES_128_CFB128,
  8237. MBEDTLS_MODE_CFB,
  8238. 128,
  8239. "AES-128-CFB128",
  8240. 16,
  8241. 0,
  8242. 16,
  8243. &aes_info
  8244. };
  8245. static const mbedtls_cipher_info_t aes_192_cfb128_info = {
  8246. MBEDTLS_CIPHER_AES_192_CFB128,
  8247. MBEDTLS_MODE_CFB,
  8248. 192,
  8249. "AES-192-CFB128",
  8250. 16,
  8251. 0,
  8252. 16,
  8253. &aes_info
  8254. };
  8255. static const mbedtls_cipher_info_t aes_256_cfb128_info = {
  8256. MBEDTLS_CIPHER_AES_256_CFB128,
  8257. MBEDTLS_MODE_CFB,
  8258. 256,
  8259. "AES-256-CFB128",
  8260. 16,
  8261. 0,
  8262. 16,
  8263. &aes_info
  8264. };
  8265. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  8266. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  8267. static const mbedtls_cipher_info_t aes_128_ctr_info = {
  8268. MBEDTLS_CIPHER_AES_128_CTR,
  8269. MBEDTLS_MODE_CTR,
  8270. 128,
  8271. "AES-128-CTR",
  8272. 16,
  8273. 0,
  8274. 16,
  8275. &aes_info
  8276. };
  8277. static const mbedtls_cipher_info_t aes_192_ctr_info = {
  8278. MBEDTLS_CIPHER_AES_192_CTR,
  8279. MBEDTLS_MODE_CTR,
  8280. 192,
  8281. "AES-192-CTR",
  8282. 16,
  8283. 0,
  8284. 16,
  8285. &aes_info
  8286. };
  8287. static const mbedtls_cipher_info_t aes_256_ctr_info = {
  8288. MBEDTLS_CIPHER_AES_256_CTR,
  8289. MBEDTLS_MODE_CTR,
  8290. 256,
  8291. "AES-256-CTR",
  8292. 16,
  8293. 0,
  8294. 16,
  8295. &aes_info
  8296. };
  8297. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  8298. #if defined(MBEDTLS_GCM_C)
  8299. static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key,
  8300. unsigned int key_bitlen )
  8301. {
  8302. return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
  8303. key, key_bitlen );
  8304. }
  8305. static const mbedtls_cipher_base_t gcm_aes_info = {
  8306. MBEDTLS_CIPHER_ID_AES,
  8307. NULL,
  8308. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8309. NULL,
  8310. #endif
  8311. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  8312. NULL,
  8313. #endif
  8314. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  8315. NULL,
  8316. #endif
  8317. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  8318. NULL,
  8319. #endif
  8320. gcm_aes_setkey_wrap,
  8321. gcm_aes_setkey_wrap,
  8322. gcm_ctx_alloc,
  8323. gcm_ctx_free,
  8324. };
  8325. static const mbedtls_cipher_info_t aes_128_gcm_info = {
  8326. MBEDTLS_CIPHER_AES_128_GCM,
  8327. MBEDTLS_MODE_GCM,
  8328. 128,
  8329. "AES-128-GCM",
  8330. 12,
  8331. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  8332. 16,
  8333. &gcm_aes_info
  8334. };
  8335. static const mbedtls_cipher_info_t aes_192_gcm_info = {
  8336. MBEDTLS_CIPHER_AES_192_GCM,
  8337. MBEDTLS_MODE_GCM,
  8338. 192,
  8339. "AES-192-GCM",
  8340. 12,
  8341. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  8342. 16,
  8343. &gcm_aes_info
  8344. };
  8345. static const mbedtls_cipher_info_t aes_256_gcm_info = {
  8346. MBEDTLS_CIPHER_AES_256_GCM,
  8347. MBEDTLS_MODE_GCM,
  8348. 256,
  8349. "AES-256-GCM",
  8350. 12,
  8351. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  8352. 16,
  8353. &gcm_aes_info
  8354. };
  8355. #endif /* MBEDTLS_GCM_C */
  8356. #if defined(MBEDTLS_CCM_C)
  8357. static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key,
  8358. unsigned int key_bitlen )
  8359. {
  8360. return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
  8361. key, key_bitlen );
  8362. }
  8363. static const mbedtls_cipher_base_t ccm_aes_info = {
  8364. MBEDTLS_CIPHER_ID_AES,
  8365. NULL,
  8366. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8367. NULL,
  8368. #endif
  8369. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  8370. NULL,
  8371. #endif
  8372. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  8373. NULL,
  8374. #endif
  8375. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  8376. NULL,
  8377. #endif
  8378. ccm_aes_setkey_wrap,
  8379. ccm_aes_setkey_wrap,
  8380. ccm_ctx_alloc,
  8381. ccm_ctx_free,
  8382. };
  8383. static const mbedtls_cipher_info_t aes_128_ccm_info = {
  8384. MBEDTLS_CIPHER_AES_128_CCM,
  8385. MBEDTLS_MODE_CCM,
  8386. 128,
  8387. "AES-128-CCM",
  8388. 12,
  8389. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  8390. 16,
  8391. &ccm_aes_info
  8392. };
  8393. static const mbedtls_cipher_info_t aes_192_ccm_info = {
  8394. MBEDTLS_CIPHER_AES_192_CCM,
  8395. MBEDTLS_MODE_CCM,
  8396. 192,
  8397. "AES-192-CCM",
  8398. 12,
  8399. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  8400. 16,
  8401. &ccm_aes_info
  8402. };
  8403. static const mbedtls_cipher_info_t aes_256_ccm_info = {
  8404. MBEDTLS_CIPHER_AES_256_CCM,
  8405. MBEDTLS_MODE_CCM,
  8406. 256,
  8407. "AES-256-CCM",
  8408. 12,
  8409. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  8410. 16,
  8411. &ccm_aes_info
  8412. };
  8413. #endif /* MBEDTLS_CCM_C */
  8414. #endif /* MBEDTLS_AES_C */
  8415. #if defined(MBEDTLS_CAMELLIA_C)
  8416. static int camellia_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
  8417. const unsigned char *input, unsigned char *output )
  8418. {
  8419. return mbedtls_camellia_crypt_ecb( (mbedtls_camellia_context *) ctx, operation, input,
  8420. output );
  8421. }
  8422. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8423. static int camellia_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
  8424. size_t length, unsigned char *iv,
  8425. const unsigned char *input, unsigned char *output )
  8426. {
  8427. return mbedtls_camellia_crypt_cbc( (mbedtls_camellia_context *) ctx, operation, length, iv,
  8428. input, output );
  8429. }
  8430. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  8431. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  8432. static int camellia_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
  8433. size_t length, size_t *iv_off, unsigned char *iv,
  8434. const unsigned char *input, unsigned char *output )
  8435. {
  8436. return mbedtls_camellia_crypt_cfb128( (mbedtls_camellia_context *) ctx, operation, length,
  8437. iv_off, iv, input, output );
  8438. }
  8439. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  8440. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  8441. static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
  8442. unsigned char *nonce_counter, unsigned char *stream_block,
  8443. const unsigned char *input, unsigned char *output )
  8444. {
  8445. return mbedtls_camellia_crypt_ctr( (mbedtls_camellia_context *) ctx, length, nc_off,
  8446. nonce_counter, stream_block, input, output );
  8447. }
  8448. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  8449. static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key,
  8450. unsigned int key_bitlen )
  8451. {
  8452. return mbedtls_camellia_setkey_dec( (mbedtls_camellia_context *) ctx, key, key_bitlen );
  8453. }
  8454. static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
  8455. unsigned int key_bitlen )
  8456. {
  8457. return mbedtls_camellia_setkey_enc( (mbedtls_camellia_context *) ctx, key, key_bitlen );
  8458. }
  8459. static void * camellia_ctx_alloc( void )
  8460. {
  8461. mbedtls_camellia_context *ctx;
  8462. ctx = mbedtls_calloc( 1, sizeof( mbedtls_camellia_context ) );
  8463. if( ctx == NULL )
  8464. return( NULL );
  8465. mbedtls_camellia_init( ctx );
  8466. return( ctx );
  8467. }
  8468. static void camellia_ctx_free( void *ctx )
  8469. {
  8470. mbedtls_camellia_free( (mbedtls_camellia_context *) ctx );
  8471. mbedtls_free( ctx );
  8472. }
  8473. static const mbedtls_cipher_base_t camellia_info = {
  8474. MBEDTLS_CIPHER_ID_CAMELLIA,
  8475. camellia_crypt_ecb_wrap,
  8476. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8477. camellia_crypt_cbc_wrap,
  8478. #endif
  8479. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  8480. camellia_crypt_cfb128_wrap,
  8481. #endif
  8482. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  8483. camellia_crypt_ctr_wrap,
  8484. #endif
  8485. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  8486. NULL,
  8487. #endif
  8488. camellia_setkey_enc_wrap,
  8489. camellia_setkey_dec_wrap,
  8490. camellia_ctx_alloc,
  8491. camellia_ctx_free
  8492. };
  8493. static const mbedtls_cipher_info_t camellia_128_ecb_info = {
  8494. MBEDTLS_CIPHER_CAMELLIA_128_ECB,
  8495. MBEDTLS_MODE_ECB,
  8496. 128,
  8497. "CAMELLIA-128-ECB",
  8498. 16,
  8499. 0,
  8500. 16,
  8501. &camellia_info
  8502. };
  8503. static const mbedtls_cipher_info_t camellia_192_ecb_info = {
  8504. MBEDTLS_CIPHER_CAMELLIA_192_ECB,
  8505. MBEDTLS_MODE_ECB,
  8506. 192,
  8507. "CAMELLIA-192-ECB",
  8508. 16,
  8509. 0,
  8510. 16,
  8511. &camellia_info
  8512. };
  8513. static const mbedtls_cipher_info_t camellia_256_ecb_info = {
  8514. MBEDTLS_CIPHER_CAMELLIA_256_ECB,
  8515. MBEDTLS_MODE_ECB,
  8516. 256,
  8517. "CAMELLIA-256-ECB",
  8518. 16,
  8519. 0,
  8520. 16,
  8521. &camellia_info
  8522. };
  8523. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8524. static const mbedtls_cipher_info_t camellia_128_cbc_info = {
  8525. MBEDTLS_CIPHER_CAMELLIA_128_CBC,
  8526. MBEDTLS_MODE_CBC,
  8527. 128,
  8528. "CAMELLIA-128-CBC",
  8529. 16,
  8530. 0,
  8531. 16,
  8532. &camellia_info
  8533. };
  8534. static const mbedtls_cipher_info_t camellia_192_cbc_info = {
  8535. MBEDTLS_CIPHER_CAMELLIA_192_CBC,
  8536. MBEDTLS_MODE_CBC,
  8537. 192,
  8538. "CAMELLIA-192-CBC",
  8539. 16,
  8540. 0,
  8541. 16,
  8542. &camellia_info
  8543. };
  8544. static const mbedtls_cipher_info_t camellia_256_cbc_info = {
  8545. MBEDTLS_CIPHER_CAMELLIA_256_CBC,
  8546. MBEDTLS_MODE_CBC,
  8547. 256,
  8548. "CAMELLIA-256-CBC",
  8549. 16,
  8550. 0,
  8551. 16,
  8552. &camellia_info
  8553. };
  8554. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  8555. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  8556. static const mbedtls_cipher_info_t camellia_128_cfb128_info = {
  8557. MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
  8558. MBEDTLS_MODE_CFB,
  8559. 128,
  8560. "CAMELLIA-128-CFB128",
  8561. 16,
  8562. 0,
  8563. 16,
  8564. &camellia_info
  8565. };
  8566. static const mbedtls_cipher_info_t camellia_192_cfb128_info = {
  8567. MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
  8568. MBEDTLS_MODE_CFB,
  8569. 192,
  8570. "CAMELLIA-192-CFB128",
  8571. 16,
  8572. 0,
  8573. 16,
  8574. &camellia_info
  8575. };
  8576. static const mbedtls_cipher_info_t camellia_256_cfb128_info = {
  8577. MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
  8578. MBEDTLS_MODE_CFB,
  8579. 256,
  8580. "CAMELLIA-256-CFB128",
  8581. 16,
  8582. 0,
  8583. 16,
  8584. &camellia_info
  8585. };
  8586. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  8587. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  8588. static const mbedtls_cipher_info_t camellia_128_ctr_info = {
  8589. MBEDTLS_CIPHER_CAMELLIA_128_CTR,
  8590. MBEDTLS_MODE_CTR,
  8591. 128,
  8592. "CAMELLIA-128-CTR",
  8593. 16,
  8594. 0,
  8595. 16,
  8596. &camellia_info
  8597. };
  8598. static const mbedtls_cipher_info_t camellia_192_ctr_info = {
  8599. MBEDTLS_CIPHER_CAMELLIA_192_CTR,
  8600. MBEDTLS_MODE_CTR,
  8601. 192,
  8602. "CAMELLIA-192-CTR",
  8603. 16,
  8604. 0,
  8605. 16,
  8606. &camellia_info
  8607. };
  8608. static const mbedtls_cipher_info_t camellia_256_ctr_info = {
  8609. MBEDTLS_CIPHER_CAMELLIA_256_CTR,
  8610. MBEDTLS_MODE_CTR,
  8611. 256,
  8612. "CAMELLIA-256-CTR",
  8613. 16,
  8614. 0,
  8615. 16,
  8616. &camellia_info
  8617. };
  8618. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  8619. #if defined(MBEDTLS_GCM_C)
  8620. static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
  8621. unsigned int key_bitlen )
  8622. {
  8623. return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
  8624. key, key_bitlen );
  8625. }
  8626. static const mbedtls_cipher_base_t gcm_camellia_info = {
  8627. MBEDTLS_CIPHER_ID_CAMELLIA,
  8628. NULL,
  8629. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8630. NULL,
  8631. #endif
  8632. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  8633. NULL,
  8634. #endif
  8635. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  8636. NULL,
  8637. #endif
  8638. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  8639. NULL,
  8640. #endif
  8641. gcm_camellia_setkey_wrap,
  8642. gcm_camellia_setkey_wrap,
  8643. gcm_ctx_alloc,
  8644. gcm_ctx_free,
  8645. };
  8646. static const mbedtls_cipher_info_t camellia_128_gcm_info = {
  8647. MBEDTLS_CIPHER_CAMELLIA_128_GCM,
  8648. MBEDTLS_MODE_GCM,
  8649. 128,
  8650. "CAMELLIA-128-GCM",
  8651. 12,
  8652. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  8653. 16,
  8654. &gcm_camellia_info
  8655. };
  8656. static const mbedtls_cipher_info_t camellia_192_gcm_info = {
  8657. MBEDTLS_CIPHER_CAMELLIA_192_GCM,
  8658. MBEDTLS_MODE_GCM,
  8659. 192,
  8660. "CAMELLIA-192-GCM",
  8661. 12,
  8662. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  8663. 16,
  8664. &gcm_camellia_info
  8665. };
  8666. static const mbedtls_cipher_info_t camellia_256_gcm_info = {
  8667. MBEDTLS_CIPHER_CAMELLIA_256_GCM,
  8668. MBEDTLS_MODE_GCM,
  8669. 256,
  8670. "CAMELLIA-256-GCM",
  8671. 12,
  8672. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  8673. 16,
  8674. &gcm_camellia_info
  8675. };
  8676. #endif /* MBEDTLS_GCM_C */
  8677. #if defined(MBEDTLS_CCM_C)
  8678. static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
  8679. unsigned int key_bitlen )
  8680. {
  8681. return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
  8682. key, key_bitlen );
  8683. }
  8684. static const mbedtls_cipher_base_t ccm_camellia_info = {
  8685. MBEDTLS_CIPHER_ID_CAMELLIA,
  8686. NULL,
  8687. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8688. NULL,
  8689. #endif
  8690. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  8691. NULL,
  8692. #endif
  8693. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  8694. NULL,
  8695. #endif
  8696. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  8697. NULL,
  8698. #endif
  8699. ccm_camellia_setkey_wrap,
  8700. ccm_camellia_setkey_wrap,
  8701. ccm_ctx_alloc,
  8702. ccm_ctx_free,
  8703. };
  8704. static const mbedtls_cipher_info_t camellia_128_ccm_info = {
  8705. MBEDTLS_CIPHER_CAMELLIA_128_CCM,
  8706. MBEDTLS_MODE_CCM,
  8707. 128,
  8708. "CAMELLIA-128-CCM",
  8709. 12,
  8710. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  8711. 16,
  8712. &ccm_camellia_info
  8713. };
  8714. static const mbedtls_cipher_info_t camellia_192_ccm_info = {
  8715. MBEDTLS_CIPHER_CAMELLIA_192_CCM,
  8716. MBEDTLS_MODE_CCM,
  8717. 192,
  8718. "CAMELLIA-192-CCM",
  8719. 12,
  8720. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  8721. 16,
  8722. &ccm_camellia_info
  8723. };
  8724. static const mbedtls_cipher_info_t camellia_256_ccm_info = {
  8725. MBEDTLS_CIPHER_CAMELLIA_256_CCM,
  8726. MBEDTLS_MODE_CCM,
  8727. 256,
  8728. "CAMELLIA-256-CCM",
  8729. 12,
  8730. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  8731. 16,
  8732. &ccm_camellia_info
  8733. };
  8734. #endif /* MBEDTLS_CCM_C */
  8735. #endif /* MBEDTLS_CAMELLIA_C */
  8736. #if defined(MBEDTLS_DES_C)
  8737. static int des_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
  8738. const unsigned char *input, unsigned char *output )
  8739. {
  8740. ((void) operation);
  8741. return mbedtls_des_crypt_ecb( (mbedtls_des_context *) ctx, input, output );
  8742. }
  8743. static int des3_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
  8744. const unsigned char *input, unsigned char *output )
  8745. {
  8746. ((void) operation);
  8747. return mbedtls_des3_crypt_ecb( (mbedtls_des3_context *) ctx, input, output );
  8748. }
  8749. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8750. static int des_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
  8751. unsigned char *iv, const unsigned char *input, unsigned char *output )
  8752. {
  8753. return mbedtls_des_crypt_cbc( (mbedtls_des_context *) ctx, operation, length, iv, input,
  8754. output );
  8755. }
  8756. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  8757. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8758. static int des3_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
  8759. unsigned char *iv, const unsigned char *input, unsigned char *output )
  8760. {
  8761. return mbedtls_des3_crypt_cbc( (mbedtls_des3_context *) ctx, operation, length, iv, input,
  8762. output );
  8763. }
  8764. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  8765. static int des_setkey_dec_wrap( void *ctx, const unsigned char *key,
  8766. unsigned int key_bitlen )
  8767. {
  8768. ((void) key_bitlen);
  8769. return mbedtls_des_setkey_dec( (mbedtls_des_context *) ctx, key );
  8770. }
  8771. static int des_setkey_enc_wrap( void *ctx, const unsigned char *key,
  8772. unsigned int key_bitlen )
  8773. {
  8774. ((void) key_bitlen);
  8775. return mbedtls_des_setkey_enc( (mbedtls_des_context *) ctx, key );
  8776. }
  8777. static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key,
  8778. unsigned int key_bitlen )
  8779. {
  8780. ((void) key_bitlen);
  8781. return mbedtls_des3_set2key_dec( (mbedtls_des3_context *) ctx, key );
  8782. }
  8783. static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key,
  8784. unsigned int key_bitlen )
  8785. {
  8786. ((void) key_bitlen);
  8787. return mbedtls_des3_set2key_enc( (mbedtls_des3_context *) ctx, key );
  8788. }
  8789. static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key,
  8790. unsigned int key_bitlen )
  8791. {
  8792. ((void) key_bitlen);
  8793. return mbedtls_des3_set3key_dec( (mbedtls_des3_context *) ctx, key );
  8794. }
  8795. static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key,
  8796. unsigned int key_bitlen )
  8797. {
  8798. ((void) key_bitlen);
  8799. return mbedtls_des3_set3key_enc( (mbedtls_des3_context *) ctx, key );
  8800. }
  8801. static void * des_ctx_alloc( void )
  8802. {
  8803. mbedtls_des_context *des = mbedtls_calloc( 1, sizeof( mbedtls_des_context ) );
  8804. if( des == NULL )
  8805. return( NULL );
  8806. mbedtls_des_init( des );
  8807. return( des );
  8808. }
  8809. static void des_ctx_free( void *ctx )
  8810. {
  8811. mbedtls_des_free( (mbedtls_des_context *) ctx );
  8812. mbedtls_free( ctx );
  8813. }
  8814. static void * des3_ctx_alloc( void )
  8815. {
  8816. mbedtls_des3_context *des3;
  8817. des3 = mbedtls_calloc( 1, sizeof( mbedtls_des3_context ) );
  8818. if( des3 == NULL )
  8819. return( NULL );
  8820. mbedtls_des3_init( des3 );
  8821. return( des3 );
  8822. }
  8823. static void des3_ctx_free( void *ctx )
  8824. {
  8825. mbedtls_des3_free( (mbedtls_des3_context *) ctx );
  8826. mbedtls_free( ctx );
  8827. }
  8828. static const mbedtls_cipher_base_t des_info = {
  8829. MBEDTLS_CIPHER_ID_DES,
  8830. des_crypt_ecb_wrap,
  8831. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8832. des_crypt_cbc_wrap,
  8833. #endif
  8834. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  8835. NULL,
  8836. #endif
  8837. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  8838. NULL,
  8839. #endif
  8840. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  8841. NULL,
  8842. #endif
  8843. des_setkey_enc_wrap,
  8844. des_setkey_dec_wrap,
  8845. des_ctx_alloc,
  8846. des_ctx_free
  8847. };
  8848. static const mbedtls_cipher_info_t des_ecb_info = {
  8849. MBEDTLS_CIPHER_DES_ECB,
  8850. MBEDTLS_MODE_ECB,
  8851. MBEDTLS_KEY_LENGTH_DES,
  8852. "DES-ECB",
  8853. 8,
  8854. 0,
  8855. 8,
  8856. &des_info
  8857. };
  8858. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8859. static const mbedtls_cipher_info_t des_cbc_info = {
  8860. MBEDTLS_CIPHER_DES_CBC,
  8861. MBEDTLS_MODE_CBC,
  8862. MBEDTLS_KEY_LENGTH_DES,
  8863. "DES-CBC",
  8864. 8,
  8865. 0,
  8866. 8,
  8867. &des_info
  8868. };
  8869. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  8870. static const mbedtls_cipher_base_t des_ede_info = {
  8871. MBEDTLS_CIPHER_ID_DES,
  8872. des3_crypt_ecb_wrap,
  8873. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8874. des3_crypt_cbc_wrap,
  8875. #endif
  8876. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  8877. NULL,
  8878. #endif
  8879. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  8880. NULL,
  8881. #endif
  8882. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  8883. NULL,
  8884. #endif
  8885. des3_set2key_enc_wrap,
  8886. des3_set2key_dec_wrap,
  8887. des3_ctx_alloc,
  8888. des3_ctx_free
  8889. };
  8890. static const mbedtls_cipher_info_t des_ede_ecb_info = {
  8891. MBEDTLS_CIPHER_DES_EDE_ECB,
  8892. MBEDTLS_MODE_ECB,
  8893. MBEDTLS_KEY_LENGTH_DES_EDE,
  8894. "DES-EDE-ECB",
  8895. 8,
  8896. 0,
  8897. 8,
  8898. &des_ede_info
  8899. };
  8900. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8901. static const mbedtls_cipher_info_t des_ede_cbc_info = {
  8902. MBEDTLS_CIPHER_DES_EDE_CBC,
  8903. MBEDTLS_MODE_CBC,
  8904. MBEDTLS_KEY_LENGTH_DES_EDE,
  8905. "DES-EDE-CBC",
  8906. 8,
  8907. 0,
  8908. 8,
  8909. &des_ede_info
  8910. };
  8911. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  8912. static const mbedtls_cipher_base_t des_ede3_info = {
  8913. MBEDTLS_CIPHER_ID_3DES,
  8914. des3_crypt_ecb_wrap,
  8915. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8916. des3_crypt_cbc_wrap,
  8917. #endif
  8918. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  8919. NULL,
  8920. #endif
  8921. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  8922. NULL,
  8923. #endif
  8924. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  8925. NULL,
  8926. #endif
  8927. des3_set3key_enc_wrap,
  8928. des3_set3key_dec_wrap,
  8929. des3_ctx_alloc,
  8930. des3_ctx_free
  8931. };
  8932. static const mbedtls_cipher_info_t des_ede3_ecb_info = {
  8933. MBEDTLS_CIPHER_DES_EDE3_ECB,
  8934. MBEDTLS_MODE_ECB,
  8935. MBEDTLS_KEY_LENGTH_DES_EDE3,
  8936. "DES-EDE3-ECB",
  8937. 8,
  8938. 0,
  8939. 8,
  8940. &des_ede3_info
  8941. };
  8942. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8943. static const mbedtls_cipher_info_t des_ede3_cbc_info = {
  8944. MBEDTLS_CIPHER_DES_EDE3_CBC,
  8945. MBEDTLS_MODE_CBC,
  8946. MBEDTLS_KEY_LENGTH_DES_EDE3,
  8947. "DES-EDE3-CBC",
  8948. 8,
  8949. 0,
  8950. 8,
  8951. &des_ede3_info
  8952. };
  8953. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  8954. #endif /* MBEDTLS_DES_C */
  8955. #if defined(MBEDTLS_BLOWFISH_C)
  8956. static int blowfish_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
  8957. const unsigned char *input, unsigned char *output )
  8958. {
  8959. return mbedtls_blowfish_crypt_ecb( (mbedtls_blowfish_context *) ctx, operation, input,
  8960. output );
  8961. }
  8962. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  8963. static int blowfish_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
  8964. size_t length, unsigned char *iv, const unsigned char *input,
  8965. unsigned char *output )
  8966. {
  8967. return mbedtls_blowfish_crypt_cbc( (mbedtls_blowfish_context *) ctx, operation, length, iv,
  8968. input, output );
  8969. }
  8970. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  8971. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  8972. static int blowfish_crypt_cfb64_wrap( void *ctx, mbedtls_operation_t operation,
  8973. size_t length, size_t *iv_off, unsigned char *iv,
  8974. const unsigned char *input, unsigned char *output )
  8975. {
  8976. return mbedtls_blowfish_crypt_cfb64( (mbedtls_blowfish_context *) ctx, operation, length,
  8977. iv_off, iv, input, output );
  8978. }
  8979. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  8980. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  8981. static int blowfish_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
  8982. unsigned char *nonce_counter, unsigned char *stream_block,
  8983. const unsigned char *input, unsigned char *output )
  8984. {
  8985. return mbedtls_blowfish_crypt_ctr( (mbedtls_blowfish_context *) ctx, length, nc_off,
  8986. nonce_counter, stream_block, input, output );
  8987. }
  8988. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  8989. static int blowfish_setkey_wrap( void *ctx, const unsigned char *key,
  8990. unsigned int key_bitlen )
  8991. {
  8992. return mbedtls_blowfish_setkey( (mbedtls_blowfish_context *) ctx, key, key_bitlen );
  8993. }
  8994. static void * blowfish_ctx_alloc( void )
  8995. {
  8996. mbedtls_blowfish_context *ctx;
  8997. ctx = mbedtls_calloc( 1, sizeof( mbedtls_blowfish_context ) );
  8998. if( ctx == NULL )
  8999. return( NULL );
  9000. mbedtls_blowfish_init( ctx );
  9001. return( ctx );
  9002. }
  9003. static void blowfish_ctx_free( void *ctx )
  9004. {
  9005. mbedtls_blowfish_free( (mbedtls_blowfish_context *) ctx );
  9006. mbedtls_free( ctx );
  9007. }
  9008. static const mbedtls_cipher_base_t blowfish_info = {
  9009. MBEDTLS_CIPHER_ID_BLOWFISH,
  9010. blowfish_crypt_ecb_wrap,
  9011. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  9012. blowfish_crypt_cbc_wrap,
  9013. #endif
  9014. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  9015. blowfish_crypt_cfb64_wrap,
  9016. #endif
  9017. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  9018. blowfish_crypt_ctr_wrap,
  9019. #endif
  9020. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  9021. NULL,
  9022. #endif
  9023. blowfish_setkey_wrap,
  9024. blowfish_setkey_wrap,
  9025. blowfish_ctx_alloc,
  9026. blowfish_ctx_free
  9027. };
  9028. static const mbedtls_cipher_info_t blowfish_ecb_info = {
  9029. MBEDTLS_CIPHER_BLOWFISH_ECB,
  9030. MBEDTLS_MODE_ECB,
  9031. 128,
  9032. "BLOWFISH-ECB",
  9033. 8,
  9034. MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
  9035. 8,
  9036. &blowfish_info
  9037. };
  9038. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  9039. static const mbedtls_cipher_info_t blowfish_cbc_info = {
  9040. MBEDTLS_CIPHER_BLOWFISH_CBC,
  9041. MBEDTLS_MODE_CBC,
  9042. 128,
  9043. "BLOWFISH-CBC",
  9044. 8,
  9045. MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
  9046. 8,
  9047. &blowfish_info
  9048. };
  9049. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  9050. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  9051. static const mbedtls_cipher_info_t blowfish_cfb64_info = {
  9052. MBEDTLS_CIPHER_BLOWFISH_CFB64,
  9053. MBEDTLS_MODE_CFB,
  9054. 128,
  9055. "BLOWFISH-CFB64",
  9056. 8,
  9057. MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
  9058. 8,
  9059. &blowfish_info
  9060. };
  9061. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  9062. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  9063. static const mbedtls_cipher_info_t blowfish_ctr_info = {
  9064. MBEDTLS_CIPHER_BLOWFISH_CTR,
  9065. MBEDTLS_MODE_CTR,
  9066. 128,
  9067. "BLOWFISH-CTR",
  9068. 8,
  9069. MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
  9070. 8,
  9071. &blowfish_info
  9072. };
  9073. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  9074. #endif /* MBEDTLS_BLOWFISH_C */
  9075. #if defined(MBEDTLS_ARC4_C)
  9076. static int arc4_crypt_stream_wrap( void *ctx, size_t length,
  9077. const unsigned char *input,
  9078. unsigned char *output )
  9079. {
  9080. return( mbedtls_arc4_crypt( (mbedtls_arc4_context *) ctx, length, input, output ) );
  9081. }
  9082. static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
  9083. unsigned int key_bitlen )
  9084. {
  9085. /* we get key_bitlen in bits, arc4 expects it in bytes */
  9086. if( key_bitlen % 8 != 0 )
  9087. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  9088. mbedtls_arc4_setup( (mbedtls_arc4_context *) ctx, key, key_bitlen / 8 );
  9089. return( 0 );
  9090. }
  9091. static void * arc4_ctx_alloc( void )
  9092. {
  9093. mbedtls_arc4_context *ctx;
  9094. ctx = mbedtls_calloc( 1, sizeof( mbedtls_arc4_context ) );
  9095. if( ctx == NULL )
  9096. return( NULL );
  9097. mbedtls_arc4_init( ctx );
  9098. return( ctx );
  9099. }
  9100. static void arc4_ctx_free( void *ctx )
  9101. {
  9102. mbedtls_arc4_free( (mbedtls_arc4_context *) ctx );
  9103. mbedtls_free( ctx );
  9104. }
  9105. static const mbedtls_cipher_base_t arc4_base_info = {
  9106. MBEDTLS_CIPHER_ID_ARC4,
  9107. NULL,
  9108. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  9109. NULL,
  9110. #endif
  9111. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  9112. NULL,
  9113. #endif
  9114. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  9115. NULL,
  9116. #endif
  9117. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  9118. arc4_crypt_stream_wrap,
  9119. #endif
  9120. arc4_setkey_wrap,
  9121. arc4_setkey_wrap,
  9122. arc4_ctx_alloc,
  9123. arc4_ctx_free
  9124. };
  9125. static const mbedtls_cipher_info_t arc4_128_info = {
  9126. MBEDTLS_CIPHER_ARC4_128,
  9127. MBEDTLS_MODE_STREAM,
  9128. 128,
  9129. "ARC4-128",
  9130. 0,
  9131. 0,
  9132. 1,
  9133. &arc4_base_info
  9134. };
  9135. #endif /* MBEDTLS_ARC4_C */
  9136. #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
  9137. static int null_crypt_stream( void *ctx, size_t length,
  9138. const unsigned char *input,
  9139. unsigned char *output )
  9140. {
  9141. ((void) ctx);
  9142. memmove( output, input, length );
  9143. return( 0 );
  9144. }
  9145. static int null_setkey( void *ctx, const unsigned char *key,
  9146. unsigned int key_bitlen )
  9147. {
  9148. ((void) ctx);
  9149. ((void) key);
  9150. ((void) key_bitlen);
  9151. return( 0 );
  9152. }
  9153. static void * null_ctx_alloc( void )
  9154. {
  9155. return( (void *) 1 );
  9156. }
  9157. static void null_ctx_free( void *ctx )
  9158. {
  9159. ((void) ctx);
  9160. }
  9161. static const mbedtls_cipher_base_t null_base_info = {
  9162. MBEDTLS_CIPHER_ID_NULL,
  9163. NULL,
  9164. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  9165. NULL,
  9166. #endif
  9167. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  9168. NULL,
  9169. #endif
  9170. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  9171. NULL,
  9172. #endif
  9173. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  9174. null_crypt_stream,
  9175. #endif
  9176. null_setkey,
  9177. null_setkey,
  9178. null_ctx_alloc,
  9179. null_ctx_free
  9180. };
  9181. static const mbedtls_cipher_info_t null_cipher_info = {
  9182. MBEDTLS_CIPHER_NULL,
  9183. MBEDTLS_MODE_STREAM,
  9184. 0,
  9185. "NULL",
  9186. 0,
  9187. 0,
  9188. 1,
  9189. &null_base_info
  9190. };
  9191. #endif /* defined(MBEDTLS_CIPHER_NULL_CIPHER) */
  9192. const mbedtls_cipher_definition_t mbedtls_cipher_definitions[] =
  9193. {
  9194. #if defined(MBEDTLS_AES_C)
  9195. { MBEDTLS_CIPHER_AES_128_ECB, &aes_128_ecb_info },
  9196. { MBEDTLS_CIPHER_AES_192_ECB, &aes_192_ecb_info },
  9197. { MBEDTLS_CIPHER_AES_256_ECB, &aes_256_ecb_info },
  9198. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  9199. { MBEDTLS_CIPHER_AES_128_CBC, &aes_128_cbc_info },
  9200. { MBEDTLS_CIPHER_AES_192_CBC, &aes_192_cbc_info },
  9201. { MBEDTLS_CIPHER_AES_256_CBC, &aes_256_cbc_info },
  9202. #endif
  9203. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  9204. { MBEDTLS_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
  9205. { MBEDTLS_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
  9206. { MBEDTLS_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
  9207. #endif
  9208. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  9209. { MBEDTLS_CIPHER_AES_128_CTR, &aes_128_ctr_info },
  9210. { MBEDTLS_CIPHER_AES_192_CTR, &aes_192_ctr_info },
  9211. { MBEDTLS_CIPHER_AES_256_CTR, &aes_256_ctr_info },
  9212. #endif
  9213. #if defined(MBEDTLS_GCM_C)
  9214. { MBEDTLS_CIPHER_AES_128_GCM, &aes_128_gcm_info },
  9215. { MBEDTLS_CIPHER_AES_192_GCM, &aes_192_gcm_info },
  9216. { MBEDTLS_CIPHER_AES_256_GCM, &aes_256_gcm_info },
  9217. #endif
  9218. #if defined(MBEDTLS_CCM_C)
  9219. { MBEDTLS_CIPHER_AES_128_CCM, &aes_128_ccm_info },
  9220. { MBEDTLS_CIPHER_AES_192_CCM, &aes_192_ccm_info },
  9221. { MBEDTLS_CIPHER_AES_256_CCM, &aes_256_ccm_info },
  9222. #endif
  9223. #endif /* MBEDTLS_AES_C */
  9224. #if defined(MBEDTLS_ARC4_C)
  9225. { MBEDTLS_CIPHER_ARC4_128, &arc4_128_info },
  9226. #endif
  9227. #if defined(MBEDTLS_BLOWFISH_C)
  9228. { MBEDTLS_CIPHER_BLOWFISH_ECB, &blowfish_ecb_info },
  9229. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  9230. { MBEDTLS_CIPHER_BLOWFISH_CBC, &blowfish_cbc_info },
  9231. #endif
  9232. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  9233. { MBEDTLS_CIPHER_BLOWFISH_CFB64, &blowfish_cfb64_info },
  9234. #endif
  9235. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  9236. { MBEDTLS_CIPHER_BLOWFISH_CTR, &blowfish_ctr_info },
  9237. #endif
  9238. #endif /* MBEDTLS_BLOWFISH_C */
  9239. #if defined(MBEDTLS_CAMELLIA_C)
  9240. { MBEDTLS_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
  9241. { MBEDTLS_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
  9242. { MBEDTLS_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
  9243. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  9244. { MBEDTLS_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
  9245. { MBEDTLS_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
  9246. { MBEDTLS_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
  9247. #endif
  9248. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  9249. { MBEDTLS_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
  9250. { MBEDTLS_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
  9251. { MBEDTLS_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
  9252. #endif
  9253. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  9254. { MBEDTLS_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
  9255. { MBEDTLS_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
  9256. { MBEDTLS_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
  9257. #endif
  9258. #if defined(MBEDTLS_GCM_C)
  9259. { MBEDTLS_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
  9260. { MBEDTLS_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
  9261. { MBEDTLS_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
  9262. #endif
  9263. #if defined(MBEDTLS_CCM_C)
  9264. { MBEDTLS_CIPHER_CAMELLIA_128_CCM, &camellia_128_ccm_info },
  9265. { MBEDTLS_CIPHER_CAMELLIA_192_CCM, &camellia_192_ccm_info },
  9266. { MBEDTLS_CIPHER_CAMELLIA_256_CCM, &camellia_256_ccm_info },
  9267. #endif
  9268. #endif /* MBEDTLS_CAMELLIA_C */
  9269. #if defined(MBEDTLS_DES_C)
  9270. { MBEDTLS_CIPHER_DES_ECB, &des_ecb_info },
  9271. { MBEDTLS_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
  9272. { MBEDTLS_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
  9273. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  9274. { MBEDTLS_CIPHER_DES_CBC, &des_cbc_info },
  9275. { MBEDTLS_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
  9276. { MBEDTLS_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
  9277. #endif
  9278. #endif /* MBEDTLS_DES_C */
  9279. #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
  9280. { MBEDTLS_CIPHER_NULL, &null_cipher_info },
  9281. #endif /* MBEDTLS_CIPHER_NULL_CIPHER */
  9282. { MBEDTLS_CIPHER_NONE, NULL }
  9283. };
  9284. #define NUM_CIPHERS sizeof mbedtls_cipher_definitions / sizeof mbedtls_cipher_definitions[0]
  9285. int mbedtls_cipher_supported[NUM_CIPHERS];
  9286. #endif /* MBEDTLS_CIPHER_C */
  9287. /*
  9288. Amalgamated build undefines
  9289. */
  9290. #undef ADD
  9291. #undef BC
  9292. #undef BEFORE_COLON
  9293. #undef F
  9294. #undef F0
  9295. #undef F1
  9296. #undef F2
  9297. #undef F3
  9298. #undef F4
  9299. #undef F5
  9300. #undef FSb
  9301. #undef K
  9302. #undef KK
  9303. #undef P
  9304. #undef R
  9305. #undef ROTR
  9306. #undef S
  9307. #undef S0
  9308. #undef S1
  9309. #undef S2
  9310. #undef S3
  9311. #undef SAFE_SNPRINTF
  9312. #undef SHR
  9313. #undef close
  9314. #undef read
  9315. #undef supported_init
  9316. #undef write
  9317. /********* Start of file library/cmac.c ************/
  9318. /**
  9319. * \file cmac.c
  9320. *
  9321. * \brief NIST SP800-38B compliant CMAC implementation for AES and 3DES
  9322. *
  9323. * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
  9324. * SPDX-License-Identifier: Apache-2.0
  9325. *
  9326. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9327. * not use this file except in compliance with the License.
  9328. * You may obtain a copy of the License at
  9329. *
  9330. * http://www.apache.org/licenses/LICENSE-2.0
  9331. *
  9332. * Unless required by applicable law or agreed to in writing, software
  9333. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9334. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9335. * See the License for the specific language governing permissions and
  9336. * limitations under the License.
  9337. *
  9338. * This file is part of mbed TLS (https://tls.mbed.org)
  9339. */
  9340. /*
  9341. * References:
  9342. *
  9343. * - NIST SP 800-38B Recommendation for Block Cipher Modes of Operation: The
  9344. * CMAC Mode for Authentication
  9345. * http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38b.pdf
  9346. *
  9347. * - RFC 4493 - The AES-CMAC Algorithm
  9348. * https://tools.ietf.org/html/rfc4493
  9349. *
  9350. * - RFC 4615 - The Advanced Encryption Standard-Cipher-based Message
  9351. * Authentication Code-Pseudo-Random Function-128 (AES-CMAC-PRF-128)
  9352. * Algorithm for the Internet Key Exchange Protocol (IKE)
  9353. * https://tools.ietf.org/html/rfc4615
  9354. *
  9355. * Additional test vectors: ISO/IEC 9797-1
  9356. *
  9357. */
  9358. #if !defined(MBEDTLS_CONFIG_FILE)
  9359. #else
  9360. #endif
  9361. #if defined(MBEDTLS_CMAC_C)
  9362. #include <string.h>
  9363. #if defined(MBEDTLS_PLATFORM_C)
  9364. #else
  9365. #include <stdlib.h>
  9366. #define mbedtls_calloc calloc
  9367. #define mbedtls_free free
  9368. #if defined(MBEDTLS_SELF_TEST)
  9369. #include <stdio.h>
  9370. #define mbedtls_printf printf
  9371. #endif /* MBEDTLS_SELF_TEST */
  9372. #endif /* MBEDTLS_PLATFORM_C */
  9373. #if !defined(MBEDTLS_CMAC_ALT) || defined(MBEDTLS_SELF_TEST)
  9374. /* Implementation that should never be optimized out by the compiler */
  9375. /* zeroize was here */
  9376. /*
  9377. * Multiplication by u in the Galois field of GF(2^n)
  9378. *
  9379. * As explained in NIST SP 800-38B, this can be computed:
  9380. *
  9381. * If MSB(p) = 0, then p = (p << 1)
  9382. * If MSB(p) = 1, then p = (p << 1) ^ R_n
  9383. * with R_64 = 0x1B and R_128 = 0x87
  9384. *
  9385. * Input and output MUST NOT point to the same buffer
  9386. * Block size must be 8 bytes or 16 bytes - the block sizes for DES and AES.
  9387. */
  9388. static int cmac_multiply_by_u( unsigned char *output,
  9389. const unsigned char *input,
  9390. size_t blocksize )
  9391. {
  9392. const unsigned char R_128 = 0x87;
  9393. const unsigned char R_64 = 0x1B;
  9394. unsigned char R_n, mask;
  9395. unsigned char overflow = 0x00;
  9396. int i;
  9397. if( blocksize == MBEDTLS_AES_BLOCK_SIZE )
  9398. {
  9399. R_n = R_128;
  9400. }
  9401. else if( blocksize == MBEDTLS_DES3_BLOCK_SIZE )
  9402. {
  9403. R_n = R_64;
  9404. }
  9405. else
  9406. {
  9407. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  9408. }
  9409. for( i = (int)blocksize - 1; i >= 0; i-- )
  9410. {
  9411. output[i] = input[i] << 1 | overflow;
  9412. overflow = input[i] >> 7;
  9413. }
  9414. /* mask = ( input[0] >> 7 ) ? 0xff : 0x00
  9415. * using bit operations to avoid branches */
  9416. /* MSVC has a warning about unary minus on unsigned, but this is
  9417. * well-defined and precisely what we want to do here */
  9418. #if defined(_MSC_VER)
  9419. #pragma warning( push )
  9420. #pragma warning( disable : 4146 )
  9421. #endif
  9422. mask = - ( input[0] >> 7 );
  9423. #if defined(_MSC_VER)
  9424. #pragma warning( pop )
  9425. #endif
  9426. output[ blocksize - 1 ] ^= R_n & mask;
  9427. return( 0 );
  9428. }
  9429. /*
  9430. * Generate subkeys
  9431. *
  9432. * - as specified by RFC 4493, section 2.3 Subkey Generation Algorithm
  9433. */
  9434. static int cmac_generate_subkeys( mbedtls_cipher_context_t *ctx,
  9435. unsigned char* K1, unsigned char* K2 )
  9436. {
  9437. int ret;
  9438. unsigned char L[MBEDTLS_CIPHER_BLKSIZE_MAX];
  9439. size_t olen, block_size;
  9440. mbedtls_zeroize( L, sizeof( L ) );
  9441. block_size = ctx->cipher_info->block_size;
  9442. /* Calculate Ek(0) */
  9443. if( ( ret = mbedtls_cipher_update( ctx, L, block_size, L, &olen ) ) != 0 )
  9444. goto exit;
  9445. /*
  9446. * Generate K1 and K2
  9447. */
  9448. if( ( ret = cmac_multiply_by_u( K1, L , block_size ) ) != 0 )
  9449. goto exit;
  9450. if( ( ret = cmac_multiply_by_u( K2, K1 , block_size ) ) != 0 )
  9451. goto exit;
  9452. exit:
  9453. mbedtls_zeroize( L, sizeof( L ) );
  9454. return( ret );
  9455. }
  9456. #endif /* !defined(MBEDTLS_CMAC_ALT) || defined(MBEDTLS_SELF_TEST) */
  9457. #if !defined(MBEDTLS_CMAC_ALT)
  9458. static void cmac_xor_block( unsigned char *output, const unsigned char *input1,
  9459. const unsigned char *input2,
  9460. const size_t block_size )
  9461. {
  9462. size_t idx;
  9463. for( idx = 0; idx < block_size; idx++ )
  9464. output[ idx ] = input1[ idx ] ^ input2[ idx ];
  9465. }
  9466. /*
  9467. * Create padded last block from (partial) last block.
  9468. *
  9469. * We can't use the padding option from the cipher layer, as it only works for
  9470. * CBC and we use ECB mode, and anyway we need to XOR K1 or K2 in addition.
  9471. */
  9472. static void cmac_pad( unsigned char padded_block[MBEDTLS_CIPHER_BLKSIZE_MAX],
  9473. size_t padded_block_len,
  9474. const unsigned char *last_block,
  9475. size_t last_block_len )
  9476. {
  9477. size_t j;
  9478. for( j = 0; j < padded_block_len; j++ )
  9479. {
  9480. if( j < last_block_len )
  9481. padded_block[j] = last_block[j];
  9482. else if( j == last_block_len )
  9483. padded_block[j] = 0x80;
  9484. else
  9485. padded_block[j] = 0x00;
  9486. }
  9487. }
  9488. int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
  9489. const unsigned char *key, size_t keybits )
  9490. {
  9491. mbedtls_cipher_type_t type;
  9492. mbedtls_cmac_context_t *cmac_ctx;
  9493. int retval;
  9494. if( ctx == NULL || ctx->cipher_info == NULL || key == NULL )
  9495. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  9496. if( ( retval = mbedtls_cipher_setkey( ctx, key, (int)keybits,
  9497. MBEDTLS_ENCRYPT ) ) != 0 )
  9498. return( retval );
  9499. type = ctx->cipher_info->type;
  9500. switch( type )
  9501. {
  9502. case MBEDTLS_CIPHER_AES_128_ECB:
  9503. case MBEDTLS_CIPHER_AES_192_ECB:
  9504. case MBEDTLS_CIPHER_AES_256_ECB:
  9505. case MBEDTLS_CIPHER_DES_EDE3_ECB:
  9506. break;
  9507. default:
  9508. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  9509. }
  9510. /* Allocated and initialise in the cipher context memory for the CMAC
  9511. * context */
  9512. cmac_ctx = mbedtls_calloc( 1, sizeof( mbedtls_cmac_context_t ) );
  9513. if( cmac_ctx == NULL )
  9514. return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
  9515. ctx->cmac_ctx = cmac_ctx;
  9516. mbedtls_zeroize( cmac_ctx->state, sizeof( cmac_ctx->state ) );
  9517. return 0;
  9518. }
  9519. int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
  9520. const unsigned char *input, size_t ilen )
  9521. {
  9522. mbedtls_cmac_context_t* cmac_ctx;
  9523. unsigned char *state;
  9524. int ret = 0;
  9525. size_t n, j, olen, block_size;
  9526. if( ctx == NULL || ctx->cipher_info == NULL || input == NULL ||
  9527. ctx->cmac_ctx == NULL )
  9528. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  9529. cmac_ctx = ctx->cmac_ctx;
  9530. block_size = ctx->cipher_info->block_size;
  9531. state = ctx->cmac_ctx->state;
  9532. /* Is there data still to process from the last call, that's greater in
  9533. * size than a block? */
  9534. if( cmac_ctx->unprocessed_len > 0 &&
  9535. ilen > block_size - cmac_ctx->unprocessed_len )
  9536. {
  9537. memcpy( &cmac_ctx->unprocessed_block[cmac_ctx->unprocessed_len],
  9538. input,
  9539. block_size - cmac_ctx->unprocessed_len );
  9540. cmac_xor_block( state, cmac_ctx->unprocessed_block, state, block_size );
  9541. if( ( ret = mbedtls_cipher_update( ctx, state, block_size, state,
  9542. &olen ) ) != 0 )
  9543. {
  9544. goto exit;
  9545. }
  9546. input += block_size - cmac_ctx->unprocessed_len;
  9547. ilen -= block_size - cmac_ctx->unprocessed_len;
  9548. cmac_ctx->unprocessed_len = 0;
  9549. }
  9550. /* n is the number of blocks including any final partial block */
  9551. n = ( ilen + block_size - 1 ) / block_size;
  9552. /* Iterate across the input data in block sized chunks, excluding any
  9553. * final partial or complete block */
  9554. for( j = 1; j < n; j++ )
  9555. {
  9556. cmac_xor_block( state, input, state, block_size );
  9557. if( ( ret = mbedtls_cipher_update( ctx, state, block_size, state,
  9558. &olen ) ) != 0 )
  9559. goto exit;
  9560. ilen -= block_size;
  9561. input += block_size;
  9562. }
  9563. /* If there is data left over that wasn't aligned to a block */
  9564. if( ilen > 0 )
  9565. {
  9566. memcpy( &cmac_ctx->unprocessed_block[cmac_ctx->unprocessed_len],
  9567. input,
  9568. ilen );
  9569. cmac_ctx->unprocessed_len += ilen;
  9570. }
  9571. exit:
  9572. return( ret );
  9573. }
  9574. int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
  9575. unsigned char *output )
  9576. {
  9577. mbedtls_cmac_context_t* cmac_ctx;
  9578. unsigned char *state, *last_block;
  9579. unsigned char K1[MBEDTLS_CIPHER_BLKSIZE_MAX];
  9580. unsigned char K2[MBEDTLS_CIPHER_BLKSIZE_MAX];
  9581. unsigned char M_last[MBEDTLS_CIPHER_BLKSIZE_MAX];
  9582. int ret;
  9583. size_t olen, block_size;
  9584. if( ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL ||
  9585. output == NULL )
  9586. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  9587. cmac_ctx = ctx->cmac_ctx;
  9588. block_size = ctx->cipher_info->block_size;
  9589. state = cmac_ctx->state;
  9590. mbedtls_zeroize( K1, sizeof( K1 ) );
  9591. mbedtls_zeroize( K2, sizeof( K2 ) );
  9592. cmac_generate_subkeys( ctx, K1, K2 );
  9593. last_block = cmac_ctx->unprocessed_block;
  9594. /* Calculate last block */
  9595. if( cmac_ctx->unprocessed_len < block_size )
  9596. {
  9597. cmac_pad( M_last, block_size, last_block, cmac_ctx->unprocessed_len );
  9598. cmac_xor_block( M_last, M_last, K2, block_size );
  9599. }
  9600. else
  9601. {
  9602. /* Last block is complete block */
  9603. cmac_xor_block( M_last, last_block, K1, block_size );
  9604. }
  9605. cmac_xor_block( state, M_last, state, block_size );
  9606. if( ( ret = mbedtls_cipher_update( ctx, state, block_size, state,
  9607. &olen ) ) != 0 )
  9608. {
  9609. goto exit;
  9610. }
  9611. memcpy( output, state, block_size );
  9612. exit:
  9613. /* Wipe the generated keys on the stack, and any other transients to avoid
  9614. * side channel leakage */
  9615. mbedtls_zeroize( K1, sizeof( K1 ) );
  9616. mbedtls_zeroize( K2, sizeof( K2 ) );
  9617. cmac_ctx->unprocessed_len = 0;
  9618. mbedtls_zeroize( cmac_ctx->unprocessed_block,
  9619. sizeof( cmac_ctx->unprocessed_block ) );
  9620. mbedtls_zeroize( state, MBEDTLS_CIPHER_BLKSIZE_MAX );
  9621. return( ret );
  9622. }
  9623. int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx )
  9624. {
  9625. mbedtls_cmac_context_t* cmac_ctx;
  9626. if( ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL )
  9627. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  9628. cmac_ctx = ctx->cmac_ctx;
  9629. /* Reset the internal state */
  9630. cmac_ctx->unprocessed_len = 0;
  9631. mbedtls_zeroize( cmac_ctx->unprocessed_block,
  9632. sizeof( cmac_ctx->unprocessed_block ) );
  9633. mbedtls_zeroize( cmac_ctx->state,
  9634. sizeof( cmac_ctx->state ) );
  9635. return( 0 );
  9636. }
  9637. int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
  9638. const unsigned char *key, size_t keylen,
  9639. const unsigned char *input, size_t ilen,
  9640. unsigned char *output )
  9641. {
  9642. mbedtls_cipher_context_t ctx;
  9643. int ret;
  9644. if( cipher_info == NULL || key == NULL || input == NULL || output == NULL )
  9645. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  9646. mbedtls_cipher_init( &ctx );
  9647. if( ( ret = mbedtls_cipher_setup( &ctx, cipher_info ) ) != 0 )
  9648. goto exit;
  9649. ret = mbedtls_cipher_cmac_starts( &ctx, key, keylen );
  9650. if( ret != 0 )
  9651. goto exit;
  9652. ret = mbedtls_cipher_cmac_update( &ctx, input, ilen );
  9653. if( ret != 0 )
  9654. goto exit;
  9655. ret = mbedtls_cipher_cmac_finish( &ctx, output );
  9656. exit:
  9657. mbedtls_cipher_free( &ctx );
  9658. return( ret );
  9659. }
  9660. #if defined(MBEDTLS_AES_C)
  9661. /*
  9662. * Implementation of AES-CMAC-PRF-128 defined in RFC 4615
  9663. */
  9664. int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_length,
  9665. const unsigned char *input, size_t in_len,
  9666. unsigned char *output )
  9667. {
  9668. int ret;
  9669. const mbedtls_cipher_info_t *cipher_info;
  9670. unsigned char zero_key[MBEDTLS_AES_BLOCK_SIZE];
  9671. unsigned char int_key[MBEDTLS_AES_BLOCK_SIZE];
  9672. if( key == NULL || input == NULL || output == NULL )
  9673. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  9674. cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
  9675. if( cipher_info == NULL )
  9676. {
  9677. /* Failing at this point must be due to a build issue */
  9678. ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  9679. goto exit;
  9680. }
  9681. if( key_length == MBEDTLS_AES_BLOCK_SIZE )
  9682. {
  9683. /* Use key as is */
  9684. memcpy( int_key, key, MBEDTLS_AES_BLOCK_SIZE );
  9685. }
  9686. else
  9687. {
  9688. memset( zero_key, 0, MBEDTLS_AES_BLOCK_SIZE );
  9689. ret = mbedtls_cipher_cmac( cipher_info, zero_key, 128, key,
  9690. key_length, int_key );
  9691. if( ret != 0 )
  9692. goto exit;
  9693. }
  9694. ret = mbedtls_cipher_cmac( cipher_info, int_key, 128, input, in_len,
  9695. output );
  9696. exit:
  9697. mbedtls_zeroize( int_key, sizeof( int_key ) );
  9698. return( ret );
  9699. }
  9700. #endif /* MBEDTLS_AES_C */
  9701. #endif /* !MBEDTLS_CMAC_ALT */
  9702. #if defined(MBEDTLS_SELF_TEST)
  9703. /*
  9704. * CMAC test data for SP800-38B
  9705. * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/AES_CMAC.pdf
  9706. * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/TDES_CMAC.pdf
  9707. *
  9708. * AES-CMAC-PRF-128 test data from RFC 4615
  9709. * https://tools.ietf.org/html/rfc4615#page-4
  9710. */
  9711. #define NB_CMAC_TESTS_PER_KEY 4
  9712. #define NB_PRF_TESTS 3
  9713. #if defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C)
  9714. /* All CMAC test inputs are truncated from the same 64 byte buffer. */
  9715. static const unsigned char test_message[] = {
  9716. /* PT */
  9717. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  9718. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  9719. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  9720. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  9721. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  9722. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  9723. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  9724. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  9725. };
  9726. #endif /* MBEDTLS_AES_C || MBEDTLS_DES_C */
  9727. #if defined(MBEDTLS_AES_C)
  9728. /* Truncation point of message for AES CMAC tests */
  9729. static const unsigned int aes_message_lengths[NB_CMAC_TESTS_PER_KEY] = {
  9730. /* Mlen */
  9731. 0,
  9732. 16,
  9733. 20,
  9734. 64
  9735. };
  9736. /* CMAC-AES128 Test Data */
  9737. static const unsigned char aes_128_key[16] = {
  9738. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  9739. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  9740. };
  9741. static const unsigned char aes_128_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = {
  9742. {
  9743. /* K1 */
  9744. 0xfb, 0xee, 0xd6, 0x18, 0x35, 0x71, 0x33, 0x66,
  9745. 0x7c, 0x85, 0xe0, 0x8f, 0x72, 0x36, 0xa8, 0xde
  9746. },
  9747. {
  9748. /* K2 */
  9749. 0xf7, 0xdd, 0xac, 0x30, 0x6a, 0xe2, 0x66, 0xcc,
  9750. 0xf9, 0x0b, 0xc1, 0x1e, 0xe4, 0x6d, 0x51, 0x3b
  9751. }
  9752. };
  9753. static const unsigned char aes_128_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] = {
  9754. {
  9755. /* Example #1 */
  9756. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  9757. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  9758. },
  9759. {
  9760. /* Example #2 */
  9761. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  9762. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  9763. },
  9764. {
  9765. /* Example #3 */
  9766. 0x7d, 0x85, 0x44, 0x9e, 0xa6, 0xea, 0x19, 0xc8,
  9767. 0x23, 0xa7, 0xbf, 0x78, 0x83, 0x7d, 0xfa, 0xde
  9768. },
  9769. {
  9770. /* Example #4 */
  9771. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  9772. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  9773. }
  9774. };
  9775. /* CMAC-AES192 Test Data */
  9776. static const unsigned char aes_192_key[24] = {
  9777. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  9778. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  9779. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  9780. };
  9781. static const unsigned char aes_192_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = {
  9782. {
  9783. /* K1 */
  9784. 0x44, 0x8a, 0x5b, 0x1c, 0x93, 0x51, 0x4b, 0x27,
  9785. 0x3e, 0xe6, 0x43, 0x9d, 0xd4, 0xda, 0xa2, 0x96
  9786. },
  9787. {
  9788. /* K2 */
  9789. 0x89, 0x14, 0xb6, 0x39, 0x26, 0xa2, 0x96, 0x4e,
  9790. 0x7d, 0xcc, 0x87, 0x3b, 0xa9, 0xb5, 0x45, 0x2c
  9791. }
  9792. };
  9793. static const unsigned char aes_192_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] = {
  9794. {
  9795. /* Example #1 */
  9796. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  9797. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  9798. },
  9799. {
  9800. /* Example #2 */
  9801. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  9802. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  9803. },
  9804. {
  9805. /* Example #3 */
  9806. 0x3d, 0x75, 0xc1, 0x94, 0xed, 0x96, 0x07, 0x04,
  9807. 0x44, 0xa9, 0xfa, 0x7e, 0xc7, 0x40, 0xec, 0xf8
  9808. },
  9809. {
  9810. /* Example #4 */
  9811. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  9812. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  9813. }
  9814. };
  9815. /* CMAC-AES256 Test Data */
  9816. static const unsigned char aes_256_key[32] = {
  9817. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  9818. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  9819. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  9820. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  9821. };
  9822. static const unsigned char aes_256_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = {
  9823. {
  9824. /* K1 */
  9825. 0xca, 0xd1, 0xed, 0x03, 0x29, 0x9e, 0xed, 0xac,
  9826. 0x2e, 0x9a, 0x99, 0x80, 0x86, 0x21, 0x50, 0x2f
  9827. },
  9828. {
  9829. /* K2 */
  9830. 0x95, 0xa3, 0xda, 0x06, 0x53, 0x3d, 0xdb, 0x58,
  9831. 0x5d, 0x35, 0x33, 0x01, 0x0c, 0x42, 0xa0, 0xd9
  9832. }
  9833. };
  9834. static const unsigned char aes_256_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] = {
  9835. {
  9836. /* Example #1 */
  9837. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  9838. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  9839. },
  9840. {
  9841. /* Example #2 */
  9842. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  9843. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  9844. },
  9845. {
  9846. /* Example #3 */
  9847. 0x15, 0x67, 0x27, 0xdc, 0x08, 0x78, 0x94, 0x4a,
  9848. 0x02, 0x3c, 0x1f, 0xe0, 0x3b, 0xad, 0x6d, 0x93
  9849. },
  9850. {
  9851. /* Example #4 */
  9852. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  9853. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  9854. }
  9855. };
  9856. #endif /* MBEDTLS_AES_C */
  9857. #if defined(MBEDTLS_DES_C)
  9858. /* Truncation point of message for 3DES CMAC tests */
  9859. static const unsigned int des3_message_lengths[NB_CMAC_TESTS_PER_KEY] = {
  9860. 0,
  9861. 16,
  9862. 20,
  9863. 32
  9864. };
  9865. /* CMAC-TDES (Generation) - 2 Key Test Data */
  9866. static const unsigned char des3_2key_key[24] = {
  9867. /* Key1 */
  9868. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  9869. /* Key2 */
  9870. 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xEF, 0x01,
  9871. /* Key3 */
  9872. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef
  9873. };
  9874. static const unsigned char des3_2key_subkeys[2][8] = {
  9875. {
  9876. /* K1 */
  9877. 0x0d, 0xd2, 0xcb, 0x7a, 0x3d, 0x88, 0x88, 0xd9
  9878. },
  9879. {
  9880. /* K2 */
  9881. 0x1b, 0xa5, 0x96, 0xf4, 0x7b, 0x11, 0x11, 0xb2
  9882. }
  9883. };
  9884. static const unsigned char des3_2key_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_DES3_BLOCK_SIZE] = {
  9885. {
  9886. /* Sample #1 */
  9887. 0x79, 0xce, 0x52, 0xa7, 0xf7, 0x86, 0xa9, 0x60
  9888. },
  9889. {
  9890. /* Sample #2 */
  9891. 0xcc, 0x18, 0xa0, 0xb7, 0x9a, 0xf2, 0x41, 0x3b
  9892. },
  9893. {
  9894. /* Sample #3 */
  9895. 0xc0, 0x6d, 0x37, 0x7e, 0xcd, 0x10, 0x19, 0x69
  9896. },
  9897. {
  9898. /* Sample #4 */
  9899. 0x9c, 0xd3, 0x35, 0x80, 0xf9, 0xb6, 0x4d, 0xfb
  9900. }
  9901. };
  9902. /* CMAC-TDES (Generation) - 3 Key Test Data */
  9903. static const unsigned char des3_3key_key[24] = {
  9904. /* Key1 */
  9905. 0x01, 0x23, 0x45, 0x67, 0x89, 0xaa, 0xcd, 0xef,
  9906. /* Key2 */
  9907. 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01,
  9908. /* Key3 */
  9909. 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23
  9910. };
  9911. static const unsigned char des3_3key_subkeys[2][8] = {
  9912. {
  9913. /* K1 */
  9914. 0x9d, 0x74, 0xe7, 0x39, 0x33, 0x17, 0x96, 0xc0
  9915. },
  9916. {
  9917. /* K2 */
  9918. 0x3a, 0xe9, 0xce, 0x72, 0x66, 0x2f, 0x2d, 0x9b
  9919. }
  9920. };
  9921. static const unsigned char des3_3key_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_DES3_BLOCK_SIZE] = {
  9922. {
  9923. /* Sample #1 */
  9924. 0x7d, 0xb0, 0xd3, 0x7d, 0xf9, 0x36, 0xc5, 0x50
  9925. },
  9926. {
  9927. /* Sample #2 */
  9928. 0x30, 0x23, 0x9c, 0xf1, 0xf5, 0x2e, 0x66, 0x09
  9929. },
  9930. {
  9931. /* Sample #3 */
  9932. 0x6c, 0x9f, 0x3e, 0xe4, 0x92, 0x3f, 0x6b, 0xe2
  9933. },
  9934. {
  9935. /* Sample #4 */
  9936. 0x99, 0x42, 0x9b, 0xd0, 0xbF, 0x79, 0x04, 0xe5
  9937. }
  9938. };
  9939. #endif /* MBEDTLS_DES_C */
  9940. #if defined(MBEDTLS_AES_C)
  9941. /* AES AES-CMAC-PRF-128 Test Data */
  9942. static const unsigned char PRFK[] = {
  9943. /* Key */
  9944. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9945. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  9946. 0xed, 0xcb
  9947. };
  9948. /* Sizes in bytes */
  9949. static const size_t PRFKlen[NB_PRF_TESTS] = {
  9950. 18,
  9951. 16,
  9952. 10
  9953. };
  9954. /* Message */
  9955. static const unsigned char PRFM[] = {
  9956. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9957. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  9958. 0x10, 0x11, 0x12, 0x13
  9959. };
  9960. static const unsigned char PRFT[NB_PRF_TESTS][16] = {
  9961. {
  9962. 0x84, 0xa3, 0x48, 0xa4, 0xa4, 0x5d, 0x23, 0x5b,
  9963. 0xab, 0xff, 0xfc, 0x0d, 0x2b, 0x4d, 0xa0, 0x9a
  9964. },
  9965. {
  9966. 0x98, 0x0a, 0xe8, 0x7b, 0x5f, 0x4c, 0x9c, 0x52,
  9967. 0x14, 0xf5, 0xb6, 0xa8, 0x45, 0x5e, 0x4c, 0x2d
  9968. },
  9969. {
  9970. 0x29, 0x0d, 0x9e, 0x11, 0x2e, 0xdb, 0x09, 0xee,
  9971. 0x14, 0x1f, 0xcf, 0x64, 0xc0, 0xb7, 0x2f, 0x3d
  9972. }
  9973. };
  9974. #endif /* MBEDTLS_AES_C */
  9975. static int cmac_test_subkeys( int verbose,
  9976. const char* testname,
  9977. const unsigned char* key,
  9978. int keybits,
  9979. const unsigned char* subkeys,
  9980. mbedtls_cipher_type_t cipher_type,
  9981. int block_size,
  9982. int num_tests )
  9983. {
  9984. int i, ret;
  9985. mbedtls_cipher_context_t ctx;
  9986. const mbedtls_cipher_info_t *cipher_info;
  9987. unsigned char K1[MBEDTLS_CIPHER_BLKSIZE_MAX];
  9988. unsigned char K2[MBEDTLS_CIPHER_BLKSIZE_MAX];
  9989. cipher_info = mbedtls_cipher_info_from_type( cipher_type );
  9990. if( cipher_info == NULL )
  9991. {
  9992. /* Failing at this point must be due to a build issue */
  9993. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  9994. }
  9995. for( i = 0; i < num_tests; i++ )
  9996. {
  9997. if( verbose != 0 )
  9998. mbedtls_printf( " %s CMAC subkey #%u: ", testname, i + 1 );
  9999. mbedtls_cipher_init( &ctx );
  10000. if( ( ret = mbedtls_cipher_setup( &ctx, cipher_info ) ) != 0 )
  10001. {
  10002. if( verbose != 0 )
  10003. mbedtls_printf( "test execution failed\n" );
  10004. goto cleanup;
  10005. }
  10006. if( ( ret = mbedtls_cipher_setkey( &ctx, key, keybits,
  10007. MBEDTLS_ENCRYPT ) ) != 0 )
  10008. {
  10009. if( verbose != 0 )
  10010. mbedtls_printf( "test execution failed\n" );
  10011. goto cleanup;
  10012. }
  10013. ret = cmac_generate_subkeys( &ctx, K1, K2 );
  10014. if( ret != 0 )
  10015. {
  10016. if( verbose != 0 )
  10017. mbedtls_printf( "failed\n" );
  10018. goto cleanup;
  10019. }
  10020. if( ( ret = memcmp( K1, subkeys, block_size ) ) != 0 ||
  10021. ( ret = memcmp( K2, &subkeys[block_size], block_size ) ) != 0 )
  10022. {
  10023. if( verbose != 0 )
  10024. mbedtls_printf( "failed\n" );
  10025. goto cleanup;
  10026. }
  10027. if( verbose != 0 )
  10028. mbedtls_printf( "passed\n" );
  10029. mbedtls_cipher_free( &ctx );
  10030. }
  10031. goto exit;
  10032. cleanup:
  10033. mbedtls_cipher_free( &ctx );
  10034. exit:
  10035. return( ret );
  10036. }
  10037. static int cmac_test_wth_cipher( int verbose,
  10038. const char* testname,
  10039. const unsigned char* key,
  10040. int keybits,
  10041. const unsigned char* messages,
  10042. const unsigned int message_lengths[4],
  10043. const unsigned char* expected_result,
  10044. mbedtls_cipher_type_t cipher_type,
  10045. int block_size,
  10046. int num_tests )
  10047. {
  10048. const mbedtls_cipher_info_t *cipher_info;
  10049. int i, ret;
  10050. unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX];
  10051. cipher_info = mbedtls_cipher_info_from_type( cipher_type );
  10052. if( cipher_info == NULL )
  10053. {
  10054. /* Failing at this point must be due to a build issue */
  10055. ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  10056. goto exit;
  10057. }
  10058. for( i = 0; i < num_tests; i++ )
  10059. {
  10060. if( verbose != 0 )
  10061. mbedtls_printf( " %s CMAC #%u: ", testname, i + 1 );
  10062. if( ( ret = mbedtls_cipher_cmac( cipher_info, key, keybits, messages,
  10063. message_lengths[i], output ) ) != 0 )
  10064. {
  10065. if( verbose != 0 )
  10066. mbedtls_printf( "failed\n" );
  10067. goto exit;
  10068. }
  10069. if( ( ret = memcmp( output, &expected_result[i * block_size], block_size ) ) != 0 )
  10070. {
  10071. if( verbose != 0 )
  10072. mbedtls_printf( "failed\n" );
  10073. goto exit;
  10074. }
  10075. if( verbose != 0 )
  10076. mbedtls_printf( "passed\n" );
  10077. }
  10078. exit:
  10079. return( ret );
  10080. }
  10081. #if defined(MBEDTLS_AES_C)
  10082. static int test_aes128_cmac_prf( int verbose )
  10083. {
  10084. int i;
  10085. int ret;
  10086. unsigned char output[MBEDTLS_AES_BLOCK_SIZE];
  10087. for( i = 0; i < NB_PRF_TESTS; i++ )
  10088. {
  10089. mbedtls_printf( " AES CMAC 128 PRF #%u: ", i );
  10090. ret = mbedtls_aes_cmac_prf_128( PRFK, PRFKlen[i], PRFM, 20, output );
  10091. if( ret != 0 ||
  10092. memcmp( output, PRFT[i], MBEDTLS_AES_BLOCK_SIZE ) != 0 )
  10093. {
  10094. if( verbose != 0 )
  10095. mbedtls_printf( "failed\n" );
  10096. return( ret );
  10097. }
  10098. else if( verbose != 0 )
  10099. {
  10100. mbedtls_printf( "passed\n" );
  10101. }
  10102. }
  10103. return( ret );
  10104. }
  10105. #endif /* MBEDTLS_AES_C */
  10106. int mbedtls_cmac_self_test( int verbose )
  10107. {
  10108. int ret;
  10109. #if defined(MBEDTLS_AES_C)
  10110. /* AES-128 */
  10111. if( ( ret = cmac_test_subkeys( verbose,
  10112. "AES 128",
  10113. aes_128_key,
  10114. 128,
  10115. (const unsigned char*)aes_128_subkeys,
  10116. MBEDTLS_CIPHER_AES_128_ECB,
  10117. MBEDTLS_AES_BLOCK_SIZE,
  10118. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  10119. {
  10120. return( ret );
  10121. }
  10122. if( ( ret = cmac_test_wth_cipher( verbose,
  10123. "AES 128",
  10124. aes_128_key,
  10125. 128,
  10126. test_message,
  10127. aes_message_lengths,
  10128. (const unsigned char*)aes_128_expected_result,
  10129. MBEDTLS_CIPHER_AES_128_ECB,
  10130. MBEDTLS_AES_BLOCK_SIZE,
  10131. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  10132. {
  10133. return( ret );
  10134. }
  10135. /* AES-192 */
  10136. if( ( ret = cmac_test_subkeys( verbose,
  10137. "AES 192",
  10138. aes_192_key,
  10139. 192,
  10140. (const unsigned char*)aes_192_subkeys,
  10141. MBEDTLS_CIPHER_AES_192_ECB,
  10142. MBEDTLS_AES_BLOCK_SIZE,
  10143. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  10144. {
  10145. return( ret );
  10146. }
  10147. if( ( ret = cmac_test_wth_cipher( verbose,
  10148. "AES 192",
  10149. aes_192_key,
  10150. 192,
  10151. test_message,
  10152. aes_message_lengths,
  10153. (const unsigned char*)aes_192_expected_result,
  10154. MBEDTLS_CIPHER_AES_192_ECB,
  10155. MBEDTLS_AES_BLOCK_SIZE,
  10156. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  10157. {
  10158. return( ret );
  10159. }
  10160. /* AES-256 */
  10161. if( ( ret = cmac_test_subkeys( verbose,
  10162. "AES 256",
  10163. aes_256_key,
  10164. 256,
  10165. (const unsigned char*)aes_256_subkeys,
  10166. MBEDTLS_CIPHER_AES_256_ECB,
  10167. MBEDTLS_AES_BLOCK_SIZE,
  10168. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  10169. {
  10170. return( ret );
  10171. }
  10172. if( ( ret = cmac_test_wth_cipher ( verbose,
  10173. "AES 256",
  10174. aes_256_key,
  10175. 256,
  10176. test_message,
  10177. aes_message_lengths,
  10178. (const unsigned char*)aes_256_expected_result,
  10179. MBEDTLS_CIPHER_AES_256_ECB,
  10180. MBEDTLS_AES_BLOCK_SIZE,
  10181. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  10182. {
  10183. return( ret );
  10184. }
  10185. #endif /* MBEDTLS_AES_C */
  10186. #if defined(MBEDTLS_DES_C)
  10187. /* 3DES 2 key */
  10188. if( ( ret = cmac_test_subkeys( verbose,
  10189. "3DES 2 key",
  10190. des3_2key_key,
  10191. 192,
  10192. (const unsigned char*)des3_2key_subkeys,
  10193. MBEDTLS_CIPHER_DES_EDE3_ECB,
  10194. MBEDTLS_DES3_BLOCK_SIZE,
  10195. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  10196. {
  10197. return( ret );
  10198. }
  10199. if( ( ret = cmac_test_wth_cipher( verbose,
  10200. "3DES 2 key",
  10201. des3_2key_key,
  10202. 192,
  10203. test_message,
  10204. des3_message_lengths,
  10205. (const unsigned char*)des3_2key_expected_result,
  10206. MBEDTLS_CIPHER_DES_EDE3_ECB,
  10207. MBEDTLS_DES3_BLOCK_SIZE,
  10208. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  10209. {
  10210. return( ret );
  10211. }
  10212. /* 3DES 3 key */
  10213. if( ( ret = cmac_test_subkeys( verbose,
  10214. "3DES 3 key",
  10215. des3_3key_key,
  10216. 192,
  10217. (const unsigned char*)des3_3key_subkeys,
  10218. MBEDTLS_CIPHER_DES_EDE3_ECB,
  10219. MBEDTLS_DES3_BLOCK_SIZE,
  10220. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  10221. {
  10222. return( ret );
  10223. }
  10224. if( ( ret = cmac_test_wth_cipher( verbose,
  10225. "3DES 3 key",
  10226. des3_3key_key,
  10227. 192,
  10228. test_message,
  10229. des3_message_lengths,
  10230. (const unsigned char*)des3_3key_expected_result,
  10231. MBEDTLS_CIPHER_DES_EDE3_ECB,
  10232. MBEDTLS_DES3_BLOCK_SIZE,
  10233. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  10234. {
  10235. return( ret );
  10236. }
  10237. #endif /* MBEDTLS_DES_C */
  10238. #if defined(MBEDTLS_AES_C)
  10239. if( ( ret = test_aes128_cmac_prf( verbose ) ) != 0 )
  10240. return( ret );
  10241. #endif /* MBEDTLS_AES_C */
  10242. if( verbose != 0 )
  10243. mbedtls_printf( "\n" );
  10244. return( 0 );
  10245. }
  10246. #endif /* MBEDTLS_SELF_TEST */
  10247. #endif /* MBEDTLS_CMAC_C */
  10248. /*
  10249. Amalgamated build undefines
  10250. */
  10251. #undef ADD
  10252. #undef BC
  10253. #undef BEFORE_COLON
  10254. #undef F
  10255. #undef F0
  10256. #undef F1
  10257. #undef F2
  10258. #undef F3
  10259. #undef F4
  10260. #undef F5
  10261. #undef FSb
  10262. #undef K
  10263. #undef KK
  10264. #undef P
  10265. #undef R
  10266. #undef ROTR
  10267. #undef S
  10268. #undef S0
  10269. #undef S1
  10270. #undef S2
  10271. #undef S3
  10272. #undef SAFE_SNPRINTF
  10273. #undef SHR
  10274. #undef close
  10275. #undef read
  10276. #undef supported_init
  10277. #undef write
  10278. /********* Start of file library/ctr_drbg.c ************/
  10279. /*
  10280. * CTR_DRBG implementation based on AES-256 (NIST SP 800-90)
  10281. *
  10282. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  10283. * SPDX-License-Identifier: Apache-2.0
  10284. *
  10285. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10286. * not use this file except in compliance with the License.
  10287. * You may obtain a copy of the License at
  10288. *
  10289. * http://www.apache.org/licenses/LICENSE-2.0
  10290. *
  10291. * Unless required by applicable law or agreed to in writing, software
  10292. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  10293. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10294. * See the License for the specific language governing permissions and
  10295. * limitations under the License.
  10296. *
  10297. * This file is part of mbed TLS (https://tls.mbed.org)
  10298. */
  10299. /*
  10300. * The NIST SP 800-90 DRBGs are described in the following publication.
  10301. *
  10302. * http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf
  10303. */
  10304. #if !defined(MBEDTLS_CONFIG_FILE)
  10305. #else
  10306. #endif
  10307. #if defined(MBEDTLS_CTR_DRBG_C)
  10308. #include <string.h>
  10309. #if defined(MBEDTLS_FS_IO)
  10310. #include <stdio.h>
  10311. #endif
  10312. #if defined(MBEDTLS_SELF_TEST)
  10313. #if defined(MBEDTLS_PLATFORM_C)
  10314. #else
  10315. #include <stdio.h>
  10316. #define mbedtls_printf printf
  10317. #endif /* MBEDTLS_PLATFORM_C */
  10318. #endif /* MBEDTLS_SELF_TEST */
  10319. /* Implementation that should never be optimized out by the compiler */
  10320. /* zeroize was here */
  10321. /*
  10322. * CTR_DRBG context initialization
  10323. */
  10324. void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx )
  10325. {
  10326. memset( ctx, 0, sizeof( mbedtls_ctr_drbg_context ) );
  10327. #if defined(MBEDTLS_THREADING_C)
  10328. mbedtls_mutex_init( &ctx->mutex );
  10329. #endif
  10330. }
  10331. /*
  10332. * Non-public function wrapped by mbedtls_ctr_drbg_seed(). Necessary to allow
  10333. * NIST tests to succeed (which require known length fixed entropy)
  10334. */
  10335. int mbedtls_ctr_drbg_seed_entropy_len(
  10336. mbedtls_ctr_drbg_context *ctx,
  10337. int (*f_entropy)(void *, unsigned char *, size_t),
  10338. void *p_entropy,
  10339. const unsigned char *custom,
  10340. size_t len,
  10341. size_t entropy_len )
  10342. {
  10343. int ret;
  10344. unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
  10345. memset( key, 0, MBEDTLS_CTR_DRBG_KEYSIZE );
  10346. mbedtls_aes_init( &ctx->aes_ctx );
  10347. ctx->f_entropy = f_entropy;
  10348. ctx->p_entropy = p_entropy;
  10349. ctx->entropy_len = entropy_len;
  10350. ctx->reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL;
  10351. /*
  10352. * Initialize with an empty key
  10353. */
  10354. if( ( ret = mbedtls_aes_setkey_enc( &ctx->aes_ctx, key, MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 )
  10355. {
  10356. return( ret );
  10357. }
  10358. if( ( ret = mbedtls_ctr_drbg_reseed( ctx, custom, len ) ) != 0 )
  10359. {
  10360. return( ret );
  10361. }
  10362. return( 0 );
  10363. }
  10364. int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
  10365. int (*f_entropy)(void *, unsigned char *, size_t),
  10366. void *p_entropy,
  10367. const unsigned char *custom,
  10368. size_t len )
  10369. {
  10370. return( mbedtls_ctr_drbg_seed_entropy_len( ctx, f_entropy, p_entropy, custom, len,
  10371. MBEDTLS_CTR_DRBG_ENTROPY_LEN ) );
  10372. }
  10373. void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx )
  10374. {
  10375. if( ctx == NULL )
  10376. return;
  10377. #if defined(MBEDTLS_THREADING_C)
  10378. mbedtls_mutex_free( &ctx->mutex );
  10379. #endif
  10380. mbedtls_aes_free( &ctx->aes_ctx );
  10381. mbedtls_zeroize( ctx, sizeof( mbedtls_ctr_drbg_context ) );
  10382. }
  10383. void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx, int resistance )
  10384. {
  10385. ctx->prediction_resistance = resistance;
  10386. }
  10387. void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx, size_t len )
  10388. {
  10389. ctx->entropy_len = len;
  10390. }
  10391. void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx, int interval )
  10392. {
  10393. ctx->reseed_interval = interval;
  10394. }
  10395. static int block_cipher_df( unsigned char *output,
  10396. const unsigned char *data, size_t data_len )
  10397. {
  10398. unsigned char buf[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + MBEDTLS_CTR_DRBG_BLOCKSIZE + 16];
  10399. unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN];
  10400. unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
  10401. unsigned char chain[MBEDTLS_CTR_DRBG_BLOCKSIZE];
  10402. unsigned char *p, *iv;
  10403. mbedtls_aes_context aes_ctx;
  10404. int ret = 0;
  10405. int i, j;
  10406. size_t buf_len, use_len;
  10407. if( data_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT )
  10408. return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
  10409. memset( buf, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + MBEDTLS_CTR_DRBG_BLOCKSIZE + 16 );
  10410. mbedtls_aes_init( &aes_ctx );
  10411. /*
  10412. * Construct IV (16 bytes) and S in buffer
  10413. * IV = Counter (in 32-bits) padded to 16 with zeroes
  10414. * S = Length input string (in 32-bits) || Length of output (in 32-bits) ||
  10415. * data || 0x80
  10416. * (Total is padded to a multiple of 16-bytes with zeroes)
  10417. */
  10418. p = buf + MBEDTLS_CTR_DRBG_BLOCKSIZE;
  10419. *p++ = ( data_len >> 24 ) & 0xff;
  10420. *p++ = ( data_len >> 16 ) & 0xff;
  10421. *p++ = ( data_len >> 8 ) & 0xff;
  10422. *p++ = ( data_len ) & 0xff;
  10423. p += 3;
  10424. *p++ = MBEDTLS_CTR_DRBG_SEEDLEN;
  10425. memcpy( p, data, data_len );
  10426. p[data_len] = 0x80;
  10427. buf_len = MBEDTLS_CTR_DRBG_BLOCKSIZE + 8 + data_len + 1;
  10428. for( i = 0; i < MBEDTLS_CTR_DRBG_KEYSIZE; i++ )
  10429. key[i] = i;
  10430. if( ( ret = mbedtls_aes_setkey_enc( &aes_ctx, key, MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 )
  10431. {
  10432. goto exit;
  10433. }
  10434. /*
  10435. * Reduce data to MBEDTLS_CTR_DRBG_SEEDLEN bytes of data
  10436. */
  10437. for( j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE )
  10438. {
  10439. p = buf;
  10440. memset( chain, 0, MBEDTLS_CTR_DRBG_BLOCKSIZE );
  10441. use_len = buf_len;
  10442. while( use_len > 0 )
  10443. {
  10444. for( i = 0; i < MBEDTLS_CTR_DRBG_BLOCKSIZE; i++ )
  10445. chain[i] ^= p[i];
  10446. p += MBEDTLS_CTR_DRBG_BLOCKSIZE;
  10447. use_len -= ( use_len >= MBEDTLS_CTR_DRBG_BLOCKSIZE ) ?
  10448. MBEDTLS_CTR_DRBG_BLOCKSIZE : use_len;
  10449. if( ( ret = mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, chain, chain ) ) != 0 )
  10450. {
  10451. goto exit;
  10452. }
  10453. }
  10454. memcpy( tmp + j, chain, MBEDTLS_CTR_DRBG_BLOCKSIZE );
  10455. /*
  10456. * Update IV
  10457. */
  10458. buf[3]++;
  10459. }
  10460. /*
  10461. * Do final encryption with reduced data
  10462. */
  10463. if( ( ret = mbedtls_aes_setkey_enc( &aes_ctx, tmp, MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 )
  10464. {
  10465. goto exit;
  10466. }
  10467. iv = tmp + MBEDTLS_CTR_DRBG_KEYSIZE;
  10468. p = output;
  10469. for( j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE )
  10470. {
  10471. if( ( ret = mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, iv, iv ) ) != 0 )
  10472. {
  10473. goto exit;
  10474. }
  10475. memcpy( p, iv, MBEDTLS_CTR_DRBG_BLOCKSIZE );
  10476. p += MBEDTLS_CTR_DRBG_BLOCKSIZE;
  10477. }
  10478. exit:
  10479. mbedtls_aes_free( &aes_ctx );
  10480. /*
  10481. * tidy up the stack
  10482. */
  10483. mbedtls_zeroize( buf, sizeof( buf ) );
  10484. mbedtls_zeroize( tmp, sizeof( tmp ) );
  10485. mbedtls_zeroize( key, sizeof( key ) );
  10486. mbedtls_zeroize( chain, sizeof( chain ) );
  10487. if( 0 != ret )
  10488. {
  10489. /*
  10490. * wipe partial seed from memory
  10491. */
  10492. mbedtls_zeroize( output, MBEDTLS_CTR_DRBG_SEEDLEN );
  10493. }
  10494. return( ret );
  10495. }
  10496. static int ctr_drbg_update_internal( mbedtls_ctr_drbg_context *ctx,
  10497. const unsigned char data[MBEDTLS_CTR_DRBG_SEEDLEN] )
  10498. {
  10499. unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN];
  10500. unsigned char *p = tmp;
  10501. int i, j;
  10502. int ret = 0;
  10503. memset( tmp, 0, MBEDTLS_CTR_DRBG_SEEDLEN );
  10504. for( j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE )
  10505. {
  10506. /*
  10507. * Increase counter
  10508. */
  10509. for( i = MBEDTLS_CTR_DRBG_BLOCKSIZE; i > 0; i-- )
  10510. if( ++ctx->counter[i - 1] != 0 )
  10511. break;
  10512. /*
  10513. * Crypt counter block
  10514. */
  10515. if( ( ret = mbedtls_aes_crypt_ecb( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, ctx->counter, p ) ) != 0 )
  10516. {
  10517. return( ret );
  10518. }
  10519. p += MBEDTLS_CTR_DRBG_BLOCKSIZE;
  10520. }
  10521. for( i = 0; i < MBEDTLS_CTR_DRBG_SEEDLEN; i++ )
  10522. tmp[i] ^= data[i];
  10523. /*
  10524. * Update key and counter
  10525. */
  10526. if( ( ret = mbedtls_aes_setkey_enc( &ctx->aes_ctx, tmp, MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 )
  10527. {
  10528. return( ret );
  10529. }
  10530. memcpy( ctx->counter, tmp + MBEDTLS_CTR_DRBG_KEYSIZE, MBEDTLS_CTR_DRBG_BLOCKSIZE );
  10531. return( 0 );
  10532. }
  10533. void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
  10534. const unsigned char *additional, size_t add_len )
  10535. {
  10536. unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN];
  10537. if( add_len > 0 )
  10538. {
  10539. /* MAX_INPUT would be more logical here, but we have to match
  10540. * block_cipher_df()'s limits since we can't propagate errors */
  10541. if( add_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT )
  10542. add_len = MBEDTLS_CTR_DRBG_MAX_SEED_INPUT;
  10543. block_cipher_df( add_input, additional, add_len );
  10544. ctr_drbg_update_internal( ctx, add_input );
  10545. }
  10546. }
  10547. int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
  10548. const unsigned char *additional, size_t len )
  10549. {
  10550. unsigned char seed[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT];
  10551. size_t seedlen = 0;
  10552. int ret;
  10553. if( ctx->entropy_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT ||
  10554. len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - ctx->entropy_len )
  10555. return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
  10556. memset( seed, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT );
  10557. /*
  10558. * Gather entropy_len bytes of entropy to seed state
  10559. */
  10560. if( 0 != ctx->f_entropy( ctx->p_entropy, seed,
  10561. ctx->entropy_len ) )
  10562. {
  10563. return( MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED );
  10564. }
  10565. seedlen += ctx->entropy_len;
  10566. /*
  10567. * Add additional data
  10568. */
  10569. if( additional && len )
  10570. {
  10571. memcpy( seed + seedlen, additional, len );
  10572. seedlen += len;
  10573. }
  10574. /*
  10575. * Reduce to 384 bits
  10576. */
  10577. if( ( ret = block_cipher_df( seed, seed, seedlen ) ) != 0 )
  10578. {
  10579. return( ret );
  10580. }
  10581. /*
  10582. * Update state
  10583. */
  10584. if( ( ret = ctr_drbg_update_internal( ctx, seed ) ) != 0 )
  10585. {
  10586. return( ret );
  10587. }
  10588. ctx->reseed_counter = 1;
  10589. return( 0 );
  10590. }
  10591. int mbedtls_ctr_drbg_random_with_add( void *p_rng,
  10592. unsigned char *output, size_t output_len,
  10593. const unsigned char *additional, size_t add_len )
  10594. {
  10595. int ret = 0;
  10596. mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng;
  10597. unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN];
  10598. unsigned char *p = output;
  10599. unsigned char tmp[MBEDTLS_CTR_DRBG_BLOCKSIZE];
  10600. int i;
  10601. size_t use_len;
  10602. if( output_len > MBEDTLS_CTR_DRBG_MAX_REQUEST )
  10603. return( MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG );
  10604. if( add_len > MBEDTLS_CTR_DRBG_MAX_INPUT )
  10605. return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
  10606. memset( add_input, 0, MBEDTLS_CTR_DRBG_SEEDLEN );
  10607. if( ctx->reseed_counter > ctx->reseed_interval ||
  10608. ctx->prediction_resistance )
  10609. {
  10610. if( ( ret = mbedtls_ctr_drbg_reseed( ctx, additional, add_len ) ) != 0 )
  10611. {
  10612. return( ret );
  10613. }
  10614. add_len = 0;
  10615. }
  10616. if( add_len > 0 )
  10617. {
  10618. if( ( ret = block_cipher_df( add_input, additional, add_len ) ) != 0 )
  10619. {
  10620. return( ret );
  10621. }
  10622. if( ( ret = ctr_drbg_update_internal( ctx, add_input ) ) != 0 )
  10623. {
  10624. return( ret );
  10625. }
  10626. }
  10627. while( output_len > 0 )
  10628. {
  10629. /*
  10630. * Increase counter
  10631. */
  10632. for( i = MBEDTLS_CTR_DRBG_BLOCKSIZE; i > 0; i-- )
  10633. if( ++ctx->counter[i - 1] != 0 )
  10634. break;
  10635. /*
  10636. * Crypt counter block
  10637. */
  10638. if( ( ret = mbedtls_aes_crypt_ecb( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, ctx->counter, tmp ) ) != 0 )
  10639. {
  10640. return( ret );
  10641. }
  10642. use_len = ( output_len > MBEDTLS_CTR_DRBG_BLOCKSIZE ) ? MBEDTLS_CTR_DRBG_BLOCKSIZE :
  10643. output_len;
  10644. /*
  10645. * Copy random block to destination
  10646. */
  10647. memcpy( p, tmp, use_len );
  10648. p += use_len;
  10649. output_len -= use_len;
  10650. }
  10651. if( ( ret = ctr_drbg_update_internal( ctx, add_input ) ) != 0 )
  10652. {
  10653. return( ret );
  10654. }
  10655. ctx->reseed_counter++;
  10656. return( 0 );
  10657. }
  10658. int mbedtls_ctr_drbg_random( void *p_rng, unsigned char *output, size_t output_len )
  10659. {
  10660. int ret;
  10661. mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng;
  10662. #if defined(MBEDTLS_THREADING_C)
  10663. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  10664. return( ret );
  10665. #endif
  10666. ret = mbedtls_ctr_drbg_random_with_add( ctx, output, output_len, NULL, 0 );
  10667. #if defined(MBEDTLS_THREADING_C)
  10668. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  10669. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  10670. #endif
  10671. return( ret );
  10672. }
  10673. #if defined(MBEDTLS_FS_IO)
  10674. int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path )
  10675. {
  10676. int ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
  10677. FILE *f;
  10678. unsigned char buf[ MBEDTLS_CTR_DRBG_MAX_INPUT ];
  10679. if( ( f = fopen( path, "wb" ) ) == NULL )
  10680. return( MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR );
  10681. if( ( ret = mbedtls_ctr_drbg_random( ctx, buf, MBEDTLS_CTR_DRBG_MAX_INPUT ) ) != 0 )
  10682. goto exit;
  10683. if( fwrite( buf, 1, MBEDTLS_CTR_DRBG_MAX_INPUT, f ) != MBEDTLS_CTR_DRBG_MAX_INPUT )
  10684. ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
  10685. else
  10686. ret = 0;
  10687. exit:
  10688. mbedtls_zeroize( buf, sizeof( buf ) );
  10689. fclose( f );
  10690. return( ret );
  10691. }
  10692. int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path )
  10693. {
  10694. int ret = 0;
  10695. FILE *f;
  10696. size_t n;
  10697. unsigned char buf[ MBEDTLS_CTR_DRBG_MAX_INPUT ];
  10698. if( ( f = fopen( path, "rb" ) ) == NULL )
  10699. return( MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR );
  10700. fseek( f, 0, SEEK_END );
  10701. n = (size_t) ftell( f );
  10702. fseek( f, 0, SEEK_SET );
  10703. if( n > MBEDTLS_CTR_DRBG_MAX_INPUT )
  10704. {
  10705. fclose( f );
  10706. return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
  10707. }
  10708. if( fread( buf, 1, n, f ) != n )
  10709. ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
  10710. else
  10711. mbedtls_ctr_drbg_update( ctx, buf, n );
  10712. fclose( f );
  10713. mbedtls_zeroize( buf, sizeof( buf ) );
  10714. if( ret != 0 )
  10715. return( ret );
  10716. return( mbedtls_ctr_drbg_write_seed_file( ctx, path ) );
  10717. }
  10718. #endif /* MBEDTLS_FS_IO */
  10719. #if defined(MBEDTLS_SELF_TEST)
  10720. static const unsigned char entropy_source_pr[96] =
  10721. { 0xc1, 0x80, 0x81, 0xa6, 0x5d, 0x44, 0x02, 0x16,
  10722. 0x19, 0xb3, 0xf1, 0x80, 0xb1, 0xc9, 0x20, 0x02,
  10723. 0x6a, 0x54, 0x6f, 0x0c, 0x70, 0x81, 0x49, 0x8b,
  10724. 0x6e, 0xa6, 0x62, 0x52, 0x6d, 0x51, 0xb1, 0xcb,
  10725. 0x58, 0x3b, 0xfa, 0xd5, 0x37, 0x5f, 0xfb, 0xc9,
  10726. 0xff, 0x46, 0xd2, 0x19, 0xc7, 0x22, 0x3e, 0x95,
  10727. 0x45, 0x9d, 0x82, 0xe1, 0xe7, 0x22, 0x9f, 0x63,
  10728. 0x31, 0x69, 0xd2, 0x6b, 0x57, 0x47, 0x4f, 0xa3,
  10729. 0x37, 0xc9, 0x98, 0x1c, 0x0b, 0xfb, 0x91, 0x31,
  10730. 0x4d, 0x55, 0xb9, 0xe9, 0x1c, 0x5a, 0x5e, 0xe4,
  10731. 0x93, 0x92, 0xcf, 0xc5, 0x23, 0x12, 0xd5, 0x56,
  10732. 0x2c, 0x4a, 0x6e, 0xff, 0xdc, 0x10, 0xd0, 0x68 };
  10733. static const unsigned char entropy_source_nopr[64] =
  10734. { 0x5a, 0x19, 0x4d, 0x5e, 0x2b, 0x31, 0x58, 0x14,
  10735. 0x54, 0xde, 0xf6, 0x75, 0xfb, 0x79, 0x58, 0xfe,
  10736. 0xc7, 0xdb, 0x87, 0x3e, 0x56, 0x89, 0xfc, 0x9d,
  10737. 0x03, 0x21, 0x7c, 0x68, 0xd8, 0x03, 0x38, 0x20,
  10738. 0xf9, 0xe6, 0x5e, 0x04, 0xd8, 0x56, 0xf3, 0xa9,
  10739. 0xc4, 0x4a, 0x4c, 0xbd, 0xc1, 0xd0, 0x08, 0x46,
  10740. 0xf5, 0x98, 0x3d, 0x77, 0x1c, 0x1b, 0x13, 0x7e,
  10741. 0x4e, 0x0f, 0x9d, 0x8e, 0xf4, 0x09, 0xf9, 0x2e };
  10742. static const unsigned char nonce_pers_pr[16] =
  10743. { 0xd2, 0x54, 0xfc, 0xff, 0x02, 0x1e, 0x69, 0xd2,
  10744. 0x29, 0xc9, 0xcf, 0xad, 0x85, 0xfa, 0x48, 0x6c };
  10745. static const unsigned char nonce_pers_nopr[16] =
  10746. { 0x1b, 0x54, 0xb8, 0xff, 0x06, 0x42, 0xbf, 0xf5,
  10747. 0x21, 0xf1, 0x5c, 0x1c, 0x0b, 0x66, 0x5f, 0x3f };
  10748. static const unsigned char result_pr[16] =
  10749. { 0x34, 0x01, 0x16, 0x56, 0xb4, 0x29, 0x00, 0x8f,
  10750. 0x35, 0x63, 0xec, 0xb5, 0xf2, 0x59, 0x07, 0x23 };
  10751. static const unsigned char result_nopr[16] =
  10752. { 0xa0, 0x54, 0x30, 0x3d, 0x8a, 0x7e, 0xa9, 0x88,
  10753. 0x9d, 0x90, 0x3e, 0x07, 0x7c, 0x6f, 0x21, 0x8f };
  10754. static size_t test_offset;
  10755. static int ctr_drbg_self_test_entropy( void *data, unsigned char *buf,
  10756. size_t len )
  10757. {
  10758. const unsigned char *p = data;
  10759. memcpy( buf, p + test_offset, len );
  10760. test_offset += len;
  10761. return( 0 );
  10762. }
  10763. #define CHK( c ) if( (c) != 0 ) \
  10764. { \
  10765. if( verbose != 0 ) \
  10766. mbedtls_printf( "failed\n" ); \
  10767. return( 1 ); \
  10768. }
  10769. /*
  10770. * Checkup routine
  10771. */
  10772. int mbedtls_ctr_drbg_self_test( int verbose )
  10773. {
  10774. mbedtls_ctr_drbg_context ctx;
  10775. unsigned char buf[16];
  10776. mbedtls_ctr_drbg_init( &ctx );
  10777. /*
  10778. * Based on a NIST CTR_DRBG test vector (PR = True)
  10779. */
  10780. if( verbose != 0 )
  10781. mbedtls_printf( " CTR_DRBG (PR = TRUE) : " );
  10782. test_offset = 0;
  10783. CHK( mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy,
  10784. (void *) entropy_source_pr, nonce_pers_pr, 16, 32 ) );
  10785. mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
  10786. CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
  10787. CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
  10788. CHK( memcmp( buf, result_pr, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
  10789. mbedtls_ctr_drbg_free( &ctx );
  10790. if( verbose != 0 )
  10791. mbedtls_printf( "passed\n" );
  10792. /*
  10793. * Based on a NIST CTR_DRBG test vector (PR = FALSE)
  10794. */
  10795. if( verbose != 0 )
  10796. mbedtls_printf( " CTR_DRBG (PR = FALSE): " );
  10797. mbedtls_ctr_drbg_init( &ctx );
  10798. test_offset = 0;
  10799. CHK( mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy,
  10800. (void *) entropy_source_nopr, nonce_pers_nopr, 16, 32 ) );
  10801. CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) );
  10802. CHK( mbedtls_ctr_drbg_reseed( &ctx, NULL, 0 ) );
  10803. CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) );
  10804. CHK( memcmp( buf, result_nopr, 16 ) );
  10805. mbedtls_ctr_drbg_free( &ctx );
  10806. if( verbose != 0 )
  10807. mbedtls_printf( "passed\n" );
  10808. if( verbose != 0 )
  10809. mbedtls_printf( "\n" );
  10810. return( 0 );
  10811. }
  10812. #endif /* MBEDTLS_SELF_TEST */
  10813. #endif /* MBEDTLS_CTR_DRBG_C */
  10814. /*
  10815. Amalgamated build undefines
  10816. */
  10817. #undef ADD
  10818. #undef BC
  10819. #undef BEFORE_COLON
  10820. #undef F
  10821. #undef F0
  10822. #undef F1
  10823. #undef F2
  10824. #undef F3
  10825. #undef F4
  10826. #undef F5
  10827. #undef FSb
  10828. #undef K
  10829. #undef KK
  10830. #undef P
  10831. #undef R
  10832. #undef ROTR
  10833. #undef S
  10834. #undef S0
  10835. #undef S1
  10836. #undef S2
  10837. #undef S3
  10838. #undef SAFE_SNPRINTF
  10839. #undef SHR
  10840. #undef close
  10841. #undef read
  10842. #undef supported_init
  10843. #undef write
  10844. /********* Start of file library/debug.c ************/
  10845. /*
  10846. * Debugging routines
  10847. *
  10848. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  10849. * SPDX-License-Identifier: Apache-2.0
  10850. *
  10851. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10852. * not use this file except in compliance with the License.
  10853. * You may obtain a copy of the License at
  10854. *
  10855. * http://www.apache.org/licenses/LICENSE-2.0
  10856. *
  10857. * Unless required by applicable law or agreed to in writing, software
  10858. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  10859. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10860. * See the License for the specific language governing permissions and
  10861. * limitations under the License.
  10862. *
  10863. * This file is part of mbed TLS (https://tls.mbed.org)
  10864. */
  10865. #if !defined(MBEDTLS_CONFIG_FILE)
  10866. #else
  10867. #endif
  10868. #if defined(MBEDTLS_DEBUG_C)
  10869. #if defined(MBEDTLS_PLATFORM_C)
  10870. #else
  10871. #include <stdlib.h>
  10872. #define mbedtls_calloc calloc
  10873. #define mbedtls_free free
  10874. #define mbedtls_time_t time_t
  10875. #define mbedtls_snprintf snprintf
  10876. #endif
  10877. #include <stdarg.h>
  10878. #include <stdio.h>
  10879. #include <string.h>
  10880. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  10881. !defined(inline) && !defined(__cplusplus)
  10882. #define inline __inline
  10883. #endif
  10884. #define DEBUG_BUF_SIZE 512
  10885. static int debug_threshold = 0;
  10886. void mbedtls_debug_set_threshold( int threshold )
  10887. {
  10888. debug_threshold = threshold;
  10889. }
  10890. /*
  10891. * All calls to f_dbg must be made via this function
  10892. */
  10893. static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
  10894. const char *file, int line,
  10895. const char *str )
  10896. {
  10897. /*
  10898. * If in a threaded environment, we need a thread identifier.
  10899. * Since there is no portable way to get one, use the address of the ssl
  10900. * context instead, as it shouldn't be shared between threads.
  10901. */
  10902. #if defined(MBEDTLS_THREADING_C)
  10903. char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
  10904. mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str );
  10905. ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
  10906. #else
  10907. ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
  10908. #endif
  10909. }
  10910. void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
  10911. const char *file, int line,
  10912. const char *format, ... )
  10913. {
  10914. va_list argp;
  10915. char str[DEBUG_BUF_SIZE];
  10916. int ret;
  10917. if( NULL == ssl || NULL == ssl->conf || NULL == ssl->conf->f_dbg || level > debug_threshold )
  10918. return;
  10919. va_start( argp, format );
  10920. #if defined(_WIN32)
  10921. #if defined(_TRUNCATE) && !defined(__MINGW32__)
  10922. ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp );
  10923. #else
  10924. ret = _vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
  10925. if( ret < 0 || (size_t) ret == DEBUG_BUF_SIZE )
  10926. {
  10927. str[DEBUG_BUF_SIZE-1] = '\0';
  10928. ret = -1;
  10929. }
  10930. #endif
  10931. #else
  10932. ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
  10933. #endif
  10934. va_end( argp );
  10935. if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
  10936. {
  10937. str[ret] = '\n';
  10938. str[ret + 1] = '\0';
  10939. }
  10940. debug_send_line( ssl, level, file, line, str );
  10941. }
  10942. void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
  10943. const char *file, int line,
  10944. const char *text, int ret )
  10945. {
  10946. char str[DEBUG_BUF_SIZE];
  10947. if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
  10948. return;
  10949. /*
  10950. * With non-blocking I/O and examples that just retry immediately,
  10951. * the logs would be quickly flooded with WANT_READ, so ignore that.
  10952. * Don't ignore WANT_WRITE however, since is is usually rare.
  10953. */
  10954. if( ret == MBEDTLS_ERR_SSL_WANT_READ )
  10955. return;
  10956. mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
  10957. text, ret, -ret );
  10958. debug_send_line( ssl, level, file, line, str );
  10959. }
  10960. void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
  10961. const char *file, int line, const char *text,
  10962. const unsigned char *buf, size_t len )
  10963. {
  10964. char str[DEBUG_BUF_SIZE];
  10965. char txt[17];
  10966. size_t i, idx = 0;
  10967. if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
  10968. return;
  10969. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
  10970. text, (unsigned int) len );
  10971. debug_send_line( ssl, level, file, line, str );
  10972. idx = 0;
  10973. memset( txt, 0, sizeof( txt ) );
  10974. for( i = 0; i < len; i++ )
  10975. {
  10976. if( i >= 4096 )
  10977. break;
  10978. if( i % 16 == 0 )
  10979. {
  10980. if( i > 0 )
  10981. {
  10982. mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
  10983. debug_send_line( ssl, level, file, line, str );
  10984. idx = 0;
  10985. memset( txt, 0, sizeof( txt ) );
  10986. }
  10987. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
  10988. (unsigned int) i );
  10989. }
  10990. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
  10991. (unsigned int) buf[i] );
  10992. txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
  10993. }
  10994. if( len > 0 )
  10995. {
  10996. for( /* i = i */; i % 16 != 0; i++ )
  10997. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
  10998. mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
  10999. debug_send_line( ssl, level, file, line, str );
  11000. }
  11001. }
  11002. #if defined(MBEDTLS_ECP_C)
  11003. void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
  11004. const char *file, int line,
  11005. const char *text, const mbedtls_ecp_point *X )
  11006. {
  11007. char str[DEBUG_BUF_SIZE];
  11008. if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
  11009. return;
  11010. mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
  11011. mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
  11012. mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
  11013. mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
  11014. }
  11015. #endif /* MBEDTLS_ECP_C */
  11016. #if defined(MBEDTLS_BIGNUM_C)
  11017. void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
  11018. const char *file, int line,
  11019. const char *text, const mbedtls_mpi *X )
  11020. {
  11021. char str[DEBUG_BUF_SIZE];
  11022. int j, k, zeros = 1;
  11023. size_t i, n, idx = 0;
  11024. if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold )
  11025. return;
  11026. for( n = X->n - 1; n > 0; n-- )
  11027. if( X->p[n] != 0 )
  11028. break;
  11029. for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
  11030. if( ( ( X->p[n] >> j ) & 1 ) != 0 )
  11031. break;
  11032. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
  11033. text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
  11034. debug_send_line( ssl, level, file, line, str );
  11035. idx = 0;
  11036. for( i = n + 1, j = 0; i > 0; i-- )
  11037. {
  11038. if( zeros && X->p[i - 1] == 0 )
  11039. continue;
  11040. for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
  11041. {
  11042. if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
  11043. continue;
  11044. else
  11045. zeros = 0;
  11046. if( j % 16 == 0 )
  11047. {
  11048. if( j > 0 )
  11049. {
  11050. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
  11051. debug_send_line( ssl, level, file, line, str );
  11052. idx = 0;
  11053. }
  11054. }
  11055. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
  11056. ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
  11057. j++;
  11058. }
  11059. }
  11060. if( zeros == 1 )
  11061. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
  11062. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
  11063. debug_send_line( ssl, level, file, line, str );
  11064. }
  11065. #endif /* MBEDTLS_BIGNUM_C */
  11066. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  11067. static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
  11068. const char *file, int line,
  11069. const char *text, const mbedtls_pk_context *pk )
  11070. {
  11071. size_t i;
  11072. mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
  11073. char name[16];
  11074. memset( items, 0, sizeof( items ) );
  11075. if( mbedtls_pk_debug( pk, items ) != 0 )
  11076. {
  11077. debug_send_line( ssl, level, file, line,
  11078. "invalid PK context\n" );
  11079. return;
  11080. }
  11081. for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
  11082. {
  11083. if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
  11084. return;
  11085. mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
  11086. name[sizeof( name ) - 1] = '\0';
  11087. if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
  11088. mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
  11089. else
  11090. #if defined(MBEDTLS_ECP_C)
  11091. if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
  11092. mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
  11093. else
  11094. #endif
  11095. debug_send_line( ssl, level, file, line,
  11096. "should not happen\n" );
  11097. }
  11098. }
  11099. static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
  11100. const char *file, int line, const char *text )
  11101. {
  11102. char str[DEBUG_BUF_SIZE];
  11103. const char *start, *cur;
  11104. start = text;
  11105. for( cur = text; *cur != '\0'; cur++ )
  11106. {
  11107. if( *cur == '\n' )
  11108. {
  11109. size_t len = cur - start + 1;
  11110. if( len > DEBUG_BUF_SIZE - 1 )
  11111. len = DEBUG_BUF_SIZE - 1;
  11112. memcpy( str, start, len );
  11113. str[len] = '\0';
  11114. debug_send_line( ssl, level, file, line, str );
  11115. start = cur + 1;
  11116. }
  11117. }
  11118. }
  11119. void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
  11120. const char *file, int line,
  11121. const char *text, const mbedtls_x509_crt *crt )
  11122. {
  11123. char str[DEBUG_BUF_SIZE];
  11124. int i = 0;
  11125. if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold )
  11126. return;
  11127. while( crt != NULL )
  11128. {
  11129. char buf[1024];
  11130. mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
  11131. debug_send_line( ssl, level, file, line, str );
  11132. mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
  11133. debug_print_line_by_line( ssl, level, file, line, buf );
  11134. debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
  11135. crt = crt->next;
  11136. }
  11137. }
  11138. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  11139. #endif /* MBEDTLS_DEBUG_C */
  11140. /*
  11141. Amalgamated build undefines
  11142. */
  11143. #undef ADD
  11144. #undef BC
  11145. #undef BEFORE_COLON
  11146. #undef F
  11147. #undef F0
  11148. #undef F1
  11149. #undef F2
  11150. #undef F3
  11151. #undef F4
  11152. #undef F5
  11153. #undef FSb
  11154. #undef K
  11155. #undef KK
  11156. #undef P
  11157. #undef R
  11158. #undef ROTR
  11159. #undef S
  11160. #undef S0
  11161. #undef S1
  11162. #undef S2
  11163. #undef S3
  11164. #undef SAFE_SNPRINTF
  11165. #undef SHR
  11166. #undef close
  11167. #undef read
  11168. #undef supported_init
  11169. #undef write
  11170. /********* Start of file library/des.c ************/
  11171. /*
  11172. * FIPS-46-3 compliant Triple-DES implementation
  11173. *
  11174. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  11175. * SPDX-License-Identifier: Apache-2.0
  11176. *
  11177. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11178. * not use this file except in compliance with the License.
  11179. * You may obtain a copy of the License at
  11180. *
  11181. * http://www.apache.org/licenses/LICENSE-2.0
  11182. *
  11183. * Unless required by applicable law or agreed to in writing, software
  11184. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11185. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11186. * See the License for the specific language governing permissions and
  11187. * limitations under the License.
  11188. *
  11189. * This file is part of mbed TLS (https://tls.mbed.org)
  11190. */
  11191. /*
  11192. * DES, on which TDES is based, was originally designed by Horst Feistel
  11193. * at IBM in 1974, and was adopted as a standard by NIST (formerly NBS).
  11194. *
  11195. * http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
  11196. */
  11197. #if !defined(MBEDTLS_CONFIG_FILE)
  11198. #else
  11199. #endif
  11200. #if defined(MBEDTLS_DES_C)
  11201. #include <string.h>
  11202. #if defined(MBEDTLS_SELF_TEST)
  11203. #if defined(MBEDTLS_PLATFORM_C)
  11204. #else
  11205. #include <stdio.h>
  11206. #define mbedtls_printf printf
  11207. #endif /* MBEDTLS_PLATFORM_C */
  11208. #endif /* MBEDTLS_SELF_TEST */
  11209. #if !defined(MBEDTLS_DES_ALT)
  11210. /* Implementation that should never be optimized out by the compiler */
  11211. /* zeroize was here */
  11212. /*
  11213. * 32-bit integer manipulation macros (big endian)
  11214. */
  11215. #ifndef GET_UINT32_BE
  11216. #define GET_UINT32_BE(n,b,i) \
  11217. { \
  11218. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  11219. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  11220. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  11221. | ( (uint32_t) (b)[(i) + 3] ); \
  11222. }
  11223. #endif
  11224. #ifndef PUT_UINT32_BE
  11225. #define PUT_UINT32_BE(n,b,i) \
  11226. { \
  11227. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  11228. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  11229. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  11230. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  11231. }
  11232. #endif
  11233. /*
  11234. * Expanded DES S-boxes
  11235. */
  11236. static const uint32_t SB1[64] =
  11237. {
  11238. 0x01010400, 0x00000000, 0x00010000, 0x01010404,
  11239. 0x01010004, 0x00010404, 0x00000004, 0x00010000,
  11240. 0x00000400, 0x01010400, 0x01010404, 0x00000400,
  11241. 0x01000404, 0x01010004, 0x01000000, 0x00000004,
  11242. 0x00000404, 0x01000400, 0x01000400, 0x00010400,
  11243. 0x00010400, 0x01010000, 0x01010000, 0x01000404,
  11244. 0x00010004, 0x01000004, 0x01000004, 0x00010004,
  11245. 0x00000000, 0x00000404, 0x00010404, 0x01000000,
  11246. 0x00010000, 0x01010404, 0x00000004, 0x01010000,
  11247. 0x01010400, 0x01000000, 0x01000000, 0x00000400,
  11248. 0x01010004, 0x00010000, 0x00010400, 0x01000004,
  11249. 0x00000400, 0x00000004, 0x01000404, 0x00010404,
  11250. 0x01010404, 0x00010004, 0x01010000, 0x01000404,
  11251. 0x01000004, 0x00000404, 0x00010404, 0x01010400,
  11252. 0x00000404, 0x01000400, 0x01000400, 0x00000000,
  11253. 0x00010004, 0x00010400, 0x00000000, 0x01010004
  11254. };
  11255. static const uint32_t SB2[64] =
  11256. {
  11257. 0x80108020, 0x80008000, 0x00008000, 0x00108020,
  11258. 0x00100000, 0x00000020, 0x80100020, 0x80008020,
  11259. 0x80000020, 0x80108020, 0x80108000, 0x80000000,
  11260. 0x80008000, 0x00100000, 0x00000020, 0x80100020,
  11261. 0x00108000, 0x00100020, 0x80008020, 0x00000000,
  11262. 0x80000000, 0x00008000, 0x00108020, 0x80100000,
  11263. 0x00100020, 0x80000020, 0x00000000, 0x00108000,
  11264. 0x00008020, 0x80108000, 0x80100000, 0x00008020,
  11265. 0x00000000, 0x00108020, 0x80100020, 0x00100000,
  11266. 0x80008020, 0x80100000, 0x80108000, 0x00008000,
  11267. 0x80100000, 0x80008000, 0x00000020, 0x80108020,
  11268. 0x00108020, 0x00000020, 0x00008000, 0x80000000,
  11269. 0x00008020, 0x80108000, 0x00100000, 0x80000020,
  11270. 0x00100020, 0x80008020, 0x80000020, 0x00100020,
  11271. 0x00108000, 0x00000000, 0x80008000, 0x00008020,
  11272. 0x80000000, 0x80100020, 0x80108020, 0x00108000
  11273. };
  11274. static const uint32_t SB3[64] =
  11275. {
  11276. 0x00000208, 0x08020200, 0x00000000, 0x08020008,
  11277. 0x08000200, 0x00000000, 0x00020208, 0x08000200,
  11278. 0x00020008, 0x08000008, 0x08000008, 0x00020000,
  11279. 0x08020208, 0x00020008, 0x08020000, 0x00000208,
  11280. 0x08000000, 0x00000008, 0x08020200, 0x00000200,
  11281. 0x00020200, 0x08020000, 0x08020008, 0x00020208,
  11282. 0x08000208, 0x00020200, 0x00020000, 0x08000208,
  11283. 0x00000008, 0x08020208, 0x00000200, 0x08000000,
  11284. 0x08020200, 0x08000000, 0x00020008, 0x00000208,
  11285. 0x00020000, 0x08020200, 0x08000200, 0x00000000,
  11286. 0x00000200, 0x00020008, 0x08020208, 0x08000200,
  11287. 0x08000008, 0x00000200, 0x00000000, 0x08020008,
  11288. 0x08000208, 0x00020000, 0x08000000, 0x08020208,
  11289. 0x00000008, 0x00020208, 0x00020200, 0x08000008,
  11290. 0x08020000, 0x08000208, 0x00000208, 0x08020000,
  11291. 0x00020208, 0x00000008, 0x08020008, 0x00020200
  11292. };
  11293. static const uint32_t SB4[64] =
  11294. {
  11295. 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  11296. 0x00802080, 0x00800081, 0x00800001, 0x00002001,
  11297. 0x00000000, 0x00802000, 0x00802000, 0x00802081,
  11298. 0x00000081, 0x00000000, 0x00800080, 0x00800001,
  11299. 0x00000001, 0x00002000, 0x00800000, 0x00802001,
  11300. 0x00000080, 0x00800000, 0x00002001, 0x00002080,
  11301. 0x00800081, 0x00000001, 0x00002080, 0x00800080,
  11302. 0x00002000, 0x00802080, 0x00802081, 0x00000081,
  11303. 0x00800080, 0x00800001, 0x00802000, 0x00802081,
  11304. 0x00000081, 0x00000000, 0x00000000, 0x00802000,
  11305. 0x00002080, 0x00800080, 0x00800081, 0x00000001,
  11306. 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  11307. 0x00802081, 0x00000081, 0x00000001, 0x00002000,
  11308. 0x00800001, 0x00002001, 0x00802080, 0x00800081,
  11309. 0x00002001, 0x00002080, 0x00800000, 0x00802001,
  11310. 0x00000080, 0x00800000, 0x00002000, 0x00802080
  11311. };
  11312. static const uint32_t SB5[64] =
  11313. {
  11314. 0x00000100, 0x02080100, 0x02080000, 0x42000100,
  11315. 0x00080000, 0x00000100, 0x40000000, 0x02080000,
  11316. 0x40080100, 0x00080000, 0x02000100, 0x40080100,
  11317. 0x42000100, 0x42080000, 0x00080100, 0x40000000,
  11318. 0x02000000, 0x40080000, 0x40080000, 0x00000000,
  11319. 0x40000100, 0x42080100, 0x42080100, 0x02000100,
  11320. 0x42080000, 0x40000100, 0x00000000, 0x42000000,
  11321. 0x02080100, 0x02000000, 0x42000000, 0x00080100,
  11322. 0x00080000, 0x42000100, 0x00000100, 0x02000000,
  11323. 0x40000000, 0x02080000, 0x42000100, 0x40080100,
  11324. 0x02000100, 0x40000000, 0x42080000, 0x02080100,
  11325. 0x40080100, 0x00000100, 0x02000000, 0x42080000,
  11326. 0x42080100, 0x00080100, 0x42000000, 0x42080100,
  11327. 0x02080000, 0x00000000, 0x40080000, 0x42000000,
  11328. 0x00080100, 0x02000100, 0x40000100, 0x00080000,
  11329. 0x00000000, 0x40080000, 0x02080100, 0x40000100
  11330. };
  11331. static const uint32_t SB6[64] =
  11332. {
  11333. 0x20000010, 0x20400000, 0x00004000, 0x20404010,
  11334. 0x20400000, 0x00000010, 0x20404010, 0x00400000,
  11335. 0x20004000, 0x00404010, 0x00400000, 0x20000010,
  11336. 0x00400010, 0x20004000, 0x20000000, 0x00004010,
  11337. 0x00000000, 0x00400010, 0x20004010, 0x00004000,
  11338. 0x00404000, 0x20004010, 0x00000010, 0x20400010,
  11339. 0x20400010, 0x00000000, 0x00404010, 0x20404000,
  11340. 0x00004010, 0x00404000, 0x20404000, 0x20000000,
  11341. 0x20004000, 0x00000010, 0x20400010, 0x00404000,
  11342. 0x20404010, 0x00400000, 0x00004010, 0x20000010,
  11343. 0x00400000, 0x20004000, 0x20000000, 0x00004010,
  11344. 0x20000010, 0x20404010, 0x00404000, 0x20400000,
  11345. 0x00404010, 0x20404000, 0x00000000, 0x20400010,
  11346. 0x00000010, 0x00004000, 0x20400000, 0x00404010,
  11347. 0x00004000, 0x00400010, 0x20004010, 0x00000000,
  11348. 0x20404000, 0x20000000, 0x00400010, 0x20004010
  11349. };
  11350. static const uint32_t SB7[64] =
  11351. {
  11352. 0x00200000, 0x04200002, 0x04000802, 0x00000000,
  11353. 0x00000800, 0x04000802, 0x00200802, 0x04200800,
  11354. 0x04200802, 0x00200000, 0x00000000, 0x04000002,
  11355. 0x00000002, 0x04000000, 0x04200002, 0x00000802,
  11356. 0x04000800, 0x00200802, 0x00200002, 0x04000800,
  11357. 0x04000002, 0x04200000, 0x04200800, 0x00200002,
  11358. 0x04200000, 0x00000800, 0x00000802, 0x04200802,
  11359. 0x00200800, 0x00000002, 0x04000000, 0x00200800,
  11360. 0x04000000, 0x00200800, 0x00200000, 0x04000802,
  11361. 0x04000802, 0x04200002, 0x04200002, 0x00000002,
  11362. 0x00200002, 0x04000000, 0x04000800, 0x00200000,
  11363. 0x04200800, 0x00000802, 0x00200802, 0x04200800,
  11364. 0x00000802, 0x04000002, 0x04200802, 0x04200000,
  11365. 0x00200800, 0x00000000, 0x00000002, 0x04200802,
  11366. 0x00000000, 0x00200802, 0x04200000, 0x00000800,
  11367. 0x04000002, 0x04000800, 0x00000800, 0x00200002
  11368. };
  11369. static const uint32_t SB8[64] =
  11370. {
  11371. 0x10001040, 0x00001000, 0x00040000, 0x10041040,
  11372. 0x10000000, 0x10001040, 0x00000040, 0x10000000,
  11373. 0x00040040, 0x10040000, 0x10041040, 0x00041000,
  11374. 0x10041000, 0x00041040, 0x00001000, 0x00000040,
  11375. 0x10040000, 0x10000040, 0x10001000, 0x00001040,
  11376. 0x00041000, 0x00040040, 0x10040040, 0x10041000,
  11377. 0x00001040, 0x00000000, 0x00000000, 0x10040040,
  11378. 0x10000040, 0x10001000, 0x00041040, 0x00040000,
  11379. 0x00041040, 0x00040000, 0x10041000, 0x00001000,
  11380. 0x00000040, 0x10040040, 0x00001000, 0x00041040,
  11381. 0x10001000, 0x00000040, 0x10000040, 0x10040000,
  11382. 0x10040040, 0x10000000, 0x00040000, 0x10001040,
  11383. 0x00000000, 0x10041040, 0x00040040, 0x10000040,
  11384. 0x10040000, 0x10001000, 0x10001040, 0x00000000,
  11385. 0x10041040, 0x00041000, 0x00041000, 0x00001040,
  11386. 0x00001040, 0x00040040, 0x10000000, 0x10041000
  11387. };
  11388. /*
  11389. * PC1: left and right halves bit-swap
  11390. */
  11391. static const uint32_t LHs[16] =
  11392. {
  11393. 0x00000000, 0x00000001, 0x00000100, 0x00000101,
  11394. 0x00010000, 0x00010001, 0x00010100, 0x00010101,
  11395. 0x01000000, 0x01000001, 0x01000100, 0x01000101,
  11396. 0x01010000, 0x01010001, 0x01010100, 0x01010101
  11397. };
  11398. static const uint32_t RHs[16] =
  11399. {
  11400. 0x00000000, 0x01000000, 0x00010000, 0x01010000,
  11401. 0x00000100, 0x01000100, 0x00010100, 0x01010100,
  11402. 0x00000001, 0x01000001, 0x00010001, 0x01010001,
  11403. 0x00000101, 0x01000101, 0x00010101, 0x01010101,
  11404. };
  11405. /*
  11406. * Initial Permutation macro
  11407. */
  11408. #define DES_IP(X,Y) \
  11409. { \
  11410. T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \
  11411. T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \
  11412. T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \
  11413. T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \
  11414. Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF; \
  11415. T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T; \
  11416. X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF; \
  11417. }
  11418. /*
  11419. * Final Permutation macro
  11420. */
  11421. #define DES_FP(X,Y) \
  11422. { \
  11423. X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF; \
  11424. T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T; \
  11425. Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF; \
  11426. T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \
  11427. T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \
  11428. T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \
  11429. T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \
  11430. }
  11431. /*
  11432. * DES round macro
  11433. */
  11434. #define DES_ROUND(X,Y) \
  11435. { \
  11436. T = *SK++ ^ X; \
  11437. Y ^= SB8[ (T ) & 0x3F ] ^ \
  11438. SB6[ (T >> 8) & 0x3F ] ^ \
  11439. SB4[ (T >> 16) & 0x3F ] ^ \
  11440. SB2[ (T >> 24) & 0x3F ]; \
  11441. \
  11442. T = *SK++ ^ ((X << 28) | (X >> 4)); \
  11443. Y ^= SB7[ (T ) & 0x3F ] ^ \
  11444. SB5[ (T >> 8) & 0x3F ] ^ \
  11445. SB3[ (T >> 16) & 0x3F ] ^ \
  11446. SB1[ (T >> 24) & 0x3F ]; \
  11447. }
  11448. #define SWAP(a,b) { uint32_t t = a; a = b; b = t; t = 0; }
  11449. void mbedtls_des_init( mbedtls_des_context *ctx )
  11450. {
  11451. memset( ctx, 0, sizeof( mbedtls_des_context ) );
  11452. }
  11453. void mbedtls_des_free( mbedtls_des_context *ctx )
  11454. {
  11455. if( ctx == NULL )
  11456. return;
  11457. mbedtls_zeroize( ctx, sizeof( mbedtls_des_context ) );
  11458. }
  11459. void mbedtls_des3_init( mbedtls_des3_context *ctx )
  11460. {
  11461. memset( ctx, 0, sizeof( mbedtls_des3_context ) );
  11462. }
  11463. void mbedtls_des3_free( mbedtls_des3_context *ctx )
  11464. {
  11465. if( ctx == NULL )
  11466. return;
  11467. mbedtls_zeroize( ctx, sizeof( mbedtls_des3_context ) );
  11468. }
  11469. static const unsigned char odd_parity_table[128] = { 1, 2, 4, 7, 8,
  11470. 11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44,
  11471. 47, 49, 50, 52, 55, 56, 59, 61, 62, 64, 67, 69, 70, 73, 74, 76, 79, 81,
  11472. 82, 84, 87, 88, 91, 93, 94, 97, 98, 100, 103, 104, 107, 109, 110, 112,
  11473. 115, 117, 118, 121, 122, 124, 127, 128, 131, 133, 134, 137, 138, 140,
  11474. 143, 145, 146, 148, 151, 152, 155, 157, 158, 161, 162, 164, 167, 168,
  11475. 171, 173, 174, 176, 179, 181, 182, 185, 186, 188, 191, 193, 194, 196,
  11476. 199, 200, 203, 205, 206, 208, 211, 213, 214, 217, 218, 220, 223, 224,
  11477. 227, 229, 230, 233, 234, 236, 239, 241, 242, 244, 247, 248, 251, 253,
  11478. 254 };
  11479. void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  11480. {
  11481. int i;
  11482. for( i = 0; i < MBEDTLS_DES_KEY_SIZE; i++ )
  11483. key[i] = odd_parity_table[key[i] / 2];
  11484. }
  11485. /*
  11486. * Check the given key's parity, returns 1 on failure, 0 on SUCCESS
  11487. */
  11488. int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  11489. {
  11490. int i;
  11491. for( i = 0; i < MBEDTLS_DES_KEY_SIZE; i++ )
  11492. if( key[i] != odd_parity_table[key[i] / 2] )
  11493. return( 1 );
  11494. return( 0 );
  11495. }
  11496. /*
  11497. * Table of weak and semi-weak keys
  11498. *
  11499. * Source: http://en.wikipedia.org/wiki/Weak_key
  11500. *
  11501. * Weak:
  11502. * Alternating ones + zeros (0x0101010101010101)
  11503. * Alternating 'F' + 'E' (0xFEFEFEFEFEFEFEFE)
  11504. * '0xE0E0E0E0F1F1F1F1'
  11505. * '0x1F1F1F1F0E0E0E0E'
  11506. *
  11507. * Semi-weak:
  11508. * 0x011F011F010E010E and 0x1F011F010E010E01
  11509. * 0x01E001E001F101F1 and 0xE001E001F101F101
  11510. * 0x01FE01FE01FE01FE and 0xFE01FE01FE01FE01
  11511. * 0x1FE01FE00EF10EF1 and 0xE01FE01FF10EF10E
  11512. * 0x1FFE1FFE0EFE0EFE and 0xFE1FFE1FFE0EFE0E
  11513. * 0xE0FEE0FEF1FEF1FE and 0xFEE0FEE0FEF1FEF1
  11514. *
  11515. */
  11516. #define WEAK_KEY_COUNT 16
  11517. static const unsigned char weak_key_table[WEAK_KEY_COUNT][MBEDTLS_DES_KEY_SIZE] =
  11518. {
  11519. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  11520. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  11521. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E },
  11522. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  11523. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  11524. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  11525. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  11526. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  11527. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  11528. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  11529. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  11530. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  11531. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  11532. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  11533. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  11534. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  11535. };
  11536. int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  11537. {
  11538. int i;
  11539. for( i = 0; i < WEAK_KEY_COUNT; i++ )
  11540. if( memcmp( weak_key_table[i], key, MBEDTLS_DES_KEY_SIZE) == 0 )
  11541. return( 1 );
  11542. return( 0 );
  11543. }
  11544. #if !defined(MBEDTLS_DES_SETKEY_ALT)
  11545. void mbedtls_des_setkey( uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  11546. {
  11547. int i;
  11548. uint32_t X, Y, T;
  11549. GET_UINT32_BE( X, key, 0 );
  11550. GET_UINT32_BE( Y, key, 4 );
  11551. /*
  11552. * Permuted Choice 1
  11553. */
  11554. T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4);
  11555. T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T );
  11556. X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2)
  11557. | (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] )
  11558. | (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6)
  11559. | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4);
  11560. Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2)
  11561. | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] )
  11562. | (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6)
  11563. | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4);
  11564. X &= 0x0FFFFFFF;
  11565. Y &= 0x0FFFFFFF;
  11566. /*
  11567. * calculate subkeys
  11568. */
  11569. for( i = 0; i < 16; i++ )
  11570. {
  11571. if( i < 2 || i == 8 || i == 15 )
  11572. {
  11573. X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF;
  11574. Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF;
  11575. }
  11576. else
  11577. {
  11578. X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF;
  11579. Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF;
  11580. }
  11581. *SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000)
  11582. | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000)
  11583. | ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000)
  11584. | ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000)
  11585. | ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000)
  11586. | ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000)
  11587. | ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400)
  11588. | ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100)
  11589. | ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010)
  11590. | ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004)
  11591. | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001);
  11592. *SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000)
  11593. | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000)
  11594. | ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000)
  11595. | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000)
  11596. | ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000)
  11597. | ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000)
  11598. | ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000)
  11599. | ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400)
  11600. | ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100)
  11601. | ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011)
  11602. | ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002);
  11603. }
  11604. }
  11605. #endif /* !MBEDTLS_DES_SETKEY_ALT */
  11606. /*
  11607. * DES key schedule (56-bit, encryption)
  11608. */
  11609. int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  11610. {
  11611. mbedtls_des_setkey( ctx->sk, key );
  11612. return( 0 );
  11613. }
  11614. /*
  11615. * DES key schedule (56-bit, decryption)
  11616. */
  11617. int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  11618. {
  11619. int i;
  11620. mbedtls_des_setkey( ctx->sk, key );
  11621. for( i = 0; i < 16; i += 2 )
  11622. {
  11623. SWAP( ctx->sk[i ], ctx->sk[30 - i] );
  11624. SWAP( ctx->sk[i + 1], ctx->sk[31 - i] );
  11625. }
  11626. return( 0 );
  11627. }
  11628. static void des3_set2key( uint32_t esk[96],
  11629. uint32_t dsk[96],
  11630. const unsigned char key[MBEDTLS_DES_KEY_SIZE*2] )
  11631. {
  11632. int i;
  11633. mbedtls_des_setkey( esk, key );
  11634. mbedtls_des_setkey( dsk + 32, key + 8 );
  11635. for( i = 0; i < 32; i += 2 )
  11636. {
  11637. dsk[i ] = esk[30 - i];
  11638. dsk[i + 1] = esk[31 - i];
  11639. esk[i + 32] = dsk[62 - i];
  11640. esk[i + 33] = dsk[63 - i];
  11641. esk[i + 64] = esk[i ];
  11642. esk[i + 65] = esk[i + 1];
  11643. dsk[i + 64] = dsk[i ];
  11644. dsk[i + 65] = dsk[i + 1];
  11645. }
  11646. }
  11647. /*
  11648. * Triple-DES key schedule (112-bit, encryption)
  11649. */
  11650. int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
  11651. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] )
  11652. {
  11653. uint32_t sk[96];
  11654. des3_set2key( ctx->sk, sk, key );
  11655. mbedtls_zeroize( sk, sizeof( sk ) );
  11656. return( 0 );
  11657. }
  11658. /*
  11659. * Triple-DES key schedule (112-bit, decryption)
  11660. */
  11661. int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
  11662. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] )
  11663. {
  11664. uint32_t sk[96];
  11665. des3_set2key( sk, ctx->sk, key );
  11666. mbedtls_zeroize( sk, sizeof( sk ) );
  11667. return( 0 );
  11668. }
  11669. static void des3_set3key( uint32_t esk[96],
  11670. uint32_t dsk[96],
  11671. const unsigned char key[24] )
  11672. {
  11673. int i;
  11674. mbedtls_des_setkey( esk, key );
  11675. mbedtls_des_setkey( dsk + 32, key + 8 );
  11676. mbedtls_des_setkey( esk + 64, key + 16 );
  11677. for( i = 0; i < 32; i += 2 )
  11678. {
  11679. dsk[i ] = esk[94 - i];
  11680. dsk[i + 1] = esk[95 - i];
  11681. esk[i + 32] = dsk[62 - i];
  11682. esk[i + 33] = dsk[63 - i];
  11683. dsk[i + 64] = esk[30 - i];
  11684. dsk[i + 65] = esk[31 - i];
  11685. }
  11686. }
  11687. /*
  11688. * Triple-DES key schedule (168-bit, encryption)
  11689. */
  11690. int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
  11691. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] )
  11692. {
  11693. uint32_t sk[96];
  11694. des3_set3key( ctx->sk, sk, key );
  11695. mbedtls_zeroize( sk, sizeof( sk ) );
  11696. return( 0 );
  11697. }
  11698. /*
  11699. * Triple-DES key schedule (168-bit, decryption)
  11700. */
  11701. int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
  11702. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] )
  11703. {
  11704. uint32_t sk[96];
  11705. des3_set3key( sk, ctx->sk, key );
  11706. mbedtls_zeroize( sk, sizeof( sk ) );
  11707. return( 0 );
  11708. }
  11709. /*
  11710. * DES-ECB block encryption/decryption
  11711. */
  11712. #if !defined(MBEDTLS_DES_CRYPT_ECB_ALT)
  11713. int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
  11714. const unsigned char input[8],
  11715. unsigned char output[8] )
  11716. {
  11717. int i;
  11718. uint32_t X, Y, T, *SK;
  11719. SK = ctx->sk;
  11720. GET_UINT32_BE( X, input, 0 );
  11721. GET_UINT32_BE( Y, input, 4 );
  11722. DES_IP( X, Y );
  11723. for( i = 0; i < 8; i++ )
  11724. {
  11725. DES_ROUND( Y, X );
  11726. DES_ROUND( X, Y );
  11727. }
  11728. DES_FP( Y, X );
  11729. PUT_UINT32_BE( Y, output, 0 );
  11730. PUT_UINT32_BE( X, output, 4 );
  11731. return( 0 );
  11732. }
  11733. #endif /* !MBEDTLS_DES_CRYPT_ECB_ALT */
  11734. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  11735. /*
  11736. * DES-CBC buffer encryption/decryption
  11737. */
  11738. int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
  11739. int mode,
  11740. size_t length,
  11741. unsigned char iv[8],
  11742. const unsigned char *input,
  11743. unsigned char *output )
  11744. {
  11745. int i;
  11746. unsigned char temp[8];
  11747. if( length % 8 )
  11748. return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH );
  11749. if( mode == MBEDTLS_DES_ENCRYPT )
  11750. {
  11751. while( length > 0 )
  11752. {
  11753. for( i = 0; i < 8; i++ )
  11754. output[i] = (unsigned char)( input[i] ^ iv[i] );
  11755. mbedtls_des_crypt_ecb( ctx, output, output );
  11756. memcpy( iv, output, 8 );
  11757. input += 8;
  11758. output += 8;
  11759. length -= 8;
  11760. }
  11761. }
  11762. else /* MBEDTLS_DES_DECRYPT */
  11763. {
  11764. while( length > 0 )
  11765. {
  11766. memcpy( temp, input, 8 );
  11767. mbedtls_des_crypt_ecb( ctx, input, output );
  11768. for( i = 0; i < 8; i++ )
  11769. output[i] = (unsigned char)( output[i] ^ iv[i] );
  11770. memcpy( iv, temp, 8 );
  11771. input += 8;
  11772. output += 8;
  11773. length -= 8;
  11774. }
  11775. }
  11776. return( 0 );
  11777. }
  11778. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  11779. /*
  11780. * 3DES-ECB block encryption/decryption
  11781. */
  11782. #if !defined(MBEDTLS_DES3_CRYPT_ECB_ALT)
  11783. int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
  11784. const unsigned char input[8],
  11785. unsigned char output[8] )
  11786. {
  11787. int i;
  11788. uint32_t X, Y, T, *SK;
  11789. SK = ctx->sk;
  11790. GET_UINT32_BE( X, input, 0 );
  11791. GET_UINT32_BE( Y, input, 4 );
  11792. DES_IP( X, Y );
  11793. for( i = 0; i < 8; i++ )
  11794. {
  11795. DES_ROUND( Y, X );
  11796. DES_ROUND( X, Y );
  11797. }
  11798. for( i = 0; i < 8; i++ )
  11799. {
  11800. DES_ROUND( X, Y );
  11801. DES_ROUND( Y, X );
  11802. }
  11803. for( i = 0; i < 8; i++ )
  11804. {
  11805. DES_ROUND( Y, X );
  11806. DES_ROUND( X, Y );
  11807. }
  11808. DES_FP( Y, X );
  11809. PUT_UINT32_BE( Y, output, 0 );
  11810. PUT_UINT32_BE( X, output, 4 );
  11811. return( 0 );
  11812. }
  11813. #endif /* !MBEDTLS_DES3_CRYPT_ECB_ALT */
  11814. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  11815. /*
  11816. * 3DES-CBC buffer encryption/decryption
  11817. */
  11818. int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
  11819. int mode,
  11820. size_t length,
  11821. unsigned char iv[8],
  11822. const unsigned char *input,
  11823. unsigned char *output )
  11824. {
  11825. int i;
  11826. unsigned char temp[8];
  11827. if( length % 8 )
  11828. return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH );
  11829. if( mode == MBEDTLS_DES_ENCRYPT )
  11830. {
  11831. while( length > 0 )
  11832. {
  11833. for( i = 0; i < 8; i++ )
  11834. output[i] = (unsigned char)( input[i] ^ iv[i] );
  11835. mbedtls_des3_crypt_ecb( ctx, output, output );
  11836. memcpy( iv, output, 8 );
  11837. input += 8;
  11838. output += 8;
  11839. length -= 8;
  11840. }
  11841. }
  11842. else /* MBEDTLS_DES_DECRYPT */
  11843. {
  11844. while( length > 0 )
  11845. {
  11846. memcpy( temp, input, 8 );
  11847. mbedtls_des3_crypt_ecb( ctx, input, output );
  11848. for( i = 0; i < 8; i++ )
  11849. output[i] = (unsigned char)( output[i] ^ iv[i] );
  11850. memcpy( iv, temp, 8 );
  11851. input += 8;
  11852. output += 8;
  11853. length -= 8;
  11854. }
  11855. }
  11856. return( 0 );
  11857. }
  11858. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  11859. #endif /* !MBEDTLS_DES_ALT */
  11860. #if defined(MBEDTLS_SELF_TEST)
  11861. /*
  11862. * DES and 3DES test vectors from:
  11863. *
  11864. * http://csrc.nist.gov/groups/STM/cavp/documents/des/tripledes-vectors.zip
  11865. */
  11866. static const unsigned char des3_test_keys[24] =
  11867. {
  11868. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  11869. 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01,
  11870. 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23
  11871. };
  11872. static const unsigned char des3_test_buf[8] =
  11873. {
  11874. 0x4E, 0x6F, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74
  11875. };
  11876. static const unsigned char des3_test_ecb_dec[3][8] =
  11877. {
  11878. { 0xCD, 0xD6, 0x4F, 0x2F, 0x94, 0x27, 0xC1, 0x5D },
  11879. { 0x69, 0x96, 0xC8, 0xFA, 0x47, 0xA2, 0xAB, 0xEB },
  11880. { 0x83, 0x25, 0x39, 0x76, 0x44, 0x09, 0x1A, 0x0A }
  11881. };
  11882. static const unsigned char des3_test_ecb_enc[3][8] =
  11883. {
  11884. { 0x6A, 0x2A, 0x19, 0xF4, 0x1E, 0xCA, 0x85, 0x4B },
  11885. { 0x03, 0xE6, 0x9F, 0x5B, 0xFA, 0x58, 0xEB, 0x42 },
  11886. { 0xDD, 0x17, 0xE8, 0xB8, 0xB4, 0x37, 0xD2, 0x32 }
  11887. };
  11888. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  11889. static const unsigned char des3_test_iv[8] =
  11890. {
  11891. 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF,
  11892. };
  11893. static const unsigned char des3_test_cbc_dec[3][8] =
  11894. {
  11895. { 0x12, 0x9F, 0x40, 0xB9, 0xD2, 0x00, 0x56, 0xB3 },
  11896. { 0x47, 0x0E, 0xFC, 0x9A, 0x6B, 0x8E, 0xE3, 0x93 },
  11897. { 0xC5, 0xCE, 0xCF, 0x63, 0xEC, 0xEC, 0x51, 0x4C }
  11898. };
  11899. static const unsigned char des3_test_cbc_enc[3][8] =
  11900. {
  11901. { 0x54, 0xF1, 0x5A, 0xF6, 0xEB, 0xE3, 0xA4, 0xB4 },
  11902. { 0x35, 0x76, 0x11, 0x56, 0x5F, 0xA1, 0x8E, 0x4D },
  11903. { 0xCB, 0x19, 0x1F, 0x85, 0xD1, 0xED, 0x84, 0x39 }
  11904. };
  11905. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  11906. /*
  11907. * Checkup routine
  11908. */
  11909. int mbedtls_des_self_test( int verbose )
  11910. {
  11911. int i, j, u, v, ret = 0;
  11912. mbedtls_des_context ctx;
  11913. mbedtls_des3_context ctx3;
  11914. unsigned char buf[8];
  11915. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  11916. unsigned char prv[8];
  11917. unsigned char iv[8];
  11918. #endif
  11919. mbedtls_des_init( &ctx );
  11920. mbedtls_des3_init( &ctx3 );
  11921. /*
  11922. * ECB mode
  11923. */
  11924. for( i = 0; i < 6; i++ )
  11925. {
  11926. u = i >> 1;
  11927. v = i & 1;
  11928. if( verbose != 0 )
  11929. mbedtls_printf( " DES%c-ECB-%3d (%s): ",
  11930. ( u == 0 ) ? ' ' : '3', 56 + u * 56,
  11931. ( v == MBEDTLS_DES_DECRYPT ) ? "dec" : "enc" );
  11932. memcpy( buf, des3_test_buf, 8 );
  11933. switch( i )
  11934. {
  11935. case 0:
  11936. mbedtls_des_setkey_dec( &ctx, des3_test_keys );
  11937. break;
  11938. case 1:
  11939. mbedtls_des_setkey_enc( &ctx, des3_test_keys );
  11940. break;
  11941. case 2:
  11942. mbedtls_des3_set2key_dec( &ctx3, des3_test_keys );
  11943. break;
  11944. case 3:
  11945. mbedtls_des3_set2key_enc( &ctx3, des3_test_keys );
  11946. break;
  11947. case 4:
  11948. mbedtls_des3_set3key_dec( &ctx3, des3_test_keys );
  11949. break;
  11950. case 5:
  11951. mbedtls_des3_set3key_enc( &ctx3, des3_test_keys );
  11952. break;
  11953. default:
  11954. return( 1 );
  11955. }
  11956. for( j = 0; j < 10000; j++ )
  11957. {
  11958. if( u == 0 )
  11959. mbedtls_des_crypt_ecb( &ctx, buf, buf );
  11960. else
  11961. mbedtls_des3_crypt_ecb( &ctx3, buf, buf );
  11962. }
  11963. if( ( v == MBEDTLS_DES_DECRYPT &&
  11964. memcmp( buf, des3_test_ecb_dec[u], 8 ) != 0 ) ||
  11965. ( v != MBEDTLS_DES_DECRYPT &&
  11966. memcmp( buf, des3_test_ecb_enc[u], 8 ) != 0 ) )
  11967. {
  11968. if( verbose != 0 )
  11969. mbedtls_printf( "failed\n" );
  11970. ret = 1;
  11971. goto exit;
  11972. }
  11973. if( verbose != 0 )
  11974. mbedtls_printf( "passed\n" );
  11975. }
  11976. if( verbose != 0 )
  11977. mbedtls_printf( "\n" );
  11978. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  11979. /*
  11980. * CBC mode
  11981. */
  11982. for( i = 0; i < 6; i++ )
  11983. {
  11984. u = i >> 1;
  11985. v = i & 1;
  11986. if( verbose != 0 )
  11987. mbedtls_printf( " DES%c-CBC-%3d (%s): ",
  11988. ( u == 0 ) ? ' ' : '3', 56 + u * 56,
  11989. ( v == MBEDTLS_DES_DECRYPT ) ? "dec" : "enc" );
  11990. memcpy( iv, des3_test_iv, 8 );
  11991. memcpy( prv, des3_test_iv, 8 );
  11992. memcpy( buf, des3_test_buf, 8 );
  11993. switch( i )
  11994. {
  11995. case 0:
  11996. mbedtls_des_setkey_dec( &ctx, des3_test_keys );
  11997. break;
  11998. case 1:
  11999. mbedtls_des_setkey_enc( &ctx, des3_test_keys );
  12000. break;
  12001. case 2:
  12002. mbedtls_des3_set2key_dec( &ctx3, des3_test_keys );
  12003. break;
  12004. case 3:
  12005. mbedtls_des3_set2key_enc( &ctx3, des3_test_keys );
  12006. break;
  12007. case 4:
  12008. mbedtls_des3_set3key_dec( &ctx3, des3_test_keys );
  12009. break;
  12010. case 5:
  12011. mbedtls_des3_set3key_enc( &ctx3, des3_test_keys );
  12012. break;
  12013. default:
  12014. return( 1 );
  12015. }
  12016. if( v == MBEDTLS_DES_DECRYPT )
  12017. {
  12018. for( j = 0; j < 10000; j++ )
  12019. {
  12020. if( u == 0 )
  12021. mbedtls_des_crypt_cbc( &ctx, v, 8, iv, buf, buf );
  12022. else
  12023. mbedtls_des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf );
  12024. }
  12025. }
  12026. else
  12027. {
  12028. for( j = 0; j < 10000; j++ )
  12029. {
  12030. unsigned char tmp[8];
  12031. if( u == 0 )
  12032. mbedtls_des_crypt_cbc( &ctx, v, 8, iv, buf, buf );
  12033. else
  12034. mbedtls_des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf );
  12035. memcpy( tmp, prv, 8 );
  12036. memcpy( prv, buf, 8 );
  12037. memcpy( buf, tmp, 8 );
  12038. }
  12039. memcpy( buf, prv, 8 );
  12040. }
  12041. if( ( v == MBEDTLS_DES_DECRYPT &&
  12042. memcmp( buf, des3_test_cbc_dec[u], 8 ) != 0 ) ||
  12043. ( v != MBEDTLS_DES_DECRYPT &&
  12044. memcmp( buf, des3_test_cbc_enc[u], 8 ) != 0 ) )
  12045. {
  12046. if( verbose != 0 )
  12047. mbedtls_printf( "failed\n" );
  12048. ret = 1;
  12049. goto exit;
  12050. }
  12051. if( verbose != 0 )
  12052. mbedtls_printf( "passed\n" );
  12053. }
  12054. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  12055. if( verbose != 0 )
  12056. mbedtls_printf( "\n" );
  12057. exit:
  12058. mbedtls_des_free( &ctx );
  12059. mbedtls_des3_free( &ctx3 );
  12060. return( ret );
  12061. }
  12062. #endif /* MBEDTLS_SELF_TEST */
  12063. #endif /* MBEDTLS_DES_C */
  12064. /*
  12065. Amalgamated build undefines
  12066. */
  12067. #undef ADD
  12068. #undef BC
  12069. #undef BEFORE_COLON
  12070. #undef F
  12071. #undef F0
  12072. #undef F1
  12073. #undef F2
  12074. #undef F3
  12075. #undef F4
  12076. #undef F5
  12077. #undef FSb
  12078. #undef K
  12079. #undef KK
  12080. #undef P
  12081. #undef R
  12082. #undef ROTR
  12083. #undef S
  12084. #undef S0
  12085. #undef S1
  12086. #undef S2
  12087. #undef S3
  12088. #undef SAFE_SNPRINTF
  12089. #undef SHR
  12090. #undef close
  12091. #undef read
  12092. #undef supported_init
  12093. #undef write
  12094. /********* Start of file library/dhm.c ************/
  12095. /*
  12096. * Diffie-Hellman-Merkle key exchange
  12097. *
  12098. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  12099. * SPDX-License-Identifier: Apache-2.0
  12100. *
  12101. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  12102. * not use this file except in compliance with the License.
  12103. * You may obtain a copy of the License at
  12104. *
  12105. * http://www.apache.org/licenses/LICENSE-2.0
  12106. *
  12107. * Unless required by applicable law or agreed to in writing, software
  12108. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12109. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12110. * See the License for the specific language governing permissions and
  12111. * limitations under the License.
  12112. *
  12113. * This file is part of mbed TLS (https://tls.mbed.org)
  12114. */
  12115. /*
  12116. * The following sources were referenced in the design of this implementation
  12117. * of the Diffie-Hellman-Merkle algorithm:
  12118. *
  12119. * [1] Handbook of Applied Cryptography - 1997, Chapter 12
  12120. * Menezes, van Oorschot and Vanstone
  12121. *
  12122. */
  12123. #if !defined(MBEDTLS_CONFIG_FILE)
  12124. #else
  12125. #endif
  12126. #if defined(MBEDTLS_DHM_C)
  12127. #include <string.h>
  12128. #if defined(MBEDTLS_PEM_PARSE_C)
  12129. #endif
  12130. #if defined(MBEDTLS_ASN1_PARSE_C)
  12131. #endif
  12132. #if defined(MBEDTLS_PLATFORM_C)
  12133. #else
  12134. #include <stdlib.h>
  12135. #include <stdio.h>
  12136. #define mbedtls_printf printf
  12137. #define mbedtls_calloc calloc
  12138. #define mbedtls_free free
  12139. #endif
  12140. #if !defined(MBEDTLS_DHM_ALT)
  12141. /* Implementation that should never be optimized out by the compiler */
  12142. /* zeroize was here */
  12143. /*
  12144. * helper to validate the mbedtls_mpi size and import it
  12145. */
  12146. static int dhm_read_bignum( mbedtls_mpi *X,
  12147. unsigned char **p,
  12148. const unsigned char *end )
  12149. {
  12150. int ret, n;
  12151. if( end - *p < 2 )
  12152. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  12153. n = ( (*p)[0] << 8 ) | (*p)[1];
  12154. (*p) += 2;
  12155. if( (int)( end - *p ) < n )
  12156. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  12157. if( ( ret = mbedtls_mpi_read_binary( X, *p, n ) ) != 0 )
  12158. return( MBEDTLS_ERR_DHM_READ_PARAMS_FAILED + ret );
  12159. (*p) += n;
  12160. return( 0 );
  12161. }
  12162. /*
  12163. * Verify sanity of parameter with regards to P
  12164. *
  12165. * Parameter should be: 2 <= public_param <= P - 2
  12166. *
  12167. * This means that we need to return an error if
  12168. * public_param < 2 or public_param > P-2
  12169. *
  12170. * For more information on the attack, see:
  12171. * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
  12172. * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
  12173. */
  12174. static int dhm_check_range( const mbedtls_mpi *param, const mbedtls_mpi *P )
  12175. {
  12176. mbedtls_mpi L, U;
  12177. int ret = 0;
  12178. mbedtls_mpi_init( &L ); mbedtls_mpi_init( &U );
  12179. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &L, 2 ) );
  12180. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &U, P, 2 ) );
  12181. if( mbedtls_mpi_cmp_mpi( param, &L ) < 0 ||
  12182. mbedtls_mpi_cmp_mpi( param, &U ) > 0 )
  12183. {
  12184. ret = MBEDTLS_ERR_DHM_BAD_INPUT_DATA;
  12185. }
  12186. cleanup:
  12187. mbedtls_mpi_free( &L ); mbedtls_mpi_free( &U );
  12188. return( ret );
  12189. }
  12190. void mbedtls_dhm_init( mbedtls_dhm_context *ctx )
  12191. {
  12192. memset( ctx, 0, sizeof( mbedtls_dhm_context ) );
  12193. }
  12194. /*
  12195. * Parse the ServerKeyExchange parameters
  12196. */
  12197. int mbedtls_dhm_read_params( mbedtls_dhm_context *ctx,
  12198. unsigned char **p,
  12199. const unsigned char *end )
  12200. {
  12201. int ret;
  12202. if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
  12203. ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
  12204. ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
  12205. return( ret );
  12206. if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
  12207. return( ret );
  12208. ctx->len = mbedtls_mpi_size( &ctx->P );
  12209. return( 0 );
  12210. }
  12211. /*
  12212. * Setup and write the ServerKeyExchange parameters
  12213. */
  12214. int mbedtls_dhm_make_params( mbedtls_dhm_context *ctx, int x_size,
  12215. unsigned char *output, size_t *olen,
  12216. int (*f_rng)(void *, unsigned char *, size_t),
  12217. void *p_rng )
  12218. {
  12219. int ret, count = 0;
  12220. size_t n1, n2, n3;
  12221. unsigned char *p;
  12222. if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 )
  12223. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  12224. /*
  12225. * Generate X as large as possible ( < P )
  12226. */
  12227. do
  12228. {
  12229. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->X, x_size, f_rng, p_rng ) );
  12230. while( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
  12231. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->X, 1 ) );
  12232. if( count++ > 10 )
  12233. return( MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED );
  12234. }
  12235. while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
  12236. /*
  12237. * Calculate GX = G^X mod P
  12238. */
  12239. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
  12240. &ctx->P , &ctx->RP ) );
  12241. if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
  12242. return( ret );
  12243. /*
  12244. * export P, G, GX
  12245. */
  12246. #define DHM_MPI_EXPORT( X, n ) \
  12247. do { \
  12248. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( ( X ), \
  12249. p + 2, \
  12250. ( n ) ) ); \
  12251. *p++ = (unsigned char)( ( n ) >> 8 ); \
  12252. *p++ = (unsigned char)( ( n ) ); \
  12253. p += ( n ); \
  12254. } while( 0 )
  12255. n1 = mbedtls_mpi_size( &ctx->P );
  12256. n2 = mbedtls_mpi_size( &ctx->G );
  12257. n3 = mbedtls_mpi_size( &ctx->GX );
  12258. p = output;
  12259. DHM_MPI_EXPORT( &ctx->P , n1 );
  12260. DHM_MPI_EXPORT( &ctx->G , n2 );
  12261. DHM_MPI_EXPORT( &ctx->GX, n3 );
  12262. *olen = p - output;
  12263. ctx->len = n1;
  12264. cleanup:
  12265. if( ret != 0 )
  12266. return( MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED + ret );
  12267. return( 0 );
  12268. }
  12269. /*
  12270. * Set prime modulus and generator
  12271. */
  12272. int mbedtls_dhm_set_group( mbedtls_dhm_context *ctx,
  12273. const mbedtls_mpi *P,
  12274. const mbedtls_mpi *G )
  12275. {
  12276. int ret;
  12277. if( ctx == NULL || P == NULL || G == NULL )
  12278. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  12279. if( ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ||
  12280. ( ret = mbedtls_mpi_copy( &ctx->G, G ) ) != 0 )
  12281. {
  12282. return( MBEDTLS_ERR_DHM_SET_GROUP_FAILED + ret );
  12283. }
  12284. ctx->len = mbedtls_mpi_size( &ctx->P );
  12285. return( 0 );
  12286. }
  12287. /*
  12288. * Import the peer's public value G^Y
  12289. */
  12290. int mbedtls_dhm_read_public( mbedtls_dhm_context *ctx,
  12291. const unsigned char *input, size_t ilen )
  12292. {
  12293. int ret;
  12294. if( ctx == NULL || ilen < 1 || ilen > ctx->len )
  12295. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  12296. if( ( ret = mbedtls_mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
  12297. return( MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED + ret );
  12298. return( 0 );
  12299. }
  12300. /*
  12301. * Create own private value X and export G^X
  12302. */
  12303. int mbedtls_dhm_make_public( mbedtls_dhm_context *ctx, int x_size,
  12304. unsigned char *output, size_t olen,
  12305. int (*f_rng)(void *, unsigned char *, size_t),
  12306. void *p_rng )
  12307. {
  12308. int ret, count = 0;
  12309. if( ctx == NULL || olen < 1 || olen > ctx->len )
  12310. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  12311. if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 )
  12312. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  12313. /*
  12314. * generate X and calculate GX = G^X mod P
  12315. */
  12316. do
  12317. {
  12318. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->X, x_size, f_rng, p_rng ) );
  12319. while( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
  12320. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->X, 1 ) );
  12321. if( count++ > 10 )
  12322. return( MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED );
  12323. }
  12324. while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
  12325. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
  12326. &ctx->P , &ctx->RP ) );
  12327. if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
  12328. return( ret );
  12329. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->GX, output, olen ) );
  12330. cleanup:
  12331. if( ret != 0 )
  12332. return( MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
  12333. return( 0 );
  12334. }
  12335. /*
  12336. * Use the blinding method and optimisation suggested in section 10 of:
  12337. * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
  12338. * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
  12339. * Berlin Heidelberg, 1996. p. 104-113.
  12340. */
  12341. static int dhm_update_blinding( mbedtls_dhm_context *ctx,
  12342. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  12343. {
  12344. int ret, count;
  12345. /*
  12346. * Don't use any blinding the first time a particular X is used,
  12347. * but remember it to use blinding next time.
  12348. */
  12349. if( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 )
  12350. {
  12351. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &ctx->pX, &ctx->X ) );
  12352. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->Vi, 1 ) );
  12353. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->Vf, 1 ) );
  12354. return( 0 );
  12355. }
  12356. /*
  12357. * Ok, we need blinding. Can we re-use existing values?
  12358. * If yes, just update them by squaring them.
  12359. */
  12360. if( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
  12361. {
  12362. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
  12363. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
  12364. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
  12365. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
  12366. return( 0 );
  12367. }
  12368. /*
  12369. * We need to generate blinding values from scratch
  12370. */
  12371. /* Vi = random( 2, P-1 ) */
  12372. count = 0;
  12373. do
  12374. {
  12375. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vi, mbedtls_mpi_size( &ctx->P ), f_rng, p_rng ) );
  12376. while( mbedtls_mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
  12377. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->Vi, 1 ) );
  12378. if( count++ > 10 )
  12379. return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
  12380. }
  12381. while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
  12382. /* Vf = Vi^-X mod P */
  12383. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
  12384. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
  12385. cleanup:
  12386. return( ret );
  12387. }
  12388. /*
  12389. * Derive and export the shared secret (G^Y)^X mod P
  12390. */
  12391. int mbedtls_dhm_calc_secret( mbedtls_dhm_context *ctx,
  12392. unsigned char *output, size_t output_size, size_t *olen,
  12393. int (*f_rng)(void *, unsigned char *, size_t),
  12394. void *p_rng )
  12395. {
  12396. int ret;
  12397. mbedtls_mpi GYb;
  12398. if( ctx == NULL || output_size < ctx->len )
  12399. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  12400. if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
  12401. return( ret );
  12402. mbedtls_mpi_init( &GYb );
  12403. /* Blind peer's value */
  12404. if( f_rng != NULL )
  12405. {
  12406. MBEDTLS_MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
  12407. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
  12408. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
  12409. }
  12410. else
  12411. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &GYb, &ctx->GY ) );
  12412. /* Do modular exponentiation */
  12413. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
  12414. &ctx->P, &ctx->RP ) );
  12415. /* Unblind secret value */
  12416. if( f_rng != NULL )
  12417. {
  12418. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
  12419. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
  12420. }
  12421. *olen = mbedtls_mpi_size( &ctx->K );
  12422. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->K, output, *olen ) );
  12423. cleanup:
  12424. mbedtls_mpi_free( &GYb );
  12425. if( ret != 0 )
  12426. return( MBEDTLS_ERR_DHM_CALC_SECRET_FAILED + ret );
  12427. return( 0 );
  12428. }
  12429. /*
  12430. * Free the components of a DHM key
  12431. */
  12432. void mbedtls_dhm_free( mbedtls_dhm_context *ctx )
  12433. {
  12434. mbedtls_mpi_free( &ctx->pX ); mbedtls_mpi_free( &ctx->Vf );
  12435. mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->RP );
  12436. mbedtls_mpi_free( &ctx->K ); mbedtls_mpi_free( &ctx->GY );
  12437. mbedtls_mpi_free( &ctx->GX ); mbedtls_mpi_free( &ctx->X );
  12438. mbedtls_mpi_free( &ctx->G ); mbedtls_mpi_free( &ctx->P );
  12439. mbedtls_zeroize( ctx, sizeof( mbedtls_dhm_context ) );
  12440. }
  12441. #if defined(MBEDTLS_ASN1_PARSE_C)
  12442. /*
  12443. * Parse DHM parameters
  12444. */
  12445. int mbedtls_dhm_parse_dhm( mbedtls_dhm_context *dhm, const unsigned char *dhmin,
  12446. size_t dhminlen )
  12447. {
  12448. int ret;
  12449. size_t len;
  12450. unsigned char *p, *end;
  12451. #if defined(MBEDTLS_PEM_PARSE_C)
  12452. mbedtls_pem_context pem;
  12453. mbedtls_pem_init( &pem );
  12454. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  12455. if( dhminlen == 0 || dhmin[dhminlen - 1] != '\0' )
  12456. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  12457. else
  12458. ret = mbedtls_pem_read_buffer( &pem,
  12459. "-----BEGIN DH PARAMETERS-----",
  12460. "-----END DH PARAMETERS-----",
  12461. dhmin, NULL, 0, &dhminlen );
  12462. if( ret == 0 )
  12463. {
  12464. /*
  12465. * Was PEM encoded
  12466. */
  12467. dhminlen = pem.buflen;
  12468. }
  12469. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  12470. goto exit;
  12471. p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
  12472. #else
  12473. p = (unsigned char *) dhmin;
  12474. #endif /* MBEDTLS_PEM_PARSE_C */
  12475. end = p + dhminlen;
  12476. /*
  12477. * DHParams ::= SEQUENCE {
  12478. * prime INTEGER, -- P
  12479. * generator INTEGER, -- g
  12480. * privateValueLength INTEGER OPTIONAL
  12481. * }
  12482. */
  12483. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  12484. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  12485. {
  12486. ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
  12487. goto exit;
  12488. }
  12489. end = p + len;
  12490. if( ( ret = mbedtls_asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
  12491. ( ret = mbedtls_asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
  12492. {
  12493. ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
  12494. goto exit;
  12495. }
  12496. if( p != end )
  12497. {
  12498. /* This might be the optional privateValueLength.
  12499. * If so, we can cleanly discard it */
  12500. mbedtls_mpi rec;
  12501. mbedtls_mpi_init( &rec );
  12502. ret = mbedtls_asn1_get_mpi( &p, end, &rec );
  12503. mbedtls_mpi_free( &rec );
  12504. if ( ret != 0 )
  12505. {
  12506. ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
  12507. goto exit;
  12508. }
  12509. if ( p != end )
  12510. {
  12511. ret = MBEDTLS_ERR_DHM_INVALID_FORMAT +
  12512. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
  12513. goto exit;
  12514. }
  12515. }
  12516. ret = 0;
  12517. dhm->len = mbedtls_mpi_size( &dhm->P );
  12518. exit:
  12519. #if defined(MBEDTLS_PEM_PARSE_C)
  12520. mbedtls_pem_free( &pem );
  12521. #endif
  12522. if( ret != 0 )
  12523. mbedtls_dhm_free( dhm );
  12524. return( ret );
  12525. }
  12526. #if defined(MBEDTLS_FS_IO)
  12527. /*
  12528. * Load all data from a file into a given buffer.
  12529. *
  12530. * The file is expected to contain either PEM or DER encoded data.
  12531. * A terminating null byte is always appended. It is included in the announced
  12532. * length only if the data looks like it is PEM encoded.
  12533. */
  12534. static int load_file( const char *path, unsigned char **buf, size_t *n )
  12535. {
  12536. FILE *f;
  12537. long size;
  12538. if( ( f = fopen( path, "rb" ) ) == NULL )
  12539. return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
  12540. fseek( f, 0, SEEK_END );
  12541. if( ( size = ftell( f ) ) == -1 )
  12542. {
  12543. fclose( f );
  12544. return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
  12545. }
  12546. fseek( f, 0, SEEK_SET );
  12547. *n = (size_t) size;
  12548. if( *n + 1 == 0 ||
  12549. ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
  12550. {
  12551. fclose( f );
  12552. return( MBEDTLS_ERR_DHM_ALLOC_FAILED );
  12553. }
  12554. if( fread( *buf, 1, *n, f ) != *n )
  12555. {
  12556. fclose( f );
  12557. mbedtls_zeroize( *buf, *n + 1 );
  12558. mbedtls_free( *buf );
  12559. return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
  12560. }
  12561. fclose( f );
  12562. (*buf)[*n] = '\0';
  12563. if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
  12564. ++*n;
  12565. return( 0 );
  12566. }
  12567. /*
  12568. * Load and parse DHM parameters
  12569. */
  12570. int mbedtls_dhm_parse_dhmfile( mbedtls_dhm_context *dhm, const char *path )
  12571. {
  12572. int ret;
  12573. size_t n;
  12574. unsigned char *buf;
  12575. if( ( ret = load_file( path, &buf, &n ) ) != 0 )
  12576. return( ret );
  12577. ret = mbedtls_dhm_parse_dhm( dhm, buf, n );
  12578. mbedtls_zeroize( buf, n );
  12579. mbedtls_free( buf );
  12580. return( ret );
  12581. }
  12582. #endif /* MBEDTLS_FS_IO */
  12583. #endif /* MBEDTLS_ASN1_PARSE_C */
  12584. #endif /* MBEDTLS_DHM_ALT */
  12585. #if defined(MBEDTLS_SELF_TEST)
  12586. static const char mbedtls_test_dhm_params[] =
  12587. "-----BEGIN DH PARAMETERS-----\r\n"
  12588. "MIGHAoGBAJ419DBEOgmQTzo5qXl5fQcN9TN455wkOL7052HzxxRVMyhYmwQcgJvh\r\n"
  12589. "1sa18fyfR9OiVEMYglOpkqVoGLN7qd5aQNNi5W7/C+VBdHTBJcGZJyyP5B3qcz32\r\n"
  12590. "9mLJKudlVudV0Qxk5qUJaPZ/xupz0NyoVpviuiBOI1gNi8ovSXWzAgEC\r\n"
  12591. "-----END DH PARAMETERS-----\r\n";
  12592. static const size_t mbedtls_test_dhm_params_len = sizeof( mbedtls_test_dhm_params );
  12593. /*
  12594. * Checkup routine
  12595. */
  12596. int mbedtls_dhm_self_test( int verbose )
  12597. {
  12598. int ret;
  12599. mbedtls_dhm_context dhm;
  12600. mbedtls_dhm_init( &dhm );
  12601. if( verbose != 0 )
  12602. mbedtls_printf( " DHM parameter load: " );
  12603. if( ( ret = mbedtls_dhm_parse_dhm( &dhm,
  12604. (const unsigned char *) mbedtls_test_dhm_params,
  12605. mbedtls_test_dhm_params_len ) ) != 0 )
  12606. {
  12607. if( verbose != 0 )
  12608. mbedtls_printf( "failed\n" );
  12609. ret = 1;
  12610. goto exit;
  12611. }
  12612. if( verbose != 0 )
  12613. mbedtls_printf( "passed\n\n" );
  12614. exit:
  12615. mbedtls_dhm_free( &dhm );
  12616. return( ret );
  12617. }
  12618. #endif /* MBEDTLS_SELF_TEST */
  12619. #endif /* MBEDTLS_DHM_C */
  12620. /*
  12621. Amalgamated build undefines
  12622. */
  12623. #undef ADD
  12624. #undef BC
  12625. #undef BEFORE_COLON
  12626. #undef F
  12627. #undef F0
  12628. #undef F1
  12629. #undef F2
  12630. #undef F3
  12631. #undef F4
  12632. #undef F5
  12633. #undef FSb
  12634. #undef K
  12635. #undef KK
  12636. #undef P
  12637. #undef R
  12638. #undef ROTR
  12639. #undef S
  12640. #undef S0
  12641. #undef S1
  12642. #undef S2
  12643. #undef S3
  12644. #undef SAFE_SNPRINTF
  12645. #undef SHR
  12646. #undef close
  12647. #undef read
  12648. #undef supported_init
  12649. #undef write
  12650. /********* Start of file library/ecdh.c ************/
  12651. /*
  12652. * Elliptic curve Diffie-Hellman
  12653. *
  12654. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  12655. * SPDX-License-Identifier: Apache-2.0
  12656. *
  12657. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  12658. * not use this file except in compliance with the License.
  12659. * You may obtain a copy of the License at
  12660. *
  12661. * http://www.apache.org/licenses/LICENSE-2.0
  12662. *
  12663. * Unless required by applicable law or agreed to in writing, software
  12664. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12665. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12666. * See the License for the specific language governing permissions and
  12667. * limitations under the License.
  12668. *
  12669. * This file is part of mbed TLS (https://tls.mbed.org)
  12670. */
  12671. /*
  12672. * References:
  12673. *
  12674. * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
  12675. * RFC 4492
  12676. */
  12677. #if !defined(MBEDTLS_CONFIG_FILE)
  12678. #else
  12679. #endif
  12680. #if defined(MBEDTLS_ECDH_C)
  12681. #include <string.h>
  12682. #if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
  12683. /*
  12684. * Generate public key: simple wrapper around mbedtls_ecp_gen_keypair
  12685. */
  12686. int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
  12687. int (*f_rng)(void *, unsigned char *, size_t),
  12688. void *p_rng )
  12689. {
  12690. return mbedtls_ecp_gen_keypair( grp, d, Q, f_rng, p_rng );
  12691. }
  12692. #endif /* MBEDTLS_ECDH_GEN_PUBLIC_ALT */
  12693. #if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
  12694. /*
  12695. * Compute shared secret (SEC1 3.3.1)
  12696. */
  12697. int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
  12698. const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
  12699. int (*f_rng)(void *, unsigned char *, size_t),
  12700. void *p_rng )
  12701. {
  12702. int ret;
  12703. mbedtls_ecp_point P;
  12704. mbedtls_ecp_point_init( &P );
  12705. /*
  12706. * Make sure Q is a valid pubkey before using it
  12707. */
  12708. MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) );
  12709. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, &P, d, Q, f_rng, p_rng ) );
  12710. if( mbedtls_ecp_is_zero( &P ) )
  12711. {
  12712. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  12713. goto cleanup;
  12714. }
  12715. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
  12716. cleanup:
  12717. mbedtls_ecp_point_free( &P );
  12718. return( ret );
  12719. }
  12720. #endif /* MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
  12721. /*
  12722. * Initialize context
  12723. */
  12724. void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
  12725. {
  12726. memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
  12727. }
  12728. /*
  12729. * Free context
  12730. */
  12731. void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
  12732. {
  12733. if( ctx == NULL )
  12734. return;
  12735. mbedtls_ecp_group_free( &ctx->grp );
  12736. mbedtls_ecp_point_free( &ctx->Q );
  12737. mbedtls_ecp_point_free( &ctx->Qp );
  12738. mbedtls_ecp_point_free( &ctx->Vi );
  12739. mbedtls_ecp_point_free( &ctx->Vf );
  12740. mbedtls_mpi_free( &ctx->d );
  12741. mbedtls_mpi_free( &ctx->z );
  12742. mbedtls_mpi_free( &ctx->_d );
  12743. }
  12744. /*
  12745. * Setup and write the ServerKeyExhange parameters (RFC 4492)
  12746. * struct {
  12747. * ECParameters curve_params;
  12748. * ECPoint public;
  12749. * } ServerECDHParams;
  12750. */
  12751. int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
  12752. unsigned char *buf, size_t blen,
  12753. int (*f_rng)(void *, unsigned char *, size_t),
  12754. void *p_rng )
  12755. {
  12756. int ret;
  12757. size_t grp_len, pt_len;
  12758. if( ctx == NULL || ctx->grp.pbits == 0 )
  12759. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  12760. if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
  12761. != 0 )
  12762. return( ret );
  12763. if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
  12764. != 0 )
  12765. return( ret );
  12766. buf += grp_len;
  12767. blen -= grp_len;
  12768. if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
  12769. &pt_len, buf, blen ) ) != 0 )
  12770. return( ret );
  12771. *olen = grp_len + pt_len;
  12772. return( 0 );
  12773. }
  12774. /*
  12775. * Read the ServerKeyExhange parameters (RFC 4492)
  12776. * struct {
  12777. * ECParameters curve_params;
  12778. * ECPoint public;
  12779. * } ServerECDHParams;
  12780. */
  12781. int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
  12782. const unsigned char **buf, const unsigned char *end )
  12783. {
  12784. int ret;
  12785. if( ( ret = mbedtls_ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
  12786. return( ret );
  12787. if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
  12788. != 0 )
  12789. return( ret );
  12790. return( 0 );
  12791. }
  12792. /*
  12793. * Get parameters from a keypair
  12794. */
  12795. int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
  12796. mbedtls_ecdh_side side )
  12797. {
  12798. int ret;
  12799. if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
  12800. return( ret );
  12801. /* If it's not our key, just import the public part as Qp */
  12802. if( side == MBEDTLS_ECDH_THEIRS )
  12803. return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
  12804. /* Our key: import public (as Q) and private parts */
  12805. if( side != MBEDTLS_ECDH_OURS )
  12806. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  12807. if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
  12808. ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
  12809. return( ret );
  12810. return( 0 );
  12811. }
  12812. /*
  12813. * Setup and export the client public value
  12814. */
  12815. int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
  12816. unsigned char *buf, size_t blen,
  12817. int (*f_rng)(void *, unsigned char *, size_t),
  12818. void *p_rng )
  12819. {
  12820. int ret;
  12821. if( ctx == NULL || ctx->grp.pbits == 0 )
  12822. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  12823. if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
  12824. != 0 )
  12825. return( ret );
  12826. return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
  12827. olen, buf, blen );
  12828. }
  12829. /*
  12830. * Parse and import the client's public value
  12831. */
  12832. int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
  12833. const unsigned char *buf, size_t blen )
  12834. {
  12835. int ret;
  12836. const unsigned char *p = buf;
  12837. if( ctx == NULL )
  12838. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  12839. if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
  12840. return( ret );
  12841. if( (size_t)( p - buf ) != blen )
  12842. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  12843. return( 0 );
  12844. }
  12845. /*
  12846. * Derive and export the shared secret
  12847. */
  12848. int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
  12849. unsigned char *buf, size_t blen,
  12850. int (*f_rng)(void *, unsigned char *, size_t),
  12851. void *p_rng )
  12852. {
  12853. int ret;
  12854. if( ctx == NULL )
  12855. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  12856. if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, &ctx->d,
  12857. f_rng, p_rng ) ) != 0 )
  12858. {
  12859. return( ret );
  12860. }
  12861. if( mbedtls_mpi_size( &ctx->z ) > blen )
  12862. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  12863. *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
  12864. return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
  12865. }
  12866. #endif /* MBEDTLS_ECDH_C */
  12867. /*
  12868. Amalgamated build undefines
  12869. */
  12870. #undef ADD
  12871. #undef BC
  12872. #undef BEFORE_COLON
  12873. #undef F
  12874. #undef F0
  12875. #undef F1
  12876. #undef F2
  12877. #undef F3
  12878. #undef F4
  12879. #undef F5
  12880. #undef FSb
  12881. #undef K
  12882. #undef KK
  12883. #undef P
  12884. #undef R
  12885. #undef ROTR
  12886. #undef S
  12887. #undef S0
  12888. #undef S1
  12889. #undef S2
  12890. #undef S3
  12891. #undef SAFE_SNPRINTF
  12892. #undef SHR
  12893. #undef close
  12894. #undef read
  12895. #undef supported_init
  12896. #undef write
  12897. /********* Start of file library/ecdsa.c ************/
  12898. /*
  12899. * Elliptic curve DSA
  12900. *
  12901. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  12902. * SPDX-License-Identifier: Apache-2.0
  12903. *
  12904. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  12905. * not use this file except in compliance with the License.
  12906. * You may obtain a copy of the License at
  12907. *
  12908. * http://www.apache.org/licenses/LICENSE-2.0
  12909. *
  12910. * Unless required by applicable law or agreed to in writing, software
  12911. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12912. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12913. * See the License for the specific language governing permissions and
  12914. * limitations under the License.
  12915. *
  12916. * This file is part of mbed TLS (https://tls.mbed.org)
  12917. */
  12918. /*
  12919. * References:
  12920. *
  12921. * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
  12922. */
  12923. #if !defined(MBEDTLS_CONFIG_FILE)
  12924. #else
  12925. #endif
  12926. #if defined(MBEDTLS_ECDSA_C)
  12927. #include <string.h>
  12928. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  12929. #endif
  12930. /*
  12931. * Derive a suitable integer for group grp from a buffer of length len
  12932. * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
  12933. */
  12934. static int derive_mpi( const mbedtls_ecp_group *grp, mbedtls_mpi *x,
  12935. const unsigned char *buf, size_t blen )
  12936. {
  12937. int ret;
  12938. size_t n_size = ( grp->nbits + 7 ) / 8;
  12939. size_t use_size = blen > n_size ? n_size : blen;
  12940. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( x, buf, use_size ) );
  12941. if( use_size * 8 > grp->nbits )
  12942. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( x, use_size * 8 - grp->nbits ) );
  12943. /* While at it, reduce modulo N */
  12944. if( mbedtls_mpi_cmp_mpi( x, &grp->N ) >= 0 )
  12945. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( x, x, &grp->N ) );
  12946. cleanup:
  12947. return( ret );
  12948. }
  12949. #if !defined(MBEDTLS_ECDSA_SIGN_ALT)
  12950. /*
  12951. * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
  12952. * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
  12953. */
  12954. int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
  12955. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  12956. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  12957. {
  12958. int ret, key_tries, sign_tries, blind_tries;
  12959. mbedtls_ecp_point R;
  12960. mbedtls_mpi k, e, t;
  12961. /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
  12962. if( grp->N.p == NULL )
  12963. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  12964. /* Make sure d is in range 1..n-1 */
  12965. if( mbedtls_mpi_cmp_int( d, 1 ) < 0 || mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
  12966. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  12967. mbedtls_ecp_point_init( &R );
  12968. mbedtls_mpi_init( &k ); mbedtls_mpi_init( &e ); mbedtls_mpi_init( &t );
  12969. sign_tries = 0;
  12970. do
  12971. {
  12972. /*
  12973. * Steps 1-3: generate a suitable ephemeral keypair
  12974. * and set r = xR mod n
  12975. */
  12976. key_tries = 0;
  12977. do
  12978. {
  12979. MBEDTLS_MPI_CHK( mbedtls_ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
  12980. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( r, &R.X, &grp->N ) );
  12981. if( key_tries++ > 10 )
  12982. {
  12983. ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
  12984. goto cleanup;
  12985. }
  12986. }
  12987. while( mbedtls_mpi_cmp_int( r, 0 ) == 0 );
  12988. /*
  12989. * Step 5: derive MPI from hashed message
  12990. */
  12991. MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
  12992. /*
  12993. * Generate a random value to blind inv_mod in next step,
  12994. * avoiding a potential timing leak.
  12995. */
  12996. blind_tries = 0;
  12997. do
  12998. {
  12999. size_t n_size = ( grp->nbits + 7 ) / 8;
  13000. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &t, n_size, f_rng, p_rng ) );
  13001. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &t, 8 * n_size - grp->nbits ) );
  13002. /* See mbedtls_ecp_gen_keypair() */
  13003. if( ++blind_tries > 30 )
  13004. return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
  13005. }
  13006. while( mbedtls_mpi_cmp_int( &t, 1 ) < 0 ||
  13007. mbedtls_mpi_cmp_mpi( &t, &grp->N ) >= 0 );
  13008. /*
  13009. * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
  13010. */
  13011. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, r, d ) );
  13012. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &e, &e, s ) );
  13013. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &e, &e, &t ) );
  13014. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &k, &k, &t ) );
  13015. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( s, &k, &grp->N ) );
  13016. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, s, &e ) );
  13017. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( s, s, &grp->N ) );
  13018. if( sign_tries++ > 10 )
  13019. {
  13020. ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
  13021. goto cleanup;
  13022. }
  13023. }
  13024. while( mbedtls_mpi_cmp_int( s, 0 ) == 0 );
  13025. cleanup:
  13026. mbedtls_ecp_point_free( &R );
  13027. mbedtls_mpi_free( &k ); mbedtls_mpi_free( &e ); mbedtls_mpi_free( &t );
  13028. return( ret );
  13029. }
  13030. #endif /* MBEDTLS_ECDSA_SIGN_ALT */
  13031. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  13032. /*
  13033. * Deterministic signature wrapper
  13034. */
  13035. int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
  13036. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  13037. mbedtls_md_type_t md_alg )
  13038. {
  13039. int ret;
  13040. mbedtls_hmac_drbg_context rng_ctx;
  13041. unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
  13042. size_t grp_len = ( grp->nbits + 7 ) / 8;
  13043. const mbedtls_md_info_t *md_info;
  13044. mbedtls_mpi h;
  13045. if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
  13046. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  13047. mbedtls_mpi_init( &h );
  13048. mbedtls_hmac_drbg_init( &rng_ctx );
  13049. /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
  13050. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, data, grp_len ) );
  13051. MBEDTLS_MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
  13052. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, data + grp_len, grp_len ) );
  13053. mbedtls_hmac_drbg_seed_buf( &rng_ctx, md_info, data, 2 * grp_len );
  13054. ret = mbedtls_ecdsa_sign( grp, r, s, d, buf, blen,
  13055. mbedtls_hmac_drbg_random, &rng_ctx );
  13056. cleanup:
  13057. mbedtls_hmac_drbg_free( &rng_ctx );
  13058. mbedtls_mpi_free( &h );
  13059. return( ret );
  13060. }
  13061. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  13062. #if !defined(MBEDTLS_ECDSA_VERIFY_ALT)
  13063. /*
  13064. * Verify ECDSA signature of hashed message (SEC1 4.1.4)
  13065. * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
  13066. */
  13067. int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
  13068. const unsigned char *buf, size_t blen,
  13069. const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s)
  13070. {
  13071. int ret;
  13072. mbedtls_mpi e, s_inv, u1, u2;
  13073. mbedtls_ecp_point R;
  13074. mbedtls_ecp_point_init( &R );
  13075. mbedtls_mpi_init( &e ); mbedtls_mpi_init( &s_inv ); mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 );
  13076. /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
  13077. if( grp->N.p == NULL )
  13078. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  13079. /*
  13080. * Step 1: make sure r and s are in range 1..n-1
  13081. */
  13082. if( mbedtls_mpi_cmp_int( r, 1 ) < 0 || mbedtls_mpi_cmp_mpi( r, &grp->N ) >= 0 ||
  13083. mbedtls_mpi_cmp_int( s, 1 ) < 0 || mbedtls_mpi_cmp_mpi( s, &grp->N ) >= 0 )
  13084. {
  13085. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  13086. goto cleanup;
  13087. }
  13088. /*
  13089. * Additional precaution: make sure Q is valid
  13090. */
  13091. MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) );
  13092. /*
  13093. * Step 3: derive MPI from hashed message
  13094. */
  13095. MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
  13096. /*
  13097. * Step 4: u1 = e / s mod n, u2 = r / s mod n
  13098. */
  13099. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &s_inv, s, &grp->N ) );
  13100. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u1, &e, &s_inv ) );
  13101. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &u1, &u1, &grp->N ) );
  13102. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u2, r, &s_inv ) );
  13103. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &u2, &u2, &grp->N ) );
  13104. /*
  13105. * Step 5: R = u1 G + u2 Q
  13106. *
  13107. * Since we're not using any secret data, no need to pass a RNG to
  13108. * mbedtls_ecp_mul() for countermesures.
  13109. */
  13110. MBEDTLS_MPI_CHK( mbedtls_ecp_muladd( grp, &R, &u1, &grp->G, &u2, Q ) );
  13111. if( mbedtls_ecp_is_zero( &R ) )
  13112. {
  13113. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  13114. goto cleanup;
  13115. }
  13116. /*
  13117. * Step 6: convert xR to an integer (no-op)
  13118. * Step 7: reduce xR mod n (gives v)
  13119. */
  13120. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
  13121. /*
  13122. * Step 8: check if v (that is, R.X) is equal to r
  13123. */
  13124. if( mbedtls_mpi_cmp_mpi( &R.X, r ) != 0 )
  13125. {
  13126. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  13127. goto cleanup;
  13128. }
  13129. cleanup:
  13130. mbedtls_ecp_point_free( &R );
  13131. mbedtls_mpi_free( &e ); mbedtls_mpi_free( &s_inv ); mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 );
  13132. return( ret );
  13133. }
  13134. #endif /* MBEDTLS_ECDSA_VERIFY_ALT */
  13135. /*
  13136. * Convert a signature (given by context) to ASN.1
  13137. */
  13138. static int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s,
  13139. unsigned char *sig, size_t *slen )
  13140. {
  13141. int ret;
  13142. unsigned char buf[MBEDTLS_ECDSA_MAX_LEN];
  13143. unsigned char *p = buf + sizeof( buf );
  13144. size_t len = 0;
  13145. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, s ) );
  13146. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, r ) );
  13147. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, buf, len ) );
  13148. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, buf,
  13149. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  13150. memcpy( sig, p, len );
  13151. *slen = len;
  13152. return( 0 );
  13153. }
  13154. /*
  13155. * Compute and write signature
  13156. */
  13157. int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
  13158. const unsigned char *hash, size_t hlen,
  13159. unsigned char *sig, size_t *slen,
  13160. int (*f_rng)(void *, unsigned char *, size_t),
  13161. void *p_rng )
  13162. {
  13163. int ret;
  13164. mbedtls_mpi r, s;
  13165. mbedtls_mpi_init( &r );
  13166. mbedtls_mpi_init( &s );
  13167. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  13168. (void) f_rng;
  13169. (void) p_rng;
  13170. MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ctx->grp, &r, &s, &ctx->d,
  13171. hash, hlen, md_alg ) );
  13172. #else
  13173. (void) md_alg;
  13174. MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ctx->grp, &r, &s, &ctx->d,
  13175. hash, hlen, f_rng, p_rng ) );
  13176. #endif
  13177. MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) );
  13178. cleanup:
  13179. mbedtls_mpi_free( &r );
  13180. mbedtls_mpi_free( &s );
  13181. return( ret );
  13182. }
  13183. #if ! defined(MBEDTLS_DEPRECATED_REMOVED) && \
  13184. defined(MBEDTLS_ECDSA_DETERMINISTIC)
  13185. int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
  13186. const unsigned char *hash, size_t hlen,
  13187. unsigned char *sig, size_t *slen,
  13188. mbedtls_md_type_t md_alg )
  13189. {
  13190. return( mbedtls_ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen,
  13191. NULL, NULL ) );
  13192. }
  13193. #endif
  13194. /*
  13195. * Read and check signature
  13196. */
  13197. int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
  13198. const unsigned char *hash, size_t hlen,
  13199. const unsigned char *sig, size_t slen )
  13200. {
  13201. int ret;
  13202. unsigned char *p = (unsigned char *) sig;
  13203. const unsigned char *end = sig + slen;
  13204. size_t len;
  13205. mbedtls_mpi r, s;
  13206. mbedtls_mpi_init( &r );
  13207. mbedtls_mpi_init( &s );
  13208. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  13209. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  13210. {
  13211. ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  13212. goto cleanup;
  13213. }
  13214. if( p + len != end )
  13215. {
  13216. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA +
  13217. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
  13218. goto cleanup;
  13219. }
  13220. if( ( ret = mbedtls_asn1_get_mpi( &p, end, &r ) ) != 0 ||
  13221. ( ret = mbedtls_asn1_get_mpi( &p, end, &s ) ) != 0 )
  13222. {
  13223. ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  13224. goto cleanup;
  13225. }
  13226. if( ( ret = mbedtls_ecdsa_verify( &ctx->grp, hash, hlen,
  13227. &ctx->Q, &r, &s ) ) != 0 )
  13228. goto cleanup;
  13229. if( p != end )
  13230. ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
  13231. cleanup:
  13232. mbedtls_mpi_free( &r );
  13233. mbedtls_mpi_free( &s );
  13234. return( ret );
  13235. }
  13236. #if !defined(MBEDTLS_ECDSA_GENKEY_ALT)
  13237. /*
  13238. * Generate key pair
  13239. */
  13240. int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
  13241. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  13242. {
  13243. return( mbedtls_ecp_group_load( &ctx->grp, gid ) ||
  13244. mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
  13245. }
  13246. #endif /* MBEDTLS_ECDSA_GENKEY_ALT */
  13247. /*
  13248. * Set context from an mbedtls_ecp_keypair
  13249. */
  13250. int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key )
  13251. {
  13252. int ret;
  13253. if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
  13254. ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ||
  13255. ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
  13256. {
  13257. mbedtls_ecdsa_free( ctx );
  13258. }
  13259. return( ret );
  13260. }
  13261. /*
  13262. * Initialize context
  13263. */
  13264. void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx )
  13265. {
  13266. mbedtls_ecp_keypair_init( ctx );
  13267. }
  13268. /*
  13269. * Free context
  13270. */
  13271. void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx )
  13272. {
  13273. mbedtls_ecp_keypair_free( ctx );
  13274. }
  13275. #endif /* MBEDTLS_ECDSA_C */
  13276. /*
  13277. Amalgamated build undefines
  13278. */
  13279. #undef ADD
  13280. #undef BC
  13281. #undef BEFORE_COLON
  13282. #undef F
  13283. #undef F0
  13284. #undef F1
  13285. #undef F2
  13286. #undef F3
  13287. #undef F4
  13288. #undef F5
  13289. #undef FSb
  13290. #undef K
  13291. #undef KK
  13292. #undef P
  13293. #undef R
  13294. #undef ROTR
  13295. #undef S
  13296. #undef S0
  13297. #undef S1
  13298. #undef S2
  13299. #undef S3
  13300. #undef SAFE_SNPRINTF
  13301. #undef SHR
  13302. #undef close
  13303. #undef read
  13304. #undef supported_init
  13305. #undef write
  13306. /********* Start of file library/ecjpake.c ************/
  13307. /*
  13308. * Elliptic curve J-PAKE
  13309. *
  13310. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  13311. * SPDX-License-Identifier: Apache-2.0
  13312. *
  13313. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  13314. * not use this file except in compliance with the License.
  13315. * You may obtain a copy of the License at
  13316. *
  13317. * http://www.apache.org/licenses/LICENSE-2.0
  13318. *
  13319. * Unless required by applicable law or agreed to in writing, software
  13320. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13321. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13322. * See the License for the specific language governing permissions and
  13323. * limitations under the License.
  13324. *
  13325. * This file is part of mbed TLS (https://tls.mbed.org)
  13326. */
  13327. /*
  13328. * References in the code are to the Thread v1.0 Specification,
  13329. * available to members of the Thread Group http://threadgroup.org/
  13330. */
  13331. #if !defined(MBEDTLS_CONFIG_FILE)
  13332. #else
  13333. #endif
  13334. #if defined(MBEDTLS_ECJPAKE_C)
  13335. #include <string.h>
  13336. #if !defined(MBEDTLS_ECJPAKE_ALT)
  13337. /*
  13338. * Convert a mbedtls_ecjpake_role to identifier string
  13339. */
  13340. static const char * const ecjpake_id[] = {
  13341. "client",
  13342. "server"
  13343. };
  13344. #define ID_MINE ( ecjpake_id[ ctx->role ] )
  13345. #define ID_PEER ( ecjpake_id[ 1 - ctx->role ] )
  13346. /*
  13347. * Initialize context
  13348. */
  13349. void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx )
  13350. {
  13351. if( ctx == NULL )
  13352. return;
  13353. ctx->md_info = NULL;
  13354. mbedtls_ecp_group_init( &ctx->grp );
  13355. ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
  13356. mbedtls_ecp_point_init( &ctx->Xm1 );
  13357. mbedtls_ecp_point_init( &ctx->Xm2 );
  13358. mbedtls_ecp_point_init( &ctx->Xp1 );
  13359. mbedtls_ecp_point_init( &ctx->Xp2 );
  13360. mbedtls_ecp_point_init( &ctx->Xp );
  13361. mbedtls_mpi_init( &ctx->xm1 );
  13362. mbedtls_mpi_init( &ctx->xm2 );
  13363. mbedtls_mpi_init( &ctx->s );
  13364. }
  13365. /*
  13366. * Free context
  13367. */
  13368. void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx )
  13369. {
  13370. if( ctx == NULL )
  13371. return;
  13372. ctx->md_info = NULL;
  13373. mbedtls_ecp_group_free( &ctx->grp );
  13374. mbedtls_ecp_point_free( &ctx->Xm1 );
  13375. mbedtls_ecp_point_free( &ctx->Xm2 );
  13376. mbedtls_ecp_point_free( &ctx->Xp1 );
  13377. mbedtls_ecp_point_free( &ctx->Xp2 );
  13378. mbedtls_ecp_point_free( &ctx->Xp );
  13379. mbedtls_mpi_free( &ctx->xm1 );
  13380. mbedtls_mpi_free( &ctx->xm2 );
  13381. mbedtls_mpi_free( &ctx->s );
  13382. }
  13383. /*
  13384. * Setup context
  13385. */
  13386. int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx,
  13387. mbedtls_ecjpake_role role,
  13388. mbedtls_md_type_t hash,
  13389. mbedtls_ecp_group_id curve,
  13390. const unsigned char *secret,
  13391. size_t len )
  13392. {
  13393. int ret;
  13394. ctx->role = role;
  13395. if( ( ctx->md_info = mbedtls_md_info_from_type( hash ) ) == NULL )
  13396. return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
  13397. MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ctx->grp, curve ) );
  13398. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->s, secret, len ) );
  13399. cleanup:
  13400. if( ret != 0 )
  13401. mbedtls_ecjpake_free( ctx );
  13402. return( ret );
  13403. }
  13404. /*
  13405. * Check if context is ready for use
  13406. */
  13407. int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx )
  13408. {
  13409. if( ctx->md_info == NULL ||
  13410. ctx->grp.id == MBEDTLS_ECP_DP_NONE ||
  13411. ctx->s.p == NULL )
  13412. {
  13413. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  13414. }
  13415. return( 0 );
  13416. }
  13417. /*
  13418. * Write a point plus its length to a buffer
  13419. */
  13420. static int ecjpake_write_len_point( unsigned char **p,
  13421. const unsigned char *end,
  13422. const mbedtls_ecp_group *grp,
  13423. const int pf,
  13424. const mbedtls_ecp_point *P )
  13425. {
  13426. int ret;
  13427. size_t len;
  13428. /* Need at least 4 for length plus 1 for point */
  13429. if( end < *p || end - *p < 5 )
  13430. return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
  13431. ret = mbedtls_ecp_point_write_binary( grp, P, pf,
  13432. &len, *p + 4, end - ( *p + 4 ) );
  13433. if( ret != 0 )
  13434. return( ret );
  13435. (*p)[0] = (unsigned char)( ( len >> 24 ) & 0xFF );
  13436. (*p)[1] = (unsigned char)( ( len >> 16 ) & 0xFF );
  13437. (*p)[2] = (unsigned char)( ( len >> 8 ) & 0xFF );
  13438. (*p)[3] = (unsigned char)( ( len ) & 0xFF );
  13439. *p += 4 + len;
  13440. return( 0 );
  13441. }
  13442. /*
  13443. * Size of the temporary buffer for ecjpake_hash:
  13444. * 3 EC points plus their length, plus ID and its length (4 + 6 bytes)
  13445. */
  13446. #define ECJPAKE_HASH_BUF_LEN ( 3 * ( 4 + MBEDTLS_ECP_MAX_PT_LEN ) + 4 + 6 )
  13447. /*
  13448. * Compute hash for ZKP (7.4.2.2.2.1)
  13449. */
  13450. static int ecjpake_hash( const mbedtls_md_info_t *md_info,
  13451. const mbedtls_ecp_group *grp,
  13452. const int pf,
  13453. const mbedtls_ecp_point *G,
  13454. const mbedtls_ecp_point *V,
  13455. const mbedtls_ecp_point *X,
  13456. const char *id,
  13457. mbedtls_mpi *h )
  13458. {
  13459. int ret;
  13460. unsigned char buf[ECJPAKE_HASH_BUF_LEN];
  13461. unsigned char *p = buf;
  13462. const unsigned char *end = buf + sizeof( buf );
  13463. const size_t id_len = strlen( id );
  13464. unsigned char hash[MBEDTLS_MD_MAX_SIZE];
  13465. /* Write things to temporary buffer */
  13466. MBEDTLS_MPI_CHK( ecjpake_write_len_point( &p, end, grp, pf, G ) );
  13467. MBEDTLS_MPI_CHK( ecjpake_write_len_point( &p, end, grp, pf, V ) );
  13468. MBEDTLS_MPI_CHK( ecjpake_write_len_point( &p, end, grp, pf, X ) );
  13469. if( end - p < 4 )
  13470. return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
  13471. *p++ = (unsigned char)( ( id_len >> 24 ) & 0xFF );
  13472. *p++ = (unsigned char)( ( id_len >> 16 ) & 0xFF );
  13473. *p++ = (unsigned char)( ( id_len >> 8 ) & 0xFF );
  13474. *p++ = (unsigned char)( ( id_len ) & 0xFF );
  13475. if( end < p || (size_t)( end - p ) < id_len )
  13476. return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
  13477. memcpy( p, id, id_len );
  13478. p += id_len;
  13479. /* Compute hash */
  13480. mbedtls_md( md_info, buf, p - buf, hash );
  13481. /* Turn it into an integer mod n */
  13482. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( h, hash,
  13483. mbedtls_md_get_size( md_info ) ) );
  13484. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( h, h, &grp->N ) );
  13485. cleanup:
  13486. return( ret );
  13487. }
  13488. /*
  13489. * Parse a ECShnorrZKP (7.4.2.2.2) and verify it (7.4.2.3.3)
  13490. */
  13491. static int ecjpake_zkp_read( const mbedtls_md_info_t *md_info,
  13492. const mbedtls_ecp_group *grp,
  13493. const int pf,
  13494. const mbedtls_ecp_point *G,
  13495. const mbedtls_ecp_point *X,
  13496. const char *id,
  13497. const unsigned char **p,
  13498. const unsigned char *end )
  13499. {
  13500. int ret;
  13501. mbedtls_ecp_point V, VV;
  13502. mbedtls_mpi r, h;
  13503. size_t r_len;
  13504. mbedtls_ecp_point_init( &V );
  13505. mbedtls_ecp_point_init( &VV );
  13506. mbedtls_mpi_init( &r );
  13507. mbedtls_mpi_init( &h );
  13508. /*
  13509. * struct {
  13510. * ECPoint V;
  13511. * opaque r<1..2^8-1>;
  13512. * } ECSchnorrZKP;
  13513. */
  13514. if( end < *p )
  13515. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  13516. MBEDTLS_MPI_CHK( mbedtls_ecp_tls_read_point( grp, &V, p, end - *p ) );
  13517. if( end < *p || (size_t)( end - *p ) < 1 )
  13518. {
  13519. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  13520. goto cleanup;
  13521. }
  13522. r_len = *(*p)++;
  13523. if( end < *p || (size_t)( end - *p ) < r_len )
  13524. {
  13525. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  13526. goto cleanup;
  13527. }
  13528. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, *p, r_len ) );
  13529. *p += r_len;
  13530. /*
  13531. * Verification
  13532. */
  13533. MBEDTLS_MPI_CHK( ecjpake_hash( md_info, grp, pf, G, &V, X, id, &h ) );
  13534. MBEDTLS_MPI_CHK( mbedtls_ecp_muladd( (mbedtls_ecp_group *) grp,
  13535. &VV, &h, X, &r, G ) );
  13536. if( mbedtls_ecp_point_cmp( &VV, &V ) != 0 )
  13537. {
  13538. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  13539. goto cleanup;
  13540. }
  13541. cleanup:
  13542. mbedtls_ecp_point_free( &V );
  13543. mbedtls_ecp_point_free( &VV );
  13544. mbedtls_mpi_free( &r );
  13545. mbedtls_mpi_free( &h );
  13546. return( ret );
  13547. }
  13548. /*
  13549. * Generate ZKP (7.4.2.3.2) and write it as ECSchnorrZKP (7.4.2.2.2)
  13550. */
  13551. static int ecjpake_zkp_write( const mbedtls_md_info_t *md_info,
  13552. const mbedtls_ecp_group *grp,
  13553. const int pf,
  13554. const mbedtls_ecp_point *G,
  13555. const mbedtls_mpi *x,
  13556. const mbedtls_ecp_point *X,
  13557. const char *id,
  13558. unsigned char **p,
  13559. const unsigned char *end,
  13560. int (*f_rng)(void *, unsigned char *, size_t),
  13561. void *p_rng )
  13562. {
  13563. int ret;
  13564. mbedtls_ecp_point V;
  13565. mbedtls_mpi v;
  13566. mbedtls_mpi h; /* later recycled to hold r */
  13567. size_t len;
  13568. if( end < *p )
  13569. return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
  13570. mbedtls_ecp_point_init( &V );
  13571. mbedtls_mpi_init( &v );
  13572. mbedtls_mpi_init( &h );
  13573. /* Compute signature */
  13574. MBEDTLS_MPI_CHK( mbedtls_ecp_gen_keypair_base( (mbedtls_ecp_group *) grp,
  13575. G, &v, &V, f_rng, p_rng ) );
  13576. MBEDTLS_MPI_CHK( ecjpake_hash( md_info, grp, pf, G, &V, X, id, &h ) );
  13577. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &h, &h, x ) ); /* x*h */
  13578. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &h, &v, &h ) ); /* v - x*h */
  13579. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &h, &h, &grp->N ) ); /* r */
  13580. /* Write it out */
  13581. MBEDTLS_MPI_CHK( mbedtls_ecp_tls_write_point( grp, &V,
  13582. pf, &len, *p, end - *p ) );
  13583. *p += len;
  13584. len = mbedtls_mpi_size( &h ); /* actually r */
  13585. if( end < *p || (size_t)( end - *p ) < 1 + len || len > 255 )
  13586. {
  13587. ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
  13588. goto cleanup;
  13589. }
  13590. *(*p)++ = (unsigned char)( len & 0xFF );
  13591. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, *p, len ) ); /* r */
  13592. *p += len;
  13593. cleanup:
  13594. mbedtls_ecp_point_free( &V );
  13595. mbedtls_mpi_free( &v );
  13596. mbedtls_mpi_free( &h );
  13597. return( ret );
  13598. }
  13599. /*
  13600. * Parse a ECJPAKEKeyKP (7.4.2.2.1) and check proof
  13601. * Output: verified public key X
  13602. */
  13603. static int ecjpake_kkp_read( const mbedtls_md_info_t *md_info,
  13604. const mbedtls_ecp_group *grp,
  13605. const int pf,
  13606. const mbedtls_ecp_point *G,
  13607. mbedtls_ecp_point *X,
  13608. const char *id,
  13609. const unsigned char **p,
  13610. const unsigned char *end )
  13611. {
  13612. int ret;
  13613. if( end < *p )
  13614. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  13615. /*
  13616. * struct {
  13617. * ECPoint X;
  13618. * ECSchnorrZKP zkp;
  13619. * } ECJPAKEKeyKP;
  13620. */
  13621. MBEDTLS_MPI_CHK( mbedtls_ecp_tls_read_point( grp, X, p, end - *p ) );
  13622. if( mbedtls_ecp_is_zero( X ) )
  13623. {
  13624. ret = MBEDTLS_ERR_ECP_INVALID_KEY;
  13625. goto cleanup;
  13626. }
  13627. MBEDTLS_MPI_CHK( ecjpake_zkp_read( md_info, grp, pf, G, X, id, p, end ) );
  13628. cleanup:
  13629. return( ret );
  13630. }
  13631. /*
  13632. * Generate an ECJPAKEKeyKP
  13633. * Output: the serialized structure, plus private/public key pair
  13634. */
  13635. static int ecjpake_kkp_write( const mbedtls_md_info_t *md_info,
  13636. const mbedtls_ecp_group *grp,
  13637. const int pf,
  13638. const mbedtls_ecp_point *G,
  13639. mbedtls_mpi *x,
  13640. mbedtls_ecp_point *X,
  13641. const char *id,
  13642. unsigned char **p,
  13643. const unsigned char *end,
  13644. int (*f_rng)(void *, unsigned char *, size_t),
  13645. void *p_rng )
  13646. {
  13647. int ret;
  13648. size_t len;
  13649. if( end < *p )
  13650. return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
  13651. /* Generate key (7.4.2.3.1) and write it out */
  13652. MBEDTLS_MPI_CHK( mbedtls_ecp_gen_keypair_base( (mbedtls_ecp_group *) grp, G, x, X,
  13653. f_rng, p_rng ) );
  13654. MBEDTLS_MPI_CHK( mbedtls_ecp_tls_write_point( grp, X,
  13655. pf, &len, *p, end - *p ) );
  13656. *p += len;
  13657. /* Generate and write proof */
  13658. MBEDTLS_MPI_CHK( ecjpake_zkp_write( md_info, grp, pf, G, x, X, id,
  13659. p, end, f_rng, p_rng ) );
  13660. cleanup:
  13661. return( ret );
  13662. }
  13663. /*
  13664. * Read a ECJPAKEKeyKPPairList (7.4.2.3) and check proofs
  13665. * Ouputs: verified peer public keys Xa, Xb
  13666. */
  13667. static int ecjpake_kkpp_read( const mbedtls_md_info_t *md_info,
  13668. const mbedtls_ecp_group *grp,
  13669. const int pf,
  13670. const mbedtls_ecp_point *G,
  13671. mbedtls_ecp_point *Xa,
  13672. mbedtls_ecp_point *Xb,
  13673. const char *id,
  13674. const unsigned char *buf,
  13675. size_t len )
  13676. {
  13677. int ret;
  13678. const unsigned char *p = buf;
  13679. const unsigned char *end = buf + len;
  13680. /*
  13681. * struct {
  13682. * ECJPAKEKeyKP ecjpake_key_kp_pair_list[2];
  13683. * } ECJPAKEKeyKPPairList;
  13684. */
  13685. MBEDTLS_MPI_CHK( ecjpake_kkp_read( md_info, grp, pf, G, Xa, id, &p, end ) );
  13686. MBEDTLS_MPI_CHK( ecjpake_kkp_read( md_info, grp, pf, G, Xb, id, &p, end ) );
  13687. if( p != end )
  13688. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  13689. cleanup:
  13690. return( ret );
  13691. }
  13692. /*
  13693. * Generate a ECJPAKEKeyKPPairList
  13694. * Outputs: the serialized structure, plus two private/public key pairs
  13695. */
  13696. static int ecjpake_kkpp_write( const mbedtls_md_info_t *md_info,
  13697. const mbedtls_ecp_group *grp,
  13698. const int pf,
  13699. const mbedtls_ecp_point *G,
  13700. mbedtls_mpi *xm1,
  13701. mbedtls_ecp_point *Xa,
  13702. mbedtls_mpi *xm2,
  13703. mbedtls_ecp_point *Xb,
  13704. const char *id,
  13705. unsigned char *buf,
  13706. size_t len,
  13707. size_t *olen,
  13708. int (*f_rng)(void *, unsigned char *, size_t),
  13709. void *p_rng )
  13710. {
  13711. int ret;
  13712. unsigned char *p = buf;
  13713. const unsigned char *end = buf + len;
  13714. MBEDTLS_MPI_CHK( ecjpake_kkp_write( md_info, grp, pf, G, xm1, Xa, id,
  13715. &p, end, f_rng, p_rng ) );
  13716. MBEDTLS_MPI_CHK( ecjpake_kkp_write( md_info, grp, pf, G, xm2, Xb, id,
  13717. &p, end, f_rng, p_rng ) );
  13718. *olen = p - buf;
  13719. cleanup:
  13720. return( ret );
  13721. }
  13722. /*
  13723. * Read and process the first round message
  13724. */
  13725. int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx,
  13726. const unsigned char *buf,
  13727. size_t len )
  13728. {
  13729. return( ecjpake_kkpp_read( ctx->md_info, &ctx->grp, ctx->point_format,
  13730. &ctx->grp.G,
  13731. &ctx->Xp1, &ctx->Xp2, ID_PEER,
  13732. buf, len ) );
  13733. }
  13734. /*
  13735. * Generate and write the first round message
  13736. */
  13737. int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx,
  13738. unsigned char *buf, size_t len, size_t *olen,
  13739. int (*f_rng)(void *, unsigned char *, size_t),
  13740. void *p_rng )
  13741. {
  13742. return( ecjpake_kkpp_write( ctx->md_info, &ctx->grp, ctx->point_format,
  13743. &ctx->grp.G,
  13744. &ctx->xm1, &ctx->Xm1, &ctx->xm2, &ctx->Xm2,
  13745. ID_MINE, buf, len, olen, f_rng, p_rng ) );
  13746. }
  13747. /*
  13748. * Compute the sum of three points R = A + B + C
  13749. */
  13750. static int ecjpake_ecp_add3( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  13751. const mbedtls_ecp_point *A,
  13752. const mbedtls_ecp_point *B,
  13753. const mbedtls_ecp_point *C )
  13754. {
  13755. int ret;
  13756. mbedtls_mpi one;
  13757. mbedtls_mpi_init( &one );
  13758. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &one, 1 ) );
  13759. MBEDTLS_MPI_CHK( mbedtls_ecp_muladd( grp, R, &one, A, &one, B ) );
  13760. MBEDTLS_MPI_CHK( mbedtls_ecp_muladd( grp, R, &one, R, &one, C ) );
  13761. cleanup:
  13762. mbedtls_mpi_free( &one );
  13763. return( ret );
  13764. }
  13765. /*
  13766. * Read and process second round message (C: 7.4.2.5, S: 7.4.2.6)
  13767. */
  13768. int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx,
  13769. const unsigned char *buf,
  13770. size_t len )
  13771. {
  13772. int ret;
  13773. const unsigned char *p = buf;
  13774. const unsigned char *end = buf + len;
  13775. mbedtls_ecp_group grp;
  13776. mbedtls_ecp_point G; /* C: GB, S: GA */
  13777. mbedtls_ecp_group_init( &grp );
  13778. mbedtls_ecp_point_init( &G );
  13779. /*
  13780. * Server: GA = X3 + X4 + X1 (7.4.2.6.1)
  13781. * Client: GB = X1 + X2 + X3 (7.4.2.5.1)
  13782. * Unified: G = Xm1 + Xm2 + Xp1
  13783. * We need that before parsing in order to check Xp as we read it
  13784. */
  13785. MBEDTLS_MPI_CHK( ecjpake_ecp_add3( &ctx->grp, &G,
  13786. &ctx->Xm1, &ctx->Xm2, &ctx->Xp1 ) );
  13787. /*
  13788. * struct {
  13789. * ECParameters curve_params; // only client reading server msg
  13790. * ECJPAKEKeyKP ecjpake_key_kp;
  13791. * } Client/ServerECJPAKEParams;
  13792. */
  13793. if( ctx->role == MBEDTLS_ECJPAKE_CLIENT )
  13794. {
  13795. MBEDTLS_MPI_CHK( mbedtls_ecp_tls_read_group( &grp, &p, len ) );
  13796. if( grp.id != ctx->grp.id )
  13797. {
  13798. ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
  13799. goto cleanup;
  13800. }
  13801. }
  13802. MBEDTLS_MPI_CHK( ecjpake_kkp_read( ctx->md_info, &ctx->grp,
  13803. ctx->point_format,
  13804. &G, &ctx->Xp, ID_PEER, &p, end ) );
  13805. if( p != end )
  13806. {
  13807. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  13808. goto cleanup;
  13809. }
  13810. cleanup:
  13811. mbedtls_ecp_group_free( &grp );
  13812. mbedtls_ecp_point_free( &G );
  13813. return( ret );
  13814. }
  13815. /*
  13816. * Compute R = +/- X * S mod N, taking care not to leak S
  13817. */
  13818. static int ecjpake_mul_secret( mbedtls_mpi *R, int sign,
  13819. const mbedtls_mpi *X,
  13820. const mbedtls_mpi *S,
  13821. const mbedtls_mpi *N,
  13822. int (*f_rng)(void *, unsigned char *, size_t),
  13823. void *p_rng )
  13824. {
  13825. int ret;
  13826. mbedtls_mpi b; /* Blinding value, then s + N * blinding */
  13827. mbedtls_mpi_init( &b );
  13828. /* b = s + rnd-128-bit * N */
  13829. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &b, 16, f_rng, p_rng ) );
  13830. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &b, &b, N ) );
  13831. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &b, &b, S ) );
  13832. /* R = sign * X * b mod N */
  13833. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( R, X, &b ) );
  13834. R->s *= sign;
  13835. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( R, R, N ) );
  13836. cleanup:
  13837. mbedtls_mpi_free( &b );
  13838. return( ret );
  13839. }
  13840. /*
  13841. * Generate and write the second round message (S: 7.4.2.5, C: 7.4.2.6)
  13842. */
  13843. int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx,
  13844. unsigned char *buf, size_t len, size_t *olen,
  13845. int (*f_rng)(void *, unsigned char *, size_t),
  13846. void *p_rng )
  13847. {
  13848. int ret;
  13849. mbedtls_ecp_point G; /* C: GA, S: GB */
  13850. mbedtls_ecp_point Xm; /* C: Xc, S: Xs */
  13851. mbedtls_mpi xm; /* C: xc, S: xs */
  13852. unsigned char *p = buf;
  13853. const unsigned char *end = buf + len;
  13854. size_t ec_len;
  13855. mbedtls_ecp_point_init( &G );
  13856. mbedtls_ecp_point_init( &Xm );
  13857. mbedtls_mpi_init( &xm );
  13858. /*
  13859. * First generate private/public key pair (S: 7.4.2.5.1, C: 7.4.2.6.1)
  13860. *
  13861. * Client: GA = X1 + X3 + X4 | xs = x2 * s | Xc = xc * GA
  13862. * Server: GB = X3 + X1 + X2 | xs = x4 * s | Xs = xs * GB
  13863. * Unified: G = Xm1 + Xp1 + Xp2 | xm = xm2 * s | Xm = xm * G
  13864. */
  13865. MBEDTLS_MPI_CHK( ecjpake_ecp_add3( &ctx->grp, &G,
  13866. &ctx->Xp1, &ctx->Xp2, &ctx->Xm1 ) );
  13867. MBEDTLS_MPI_CHK( ecjpake_mul_secret( &xm, 1, &ctx->xm2, &ctx->s,
  13868. &ctx->grp.N, f_rng, p_rng ) );
  13869. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &ctx->grp, &Xm, &xm, &G, f_rng, p_rng ) );
  13870. /*
  13871. * Now write things out
  13872. *
  13873. * struct {
  13874. * ECParameters curve_params; // only server writing its message
  13875. * ECJPAKEKeyKP ecjpake_key_kp;
  13876. * } Client/ServerECJPAKEParams;
  13877. */
  13878. if( ctx->role == MBEDTLS_ECJPAKE_SERVER )
  13879. {
  13880. if( end < p )
  13881. {
  13882. ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
  13883. goto cleanup;
  13884. }
  13885. MBEDTLS_MPI_CHK( mbedtls_ecp_tls_write_group( &ctx->grp, &ec_len,
  13886. p, end - p ) );
  13887. p += ec_len;
  13888. }
  13889. if( end < p )
  13890. {
  13891. ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
  13892. goto cleanup;
  13893. }
  13894. MBEDTLS_MPI_CHK( mbedtls_ecp_tls_write_point( &ctx->grp, &Xm,
  13895. ctx->point_format, &ec_len, p, end - p ) );
  13896. p += ec_len;
  13897. MBEDTLS_MPI_CHK( ecjpake_zkp_write( ctx->md_info, &ctx->grp,
  13898. ctx->point_format,
  13899. &G, &xm, &Xm, ID_MINE,
  13900. &p, end, f_rng, p_rng ) );
  13901. *olen = p - buf;
  13902. cleanup:
  13903. mbedtls_ecp_point_free( &G );
  13904. mbedtls_ecp_point_free( &Xm );
  13905. mbedtls_mpi_free( &xm );
  13906. return( ret );
  13907. }
  13908. /*
  13909. * Derive PMS (7.4.2.7 / 7.4.2.8)
  13910. */
  13911. int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx,
  13912. unsigned char *buf, size_t len, size_t *olen,
  13913. int (*f_rng)(void *, unsigned char *, size_t),
  13914. void *p_rng )
  13915. {
  13916. int ret;
  13917. mbedtls_ecp_point K;
  13918. mbedtls_mpi m_xm2_s, one;
  13919. unsigned char kx[MBEDTLS_ECP_MAX_BYTES];
  13920. size_t x_bytes;
  13921. *olen = mbedtls_md_get_size( ctx->md_info );
  13922. if( len < *olen )
  13923. return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
  13924. mbedtls_ecp_point_init( &K );
  13925. mbedtls_mpi_init( &m_xm2_s );
  13926. mbedtls_mpi_init( &one );
  13927. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &one, 1 ) );
  13928. /*
  13929. * Client: K = ( Xs - X4 * x2 * s ) * x2
  13930. * Server: K = ( Xc - X2 * x4 * s ) * x4
  13931. * Unified: K = ( Xp - Xp2 * xm2 * s ) * xm2
  13932. */
  13933. MBEDTLS_MPI_CHK( ecjpake_mul_secret( &m_xm2_s, -1, &ctx->xm2, &ctx->s,
  13934. &ctx->grp.N, f_rng, p_rng ) );
  13935. MBEDTLS_MPI_CHK( mbedtls_ecp_muladd( &ctx->grp, &K,
  13936. &one, &ctx->Xp,
  13937. &m_xm2_s, &ctx->Xp2 ) );
  13938. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &ctx->grp, &K, &ctx->xm2, &K,
  13939. f_rng, p_rng ) );
  13940. /* PMS = SHA-256( K.X ) */
  13941. x_bytes = ( ctx->grp.pbits + 7 ) / 8;
  13942. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &K.X, kx, x_bytes ) );
  13943. MBEDTLS_MPI_CHK( mbedtls_md( ctx->md_info, kx, x_bytes, buf ) );
  13944. cleanup:
  13945. mbedtls_ecp_point_free( &K );
  13946. mbedtls_mpi_free( &m_xm2_s );
  13947. mbedtls_mpi_free( &one );
  13948. return( ret );
  13949. }
  13950. #undef ID_MINE
  13951. #undef ID_PEER
  13952. #endif /* ! MBEDTLS_ECJPAKE_ALT */
  13953. #if defined(MBEDTLS_SELF_TEST)
  13954. #if defined(MBEDTLS_PLATFORM_C)
  13955. #else
  13956. #include <stdio.h>
  13957. #define mbedtls_printf printf
  13958. #endif
  13959. #if !defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
  13960. !defined(MBEDTLS_SHA256_C)
  13961. int mbedtls_ecjpake_self_test( int verbose )
  13962. {
  13963. (void) verbose;
  13964. return( 0 );
  13965. }
  13966. #else
  13967. static const unsigned char ecjpake_test_password[] = {
  13968. 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x6a, 0x70, 0x61, 0x6b, 0x65, 0x74,
  13969. 0x65, 0x73, 0x74
  13970. };
  13971. static const unsigned char ecjpake_test_x1[] = {
  13972. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
  13973. 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
  13974. 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x21
  13975. };
  13976. static const unsigned char ecjpake_test_x2[] = {
  13977. 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
  13978. 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  13979. 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x81
  13980. };
  13981. static const unsigned char ecjpake_test_x3[] = {
  13982. 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
  13983. 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  13984. 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x81
  13985. };
  13986. static const unsigned char ecjpake_test_x4[] = {
  13987. 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc,
  13988. 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
  13989. 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe1
  13990. };
  13991. static const unsigned char ecjpake_test_cli_one[] = {
  13992. 0x41, 0x04, 0xac, 0xcf, 0x01, 0x06, 0xef, 0x85, 0x8f, 0xa2, 0xd9, 0x19,
  13993. 0x33, 0x13, 0x46, 0x80, 0x5a, 0x78, 0xb5, 0x8b, 0xba, 0xd0, 0xb8, 0x44,
  13994. 0xe5, 0xc7, 0x89, 0x28, 0x79, 0x14, 0x61, 0x87, 0xdd, 0x26, 0x66, 0xad,
  13995. 0xa7, 0x81, 0xbb, 0x7f, 0x11, 0x13, 0x72, 0x25, 0x1a, 0x89, 0x10, 0x62,
  13996. 0x1f, 0x63, 0x4d, 0xf1, 0x28, 0xac, 0x48, 0xe3, 0x81, 0xfd, 0x6e, 0xf9,
  13997. 0x06, 0x07, 0x31, 0xf6, 0x94, 0xa4, 0x41, 0x04, 0x1d, 0xd0, 0xbd, 0x5d,
  13998. 0x45, 0x66, 0xc9, 0xbe, 0xd9, 0xce, 0x7d, 0xe7, 0x01, 0xb5, 0xe8, 0x2e,
  13999. 0x08, 0xe8, 0x4b, 0x73, 0x04, 0x66, 0x01, 0x8a, 0xb9, 0x03, 0xc7, 0x9e,
  14000. 0xb9, 0x82, 0x17, 0x22, 0x36, 0xc0, 0xc1, 0x72, 0x8a, 0xe4, 0xbf, 0x73,
  14001. 0x61, 0x0d, 0x34, 0xde, 0x44, 0x24, 0x6e, 0xf3, 0xd9, 0xc0, 0x5a, 0x22,
  14002. 0x36, 0xfb, 0x66, 0xa6, 0x58, 0x3d, 0x74, 0x49, 0x30, 0x8b, 0xab, 0xce,
  14003. 0x20, 0x72, 0xfe, 0x16, 0x66, 0x29, 0x92, 0xe9, 0x23, 0x5c, 0x25, 0x00,
  14004. 0x2f, 0x11, 0xb1, 0x50, 0x87, 0xb8, 0x27, 0x38, 0xe0, 0x3c, 0x94, 0x5b,
  14005. 0xf7, 0xa2, 0x99, 0x5d, 0xda, 0x1e, 0x98, 0x34, 0x58, 0x41, 0x04, 0x7e,
  14006. 0xa6, 0xe3, 0xa4, 0x48, 0x70, 0x37, 0xa9, 0xe0, 0xdb, 0xd7, 0x92, 0x62,
  14007. 0xb2, 0xcc, 0x27, 0x3e, 0x77, 0x99, 0x30, 0xfc, 0x18, 0x40, 0x9a, 0xc5,
  14008. 0x36, 0x1c, 0x5f, 0xe6, 0x69, 0xd7, 0x02, 0xe1, 0x47, 0x79, 0x0a, 0xeb,
  14009. 0x4c, 0xe7, 0xfd, 0x65, 0x75, 0xab, 0x0f, 0x6c, 0x7f, 0xd1, 0xc3, 0x35,
  14010. 0x93, 0x9a, 0xa8, 0x63, 0xba, 0x37, 0xec, 0x91, 0xb7, 0xe3, 0x2b, 0xb0,
  14011. 0x13, 0xbb, 0x2b, 0x41, 0x04, 0xa4, 0x95, 0x58, 0xd3, 0x2e, 0xd1, 0xeb,
  14012. 0xfc, 0x18, 0x16, 0xaf, 0x4f, 0xf0, 0x9b, 0x55, 0xfc, 0xb4, 0xca, 0x47,
  14013. 0xb2, 0xa0, 0x2d, 0x1e, 0x7c, 0xaf, 0x11, 0x79, 0xea, 0x3f, 0xe1, 0x39,
  14014. 0x5b, 0x22, 0xb8, 0x61, 0x96, 0x40, 0x16, 0xfa, 0xba, 0xf7, 0x2c, 0x97,
  14015. 0x56, 0x95, 0xd9, 0x3d, 0x4d, 0xf0, 0xe5, 0x19, 0x7f, 0xe9, 0xf0, 0x40,
  14016. 0x63, 0x4e, 0xd5, 0x97, 0x64, 0x93, 0x77, 0x87, 0xbe, 0x20, 0xbc, 0x4d,
  14017. 0xee, 0xbb, 0xf9, 0xb8, 0xd6, 0x0a, 0x33, 0x5f, 0x04, 0x6c, 0xa3, 0xaa,
  14018. 0x94, 0x1e, 0x45, 0x86, 0x4c, 0x7c, 0xad, 0xef, 0x9c, 0xf7, 0x5b, 0x3d,
  14019. 0x8b, 0x01, 0x0e, 0x44, 0x3e, 0xf0
  14020. };
  14021. static const unsigned char ecjpake_test_srv_one[] = {
  14022. 0x41, 0x04, 0x7e, 0xa6, 0xe3, 0xa4, 0x48, 0x70, 0x37, 0xa9, 0xe0, 0xdb,
  14023. 0xd7, 0x92, 0x62, 0xb2, 0xcc, 0x27, 0x3e, 0x77, 0x99, 0x30, 0xfc, 0x18,
  14024. 0x40, 0x9a, 0xc5, 0x36, 0x1c, 0x5f, 0xe6, 0x69, 0xd7, 0x02, 0xe1, 0x47,
  14025. 0x79, 0x0a, 0xeb, 0x4c, 0xe7, 0xfd, 0x65, 0x75, 0xab, 0x0f, 0x6c, 0x7f,
  14026. 0xd1, 0xc3, 0x35, 0x93, 0x9a, 0xa8, 0x63, 0xba, 0x37, 0xec, 0x91, 0xb7,
  14027. 0xe3, 0x2b, 0xb0, 0x13, 0xbb, 0x2b, 0x41, 0x04, 0x09, 0xf8, 0x5b, 0x3d,
  14028. 0x20, 0xeb, 0xd7, 0x88, 0x5c, 0xe4, 0x64, 0xc0, 0x8d, 0x05, 0x6d, 0x64,
  14029. 0x28, 0xfe, 0x4d, 0xd9, 0x28, 0x7a, 0xa3, 0x65, 0xf1, 0x31, 0xf4, 0x36,
  14030. 0x0f, 0xf3, 0x86, 0xd8, 0x46, 0x89, 0x8b, 0xc4, 0xb4, 0x15, 0x83, 0xc2,
  14031. 0xa5, 0x19, 0x7f, 0x65, 0xd7, 0x87, 0x42, 0x74, 0x6c, 0x12, 0xa5, 0xec,
  14032. 0x0a, 0x4f, 0xfe, 0x2f, 0x27, 0x0a, 0x75, 0x0a, 0x1d, 0x8f, 0xb5, 0x16,
  14033. 0x20, 0x93, 0x4d, 0x74, 0xeb, 0x43, 0xe5, 0x4d, 0xf4, 0x24, 0xfd, 0x96,
  14034. 0x30, 0x6c, 0x01, 0x17, 0xbf, 0x13, 0x1a, 0xfa, 0xbf, 0x90, 0xa9, 0xd3,
  14035. 0x3d, 0x11, 0x98, 0xd9, 0x05, 0x19, 0x37, 0x35, 0x14, 0x41, 0x04, 0x19,
  14036. 0x0a, 0x07, 0x70, 0x0f, 0xfa, 0x4b, 0xe6, 0xae, 0x1d, 0x79, 0xee, 0x0f,
  14037. 0x06, 0xae, 0xb5, 0x44, 0xcd, 0x5a, 0xdd, 0xaa, 0xbe, 0xdf, 0x70, 0xf8,
  14038. 0x62, 0x33, 0x21, 0x33, 0x2c, 0x54, 0xf3, 0x55, 0xf0, 0xfb, 0xfe, 0xc7,
  14039. 0x83, 0xed, 0x35, 0x9e, 0x5d, 0x0b, 0xf7, 0x37, 0x7a, 0x0f, 0xc4, 0xea,
  14040. 0x7a, 0xce, 0x47, 0x3c, 0x9c, 0x11, 0x2b, 0x41, 0xcc, 0xd4, 0x1a, 0xc5,
  14041. 0x6a, 0x56, 0x12, 0x41, 0x04, 0x36, 0x0a, 0x1c, 0xea, 0x33, 0xfc, 0xe6,
  14042. 0x41, 0x15, 0x64, 0x58, 0xe0, 0xa4, 0xea, 0xc2, 0x19, 0xe9, 0x68, 0x31,
  14043. 0xe6, 0xae, 0xbc, 0x88, 0xb3, 0xf3, 0x75, 0x2f, 0x93, 0xa0, 0x28, 0x1d,
  14044. 0x1b, 0xf1, 0xfb, 0x10, 0x60, 0x51, 0xdb, 0x96, 0x94, 0xa8, 0xd6, 0xe8,
  14045. 0x62, 0xa5, 0xef, 0x13, 0x24, 0xa3, 0xd9, 0xe2, 0x78, 0x94, 0xf1, 0xee,
  14046. 0x4f, 0x7c, 0x59, 0x19, 0x99, 0x65, 0xa8, 0xdd, 0x4a, 0x20, 0x91, 0x84,
  14047. 0x7d, 0x2d, 0x22, 0xdf, 0x3e, 0xe5, 0x5f, 0xaa, 0x2a, 0x3f, 0xb3, 0x3f,
  14048. 0xd2, 0xd1, 0xe0, 0x55, 0xa0, 0x7a, 0x7c, 0x61, 0xec, 0xfb, 0x8d, 0x80,
  14049. 0xec, 0x00, 0xc2, 0xc9, 0xeb, 0x12
  14050. };
  14051. static const unsigned char ecjpake_test_srv_two[] = {
  14052. 0x03, 0x00, 0x17, 0x41, 0x04, 0x0f, 0xb2, 0x2b, 0x1d, 0x5d, 0x11, 0x23,
  14053. 0xe0, 0xef, 0x9f, 0xeb, 0x9d, 0x8a, 0x2e, 0x59, 0x0a, 0x1f, 0x4d, 0x7c,
  14054. 0xed, 0x2c, 0x2b, 0x06, 0x58, 0x6e, 0x8f, 0x2a, 0x16, 0xd4, 0xeb, 0x2f,
  14055. 0xda, 0x43, 0x28, 0xa2, 0x0b, 0x07, 0xd8, 0xfd, 0x66, 0x76, 0x54, 0xca,
  14056. 0x18, 0xc5, 0x4e, 0x32, 0xa3, 0x33, 0xa0, 0x84, 0x54, 0x51, 0xe9, 0x26,
  14057. 0xee, 0x88, 0x04, 0xfd, 0x7a, 0xf0, 0xaa, 0xa7, 0xa6, 0x41, 0x04, 0x55,
  14058. 0x16, 0xea, 0x3e, 0x54, 0xa0, 0xd5, 0xd8, 0xb2, 0xce, 0x78, 0x6b, 0x38,
  14059. 0xd3, 0x83, 0x37, 0x00, 0x29, 0xa5, 0xdb, 0xe4, 0x45, 0x9c, 0x9d, 0xd6,
  14060. 0x01, 0xb4, 0x08, 0xa2, 0x4a, 0xe6, 0x46, 0x5c, 0x8a, 0xc9, 0x05, 0xb9,
  14061. 0xeb, 0x03, 0xb5, 0xd3, 0x69, 0x1c, 0x13, 0x9e, 0xf8, 0x3f, 0x1c, 0xd4,
  14062. 0x20, 0x0f, 0x6c, 0x9c, 0xd4, 0xec, 0x39, 0x22, 0x18, 0xa5, 0x9e, 0xd2,
  14063. 0x43, 0xd3, 0xc8, 0x20, 0xff, 0x72, 0x4a, 0x9a, 0x70, 0xb8, 0x8c, 0xb8,
  14064. 0x6f, 0x20, 0xb4, 0x34, 0xc6, 0x86, 0x5a, 0xa1, 0xcd, 0x79, 0x06, 0xdd,
  14065. 0x7c, 0x9b, 0xce, 0x35, 0x25, 0xf5, 0x08, 0x27, 0x6f, 0x26, 0x83, 0x6c
  14066. };
  14067. static const unsigned char ecjpake_test_cli_two[] = {
  14068. 0x41, 0x04, 0x69, 0xd5, 0x4e, 0xe8, 0x5e, 0x90, 0xce, 0x3f, 0x12, 0x46,
  14069. 0x74, 0x2d, 0xe5, 0x07, 0xe9, 0x39, 0xe8, 0x1d, 0x1d, 0xc1, 0xc5, 0xcb,
  14070. 0x98, 0x8b, 0x58, 0xc3, 0x10, 0xc9, 0xfd, 0xd9, 0x52, 0x4d, 0x93, 0x72,
  14071. 0x0b, 0x45, 0x54, 0x1c, 0x83, 0xee, 0x88, 0x41, 0x19, 0x1d, 0xa7, 0xce,
  14072. 0xd8, 0x6e, 0x33, 0x12, 0xd4, 0x36, 0x23, 0xc1, 0xd6, 0x3e, 0x74, 0x98,
  14073. 0x9a, 0xba, 0x4a, 0xff, 0xd1, 0xee, 0x41, 0x04, 0x07, 0x7e, 0x8c, 0x31,
  14074. 0xe2, 0x0e, 0x6b, 0xed, 0xb7, 0x60, 0xc1, 0x35, 0x93, 0xe6, 0x9f, 0x15,
  14075. 0xbe, 0x85, 0xc2, 0x7d, 0x68, 0xcd, 0x09, 0xcc, 0xb8, 0xc4, 0x18, 0x36,
  14076. 0x08, 0x91, 0x7c, 0x5c, 0x3d, 0x40, 0x9f, 0xac, 0x39, 0xfe, 0xfe, 0xe8,
  14077. 0x2f, 0x72, 0x92, 0xd3, 0x6f, 0x0d, 0x23, 0xe0, 0x55, 0x91, 0x3f, 0x45,
  14078. 0xa5, 0x2b, 0x85, 0xdd, 0x8a, 0x20, 0x52, 0xe9, 0xe1, 0x29, 0xbb, 0x4d,
  14079. 0x20, 0x0f, 0x01, 0x1f, 0x19, 0x48, 0x35, 0x35, 0xa6, 0xe8, 0x9a, 0x58,
  14080. 0x0c, 0x9b, 0x00, 0x03, 0xba, 0xf2, 0x14, 0x62, 0xec, 0xe9, 0x1a, 0x82,
  14081. 0xcc, 0x38, 0xdb, 0xdc, 0xae, 0x60, 0xd9, 0xc5, 0x4c
  14082. };
  14083. static const unsigned char ecjpake_test_pms[] = {
  14084. 0xf3, 0xd4, 0x7f, 0x59, 0x98, 0x44, 0xdb, 0x92, 0xa5, 0x69, 0xbb, 0xe7,
  14085. 0x98, 0x1e, 0x39, 0xd9, 0x31, 0xfd, 0x74, 0x3b, 0xf2, 0x2e, 0x98, 0xf9,
  14086. 0xb4, 0x38, 0xf7, 0x19, 0xd3, 0xc4, 0xf3, 0x51
  14087. };
  14088. /* Load my private keys and generate the correponding public keys */
  14089. static int ecjpake_test_load( mbedtls_ecjpake_context *ctx,
  14090. const unsigned char *xm1, size_t len1,
  14091. const unsigned char *xm2, size_t len2 )
  14092. {
  14093. int ret;
  14094. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->xm1, xm1, len1 ) );
  14095. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->xm2, xm2, len2 ) );
  14096. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &ctx->grp, &ctx->Xm1, &ctx->xm1,
  14097. &ctx->grp.G, NULL, NULL ) );
  14098. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &ctx->grp, &ctx->Xm2, &ctx->xm2,
  14099. &ctx->grp.G, NULL, NULL ) );
  14100. cleanup:
  14101. return( ret );
  14102. }
  14103. /* For tests we don't need a secure RNG;
  14104. * use the LGC from Numerical Recipes for simplicity */
  14105. static int ecjpake_lgc( void *p, unsigned char *out, size_t len )
  14106. {
  14107. static uint32_t x = 42;
  14108. (void) p;
  14109. while( len > 0 )
  14110. {
  14111. size_t use_len = len > 4 ? 4 : len;
  14112. x = 1664525 * x + 1013904223;
  14113. memcpy( out, &x, use_len );
  14114. out += use_len;
  14115. len -= use_len;
  14116. }
  14117. return( 0 );
  14118. }
  14119. #define TEST_ASSERT( x ) \
  14120. do { \
  14121. if( x ) \
  14122. ret = 0; \
  14123. else \
  14124. { \
  14125. ret = 1; \
  14126. goto cleanup; \
  14127. } \
  14128. } while( 0 )
  14129. /*
  14130. * Checkup routine
  14131. */
  14132. int mbedtls_ecjpake_self_test( int verbose )
  14133. {
  14134. int ret;
  14135. mbedtls_ecjpake_context cli;
  14136. mbedtls_ecjpake_context srv;
  14137. unsigned char buf[512], pms[32];
  14138. size_t len, pmslen;
  14139. mbedtls_ecjpake_init( &cli );
  14140. mbedtls_ecjpake_init( &srv );
  14141. if( verbose != 0 )
  14142. mbedtls_printf( " ECJPAKE test #0 (setup): " );
  14143. TEST_ASSERT( mbedtls_ecjpake_setup( &cli, MBEDTLS_ECJPAKE_CLIENT,
  14144. MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1,
  14145. ecjpake_test_password,
  14146. sizeof( ecjpake_test_password ) ) == 0 );
  14147. TEST_ASSERT( mbedtls_ecjpake_setup( &srv, MBEDTLS_ECJPAKE_SERVER,
  14148. MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1,
  14149. ecjpake_test_password,
  14150. sizeof( ecjpake_test_password ) ) == 0 );
  14151. if( verbose != 0 )
  14152. mbedtls_printf( "passed\n" );
  14153. if( verbose != 0 )
  14154. mbedtls_printf( " ECJPAKE test #1 (random handshake): " );
  14155. TEST_ASSERT( mbedtls_ecjpake_write_round_one( &cli,
  14156. buf, sizeof( buf ), &len, ecjpake_lgc, NULL ) == 0 );
  14157. TEST_ASSERT( mbedtls_ecjpake_read_round_one( &srv, buf, len ) == 0 );
  14158. TEST_ASSERT( mbedtls_ecjpake_write_round_one( &srv,
  14159. buf, sizeof( buf ), &len, ecjpake_lgc, NULL ) == 0 );
  14160. TEST_ASSERT( mbedtls_ecjpake_read_round_one( &cli, buf, len ) == 0 );
  14161. TEST_ASSERT( mbedtls_ecjpake_write_round_two( &srv,
  14162. buf, sizeof( buf ), &len, ecjpake_lgc, NULL ) == 0 );
  14163. TEST_ASSERT( mbedtls_ecjpake_read_round_two( &cli, buf, len ) == 0 );
  14164. TEST_ASSERT( mbedtls_ecjpake_derive_secret( &cli,
  14165. pms, sizeof( pms ), &pmslen, ecjpake_lgc, NULL ) == 0 );
  14166. TEST_ASSERT( mbedtls_ecjpake_write_round_two( &cli,
  14167. buf, sizeof( buf ), &len, ecjpake_lgc, NULL ) == 0 );
  14168. TEST_ASSERT( mbedtls_ecjpake_read_round_two( &srv, buf, len ) == 0 );
  14169. TEST_ASSERT( mbedtls_ecjpake_derive_secret( &srv,
  14170. buf, sizeof( buf ), &len, ecjpake_lgc, NULL ) == 0 );
  14171. TEST_ASSERT( len == pmslen );
  14172. TEST_ASSERT( memcmp( buf, pms, len ) == 0 );
  14173. if( verbose != 0 )
  14174. mbedtls_printf( "passed\n" );
  14175. if( verbose != 0 )
  14176. mbedtls_printf( " ECJPAKE test #2 (reference handshake): " );
  14177. /* Simulate generation of round one */
  14178. MBEDTLS_MPI_CHK( ecjpake_test_load( &cli,
  14179. ecjpake_test_x1, sizeof( ecjpake_test_x1 ),
  14180. ecjpake_test_x2, sizeof( ecjpake_test_x2 ) ) );
  14181. MBEDTLS_MPI_CHK( ecjpake_test_load( &srv,
  14182. ecjpake_test_x3, sizeof( ecjpake_test_x3 ),
  14183. ecjpake_test_x4, sizeof( ecjpake_test_x4 ) ) );
  14184. /* Read round one */
  14185. TEST_ASSERT( mbedtls_ecjpake_read_round_one( &srv,
  14186. ecjpake_test_cli_one,
  14187. sizeof( ecjpake_test_cli_one ) ) == 0 );
  14188. TEST_ASSERT( mbedtls_ecjpake_read_round_one( &cli,
  14189. ecjpake_test_srv_one,
  14190. sizeof( ecjpake_test_srv_one ) ) == 0 );
  14191. /* Skip generation of round two, read round two */
  14192. TEST_ASSERT( mbedtls_ecjpake_read_round_two( &cli,
  14193. ecjpake_test_srv_two,
  14194. sizeof( ecjpake_test_srv_two ) ) == 0 );
  14195. TEST_ASSERT( mbedtls_ecjpake_read_round_two( &srv,
  14196. ecjpake_test_cli_two,
  14197. sizeof( ecjpake_test_cli_two ) ) == 0 );
  14198. /* Server derives PMS */
  14199. TEST_ASSERT( mbedtls_ecjpake_derive_secret( &srv,
  14200. buf, sizeof( buf ), &len, ecjpake_lgc, NULL ) == 0 );
  14201. TEST_ASSERT( len == sizeof( ecjpake_test_pms ) );
  14202. TEST_ASSERT( memcmp( buf, ecjpake_test_pms, len ) == 0 );
  14203. memset( buf, 0, len ); /* Avoid interferences with next step */
  14204. /* Client derives PMS */
  14205. TEST_ASSERT( mbedtls_ecjpake_derive_secret( &cli,
  14206. buf, sizeof( buf ), &len, ecjpake_lgc, NULL ) == 0 );
  14207. TEST_ASSERT( len == sizeof( ecjpake_test_pms ) );
  14208. TEST_ASSERT( memcmp( buf, ecjpake_test_pms, len ) == 0 );
  14209. if( verbose != 0 )
  14210. mbedtls_printf( "passed\n" );
  14211. cleanup:
  14212. mbedtls_ecjpake_free( &cli );
  14213. mbedtls_ecjpake_free( &srv );
  14214. if( ret != 0 )
  14215. {
  14216. if( verbose != 0 )
  14217. mbedtls_printf( "failed\n" );
  14218. ret = 1;
  14219. }
  14220. if( verbose != 0 )
  14221. mbedtls_printf( "\n" );
  14222. return( ret );
  14223. }
  14224. #undef TEST_ASSERT
  14225. #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED && MBEDTLS_SHA256_C */
  14226. #endif /* MBEDTLS_SELF_TEST */
  14227. #endif /* MBEDTLS_ECJPAKE_C */
  14228. /*
  14229. Amalgamated build undefines
  14230. */
  14231. #undef ADD
  14232. #undef BC
  14233. #undef BEFORE_COLON
  14234. #undef F
  14235. #undef F0
  14236. #undef F1
  14237. #undef F2
  14238. #undef F3
  14239. #undef F4
  14240. #undef F5
  14241. #undef FSb
  14242. #undef K
  14243. #undef KK
  14244. #undef P
  14245. #undef R
  14246. #undef ROTR
  14247. #undef S
  14248. #undef S0
  14249. #undef S1
  14250. #undef S2
  14251. #undef S3
  14252. #undef SAFE_SNPRINTF
  14253. #undef SHR
  14254. #undef close
  14255. #undef read
  14256. #undef supported_init
  14257. #undef write
  14258. /********* Start of file library/ecp.c ************/
  14259. /*
  14260. * Elliptic curves over GF(p): generic functions
  14261. *
  14262. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  14263. * SPDX-License-Identifier: Apache-2.0
  14264. *
  14265. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14266. * not use this file except in compliance with the License.
  14267. * You may obtain a copy of the License at
  14268. *
  14269. * http://www.apache.org/licenses/LICENSE-2.0
  14270. *
  14271. * Unless required by applicable law or agreed to in writing, software
  14272. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14273. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14274. * See the License for the specific language governing permissions and
  14275. * limitations under the License.
  14276. *
  14277. * This file is part of mbed TLS (https://tls.mbed.org)
  14278. */
  14279. /*
  14280. * References:
  14281. *
  14282. * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
  14283. * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
  14284. * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
  14285. * RFC 4492 for the related TLS structures and constants
  14286. *
  14287. * [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
  14288. *
  14289. * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
  14290. * for elliptic curve cryptosystems. In : Cryptographic Hardware and
  14291. * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
  14292. * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
  14293. *
  14294. * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
  14295. * render ECC resistant against Side Channel Attacks. IACR Cryptology
  14296. * ePrint Archive, 2004, vol. 2004, p. 342.
  14297. * <http://eprint.iacr.org/2004/342.pdf>
  14298. */
  14299. #if !defined(MBEDTLS_CONFIG_FILE)
  14300. #else
  14301. #endif
  14302. #if defined(MBEDTLS_ECP_C)
  14303. #include <string.h>
  14304. #if !defined(MBEDTLS_ECP_ALT)
  14305. #if defined(MBEDTLS_PLATFORM_C)
  14306. #else
  14307. #include <stdlib.h>
  14308. #include <stdio.h>
  14309. #define mbedtls_printf printf
  14310. #define mbedtls_calloc calloc
  14311. #define mbedtls_free free
  14312. #endif
  14313. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  14314. !defined(inline) && !defined(__cplusplus)
  14315. #define inline __inline
  14316. #endif
  14317. /* Implementation that should never be optimized out by the compiler */
  14318. /* zeroize was here */
  14319. #if defined(MBEDTLS_SELF_TEST)
  14320. /*
  14321. * Counts of point addition and doubling, and field multiplications.
  14322. * Used to test resistance of point multiplication to simple timing attacks.
  14323. */
  14324. static unsigned long add_count, dbl_count, mul_count;
  14325. #endif
  14326. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \
  14327. defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
  14328. defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
  14329. defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \
  14330. defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \
  14331. defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \
  14332. defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \
  14333. defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \
  14334. defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
  14335. defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
  14336. defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
  14337. #define ECP_SHORTWEIERSTRASS
  14338. #endif
  14339. #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
  14340. #define ECP_MONTGOMERY
  14341. #endif
  14342. /*
  14343. * Curve types: internal for now, might be exposed later
  14344. */
  14345. typedef enum
  14346. {
  14347. ECP_TYPE_NONE = 0,
  14348. ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
  14349. ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
  14350. } ecp_curve_type;
  14351. /*
  14352. * List of supported curves:
  14353. * - internal ID
  14354. * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
  14355. * - size in bits
  14356. * - readable name
  14357. *
  14358. * Curves are listed in order: largest curves first, and for a given size,
  14359. * fastest curves first. This provides the default order for the SSL module.
  14360. *
  14361. * Reminder: update profiles in x509_crt.c when adding a new curves!
  14362. */
  14363. static const mbedtls_ecp_curve_info ecp_supported_curves[] =
  14364. {
  14365. #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
  14366. { MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
  14367. #endif
  14368. #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
  14369. { MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
  14370. #endif
  14371. #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
  14372. { MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
  14373. #endif
  14374. #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
  14375. { MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
  14376. #endif
  14377. #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
  14378. { MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
  14379. #endif
  14380. #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
  14381. { MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
  14382. #endif
  14383. #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
  14384. { MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
  14385. #endif
  14386. #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
  14387. { MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
  14388. #endif
  14389. #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
  14390. { MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
  14391. #endif
  14392. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
  14393. { MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
  14394. #endif
  14395. #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
  14396. { MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
  14397. #endif
  14398. { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
  14399. };
  14400. #define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \
  14401. sizeof( ecp_supported_curves[0] )
  14402. static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
  14403. /*
  14404. * List of supported curves and associated info
  14405. */
  14406. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void )
  14407. {
  14408. return( ecp_supported_curves );
  14409. }
  14410. /*
  14411. * List of supported curves, group ID only
  14412. */
  14413. const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void )
  14414. {
  14415. static int init_done = 0;
  14416. if( ! init_done )
  14417. {
  14418. size_t i = 0;
  14419. const mbedtls_ecp_curve_info *curve_info;
  14420. for( curve_info = mbedtls_ecp_curve_list();
  14421. curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
  14422. curve_info++ )
  14423. {
  14424. ecp_supported_grp_id[i++] = curve_info->grp_id;
  14425. }
  14426. ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
  14427. init_done = 1;
  14428. }
  14429. return( ecp_supported_grp_id );
  14430. }
  14431. /*
  14432. * Get the curve info for the internal identifier
  14433. */
  14434. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id )
  14435. {
  14436. const mbedtls_ecp_curve_info *curve_info;
  14437. for( curve_info = mbedtls_ecp_curve_list();
  14438. curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
  14439. curve_info++ )
  14440. {
  14441. if( curve_info->grp_id == grp_id )
  14442. return( curve_info );
  14443. }
  14444. return( NULL );
  14445. }
  14446. /*
  14447. * Get the curve info from the TLS identifier
  14448. */
  14449. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id )
  14450. {
  14451. const mbedtls_ecp_curve_info *curve_info;
  14452. for( curve_info = mbedtls_ecp_curve_list();
  14453. curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
  14454. curve_info++ )
  14455. {
  14456. if( curve_info->tls_id == tls_id )
  14457. return( curve_info );
  14458. }
  14459. return( NULL );
  14460. }
  14461. /*
  14462. * Get the curve info from the name
  14463. */
  14464. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name )
  14465. {
  14466. const mbedtls_ecp_curve_info *curve_info;
  14467. for( curve_info = mbedtls_ecp_curve_list();
  14468. curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
  14469. curve_info++ )
  14470. {
  14471. if( strcmp( curve_info->name, name ) == 0 )
  14472. return( curve_info );
  14473. }
  14474. return( NULL );
  14475. }
  14476. /*
  14477. * Get the type of a curve
  14478. */
  14479. static inline ecp_curve_type ecp_get_type( const mbedtls_ecp_group *grp )
  14480. {
  14481. if( grp->G.X.p == NULL )
  14482. return( ECP_TYPE_NONE );
  14483. if( grp->G.Y.p == NULL )
  14484. return( ECP_TYPE_MONTGOMERY );
  14485. else
  14486. return( ECP_TYPE_SHORT_WEIERSTRASS );
  14487. }
  14488. /*
  14489. * Initialize (the components of) a point
  14490. */
  14491. void mbedtls_ecp_point_init( mbedtls_ecp_point *pt )
  14492. {
  14493. if( pt == NULL )
  14494. return;
  14495. mbedtls_mpi_init( &pt->X );
  14496. mbedtls_mpi_init( &pt->Y );
  14497. mbedtls_mpi_init( &pt->Z );
  14498. }
  14499. /*
  14500. * Initialize (the components of) a group
  14501. */
  14502. void mbedtls_ecp_group_init( mbedtls_ecp_group *grp )
  14503. {
  14504. if( grp == NULL )
  14505. return;
  14506. memset( grp, 0, sizeof( mbedtls_ecp_group ) );
  14507. }
  14508. /*
  14509. * Initialize (the components of) a key pair
  14510. */
  14511. void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key )
  14512. {
  14513. if( key == NULL )
  14514. return;
  14515. mbedtls_ecp_group_init( &key->grp );
  14516. mbedtls_mpi_init( &key->d );
  14517. mbedtls_ecp_point_init( &key->Q );
  14518. }
  14519. /*
  14520. * Unallocate (the components of) a point
  14521. */
  14522. void mbedtls_ecp_point_free( mbedtls_ecp_point *pt )
  14523. {
  14524. if( pt == NULL )
  14525. return;
  14526. mbedtls_mpi_free( &( pt->X ) );
  14527. mbedtls_mpi_free( &( pt->Y ) );
  14528. mbedtls_mpi_free( &( pt->Z ) );
  14529. }
  14530. /*
  14531. * Unallocate (the components of) a group
  14532. */
  14533. void mbedtls_ecp_group_free( mbedtls_ecp_group *grp )
  14534. {
  14535. size_t i;
  14536. if( grp == NULL )
  14537. return;
  14538. if( grp->h != 1 )
  14539. {
  14540. mbedtls_mpi_free( &grp->P );
  14541. mbedtls_mpi_free( &grp->A );
  14542. mbedtls_mpi_free( &grp->B );
  14543. mbedtls_ecp_point_free( &grp->G );
  14544. mbedtls_mpi_free( &grp->N );
  14545. }
  14546. if( grp->T != NULL )
  14547. {
  14548. for( i = 0; i < grp->T_size; i++ )
  14549. mbedtls_ecp_point_free( &grp->T[i] );
  14550. mbedtls_free( grp->T );
  14551. }
  14552. mbedtls_zeroize( grp, sizeof( mbedtls_ecp_group ) );
  14553. }
  14554. /*
  14555. * Unallocate (the components of) a key pair
  14556. */
  14557. void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key )
  14558. {
  14559. if( key == NULL )
  14560. return;
  14561. mbedtls_ecp_group_free( &key->grp );
  14562. mbedtls_mpi_free( &key->d );
  14563. mbedtls_ecp_point_free( &key->Q );
  14564. }
  14565. /*
  14566. * Copy the contents of a point
  14567. */
  14568. int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
  14569. {
  14570. int ret;
  14571. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->X, &Q->X ) );
  14572. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Y, &Q->Y ) );
  14573. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Z, &Q->Z ) );
  14574. cleanup:
  14575. return( ret );
  14576. }
  14577. /*
  14578. * Copy the contents of a group object
  14579. */
  14580. int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src )
  14581. {
  14582. return mbedtls_ecp_group_load( dst, src->id );
  14583. }
  14584. /*
  14585. * Set point to zero
  14586. */
  14587. int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt )
  14588. {
  14589. int ret;
  14590. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->X , 1 ) );
  14591. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Y , 1 ) );
  14592. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z , 0 ) );
  14593. cleanup:
  14594. return( ret );
  14595. }
  14596. /*
  14597. * Tell if a point is zero
  14598. */
  14599. int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt )
  14600. {
  14601. return( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 );
  14602. }
  14603. /*
  14604. * Compare two points lazyly
  14605. */
  14606. int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
  14607. const mbedtls_ecp_point *Q )
  14608. {
  14609. if( mbedtls_mpi_cmp_mpi( &P->X, &Q->X ) == 0 &&
  14610. mbedtls_mpi_cmp_mpi( &P->Y, &Q->Y ) == 0 &&
  14611. mbedtls_mpi_cmp_mpi( &P->Z, &Q->Z ) == 0 )
  14612. {
  14613. return( 0 );
  14614. }
  14615. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  14616. }
  14617. /*
  14618. * Import a non-zero point from ASCII strings
  14619. */
  14620. int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
  14621. const char *x, const char *y )
  14622. {
  14623. int ret;
  14624. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->X, radix, x ) );
  14625. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->Y, radix, y ) );
  14626. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
  14627. cleanup:
  14628. return( ret );
  14629. }
  14630. /*
  14631. * Export a point into unsigned binary data (SEC1 2.3.3)
  14632. */
  14633. int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
  14634. int format, size_t *olen,
  14635. unsigned char *buf, size_t buflen )
  14636. {
  14637. int ret = 0;
  14638. size_t plen;
  14639. if( format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
  14640. format != MBEDTLS_ECP_PF_COMPRESSED )
  14641. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  14642. /*
  14643. * Common case: P == 0
  14644. */
  14645. if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
  14646. {
  14647. if( buflen < 1 )
  14648. return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
  14649. buf[0] = 0x00;
  14650. *olen = 1;
  14651. return( 0 );
  14652. }
  14653. plen = mbedtls_mpi_size( &grp->P );
  14654. if( format == MBEDTLS_ECP_PF_UNCOMPRESSED )
  14655. {
  14656. *olen = 2 * plen + 1;
  14657. if( buflen < *olen )
  14658. return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
  14659. buf[0] = 0x04;
  14660. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
  14661. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
  14662. }
  14663. else if( format == MBEDTLS_ECP_PF_COMPRESSED )
  14664. {
  14665. *olen = plen + 1;
  14666. if( buflen < *olen )
  14667. return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
  14668. buf[0] = 0x02 + mbedtls_mpi_get_bit( &P->Y, 0 );
  14669. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
  14670. }
  14671. cleanup:
  14672. return( ret );
  14673. }
  14674. /*
  14675. * Import a point from unsigned binary data (SEC1 2.3.4)
  14676. */
  14677. int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
  14678. const unsigned char *buf, size_t ilen )
  14679. {
  14680. int ret;
  14681. size_t plen;
  14682. if( ilen < 1 )
  14683. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  14684. if( buf[0] == 0x00 )
  14685. {
  14686. if( ilen == 1 )
  14687. return( mbedtls_ecp_set_zero( pt ) );
  14688. else
  14689. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  14690. }
  14691. plen = mbedtls_mpi_size( &grp->P );
  14692. if( buf[0] != 0x04 )
  14693. return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
  14694. if( ilen != 2 * plen + 1 )
  14695. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  14696. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->X, buf + 1, plen ) );
  14697. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
  14698. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
  14699. cleanup:
  14700. return( ret );
  14701. }
  14702. /*
  14703. * Import a point from a TLS ECPoint record (RFC 4492)
  14704. * struct {
  14705. * opaque point <1..2^8-1>;
  14706. * } ECPoint;
  14707. */
  14708. int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
  14709. const unsigned char **buf, size_t buf_len )
  14710. {
  14711. unsigned char data_len;
  14712. const unsigned char *buf_start;
  14713. /*
  14714. * We must have at least two bytes (1 for length, at least one for data)
  14715. */
  14716. if( buf_len < 2 )
  14717. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  14718. data_len = *(*buf)++;
  14719. if( data_len < 1 || data_len > buf_len - 1 )
  14720. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  14721. /*
  14722. * Save buffer start for read_binary and update buf
  14723. */
  14724. buf_start = *buf;
  14725. *buf += data_len;
  14726. return mbedtls_ecp_point_read_binary( grp, pt, buf_start, data_len );
  14727. }
  14728. /*
  14729. * Export a point as a TLS ECPoint record (RFC 4492)
  14730. * struct {
  14731. * opaque point <1..2^8-1>;
  14732. * } ECPoint;
  14733. */
  14734. int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
  14735. int format, size_t *olen,
  14736. unsigned char *buf, size_t blen )
  14737. {
  14738. int ret;
  14739. /*
  14740. * buffer length must be at least one, for our length byte
  14741. */
  14742. if( blen < 1 )
  14743. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  14744. if( ( ret = mbedtls_ecp_point_write_binary( grp, pt, format,
  14745. olen, buf + 1, blen - 1) ) != 0 )
  14746. return( ret );
  14747. /*
  14748. * write length to the first byte and update total length
  14749. */
  14750. buf[0] = (unsigned char) *olen;
  14751. ++*olen;
  14752. return( 0 );
  14753. }
  14754. /*
  14755. * Set a group from an ECParameters record (RFC 4492)
  14756. */
  14757. int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len )
  14758. {
  14759. uint16_t tls_id;
  14760. const mbedtls_ecp_curve_info *curve_info;
  14761. /*
  14762. * We expect at least three bytes (see below)
  14763. */
  14764. if( len < 3 )
  14765. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  14766. /*
  14767. * First byte is curve_type; only named_curve is handled
  14768. */
  14769. if( *(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
  14770. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  14771. /*
  14772. * Next two bytes are the namedcurve value
  14773. */
  14774. tls_id = *(*buf)++;
  14775. tls_id <<= 8;
  14776. tls_id |= *(*buf)++;
  14777. if( ( curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
  14778. return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
  14779. return mbedtls_ecp_group_load( grp, curve_info->grp_id );
  14780. }
  14781. /*
  14782. * Write the ECParameters record corresponding to a group (RFC 4492)
  14783. */
  14784. int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
  14785. unsigned char *buf, size_t blen )
  14786. {
  14787. const mbedtls_ecp_curve_info *curve_info;
  14788. if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
  14789. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  14790. /*
  14791. * We are going to write 3 bytes (see below)
  14792. */
  14793. *olen = 3;
  14794. if( blen < *olen )
  14795. return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
  14796. /*
  14797. * First byte is curve_type, always named_curve
  14798. */
  14799. *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
  14800. /*
  14801. * Next two bytes are the namedcurve value
  14802. */
  14803. buf[0] = curve_info->tls_id >> 8;
  14804. buf[1] = curve_info->tls_id & 0xFF;
  14805. return( 0 );
  14806. }
  14807. /*
  14808. * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
  14809. * See the documentation of struct mbedtls_ecp_group.
  14810. *
  14811. * This function is in the critial loop for mbedtls_ecp_mul, so pay attention to perf.
  14812. */
  14813. static int ecp_modp( mbedtls_mpi *N, const mbedtls_ecp_group *grp )
  14814. {
  14815. int ret;
  14816. if( grp->modp == NULL )
  14817. return( mbedtls_mpi_mod_mpi( N, N, &grp->P ) );
  14818. /* N->s < 0 is a much faster test, which fails only if N is 0 */
  14819. if( ( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 ) ||
  14820. mbedtls_mpi_bitlen( N ) > 2 * grp->pbits )
  14821. {
  14822. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  14823. }
  14824. MBEDTLS_MPI_CHK( grp->modp( N ) );
  14825. /* N->s < 0 is a much faster test, which fails only if N is 0 */
  14826. while( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 )
  14827. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N, N, &grp->P ) );
  14828. while( mbedtls_mpi_cmp_mpi( N, &grp->P ) >= 0 )
  14829. /* we known P, N and the result are positive */
  14830. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N, N, &grp->P ) );
  14831. cleanup:
  14832. return( ret );
  14833. }
  14834. /*
  14835. * Fast mod-p functions expect their argument to be in the 0..p^2 range.
  14836. *
  14837. * In order to guarantee that, we need to ensure that operands of
  14838. * mbedtls_mpi_mul_mpi are in the 0..p range. So, after each operation we will
  14839. * bring the result back to this range.
  14840. *
  14841. * The following macros are shortcuts for doing that.
  14842. */
  14843. /*
  14844. * Reduce a mbedtls_mpi mod p in-place, general case, to use after mbedtls_mpi_mul_mpi
  14845. */
  14846. #if defined(MBEDTLS_SELF_TEST)
  14847. #define INC_MUL_COUNT mul_count++;
  14848. #else
  14849. #define INC_MUL_COUNT
  14850. #endif
  14851. #define MOD_MUL( N ) do { MBEDTLS_MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
  14852. while( 0 )
  14853. /*
  14854. * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
  14855. * N->s < 0 is a very fast test, which fails only if N is 0
  14856. */
  14857. #define MOD_SUB( N ) \
  14858. while( N.s < 0 && mbedtls_mpi_cmp_int( &N, 0 ) != 0 ) \
  14859. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &N, &N, &grp->P ) )
  14860. /*
  14861. * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_add_mpi and mbedtls_mpi_mul_int.
  14862. * We known P, N and the result are positive, so sub_abs is correct, and
  14863. * a bit faster.
  14864. */
  14865. #define MOD_ADD( N ) \
  14866. while( mbedtls_mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
  14867. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &N, &N, &grp->P ) )
  14868. #if defined(ECP_SHORTWEIERSTRASS)
  14869. /*
  14870. * For curves in short Weierstrass form, we do all the internal operations in
  14871. * Jacobian coordinates.
  14872. *
  14873. * For multiplication, we'll use a comb method with coutermeasueres against
  14874. * SPA, hence timing attacks.
  14875. */
  14876. /*
  14877. * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
  14878. * Cost: 1N := 1I + 3M + 1S
  14879. */
  14880. static int ecp_normalize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt )
  14881. {
  14882. int ret;
  14883. mbedtls_mpi Zi, ZZi;
  14884. if( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 )
  14885. return( 0 );
  14886. #if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
  14887. if ( mbedtls_internal_ecp_grp_capable( grp ) )
  14888. {
  14889. return mbedtls_internal_ecp_normalize_jac( grp, pt );
  14890. }
  14891. #endif /* MBEDTLS_ECP_NORMALIZE_JAC_ALT */
  14892. mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
  14893. /*
  14894. * X = X / Z^2 mod p
  14895. */
  14896. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
  14897. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
  14898. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
  14899. /*
  14900. * Y = Y / Z^3 mod p
  14901. */
  14902. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
  14903. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
  14904. /*
  14905. * Z = 1
  14906. */
  14907. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
  14908. cleanup:
  14909. mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
  14910. return( ret );
  14911. }
  14912. /*
  14913. * Normalize jacobian coordinates of an array of (pointers to) points,
  14914. * using Montgomery's trick to perform only one inversion mod P.
  14915. * (See for example Cohen's "A Course in Computational Algebraic Number
  14916. * Theory", Algorithm 10.3.4.)
  14917. *
  14918. * Warning: fails (returning an error) if one of the points is zero!
  14919. * This should never happen, see choice of w in ecp_mul_comb().
  14920. *
  14921. * Cost: 1N(t) := 1I + (6t - 3)M + 1S
  14922. */
  14923. static int ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
  14924. mbedtls_ecp_point *T[], size_t t_len )
  14925. {
  14926. int ret;
  14927. size_t i;
  14928. mbedtls_mpi *c, u, Zi, ZZi;
  14929. if( t_len < 2 )
  14930. return( ecp_normalize_jac( grp, *T ) );
  14931. #if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
  14932. if ( mbedtls_internal_ecp_grp_capable( grp ) )
  14933. {
  14934. return mbedtls_internal_ecp_normalize_jac_many(grp, T, t_len);
  14935. }
  14936. #endif
  14937. if( ( c = mbedtls_calloc( t_len, sizeof( mbedtls_mpi ) ) ) == NULL )
  14938. return( MBEDTLS_ERR_ECP_ALLOC_FAILED );
  14939. mbedtls_mpi_init( &u ); mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
  14940. /*
  14941. * c[i] = Z_0 * ... * Z_i
  14942. */
  14943. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &c[0], &T[0]->Z ) );
  14944. for( i = 1; i < t_len; i++ )
  14945. {
  14946. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
  14947. MOD_MUL( c[i] );
  14948. }
  14949. /*
  14950. * u = 1 / (Z_0 * ... * Z_n) mod P
  14951. */
  14952. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
  14953. for( i = t_len - 1; ; i-- )
  14954. {
  14955. /*
  14956. * Zi = 1 / Z_i mod p
  14957. * u = 1 / (Z_0 * ... * Z_i) mod P
  14958. */
  14959. if( i == 0 ) {
  14960. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Zi, &u ) );
  14961. }
  14962. else
  14963. {
  14964. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
  14965. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
  14966. }
  14967. /*
  14968. * proceed as in normalize()
  14969. */
  14970. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
  14971. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
  14972. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
  14973. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
  14974. /*
  14975. * Post-precessing: reclaim some memory by shrinking coordinates
  14976. * - not storing Z (always 1)
  14977. * - shrinking other coordinates, but still keeping the same number of
  14978. * limbs as P, as otherwise it will too likely be regrown too fast.
  14979. */
  14980. MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->X, grp->P.n ) );
  14981. MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->Y, grp->P.n ) );
  14982. mbedtls_mpi_free( &T[i]->Z );
  14983. if( i == 0 )
  14984. break;
  14985. }
  14986. cleanup:
  14987. mbedtls_mpi_free( &u ); mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
  14988. for( i = 0; i < t_len; i++ )
  14989. mbedtls_mpi_free( &c[i] );
  14990. mbedtls_free( c );
  14991. return( ret );
  14992. }
  14993. /*
  14994. * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
  14995. * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
  14996. */
  14997. static int ecp_safe_invert_jac( const mbedtls_ecp_group *grp,
  14998. mbedtls_ecp_point *Q,
  14999. unsigned char inv )
  15000. {
  15001. int ret;
  15002. unsigned char nonzero;
  15003. mbedtls_mpi mQY;
  15004. mbedtls_mpi_init( &mQY );
  15005. /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
  15006. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
  15007. nonzero = mbedtls_mpi_cmp_int( &Q->Y, 0 ) != 0;
  15008. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
  15009. cleanup:
  15010. mbedtls_mpi_free( &mQY );
  15011. return( ret );
  15012. }
  15013. /*
  15014. * Point doubling R = 2 P, Jacobian coordinates
  15015. *
  15016. * Based on http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2 .
  15017. *
  15018. * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
  15019. * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
  15020. *
  15021. * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
  15022. *
  15023. * Cost: 1D := 3M + 4S (A == 0)
  15024. * 4M + 4S (A == -3)
  15025. * 3M + 6S + 1a otherwise
  15026. */
  15027. static int ecp_double_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  15028. const mbedtls_ecp_point *P )
  15029. {
  15030. int ret;
  15031. mbedtls_mpi M, S, T, U;
  15032. #if defined(MBEDTLS_SELF_TEST)
  15033. dbl_count++;
  15034. #endif
  15035. #if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
  15036. if ( mbedtls_internal_ecp_grp_capable( grp ) )
  15037. {
  15038. return mbedtls_internal_ecp_double_jac( grp, R, P );
  15039. }
  15040. #endif /* MBEDTLS_ECP_DOUBLE_JAC_ALT */
  15041. mbedtls_mpi_init( &M ); mbedtls_mpi_init( &S ); mbedtls_mpi_init( &T ); mbedtls_mpi_init( &U );
  15042. /* Special case for A = -3 */
  15043. if( grp->A.p == NULL )
  15044. {
  15045. /* M = 3(X + Z^2)(X - Z^2) */
  15046. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
  15047. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &P->X, &S ) ); MOD_ADD( T );
  15048. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U, &P->X, &S ) ); MOD_SUB( U );
  15049. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &U ) ); MOD_MUL( S );
  15050. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
  15051. }
  15052. else
  15053. {
  15054. /* M = 3.X^2 */
  15055. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &P->X ) ); MOD_MUL( S );
  15056. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
  15057. /* Optimize away for "koblitz" curves with A = 0 */
  15058. if( mbedtls_mpi_cmp_int( &grp->A, 0 ) != 0 )
  15059. {
  15060. /* M += A.Z^4 */
  15061. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
  15062. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &S, &S ) ); MOD_MUL( T );
  15063. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &grp->A ) ); MOD_MUL( S );
  15064. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &M, &M, &S ) ); MOD_ADD( M );
  15065. }
  15066. }
  15067. /* S = 4.X.Y^2 */
  15068. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &P->Y, &P->Y ) ); MOD_MUL( T );
  15069. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T, 1 ) ); MOD_ADD( T );
  15070. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &T ) ); MOD_MUL( S );
  15071. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &S, 1 ) ); MOD_ADD( S );
  15072. /* U = 8.Y^4 */
  15073. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &T, &T ) ); MOD_MUL( U );
  15074. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
  15075. /* T = M^2 - 2.S */
  15076. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &M, &M ) ); MOD_MUL( T );
  15077. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
  15078. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
  15079. /* S = M(S - T) - U */
  15080. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &T ) ); MOD_SUB( S );
  15081. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &S, &M ) ); MOD_MUL( S );
  15082. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &U ) ); MOD_SUB( S );
  15083. /* U = 2.Y.Z */
  15084. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &P->Y, &P->Z ) ); MOD_MUL( U );
  15085. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
  15086. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &T ) );
  15087. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &S ) );
  15088. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &U ) );
  15089. cleanup:
  15090. mbedtls_mpi_free( &M ); mbedtls_mpi_free( &S ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &U );
  15091. return( ret );
  15092. }
  15093. /*
  15094. * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
  15095. *
  15096. * The coordinates of Q must be normalized (= affine),
  15097. * but those of P don't need to. R is not normalized.
  15098. *
  15099. * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
  15100. * None of these cases can happen as intermediate step in ecp_mul_comb():
  15101. * - at each step, P, Q and R are multiples of the base point, the factor
  15102. * being less than its order, so none of them is zero;
  15103. * - Q is an odd multiple of the base point, P an even multiple,
  15104. * due to the choice of precomputed points in the modified comb method.
  15105. * So branches for these cases do not leak secret information.
  15106. *
  15107. * We accept Q->Z being unset (saving memory in tables) as meaning 1.
  15108. *
  15109. * Cost: 1A := 8M + 3S
  15110. */
  15111. static int ecp_add_mixed( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  15112. const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
  15113. {
  15114. int ret;
  15115. mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
  15116. #if defined(MBEDTLS_SELF_TEST)
  15117. add_count++;
  15118. #endif
  15119. #if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
  15120. if ( mbedtls_internal_ecp_grp_capable( grp ) )
  15121. {
  15122. return mbedtls_internal_ecp_add_mixed( grp, R, P, Q );
  15123. }
  15124. #endif /* MBEDTLS_ECP_ADD_MIXED_ALT */
  15125. /*
  15126. * Trivial cases: P == 0 or Q == 0 (case 1)
  15127. */
  15128. if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
  15129. return( mbedtls_ecp_copy( R, Q ) );
  15130. if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 0 ) == 0 )
  15131. return( mbedtls_ecp_copy( R, P ) );
  15132. /*
  15133. * Make sure Q coordinates are normalized
  15134. */
  15135. if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 1 ) != 0 )
  15136. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  15137. mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); mbedtls_mpi_init( &T3 ); mbedtls_mpi_init( &T4 );
  15138. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
  15139. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
  15140. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
  15141. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
  15142. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
  15143. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
  15144. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
  15145. /* Special cases (2) and (3) */
  15146. if( mbedtls_mpi_cmp_int( &T1, 0 ) == 0 )
  15147. {
  15148. if( mbedtls_mpi_cmp_int( &T2, 0 ) == 0 )
  15149. {
  15150. ret = ecp_double_jac( grp, R, P );
  15151. goto cleanup;
  15152. }
  15153. else
  15154. {
  15155. ret = mbedtls_ecp_set_zero( R );
  15156. goto cleanup;
  15157. }
  15158. }
  15159. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
  15160. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
  15161. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
  15162. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
  15163. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
  15164. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
  15165. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
  15166. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
  15167. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
  15168. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
  15169. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
  15170. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
  15171. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &X ) );
  15172. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &Y ) );
  15173. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &Z ) );
  15174. cleanup:
  15175. mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); mbedtls_mpi_free( &T3 ); mbedtls_mpi_free( &T4 );
  15176. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
  15177. return( ret );
  15178. }
  15179. /*
  15180. * Randomize jacobian coordinates:
  15181. * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
  15182. * This is sort of the reverse operation of ecp_normalize_jac().
  15183. *
  15184. * This countermeasure was first suggested in [2].
  15185. */
  15186. static int ecp_randomize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
  15187. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  15188. {
  15189. int ret;
  15190. mbedtls_mpi l, ll;
  15191. size_t p_size;
  15192. int count = 0;
  15193. #if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
  15194. if ( mbedtls_internal_ecp_grp_capable( grp ) )
  15195. {
  15196. return mbedtls_internal_ecp_randomize_jac( grp, pt, f_rng, p_rng );
  15197. }
  15198. #endif /* MBEDTLS_ECP_RANDOMIZE_JAC_ALT */
  15199. p_size = ( grp->pbits + 7 ) / 8;
  15200. mbedtls_mpi_init( &l ); mbedtls_mpi_init( &ll );
  15201. /* Generate l such that 1 < l < p */
  15202. do
  15203. {
  15204. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
  15205. while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
  15206. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
  15207. if( count++ > 10 )
  15208. return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
  15209. }
  15210. while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
  15211. /* Z = l * Z */
  15212. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
  15213. /* X = l^2 * X */
  15214. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
  15215. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
  15216. /* Y = l^3 * Y */
  15217. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
  15218. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
  15219. cleanup:
  15220. mbedtls_mpi_free( &l ); mbedtls_mpi_free( &ll );
  15221. return( ret );
  15222. }
  15223. /*
  15224. * Check and define parameters used by the comb method (see below for details)
  15225. */
  15226. #if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
  15227. #error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
  15228. #endif
  15229. /* d = ceil( n / w ) */
  15230. #define COMB_MAX_D ( MBEDTLS_ECP_MAX_BITS + 1 ) / 2
  15231. /* number of precomputed points */
  15232. #define COMB_MAX_PRE ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
  15233. /*
  15234. * Compute the representation of m that will be used with our comb method.
  15235. *
  15236. * The basic comb method is described in GECC 3.44 for example. We use a
  15237. * modified version that provides resistance to SPA by avoiding zero
  15238. * digits in the representation as in [3]. We modify the method further by
  15239. * requiring that all K_i be odd, which has the small cost that our
  15240. * representation uses one more K_i, due to carries.
  15241. *
  15242. * Also, for the sake of compactness, only the seven low-order bits of x[i]
  15243. * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
  15244. * the paper): it is set if and only if if s_i == -1;
  15245. *
  15246. * Calling conventions:
  15247. * - x is an array of size d + 1
  15248. * - w is the size, ie number of teeth, of the comb, and must be between
  15249. * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
  15250. * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
  15251. * (the result will be incorrect if these assumptions are not satisfied)
  15252. */
  15253. static void ecp_comb_fixed( unsigned char x[], size_t d,
  15254. unsigned char w, const mbedtls_mpi *m )
  15255. {
  15256. size_t i, j;
  15257. unsigned char c, cc, adjust;
  15258. memset( x, 0, d+1 );
  15259. /* First get the classical comb values (except for x_d = 0) */
  15260. for( i = 0; i < d; i++ )
  15261. for( j = 0; j < w; j++ )
  15262. x[i] |= mbedtls_mpi_get_bit( m, i + d * j ) << j;
  15263. /* Now make sure x_1 .. x_d are odd */
  15264. c = 0;
  15265. for( i = 1; i <= d; i++ )
  15266. {
  15267. /* Add carry and update it */
  15268. cc = x[i] & c;
  15269. x[i] = x[i] ^ c;
  15270. c = cc;
  15271. /* Adjust if needed, avoiding branches */
  15272. adjust = 1 - ( x[i] & 0x01 );
  15273. c |= x[i] & ( x[i-1] * adjust );
  15274. x[i] = x[i] ^ ( x[i-1] * adjust );
  15275. x[i-1] |= adjust << 7;
  15276. }
  15277. }
  15278. /*
  15279. * Precompute points for the comb method
  15280. *
  15281. * If i = i_{w-1} ... i_1 is the binary representation of i, then
  15282. * T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P
  15283. *
  15284. * T must be able to hold 2^{w - 1} elements
  15285. *
  15286. * Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1)
  15287. */
  15288. static int ecp_precompute_comb( const mbedtls_ecp_group *grp,
  15289. mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
  15290. unsigned char w, size_t d )
  15291. {
  15292. int ret;
  15293. unsigned char i, k;
  15294. size_t j;
  15295. mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1];
  15296. /*
  15297. * Set T[0] = P and
  15298. * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
  15299. */
  15300. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &T[0], P ) );
  15301. k = 0;
  15302. for( i = 1; i < ( 1U << ( w - 1 ) ); i <<= 1 )
  15303. {
  15304. cur = T + i;
  15305. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( cur, T + ( i >> 1 ) ) );
  15306. for( j = 0; j < d; j++ )
  15307. MBEDTLS_MPI_CHK( ecp_double_jac( grp, cur, cur ) );
  15308. TT[k++] = cur;
  15309. }
  15310. MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
  15311. /*
  15312. * Compute the remaining ones using the minimal number of additions
  15313. * Be careful to update T[2^l] only after using it!
  15314. */
  15315. k = 0;
  15316. for( i = 1; i < ( 1U << ( w - 1 ) ); i <<= 1 )
  15317. {
  15318. j = i;
  15319. while( j-- )
  15320. {
  15321. MBEDTLS_MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
  15322. TT[k++] = &T[i + j];
  15323. }
  15324. }
  15325. MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
  15326. cleanup:
  15327. return( ret );
  15328. }
  15329. /*
  15330. * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
  15331. */
  15332. static int ecp_select_comb( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  15333. const mbedtls_ecp_point T[], unsigned char t_len,
  15334. unsigned char i )
  15335. {
  15336. int ret;
  15337. unsigned char ii, j;
  15338. /* Ignore the "sign" bit and scale down */
  15339. ii = ( i & 0x7Fu ) >> 1;
  15340. /* Read the whole table to thwart cache-based timing attacks */
  15341. for( j = 0; j < t_len; j++ )
  15342. {
  15343. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
  15344. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
  15345. }
  15346. /* Safely invert result if i is "negative" */
  15347. MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
  15348. cleanup:
  15349. return( ret );
  15350. }
  15351. /*
  15352. * Core multiplication algorithm for the (modified) comb method.
  15353. * This part is actually common with the basic comb method (GECC 3.44)
  15354. *
  15355. * Cost: d A + d D + 1 R
  15356. */
  15357. static int ecp_mul_comb_core( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  15358. const mbedtls_ecp_point T[], unsigned char t_len,
  15359. const unsigned char x[], size_t d,
  15360. int (*f_rng)(void *, unsigned char *, size_t),
  15361. void *p_rng )
  15362. {
  15363. int ret;
  15364. mbedtls_ecp_point Txi;
  15365. size_t i;
  15366. mbedtls_ecp_point_init( &Txi );
  15367. /* Start with a non-zero point and randomize its coordinates */
  15368. i = d;
  15369. MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
  15370. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) );
  15371. if( f_rng != 0 )
  15372. MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
  15373. while( i-- != 0 )
  15374. {
  15375. MBEDTLS_MPI_CHK( ecp_double_jac( grp, R, R ) );
  15376. MBEDTLS_MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
  15377. MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
  15378. }
  15379. cleanup:
  15380. mbedtls_ecp_point_free( &Txi );
  15381. return( ret );
  15382. }
  15383. /*
  15384. * Multiplication using the comb method,
  15385. * for curves in short Weierstrass form
  15386. */
  15387. static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  15388. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  15389. int (*f_rng)(void *, unsigned char *, size_t),
  15390. void *p_rng )
  15391. {
  15392. int ret;
  15393. unsigned char w, m_is_odd, p_eq_g, pre_len, i;
  15394. size_t d;
  15395. unsigned char k[COMB_MAX_D + 1];
  15396. mbedtls_ecp_point *T;
  15397. mbedtls_mpi M, mm;
  15398. mbedtls_mpi_init( &M );
  15399. mbedtls_mpi_init( &mm );
  15400. /* we need N to be odd to trnaform m in an odd number, check now */
  15401. if( mbedtls_mpi_get_bit( &grp->N, 0 ) != 1 )
  15402. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  15403. /*
  15404. * Minimize the number of multiplications, that is minimize
  15405. * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
  15406. * (see costs of the various parts, with 1S = 1M)
  15407. */
  15408. w = grp->nbits >= 384 ? 5 : 4;
  15409. /*
  15410. * If P == G, pre-compute a bit more, since this may be re-used later.
  15411. * Just adding one avoids upping the cost of the first mul too much,
  15412. * and the memory cost too.
  15413. */
  15414. #if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
  15415. p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
  15416. mbedtls_mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
  15417. if( p_eq_g )
  15418. w++;
  15419. #else
  15420. p_eq_g = 0;
  15421. #endif
  15422. /*
  15423. * Make sure w is within bounds.
  15424. * (The last test is useful only for very small curves in the test suite.)
  15425. */
  15426. if( w > MBEDTLS_ECP_WINDOW_SIZE )
  15427. w = MBEDTLS_ECP_WINDOW_SIZE;
  15428. if( w >= grp->nbits )
  15429. w = 2;
  15430. /* Other sizes that depend on w */
  15431. pre_len = 1U << ( w - 1 );
  15432. d = ( grp->nbits + w - 1 ) / w;
  15433. /*
  15434. * Prepare precomputed points: if P == G we want to
  15435. * use grp->T if already initialized, or initialize it.
  15436. */
  15437. T = p_eq_g ? grp->T : NULL;
  15438. if( T == NULL )
  15439. {
  15440. T = mbedtls_calloc( pre_len, sizeof( mbedtls_ecp_point ) );
  15441. if( T == NULL )
  15442. {
  15443. ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
  15444. goto cleanup;
  15445. }
  15446. MBEDTLS_MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
  15447. if( p_eq_g )
  15448. {
  15449. grp->T = T;
  15450. grp->T_size = pre_len;
  15451. }
  15452. }
  15453. /*
  15454. * Make sure M is odd (M = m or M = N - m, since N is odd)
  15455. * using the fact that m * P = - (N - m) * P
  15456. */
  15457. m_is_odd = ( mbedtls_mpi_get_bit( m, 0 ) == 1 );
  15458. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &M, m ) );
  15459. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mm, &grp->N, m ) );
  15460. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
  15461. /*
  15462. * Go for comb multiplication, R = M * P
  15463. */
  15464. ecp_comb_fixed( k, d, w, &M );
  15465. MBEDTLS_MPI_CHK( ecp_mul_comb_core( grp, R, T, pre_len, k, d, f_rng, p_rng ) );
  15466. /*
  15467. * Now get m * P from M * P and normalize it
  15468. */
  15469. MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
  15470. MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, R ) );
  15471. cleanup:
  15472. if( T != NULL && ! p_eq_g )
  15473. {
  15474. for( i = 0; i < pre_len; i++ )
  15475. mbedtls_ecp_point_free( &T[i] );
  15476. mbedtls_free( T );
  15477. }
  15478. mbedtls_mpi_free( &M );
  15479. mbedtls_mpi_free( &mm );
  15480. if( ret != 0 )
  15481. mbedtls_ecp_point_free( R );
  15482. return( ret );
  15483. }
  15484. #endif /* ECP_SHORTWEIERSTRASS */
  15485. #if defined(ECP_MONTGOMERY)
  15486. /*
  15487. * For Montgomery curves, we do all the internal arithmetic in projective
  15488. * coordinates. Import/export of points uses only the x coordinates, which is
  15489. * internaly represented as X / Z.
  15490. *
  15491. * For scalar multiplication, we'll use a Montgomery ladder.
  15492. */
  15493. /*
  15494. * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
  15495. * Cost: 1M + 1I
  15496. */
  15497. static int ecp_normalize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P )
  15498. {
  15499. int ret;
  15500. #if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
  15501. if ( mbedtls_internal_ecp_grp_capable( grp ) )
  15502. {
  15503. return mbedtls_internal_ecp_normalize_mxz( grp, P );
  15504. }
  15505. #endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */
  15506. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
  15507. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
  15508. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
  15509. cleanup:
  15510. return( ret );
  15511. }
  15512. /*
  15513. * Randomize projective x/z coordinates:
  15514. * (X, Z) -> (l X, l Z) for random l
  15515. * This is sort of the reverse operation of ecp_normalize_mxz().
  15516. *
  15517. * This countermeasure was first suggested in [2].
  15518. * Cost: 2M
  15519. */
  15520. static int ecp_randomize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
  15521. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  15522. {
  15523. int ret;
  15524. mbedtls_mpi l;
  15525. size_t p_size;
  15526. int count = 0;
  15527. #if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
  15528. if ( mbedtls_internal_ecp_grp_capable( grp ) )
  15529. {
  15530. return mbedtls_internal_ecp_randomize_mxz( grp, P, f_rng, p_rng );
  15531. }
  15532. #endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */
  15533. p_size = ( grp->pbits + 7 ) / 8;
  15534. mbedtls_mpi_init( &l );
  15535. /* Generate l such that 1 < l < p */
  15536. do
  15537. {
  15538. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
  15539. while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
  15540. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
  15541. if( count++ > 10 )
  15542. return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
  15543. }
  15544. while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
  15545. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
  15546. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
  15547. cleanup:
  15548. mbedtls_mpi_free( &l );
  15549. return( ret );
  15550. }
  15551. /*
  15552. * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
  15553. * for Montgomery curves in x/z coordinates.
  15554. *
  15555. * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
  15556. * with
  15557. * d = X1
  15558. * P = (X2, Z2)
  15559. * Q = (X3, Z3)
  15560. * R = (X4, Z4)
  15561. * S = (X5, Z5)
  15562. * and eliminating temporary variables tO, ..., t4.
  15563. *
  15564. * Cost: 5M + 4S
  15565. */
  15566. static int ecp_double_add_mxz( const mbedtls_ecp_group *grp,
  15567. mbedtls_ecp_point *R, mbedtls_ecp_point *S,
  15568. const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
  15569. const mbedtls_mpi *d )
  15570. {
  15571. int ret;
  15572. mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
  15573. #if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
  15574. if ( mbedtls_internal_ecp_grp_capable( grp ) )
  15575. {
  15576. return mbedtls_internal_ecp_double_add_mxz( grp, R, S, P, Q, d );
  15577. }
  15578. #endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */
  15579. mbedtls_mpi_init( &A ); mbedtls_mpi_init( &AA ); mbedtls_mpi_init( &B );
  15580. mbedtls_mpi_init( &BB ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &C );
  15581. mbedtls_mpi_init( &D ); mbedtls_mpi_init( &DA ); mbedtls_mpi_init( &CB );
  15582. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
  15583. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
  15584. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
  15585. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
  15586. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
  15587. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
  15588. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
  15589. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
  15590. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
  15591. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
  15592. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
  15593. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
  15594. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
  15595. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
  15596. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
  15597. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
  15598. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
  15599. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
  15600. cleanup:
  15601. mbedtls_mpi_free( &A ); mbedtls_mpi_free( &AA ); mbedtls_mpi_free( &B );
  15602. mbedtls_mpi_free( &BB ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &C );
  15603. mbedtls_mpi_free( &D ); mbedtls_mpi_free( &DA ); mbedtls_mpi_free( &CB );
  15604. return( ret );
  15605. }
  15606. /*
  15607. * Multiplication with Montgomery ladder in x/z coordinates,
  15608. * for curves in Montgomery form
  15609. */
  15610. static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  15611. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  15612. int (*f_rng)(void *, unsigned char *, size_t),
  15613. void *p_rng )
  15614. {
  15615. int ret;
  15616. size_t i;
  15617. unsigned char b;
  15618. mbedtls_ecp_point RP;
  15619. mbedtls_mpi PX;
  15620. mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX );
  15621. /* Save PX and read from P before writing to R, in case P == R */
  15622. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) );
  15623. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) );
  15624. /* Set R to zero in modified x/z coordinates */
  15625. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->X, 1 ) );
  15626. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 0 ) );
  15627. mbedtls_mpi_free( &R->Y );
  15628. /* RP.X might be sligtly larger than P, so reduce it */
  15629. MOD_ADD( RP.X );
  15630. /* Randomize coordinates of the starting point */
  15631. if( f_rng != NULL )
  15632. MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
  15633. /* Loop invariant: R = result so far, RP = R + P */
  15634. i = mbedtls_mpi_bitlen( m ); /* one past the (zero-based) most significant bit */
  15635. while( i-- > 0 )
  15636. {
  15637. b = mbedtls_mpi_get_bit( m, i );
  15638. /*
  15639. * if (b) R = 2R + P else R = 2R,
  15640. * which is:
  15641. * if (b) double_add( RP, R, RP, R )
  15642. * else double_add( R, RP, R, RP )
  15643. * but using safe conditional swaps to avoid leaks
  15644. */
  15645. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
  15646. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
  15647. MBEDTLS_MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
  15648. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
  15649. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
  15650. }
  15651. MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) );
  15652. cleanup:
  15653. mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX );
  15654. return( ret );
  15655. }
  15656. #endif /* ECP_MONTGOMERY */
  15657. /*
  15658. * Multiplication R = m * P
  15659. */
  15660. int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  15661. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  15662. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  15663. {
  15664. int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  15665. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  15666. char is_grp_capable = 0;
  15667. #endif
  15668. /* Common sanity checks */
  15669. if( mbedtls_mpi_cmp_int( &P->Z, 1 ) != 0 )
  15670. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  15671. if( ( ret = mbedtls_ecp_check_privkey( grp, m ) ) != 0 ||
  15672. ( ret = mbedtls_ecp_check_pubkey( grp, P ) ) != 0 )
  15673. return( ret );
  15674. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  15675. if ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) )
  15676. {
  15677. MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
  15678. }
  15679. #endif /* MBEDTLS_ECP_INTERNAL_ALT */
  15680. #if defined(ECP_MONTGOMERY)
  15681. if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
  15682. ret = ecp_mul_mxz( grp, R, m, P, f_rng, p_rng );
  15683. #endif
  15684. #if defined(ECP_SHORTWEIERSTRASS)
  15685. if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
  15686. ret = ecp_mul_comb( grp, R, m, P, f_rng, p_rng );
  15687. #endif
  15688. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  15689. cleanup:
  15690. if ( is_grp_capable )
  15691. {
  15692. mbedtls_internal_ecp_free( grp );
  15693. }
  15694. #endif /* MBEDTLS_ECP_INTERNAL_ALT */
  15695. return( ret );
  15696. }
  15697. #if defined(ECP_SHORTWEIERSTRASS)
  15698. /*
  15699. * Check that an affine point is valid as a public key,
  15700. * short weierstrass curves (SEC1 3.2.3.1)
  15701. */
  15702. static int ecp_check_pubkey_sw( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
  15703. {
  15704. int ret;
  15705. mbedtls_mpi YY, RHS;
  15706. /* pt coordinates must be normalized for our checks */
  15707. if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 ||
  15708. mbedtls_mpi_cmp_int( &pt->Y, 0 ) < 0 ||
  15709. mbedtls_mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
  15710. mbedtls_mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
  15711. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  15712. mbedtls_mpi_init( &YY ); mbedtls_mpi_init( &RHS );
  15713. /*
  15714. * YY = Y^2
  15715. * RHS = X (X^2 + A) + B = X^3 + A X + B
  15716. */
  15717. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
  15718. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
  15719. /* Special case for A = -3 */
  15720. if( grp->A.p == NULL )
  15721. {
  15722. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
  15723. }
  15724. else
  15725. {
  15726. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
  15727. }
  15728. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
  15729. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
  15730. if( mbedtls_mpi_cmp_mpi( &YY, &RHS ) != 0 )
  15731. ret = MBEDTLS_ERR_ECP_INVALID_KEY;
  15732. cleanup:
  15733. mbedtls_mpi_free( &YY ); mbedtls_mpi_free( &RHS );
  15734. return( ret );
  15735. }
  15736. #endif /* ECP_SHORTWEIERSTRASS */
  15737. /*
  15738. * R = m * P with shortcuts for m == 1 and m == -1
  15739. * NOT constant-time - ONLY for short Weierstrass!
  15740. */
  15741. static int mbedtls_ecp_mul_shortcuts( mbedtls_ecp_group *grp,
  15742. mbedtls_ecp_point *R,
  15743. const mbedtls_mpi *m,
  15744. const mbedtls_ecp_point *P )
  15745. {
  15746. int ret;
  15747. if( mbedtls_mpi_cmp_int( m, 1 ) == 0 )
  15748. {
  15749. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
  15750. }
  15751. else if( mbedtls_mpi_cmp_int( m, -1 ) == 0 )
  15752. {
  15753. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
  15754. if( mbedtls_mpi_cmp_int( &R->Y, 0 ) != 0 )
  15755. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) );
  15756. }
  15757. else
  15758. {
  15759. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, R, m, P, NULL, NULL ) );
  15760. }
  15761. cleanup:
  15762. return( ret );
  15763. }
  15764. /*
  15765. * Linear combination
  15766. * NOT constant-time
  15767. */
  15768. int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  15769. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  15770. const mbedtls_mpi *n, const mbedtls_ecp_point *Q )
  15771. {
  15772. int ret;
  15773. mbedtls_ecp_point mP;
  15774. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  15775. char is_grp_capable = 0;
  15776. #endif
  15777. if( ecp_get_type( grp ) != ECP_TYPE_SHORT_WEIERSTRASS )
  15778. return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
  15779. mbedtls_ecp_point_init( &mP );
  15780. MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, &mP, m, P ) );
  15781. MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, R, n, Q ) );
  15782. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  15783. if ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) )
  15784. {
  15785. MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
  15786. }
  15787. #endif /* MBEDTLS_ECP_INTERNAL_ALT */
  15788. MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, &mP, R ) );
  15789. MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, R ) );
  15790. cleanup:
  15791. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  15792. if ( is_grp_capable )
  15793. {
  15794. mbedtls_internal_ecp_free( grp );
  15795. }
  15796. #endif /* MBEDTLS_ECP_INTERNAL_ALT */
  15797. mbedtls_ecp_point_free( &mP );
  15798. return( ret );
  15799. }
  15800. #if defined(ECP_MONTGOMERY)
  15801. /*
  15802. * Check validity of a public key for Montgomery curves with x-only schemes
  15803. */
  15804. static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
  15805. {
  15806. /* [Curve25519 p. 5] Just check X is the correct number of bytes */
  15807. if( mbedtls_mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
  15808. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  15809. return( 0 );
  15810. }
  15811. #endif /* ECP_MONTGOMERY */
  15812. /*
  15813. * Check that a point is valid as a public key
  15814. */
  15815. int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
  15816. {
  15817. /* Must use affine coordinates */
  15818. if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 )
  15819. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  15820. #if defined(ECP_MONTGOMERY)
  15821. if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
  15822. return( ecp_check_pubkey_mx( grp, pt ) );
  15823. #endif
  15824. #if defined(ECP_SHORTWEIERSTRASS)
  15825. if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
  15826. return( ecp_check_pubkey_sw( grp, pt ) );
  15827. #endif
  15828. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  15829. }
  15830. /*
  15831. * Check that an mbedtls_mpi is valid as a private key
  15832. */
  15833. int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d )
  15834. {
  15835. #if defined(ECP_MONTGOMERY)
  15836. if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
  15837. {
  15838. /* see [Curve25519] page 5 */
  15839. if( mbedtls_mpi_get_bit( d, 0 ) != 0 ||
  15840. mbedtls_mpi_get_bit( d, 1 ) != 0 ||
  15841. mbedtls_mpi_get_bit( d, 2 ) != 0 ||
  15842. mbedtls_mpi_bitlen( d ) - 1 != grp->nbits ) /* mbedtls_mpi_bitlen is one-based! */
  15843. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  15844. else
  15845. return( 0 );
  15846. }
  15847. #endif /* ECP_MONTGOMERY */
  15848. #if defined(ECP_SHORTWEIERSTRASS)
  15849. if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
  15850. {
  15851. /* see SEC1 3.2 */
  15852. if( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
  15853. mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
  15854. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  15855. else
  15856. return( 0 );
  15857. }
  15858. #endif /* ECP_SHORTWEIERSTRASS */
  15859. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  15860. }
  15861. /*
  15862. * Generate a keypair with configurable base point
  15863. */
  15864. int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
  15865. const mbedtls_ecp_point *G,
  15866. mbedtls_mpi *d, mbedtls_ecp_point *Q,
  15867. int (*f_rng)(void *, unsigned char *, size_t),
  15868. void *p_rng )
  15869. {
  15870. int ret;
  15871. size_t n_size = ( grp->nbits + 7 ) / 8;
  15872. #if defined(ECP_MONTGOMERY)
  15873. if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
  15874. {
  15875. /* [M225] page 5 */
  15876. size_t b;
  15877. do {
  15878. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
  15879. } while( mbedtls_mpi_bitlen( d ) == 0);
  15880. /* Make sure the most significant bit is nbits */
  15881. b = mbedtls_mpi_bitlen( d ) - 1; /* mbedtls_mpi_bitlen is one-based */
  15882. if( b > grp->nbits )
  15883. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, b - grp->nbits ) );
  15884. else
  15885. MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, grp->nbits, 1 ) );
  15886. /* Make sure the last three bits are unset */
  15887. MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 0, 0 ) );
  15888. MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 1, 0 ) );
  15889. MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) );
  15890. }
  15891. else
  15892. #endif /* ECP_MONTGOMERY */
  15893. #if defined(ECP_SHORTWEIERSTRASS)
  15894. if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
  15895. {
  15896. /* SEC1 3.2.1: Generate d such that 1 <= n < N */
  15897. int count = 0;
  15898. /*
  15899. * Match the procedure given in RFC 6979 (deterministic ECDSA):
  15900. * - use the same byte ordering;
  15901. * - keep the leftmost nbits bits of the generated octet string;
  15902. * - try until result is in the desired range.
  15903. * This also avoids any biais, which is especially important for ECDSA.
  15904. */
  15905. do
  15906. {
  15907. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
  15908. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, 8 * n_size - grp->nbits ) );
  15909. /*
  15910. * Each try has at worst a probability 1/2 of failing (the msb has
  15911. * a probability 1/2 of being 0, and then the result will be < N),
  15912. * so after 30 tries failure probability is a most 2**(-30).
  15913. *
  15914. * For most curves, 1 try is enough with overwhelming probability,
  15915. * since N starts with a lot of 1s in binary, but some curves
  15916. * such as secp224k1 are actually very close to the worst case.
  15917. */
  15918. if( ++count > 30 )
  15919. return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
  15920. }
  15921. while( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
  15922. mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 );
  15923. }
  15924. else
  15925. #endif /* ECP_SHORTWEIERSTRASS */
  15926. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  15927. cleanup:
  15928. if( ret != 0 )
  15929. return( ret );
  15930. return( mbedtls_ecp_mul( grp, Q, d, G, f_rng, p_rng ) );
  15931. }
  15932. /*
  15933. * Generate key pair, wrapper for conventional base point
  15934. */
  15935. int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp,
  15936. mbedtls_mpi *d, mbedtls_ecp_point *Q,
  15937. int (*f_rng)(void *, unsigned char *, size_t),
  15938. void *p_rng )
  15939. {
  15940. return( mbedtls_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) );
  15941. }
  15942. /*
  15943. * Generate a keypair, prettier wrapper
  15944. */
  15945. int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
  15946. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  15947. {
  15948. int ret;
  15949. if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 )
  15950. return( ret );
  15951. return( mbedtls_ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
  15952. }
  15953. /*
  15954. * Check a public-private key pair
  15955. */
  15956. int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv )
  15957. {
  15958. int ret;
  15959. mbedtls_ecp_point Q;
  15960. mbedtls_ecp_group grp;
  15961. if( pub->grp.id == MBEDTLS_ECP_DP_NONE ||
  15962. pub->grp.id != prv->grp.id ||
  15963. mbedtls_mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
  15964. mbedtls_mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
  15965. mbedtls_mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
  15966. {
  15967. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  15968. }
  15969. mbedtls_ecp_point_init( &Q );
  15970. mbedtls_ecp_group_init( &grp );
  15971. /* mbedtls_ecp_mul() needs a non-const group... */
  15972. mbedtls_ecp_group_copy( &grp, &prv->grp );
  15973. /* Also checks d is valid */
  15974. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) );
  15975. if( mbedtls_mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
  15976. mbedtls_mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
  15977. mbedtls_mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
  15978. {
  15979. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  15980. goto cleanup;
  15981. }
  15982. cleanup:
  15983. mbedtls_ecp_point_free( &Q );
  15984. mbedtls_ecp_group_free( &grp );
  15985. return( ret );
  15986. }
  15987. #if defined(MBEDTLS_SELF_TEST)
  15988. /*
  15989. * Checkup routine
  15990. */
  15991. int mbedtls_ecp_self_test( int verbose )
  15992. {
  15993. int ret;
  15994. size_t i;
  15995. mbedtls_ecp_group grp;
  15996. mbedtls_ecp_point R, P;
  15997. mbedtls_mpi m;
  15998. unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
  15999. /* exponents especially adapted for secp192r1 */
  16000. const char *exponents[] =
  16001. {
  16002. "000000000000000000000000000000000000000000000001", /* one */
  16003. "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
  16004. "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
  16005. "400000000000000000000000000000000000000000000000", /* one and zeros */
  16006. "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
  16007. "555555555555555555555555555555555555555555555555", /* 101010... */
  16008. };
  16009. mbedtls_ecp_group_init( &grp );
  16010. mbedtls_ecp_point_init( &R );
  16011. mbedtls_ecp_point_init( &P );
  16012. mbedtls_mpi_init( &m );
  16013. /* Use secp192r1 if available, or any available curve */
  16014. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
  16015. MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_SECP192R1 ) );
  16016. #else
  16017. MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, mbedtls_ecp_curve_list()->grp_id ) );
  16018. #endif
  16019. if( verbose != 0 )
  16020. mbedtls_printf( " ECP test #1 (constant op_count, base point G): " );
  16021. /* Do a dummy multiplication first to trigger precomputation */
  16022. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &m, 2 ) );
  16023. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
  16024. add_count = 0;
  16025. dbl_count = 0;
  16026. mul_count = 0;
  16027. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
  16028. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
  16029. for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
  16030. {
  16031. add_c_prev = add_count;
  16032. dbl_c_prev = dbl_count;
  16033. mul_c_prev = mul_count;
  16034. add_count = 0;
  16035. dbl_count = 0;
  16036. mul_count = 0;
  16037. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
  16038. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
  16039. if( add_count != add_c_prev ||
  16040. dbl_count != dbl_c_prev ||
  16041. mul_count != mul_c_prev )
  16042. {
  16043. if( verbose != 0 )
  16044. mbedtls_printf( "failed (%u)\n", (unsigned int) i );
  16045. ret = 1;
  16046. goto cleanup;
  16047. }
  16048. }
  16049. if( verbose != 0 )
  16050. mbedtls_printf( "passed\n" );
  16051. if( verbose != 0 )
  16052. mbedtls_printf( " ECP test #2 (constant op_count, other point): " );
  16053. /* We computed P = 2G last time, use it */
  16054. add_count = 0;
  16055. dbl_count = 0;
  16056. mul_count = 0;
  16057. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
  16058. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
  16059. for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
  16060. {
  16061. add_c_prev = add_count;
  16062. dbl_c_prev = dbl_count;
  16063. mul_c_prev = mul_count;
  16064. add_count = 0;
  16065. dbl_count = 0;
  16066. mul_count = 0;
  16067. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
  16068. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
  16069. if( add_count != add_c_prev ||
  16070. dbl_count != dbl_c_prev ||
  16071. mul_count != mul_c_prev )
  16072. {
  16073. if( verbose != 0 )
  16074. mbedtls_printf( "failed (%u)\n", (unsigned int) i );
  16075. ret = 1;
  16076. goto cleanup;
  16077. }
  16078. }
  16079. if( verbose != 0 )
  16080. mbedtls_printf( "passed\n" );
  16081. cleanup:
  16082. if( ret < 0 && verbose != 0 )
  16083. mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
  16084. mbedtls_ecp_group_free( &grp );
  16085. mbedtls_ecp_point_free( &R );
  16086. mbedtls_ecp_point_free( &P );
  16087. mbedtls_mpi_free( &m );
  16088. if( verbose != 0 )
  16089. mbedtls_printf( "\n" );
  16090. return( ret );
  16091. }
  16092. #endif /* MBEDTLS_SELF_TEST */
  16093. #endif /* !MBEDTLS_ECP_ALT */
  16094. #endif /* MBEDTLS_ECP_C */
  16095. /*
  16096. Amalgamated build undefines
  16097. */
  16098. #undef ADD
  16099. #undef BC
  16100. #undef BEFORE_COLON
  16101. #undef F
  16102. #undef F0
  16103. #undef F1
  16104. #undef F2
  16105. #undef F3
  16106. #undef F4
  16107. #undef F5
  16108. #undef FSb
  16109. #undef K
  16110. #undef KK
  16111. #undef P
  16112. #undef R
  16113. #undef ROTR
  16114. #undef S
  16115. #undef S0
  16116. #undef S1
  16117. #undef S2
  16118. #undef S3
  16119. #undef SAFE_SNPRINTF
  16120. #undef SHR
  16121. #undef close
  16122. #undef read
  16123. #undef supported_init
  16124. #undef write
  16125. /********* Start of file library/ecp_curves.c ************/
  16126. /*
  16127. * Elliptic curves over GF(p): curve-specific data and functions
  16128. *
  16129. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  16130. * SPDX-License-Identifier: Apache-2.0
  16131. *
  16132. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  16133. * not use this file except in compliance with the License.
  16134. * You may obtain a copy of the License at
  16135. *
  16136. * http://www.apache.org/licenses/LICENSE-2.0
  16137. *
  16138. * Unless required by applicable law or agreed to in writing, software
  16139. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16140. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16141. * See the License for the specific language governing permissions and
  16142. * limitations under the License.
  16143. *
  16144. * This file is part of mbed TLS (https://tls.mbed.org)
  16145. */
  16146. #if !defined(MBEDTLS_CONFIG_FILE)
  16147. #else
  16148. #endif
  16149. #if defined(MBEDTLS_ECP_C)
  16150. #include <string.h>
  16151. #if !defined(MBEDTLS_ECP_ALT)
  16152. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  16153. !defined(inline) && !defined(__cplusplus)
  16154. #define inline __inline
  16155. #endif
  16156. /*
  16157. * Conversion macros for embedded constants:
  16158. * build lists of mbedtls_mpi_uint's from lists of unsigned char's grouped by 8, 4 or 2
  16159. */
  16160. #if defined(MBEDTLS_HAVE_INT32)
  16161. #define BYTES_TO_T_UINT_4( a, b, c, d ) \
  16162. ( (mbedtls_mpi_uint) a << 0 ) | \
  16163. ( (mbedtls_mpi_uint) b << 8 ) | \
  16164. ( (mbedtls_mpi_uint) c << 16 ) | \
  16165. ( (mbedtls_mpi_uint) d << 24 )
  16166. #define BYTES_TO_T_UINT_2( a, b ) \
  16167. BYTES_TO_T_UINT_4( a, b, 0, 0 )
  16168. #define BYTES_TO_T_UINT_8( a, b, c, d, e, f, g, h ) \
  16169. BYTES_TO_T_UINT_4( a, b, c, d ), \
  16170. BYTES_TO_T_UINT_4( e, f, g, h )
  16171. #else /* 64-bits */
  16172. #define BYTES_TO_T_UINT_8( a, b, c, d, e, f, g, h ) \
  16173. ( (mbedtls_mpi_uint) a << 0 ) | \
  16174. ( (mbedtls_mpi_uint) b << 8 ) | \
  16175. ( (mbedtls_mpi_uint) c << 16 ) | \
  16176. ( (mbedtls_mpi_uint) d << 24 ) | \
  16177. ( (mbedtls_mpi_uint) e << 32 ) | \
  16178. ( (mbedtls_mpi_uint) f << 40 ) | \
  16179. ( (mbedtls_mpi_uint) g << 48 ) | \
  16180. ( (mbedtls_mpi_uint) h << 56 )
  16181. #define BYTES_TO_T_UINT_4( a, b, c, d ) \
  16182. BYTES_TO_T_UINT_8( a, b, c, d, 0, 0, 0, 0 )
  16183. #define BYTES_TO_T_UINT_2( a, b ) \
  16184. BYTES_TO_T_UINT_8( a, b, 0, 0, 0, 0, 0, 0 )
  16185. #endif /* bits in mbedtls_mpi_uint */
  16186. /*
  16187. * Note: the constants are in little-endian order
  16188. * to be directly usable in MPIs
  16189. */
  16190. /*
  16191. * Domain parameters for secp192r1
  16192. */
  16193. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
  16194. static const mbedtls_mpi_uint secp192r1_p[] = {
  16195. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16196. BYTES_TO_T_UINT_8( 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16197. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16198. };
  16199. static const mbedtls_mpi_uint secp192r1_b[] = {
  16200. BYTES_TO_T_UINT_8( 0xB1, 0xB9, 0x46, 0xC1, 0xEC, 0xDE, 0xB8, 0xFE ),
  16201. BYTES_TO_T_UINT_8( 0x49, 0x30, 0x24, 0x72, 0xAB, 0xE9, 0xA7, 0x0F ),
  16202. BYTES_TO_T_UINT_8( 0xE7, 0x80, 0x9C, 0xE5, 0x19, 0x05, 0x21, 0x64 ),
  16203. };
  16204. static const mbedtls_mpi_uint secp192r1_gx[] = {
  16205. BYTES_TO_T_UINT_8( 0x12, 0x10, 0xFF, 0x82, 0xFD, 0x0A, 0xFF, 0xF4 ),
  16206. BYTES_TO_T_UINT_8( 0x00, 0x88, 0xA1, 0x43, 0xEB, 0x20, 0xBF, 0x7C ),
  16207. BYTES_TO_T_UINT_8( 0xF6, 0x90, 0x30, 0xB0, 0x0E, 0xA8, 0x8D, 0x18 ),
  16208. };
  16209. static const mbedtls_mpi_uint secp192r1_gy[] = {
  16210. BYTES_TO_T_UINT_8( 0x11, 0x48, 0x79, 0x1E, 0xA1, 0x77, 0xF9, 0x73 ),
  16211. BYTES_TO_T_UINT_8( 0xD5, 0xCD, 0x24, 0x6B, 0xED, 0x11, 0x10, 0x63 ),
  16212. BYTES_TO_T_UINT_8( 0x78, 0xDA, 0xC8, 0xFF, 0x95, 0x2B, 0x19, 0x07 ),
  16213. };
  16214. static const mbedtls_mpi_uint secp192r1_n[] = {
  16215. BYTES_TO_T_UINT_8( 0x31, 0x28, 0xD2, 0xB4, 0xB1, 0xC9, 0x6B, 0x14 ),
  16216. BYTES_TO_T_UINT_8( 0x36, 0xF8, 0xDE, 0x99, 0xFF, 0xFF, 0xFF, 0xFF ),
  16217. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16218. };
  16219. #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
  16220. /*
  16221. * Domain parameters for secp224r1
  16222. */
  16223. #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
  16224. static const mbedtls_mpi_uint secp224r1_p[] = {
  16225. BYTES_TO_T_UINT_8( 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
  16226. BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ),
  16227. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16228. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00 ),
  16229. };
  16230. static const mbedtls_mpi_uint secp224r1_b[] = {
  16231. BYTES_TO_T_UINT_8( 0xB4, 0xFF, 0x55, 0x23, 0x43, 0x39, 0x0B, 0x27 ),
  16232. BYTES_TO_T_UINT_8( 0xBA, 0xD8, 0xBF, 0xD7, 0xB7, 0xB0, 0x44, 0x50 ),
  16233. BYTES_TO_T_UINT_8( 0x56, 0x32, 0x41, 0xF5, 0xAB, 0xB3, 0x04, 0x0C ),
  16234. BYTES_TO_T_UINT_4( 0x85, 0x0A, 0x05, 0xB4 ),
  16235. };
  16236. static const mbedtls_mpi_uint secp224r1_gx[] = {
  16237. BYTES_TO_T_UINT_8( 0x21, 0x1D, 0x5C, 0x11, 0xD6, 0x80, 0x32, 0x34 ),
  16238. BYTES_TO_T_UINT_8( 0x22, 0x11, 0xC2, 0x56, 0xD3, 0xC1, 0x03, 0x4A ),
  16239. BYTES_TO_T_UINT_8( 0xB9, 0x90, 0x13, 0x32, 0x7F, 0xBF, 0xB4, 0x6B ),
  16240. BYTES_TO_T_UINT_4( 0xBD, 0x0C, 0x0E, 0xB7 ),
  16241. };
  16242. static const mbedtls_mpi_uint secp224r1_gy[] = {
  16243. BYTES_TO_T_UINT_8( 0x34, 0x7E, 0x00, 0x85, 0x99, 0x81, 0xD5, 0x44 ),
  16244. BYTES_TO_T_UINT_8( 0x64, 0x47, 0x07, 0x5A, 0xA0, 0x75, 0x43, 0xCD ),
  16245. BYTES_TO_T_UINT_8( 0xE6, 0xDF, 0x22, 0x4C, 0xFB, 0x23, 0xF7, 0xB5 ),
  16246. BYTES_TO_T_UINT_4( 0x88, 0x63, 0x37, 0xBD ),
  16247. };
  16248. static const mbedtls_mpi_uint secp224r1_n[] = {
  16249. BYTES_TO_T_UINT_8( 0x3D, 0x2A, 0x5C, 0x5C, 0x45, 0x29, 0xDD, 0x13 ),
  16250. BYTES_TO_T_UINT_8( 0x3E, 0xF0, 0xB8, 0xE0, 0xA2, 0x16, 0xFF, 0xFF ),
  16251. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16252. BYTES_TO_T_UINT_4( 0xFF, 0xFF, 0xFF, 0xFF ),
  16253. };
  16254. #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
  16255. /*
  16256. * Domain parameters for secp256r1
  16257. */
  16258. #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
  16259. static const mbedtls_mpi_uint secp256r1_p[] = {
  16260. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16261. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00 ),
  16262. BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
  16263. BYTES_TO_T_UINT_8( 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ),
  16264. };
  16265. static const mbedtls_mpi_uint secp256r1_b[] = {
  16266. BYTES_TO_T_UINT_8( 0x4B, 0x60, 0xD2, 0x27, 0x3E, 0x3C, 0xCE, 0x3B ),
  16267. BYTES_TO_T_UINT_8( 0xF6, 0xB0, 0x53, 0xCC, 0xB0, 0x06, 0x1D, 0x65 ),
  16268. BYTES_TO_T_UINT_8( 0xBC, 0x86, 0x98, 0x76, 0x55, 0xBD, 0xEB, 0xB3 ),
  16269. BYTES_TO_T_UINT_8( 0xE7, 0x93, 0x3A, 0xAA, 0xD8, 0x35, 0xC6, 0x5A ),
  16270. };
  16271. static const mbedtls_mpi_uint secp256r1_gx[] = {
  16272. BYTES_TO_T_UINT_8( 0x96, 0xC2, 0x98, 0xD8, 0x45, 0x39, 0xA1, 0xF4 ),
  16273. BYTES_TO_T_UINT_8( 0xA0, 0x33, 0xEB, 0x2D, 0x81, 0x7D, 0x03, 0x77 ),
  16274. BYTES_TO_T_UINT_8( 0xF2, 0x40, 0xA4, 0x63, 0xE5, 0xE6, 0xBC, 0xF8 ),
  16275. BYTES_TO_T_UINT_8( 0x47, 0x42, 0x2C, 0xE1, 0xF2, 0xD1, 0x17, 0x6B ),
  16276. };
  16277. static const mbedtls_mpi_uint secp256r1_gy[] = {
  16278. BYTES_TO_T_UINT_8( 0xF5, 0x51, 0xBF, 0x37, 0x68, 0x40, 0xB6, 0xCB ),
  16279. BYTES_TO_T_UINT_8( 0xCE, 0x5E, 0x31, 0x6B, 0x57, 0x33, 0xCE, 0x2B ),
  16280. BYTES_TO_T_UINT_8( 0x16, 0x9E, 0x0F, 0x7C, 0x4A, 0xEB, 0xE7, 0x8E ),
  16281. BYTES_TO_T_UINT_8( 0x9B, 0x7F, 0x1A, 0xFE, 0xE2, 0x42, 0xE3, 0x4F ),
  16282. };
  16283. static const mbedtls_mpi_uint secp256r1_n[] = {
  16284. BYTES_TO_T_UINT_8( 0x51, 0x25, 0x63, 0xFC, 0xC2, 0xCA, 0xB9, 0xF3 ),
  16285. BYTES_TO_T_UINT_8( 0x84, 0x9E, 0x17, 0xA7, 0xAD, 0xFA, 0xE6, 0xBC ),
  16286. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16287. BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ),
  16288. };
  16289. #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
  16290. /*
  16291. * Domain parameters for secp384r1
  16292. */
  16293. #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
  16294. static const mbedtls_mpi_uint secp384r1_p[] = {
  16295. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00 ),
  16296. BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ),
  16297. BYTES_TO_T_UINT_8( 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16298. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16299. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16300. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16301. };
  16302. static const mbedtls_mpi_uint secp384r1_b[] = {
  16303. BYTES_TO_T_UINT_8( 0xEF, 0x2A, 0xEC, 0xD3, 0xED, 0xC8, 0x85, 0x2A ),
  16304. BYTES_TO_T_UINT_8( 0x9D, 0xD1, 0x2E, 0x8A, 0x8D, 0x39, 0x56, 0xC6 ),
  16305. BYTES_TO_T_UINT_8( 0x5A, 0x87, 0x13, 0x50, 0x8F, 0x08, 0x14, 0x03 ),
  16306. BYTES_TO_T_UINT_8( 0x12, 0x41, 0x81, 0xFE, 0x6E, 0x9C, 0x1D, 0x18 ),
  16307. BYTES_TO_T_UINT_8( 0x19, 0x2D, 0xF8, 0xE3, 0x6B, 0x05, 0x8E, 0x98 ),
  16308. BYTES_TO_T_UINT_8( 0xE4, 0xE7, 0x3E, 0xE2, 0xA7, 0x2F, 0x31, 0xB3 ),
  16309. };
  16310. static const mbedtls_mpi_uint secp384r1_gx[] = {
  16311. BYTES_TO_T_UINT_8( 0xB7, 0x0A, 0x76, 0x72, 0x38, 0x5E, 0x54, 0x3A ),
  16312. BYTES_TO_T_UINT_8( 0x6C, 0x29, 0x55, 0xBF, 0x5D, 0xF2, 0x02, 0x55 ),
  16313. BYTES_TO_T_UINT_8( 0x38, 0x2A, 0x54, 0x82, 0xE0, 0x41, 0xF7, 0x59 ),
  16314. BYTES_TO_T_UINT_8( 0x98, 0x9B, 0xA7, 0x8B, 0x62, 0x3B, 0x1D, 0x6E ),
  16315. BYTES_TO_T_UINT_8( 0x74, 0xAD, 0x20, 0xF3, 0x1E, 0xC7, 0xB1, 0x8E ),
  16316. BYTES_TO_T_UINT_8( 0x37, 0x05, 0x8B, 0xBE, 0x22, 0xCA, 0x87, 0xAA ),
  16317. };
  16318. static const mbedtls_mpi_uint secp384r1_gy[] = {
  16319. BYTES_TO_T_UINT_8( 0x5F, 0x0E, 0xEA, 0x90, 0x7C, 0x1D, 0x43, 0x7A ),
  16320. BYTES_TO_T_UINT_8( 0x9D, 0x81, 0x7E, 0x1D, 0xCE, 0xB1, 0x60, 0x0A ),
  16321. BYTES_TO_T_UINT_8( 0xC0, 0xB8, 0xF0, 0xB5, 0x13, 0x31, 0xDA, 0xE9 ),
  16322. BYTES_TO_T_UINT_8( 0x7C, 0x14, 0x9A, 0x28, 0xBD, 0x1D, 0xF4, 0xF8 ),
  16323. BYTES_TO_T_UINT_8( 0x29, 0xDC, 0x92, 0x92, 0xBF, 0x98, 0x9E, 0x5D ),
  16324. BYTES_TO_T_UINT_8( 0x6F, 0x2C, 0x26, 0x96, 0x4A, 0xDE, 0x17, 0x36 ),
  16325. };
  16326. static const mbedtls_mpi_uint secp384r1_n[] = {
  16327. BYTES_TO_T_UINT_8( 0x73, 0x29, 0xC5, 0xCC, 0x6A, 0x19, 0xEC, 0xEC ),
  16328. BYTES_TO_T_UINT_8( 0x7A, 0xA7, 0xB0, 0x48, 0xB2, 0x0D, 0x1A, 0x58 ),
  16329. BYTES_TO_T_UINT_8( 0xDF, 0x2D, 0x37, 0xF4, 0x81, 0x4D, 0x63, 0xC7 ),
  16330. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16331. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16332. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16333. };
  16334. #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
  16335. /*
  16336. * Domain parameters for secp521r1
  16337. */
  16338. #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
  16339. static const mbedtls_mpi_uint secp521r1_p[] = {
  16340. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16341. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16342. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16343. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16344. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16345. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16346. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16347. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16348. BYTES_TO_T_UINT_2( 0xFF, 0x01 ),
  16349. };
  16350. static const mbedtls_mpi_uint secp521r1_b[] = {
  16351. BYTES_TO_T_UINT_8( 0x00, 0x3F, 0x50, 0x6B, 0xD4, 0x1F, 0x45, 0xEF ),
  16352. BYTES_TO_T_UINT_8( 0xF1, 0x34, 0x2C, 0x3D, 0x88, 0xDF, 0x73, 0x35 ),
  16353. BYTES_TO_T_UINT_8( 0x07, 0xBF, 0xB1, 0x3B, 0xBD, 0xC0, 0x52, 0x16 ),
  16354. BYTES_TO_T_UINT_8( 0x7B, 0x93, 0x7E, 0xEC, 0x51, 0x39, 0x19, 0x56 ),
  16355. BYTES_TO_T_UINT_8( 0xE1, 0x09, 0xF1, 0x8E, 0x91, 0x89, 0xB4, 0xB8 ),
  16356. BYTES_TO_T_UINT_8( 0xF3, 0x15, 0xB3, 0x99, 0x5B, 0x72, 0xDA, 0xA2 ),
  16357. BYTES_TO_T_UINT_8( 0xEE, 0x40, 0x85, 0xB6, 0xA0, 0x21, 0x9A, 0x92 ),
  16358. BYTES_TO_T_UINT_8( 0x1F, 0x9A, 0x1C, 0x8E, 0x61, 0xB9, 0x3E, 0x95 ),
  16359. BYTES_TO_T_UINT_2( 0x51, 0x00 ),
  16360. };
  16361. static const mbedtls_mpi_uint secp521r1_gx[] = {
  16362. BYTES_TO_T_UINT_8( 0x66, 0xBD, 0xE5, 0xC2, 0x31, 0x7E, 0x7E, 0xF9 ),
  16363. BYTES_TO_T_UINT_8( 0x9B, 0x42, 0x6A, 0x85, 0xC1, 0xB3, 0x48, 0x33 ),
  16364. BYTES_TO_T_UINT_8( 0xDE, 0xA8, 0xFF, 0xA2, 0x27, 0xC1, 0x1D, 0xFE ),
  16365. BYTES_TO_T_UINT_8( 0x28, 0x59, 0xE7, 0xEF, 0x77, 0x5E, 0x4B, 0xA1 ),
  16366. BYTES_TO_T_UINT_8( 0xBA, 0x3D, 0x4D, 0x6B, 0x60, 0xAF, 0x28, 0xF8 ),
  16367. BYTES_TO_T_UINT_8( 0x21, 0xB5, 0x3F, 0x05, 0x39, 0x81, 0x64, 0x9C ),
  16368. BYTES_TO_T_UINT_8( 0x42, 0xB4, 0x95, 0x23, 0x66, 0xCB, 0x3E, 0x9E ),
  16369. BYTES_TO_T_UINT_8( 0xCD, 0xE9, 0x04, 0x04, 0xB7, 0x06, 0x8E, 0x85 ),
  16370. BYTES_TO_T_UINT_2( 0xC6, 0x00 ),
  16371. };
  16372. static const mbedtls_mpi_uint secp521r1_gy[] = {
  16373. BYTES_TO_T_UINT_8( 0x50, 0x66, 0xD1, 0x9F, 0x76, 0x94, 0xBE, 0x88 ),
  16374. BYTES_TO_T_UINT_8( 0x40, 0xC2, 0x72, 0xA2, 0x86, 0x70, 0x3C, 0x35 ),
  16375. BYTES_TO_T_UINT_8( 0x61, 0x07, 0xAD, 0x3F, 0x01, 0xB9, 0x50, 0xC5 ),
  16376. BYTES_TO_T_UINT_8( 0x40, 0x26, 0xF4, 0x5E, 0x99, 0x72, 0xEE, 0x97 ),
  16377. BYTES_TO_T_UINT_8( 0x2C, 0x66, 0x3E, 0x27, 0x17, 0xBD, 0xAF, 0x17 ),
  16378. BYTES_TO_T_UINT_8( 0x68, 0x44, 0x9B, 0x57, 0x49, 0x44, 0xF5, 0x98 ),
  16379. BYTES_TO_T_UINT_8( 0xD9, 0x1B, 0x7D, 0x2C, 0xB4, 0x5F, 0x8A, 0x5C ),
  16380. BYTES_TO_T_UINT_8( 0x04, 0xC0, 0x3B, 0x9A, 0x78, 0x6A, 0x29, 0x39 ),
  16381. BYTES_TO_T_UINT_2( 0x18, 0x01 ),
  16382. };
  16383. static const mbedtls_mpi_uint secp521r1_n[] = {
  16384. BYTES_TO_T_UINT_8( 0x09, 0x64, 0x38, 0x91, 0x1E, 0xB7, 0x6F, 0xBB ),
  16385. BYTES_TO_T_UINT_8( 0xAE, 0x47, 0x9C, 0x89, 0xB8, 0xC9, 0xB5, 0x3B ),
  16386. BYTES_TO_T_UINT_8( 0xD0, 0xA5, 0x09, 0xF7, 0x48, 0x01, 0xCC, 0x7F ),
  16387. BYTES_TO_T_UINT_8( 0x6B, 0x96, 0x2F, 0xBF, 0x83, 0x87, 0x86, 0x51 ),
  16388. BYTES_TO_T_UINT_8( 0xFA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16389. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16390. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16391. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16392. BYTES_TO_T_UINT_2( 0xFF, 0x01 ),
  16393. };
  16394. #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
  16395. #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
  16396. static const mbedtls_mpi_uint secp192k1_p[] = {
  16397. BYTES_TO_T_UINT_8( 0x37, 0xEE, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF ),
  16398. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16399. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16400. };
  16401. static const mbedtls_mpi_uint secp192k1_a[] = {
  16402. BYTES_TO_T_UINT_2( 0x00, 0x00 ),
  16403. };
  16404. static const mbedtls_mpi_uint secp192k1_b[] = {
  16405. BYTES_TO_T_UINT_2( 0x03, 0x00 ),
  16406. };
  16407. static const mbedtls_mpi_uint secp192k1_gx[] = {
  16408. BYTES_TO_T_UINT_8( 0x7D, 0x6C, 0xE0, 0xEA, 0xB1, 0xD1, 0xA5, 0x1D ),
  16409. BYTES_TO_T_UINT_8( 0x34, 0xF4, 0xB7, 0x80, 0x02, 0x7D, 0xB0, 0x26 ),
  16410. BYTES_TO_T_UINT_8( 0xAE, 0xE9, 0x57, 0xC0, 0x0E, 0xF1, 0x4F, 0xDB ),
  16411. };
  16412. static const mbedtls_mpi_uint secp192k1_gy[] = {
  16413. BYTES_TO_T_UINT_8( 0x9D, 0x2F, 0x5E, 0xD9, 0x88, 0xAA, 0x82, 0x40 ),
  16414. BYTES_TO_T_UINT_8( 0x34, 0x86, 0xBE, 0x15, 0xD0, 0x63, 0x41, 0x84 ),
  16415. BYTES_TO_T_UINT_8( 0xA7, 0x28, 0x56, 0x9C, 0x6D, 0x2F, 0x2F, 0x9B ),
  16416. };
  16417. static const mbedtls_mpi_uint secp192k1_n[] = {
  16418. BYTES_TO_T_UINT_8( 0x8D, 0xFD, 0xDE, 0x74, 0x6A, 0x46, 0x69, 0x0F ),
  16419. BYTES_TO_T_UINT_8( 0x17, 0xFC, 0xF2, 0x26, 0xFE, 0xFF, 0xFF, 0xFF ),
  16420. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16421. };
  16422. #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
  16423. #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
  16424. static const mbedtls_mpi_uint secp224k1_p[] = {
  16425. BYTES_TO_T_UINT_8( 0x6D, 0xE5, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF ),
  16426. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16427. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16428. BYTES_TO_T_UINT_4( 0xFF, 0xFF, 0xFF, 0xFF ),
  16429. };
  16430. static const mbedtls_mpi_uint secp224k1_a[] = {
  16431. BYTES_TO_T_UINT_2( 0x00, 0x00 ),
  16432. };
  16433. static const mbedtls_mpi_uint secp224k1_b[] = {
  16434. BYTES_TO_T_UINT_2( 0x05, 0x00 ),
  16435. };
  16436. static const mbedtls_mpi_uint secp224k1_gx[] = {
  16437. BYTES_TO_T_UINT_8( 0x5C, 0xA4, 0xB7, 0xB6, 0x0E, 0x65, 0x7E, 0x0F ),
  16438. BYTES_TO_T_UINT_8( 0xA9, 0x75, 0x70, 0xE4, 0xE9, 0x67, 0xA4, 0x69 ),
  16439. BYTES_TO_T_UINT_8( 0xA1, 0x28, 0xFC, 0x30, 0xDF, 0x99, 0xF0, 0x4D ),
  16440. BYTES_TO_T_UINT_4( 0x33, 0x5B, 0x45, 0xA1 ),
  16441. };
  16442. static const mbedtls_mpi_uint secp224k1_gy[] = {
  16443. BYTES_TO_T_UINT_8( 0xA5, 0x61, 0x6D, 0x55, 0xDB, 0x4B, 0xCA, 0xE2 ),
  16444. BYTES_TO_T_UINT_8( 0x59, 0xBD, 0xB0, 0xC0, 0xF7, 0x19, 0xE3, 0xF7 ),
  16445. BYTES_TO_T_UINT_8( 0xD6, 0xFB, 0xCA, 0x82, 0x42, 0x34, 0xBA, 0x7F ),
  16446. BYTES_TO_T_UINT_4( 0xED, 0x9F, 0x08, 0x7E ),
  16447. };
  16448. static const mbedtls_mpi_uint secp224k1_n[] = {
  16449. BYTES_TO_T_UINT_8( 0xF7, 0xB1, 0x9F, 0x76, 0x71, 0xA9, 0xF0, 0xCA ),
  16450. BYTES_TO_T_UINT_8( 0x84, 0x61, 0xEC, 0xD2, 0xE8, 0xDC, 0x01, 0x00 ),
  16451. BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
  16452. BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 ),
  16453. };
  16454. #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
  16455. #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
  16456. static const mbedtls_mpi_uint secp256k1_p[] = {
  16457. BYTES_TO_T_UINT_8( 0x2F, 0xFC, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF ),
  16458. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16459. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16460. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16461. };
  16462. static const mbedtls_mpi_uint secp256k1_a[] = {
  16463. BYTES_TO_T_UINT_2( 0x00, 0x00 ),
  16464. };
  16465. static const mbedtls_mpi_uint secp256k1_b[] = {
  16466. BYTES_TO_T_UINT_2( 0x07, 0x00 ),
  16467. };
  16468. static const mbedtls_mpi_uint secp256k1_gx[] = {
  16469. BYTES_TO_T_UINT_8( 0x98, 0x17, 0xF8, 0x16, 0x5B, 0x81, 0xF2, 0x59 ),
  16470. BYTES_TO_T_UINT_8( 0xD9, 0x28, 0xCE, 0x2D, 0xDB, 0xFC, 0x9B, 0x02 ),
  16471. BYTES_TO_T_UINT_8( 0x07, 0x0B, 0x87, 0xCE, 0x95, 0x62, 0xA0, 0x55 ),
  16472. BYTES_TO_T_UINT_8( 0xAC, 0xBB, 0xDC, 0xF9, 0x7E, 0x66, 0xBE, 0x79 ),
  16473. };
  16474. static const mbedtls_mpi_uint secp256k1_gy[] = {
  16475. BYTES_TO_T_UINT_8( 0xB8, 0xD4, 0x10, 0xFB, 0x8F, 0xD0, 0x47, 0x9C ),
  16476. BYTES_TO_T_UINT_8( 0x19, 0x54, 0x85, 0xA6, 0x48, 0xB4, 0x17, 0xFD ),
  16477. BYTES_TO_T_UINT_8( 0xA8, 0x08, 0x11, 0x0E, 0xFC, 0xFB, 0xA4, 0x5D ),
  16478. BYTES_TO_T_UINT_8( 0x65, 0xC4, 0xA3, 0x26, 0x77, 0xDA, 0x3A, 0x48 ),
  16479. };
  16480. static const mbedtls_mpi_uint secp256k1_n[] = {
  16481. BYTES_TO_T_UINT_8( 0x41, 0x41, 0x36, 0xD0, 0x8C, 0x5E, 0xD2, 0xBF ),
  16482. BYTES_TO_T_UINT_8( 0x3B, 0xA0, 0x48, 0xAF, 0xE6, 0xDC, 0xAE, 0xBA ),
  16483. BYTES_TO_T_UINT_8( 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16484. BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
  16485. };
  16486. #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
  16487. /*
  16488. * Domain parameters for brainpoolP256r1 (RFC 5639 3.4)
  16489. */
  16490. #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
  16491. static const mbedtls_mpi_uint brainpoolP256r1_p[] = {
  16492. BYTES_TO_T_UINT_8( 0x77, 0x53, 0x6E, 0x1F, 0x1D, 0x48, 0x13, 0x20 ),
  16493. BYTES_TO_T_UINT_8( 0x28, 0x20, 0x26, 0xD5, 0x23, 0xF6, 0x3B, 0x6E ),
  16494. BYTES_TO_T_UINT_8( 0x72, 0x8D, 0x83, 0x9D, 0x90, 0x0A, 0x66, 0x3E ),
  16495. BYTES_TO_T_UINT_8( 0xBC, 0xA9, 0xEE, 0xA1, 0xDB, 0x57, 0xFB, 0xA9 ),
  16496. };
  16497. static const mbedtls_mpi_uint brainpoolP256r1_a[] = {
  16498. BYTES_TO_T_UINT_8( 0xD9, 0xB5, 0x30, 0xF3, 0x44, 0x4B, 0x4A, 0xE9 ),
  16499. BYTES_TO_T_UINT_8( 0x6C, 0x5C, 0xDC, 0x26, 0xC1, 0x55, 0x80, 0xFB ),
  16500. BYTES_TO_T_UINT_8( 0xE7, 0xFF, 0x7A, 0x41, 0x30, 0x75, 0xF6, 0xEE ),
  16501. BYTES_TO_T_UINT_8( 0x57, 0x30, 0x2C, 0xFC, 0x75, 0x09, 0x5A, 0x7D ),
  16502. };
  16503. static const mbedtls_mpi_uint brainpoolP256r1_b[] = {
  16504. BYTES_TO_T_UINT_8( 0xB6, 0x07, 0x8C, 0xFF, 0x18, 0xDC, 0xCC, 0x6B ),
  16505. BYTES_TO_T_UINT_8( 0xCE, 0xE1, 0xF7, 0x5C, 0x29, 0x16, 0x84, 0x95 ),
  16506. BYTES_TO_T_UINT_8( 0xBF, 0x7C, 0xD7, 0xBB, 0xD9, 0xB5, 0x30, 0xF3 ),
  16507. BYTES_TO_T_UINT_8( 0x44, 0x4B, 0x4A, 0xE9, 0x6C, 0x5C, 0xDC, 0x26 ),
  16508. };
  16509. static const mbedtls_mpi_uint brainpoolP256r1_gx[] = {
  16510. BYTES_TO_T_UINT_8( 0x62, 0x32, 0xCE, 0x9A, 0xBD, 0x53, 0x44, 0x3A ),
  16511. BYTES_TO_T_UINT_8( 0xC2, 0x23, 0xBD, 0xE3, 0xE1, 0x27, 0xDE, 0xB9 ),
  16512. BYTES_TO_T_UINT_8( 0xAF, 0xB7, 0x81, 0xFC, 0x2F, 0x48, 0x4B, 0x2C ),
  16513. BYTES_TO_T_UINT_8( 0xCB, 0x57, 0x7E, 0xCB, 0xB9, 0xAE, 0xD2, 0x8B ),
  16514. };
  16515. static const mbedtls_mpi_uint brainpoolP256r1_gy[] = {
  16516. BYTES_TO_T_UINT_8( 0x97, 0x69, 0x04, 0x2F, 0xC7, 0x54, 0x1D, 0x5C ),
  16517. BYTES_TO_T_UINT_8( 0x54, 0x8E, 0xED, 0x2D, 0x13, 0x45, 0x77, 0xC2 ),
  16518. BYTES_TO_T_UINT_8( 0xC9, 0x1D, 0x61, 0x14, 0x1A, 0x46, 0xF8, 0x97 ),
  16519. BYTES_TO_T_UINT_8( 0xFD, 0xC4, 0xDA, 0xC3, 0x35, 0xF8, 0x7E, 0x54 ),
  16520. };
  16521. static const mbedtls_mpi_uint brainpoolP256r1_n[] = {
  16522. BYTES_TO_T_UINT_8( 0xA7, 0x56, 0x48, 0x97, 0x82, 0x0E, 0x1E, 0x90 ),
  16523. BYTES_TO_T_UINT_8( 0xF7, 0xA6, 0x61, 0xB5, 0xA3, 0x7A, 0x39, 0x8C ),
  16524. BYTES_TO_T_UINT_8( 0x71, 0x8D, 0x83, 0x9D, 0x90, 0x0A, 0x66, 0x3E ),
  16525. BYTES_TO_T_UINT_8( 0xBC, 0xA9, 0xEE, 0xA1, 0xDB, 0x57, 0xFB, 0xA9 ),
  16526. };
  16527. #endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
  16528. /*
  16529. * Domain parameters for brainpoolP384r1 (RFC 5639 3.6)
  16530. */
  16531. #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
  16532. static const mbedtls_mpi_uint brainpoolP384r1_p[] = {
  16533. BYTES_TO_T_UINT_8( 0x53, 0xEC, 0x07, 0x31, 0x13, 0x00, 0x47, 0x87 ),
  16534. BYTES_TO_T_UINT_8( 0x71, 0x1A, 0x1D, 0x90, 0x29, 0xA7, 0xD3, 0xAC ),
  16535. BYTES_TO_T_UINT_8( 0x23, 0x11, 0xB7, 0x7F, 0x19, 0xDA, 0xB1, 0x12 ),
  16536. BYTES_TO_T_UINT_8( 0xB4, 0x56, 0x54, 0xED, 0x09, 0x71, 0x2F, 0x15 ),
  16537. BYTES_TO_T_UINT_8( 0xDF, 0x41, 0xE6, 0x50, 0x7E, 0x6F, 0x5D, 0x0F ),
  16538. BYTES_TO_T_UINT_8( 0x28, 0x6D, 0x38, 0xA3, 0x82, 0x1E, 0xB9, 0x8C ),
  16539. };
  16540. static const mbedtls_mpi_uint brainpoolP384r1_a[] = {
  16541. BYTES_TO_T_UINT_8( 0x26, 0x28, 0xCE, 0x22, 0xDD, 0xC7, 0xA8, 0x04 ),
  16542. BYTES_TO_T_UINT_8( 0xEB, 0xD4, 0x3A, 0x50, 0x4A, 0x81, 0xA5, 0x8A ),
  16543. BYTES_TO_T_UINT_8( 0x0F, 0xF9, 0x91, 0xBA, 0xEF, 0x65, 0x91, 0x13 ),
  16544. BYTES_TO_T_UINT_8( 0x87, 0x27, 0xB2, 0x4F, 0x8E, 0xA2, 0xBE, 0xC2 ),
  16545. BYTES_TO_T_UINT_8( 0xA0, 0xAF, 0x05, 0xCE, 0x0A, 0x08, 0x72, 0x3C ),
  16546. BYTES_TO_T_UINT_8( 0x0C, 0x15, 0x8C, 0x3D, 0xC6, 0x82, 0xC3, 0x7B ),
  16547. };
  16548. static const mbedtls_mpi_uint brainpoolP384r1_b[] = {
  16549. BYTES_TO_T_UINT_8( 0x11, 0x4C, 0x50, 0xFA, 0x96, 0x86, 0xB7, 0x3A ),
  16550. BYTES_TO_T_UINT_8( 0x94, 0xC9, 0xDB, 0x95, 0x02, 0x39, 0xB4, 0x7C ),
  16551. BYTES_TO_T_UINT_8( 0xD5, 0x62, 0xEB, 0x3E, 0xA5, 0x0E, 0x88, 0x2E ),
  16552. BYTES_TO_T_UINT_8( 0xA6, 0xD2, 0xDC, 0x07, 0xE1, 0x7D, 0xB7, 0x2F ),
  16553. BYTES_TO_T_UINT_8( 0x7C, 0x44, 0xF0, 0x16, 0x54, 0xB5, 0x39, 0x8B ),
  16554. BYTES_TO_T_UINT_8( 0x26, 0x28, 0xCE, 0x22, 0xDD, 0xC7, 0xA8, 0x04 ),
  16555. };
  16556. static const mbedtls_mpi_uint brainpoolP384r1_gx[] = {
  16557. BYTES_TO_T_UINT_8( 0x1E, 0xAF, 0xD4, 0x47, 0xE2, 0xB2, 0x87, 0xEF ),
  16558. BYTES_TO_T_UINT_8( 0xAA, 0x46, 0xD6, 0x36, 0x34, 0xE0, 0x26, 0xE8 ),
  16559. BYTES_TO_T_UINT_8( 0xE8, 0x10, 0xBD, 0x0C, 0xFE, 0xCA, 0x7F, 0xDB ),
  16560. BYTES_TO_T_UINT_8( 0xE3, 0x4F, 0xF1, 0x7E, 0xE7, 0xA3, 0x47, 0x88 ),
  16561. BYTES_TO_T_UINT_8( 0x6B, 0x3F, 0xC1, 0xB7, 0x81, 0x3A, 0xA6, 0xA2 ),
  16562. BYTES_TO_T_UINT_8( 0xFF, 0x45, 0xCF, 0x68, 0xF0, 0x64, 0x1C, 0x1D ),
  16563. };
  16564. static const mbedtls_mpi_uint brainpoolP384r1_gy[] = {
  16565. BYTES_TO_T_UINT_8( 0x15, 0x53, 0x3C, 0x26, 0x41, 0x03, 0x82, 0x42 ),
  16566. BYTES_TO_T_UINT_8( 0x11, 0x81, 0x91, 0x77, 0x21, 0x46, 0x46, 0x0E ),
  16567. BYTES_TO_T_UINT_8( 0x28, 0x29, 0x91, 0xF9, 0x4F, 0x05, 0x9C, 0xE1 ),
  16568. BYTES_TO_T_UINT_8( 0x64, 0x58, 0xEC, 0xFE, 0x29, 0x0B, 0xB7, 0x62 ),
  16569. BYTES_TO_T_UINT_8( 0x52, 0xD5, 0xCF, 0x95, 0x8E, 0xEB, 0xB1, 0x5C ),
  16570. BYTES_TO_T_UINT_8( 0xA4, 0xC2, 0xF9, 0x20, 0x75, 0x1D, 0xBE, 0x8A ),
  16571. };
  16572. static const mbedtls_mpi_uint brainpoolP384r1_n[] = {
  16573. BYTES_TO_T_UINT_8( 0x65, 0x65, 0x04, 0xE9, 0x02, 0x32, 0x88, 0x3B ),
  16574. BYTES_TO_T_UINT_8( 0x10, 0xC3, 0x7F, 0x6B, 0xAF, 0xB6, 0x3A, 0xCF ),
  16575. BYTES_TO_T_UINT_8( 0xA7, 0x25, 0x04, 0xAC, 0x6C, 0x6E, 0x16, 0x1F ),
  16576. BYTES_TO_T_UINT_8( 0xB3, 0x56, 0x54, 0xED, 0x09, 0x71, 0x2F, 0x15 ),
  16577. BYTES_TO_T_UINT_8( 0xDF, 0x41, 0xE6, 0x50, 0x7E, 0x6F, 0x5D, 0x0F ),
  16578. BYTES_TO_T_UINT_8( 0x28, 0x6D, 0x38, 0xA3, 0x82, 0x1E, 0xB9, 0x8C ),
  16579. };
  16580. #endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
  16581. /*
  16582. * Domain parameters for brainpoolP512r1 (RFC 5639 3.7)
  16583. */
  16584. #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
  16585. static const mbedtls_mpi_uint brainpoolP512r1_p[] = {
  16586. BYTES_TO_T_UINT_8( 0xF3, 0x48, 0x3A, 0x58, 0x56, 0x60, 0xAA, 0x28 ),
  16587. BYTES_TO_T_UINT_8( 0x85, 0xC6, 0x82, 0x2D, 0x2F, 0xFF, 0x81, 0x28 ),
  16588. BYTES_TO_T_UINT_8( 0xE6, 0x80, 0xA3, 0xE6, 0x2A, 0xA1, 0xCD, 0xAE ),
  16589. BYTES_TO_T_UINT_8( 0x42, 0x68, 0xC6, 0x9B, 0x00, 0x9B, 0x4D, 0x7D ),
  16590. BYTES_TO_T_UINT_8( 0x71, 0x08, 0x33, 0x70, 0xCA, 0x9C, 0x63, 0xD6 ),
  16591. BYTES_TO_T_UINT_8( 0x0E, 0xD2, 0xC9, 0xB3, 0xB3, 0x8D, 0x30, 0xCB ),
  16592. BYTES_TO_T_UINT_8( 0x07, 0xFC, 0xC9, 0x33, 0xAE, 0xE6, 0xD4, 0x3F ),
  16593. BYTES_TO_T_UINT_8( 0x8B, 0xC4, 0xE9, 0xDB, 0xB8, 0x9D, 0xDD, 0xAA ),
  16594. };
  16595. static const mbedtls_mpi_uint brainpoolP512r1_a[] = {
  16596. BYTES_TO_T_UINT_8( 0xCA, 0x94, 0xFC, 0x77, 0x4D, 0xAC, 0xC1, 0xE7 ),
  16597. BYTES_TO_T_UINT_8( 0xB9, 0xC7, 0xF2, 0x2B, 0xA7, 0x17, 0x11, 0x7F ),
  16598. BYTES_TO_T_UINT_8( 0xB5, 0xC8, 0x9A, 0x8B, 0xC9, 0xF1, 0x2E, 0x0A ),
  16599. BYTES_TO_T_UINT_8( 0xA1, 0x3A, 0x25, 0xA8, 0x5A, 0x5D, 0xED, 0x2D ),
  16600. BYTES_TO_T_UINT_8( 0xBC, 0x63, 0x98, 0xEA, 0xCA, 0x41, 0x34, 0xA8 ),
  16601. BYTES_TO_T_UINT_8( 0x10, 0x16, 0xF9, 0x3D, 0x8D, 0xDD, 0xCB, 0x94 ),
  16602. BYTES_TO_T_UINT_8( 0xC5, 0x4C, 0x23, 0xAC, 0x45, 0x71, 0x32, 0xE2 ),
  16603. BYTES_TO_T_UINT_8( 0x89, 0x3B, 0x60, 0x8B, 0x31, 0xA3, 0x30, 0x78 ),
  16604. };
  16605. static const mbedtls_mpi_uint brainpoolP512r1_b[] = {
  16606. BYTES_TO_T_UINT_8( 0x23, 0xF7, 0x16, 0x80, 0x63, 0xBD, 0x09, 0x28 ),
  16607. BYTES_TO_T_UINT_8( 0xDD, 0xE5, 0xBA, 0x5E, 0xB7, 0x50, 0x40, 0x98 ),
  16608. BYTES_TO_T_UINT_8( 0x67, 0x3E, 0x08, 0xDC, 0xCA, 0x94, 0xFC, 0x77 ),
  16609. BYTES_TO_T_UINT_8( 0x4D, 0xAC, 0xC1, 0xE7, 0xB9, 0xC7, 0xF2, 0x2B ),
  16610. BYTES_TO_T_UINT_8( 0xA7, 0x17, 0x11, 0x7F, 0xB5, 0xC8, 0x9A, 0x8B ),
  16611. BYTES_TO_T_UINT_8( 0xC9, 0xF1, 0x2E, 0x0A, 0xA1, 0x3A, 0x25, 0xA8 ),
  16612. BYTES_TO_T_UINT_8( 0x5A, 0x5D, 0xED, 0x2D, 0xBC, 0x63, 0x98, 0xEA ),
  16613. BYTES_TO_T_UINT_8( 0xCA, 0x41, 0x34, 0xA8, 0x10, 0x16, 0xF9, 0x3D ),
  16614. };
  16615. static const mbedtls_mpi_uint brainpoolP512r1_gx[] = {
  16616. BYTES_TO_T_UINT_8( 0x22, 0xF8, 0xB9, 0xBC, 0x09, 0x22, 0x35, 0x8B ),
  16617. BYTES_TO_T_UINT_8( 0x68, 0x5E, 0x6A, 0x40, 0x47, 0x50, 0x6D, 0x7C ),
  16618. BYTES_TO_T_UINT_8( 0x5F, 0x7D, 0xB9, 0x93, 0x7B, 0x68, 0xD1, 0x50 ),
  16619. BYTES_TO_T_UINT_8( 0x8D, 0xD4, 0xD0, 0xE2, 0x78, 0x1F, 0x3B, 0xFF ),
  16620. BYTES_TO_T_UINT_8( 0x8E, 0x09, 0xD0, 0xF4, 0xEE, 0x62, 0x3B, 0xB4 ),
  16621. BYTES_TO_T_UINT_8( 0xC1, 0x16, 0xD9, 0xB5, 0x70, 0x9F, 0xED, 0x85 ),
  16622. BYTES_TO_T_UINT_8( 0x93, 0x6A, 0x4C, 0x9C, 0x2E, 0x32, 0x21, 0x5A ),
  16623. BYTES_TO_T_UINT_8( 0x64, 0xD9, 0x2E, 0xD8, 0xBD, 0xE4, 0xAE, 0x81 ),
  16624. };
  16625. static const mbedtls_mpi_uint brainpoolP512r1_gy[] = {
  16626. BYTES_TO_T_UINT_8( 0x92, 0x08, 0xD8, 0x3A, 0x0F, 0x1E, 0xCD, 0x78 ),
  16627. BYTES_TO_T_UINT_8( 0x06, 0x54, 0xF0, 0xA8, 0x2F, 0x2B, 0xCA, 0xD1 ),
  16628. BYTES_TO_T_UINT_8( 0xAE, 0x63, 0x27, 0x8A, 0xD8, 0x4B, 0xCA, 0x5B ),
  16629. BYTES_TO_T_UINT_8( 0x5E, 0x48, 0x5F, 0x4A, 0x49, 0xDE, 0xDC, 0xB2 ),
  16630. BYTES_TO_T_UINT_8( 0x11, 0x81, 0x1F, 0x88, 0x5B, 0xC5, 0x00, 0xA0 ),
  16631. BYTES_TO_T_UINT_8( 0x1A, 0x7B, 0xA5, 0x24, 0x00, 0xF7, 0x09, 0xF2 ),
  16632. BYTES_TO_T_UINT_8( 0xFD, 0x22, 0x78, 0xCF, 0xA9, 0xBF, 0xEA, 0xC0 ),
  16633. BYTES_TO_T_UINT_8( 0xEC, 0x32, 0x63, 0x56, 0x5D, 0x38, 0xDE, 0x7D ),
  16634. };
  16635. static const mbedtls_mpi_uint brainpoolP512r1_n[] = {
  16636. BYTES_TO_T_UINT_8( 0x69, 0x00, 0xA9, 0x9C, 0x82, 0x96, 0x87, 0xB5 ),
  16637. BYTES_TO_T_UINT_8( 0xDD, 0xDA, 0x5D, 0x08, 0x81, 0xD3, 0xB1, 0x1D ),
  16638. BYTES_TO_T_UINT_8( 0x47, 0x10, 0xAC, 0x7F, 0x19, 0x61, 0x86, 0x41 ),
  16639. BYTES_TO_T_UINT_8( 0x19, 0x26, 0xA9, 0x4C, 0x41, 0x5C, 0x3E, 0x55 ),
  16640. BYTES_TO_T_UINT_8( 0x70, 0x08, 0x33, 0x70, 0xCA, 0x9C, 0x63, 0xD6 ),
  16641. BYTES_TO_T_UINT_8( 0x0E, 0xD2, 0xC9, 0xB3, 0xB3, 0x8D, 0x30, 0xCB ),
  16642. BYTES_TO_T_UINT_8( 0x07, 0xFC, 0xC9, 0x33, 0xAE, 0xE6, 0xD4, 0x3F ),
  16643. BYTES_TO_T_UINT_8( 0x8B, 0xC4, 0xE9, 0xDB, 0xB8, 0x9D, 0xDD, 0xAA ),
  16644. };
  16645. #endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
  16646. /*
  16647. * Create an MPI from embedded constants
  16648. * (assumes len is an exact multiple of sizeof mbedtls_mpi_uint)
  16649. */
  16650. static inline void ecp_mpi_load( mbedtls_mpi *X, const mbedtls_mpi_uint *p, size_t len )
  16651. {
  16652. X->s = 1;
  16653. X->n = len / sizeof( mbedtls_mpi_uint );
  16654. X->p = (mbedtls_mpi_uint *) p;
  16655. }
  16656. /*
  16657. * Set an MPI to static value 1
  16658. */
  16659. static inline void ecp_mpi_set1( mbedtls_mpi *X )
  16660. {
  16661. static mbedtls_mpi_uint one[] = { 1 };
  16662. X->s = 1;
  16663. X->n = 1;
  16664. X->p = one;
  16665. }
  16666. /*
  16667. * Make group available from embedded constants
  16668. */
  16669. static int ecp_group_load( mbedtls_ecp_group *grp,
  16670. const mbedtls_mpi_uint *p, size_t plen,
  16671. const mbedtls_mpi_uint *a, size_t alen,
  16672. const mbedtls_mpi_uint *b, size_t blen,
  16673. const mbedtls_mpi_uint *gx, size_t gxlen,
  16674. const mbedtls_mpi_uint *gy, size_t gylen,
  16675. const mbedtls_mpi_uint *n, size_t nlen)
  16676. {
  16677. ecp_mpi_load( &grp->P, p, plen );
  16678. if( a != NULL )
  16679. ecp_mpi_load( &grp->A, a, alen );
  16680. ecp_mpi_load( &grp->B, b, blen );
  16681. ecp_mpi_load( &grp->N, n, nlen );
  16682. ecp_mpi_load( &grp->G.X, gx, gxlen );
  16683. ecp_mpi_load( &grp->G.Y, gy, gylen );
  16684. ecp_mpi_set1( &grp->G.Z );
  16685. grp->pbits = mbedtls_mpi_bitlen( &grp->P );
  16686. grp->nbits = mbedtls_mpi_bitlen( &grp->N );
  16687. grp->h = 1;
  16688. return( 0 );
  16689. }
  16690. #if defined(MBEDTLS_ECP_NIST_OPTIM)
  16691. /* Forward declarations */
  16692. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
  16693. static int ecp_mod_p192( mbedtls_mpi * );
  16694. #endif
  16695. #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
  16696. static int ecp_mod_p224( mbedtls_mpi * );
  16697. #endif
  16698. #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
  16699. static int ecp_mod_p256( mbedtls_mpi * );
  16700. #endif
  16701. #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
  16702. static int ecp_mod_p384( mbedtls_mpi * );
  16703. #endif
  16704. #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
  16705. static int ecp_mod_p521( mbedtls_mpi * );
  16706. #endif
  16707. #define NIST_MODP( P ) grp->modp = ecp_mod_ ## P;
  16708. #else
  16709. #define NIST_MODP( P )
  16710. #endif /* MBEDTLS_ECP_NIST_OPTIM */
  16711. /* Additional forward declarations */
  16712. #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
  16713. static int ecp_mod_p255( mbedtls_mpi * );
  16714. #endif
  16715. #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
  16716. static int ecp_mod_p192k1( mbedtls_mpi * );
  16717. #endif
  16718. #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
  16719. static int ecp_mod_p224k1( mbedtls_mpi * );
  16720. #endif
  16721. #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
  16722. static int ecp_mod_p256k1( mbedtls_mpi * );
  16723. #endif
  16724. #define LOAD_GROUP_A( G ) ecp_group_load( grp, \
  16725. G ## _p, sizeof( G ## _p ), \
  16726. G ## _a, sizeof( G ## _a ), \
  16727. G ## _b, sizeof( G ## _b ), \
  16728. G ## _gx, sizeof( G ## _gx ), \
  16729. G ## _gy, sizeof( G ## _gy ), \
  16730. G ## _n, sizeof( G ## _n ) )
  16731. #define LOAD_GROUP( G ) ecp_group_load( grp, \
  16732. G ## _p, sizeof( G ## _p ), \
  16733. NULL, 0, \
  16734. G ## _b, sizeof( G ## _b ), \
  16735. G ## _gx, sizeof( G ## _gx ), \
  16736. G ## _gy, sizeof( G ## _gy ), \
  16737. G ## _n, sizeof( G ## _n ) )
  16738. #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
  16739. /*
  16740. * Specialized function for creating the Curve25519 group
  16741. */
  16742. static int ecp_use_curve25519( mbedtls_ecp_group *grp )
  16743. {
  16744. int ret;
  16745. /* Actually ( A + 2 ) / 4 */
  16746. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &grp->A, 16, "01DB42" ) );
  16747. /* P = 2^255 - 19 */
  16748. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &grp->P, 1 ) );
  16749. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &grp->P, 255 ) );
  16750. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &grp->P, &grp->P, 19 ) );
  16751. grp->pbits = mbedtls_mpi_bitlen( &grp->P );
  16752. /* Y intentionaly not set, since we use x/z coordinates.
  16753. * This is used as a marker to identify Montgomery curves! */
  16754. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &grp->G.X, 9 ) );
  16755. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &grp->G.Z, 1 ) );
  16756. mbedtls_mpi_free( &grp->G.Y );
  16757. /* Actually, the required msb for private keys */
  16758. grp->nbits = 254;
  16759. cleanup:
  16760. if( ret != 0 )
  16761. mbedtls_ecp_group_free( grp );
  16762. return( ret );
  16763. }
  16764. #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
  16765. /*
  16766. * Set a group using well-known domain parameters
  16767. */
  16768. int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id )
  16769. {
  16770. mbedtls_ecp_group_free( grp );
  16771. grp->id = id;
  16772. switch( id )
  16773. {
  16774. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
  16775. case MBEDTLS_ECP_DP_SECP192R1:
  16776. NIST_MODP( p192 );
  16777. return( LOAD_GROUP( secp192r1 ) );
  16778. #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
  16779. #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
  16780. case MBEDTLS_ECP_DP_SECP224R1:
  16781. NIST_MODP( p224 );
  16782. return( LOAD_GROUP( secp224r1 ) );
  16783. #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
  16784. #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
  16785. case MBEDTLS_ECP_DP_SECP256R1:
  16786. NIST_MODP( p256 );
  16787. return( LOAD_GROUP( secp256r1 ) );
  16788. #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
  16789. #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
  16790. case MBEDTLS_ECP_DP_SECP384R1:
  16791. NIST_MODP( p384 );
  16792. return( LOAD_GROUP( secp384r1 ) );
  16793. #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
  16794. #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
  16795. case MBEDTLS_ECP_DP_SECP521R1:
  16796. NIST_MODP( p521 );
  16797. return( LOAD_GROUP( secp521r1 ) );
  16798. #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
  16799. #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
  16800. case MBEDTLS_ECP_DP_SECP192K1:
  16801. grp->modp = ecp_mod_p192k1;
  16802. return( LOAD_GROUP_A( secp192k1 ) );
  16803. #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
  16804. #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
  16805. case MBEDTLS_ECP_DP_SECP224K1:
  16806. grp->modp = ecp_mod_p224k1;
  16807. return( LOAD_GROUP_A( secp224k1 ) );
  16808. #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
  16809. #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
  16810. case MBEDTLS_ECP_DP_SECP256K1:
  16811. grp->modp = ecp_mod_p256k1;
  16812. return( LOAD_GROUP_A( secp256k1 ) );
  16813. #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
  16814. #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
  16815. case MBEDTLS_ECP_DP_BP256R1:
  16816. return( LOAD_GROUP_A( brainpoolP256r1 ) );
  16817. #endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
  16818. #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
  16819. case MBEDTLS_ECP_DP_BP384R1:
  16820. return( LOAD_GROUP_A( brainpoolP384r1 ) );
  16821. #endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
  16822. #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
  16823. case MBEDTLS_ECP_DP_BP512R1:
  16824. return( LOAD_GROUP_A( brainpoolP512r1 ) );
  16825. #endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
  16826. #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
  16827. case MBEDTLS_ECP_DP_CURVE25519:
  16828. grp->modp = ecp_mod_p255;
  16829. return( ecp_use_curve25519( grp ) );
  16830. #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
  16831. default:
  16832. mbedtls_ecp_group_free( grp );
  16833. return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
  16834. }
  16835. }
  16836. #if defined(MBEDTLS_ECP_NIST_OPTIM)
  16837. /*
  16838. * Fast reduction modulo the primes used by the NIST curves.
  16839. *
  16840. * These functions are critical for speed, but not needed for correct
  16841. * operations. So, we make the choice to heavily rely on the internals of our
  16842. * bignum library, which creates a tight coupling between these functions and
  16843. * our MPI implementation. However, the coupling between the ECP module and
  16844. * MPI remains loose, since these functions can be deactivated at will.
  16845. */
  16846. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
  16847. /*
  16848. * Compared to the way things are presented in FIPS 186-3 D.2,
  16849. * we proceed in columns, from right (least significant chunk) to left,
  16850. * adding chunks to N in place, and keeping a carry for the next chunk.
  16851. * This avoids moving things around in memory, and uselessly adding zeros,
  16852. * compared to the more straightforward, line-oriented approach.
  16853. *
  16854. * For this prime we need to handle data in chunks of 64 bits.
  16855. * Since this is always a multiple of our basic mbedtls_mpi_uint, we can
  16856. * use a mbedtls_mpi_uint * to designate such a chunk, and small loops to handle it.
  16857. */
  16858. /* Add 64-bit chunks (dst += src) and update carry */
  16859. static inline void add64( mbedtls_mpi_uint *dst, mbedtls_mpi_uint *src, mbedtls_mpi_uint *carry )
  16860. {
  16861. unsigned char i;
  16862. mbedtls_mpi_uint c = 0;
  16863. for( i = 0; i < 8 / sizeof( mbedtls_mpi_uint ); i++, dst++, src++ )
  16864. {
  16865. *dst += c; c = ( *dst < c );
  16866. *dst += *src; c += ( *dst < *src );
  16867. }
  16868. *carry += c;
  16869. }
  16870. /* Add carry to a 64-bit chunk and update carry */
  16871. static inline void carry64( mbedtls_mpi_uint *dst, mbedtls_mpi_uint *carry )
  16872. {
  16873. unsigned char i;
  16874. for( i = 0; i < 8 / sizeof( mbedtls_mpi_uint ); i++, dst++ )
  16875. {
  16876. *dst += *carry;
  16877. *carry = ( *dst < *carry );
  16878. }
  16879. }
  16880. #define WIDTH 8 / sizeof( mbedtls_mpi_uint )
  16881. #define A( i ) N->p + i * WIDTH
  16882. #define ADD( i ) add64( p, A( i ), &c )
  16883. #define NEXT p += WIDTH; carry64( p, &c )
  16884. #define LAST p += WIDTH; *p = c; while( ++p < end ) *p = 0
  16885. /*
  16886. * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
  16887. */
  16888. static int ecp_mod_p192( mbedtls_mpi *N )
  16889. {
  16890. int ret;
  16891. mbedtls_mpi_uint c = 0;
  16892. mbedtls_mpi_uint *p, *end;
  16893. /* Make sure we have enough blocks so that A(5) is legal */
  16894. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( N, 6 * WIDTH ) );
  16895. p = N->p;
  16896. end = p + N->n;
  16897. ADD( 3 ); ADD( 5 ); NEXT; // A0 += A3 + A5
  16898. ADD( 3 ); ADD( 4 ); ADD( 5 ); NEXT; // A1 += A3 + A4 + A5
  16899. ADD( 4 ); ADD( 5 ); LAST; // A2 += A4 + A5
  16900. cleanup:
  16901. return( ret );
  16902. }
  16903. #undef WIDTH
  16904. #undef A
  16905. #undef ADD
  16906. #undef NEXT
  16907. #undef LAST
  16908. #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
  16909. #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
  16910. defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
  16911. defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
  16912. /*
  16913. * The reader is advised to first understand ecp_mod_p192() since the same
  16914. * general structure is used here, but with additional complications:
  16915. * (1) chunks of 32 bits, and (2) subtractions.
  16916. */
  16917. /*
  16918. * For these primes, we need to handle data in chunks of 32 bits.
  16919. * This makes it more complicated if we use 64 bits limbs in MPI,
  16920. * which prevents us from using a uniform access method as for p192.
  16921. *
  16922. * So, we define a mini abstraction layer to access 32 bit chunks,
  16923. * load them in 'cur' for work, and store them back from 'cur' when done.
  16924. *
  16925. * While at it, also define the size of N in terms of 32-bit chunks.
  16926. */
  16927. #define LOAD32 cur = A( i );
  16928. #if defined(MBEDTLS_HAVE_INT32) /* 32 bit */
  16929. #define MAX32 N->n
  16930. #define A( j ) N->p[j]
  16931. #define STORE32 N->p[i] = cur;
  16932. #else /* 64-bit */
  16933. #define MAX32 N->n * 2
  16934. #define A( j ) j % 2 ? (uint32_t)( N->p[j/2] >> 32 ) : (uint32_t)( N->p[j/2] )
  16935. #define STORE32 \
  16936. if( i % 2 ) { \
  16937. N->p[i/2] &= 0x00000000FFFFFFFF; \
  16938. N->p[i/2] |= ((mbedtls_mpi_uint) cur) << 32; \
  16939. } else { \
  16940. N->p[i/2] &= 0xFFFFFFFF00000000; \
  16941. N->p[i/2] |= (mbedtls_mpi_uint) cur; \
  16942. }
  16943. #endif /* sizeof( mbedtls_mpi_uint ) */
  16944. /*
  16945. * Helpers for addition and subtraction of chunks, with signed carry.
  16946. */
  16947. static inline void add32( uint32_t *dst, uint32_t src, signed char *carry )
  16948. {
  16949. *dst += src;
  16950. *carry += ( *dst < src );
  16951. }
  16952. static inline void sub32( uint32_t *dst, uint32_t src, signed char *carry )
  16953. {
  16954. *carry -= ( *dst < src );
  16955. *dst -= src;
  16956. }
  16957. #define ADD( j ) add32( &cur, A( j ), &c );
  16958. #define SUB( j ) sub32( &cur, A( j ), &c );
  16959. /*
  16960. * Helpers for the main 'loop'
  16961. * (see fix_negative for the motivation of C)
  16962. */
  16963. #define INIT( b ) \
  16964. int ret; \
  16965. signed char c = 0, cc; \
  16966. uint32_t cur; \
  16967. size_t i = 0, bits = b; \
  16968. mbedtls_mpi C; \
  16969. mbedtls_mpi_uint Cp[ b / 8 / sizeof( mbedtls_mpi_uint) + 1 ]; \
  16970. \
  16971. C.s = 1; \
  16972. C.n = b / 8 / sizeof( mbedtls_mpi_uint) + 1; \
  16973. C.p = Cp; \
  16974. memset( Cp, 0, C.n * sizeof( mbedtls_mpi_uint ) ); \
  16975. \
  16976. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( N, b * 2 / 8 / sizeof( mbedtls_mpi_uint ) ) ); \
  16977. LOAD32;
  16978. #define NEXT \
  16979. STORE32; i++; LOAD32; \
  16980. cc = c; c = 0; \
  16981. if( cc < 0 ) \
  16982. sub32( &cur, -cc, &c ); \
  16983. else \
  16984. add32( &cur, cc, &c ); \
  16985. #define LAST \
  16986. STORE32; i++; \
  16987. cur = c > 0 ? c : 0; STORE32; \
  16988. cur = 0; while( ++i < MAX32 ) { STORE32; } \
  16989. if( c < 0 ) fix_negative( N, c, &C, bits );
  16990. /*
  16991. * If the result is negative, we get it in the form
  16992. * c * 2^(bits + 32) + N, with c negative and N positive shorter than 'bits'
  16993. */
  16994. static inline int fix_negative( mbedtls_mpi *N, signed char c, mbedtls_mpi *C, size_t bits )
  16995. {
  16996. int ret;
  16997. /* C = - c * 2^(bits + 32) */
  16998. #if !defined(MBEDTLS_HAVE_INT64)
  16999. ((void) bits);
  17000. #else
  17001. if( bits == 224 )
  17002. C->p[ C->n - 1 ] = ((mbedtls_mpi_uint) -c) << 32;
  17003. else
  17004. #endif
  17005. C->p[ C->n - 1 ] = (mbedtls_mpi_uint) -c;
  17006. /* N = - ( C - N ) */
  17007. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N, C, N ) );
  17008. N->s = -1;
  17009. cleanup:
  17010. return( ret );
  17011. }
  17012. #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
  17013. /*
  17014. * Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
  17015. */
  17016. static int ecp_mod_p224( mbedtls_mpi *N )
  17017. {
  17018. INIT( 224 );
  17019. SUB( 7 ); SUB( 11 ); NEXT; // A0 += -A7 - A11
  17020. SUB( 8 ); SUB( 12 ); NEXT; // A1 += -A8 - A12
  17021. SUB( 9 ); SUB( 13 ); NEXT; // A2 += -A9 - A13
  17022. SUB( 10 ); ADD( 7 ); ADD( 11 ); NEXT; // A3 += -A10 + A7 + A11
  17023. SUB( 11 ); ADD( 8 ); ADD( 12 ); NEXT; // A4 += -A11 + A8 + A12
  17024. SUB( 12 ); ADD( 9 ); ADD( 13 ); NEXT; // A5 += -A12 + A9 + A13
  17025. SUB( 13 ); ADD( 10 ); LAST; // A6 += -A13 + A10
  17026. cleanup:
  17027. return( ret );
  17028. }
  17029. #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
  17030. #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
  17031. /*
  17032. * Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
  17033. */
  17034. static int ecp_mod_p256( mbedtls_mpi *N )
  17035. {
  17036. INIT( 256 );
  17037. ADD( 8 ); ADD( 9 );
  17038. SUB( 11 ); SUB( 12 ); SUB( 13 ); SUB( 14 ); NEXT; // A0
  17039. ADD( 9 ); ADD( 10 );
  17040. SUB( 12 ); SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A1
  17041. ADD( 10 ); ADD( 11 );
  17042. SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A2
  17043. ADD( 11 ); ADD( 11 ); ADD( 12 ); ADD( 12 ); ADD( 13 );
  17044. SUB( 15 ); SUB( 8 ); SUB( 9 ); NEXT; // A3
  17045. ADD( 12 ); ADD( 12 ); ADD( 13 ); ADD( 13 ); ADD( 14 );
  17046. SUB( 9 ); SUB( 10 ); NEXT; // A4
  17047. ADD( 13 ); ADD( 13 ); ADD( 14 ); ADD( 14 ); ADD( 15 );
  17048. SUB( 10 ); SUB( 11 ); NEXT; // A5
  17049. ADD( 14 ); ADD( 14 ); ADD( 15 ); ADD( 15 ); ADD( 14 ); ADD( 13 );
  17050. SUB( 8 ); SUB( 9 ); NEXT; // A6
  17051. ADD( 15 ); ADD( 15 ); ADD( 15 ); ADD( 8 );
  17052. SUB( 10 ); SUB( 11 ); SUB( 12 ); SUB( 13 ); LAST; // A7
  17053. cleanup:
  17054. return( ret );
  17055. }
  17056. #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
  17057. #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
  17058. /*
  17059. * Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
  17060. */
  17061. static int ecp_mod_p384( mbedtls_mpi *N )
  17062. {
  17063. INIT( 384 );
  17064. ADD( 12 ); ADD( 21 ); ADD( 20 );
  17065. SUB( 23 ); NEXT; // A0
  17066. ADD( 13 ); ADD( 22 ); ADD( 23 );
  17067. SUB( 12 ); SUB( 20 ); NEXT; // A2
  17068. ADD( 14 ); ADD( 23 );
  17069. SUB( 13 ); SUB( 21 ); NEXT; // A2
  17070. ADD( 15 ); ADD( 12 ); ADD( 20 ); ADD( 21 );
  17071. SUB( 14 ); SUB( 22 ); SUB( 23 ); NEXT; // A3
  17072. ADD( 21 ); ADD( 21 ); ADD( 16 ); ADD( 13 ); ADD( 12 ); ADD( 20 ); ADD( 22 );
  17073. SUB( 15 ); SUB( 23 ); SUB( 23 ); NEXT; // A4
  17074. ADD( 22 ); ADD( 22 ); ADD( 17 ); ADD( 14 ); ADD( 13 ); ADD( 21 ); ADD( 23 );
  17075. SUB( 16 ); NEXT; // A5
  17076. ADD( 23 ); ADD( 23 ); ADD( 18 ); ADD( 15 ); ADD( 14 ); ADD( 22 );
  17077. SUB( 17 ); NEXT; // A6
  17078. ADD( 19 ); ADD( 16 ); ADD( 15 ); ADD( 23 );
  17079. SUB( 18 ); NEXT; // A7
  17080. ADD( 20 ); ADD( 17 ); ADD( 16 );
  17081. SUB( 19 ); NEXT; // A8
  17082. ADD( 21 ); ADD( 18 ); ADD( 17 );
  17083. SUB( 20 ); NEXT; // A9
  17084. ADD( 22 ); ADD( 19 ); ADD( 18 );
  17085. SUB( 21 ); NEXT; // A10
  17086. ADD( 23 ); ADD( 20 ); ADD( 19 );
  17087. SUB( 22 ); LAST; // A11
  17088. cleanup:
  17089. return( ret );
  17090. }
  17091. #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
  17092. #undef A
  17093. #undef LOAD32
  17094. #undef STORE32
  17095. #undef MAX32
  17096. #undef INIT
  17097. #undef NEXT
  17098. #undef LAST
  17099. #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED ||
  17100. MBEDTLS_ECP_DP_SECP256R1_ENABLED ||
  17101. MBEDTLS_ECP_DP_SECP384R1_ENABLED */
  17102. #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
  17103. /*
  17104. * Here we have an actual Mersenne prime, so things are more straightforward.
  17105. * However, chunks are aligned on a 'weird' boundary (521 bits).
  17106. */
  17107. /* Size of p521 in terms of mbedtls_mpi_uint */
  17108. #define P521_WIDTH ( 521 / 8 / sizeof( mbedtls_mpi_uint ) + 1 )
  17109. /* Bits to keep in the most significant mbedtls_mpi_uint */
  17110. #define P521_MASK 0x01FF
  17111. /*
  17112. * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
  17113. * Write N as A1 + 2^521 A0, return A0 + A1
  17114. */
  17115. static int ecp_mod_p521( mbedtls_mpi *N )
  17116. {
  17117. int ret;
  17118. size_t i;
  17119. mbedtls_mpi M;
  17120. mbedtls_mpi_uint Mp[P521_WIDTH + 1];
  17121. /* Worst case for the size of M is when mbedtls_mpi_uint is 16 bits:
  17122. * we need to hold bits 513 to 1056, which is 34 limbs, that is
  17123. * P521_WIDTH + 1. Otherwise P521_WIDTH is enough. */
  17124. if( N->n < P521_WIDTH )
  17125. return( 0 );
  17126. /* M = A1 */
  17127. M.s = 1;
  17128. M.n = N->n - ( P521_WIDTH - 1 );
  17129. if( M.n > P521_WIDTH + 1 )
  17130. M.n = P521_WIDTH + 1;
  17131. M.p = Mp;
  17132. memcpy( Mp, N->p + P521_WIDTH - 1, M.n * sizeof( mbedtls_mpi_uint ) );
  17133. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &M, 521 % ( 8 * sizeof( mbedtls_mpi_uint ) ) ) );
  17134. /* N = A0 */
  17135. N->p[P521_WIDTH - 1] &= P521_MASK;
  17136. for( i = P521_WIDTH; i < N->n; i++ )
  17137. N->p[i] = 0;
  17138. /* N = A0 + A1 */
  17139. MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( N, N, &M ) );
  17140. cleanup:
  17141. return( ret );
  17142. }
  17143. #undef P521_WIDTH
  17144. #undef P521_MASK
  17145. #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
  17146. #endif /* MBEDTLS_ECP_NIST_OPTIM */
  17147. #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
  17148. /* Size of p255 in terms of mbedtls_mpi_uint */
  17149. #define P255_WIDTH ( 255 / 8 / sizeof( mbedtls_mpi_uint ) + 1 )
  17150. /*
  17151. * Fast quasi-reduction modulo p255 = 2^255 - 19
  17152. * Write N as A0 + 2^255 A1, return A0 + 19 * A1
  17153. */
  17154. static int ecp_mod_p255( mbedtls_mpi *N )
  17155. {
  17156. int ret;
  17157. size_t i;
  17158. mbedtls_mpi M;
  17159. mbedtls_mpi_uint Mp[P255_WIDTH + 2];
  17160. if( N->n < P255_WIDTH )
  17161. return( 0 );
  17162. /* M = A1 */
  17163. M.s = 1;
  17164. M.n = N->n - ( P255_WIDTH - 1 );
  17165. if( M.n > P255_WIDTH + 1 )
  17166. M.n = P255_WIDTH + 1;
  17167. M.p = Mp;
  17168. memset( Mp, 0, sizeof Mp );
  17169. memcpy( Mp, N->p + P255_WIDTH - 1, M.n * sizeof( mbedtls_mpi_uint ) );
  17170. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &M, 255 % ( 8 * sizeof( mbedtls_mpi_uint ) ) ) );
  17171. M.n++; /* Make room for multiplication by 19 */
  17172. /* N = A0 */
  17173. MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( N, 255, 0 ) );
  17174. for( i = P255_WIDTH; i < N->n; i++ )
  17175. N->p[i] = 0;
  17176. /* N = A0 + 19 * A1 */
  17177. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &M, 19 ) );
  17178. MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( N, N, &M ) );
  17179. cleanup:
  17180. return( ret );
  17181. }
  17182. #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
  17183. #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
  17184. defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
  17185. defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
  17186. /*
  17187. * Fast quasi-reduction modulo P = 2^s - R,
  17188. * with R about 33 bits, used by the Koblitz curves.
  17189. *
  17190. * Write N as A0 + 2^224 A1, return A0 + R * A1.
  17191. * Actually do two passes, since R is big.
  17192. */
  17193. #define P_KOBLITZ_MAX ( 256 / 8 / sizeof( mbedtls_mpi_uint ) ) // Max limbs in P
  17194. #define P_KOBLITZ_R ( 8 / sizeof( mbedtls_mpi_uint ) ) // Limbs in R
  17195. static inline int ecp_mod_koblitz( mbedtls_mpi *N, mbedtls_mpi_uint *Rp, size_t p_limbs,
  17196. size_t adjust, size_t shift, mbedtls_mpi_uint mask )
  17197. {
  17198. int ret;
  17199. size_t i;
  17200. mbedtls_mpi M, R;
  17201. mbedtls_mpi_uint Mp[P_KOBLITZ_MAX + P_KOBLITZ_R + 1];
  17202. if( N->n < p_limbs )
  17203. return( 0 );
  17204. /* Init R */
  17205. R.s = 1;
  17206. R.p = Rp;
  17207. R.n = P_KOBLITZ_R;
  17208. /* Common setup for M */
  17209. M.s = 1;
  17210. M.p = Mp;
  17211. /* M = A1 */
  17212. M.n = N->n - ( p_limbs - adjust );
  17213. if( M.n > p_limbs + adjust )
  17214. M.n = p_limbs + adjust;
  17215. memset( Mp, 0, sizeof Mp );
  17216. memcpy( Mp, N->p + p_limbs - adjust, M.n * sizeof( mbedtls_mpi_uint ) );
  17217. if( shift != 0 )
  17218. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &M, shift ) );
  17219. M.n += R.n; /* Make room for multiplication by R */
  17220. /* N = A0 */
  17221. if( mask != 0 )
  17222. N->p[p_limbs - 1] &= mask;
  17223. for( i = p_limbs; i < N->n; i++ )
  17224. N->p[i] = 0;
  17225. /* N = A0 + R * A1 */
  17226. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &M, &M, &R ) );
  17227. MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( N, N, &M ) );
  17228. /* Second pass */
  17229. /* M = A1 */
  17230. M.n = N->n - ( p_limbs - adjust );
  17231. if( M.n > p_limbs + adjust )
  17232. M.n = p_limbs + adjust;
  17233. memset( Mp, 0, sizeof Mp );
  17234. memcpy( Mp, N->p + p_limbs - adjust, M.n * sizeof( mbedtls_mpi_uint ) );
  17235. if( shift != 0 )
  17236. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &M, shift ) );
  17237. M.n += R.n; /* Make room for multiplication by R */
  17238. /* N = A0 */
  17239. if( mask != 0 )
  17240. N->p[p_limbs - 1] &= mask;
  17241. for( i = p_limbs; i < N->n; i++ )
  17242. N->p[i] = 0;
  17243. /* N = A0 + R * A1 */
  17244. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &M, &M, &R ) );
  17245. MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( N, N, &M ) );
  17246. cleanup:
  17247. return( ret );
  17248. }
  17249. #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED) ||
  17250. MBEDTLS_ECP_DP_SECP224K1_ENABLED) ||
  17251. MBEDTLS_ECP_DP_SECP256K1_ENABLED) */
  17252. #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
  17253. /*
  17254. * Fast quasi-reduction modulo p192k1 = 2^192 - R,
  17255. * with R = 2^32 + 2^12 + 2^8 + 2^7 + 2^6 + 2^3 + 1 = 0x0100001119
  17256. */
  17257. static int ecp_mod_p192k1( mbedtls_mpi *N )
  17258. {
  17259. static mbedtls_mpi_uint Rp[] = {
  17260. BYTES_TO_T_UINT_8( 0xC9, 0x11, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 ) };
  17261. return( ecp_mod_koblitz( N, Rp, 192 / 8 / sizeof( mbedtls_mpi_uint ), 0, 0, 0 ) );
  17262. }
  17263. #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
  17264. #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
  17265. /*
  17266. * Fast quasi-reduction modulo p224k1 = 2^224 - R,
  17267. * with R = 2^32 + 2^12 + 2^11 + 2^9 + 2^7 + 2^4 + 2 + 1 = 0x0100001A93
  17268. */
  17269. static int ecp_mod_p224k1( mbedtls_mpi *N )
  17270. {
  17271. static mbedtls_mpi_uint Rp[] = {
  17272. BYTES_TO_T_UINT_8( 0x93, 0x1A, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 ) };
  17273. #if defined(MBEDTLS_HAVE_INT64)
  17274. return( ecp_mod_koblitz( N, Rp, 4, 1, 32, 0xFFFFFFFF ) );
  17275. #else
  17276. return( ecp_mod_koblitz( N, Rp, 224 / 8 / sizeof( mbedtls_mpi_uint ), 0, 0, 0 ) );
  17277. #endif
  17278. }
  17279. #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
  17280. #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
  17281. /*
  17282. * Fast quasi-reduction modulo p256k1 = 2^256 - R,
  17283. * with R = 2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1 = 0x01000003D1
  17284. */
  17285. static int ecp_mod_p256k1( mbedtls_mpi *N )
  17286. {
  17287. static mbedtls_mpi_uint Rp[] = {
  17288. BYTES_TO_T_UINT_8( 0xD1, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 ) };
  17289. return( ecp_mod_koblitz( N, Rp, 256 / 8 / sizeof( mbedtls_mpi_uint ), 0, 0, 0 ) );
  17290. }
  17291. #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
  17292. #endif /* !MBEDTLS_ECP_ALT */
  17293. #endif /* MBEDTLS_ECP_C */
  17294. /*
  17295. Amalgamated build undefines
  17296. */
  17297. #undef ADD
  17298. #undef BC
  17299. #undef BEFORE_COLON
  17300. #undef F
  17301. #undef F0
  17302. #undef F1
  17303. #undef F2
  17304. #undef F3
  17305. #undef F4
  17306. #undef F5
  17307. #undef FSb
  17308. #undef K
  17309. #undef KK
  17310. #undef P
  17311. #undef R
  17312. #undef ROTR
  17313. #undef S
  17314. #undef S0
  17315. #undef S1
  17316. #undef S2
  17317. #undef S3
  17318. #undef SAFE_SNPRINTF
  17319. #undef SHR
  17320. #undef close
  17321. #undef read
  17322. #undef supported_init
  17323. #undef write
  17324. /********* Start of file library/entropy.c ************/
  17325. /*
  17326. * Entropy accumulator implementation
  17327. *
  17328. * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
  17329. * SPDX-License-Identifier: Apache-2.0
  17330. *
  17331. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  17332. * not use this file except in compliance with the License.
  17333. * You may obtain a copy of the License at
  17334. *
  17335. * http://www.apache.org/licenses/LICENSE-2.0
  17336. *
  17337. * Unless required by applicable law or agreed to in writing, software
  17338. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17339. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17340. * See the License for the specific language governing permissions and
  17341. * limitations under the License.
  17342. *
  17343. * This file is part of mbed TLS (https://tls.mbed.org)
  17344. */
  17345. #if !defined(MBEDTLS_CONFIG_FILE)
  17346. #else
  17347. #endif
  17348. #if defined(MBEDTLS_ENTROPY_C)
  17349. #if defined(MBEDTLS_TEST_NULL_ENTROPY)
  17350. #warning "**** WARNING! MBEDTLS_TEST_NULL_ENTROPY defined! "
  17351. #warning "**** THIS BUILD HAS NO DEFINED ENTROPY SOURCES "
  17352. #warning "**** THIS BUILD IS *NOT* SUITABLE FOR PRODUCTION USE "
  17353. #endif
  17354. #include <string.h>
  17355. #if defined(MBEDTLS_FS_IO)
  17356. #include <stdio.h>
  17357. #endif
  17358. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  17359. #endif
  17360. #if defined(MBEDTLS_SELF_TEST)
  17361. #if defined(MBEDTLS_PLATFORM_C)
  17362. #else
  17363. #include <stdio.h>
  17364. #define mbedtls_printf printf
  17365. #endif /* MBEDTLS_PLATFORM_C */
  17366. #endif /* MBEDTLS_SELF_TEST */
  17367. #if defined(MBEDTLS_HAVEGE_C)
  17368. #endif
  17369. /* Implementation that should never be optimized out by the compiler */
  17370. /* zeroize was here */
  17371. #define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
  17372. void mbedtls_entropy_init( mbedtls_entropy_context *ctx )
  17373. {
  17374. ctx->source_count = 0;
  17375. memset( ctx->source, 0, sizeof( ctx->source ) );
  17376. #if defined(MBEDTLS_THREADING_C)
  17377. mbedtls_mutex_init( &ctx->mutex );
  17378. #endif
  17379. ctx->accumulator_started = 0;
  17380. #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
  17381. mbedtls_sha512_init( &ctx->accumulator );
  17382. #else
  17383. mbedtls_sha256_init( &ctx->accumulator );
  17384. #endif
  17385. #if defined(MBEDTLS_HAVEGE_C)
  17386. mbedtls_havege_init( &ctx->havege_data );
  17387. #endif
  17388. /* Reminder: Update ENTROPY_HAVE_STRONG in the test files
  17389. * when adding more strong entropy sources here. */
  17390. #if defined(MBEDTLS_TEST_NULL_ENTROPY)
  17391. mbedtls_entropy_add_source( ctx, mbedtls_null_entropy_poll, NULL,
  17392. 1, MBEDTLS_ENTROPY_SOURCE_STRONG );
  17393. #endif
  17394. #if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
  17395. #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
  17396. mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL,
  17397. MBEDTLS_ENTROPY_MIN_PLATFORM,
  17398. MBEDTLS_ENTROPY_SOURCE_STRONG );
  17399. #endif
  17400. #if defined(MBEDTLS_TIMING_C)
  17401. mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL,
  17402. MBEDTLS_ENTROPY_MIN_HARDCLOCK,
  17403. MBEDTLS_ENTROPY_SOURCE_WEAK );
  17404. #endif
  17405. #if defined(MBEDTLS_HAVEGE_C)
  17406. mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data,
  17407. MBEDTLS_ENTROPY_MIN_HAVEGE,
  17408. MBEDTLS_ENTROPY_SOURCE_STRONG );
  17409. #endif
  17410. #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
  17411. mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL,
  17412. MBEDTLS_ENTROPY_MIN_HARDWARE,
  17413. MBEDTLS_ENTROPY_SOURCE_STRONG );
  17414. #endif
  17415. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  17416. mbedtls_entropy_add_source( ctx, mbedtls_nv_seed_poll, NULL,
  17417. MBEDTLS_ENTROPY_BLOCK_SIZE,
  17418. MBEDTLS_ENTROPY_SOURCE_STRONG );
  17419. ctx->initial_entropy_run = 0;
  17420. #endif
  17421. #endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
  17422. }
  17423. void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
  17424. {
  17425. #if defined(MBEDTLS_HAVEGE_C)
  17426. mbedtls_havege_free( &ctx->havege_data );
  17427. #endif
  17428. #if defined(MBEDTLS_THREADING_C)
  17429. mbedtls_mutex_free( &ctx->mutex );
  17430. #endif
  17431. #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
  17432. mbedtls_sha512_free( &ctx->accumulator );
  17433. #else
  17434. mbedtls_sha256_free( &ctx->accumulator );
  17435. #endif
  17436. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  17437. ctx->initial_entropy_run = 0;
  17438. #endif
  17439. ctx->source_count = 0;
  17440. mbedtls_zeroize( ctx->source, sizeof( ctx->source ) );
  17441. ctx->accumulator_started = 0;
  17442. }
  17443. int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
  17444. mbedtls_entropy_f_source_ptr f_source, void *p_source,
  17445. size_t threshold, int strong )
  17446. {
  17447. int idx, ret = 0;
  17448. #if defined(MBEDTLS_THREADING_C)
  17449. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  17450. return( ret );
  17451. #endif
  17452. idx = ctx->source_count;
  17453. if( idx >= MBEDTLS_ENTROPY_MAX_SOURCES )
  17454. {
  17455. ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
  17456. goto exit;
  17457. }
  17458. ctx->source[idx].f_source = f_source;
  17459. ctx->source[idx].p_source = p_source;
  17460. ctx->source[idx].threshold = threshold;
  17461. ctx->source[idx].strong = strong;
  17462. ctx->source_count++;
  17463. exit:
  17464. #if defined(MBEDTLS_THREADING_C)
  17465. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  17466. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  17467. #endif
  17468. return( ret );
  17469. }
  17470. /*
  17471. * Entropy accumulator update
  17472. */
  17473. static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
  17474. const unsigned char *data, size_t len )
  17475. {
  17476. unsigned char header[2];
  17477. unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
  17478. size_t use_len = len;
  17479. const unsigned char *p = data;
  17480. int ret = 0;
  17481. if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
  17482. {
  17483. #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
  17484. if( ( ret = mbedtls_sha512_ret( data, len, tmp, 0 ) ) != 0 )
  17485. goto cleanup;
  17486. #else
  17487. if( ( ret = mbedtls_sha256_ret( data, len, tmp, 0 ) ) != 0 )
  17488. goto cleanup;
  17489. #endif
  17490. p = tmp;
  17491. use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
  17492. }
  17493. header[0] = source_id;
  17494. header[1] = use_len & 0xFF;
  17495. /*
  17496. * Start the accumulator if this has not already happened. Note that
  17497. * it is sufficient to start the accumulator here only because all calls to
  17498. * gather entropy eventually execute this code.
  17499. */
  17500. #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
  17501. if( ctx->accumulator_started == 0 &&
  17502. ( ret = mbedtls_sha512_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
  17503. goto cleanup;
  17504. else
  17505. ctx->accumulator_started = 1;
  17506. if( ( ret = mbedtls_sha512_update_ret( &ctx->accumulator, header, 2 ) ) != 0 )
  17507. goto cleanup;
  17508. ret = mbedtls_sha512_update_ret( &ctx->accumulator, p, use_len );
  17509. #else
  17510. if( ctx->accumulator_started == 0 &&
  17511. ( ret = mbedtls_sha256_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
  17512. goto cleanup;
  17513. else
  17514. ctx->accumulator_started = 1;
  17515. if( ( ret = mbedtls_sha256_update_ret( &ctx->accumulator, header, 2 ) ) != 0 )
  17516. goto cleanup;
  17517. ret = mbedtls_sha256_update_ret( &ctx->accumulator, p, use_len );
  17518. #endif
  17519. cleanup:
  17520. mbedtls_zeroize( tmp, sizeof( tmp ) );
  17521. return( ret );
  17522. }
  17523. int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
  17524. const unsigned char *data, size_t len )
  17525. {
  17526. int ret;
  17527. #if defined(MBEDTLS_THREADING_C)
  17528. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  17529. return( ret );
  17530. #endif
  17531. ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len );
  17532. #if defined(MBEDTLS_THREADING_C)
  17533. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  17534. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  17535. #endif
  17536. return( ret );
  17537. }
  17538. /*
  17539. * Run through the different sources to add entropy to our accumulator
  17540. */
  17541. static int entropy_gather_internal( mbedtls_entropy_context *ctx )
  17542. {
  17543. int ret, i, have_one_strong = 0;
  17544. unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
  17545. size_t olen;
  17546. if( ctx->source_count == 0 )
  17547. return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
  17548. /*
  17549. * Run through our entropy sources
  17550. */
  17551. for( i = 0; i < ctx->source_count; i++ )
  17552. {
  17553. if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
  17554. have_one_strong = 1;
  17555. olen = 0;
  17556. if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
  17557. buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
  17558. {
  17559. goto cleanup;
  17560. }
  17561. /*
  17562. * Add if we actually gathered something
  17563. */
  17564. if( olen > 0 )
  17565. {
  17566. if( ( ret = entropy_update( ctx, (unsigned char) i,
  17567. buf, olen ) ) != 0 )
  17568. return( ret );
  17569. ctx->source[i].size += olen;
  17570. }
  17571. }
  17572. if( have_one_strong == 0 )
  17573. ret = MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE;
  17574. cleanup:
  17575. mbedtls_zeroize( buf, sizeof( buf ) );
  17576. return( ret );
  17577. }
  17578. /*
  17579. * Thread-safe wrapper for entropy_gather_internal()
  17580. */
  17581. int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
  17582. {
  17583. int ret;
  17584. #if defined(MBEDTLS_THREADING_C)
  17585. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  17586. return( ret );
  17587. #endif
  17588. ret = entropy_gather_internal( ctx );
  17589. #if defined(MBEDTLS_THREADING_C)
  17590. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  17591. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  17592. #endif
  17593. return( ret );
  17594. }
  17595. int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
  17596. {
  17597. int ret, count = 0, i, done;
  17598. mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
  17599. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  17600. if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
  17601. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  17602. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  17603. /* Update the NV entropy seed before generating any entropy for outside
  17604. * use.
  17605. */
  17606. if( ctx->initial_entropy_run == 0 )
  17607. {
  17608. ctx->initial_entropy_run = 1;
  17609. if( ( ret = mbedtls_entropy_update_nv_seed( ctx ) ) != 0 )
  17610. return( ret );
  17611. }
  17612. #endif
  17613. #if defined(MBEDTLS_THREADING_C)
  17614. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  17615. return( ret );
  17616. #endif
  17617. /*
  17618. * Always gather extra entropy before a call
  17619. */
  17620. do
  17621. {
  17622. if( count++ > ENTROPY_MAX_LOOP )
  17623. {
  17624. ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  17625. goto exit;
  17626. }
  17627. if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
  17628. goto exit;
  17629. done = 1;
  17630. for( i = 0; i < ctx->source_count; i++ )
  17631. if( ctx->source[i].size < ctx->source[i].threshold )
  17632. done = 0;
  17633. }
  17634. while( ! done );
  17635. memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
  17636. #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
  17637. /*
  17638. * Note that at this stage it is assumed that the accumulator was started
  17639. * in a previous call to entropy_update(). If this is not guaranteed, the
  17640. * code below will fail.
  17641. */
  17642. if( ( ret = mbedtls_sha512_finish_ret( &ctx->accumulator, buf ) ) != 0 )
  17643. goto exit;
  17644. /*
  17645. * Reset accumulator and counters and recycle existing entropy
  17646. */
  17647. mbedtls_sha512_free( &ctx->accumulator );
  17648. mbedtls_sha512_init( &ctx->accumulator );
  17649. if( ( ret = mbedtls_sha512_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
  17650. goto exit;
  17651. if( ( ret = mbedtls_sha512_update_ret( &ctx->accumulator, buf,
  17652. MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
  17653. goto exit;
  17654. /*
  17655. * Perform second SHA-512 on entropy
  17656. */
  17657. if( ( ret = mbedtls_sha512_ret( buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
  17658. buf, 0 ) ) != 0 )
  17659. goto exit;
  17660. #else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
  17661. if( ( ret = mbedtls_sha256_finish_ret( &ctx->accumulator, buf ) ) != 0 )
  17662. goto exit;
  17663. /*
  17664. * Reset accumulator and counters and recycle existing entropy
  17665. */
  17666. mbedtls_sha256_free( &ctx->accumulator );
  17667. mbedtls_sha256_init( &ctx->accumulator );
  17668. if( ( ret = mbedtls_sha256_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
  17669. goto exit;
  17670. if( ( ret = mbedtls_sha256_update_ret( &ctx->accumulator, buf,
  17671. MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
  17672. goto exit;
  17673. /*
  17674. * Perform second SHA-256 on entropy
  17675. */
  17676. if( ( ret = mbedtls_sha256_ret( buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
  17677. buf, 0 ) ) != 0 )
  17678. goto exit;
  17679. #endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
  17680. for( i = 0; i < ctx->source_count; i++ )
  17681. ctx->source[i].size = 0;
  17682. memcpy( output, buf, len );
  17683. ret = 0;
  17684. exit:
  17685. mbedtls_zeroize( buf, sizeof( buf ) );
  17686. #if defined(MBEDTLS_THREADING_C)
  17687. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  17688. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  17689. #endif
  17690. return( ret );
  17691. }
  17692. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  17693. int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx )
  17694. {
  17695. int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
  17696. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  17697. /* Read new seed and write it to NV */
  17698. if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
  17699. return( ret );
  17700. if( mbedtls_nv_seed_write( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
  17701. return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
  17702. /* Manually update the remaining stream with a separator value to diverge */
  17703. memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
  17704. ret = mbedtls_entropy_update_manual( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
  17705. return( ret );
  17706. }
  17707. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  17708. #if defined(MBEDTLS_FS_IO)
  17709. int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
  17710. {
  17711. int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
  17712. FILE *f;
  17713. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  17714. if( ( f = fopen( path, "wb" ) ) == NULL )
  17715. return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
  17716. if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
  17717. goto exit;
  17718. if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
  17719. {
  17720. ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
  17721. goto exit;
  17722. }
  17723. ret = 0;
  17724. exit:
  17725. mbedtls_zeroize( buf, sizeof( buf ) );
  17726. fclose( f );
  17727. return( ret );
  17728. }
  17729. int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
  17730. {
  17731. int ret = 0;
  17732. FILE *f;
  17733. size_t n;
  17734. unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
  17735. if( ( f = fopen( path, "rb" ) ) == NULL )
  17736. return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
  17737. fseek( f, 0, SEEK_END );
  17738. n = (size_t) ftell( f );
  17739. fseek( f, 0, SEEK_SET );
  17740. if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
  17741. n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
  17742. if( fread( buf, 1, n, f ) != n )
  17743. ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
  17744. else
  17745. ret = mbedtls_entropy_update_manual( ctx, buf, n );
  17746. fclose( f );
  17747. mbedtls_zeroize( buf, sizeof( buf ) );
  17748. if( ret != 0 )
  17749. return( ret );
  17750. return( mbedtls_entropy_write_seed_file( ctx, path ) );
  17751. }
  17752. #endif /* MBEDTLS_FS_IO */
  17753. #if defined(MBEDTLS_SELF_TEST)
  17754. #if !defined(MBEDTLS_TEST_NULL_ENTROPY)
  17755. /*
  17756. * Dummy source function
  17757. */
  17758. static int entropy_dummy_source( void *data, unsigned char *output,
  17759. size_t len, size_t *olen )
  17760. {
  17761. ((void) data);
  17762. memset( output, 0x2a, len );
  17763. *olen = len;
  17764. return( 0 );
  17765. }
  17766. #endif /* !MBEDTLS_TEST_NULL_ENTROPY */
  17767. #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
  17768. static int mbedtls_entropy_source_self_test_gather( unsigned char *buf, size_t buf_len )
  17769. {
  17770. int ret = 0;
  17771. size_t entropy_len = 0;
  17772. size_t olen = 0;
  17773. size_t attempts = buf_len;
  17774. while( attempts > 0 && entropy_len < buf_len )
  17775. {
  17776. if( ( ret = mbedtls_hardware_poll( NULL, buf + entropy_len,
  17777. buf_len - entropy_len, &olen ) ) != 0 )
  17778. return( ret );
  17779. entropy_len += olen;
  17780. attempts--;
  17781. }
  17782. if( entropy_len < buf_len )
  17783. {
  17784. ret = 1;
  17785. }
  17786. return( ret );
  17787. }
  17788. static int mbedtls_entropy_source_self_test_check_bits( const unsigned char *buf,
  17789. size_t buf_len )
  17790. {
  17791. unsigned char set= 0xFF;
  17792. unsigned char unset = 0x00;
  17793. size_t i;
  17794. for( i = 0; i < buf_len; i++ )
  17795. {
  17796. set &= buf[i];
  17797. unset |= buf[i];
  17798. }
  17799. return( set == 0xFF || unset == 0x00 );
  17800. }
  17801. /*
  17802. * A test to ensure hat the entropy sources are functioning correctly
  17803. * and there is no obvious failure. The test performs the following checks:
  17804. * - The entropy source is not providing only 0s (all bits unset) or 1s (all
  17805. * bits set).
  17806. * - The entropy source is not providing values in a pattern. Because the
  17807. * hardware could be providing data in an arbitrary length, this check polls
  17808. * the hardware entropy source twice and compares the result to ensure they
  17809. * are not equal.
  17810. * - The error code returned by the entropy source is not an error.
  17811. */
  17812. int mbedtls_entropy_source_self_test( int verbose )
  17813. {
  17814. int ret = 0;
  17815. unsigned char buf0[2 * sizeof( unsigned long long int )];
  17816. unsigned char buf1[2 * sizeof( unsigned long long int )];
  17817. if( verbose != 0 )
  17818. mbedtls_printf( " ENTROPY_BIAS test: " );
  17819. memset( buf0, 0x00, sizeof( buf0 ) );
  17820. memset( buf1, 0x00, sizeof( buf1 ) );
  17821. if( ( ret = mbedtls_entropy_source_self_test_gather( buf0, sizeof( buf0 ) ) ) != 0 )
  17822. goto cleanup;
  17823. if( ( ret = mbedtls_entropy_source_self_test_gather( buf1, sizeof( buf1 ) ) ) != 0 )
  17824. goto cleanup;
  17825. /* Make sure that the returned values are not all 0 or 1 */
  17826. if( ( ret = mbedtls_entropy_source_self_test_check_bits( buf0, sizeof( buf0 ) ) ) != 0 )
  17827. goto cleanup;
  17828. if( ( ret = mbedtls_entropy_source_self_test_check_bits( buf1, sizeof( buf1 ) ) ) != 0 )
  17829. goto cleanup;
  17830. /* Make sure that the entropy source is not returning values in a
  17831. * pattern */
  17832. ret = memcmp( buf0, buf1, sizeof( buf0 ) ) == 0;
  17833. cleanup:
  17834. if( verbose != 0 )
  17835. {
  17836. if( ret != 0 )
  17837. mbedtls_printf( "failed\n" );
  17838. else
  17839. mbedtls_printf( "passed\n" );
  17840. mbedtls_printf( "\n" );
  17841. }
  17842. return( ret != 0 );
  17843. }
  17844. #endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
  17845. /*
  17846. * The actual entropy quality is hard to test, but we can at least
  17847. * test that the functions don't cause errors and write the correct
  17848. * amount of data to buffers.
  17849. */
  17850. int mbedtls_entropy_self_test( int verbose )
  17851. {
  17852. int ret = 1;
  17853. #if !defined(MBEDTLS_TEST_NULL_ENTROPY)
  17854. mbedtls_entropy_context ctx;
  17855. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
  17856. unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
  17857. size_t i, j;
  17858. #endif /* !MBEDTLS_TEST_NULL_ENTROPY */
  17859. if( verbose != 0 )
  17860. mbedtls_printf( " ENTROPY test: " );
  17861. #if !defined(MBEDTLS_TEST_NULL_ENTROPY)
  17862. mbedtls_entropy_init( &ctx );
  17863. /* First do a gather to make sure we have default sources */
  17864. if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
  17865. goto cleanup;
  17866. ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16,
  17867. MBEDTLS_ENTROPY_SOURCE_WEAK );
  17868. if( ret != 0 )
  17869. goto cleanup;
  17870. if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
  17871. goto cleanup;
  17872. /*
  17873. * To test that mbedtls_entropy_func writes correct number of bytes:
  17874. * - use the whole buffer and rely on ASan to detect overruns
  17875. * - collect entropy 8 times and OR the result in an accumulator:
  17876. * any byte should then be 0 with probably 2^(-64), so requiring
  17877. * each of the 32 or 64 bytes to be non-zero has a false failure rate
  17878. * of at most 2^(-58) which is acceptable.
  17879. */
  17880. for( i = 0; i < 8; i++ )
  17881. {
  17882. if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
  17883. goto cleanup;
  17884. for( j = 0; j < sizeof( buf ); j++ )
  17885. acc[j] |= buf[j];
  17886. }
  17887. for( j = 0; j < sizeof( buf ); j++ )
  17888. {
  17889. if( acc[j] == 0 )
  17890. {
  17891. ret = 1;
  17892. goto cleanup;
  17893. }
  17894. }
  17895. #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
  17896. if( ( ret = mbedtls_entropy_source_self_test( 0 ) ) != 0 )
  17897. goto cleanup;
  17898. #endif
  17899. cleanup:
  17900. mbedtls_entropy_free( &ctx );
  17901. #endif /* !MBEDTLS_TEST_NULL_ENTROPY */
  17902. if( verbose != 0 )
  17903. {
  17904. if( ret != 0 )
  17905. mbedtls_printf( "failed\n" );
  17906. else
  17907. mbedtls_printf( "passed\n" );
  17908. mbedtls_printf( "\n" );
  17909. }
  17910. return( ret != 0 );
  17911. }
  17912. #endif /* MBEDTLS_SELF_TEST */
  17913. #endif /* MBEDTLS_ENTROPY_C */
  17914. /*
  17915. Amalgamated build undefines
  17916. */
  17917. #undef ADD
  17918. #undef BC
  17919. #undef BEFORE_COLON
  17920. #undef F
  17921. #undef F0
  17922. #undef F1
  17923. #undef F2
  17924. #undef F3
  17925. #undef F4
  17926. #undef F5
  17927. #undef FSb
  17928. #undef K
  17929. #undef KK
  17930. #undef P
  17931. #undef R
  17932. #undef ROTR
  17933. #undef S
  17934. #undef S0
  17935. #undef S1
  17936. #undef S2
  17937. #undef S3
  17938. #undef SAFE_SNPRINTF
  17939. #undef SHR
  17940. #undef close
  17941. #undef read
  17942. #undef supported_init
  17943. #undef write
  17944. /********* Start of file library/entropy_poll.c ************/
  17945. /*
  17946. * Platform-specific and custom entropy polling functions
  17947. *
  17948. * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
  17949. * SPDX-License-Identifier: Apache-2.0
  17950. *
  17951. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  17952. * not use this file except in compliance with the License.
  17953. * You may obtain a copy of the License at
  17954. *
  17955. * http://www.apache.org/licenses/LICENSE-2.0
  17956. *
  17957. * Unless required by applicable law or agreed to in writing, software
  17958. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17959. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17960. * See the License for the specific language governing permissions and
  17961. * limitations under the License.
  17962. *
  17963. * This file is part of mbed TLS (https://tls.mbed.org)
  17964. */
  17965. #if !defined(MBEDTLS_CONFIG_FILE)
  17966. #else
  17967. #endif
  17968. #if defined(MBEDTLS_ENTROPY_C)
  17969. #if defined(MBEDTLS_TIMING_C)
  17970. #include <string.h>
  17971. #endif
  17972. #if defined(MBEDTLS_HAVEGE_C)
  17973. #endif
  17974. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  17975. #endif
  17976. #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
  17977. #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
  17978. !defined(__APPLE__) && !defined(_WIN32)
  17979. #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
  17980. #endif
  17981. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  17982. #if !defined(_WIN32_WINNT)
  17983. #define _WIN32_WINNT 0x0400
  17984. #endif
  17985. #include <windows.h>
  17986. #include <wincrypt.h>
  17987. int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
  17988. size_t *olen )
  17989. {
  17990. HCRYPTPROV provider;
  17991. ((void) data);
  17992. *olen = 0;
  17993. if( CryptAcquireContext( &provider, NULL, NULL,
  17994. PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
  17995. {
  17996. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  17997. }
  17998. if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
  17999. {
  18000. CryptReleaseContext( provider, 0 );
  18001. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  18002. }
  18003. CryptReleaseContext( provider, 0 );
  18004. *olen = len;
  18005. return( 0 );
  18006. }
  18007. #else /* _WIN32 && !EFIX64 && !EFI32 */
  18008. /*
  18009. * Test for Linux getrandom() support.
  18010. * Since there is no wrapper in the libc yet, use the generic syscall wrapper
  18011. * available in GNU libc and compatible libc's (eg uClibc).
  18012. */
  18013. #if defined(__linux__) && defined(__GLIBC__)
  18014. #include <unistd.h>
  18015. #include <sys/syscall.h>
  18016. #if defined(SYS_getrandom)
  18017. #define HAVE_GETRANDOM
  18018. static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
  18019. {
  18020. /* MemSan cannot understand that the syscall writes to the buffer */
  18021. #if defined(__has_feature)
  18022. #if __has_feature(memory_sanitizer)
  18023. memset( buf, 0, buflen );
  18024. #endif
  18025. #endif
  18026. return( syscall( SYS_getrandom, buf, buflen, flags ) );
  18027. }
  18028. #include <sys/utsname.h>
  18029. /* Check if version is at least 3.17.0 */
  18030. static int check_version_3_17_plus( void )
  18031. {
  18032. int minor;
  18033. struct utsname un;
  18034. const char *ver;
  18035. /* Get version information */
  18036. uname(&un);
  18037. ver = un.release;
  18038. /* Check major version; assume a single digit */
  18039. if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' )
  18040. return( -1 );
  18041. if( ver[0] - '0' > 3 )
  18042. return( 0 );
  18043. /* Ok, so now we know major == 3, check minor.
  18044. * Assume 1 or 2 digits. */
  18045. if( ver[2] < '0' || ver[2] > '9' )
  18046. return( -1 );
  18047. minor = ver[2] - '0';
  18048. if( ver[3] >= '0' && ver[3] <= '9' )
  18049. minor = 10 * minor + ver[3] - '0';
  18050. else if( ver [3] != '.' )
  18051. return( -1 );
  18052. if( minor < 17 )
  18053. return( -1 );
  18054. return( 0 );
  18055. }
  18056. static int has_getrandom = -1;
  18057. #endif /* SYS_getrandom */
  18058. #endif /* __linux__ */
  18059. #include <stdio.h>
  18060. int mbedtls_platform_entropy_poll( void *data,
  18061. unsigned char *output, size_t len, size_t *olen )
  18062. {
  18063. FILE *file;
  18064. size_t read_len;
  18065. ((void) data);
  18066. #if defined(HAVE_GETRANDOM)
  18067. if( has_getrandom == -1 )
  18068. has_getrandom = ( check_version_3_17_plus() == 0 );
  18069. if( has_getrandom )
  18070. {
  18071. int ret;
  18072. if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
  18073. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  18074. *olen = ret;
  18075. return( 0 );
  18076. }
  18077. #endif /* HAVE_GETRANDOM */
  18078. *olen = 0;
  18079. file = fopen( "/dev/urandom", "rb" );
  18080. if( file == NULL )
  18081. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  18082. read_len = fread( output, 1, len, file );
  18083. if( read_len != len )
  18084. {
  18085. fclose( file );
  18086. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  18087. }
  18088. fclose( file );
  18089. *olen = len;
  18090. return( 0 );
  18091. }
  18092. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  18093. #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
  18094. #if defined(MBEDTLS_TEST_NULL_ENTROPY)
  18095. int mbedtls_null_entropy_poll( void *data,
  18096. unsigned char *output, size_t len, size_t *olen )
  18097. {
  18098. ((void) data);
  18099. ((void) output);
  18100. *olen = 0;
  18101. if( len < sizeof(unsigned char) )
  18102. return( 0 );
  18103. *olen = sizeof(unsigned char);
  18104. return( 0 );
  18105. }
  18106. #endif
  18107. #if defined(MBEDTLS_TIMING_C)
  18108. int mbedtls_hardclock_poll( void *data,
  18109. unsigned char *output, size_t len, size_t *olen )
  18110. {
  18111. unsigned long timer = mbedtls_timing_hardclock();
  18112. ((void) data);
  18113. *olen = 0;
  18114. if( len < sizeof(unsigned long) )
  18115. return( 0 );
  18116. memcpy( output, &timer, sizeof(unsigned long) );
  18117. *olen = sizeof(unsigned long);
  18118. return( 0 );
  18119. }
  18120. #endif /* MBEDTLS_TIMING_C */
  18121. #if defined(MBEDTLS_HAVEGE_C)
  18122. int mbedtls_havege_poll( void *data,
  18123. unsigned char *output, size_t len, size_t *olen )
  18124. {
  18125. mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
  18126. *olen = 0;
  18127. if( mbedtls_havege_random( hs, output, len ) != 0 )
  18128. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  18129. *olen = len;
  18130. return( 0 );
  18131. }
  18132. #endif /* MBEDTLS_HAVEGE_C */
  18133. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  18134. int mbedtls_nv_seed_poll( void *data,
  18135. unsigned char *output, size_t len, size_t *olen )
  18136. {
  18137. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  18138. size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
  18139. ((void) data);
  18140. memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
  18141. if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
  18142. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  18143. if( len < use_len )
  18144. use_len = len;
  18145. memcpy( output, buf, use_len );
  18146. *olen = use_len;
  18147. return( 0 );
  18148. }
  18149. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  18150. #endif /* MBEDTLS_ENTROPY_C */
  18151. /*
  18152. Amalgamated build undefines
  18153. */
  18154. #undef ADD
  18155. #undef BC
  18156. #undef BEFORE_COLON
  18157. #undef F
  18158. #undef F0
  18159. #undef F1
  18160. #undef F2
  18161. #undef F3
  18162. #undef F4
  18163. #undef F5
  18164. #undef FSb
  18165. #undef K
  18166. #undef KK
  18167. #undef P
  18168. #undef R
  18169. #undef ROTR
  18170. #undef S
  18171. #undef S0
  18172. #undef S1
  18173. #undef S2
  18174. #undef S3
  18175. #undef SAFE_SNPRINTF
  18176. #undef SHR
  18177. #undef close
  18178. #undef read
  18179. #undef supported_init
  18180. #undef write
  18181. /********* Start of file library/error.c ************/
  18182. /*
  18183. * Error message information
  18184. *
  18185. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  18186. * SPDX-License-Identifier: Apache-2.0
  18187. *
  18188. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  18189. * not use this file except in compliance with the License.
  18190. * You may obtain a copy of the License at
  18191. *
  18192. * http://www.apache.org/licenses/LICENSE-2.0
  18193. *
  18194. * Unless required by applicable law or agreed to in writing, software
  18195. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18196. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18197. * See the License for the specific language governing permissions and
  18198. * limitations under the License.
  18199. *
  18200. * This file is part of mbed TLS (https://tls.mbed.org)
  18201. */
  18202. #if !defined(MBEDTLS_CONFIG_FILE)
  18203. #else
  18204. #endif
  18205. #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
  18206. #include <string.h>
  18207. #endif
  18208. #if defined(MBEDTLS_PLATFORM_C)
  18209. #else
  18210. #define mbedtls_snprintf snprintf
  18211. #define mbedtls_time_t time_t
  18212. #endif
  18213. #if defined(MBEDTLS_ERROR_C)
  18214. #include <stdio.h>
  18215. #if defined(MBEDTLS_AES_C)
  18216. #endif
  18217. #if defined(MBEDTLS_ARC4_C)
  18218. #endif
  18219. #if defined(MBEDTLS_BASE64_C)
  18220. #endif
  18221. #if defined(MBEDTLS_BIGNUM_C)
  18222. #endif
  18223. #if defined(MBEDTLS_BLOWFISH_C)
  18224. #endif
  18225. #if defined(MBEDTLS_CAMELLIA_C)
  18226. #endif
  18227. #if defined(MBEDTLS_CCM_C)
  18228. #endif
  18229. #if defined(MBEDTLS_CIPHER_C)
  18230. #endif
  18231. #if defined(MBEDTLS_CMAC_C)
  18232. #endif
  18233. #if defined(MBEDTLS_CTR_DRBG_C)
  18234. #endif
  18235. #if defined(MBEDTLS_DES_C)
  18236. #endif
  18237. #if defined(MBEDTLS_DHM_C)
  18238. #endif
  18239. #if defined(MBEDTLS_ECP_C)
  18240. #endif
  18241. #if defined(MBEDTLS_ENTROPY_C)
  18242. #endif
  18243. #if defined(MBEDTLS_GCM_C)
  18244. #endif
  18245. #if defined(MBEDTLS_HMAC_DRBG_C)
  18246. #endif
  18247. #if defined(MBEDTLS_MD_C)
  18248. #endif
  18249. #if defined(MBEDTLS_MD2_C)
  18250. #endif
  18251. #if defined(MBEDTLS_MD4_C)
  18252. #endif
  18253. #if defined(MBEDTLS_MD5_C)
  18254. #endif
  18255. #if defined(MBEDTLS_NET_C)
  18256. #endif
  18257. #if defined(MBEDTLS_OID_C)
  18258. #endif
  18259. #if defined(MBEDTLS_PADLOCK_C)
  18260. #endif
  18261. #if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
  18262. #endif
  18263. #if defined(MBEDTLS_PK_C)
  18264. #endif
  18265. #if defined(MBEDTLS_PKCS12_C)
  18266. #endif
  18267. #if defined(MBEDTLS_PKCS5_C)
  18268. #endif
  18269. #if defined(MBEDTLS_RIPEMD160_C)
  18270. #endif
  18271. #if defined(MBEDTLS_RSA_C)
  18272. #endif
  18273. #if defined(MBEDTLS_SHA1_C)
  18274. #endif
  18275. #if defined(MBEDTLS_SHA256_C)
  18276. #endif
  18277. #if defined(MBEDTLS_SHA512_C)
  18278. #endif
  18279. #if defined(MBEDTLS_SSL_TLS_C)
  18280. #endif
  18281. #if defined(MBEDTLS_THREADING_C)
  18282. #endif
  18283. #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C)
  18284. #endif
  18285. #if defined(MBEDTLS_XTEA_C)
  18286. #endif
  18287. void mbedtls_strerror( int ret, char *buf, size_t buflen )
  18288. {
  18289. size_t len;
  18290. int use_ret;
  18291. if( buflen == 0 )
  18292. return;
  18293. memset( buf, 0x00, buflen );
  18294. if( ret < 0 )
  18295. ret = -ret;
  18296. if( ret & 0xFF80 )
  18297. {
  18298. use_ret = ret & 0xFF80;
  18299. // High level error codes
  18300. //
  18301. // BEGIN generated code
  18302. #if defined(MBEDTLS_CIPHER_C)
  18303. if( use_ret == -(MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) )
  18304. mbedtls_snprintf( buf, buflen, "CIPHER - The selected feature is not available" );
  18305. if( use_ret == -(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA) )
  18306. mbedtls_snprintf( buf, buflen, "CIPHER - Bad input parameters" );
  18307. if( use_ret == -(MBEDTLS_ERR_CIPHER_ALLOC_FAILED) )
  18308. mbedtls_snprintf( buf, buflen, "CIPHER - Failed to allocate memory" );
  18309. if( use_ret == -(MBEDTLS_ERR_CIPHER_INVALID_PADDING) )
  18310. mbedtls_snprintf( buf, buflen, "CIPHER - Input data contains invalid padding and is rejected" );
  18311. if( use_ret == -(MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED) )
  18312. mbedtls_snprintf( buf, buflen, "CIPHER - Decryption of block requires a full block" );
  18313. if( use_ret == -(MBEDTLS_ERR_CIPHER_AUTH_FAILED) )
  18314. mbedtls_snprintf( buf, buflen, "CIPHER - Authentication failed (for AEAD modes)" );
  18315. if( use_ret == -(MBEDTLS_ERR_CIPHER_INVALID_CONTEXT) )
  18316. mbedtls_snprintf( buf, buflen, "CIPHER - The context is invalid. For example, because it was freed" );
  18317. if( use_ret == -(MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED) )
  18318. mbedtls_snprintf( buf, buflen, "CIPHER - Cipher hardware accelerator failed" );
  18319. #endif /* MBEDTLS_CIPHER_C */
  18320. #if defined(MBEDTLS_DHM_C)
  18321. if( use_ret == -(MBEDTLS_ERR_DHM_BAD_INPUT_DATA) )
  18322. mbedtls_snprintf( buf, buflen, "DHM - Bad input parameters" );
  18323. if( use_ret == -(MBEDTLS_ERR_DHM_READ_PARAMS_FAILED) )
  18324. mbedtls_snprintf( buf, buflen, "DHM - Reading of the DHM parameters failed" );
  18325. if( use_ret == -(MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED) )
  18326. mbedtls_snprintf( buf, buflen, "DHM - Making of the DHM parameters failed" );
  18327. if( use_ret == -(MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED) )
  18328. mbedtls_snprintf( buf, buflen, "DHM - Reading of the public values failed" );
  18329. if( use_ret == -(MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED) )
  18330. mbedtls_snprintf( buf, buflen, "DHM - Making of the public value failed" );
  18331. if( use_ret == -(MBEDTLS_ERR_DHM_CALC_SECRET_FAILED) )
  18332. mbedtls_snprintf( buf, buflen, "DHM - Calculation of the DHM secret failed" );
  18333. if( use_ret == -(MBEDTLS_ERR_DHM_INVALID_FORMAT) )
  18334. mbedtls_snprintf( buf, buflen, "DHM - The ASN.1 data is not formatted correctly" );
  18335. if( use_ret == -(MBEDTLS_ERR_DHM_ALLOC_FAILED) )
  18336. mbedtls_snprintf( buf, buflen, "DHM - Allocation of memory failed" );
  18337. if( use_ret == -(MBEDTLS_ERR_DHM_FILE_IO_ERROR) )
  18338. mbedtls_snprintf( buf, buflen, "DHM - Read or write of file failed" );
  18339. if( use_ret == -(MBEDTLS_ERR_DHM_HW_ACCEL_FAILED) )
  18340. mbedtls_snprintf( buf, buflen, "DHM - DHM hardware accelerator failed" );
  18341. if( use_ret == -(MBEDTLS_ERR_DHM_SET_GROUP_FAILED) )
  18342. mbedtls_snprintf( buf, buflen, "DHM - Setting the modulus and generator failed" );
  18343. #endif /* MBEDTLS_DHM_C */
  18344. #if defined(MBEDTLS_ECP_C)
  18345. if( use_ret == -(MBEDTLS_ERR_ECP_BAD_INPUT_DATA) )
  18346. mbedtls_snprintf( buf, buflen, "ECP - Bad input parameters to function" );
  18347. if( use_ret == -(MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL) )
  18348. mbedtls_snprintf( buf, buflen, "ECP - The buffer is too small to write to" );
  18349. if( use_ret == -(MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) )
  18350. mbedtls_snprintf( buf, buflen, "ECP - Requested curve not available" );
  18351. if( use_ret == -(MBEDTLS_ERR_ECP_VERIFY_FAILED) )
  18352. mbedtls_snprintf( buf, buflen, "ECP - The signature is not valid" );
  18353. if( use_ret == -(MBEDTLS_ERR_ECP_ALLOC_FAILED) )
  18354. mbedtls_snprintf( buf, buflen, "ECP - Memory allocation failed" );
  18355. if( use_ret == -(MBEDTLS_ERR_ECP_RANDOM_FAILED) )
  18356. mbedtls_snprintf( buf, buflen, "ECP - Generation of random value, such as (ephemeral) key, failed" );
  18357. if( use_ret == -(MBEDTLS_ERR_ECP_INVALID_KEY) )
  18358. mbedtls_snprintf( buf, buflen, "ECP - Invalid private or public key" );
  18359. if( use_ret == -(MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) )
  18360. mbedtls_snprintf( buf, buflen, "ECP - Signature is valid but shorter than the user-supplied length" );
  18361. if( use_ret == -(MBEDTLS_ERR_ECP_HW_ACCEL_FAILED) )
  18362. mbedtls_snprintf( buf, buflen, "ECP - ECP hardware accelerator failed" );
  18363. #endif /* MBEDTLS_ECP_C */
  18364. #if defined(MBEDTLS_MD_C)
  18365. if( use_ret == -(MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE) )
  18366. mbedtls_snprintf( buf, buflen, "MD - The selected feature is not available" );
  18367. if( use_ret == -(MBEDTLS_ERR_MD_BAD_INPUT_DATA) )
  18368. mbedtls_snprintf( buf, buflen, "MD - Bad input parameters to function" );
  18369. if( use_ret == -(MBEDTLS_ERR_MD_ALLOC_FAILED) )
  18370. mbedtls_snprintf( buf, buflen, "MD - Failed to allocate memory" );
  18371. if( use_ret == -(MBEDTLS_ERR_MD_FILE_IO_ERROR) )
  18372. mbedtls_snprintf( buf, buflen, "MD - Opening or reading of file failed" );
  18373. if( use_ret == -(MBEDTLS_ERR_MD_HW_ACCEL_FAILED) )
  18374. mbedtls_snprintf( buf, buflen, "MD - MD hardware accelerator failed" );
  18375. #endif /* MBEDTLS_MD_C */
  18376. #if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
  18377. if( use_ret == -(MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) )
  18378. mbedtls_snprintf( buf, buflen, "PEM - No PEM header or footer found" );
  18379. if( use_ret == -(MBEDTLS_ERR_PEM_INVALID_DATA) )
  18380. mbedtls_snprintf( buf, buflen, "PEM - PEM string is not as expected" );
  18381. if( use_ret == -(MBEDTLS_ERR_PEM_ALLOC_FAILED) )
  18382. mbedtls_snprintf( buf, buflen, "PEM - Failed to allocate memory" );
  18383. if( use_ret == -(MBEDTLS_ERR_PEM_INVALID_ENC_IV) )
  18384. mbedtls_snprintf( buf, buflen, "PEM - RSA IV is not in hex-format" );
  18385. if( use_ret == -(MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG) )
  18386. mbedtls_snprintf( buf, buflen, "PEM - Unsupported key encryption algorithm" );
  18387. if( use_ret == -(MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) )
  18388. mbedtls_snprintf( buf, buflen, "PEM - Private key password can't be empty" );
  18389. if( use_ret == -(MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) )
  18390. mbedtls_snprintf( buf, buflen, "PEM - Given private key password does not allow for correct decryption" );
  18391. if( use_ret == -(MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE) )
  18392. mbedtls_snprintf( buf, buflen, "PEM - Unavailable feature, e.g. hashing/encryption combination" );
  18393. if( use_ret == -(MBEDTLS_ERR_PEM_BAD_INPUT_DATA) )
  18394. mbedtls_snprintf( buf, buflen, "PEM - Bad input parameters to function" );
  18395. #endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */
  18396. #if defined(MBEDTLS_PK_C)
  18397. if( use_ret == -(MBEDTLS_ERR_PK_ALLOC_FAILED) )
  18398. mbedtls_snprintf( buf, buflen, "PK - Memory allocation failed" );
  18399. if( use_ret == -(MBEDTLS_ERR_PK_TYPE_MISMATCH) )
  18400. mbedtls_snprintf( buf, buflen, "PK - Type mismatch, eg attempt to encrypt with an ECDSA key" );
  18401. if( use_ret == -(MBEDTLS_ERR_PK_BAD_INPUT_DATA) )
  18402. mbedtls_snprintf( buf, buflen, "PK - Bad input parameters to function" );
  18403. if( use_ret == -(MBEDTLS_ERR_PK_FILE_IO_ERROR) )
  18404. mbedtls_snprintf( buf, buflen, "PK - Read/write of file failed" );
  18405. if( use_ret == -(MBEDTLS_ERR_PK_KEY_INVALID_VERSION) )
  18406. mbedtls_snprintf( buf, buflen, "PK - Unsupported key version" );
  18407. if( use_ret == -(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT) )
  18408. mbedtls_snprintf( buf, buflen, "PK - Invalid key tag or value" );
  18409. if( use_ret == -(MBEDTLS_ERR_PK_UNKNOWN_PK_ALG) )
  18410. mbedtls_snprintf( buf, buflen, "PK - Key algorithm is unsupported (only RSA and EC are supported)" );
  18411. if( use_ret == -(MBEDTLS_ERR_PK_PASSWORD_REQUIRED) )
  18412. mbedtls_snprintf( buf, buflen, "PK - Private key password can't be empty" );
  18413. if( use_ret == -(MBEDTLS_ERR_PK_PASSWORD_MISMATCH) )
  18414. mbedtls_snprintf( buf, buflen, "PK - Given private key password does not allow for correct decryption" );
  18415. if( use_ret == -(MBEDTLS_ERR_PK_INVALID_PUBKEY) )
  18416. mbedtls_snprintf( buf, buflen, "PK - The pubkey tag or value is invalid (only RSA and EC are supported)" );
  18417. if( use_ret == -(MBEDTLS_ERR_PK_INVALID_ALG) )
  18418. mbedtls_snprintf( buf, buflen, "PK - The algorithm tag or value is invalid" );
  18419. if( use_ret == -(MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE) )
  18420. mbedtls_snprintf( buf, buflen, "PK - Elliptic curve is unsupported (only NIST curves are supported)" );
  18421. if( use_ret == -(MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE) )
  18422. mbedtls_snprintf( buf, buflen, "PK - Unavailable feature, e.g. RSA disabled for RSA key" );
  18423. if( use_ret == -(MBEDTLS_ERR_PK_SIG_LEN_MISMATCH) )
  18424. mbedtls_snprintf( buf, buflen, "PK - The signature is valid but its length is less than expected" );
  18425. if( use_ret == -(MBEDTLS_ERR_PK_HW_ACCEL_FAILED) )
  18426. mbedtls_snprintf( buf, buflen, "PK - PK hardware accelerator failed" );
  18427. #endif /* MBEDTLS_PK_C */
  18428. #if defined(MBEDTLS_PKCS12_C)
  18429. if( use_ret == -(MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA) )
  18430. mbedtls_snprintf( buf, buflen, "PKCS12 - Bad input parameters to function" );
  18431. if( use_ret == -(MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE) )
  18432. mbedtls_snprintf( buf, buflen, "PKCS12 - Feature not available, e.g. unsupported encryption scheme" );
  18433. if( use_ret == -(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT) )
  18434. mbedtls_snprintf( buf, buflen, "PKCS12 - PBE ASN.1 data not as expected" );
  18435. if( use_ret == -(MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) )
  18436. mbedtls_snprintf( buf, buflen, "PKCS12 - Given private key password does not allow for correct decryption" );
  18437. #endif /* MBEDTLS_PKCS12_C */
  18438. #if defined(MBEDTLS_PKCS5_C)
  18439. if( use_ret == -(MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA) )
  18440. mbedtls_snprintf( buf, buflen, "PKCS5 - Bad input parameters to function" );
  18441. if( use_ret == -(MBEDTLS_ERR_PKCS5_INVALID_FORMAT) )
  18442. mbedtls_snprintf( buf, buflen, "PKCS5 - Unexpected ASN.1 data" );
  18443. if( use_ret == -(MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE) )
  18444. mbedtls_snprintf( buf, buflen, "PKCS5 - Requested encryption or digest alg not available" );
  18445. if( use_ret == -(MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) )
  18446. mbedtls_snprintf( buf, buflen, "PKCS5 - Given private key password does not allow for correct decryption" );
  18447. #endif /* MBEDTLS_PKCS5_C */
  18448. #if defined(MBEDTLS_RSA_C)
  18449. if( use_ret == -(MBEDTLS_ERR_RSA_BAD_INPUT_DATA) )
  18450. mbedtls_snprintf( buf, buflen, "RSA - Bad input parameters to function" );
  18451. if( use_ret == -(MBEDTLS_ERR_RSA_INVALID_PADDING) )
  18452. mbedtls_snprintf( buf, buflen, "RSA - Input data contains invalid padding and is rejected" );
  18453. if( use_ret == -(MBEDTLS_ERR_RSA_KEY_GEN_FAILED) )
  18454. mbedtls_snprintf( buf, buflen, "RSA - Something failed during generation of a key" );
  18455. if( use_ret == -(MBEDTLS_ERR_RSA_KEY_CHECK_FAILED) )
  18456. mbedtls_snprintf( buf, buflen, "RSA - Key failed to pass the validity check of the library" );
  18457. if( use_ret == -(MBEDTLS_ERR_RSA_PUBLIC_FAILED) )
  18458. mbedtls_snprintf( buf, buflen, "RSA - The public key operation failed" );
  18459. if( use_ret == -(MBEDTLS_ERR_RSA_PRIVATE_FAILED) )
  18460. mbedtls_snprintf( buf, buflen, "RSA - The private key operation failed" );
  18461. if( use_ret == -(MBEDTLS_ERR_RSA_VERIFY_FAILED) )
  18462. mbedtls_snprintf( buf, buflen, "RSA - The PKCS#1 verification failed" );
  18463. if( use_ret == -(MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE) )
  18464. mbedtls_snprintf( buf, buflen, "RSA - The output buffer for decryption is not large enough" );
  18465. if( use_ret == -(MBEDTLS_ERR_RSA_RNG_FAILED) )
  18466. mbedtls_snprintf( buf, buflen, "RSA - The random generator failed to generate non-zeros" );
  18467. if( use_ret == -(MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION) )
  18468. mbedtls_snprintf( buf, buflen, "RSA - The implementation does not offer the requested operation, for example, because of security violations or lack of functionality" );
  18469. if( use_ret == -(MBEDTLS_ERR_RSA_HW_ACCEL_FAILED) )
  18470. mbedtls_snprintf( buf, buflen, "RSA - RSA hardware accelerator failed" );
  18471. #endif /* MBEDTLS_RSA_C */
  18472. #if defined(MBEDTLS_SSL_TLS_C)
  18473. if( use_ret == -(MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE) )
  18474. mbedtls_snprintf( buf, buflen, "SSL - The requested feature is not available" );
  18475. if( use_ret == -(MBEDTLS_ERR_SSL_BAD_INPUT_DATA) )
  18476. mbedtls_snprintf( buf, buflen, "SSL - Bad input parameters to function" );
  18477. if( use_ret == -(MBEDTLS_ERR_SSL_INVALID_MAC) )
  18478. mbedtls_snprintf( buf, buflen, "SSL - Verification of the message MAC failed" );
  18479. if( use_ret == -(MBEDTLS_ERR_SSL_INVALID_RECORD) )
  18480. mbedtls_snprintf( buf, buflen, "SSL - An invalid SSL record was received" );
  18481. if( use_ret == -(MBEDTLS_ERR_SSL_CONN_EOF) )
  18482. mbedtls_snprintf( buf, buflen, "SSL - The connection indicated an EOF" );
  18483. if( use_ret == -(MBEDTLS_ERR_SSL_UNKNOWN_CIPHER) )
  18484. mbedtls_snprintf( buf, buflen, "SSL - An unknown cipher was received" );
  18485. if( use_ret == -(MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN) )
  18486. mbedtls_snprintf( buf, buflen, "SSL - The server has no ciphersuites in common with the client" );
  18487. if( use_ret == -(MBEDTLS_ERR_SSL_NO_RNG) )
  18488. mbedtls_snprintf( buf, buflen, "SSL - No RNG was provided to the SSL module" );
  18489. if( use_ret == -(MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE) )
  18490. mbedtls_snprintf( buf, buflen, "SSL - No client certification received from the client, but required by the authentication mode" );
  18491. if( use_ret == -(MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE) )
  18492. mbedtls_snprintf( buf, buflen, "SSL - Our own certificate(s) is/are too large to send in an SSL message" );
  18493. if( use_ret == -(MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED) )
  18494. mbedtls_snprintf( buf, buflen, "SSL - The own certificate is not set, but needed by the server" );
  18495. if( use_ret == -(MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) )
  18496. mbedtls_snprintf( buf, buflen, "SSL - The own private key or pre-shared key is not set, but needed" );
  18497. if( use_ret == -(MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED) )
  18498. mbedtls_snprintf( buf, buflen, "SSL - No CA Chain is set, but required to operate" );
  18499. if( use_ret == -(MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE) )
  18500. mbedtls_snprintf( buf, buflen, "SSL - An unexpected message was received from our peer" );
  18501. if( use_ret == -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE) )
  18502. {
  18503. mbedtls_snprintf( buf, buflen, "SSL - A fatal alert message was received from our peer" );
  18504. return;
  18505. }
  18506. if( use_ret == -(MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED) )
  18507. mbedtls_snprintf( buf, buflen, "SSL - Verification of our peer failed" );
  18508. if( use_ret == -(MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) )
  18509. mbedtls_snprintf( buf, buflen, "SSL - The peer notified us that the connection is going to be closed" );
  18510. if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO) )
  18511. mbedtls_snprintf( buf, buflen, "SSL - Processing of the ClientHello handshake message failed" );
  18512. if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO) )
  18513. mbedtls_snprintf( buf, buflen, "SSL - Processing of the ServerHello handshake message failed" );
  18514. if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE) )
  18515. mbedtls_snprintf( buf, buflen, "SSL - Processing of the Certificate handshake message failed" );
  18516. if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST) )
  18517. mbedtls_snprintf( buf, buflen, "SSL - Processing of the CertificateRequest handshake message failed" );
  18518. if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE) )
  18519. mbedtls_snprintf( buf, buflen, "SSL - Processing of the ServerKeyExchange handshake message failed" );
  18520. if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE) )
  18521. mbedtls_snprintf( buf, buflen, "SSL - Processing of the ServerHelloDone handshake message failed" );
  18522. if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE) )
  18523. mbedtls_snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed" );
  18524. if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP) )
  18525. mbedtls_snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Read Public" );
  18526. if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS) )
  18527. mbedtls_snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Calculate Secret" );
  18528. if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY) )
  18529. mbedtls_snprintf( buf, buflen, "SSL - Processing of the CertificateVerify handshake message failed" );
  18530. if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC) )
  18531. mbedtls_snprintf( buf, buflen, "SSL - Processing of the ChangeCipherSpec handshake message failed" );
  18532. if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_FINISHED) )
  18533. mbedtls_snprintf( buf, buflen, "SSL - Processing of the Finished handshake message failed" );
  18534. if( use_ret == -(MBEDTLS_ERR_SSL_ALLOC_FAILED) )
  18535. mbedtls_snprintf( buf, buflen, "SSL - Memory allocation failed" );
  18536. if( use_ret == -(MBEDTLS_ERR_SSL_HW_ACCEL_FAILED) )
  18537. mbedtls_snprintf( buf, buflen, "SSL - Hardware acceleration function returned with error" );
  18538. if( use_ret == -(MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH) )
  18539. mbedtls_snprintf( buf, buflen, "SSL - Hardware acceleration function skipped / left alone data" );
  18540. if( use_ret == -(MBEDTLS_ERR_SSL_COMPRESSION_FAILED) )
  18541. mbedtls_snprintf( buf, buflen, "SSL - Processing of the compression / decompression failed" );
  18542. if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION) )
  18543. mbedtls_snprintf( buf, buflen, "SSL - Handshake protocol not within min/max boundaries" );
  18544. if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET) )
  18545. mbedtls_snprintf( buf, buflen, "SSL - Processing of the NewSessionTicket handshake message failed" );
  18546. if( use_ret == -(MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) )
  18547. mbedtls_snprintf( buf, buflen, "SSL - Session ticket has expired" );
  18548. if( use_ret == -(MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH) )
  18549. mbedtls_snprintf( buf, buflen, "SSL - Public key type mismatch (eg, asked for RSA key exchange and presented EC key)" );
  18550. if( use_ret == -(MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) )
  18551. mbedtls_snprintf( buf, buflen, "SSL - Unknown identity received (eg, PSK identity)" );
  18552. if( use_ret == -(MBEDTLS_ERR_SSL_INTERNAL_ERROR) )
  18553. mbedtls_snprintf( buf, buflen, "SSL - Internal error (eg, unexpected failure in lower-level module)" );
  18554. if( use_ret == -(MBEDTLS_ERR_SSL_COUNTER_WRAPPING) )
  18555. mbedtls_snprintf( buf, buflen, "SSL - A counter would wrap (eg, too many messages exchanged)" );
  18556. if( use_ret == -(MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO) )
  18557. mbedtls_snprintf( buf, buflen, "SSL - Unexpected message at ServerHello in renegotiation" );
  18558. if( use_ret == -(MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) )
  18559. mbedtls_snprintf( buf, buflen, "SSL - DTLS client must retry for hello verification" );
  18560. if( use_ret == -(MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) )
  18561. mbedtls_snprintf( buf, buflen, "SSL - A buffer is too small to receive or write a message" );
  18562. if( use_ret == -(MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE) )
  18563. mbedtls_snprintf( buf, buflen, "SSL - None of the common ciphersuites is usable (eg, no suitable certificate, see debug messages)" );
  18564. if( use_ret == -(MBEDTLS_ERR_SSL_WANT_READ) )
  18565. mbedtls_snprintf( buf, buflen, "SSL - Connection requires a read call" );
  18566. if( use_ret == -(MBEDTLS_ERR_SSL_WANT_WRITE) )
  18567. mbedtls_snprintf( buf, buflen, "SSL - Connection requires a write call" );
  18568. if( use_ret == -(MBEDTLS_ERR_SSL_TIMEOUT) )
  18569. mbedtls_snprintf( buf, buflen, "SSL - The operation timed out" );
  18570. if( use_ret == -(MBEDTLS_ERR_SSL_CLIENT_RECONNECT) )
  18571. mbedtls_snprintf( buf, buflen, "SSL - The client initiated a reconnect from the same port" );
  18572. if( use_ret == -(MBEDTLS_ERR_SSL_UNEXPECTED_RECORD) )
  18573. mbedtls_snprintf( buf, buflen, "SSL - Record header looks valid but is not expected" );
  18574. if( use_ret == -(MBEDTLS_ERR_SSL_NON_FATAL) )
  18575. mbedtls_snprintf( buf, buflen, "SSL - The alert message received indicates a non-fatal error" );
  18576. if( use_ret == -(MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH) )
  18577. mbedtls_snprintf( buf, buflen, "SSL - Couldn't set the hash for verifying CertificateVerify" );
  18578. #endif /* MBEDTLS_SSL_TLS_C */
  18579. #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C)
  18580. if( use_ret == -(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) )
  18581. mbedtls_snprintf( buf, buflen, "X509 - Unavailable feature, e.g. RSA hashing/encryption combination" );
  18582. if( use_ret == -(MBEDTLS_ERR_X509_UNKNOWN_OID) )
  18583. mbedtls_snprintf( buf, buflen, "X509 - Requested OID is unknown" );
  18584. if( use_ret == -(MBEDTLS_ERR_X509_INVALID_FORMAT) )
  18585. mbedtls_snprintf( buf, buflen, "X509 - The CRT/CRL/CSR format is invalid, e.g. different type expected" );
  18586. if( use_ret == -(MBEDTLS_ERR_X509_INVALID_VERSION) )
  18587. mbedtls_snprintf( buf, buflen, "X509 - The CRT/CRL/CSR version element is invalid" );
  18588. if( use_ret == -(MBEDTLS_ERR_X509_INVALID_SERIAL) )
  18589. mbedtls_snprintf( buf, buflen, "X509 - The serial tag or value is invalid" );
  18590. if( use_ret == -(MBEDTLS_ERR_X509_INVALID_ALG) )
  18591. mbedtls_snprintf( buf, buflen, "X509 - The algorithm tag or value is invalid" );
  18592. if( use_ret == -(MBEDTLS_ERR_X509_INVALID_NAME) )
  18593. mbedtls_snprintf( buf, buflen, "X509 - The name tag or value is invalid" );
  18594. if( use_ret == -(MBEDTLS_ERR_X509_INVALID_DATE) )
  18595. mbedtls_snprintf( buf, buflen, "X509 - The date tag or value is invalid" );
  18596. if( use_ret == -(MBEDTLS_ERR_X509_INVALID_SIGNATURE) )
  18597. mbedtls_snprintf( buf, buflen, "X509 - The signature tag or value invalid" );
  18598. if( use_ret == -(MBEDTLS_ERR_X509_INVALID_EXTENSIONS) )
  18599. mbedtls_snprintf( buf, buflen, "X509 - The extension tag or value is invalid" );
  18600. if( use_ret == -(MBEDTLS_ERR_X509_UNKNOWN_VERSION) )
  18601. mbedtls_snprintf( buf, buflen, "X509 - CRT/CRL/CSR has an unsupported version number" );
  18602. if( use_ret == -(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG) )
  18603. mbedtls_snprintf( buf, buflen, "X509 - Signature algorithm (oid) is unsupported" );
  18604. if( use_ret == -(MBEDTLS_ERR_X509_SIG_MISMATCH) )
  18605. mbedtls_snprintf( buf, buflen, "X509 - Signature algorithms do not match. (see \\c ::mbedtls_x509_crt sig_oid)" );
  18606. if( use_ret == -(MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) )
  18607. mbedtls_snprintf( buf, buflen, "X509 - Certificate verification failed, e.g. CRL, CA or signature check failed" );
  18608. if( use_ret == -(MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT) )
  18609. mbedtls_snprintf( buf, buflen, "X509 - Format not recognized as DER or PEM" );
  18610. if( use_ret == -(MBEDTLS_ERR_X509_BAD_INPUT_DATA) )
  18611. mbedtls_snprintf( buf, buflen, "X509 - Input invalid" );
  18612. if( use_ret == -(MBEDTLS_ERR_X509_ALLOC_FAILED) )
  18613. mbedtls_snprintf( buf, buflen, "X509 - Allocation of memory failed" );
  18614. if( use_ret == -(MBEDTLS_ERR_X509_FILE_IO_ERROR) )
  18615. mbedtls_snprintf( buf, buflen, "X509 - Read/write of file failed" );
  18616. if( use_ret == -(MBEDTLS_ERR_X509_BUFFER_TOO_SMALL) )
  18617. mbedtls_snprintf( buf, buflen, "X509 - Destination buffer is too small" );
  18618. if( use_ret == -(MBEDTLS_ERR_X509_FATAL_ERROR) )
  18619. mbedtls_snprintf( buf, buflen, "X509 - A fatal error occured, eg the chain is too long or the vrfy callback failed" );
  18620. #endif /* MBEDTLS_X509_USE_C || MBEDTLS_X509_CREATE_C */
  18621. // END generated code
  18622. if( strlen( buf ) == 0 )
  18623. mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
  18624. }
  18625. use_ret = ret & ~0xFF80;
  18626. if( use_ret == 0 )
  18627. return;
  18628. // If high level code is present, make a concatenation between both
  18629. // error strings.
  18630. //
  18631. len = strlen( buf );
  18632. if( len > 0 )
  18633. {
  18634. if( buflen - len < 5 )
  18635. return;
  18636. mbedtls_snprintf( buf + len, buflen - len, " : " );
  18637. buf += len + 3;
  18638. buflen -= len + 3;
  18639. }
  18640. // Low level error codes
  18641. //
  18642. // BEGIN generated code
  18643. #if defined(MBEDTLS_AES_C)
  18644. if( use_ret == -(MBEDTLS_ERR_AES_INVALID_KEY_LENGTH) )
  18645. mbedtls_snprintf( buf, buflen, "AES - Invalid key length" );
  18646. if( use_ret == -(MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH) )
  18647. mbedtls_snprintf( buf, buflen, "AES - Invalid data input length" );
  18648. if( use_ret == -(MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE) )
  18649. mbedtls_snprintf( buf, buflen, "AES - Feature not available. For example, an unsupported AES key size" );
  18650. if( use_ret == -(MBEDTLS_ERR_AES_HW_ACCEL_FAILED) )
  18651. mbedtls_snprintf( buf, buflen, "AES - AES hardware accelerator failed" );
  18652. #endif /* MBEDTLS_AES_C */
  18653. #if defined(MBEDTLS_ARC4_C)
  18654. if( use_ret == -(MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED) )
  18655. mbedtls_snprintf( buf, buflen, "ARC4 - ARC4 hardware accelerator failed" );
  18656. #endif /* MBEDTLS_ARC4_C */
  18657. #if defined(MBEDTLS_ASN1_PARSE_C)
  18658. if( use_ret == -(MBEDTLS_ERR_ASN1_OUT_OF_DATA) )
  18659. mbedtls_snprintf( buf, buflen, "ASN1 - Out of data when parsing an ASN1 data structure" );
  18660. if( use_ret == -(MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) )
  18661. mbedtls_snprintf( buf, buflen, "ASN1 - ASN1 tag was of an unexpected value" );
  18662. if( use_ret == -(MBEDTLS_ERR_ASN1_INVALID_LENGTH) )
  18663. mbedtls_snprintf( buf, buflen, "ASN1 - Error when trying to determine the length or invalid length" );
  18664. if( use_ret == -(MBEDTLS_ERR_ASN1_LENGTH_MISMATCH) )
  18665. mbedtls_snprintf( buf, buflen, "ASN1 - Actual length differs from expected length" );
  18666. if( use_ret == -(MBEDTLS_ERR_ASN1_INVALID_DATA) )
  18667. mbedtls_snprintf( buf, buflen, "ASN1 - Data is invalid. (not used)" );
  18668. if( use_ret == -(MBEDTLS_ERR_ASN1_ALLOC_FAILED) )
  18669. mbedtls_snprintf( buf, buflen, "ASN1 - Memory allocation failed" );
  18670. if( use_ret == -(MBEDTLS_ERR_ASN1_BUF_TOO_SMALL) )
  18671. mbedtls_snprintf( buf, buflen, "ASN1 - Buffer too small when writing ASN.1 data structure" );
  18672. #endif /* MBEDTLS_ASN1_PARSE_C */
  18673. #if defined(MBEDTLS_BASE64_C)
  18674. if( use_ret == -(MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL) )
  18675. mbedtls_snprintf( buf, buflen, "BASE64 - Output buffer too small" );
  18676. if( use_ret == -(MBEDTLS_ERR_BASE64_INVALID_CHARACTER) )
  18677. mbedtls_snprintf( buf, buflen, "BASE64 - Invalid character in input" );
  18678. #endif /* MBEDTLS_BASE64_C */
  18679. #if defined(MBEDTLS_BIGNUM_C)
  18680. if( use_ret == -(MBEDTLS_ERR_MPI_FILE_IO_ERROR) )
  18681. mbedtls_snprintf( buf, buflen, "BIGNUM - An error occurred while reading from or writing to a file" );
  18682. if( use_ret == -(MBEDTLS_ERR_MPI_BAD_INPUT_DATA) )
  18683. mbedtls_snprintf( buf, buflen, "BIGNUM - Bad input parameters to function" );
  18684. if( use_ret == -(MBEDTLS_ERR_MPI_INVALID_CHARACTER) )
  18685. mbedtls_snprintf( buf, buflen, "BIGNUM - There is an invalid character in the digit string" );
  18686. if( use_ret == -(MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL) )
  18687. mbedtls_snprintf( buf, buflen, "BIGNUM - The buffer is too small to write to" );
  18688. if( use_ret == -(MBEDTLS_ERR_MPI_NEGATIVE_VALUE) )
  18689. mbedtls_snprintf( buf, buflen, "BIGNUM - The input arguments are negative or result in illegal output" );
  18690. if( use_ret == -(MBEDTLS_ERR_MPI_DIVISION_BY_ZERO) )
  18691. mbedtls_snprintf( buf, buflen, "BIGNUM - The input argument for division is zero, which is not allowed" );
  18692. if( use_ret == -(MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) )
  18693. mbedtls_snprintf( buf, buflen, "BIGNUM - The input arguments are not acceptable" );
  18694. if( use_ret == -(MBEDTLS_ERR_MPI_ALLOC_FAILED) )
  18695. mbedtls_snprintf( buf, buflen, "BIGNUM - Memory allocation failed" );
  18696. #endif /* MBEDTLS_BIGNUM_C */
  18697. #if defined(MBEDTLS_BLOWFISH_C)
  18698. if( use_ret == -(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) )
  18699. mbedtls_snprintf( buf, buflen, "BLOWFISH - Invalid key length" );
  18700. if( use_ret == -(MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED) )
  18701. mbedtls_snprintf( buf, buflen, "BLOWFISH - Blowfish hardware accelerator failed" );
  18702. if( use_ret == -(MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH) )
  18703. mbedtls_snprintf( buf, buflen, "BLOWFISH - Invalid data input length" );
  18704. #endif /* MBEDTLS_BLOWFISH_C */
  18705. #if defined(MBEDTLS_CAMELLIA_C)
  18706. if( use_ret == -(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) )
  18707. mbedtls_snprintf( buf, buflen, "CAMELLIA - Invalid key length" );
  18708. if( use_ret == -(MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH) )
  18709. mbedtls_snprintf( buf, buflen, "CAMELLIA - Invalid data input length" );
  18710. if( use_ret == -(MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED) )
  18711. mbedtls_snprintf( buf, buflen, "CAMELLIA - Camellia hardware accelerator failed" );
  18712. #endif /* MBEDTLS_CAMELLIA_C */
  18713. #if defined(MBEDTLS_CCM_C)
  18714. if( use_ret == -(MBEDTLS_ERR_CCM_BAD_INPUT) )
  18715. mbedtls_snprintf( buf, buflen, "CCM - Bad input parameters to the function" );
  18716. if( use_ret == -(MBEDTLS_ERR_CCM_AUTH_FAILED) )
  18717. mbedtls_snprintf( buf, buflen, "CCM - Authenticated decryption failed" );
  18718. if( use_ret == -(MBEDTLS_ERR_CCM_HW_ACCEL_FAILED) )
  18719. mbedtls_snprintf( buf, buflen, "CCM - CCM hardware accelerator failed" );
  18720. #endif /* MBEDTLS_CCM_C */
  18721. #if defined(MBEDTLS_CMAC_C)
  18722. if( use_ret == -(MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED) )
  18723. mbedtls_snprintf( buf, buflen, "CMAC - CMAC hardware accelerator failed" );
  18724. #endif /* MBEDTLS_CMAC_C */
  18725. #if defined(MBEDTLS_CTR_DRBG_C)
  18726. if( use_ret == -(MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED) )
  18727. mbedtls_snprintf( buf, buflen, "CTR_DRBG - The entropy source failed" );
  18728. if( use_ret == -(MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG) )
  18729. mbedtls_snprintf( buf, buflen, "CTR_DRBG - The requested random buffer length is too big" );
  18730. if( use_ret == -(MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG) )
  18731. mbedtls_snprintf( buf, buflen, "CTR_DRBG - The input (entropy + additional data) is too large" );
  18732. if( use_ret == -(MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR) )
  18733. mbedtls_snprintf( buf, buflen, "CTR_DRBG - Read or write error in file" );
  18734. #endif /* MBEDTLS_CTR_DRBG_C */
  18735. #if defined(MBEDTLS_DES_C)
  18736. if( use_ret == -(MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH) )
  18737. mbedtls_snprintf( buf, buflen, "DES - The data input has an invalid length" );
  18738. if( use_ret == -(MBEDTLS_ERR_DES_HW_ACCEL_FAILED) )
  18739. mbedtls_snprintf( buf, buflen, "DES - DES hardware accelerator failed" );
  18740. #endif /* MBEDTLS_DES_C */
  18741. #if defined(MBEDTLS_ENTROPY_C)
  18742. if( use_ret == -(MBEDTLS_ERR_ENTROPY_SOURCE_FAILED) )
  18743. mbedtls_snprintf( buf, buflen, "ENTROPY - Critical entropy source failure" );
  18744. if( use_ret == -(MBEDTLS_ERR_ENTROPY_MAX_SOURCES) )
  18745. mbedtls_snprintf( buf, buflen, "ENTROPY - No more sources can be added" );
  18746. if( use_ret == -(MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED) )
  18747. mbedtls_snprintf( buf, buflen, "ENTROPY - No sources have been added to poll" );
  18748. if( use_ret == -(MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE) )
  18749. mbedtls_snprintf( buf, buflen, "ENTROPY - No strong sources have been added to poll" );
  18750. if( use_ret == -(MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR) )
  18751. mbedtls_snprintf( buf, buflen, "ENTROPY - Read/write error in file" );
  18752. #endif /* MBEDTLS_ENTROPY_C */
  18753. #if defined(MBEDTLS_GCM_C)
  18754. if( use_ret == -(MBEDTLS_ERR_GCM_AUTH_FAILED) )
  18755. mbedtls_snprintf( buf, buflen, "GCM - Authenticated decryption failed" );
  18756. if( use_ret == -(MBEDTLS_ERR_GCM_HW_ACCEL_FAILED) )
  18757. mbedtls_snprintf( buf, buflen, "GCM - GCM hardware accelerator failed" );
  18758. if( use_ret == -(MBEDTLS_ERR_GCM_BAD_INPUT) )
  18759. mbedtls_snprintf( buf, buflen, "GCM - Bad input parameters to function" );
  18760. #endif /* MBEDTLS_GCM_C */
  18761. #if defined(MBEDTLS_HMAC_DRBG_C)
  18762. if( use_ret == -(MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG) )
  18763. mbedtls_snprintf( buf, buflen, "HMAC_DRBG - Too many random requested in single call" );
  18764. if( use_ret == -(MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG) )
  18765. mbedtls_snprintf( buf, buflen, "HMAC_DRBG - Input too large (Entropy + additional)" );
  18766. if( use_ret == -(MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR) )
  18767. mbedtls_snprintf( buf, buflen, "HMAC_DRBG - Read/write error in file" );
  18768. if( use_ret == -(MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED) )
  18769. mbedtls_snprintf( buf, buflen, "HMAC_DRBG - The entropy source failed" );
  18770. #endif /* MBEDTLS_HMAC_DRBG_C */
  18771. #if defined(MBEDTLS_MD2_C)
  18772. if( use_ret == -(MBEDTLS_ERR_MD2_HW_ACCEL_FAILED) )
  18773. mbedtls_snprintf( buf, buflen, "MD2 - MD2 hardware accelerator failed" );
  18774. #endif /* MBEDTLS_MD2_C */
  18775. #if defined(MBEDTLS_MD4_C)
  18776. if( use_ret == -(MBEDTLS_ERR_MD4_HW_ACCEL_FAILED) )
  18777. mbedtls_snprintf( buf, buflen, "MD4 - MD4 hardware accelerator failed" );
  18778. #endif /* MBEDTLS_MD4_C */
  18779. #if defined(MBEDTLS_MD5_C)
  18780. if( use_ret == -(MBEDTLS_ERR_MD5_HW_ACCEL_FAILED) )
  18781. mbedtls_snprintf( buf, buflen, "MD5 - MD5 hardware accelerator failed" );
  18782. #endif /* MBEDTLS_MD5_C */
  18783. #if defined(MBEDTLS_NET_C)
  18784. if( use_ret == -(MBEDTLS_ERR_NET_SOCKET_FAILED) )
  18785. mbedtls_snprintf( buf, buflen, "NET - Failed to open a socket" );
  18786. if( use_ret == -(MBEDTLS_ERR_NET_CONNECT_FAILED) )
  18787. mbedtls_snprintf( buf, buflen, "NET - The connection to the given server / port failed" );
  18788. if( use_ret == -(MBEDTLS_ERR_NET_BIND_FAILED) )
  18789. mbedtls_snprintf( buf, buflen, "NET - Binding of the socket failed" );
  18790. if( use_ret == -(MBEDTLS_ERR_NET_LISTEN_FAILED) )
  18791. mbedtls_snprintf( buf, buflen, "NET - Could not listen on the socket" );
  18792. if( use_ret == -(MBEDTLS_ERR_NET_ACCEPT_FAILED) )
  18793. mbedtls_snprintf( buf, buflen, "NET - Could not accept the incoming connection" );
  18794. if( use_ret == -(MBEDTLS_ERR_NET_RECV_FAILED) )
  18795. mbedtls_snprintf( buf, buflen, "NET - Reading information from the socket failed" );
  18796. if( use_ret == -(MBEDTLS_ERR_NET_SEND_FAILED) )
  18797. mbedtls_snprintf( buf, buflen, "NET - Sending information through the socket failed" );
  18798. if( use_ret == -(MBEDTLS_ERR_NET_CONN_RESET) )
  18799. mbedtls_snprintf( buf, buflen, "NET - Connection was reset by peer" );
  18800. if( use_ret == -(MBEDTLS_ERR_NET_UNKNOWN_HOST) )
  18801. mbedtls_snprintf( buf, buflen, "NET - Failed to get an IP address for the given hostname" );
  18802. if( use_ret == -(MBEDTLS_ERR_NET_BUFFER_TOO_SMALL) )
  18803. mbedtls_snprintf( buf, buflen, "NET - Buffer is too small to hold the data" );
  18804. if( use_ret == -(MBEDTLS_ERR_NET_INVALID_CONTEXT) )
  18805. mbedtls_snprintf( buf, buflen, "NET - The context is invalid, eg because it was free()ed" );
  18806. #endif /* MBEDTLS_NET_C */
  18807. #if defined(MBEDTLS_OID_C)
  18808. if( use_ret == -(MBEDTLS_ERR_OID_NOT_FOUND) )
  18809. mbedtls_snprintf( buf, buflen, "OID - OID is not found" );
  18810. if( use_ret == -(MBEDTLS_ERR_OID_BUF_TOO_SMALL) )
  18811. mbedtls_snprintf( buf, buflen, "OID - output buffer is too small" );
  18812. #endif /* MBEDTLS_OID_C */
  18813. #if defined(MBEDTLS_PADLOCK_C)
  18814. if( use_ret == -(MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED) )
  18815. mbedtls_snprintf( buf, buflen, "PADLOCK - Input data should be aligned" );
  18816. #endif /* MBEDTLS_PADLOCK_C */
  18817. #if defined(MBEDTLS_RIPEMD160_C)
  18818. if( use_ret == -(MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED) )
  18819. mbedtls_snprintf( buf, buflen, "RIPEMD160 - RIPEMD160 hardware accelerator failed" );
  18820. #endif /* MBEDTLS_RIPEMD160_C */
  18821. #if defined(MBEDTLS_SHA1_C)
  18822. if( use_ret == -(MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED) )
  18823. mbedtls_snprintf( buf, buflen, "SHA1 - SHA-1 hardware accelerator failed" );
  18824. #endif /* MBEDTLS_SHA1_C */
  18825. #if defined(MBEDTLS_SHA256_C)
  18826. if( use_ret == -(MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED) )
  18827. mbedtls_snprintf( buf, buflen, "SHA256 - SHA-256 hardware accelerator failed" );
  18828. #endif /* MBEDTLS_SHA256_C */
  18829. #if defined(MBEDTLS_SHA512_C)
  18830. if( use_ret == -(MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED) )
  18831. mbedtls_snprintf( buf, buflen, "SHA512 - SHA-512 hardware accelerator failed" );
  18832. #endif /* MBEDTLS_SHA512_C */
  18833. #if defined(MBEDTLS_THREADING_C)
  18834. if( use_ret == -(MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE) )
  18835. mbedtls_snprintf( buf, buflen, "THREADING - The selected feature is not available" );
  18836. if( use_ret == -(MBEDTLS_ERR_THREADING_BAD_INPUT_DATA) )
  18837. mbedtls_snprintf( buf, buflen, "THREADING - Bad input parameters to function" );
  18838. if( use_ret == -(MBEDTLS_ERR_THREADING_MUTEX_ERROR) )
  18839. mbedtls_snprintf( buf, buflen, "THREADING - Locking / unlocking / free failed with error code" );
  18840. #endif /* MBEDTLS_THREADING_C */
  18841. #if defined(MBEDTLS_XTEA_C)
  18842. if( use_ret == -(MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH) )
  18843. mbedtls_snprintf( buf, buflen, "XTEA - The data input has an invalid length" );
  18844. if( use_ret == -(MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED) )
  18845. mbedtls_snprintf( buf, buflen, "XTEA - XTEA hardware accelerator failed" );
  18846. #endif /* MBEDTLS_XTEA_C */
  18847. // END generated code
  18848. if( strlen( buf ) != 0 )
  18849. return;
  18850. mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
  18851. }
  18852. #else /* MBEDTLS_ERROR_C */
  18853. #if defined(MBEDTLS_ERROR_STRERROR_DUMMY)
  18854. /*
  18855. * Provide an non-function in case MBEDTLS_ERROR_C is not defined
  18856. */
  18857. void mbedtls_strerror( int ret, char *buf, size_t buflen )
  18858. {
  18859. ((void) ret);
  18860. if( buflen > 0 )
  18861. buf[0] = '\0';
  18862. }
  18863. #endif /* MBEDTLS_ERROR_STRERROR_DUMMY */
  18864. #endif /* MBEDTLS_ERROR_C */
  18865. /*
  18866. Amalgamated build undefines
  18867. */
  18868. #undef ADD
  18869. #undef BC
  18870. #undef BEFORE_COLON
  18871. #undef F
  18872. #undef F0
  18873. #undef F1
  18874. #undef F2
  18875. #undef F3
  18876. #undef F4
  18877. #undef F5
  18878. #undef FSb
  18879. #undef K
  18880. #undef KK
  18881. #undef P
  18882. #undef R
  18883. #undef ROTR
  18884. #undef S
  18885. #undef S0
  18886. #undef S1
  18887. #undef S2
  18888. #undef S3
  18889. #undef SAFE_SNPRINTF
  18890. #undef SHR
  18891. #undef close
  18892. #undef read
  18893. #undef supported_init
  18894. #undef write
  18895. /********* Start of file library/gcm.c ************/
  18896. /*
  18897. * NIST SP800-38D compliant GCM implementation
  18898. *
  18899. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  18900. * SPDX-License-Identifier: Apache-2.0
  18901. *
  18902. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  18903. * not use this file except in compliance with the License.
  18904. * You may obtain a copy of the License at
  18905. *
  18906. * http://www.apache.org/licenses/LICENSE-2.0
  18907. *
  18908. * Unless required by applicable law or agreed to in writing, software
  18909. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18910. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18911. * See the License for the specific language governing permissions and
  18912. * limitations under the License.
  18913. *
  18914. * This file is part of mbed TLS (https://tls.mbed.org)
  18915. */
  18916. /*
  18917. * http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
  18918. *
  18919. * See also:
  18920. * [MGV] http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf
  18921. *
  18922. * We use the algorithm described as Shoup's method with 4-bit tables in
  18923. * [MGV] 4.1, pp. 12-13, to enhance speed without using too much memory.
  18924. */
  18925. #if !defined(MBEDTLS_CONFIG_FILE)
  18926. #else
  18927. #endif
  18928. #if defined(MBEDTLS_GCM_C)
  18929. #include <string.h>
  18930. #if defined(MBEDTLS_AESNI_C)
  18931. #endif
  18932. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  18933. #if defined(MBEDTLS_PLATFORM_C)
  18934. #else
  18935. #include <stdio.h>
  18936. #define mbedtls_printf printf
  18937. #endif /* MBEDTLS_PLATFORM_C */
  18938. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  18939. #if !defined(MBEDTLS_GCM_ALT)
  18940. /*
  18941. * 32-bit integer manipulation macros (big endian)
  18942. */
  18943. #ifndef GET_UINT32_BE
  18944. #define GET_UINT32_BE(n,b,i) \
  18945. { \
  18946. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  18947. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  18948. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  18949. | ( (uint32_t) (b)[(i) + 3] ); \
  18950. }
  18951. #endif
  18952. #ifndef PUT_UINT32_BE
  18953. #define PUT_UINT32_BE(n,b,i) \
  18954. { \
  18955. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  18956. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  18957. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  18958. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  18959. }
  18960. #endif
  18961. /* Implementation that should never be optimized out by the compiler */
  18962. /* zeroize was here */
  18963. /*
  18964. * Initialize a context
  18965. */
  18966. void mbedtls_gcm_init( mbedtls_gcm_context *ctx )
  18967. {
  18968. memset( ctx, 0, sizeof( mbedtls_gcm_context ) );
  18969. }
  18970. /*
  18971. * Precompute small multiples of H, that is set
  18972. * HH[i] || HL[i] = H times i,
  18973. * where i is seen as a field element as in [MGV], ie high-order bits
  18974. * correspond to low powers of P. The result is stored in the same way, that
  18975. * is the high-order bit of HH corresponds to P^0 and the low-order bit of HL
  18976. * corresponds to P^127.
  18977. */
  18978. static int gcm_gen_table( mbedtls_gcm_context *ctx )
  18979. {
  18980. int ret, i, j;
  18981. uint64_t hi, lo;
  18982. uint64_t vl, vh;
  18983. unsigned char h[16];
  18984. size_t olen = 0;
  18985. memset( h, 0, 16 );
  18986. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, h, 16, h, &olen ) ) != 0 )
  18987. return( ret );
  18988. /* pack h as two 64-bits ints, big-endian */
  18989. GET_UINT32_BE( hi, h, 0 );
  18990. GET_UINT32_BE( lo, h, 4 );
  18991. vh = (uint64_t) hi << 32 | lo;
  18992. GET_UINT32_BE( hi, h, 8 );
  18993. GET_UINT32_BE( lo, h, 12 );
  18994. vl = (uint64_t) hi << 32 | lo;
  18995. /* 8 = 1000 corresponds to 1 in GF(2^128) */
  18996. ctx->HL[8] = vl;
  18997. ctx->HH[8] = vh;
  18998. #if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64)
  18999. /* With CLMUL support, we need only h, not the rest of the table */
  19000. if( mbedtls_aesni_has_support( MBEDTLS_AESNI_CLMUL ) )
  19001. return( 0 );
  19002. #endif
  19003. /* 0 corresponds to 0 in GF(2^128) */
  19004. ctx->HH[0] = 0;
  19005. ctx->HL[0] = 0;
  19006. for( i = 4; i > 0; i >>= 1 )
  19007. {
  19008. uint32_t T = ( vl & 1 ) * 0xe1000000U;
  19009. vl = ( vh << 63 ) | ( vl >> 1 );
  19010. vh = ( vh >> 1 ) ^ ( (uint64_t) T << 32);
  19011. ctx->HL[i] = vl;
  19012. ctx->HH[i] = vh;
  19013. }
  19014. for( i = 2; i <= 8; i *= 2 )
  19015. {
  19016. uint64_t *HiL = ctx->HL + i, *HiH = ctx->HH + i;
  19017. vh = *HiH;
  19018. vl = *HiL;
  19019. for( j = 1; j < i; j++ )
  19020. {
  19021. HiH[j] = vh ^ ctx->HH[j];
  19022. HiL[j] = vl ^ ctx->HL[j];
  19023. }
  19024. }
  19025. return( 0 );
  19026. }
  19027. int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx,
  19028. mbedtls_cipher_id_t cipher,
  19029. const unsigned char *key,
  19030. unsigned int keybits )
  19031. {
  19032. int ret;
  19033. const mbedtls_cipher_info_t *cipher_info;
  19034. cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, MBEDTLS_MODE_ECB );
  19035. if( cipher_info == NULL )
  19036. return( MBEDTLS_ERR_GCM_BAD_INPUT );
  19037. if( cipher_info->block_size != 16 )
  19038. return( MBEDTLS_ERR_GCM_BAD_INPUT );
  19039. mbedtls_cipher_free( &ctx->cipher_ctx );
  19040. if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 )
  19041. return( ret );
  19042. if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits,
  19043. MBEDTLS_ENCRYPT ) ) != 0 )
  19044. {
  19045. return( ret );
  19046. }
  19047. if( ( ret = gcm_gen_table( ctx ) ) != 0 )
  19048. return( ret );
  19049. return( 0 );
  19050. }
  19051. /*
  19052. * Shoup's method for multiplication use this table with
  19053. * last4[x] = x times P^128
  19054. * where x and last4[x] are seen as elements of GF(2^128) as in [MGV]
  19055. */
  19056. static const uint64_t last4[16] =
  19057. {
  19058. 0x0000, 0x1c20, 0x3840, 0x2460,
  19059. 0x7080, 0x6ca0, 0x48c0, 0x54e0,
  19060. 0xe100, 0xfd20, 0xd940, 0xc560,
  19061. 0x9180, 0x8da0, 0xa9c0, 0xb5e0
  19062. };
  19063. /*
  19064. * Sets output to x times H using the precomputed tables.
  19065. * x and output are seen as elements of GF(2^128) as in [MGV].
  19066. */
  19067. static void gcm_mult( mbedtls_gcm_context *ctx, const unsigned char x[16],
  19068. unsigned char output[16] )
  19069. {
  19070. int i = 0;
  19071. unsigned char lo, hi, rem;
  19072. uint64_t zh, zl;
  19073. #if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64)
  19074. if( mbedtls_aesni_has_support( MBEDTLS_AESNI_CLMUL ) ) {
  19075. unsigned char h[16];
  19076. PUT_UINT32_BE( ctx->HH[8] >> 32, h, 0 );
  19077. PUT_UINT32_BE( ctx->HH[8], h, 4 );
  19078. PUT_UINT32_BE( ctx->HL[8] >> 32, h, 8 );
  19079. PUT_UINT32_BE( ctx->HL[8], h, 12 );
  19080. mbedtls_aesni_gcm_mult( output, x, h );
  19081. return;
  19082. }
  19083. #endif /* MBEDTLS_AESNI_C && MBEDTLS_HAVE_X86_64 */
  19084. lo = x[15] & 0xf;
  19085. zh = ctx->HH[lo];
  19086. zl = ctx->HL[lo];
  19087. for( i = 15; i >= 0; i-- )
  19088. {
  19089. lo = x[i] & 0xf;
  19090. hi = x[i] >> 4;
  19091. if( i != 15 )
  19092. {
  19093. rem = (unsigned char) zl & 0xf;
  19094. zl = ( zh << 60 ) | ( zl >> 4 );
  19095. zh = ( zh >> 4 );
  19096. zh ^= (uint64_t) last4[rem] << 48;
  19097. zh ^= ctx->HH[lo];
  19098. zl ^= ctx->HL[lo];
  19099. }
  19100. rem = (unsigned char) zl & 0xf;
  19101. zl = ( zh << 60 ) | ( zl >> 4 );
  19102. zh = ( zh >> 4 );
  19103. zh ^= (uint64_t) last4[rem] << 48;
  19104. zh ^= ctx->HH[hi];
  19105. zl ^= ctx->HL[hi];
  19106. }
  19107. PUT_UINT32_BE( zh >> 32, output, 0 );
  19108. PUT_UINT32_BE( zh, output, 4 );
  19109. PUT_UINT32_BE( zl >> 32, output, 8 );
  19110. PUT_UINT32_BE( zl, output, 12 );
  19111. }
  19112. int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
  19113. int mode,
  19114. const unsigned char *iv,
  19115. size_t iv_len,
  19116. const unsigned char *add,
  19117. size_t add_len )
  19118. {
  19119. int ret;
  19120. unsigned char work_buf[16];
  19121. size_t i;
  19122. const unsigned char *p;
  19123. size_t use_len, olen = 0;
  19124. /* IV and AD are limited to 2^64 bits, so 2^61 bytes */
  19125. /* IV is not allowed to be zero length */
  19126. if( iv_len == 0 ||
  19127. ( (uint64_t) iv_len ) >> 61 != 0 ||
  19128. ( (uint64_t) add_len ) >> 61 != 0 )
  19129. {
  19130. return( MBEDTLS_ERR_GCM_BAD_INPUT );
  19131. }
  19132. memset( ctx->y, 0x00, sizeof(ctx->y) );
  19133. memset( ctx->buf, 0x00, sizeof(ctx->buf) );
  19134. ctx->mode = mode;
  19135. ctx->len = 0;
  19136. ctx->add_len = 0;
  19137. if( iv_len == 12 )
  19138. {
  19139. memcpy( ctx->y, iv, iv_len );
  19140. ctx->y[15] = 1;
  19141. }
  19142. else
  19143. {
  19144. memset( work_buf, 0x00, 16 );
  19145. PUT_UINT32_BE( iv_len * 8, work_buf, 12 );
  19146. p = iv;
  19147. while( iv_len > 0 )
  19148. {
  19149. use_len = ( iv_len < 16 ) ? iv_len : 16;
  19150. for( i = 0; i < use_len; i++ )
  19151. ctx->y[i] ^= p[i];
  19152. gcm_mult( ctx, ctx->y, ctx->y );
  19153. iv_len -= use_len;
  19154. p += use_len;
  19155. }
  19156. for( i = 0; i < 16; i++ )
  19157. ctx->y[i] ^= work_buf[i];
  19158. gcm_mult( ctx, ctx->y, ctx->y );
  19159. }
  19160. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->y, 16, ctx->base_ectr,
  19161. &olen ) ) != 0 )
  19162. {
  19163. return( ret );
  19164. }
  19165. ctx->add_len = add_len;
  19166. p = add;
  19167. while( add_len > 0 )
  19168. {
  19169. use_len = ( add_len < 16 ) ? add_len : 16;
  19170. for( i = 0; i < use_len; i++ )
  19171. ctx->buf[i] ^= p[i];
  19172. gcm_mult( ctx, ctx->buf, ctx->buf );
  19173. add_len -= use_len;
  19174. p += use_len;
  19175. }
  19176. return( 0 );
  19177. }
  19178. int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
  19179. size_t length,
  19180. const unsigned char *input,
  19181. unsigned char *output )
  19182. {
  19183. int ret;
  19184. unsigned char ectr[16];
  19185. size_t i;
  19186. const unsigned char *p;
  19187. unsigned char *out_p = output;
  19188. size_t use_len, olen = 0;
  19189. if( output > input && (size_t) ( output - input ) < length )
  19190. return( MBEDTLS_ERR_GCM_BAD_INPUT );
  19191. /* Total length is restricted to 2^39 - 256 bits, ie 2^36 - 2^5 bytes
  19192. * Also check for possible overflow */
  19193. if( ctx->len + length < ctx->len ||
  19194. (uint64_t) ctx->len + length > 0xFFFFFFFE0ull )
  19195. {
  19196. return( MBEDTLS_ERR_GCM_BAD_INPUT );
  19197. }
  19198. ctx->len += length;
  19199. p = input;
  19200. while( length > 0 )
  19201. {
  19202. use_len = ( length < 16 ) ? length : 16;
  19203. for( i = 16; i > 12; i-- )
  19204. if( ++ctx->y[i - 1] != 0 )
  19205. break;
  19206. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->y, 16, ectr,
  19207. &olen ) ) != 0 )
  19208. {
  19209. return( ret );
  19210. }
  19211. for( i = 0; i < use_len; i++ )
  19212. {
  19213. if( ctx->mode == MBEDTLS_GCM_DECRYPT )
  19214. ctx->buf[i] ^= p[i];
  19215. out_p[i] = ectr[i] ^ p[i];
  19216. if( ctx->mode == MBEDTLS_GCM_ENCRYPT )
  19217. ctx->buf[i] ^= out_p[i];
  19218. }
  19219. gcm_mult( ctx, ctx->buf, ctx->buf );
  19220. length -= use_len;
  19221. p += use_len;
  19222. out_p += use_len;
  19223. }
  19224. return( 0 );
  19225. }
  19226. int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
  19227. unsigned char *tag,
  19228. size_t tag_len )
  19229. {
  19230. unsigned char work_buf[16];
  19231. size_t i;
  19232. uint64_t orig_len = ctx->len * 8;
  19233. uint64_t orig_add_len = ctx->add_len * 8;
  19234. if( tag_len > 16 || tag_len < 4 )
  19235. return( MBEDTLS_ERR_GCM_BAD_INPUT );
  19236. memcpy( tag, ctx->base_ectr, tag_len );
  19237. if( orig_len || orig_add_len )
  19238. {
  19239. memset( work_buf, 0x00, 16 );
  19240. PUT_UINT32_BE( ( orig_add_len >> 32 ), work_buf, 0 );
  19241. PUT_UINT32_BE( ( orig_add_len ), work_buf, 4 );
  19242. PUT_UINT32_BE( ( orig_len >> 32 ), work_buf, 8 );
  19243. PUT_UINT32_BE( ( orig_len ), work_buf, 12 );
  19244. for( i = 0; i < 16; i++ )
  19245. ctx->buf[i] ^= work_buf[i];
  19246. gcm_mult( ctx, ctx->buf, ctx->buf );
  19247. for( i = 0; i < tag_len; i++ )
  19248. tag[i] ^= ctx->buf[i];
  19249. }
  19250. return( 0 );
  19251. }
  19252. int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
  19253. int mode,
  19254. size_t length,
  19255. const unsigned char *iv,
  19256. size_t iv_len,
  19257. const unsigned char *add,
  19258. size_t add_len,
  19259. const unsigned char *input,
  19260. unsigned char *output,
  19261. size_t tag_len,
  19262. unsigned char *tag )
  19263. {
  19264. int ret;
  19265. if( ( ret = mbedtls_gcm_starts( ctx, mode, iv, iv_len, add, add_len ) ) != 0 )
  19266. return( ret );
  19267. if( ( ret = mbedtls_gcm_update( ctx, length, input, output ) ) != 0 )
  19268. return( ret );
  19269. if( ( ret = mbedtls_gcm_finish( ctx, tag, tag_len ) ) != 0 )
  19270. return( ret );
  19271. return( 0 );
  19272. }
  19273. int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
  19274. size_t length,
  19275. const unsigned char *iv,
  19276. size_t iv_len,
  19277. const unsigned char *add,
  19278. size_t add_len,
  19279. const unsigned char *tag,
  19280. size_t tag_len,
  19281. const unsigned char *input,
  19282. unsigned char *output )
  19283. {
  19284. int ret;
  19285. unsigned char check_tag[16];
  19286. size_t i;
  19287. int diff;
  19288. if( ( ret = mbedtls_gcm_crypt_and_tag( ctx, MBEDTLS_GCM_DECRYPT, length,
  19289. iv, iv_len, add, add_len,
  19290. input, output, tag_len, check_tag ) ) != 0 )
  19291. {
  19292. return( ret );
  19293. }
  19294. /* Check tag in "constant-time" */
  19295. for( diff = 0, i = 0; i < tag_len; i++ )
  19296. diff |= tag[i] ^ check_tag[i];
  19297. if( diff != 0 )
  19298. {
  19299. mbedtls_zeroize( output, length );
  19300. return( MBEDTLS_ERR_GCM_AUTH_FAILED );
  19301. }
  19302. return( 0 );
  19303. }
  19304. void mbedtls_gcm_free( mbedtls_gcm_context *ctx )
  19305. {
  19306. mbedtls_cipher_free( &ctx->cipher_ctx );
  19307. mbedtls_zeroize( ctx, sizeof( mbedtls_gcm_context ) );
  19308. }
  19309. #endif /* !MBEDTLS_GCM_ALT */
  19310. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  19311. /*
  19312. * AES-GCM test vectors from:
  19313. *
  19314. * http://csrc.nist.gov/groups/STM/cavp/documents/mac/gcmtestvectors.zip
  19315. */
  19316. #define MAX_TESTS 6
  19317. static const int key_index[MAX_TESTS] =
  19318. { 0, 0, 1, 1, 1, 1 };
  19319. static const unsigned char key[MAX_TESTS][32] =
  19320. {
  19321. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19322. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19323. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19324. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  19325. { 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  19326. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  19327. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  19328. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08 },
  19329. };
  19330. static const size_t iv_len[MAX_TESTS] =
  19331. { 12, 12, 12, 12, 8, 60 };
  19332. static const int iv_index[MAX_TESTS] =
  19333. { 0, 0, 1, 1, 1, 2 };
  19334. static const unsigned char iv[MAX_TESTS][64] =
  19335. {
  19336. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19337. 0x00, 0x00, 0x00, 0x00 },
  19338. { 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  19339. 0xde, 0xca, 0xf8, 0x88 },
  19340. { 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  19341. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  19342. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  19343. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  19344. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  19345. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  19346. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  19347. 0xa6, 0x37, 0xb3, 0x9b },
  19348. };
  19349. static const size_t add_len[MAX_TESTS] =
  19350. { 0, 0, 0, 20, 20, 20 };
  19351. static const int add_index[MAX_TESTS] =
  19352. { 0, 0, 0, 1, 1, 1 };
  19353. static const unsigned char additional[MAX_TESTS][64] =
  19354. {
  19355. { 0x00 },
  19356. { 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  19357. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  19358. 0xab, 0xad, 0xda, 0xd2 },
  19359. };
  19360. static const size_t pt_len[MAX_TESTS] =
  19361. { 0, 16, 64, 60, 60, 60 };
  19362. static const int pt_index[MAX_TESTS] =
  19363. { 0, 0, 1, 1, 1, 1 };
  19364. static const unsigned char pt[MAX_TESTS][64] =
  19365. {
  19366. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19367. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  19368. { 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  19369. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  19370. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  19371. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  19372. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  19373. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  19374. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  19375. 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55 },
  19376. };
  19377. static const unsigned char ct[MAX_TESTS * 3][64] =
  19378. {
  19379. { 0x00 },
  19380. { 0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92,
  19381. 0xf3, 0x28, 0xc2, 0xb9, 0x71, 0xb2, 0xfe, 0x78 },
  19382. { 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24,
  19383. 0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c,
  19384. 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0,
  19385. 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e,
  19386. 0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c,
  19387. 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05,
  19388. 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97,
  19389. 0x3d, 0x58, 0xe0, 0x91, 0x47, 0x3f, 0x59, 0x85 },
  19390. { 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24,
  19391. 0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c,
  19392. 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0,
  19393. 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e,
  19394. 0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c,
  19395. 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05,
  19396. 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97,
  19397. 0x3d, 0x58, 0xe0, 0x91 },
  19398. { 0x61, 0x35, 0x3b, 0x4c, 0x28, 0x06, 0x93, 0x4a,
  19399. 0x77, 0x7f, 0xf5, 0x1f, 0xa2, 0x2a, 0x47, 0x55,
  19400. 0x69, 0x9b, 0x2a, 0x71, 0x4f, 0xcd, 0xc6, 0xf8,
  19401. 0x37, 0x66, 0xe5, 0xf9, 0x7b, 0x6c, 0x74, 0x23,
  19402. 0x73, 0x80, 0x69, 0x00, 0xe4, 0x9f, 0x24, 0xb2,
  19403. 0x2b, 0x09, 0x75, 0x44, 0xd4, 0x89, 0x6b, 0x42,
  19404. 0x49, 0x89, 0xb5, 0xe1, 0xeb, 0xac, 0x0f, 0x07,
  19405. 0xc2, 0x3f, 0x45, 0x98 },
  19406. { 0x8c, 0xe2, 0x49, 0x98, 0x62, 0x56, 0x15, 0xb6,
  19407. 0x03, 0xa0, 0x33, 0xac, 0xa1, 0x3f, 0xb8, 0x94,
  19408. 0xbe, 0x91, 0x12, 0xa5, 0xc3, 0xa2, 0x11, 0xa8,
  19409. 0xba, 0x26, 0x2a, 0x3c, 0xca, 0x7e, 0x2c, 0xa7,
  19410. 0x01, 0xe4, 0xa9, 0xa4, 0xfb, 0xa4, 0x3c, 0x90,
  19411. 0xcc, 0xdc, 0xb2, 0x81, 0xd4, 0x8c, 0x7c, 0x6f,
  19412. 0xd6, 0x28, 0x75, 0xd2, 0xac, 0xa4, 0x17, 0x03,
  19413. 0x4c, 0x34, 0xae, 0xe5 },
  19414. { 0x00 },
  19415. { 0x98, 0xe7, 0x24, 0x7c, 0x07, 0xf0, 0xfe, 0x41,
  19416. 0x1c, 0x26, 0x7e, 0x43, 0x84, 0xb0, 0xf6, 0x00 },
  19417. { 0x39, 0x80, 0xca, 0x0b, 0x3c, 0x00, 0xe8, 0x41,
  19418. 0xeb, 0x06, 0xfa, 0xc4, 0x87, 0x2a, 0x27, 0x57,
  19419. 0x85, 0x9e, 0x1c, 0xea, 0xa6, 0xef, 0xd9, 0x84,
  19420. 0x62, 0x85, 0x93, 0xb4, 0x0c, 0xa1, 0xe1, 0x9c,
  19421. 0x7d, 0x77, 0x3d, 0x00, 0xc1, 0x44, 0xc5, 0x25,
  19422. 0xac, 0x61, 0x9d, 0x18, 0xc8, 0x4a, 0x3f, 0x47,
  19423. 0x18, 0xe2, 0x44, 0x8b, 0x2f, 0xe3, 0x24, 0xd9,
  19424. 0xcc, 0xda, 0x27, 0x10, 0xac, 0xad, 0xe2, 0x56 },
  19425. { 0x39, 0x80, 0xca, 0x0b, 0x3c, 0x00, 0xe8, 0x41,
  19426. 0xeb, 0x06, 0xfa, 0xc4, 0x87, 0x2a, 0x27, 0x57,
  19427. 0x85, 0x9e, 0x1c, 0xea, 0xa6, 0xef, 0xd9, 0x84,
  19428. 0x62, 0x85, 0x93, 0xb4, 0x0c, 0xa1, 0xe1, 0x9c,
  19429. 0x7d, 0x77, 0x3d, 0x00, 0xc1, 0x44, 0xc5, 0x25,
  19430. 0xac, 0x61, 0x9d, 0x18, 0xc8, 0x4a, 0x3f, 0x47,
  19431. 0x18, 0xe2, 0x44, 0x8b, 0x2f, 0xe3, 0x24, 0xd9,
  19432. 0xcc, 0xda, 0x27, 0x10 },
  19433. { 0x0f, 0x10, 0xf5, 0x99, 0xae, 0x14, 0xa1, 0x54,
  19434. 0xed, 0x24, 0xb3, 0x6e, 0x25, 0x32, 0x4d, 0xb8,
  19435. 0xc5, 0x66, 0x63, 0x2e, 0xf2, 0xbb, 0xb3, 0x4f,
  19436. 0x83, 0x47, 0x28, 0x0f, 0xc4, 0x50, 0x70, 0x57,
  19437. 0xfd, 0xdc, 0x29, 0xdf, 0x9a, 0x47, 0x1f, 0x75,
  19438. 0xc6, 0x65, 0x41, 0xd4, 0xd4, 0xda, 0xd1, 0xc9,
  19439. 0xe9, 0x3a, 0x19, 0xa5, 0x8e, 0x8b, 0x47, 0x3f,
  19440. 0xa0, 0xf0, 0x62, 0xf7 },
  19441. { 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  19442. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  19443. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  19444. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  19445. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  19446. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  19447. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  19448. 0xe9, 0xb7, 0x37, 0x3b },
  19449. { 0x00 },
  19450. { 0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e,
  19451. 0x07, 0x4e, 0xc5, 0xd3, 0xba, 0xf3, 0x9d, 0x18 },
  19452. { 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  19453. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  19454. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  19455. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  19456. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  19457. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  19458. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  19459. 0xbc, 0xc9, 0xf6, 0x62, 0x89, 0x80, 0x15, 0xad },
  19460. { 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  19461. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  19462. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  19463. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  19464. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  19465. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  19466. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  19467. 0xbc, 0xc9, 0xf6, 0x62 },
  19468. { 0xc3, 0x76, 0x2d, 0xf1, 0xca, 0x78, 0x7d, 0x32,
  19469. 0xae, 0x47, 0xc1, 0x3b, 0xf1, 0x98, 0x44, 0xcb,
  19470. 0xaf, 0x1a, 0xe1, 0x4d, 0x0b, 0x97, 0x6a, 0xfa,
  19471. 0xc5, 0x2f, 0xf7, 0xd7, 0x9b, 0xba, 0x9d, 0xe0,
  19472. 0xfe, 0xb5, 0x82, 0xd3, 0x39, 0x34, 0xa4, 0xf0,
  19473. 0x95, 0x4c, 0xc2, 0x36, 0x3b, 0xc7, 0x3f, 0x78,
  19474. 0x62, 0xac, 0x43, 0x0e, 0x64, 0xab, 0xe4, 0x99,
  19475. 0xf4, 0x7c, 0x9b, 0x1f },
  19476. { 0x5a, 0x8d, 0xef, 0x2f, 0x0c, 0x9e, 0x53, 0xf1,
  19477. 0xf7, 0x5d, 0x78, 0x53, 0x65, 0x9e, 0x2a, 0x20,
  19478. 0xee, 0xb2, 0xb2, 0x2a, 0xaf, 0xde, 0x64, 0x19,
  19479. 0xa0, 0x58, 0xab, 0x4f, 0x6f, 0x74, 0x6b, 0xf4,
  19480. 0x0f, 0xc0, 0xc3, 0xb7, 0x80, 0xf2, 0x44, 0x45,
  19481. 0x2d, 0xa3, 0xeb, 0xf1, 0xc5, 0xd8, 0x2c, 0xde,
  19482. 0xa2, 0x41, 0x89, 0x97, 0x20, 0x0e, 0xf8, 0x2e,
  19483. 0x44, 0xae, 0x7e, 0x3f },
  19484. };
  19485. static const unsigned char tag[MAX_TESTS * 3][16] =
  19486. {
  19487. { 0x58, 0xe2, 0xfc, 0xce, 0xfa, 0x7e, 0x30, 0x61,
  19488. 0x36, 0x7f, 0x1d, 0x57, 0xa4, 0xe7, 0x45, 0x5a },
  19489. { 0xab, 0x6e, 0x47, 0xd4, 0x2c, 0xec, 0x13, 0xbd,
  19490. 0xf5, 0x3a, 0x67, 0xb2, 0x12, 0x57, 0xbd, 0xdf },
  19491. { 0x4d, 0x5c, 0x2a, 0xf3, 0x27, 0xcd, 0x64, 0xa6,
  19492. 0x2c, 0xf3, 0x5a, 0xbd, 0x2b, 0xa6, 0xfa, 0xb4 },
  19493. { 0x5b, 0xc9, 0x4f, 0xbc, 0x32, 0x21, 0xa5, 0xdb,
  19494. 0x94, 0xfa, 0xe9, 0x5a, 0xe7, 0x12, 0x1a, 0x47 },
  19495. { 0x36, 0x12, 0xd2, 0xe7, 0x9e, 0x3b, 0x07, 0x85,
  19496. 0x56, 0x1b, 0xe1, 0x4a, 0xac, 0xa2, 0xfc, 0xcb },
  19497. { 0x61, 0x9c, 0xc5, 0xae, 0xff, 0xfe, 0x0b, 0xfa,
  19498. 0x46, 0x2a, 0xf4, 0x3c, 0x16, 0x99, 0xd0, 0x50 },
  19499. { 0xcd, 0x33, 0xb2, 0x8a, 0xc7, 0x73, 0xf7, 0x4b,
  19500. 0xa0, 0x0e, 0xd1, 0xf3, 0x12, 0x57, 0x24, 0x35 },
  19501. { 0x2f, 0xf5, 0x8d, 0x80, 0x03, 0x39, 0x27, 0xab,
  19502. 0x8e, 0xf4, 0xd4, 0x58, 0x75, 0x14, 0xf0, 0xfb },
  19503. { 0x99, 0x24, 0xa7, 0xc8, 0x58, 0x73, 0x36, 0xbf,
  19504. 0xb1, 0x18, 0x02, 0x4d, 0xb8, 0x67, 0x4a, 0x14 },
  19505. { 0x25, 0x19, 0x49, 0x8e, 0x80, 0xf1, 0x47, 0x8f,
  19506. 0x37, 0xba, 0x55, 0xbd, 0x6d, 0x27, 0x61, 0x8c },
  19507. { 0x65, 0xdc, 0xc5, 0x7f, 0xcf, 0x62, 0x3a, 0x24,
  19508. 0x09, 0x4f, 0xcc, 0xa4, 0x0d, 0x35, 0x33, 0xf8 },
  19509. { 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  19510. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9 },
  19511. { 0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9,
  19512. 0xa9, 0x63, 0xb4, 0xf1, 0xc4, 0xcb, 0x73, 0x8b },
  19513. { 0xd0, 0xd1, 0xc8, 0xa7, 0x99, 0x99, 0x6b, 0xf0,
  19514. 0x26, 0x5b, 0x98, 0xb5, 0xd4, 0x8a, 0xb9, 0x19 },
  19515. { 0xb0, 0x94, 0xda, 0xc5, 0xd9, 0x34, 0x71, 0xbd,
  19516. 0xec, 0x1a, 0x50, 0x22, 0x70, 0xe3, 0xcc, 0x6c },
  19517. { 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  19518. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b },
  19519. { 0x3a, 0x33, 0x7d, 0xbf, 0x46, 0xa7, 0x92, 0xc4,
  19520. 0x5e, 0x45, 0x49, 0x13, 0xfe, 0x2e, 0xa8, 0xf2 },
  19521. { 0xa4, 0x4a, 0x82, 0x66, 0xee, 0x1c, 0x8e, 0xb0,
  19522. 0xc8, 0xb5, 0xd4, 0xcf, 0x5a, 0xe9, 0xf1, 0x9a },
  19523. };
  19524. int mbedtls_gcm_self_test( int verbose )
  19525. {
  19526. mbedtls_gcm_context ctx;
  19527. unsigned char buf[64];
  19528. unsigned char tag_buf[16];
  19529. int i, j, ret;
  19530. mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
  19531. for( j = 0; j < 3; j++ )
  19532. {
  19533. int key_len = 128 + 64 * j;
  19534. for( i = 0; i < MAX_TESTS; i++ )
  19535. {
  19536. mbedtls_gcm_init( &ctx );
  19537. if( verbose != 0 )
  19538. mbedtls_printf( " AES-GCM-%3d #%d (%s): ",
  19539. key_len, i, "enc" );
  19540. ret = mbedtls_gcm_setkey( &ctx, cipher, key[key_index[i]],
  19541. key_len );
  19542. /*
  19543. * AES-192 is an optional feature that may be unavailable when
  19544. * there is an alternative underlying implementation i.e. when
  19545. * MBEDTLS_AES_ALT is defined.
  19546. */
  19547. if( ret == MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE && key_len == 192 )
  19548. {
  19549. mbedtls_printf( "skipped\n" );
  19550. break;
  19551. }
  19552. else if( ret != 0 )
  19553. {
  19554. goto exit;
  19555. }
  19556. ret = mbedtls_gcm_crypt_and_tag( &ctx, MBEDTLS_GCM_ENCRYPT,
  19557. pt_len[i],
  19558. iv[iv_index[i]], iv_len[i],
  19559. additional[add_index[i]], add_len[i],
  19560. pt[pt_index[i]], buf, 16, tag_buf );
  19561. if( ret != 0 )
  19562. goto exit;
  19563. if ( memcmp( buf, ct[j * 6 + i], pt_len[i] ) != 0 ||
  19564. memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 )
  19565. {
  19566. ret = 1;
  19567. goto exit;
  19568. }
  19569. mbedtls_gcm_free( &ctx );
  19570. if( verbose != 0 )
  19571. mbedtls_printf( "passed\n" );
  19572. mbedtls_gcm_init( &ctx );
  19573. if( verbose != 0 )
  19574. mbedtls_printf( " AES-GCM-%3d #%d (%s): ",
  19575. key_len, i, "dec" );
  19576. ret = mbedtls_gcm_setkey( &ctx, cipher, key[key_index[i]],
  19577. key_len );
  19578. if( ret != 0 )
  19579. goto exit;
  19580. ret = mbedtls_gcm_crypt_and_tag( &ctx, MBEDTLS_GCM_DECRYPT,
  19581. pt_len[i],
  19582. iv[iv_index[i]], iv_len[i],
  19583. additional[add_index[i]], add_len[i],
  19584. ct[j * 6 + i], buf, 16, tag_buf );
  19585. if( ret != 0 )
  19586. goto exit;
  19587. if( memcmp( buf, pt[pt_index[i]], pt_len[i] ) != 0 ||
  19588. memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 )
  19589. {
  19590. ret = 1;
  19591. goto exit;
  19592. }
  19593. mbedtls_gcm_free( &ctx );
  19594. if( verbose != 0 )
  19595. mbedtls_printf( "passed\n" );
  19596. mbedtls_gcm_init( &ctx );
  19597. if( verbose != 0 )
  19598. mbedtls_printf( " AES-GCM-%3d #%d split (%s): ",
  19599. key_len, i, "enc" );
  19600. ret = mbedtls_gcm_setkey( &ctx, cipher, key[key_index[i]],
  19601. key_len );
  19602. if( ret != 0 )
  19603. goto exit;
  19604. ret = mbedtls_gcm_starts( &ctx, MBEDTLS_GCM_ENCRYPT,
  19605. iv[iv_index[i]], iv_len[i],
  19606. additional[add_index[i]], add_len[i] );
  19607. if( ret != 0 )
  19608. goto exit;
  19609. if( pt_len[i] > 32 )
  19610. {
  19611. size_t rest_len = pt_len[i] - 32;
  19612. ret = mbedtls_gcm_update( &ctx, 32, pt[pt_index[i]], buf );
  19613. if( ret != 0 )
  19614. goto exit;
  19615. ret = mbedtls_gcm_update( &ctx, rest_len, pt[pt_index[i]] + 32,
  19616. buf + 32 );
  19617. if( ret != 0 )
  19618. goto exit;
  19619. }
  19620. else
  19621. {
  19622. ret = mbedtls_gcm_update( &ctx, pt_len[i], pt[pt_index[i]], buf );
  19623. if( ret != 0 )
  19624. goto exit;
  19625. }
  19626. ret = mbedtls_gcm_finish( &ctx, tag_buf, 16 );
  19627. if( ret != 0 )
  19628. goto exit;
  19629. if( memcmp( buf, ct[j * 6 + i], pt_len[i] ) != 0 ||
  19630. memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 )
  19631. {
  19632. ret = 1;
  19633. goto exit;
  19634. }
  19635. mbedtls_gcm_free( &ctx );
  19636. if( verbose != 0 )
  19637. mbedtls_printf( "passed\n" );
  19638. mbedtls_gcm_init( &ctx );
  19639. if( verbose != 0 )
  19640. mbedtls_printf( " AES-GCM-%3d #%d split (%s): ",
  19641. key_len, i, "dec" );
  19642. ret = mbedtls_gcm_setkey( &ctx, cipher, key[key_index[i]],
  19643. key_len );
  19644. if( ret != 0 )
  19645. goto exit;
  19646. ret = mbedtls_gcm_starts( &ctx, MBEDTLS_GCM_DECRYPT,
  19647. iv[iv_index[i]], iv_len[i],
  19648. additional[add_index[i]], add_len[i] );
  19649. if( ret != 0 )
  19650. goto exit;
  19651. if( pt_len[i] > 32 )
  19652. {
  19653. size_t rest_len = pt_len[i] - 32;
  19654. ret = mbedtls_gcm_update( &ctx, 32, ct[j * 6 + i], buf );
  19655. if( ret != 0 )
  19656. goto exit;
  19657. ret = mbedtls_gcm_update( &ctx, rest_len, ct[j * 6 + i] + 32,
  19658. buf + 32 );
  19659. if( ret != 0 )
  19660. goto exit;
  19661. }
  19662. else
  19663. {
  19664. ret = mbedtls_gcm_update( &ctx, pt_len[i], ct[j * 6 + i],
  19665. buf );
  19666. if( ret != 0 )
  19667. goto exit;
  19668. }
  19669. ret = mbedtls_gcm_finish( &ctx, tag_buf, 16 );
  19670. if( ret != 0 )
  19671. goto exit;
  19672. if( memcmp( buf, pt[pt_index[i]], pt_len[i] ) != 0 ||
  19673. memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 )
  19674. {
  19675. ret = 1;
  19676. goto exit;
  19677. }
  19678. mbedtls_gcm_free( &ctx );
  19679. if( verbose != 0 )
  19680. mbedtls_printf( "passed\n" );
  19681. }
  19682. }
  19683. if( verbose != 0 )
  19684. mbedtls_printf( "\n" );
  19685. ret = 0;
  19686. exit:
  19687. if( ret != 0 )
  19688. {
  19689. if( verbose != 0 )
  19690. mbedtls_printf( "failed\n" );
  19691. mbedtls_gcm_free( &ctx );
  19692. }
  19693. return( ret );
  19694. }
  19695. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  19696. #endif /* MBEDTLS_GCM_C */
  19697. /*
  19698. Amalgamated build undefines
  19699. */
  19700. #undef ADD
  19701. #undef BC
  19702. #undef BEFORE_COLON
  19703. #undef F
  19704. #undef F0
  19705. #undef F1
  19706. #undef F2
  19707. #undef F3
  19708. #undef F4
  19709. #undef F5
  19710. #undef FSb
  19711. #undef K
  19712. #undef KK
  19713. #undef P
  19714. #undef R
  19715. #undef ROTR
  19716. #undef S
  19717. #undef S0
  19718. #undef S1
  19719. #undef S2
  19720. #undef S3
  19721. #undef SAFE_SNPRINTF
  19722. #undef SHR
  19723. #undef close
  19724. #undef read
  19725. #undef supported_init
  19726. #undef write
  19727. /********* Start of file library/havege.c ************/
  19728. /**
  19729. * \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion
  19730. *
  19731. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  19732. * SPDX-License-Identifier: Apache-2.0
  19733. *
  19734. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  19735. * not use this file except in compliance with the License.
  19736. * You may obtain a copy of the License at
  19737. *
  19738. * http://www.apache.org/licenses/LICENSE-2.0
  19739. *
  19740. * Unless required by applicable law or agreed to in writing, software
  19741. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  19742. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19743. * See the License for the specific language governing permissions and
  19744. * limitations under the License.
  19745. *
  19746. * This file is part of mbed TLS (https://tls.mbed.org)
  19747. */
  19748. /*
  19749. * The HAVEGE RNG was designed by Andre Seznec in 2002.
  19750. *
  19751. * http://www.irisa.fr/caps/projects/hipsor/publi.php
  19752. *
  19753. * Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr
  19754. */
  19755. #if !defined(MBEDTLS_CONFIG_FILE)
  19756. #else
  19757. #endif
  19758. #if defined(MBEDTLS_HAVEGE_C)
  19759. #include <string.h>
  19760. /* Implementation that should never be optimized out by the compiler */
  19761. /* zeroize was here */
  19762. /* ------------------------------------------------------------------------
  19763. * On average, one iteration accesses two 8-word blocks in the havege WALK
  19764. * table, and generates 16 words in the RES array.
  19765. *
  19766. * The data read in the WALK table is updated and permuted after each use.
  19767. * The result of the hardware clock counter read is used for this update.
  19768. *
  19769. * 25 conditional tests are present. The conditional tests are grouped in
  19770. * two nested groups of 12 conditional tests and 1 test that controls the
  19771. * permutation; on average, there should be 6 tests executed and 3 of them
  19772. * should be mispredicted.
  19773. * ------------------------------------------------------------------------
  19774. */
  19775. #define SWAP(X,Y) { int *T = X; X = Y; Y = T; }
  19776. #define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
  19777. #define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
  19778. #define TST1_LEAVE U1++; }
  19779. #define TST2_LEAVE U2++; }
  19780. #define ONE_ITERATION \
  19781. \
  19782. PTEST = PT1 >> 20; \
  19783. \
  19784. TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
  19785. TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
  19786. TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
  19787. \
  19788. TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
  19789. TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
  19790. TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
  19791. \
  19792. PTX = (PT1 >> 18) & 7; \
  19793. PT1 &= 0x1FFF; \
  19794. PT2 &= 0x1FFF; \
  19795. CLK = (int) mbedtls_timing_hardclock(); \
  19796. \
  19797. i = 0; \
  19798. A = &WALK[PT1 ]; RES[i++] ^= *A; \
  19799. B = &WALK[PT2 ]; RES[i++] ^= *B; \
  19800. C = &WALK[PT1 ^ 1]; RES[i++] ^= *C; \
  19801. D = &WALK[PT2 ^ 4]; RES[i++] ^= *D; \
  19802. \
  19803. IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \
  19804. *A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \
  19805. *B = IN ^ U1; \
  19806. *C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \
  19807. *D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \
  19808. \
  19809. A = &WALK[PT1 ^ 2]; RES[i++] ^= *A; \
  19810. B = &WALK[PT2 ^ 2]; RES[i++] ^= *B; \
  19811. C = &WALK[PT1 ^ 3]; RES[i++] ^= *C; \
  19812. D = &WALK[PT2 ^ 6]; RES[i++] ^= *D; \
  19813. \
  19814. if( PTEST & 1 ) SWAP( A, C ); \
  19815. \
  19816. IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \
  19817. *A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \
  19818. *B = IN; CLK = (int) mbedtls_timing_hardclock(); \
  19819. *C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \
  19820. *D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \
  19821. \
  19822. A = &WALK[PT1 ^ 4]; \
  19823. B = &WALK[PT2 ^ 1]; \
  19824. \
  19825. PTEST = PT2 >> 1; \
  19826. \
  19827. PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \
  19828. PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \
  19829. PTY = (PT2 >> 10) & 7; \
  19830. \
  19831. TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
  19832. TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
  19833. TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
  19834. \
  19835. TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
  19836. TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
  19837. TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
  19838. \
  19839. C = &WALK[PT1 ^ 5]; \
  19840. D = &WALK[PT2 ^ 5]; \
  19841. \
  19842. RES[i++] ^= *A; \
  19843. RES[i++] ^= *B; \
  19844. RES[i++] ^= *C; \
  19845. RES[i++] ^= *D; \
  19846. \
  19847. IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \
  19848. *A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \
  19849. *B = IN ^ U2; \
  19850. *C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \
  19851. *D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \
  19852. \
  19853. A = &WALK[PT1 ^ 6]; RES[i++] ^= *A; \
  19854. B = &WALK[PT2 ^ 3]; RES[i++] ^= *B; \
  19855. C = &WALK[PT1 ^ 7]; RES[i++] ^= *C; \
  19856. D = &WALK[PT2 ^ 7]; RES[i++] ^= *D; \
  19857. \
  19858. IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \
  19859. *A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \
  19860. *B = IN; \
  19861. *C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \
  19862. *D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \
  19863. \
  19864. PT1 = ( RES[( i - 8 ) ^ PTX] ^ \
  19865. WALK[PT1 ^ PTX ^ 7] ) & (~1); \
  19866. PT1 ^= (PT2 ^ 0x10) & 0x10; \
  19867. \
  19868. for( n++, i = 0; i < 16; i++ ) \
  19869. hs->pool[n % MBEDTLS_HAVEGE_COLLECT_SIZE] ^= RES[i];
  19870. /*
  19871. * Entropy gathering function
  19872. */
  19873. static void havege_fill( mbedtls_havege_state *hs )
  19874. {
  19875. int i, n = 0;
  19876. int U1, U2, *A, *B, *C, *D;
  19877. int PT1, PT2, *WALK, RES[16];
  19878. int PTX, PTY, CLK, PTEST, IN;
  19879. WALK = hs->WALK;
  19880. PT1 = hs->PT1;
  19881. PT2 = hs->PT2;
  19882. PTX = U1 = 0;
  19883. PTY = U2 = 0;
  19884. (void)PTX;
  19885. memset( RES, 0, sizeof( RES ) );
  19886. while( n < MBEDTLS_HAVEGE_COLLECT_SIZE * 4 )
  19887. {
  19888. ONE_ITERATION
  19889. ONE_ITERATION
  19890. ONE_ITERATION
  19891. ONE_ITERATION
  19892. }
  19893. hs->PT1 = PT1;
  19894. hs->PT2 = PT2;
  19895. hs->offset[0] = 0;
  19896. hs->offset[1] = MBEDTLS_HAVEGE_COLLECT_SIZE / 2;
  19897. }
  19898. /*
  19899. * HAVEGE initialization
  19900. */
  19901. void mbedtls_havege_init( mbedtls_havege_state *hs )
  19902. {
  19903. memset( hs, 0, sizeof( mbedtls_havege_state ) );
  19904. havege_fill( hs );
  19905. }
  19906. void mbedtls_havege_free( mbedtls_havege_state *hs )
  19907. {
  19908. if( hs == NULL )
  19909. return;
  19910. mbedtls_zeroize( hs, sizeof( mbedtls_havege_state ) );
  19911. }
  19912. /*
  19913. * HAVEGE rand function
  19914. */
  19915. int mbedtls_havege_random( void *p_rng, unsigned char *buf, size_t len )
  19916. {
  19917. int val;
  19918. size_t use_len;
  19919. mbedtls_havege_state *hs = (mbedtls_havege_state *) p_rng;
  19920. unsigned char *p = buf;
  19921. while( len > 0 )
  19922. {
  19923. use_len = len;
  19924. if( use_len > sizeof(int) )
  19925. use_len = sizeof(int);
  19926. if( hs->offset[1] >= MBEDTLS_HAVEGE_COLLECT_SIZE )
  19927. havege_fill( hs );
  19928. val = hs->pool[hs->offset[0]++];
  19929. val ^= hs->pool[hs->offset[1]++];
  19930. memcpy( p, &val, use_len );
  19931. len -= use_len;
  19932. p += use_len;
  19933. }
  19934. return( 0 );
  19935. }
  19936. #endif /* MBEDTLS_HAVEGE_C */
  19937. /*
  19938. Amalgamated build undefines
  19939. */
  19940. #undef ADD
  19941. #undef BC
  19942. #undef BEFORE_COLON
  19943. #undef F
  19944. #undef F0
  19945. #undef F1
  19946. #undef F2
  19947. #undef F3
  19948. #undef F4
  19949. #undef F5
  19950. #undef FSb
  19951. #undef K
  19952. #undef KK
  19953. #undef P
  19954. #undef R
  19955. #undef ROTR
  19956. #undef S
  19957. #undef S0
  19958. #undef S1
  19959. #undef S2
  19960. #undef S3
  19961. #undef SAFE_SNPRINTF
  19962. #undef SHR
  19963. #undef close
  19964. #undef read
  19965. #undef supported_init
  19966. #undef write
  19967. /********* Start of file library/hmac_drbg.c ************/
  19968. /*
  19969. * HMAC_DRBG implementation (NIST SP 800-90)
  19970. *
  19971. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  19972. * SPDX-License-Identifier: Apache-2.0
  19973. *
  19974. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  19975. * not use this file except in compliance with the License.
  19976. * You may obtain a copy of the License at
  19977. *
  19978. * http://www.apache.org/licenses/LICENSE-2.0
  19979. *
  19980. * Unless required by applicable law or agreed to in writing, software
  19981. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  19982. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19983. * See the License for the specific language governing permissions and
  19984. * limitations under the License.
  19985. *
  19986. * This file is part of mbed TLS (https://tls.mbed.org)
  19987. */
  19988. /*
  19989. * The NIST SP 800-90A DRBGs are described in the following publication.
  19990. * http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf
  19991. * References below are based on rev. 1 (January 2012).
  19992. */
  19993. #if !defined(MBEDTLS_CONFIG_FILE)
  19994. #else
  19995. #endif
  19996. #if defined(MBEDTLS_HMAC_DRBG_C)
  19997. #include <string.h>
  19998. #if defined(MBEDTLS_FS_IO)
  19999. #include <stdio.h>
  20000. #endif
  20001. #if defined(MBEDTLS_SELF_TEST)
  20002. #if defined(MBEDTLS_PLATFORM_C)
  20003. #else
  20004. #include <stdio.h>
  20005. #define mbedtls_printf printf
  20006. #endif /* MBEDTLS_SELF_TEST */
  20007. #endif /* MBEDTLS_PLATFORM_C */
  20008. /* Implementation that should never be optimized out by the compiler */
  20009. /* zeroize was here */
  20010. /*
  20011. * HMAC_DRBG context initialization
  20012. */
  20013. void mbedtls_hmac_drbg_init( mbedtls_hmac_drbg_context *ctx )
  20014. {
  20015. memset( ctx, 0, sizeof( mbedtls_hmac_drbg_context ) );
  20016. #if defined(MBEDTLS_THREADING_C)
  20017. mbedtls_mutex_init( &ctx->mutex );
  20018. #endif
  20019. }
  20020. /*
  20021. * HMAC_DRBG update, using optional additional data (10.1.2.2)
  20022. */
  20023. void mbedtls_hmac_drbg_update( mbedtls_hmac_drbg_context *ctx,
  20024. const unsigned char *additional, size_t add_len )
  20025. {
  20026. size_t md_len = mbedtls_md_get_size( ctx->md_ctx.md_info );
  20027. unsigned char rounds = ( additional != NULL && add_len != 0 ) ? 2 : 1;
  20028. unsigned char sep[1];
  20029. unsigned char K[MBEDTLS_MD_MAX_SIZE];
  20030. for( sep[0] = 0; sep[0] < rounds; sep[0]++ )
  20031. {
  20032. /* Step 1 or 4 */
  20033. mbedtls_md_hmac_reset( &ctx->md_ctx );
  20034. mbedtls_md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
  20035. mbedtls_md_hmac_update( &ctx->md_ctx, sep, 1 );
  20036. if( rounds == 2 )
  20037. mbedtls_md_hmac_update( &ctx->md_ctx, additional, add_len );
  20038. mbedtls_md_hmac_finish( &ctx->md_ctx, K );
  20039. /* Step 2 or 5 */
  20040. mbedtls_md_hmac_starts( &ctx->md_ctx, K, md_len );
  20041. mbedtls_md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
  20042. mbedtls_md_hmac_finish( &ctx->md_ctx, ctx->V );
  20043. }
  20044. }
  20045. /*
  20046. * Simplified HMAC_DRBG initialisation (for use with deterministic ECDSA)
  20047. */
  20048. int mbedtls_hmac_drbg_seed_buf( mbedtls_hmac_drbg_context *ctx,
  20049. const mbedtls_md_info_t * md_info,
  20050. const unsigned char *data, size_t data_len )
  20051. {
  20052. int ret;
  20053. if( ( ret = mbedtls_md_setup( &ctx->md_ctx, md_info, 1 ) ) != 0 )
  20054. return( ret );
  20055. /*
  20056. * Set initial working state.
  20057. * Use the V memory location, which is currently all 0, to initialize the
  20058. * MD context with an all-zero key. Then set V to its initial value.
  20059. */
  20060. mbedtls_md_hmac_starts( &ctx->md_ctx, ctx->V, mbedtls_md_get_size( md_info ) );
  20061. memset( ctx->V, 0x01, mbedtls_md_get_size( md_info ) );
  20062. mbedtls_hmac_drbg_update( ctx, data, data_len );
  20063. return( 0 );
  20064. }
  20065. /*
  20066. * HMAC_DRBG reseeding: 10.1.2.4 (arabic) + 9.2 (Roman)
  20067. */
  20068. int mbedtls_hmac_drbg_reseed( mbedtls_hmac_drbg_context *ctx,
  20069. const unsigned char *additional, size_t len )
  20070. {
  20071. unsigned char seed[MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT];
  20072. size_t seedlen;
  20073. /* III. Check input length */
  20074. if( len > MBEDTLS_HMAC_DRBG_MAX_INPUT ||
  20075. ctx->entropy_len + len > MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT )
  20076. {
  20077. return( MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG );
  20078. }
  20079. memset( seed, 0, MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT );
  20080. /* IV. Gather entropy_len bytes of entropy for the seed */
  20081. if( ctx->f_entropy( ctx->p_entropy, seed, ctx->entropy_len ) != 0 )
  20082. return( MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED );
  20083. seedlen = ctx->entropy_len;
  20084. /* 1. Concatenate entropy and additional data if any */
  20085. if( additional != NULL && len != 0 )
  20086. {
  20087. memcpy( seed + seedlen, additional, len );
  20088. seedlen += len;
  20089. }
  20090. /* 2. Update state */
  20091. mbedtls_hmac_drbg_update( ctx, seed, seedlen );
  20092. /* 3. Reset reseed_counter */
  20093. ctx->reseed_counter = 1;
  20094. /* 4. Done */
  20095. return( 0 );
  20096. }
  20097. /*
  20098. * HMAC_DRBG initialisation (10.1.2.3 + 9.1)
  20099. */
  20100. int mbedtls_hmac_drbg_seed( mbedtls_hmac_drbg_context *ctx,
  20101. const mbedtls_md_info_t * md_info,
  20102. int (*f_entropy)(void *, unsigned char *, size_t),
  20103. void *p_entropy,
  20104. const unsigned char *custom,
  20105. size_t len )
  20106. {
  20107. int ret;
  20108. size_t entropy_len, md_size;
  20109. if( ( ret = mbedtls_md_setup( &ctx->md_ctx, md_info, 1 ) ) != 0 )
  20110. return( ret );
  20111. md_size = mbedtls_md_get_size( md_info );
  20112. /*
  20113. * Set initial working state.
  20114. * Use the V memory location, which is currently all 0, to initialize the
  20115. * MD context with an all-zero key. Then set V to its initial value.
  20116. */
  20117. mbedtls_md_hmac_starts( &ctx->md_ctx, ctx->V, md_size );
  20118. memset( ctx->V, 0x01, md_size );
  20119. ctx->f_entropy = f_entropy;
  20120. ctx->p_entropy = p_entropy;
  20121. ctx->reseed_interval = MBEDTLS_HMAC_DRBG_RESEED_INTERVAL;
  20122. /*
  20123. * See SP800-57 5.6.1 (p. 65-66) for the security strength provided by
  20124. * each hash function, then according to SP800-90A rev1 10.1 table 2,
  20125. * min_entropy_len (in bits) is security_strength.
  20126. *
  20127. * (This also matches the sizes used in the NIST test vectors.)
  20128. */
  20129. entropy_len = md_size <= 20 ? 16 : /* 160-bits hash -> 128 bits */
  20130. md_size <= 28 ? 24 : /* 224-bits hash -> 192 bits */
  20131. 32; /* better (256+) -> 256 bits */
  20132. /*
  20133. * For initialisation, use more entropy to emulate a nonce
  20134. * (Again, matches test vectors.)
  20135. */
  20136. ctx->entropy_len = entropy_len * 3 / 2;
  20137. if( ( ret = mbedtls_hmac_drbg_reseed( ctx, custom, len ) ) != 0 )
  20138. return( ret );
  20139. ctx->entropy_len = entropy_len;
  20140. return( 0 );
  20141. }
  20142. /*
  20143. * Set prediction resistance
  20144. */
  20145. void mbedtls_hmac_drbg_set_prediction_resistance( mbedtls_hmac_drbg_context *ctx,
  20146. int resistance )
  20147. {
  20148. ctx->prediction_resistance = resistance;
  20149. }
  20150. /*
  20151. * Set entropy length grabbed for reseeds
  20152. */
  20153. void mbedtls_hmac_drbg_set_entropy_len( mbedtls_hmac_drbg_context *ctx, size_t len )
  20154. {
  20155. ctx->entropy_len = len;
  20156. }
  20157. /*
  20158. * Set reseed interval
  20159. */
  20160. void mbedtls_hmac_drbg_set_reseed_interval( mbedtls_hmac_drbg_context *ctx, int interval )
  20161. {
  20162. ctx->reseed_interval = interval;
  20163. }
  20164. /*
  20165. * HMAC_DRBG random function with optional additional data:
  20166. * 10.1.2.5 (arabic) + 9.3 (Roman)
  20167. */
  20168. int mbedtls_hmac_drbg_random_with_add( void *p_rng,
  20169. unsigned char *output, size_t out_len,
  20170. const unsigned char *additional, size_t add_len )
  20171. {
  20172. int ret;
  20173. mbedtls_hmac_drbg_context *ctx = (mbedtls_hmac_drbg_context *) p_rng;
  20174. size_t md_len = mbedtls_md_get_size( ctx->md_ctx.md_info );
  20175. size_t left = out_len;
  20176. unsigned char *out = output;
  20177. /* II. Check request length */
  20178. if( out_len > MBEDTLS_HMAC_DRBG_MAX_REQUEST )
  20179. return( MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG );
  20180. /* III. Check input length */
  20181. if( add_len > MBEDTLS_HMAC_DRBG_MAX_INPUT )
  20182. return( MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG );
  20183. /* 1. (aka VII and IX) Check reseed counter and PR */
  20184. if( ctx->f_entropy != NULL && /* For no-reseeding instances */
  20185. ( ctx->prediction_resistance == MBEDTLS_HMAC_DRBG_PR_ON ||
  20186. ctx->reseed_counter > ctx->reseed_interval ) )
  20187. {
  20188. if( ( ret = mbedtls_hmac_drbg_reseed( ctx, additional, add_len ) ) != 0 )
  20189. return( ret );
  20190. add_len = 0; /* VII.4 */
  20191. }
  20192. /* 2. Use additional data if any */
  20193. if( additional != NULL && add_len != 0 )
  20194. mbedtls_hmac_drbg_update( ctx, additional, add_len );
  20195. /* 3, 4, 5. Generate bytes */
  20196. while( left != 0 )
  20197. {
  20198. size_t use_len = left > md_len ? md_len : left;
  20199. mbedtls_md_hmac_reset( &ctx->md_ctx );
  20200. mbedtls_md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
  20201. mbedtls_md_hmac_finish( &ctx->md_ctx, ctx->V );
  20202. memcpy( out, ctx->V, use_len );
  20203. out += use_len;
  20204. left -= use_len;
  20205. }
  20206. /* 6. Update */
  20207. mbedtls_hmac_drbg_update( ctx, additional, add_len );
  20208. /* 7. Update reseed counter */
  20209. ctx->reseed_counter++;
  20210. /* 8. Done */
  20211. return( 0 );
  20212. }
  20213. /*
  20214. * HMAC_DRBG random function
  20215. */
  20216. int mbedtls_hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len )
  20217. {
  20218. int ret;
  20219. mbedtls_hmac_drbg_context *ctx = (mbedtls_hmac_drbg_context *) p_rng;
  20220. #if defined(MBEDTLS_THREADING_C)
  20221. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  20222. return( ret );
  20223. #endif
  20224. ret = mbedtls_hmac_drbg_random_with_add( ctx, output, out_len, NULL, 0 );
  20225. #if defined(MBEDTLS_THREADING_C)
  20226. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  20227. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  20228. #endif
  20229. return( ret );
  20230. }
  20231. /*
  20232. * Free an HMAC_DRBG context
  20233. */
  20234. void mbedtls_hmac_drbg_free( mbedtls_hmac_drbg_context *ctx )
  20235. {
  20236. if( ctx == NULL )
  20237. return;
  20238. #if defined(MBEDTLS_THREADING_C)
  20239. mbedtls_mutex_free( &ctx->mutex );
  20240. #endif
  20241. mbedtls_md_free( &ctx->md_ctx );
  20242. mbedtls_zeroize( ctx, sizeof( mbedtls_hmac_drbg_context ) );
  20243. }
  20244. #if defined(MBEDTLS_FS_IO)
  20245. int mbedtls_hmac_drbg_write_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path )
  20246. {
  20247. int ret;
  20248. FILE *f;
  20249. unsigned char buf[ MBEDTLS_HMAC_DRBG_MAX_INPUT ];
  20250. if( ( f = fopen( path, "wb" ) ) == NULL )
  20251. return( MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR );
  20252. if( ( ret = mbedtls_hmac_drbg_random( ctx, buf, sizeof( buf ) ) ) != 0 )
  20253. goto exit;
  20254. if( fwrite( buf, 1, sizeof( buf ), f ) != sizeof( buf ) )
  20255. {
  20256. ret = MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR;
  20257. goto exit;
  20258. }
  20259. ret = 0;
  20260. exit:
  20261. fclose( f );
  20262. mbedtls_zeroize( buf, sizeof( buf ) );
  20263. return( ret );
  20264. }
  20265. int mbedtls_hmac_drbg_update_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path )
  20266. {
  20267. int ret = 0;
  20268. FILE *f;
  20269. size_t n;
  20270. unsigned char buf[ MBEDTLS_HMAC_DRBG_MAX_INPUT ];
  20271. if( ( f = fopen( path, "rb" ) ) == NULL )
  20272. return( MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR );
  20273. fseek( f, 0, SEEK_END );
  20274. n = (size_t) ftell( f );
  20275. fseek( f, 0, SEEK_SET );
  20276. if( n > MBEDTLS_HMAC_DRBG_MAX_INPUT )
  20277. {
  20278. fclose( f );
  20279. return( MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG );
  20280. }
  20281. if( fread( buf, 1, n, f ) != n )
  20282. ret = MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR;
  20283. else
  20284. mbedtls_hmac_drbg_update( ctx, buf, n );
  20285. fclose( f );
  20286. mbedtls_zeroize( buf, sizeof( buf ) );
  20287. if( ret != 0 )
  20288. return( ret );
  20289. return( mbedtls_hmac_drbg_write_seed_file( ctx, path ) );
  20290. }
  20291. #endif /* MBEDTLS_FS_IO */
  20292. #if defined(MBEDTLS_SELF_TEST)
  20293. #if !defined(MBEDTLS_SHA1_C)
  20294. /* Dummy checkup routine */
  20295. int mbedtls_hmac_drbg_self_test( int verbose )
  20296. {
  20297. (void) verbose;
  20298. return( 0 );
  20299. }
  20300. #else
  20301. #define OUTPUT_LEN 80
  20302. /* From a NIST PR=true test vector */
  20303. static const unsigned char entropy_pr[] = {
  20304. 0xa0, 0xc9, 0xab, 0x58, 0xf1, 0xe2, 0xe5, 0xa4, 0xde, 0x3e, 0xbd, 0x4f,
  20305. 0xf7, 0x3e, 0x9c, 0x5b, 0x64, 0xef, 0xd8, 0xca, 0x02, 0x8c, 0xf8, 0x11,
  20306. 0x48, 0xa5, 0x84, 0xfe, 0x69, 0xab, 0x5a, 0xee, 0x42, 0xaa, 0x4d, 0x42,
  20307. 0x17, 0x60, 0x99, 0xd4, 0x5e, 0x13, 0x97, 0xdc, 0x40, 0x4d, 0x86, 0xa3,
  20308. 0x7b, 0xf5, 0x59, 0x54, 0x75, 0x69, 0x51, 0xe4 };
  20309. static const unsigned char result_pr[OUTPUT_LEN] = {
  20310. 0x9a, 0x00, 0xa2, 0xd0, 0x0e, 0xd5, 0x9b, 0xfe, 0x31, 0xec, 0xb1, 0x39,
  20311. 0x9b, 0x60, 0x81, 0x48, 0xd1, 0x96, 0x9d, 0x25, 0x0d, 0x3c, 0x1e, 0x94,
  20312. 0x10, 0x10, 0x98, 0x12, 0x93, 0x25, 0xca, 0xb8, 0xfc, 0xcc, 0x2d, 0x54,
  20313. 0x73, 0x19, 0x70, 0xc0, 0x10, 0x7a, 0xa4, 0x89, 0x25, 0x19, 0x95, 0x5e,
  20314. 0x4b, 0xc6, 0x00, 0x1d, 0x7f, 0x4e, 0x6a, 0x2b, 0xf8, 0xa3, 0x01, 0xab,
  20315. 0x46, 0x05, 0x5c, 0x09, 0xa6, 0x71, 0x88, 0xf1, 0xa7, 0x40, 0xee, 0xf3,
  20316. 0xe1, 0x5c, 0x02, 0x9b, 0x44, 0xaf, 0x03, 0x44 };
  20317. /* From a NIST PR=false test vector */
  20318. static const unsigned char entropy_nopr[] = {
  20319. 0x79, 0x34, 0x9b, 0xbf, 0x7c, 0xdd, 0xa5, 0x79, 0x95, 0x57, 0x86, 0x66,
  20320. 0x21, 0xc9, 0x13, 0x83, 0x11, 0x46, 0x73, 0x3a, 0xbf, 0x8c, 0x35, 0xc8,
  20321. 0xc7, 0x21, 0x5b, 0x5b, 0x96, 0xc4, 0x8e, 0x9b, 0x33, 0x8c, 0x74, 0xe3,
  20322. 0xe9, 0x9d, 0xfe, 0xdf };
  20323. static const unsigned char result_nopr[OUTPUT_LEN] = {
  20324. 0xc6, 0xa1, 0x6a, 0xb8, 0xd4, 0x20, 0x70, 0x6f, 0x0f, 0x34, 0xab, 0x7f,
  20325. 0xec, 0x5a, 0xdc, 0xa9, 0xd8, 0xca, 0x3a, 0x13, 0x3e, 0x15, 0x9c, 0xa6,
  20326. 0xac, 0x43, 0xc6, 0xf8, 0xa2, 0xbe, 0x22, 0x83, 0x4a, 0x4c, 0x0a, 0x0a,
  20327. 0xff, 0xb1, 0x0d, 0x71, 0x94, 0xf1, 0xc1, 0xa5, 0xcf, 0x73, 0x22, 0xec,
  20328. 0x1a, 0xe0, 0x96, 0x4e, 0xd4, 0xbf, 0x12, 0x27, 0x46, 0xe0, 0x87, 0xfd,
  20329. 0xb5, 0xb3, 0xe9, 0x1b, 0x34, 0x93, 0xd5, 0xbb, 0x98, 0xfa, 0xed, 0x49,
  20330. 0xe8, 0x5f, 0x13, 0x0f, 0xc8, 0xa4, 0x59, 0xb7 };
  20331. /* "Entropy" from buffer */
  20332. static size_t test_offset;
  20333. static int hmac_drbg_self_test_entropy( void *data,
  20334. unsigned char *buf, size_t len )
  20335. {
  20336. const unsigned char *p = data;
  20337. memcpy( buf, p + test_offset, len );
  20338. test_offset += len;
  20339. return( 0 );
  20340. }
  20341. #define CHK( c ) if( (c) != 0 ) \
  20342. { \
  20343. if( verbose != 0 ) \
  20344. mbedtls_printf( "failed\n" ); \
  20345. return( 1 ); \
  20346. }
  20347. /*
  20348. * Checkup routine for HMAC_DRBG with SHA-1
  20349. */
  20350. int mbedtls_hmac_drbg_self_test( int verbose )
  20351. {
  20352. mbedtls_hmac_drbg_context ctx;
  20353. unsigned char buf[OUTPUT_LEN];
  20354. const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
  20355. mbedtls_hmac_drbg_init( &ctx );
  20356. /*
  20357. * PR = True
  20358. */
  20359. if( verbose != 0 )
  20360. mbedtls_printf( " HMAC_DRBG (PR = True) : " );
  20361. test_offset = 0;
  20362. CHK( mbedtls_hmac_drbg_seed( &ctx, md_info,
  20363. hmac_drbg_self_test_entropy, (void *) entropy_pr,
  20364. NULL, 0 ) );
  20365. mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON );
  20366. CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  20367. CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  20368. CHK( memcmp( buf, result_pr, OUTPUT_LEN ) );
  20369. mbedtls_hmac_drbg_free( &ctx );
  20370. mbedtls_hmac_drbg_free( &ctx );
  20371. if( verbose != 0 )
  20372. mbedtls_printf( "passed\n" );
  20373. /*
  20374. * PR = False
  20375. */
  20376. if( verbose != 0 )
  20377. mbedtls_printf( " HMAC_DRBG (PR = False) : " );
  20378. mbedtls_hmac_drbg_init( &ctx );
  20379. test_offset = 0;
  20380. CHK( mbedtls_hmac_drbg_seed( &ctx, md_info,
  20381. hmac_drbg_self_test_entropy, (void *) entropy_nopr,
  20382. NULL, 0 ) );
  20383. CHK( mbedtls_hmac_drbg_reseed( &ctx, NULL, 0 ) );
  20384. CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  20385. CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  20386. CHK( memcmp( buf, result_nopr, OUTPUT_LEN ) );
  20387. mbedtls_hmac_drbg_free( &ctx );
  20388. mbedtls_hmac_drbg_free( &ctx );
  20389. if( verbose != 0 )
  20390. mbedtls_printf( "passed\n" );
  20391. if( verbose != 0 )
  20392. mbedtls_printf( "\n" );
  20393. return( 0 );
  20394. }
  20395. #endif /* MBEDTLS_SHA1_C */
  20396. #endif /* MBEDTLS_SELF_TEST */
  20397. #endif /* MBEDTLS_HMAC_DRBG_C */
  20398. /*
  20399. Amalgamated build undefines
  20400. */
  20401. #undef ADD
  20402. #undef BC
  20403. #undef BEFORE_COLON
  20404. #undef F
  20405. #undef F0
  20406. #undef F1
  20407. #undef F2
  20408. #undef F3
  20409. #undef F4
  20410. #undef F5
  20411. #undef FSb
  20412. #undef K
  20413. #undef KK
  20414. #undef P
  20415. #undef R
  20416. #undef ROTR
  20417. #undef S
  20418. #undef S0
  20419. #undef S1
  20420. #undef S2
  20421. #undef S3
  20422. #undef SAFE_SNPRINTF
  20423. #undef SHR
  20424. #undef close
  20425. #undef read
  20426. #undef supported_init
  20427. #undef write
  20428. /********* Start of file library/md.c ************/
  20429. /**
  20430. * \file mbedtls_md.c
  20431. *
  20432. * \brief Generic message digest wrapper for mbed TLS
  20433. *
  20434. * \author Adriaan de Jong <dejong@fox-it.com>
  20435. *
  20436. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  20437. * SPDX-License-Identifier: Apache-2.0
  20438. *
  20439. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  20440. * not use this file except in compliance with the License.
  20441. * You may obtain a copy of the License at
  20442. *
  20443. * http://www.apache.org/licenses/LICENSE-2.0
  20444. *
  20445. * Unless required by applicable law or agreed to in writing, software
  20446. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  20447. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20448. * See the License for the specific language governing permissions and
  20449. * limitations under the License.
  20450. *
  20451. * This file is part of mbed TLS (https://tls.mbed.org)
  20452. */
  20453. #if !defined(MBEDTLS_CONFIG_FILE)
  20454. #else
  20455. #endif
  20456. #if defined(MBEDTLS_MD_C)
  20457. #if defined(MBEDTLS_PLATFORM_C)
  20458. #else
  20459. #include <stdlib.h>
  20460. #define mbedtls_calloc calloc
  20461. #define mbedtls_free free
  20462. #endif
  20463. #include <string.h>
  20464. #if defined(MBEDTLS_FS_IO)
  20465. #include <stdio.h>
  20466. #endif
  20467. /* Implementation that should never be optimized out by the compiler */
  20468. /* zeroize was here */
  20469. /*
  20470. * Reminder: update profiles in x509_crt.c when adding a new hash!
  20471. */
  20472. static const int supported_digests[] = {
  20473. #if defined(MBEDTLS_SHA512_C)
  20474. MBEDTLS_MD_SHA512,
  20475. MBEDTLS_MD_SHA384,
  20476. #endif
  20477. #if defined(MBEDTLS_SHA256_C)
  20478. MBEDTLS_MD_SHA256,
  20479. MBEDTLS_MD_SHA224,
  20480. #endif
  20481. #if defined(MBEDTLS_SHA1_C)
  20482. MBEDTLS_MD_SHA1,
  20483. #endif
  20484. #if defined(MBEDTLS_RIPEMD160_C)
  20485. MBEDTLS_MD_RIPEMD160,
  20486. #endif
  20487. #if defined(MBEDTLS_MD5_C)
  20488. MBEDTLS_MD_MD5,
  20489. #endif
  20490. #if defined(MBEDTLS_MD4_C)
  20491. MBEDTLS_MD_MD4,
  20492. #endif
  20493. #if defined(MBEDTLS_MD2_C)
  20494. MBEDTLS_MD_MD2,
  20495. #endif
  20496. MBEDTLS_MD_NONE
  20497. };
  20498. const int *mbedtls_md_list( void )
  20499. {
  20500. return( supported_digests );
  20501. }
  20502. const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
  20503. {
  20504. if( NULL == md_name )
  20505. return( NULL );
  20506. /* Get the appropriate digest information */
  20507. #if defined(MBEDTLS_MD2_C)
  20508. if( !strcmp( "MD2", md_name ) )
  20509. return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
  20510. #endif
  20511. #if defined(MBEDTLS_MD4_C)
  20512. if( !strcmp( "MD4", md_name ) )
  20513. return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
  20514. #endif
  20515. #if defined(MBEDTLS_MD5_C)
  20516. if( !strcmp( "MD5", md_name ) )
  20517. return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
  20518. #endif
  20519. #if defined(MBEDTLS_RIPEMD160_C)
  20520. if( !strcmp( "RIPEMD160", md_name ) )
  20521. return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
  20522. #endif
  20523. #if defined(MBEDTLS_SHA1_C)
  20524. if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
  20525. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
  20526. #endif
  20527. #if defined(MBEDTLS_SHA256_C)
  20528. if( !strcmp( "SHA224", md_name ) )
  20529. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
  20530. if( !strcmp( "SHA256", md_name ) )
  20531. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
  20532. #endif
  20533. #if defined(MBEDTLS_SHA512_C)
  20534. if( !strcmp( "SHA384", md_name ) )
  20535. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
  20536. if( !strcmp( "SHA512", md_name ) )
  20537. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
  20538. #endif
  20539. return( NULL );
  20540. }
  20541. const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
  20542. {
  20543. switch( md_type )
  20544. {
  20545. #if defined(MBEDTLS_MD2_C)
  20546. case MBEDTLS_MD_MD2:
  20547. return( &mbedtls_md2_info );
  20548. #endif
  20549. #if defined(MBEDTLS_MD4_C)
  20550. case MBEDTLS_MD_MD4:
  20551. return( &mbedtls_md4_info );
  20552. #endif
  20553. #if defined(MBEDTLS_MD5_C)
  20554. case MBEDTLS_MD_MD5:
  20555. return( &mbedtls_md5_info );
  20556. #endif
  20557. #if defined(MBEDTLS_RIPEMD160_C)
  20558. case MBEDTLS_MD_RIPEMD160:
  20559. return( &mbedtls_ripemd160_info );
  20560. #endif
  20561. #if defined(MBEDTLS_SHA1_C)
  20562. case MBEDTLS_MD_SHA1:
  20563. return( &mbedtls_sha1_info );
  20564. #endif
  20565. #if defined(MBEDTLS_SHA256_C)
  20566. case MBEDTLS_MD_SHA224:
  20567. return( &mbedtls_sha224_info );
  20568. case MBEDTLS_MD_SHA256:
  20569. return( &mbedtls_sha256_info );
  20570. #endif
  20571. #if defined(MBEDTLS_SHA512_C)
  20572. case MBEDTLS_MD_SHA384:
  20573. return( &mbedtls_sha384_info );
  20574. case MBEDTLS_MD_SHA512:
  20575. return( &mbedtls_sha512_info );
  20576. #endif
  20577. default:
  20578. return( NULL );
  20579. }
  20580. }
  20581. void mbedtls_md_init( mbedtls_md_context_t *ctx )
  20582. {
  20583. memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
  20584. }
  20585. void mbedtls_md_free( mbedtls_md_context_t *ctx )
  20586. {
  20587. if( ctx == NULL || ctx->md_info == NULL )
  20588. return;
  20589. if( ctx->md_ctx != NULL )
  20590. ctx->md_info->ctx_free_func( ctx->md_ctx );
  20591. if( ctx->hmac_ctx != NULL )
  20592. {
  20593. mbedtls_zeroize( ctx->hmac_ctx, 2 * ctx->md_info->block_size );
  20594. mbedtls_free( ctx->hmac_ctx );
  20595. }
  20596. mbedtls_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
  20597. }
  20598. int mbedtls_md_clone( mbedtls_md_context_t *dst,
  20599. const mbedtls_md_context_t *src )
  20600. {
  20601. if( dst == NULL || dst->md_info == NULL ||
  20602. src == NULL || src->md_info == NULL ||
  20603. dst->md_info != src->md_info )
  20604. {
  20605. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  20606. }
  20607. dst->md_info->clone_func( dst->md_ctx, src->md_ctx );
  20608. return( 0 );
  20609. }
  20610. #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
  20611. int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info )
  20612. {
  20613. return mbedtls_md_setup( ctx, md_info, 1 );
  20614. }
  20615. #endif
  20616. int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
  20617. {
  20618. if( md_info == NULL || ctx == NULL )
  20619. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  20620. if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
  20621. return( MBEDTLS_ERR_MD_ALLOC_FAILED );
  20622. if( hmac != 0 )
  20623. {
  20624. ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
  20625. if( ctx->hmac_ctx == NULL )
  20626. {
  20627. md_info->ctx_free_func( ctx->md_ctx );
  20628. return( MBEDTLS_ERR_MD_ALLOC_FAILED );
  20629. }
  20630. }
  20631. ctx->md_info = md_info;
  20632. return( 0 );
  20633. }
  20634. int mbedtls_md_starts( mbedtls_md_context_t *ctx )
  20635. {
  20636. if( ctx == NULL || ctx->md_info == NULL )
  20637. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  20638. return( ctx->md_info->starts_func( ctx->md_ctx ) );
  20639. }
  20640. int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
  20641. {
  20642. if( ctx == NULL || ctx->md_info == NULL )
  20643. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  20644. return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
  20645. }
  20646. int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
  20647. {
  20648. if( ctx == NULL || ctx->md_info == NULL )
  20649. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  20650. return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
  20651. }
  20652. int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
  20653. unsigned char *output )
  20654. {
  20655. if( md_info == NULL )
  20656. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  20657. return( md_info->digest_func( input, ilen, output ) );
  20658. }
  20659. #if defined(MBEDTLS_FS_IO)
  20660. int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
  20661. {
  20662. int ret;
  20663. FILE *f;
  20664. size_t n;
  20665. mbedtls_md_context_t ctx;
  20666. unsigned char buf[1024];
  20667. if( md_info == NULL )
  20668. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  20669. if( ( f = fopen( path, "rb" ) ) == NULL )
  20670. return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
  20671. mbedtls_md_init( &ctx );
  20672. if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
  20673. goto cleanup;
  20674. if( ( ret = md_info->starts_func( ctx.md_ctx ) ) != 0 )
  20675. goto cleanup;
  20676. while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
  20677. if( ( ret = md_info->update_func( ctx.md_ctx, buf, n ) ) != 0 )
  20678. goto cleanup;
  20679. if( ferror( f ) != 0 )
  20680. ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
  20681. else
  20682. ret = md_info->finish_func( ctx.md_ctx, output );
  20683. cleanup:
  20684. mbedtls_zeroize( buf, sizeof( buf ) );
  20685. fclose( f );
  20686. mbedtls_md_free( &ctx );
  20687. return( ret );
  20688. }
  20689. #endif /* MBEDTLS_FS_IO */
  20690. int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
  20691. {
  20692. int ret;
  20693. unsigned char sum[MBEDTLS_MD_MAX_SIZE];
  20694. unsigned char *ipad, *opad;
  20695. size_t i;
  20696. if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
  20697. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  20698. if( keylen > (size_t) ctx->md_info->block_size )
  20699. {
  20700. if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
  20701. goto cleanup;
  20702. if( ( ret = ctx->md_info->update_func( ctx->md_ctx, key, keylen ) ) != 0 )
  20703. goto cleanup;
  20704. if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, sum ) ) != 0 )
  20705. goto cleanup;
  20706. keylen = ctx->md_info->size;
  20707. key = sum;
  20708. }
  20709. ipad = (unsigned char *) ctx->hmac_ctx;
  20710. opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
  20711. memset( ipad, 0x36, ctx->md_info->block_size );
  20712. memset( opad, 0x5C, ctx->md_info->block_size );
  20713. for( i = 0; i < keylen; i++ )
  20714. {
  20715. ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
  20716. opad[i] = (unsigned char)( opad[i] ^ key[i] );
  20717. }
  20718. if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
  20719. goto cleanup;
  20720. if( ( ret = ctx->md_info->update_func( ctx->md_ctx, ipad,
  20721. ctx->md_info->block_size ) ) != 0 )
  20722. goto cleanup;
  20723. cleanup:
  20724. mbedtls_zeroize( sum, sizeof( sum ) );
  20725. return( ret );
  20726. }
  20727. int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
  20728. {
  20729. if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
  20730. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  20731. return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
  20732. }
  20733. int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
  20734. {
  20735. int ret;
  20736. unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
  20737. unsigned char *opad;
  20738. if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
  20739. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  20740. opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
  20741. if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, tmp ) ) != 0 )
  20742. return( ret );
  20743. if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
  20744. return( ret );
  20745. if( ( ret = ctx->md_info->update_func( ctx->md_ctx, opad,
  20746. ctx->md_info->block_size ) ) != 0 )
  20747. return( ret );
  20748. if( ( ret = ctx->md_info->update_func( ctx->md_ctx, tmp,
  20749. ctx->md_info->size ) ) != 0 )
  20750. return( ret );
  20751. return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
  20752. }
  20753. int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
  20754. {
  20755. int ret;
  20756. unsigned char *ipad;
  20757. if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
  20758. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  20759. ipad = (unsigned char *) ctx->hmac_ctx;
  20760. if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
  20761. return( ret );
  20762. return( ctx->md_info->update_func( ctx->md_ctx, ipad,
  20763. ctx->md_info->block_size ) );
  20764. }
  20765. int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
  20766. const unsigned char *key, size_t keylen,
  20767. const unsigned char *input, size_t ilen,
  20768. unsigned char *output )
  20769. {
  20770. mbedtls_md_context_t ctx;
  20771. int ret;
  20772. if( md_info == NULL )
  20773. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  20774. mbedtls_md_init( &ctx );
  20775. if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
  20776. goto cleanup;
  20777. if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
  20778. goto cleanup;
  20779. if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
  20780. goto cleanup;
  20781. if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
  20782. goto cleanup;
  20783. cleanup:
  20784. mbedtls_md_free( &ctx );
  20785. return( ret );
  20786. }
  20787. int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
  20788. {
  20789. if( ctx == NULL || ctx->md_info == NULL )
  20790. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  20791. return( ctx->md_info->process_func( ctx->md_ctx, data ) );
  20792. }
  20793. unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
  20794. {
  20795. if( md_info == NULL )
  20796. return( 0 );
  20797. return md_info->size;
  20798. }
  20799. mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
  20800. {
  20801. if( md_info == NULL )
  20802. return( MBEDTLS_MD_NONE );
  20803. return md_info->type;
  20804. }
  20805. const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
  20806. {
  20807. if( md_info == NULL )
  20808. return( NULL );
  20809. return md_info->name;
  20810. }
  20811. #endif /* MBEDTLS_MD_C */
  20812. /*
  20813. Amalgamated build undefines
  20814. */
  20815. #undef ADD
  20816. #undef BC
  20817. #undef BEFORE_COLON
  20818. #undef F
  20819. #undef F0
  20820. #undef F1
  20821. #undef F2
  20822. #undef F3
  20823. #undef F4
  20824. #undef F5
  20825. #undef FSb
  20826. #undef K
  20827. #undef KK
  20828. #undef P
  20829. #undef R
  20830. #undef ROTR
  20831. #undef S
  20832. #undef S0
  20833. #undef S1
  20834. #undef S2
  20835. #undef S3
  20836. #undef SAFE_SNPRINTF
  20837. #undef SHR
  20838. #undef close
  20839. #undef read
  20840. #undef supported_init
  20841. #undef write
  20842. /********* Start of file library/md2.c ************/
  20843. /*
  20844. * RFC 1115/1319 compliant MD2 implementation
  20845. *
  20846. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  20847. * SPDX-License-Identifier: Apache-2.0
  20848. *
  20849. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  20850. * not use this file except in compliance with the License.
  20851. * You may obtain a copy of the License at
  20852. *
  20853. * http://www.apache.org/licenses/LICENSE-2.0
  20854. *
  20855. * Unless required by applicable law or agreed to in writing, software
  20856. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  20857. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20858. * See the License for the specific language governing permissions and
  20859. * limitations under the License.
  20860. *
  20861. * This file is part of mbed TLS (https://tls.mbed.org)
  20862. */
  20863. /*
  20864. * The MD2 algorithm was designed by Ron Rivest in 1989.
  20865. *
  20866. * http://www.ietf.org/rfc/rfc1115.txt
  20867. * http://www.ietf.org/rfc/rfc1319.txt
  20868. */
  20869. #if !defined(MBEDTLS_CONFIG_FILE)
  20870. #else
  20871. #endif
  20872. #if defined(MBEDTLS_MD2_C)
  20873. #include <string.h>
  20874. #if defined(MBEDTLS_SELF_TEST)
  20875. #if defined(MBEDTLS_PLATFORM_C)
  20876. #else
  20877. #include <stdio.h>
  20878. #define mbedtls_printf printf
  20879. #endif /* MBEDTLS_PLATFORM_C */
  20880. #endif /* MBEDTLS_SELF_TEST */
  20881. #if !defined(MBEDTLS_MD2_ALT)
  20882. /* Implementation that should never be optimized out by the compiler */
  20883. /* zeroize was here */
  20884. static const unsigned char PI_SUBST[256] =
  20885. {
  20886. 0x29, 0x2E, 0x43, 0xC9, 0xA2, 0xD8, 0x7C, 0x01, 0x3D, 0x36,
  20887. 0x54, 0xA1, 0xEC, 0xF0, 0x06, 0x13, 0x62, 0xA7, 0x05, 0xF3,
  20888. 0xC0, 0xC7, 0x73, 0x8C, 0x98, 0x93, 0x2B, 0xD9, 0xBC, 0x4C,
  20889. 0x82, 0xCA, 0x1E, 0x9B, 0x57, 0x3C, 0xFD, 0xD4, 0xE0, 0x16,
  20890. 0x67, 0x42, 0x6F, 0x18, 0x8A, 0x17, 0xE5, 0x12, 0xBE, 0x4E,
  20891. 0xC4, 0xD6, 0xDA, 0x9E, 0xDE, 0x49, 0xA0, 0xFB, 0xF5, 0x8E,
  20892. 0xBB, 0x2F, 0xEE, 0x7A, 0xA9, 0x68, 0x79, 0x91, 0x15, 0xB2,
  20893. 0x07, 0x3F, 0x94, 0xC2, 0x10, 0x89, 0x0B, 0x22, 0x5F, 0x21,
  20894. 0x80, 0x7F, 0x5D, 0x9A, 0x5A, 0x90, 0x32, 0x27, 0x35, 0x3E,
  20895. 0xCC, 0xE7, 0xBF, 0xF7, 0x97, 0x03, 0xFF, 0x19, 0x30, 0xB3,
  20896. 0x48, 0xA5, 0xB5, 0xD1, 0xD7, 0x5E, 0x92, 0x2A, 0xAC, 0x56,
  20897. 0xAA, 0xC6, 0x4F, 0xB8, 0x38, 0xD2, 0x96, 0xA4, 0x7D, 0xB6,
  20898. 0x76, 0xFC, 0x6B, 0xE2, 0x9C, 0x74, 0x04, 0xF1, 0x45, 0x9D,
  20899. 0x70, 0x59, 0x64, 0x71, 0x87, 0x20, 0x86, 0x5B, 0xCF, 0x65,
  20900. 0xE6, 0x2D, 0xA8, 0x02, 0x1B, 0x60, 0x25, 0xAD, 0xAE, 0xB0,
  20901. 0xB9, 0xF6, 0x1C, 0x46, 0x61, 0x69, 0x34, 0x40, 0x7E, 0x0F,
  20902. 0x55, 0x47, 0xA3, 0x23, 0xDD, 0x51, 0xAF, 0x3A, 0xC3, 0x5C,
  20903. 0xF9, 0xCE, 0xBA, 0xC5, 0xEA, 0x26, 0x2C, 0x53, 0x0D, 0x6E,
  20904. 0x85, 0x28, 0x84, 0x09, 0xD3, 0xDF, 0xCD, 0xF4, 0x41, 0x81,
  20905. 0x4D, 0x52, 0x6A, 0xDC, 0x37, 0xC8, 0x6C, 0xC1, 0xAB, 0xFA,
  20906. 0x24, 0xE1, 0x7B, 0x08, 0x0C, 0xBD, 0xB1, 0x4A, 0x78, 0x88,
  20907. 0x95, 0x8B, 0xE3, 0x63, 0xE8, 0x6D, 0xE9, 0xCB, 0xD5, 0xFE,
  20908. 0x3B, 0x00, 0x1D, 0x39, 0xF2, 0xEF, 0xB7, 0x0E, 0x66, 0x58,
  20909. 0xD0, 0xE4, 0xA6, 0x77, 0x72, 0xF8, 0xEB, 0x75, 0x4B, 0x0A,
  20910. 0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A, 0xDB, 0x99,
  20911. 0x8D, 0x33, 0x9F, 0x11, 0x83, 0x14
  20912. };
  20913. void mbedtls_md2_init( mbedtls_md2_context *ctx )
  20914. {
  20915. memset( ctx, 0, sizeof( mbedtls_md2_context ) );
  20916. }
  20917. void mbedtls_md2_free( mbedtls_md2_context *ctx )
  20918. {
  20919. if( ctx == NULL )
  20920. return;
  20921. mbedtls_zeroize( ctx, sizeof( mbedtls_md2_context ) );
  20922. }
  20923. void mbedtls_md2_clone( mbedtls_md2_context *dst,
  20924. const mbedtls_md2_context *src )
  20925. {
  20926. *dst = *src;
  20927. }
  20928. /*
  20929. * MD2 context setup
  20930. */
  20931. int mbedtls_md2_starts_ret( mbedtls_md2_context *ctx )
  20932. {
  20933. memset( ctx->cksum, 0, 16 );
  20934. memset( ctx->state, 0, 46 );
  20935. memset( ctx->buffer, 0, 16 );
  20936. ctx->left = 0;
  20937. return( 0 );
  20938. }
  20939. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  20940. void mbedtls_md2_starts( mbedtls_md2_context *ctx )
  20941. {
  20942. mbedtls_md2_starts_ret( ctx );
  20943. }
  20944. #endif
  20945. #if !defined(MBEDTLS_MD2_PROCESS_ALT)
  20946. int mbedtls_internal_md2_process( mbedtls_md2_context *ctx )
  20947. {
  20948. int i, j;
  20949. unsigned char t = 0;
  20950. for( i = 0; i < 16; i++ )
  20951. {
  20952. ctx->state[i + 16] = ctx->buffer[i];
  20953. ctx->state[i + 32] =
  20954. (unsigned char)( ctx->buffer[i] ^ ctx->state[i]);
  20955. }
  20956. for( i = 0; i < 18; i++ )
  20957. {
  20958. for( j = 0; j < 48; j++ )
  20959. {
  20960. ctx->state[j] = (unsigned char)
  20961. ( ctx->state[j] ^ PI_SUBST[t] );
  20962. t = ctx->state[j];
  20963. }
  20964. t = (unsigned char)( t + i );
  20965. }
  20966. t = ctx->cksum[15];
  20967. for( i = 0; i < 16; i++ )
  20968. {
  20969. ctx->cksum[i] = (unsigned char)
  20970. ( ctx->cksum[i] ^ PI_SUBST[ctx->buffer[i] ^ t] );
  20971. t = ctx->cksum[i];
  20972. }
  20973. return( 0 );
  20974. }
  20975. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  20976. void mbedtls_md2_process( mbedtls_md2_context *ctx )
  20977. {
  20978. mbedtls_internal_md2_process( ctx );
  20979. }
  20980. #endif
  20981. #endif /* !MBEDTLS_MD2_PROCESS_ALT */
  20982. /*
  20983. * MD2 process buffer
  20984. */
  20985. int mbedtls_md2_update_ret( mbedtls_md2_context *ctx,
  20986. const unsigned char *input,
  20987. size_t ilen )
  20988. {
  20989. int ret;
  20990. size_t fill;
  20991. while( ilen > 0 )
  20992. {
  20993. if( ilen > 16 - ctx->left )
  20994. fill = 16 - ctx->left;
  20995. else
  20996. fill = ilen;
  20997. memcpy( ctx->buffer + ctx->left, input, fill );
  20998. ctx->left += fill;
  20999. input += fill;
  21000. ilen -= fill;
  21001. if( ctx->left == 16 )
  21002. {
  21003. ctx->left = 0;
  21004. if( ( ret = mbedtls_internal_md2_process( ctx ) ) != 0 )
  21005. return( ret );
  21006. }
  21007. }
  21008. return( 0 );
  21009. }
  21010. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  21011. void mbedtls_md2_update( mbedtls_md2_context *ctx,
  21012. const unsigned char *input,
  21013. size_t ilen )
  21014. {
  21015. mbedtls_md2_update_ret( ctx, input, ilen );
  21016. }
  21017. #endif
  21018. /*
  21019. * MD2 final digest
  21020. */
  21021. int mbedtls_md2_finish_ret( mbedtls_md2_context *ctx,
  21022. unsigned char output[16] )
  21023. {
  21024. int ret;
  21025. size_t i;
  21026. unsigned char x;
  21027. x = (unsigned char)( 16 - ctx->left );
  21028. for( i = ctx->left; i < 16; i++ )
  21029. ctx->buffer[i] = x;
  21030. if( ( ret = mbedtls_internal_md2_process( ctx ) ) != 0 )
  21031. return( ret );
  21032. memcpy( ctx->buffer, ctx->cksum, 16 );
  21033. if( ( ret = mbedtls_internal_md2_process( ctx ) ) != 0 )
  21034. return( ret );
  21035. memcpy( output, ctx->state, 16 );
  21036. return( 0 );
  21037. }
  21038. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  21039. void mbedtls_md2_finish( mbedtls_md2_context *ctx,
  21040. unsigned char output[16] )
  21041. {
  21042. mbedtls_md2_finish_ret( ctx, output );
  21043. }
  21044. #endif
  21045. #endif /* !MBEDTLS_MD2_ALT */
  21046. /*
  21047. * output = MD2( input buffer )
  21048. */
  21049. int mbedtls_md2_ret( const unsigned char *input,
  21050. size_t ilen,
  21051. unsigned char output[16] )
  21052. {
  21053. int ret;
  21054. mbedtls_md2_context ctx;
  21055. mbedtls_md2_init( &ctx );
  21056. if( ( ret = mbedtls_md2_starts_ret( &ctx ) ) != 0 )
  21057. goto exit;
  21058. if( ( ret = mbedtls_md2_update_ret( &ctx, input, ilen ) ) != 0 )
  21059. goto exit;
  21060. if( ( ret = mbedtls_md2_finish_ret( &ctx, output ) ) != 0 )
  21061. goto exit;
  21062. exit:
  21063. mbedtls_md2_free( &ctx );
  21064. return( ret );
  21065. }
  21066. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  21067. void mbedtls_md2( const unsigned char *input,
  21068. size_t ilen,
  21069. unsigned char output[16] )
  21070. {
  21071. mbedtls_md2_ret( input, ilen, output );
  21072. }
  21073. #endif
  21074. #if defined(MBEDTLS_SELF_TEST)
  21075. /*
  21076. * RFC 1319 test vectors
  21077. */
  21078. static const unsigned char md2_test_str[7][81] =
  21079. {
  21080. { "" },
  21081. { "a" },
  21082. { "abc" },
  21083. { "message digest" },
  21084. { "abcdefghijklmnopqrstuvwxyz" },
  21085. { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
  21086. { "12345678901234567890123456789012345678901234567890123456789012"
  21087. "345678901234567890" }
  21088. };
  21089. static const size_t md2_test_strlen[7] =
  21090. {
  21091. 0, 1, 3, 14, 26, 62, 80
  21092. };
  21093. static const unsigned char md2_test_sum[7][16] =
  21094. {
  21095. { 0x83, 0x50, 0xE5, 0xA3, 0xE2, 0x4C, 0x15, 0x3D,
  21096. 0xF2, 0x27, 0x5C, 0x9F, 0x80, 0x69, 0x27, 0x73 },
  21097. { 0x32, 0xEC, 0x01, 0xEC, 0x4A, 0x6D, 0xAC, 0x72,
  21098. 0xC0, 0xAB, 0x96, 0xFB, 0x34, 0xC0, 0xB5, 0xD1 },
  21099. { 0xDA, 0x85, 0x3B, 0x0D, 0x3F, 0x88, 0xD9, 0x9B,
  21100. 0x30, 0x28, 0x3A, 0x69, 0xE6, 0xDE, 0xD6, 0xBB },
  21101. { 0xAB, 0x4F, 0x49, 0x6B, 0xFB, 0x2A, 0x53, 0x0B,
  21102. 0x21, 0x9F, 0xF3, 0x30, 0x31, 0xFE, 0x06, 0xB0 },
  21103. { 0x4E, 0x8D, 0xDF, 0xF3, 0x65, 0x02, 0x92, 0xAB,
  21104. 0x5A, 0x41, 0x08, 0xC3, 0xAA, 0x47, 0x94, 0x0B },
  21105. { 0xDA, 0x33, 0xDE, 0xF2, 0xA4, 0x2D, 0xF1, 0x39,
  21106. 0x75, 0x35, 0x28, 0x46, 0xC3, 0x03, 0x38, 0xCD },
  21107. { 0xD5, 0x97, 0x6F, 0x79, 0xD8, 0x3D, 0x3A, 0x0D,
  21108. 0xC9, 0x80, 0x6C, 0x3C, 0x66, 0xF3, 0xEF, 0xD8 }
  21109. };
  21110. /*
  21111. * Checkup routine
  21112. */
  21113. int mbedtls_md2_self_test( int verbose )
  21114. {
  21115. int i, ret = 0;
  21116. unsigned char md2sum[16];
  21117. for( i = 0; i < 7; i++ )
  21118. {
  21119. if( verbose != 0 )
  21120. mbedtls_printf( " MD2 test #%d: ", i + 1 );
  21121. ret = mbedtls_md2_ret( md2_test_str[i], md2_test_strlen[i], md2sum );
  21122. if( ret != 0 )
  21123. goto fail;
  21124. if( memcmp( md2sum, md2_test_sum[i], 16 ) != 0 )
  21125. {
  21126. ret = 1;
  21127. goto fail;
  21128. }
  21129. if( verbose != 0 )
  21130. mbedtls_printf( "passed\n" );
  21131. }
  21132. if( verbose != 0 )
  21133. mbedtls_printf( "\n" );
  21134. return( 0 );
  21135. fail:
  21136. if( verbose != 0 )
  21137. mbedtls_printf( "failed\n" );
  21138. return( ret );
  21139. }
  21140. #endif /* MBEDTLS_SELF_TEST */
  21141. #endif /* MBEDTLS_MD2_C */
  21142. /*
  21143. Amalgamated build undefines
  21144. */
  21145. #undef ADD
  21146. #undef BC
  21147. #undef BEFORE_COLON
  21148. #undef F
  21149. #undef F0
  21150. #undef F1
  21151. #undef F2
  21152. #undef F3
  21153. #undef F4
  21154. #undef F5
  21155. #undef FSb
  21156. #undef K
  21157. #undef KK
  21158. #undef P
  21159. #undef R
  21160. #undef ROTR
  21161. #undef S
  21162. #undef S0
  21163. #undef S1
  21164. #undef S2
  21165. #undef S3
  21166. #undef SAFE_SNPRINTF
  21167. #undef SHR
  21168. #undef close
  21169. #undef read
  21170. #undef supported_init
  21171. #undef write
  21172. /********* Start of file library/md4.c ************/
  21173. /*
  21174. * RFC 1186/1320 compliant MD4 implementation
  21175. *
  21176. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  21177. * SPDX-License-Identifier: Apache-2.0
  21178. *
  21179. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  21180. * not use this file except in compliance with the License.
  21181. * You may obtain a copy of the License at
  21182. *
  21183. * http://www.apache.org/licenses/LICENSE-2.0
  21184. *
  21185. * Unless required by applicable law or agreed to in writing, software
  21186. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21187. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21188. * See the License for the specific language governing permissions and
  21189. * limitations under the License.
  21190. *
  21191. * This file is part of mbed TLS (https://tls.mbed.org)
  21192. */
  21193. /*
  21194. * The MD4 algorithm was designed by Ron Rivest in 1990.
  21195. *
  21196. * http://www.ietf.org/rfc/rfc1186.txt
  21197. * http://www.ietf.org/rfc/rfc1320.txt
  21198. */
  21199. #if !defined(MBEDTLS_CONFIG_FILE)
  21200. #else
  21201. #endif
  21202. #if defined(MBEDTLS_MD4_C)
  21203. #include <string.h>
  21204. #if defined(MBEDTLS_SELF_TEST)
  21205. #if defined(MBEDTLS_PLATFORM_C)
  21206. #else
  21207. #include <stdio.h>
  21208. #define mbedtls_printf printf
  21209. #endif /* MBEDTLS_PLATFORM_C */
  21210. #endif /* MBEDTLS_SELF_TEST */
  21211. #if !defined(MBEDTLS_MD4_ALT)
  21212. /* Implementation that should never be optimized out by the compiler */
  21213. /* zeroize was here */
  21214. /*
  21215. * 32-bit integer manipulation macros (little endian)
  21216. */
  21217. #ifndef GET_UINT32_LE
  21218. #define GET_UINT32_LE(n,b,i) \
  21219. { \
  21220. (n) = ( (uint32_t) (b)[(i) ] ) \
  21221. | ( (uint32_t) (b)[(i) + 1] << 8 ) \
  21222. | ( (uint32_t) (b)[(i) + 2] << 16 ) \
  21223. | ( (uint32_t) (b)[(i) + 3] << 24 ); \
  21224. }
  21225. #endif
  21226. #ifndef PUT_UINT32_LE
  21227. #define PUT_UINT32_LE(n,b,i) \
  21228. { \
  21229. (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
  21230. (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
  21231. (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
  21232. (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
  21233. }
  21234. #endif
  21235. void mbedtls_md4_init( mbedtls_md4_context *ctx )
  21236. {
  21237. memset( ctx, 0, sizeof( mbedtls_md4_context ) );
  21238. }
  21239. void mbedtls_md4_free( mbedtls_md4_context *ctx )
  21240. {
  21241. if( ctx == NULL )
  21242. return;
  21243. mbedtls_zeroize( ctx, sizeof( mbedtls_md4_context ) );
  21244. }
  21245. void mbedtls_md4_clone( mbedtls_md4_context *dst,
  21246. const mbedtls_md4_context *src )
  21247. {
  21248. *dst = *src;
  21249. }
  21250. /*
  21251. * MD4 context setup
  21252. */
  21253. int mbedtls_md4_starts_ret( mbedtls_md4_context *ctx )
  21254. {
  21255. ctx->total[0] = 0;
  21256. ctx->total[1] = 0;
  21257. ctx->state[0] = 0x67452301;
  21258. ctx->state[1] = 0xEFCDAB89;
  21259. ctx->state[2] = 0x98BADCFE;
  21260. ctx->state[3] = 0x10325476;
  21261. return( 0 );
  21262. }
  21263. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  21264. void mbedtls_md4_starts( mbedtls_md4_context *ctx )
  21265. {
  21266. mbedtls_md4_starts_ret( ctx );
  21267. }
  21268. #endif
  21269. #if !defined(MBEDTLS_MD4_PROCESS_ALT)
  21270. int mbedtls_internal_md4_process( mbedtls_md4_context *ctx,
  21271. const unsigned char data[64] )
  21272. {
  21273. uint32_t X[16], A, B, C, D;
  21274. GET_UINT32_LE( X[ 0], data, 0 );
  21275. GET_UINT32_LE( X[ 1], data, 4 );
  21276. GET_UINT32_LE( X[ 2], data, 8 );
  21277. GET_UINT32_LE( X[ 3], data, 12 );
  21278. GET_UINT32_LE( X[ 4], data, 16 );
  21279. GET_UINT32_LE( X[ 5], data, 20 );
  21280. GET_UINT32_LE( X[ 6], data, 24 );
  21281. GET_UINT32_LE( X[ 7], data, 28 );
  21282. GET_UINT32_LE( X[ 8], data, 32 );
  21283. GET_UINT32_LE( X[ 9], data, 36 );
  21284. GET_UINT32_LE( X[10], data, 40 );
  21285. GET_UINT32_LE( X[11], data, 44 );
  21286. GET_UINT32_LE( X[12], data, 48 );
  21287. GET_UINT32_LE( X[13], data, 52 );
  21288. GET_UINT32_LE( X[14], data, 56 );
  21289. GET_UINT32_LE( X[15], data, 60 );
  21290. #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  21291. A = ctx->state[0];
  21292. B = ctx->state[1];
  21293. C = ctx->state[2];
  21294. D = ctx->state[3];
  21295. #define F(x, y, z) ((x & y) | ((~x) & z))
  21296. #define P(a,b,c,d,x,s) { a += F(b,c,d) + x; a = S(a,s); }
  21297. P( A, B, C, D, X[ 0], 3 );
  21298. P( D, A, B, C, X[ 1], 7 );
  21299. P( C, D, A, B, X[ 2], 11 );
  21300. P( B, C, D, A, X[ 3], 19 );
  21301. P( A, B, C, D, X[ 4], 3 );
  21302. P( D, A, B, C, X[ 5], 7 );
  21303. P( C, D, A, B, X[ 6], 11 );
  21304. P( B, C, D, A, X[ 7], 19 );
  21305. P( A, B, C, D, X[ 8], 3 );
  21306. P( D, A, B, C, X[ 9], 7 );
  21307. P( C, D, A, B, X[10], 11 );
  21308. P( B, C, D, A, X[11], 19 );
  21309. P( A, B, C, D, X[12], 3 );
  21310. P( D, A, B, C, X[13], 7 );
  21311. P( C, D, A, B, X[14], 11 );
  21312. P( B, C, D, A, X[15], 19 );
  21313. #undef P
  21314. #undef F
  21315. #define F(x,y,z) ((x & y) | (x & z) | (y & z))
  21316. #define P(a,b,c,d,x,s) { a += F(b,c,d) + x + 0x5A827999; a = S(a,s); }
  21317. P( A, B, C, D, X[ 0], 3 );
  21318. P( D, A, B, C, X[ 4], 5 );
  21319. P( C, D, A, B, X[ 8], 9 );
  21320. P( B, C, D, A, X[12], 13 );
  21321. P( A, B, C, D, X[ 1], 3 );
  21322. P( D, A, B, C, X[ 5], 5 );
  21323. P( C, D, A, B, X[ 9], 9 );
  21324. P( B, C, D, A, X[13], 13 );
  21325. P( A, B, C, D, X[ 2], 3 );
  21326. P( D, A, B, C, X[ 6], 5 );
  21327. P( C, D, A, B, X[10], 9 );
  21328. P( B, C, D, A, X[14], 13 );
  21329. P( A, B, C, D, X[ 3], 3 );
  21330. P( D, A, B, C, X[ 7], 5 );
  21331. P( C, D, A, B, X[11], 9 );
  21332. P( B, C, D, A, X[15], 13 );
  21333. #undef P
  21334. #undef F
  21335. #define F(x,y,z) (x ^ y ^ z)
  21336. #define P(a,b,c,d,x,s) { a += F(b,c,d) + x + 0x6ED9EBA1; a = S(a,s); }
  21337. P( A, B, C, D, X[ 0], 3 );
  21338. P( D, A, B, C, X[ 8], 9 );
  21339. P( C, D, A, B, X[ 4], 11 );
  21340. P( B, C, D, A, X[12], 15 );
  21341. P( A, B, C, D, X[ 2], 3 );
  21342. P( D, A, B, C, X[10], 9 );
  21343. P( C, D, A, B, X[ 6], 11 );
  21344. P( B, C, D, A, X[14], 15 );
  21345. P( A, B, C, D, X[ 1], 3 );
  21346. P( D, A, B, C, X[ 9], 9 );
  21347. P( C, D, A, B, X[ 5], 11 );
  21348. P( B, C, D, A, X[13], 15 );
  21349. P( A, B, C, D, X[ 3], 3 );
  21350. P( D, A, B, C, X[11], 9 );
  21351. P( C, D, A, B, X[ 7], 11 );
  21352. P( B, C, D, A, X[15], 15 );
  21353. #undef F
  21354. #undef P
  21355. ctx->state[0] += A;
  21356. ctx->state[1] += B;
  21357. ctx->state[2] += C;
  21358. ctx->state[3] += D;
  21359. return( 0 );
  21360. }
  21361. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  21362. void mbedtls_md4_process( mbedtls_md4_context *ctx,
  21363. const unsigned char data[64] )
  21364. {
  21365. mbedtls_internal_md4_process( ctx, data );
  21366. }
  21367. #endif
  21368. #endif /* !MBEDTLS_MD4_PROCESS_ALT */
  21369. /*
  21370. * MD4 process buffer
  21371. */
  21372. int mbedtls_md4_update_ret( mbedtls_md4_context *ctx,
  21373. const unsigned char *input,
  21374. size_t ilen )
  21375. {
  21376. int ret;
  21377. size_t fill;
  21378. uint32_t left;
  21379. if( ilen == 0 )
  21380. return( 0 );
  21381. left = ctx->total[0] & 0x3F;
  21382. fill = 64 - left;
  21383. ctx->total[0] += (uint32_t) ilen;
  21384. ctx->total[0] &= 0xFFFFFFFF;
  21385. if( ctx->total[0] < (uint32_t) ilen )
  21386. ctx->total[1]++;
  21387. if( left && ilen >= fill )
  21388. {
  21389. memcpy( (void *) (ctx->buffer + left),
  21390. (void *) input, fill );
  21391. if( ( ret = mbedtls_internal_md4_process( ctx, ctx->buffer ) ) != 0 )
  21392. return( ret );
  21393. input += fill;
  21394. ilen -= fill;
  21395. left = 0;
  21396. }
  21397. while( ilen >= 64 )
  21398. {
  21399. if( ( ret = mbedtls_internal_md4_process( ctx, input ) ) != 0 )
  21400. return( ret );
  21401. input += 64;
  21402. ilen -= 64;
  21403. }
  21404. if( ilen > 0 )
  21405. {
  21406. memcpy( (void *) (ctx->buffer + left),
  21407. (void *) input, ilen );
  21408. }
  21409. return( 0 );
  21410. }
  21411. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  21412. void mbedtls_md4_update( mbedtls_md4_context *ctx,
  21413. const unsigned char *input,
  21414. size_t ilen )
  21415. {
  21416. mbedtls_md4_update_ret( ctx, input, ilen );
  21417. }
  21418. #endif
  21419. static const unsigned char md4_padding[64] =
  21420. {
  21421. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  21422. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  21423. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  21424. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  21425. };
  21426. /*
  21427. * MD4 final digest
  21428. */
  21429. int mbedtls_md4_finish_ret( mbedtls_md4_context *ctx,
  21430. unsigned char output[16] )
  21431. {
  21432. int ret;
  21433. uint32_t last, padn;
  21434. uint32_t high, low;
  21435. unsigned char msglen[8];
  21436. high = ( ctx->total[0] >> 29 )
  21437. | ( ctx->total[1] << 3 );
  21438. low = ( ctx->total[0] << 3 );
  21439. PUT_UINT32_LE( low, msglen, 0 );
  21440. PUT_UINT32_LE( high, msglen, 4 );
  21441. last = ctx->total[0] & 0x3F;
  21442. padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  21443. ret = mbedtls_md4_update_ret( ctx, (unsigned char *)md4_padding, padn );
  21444. if( ret != 0 )
  21445. return( ret );
  21446. if( ( ret = mbedtls_md4_update_ret( ctx, msglen, 8 ) ) != 0 )
  21447. return( ret );
  21448. PUT_UINT32_LE( ctx->state[0], output, 0 );
  21449. PUT_UINT32_LE( ctx->state[1], output, 4 );
  21450. PUT_UINT32_LE( ctx->state[2], output, 8 );
  21451. PUT_UINT32_LE( ctx->state[3], output, 12 );
  21452. return( 0 );
  21453. }
  21454. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  21455. void mbedtls_md4_finish( mbedtls_md4_context *ctx,
  21456. unsigned char output[16] )
  21457. {
  21458. mbedtls_md4_finish_ret( ctx, output );
  21459. }
  21460. #endif
  21461. #endif /* !MBEDTLS_MD4_ALT */
  21462. /*
  21463. * output = MD4( input buffer )
  21464. */
  21465. int mbedtls_md4_ret( const unsigned char *input,
  21466. size_t ilen,
  21467. unsigned char output[16] )
  21468. {
  21469. int ret;
  21470. mbedtls_md4_context ctx;
  21471. mbedtls_md4_init( &ctx );
  21472. if( ( ret = mbedtls_md4_starts_ret( &ctx ) ) != 0 )
  21473. goto exit;
  21474. if( ( ret = mbedtls_md4_update_ret( &ctx, input, ilen ) ) != 0 )
  21475. goto exit;
  21476. if( ( ret = mbedtls_md4_finish_ret( &ctx, output ) ) != 0 )
  21477. goto exit;
  21478. exit:
  21479. mbedtls_md4_free( &ctx );
  21480. return( ret );
  21481. }
  21482. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  21483. void mbedtls_md4( const unsigned char *input,
  21484. size_t ilen,
  21485. unsigned char output[16] )
  21486. {
  21487. mbedtls_md4_ret( input, ilen, output );
  21488. }
  21489. #endif
  21490. #if defined(MBEDTLS_SELF_TEST)
  21491. /*
  21492. * RFC 1320 test vectors
  21493. */
  21494. static const unsigned char md4_test_str[7][81] =
  21495. {
  21496. { "" },
  21497. { "a" },
  21498. { "abc" },
  21499. { "message digest" },
  21500. { "abcdefghijklmnopqrstuvwxyz" },
  21501. { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
  21502. { "12345678901234567890123456789012345678901234567890123456789012"
  21503. "345678901234567890" }
  21504. };
  21505. static const size_t md4_test_strlen[7] =
  21506. {
  21507. 0, 1, 3, 14, 26, 62, 80
  21508. };
  21509. static const unsigned char md4_test_sum[7][16] =
  21510. {
  21511. { 0x31, 0xD6, 0xCF, 0xE0, 0xD1, 0x6A, 0xE9, 0x31,
  21512. 0xB7, 0x3C, 0x59, 0xD7, 0xE0, 0xC0, 0x89, 0xC0 },
  21513. { 0xBD, 0xE5, 0x2C, 0xB3, 0x1D, 0xE3, 0x3E, 0x46,
  21514. 0x24, 0x5E, 0x05, 0xFB, 0xDB, 0xD6, 0xFB, 0x24 },
  21515. { 0xA4, 0x48, 0x01, 0x7A, 0xAF, 0x21, 0xD8, 0x52,
  21516. 0x5F, 0xC1, 0x0A, 0xE8, 0x7A, 0xA6, 0x72, 0x9D },
  21517. { 0xD9, 0x13, 0x0A, 0x81, 0x64, 0x54, 0x9F, 0xE8,
  21518. 0x18, 0x87, 0x48, 0x06, 0xE1, 0xC7, 0x01, 0x4B },
  21519. { 0xD7, 0x9E, 0x1C, 0x30, 0x8A, 0xA5, 0xBB, 0xCD,
  21520. 0xEE, 0xA8, 0xED, 0x63, 0xDF, 0x41, 0x2D, 0xA9 },
  21521. { 0x04, 0x3F, 0x85, 0x82, 0xF2, 0x41, 0xDB, 0x35,
  21522. 0x1C, 0xE6, 0x27, 0xE1, 0x53, 0xE7, 0xF0, 0xE4 },
  21523. { 0xE3, 0x3B, 0x4D, 0xDC, 0x9C, 0x38, 0xF2, 0x19,
  21524. 0x9C, 0x3E, 0x7B, 0x16, 0x4F, 0xCC, 0x05, 0x36 }
  21525. };
  21526. /*
  21527. * Checkup routine
  21528. */
  21529. int mbedtls_md4_self_test( int verbose )
  21530. {
  21531. int i, ret = 0;
  21532. unsigned char md4sum[16];
  21533. for( i = 0; i < 7; i++ )
  21534. {
  21535. if( verbose != 0 )
  21536. mbedtls_printf( " MD4 test #%d: ", i + 1 );
  21537. ret = mbedtls_md4_ret( md4_test_str[i], md4_test_strlen[i], md4sum );
  21538. if( ret != 0 )
  21539. goto fail;
  21540. if( memcmp( md4sum, md4_test_sum[i], 16 ) != 0 )
  21541. {
  21542. ret = 1;
  21543. goto fail;
  21544. }
  21545. if( verbose != 0 )
  21546. mbedtls_printf( "passed\n" );
  21547. }
  21548. if( verbose != 0 )
  21549. mbedtls_printf( "\n" );
  21550. return( 0 );
  21551. fail:
  21552. if( verbose != 0 )
  21553. mbedtls_printf( "failed\n" );
  21554. return( ret );
  21555. }
  21556. #endif /* MBEDTLS_SELF_TEST */
  21557. #endif /* MBEDTLS_MD4_C */
  21558. /*
  21559. Amalgamated build undefines
  21560. */
  21561. #undef ADD
  21562. #undef BC
  21563. #undef BEFORE_COLON
  21564. #undef F
  21565. #undef F0
  21566. #undef F1
  21567. #undef F2
  21568. #undef F3
  21569. #undef F4
  21570. #undef F5
  21571. #undef FSb
  21572. #undef K
  21573. #undef KK
  21574. #undef P
  21575. #undef R
  21576. #undef ROTR
  21577. #undef S
  21578. #undef S0
  21579. #undef S1
  21580. #undef S2
  21581. #undef S3
  21582. #undef SAFE_SNPRINTF
  21583. #undef SHR
  21584. #undef close
  21585. #undef read
  21586. #undef supported_init
  21587. #undef write
  21588. /********* Start of file library/md5.c ************/
  21589. /*
  21590. * RFC 1321 compliant MD5 implementation
  21591. *
  21592. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  21593. * SPDX-License-Identifier: Apache-2.0
  21594. *
  21595. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  21596. * not use this file except in compliance with the License.
  21597. * You may obtain a copy of the License at
  21598. *
  21599. * http://www.apache.org/licenses/LICENSE-2.0
  21600. *
  21601. * Unless required by applicable law or agreed to in writing, software
  21602. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21603. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21604. * See the License for the specific language governing permissions and
  21605. * limitations under the License.
  21606. *
  21607. * This file is part of mbed TLS (https://tls.mbed.org)
  21608. */
  21609. /*
  21610. * The MD5 algorithm was designed by Ron Rivest in 1991.
  21611. *
  21612. * http://www.ietf.org/rfc/rfc1321.txt
  21613. */
  21614. #if !defined(MBEDTLS_CONFIG_FILE)
  21615. #else
  21616. #endif
  21617. #if defined(MBEDTLS_MD5_C)
  21618. #include <string.h>
  21619. #if defined(MBEDTLS_SELF_TEST)
  21620. #if defined(MBEDTLS_PLATFORM_C)
  21621. #else
  21622. #include <stdio.h>
  21623. #define mbedtls_printf printf
  21624. #endif /* MBEDTLS_PLATFORM_C */
  21625. #endif /* MBEDTLS_SELF_TEST */
  21626. #if !defined(MBEDTLS_MD5_ALT)
  21627. /* Implementation that should never be optimized out by the compiler */
  21628. /* zeroize was here */
  21629. /*
  21630. * 32-bit integer manipulation macros (little endian)
  21631. */
  21632. #ifndef GET_UINT32_LE
  21633. #define GET_UINT32_LE(n,b,i) \
  21634. { \
  21635. (n) = ( (uint32_t) (b)[(i) ] ) \
  21636. | ( (uint32_t) (b)[(i) + 1] << 8 ) \
  21637. | ( (uint32_t) (b)[(i) + 2] << 16 ) \
  21638. | ( (uint32_t) (b)[(i) + 3] << 24 ); \
  21639. }
  21640. #endif
  21641. #ifndef PUT_UINT32_LE
  21642. #define PUT_UINT32_LE(n,b,i) \
  21643. { \
  21644. (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
  21645. (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
  21646. (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
  21647. (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
  21648. }
  21649. #endif
  21650. void mbedtls_md5_init( mbedtls_md5_context *ctx )
  21651. {
  21652. memset( ctx, 0, sizeof( mbedtls_md5_context ) );
  21653. }
  21654. void mbedtls_md5_free( mbedtls_md5_context *ctx )
  21655. {
  21656. if( ctx == NULL )
  21657. return;
  21658. mbedtls_zeroize( ctx, sizeof( mbedtls_md5_context ) );
  21659. }
  21660. void mbedtls_md5_clone( mbedtls_md5_context *dst,
  21661. const mbedtls_md5_context *src )
  21662. {
  21663. *dst = *src;
  21664. }
  21665. /*
  21666. * MD5 context setup
  21667. */
  21668. int mbedtls_md5_starts_ret( mbedtls_md5_context *ctx )
  21669. {
  21670. ctx->total[0] = 0;
  21671. ctx->total[1] = 0;
  21672. ctx->state[0] = 0x67452301;
  21673. ctx->state[1] = 0xEFCDAB89;
  21674. ctx->state[2] = 0x98BADCFE;
  21675. ctx->state[3] = 0x10325476;
  21676. return( 0 );
  21677. }
  21678. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  21679. void mbedtls_md5_starts( mbedtls_md5_context *ctx )
  21680. {
  21681. mbedtls_md5_starts_ret( ctx );
  21682. }
  21683. #endif
  21684. #if !defined(MBEDTLS_MD5_PROCESS_ALT)
  21685. int mbedtls_internal_md5_process( mbedtls_md5_context *ctx,
  21686. const unsigned char data[64] )
  21687. {
  21688. uint32_t X[16], A, B, C, D;
  21689. GET_UINT32_LE( X[ 0], data, 0 );
  21690. GET_UINT32_LE( X[ 1], data, 4 );
  21691. GET_UINT32_LE( X[ 2], data, 8 );
  21692. GET_UINT32_LE( X[ 3], data, 12 );
  21693. GET_UINT32_LE( X[ 4], data, 16 );
  21694. GET_UINT32_LE( X[ 5], data, 20 );
  21695. GET_UINT32_LE( X[ 6], data, 24 );
  21696. GET_UINT32_LE( X[ 7], data, 28 );
  21697. GET_UINT32_LE( X[ 8], data, 32 );
  21698. GET_UINT32_LE( X[ 9], data, 36 );
  21699. GET_UINT32_LE( X[10], data, 40 );
  21700. GET_UINT32_LE( X[11], data, 44 );
  21701. GET_UINT32_LE( X[12], data, 48 );
  21702. GET_UINT32_LE( X[13], data, 52 );
  21703. GET_UINT32_LE( X[14], data, 56 );
  21704. GET_UINT32_LE( X[15], data, 60 );
  21705. #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  21706. #define P(a,b,c,d,k,s,t) \
  21707. { \
  21708. a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \
  21709. }
  21710. A = ctx->state[0];
  21711. B = ctx->state[1];
  21712. C = ctx->state[2];
  21713. D = ctx->state[3];
  21714. #define F(x,y,z) (z ^ (x & (y ^ z)))
  21715. P( A, B, C, D, 0, 7, 0xD76AA478 );
  21716. P( D, A, B, C, 1, 12, 0xE8C7B756 );
  21717. P( C, D, A, B, 2, 17, 0x242070DB );
  21718. P( B, C, D, A, 3, 22, 0xC1BDCEEE );
  21719. P( A, B, C, D, 4, 7, 0xF57C0FAF );
  21720. P( D, A, B, C, 5, 12, 0x4787C62A );
  21721. P( C, D, A, B, 6, 17, 0xA8304613 );
  21722. P( B, C, D, A, 7, 22, 0xFD469501 );
  21723. P( A, B, C, D, 8, 7, 0x698098D8 );
  21724. P( D, A, B, C, 9, 12, 0x8B44F7AF );
  21725. P( C, D, A, B, 10, 17, 0xFFFF5BB1 );
  21726. P( B, C, D, A, 11, 22, 0x895CD7BE );
  21727. P( A, B, C, D, 12, 7, 0x6B901122 );
  21728. P( D, A, B, C, 13, 12, 0xFD987193 );
  21729. P( C, D, A, B, 14, 17, 0xA679438E );
  21730. P( B, C, D, A, 15, 22, 0x49B40821 );
  21731. #undef F
  21732. #define F(x,y,z) (y ^ (z & (x ^ y)))
  21733. P( A, B, C, D, 1, 5, 0xF61E2562 );
  21734. P( D, A, B, C, 6, 9, 0xC040B340 );
  21735. P( C, D, A, B, 11, 14, 0x265E5A51 );
  21736. P( B, C, D, A, 0, 20, 0xE9B6C7AA );
  21737. P( A, B, C, D, 5, 5, 0xD62F105D );
  21738. P( D, A, B, C, 10, 9, 0x02441453 );
  21739. P( C, D, A, B, 15, 14, 0xD8A1E681 );
  21740. P( B, C, D, A, 4, 20, 0xE7D3FBC8 );
  21741. P( A, B, C, D, 9, 5, 0x21E1CDE6 );
  21742. P( D, A, B, C, 14, 9, 0xC33707D6 );
  21743. P( C, D, A, B, 3, 14, 0xF4D50D87 );
  21744. P( B, C, D, A, 8, 20, 0x455A14ED );
  21745. P( A, B, C, D, 13, 5, 0xA9E3E905 );
  21746. P( D, A, B, C, 2, 9, 0xFCEFA3F8 );
  21747. P( C, D, A, B, 7, 14, 0x676F02D9 );
  21748. P( B, C, D, A, 12, 20, 0x8D2A4C8A );
  21749. #undef F
  21750. #define F(x,y,z) (x ^ y ^ z)
  21751. P( A, B, C, D, 5, 4, 0xFFFA3942 );
  21752. P( D, A, B, C, 8, 11, 0x8771F681 );
  21753. P( C, D, A, B, 11, 16, 0x6D9D6122 );
  21754. P( B, C, D, A, 14, 23, 0xFDE5380C );
  21755. P( A, B, C, D, 1, 4, 0xA4BEEA44 );
  21756. P( D, A, B, C, 4, 11, 0x4BDECFA9 );
  21757. P( C, D, A, B, 7, 16, 0xF6BB4B60 );
  21758. P( B, C, D, A, 10, 23, 0xBEBFBC70 );
  21759. P( A, B, C, D, 13, 4, 0x289B7EC6 );
  21760. P( D, A, B, C, 0, 11, 0xEAA127FA );
  21761. P( C, D, A, B, 3, 16, 0xD4EF3085 );
  21762. P( B, C, D, A, 6, 23, 0x04881D05 );
  21763. P( A, B, C, D, 9, 4, 0xD9D4D039 );
  21764. P( D, A, B, C, 12, 11, 0xE6DB99E5 );
  21765. P( C, D, A, B, 15, 16, 0x1FA27CF8 );
  21766. P( B, C, D, A, 2, 23, 0xC4AC5665 );
  21767. #undef F
  21768. #define F(x,y,z) (y ^ (x | ~z))
  21769. P( A, B, C, D, 0, 6, 0xF4292244 );
  21770. P( D, A, B, C, 7, 10, 0x432AFF97 );
  21771. P( C, D, A, B, 14, 15, 0xAB9423A7 );
  21772. P( B, C, D, A, 5, 21, 0xFC93A039 );
  21773. P( A, B, C, D, 12, 6, 0x655B59C3 );
  21774. P( D, A, B, C, 3, 10, 0x8F0CCC92 );
  21775. P( C, D, A, B, 10, 15, 0xFFEFF47D );
  21776. P( B, C, D, A, 1, 21, 0x85845DD1 );
  21777. P( A, B, C, D, 8, 6, 0x6FA87E4F );
  21778. P( D, A, B, C, 15, 10, 0xFE2CE6E0 );
  21779. P( C, D, A, B, 6, 15, 0xA3014314 );
  21780. P( B, C, D, A, 13, 21, 0x4E0811A1 );
  21781. P( A, B, C, D, 4, 6, 0xF7537E82 );
  21782. P( D, A, B, C, 11, 10, 0xBD3AF235 );
  21783. P( C, D, A, B, 2, 15, 0x2AD7D2BB );
  21784. P( B, C, D, A, 9, 21, 0xEB86D391 );
  21785. #undef F
  21786. ctx->state[0] += A;
  21787. ctx->state[1] += B;
  21788. ctx->state[2] += C;
  21789. ctx->state[3] += D;
  21790. return( 0 );
  21791. }
  21792. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  21793. void mbedtls_md5_process( mbedtls_md5_context *ctx,
  21794. const unsigned char data[64] )
  21795. {
  21796. mbedtls_internal_md5_process( ctx, data );
  21797. }
  21798. #endif
  21799. #endif /* !MBEDTLS_MD5_PROCESS_ALT */
  21800. /*
  21801. * MD5 process buffer
  21802. */
  21803. int mbedtls_md5_update_ret( mbedtls_md5_context *ctx,
  21804. const unsigned char *input,
  21805. size_t ilen )
  21806. {
  21807. int ret;
  21808. size_t fill;
  21809. uint32_t left;
  21810. if( ilen == 0 )
  21811. return( 0 );
  21812. left = ctx->total[0] & 0x3F;
  21813. fill = 64 - left;
  21814. ctx->total[0] += (uint32_t) ilen;
  21815. ctx->total[0] &= 0xFFFFFFFF;
  21816. if( ctx->total[0] < (uint32_t) ilen )
  21817. ctx->total[1]++;
  21818. if( left && ilen >= fill )
  21819. {
  21820. memcpy( (void *) (ctx->buffer + left), input, fill );
  21821. if( ( ret = mbedtls_internal_md5_process( ctx, ctx->buffer ) ) != 0 )
  21822. return( ret );
  21823. input += fill;
  21824. ilen -= fill;
  21825. left = 0;
  21826. }
  21827. while( ilen >= 64 )
  21828. {
  21829. if( ( ret = mbedtls_internal_md5_process( ctx, input ) ) != 0 )
  21830. return( ret );
  21831. input += 64;
  21832. ilen -= 64;
  21833. }
  21834. if( ilen > 0 )
  21835. {
  21836. memcpy( (void *) (ctx->buffer + left), input, ilen );
  21837. }
  21838. return( 0 );
  21839. }
  21840. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  21841. void mbedtls_md5_update( mbedtls_md5_context *ctx,
  21842. const unsigned char *input,
  21843. size_t ilen )
  21844. {
  21845. mbedtls_md5_update_ret( ctx, input, ilen );
  21846. }
  21847. #endif
  21848. static const unsigned char md5_padding[64] =
  21849. {
  21850. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  21851. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  21852. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  21853. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  21854. };
  21855. /*
  21856. * MD5 final digest
  21857. */
  21858. int mbedtls_md5_finish_ret( mbedtls_md5_context *ctx,
  21859. unsigned char output[16] )
  21860. {
  21861. int ret;
  21862. uint32_t last, padn;
  21863. uint32_t high, low;
  21864. unsigned char msglen[8];
  21865. high = ( ctx->total[0] >> 29 )
  21866. | ( ctx->total[1] << 3 );
  21867. low = ( ctx->total[0] << 3 );
  21868. PUT_UINT32_LE( low, msglen, 0 );
  21869. PUT_UINT32_LE( high, msglen, 4 );
  21870. last = ctx->total[0] & 0x3F;
  21871. padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  21872. if( ( ret = mbedtls_md5_update_ret( ctx, md5_padding, padn ) ) != 0 )
  21873. return( ret );
  21874. if( ( ret = mbedtls_md5_update_ret( ctx, msglen, 8 ) ) != 0 )
  21875. return( ret );
  21876. PUT_UINT32_LE( ctx->state[0], output, 0 );
  21877. PUT_UINT32_LE( ctx->state[1], output, 4 );
  21878. PUT_UINT32_LE( ctx->state[2], output, 8 );
  21879. PUT_UINT32_LE( ctx->state[3], output, 12 );
  21880. return( 0 );
  21881. }
  21882. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  21883. void mbedtls_md5_finish( mbedtls_md5_context *ctx,
  21884. unsigned char output[16] )
  21885. {
  21886. mbedtls_md5_finish_ret( ctx, output );
  21887. }
  21888. #endif
  21889. #endif /* !MBEDTLS_MD5_ALT */
  21890. /*
  21891. * output = MD5( input buffer )
  21892. */
  21893. int mbedtls_md5_ret( const unsigned char *input,
  21894. size_t ilen,
  21895. unsigned char output[16] )
  21896. {
  21897. int ret;
  21898. mbedtls_md5_context ctx;
  21899. mbedtls_md5_init( &ctx );
  21900. if( ( ret = mbedtls_md5_starts_ret( &ctx ) ) != 0 )
  21901. goto exit;
  21902. if( ( ret = mbedtls_md5_update_ret( &ctx, input, ilen ) ) != 0 )
  21903. goto exit;
  21904. if( ( ret = mbedtls_md5_finish_ret( &ctx, output ) ) != 0 )
  21905. goto exit;
  21906. exit:
  21907. mbedtls_md5_free( &ctx );
  21908. return( ret );
  21909. }
  21910. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  21911. void mbedtls_md5( const unsigned char *input,
  21912. size_t ilen,
  21913. unsigned char output[16] )
  21914. {
  21915. mbedtls_md5_ret( input, ilen, output );
  21916. }
  21917. #endif
  21918. #if defined(MBEDTLS_SELF_TEST)
  21919. /*
  21920. * RFC 1321 test vectors
  21921. */
  21922. static const unsigned char md5_test_buf[7][81] =
  21923. {
  21924. { "" },
  21925. { "a" },
  21926. { "abc" },
  21927. { "message digest" },
  21928. { "abcdefghijklmnopqrstuvwxyz" },
  21929. { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
  21930. { "12345678901234567890123456789012345678901234567890123456789012"
  21931. "345678901234567890" }
  21932. };
  21933. static const size_t md5_test_buflen[7] =
  21934. {
  21935. 0, 1, 3, 14, 26, 62, 80
  21936. };
  21937. static const unsigned char md5_test_sum[7][16] =
  21938. {
  21939. { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
  21940. 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E },
  21941. { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8,
  21942. 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 },
  21943. { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,
  21944. 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 },
  21945. { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D,
  21946. 0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 },
  21947. { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00,
  21948. 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B },
  21949. { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5,
  21950. 0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F },
  21951. { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55,
  21952. 0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A }
  21953. };
  21954. /*
  21955. * Checkup routine
  21956. */
  21957. int mbedtls_md5_self_test( int verbose )
  21958. {
  21959. int i, ret = 0;
  21960. unsigned char md5sum[16];
  21961. for( i = 0; i < 7; i++ )
  21962. {
  21963. if( verbose != 0 )
  21964. mbedtls_printf( " MD5 test #%d: ", i + 1 );
  21965. ret = mbedtls_md5_ret( md5_test_buf[i], md5_test_buflen[i], md5sum );
  21966. if( ret != 0 )
  21967. goto fail;
  21968. if( memcmp( md5sum, md5_test_sum[i], 16 ) != 0 )
  21969. {
  21970. ret = 1;
  21971. goto fail;
  21972. }
  21973. if( verbose != 0 )
  21974. mbedtls_printf( "passed\n" );
  21975. }
  21976. if( verbose != 0 )
  21977. mbedtls_printf( "\n" );
  21978. return( 0 );
  21979. fail:
  21980. if( verbose != 0 )
  21981. mbedtls_printf( "failed\n" );
  21982. return( ret );
  21983. }
  21984. #endif /* MBEDTLS_SELF_TEST */
  21985. #endif /* MBEDTLS_MD5_C */
  21986. /*
  21987. Amalgamated build undefines
  21988. */
  21989. #undef ADD
  21990. #undef BC
  21991. #undef BEFORE_COLON
  21992. #undef F
  21993. #undef F0
  21994. #undef F1
  21995. #undef F2
  21996. #undef F3
  21997. #undef F4
  21998. #undef F5
  21999. #undef FSb
  22000. #undef K
  22001. #undef KK
  22002. #undef P
  22003. #undef R
  22004. #undef ROTR
  22005. #undef S
  22006. #undef S0
  22007. #undef S1
  22008. #undef S2
  22009. #undef S3
  22010. #undef SAFE_SNPRINTF
  22011. #undef SHR
  22012. #undef close
  22013. #undef read
  22014. #undef supported_init
  22015. #undef write
  22016. /********* Start of file library/md_wrap.c ************/
  22017. /**
  22018. * \file md_wrap.c
  22019. *
  22020. * \brief Generic message digest wrapper for mbed TLS
  22021. *
  22022. * \author Adriaan de Jong <dejong@fox-it.com>
  22023. *
  22024. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  22025. * SPDX-License-Identifier: Apache-2.0
  22026. *
  22027. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  22028. * not use this file except in compliance with the License.
  22029. * You may obtain a copy of the License at
  22030. *
  22031. * http://www.apache.org/licenses/LICENSE-2.0
  22032. *
  22033. * Unless required by applicable law or agreed to in writing, software
  22034. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  22035. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22036. * See the License for the specific language governing permissions and
  22037. * limitations under the License.
  22038. *
  22039. * This file is part of mbed TLS (https://tls.mbed.org)
  22040. */
  22041. #if !defined(MBEDTLS_CONFIG_FILE)
  22042. #else
  22043. #endif
  22044. #if defined(MBEDTLS_MD_C)
  22045. #if defined(MBEDTLS_MD2_C)
  22046. #endif
  22047. #if defined(MBEDTLS_MD4_C)
  22048. #endif
  22049. #if defined(MBEDTLS_MD5_C)
  22050. #endif
  22051. #if defined(MBEDTLS_RIPEMD160_C)
  22052. #endif
  22053. #if defined(MBEDTLS_SHA1_C)
  22054. #endif
  22055. #if defined(MBEDTLS_SHA256_C)
  22056. #endif
  22057. #if defined(MBEDTLS_SHA512_C)
  22058. #endif
  22059. #if defined(MBEDTLS_PLATFORM_C)
  22060. #else
  22061. #include <stdlib.h>
  22062. #define mbedtls_calloc calloc
  22063. #define mbedtls_free free
  22064. #endif
  22065. #if defined(MBEDTLS_MD2_C)
  22066. static int md2_starts_wrap( void *ctx )
  22067. {
  22068. return( mbedtls_md2_starts_ret( (mbedtls_md2_context *) ctx ) );
  22069. }
  22070. static int md2_update_wrap( void *ctx, const unsigned char *input,
  22071. size_t ilen )
  22072. {
  22073. return( mbedtls_md2_update_ret( (mbedtls_md2_context *) ctx, input, ilen ) );
  22074. }
  22075. static int md2_finish_wrap( void *ctx, unsigned char *output )
  22076. {
  22077. return( mbedtls_md2_finish_ret( (mbedtls_md2_context *) ctx, output ) );
  22078. }
  22079. static void *md2_ctx_alloc( void )
  22080. {
  22081. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
  22082. if( ctx != NULL )
  22083. mbedtls_md2_init( (mbedtls_md2_context *) ctx );
  22084. return( ctx );
  22085. }
  22086. static void md2_ctx_free( void *ctx )
  22087. {
  22088. mbedtls_md2_free( (mbedtls_md2_context *) ctx );
  22089. mbedtls_free( ctx );
  22090. }
  22091. static void md2_clone_wrap( void *dst, const void *src )
  22092. {
  22093. mbedtls_md2_clone( (mbedtls_md2_context *) dst,
  22094. (const mbedtls_md2_context *) src );
  22095. }
  22096. static int md2_process_wrap( void *ctx, const unsigned char *data )
  22097. {
  22098. ((void) data);
  22099. return( mbedtls_internal_md2_process( (mbedtls_md2_context *) ctx ) );
  22100. }
  22101. const mbedtls_md_info_t mbedtls_md2_info = {
  22102. MBEDTLS_MD_MD2,
  22103. "MD2",
  22104. 16,
  22105. 16,
  22106. md2_starts_wrap,
  22107. md2_update_wrap,
  22108. md2_finish_wrap,
  22109. mbedtls_md2_ret,
  22110. md2_ctx_alloc,
  22111. md2_ctx_free,
  22112. md2_clone_wrap,
  22113. md2_process_wrap,
  22114. };
  22115. #endif /* MBEDTLS_MD2_C */
  22116. #if defined(MBEDTLS_MD4_C)
  22117. static int md4_starts_wrap( void *ctx )
  22118. {
  22119. return( mbedtls_md4_starts_ret( (mbedtls_md4_context *) ctx ) );
  22120. }
  22121. static int md4_update_wrap( void *ctx, const unsigned char *input,
  22122. size_t ilen )
  22123. {
  22124. return( mbedtls_md4_update_ret( (mbedtls_md4_context *) ctx, input, ilen ) );
  22125. }
  22126. static int md4_finish_wrap( void *ctx, unsigned char *output )
  22127. {
  22128. return( mbedtls_md4_finish_ret( (mbedtls_md4_context *) ctx, output ) );
  22129. }
  22130. static void *md4_ctx_alloc( void )
  22131. {
  22132. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
  22133. if( ctx != NULL )
  22134. mbedtls_md4_init( (mbedtls_md4_context *) ctx );
  22135. return( ctx );
  22136. }
  22137. static void md4_ctx_free( void *ctx )
  22138. {
  22139. mbedtls_md4_free( (mbedtls_md4_context *) ctx );
  22140. mbedtls_free( ctx );
  22141. }
  22142. static void md4_clone_wrap( void *dst, const void *src )
  22143. {
  22144. mbedtls_md4_clone( (mbedtls_md4_context *) dst,
  22145. (const mbedtls_md4_context *) src );
  22146. }
  22147. static int md4_process_wrap( void *ctx, const unsigned char *data )
  22148. {
  22149. return( mbedtls_internal_md4_process( (mbedtls_md4_context *) ctx, data ) );
  22150. }
  22151. const mbedtls_md_info_t mbedtls_md4_info = {
  22152. MBEDTLS_MD_MD4,
  22153. "MD4",
  22154. 16,
  22155. 64,
  22156. md4_starts_wrap,
  22157. md4_update_wrap,
  22158. md4_finish_wrap,
  22159. mbedtls_md4_ret,
  22160. md4_ctx_alloc,
  22161. md4_ctx_free,
  22162. md4_clone_wrap,
  22163. md4_process_wrap,
  22164. };
  22165. #endif /* MBEDTLS_MD4_C */
  22166. #if defined(MBEDTLS_MD5_C)
  22167. static int md5_starts_wrap( void *ctx )
  22168. {
  22169. return( mbedtls_md5_starts_ret( (mbedtls_md5_context *) ctx ) );
  22170. }
  22171. static int md5_update_wrap( void *ctx, const unsigned char *input,
  22172. size_t ilen )
  22173. {
  22174. return( mbedtls_md5_update_ret( (mbedtls_md5_context *) ctx, input, ilen ) );
  22175. }
  22176. static int md5_finish_wrap( void *ctx, unsigned char *output )
  22177. {
  22178. return( mbedtls_md5_finish_ret( (mbedtls_md5_context *) ctx, output ) );
  22179. }
  22180. static void *md5_ctx_alloc( void )
  22181. {
  22182. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
  22183. if( ctx != NULL )
  22184. mbedtls_md5_init( (mbedtls_md5_context *) ctx );
  22185. return( ctx );
  22186. }
  22187. static void md5_ctx_free( void *ctx )
  22188. {
  22189. mbedtls_md5_free( (mbedtls_md5_context *) ctx );
  22190. mbedtls_free( ctx );
  22191. }
  22192. static void md5_clone_wrap( void *dst, const void *src )
  22193. {
  22194. mbedtls_md5_clone( (mbedtls_md5_context *) dst,
  22195. (const mbedtls_md5_context *) src );
  22196. }
  22197. static int md5_process_wrap( void *ctx, const unsigned char *data )
  22198. {
  22199. return( mbedtls_internal_md5_process( (mbedtls_md5_context *) ctx, data ) );
  22200. }
  22201. const mbedtls_md_info_t mbedtls_md5_info = {
  22202. MBEDTLS_MD_MD5,
  22203. "MD5",
  22204. 16,
  22205. 64,
  22206. md5_starts_wrap,
  22207. md5_update_wrap,
  22208. md5_finish_wrap,
  22209. mbedtls_md5_ret,
  22210. md5_ctx_alloc,
  22211. md5_ctx_free,
  22212. md5_clone_wrap,
  22213. md5_process_wrap,
  22214. };
  22215. #endif /* MBEDTLS_MD5_C */
  22216. #if defined(MBEDTLS_RIPEMD160_C)
  22217. static int ripemd160_starts_wrap( void *ctx )
  22218. {
  22219. return( mbedtls_ripemd160_starts_ret( (mbedtls_ripemd160_context *) ctx ) );
  22220. }
  22221. static int ripemd160_update_wrap( void *ctx, const unsigned char *input,
  22222. size_t ilen )
  22223. {
  22224. return( mbedtls_ripemd160_update_ret( (mbedtls_ripemd160_context *) ctx,
  22225. input, ilen ) );
  22226. }
  22227. static int ripemd160_finish_wrap( void *ctx, unsigned char *output )
  22228. {
  22229. return( mbedtls_ripemd160_finish_ret( (mbedtls_ripemd160_context *) ctx,
  22230. output ) );
  22231. }
  22232. static void *ripemd160_ctx_alloc( void )
  22233. {
  22234. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
  22235. if( ctx != NULL )
  22236. mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx );
  22237. return( ctx );
  22238. }
  22239. static void ripemd160_ctx_free( void *ctx )
  22240. {
  22241. mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
  22242. mbedtls_free( ctx );
  22243. }
  22244. static void ripemd160_clone_wrap( void *dst, const void *src )
  22245. {
  22246. mbedtls_ripemd160_clone( (mbedtls_ripemd160_context *) dst,
  22247. (const mbedtls_ripemd160_context *) src );
  22248. }
  22249. static int ripemd160_process_wrap( void *ctx, const unsigned char *data )
  22250. {
  22251. return( mbedtls_internal_ripemd160_process(
  22252. (mbedtls_ripemd160_context *) ctx, data ) );
  22253. }
  22254. const mbedtls_md_info_t mbedtls_ripemd160_info = {
  22255. MBEDTLS_MD_RIPEMD160,
  22256. "RIPEMD160",
  22257. 20,
  22258. 64,
  22259. ripemd160_starts_wrap,
  22260. ripemd160_update_wrap,
  22261. ripemd160_finish_wrap,
  22262. mbedtls_ripemd160_ret,
  22263. ripemd160_ctx_alloc,
  22264. ripemd160_ctx_free,
  22265. ripemd160_clone_wrap,
  22266. ripemd160_process_wrap,
  22267. };
  22268. #endif /* MBEDTLS_RIPEMD160_C */
  22269. #if defined(MBEDTLS_SHA1_C)
  22270. static int sha1_starts_wrap( void *ctx )
  22271. {
  22272. return( mbedtls_sha1_starts_ret( (mbedtls_sha1_context *) ctx ) );
  22273. }
  22274. static int sha1_update_wrap( void *ctx, const unsigned char *input,
  22275. size_t ilen )
  22276. {
  22277. return( mbedtls_sha1_update_ret( (mbedtls_sha1_context *) ctx,
  22278. input, ilen ) );
  22279. }
  22280. static int sha1_finish_wrap( void *ctx, unsigned char *output )
  22281. {
  22282. return( mbedtls_sha1_finish_ret( (mbedtls_sha1_context *) ctx, output ) );
  22283. }
  22284. static void *sha1_ctx_alloc( void )
  22285. {
  22286. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
  22287. if( ctx != NULL )
  22288. mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
  22289. return( ctx );
  22290. }
  22291. static void sha1_clone_wrap( void *dst, const void *src )
  22292. {
  22293. mbedtls_sha1_clone( (mbedtls_sha1_context *) dst,
  22294. (const mbedtls_sha1_context *) src );
  22295. }
  22296. static void sha1_ctx_free( void *ctx )
  22297. {
  22298. mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
  22299. mbedtls_free( ctx );
  22300. }
  22301. static int sha1_process_wrap( void *ctx, const unsigned char *data )
  22302. {
  22303. return( mbedtls_internal_sha1_process( (mbedtls_sha1_context *) ctx,
  22304. data ) );
  22305. }
  22306. const mbedtls_md_info_t mbedtls_sha1_info = {
  22307. MBEDTLS_MD_SHA1,
  22308. "SHA1",
  22309. 20,
  22310. 64,
  22311. sha1_starts_wrap,
  22312. sha1_update_wrap,
  22313. sha1_finish_wrap,
  22314. mbedtls_sha1_ret,
  22315. sha1_ctx_alloc,
  22316. sha1_ctx_free,
  22317. sha1_clone_wrap,
  22318. sha1_process_wrap,
  22319. };
  22320. #endif /* MBEDTLS_SHA1_C */
  22321. /*
  22322. * Wrappers for generic message digests
  22323. */
  22324. #if defined(MBEDTLS_SHA256_C)
  22325. static int sha224_starts_wrap( void *ctx )
  22326. {
  22327. return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 1 ) );
  22328. }
  22329. static int sha224_update_wrap( void *ctx, const unsigned char *input,
  22330. size_t ilen )
  22331. {
  22332. return( mbedtls_sha256_update_ret( (mbedtls_sha256_context *) ctx,
  22333. input, ilen ) );
  22334. }
  22335. static int sha224_finish_wrap( void *ctx, unsigned char *output )
  22336. {
  22337. return( mbedtls_sha256_finish_ret( (mbedtls_sha256_context *) ctx,
  22338. output ) );
  22339. }
  22340. static int sha224_wrap( const unsigned char *input, size_t ilen,
  22341. unsigned char *output )
  22342. {
  22343. return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
  22344. }
  22345. static void *sha224_ctx_alloc( void )
  22346. {
  22347. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
  22348. if( ctx != NULL )
  22349. mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
  22350. return( ctx );
  22351. }
  22352. static void sha224_ctx_free( void *ctx )
  22353. {
  22354. mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
  22355. mbedtls_free( ctx );
  22356. }
  22357. static void sha224_clone_wrap( void *dst, const void *src )
  22358. {
  22359. mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
  22360. (const mbedtls_sha256_context *) src );
  22361. }
  22362. static int sha224_process_wrap( void *ctx, const unsigned char *data )
  22363. {
  22364. return( mbedtls_internal_sha256_process( (mbedtls_sha256_context *) ctx,
  22365. data ) );
  22366. }
  22367. const mbedtls_md_info_t mbedtls_sha224_info = {
  22368. MBEDTLS_MD_SHA224,
  22369. "SHA224",
  22370. 28,
  22371. 64,
  22372. sha224_starts_wrap,
  22373. sha224_update_wrap,
  22374. sha224_finish_wrap,
  22375. sha224_wrap,
  22376. sha224_ctx_alloc,
  22377. sha224_ctx_free,
  22378. sha224_clone_wrap,
  22379. sha224_process_wrap,
  22380. };
  22381. static int sha256_starts_wrap( void *ctx )
  22382. {
  22383. return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 0 ) );
  22384. }
  22385. static int sha256_wrap( const unsigned char *input, size_t ilen,
  22386. unsigned char *output )
  22387. {
  22388. return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
  22389. }
  22390. const mbedtls_md_info_t mbedtls_sha256_info = {
  22391. MBEDTLS_MD_SHA256,
  22392. "SHA256",
  22393. 32,
  22394. 64,
  22395. sha256_starts_wrap,
  22396. sha224_update_wrap,
  22397. sha224_finish_wrap,
  22398. sha256_wrap,
  22399. sha224_ctx_alloc,
  22400. sha224_ctx_free,
  22401. sha224_clone_wrap,
  22402. sha224_process_wrap,
  22403. };
  22404. #endif /* MBEDTLS_SHA256_C */
  22405. #if defined(MBEDTLS_SHA512_C)
  22406. static int sha384_starts_wrap( void *ctx )
  22407. {
  22408. return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 1 ) );
  22409. }
  22410. static int sha384_update_wrap( void *ctx, const unsigned char *input,
  22411. size_t ilen )
  22412. {
  22413. return( mbedtls_sha512_update_ret( (mbedtls_sha512_context *) ctx,
  22414. input, ilen ) );
  22415. }
  22416. static int sha384_finish_wrap( void *ctx, unsigned char *output )
  22417. {
  22418. return( mbedtls_sha512_finish_ret( (mbedtls_sha512_context *) ctx,
  22419. output ) );
  22420. }
  22421. static int sha384_wrap( const unsigned char *input, size_t ilen,
  22422. unsigned char *output )
  22423. {
  22424. return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
  22425. }
  22426. static void *sha384_ctx_alloc( void )
  22427. {
  22428. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
  22429. if( ctx != NULL )
  22430. mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
  22431. return( ctx );
  22432. }
  22433. static void sha384_ctx_free( void *ctx )
  22434. {
  22435. mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
  22436. mbedtls_free( ctx );
  22437. }
  22438. static void sha384_clone_wrap( void *dst, const void *src )
  22439. {
  22440. mbedtls_sha512_clone( (mbedtls_sha512_context *) dst,
  22441. (const mbedtls_sha512_context *) src );
  22442. }
  22443. static int sha384_process_wrap( void *ctx, const unsigned char *data )
  22444. {
  22445. return( mbedtls_internal_sha512_process( (mbedtls_sha512_context *) ctx,
  22446. data ) );
  22447. }
  22448. const mbedtls_md_info_t mbedtls_sha384_info = {
  22449. MBEDTLS_MD_SHA384,
  22450. "SHA384",
  22451. 48,
  22452. 128,
  22453. sha384_starts_wrap,
  22454. sha384_update_wrap,
  22455. sha384_finish_wrap,
  22456. sha384_wrap,
  22457. sha384_ctx_alloc,
  22458. sha384_ctx_free,
  22459. sha384_clone_wrap,
  22460. sha384_process_wrap,
  22461. };
  22462. static int sha512_starts_wrap( void *ctx )
  22463. {
  22464. return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 0 ) );
  22465. }
  22466. static int sha512_wrap( const unsigned char *input, size_t ilen,
  22467. unsigned char *output )
  22468. {
  22469. return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
  22470. }
  22471. const mbedtls_md_info_t mbedtls_sha512_info = {
  22472. MBEDTLS_MD_SHA512,
  22473. "SHA512",
  22474. 64,
  22475. 128,
  22476. sha512_starts_wrap,
  22477. sha384_update_wrap,
  22478. sha384_finish_wrap,
  22479. sha512_wrap,
  22480. sha384_ctx_alloc,
  22481. sha384_ctx_free,
  22482. sha384_clone_wrap,
  22483. sha384_process_wrap,
  22484. };
  22485. #endif /* MBEDTLS_SHA512_C */
  22486. #endif /* MBEDTLS_MD_C */
  22487. /*
  22488. Amalgamated build undefines
  22489. */
  22490. #undef ADD
  22491. #undef BC
  22492. #undef BEFORE_COLON
  22493. #undef F
  22494. #undef F0
  22495. #undef F1
  22496. #undef F2
  22497. #undef F3
  22498. #undef F4
  22499. #undef F5
  22500. #undef FSb
  22501. #undef K
  22502. #undef KK
  22503. #undef P
  22504. #undef R
  22505. #undef ROTR
  22506. #undef S
  22507. #undef S0
  22508. #undef S1
  22509. #undef S2
  22510. #undef S3
  22511. #undef SAFE_SNPRINTF
  22512. #undef SHR
  22513. #undef close
  22514. #undef read
  22515. #undef supported_init
  22516. #undef write
  22517. /********* Start of file library/memory_buffer_alloc.c ************/
  22518. /*
  22519. * Buffer-based memory allocator
  22520. *
  22521. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  22522. * SPDX-License-Identifier: Apache-2.0
  22523. *
  22524. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  22525. * not use this file except in compliance with the License.
  22526. * You may obtain a copy of the License at
  22527. *
  22528. * http://www.apache.org/licenses/LICENSE-2.0
  22529. *
  22530. * Unless required by applicable law or agreed to in writing, software
  22531. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  22532. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22533. * See the License for the specific language governing permissions and
  22534. * limitations under the License.
  22535. *
  22536. * This file is part of mbed TLS (https://tls.mbed.org)
  22537. */
  22538. #if !defined(MBEDTLS_CONFIG_FILE)
  22539. #else
  22540. #endif
  22541. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  22542. /* No need for the header guard as MBEDTLS_MEMORY_BUFFER_ALLOC_C
  22543. is dependent upon MBEDTLS_PLATFORM_C */
  22544. #include <string.h>
  22545. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  22546. #include <execinfo.h>
  22547. #endif
  22548. #if defined(MBEDTLS_THREADING_C)
  22549. #endif
  22550. /* Implementation that should never be optimized out by the compiler */
  22551. /* zeroize was here */
  22552. #define MAGIC1 0xFF00AA55
  22553. #define MAGIC2 0xEE119966
  22554. #define MAX_BT 20
  22555. typedef struct _memory_header memory_header;
  22556. struct _memory_header
  22557. {
  22558. size_t magic1;
  22559. size_t size;
  22560. size_t alloc;
  22561. memory_header *prev;
  22562. memory_header *next;
  22563. memory_header *prev_free;
  22564. memory_header *next_free;
  22565. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  22566. char **trace;
  22567. size_t trace_count;
  22568. #endif
  22569. size_t magic2;
  22570. };
  22571. typedef struct
  22572. {
  22573. unsigned char *buf;
  22574. size_t len;
  22575. memory_header *first;
  22576. memory_header *first_free;
  22577. int verify;
  22578. #if defined(MBEDTLS_MEMORY_DEBUG)
  22579. size_t alloc_count;
  22580. size_t free_count;
  22581. size_t total_used;
  22582. size_t maximum_used;
  22583. size_t header_count;
  22584. size_t maximum_header_count;
  22585. #endif
  22586. #if defined(MBEDTLS_THREADING_C)
  22587. mbedtls_threading_mutex_t mutex;
  22588. #endif
  22589. }
  22590. buffer_alloc_ctx;
  22591. static buffer_alloc_ctx heap;
  22592. #if defined(MBEDTLS_MEMORY_DEBUG)
  22593. static void debug_header( memory_header *hdr )
  22594. {
  22595. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  22596. size_t i;
  22597. #endif
  22598. mbedtls_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
  22599. "ALLOC(%zu), SIZE(%10zu)\n",
  22600. (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
  22601. hdr->alloc, hdr->size );
  22602. mbedtls_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
  22603. (size_t) hdr->prev_free, (size_t) hdr->next_free );
  22604. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  22605. mbedtls_fprintf( stderr, "TRACE: \n" );
  22606. for( i = 0; i < hdr->trace_count; i++ )
  22607. mbedtls_fprintf( stderr, "%s\n", hdr->trace[i] );
  22608. mbedtls_fprintf( stderr, "\n" );
  22609. #endif
  22610. }
  22611. static void debug_chain( void )
  22612. {
  22613. memory_header *cur = heap.first;
  22614. mbedtls_fprintf( stderr, "\nBlock list\n" );
  22615. while( cur != NULL )
  22616. {
  22617. debug_header( cur );
  22618. cur = cur->next;
  22619. }
  22620. mbedtls_fprintf( stderr, "Free list\n" );
  22621. cur = heap.first_free;
  22622. while( cur != NULL )
  22623. {
  22624. debug_header( cur );
  22625. cur = cur->next_free;
  22626. }
  22627. }
  22628. #endif /* MBEDTLS_MEMORY_DEBUG */
  22629. static int verify_header( memory_header *hdr )
  22630. {
  22631. if( hdr->magic1 != MAGIC1 )
  22632. {
  22633. #if defined(MBEDTLS_MEMORY_DEBUG)
  22634. mbedtls_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
  22635. #endif
  22636. return( 1 );
  22637. }
  22638. if( hdr->magic2 != MAGIC2 )
  22639. {
  22640. #if defined(MBEDTLS_MEMORY_DEBUG)
  22641. mbedtls_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
  22642. #endif
  22643. return( 1 );
  22644. }
  22645. if( hdr->alloc > 1 )
  22646. {
  22647. #if defined(MBEDTLS_MEMORY_DEBUG)
  22648. mbedtls_fprintf( stderr, "FATAL: alloc has illegal value\n" );
  22649. #endif
  22650. return( 1 );
  22651. }
  22652. if( hdr->prev != NULL && hdr->prev == hdr->next )
  22653. {
  22654. #if defined(MBEDTLS_MEMORY_DEBUG)
  22655. mbedtls_fprintf( stderr, "FATAL: prev == next\n" );
  22656. #endif
  22657. return( 1 );
  22658. }
  22659. if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
  22660. {
  22661. #if defined(MBEDTLS_MEMORY_DEBUG)
  22662. mbedtls_fprintf( stderr, "FATAL: prev_free == next_free\n" );
  22663. #endif
  22664. return( 1 );
  22665. }
  22666. return( 0 );
  22667. }
  22668. static int verify_chain( void )
  22669. {
  22670. memory_header *prv = heap.first, *cur;
  22671. if( prv == NULL || verify_header( prv ) != 0 )
  22672. {
  22673. #if defined(MBEDTLS_MEMORY_DEBUG)
  22674. mbedtls_fprintf( stderr, "FATAL: verification of first header "
  22675. "failed\n" );
  22676. #endif
  22677. return( 1 );
  22678. }
  22679. if( heap.first->prev != NULL )
  22680. {
  22681. #if defined(MBEDTLS_MEMORY_DEBUG)
  22682. mbedtls_fprintf( stderr, "FATAL: verification failed: "
  22683. "first->prev != NULL\n" );
  22684. #endif
  22685. return( 1 );
  22686. }
  22687. cur = heap.first->next;
  22688. while( cur != NULL )
  22689. {
  22690. if( verify_header( cur ) != 0 )
  22691. {
  22692. #if defined(MBEDTLS_MEMORY_DEBUG)
  22693. mbedtls_fprintf( stderr, "FATAL: verification of header "
  22694. "failed\n" );
  22695. #endif
  22696. return( 1 );
  22697. }
  22698. if( cur->prev != prv )
  22699. {
  22700. #if defined(MBEDTLS_MEMORY_DEBUG)
  22701. mbedtls_fprintf( stderr, "FATAL: verification failed: "
  22702. "cur->prev != prv\n" );
  22703. #endif
  22704. return( 1 );
  22705. }
  22706. prv = cur;
  22707. cur = cur->next;
  22708. }
  22709. return( 0 );
  22710. }
  22711. static void *buffer_alloc_calloc( size_t n, size_t size )
  22712. {
  22713. memory_header *new, *cur = heap.first_free;
  22714. unsigned char *p;
  22715. void *ret;
  22716. size_t original_len, len;
  22717. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  22718. void *trace_buffer[MAX_BT];
  22719. size_t trace_cnt;
  22720. #endif
  22721. if( heap.buf == NULL || heap.first == NULL )
  22722. return( NULL );
  22723. original_len = len = n * size;
  22724. if( n == 0 || size == 0 || len / n != size )
  22725. return( NULL );
  22726. else if( len > (size_t)-MBEDTLS_MEMORY_ALIGN_MULTIPLE )
  22727. return( NULL );
  22728. if( len % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
  22729. {
  22730. len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
  22731. len += MBEDTLS_MEMORY_ALIGN_MULTIPLE;
  22732. }
  22733. // Find block that fits
  22734. //
  22735. while( cur != NULL )
  22736. {
  22737. if( cur->size >= len )
  22738. break;
  22739. cur = cur->next_free;
  22740. }
  22741. if( cur == NULL )
  22742. return( NULL );
  22743. if( cur->alloc != 0 )
  22744. {
  22745. #if defined(MBEDTLS_MEMORY_DEBUG)
  22746. mbedtls_fprintf( stderr, "FATAL: block in free_list but allocated "
  22747. "data\n" );
  22748. #endif
  22749. mbedtls_exit( 1 );
  22750. }
  22751. #if defined(MBEDTLS_MEMORY_DEBUG)
  22752. heap.alloc_count++;
  22753. #endif
  22754. // Found location, split block if > memory_header + 4 room left
  22755. //
  22756. if( cur->size - len < sizeof(memory_header) +
  22757. MBEDTLS_MEMORY_ALIGN_MULTIPLE )
  22758. {
  22759. cur->alloc = 1;
  22760. // Remove from free_list
  22761. //
  22762. if( cur->prev_free != NULL )
  22763. cur->prev_free->next_free = cur->next_free;
  22764. else
  22765. heap.first_free = cur->next_free;
  22766. if( cur->next_free != NULL )
  22767. cur->next_free->prev_free = cur->prev_free;
  22768. cur->prev_free = NULL;
  22769. cur->next_free = NULL;
  22770. #if defined(MBEDTLS_MEMORY_DEBUG)
  22771. heap.total_used += cur->size;
  22772. if( heap.total_used > heap.maximum_used )
  22773. heap.maximum_used = heap.total_used;
  22774. #endif
  22775. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  22776. trace_cnt = backtrace( trace_buffer, MAX_BT );
  22777. cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
  22778. cur->trace_count = trace_cnt;
  22779. #endif
  22780. if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
  22781. mbedtls_exit( 1 );
  22782. ret = (unsigned char *) cur + sizeof( memory_header );
  22783. memset( ret, 0, original_len );
  22784. return( ret );
  22785. }
  22786. p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
  22787. new = (memory_header *) p;
  22788. new->size = cur->size - len - sizeof(memory_header);
  22789. new->alloc = 0;
  22790. new->prev = cur;
  22791. new->next = cur->next;
  22792. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  22793. new->trace = NULL;
  22794. new->trace_count = 0;
  22795. #endif
  22796. new->magic1 = MAGIC1;
  22797. new->magic2 = MAGIC2;
  22798. if( new->next != NULL )
  22799. new->next->prev = new;
  22800. // Replace cur with new in free_list
  22801. //
  22802. new->prev_free = cur->prev_free;
  22803. new->next_free = cur->next_free;
  22804. if( new->prev_free != NULL )
  22805. new->prev_free->next_free = new;
  22806. else
  22807. heap.first_free = new;
  22808. if( new->next_free != NULL )
  22809. new->next_free->prev_free = new;
  22810. cur->alloc = 1;
  22811. cur->size = len;
  22812. cur->next = new;
  22813. cur->prev_free = NULL;
  22814. cur->next_free = NULL;
  22815. #if defined(MBEDTLS_MEMORY_DEBUG)
  22816. heap.header_count++;
  22817. if( heap.header_count > heap.maximum_header_count )
  22818. heap.maximum_header_count = heap.header_count;
  22819. heap.total_used += cur->size;
  22820. if( heap.total_used > heap.maximum_used )
  22821. heap.maximum_used = heap.total_used;
  22822. #endif
  22823. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  22824. trace_cnt = backtrace( trace_buffer, MAX_BT );
  22825. cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
  22826. cur->trace_count = trace_cnt;
  22827. #endif
  22828. if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
  22829. mbedtls_exit( 1 );
  22830. ret = (unsigned char *) cur + sizeof( memory_header );
  22831. memset( ret, 0, original_len );
  22832. return( ret );
  22833. }
  22834. static void buffer_alloc_free( void *ptr )
  22835. {
  22836. memory_header *hdr, *old = NULL;
  22837. unsigned char *p = (unsigned char *) ptr;
  22838. if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
  22839. return;
  22840. if( p < heap.buf || p >= heap.buf + heap.len )
  22841. {
  22842. #if defined(MBEDTLS_MEMORY_DEBUG)
  22843. mbedtls_fprintf( stderr, "FATAL: mbedtls_free() outside of managed "
  22844. "space\n" );
  22845. #endif
  22846. mbedtls_exit( 1 );
  22847. }
  22848. p -= sizeof(memory_header);
  22849. hdr = (memory_header *) p;
  22850. if( verify_header( hdr ) != 0 )
  22851. mbedtls_exit( 1 );
  22852. if( hdr->alloc != 1 )
  22853. {
  22854. #if defined(MBEDTLS_MEMORY_DEBUG)
  22855. mbedtls_fprintf( stderr, "FATAL: mbedtls_free() on unallocated "
  22856. "data\n" );
  22857. #endif
  22858. mbedtls_exit( 1 );
  22859. }
  22860. hdr->alloc = 0;
  22861. #if defined(MBEDTLS_MEMORY_DEBUG)
  22862. heap.free_count++;
  22863. heap.total_used -= hdr->size;
  22864. #endif
  22865. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  22866. free( hdr->trace );
  22867. hdr->trace = NULL;
  22868. hdr->trace_count = 0;
  22869. #endif
  22870. // Regroup with block before
  22871. //
  22872. if( hdr->prev != NULL && hdr->prev->alloc == 0 )
  22873. {
  22874. #if defined(MBEDTLS_MEMORY_DEBUG)
  22875. heap.header_count--;
  22876. #endif
  22877. hdr->prev->size += sizeof(memory_header) + hdr->size;
  22878. hdr->prev->next = hdr->next;
  22879. old = hdr;
  22880. hdr = hdr->prev;
  22881. if( hdr->next != NULL )
  22882. hdr->next->prev = hdr;
  22883. memset( old, 0, sizeof(memory_header) );
  22884. }
  22885. // Regroup with block after
  22886. //
  22887. if( hdr->next != NULL && hdr->next->alloc == 0 )
  22888. {
  22889. #if defined(MBEDTLS_MEMORY_DEBUG)
  22890. heap.header_count--;
  22891. #endif
  22892. hdr->size += sizeof(memory_header) + hdr->next->size;
  22893. old = hdr->next;
  22894. hdr->next = hdr->next->next;
  22895. if( hdr->prev_free != NULL || hdr->next_free != NULL )
  22896. {
  22897. if( hdr->prev_free != NULL )
  22898. hdr->prev_free->next_free = hdr->next_free;
  22899. else
  22900. heap.first_free = hdr->next_free;
  22901. if( hdr->next_free != NULL )
  22902. hdr->next_free->prev_free = hdr->prev_free;
  22903. }
  22904. hdr->prev_free = old->prev_free;
  22905. hdr->next_free = old->next_free;
  22906. if( hdr->prev_free != NULL )
  22907. hdr->prev_free->next_free = hdr;
  22908. else
  22909. heap.first_free = hdr;
  22910. if( hdr->next_free != NULL )
  22911. hdr->next_free->prev_free = hdr;
  22912. if( hdr->next != NULL )
  22913. hdr->next->prev = hdr;
  22914. memset( old, 0, sizeof(memory_header) );
  22915. }
  22916. // Prepend to free_list if we have not merged
  22917. // (Does not have to stay in same order as prev / next list)
  22918. //
  22919. if( old == NULL )
  22920. {
  22921. hdr->next_free = heap.first_free;
  22922. if( heap.first_free != NULL )
  22923. heap.first_free->prev_free = hdr;
  22924. heap.first_free = hdr;
  22925. }
  22926. if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
  22927. mbedtls_exit( 1 );
  22928. }
  22929. void mbedtls_memory_buffer_set_verify( int verify )
  22930. {
  22931. heap.verify = verify;
  22932. }
  22933. int mbedtls_memory_buffer_alloc_verify( void )
  22934. {
  22935. return verify_chain();
  22936. }
  22937. #if defined(MBEDTLS_MEMORY_DEBUG)
  22938. void mbedtls_memory_buffer_alloc_status( void )
  22939. {
  22940. mbedtls_fprintf( stderr,
  22941. "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
  22942. "%zu bytes (total %zu bytes), alloc / free: %zu / %zu\n",
  22943. heap.header_count, heap.total_used,
  22944. heap.maximum_header_count, heap.maximum_used,
  22945. heap.maximum_header_count * sizeof( memory_header )
  22946. + heap.maximum_used,
  22947. heap.alloc_count, heap.free_count );
  22948. if( heap.first->next == NULL )
  22949. mbedtls_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
  22950. else
  22951. {
  22952. mbedtls_fprintf( stderr, "Memory currently allocated:\n" );
  22953. debug_chain();
  22954. }
  22955. }
  22956. void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
  22957. {
  22958. *max_used = heap.maximum_used;
  22959. *max_blocks = heap.maximum_header_count;
  22960. }
  22961. void mbedtls_memory_buffer_alloc_max_reset( void )
  22962. {
  22963. heap.maximum_used = 0;
  22964. heap.maximum_header_count = 0;
  22965. }
  22966. void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
  22967. {
  22968. *cur_used = heap.total_used;
  22969. *cur_blocks = heap.header_count;
  22970. }
  22971. #endif /* MBEDTLS_MEMORY_DEBUG */
  22972. #if defined(MBEDTLS_THREADING_C)
  22973. static void *buffer_alloc_calloc_mutexed( size_t n, size_t size )
  22974. {
  22975. void *buf;
  22976. if( mbedtls_mutex_lock( &heap.mutex ) != 0 )
  22977. return( NULL );
  22978. buf = buffer_alloc_calloc( n, size );
  22979. if( mbedtls_mutex_unlock( &heap.mutex ) )
  22980. return( NULL );
  22981. return( buf );
  22982. }
  22983. static void buffer_alloc_free_mutexed( void *ptr )
  22984. {
  22985. /* We have to good option here, but corrupting the heap seems
  22986. * worse than loosing memory. */
  22987. if( mbedtls_mutex_lock( &heap.mutex ) )
  22988. return;
  22989. buffer_alloc_free( ptr );
  22990. (void) mbedtls_mutex_unlock( &heap.mutex );
  22991. }
  22992. #endif /* MBEDTLS_THREADING_C */
  22993. void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len )
  22994. {
  22995. memset( &heap, 0, sizeof( buffer_alloc_ctx ) );
  22996. #if defined(MBEDTLS_THREADING_C)
  22997. mbedtls_mutex_init( &heap.mutex );
  22998. mbedtls_platform_set_calloc_free( buffer_alloc_calloc_mutexed,
  22999. buffer_alloc_free_mutexed );
  23000. #else
  23001. mbedtls_platform_set_calloc_free( buffer_alloc_calloc, buffer_alloc_free );
  23002. #endif
  23003. if( len < sizeof( memory_header ) + MBEDTLS_MEMORY_ALIGN_MULTIPLE )
  23004. return;
  23005. else if( (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
  23006. {
  23007. /* Adjust len first since buf is used in the computation */
  23008. len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE
  23009. - (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
  23010. buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE
  23011. - (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
  23012. }
  23013. memset( buf, 0, len );
  23014. heap.buf = buf;
  23015. heap.len = len;
  23016. heap.first = (memory_header *)buf;
  23017. heap.first->size = len - sizeof( memory_header );
  23018. heap.first->magic1 = MAGIC1;
  23019. heap.first->magic2 = MAGIC2;
  23020. heap.first_free = heap.first;
  23021. }
  23022. void mbedtls_memory_buffer_alloc_free( void )
  23023. {
  23024. #if defined(MBEDTLS_THREADING_C)
  23025. mbedtls_mutex_free( &heap.mutex );
  23026. #endif
  23027. mbedtls_zeroize( &heap, sizeof(buffer_alloc_ctx) );
  23028. }
  23029. #if defined(MBEDTLS_SELF_TEST)
  23030. static int check_pointer( void *p )
  23031. {
  23032. if( p == NULL )
  23033. return( -1 );
  23034. if( (size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0 )
  23035. return( -1 );
  23036. return( 0 );
  23037. }
  23038. static int check_all_free( void )
  23039. {
  23040. if(
  23041. #if defined(MBEDTLS_MEMORY_DEBUG)
  23042. heap.total_used != 0 ||
  23043. #endif
  23044. heap.first != heap.first_free ||
  23045. (void *) heap.first != (void *) heap.buf )
  23046. {
  23047. return( -1 );
  23048. }
  23049. return( 0 );
  23050. }
  23051. #define TEST_ASSERT( condition ) \
  23052. if( ! (condition) ) \
  23053. { \
  23054. if( verbose != 0 ) \
  23055. mbedtls_printf( "failed\n" ); \
  23056. \
  23057. ret = 1; \
  23058. goto cleanup; \
  23059. }
  23060. int mbedtls_memory_buffer_alloc_self_test( int verbose )
  23061. {
  23062. unsigned char buf[1024];
  23063. unsigned char *p, *q, *r, *end;
  23064. int ret = 0;
  23065. if( verbose != 0 )
  23066. mbedtls_printf( " MBA test #1 (basic alloc-free cycle): " );
  23067. mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
  23068. p = mbedtls_calloc( 1, 1 );
  23069. q = mbedtls_calloc( 1, 128 );
  23070. r = mbedtls_calloc( 1, 16 );
  23071. TEST_ASSERT( check_pointer( p ) == 0 &&
  23072. check_pointer( q ) == 0 &&
  23073. check_pointer( r ) == 0 );
  23074. mbedtls_free( r );
  23075. mbedtls_free( q );
  23076. mbedtls_free( p );
  23077. TEST_ASSERT( check_all_free( ) == 0 );
  23078. /* Memorize end to compare with the next test */
  23079. end = heap.buf + heap.len;
  23080. mbedtls_memory_buffer_alloc_free( );
  23081. if( verbose != 0 )
  23082. mbedtls_printf( "passed\n" );
  23083. if( verbose != 0 )
  23084. mbedtls_printf( " MBA test #2 (buf not aligned): " );
  23085. mbedtls_memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
  23086. TEST_ASSERT( heap.buf + heap.len == end );
  23087. p = mbedtls_calloc( 1, 1 );
  23088. q = mbedtls_calloc( 1, 128 );
  23089. r = mbedtls_calloc( 1, 16 );
  23090. TEST_ASSERT( check_pointer( p ) == 0 &&
  23091. check_pointer( q ) == 0 &&
  23092. check_pointer( r ) == 0 );
  23093. mbedtls_free( r );
  23094. mbedtls_free( q );
  23095. mbedtls_free( p );
  23096. TEST_ASSERT( check_all_free( ) == 0 );
  23097. mbedtls_memory_buffer_alloc_free( );
  23098. if( verbose != 0 )
  23099. mbedtls_printf( "passed\n" );
  23100. if( verbose != 0 )
  23101. mbedtls_printf( " MBA test #3 (full): " );
  23102. mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
  23103. p = mbedtls_calloc( 1, sizeof( buf ) - sizeof( memory_header ) );
  23104. TEST_ASSERT( check_pointer( p ) == 0 );
  23105. TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
  23106. mbedtls_free( p );
  23107. p = mbedtls_calloc( 1, sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
  23108. q = mbedtls_calloc( 1, 16 );
  23109. TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
  23110. TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
  23111. mbedtls_free( q );
  23112. TEST_ASSERT( mbedtls_calloc( 1, 17 ) == NULL );
  23113. mbedtls_free( p );
  23114. TEST_ASSERT( check_all_free( ) == 0 );
  23115. mbedtls_memory_buffer_alloc_free( );
  23116. if( verbose != 0 )
  23117. mbedtls_printf( "passed\n" );
  23118. cleanup:
  23119. mbedtls_memory_buffer_alloc_free( );
  23120. return( ret );
  23121. }
  23122. #endif /* MBEDTLS_SELF_TEST */
  23123. #endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
  23124. /*
  23125. Amalgamated build undefines
  23126. */
  23127. #undef ADD
  23128. #undef BC
  23129. #undef BEFORE_COLON
  23130. #undef F
  23131. #undef F0
  23132. #undef F1
  23133. #undef F2
  23134. #undef F3
  23135. #undef F4
  23136. #undef F5
  23137. #undef FSb
  23138. #undef K
  23139. #undef KK
  23140. #undef P
  23141. #undef R
  23142. #undef ROTR
  23143. #undef S
  23144. #undef S0
  23145. #undef S1
  23146. #undef S2
  23147. #undef S3
  23148. #undef SAFE_SNPRINTF
  23149. #undef SHR
  23150. #undef close
  23151. #undef read
  23152. #undef supported_init
  23153. #undef write
  23154. /********* Start of file library/net_sockets.c ************/
  23155. /*
  23156. * TCP/IP or UDP/IP networking functions
  23157. *
  23158. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  23159. * SPDX-License-Identifier: Apache-2.0
  23160. *
  23161. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  23162. * not use this file except in compliance with the License.
  23163. * You may obtain a copy of the License at
  23164. *
  23165. * http://www.apache.org/licenses/LICENSE-2.0
  23166. *
  23167. * Unless required by applicable law or agreed to in writing, software
  23168. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  23169. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23170. * See the License for the specific language governing permissions and
  23171. * limitations under the License.
  23172. *
  23173. * This file is part of mbed TLS (https://tls.mbed.org)
  23174. */
  23175. #if !defined(MBEDTLS_CONFIG_FILE)
  23176. #else
  23177. #endif
  23178. #if defined(MBEDTLS_NET_C)
  23179. #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
  23180. !defined(__APPLE__) && !defined(_WIN32)
  23181. #error "This module only works on Unix and Windows, see MBEDTLS_NET_C in config.h"
  23182. #endif
  23183. #if defined(MBEDTLS_PLATFORM_C)
  23184. #else
  23185. #include <stdlib.h>
  23186. #endif
  23187. #include <string.h>
  23188. #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
  23189. !defined(EFI32)
  23190. #ifdef _WIN32_WINNT
  23191. #undef _WIN32_WINNT
  23192. #endif
  23193. /* Enables getaddrinfo() & Co */
  23194. #define _WIN32_WINNT 0x0501
  23195. #include <ws2tcpip.h>
  23196. #include <winsock2.h>
  23197. #include <windows.h>
  23198. #if defined(_MSC_VER)
  23199. #if defined(_WIN32_WCE)
  23200. #pragma comment( lib, "ws2.lib" )
  23201. #else
  23202. #pragma comment( lib, "ws2_32.lib" )
  23203. #endif
  23204. #endif /* _MSC_VER */
  23205. #define read(fd,buf,len) recv( fd, (char*)( buf ), (int)( len ), 0 )
  23206. #define write(fd,buf,len) send( fd, (char*)( buf ), (int)( len ), 0 )
  23207. #define close(fd) closesocket(fd)
  23208. static int wsa_init_done = 0;
  23209. #else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
  23210. #include <sys/types.h>
  23211. #include <sys/socket.h>
  23212. #include <netinet/in.h>
  23213. #include <arpa/inet.h>
  23214. #include <sys/time.h>
  23215. #include <unistd.h>
  23216. #include <signal.h>
  23217. #include <fcntl.h>
  23218. #include <netdb.h>
  23219. #include <errno.h>
  23220. #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
  23221. /* Some MS functions want int and MSVC warns if we pass size_t,
  23222. * but the standard functions use socklen_t, so cast only for MSVC */
  23223. #if defined(_MSC_VER)
  23224. #define MSVC_INT_CAST (int)
  23225. #else
  23226. #define MSVC_INT_CAST
  23227. #endif
  23228. #include <stdio.h>
  23229. #include <time.h>
  23230. #include <stdint.h>
  23231. /*
  23232. * Prepare for using the sockets interface
  23233. */
  23234. static int net_prepare( void )
  23235. {
  23236. #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
  23237. !defined(EFI32)
  23238. WSADATA wsaData;
  23239. if( wsa_init_done == 0 )
  23240. {
  23241. if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
  23242. return( MBEDTLS_ERR_NET_SOCKET_FAILED );
  23243. wsa_init_done = 1;
  23244. }
  23245. #else
  23246. #if !defined(EFIX64) && !defined(EFI32)
  23247. signal( SIGPIPE, SIG_IGN );
  23248. #endif
  23249. #endif
  23250. return( 0 );
  23251. }
  23252. /*
  23253. * Initialize a context
  23254. */
  23255. void mbedtls_net_init( mbedtls_net_context *ctx )
  23256. {
  23257. ctx->fd = -1;
  23258. }
  23259. /*
  23260. * Initiate a TCP connection with host:port and the given protocol
  23261. */
  23262. int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host,
  23263. const char *port, int proto )
  23264. {
  23265. int ret;
  23266. struct addrinfo hints, *addr_list, *cur;
  23267. if( ( ret = net_prepare() ) != 0 )
  23268. return( ret );
  23269. /* Do name resolution with both IPv6 and IPv4 */
  23270. memset( &hints, 0, sizeof( hints ) );
  23271. hints.ai_family = AF_UNSPEC;
  23272. hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
  23273. hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
  23274. if( getaddrinfo( host, port, &hints, &addr_list ) != 0 )
  23275. return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
  23276. /* Try the sockaddrs until a connection succeeds */
  23277. ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
  23278. for( cur = addr_list; cur != NULL; cur = cur->ai_next )
  23279. {
  23280. ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
  23281. cur->ai_protocol );
  23282. if( ctx->fd < 0 )
  23283. {
  23284. ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
  23285. continue;
  23286. }
  23287. if( connect( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) == 0 )
  23288. {
  23289. ret = 0;
  23290. break;
  23291. }
  23292. close( ctx->fd );
  23293. ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
  23294. }
  23295. freeaddrinfo( addr_list );
  23296. return( ret );
  23297. }
  23298. /*
  23299. * Create a listening socket on bind_ip:port
  23300. */
  23301. int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto )
  23302. {
  23303. int n, ret;
  23304. struct addrinfo hints, *addr_list, *cur;
  23305. if( ( ret = net_prepare() ) != 0 )
  23306. return( ret );
  23307. /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
  23308. memset( &hints, 0, sizeof( hints ) );
  23309. hints.ai_family = AF_UNSPEC;
  23310. hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
  23311. hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
  23312. if( bind_ip == NULL )
  23313. hints.ai_flags = AI_PASSIVE;
  23314. if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 )
  23315. return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
  23316. /* Try the sockaddrs until a binding succeeds */
  23317. ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
  23318. for( cur = addr_list; cur != NULL; cur = cur->ai_next )
  23319. {
  23320. ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
  23321. cur->ai_protocol );
  23322. if( ctx->fd < 0 )
  23323. {
  23324. ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
  23325. continue;
  23326. }
  23327. n = 1;
  23328. if( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR,
  23329. (const char *) &n, sizeof( n ) ) != 0 )
  23330. {
  23331. close( ctx->fd );
  23332. ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
  23333. continue;
  23334. }
  23335. if( bind( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) != 0 )
  23336. {
  23337. close( ctx->fd );
  23338. ret = MBEDTLS_ERR_NET_BIND_FAILED;
  23339. continue;
  23340. }
  23341. /* Listen only makes sense for TCP */
  23342. if( proto == MBEDTLS_NET_PROTO_TCP )
  23343. {
  23344. if( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 )
  23345. {
  23346. close( ctx->fd );
  23347. ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
  23348. continue;
  23349. }
  23350. }
  23351. /* Bind was successful */
  23352. ret = 0;
  23353. break;
  23354. }
  23355. freeaddrinfo( addr_list );
  23356. return( ret );
  23357. }
  23358. #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
  23359. !defined(EFI32)
  23360. /*
  23361. * Check if the requested operation would be blocking on a non-blocking socket
  23362. * and thus 'failed' with a negative return value.
  23363. */
  23364. static int net_would_block( const mbedtls_net_context *ctx )
  23365. {
  23366. ((void) ctx);
  23367. return( WSAGetLastError() == WSAEWOULDBLOCK );
  23368. }
  23369. #else
  23370. /*
  23371. * Check if the requested operation would be blocking on a non-blocking socket
  23372. * and thus 'failed' with a negative return value.
  23373. *
  23374. * Note: on a blocking socket this function always returns 0!
  23375. */
  23376. static int net_would_block( const mbedtls_net_context *ctx )
  23377. {
  23378. int err = errno;
  23379. /*
  23380. * Never return 'WOULD BLOCK' on a non-blocking socket
  23381. */
  23382. if( ( fcntl( ctx->fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
  23383. {
  23384. errno = err;
  23385. return( 0 );
  23386. }
  23387. switch( errno = err )
  23388. {
  23389. #if defined EAGAIN
  23390. case EAGAIN:
  23391. #endif
  23392. #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
  23393. case EWOULDBLOCK:
  23394. #endif
  23395. return( 1 );
  23396. }
  23397. return( 0 );
  23398. }
  23399. #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
  23400. /*
  23401. * Accept a connection from a remote client
  23402. */
  23403. int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
  23404. mbedtls_net_context *client_ctx,
  23405. void *client_ip, size_t buf_size, size_t *ip_len )
  23406. {
  23407. int ret;
  23408. int type;
  23409. struct sockaddr_storage client_addr;
  23410. #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
  23411. defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t)
  23412. socklen_t n = (socklen_t) sizeof( client_addr );
  23413. socklen_t type_len = (socklen_t) sizeof( type );
  23414. #else
  23415. int n = (int) sizeof( client_addr );
  23416. int type_len = (int) sizeof( type );
  23417. #endif
  23418. /* Is this a TCP or UDP socket? */
  23419. if( getsockopt( bind_ctx->fd, SOL_SOCKET, SO_TYPE,
  23420. (void *) &type, &type_len ) != 0 ||
  23421. ( type != SOCK_STREAM && type != SOCK_DGRAM ) )
  23422. {
  23423. return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
  23424. }
  23425. if( type == SOCK_STREAM )
  23426. {
  23427. /* TCP: actual accept() */
  23428. ret = client_ctx->fd = (int) accept( bind_ctx->fd,
  23429. (struct sockaddr *) &client_addr, &n );
  23430. }
  23431. else
  23432. {
  23433. /* UDP: wait for a message, but keep it in the queue */
  23434. char buf[1] = { 0 };
  23435. ret = (int) recvfrom( bind_ctx->fd, buf, sizeof( buf ), MSG_PEEK,
  23436. (struct sockaddr *) &client_addr, &n );
  23437. #if defined(_WIN32)
  23438. if( ret == SOCKET_ERROR &&
  23439. WSAGetLastError() == WSAEMSGSIZE )
  23440. {
  23441. /* We know buf is too small, thanks, just peeking here */
  23442. ret = 0;
  23443. }
  23444. #endif
  23445. }
  23446. if( ret < 0 )
  23447. {
  23448. if( net_would_block( bind_ctx ) != 0 )
  23449. return( MBEDTLS_ERR_SSL_WANT_READ );
  23450. return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
  23451. }
  23452. /* UDP: hijack the listening socket to communicate with the client,
  23453. * then bind a new socket to accept new connections */
  23454. if( type != SOCK_STREAM )
  23455. {
  23456. struct sockaddr_storage local_addr;
  23457. int one = 1;
  23458. if( connect( bind_ctx->fd, (struct sockaddr *) &client_addr, n ) != 0 )
  23459. return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
  23460. client_ctx->fd = bind_ctx->fd;
  23461. bind_ctx->fd = -1; /* In case we exit early */
  23462. n = sizeof( struct sockaddr_storage );
  23463. if( getsockname( client_ctx->fd,
  23464. (struct sockaddr *) &local_addr, &n ) != 0 ||
  23465. ( bind_ctx->fd = (int) socket( local_addr.ss_family,
  23466. SOCK_DGRAM, IPPROTO_UDP ) ) < 0 ||
  23467. setsockopt( bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
  23468. (const char *) &one, sizeof( one ) ) != 0 )
  23469. {
  23470. return( MBEDTLS_ERR_NET_SOCKET_FAILED );
  23471. }
  23472. if( bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0 )
  23473. {
  23474. return( MBEDTLS_ERR_NET_BIND_FAILED );
  23475. }
  23476. }
  23477. if( client_ip != NULL )
  23478. {
  23479. if( client_addr.ss_family == AF_INET )
  23480. {
  23481. struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
  23482. *ip_len = sizeof( addr4->sin_addr.s_addr );
  23483. if( buf_size < *ip_len )
  23484. return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
  23485. memcpy( client_ip, &addr4->sin_addr.s_addr, *ip_len );
  23486. }
  23487. else
  23488. {
  23489. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
  23490. *ip_len = sizeof( addr6->sin6_addr.s6_addr );
  23491. if( buf_size < *ip_len )
  23492. return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
  23493. memcpy( client_ip, &addr6->sin6_addr.s6_addr, *ip_len);
  23494. }
  23495. }
  23496. return( 0 );
  23497. }
  23498. /*
  23499. * Set the socket blocking or non-blocking
  23500. */
  23501. int mbedtls_net_set_block( mbedtls_net_context *ctx )
  23502. {
  23503. #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
  23504. !defined(EFI32)
  23505. u_long n = 0;
  23506. return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
  23507. #else
  23508. return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) & ~O_NONBLOCK ) );
  23509. #endif
  23510. }
  23511. int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
  23512. {
  23513. #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
  23514. !defined(EFI32)
  23515. u_long n = 1;
  23516. return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
  23517. #else
  23518. return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) | O_NONBLOCK ) );
  23519. #endif
  23520. }
  23521. /*
  23522. * Portable usleep helper
  23523. */
  23524. void mbedtls_net_usleep( unsigned long usec )
  23525. {
  23526. #if defined(_WIN32)
  23527. Sleep( ( usec + 999 ) / 1000 );
  23528. #else
  23529. struct timeval tv;
  23530. tv.tv_sec = usec / 1000000;
  23531. #if defined(__unix__) || defined(__unix) || \
  23532. ( defined(__APPLE__) && defined(__MACH__) )
  23533. tv.tv_usec = (suseconds_t) usec % 1000000;
  23534. #else
  23535. tv.tv_usec = usec % 1000000;
  23536. #endif
  23537. select( 0, NULL, NULL, NULL, &tv );
  23538. #endif
  23539. }
  23540. /*
  23541. * Read at most 'len' characters
  23542. */
  23543. int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
  23544. {
  23545. int ret;
  23546. int fd = ((mbedtls_net_context *) ctx)->fd;
  23547. if( fd < 0 )
  23548. return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
  23549. ret = (int) read( fd, buf, len );
  23550. if( ret < 0 )
  23551. {
  23552. if( net_would_block( ctx ) != 0 )
  23553. return( MBEDTLS_ERR_SSL_WANT_READ );
  23554. #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
  23555. !defined(EFI32)
  23556. if( WSAGetLastError() == WSAECONNRESET )
  23557. return( MBEDTLS_ERR_NET_CONN_RESET );
  23558. #else
  23559. if( errno == EPIPE || errno == ECONNRESET )
  23560. return( MBEDTLS_ERR_NET_CONN_RESET );
  23561. if( errno == EINTR )
  23562. return( MBEDTLS_ERR_SSL_WANT_READ );
  23563. #endif
  23564. return( MBEDTLS_ERR_NET_RECV_FAILED );
  23565. }
  23566. return( ret );
  23567. }
  23568. /*
  23569. * Read at most 'len' characters, blocking for at most 'timeout' ms
  23570. */
  23571. int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
  23572. uint32_t timeout )
  23573. {
  23574. int ret;
  23575. struct timeval tv;
  23576. fd_set read_fds;
  23577. int fd = ((mbedtls_net_context *) ctx)->fd;
  23578. if( fd < 0 )
  23579. return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
  23580. FD_ZERO( &read_fds );
  23581. FD_SET( fd, &read_fds );
  23582. tv.tv_sec = timeout / 1000;
  23583. tv.tv_usec = ( timeout % 1000 ) * 1000;
  23584. ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );
  23585. /* Zero fds ready means we timed out */
  23586. if( ret == 0 )
  23587. return( MBEDTLS_ERR_SSL_TIMEOUT );
  23588. if( ret < 0 )
  23589. {
  23590. #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
  23591. !defined(EFI32)
  23592. if( WSAGetLastError() == WSAEINTR )
  23593. return( MBEDTLS_ERR_SSL_WANT_READ );
  23594. #else
  23595. if( errno == EINTR )
  23596. return( MBEDTLS_ERR_SSL_WANT_READ );
  23597. #endif
  23598. return( MBEDTLS_ERR_NET_RECV_FAILED );
  23599. }
  23600. /* This call will not block */
  23601. return( mbedtls_net_recv( ctx, buf, len ) );
  23602. }
  23603. /*
  23604. * Write at most 'len' characters
  23605. */
  23606. int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
  23607. {
  23608. int ret;
  23609. int fd = ((mbedtls_net_context *) ctx)->fd;
  23610. if( fd < 0 )
  23611. return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
  23612. ret = (int) write( fd, buf, len );
  23613. if( ret < 0 )
  23614. {
  23615. if( net_would_block( ctx ) != 0 )
  23616. return( MBEDTLS_ERR_SSL_WANT_WRITE );
  23617. #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
  23618. !defined(EFI32)
  23619. if( WSAGetLastError() == WSAECONNRESET )
  23620. return( MBEDTLS_ERR_NET_CONN_RESET );
  23621. #else
  23622. if( errno == EPIPE || errno == ECONNRESET )
  23623. return( MBEDTLS_ERR_NET_CONN_RESET );
  23624. if( errno == EINTR )
  23625. return( MBEDTLS_ERR_SSL_WANT_WRITE );
  23626. #endif
  23627. return( MBEDTLS_ERR_NET_SEND_FAILED );
  23628. }
  23629. return( ret );
  23630. }
  23631. /*
  23632. * Gracefully close the connection
  23633. */
  23634. void mbedtls_net_free( mbedtls_net_context *ctx )
  23635. {
  23636. if( ctx->fd == -1 )
  23637. return;
  23638. shutdown( ctx->fd, 2 );
  23639. close( ctx->fd );
  23640. ctx->fd = -1;
  23641. }
  23642. #endif /* MBEDTLS_NET_C */
  23643. /*
  23644. Amalgamated build undefines
  23645. */
  23646. #undef ADD
  23647. #undef BC
  23648. #undef BEFORE_COLON
  23649. #undef F
  23650. #undef F0
  23651. #undef F1
  23652. #undef F2
  23653. #undef F3
  23654. #undef F4
  23655. #undef F5
  23656. #undef FSb
  23657. #undef K
  23658. #undef KK
  23659. #undef P
  23660. #undef R
  23661. #undef ROTR
  23662. #undef S
  23663. #undef S0
  23664. #undef S1
  23665. #undef S2
  23666. #undef S3
  23667. #undef SAFE_SNPRINTF
  23668. #undef SHR
  23669. #undef close
  23670. #undef read
  23671. #undef supported_init
  23672. #undef write
  23673. /********* Start of file library/oid.c ************/
  23674. /**
  23675. * \file oid.c
  23676. *
  23677. * \brief Object Identifier (OID) database
  23678. *
  23679. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  23680. * SPDX-License-Identifier: Apache-2.0
  23681. *
  23682. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  23683. * not use this file except in compliance with the License.
  23684. * You may obtain a copy of the License at
  23685. *
  23686. * http://www.apache.org/licenses/LICENSE-2.0
  23687. *
  23688. * Unless required by applicable law or agreed to in writing, software
  23689. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  23690. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23691. * See the License for the specific language governing permissions and
  23692. * limitations under the License.
  23693. *
  23694. * This file is part of mbed TLS (https://tls.mbed.org)
  23695. */
  23696. #if !defined(MBEDTLS_CONFIG_FILE)
  23697. #else
  23698. #endif
  23699. #if defined(MBEDTLS_OID_C)
  23700. #include <stdio.h>
  23701. #include <string.h>
  23702. #if defined(MBEDTLS_PLATFORM_C)
  23703. #else
  23704. #define mbedtls_snprintf snprintf
  23705. #endif
  23706. #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C)
  23707. #endif
  23708. /*
  23709. * Macro to automatically add the size of #define'd OIDs
  23710. */
  23711. #define ADD_LEN(s) s, MBEDTLS_OID_SIZE(s)
  23712. /*
  23713. * Macro to generate an internal function for oid_XXX_from_asn1() (used by
  23714. * the other functions)
  23715. */
  23716. #define FN_OID_TYPED_FROM_ASN1( TYPE_T, NAME, LIST ) \
  23717. static const TYPE_T * oid_ ## NAME ## _from_asn1( const mbedtls_asn1_buf *oid ) \
  23718. { \
  23719. const TYPE_T *p = LIST; \
  23720. const mbedtls_oid_descriptor_t *cur = (const mbedtls_oid_descriptor_t *) p; \
  23721. if( p == NULL || oid == NULL ) return( NULL ); \
  23722. while( cur->asn1 != NULL ) { \
  23723. if( cur->asn1_len == oid->len && \
  23724. memcmp( cur->asn1, oid->p, oid->len ) == 0 ) { \
  23725. return( p ); \
  23726. } \
  23727. p++; \
  23728. cur = (const mbedtls_oid_descriptor_t *) p; \
  23729. } \
  23730. return( NULL ); \
  23731. }
  23732. /*
  23733. * Macro to generate a function for retrieving a single attribute from the
  23734. * descriptor of an mbedtls_oid_descriptor_t wrapper.
  23735. */
  23736. #define FN_OID_GET_DESCRIPTOR_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
  23737. int FN_NAME( const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
  23738. { \
  23739. const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
  23740. if( data == NULL ) return( MBEDTLS_ERR_OID_NOT_FOUND ); \
  23741. *ATTR1 = data->descriptor.ATTR1; \
  23742. return( 0 ); \
  23743. }
  23744. /*
  23745. * Macro to generate a function for retrieving a single attribute from an
  23746. * mbedtls_oid_descriptor_t wrapper.
  23747. */
  23748. #define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
  23749. int FN_NAME( const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
  23750. { \
  23751. const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
  23752. if( data == NULL ) return( MBEDTLS_ERR_OID_NOT_FOUND ); \
  23753. *ATTR1 = data->ATTR1; \
  23754. return( 0 ); \
  23755. }
  23756. /*
  23757. * Macro to generate a function for retrieving two attributes from an
  23758. * mbedtls_oid_descriptor_t wrapper.
  23759. */
  23760. #define FN_OID_GET_ATTR2(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1, \
  23761. ATTR2_TYPE, ATTR2) \
  23762. int FN_NAME( const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1, ATTR2_TYPE * ATTR2 ) \
  23763. { \
  23764. const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
  23765. if( data == NULL ) return( MBEDTLS_ERR_OID_NOT_FOUND ); \
  23766. *ATTR1 = data->ATTR1; \
  23767. *ATTR2 = data->ATTR2; \
  23768. return( 0 ); \
  23769. }
  23770. /*
  23771. * Macro to generate a function for retrieving the OID based on a single
  23772. * attribute from a mbedtls_oid_descriptor_t wrapper.
  23773. */
  23774. #define FN_OID_GET_OID_BY_ATTR1(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1) \
  23775. int FN_NAME( ATTR1_TYPE ATTR1, const char **oid, size_t *olen ) \
  23776. { \
  23777. const TYPE_T *cur = LIST; \
  23778. while( cur->descriptor.asn1 != NULL ) { \
  23779. if( cur->ATTR1 == ATTR1 ) { \
  23780. *oid = cur->descriptor.asn1; \
  23781. *olen = cur->descriptor.asn1_len; \
  23782. return( 0 ); \
  23783. } \
  23784. cur++; \
  23785. } \
  23786. return( MBEDTLS_ERR_OID_NOT_FOUND ); \
  23787. }
  23788. /*
  23789. * Macro to generate a function for retrieving the OID based on two
  23790. * attributes from a mbedtls_oid_descriptor_t wrapper.
  23791. */
  23792. #define FN_OID_GET_OID_BY_ATTR2(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1, \
  23793. ATTR2_TYPE, ATTR2) \
  23794. int FN_NAME( ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid , \
  23795. size_t *olen ) \
  23796. { \
  23797. const TYPE_T *cur = LIST; \
  23798. while( cur->descriptor.asn1 != NULL ) { \
  23799. if( cur->ATTR1 == ATTR1 && cur->ATTR2 == ATTR2 ) { \
  23800. *oid = cur->descriptor.asn1; \
  23801. *olen = cur->descriptor.asn1_len; \
  23802. return( 0 ); \
  23803. } \
  23804. cur++; \
  23805. } \
  23806. return( MBEDTLS_ERR_OID_NOT_FOUND ); \
  23807. }
  23808. #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C)
  23809. /*
  23810. * For X520 attribute types
  23811. */
  23812. typedef struct {
  23813. mbedtls_oid_descriptor_t descriptor;
  23814. const char *short_name;
  23815. } oid_x520_attr_t;
  23816. static const oid_x520_attr_t oid_x520_attr_type[] =
  23817. {
  23818. {
  23819. { ADD_LEN( MBEDTLS_OID_AT_CN ), "id-at-commonName", "Common Name" },
  23820. "CN",
  23821. },
  23822. {
  23823. { ADD_LEN( MBEDTLS_OID_AT_COUNTRY ), "id-at-countryName", "Country" },
  23824. "C",
  23825. },
  23826. {
  23827. { ADD_LEN( MBEDTLS_OID_AT_LOCALITY ), "id-at-locality", "Locality" },
  23828. "L",
  23829. },
  23830. {
  23831. { ADD_LEN( MBEDTLS_OID_AT_STATE ), "id-at-state", "State" },
  23832. "ST",
  23833. },
  23834. {
  23835. { ADD_LEN( MBEDTLS_OID_AT_ORGANIZATION ),"id-at-organizationName", "Organization" },
  23836. "O",
  23837. },
  23838. {
  23839. { ADD_LEN( MBEDTLS_OID_AT_ORG_UNIT ), "id-at-organizationalUnitName", "Org Unit" },
  23840. "OU",
  23841. },
  23842. {
  23843. { ADD_LEN( MBEDTLS_OID_PKCS9_EMAIL ), "emailAddress", "E-mail address" },
  23844. "emailAddress",
  23845. },
  23846. {
  23847. { ADD_LEN( MBEDTLS_OID_AT_SERIAL_NUMBER ),"id-at-serialNumber", "Serial number" },
  23848. "serialNumber",
  23849. },
  23850. {
  23851. { ADD_LEN( MBEDTLS_OID_AT_POSTAL_ADDRESS ),"id-at-postalAddress", "Postal address" },
  23852. "postalAddress",
  23853. },
  23854. {
  23855. { ADD_LEN( MBEDTLS_OID_AT_POSTAL_CODE ), "id-at-postalCode", "Postal code" },
  23856. "postalCode",
  23857. },
  23858. {
  23859. { ADD_LEN( MBEDTLS_OID_AT_SUR_NAME ), "id-at-surName", "Surname" },
  23860. "SN",
  23861. },
  23862. {
  23863. { ADD_LEN( MBEDTLS_OID_AT_GIVEN_NAME ), "id-at-givenName", "Given name" },
  23864. "GN",
  23865. },
  23866. {
  23867. { ADD_LEN( MBEDTLS_OID_AT_INITIALS ), "id-at-initials", "Initials" },
  23868. "initials",
  23869. },
  23870. {
  23871. { ADD_LEN( MBEDTLS_OID_AT_GENERATION_QUALIFIER ), "id-at-generationQualifier", "Generation qualifier" },
  23872. "generationQualifier",
  23873. },
  23874. {
  23875. { ADD_LEN( MBEDTLS_OID_AT_TITLE ), "id-at-title", "Title" },
  23876. "title",
  23877. },
  23878. {
  23879. { ADD_LEN( MBEDTLS_OID_AT_DN_QUALIFIER ),"id-at-dnQualifier", "Distinguished Name qualifier" },
  23880. "dnQualifier",
  23881. },
  23882. {
  23883. { ADD_LEN( MBEDTLS_OID_AT_PSEUDONYM ), "id-at-pseudonym", "Pseudonym" },
  23884. "pseudonym",
  23885. },
  23886. {
  23887. { ADD_LEN( MBEDTLS_OID_DOMAIN_COMPONENT ), "id-domainComponent", "Domain component" },
  23888. "DC",
  23889. },
  23890. {
  23891. { ADD_LEN( MBEDTLS_OID_AT_UNIQUE_IDENTIFIER ), "id-at-uniqueIdentifier", "Unique Identifier" },
  23892. "uniqueIdentifier",
  23893. },
  23894. {
  23895. { NULL, 0, NULL, NULL },
  23896. NULL,
  23897. }
  23898. };
  23899. FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type)
  23900. FN_OID_GET_ATTR1(mbedtls_oid_get_attr_short_name, oid_x520_attr_t, x520_attr, const char *, short_name)
  23901. /*
  23902. * For X509 extensions
  23903. */
  23904. typedef struct {
  23905. mbedtls_oid_descriptor_t descriptor;
  23906. int ext_type;
  23907. } oid_x509_ext_t;
  23908. static const oid_x509_ext_t oid_x509_ext[] =
  23909. {
  23910. {
  23911. { ADD_LEN( MBEDTLS_OID_BASIC_CONSTRAINTS ), "id-ce-basicConstraints", "Basic Constraints" },
  23912. MBEDTLS_X509_EXT_BASIC_CONSTRAINTS,
  23913. },
  23914. {
  23915. { ADD_LEN( MBEDTLS_OID_KEY_USAGE ), "id-ce-keyUsage", "Key Usage" },
  23916. MBEDTLS_X509_EXT_KEY_USAGE,
  23917. },
  23918. {
  23919. { ADD_LEN( MBEDTLS_OID_EXTENDED_KEY_USAGE ), "id-ce-extKeyUsage", "Extended Key Usage" },
  23920. MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE,
  23921. },
  23922. {
  23923. { ADD_LEN( MBEDTLS_OID_SUBJECT_ALT_NAME ), "id-ce-subjectAltName", "Subject Alt Name" },
  23924. MBEDTLS_X509_EXT_SUBJECT_ALT_NAME,
  23925. },
  23926. {
  23927. { ADD_LEN( MBEDTLS_OID_NS_CERT_TYPE ), "id-netscape-certtype", "Netscape Certificate Type" },
  23928. MBEDTLS_X509_EXT_NS_CERT_TYPE,
  23929. },
  23930. {
  23931. { NULL, 0, NULL, NULL },
  23932. 0,
  23933. },
  23934. };
  23935. FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext)
  23936. FN_OID_GET_ATTR1(mbedtls_oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type)
  23937. static const mbedtls_oid_descriptor_t oid_ext_key_usage[] =
  23938. {
  23939. { ADD_LEN( MBEDTLS_OID_SERVER_AUTH ), "id-kp-serverAuth", "TLS Web Server Authentication" },
  23940. { ADD_LEN( MBEDTLS_OID_CLIENT_AUTH ), "id-kp-clientAuth", "TLS Web Client Authentication" },
  23941. { ADD_LEN( MBEDTLS_OID_CODE_SIGNING ), "id-kp-codeSigning", "Code Signing" },
  23942. { ADD_LEN( MBEDTLS_OID_EMAIL_PROTECTION ), "id-kp-emailProtection", "E-mail Protection" },
  23943. { ADD_LEN( MBEDTLS_OID_TIME_STAMPING ), "id-kp-timeStamping", "Time Stamping" },
  23944. { ADD_LEN( MBEDTLS_OID_OCSP_SIGNING ), "id-kp-OCSPSigning", "OCSP Signing" },
  23945. { NULL, 0, NULL, NULL },
  23946. };
  23947. FN_OID_TYPED_FROM_ASN1(mbedtls_oid_descriptor_t, ext_key_usage, oid_ext_key_usage)
  23948. FN_OID_GET_ATTR1(mbedtls_oid_get_extended_key_usage, mbedtls_oid_descriptor_t, ext_key_usage, const char *, description)
  23949. #endif /* MBEDTLS_X509_USE_C || MBEDTLS_X509_CREATE_C */
  23950. #if defined(MBEDTLS_MD_C)
  23951. /*
  23952. * For SignatureAlgorithmIdentifier
  23953. */
  23954. typedef struct {
  23955. mbedtls_oid_descriptor_t descriptor;
  23956. mbedtls_md_type_t md_alg;
  23957. mbedtls_pk_type_t pk_alg;
  23958. } oid_sig_alg_t;
  23959. static const oid_sig_alg_t oid_sig_alg[] =
  23960. {
  23961. #if defined(MBEDTLS_RSA_C)
  23962. #if defined(MBEDTLS_MD2_C)
  23963. {
  23964. { ADD_LEN( MBEDTLS_OID_PKCS1_MD2 ), "md2WithRSAEncryption", "RSA with MD2" },
  23965. MBEDTLS_MD_MD2, MBEDTLS_PK_RSA,
  23966. },
  23967. #endif /* MBEDTLS_MD2_C */
  23968. #if defined(MBEDTLS_MD4_C)
  23969. {
  23970. { ADD_LEN( MBEDTLS_OID_PKCS1_MD4 ), "md4WithRSAEncryption", "RSA with MD4" },
  23971. MBEDTLS_MD_MD4, MBEDTLS_PK_RSA,
  23972. },
  23973. #endif /* MBEDTLS_MD4_C */
  23974. #if defined(MBEDTLS_MD5_C)
  23975. {
  23976. { ADD_LEN( MBEDTLS_OID_PKCS1_MD5 ), "md5WithRSAEncryption", "RSA with MD5" },
  23977. MBEDTLS_MD_MD5, MBEDTLS_PK_RSA,
  23978. },
  23979. #endif /* MBEDTLS_MD5_C */
  23980. #if defined(MBEDTLS_SHA1_C)
  23981. {
  23982. { ADD_LEN( MBEDTLS_OID_PKCS1_SHA1 ), "sha-1WithRSAEncryption", "RSA with SHA1" },
  23983. MBEDTLS_MD_SHA1, MBEDTLS_PK_RSA,
  23984. },
  23985. #endif /* MBEDTLS_SHA1_C */
  23986. #if defined(MBEDTLS_SHA256_C)
  23987. {
  23988. { ADD_LEN( MBEDTLS_OID_PKCS1_SHA224 ), "sha224WithRSAEncryption", "RSA with SHA-224" },
  23989. MBEDTLS_MD_SHA224, MBEDTLS_PK_RSA,
  23990. },
  23991. {
  23992. { ADD_LEN( MBEDTLS_OID_PKCS1_SHA256 ), "sha256WithRSAEncryption", "RSA with SHA-256" },
  23993. MBEDTLS_MD_SHA256, MBEDTLS_PK_RSA,
  23994. },
  23995. #endif /* MBEDTLS_SHA256_C */
  23996. #if defined(MBEDTLS_SHA512_C)
  23997. {
  23998. { ADD_LEN( MBEDTLS_OID_PKCS1_SHA384 ), "sha384WithRSAEncryption", "RSA with SHA-384" },
  23999. MBEDTLS_MD_SHA384, MBEDTLS_PK_RSA,
  24000. },
  24001. {
  24002. { ADD_LEN( MBEDTLS_OID_PKCS1_SHA512 ), "sha512WithRSAEncryption", "RSA with SHA-512" },
  24003. MBEDTLS_MD_SHA512, MBEDTLS_PK_RSA,
  24004. },
  24005. #endif /* MBEDTLS_SHA512_C */
  24006. #if defined(MBEDTLS_SHA1_C)
  24007. {
  24008. { ADD_LEN( MBEDTLS_OID_RSA_SHA_OBS ), "sha-1WithRSAEncryption", "RSA with SHA1" },
  24009. MBEDTLS_MD_SHA1, MBEDTLS_PK_RSA,
  24010. },
  24011. #endif /* MBEDTLS_SHA1_C */
  24012. #endif /* MBEDTLS_RSA_C */
  24013. #if defined(MBEDTLS_ECDSA_C)
  24014. #if defined(MBEDTLS_SHA1_C)
  24015. {
  24016. { ADD_LEN( MBEDTLS_OID_ECDSA_SHA1 ), "ecdsa-with-SHA1", "ECDSA with SHA1" },
  24017. MBEDTLS_MD_SHA1, MBEDTLS_PK_ECDSA,
  24018. },
  24019. #endif /* MBEDTLS_SHA1_C */
  24020. #if defined(MBEDTLS_SHA256_C)
  24021. {
  24022. { ADD_LEN( MBEDTLS_OID_ECDSA_SHA224 ), "ecdsa-with-SHA224", "ECDSA with SHA224" },
  24023. MBEDTLS_MD_SHA224, MBEDTLS_PK_ECDSA,
  24024. },
  24025. {
  24026. { ADD_LEN( MBEDTLS_OID_ECDSA_SHA256 ), "ecdsa-with-SHA256", "ECDSA with SHA256" },
  24027. MBEDTLS_MD_SHA256, MBEDTLS_PK_ECDSA,
  24028. },
  24029. #endif /* MBEDTLS_SHA256_C */
  24030. #if defined(MBEDTLS_SHA512_C)
  24031. {
  24032. { ADD_LEN( MBEDTLS_OID_ECDSA_SHA384 ), "ecdsa-with-SHA384", "ECDSA with SHA384" },
  24033. MBEDTLS_MD_SHA384, MBEDTLS_PK_ECDSA,
  24034. },
  24035. {
  24036. { ADD_LEN( MBEDTLS_OID_ECDSA_SHA512 ), "ecdsa-with-SHA512", "ECDSA with SHA512" },
  24037. MBEDTLS_MD_SHA512, MBEDTLS_PK_ECDSA,
  24038. },
  24039. #endif /* MBEDTLS_SHA512_C */
  24040. #endif /* MBEDTLS_ECDSA_C */
  24041. #if defined(MBEDTLS_RSA_C)
  24042. {
  24043. { ADD_LEN( MBEDTLS_OID_RSASSA_PSS ), "RSASSA-PSS", "RSASSA-PSS" },
  24044. MBEDTLS_MD_NONE, MBEDTLS_PK_RSASSA_PSS,
  24045. },
  24046. #endif /* MBEDTLS_RSA_C */
  24047. {
  24048. { NULL, 0, NULL, NULL },
  24049. MBEDTLS_MD_NONE, MBEDTLS_PK_NONE,
  24050. },
  24051. };
  24052. FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg)
  24053. FN_OID_GET_DESCRIPTOR_ATTR1(mbedtls_oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description)
  24054. FN_OID_GET_ATTR2(mbedtls_oid_get_sig_alg, oid_sig_alg_t, sig_alg, mbedtls_md_type_t, md_alg, mbedtls_pk_type_t, pk_alg)
  24055. FN_OID_GET_OID_BY_ATTR2(mbedtls_oid_get_oid_by_sig_alg, oid_sig_alg_t, oid_sig_alg, mbedtls_pk_type_t, pk_alg, mbedtls_md_type_t, md_alg)
  24056. #endif /* MBEDTLS_MD_C */
  24057. /*
  24058. * For PublicKeyInfo (PKCS1, RFC 5480)
  24059. */
  24060. typedef struct {
  24061. mbedtls_oid_descriptor_t descriptor;
  24062. mbedtls_pk_type_t pk_alg;
  24063. } oid_pk_alg_t;
  24064. static const oid_pk_alg_t oid_pk_alg[] =
  24065. {
  24066. {
  24067. { ADD_LEN( MBEDTLS_OID_PKCS1_RSA ), "rsaEncryption", "RSA" },
  24068. MBEDTLS_PK_RSA,
  24069. },
  24070. {
  24071. { ADD_LEN( MBEDTLS_OID_EC_ALG_UNRESTRICTED ), "id-ecPublicKey", "Generic EC key" },
  24072. MBEDTLS_PK_ECKEY,
  24073. },
  24074. {
  24075. { ADD_LEN( MBEDTLS_OID_EC_ALG_ECDH ), "id-ecDH", "EC key for ECDH" },
  24076. MBEDTLS_PK_ECKEY_DH,
  24077. },
  24078. {
  24079. { NULL, 0, NULL, NULL },
  24080. MBEDTLS_PK_NONE,
  24081. },
  24082. };
  24083. FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg)
  24084. FN_OID_GET_ATTR1(mbedtls_oid_get_pk_alg, oid_pk_alg_t, pk_alg, mbedtls_pk_type_t, pk_alg)
  24085. FN_OID_GET_OID_BY_ATTR1(mbedtls_oid_get_oid_by_pk_alg, oid_pk_alg_t, oid_pk_alg, mbedtls_pk_type_t, pk_alg)
  24086. #if defined(MBEDTLS_ECP_C)
  24087. /*
  24088. * For namedCurve (RFC 5480)
  24089. */
  24090. typedef struct {
  24091. mbedtls_oid_descriptor_t descriptor;
  24092. mbedtls_ecp_group_id grp_id;
  24093. } oid_ecp_grp_t;
  24094. static const oid_ecp_grp_t oid_ecp_grp[] =
  24095. {
  24096. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
  24097. {
  24098. { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP192R1 ), "secp192r1", "secp192r1" },
  24099. MBEDTLS_ECP_DP_SECP192R1,
  24100. },
  24101. #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
  24102. #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
  24103. {
  24104. { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP224R1 ), "secp224r1", "secp224r1" },
  24105. MBEDTLS_ECP_DP_SECP224R1,
  24106. },
  24107. #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
  24108. #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
  24109. {
  24110. { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP256R1 ), "secp256r1", "secp256r1" },
  24111. MBEDTLS_ECP_DP_SECP256R1,
  24112. },
  24113. #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
  24114. #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
  24115. {
  24116. { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP384R1 ), "secp384r1", "secp384r1" },
  24117. MBEDTLS_ECP_DP_SECP384R1,
  24118. },
  24119. #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
  24120. #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
  24121. {
  24122. { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP521R1 ), "secp521r1", "secp521r1" },
  24123. MBEDTLS_ECP_DP_SECP521R1,
  24124. },
  24125. #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
  24126. #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
  24127. {
  24128. { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP192K1 ), "secp192k1", "secp192k1" },
  24129. MBEDTLS_ECP_DP_SECP192K1,
  24130. },
  24131. #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
  24132. #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
  24133. {
  24134. { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP224K1 ), "secp224k1", "secp224k1" },
  24135. MBEDTLS_ECP_DP_SECP224K1,
  24136. },
  24137. #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
  24138. #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
  24139. {
  24140. { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP256K1 ), "secp256k1", "secp256k1" },
  24141. MBEDTLS_ECP_DP_SECP256K1,
  24142. },
  24143. #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
  24144. #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
  24145. {
  24146. { ADD_LEN( MBEDTLS_OID_EC_GRP_BP256R1 ), "brainpoolP256r1","brainpool256r1" },
  24147. MBEDTLS_ECP_DP_BP256R1,
  24148. },
  24149. #endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
  24150. #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
  24151. {
  24152. { ADD_LEN( MBEDTLS_OID_EC_GRP_BP384R1 ), "brainpoolP384r1","brainpool384r1" },
  24153. MBEDTLS_ECP_DP_BP384R1,
  24154. },
  24155. #endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
  24156. #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
  24157. {
  24158. { ADD_LEN( MBEDTLS_OID_EC_GRP_BP512R1 ), "brainpoolP512r1","brainpool512r1" },
  24159. MBEDTLS_ECP_DP_BP512R1,
  24160. },
  24161. #endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
  24162. {
  24163. { NULL, 0, NULL, NULL },
  24164. MBEDTLS_ECP_DP_NONE,
  24165. },
  24166. };
  24167. FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp)
  24168. FN_OID_GET_ATTR1(mbedtls_oid_get_ec_grp, oid_ecp_grp_t, grp_id, mbedtls_ecp_group_id, grp_id)
  24169. FN_OID_GET_OID_BY_ATTR1(mbedtls_oid_get_oid_by_ec_grp, oid_ecp_grp_t, oid_ecp_grp, mbedtls_ecp_group_id, grp_id)
  24170. #endif /* MBEDTLS_ECP_C */
  24171. #if defined(MBEDTLS_CIPHER_C)
  24172. /*
  24173. * For PKCS#5 PBES2 encryption algorithm
  24174. */
  24175. typedef struct {
  24176. mbedtls_oid_descriptor_t descriptor;
  24177. mbedtls_cipher_type_t cipher_alg;
  24178. } oid_cipher_alg_t;
  24179. static const oid_cipher_alg_t oid_cipher_alg[] =
  24180. {
  24181. {
  24182. { ADD_LEN( MBEDTLS_OID_DES_CBC ), "desCBC", "DES-CBC" },
  24183. MBEDTLS_CIPHER_DES_CBC,
  24184. },
  24185. {
  24186. { ADD_LEN( MBEDTLS_OID_DES_EDE3_CBC ), "des-ede3-cbc", "DES-EDE3-CBC" },
  24187. MBEDTLS_CIPHER_DES_EDE3_CBC,
  24188. },
  24189. {
  24190. { NULL, 0, NULL, NULL },
  24191. MBEDTLS_CIPHER_NONE,
  24192. },
  24193. };
  24194. FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg)
  24195. FN_OID_GET_ATTR1(mbedtls_oid_get_cipher_alg, oid_cipher_alg_t, cipher_alg, mbedtls_cipher_type_t, cipher_alg)
  24196. #endif /* MBEDTLS_CIPHER_C */
  24197. #if defined(MBEDTLS_MD_C)
  24198. /*
  24199. * For digestAlgorithm
  24200. */
  24201. typedef struct {
  24202. mbedtls_oid_descriptor_t descriptor;
  24203. mbedtls_md_type_t md_alg;
  24204. } oid_md_alg_t;
  24205. static const oid_md_alg_t oid_md_alg[] =
  24206. {
  24207. #if defined(MBEDTLS_MD2_C)
  24208. {
  24209. { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_MD2 ), "id-md2", "MD2" },
  24210. MBEDTLS_MD_MD2,
  24211. },
  24212. #endif /* MBEDTLS_MD2_C */
  24213. #if defined(MBEDTLS_MD4_C)
  24214. {
  24215. { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_MD4 ), "id-md4", "MD4" },
  24216. MBEDTLS_MD_MD4,
  24217. },
  24218. #endif /* MBEDTLS_MD4_C */
  24219. #if defined(MBEDTLS_MD5_C)
  24220. {
  24221. { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_MD5 ), "id-md5", "MD5" },
  24222. MBEDTLS_MD_MD5,
  24223. },
  24224. #endif /* MBEDTLS_MD5_C */
  24225. #if defined(MBEDTLS_SHA1_C)
  24226. {
  24227. { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
  24228. MBEDTLS_MD_SHA1,
  24229. },
  24230. #endif /* MBEDTLS_SHA1_C */
  24231. #if defined(MBEDTLS_SHA256_C)
  24232. {
  24233. { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA224 ), "id-sha224", "SHA-224" },
  24234. MBEDTLS_MD_SHA224,
  24235. },
  24236. {
  24237. { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA256 ), "id-sha256", "SHA-256" },
  24238. MBEDTLS_MD_SHA256,
  24239. },
  24240. #endif /* MBEDTLS_SHA256_C */
  24241. #if defined(MBEDTLS_SHA512_C)
  24242. {
  24243. { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA384 ), "id-sha384", "SHA-384" },
  24244. MBEDTLS_MD_SHA384,
  24245. },
  24246. {
  24247. { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA512 ), "id-sha512", "SHA-512" },
  24248. MBEDTLS_MD_SHA512,
  24249. },
  24250. #endif /* MBEDTLS_SHA512_C */
  24251. {
  24252. { NULL, 0, NULL, NULL },
  24253. MBEDTLS_MD_NONE,
  24254. },
  24255. };
  24256. FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg)
  24257. FN_OID_GET_ATTR1(mbedtls_oid_get_md_alg, oid_md_alg_t, md_alg, mbedtls_md_type_t, md_alg)
  24258. FN_OID_GET_OID_BY_ATTR1(mbedtls_oid_get_oid_by_md, oid_md_alg_t, oid_md_alg, mbedtls_md_type_t, md_alg)
  24259. /*
  24260. * For HMAC digestAlgorithm
  24261. */
  24262. typedef struct {
  24263. mbedtls_oid_descriptor_t descriptor;
  24264. mbedtls_md_type_t md_hmac;
  24265. } oid_md_hmac_t;
  24266. static const oid_md_hmac_t oid_md_hmac[] =
  24267. {
  24268. #if defined(MBEDTLS_SHA1_C)
  24269. {
  24270. { ADD_LEN( MBEDTLS_OID_HMAC_SHA1 ), "hmacSHA1", "HMAC-SHA-1" },
  24271. MBEDTLS_MD_SHA1,
  24272. },
  24273. #endif /* MBEDTLS_SHA1_C */
  24274. #if defined(MBEDTLS_SHA256_C)
  24275. {
  24276. { ADD_LEN( MBEDTLS_OID_HMAC_SHA224 ), "hmacSHA224", "HMAC-SHA-224" },
  24277. MBEDTLS_MD_SHA224,
  24278. },
  24279. {
  24280. { ADD_LEN( MBEDTLS_OID_HMAC_SHA256 ), "hmacSHA256", "HMAC-SHA-256" },
  24281. MBEDTLS_MD_SHA256,
  24282. },
  24283. #endif /* MBEDTLS_SHA256_C */
  24284. #if defined(MBEDTLS_SHA512_C)
  24285. {
  24286. { ADD_LEN( MBEDTLS_OID_HMAC_SHA384 ), "hmacSHA384", "HMAC-SHA-384" },
  24287. MBEDTLS_MD_SHA384,
  24288. },
  24289. {
  24290. { ADD_LEN( MBEDTLS_OID_HMAC_SHA512 ), "hmacSHA512", "HMAC-SHA-512" },
  24291. MBEDTLS_MD_SHA512,
  24292. },
  24293. #endif /* MBEDTLS_SHA512_C */
  24294. {
  24295. { NULL, 0, NULL, NULL },
  24296. MBEDTLS_MD_NONE,
  24297. },
  24298. };
  24299. FN_OID_TYPED_FROM_ASN1(oid_md_hmac_t, md_hmac, oid_md_hmac)
  24300. FN_OID_GET_ATTR1(mbedtls_oid_get_md_hmac, oid_md_hmac_t, md_hmac, mbedtls_md_type_t, md_hmac)
  24301. #endif /* MBEDTLS_MD_C */
  24302. #if defined(MBEDTLS_PKCS12_C)
  24303. /*
  24304. * For PKCS#12 PBEs
  24305. */
  24306. typedef struct {
  24307. mbedtls_oid_descriptor_t descriptor;
  24308. mbedtls_md_type_t md_alg;
  24309. mbedtls_cipher_type_t cipher_alg;
  24310. } oid_pkcs12_pbe_alg_t;
  24311. static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
  24312. {
  24313. {
  24314. { ADD_LEN( MBEDTLS_OID_PKCS12_PBE_SHA1_DES3_EDE_CBC ), "pbeWithSHAAnd3-KeyTripleDES-CBC", "PBE with SHA1 and 3-Key 3DES" },
  24315. MBEDTLS_MD_SHA1, MBEDTLS_CIPHER_DES_EDE3_CBC,
  24316. },
  24317. {
  24318. { ADD_LEN( MBEDTLS_OID_PKCS12_PBE_SHA1_DES2_EDE_CBC ), "pbeWithSHAAnd2-KeyTripleDES-CBC", "PBE with SHA1 and 2-Key 3DES" },
  24319. MBEDTLS_MD_SHA1, MBEDTLS_CIPHER_DES_EDE_CBC,
  24320. },
  24321. {
  24322. { NULL, 0, NULL, NULL },
  24323. MBEDTLS_MD_NONE, MBEDTLS_CIPHER_NONE,
  24324. },
  24325. };
  24326. FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg)
  24327. FN_OID_GET_ATTR2(mbedtls_oid_get_pkcs12_pbe_alg, oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, mbedtls_md_type_t, md_alg, mbedtls_cipher_type_t, cipher_alg)
  24328. #endif /* MBEDTLS_PKCS12_C */
  24329. #define OID_SAFE_SNPRINTF \
  24330. do { \
  24331. if( ret < 0 || (size_t) ret >= n ) \
  24332. return( MBEDTLS_ERR_OID_BUF_TOO_SMALL ); \
  24333. \
  24334. n -= (size_t) ret; \
  24335. p += (size_t) ret; \
  24336. } while( 0 )
  24337. /* Return the x.y.z.... style numeric string for the given OID */
  24338. int mbedtls_oid_get_numeric_string( char *buf, size_t size,
  24339. const mbedtls_asn1_buf *oid )
  24340. {
  24341. int ret;
  24342. size_t i, n;
  24343. unsigned int value;
  24344. char *p;
  24345. p = buf;
  24346. n = size;
  24347. /* First byte contains first two dots */
  24348. if( oid->len > 0 )
  24349. {
  24350. ret = mbedtls_snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
  24351. OID_SAFE_SNPRINTF;
  24352. }
  24353. value = 0;
  24354. for( i = 1; i < oid->len; i++ )
  24355. {
  24356. /* Prevent overflow in value. */
  24357. if( ( ( value << 7 ) >> 7 ) != value )
  24358. return( MBEDTLS_ERR_OID_BUF_TOO_SMALL );
  24359. value <<= 7;
  24360. value += oid->p[i] & 0x7F;
  24361. if( !( oid->p[i] & 0x80 ) )
  24362. {
  24363. /* Last byte */
  24364. ret = mbedtls_snprintf( p, n, ".%d", value );
  24365. OID_SAFE_SNPRINTF;
  24366. value = 0;
  24367. }
  24368. }
  24369. return( (int) ( size - n ) );
  24370. }
  24371. #endif /* MBEDTLS_OID_C */
  24372. /*
  24373. Amalgamated build undefines
  24374. */
  24375. #undef ADD
  24376. #undef BC
  24377. #undef BEFORE_COLON
  24378. #undef F
  24379. #undef F0
  24380. #undef F1
  24381. #undef F2
  24382. #undef F3
  24383. #undef F4
  24384. #undef F5
  24385. #undef FSb
  24386. #undef K
  24387. #undef KK
  24388. #undef P
  24389. #undef R
  24390. #undef ROTR
  24391. #undef S
  24392. #undef S0
  24393. #undef S1
  24394. #undef S2
  24395. #undef S3
  24396. #undef SAFE_SNPRINTF
  24397. #undef SHR
  24398. #undef close
  24399. #undef read
  24400. #undef supported_init
  24401. #undef write
  24402. /********* Start of file library/padlock.c ************/
  24403. /*
  24404. * VIA PadLock support functions
  24405. *
  24406. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  24407. * SPDX-License-Identifier: Apache-2.0
  24408. *
  24409. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  24410. * not use this file except in compliance with the License.
  24411. * You may obtain a copy of the License at
  24412. *
  24413. * http://www.apache.org/licenses/LICENSE-2.0
  24414. *
  24415. * Unless required by applicable law or agreed to in writing, software
  24416. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  24417. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24418. * See the License for the specific language governing permissions and
  24419. * limitations under the License.
  24420. *
  24421. * This file is part of mbed TLS (https://tls.mbed.org)
  24422. */
  24423. /*
  24424. * This implementation is based on the VIA PadLock Programming Guide:
  24425. *
  24426. * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
  24427. * programming_guide.pdf
  24428. */
  24429. #if !defined(MBEDTLS_CONFIG_FILE)
  24430. #else
  24431. #endif
  24432. #if defined(MBEDTLS_PADLOCK_C)
  24433. #include <string.h>
  24434. #ifndef asm
  24435. #define asm __asm
  24436. #endif
  24437. #if defined(MBEDTLS_HAVE_X86)
  24438. /*
  24439. * PadLock detection routine
  24440. */
  24441. int mbedtls_padlock_has_support( int feature )
  24442. {
  24443. static int flags = -1;
  24444. int ebx = 0, edx = 0;
  24445. if( flags == -1 )
  24446. {
  24447. asm( "movl %%ebx, %0 \n\t"
  24448. "movl $0xC0000000, %%eax \n\t"
  24449. "cpuid \n\t"
  24450. "cmpl $0xC0000001, %%eax \n\t"
  24451. "movl $0, %%edx \n\t"
  24452. "jb unsupported \n\t"
  24453. "movl $0xC0000001, %%eax \n\t"
  24454. "cpuid \n\t"
  24455. "unsupported: \n\t"
  24456. "movl %%edx, %1 \n\t"
  24457. "movl %2, %%ebx \n\t"
  24458. : "=m" (ebx), "=m" (edx)
  24459. : "m" (ebx)
  24460. : "eax", "ecx", "edx" );
  24461. flags = edx;
  24462. }
  24463. return( flags & feature );
  24464. }
  24465. /*
  24466. * PadLock AES-ECB block en(de)cryption
  24467. */
  24468. int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx,
  24469. int mode,
  24470. const unsigned char input[16],
  24471. unsigned char output[16] )
  24472. {
  24473. int ebx = 0;
  24474. uint32_t *rk;
  24475. uint32_t *blk;
  24476. uint32_t *ctrl;
  24477. unsigned char buf[256];
  24478. rk = ctx->rk;
  24479. blk = MBEDTLS_PADLOCK_ALIGN16( buf );
  24480. memcpy( blk, input, 16 );
  24481. ctrl = blk + 4;
  24482. *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
  24483. asm( "pushfl \n\t"
  24484. "popfl \n\t"
  24485. "movl %%ebx, %0 \n\t"
  24486. "movl $1, %%ecx \n\t"
  24487. "movl %2, %%edx \n\t"
  24488. "movl %3, %%ebx \n\t"
  24489. "movl %4, %%esi \n\t"
  24490. "movl %4, %%edi \n\t"
  24491. ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
  24492. "movl %1, %%ebx \n\t"
  24493. : "=m" (ebx)
  24494. : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
  24495. : "memory", "ecx", "edx", "esi", "edi" );
  24496. memcpy( output, blk, 16 );
  24497. return( 0 );
  24498. }
  24499. /*
  24500. * PadLock AES-CBC buffer en(de)cryption
  24501. */
  24502. int mbedtls_padlock_xcryptcbc( mbedtls_aes_context *ctx,
  24503. int mode,
  24504. size_t length,
  24505. unsigned char iv[16],
  24506. const unsigned char *input,
  24507. unsigned char *output )
  24508. {
  24509. int ebx = 0;
  24510. size_t count;
  24511. uint32_t *rk;
  24512. uint32_t *iw;
  24513. uint32_t *ctrl;
  24514. unsigned char buf[256];
  24515. if( ( (long) input & 15 ) != 0 ||
  24516. ( (long) output & 15 ) != 0 )
  24517. return( MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED );
  24518. rk = ctx->rk;
  24519. iw = MBEDTLS_PADLOCK_ALIGN16( buf );
  24520. memcpy( iw, iv, 16 );
  24521. ctrl = iw + 4;
  24522. *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode ^ 1 ) - 10 ) << 9 );
  24523. count = ( length + 15 ) >> 4;
  24524. asm( "pushfl \n\t"
  24525. "popfl \n\t"
  24526. "movl %%ebx, %0 \n\t"
  24527. "movl %2, %%ecx \n\t"
  24528. "movl %3, %%edx \n\t"
  24529. "movl %4, %%ebx \n\t"
  24530. "movl %5, %%esi \n\t"
  24531. "movl %6, %%edi \n\t"
  24532. "movl %7, %%eax \n\t"
  24533. ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
  24534. "movl %1, %%ebx \n\t"
  24535. : "=m" (ebx)
  24536. : "m" (ebx), "m" (count), "m" (ctrl),
  24537. "m" (rk), "m" (input), "m" (output), "m" (iw)
  24538. : "memory", "eax", "ecx", "edx", "esi", "edi" );
  24539. memcpy( iv, iw, 16 );
  24540. return( 0 );
  24541. }
  24542. #endif /* MBEDTLS_HAVE_X86 */
  24543. #endif /* MBEDTLS_PADLOCK_C */
  24544. /*
  24545. Amalgamated build undefines
  24546. */
  24547. #undef ADD
  24548. #undef BC
  24549. #undef BEFORE_COLON
  24550. #undef F
  24551. #undef F0
  24552. #undef F1
  24553. #undef F2
  24554. #undef F3
  24555. #undef F4
  24556. #undef F5
  24557. #undef FSb
  24558. #undef K
  24559. #undef KK
  24560. #undef P
  24561. #undef R
  24562. #undef ROTR
  24563. #undef S
  24564. #undef S0
  24565. #undef S1
  24566. #undef S2
  24567. #undef S3
  24568. #undef SAFE_SNPRINTF
  24569. #undef SHR
  24570. #undef close
  24571. #undef read
  24572. #undef supported_init
  24573. #undef write
  24574. /********* Start of file library/pem.c ************/
  24575. /*
  24576. * Privacy Enhanced Mail (PEM) decoding
  24577. *
  24578. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  24579. * SPDX-License-Identifier: Apache-2.0
  24580. *
  24581. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  24582. * not use this file except in compliance with the License.
  24583. * You may obtain a copy of the License at
  24584. *
  24585. * http://www.apache.org/licenses/LICENSE-2.0
  24586. *
  24587. * Unless required by applicable law or agreed to in writing, software
  24588. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  24589. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24590. * See the License for the specific language governing permissions and
  24591. * limitations under the License.
  24592. *
  24593. * This file is part of mbed TLS (https://tls.mbed.org)
  24594. */
  24595. #if !defined(MBEDTLS_CONFIG_FILE)
  24596. #else
  24597. #endif
  24598. #if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
  24599. #include <string.h>
  24600. #if defined(MBEDTLS_PLATFORM_C)
  24601. #else
  24602. #include <stdlib.h>
  24603. #define mbedtls_calloc calloc
  24604. #define mbedtls_free free
  24605. #endif
  24606. #if defined(MBEDTLS_PEM_PARSE_C)
  24607. /* Implementation that should never be optimized out by the compiler */
  24608. /* zeroize was here */
  24609. void mbedtls_pem_init( mbedtls_pem_context *ctx )
  24610. {
  24611. memset( ctx, 0, sizeof( mbedtls_pem_context ) );
  24612. }
  24613. #if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
  24614. ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
  24615. /*
  24616. * Read a 16-byte hex string and convert it to binary
  24617. */
  24618. static int pem_get_iv( const unsigned char *s, unsigned char *iv,
  24619. size_t iv_len )
  24620. {
  24621. size_t i, j, k;
  24622. memset( iv, 0, iv_len );
  24623. for( i = 0; i < iv_len * 2; i++, s++ )
  24624. {
  24625. if( *s >= '0' && *s <= '9' ) j = *s - '0'; else
  24626. if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else
  24627. if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else
  24628. return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
  24629. k = ( ( i & 1 ) != 0 ) ? j : j << 4;
  24630. iv[i >> 1] = (unsigned char)( iv[i >> 1] | k );
  24631. }
  24632. return( 0 );
  24633. }
  24634. static int pem_pbkdf1( unsigned char *key, size_t keylen,
  24635. unsigned char *iv,
  24636. const unsigned char *pwd, size_t pwdlen )
  24637. {
  24638. mbedtls_md5_context md5_ctx;
  24639. unsigned char md5sum[16];
  24640. size_t use_len;
  24641. int ret;
  24642. mbedtls_md5_init( &md5_ctx );
  24643. /*
  24644. * key[ 0..15] = MD5(pwd || IV)
  24645. */
  24646. if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 )
  24647. goto exit;
  24648. if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 0 )
  24649. goto exit;
  24650. if( ( ret = mbedtls_md5_update_ret( &md5_ctx, iv, 8 ) ) != 0 )
  24651. goto exit;
  24652. if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 )
  24653. goto exit;
  24654. if( keylen <= 16 )
  24655. {
  24656. memcpy( key, md5sum, keylen );
  24657. goto exit;
  24658. }
  24659. memcpy( key, md5sum, 16 );
  24660. /*
  24661. * key[16..23] = MD5(key[ 0..15] || pwd || IV])
  24662. */
  24663. if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 )
  24664. goto exit;
  24665. if( ( ret = mbedtls_md5_update_ret( &md5_ctx, md5sum, 16 ) ) != 0 )
  24666. goto exit;
  24667. if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 0 )
  24668. goto exit;
  24669. if( ( ret = mbedtls_md5_update_ret( &md5_ctx, iv, 8 ) ) != 0 )
  24670. goto exit;
  24671. if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 )
  24672. goto exit;
  24673. use_len = 16;
  24674. if( keylen < 32 )
  24675. use_len = keylen - 16;
  24676. memcpy( key + 16, md5sum, use_len );
  24677. exit:
  24678. mbedtls_md5_free( &md5_ctx );
  24679. mbedtls_zeroize( md5sum, 16 );
  24680. return( ret );
  24681. }
  24682. #if defined(MBEDTLS_DES_C)
  24683. /*
  24684. * Decrypt with DES-CBC, using PBKDF1 for key derivation
  24685. */
  24686. static int pem_des_decrypt( unsigned char des_iv[8],
  24687. unsigned char *buf, size_t buflen,
  24688. const unsigned char *pwd, size_t pwdlen )
  24689. {
  24690. mbedtls_des_context des_ctx;
  24691. unsigned char des_key[8];
  24692. int ret;
  24693. mbedtls_des_init( &des_ctx );
  24694. if( ( ret = pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen ) ) != 0 )
  24695. goto exit;
  24696. if( ( ret = mbedtls_des_setkey_dec( &des_ctx, des_key ) ) != 0 )
  24697. goto exit;
  24698. ret = mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, buflen,
  24699. des_iv, buf, buf );
  24700. exit:
  24701. mbedtls_des_free( &des_ctx );
  24702. mbedtls_zeroize( des_key, 8 );
  24703. return( ret );
  24704. }
  24705. /*
  24706. * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
  24707. */
  24708. static int pem_des3_decrypt( unsigned char des3_iv[8],
  24709. unsigned char *buf, size_t buflen,
  24710. const unsigned char *pwd, size_t pwdlen )
  24711. {
  24712. mbedtls_des3_context des3_ctx;
  24713. unsigned char des3_key[24];
  24714. int ret;
  24715. mbedtls_des3_init( &des3_ctx );
  24716. if( ( ret = pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen ) ) != 0 )
  24717. goto exit;
  24718. if( ( ret = mbedtls_des3_set3key_dec( &des3_ctx, des3_key ) ) != 0 )
  24719. goto exit;
  24720. ret = mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
  24721. des3_iv, buf, buf );
  24722. exit:
  24723. mbedtls_des3_free( &des3_ctx );
  24724. mbedtls_zeroize( des3_key, 24 );
  24725. return( ret );
  24726. }
  24727. #endif /* MBEDTLS_DES_C */
  24728. #if defined(MBEDTLS_AES_C)
  24729. /*
  24730. * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
  24731. */
  24732. static int pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen,
  24733. unsigned char *buf, size_t buflen,
  24734. const unsigned char *pwd, size_t pwdlen )
  24735. {
  24736. mbedtls_aes_context aes_ctx;
  24737. unsigned char aes_key[32];
  24738. int ret;
  24739. mbedtls_aes_init( &aes_ctx );
  24740. if( ( ret = pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen ) ) != 0 )
  24741. goto exit;
  24742. if( ( ret = mbedtls_aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ) ) != 0 )
  24743. goto exit;
  24744. ret = mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
  24745. aes_iv, buf, buf );
  24746. exit:
  24747. mbedtls_aes_free( &aes_ctx );
  24748. mbedtls_zeroize( aes_key, keylen );
  24749. return( ret );
  24750. }
  24751. #endif /* MBEDTLS_AES_C */
  24752. #endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
  24753. ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  24754. int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
  24755. const unsigned char *data, const unsigned char *pwd,
  24756. size_t pwdlen, size_t *use_len )
  24757. {
  24758. int ret, enc;
  24759. size_t len;
  24760. unsigned char *buf;
  24761. const unsigned char *s1, *s2, *end;
  24762. #if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
  24763. ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
  24764. unsigned char pem_iv[16];
  24765. mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
  24766. #else
  24767. ((void) pwd);
  24768. ((void) pwdlen);
  24769. #endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
  24770. ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  24771. if( ctx == NULL )
  24772. return( MBEDTLS_ERR_PEM_BAD_INPUT_DATA );
  24773. s1 = (unsigned char *) strstr( (const char *) data, header );
  24774. if( s1 == NULL )
  24775. return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
  24776. s2 = (unsigned char *) strstr( (const char *) data, footer );
  24777. if( s2 == NULL || s2 <= s1 )
  24778. return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
  24779. s1 += strlen( header );
  24780. if( *s1 == ' ' ) s1++;
  24781. if( *s1 == '\r' ) s1++;
  24782. if( *s1 == '\n' ) s1++;
  24783. else return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
  24784. end = s2;
  24785. end += strlen( footer );
  24786. if( *end == ' ' ) end++;
  24787. if( *end == '\r' ) end++;
  24788. if( *end == '\n' ) end++;
  24789. *use_len = end - data;
  24790. enc = 0;
  24791. if( s2 - s1 >= 22 && memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
  24792. {
  24793. #if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
  24794. ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
  24795. enc++;
  24796. s1 += 22;
  24797. if( *s1 == '\r' ) s1++;
  24798. if( *s1 == '\n' ) s1++;
  24799. else return( MBEDTLS_ERR_PEM_INVALID_DATA );
  24800. #if defined(MBEDTLS_DES_C)
  24801. if( s2 - s1 >= 23 && memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
  24802. {
  24803. enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
  24804. s1 += 23;
  24805. if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8 ) != 0 )
  24806. return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
  24807. s1 += 16;
  24808. }
  24809. else if( s2 - s1 >= 18 && memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
  24810. {
  24811. enc_alg = MBEDTLS_CIPHER_DES_CBC;
  24812. s1 += 18;
  24813. if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8) != 0 )
  24814. return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
  24815. s1 += 16;
  24816. }
  24817. #endif /* MBEDTLS_DES_C */
  24818. #if defined(MBEDTLS_AES_C)
  24819. if( s2 - s1 >= 14 && memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
  24820. {
  24821. if( s2 - s1 < 22 )
  24822. return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
  24823. else if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
  24824. enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
  24825. else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
  24826. enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
  24827. else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
  24828. enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
  24829. else
  24830. return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
  24831. s1 += 22;
  24832. if( s2 - s1 < 32 || pem_get_iv( s1, pem_iv, 16 ) != 0 )
  24833. return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
  24834. s1 += 32;
  24835. }
  24836. #endif /* MBEDTLS_AES_C */
  24837. if( enc_alg == MBEDTLS_CIPHER_NONE )
  24838. return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
  24839. if( *s1 == '\r' ) s1++;
  24840. if( *s1 == '\n' ) s1++;
  24841. else return( MBEDTLS_ERR_PEM_INVALID_DATA );
  24842. #else
  24843. return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
  24844. #endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
  24845. ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  24846. }
  24847. if( s1 >= s2 )
  24848. return( MBEDTLS_ERR_PEM_INVALID_DATA );
  24849. ret = mbedtls_base64_decode( NULL, 0, &len, s1, s2 - s1 );
  24850. if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
  24851. return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
  24852. if( ( buf = mbedtls_calloc( 1, len ) ) == NULL )
  24853. return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
  24854. if( ( ret = mbedtls_base64_decode( buf, len, &len, s1, s2 - s1 ) ) != 0 )
  24855. {
  24856. mbedtls_zeroize( buf, len );
  24857. mbedtls_free( buf );
  24858. return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
  24859. }
  24860. if( enc != 0 )
  24861. {
  24862. #if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
  24863. ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
  24864. if( pwd == NULL )
  24865. {
  24866. mbedtls_zeroize( buf, len );
  24867. mbedtls_free( buf );
  24868. return( MBEDTLS_ERR_PEM_PASSWORD_REQUIRED );
  24869. }
  24870. ret = 0;
  24871. #if defined(MBEDTLS_DES_C)
  24872. if( enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC )
  24873. ret = pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen );
  24874. else if( enc_alg == MBEDTLS_CIPHER_DES_CBC )
  24875. ret = pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen );
  24876. #endif /* MBEDTLS_DES_C */
  24877. #if defined(MBEDTLS_AES_C)
  24878. if( enc_alg == MBEDTLS_CIPHER_AES_128_CBC )
  24879. ret = pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen );
  24880. else if( enc_alg == MBEDTLS_CIPHER_AES_192_CBC )
  24881. ret = pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen );
  24882. else if( enc_alg == MBEDTLS_CIPHER_AES_256_CBC )
  24883. ret = pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen );
  24884. #endif /* MBEDTLS_AES_C */
  24885. if( ret != 0 )
  24886. {
  24887. mbedtls_free( buf );
  24888. return( ret );
  24889. }
  24890. /*
  24891. * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
  24892. * length bytes (allow 4 to be sure) in all known use cases.
  24893. *
  24894. * Use that as heurisitic to try detecting password mismatchs.
  24895. */
  24896. if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 )
  24897. {
  24898. mbedtls_zeroize( buf, len );
  24899. mbedtls_free( buf );
  24900. return( MBEDTLS_ERR_PEM_PASSWORD_MISMATCH );
  24901. }
  24902. #else
  24903. mbedtls_zeroize( buf, len );
  24904. mbedtls_free( buf );
  24905. return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
  24906. #endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
  24907. ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  24908. }
  24909. ctx->buf = buf;
  24910. ctx->buflen = len;
  24911. return( 0 );
  24912. }
  24913. void mbedtls_pem_free( mbedtls_pem_context *ctx )
  24914. {
  24915. if( ctx->buf != NULL )
  24916. mbedtls_zeroize( ctx->buf, ctx->buflen );
  24917. mbedtls_free( ctx->buf );
  24918. mbedtls_free( ctx->info );
  24919. mbedtls_zeroize( ctx, sizeof( mbedtls_pem_context ) );
  24920. }
  24921. #endif /* MBEDTLS_PEM_PARSE_C */
  24922. #if defined(MBEDTLS_PEM_WRITE_C)
  24923. int mbedtls_pem_write_buffer( const char *header, const char *footer,
  24924. const unsigned char *der_data, size_t der_len,
  24925. unsigned char *buf, size_t buf_len, size_t *olen )
  24926. {
  24927. int ret;
  24928. unsigned char *encode_buf = NULL, *c, *p = buf;
  24929. size_t len = 0, use_len, add_len = 0;
  24930. mbedtls_base64_encode( NULL, 0, &use_len, der_data, der_len );
  24931. add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1;
  24932. if( use_len + add_len > buf_len )
  24933. {
  24934. *olen = use_len + add_len;
  24935. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  24936. }
  24937. if( use_len != 0 &&
  24938. ( ( encode_buf = mbedtls_calloc( 1, use_len ) ) == NULL ) )
  24939. return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
  24940. if( ( ret = mbedtls_base64_encode( encode_buf, use_len, &use_len, der_data,
  24941. der_len ) ) != 0 )
  24942. {
  24943. mbedtls_free( encode_buf );
  24944. return( ret );
  24945. }
  24946. memcpy( p, header, strlen( header ) );
  24947. p += strlen( header );
  24948. c = encode_buf;
  24949. while( use_len )
  24950. {
  24951. len = ( use_len > 64 ) ? 64 : use_len;
  24952. memcpy( p, c, len );
  24953. use_len -= len;
  24954. p += len;
  24955. c += len;
  24956. *p++ = '\n';
  24957. }
  24958. memcpy( p, footer, strlen( footer ) );
  24959. p += strlen( footer );
  24960. *p++ = '\0';
  24961. *olen = p - buf;
  24962. mbedtls_free( encode_buf );
  24963. return( 0 );
  24964. }
  24965. #endif /* MBEDTLS_PEM_WRITE_C */
  24966. #endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */
  24967. /*
  24968. Amalgamated build undefines
  24969. */
  24970. #undef ADD
  24971. #undef BC
  24972. #undef BEFORE_COLON
  24973. #undef F
  24974. #undef F0
  24975. #undef F1
  24976. #undef F2
  24977. #undef F3
  24978. #undef F4
  24979. #undef F5
  24980. #undef FSb
  24981. #undef K
  24982. #undef KK
  24983. #undef P
  24984. #undef R
  24985. #undef ROTR
  24986. #undef S
  24987. #undef S0
  24988. #undef S1
  24989. #undef S2
  24990. #undef S3
  24991. #undef SAFE_SNPRINTF
  24992. #undef SHR
  24993. #undef close
  24994. #undef read
  24995. #undef supported_init
  24996. #undef write
  24997. /********* Start of file library/pk.c ************/
  24998. /*
  24999. * Public Key abstraction layer
  25000. *
  25001. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  25002. * SPDX-License-Identifier: Apache-2.0
  25003. *
  25004. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  25005. * not use this file except in compliance with the License.
  25006. * You may obtain a copy of the License at
  25007. *
  25008. * http://www.apache.org/licenses/LICENSE-2.0
  25009. *
  25010. * Unless required by applicable law or agreed to in writing, software
  25011. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  25012. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25013. * See the License for the specific language governing permissions and
  25014. * limitations under the License.
  25015. *
  25016. * This file is part of mbed TLS (https://tls.mbed.org)
  25017. */
  25018. #if !defined(MBEDTLS_CONFIG_FILE)
  25019. #else
  25020. #endif
  25021. #if defined(MBEDTLS_PK_C)
  25022. #if defined(MBEDTLS_RSA_C)
  25023. #endif
  25024. #if defined(MBEDTLS_ECP_C)
  25025. #endif
  25026. #if defined(MBEDTLS_ECDSA_C)
  25027. #endif
  25028. #include <limits.h>
  25029. #include <stdint.h>
  25030. /* Implementation that should never be optimized out by the compiler */
  25031. /* zeroize was here */
  25032. /*
  25033. * Initialise a mbedtls_pk_context
  25034. */
  25035. void mbedtls_pk_init( mbedtls_pk_context *ctx )
  25036. {
  25037. if( ctx == NULL )
  25038. return;
  25039. ctx->pk_info = NULL;
  25040. ctx->pk_ctx = NULL;
  25041. }
  25042. /*
  25043. * Free (the components of) a mbedtls_pk_context
  25044. */
  25045. void mbedtls_pk_free( mbedtls_pk_context *ctx )
  25046. {
  25047. if( ctx == NULL || ctx->pk_info == NULL )
  25048. return;
  25049. ctx->pk_info->ctx_free_func( ctx->pk_ctx );
  25050. mbedtls_zeroize( ctx, sizeof( mbedtls_pk_context ) );
  25051. }
  25052. /*
  25053. * Get pk_info structure from type
  25054. */
  25055. const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
  25056. {
  25057. switch( pk_type ) {
  25058. #if defined(MBEDTLS_RSA_C)
  25059. case MBEDTLS_PK_RSA:
  25060. return( &mbedtls_rsa_info );
  25061. #endif
  25062. #if defined(MBEDTLS_ECP_C)
  25063. case MBEDTLS_PK_ECKEY:
  25064. return( &mbedtls_eckey_info );
  25065. case MBEDTLS_PK_ECKEY_DH:
  25066. return( &mbedtls_eckeydh_info );
  25067. #endif
  25068. #if defined(MBEDTLS_ECDSA_C)
  25069. case MBEDTLS_PK_ECDSA:
  25070. return( &mbedtls_ecdsa_info );
  25071. #endif
  25072. /* MBEDTLS_PK_RSA_ALT omitted on purpose */
  25073. default:
  25074. return( NULL );
  25075. }
  25076. }
  25077. /*
  25078. * Initialise context
  25079. */
  25080. int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
  25081. {
  25082. if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
  25083. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  25084. if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
  25085. return( MBEDTLS_ERR_PK_ALLOC_FAILED );
  25086. ctx->pk_info = info;
  25087. return( 0 );
  25088. }
  25089. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  25090. /*
  25091. * Initialize an RSA-alt context
  25092. */
  25093. int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
  25094. mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
  25095. mbedtls_pk_rsa_alt_sign_func sign_func,
  25096. mbedtls_pk_rsa_alt_key_len_func key_len_func )
  25097. {
  25098. mbedtls_rsa_alt_context *rsa_alt;
  25099. const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
  25100. if( ctx == NULL || ctx->pk_info != NULL )
  25101. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  25102. if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
  25103. return( MBEDTLS_ERR_PK_ALLOC_FAILED );
  25104. ctx->pk_info = info;
  25105. rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
  25106. rsa_alt->key = key;
  25107. rsa_alt->decrypt_func = decrypt_func;
  25108. rsa_alt->sign_func = sign_func;
  25109. rsa_alt->key_len_func = key_len_func;
  25110. return( 0 );
  25111. }
  25112. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  25113. /*
  25114. * Tell if a PK can do the operations of the given type
  25115. */
  25116. int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
  25117. {
  25118. /* null or NONE context can't do anything */
  25119. if( ctx == NULL || ctx->pk_info == NULL )
  25120. return( 0 );
  25121. return( ctx->pk_info->can_do( type ) );
  25122. }
  25123. /*
  25124. * Helper for mbedtls_pk_sign and mbedtls_pk_verify
  25125. */
  25126. static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
  25127. {
  25128. const mbedtls_md_info_t *md_info;
  25129. if( *hash_len != 0 )
  25130. return( 0 );
  25131. if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
  25132. return( -1 );
  25133. *hash_len = mbedtls_md_get_size( md_info );
  25134. return( 0 );
  25135. }
  25136. /*
  25137. * Verify a signature
  25138. */
  25139. int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  25140. const unsigned char *hash, size_t hash_len,
  25141. const unsigned char *sig, size_t sig_len )
  25142. {
  25143. if( ctx == NULL || ctx->pk_info == NULL ||
  25144. pk_hashlen_helper( md_alg, &hash_len ) != 0 )
  25145. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  25146. if( ctx->pk_info->verify_func == NULL )
  25147. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  25148. return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
  25149. sig, sig_len ) );
  25150. }
  25151. /*
  25152. * Verify a signature with options
  25153. */
  25154. int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
  25155. mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  25156. const unsigned char *hash, size_t hash_len,
  25157. const unsigned char *sig, size_t sig_len )
  25158. {
  25159. if( ctx == NULL || ctx->pk_info == NULL )
  25160. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  25161. if( ! mbedtls_pk_can_do( ctx, type ) )
  25162. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  25163. if( type == MBEDTLS_PK_RSASSA_PSS )
  25164. {
  25165. #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
  25166. int ret;
  25167. const mbedtls_pk_rsassa_pss_options *pss_opts;
  25168. #if SIZE_MAX > UINT_MAX
  25169. if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
  25170. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  25171. #endif /* SIZE_MAX > UINT_MAX */
  25172. if( options == NULL )
  25173. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  25174. pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
  25175. if( sig_len < mbedtls_pk_get_len( ctx ) )
  25176. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  25177. ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
  25178. NULL, NULL, MBEDTLS_RSA_PUBLIC,
  25179. md_alg, (unsigned int) hash_len, hash,
  25180. pss_opts->mgf1_hash_id,
  25181. pss_opts->expected_salt_len,
  25182. sig );
  25183. if( ret != 0 )
  25184. return( ret );
  25185. if( sig_len > mbedtls_pk_get_len( ctx ) )
  25186. return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
  25187. return( 0 );
  25188. #else
  25189. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  25190. #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
  25191. }
  25192. /* General case: no options */
  25193. if( options != NULL )
  25194. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  25195. return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
  25196. }
  25197. /*
  25198. * Make a signature
  25199. */
  25200. int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  25201. const unsigned char *hash, size_t hash_len,
  25202. unsigned char *sig, size_t *sig_len,
  25203. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  25204. {
  25205. if( ctx == NULL || ctx->pk_info == NULL ||
  25206. pk_hashlen_helper( md_alg, &hash_len ) != 0 )
  25207. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  25208. if( ctx->pk_info->sign_func == NULL )
  25209. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  25210. return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
  25211. sig, sig_len, f_rng, p_rng ) );
  25212. }
  25213. /*
  25214. * Decrypt message
  25215. */
  25216. int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
  25217. const unsigned char *input, size_t ilen,
  25218. unsigned char *output, size_t *olen, size_t osize,
  25219. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  25220. {
  25221. if( ctx == NULL || ctx->pk_info == NULL )
  25222. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  25223. if( ctx->pk_info->decrypt_func == NULL )
  25224. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  25225. return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
  25226. output, olen, osize, f_rng, p_rng ) );
  25227. }
  25228. /*
  25229. * Encrypt message
  25230. */
  25231. int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
  25232. const unsigned char *input, size_t ilen,
  25233. unsigned char *output, size_t *olen, size_t osize,
  25234. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  25235. {
  25236. if( ctx == NULL || ctx->pk_info == NULL )
  25237. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  25238. if( ctx->pk_info->encrypt_func == NULL )
  25239. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  25240. return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
  25241. output, olen, osize, f_rng, p_rng ) );
  25242. }
  25243. /*
  25244. * Check public-private key pair
  25245. */
  25246. int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
  25247. {
  25248. if( pub == NULL || pub->pk_info == NULL ||
  25249. prv == NULL || prv->pk_info == NULL ||
  25250. prv->pk_info->check_pair_func == NULL )
  25251. {
  25252. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  25253. }
  25254. if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
  25255. {
  25256. if( pub->pk_info->type != MBEDTLS_PK_RSA )
  25257. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  25258. }
  25259. else
  25260. {
  25261. if( pub->pk_info != prv->pk_info )
  25262. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  25263. }
  25264. return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
  25265. }
  25266. /*
  25267. * Get key size in bits
  25268. */
  25269. size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
  25270. {
  25271. if( ctx == NULL || ctx->pk_info == NULL )
  25272. return( 0 );
  25273. return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
  25274. }
  25275. /*
  25276. * Export debug information
  25277. */
  25278. int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
  25279. {
  25280. if( ctx == NULL || ctx->pk_info == NULL )
  25281. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  25282. if( ctx->pk_info->debug_func == NULL )
  25283. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  25284. ctx->pk_info->debug_func( ctx->pk_ctx, items );
  25285. return( 0 );
  25286. }
  25287. /*
  25288. * Access the PK type name
  25289. */
  25290. const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
  25291. {
  25292. if( ctx == NULL || ctx->pk_info == NULL )
  25293. return( "invalid PK" );
  25294. return( ctx->pk_info->name );
  25295. }
  25296. /*
  25297. * Access the PK type
  25298. */
  25299. mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
  25300. {
  25301. if( ctx == NULL || ctx->pk_info == NULL )
  25302. return( MBEDTLS_PK_NONE );
  25303. return( ctx->pk_info->type );
  25304. }
  25305. #endif /* MBEDTLS_PK_C */
  25306. /*
  25307. Amalgamated build undefines
  25308. */
  25309. #undef ADD
  25310. #undef BC
  25311. #undef BEFORE_COLON
  25312. #undef F
  25313. #undef F0
  25314. #undef F1
  25315. #undef F2
  25316. #undef F3
  25317. #undef F4
  25318. #undef F5
  25319. #undef FSb
  25320. #undef K
  25321. #undef KK
  25322. #undef P
  25323. #undef R
  25324. #undef ROTR
  25325. #undef S
  25326. #undef S0
  25327. #undef S1
  25328. #undef S2
  25329. #undef S3
  25330. #undef SAFE_SNPRINTF
  25331. #undef SHR
  25332. #undef close
  25333. #undef read
  25334. #undef supported_init
  25335. #undef write
  25336. /********* Start of file library/pk_wrap.c ************/
  25337. /*
  25338. * Public Key abstraction layer: wrapper functions
  25339. *
  25340. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  25341. * SPDX-License-Identifier: Apache-2.0
  25342. *
  25343. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  25344. * not use this file except in compliance with the License.
  25345. * You may obtain a copy of the License at
  25346. *
  25347. * http://www.apache.org/licenses/LICENSE-2.0
  25348. *
  25349. * Unless required by applicable law or agreed to in writing, software
  25350. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  25351. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25352. * See the License for the specific language governing permissions and
  25353. * limitations under the License.
  25354. *
  25355. * This file is part of mbed TLS (https://tls.mbed.org)
  25356. */
  25357. #if !defined(MBEDTLS_CONFIG_FILE)
  25358. #else
  25359. #endif
  25360. #if defined(MBEDTLS_PK_C)
  25361. /* Even if RSA not activated, for the sake of RSA-alt */
  25362. #include <string.h>
  25363. #if defined(MBEDTLS_ECP_C)
  25364. #endif
  25365. #if defined(MBEDTLS_ECDSA_C)
  25366. #endif
  25367. #if defined(MBEDTLS_PLATFORM_C)
  25368. #else
  25369. #include <stdlib.h>
  25370. #define mbedtls_calloc calloc
  25371. #define mbedtls_free free
  25372. #endif
  25373. #include <limits.h>
  25374. #include <stdint.h>
  25375. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  25376. /* Implementation that should never be optimized out by the compiler */
  25377. /* zeroize was here */
  25378. #endif
  25379. #if defined(MBEDTLS_RSA_C)
  25380. static int rsa_can_do( mbedtls_pk_type_t type )
  25381. {
  25382. return( type == MBEDTLS_PK_RSA ||
  25383. type == MBEDTLS_PK_RSASSA_PSS );
  25384. }
  25385. static size_t rsa_get_bitlen( const void *ctx )
  25386. {
  25387. const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
  25388. return( 8 * mbedtls_rsa_get_len( rsa ) );
  25389. }
  25390. static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  25391. const unsigned char *hash, size_t hash_len,
  25392. const unsigned char *sig, size_t sig_len )
  25393. {
  25394. int ret;
  25395. mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
  25396. size_t rsa_len = mbedtls_rsa_get_len( rsa );
  25397. #if SIZE_MAX > UINT_MAX
  25398. if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
  25399. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  25400. #endif /* SIZE_MAX > UINT_MAX */
  25401. if( sig_len < rsa_len )
  25402. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  25403. if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, NULL, NULL,
  25404. MBEDTLS_RSA_PUBLIC, md_alg,
  25405. (unsigned int) hash_len, hash, sig ) ) != 0 )
  25406. return( ret );
  25407. if( sig_len > rsa_len )
  25408. return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
  25409. return( 0 );
  25410. }
  25411. static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  25412. const unsigned char *hash, size_t hash_len,
  25413. unsigned char *sig, size_t *sig_len,
  25414. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  25415. {
  25416. mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
  25417. #if SIZE_MAX > UINT_MAX
  25418. if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
  25419. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  25420. #endif /* SIZE_MAX > UINT_MAX */
  25421. *sig_len = mbedtls_rsa_get_len( rsa );
  25422. return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
  25423. md_alg, (unsigned int) hash_len, hash, sig ) );
  25424. }
  25425. static int rsa_decrypt_wrap( void *ctx,
  25426. const unsigned char *input, size_t ilen,
  25427. unsigned char *output, size_t *olen, size_t osize,
  25428. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  25429. {
  25430. mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
  25431. if( ilen != mbedtls_rsa_get_len( rsa ) )
  25432. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  25433. return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
  25434. MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
  25435. }
  25436. static int rsa_encrypt_wrap( void *ctx,
  25437. const unsigned char *input, size_t ilen,
  25438. unsigned char *output, size_t *olen, size_t osize,
  25439. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  25440. {
  25441. mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
  25442. *olen = mbedtls_rsa_get_len( rsa );
  25443. if( *olen > osize )
  25444. return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
  25445. return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng, MBEDTLS_RSA_PUBLIC,
  25446. ilen, input, output ) );
  25447. }
  25448. static int rsa_check_pair_wrap( const void *pub, const void *prv )
  25449. {
  25450. return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
  25451. (const mbedtls_rsa_context *) prv ) );
  25452. }
  25453. static void *rsa_alloc_wrap( void )
  25454. {
  25455. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
  25456. if( ctx != NULL )
  25457. mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 );
  25458. return( ctx );
  25459. }
  25460. static void rsa_free_wrap( void *ctx )
  25461. {
  25462. mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
  25463. mbedtls_free( ctx );
  25464. }
  25465. static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
  25466. {
  25467. items->type = MBEDTLS_PK_DEBUG_MPI;
  25468. items->name = "rsa.N";
  25469. items->value = &( ((mbedtls_rsa_context *) ctx)->N );
  25470. items++;
  25471. items->type = MBEDTLS_PK_DEBUG_MPI;
  25472. items->name = "rsa.E";
  25473. items->value = &( ((mbedtls_rsa_context *) ctx)->E );
  25474. }
  25475. const mbedtls_pk_info_t mbedtls_rsa_info = {
  25476. MBEDTLS_PK_RSA,
  25477. "RSA",
  25478. rsa_get_bitlen,
  25479. rsa_can_do,
  25480. rsa_verify_wrap,
  25481. rsa_sign_wrap,
  25482. rsa_decrypt_wrap,
  25483. rsa_encrypt_wrap,
  25484. rsa_check_pair_wrap,
  25485. rsa_alloc_wrap,
  25486. rsa_free_wrap,
  25487. rsa_debug,
  25488. };
  25489. #endif /* MBEDTLS_RSA_C */
  25490. #if defined(MBEDTLS_ECP_C)
  25491. /*
  25492. * Generic EC key
  25493. */
  25494. static int eckey_can_do( mbedtls_pk_type_t type )
  25495. {
  25496. return( type == MBEDTLS_PK_ECKEY ||
  25497. type == MBEDTLS_PK_ECKEY_DH ||
  25498. type == MBEDTLS_PK_ECDSA );
  25499. }
  25500. static size_t eckey_get_bitlen( const void *ctx )
  25501. {
  25502. return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
  25503. }
  25504. #if defined(MBEDTLS_ECDSA_C)
  25505. /* Forward declarations */
  25506. static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  25507. const unsigned char *hash, size_t hash_len,
  25508. const unsigned char *sig, size_t sig_len );
  25509. static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  25510. const unsigned char *hash, size_t hash_len,
  25511. unsigned char *sig, size_t *sig_len,
  25512. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  25513. static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  25514. const unsigned char *hash, size_t hash_len,
  25515. const unsigned char *sig, size_t sig_len )
  25516. {
  25517. int ret;
  25518. mbedtls_ecdsa_context ecdsa;
  25519. mbedtls_ecdsa_init( &ecdsa );
  25520. if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
  25521. ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
  25522. mbedtls_ecdsa_free( &ecdsa );
  25523. return( ret );
  25524. }
  25525. static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  25526. const unsigned char *hash, size_t hash_len,
  25527. unsigned char *sig, size_t *sig_len,
  25528. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  25529. {
  25530. int ret;
  25531. mbedtls_ecdsa_context ecdsa;
  25532. mbedtls_ecdsa_init( &ecdsa );
  25533. if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
  25534. ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
  25535. f_rng, p_rng );
  25536. mbedtls_ecdsa_free( &ecdsa );
  25537. return( ret );
  25538. }
  25539. #endif /* MBEDTLS_ECDSA_C */
  25540. static int eckey_check_pair( const void *pub, const void *prv )
  25541. {
  25542. return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
  25543. (const mbedtls_ecp_keypair *) prv ) );
  25544. }
  25545. static void *eckey_alloc_wrap( void )
  25546. {
  25547. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
  25548. if( ctx != NULL )
  25549. mbedtls_ecp_keypair_init( ctx );
  25550. return( ctx );
  25551. }
  25552. static void eckey_free_wrap( void *ctx )
  25553. {
  25554. mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
  25555. mbedtls_free( ctx );
  25556. }
  25557. static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
  25558. {
  25559. items->type = MBEDTLS_PK_DEBUG_ECP;
  25560. items->name = "eckey.Q";
  25561. items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
  25562. }
  25563. const mbedtls_pk_info_t mbedtls_eckey_info = {
  25564. MBEDTLS_PK_ECKEY,
  25565. "EC",
  25566. eckey_get_bitlen,
  25567. eckey_can_do,
  25568. #if defined(MBEDTLS_ECDSA_C)
  25569. eckey_verify_wrap,
  25570. eckey_sign_wrap,
  25571. #else
  25572. NULL,
  25573. NULL,
  25574. #endif
  25575. NULL,
  25576. NULL,
  25577. eckey_check_pair,
  25578. eckey_alloc_wrap,
  25579. eckey_free_wrap,
  25580. eckey_debug,
  25581. };
  25582. /*
  25583. * EC key restricted to ECDH
  25584. */
  25585. static int eckeydh_can_do( mbedtls_pk_type_t type )
  25586. {
  25587. return( type == MBEDTLS_PK_ECKEY ||
  25588. type == MBEDTLS_PK_ECKEY_DH );
  25589. }
  25590. const mbedtls_pk_info_t mbedtls_eckeydh_info = {
  25591. MBEDTLS_PK_ECKEY_DH,
  25592. "EC_DH",
  25593. eckey_get_bitlen, /* Same underlying key structure */
  25594. eckeydh_can_do,
  25595. NULL,
  25596. NULL,
  25597. NULL,
  25598. NULL,
  25599. eckey_check_pair,
  25600. eckey_alloc_wrap, /* Same underlying key structure */
  25601. eckey_free_wrap, /* Same underlying key structure */
  25602. eckey_debug, /* Same underlying key structure */
  25603. };
  25604. #endif /* MBEDTLS_ECP_C */
  25605. #if defined(MBEDTLS_ECDSA_C)
  25606. static int ecdsa_can_do( mbedtls_pk_type_t type )
  25607. {
  25608. return( type == MBEDTLS_PK_ECDSA );
  25609. }
  25610. static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  25611. const unsigned char *hash, size_t hash_len,
  25612. const unsigned char *sig, size_t sig_len )
  25613. {
  25614. int ret;
  25615. ((void) md_alg);
  25616. ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
  25617. hash, hash_len, sig, sig_len );
  25618. if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
  25619. return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
  25620. return( ret );
  25621. }
  25622. static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  25623. const unsigned char *hash, size_t hash_len,
  25624. unsigned char *sig, size_t *sig_len,
  25625. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  25626. {
  25627. return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
  25628. md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
  25629. }
  25630. static void *ecdsa_alloc_wrap( void )
  25631. {
  25632. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
  25633. if( ctx != NULL )
  25634. mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
  25635. return( ctx );
  25636. }
  25637. static void ecdsa_free_wrap( void *ctx )
  25638. {
  25639. mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
  25640. mbedtls_free( ctx );
  25641. }
  25642. const mbedtls_pk_info_t mbedtls_ecdsa_info = {
  25643. MBEDTLS_PK_ECDSA,
  25644. "ECDSA",
  25645. eckey_get_bitlen, /* Compatible key structures */
  25646. ecdsa_can_do,
  25647. ecdsa_verify_wrap,
  25648. ecdsa_sign_wrap,
  25649. NULL,
  25650. NULL,
  25651. eckey_check_pair, /* Compatible key structures */
  25652. ecdsa_alloc_wrap,
  25653. ecdsa_free_wrap,
  25654. eckey_debug, /* Compatible key structures */
  25655. };
  25656. #endif /* MBEDTLS_ECDSA_C */
  25657. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  25658. /*
  25659. * Support for alternative RSA-private implementations
  25660. */
  25661. static int rsa_alt_can_do( mbedtls_pk_type_t type )
  25662. {
  25663. return( type == MBEDTLS_PK_RSA );
  25664. }
  25665. static size_t rsa_alt_get_bitlen( const void *ctx )
  25666. {
  25667. const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
  25668. return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
  25669. }
  25670. static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  25671. const unsigned char *hash, size_t hash_len,
  25672. unsigned char *sig, size_t *sig_len,
  25673. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  25674. {
  25675. mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
  25676. #if SIZE_MAX > UINT_MAX
  25677. if( UINT_MAX < hash_len )
  25678. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  25679. #endif /* SIZE_MAX > UINT_MAX */
  25680. *sig_len = rsa_alt->key_len_func( rsa_alt->key );
  25681. return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
  25682. md_alg, (unsigned int) hash_len, hash, sig ) );
  25683. }
  25684. static int rsa_alt_decrypt_wrap( void *ctx,
  25685. const unsigned char *input, size_t ilen,
  25686. unsigned char *output, size_t *olen, size_t osize,
  25687. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  25688. {
  25689. mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
  25690. ((void) f_rng);
  25691. ((void) p_rng);
  25692. if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
  25693. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  25694. return( rsa_alt->decrypt_func( rsa_alt->key,
  25695. MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
  25696. }
  25697. #if defined(MBEDTLS_RSA_C)
  25698. static int rsa_alt_check_pair( const void *pub, const void *prv )
  25699. {
  25700. unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
  25701. unsigned char hash[32];
  25702. size_t sig_len = 0;
  25703. int ret;
  25704. if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
  25705. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  25706. memset( hash, 0x2a, sizeof( hash ) );
  25707. if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
  25708. hash, sizeof( hash ),
  25709. sig, &sig_len, NULL, NULL ) ) != 0 )
  25710. {
  25711. return( ret );
  25712. }
  25713. if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
  25714. hash, sizeof( hash ), sig, sig_len ) != 0 )
  25715. {
  25716. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  25717. }
  25718. return( 0 );
  25719. }
  25720. #endif /* MBEDTLS_RSA_C */
  25721. static void *rsa_alt_alloc_wrap( void )
  25722. {
  25723. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
  25724. if( ctx != NULL )
  25725. memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
  25726. return( ctx );
  25727. }
  25728. static void rsa_alt_free_wrap( void *ctx )
  25729. {
  25730. mbedtls_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
  25731. mbedtls_free( ctx );
  25732. }
  25733. const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
  25734. MBEDTLS_PK_RSA_ALT,
  25735. "RSA-alt",
  25736. rsa_alt_get_bitlen,
  25737. rsa_alt_can_do,
  25738. NULL,
  25739. rsa_alt_sign_wrap,
  25740. rsa_alt_decrypt_wrap,
  25741. NULL,
  25742. #if defined(MBEDTLS_RSA_C)
  25743. rsa_alt_check_pair,
  25744. #else
  25745. NULL,
  25746. #endif
  25747. rsa_alt_alloc_wrap,
  25748. rsa_alt_free_wrap,
  25749. NULL,
  25750. };
  25751. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  25752. #endif /* MBEDTLS_PK_C */
  25753. /*
  25754. Amalgamated build undefines
  25755. */
  25756. #undef ADD
  25757. #undef BC
  25758. #undef BEFORE_COLON
  25759. #undef F
  25760. #undef F0
  25761. #undef F1
  25762. #undef F2
  25763. #undef F3
  25764. #undef F4
  25765. #undef F5
  25766. #undef FSb
  25767. #undef K
  25768. #undef KK
  25769. #undef P
  25770. #undef R
  25771. #undef ROTR
  25772. #undef S
  25773. #undef S0
  25774. #undef S1
  25775. #undef S2
  25776. #undef S3
  25777. #undef SAFE_SNPRINTF
  25778. #undef SHR
  25779. #undef close
  25780. #undef read
  25781. #undef supported_init
  25782. #undef write
  25783. /********* Start of file library/pkcs11.c ************/
  25784. /**
  25785. * \file pkcs11.c
  25786. *
  25787. * \brief Wrapper for PKCS#11 library libpkcs11-helper
  25788. *
  25789. * \author Adriaan de Jong <dejong@fox-it.com>
  25790. *
  25791. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  25792. * SPDX-License-Identifier: Apache-2.0
  25793. *
  25794. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  25795. * not use this file except in compliance with the License.
  25796. * You may obtain a copy of the License at
  25797. *
  25798. * http://www.apache.org/licenses/LICENSE-2.0
  25799. *
  25800. * Unless required by applicable law or agreed to in writing, software
  25801. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  25802. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25803. * See the License for the specific language governing permissions and
  25804. * limitations under the License.
  25805. *
  25806. * This file is part of mbed TLS (https://tls.mbed.org)
  25807. */
  25808. #if defined(MBEDTLS_PKCS11_C)
  25809. #if defined(MBEDTLS_PLATFORM_C)
  25810. #else
  25811. #include <stdlib.h>
  25812. #define mbedtls_calloc calloc
  25813. #define mbedtls_free free
  25814. #endif
  25815. #include <string.h>
  25816. void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx )
  25817. {
  25818. memset( ctx, 0, sizeof( mbedtls_pkcs11_context ) );
  25819. }
  25820. int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11_cert )
  25821. {
  25822. int ret = 1;
  25823. unsigned char *cert_blob = NULL;
  25824. size_t cert_blob_size = 0;
  25825. if( cert == NULL )
  25826. {
  25827. ret = 2;
  25828. goto cleanup;
  25829. }
  25830. if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL,
  25831. &cert_blob_size ) != CKR_OK )
  25832. {
  25833. ret = 3;
  25834. goto cleanup;
  25835. }
  25836. cert_blob = mbedtls_calloc( 1, cert_blob_size );
  25837. if( NULL == cert_blob )
  25838. {
  25839. ret = 4;
  25840. goto cleanup;
  25841. }
  25842. if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob,
  25843. &cert_blob_size ) != CKR_OK )
  25844. {
  25845. ret = 5;
  25846. goto cleanup;
  25847. }
  25848. if( 0 != mbedtls_x509_crt_parse( cert, cert_blob, cert_blob_size ) )
  25849. {
  25850. ret = 6;
  25851. goto cleanup;
  25852. }
  25853. ret = 0;
  25854. cleanup:
  25855. if( NULL != cert_blob )
  25856. mbedtls_free( cert_blob );
  25857. return( ret );
  25858. }
  25859. int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
  25860. pkcs11h_certificate_t pkcs11_cert )
  25861. {
  25862. int ret = 1;
  25863. mbedtls_x509_crt cert;
  25864. mbedtls_x509_crt_init( &cert );
  25865. if( priv_key == NULL )
  25866. goto cleanup;
  25867. if( 0 != mbedtls_pkcs11_x509_cert_bind( &cert, pkcs11_cert ) )
  25868. goto cleanup;
  25869. priv_key->len = mbedtls_pk_get_len( &cert.pk );
  25870. priv_key->pkcs11h_cert = pkcs11_cert;
  25871. ret = 0;
  25872. cleanup:
  25873. mbedtls_x509_crt_free( &cert );
  25874. return( ret );
  25875. }
  25876. void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key )
  25877. {
  25878. if( NULL != priv_key )
  25879. pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert );
  25880. }
  25881. int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
  25882. int mode, size_t *olen,
  25883. const unsigned char *input,
  25884. unsigned char *output,
  25885. size_t output_max_len )
  25886. {
  25887. size_t input_len, output_len;
  25888. if( NULL == ctx )
  25889. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  25890. if( MBEDTLS_RSA_PRIVATE != mode )
  25891. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  25892. output_len = input_len = ctx->len;
  25893. if( input_len < 16 || input_len > output_max_len )
  25894. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  25895. /* Determine size of output buffer */
  25896. if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
  25897. input_len, NULL, &output_len ) != CKR_OK )
  25898. {
  25899. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  25900. }
  25901. if( output_len > output_max_len )
  25902. return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
  25903. if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
  25904. input_len, output, &output_len ) != CKR_OK )
  25905. {
  25906. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  25907. }
  25908. *olen = output_len;
  25909. return( 0 );
  25910. }
  25911. int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
  25912. int mode,
  25913. mbedtls_md_type_t md_alg,
  25914. unsigned int hashlen,
  25915. const unsigned char *hash,
  25916. unsigned char *sig )
  25917. {
  25918. size_t sig_len = 0, asn_len = 0, oid_size = 0;
  25919. unsigned char *p = sig;
  25920. const char *oid;
  25921. if( NULL == ctx )
  25922. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  25923. if( MBEDTLS_RSA_PRIVATE != mode )
  25924. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  25925. if( md_alg != MBEDTLS_MD_NONE )
  25926. {
  25927. const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
  25928. if( md_info == NULL )
  25929. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  25930. if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
  25931. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  25932. hashlen = mbedtls_md_get_size( md_info );
  25933. asn_len = 10 + oid_size;
  25934. }
  25935. sig_len = ctx->len;
  25936. if( hashlen > sig_len || asn_len > sig_len ||
  25937. hashlen + asn_len > sig_len )
  25938. {
  25939. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  25940. }
  25941. if( md_alg != MBEDTLS_MD_NONE )
  25942. {
  25943. /*
  25944. * DigestInfo ::= SEQUENCE {
  25945. * digestAlgorithm DigestAlgorithmIdentifier,
  25946. * digest Digest }
  25947. *
  25948. * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
  25949. *
  25950. * Digest ::= OCTET STRING
  25951. */
  25952. *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
  25953. *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
  25954. *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
  25955. *p++ = (unsigned char) ( 0x04 + oid_size );
  25956. *p++ = MBEDTLS_ASN1_OID;
  25957. *p++ = oid_size & 0xFF;
  25958. memcpy( p, oid, oid_size );
  25959. p += oid_size;
  25960. *p++ = MBEDTLS_ASN1_NULL;
  25961. *p++ = 0x00;
  25962. *p++ = MBEDTLS_ASN1_OCTET_STRING;
  25963. *p++ = hashlen;
  25964. }
  25965. memcpy( p, hash, hashlen );
  25966. if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig,
  25967. asn_len + hashlen, sig, &sig_len ) != CKR_OK )
  25968. {
  25969. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  25970. }
  25971. return( 0 );
  25972. }
  25973. #endif /* defined(MBEDTLS_PKCS11_C) */
  25974. /*
  25975. Amalgamated build undefines
  25976. */
  25977. #undef ADD
  25978. #undef BC
  25979. #undef BEFORE_COLON
  25980. #undef F
  25981. #undef F0
  25982. #undef F1
  25983. #undef F2
  25984. #undef F3
  25985. #undef F4
  25986. #undef F5
  25987. #undef FSb
  25988. #undef K
  25989. #undef KK
  25990. #undef P
  25991. #undef R
  25992. #undef ROTR
  25993. #undef S
  25994. #undef S0
  25995. #undef S1
  25996. #undef S2
  25997. #undef S3
  25998. #undef SAFE_SNPRINTF
  25999. #undef SHR
  26000. #undef close
  26001. #undef read
  26002. #undef supported_init
  26003. #undef write
  26004. /********* Start of file library/pkcs12.c ************/
  26005. /*
  26006. * PKCS#12 Personal Information Exchange Syntax
  26007. *
  26008. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  26009. * SPDX-License-Identifier: Apache-2.0
  26010. *
  26011. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  26012. * not use this file except in compliance with the License.
  26013. * You may obtain a copy of the License at
  26014. *
  26015. * http://www.apache.org/licenses/LICENSE-2.0
  26016. *
  26017. * Unless required by applicable law or agreed to in writing, software
  26018. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  26019. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26020. * See the License for the specific language governing permissions and
  26021. * limitations under the License.
  26022. *
  26023. * This file is part of mbed TLS (https://tls.mbed.org)
  26024. */
  26025. /*
  26026. * The PKCS #12 Personal Information Exchange Syntax Standard v1.1
  26027. *
  26028. * http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf
  26029. * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn
  26030. */
  26031. #if !defined(MBEDTLS_CONFIG_FILE)
  26032. #else
  26033. #endif
  26034. #if defined(MBEDTLS_PKCS12_C)
  26035. #include <string.h>
  26036. #if defined(MBEDTLS_ARC4_C)
  26037. #endif
  26038. #if defined(MBEDTLS_DES_C)
  26039. #endif
  26040. /* Implementation that should never be optimized out by the compiler */
  26041. /* zeroize was here */
  26042. static int pkcs12_parse_pbe_params( mbedtls_asn1_buf *params,
  26043. mbedtls_asn1_buf *salt, int *iterations )
  26044. {
  26045. int ret;
  26046. unsigned char **p = &params->p;
  26047. const unsigned char *end = params->p + params->len;
  26048. /*
  26049. * pkcs-12PbeParams ::= SEQUENCE {
  26050. * salt OCTET STRING,
  26051. * iterations INTEGER
  26052. * }
  26053. *
  26054. */
  26055. if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
  26056. return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
  26057. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  26058. if( ( ret = mbedtls_asn1_get_tag( p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  26059. return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
  26060. salt->p = *p;
  26061. *p += salt->len;
  26062. if( ( ret = mbedtls_asn1_get_int( p, end, iterations ) ) != 0 )
  26063. return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
  26064. if( *p != end )
  26065. return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
  26066. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  26067. return( 0 );
  26068. }
  26069. #define PKCS12_MAX_PWDLEN 128
  26070. static int pkcs12_pbe_derive_key_iv( mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
  26071. const unsigned char *pwd, size_t pwdlen,
  26072. unsigned char *key, size_t keylen,
  26073. unsigned char *iv, size_t ivlen )
  26074. {
  26075. int ret, iterations = 0;
  26076. mbedtls_asn1_buf salt;
  26077. size_t i;
  26078. unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
  26079. if( pwdlen > PKCS12_MAX_PWDLEN )
  26080. return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
  26081. memset( &salt, 0, sizeof(mbedtls_asn1_buf) );
  26082. memset( &unipwd, 0, sizeof(unipwd) );
  26083. if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
  26084. &iterations ) ) != 0 )
  26085. return( ret );
  26086. for( i = 0; i < pwdlen; i++ )
  26087. unipwd[i * 2 + 1] = pwd[i];
  26088. if( ( ret = mbedtls_pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
  26089. salt.p, salt.len, md_type,
  26090. MBEDTLS_PKCS12_DERIVE_KEY, iterations ) ) != 0 )
  26091. {
  26092. return( ret );
  26093. }
  26094. if( iv == NULL || ivlen == 0 )
  26095. return( 0 );
  26096. if( ( ret = mbedtls_pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
  26097. salt.p, salt.len, md_type,
  26098. MBEDTLS_PKCS12_DERIVE_IV, iterations ) ) != 0 )
  26099. {
  26100. return( ret );
  26101. }
  26102. return( 0 );
  26103. }
  26104. #undef PKCS12_MAX_PWDLEN
  26105. int mbedtls_pkcs12_pbe_sha1_rc4_128( mbedtls_asn1_buf *pbe_params, int mode,
  26106. const unsigned char *pwd, size_t pwdlen,
  26107. const unsigned char *data, size_t len,
  26108. unsigned char *output )
  26109. {
  26110. #if !defined(MBEDTLS_ARC4_C)
  26111. ((void) pbe_params);
  26112. ((void) mode);
  26113. ((void) pwd);
  26114. ((void) pwdlen);
  26115. ((void) data);
  26116. ((void) len);
  26117. ((void) output);
  26118. return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
  26119. #else
  26120. int ret;
  26121. unsigned char key[16];
  26122. mbedtls_arc4_context ctx;
  26123. ((void) mode);
  26124. mbedtls_arc4_init( &ctx );
  26125. if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, MBEDTLS_MD_SHA1,
  26126. pwd, pwdlen,
  26127. key, 16, NULL, 0 ) ) != 0 )
  26128. {
  26129. return( ret );
  26130. }
  26131. mbedtls_arc4_setup( &ctx, key, 16 );
  26132. if( ( ret = mbedtls_arc4_crypt( &ctx, len, data, output ) ) != 0 )
  26133. goto exit;
  26134. exit:
  26135. mbedtls_zeroize( key, sizeof( key ) );
  26136. mbedtls_arc4_free( &ctx );
  26137. return( ret );
  26138. #endif /* MBEDTLS_ARC4_C */
  26139. }
  26140. int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode,
  26141. mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
  26142. const unsigned char *pwd, size_t pwdlen,
  26143. const unsigned char *data, size_t len,
  26144. unsigned char *output )
  26145. {
  26146. int ret, keylen = 0;
  26147. unsigned char key[32];
  26148. unsigned char iv[16];
  26149. const mbedtls_cipher_info_t *cipher_info;
  26150. mbedtls_cipher_context_t cipher_ctx;
  26151. size_t olen = 0;
  26152. cipher_info = mbedtls_cipher_info_from_type( cipher_type );
  26153. if( cipher_info == NULL )
  26154. return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
  26155. keylen = cipher_info->key_bitlen / 8;
  26156. if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
  26157. key, keylen,
  26158. iv, cipher_info->iv_size ) ) != 0 )
  26159. {
  26160. return( ret );
  26161. }
  26162. mbedtls_cipher_init( &cipher_ctx );
  26163. if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
  26164. goto exit;
  26165. if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
  26166. goto exit;
  26167. if( ( ret = mbedtls_cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
  26168. goto exit;
  26169. if( ( ret = mbedtls_cipher_reset( &cipher_ctx ) ) != 0 )
  26170. goto exit;
  26171. if( ( ret = mbedtls_cipher_update( &cipher_ctx, data, len,
  26172. output, &olen ) ) != 0 )
  26173. {
  26174. goto exit;
  26175. }
  26176. if( ( ret = mbedtls_cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
  26177. ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
  26178. exit:
  26179. mbedtls_zeroize( key, sizeof( key ) );
  26180. mbedtls_zeroize( iv, sizeof( iv ) );
  26181. mbedtls_cipher_free( &cipher_ctx );
  26182. return( ret );
  26183. }
  26184. static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
  26185. const unsigned char *filler, size_t fill_len )
  26186. {
  26187. unsigned char *p = data;
  26188. size_t use_len;
  26189. while( data_len > 0 )
  26190. {
  26191. use_len = ( data_len > fill_len ) ? fill_len : data_len;
  26192. memcpy( p, filler, use_len );
  26193. p += use_len;
  26194. data_len -= use_len;
  26195. }
  26196. }
  26197. int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
  26198. const unsigned char *pwd, size_t pwdlen,
  26199. const unsigned char *salt, size_t saltlen,
  26200. mbedtls_md_type_t md_type, int id, int iterations )
  26201. {
  26202. int ret;
  26203. unsigned int j;
  26204. unsigned char diversifier[128];
  26205. unsigned char salt_block[128], pwd_block[128], hash_block[128];
  26206. unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
  26207. unsigned char *p;
  26208. unsigned char c;
  26209. size_t hlen, use_len, v, i;
  26210. const mbedtls_md_info_t *md_info;
  26211. mbedtls_md_context_t md_ctx;
  26212. // This version only allows max of 64 bytes of password or salt
  26213. if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
  26214. return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
  26215. md_info = mbedtls_md_info_from_type( md_type );
  26216. if( md_info == NULL )
  26217. return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
  26218. mbedtls_md_init( &md_ctx );
  26219. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
  26220. return( ret );
  26221. hlen = mbedtls_md_get_size( md_info );
  26222. if( hlen <= 32 )
  26223. v = 64;
  26224. else
  26225. v = 128;
  26226. memset( diversifier, (unsigned char) id, v );
  26227. pkcs12_fill_buffer( salt_block, v, salt, saltlen );
  26228. pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
  26229. p = data;
  26230. while( datalen > 0 )
  26231. {
  26232. // Calculate hash( diversifier || salt_block || pwd_block )
  26233. if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
  26234. goto exit;
  26235. if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 )
  26236. goto exit;
  26237. if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v ) ) != 0 )
  26238. goto exit;
  26239. if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v ) ) != 0 )
  26240. goto exit;
  26241. if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 )
  26242. goto exit;
  26243. // Perform remaining ( iterations - 1 ) recursive hash calculations
  26244. for( i = 1; i < (size_t) iterations; i++ )
  26245. {
  26246. if( ( ret = mbedtls_md( md_info, hash_output, hlen, hash_output ) ) != 0 )
  26247. goto exit;
  26248. }
  26249. use_len = ( datalen > hlen ) ? hlen : datalen;
  26250. memcpy( p, hash_output, use_len );
  26251. datalen -= use_len;
  26252. p += use_len;
  26253. if( datalen == 0 )
  26254. break;
  26255. // Concatenating copies of hash_output into hash_block (B)
  26256. pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
  26257. // B += 1
  26258. for( i = v; i > 0; i-- )
  26259. if( ++hash_block[i - 1] != 0 )
  26260. break;
  26261. // salt_block += B
  26262. c = 0;
  26263. for( i = v; i > 0; i-- )
  26264. {
  26265. j = salt_block[i - 1] + hash_block[i - 1] + c;
  26266. c = (unsigned char) (j >> 8);
  26267. salt_block[i - 1] = j & 0xFF;
  26268. }
  26269. // pwd_block += B
  26270. c = 0;
  26271. for( i = v; i > 0; i-- )
  26272. {
  26273. j = pwd_block[i - 1] + hash_block[i - 1] + c;
  26274. c = (unsigned char) (j >> 8);
  26275. pwd_block[i - 1] = j & 0xFF;
  26276. }
  26277. }
  26278. ret = 0;
  26279. exit:
  26280. mbedtls_zeroize( salt_block, sizeof( salt_block ) );
  26281. mbedtls_zeroize( pwd_block, sizeof( pwd_block ) );
  26282. mbedtls_zeroize( hash_block, sizeof( hash_block ) );
  26283. mbedtls_zeroize( hash_output, sizeof( hash_output ) );
  26284. mbedtls_md_free( &md_ctx );
  26285. return( ret );
  26286. }
  26287. #endif /* MBEDTLS_PKCS12_C */
  26288. /*
  26289. Amalgamated build undefines
  26290. */
  26291. #undef ADD
  26292. #undef BC
  26293. #undef BEFORE_COLON
  26294. #undef F
  26295. #undef F0
  26296. #undef F1
  26297. #undef F2
  26298. #undef F3
  26299. #undef F4
  26300. #undef F5
  26301. #undef FSb
  26302. #undef K
  26303. #undef KK
  26304. #undef P
  26305. #undef R
  26306. #undef ROTR
  26307. #undef S
  26308. #undef S0
  26309. #undef S1
  26310. #undef S2
  26311. #undef S3
  26312. #undef SAFE_SNPRINTF
  26313. #undef SHR
  26314. #undef close
  26315. #undef read
  26316. #undef supported_init
  26317. #undef write
  26318. /********* Start of file library/pkcs5.c ************/
  26319. /**
  26320. * \file pkcs5.c
  26321. *
  26322. * \brief PKCS#5 functions
  26323. *
  26324. * \author Mathias Olsson <mathias@kompetensum.com>
  26325. *
  26326. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  26327. * SPDX-License-Identifier: Apache-2.0
  26328. *
  26329. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  26330. * not use this file except in compliance with the License.
  26331. * You may obtain a copy of the License at
  26332. *
  26333. * http://www.apache.org/licenses/LICENSE-2.0
  26334. *
  26335. * Unless required by applicable law or agreed to in writing, software
  26336. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  26337. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26338. * See the License for the specific language governing permissions and
  26339. * limitations under the License.
  26340. *
  26341. * This file is part of mbed TLS (https://tls.mbed.org)
  26342. */
  26343. /*
  26344. * PKCS#5 includes PBKDF2 and more
  26345. *
  26346. * http://tools.ietf.org/html/rfc2898 (Specification)
  26347. * http://tools.ietf.org/html/rfc6070 (Test vectors)
  26348. */
  26349. #if !defined(MBEDTLS_CONFIG_FILE)
  26350. #else
  26351. #endif
  26352. #if defined(MBEDTLS_PKCS5_C)
  26353. #include <string.h>
  26354. #if defined(MBEDTLS_PLATFORM_C)
  26355. #else
  26356. #include <stdio.h>
  26357. #define mbedtls_printf printf
  26358. #endif
  26359. static int pkcs5_parse_pbkdf2_params( const mbedtls_asn1_buf *params,
  26360. mbedtls_asn1_buf *salt, int *iterations,
  26361. int *keylen, mbedtls_md_type_t *md_type )
  26362. {
  26363. int ret;
  26364. mbedtls_asn1_buf prf_alg_oid;
  26365. unsigned char *p = params->p;
  26366. const unsigned char *end = params->p + params->len;
  26367. if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
  26368. return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
  26369. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  26370. /*
  26371. * PBKDF2-params ::= SEQUENCE {
  26372. * salt OCTET STRING,
  26373. * iterationCount INTEGER,
  26374. * keyLength INTEGER OPTIONAL
  26375. * prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
  26376. * }
  26377. *
  26378. */
  26379. if( ( ret = mbedtls_asn1_get_tag( &p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  26380. return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
  26381. salt->p = p;
  26382. p += salt->len;
  26383. if( ( ret = mbedtls_asn1_get_int( &p, end, iterations ) ) != 0 )
  26384. return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
  26385. if( p == end )
  26386. return( 0 );
  26387. if( ( ret = mbedtls_asn1_get_int( &p, end, keylen ) ) != 0 )
  26388. {
  26389. if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  26390. return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
  26391. }
  26392. if( p == end )
  26393. return( 0 );
  26394. if( ( ret = mbedtls_asn1_get_alg_null( &p, end, &prf_alg_oid ) ) != 0 )
  26395. return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
  26396. if( mbedtls_oid_get_md_hmac( &prf_alg_oid, md_type ) != 0 )
  26397. return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
  26398. if( p != end )
  26399. return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
  26400. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  26401. return( 0 );
  26402. }
  26403. int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode,
  26404. const unsigned char *pwd, size_t pwdlen,
  26405. const unsigned char *data, size_t datalen,
  26406. unsigned char *output )
  26407. {
  26408. int ret, iterations = 0, keylen = 0;
  26409. unsigned char *p, *end;
  26410. mbedtls_asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
  26411. mbedtls_asn1_buf salt;
  26412. mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
  26413. unsigned char key[32], iv[32];
  26414. size_t olen = 0;
  26415. const mbedtls_md_info_t *md_info;
  26416. const mbedtls_cipher_info_t *cipher_info;
  26417. mbedtls_md_context_t md_ctx;
  26418. mbedtls_cipher_type_t cipher_alg;
  26419. mbedtls_cipher_context_t cipher_ctx;
  26420. p = pbe_params->p;
  26421. end = p + pbe_params->len;
  26422. /*
  26423. * PBES2-params ::= SEQUENCE {
  26424. * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
  26425. * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
  26426. * }
  26427. */
  26428. if( pbe_params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
  26429. return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
  26430. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  26431. if( ( ret = mbedtls_asn1_get_alg( &p, end, &kdf_alg_oid, &kdf_alg_params ) ) != 0 )
  26432. return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
  26433. // Only PBKDF2 supported at the moment
  26434. //
  26435. if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBKDF2, &kdf_alg_oid ) != 0 )
  26436. return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
  26437. if( ( ret = pkcs5_parse_pbkdf2_params( &kdf_alg_params,
  26438. &salt, &iterations, &keylen,
  26439. &md_type ) ) != 0 )
  26440. {
  26441. return( ret );
  26442. }
  26443. md_info = mbedtls_md_info_from_type( md_type );
  26444. if( md_info == NULL )
  26445. return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
  26446. if( ( ret = mbedtls_asn1_get_alg( &p, end, &enc_scheme_oid,
  26447. &enc_scheme_params ) ) != 0 )
  26448. {
  26449. return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
  26450. }
  26451. if( mbedtls_oid_get_cipher_alg( &enc_scheme_oid, &cipher_alg ) != 0 )
  26452. return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
  26453. cipher_info = mbedtls_cipher_info_from_type( cipher_alg );
  26454. if( cipher_info == NULL )
  26455. return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
  26456. /*
  26457. * The value of keylen from pkcs5_parse_pbkdf2_params() is ignored
  26458. * since it is optional and we don't know if it was set or not
  26459. */
  26460. keylen = cipher_info->key_bitlen / 8;
  26461. if( enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING ||
  26462. enc_scheme_params.len != cipher_info->iv_size )
  26463. {
  26464. return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT );
  26465. }
  26466. mbedtls_md_init( &md_ctx );
  26467. mbedtls_cipher_init( &cipher_ctx );
  26468. memcpy( iv, enc_scheme_params.p, enc_scheme_params.len );
  26469. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
  26470. goto exit;
  26471. if( ( ret = mbedtls_pkcs5_pbkdf2_hmac( &md_ctx, pwd, pwdlen, salt.p, salt.len,
  26472. iterations, keylen, key ) ) != 0 )
  26473. {
  26474. goto exit;
  26475. }
  26476. if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
  26477. goto exit;
  26478. if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
  26479. goto exit;
  26480. if( ( ret = mbedtls_cipher_crypt( &cipher_ctx, iv, enc_scheme_params.len,
  26481. data, datalen, output, &olen ) ) != 0 )
  26482. ret = MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH;
  26483. exit:
  26484. mbedtls_md_free( &md_ctx );
  26485. mbedtls_cipher_free( &cipher_ctx );
  26486. return( ret );
  26487. }
  26488. int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx, const unsigned char *password,
  26489. size_t plen, const unsigned char *salt, size_t slen,
  26490. unsigned int iteration_count,
  26491. uint32_t key_length, unsigned char *output )
  26492. {
  26493. int ret, j;
  26494. unsigned int i;
  26495. unsigned char md1[MBEDTLS_MD_MAX_SIZE];
  26496. unsigned char work[MBEDTLS_MD_MAX_SIZE];
  26497. unsigned char md_size = mbedtls_md_get_size( ctx->md_info );
  26498. size_t use_len;
  26499. unsigned char *out_p = output;
  26500. unsigned char counter[4];
  26501. memset( counter, 0, 4 );
  26502. counter[3] = 1;
  26503. if( iteration_count > 0xFFFFFFFF )
  26504. return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA );
  26505. while( key_length )
  26506. {
  26507. // U1 ends up in work
  26508. //
  26509. if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
  26510. return( ret );
  26511. if( ( ret = mbedtls_md_hmac_update( ctx, salt, slen ) ) != 0 )
  26512. return( ret );
  26513. if( ( ret = mbedtls_md_hmac_update( ctx, counter, 4 ) ) != 0 )
  26514. return( ret );
  26515. if( ( ret = mbedtls_md_hmac_finish( ctx, work ) ) != 0 )
  26516. return( ret );
  26517. memcpy( md1, work, md_size );
  26518. for( i = 1; i < iteration_count; i++ )
  26519. {
  26520. // U2 ends up in md1
  26521. //
  26522. if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
  26523. return( ret );
  26524. if( ( ret = mbedtls_md_hmac_update( ctx, md1, md_size ) ) != 0 )
  26525. return( ret );
  26526. if( ( ret = mbedtls_md_hmac_finish( ctx, md1 ) ) != 0 )
  26527. return( ret );
  26528. // U1 xor U2
  26529. //
  26530. for( j = 0; j < md_size; j++ )
  26531. work[j] ^= md1[j];
  26532. }
  26533. use_len = ( key_length < md_size ) ? key_length : md_size;
  26534. memcpy( out_p, work, use_len );
  26535. key_length -= (uint32_t) use_len;
  26536. out_p += use_len;
  26537. for( i = 4; i > 0; i-- )
  26538. if( ++counter[i - 1] != 0 )
  26539. break;
  26540. }
  26541. return( 0 );
  26542. }
  26543. #if defined(MBEDTLS_SELF_TEST)
  26544. #if !defined(MBEDTLS_SHA1_C)
  26545. int mbedtls_pkcs5_self_test( int verbose )
  26546. {
  26547. if( verbose != 0 )
  26548. mbedtls_printf( " PBKDF2 (SHA1): skipped\n\n" );
  26549. return( 0 );
  26550. }
  26551. #else
  26552. #define MAX_TESTS 6
  26553. static const size_t plen[MAX_TESTS] =
  26554. { 8, 8, 8, 24, 9 };
  26555. static const unsigned char password[MAX_TESTS][32] =
  26556. {
  26557. "password",
  26558. "password",
  26559. "password",
  26560. "passwordPASSWORDpassword",
  26561. "pass\0word",
  26562. };
  26563. static const size_t slen[MAX_TESTS] =
  26564. { 4, 4, 4, 36, 5 };
  26565. static const unsigned char salt[MAX_TESTS][40] =
  26566. {
  26567. "salt",
  26568. "salt",
  26569. "salt",
  26570. "saltSALTsaltSALTsaltSALTsaltSALTsalt",
  26571. "sa\0lt",
  26572. };
  26573. static const uint32_t it_cnt[MAX_TESTS] =
  26574. { 1, 2, 4096, 4096, 4096 };
  26575. static const uint32_t key_len[MAX_TESTS] =
  26576. { 20, 20, 20, 25, 16 };
  26577. static const unsigned char result_key[MAX_TESTS][32] =
  26578. {
  26579. { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
  26580. 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
  26581. 0x2f, 0xe0, 0x37, 0xa6 },
  26582. { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
  26583. 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
  26584. 0xd8, 0xde, 0x89, 0x57 },
  26585. { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
  26586. 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
  26587. 0x65, 0xa4, 0x29, 0xc1 },
  26588. { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
  26589. 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
  26590. 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
  26591. 0x38 },
  26592. { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
  26593. 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
  26594. };
  26595. int mbedtls_pkcs5_self_test( int verbose )
  26596. {
  26597. mbedtls_md_context_t sha1_ctx;
  26598. const mbedtls_md_info_t *info_sha1;
  26599. int ret, i;
  26600. unsigned char key[64];
  26601. mbedtls_md_init( &sha1_ctx );
  26602. info_sha1 = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
  26603. if( info_sha1 == NULL )
  26604. {
  26605. ret = 1;
  26606. goto exit;
  26607. }
  26608. if( ( ret = mbedtls_md_setup( &sha1_ctx, info_sha1, 1 ) ) != 0 )
  26609. {
  26610. ret = 1;
  26611. goto exit;
  26612. }
  26613. for( i = 0; i < MAX_TESTS; i++ )
  26614. {
  26615. if( verbose != 0 )
  26616. mbedtls_printf( " PBKDF2 (SHA1) #%d: ", i );
  26617. ret = mbedtls_pkcs5_pbkdf2_hmac( &sha1_ctx, password[i], plen[i], salt[i],
  26618. slen[i], it_cnt[i], key_len[i], key );
  26619. if( ret != 0 ||
  26620. memcmp( result_key[i], key, key_len[i] ) != 0 )
  26621. {
  26622. if( verbose != 0 )
  26623. mbedtls_printf( "failed\n" );
  26624. ret = 1;
  26625. goto exit;
  26626. }
  26627. if( verbose != 0 )
  26628. mbedtls_printf( "passed\n" );
  26629. }
  26630. if( verbose != 0 )
  26631. mbedtls_printf( "\n" );
  26632. exit:
  26633. mbedtls_md_free( &sha1_ctx );
  26634. return( ret );
  26635. }
  26636. #endif /* MBEDTLS_SHA1_C */
  26637. #endif /* MBEDTLS_SELF_TEST */
  26638. #endif /* MBEDTLS_PKCS5_C */
  26639. /*
  26640. Amalgamated build undefines
  26641. */
  26642. #undef ADD
  26643. #undef BC
  26644. #undef BEFORE_COLON
  26645. #undef F
  26646. #undef F0
  26647. #undef F1
  26648. #undef F2
  26649. #undef F3
  26650. #undef F4
  26651. #undef F5
  26652. #undef FSb
  26653. #undef K
  26654. #undef KK
  26655. #undef P
  26656. #undef R
  26657. #undef ROTR
  26658. #undef S
  26659. #undef S0
  26660. #undef S1
  26661. #undef S2
  26662. #undef S3
  26663. #undef SAFE_SNPRINTF
  26664. #undef SHR
  26665. #undef close
  26666. #undef read
  26667. #undef supported_init
  26668. #undef write
  26669. /********* Start of file library/pkparse.c ************/
  26670. /*
  26671. * Public Key layer for parsing key files and structures
  26672. *
  26673. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  26674. * SPDX-License-Identifier: Apache-2.0
  26675. *
  26676. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  26677. * not use this file except in compliance with the License.
  26678. * You may obtain a copy of the License at
  26679. *
  26680. * http://www.apache.org/licenses/LICENSE-2.0
  26681. *
  26682. * Unless required by applicable law or agreed to in writing, software
  26683. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  26684. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26685. * See the License for the specific language governing permissions and
  26686. * limitations under the License.
  26687. *
  26688. * This file is part of mbed TLS (https://tls.mbed.org)
  26689. */
  26690. #if !defined(MBEDTLS_CONFIG_FILE)
  26691. #else
  26692. #endif
  26693. #if defined(MBEDTLS_PK_PARSE_C)
  26694. #include <string.h>
  26695. #if defined(MBEDTLS_RSA_C)
  26696. #endif
  26697. #if defined(MBEDTLS_ECP_C)
  26698. #endif
  26699. #if defined(MBEDTLS_ECDSA_C)
  26700. #endif
  26701. #if defined(MBEDTLS_PEM_PARSE_C)
  26702. #endif
  26703. #if defined(MBEDTLS_PKCS5_C)
  26704. #endif
  26705. #if defined(MBEDTLS_PKCS12_C)
  26706. #endif
  26707. #if defined(MBEDTLS_PLATFORM_C)
  26708. #else
  26709. #include <stdlib.h>
  26710. #define mbedtls_calloc calloc
  26711. #define mbedtls_free free
  26712. #endif
  26713. #if defined(MBEDTLS_FS_IO) || \
  26714. defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
  26715. /* Implementation that should never be optimized out by the compiler */
  26716. /* zeroize was here */
  26717. #endif
  26718. #if defined(MBEDTLS_FS_IO)
  26719. /*
  26720. * Load all data from a file into a given buffer.
  26721. *
  26722. * The file is expected to contain either PEM or DER encoded data.
  26723. * A terminating null byte is always appended. It is included in the announced
  26724. * length only if the data looks like it is PEM encoded.
  26725. */
  26726. int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
  26727. {
  26728. FILE *f;
  26729. long size;
  26730. if( ( f = fopen( path, "rb" ) ) == NULL )
  26731. return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
  26732. fseek( f, 0, SEEK_END );
  26733. if( ( size = ftell( f ) ) == -1 )
  26734. {
  26735. fclose( f );
  26736. return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
  26737. }
  26738. fseek( f, 0, SEEK_SET );
  26739. *n = (size_t) size;
  26740. if( *n + 1 == 0 ||
  26741. ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
  26742. {
  26743. fclose( f );
  26744. return( MBEDTLS_ERR_PK_ALLOC_FAILED );
  26745. }
  26746. if( fread( *buf, 1, *n, f ) != *n )
  26747. {
  26748. fclose( f );
  26749. mbedtls_zeroize( *buf, *n );
  26750. mbedtls_free( *buf );
  26751. return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
  26752. }
  26753. fclose( f );
  26754. (*buf)[*n] = '\0';
  26755. if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
  26756. ++*n;
  26757. return( 0 );
  26758. }
  26759. /*
  26760. * Load and parse a private key
  26761. */
  26762. int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
  26763. const char *path, const char *pwd )
  26764. {
  26765. int ret;
  26766. size_t n;
  26767. unsigned char *buf;
  26768. if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
  26769. return( ret );
  26770. if( pwd == NULL )
  26771. ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
  26772. else
  26773. ret = mbedtls_pk_parse_key( ctx, buf, n,
  26774. (const unsigned char *) pwd, strlen( pwd ) );
  26775. mbedtls_zeroize( buf, n );
  26776. mbedtls_free( buf );
  26777. return( ret );
  26778. }
  26779. /*
  26780. * Load and parse a public key
  26781. */
  26782. int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
  26783. {
  26784. int ret;
  26785. size_t n;
  26786. unsigned char *buf;
  26787. if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
  26788. return( ret );
  26789. ret = mbedtls_pk_parse_public_key( ctx, buf, n );
  26790. mbedtls_zeroize( buf, n );
  26791. mbedtls_free( buf );
  26792. return( ret );
  26793. }
  26794. #endif /* MBEDTLS_FS_IO */
  26795. #if defined(MBEDTLS_ECP_C)
  26796. /* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
  26797. *
  26798. * ECParameters ::= CHOICE {
  26799. * namedCurve OBJECT IDENTIFIER
  26800. * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
  26801. * -- implicitCurve NULL
  26802. * }
  26803. */
  26804. static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
  26805. mbedtls_asn1_buf *params )
  26806. {
  26807. int ret;
  26808. if ( end - *p < 1 )
  26809. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
  26810. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  26811. /* Tag may be either OID or SEQUENCE */
  26812. params->tag = **p;
  26813. if( params->tag != MBEDTLS_ASN1_OID
  26814. #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
  26815. && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
  26816. #endif
  26817. )
  26818. {
  26819. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
  26820. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  26821. }
  26822. if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
  26823. {
  26824. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  26825. }
  26826. params->p = *p;
  26827. *p += params->len;
  26828. if( *p != end )
  26829. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
  26830. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  26831. return( 0 );
  26832. }
  26833. #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
  26834. /*
  26835. * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
  26836. * WARNING: the resulting group should only be used with
  26837. * pk_group_id_from_specified(), since its base point may not be set correctly
  26838. * if it was encoded compressed.
  26839. *
  26840. * SpecifiedECDomain ::= SEQUENCE {
  26841. * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
  26842. * fieldID FieldID {{FieldTypes}},
  26843. * curve Curve,
  26844. * base ECPoint,
  26845. * order INTEGER,
  26846. * cofactor INTEGER OPTIONAL,
  26847. * hash HashAlgorithm OPTIONAL,
  26848. * ...
  26849. * }
  26850. *
  26851. * We only support prime-field as field type, and ignore hash and cofactor.
  26852. */
  26853. static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
  26854. {
  26855. int ret;
  26856. unsigned char *p = params->p;
  26857. const unsigned char * const end = params->p + params->len;
  26858. const unsigned char *end_field, *end_curve;
  26859. size_t len;
  26860. int ver;
  26861. /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
  26862. if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
  26863. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  26864. if( ver < 1 || ver > 3 )
  26865. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
  26866. /*
  26867. * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
  26868. * fieldType FIELD-ID.&id({IOSet}),
  26869. * parameters FIELD-ID.&Type({IOSet}{@fieldType})
  26870. * }
  26871. */
  26872. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  26873. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  26874. return( ret );
  26875. end_field = p + len;
  26876. /*
  26877. * FIELD-ID ::= TYPE-IDENTIFIER
  26878. * FieldTypes FIELD-ID ::= {
  26879. * { Prime-p IDENTIFIED BY prime-field } |
  26880. * { Characteristic-two IDENTIFIED BY characteristic-two-field }
  26881. * }
  26882. * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
  26883. */
  26884. if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
  26885. return( ret );
  26886. if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
  26887. memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
  26888. {
  26889. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  26890. }
  26891. p += len;
  26892. /* Prime-p ::= INTEGER -- Field of size p. */
  26893. if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
  26894. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  26895. grp->pbits = mbedtls_mpi_bitlen( &grp->P );
  26896. if( p != end_field )
  26897. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
  26898. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  26899. /*
  26900. * Curve ::= SEQUENCE {
  26901. * a FieldElement,
  26902. * b FieldElement,
  26903. * seed BIT STRING OPTIONAL
  26904. * -- Shall be present if used in SpecifiedECDomain
  26905. * -- with version equal to ecdpVer2 or ecdpVer3
  26906. * }
  26907. */
  26908. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  26909. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  26910. return( ret );
  26911. end_curve = p + len;
  26912. /*
  26913. * FieldElement ::= OCTET STRING
  26914. * containing an integer in the case of a prime field
  26915. */
  26916. if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
  26917. ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
  26918. {
  26919. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  26920. }
  26921. p += len;
  26922. if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
  26923. ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
  26924. {
  26925. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  26926. }
  26927. p += len;
  26928. /* Ignore seed BIT STRING OPTIONAL */
  26929. if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
  26930. p += len;
  26931. if( p != end_curve )
  26932. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
  26933. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  26934. /*
  26935. * ECPoint ::= OCTET STRING
  26936. */
  26937. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  26938. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  26939. if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
  26940. ( const unsigned char *) p, len ) ) != 0 )
  26941. {
  26942. /*
  26943. * If we can't read the point because it's compressed, cheat by
  26944. * reading only the X coordinate and the parity bit of Y.
  26945. */
  26946. if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
  26947. ( p[0] != 0x02 && p[0] != 0x03 ) ||
  26948. len != mbedtls_mpi_size( &grp->P ) + 1 ||
  26949. mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
  26950. mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
  26951. mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
  26952. {
  26953. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
  26954. }
  26955. }
  26956. p += len;
  26957. /*
  26958. * order INTEGER
  26959. */
  26960. if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
  26961. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  26962. grp->nbits = mbedtls_mpi_bitlen( &grp->N );
  26963. /*
  26964. * Allow optional elements by purposefully not enforcing p == end here.
  26965. */
  26966. return( 0 );
  26967. }
  26968. /*
  26969. * Find the group id associated with an (almost filled) group as generated by
  26970. * pk_group_from_specified(), or return an error if unknown.
  26971. */
  26972. static int pk_group_id_from_group( const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id )
  26973. {
  26974. int ret = 0;
  26975. mbedtls_ecp_group ref;
  26976. const mbedtls_ecp_group_id *id;
  26977. mbedtls_ecp_group_init( &ref );
  26978. for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
  26979. {
  26980. /* Load the group associated to that id */
  26981. mbedtls_ecp_group_free( &ref );
  26982. MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
  26983. /* Compare to the group we were given, starting with easy tests */
  26984. if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
  26985. mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
  26986. mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
  26987. mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
  26988. mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
  26989. mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
  26990. mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
  26991. /* For Y we may only know the parity bit, so compare only that */
  26992. mbedtls_mpi_get_bit( &grp->G.Y, 0 ) == mbedtls_mpi_get_bit( &ref.G.Y, 0 ) )
  26993. {
  26994. break;
  26995. }
  26996. }
  26997. cleanup:
  26998. mbedtls_ecp_group_free( &ref );
  26999. *grp_id = *id;
  27000. if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
  27001. ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
  27002. return( ret );
  27003. }
  27004. /*
  27005. * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
  27006. */
  27007. static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
  27008. mbedtls_ecp_group_id *grp_id )
  27009. {
  27010. int ret;
  27011. mbedtls_ecp_group grp;
  27012. mbedtls_ecp_group_init( &grp );
  27013. if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
  27014. goto cleanup;
  27015. ret = pk_group_id_from_group( &grp, grp_id );
  27016. cleanup:
  27017. mbedtls_ecp_group_free( &grp );
  27018. return( ret );
  27019. }
  27020. #endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
  27021. /*
  27022. * Use EC parameters to initialise an EC group
  27023. *
  27024. * ECParameters ::= CHOICE {
  27025. * namedCurve OBJECT IDENTIFIER
  27026. * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
  27027. * -- implicitCurve NULL
  27028. */
  27029. static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
  27030. {
  27031. int ret;
  27032. mbedtls_ecp_group_id grp_id;
  27033. if( params->tag == MBEDTLS_ASN1_OID )
  27034. {
  27035. if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
  27036. return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
  27037. }
  27038. else
  27039. {
  27040. #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
  27041. if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
  27042. return( ret );
  27043. #else
  27044. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
  27045. #endif
  27046. }
  27047. /*
  27048. * grp may already be initilialized; if so, make sure IDs match
  27049. */
  27050. if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
  27051. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
  27052. if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
  27053. return( ret );
  27054. return( 0 );
  27055. }
  27056. /*
  27057. * EC public key is an EC point
  27058. *
  27059. * The caller is responsible for clearing the structure upon failure if
  27060. * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
  27061. * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
  27062. */
  27063. static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
  27064. mbedtls_ecp_keypair *key )
  27065. {
  27066. int ret;
  27067. if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
  27068. (const unsigned char *) *p, end - *p ) ) == 0 )
  27069. {
  27070. ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
  27071. }
  27072. /*
  27073. * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
  27074. */
  27075. *p = (unsigned char *) end;
  27076. return( ret );
  27077. }
  27078. #endif /* MBEDTLS_ECP_C */
  27079. #if defined(MBEDTLS_RSA_C)
  27080. /*
  27081. * RSAPublicKey ::= SEQUENCE {
  27082. * modulus INTEGER, -- n
  27083. * publicExponent INTEGER -- e
  27084. * }
  27085. */
  27086. static int pk_get_rsapubkey( unsigned char **p,
  27087. const unsigned char *end,
  27088. mbedtls_rsa_context *rsa )
  27089. {
  27090. int ret;
  27091. size_t len;
  27092. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  27093. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  27094. return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
  27095. if( *p + len != end )
  27096. return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
  27097. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  27098. /* Import N */
  27099. if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
  27100. return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
  27101. if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
  27102. NULL, 0, NULL, 0 ) ) != 0 )
  27103. return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
  27104. *p += len;
  27105. /* Import E */
  27106. if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
  27107. return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
  27108. if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
  27109. NULL, 0, *p, len ) ) != 0 )
  27110. return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
  27111. *p += len;
  27112. if( mbedtls_rsa_complete( rsa ) != 0 ||
  27113. mbedtls_rsa_check_pubkey( rsa ) != 0 )
  27114. {
  27115. return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
  27116. }
  27117. if( *p != end )
  27118. return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
  27119. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  27120. return( 0 );
  27121. }
  27122. #endif /* MBEDTLS_RSA_C */
  27123. /* Get a PK algorithm identifier
  27124. *
  27125. * AlgorithmIdentifier ::= SEQUENCE {
  27126. * algorithm OBJECT IDENTIFIER,
  27127. * parameters ANY DEFINED BY algorithm OPTIONAL }
  27128. */
  27129. static int pk_get_pk_alg( unsigned char **p,
  27130. const unsigned char *end,
  27131. mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
  27132. {
  27133. int ret;
  27134. mbedtls_asn1_buf alg_oid;
  27135. memset( params, 0, sizeof(mbedtls_asn1_buf) );
  27136. if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
  27137. return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
  27138. if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
  27139. return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
  27140. /*
  27141. * No parameters with RSA (only for EC)
  27142. */
  27143. if( *pk_alg == MBEDTLS_PK_RSA &&
  27144. ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
  27145. params->len != 0 ) )
  27146. {
  27147. return( MBEDTLS_ERR_PK_INVALID_ALG );
  27148. }
  27149. return( 0 );
  27150. }
  27151. /*
  27152. * SubjectPublicKeyInfo ::= SEQUENCE {
  27153. * algorithm AlgorithmIdentifier,
  27154. * subjectPublicKey BIT STRING }
  27155. */
  27156. int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
  27157. mbedtls_pk_context *pk )
  27158. {
  27159. int ret;
  27160. size_t len;
  27161. mbedtls_asn1_buf alg_params;
  27162. mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
  27163. const mbedtls_pk_info_t *pk_info;
  27164. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  27165. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  27166. {
  27167. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  27168. }
  27169. end = *p + len;
  27170. if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
  27171. return( ret );
  27172. if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
  27173. return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
  27174. if( *p + len != end )
  27175. return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
  27176. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  27177. if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
  27178. return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
  27179. if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
  27180. return( ret );
  27181. #if defined(MBEDTLS_RSA_C)
  27182. if( pk_alg == MBEDTLS_PK_RSA )
  27183. {
  27184. ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
  27185. } else
  27186. #endif /* MBEDTLS_RSA_C */
  27187. #if defined(MBEDTLS_ECP_C)
  27188. if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
  27189. {
  27190. ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
  27191. if( ret == 0 )
  27192. ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
  27193. } else
  27194. #endif /* MBEDTLS_ECP_C */
  27195. ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
  27196. if( ret == 0 && *p != end )
  27197. ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
  27198. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
  27199. if( ret != 0 )
  27200. mbedtls_pk_free( pk );
  27201. return( ret );
  27202. }
  27203. #if defined(MBEDTLS_RSA_C)
  27204. /*
  27205. * Parse a PKCS#1 encoded private RSA key
  27206. */
  27207. static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
  27208. const unsigned char *key,
  27209. size_t keylen )
  27210. {
  27211. int ret, version;
  27212. size_t len;
  27213. unsigned char *p, *end;
  27214. mbedtls_mpi T;
  27215. mbedtls_mpi_init( &T );
  27216. p = (unsigned char *) key;
  27217. end = p + keylen;
  27218. /*
  27219. * This function parses the RSAPrivateKey (PKCS#1)
  27220. *
  27221. * RSAPrivateKey ::= SEQUENCE {
  27222. * version Version,
  27223. * modulus INTEGER, -- n
  27224. * publicExponent INTEGER, -- e
  27225. * privateExponent INTEGER, -- d
  27226. * prime1 INTEGER, -- p
  27227. * prime2 INTEGER, -- q
  27228. * exponent1 INTEGER, -- d mod (p-1)
  27229. * exponent2 INTEGER, -- d mod (q-1)
  27230. * coefficient INTEGER, -- (inverse of q) mod p
  27231. * otherPrimeInfos OtherPrimeInfos OPTIONAL
  27232. * }
  27233. */
  27234. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  27235. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  27236. {
  27237. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  27238. }
  27239. end = p + len;
  27240. if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
  27241. {
  27242. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  27243. }
  27244. if( version != 0 )
  27245. {
  27246. return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
  27247. }
  27248. /* Import N */
  27249. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  27250. MBEDTLS_ASN1_INTEGER ) ) != 0 ||
  27251. ( ret = mbedtls_rsa_import_raw( rsa, p, len, NULL, 0, NULL, 0,
  27252. NULL, 0, NULL, 0 ) ) != 0 )
  27253. goto cleanup;
  27254. p += len;
  27255. /* Import E */
  27256. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  27257. MBEDTLS_ASN1_INTEGER ) ) != 0 ||
  27258. ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
  27259. NULL, 0, p, len ) ) != 0 )
  27260. goto cleanup;
  27261. p += len;
  27262. /* Import D */
  27263. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  27264. MBEDTLS_ASN1_INTEGER ) ) != 0 ||
  27265. ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
  27266. p, len, NULL, 0 ) ) != 0 )
  27267. goto cleanup;
  27268. p += len;
  27269. /* Import P */
  27270. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  27271. MBEDTLS_ASN1_INTEGER ) ) != 0 ||
  27272. ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, p, len, NULL, 0,
  27273. NULL, 0, NULL, 0 ) ) != 0 )
  27274. goto cleanup;
  27275. p += len;
  27276. /* Import Q */
  27277. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  27278. MBEDTLS_ASN1_INTEGER ) ) != 0 ||
  27279. ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, p, len,
  27280. NULL, 0, NULL, 0 ) ) != 0 )
  27281. goto cleanup;
  27282. p += len;
  27283. /* Complete the RSA private key */
  27284. if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 )
  27285. goto cleanup;
  27286. /* Check optional parameters */
  27287. if( ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
  27288. ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
  27289. ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 )
  27290. goto cleanup;
  27291. if( p != end )
  27292. {
  27293. ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
  27294. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
  27295. }
  27296. cleanup:
  27297. mbedtls_mpi_free( &T );
  27298. if( ret != 0 )
  27299. {
  27300. /* Wrap error code if it's coming from a lower level */
  27301. if( ( ret & 0xff80 ) == 0 )
  27302. ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
  27303. else
  27304. ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
  27305. mbedtls_rsa_free( rsa );
  27306. }
  27307. return( ret );
  27308. }
  27309. #endif /* MBEDTLS_RSA_C */
  27310. #if defined(MBEDTLS_ECP_C)
  27311. /*
  27312. * Parse a SEC1 encoded private EC key
  27313. */
  27314. static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
  27315. const unsigned char *key,
  27316. size_t keylen )
  27317. {
  27318. int ret;
  27319. int version, pubkey_done;
  27320. size_t len;
  27321. mbedtls_asn1_buf params;
  27322. unsigned char *p = (unsigned char *) key;
  27323. unsigned char *end = p + keylen;
  27324. unsigned char *end2;
  27325. /*
  27326. * RFC 5915, or SEC1 Appendix C.4
  27327. *
  27328. * ECPrivateKey ::= SEQUENCE {
  27329. * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
  27330. * privateKey OCTET STRING,
  27331. * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
  27332. * publicKey [1] BIT STRING OPTIONAL
  27333. * }
  27334. */
  27335. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  27336. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  27337. {
  27338. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  27339. }
  27340. end = p + len;
  27341. if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
  27342. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  27343. if( version != 1 )
  27344. return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
  27345. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  27346. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  27347. if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
  27348. {
  27349. mbedtls_ecp_keypair_free( eck );
  27350. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  27351. }
  27352. p += len;
  27353. pubkey_done = 0;
  27354. if( p != end )
  27355. {
  27356. /*
  27357. * Is 'parameters' present?
  27358. */
  27359. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  27360. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
  27361. {
  27362. if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
  27363. ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
  27364. {
  27365. mbedtls_ecp_keypair_free( eck );
  27366. return( ret );
  27367. }
  27368. }
  27369. else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  27370. {
  27371. mbedtls_ecp_keypair_free( eck );
  27372. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  27373. }
  27374. /*
  27375. * Is 'publickey' present? If not, or if we can't read it (eg because it
  27376. * is compressed), create it from the private key.
  27377. */
  27378. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  27379. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
  27380. {
  27381. end2 = p + len;
  27382. if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
  27383. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  27384. if( p + len != end2 )
  27385. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
  27386. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  27387. if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
  27388. pubkey_done = 1;
  27389. else
  27390. {
  27391. /*
  27392. * The only acceptable failure mode of pk_get_ecpubkey() above
  27393. * is if the point format is not recognized.
  27394. */
  27395. if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
  27396. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
  27397. }
  27398. }
  27399. else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  27400. {
  27401. mbedtls_ecp_keypair_free( eck );
  27402. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  27403. }
  27404. }
  27405. if( ! pubkey_done &&
  27406. ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
  27407. NULL, NULL ) ) != 0 )
  27408. {
  27409. mbedtls_ecp_keypair_free( eck );
  27410. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  27411. }
  27412. if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
  27413. {
  27414. mbedtls_ecp_keypair_free( eck );
  27415. return( ret );
  27416. }
  27417. return( 0 );
  27418. }
  27419. #endif /* MBEDTLS_ECP_C */
  27420. /*
  27421. * Parse an unencrypted PKCS#8 encoded private key
  27422. *
  27423. * Notes:
  27424. *
  27425. * - This function does not own the key buffer. It is the
  27426. * responsibility of the caller to take care of zeroizing
  27427. * and freeing it after use.
  27428. *
  27429. * - The function is responsible for freeing the provided
  27430. * PK context on failure.
  27431. *
  27432. */
  27433. static int pk_parse_key_pkcs8_unencrypted_der(
  27434. mbedtls_pk_context *pk,
  27435. const unsigned char* key,
  27436. size_t keylen )
  27437. {
  27438. int ret, version;
  27439. size_t len;
  27440. mbedtls_asn1_buf params;
  27441. unsigned char *p = (unsigned char *) key;
  27442. unsigned char *end = p + keylen;
  27443. mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
  27444. const mbedtls_pk_info_t *pk_info;
  27445. /*
  27446. * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
  27447. *
  27448. * PrivateKeyInfo ::= SEQUENCE {
  27449. * version Version,
  27450. * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
  27451. * privateKey PrivateKey,
  27452. * attributes [0] IMPLICIT Attributes OPTIONAL }
  27453. *
  27454. * Version ::= INTEGER
  27455. * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
  27456. * PrivateKey ::= OCTET STRING
  27457. *
  27458. * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
  27459. */
  27460. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  27461. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  27462. {
  27463. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  27464. }
  27465. end = p + len;
  27466. if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
  27467. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  27468. if( version != 0 )
  27469. return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
  27470. if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
  27471. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  27472. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  27473. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  27474. if( len < 1 )
  27475. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
  27476. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  27477. if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
  27478. return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
  27479. if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
  27480. return( ret );
  27481. #if defined(MBEDTLS_RSA_C)
  27482. if( pk_alg == MBEDTLS_PK_RSA )
  27483. {
  27484. if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
  27485. {
  27486. mbedtls_pk_free( pk );
  27487. return( ret );
  27488. }
  27489. } else
  27490. #endif /* MBEDTLS_RSA_C */
  27491. #if defined(MBEDTLS_ECP_C)
  27492. if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
  27493. {
  27494. if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
  27495. ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
  27496. {
  27497. mbedtls_pk_free( pk );
  27498. return( ret );
  27499. }
  27500. } else
  27501. #endif /* MBEDTLS_ECP_C */
  27502. return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
  27503. return( 0 );
  27504. }
  27505. /*
  27506. * Parse an encrypted PKCS#8 encoded private key
  27507. *
  27508. * To save space, the decryption happens in-place on the given key buffer.
  27509. * Also, while this function may modify the keybuffer, it doesn't own it,
  27510. * and instead it is the responsibility of the caller to zeroize and properly
  27511. * free it after use.
  27512. *
  27513. */
  27514. #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
  27515. static int pk_parse_key_pkcs8_encrypted_der(
  27516. mbedtls_pk_context *pk,
  27517. unsigned char *key, size_t keylen,
  27518. const unsigned char *pwd, size_t pwdlen )
  27519. {
  27520. int ret, decrypted = 0;
  27521. size_t len;
  27522. unsigned char *buf;
  27523. unsigned char *p, *end;
  27524. mbedtls_asn1_buf pbe_alg_oid, pbe_params;
  27525. #if defined(MBEDTLS_PKCS12_C)
  27526. mbedtls_cipher_type_t cipher_alg;
  27527. mbedtls_md_type_t md_alg;
  27528. #endif
  27529. p = key;
  27530. end = p + keylen;
  27531. if( pwdlen == 0 )
  27532. return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
  27533. /*
  27534. * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
  27535. *
  27536. * EncryptedPrivateKeyInfo ::= SEQUENCE {
  27537. * encryptionAlgorithm EncryptionAlgorithmIdentifier,
  27538. * encryptedData EncryptedData
  27539. * }
  27540. *
  27541. * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
  27542. *
  27543. * EncryptedData ::= OCTET STRING
  27544. *
  27545. * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
  27546. *
  27547. */
  27548. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  27549. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  27550. {
  27551. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  27552. }
  27553. end = p + len;
  27554. if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
  27555. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  27556. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  27557. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
  27558. buf = p;
  27559. /*
  27560. * Decrypt EncryptedData with appropriate PBE
  27561. */
  27562. #if defined(MBEDTLS_PKCS12_C)
  27563. if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
  27564. {
  27565. if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
  27566. cipher_alg, md_alg,
  27567. pwd, pwdlen, p, len, buf ) ) != 0 )
  27568. {
  27569. if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
  27570. return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
  27571. return( ret );
  27572. }
  27573. decrypted = 1;
  27574. }
  27575. else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
  27576. {
  27577. if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
  27578. MBEDTLS_PKCS12_PBE_DECRYPT,
  27579. pwd, pwdlen,
  27580. p, len, buf ) ) != 0 )
  27581. {
  27582. return( ret );
  27583. }
  27584. // Best guess for password mismatch when using RC4. If first tag is
  27585. // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
  27586. //
  27587. if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
  27588. return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
  27589. decrypted = 1;
  27590. }
  27591. else
  27592. #endif /* MBEDTLS_PKCS12_C */
  27593. #if defined(MBEDTLS_PKCS5_C)
  27594. if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
  27595. {
  27596. if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
  27597. p, len, buf ) ) != 0 )
  27598. {
  27599. if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
  27600. return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
  27601. return( ret );
  27602. }
  27603. decrypted = 1;
  27604. }
  27605. else
  27606. #endif /* MBEDTLS_PKCS5_C */
  27607. {
  27608. ((void) pwd);
  27609. }
  27610. if( decrypted == 0 )
  27611. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  27612. return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
  27613. }
  27614. #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
  27615. /*
  27616. * Parse a private key
  27617. */
  27618. int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
  27619. const unsigned char *key, size_t keylen,
  27620. const unsigned char *pwd, size_t pwdlen )
  27621. {
  27622. int ret;
  27623. const mbedtls_pk_info_t *pk_info;
  27624. #if defined(MBEDTLS_PEM_PARSE_C)
  27625. size_t len;
  27626. mbedtls_pem_context pem;
  27627. mbedtls_pem_init( &pem );
  27628. #if defined(MBEDTLS_RSA_C)
  27629. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  27630. if( keylen == 0 || key[keylen - 1] != '\0' )
  27631. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  27632. else
  27633. ret = mbedtls_pem_read_buffer( &pem,
  27634. "-----BEGIN RSA PRIVATE KEY-----",
  27635. "-----END RSA PRIVATE KEY-----",
  27636. key, pwd, pwdlen, &len );
  27637. if( ret == 0 )
  27638. {
  27639. pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
  27640. if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
  27641. ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
  27642. pem.buf, pem.buflen ) ) != 0 )
  27643. {
  27644. mbedtls_pk_free( pk );
  27645. }
  27646. mbedtls_pem_free( &pem );
  27647. return( ret );
  27648. }
  27649. else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
  27650. return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
  27651. else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
  27652. return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
  27653. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  27654. return( ret );
  27655. #endif /* MBEDTLS_RSA_C */
  27656. #if defined(MBEDTLS_ECP_C)
  27657. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  27658. if( keylen == 0 || key[keylen - 1] != '\0' )
  27659. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  27660. else
  27661. ret = mbedtls_pem_read_buffer( &pem,
  27662. "-----BEGIN EC PRIVATE KEY-----",
  27663. "-----END EC PRIVATE KEY-----",
  27664. key, pwd, pwdlen, &len );
  27665. if( ret == 0 )
  27666. {
  27667. pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
  27668. if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
  27669. ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
  27670. pem.buf, pem.buflen ) ) != 0 )
  27671. {
  27672. mbedtls_pk_free( pk );
  27673. }
  27674. mbedtls_pem_free( &pem );
  27675. return( ret );
  27676. }
  27677. else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
  27678. return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
  27679. else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
  27680. return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
  27681. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  27682. return( ret );
  27683. #endif /* MBEDTLS_ECP_C */
  27684. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  27685. if( keylen == 0 || key[keylen - 1] != '\0' )
  27686. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  27687. else
  27688. ret = mbedtls_pem_read_buffer( &pem,
  27689. "-----BEGIN PRIVATE KEY-----",
  27690. "-----END PRIVATE KEY-----",
  27691. key, NULL, 0, &len );
  27692. if( ret == 0 )
  27693. {
  27694. if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
  27695. pem.buf, pem.buflen ) ) != 0 )
  27696. {
  27697. mbedtls_pk_free( pk );
  27698. }
  27699. mbedtls_pem_free( &pem );
  27700. return( ret );
  27701. }
  27702. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  27703. return( ret );
  27704. #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
  27705. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  27706. if( keylen == 0 || key[keylen - 1] != '\0' )
  27707. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  27708. else
  27709. ret = mbedtls_pem_read_buffer( &pem,
  27710. "-----BEGIN ENCRYPTED PRIVATE KEY-----",
  27711. "-----END ENCRYPTED PRIVATE KEY-----",
  27712. key, NULL, 0, &len );
  27713. if( ret == 0 )
  27714. {
  27715. if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
  27716. pem.buf, pem.buflen,
  27717. pwd, pwdlen ) ) != 0 )
  27718. {
  27719. mbedtls_pk_free( pk );
  27720. }
  27721. mbedtls_pem_free( &pem );
  27722. return( ret );
  27723. }
  27724. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  27725. return( ret );
  27726. #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
  27727. #else
  27728. ((void) ret);
  27729. ((void) pwd);
  27730. ((void) pwdlen);
  27731. #endif /* MBEDTLS_PEM_PARSE_C */
  27732. /*
  27733. * At this point we only know it's not a PEM formatted key. Could be any
  27734. * of the known DER encoded private key formats
  27735. *
  27736. * We try the different DER format parsers to see if one passes without
  27737. * error
  27738. */
  27739. #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
  27740. {
  27741. unsigned char *key_copy;
  27742. if( keylen == 0 )
  27743. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
  27744. if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
  27745. return( MBEDTLS_ERR_PK_ALLOC_FAILED );
  27746. memcpy( key_copy, key, keylen );
  27747. ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
  27748. pwd, pwdlen );
  27749. mbedtls_zeroize( key_copy, keylen );
  27750. mbedtls_free( key_copy );
  27751. }
  27752. if( ret == 0 )
  27753. return( 0 );
  27754. mbedtls_pk_free( pk );
  27755. if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
  27756. {
  27757. return( ret );
  27758. }
  27759. #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
  27760. if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
  27761. return( 0 );
  27762. mbedtls_pk_free( pk );
  27763. #if defined(MBEDTLS_RSA_C)
  27764. pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
  27765. if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
  27766. ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
  27767. key, keylen ) ) != 0 )
  27768. {
  27769. mbedtls_pk_free( pk );
  27770. }
  27771. else
  27772. {
  27773. return( 0 );
  27774. }
  27775. #endif /* MBEDTLS_RSA_C */
  27776. #if defined(MBEDTLS_ECP_C)
  27777. pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
  27778. if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
  27779. ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
  27780. key, keylen ) ) != 0 )
  27781. {
  27782. mbedtls_pk_free( pk );
  27783. }
  27784. else
  27785. {
  27786. return( 0 );
  27787. }
  27788. #endif /* MBEDTLS_ECP_C */
  27789. return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
  27790. }
  27791. /*
  27792. * Parse a public key
  27793. */
  27794. int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
  27795. const unsigned char *key, size_t keylen )
  27796. {
  27797. int ret;
  27798. unsigned char *p;
  27799. #if defined(MBEDTLS_RSA_C)
  27800. const mbedtls_pk_info_t *pk_info;
  27801. #endif
  27802. #if defined(MBEDTLS_PEM_PARSE_C)
  27803. size_t len;
  27804. mbedtls_pem_context pem;
  27805. mbedtls_pem_init( &pem );
  27806. #if defined(MBEDTLS_RSA_C)
  27807. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  27808. if( keylen == 0 || key[keylen - 1] != '\0' )
  27809. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  27810. else
  27811. ret = mbedtls_pem_read_buffer( &pem,
  27812. "-----BEGIN RSA PUBLIC KEY-----",
  27813. "-----END RSA PUBLIC KEY-----",
  27814. key, NULL, 0, &len );
  27815. if( ret == 0 )
  27816. {
  27817. p = pem.buf;
  27818. if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
  27819. return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
  27820. if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
  27821. return( ret );
  27822. if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
  27823. mbedtls_pk_free( ctx );
  27824. mbedtls_pem_free( &pem );
  27825. return( ret );
  27826. }
  27827. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  27828. {
  27829. mbedtls_pem_free( &pem );
  27830. return( ret );
  27831. }
  27832. #endif /* MBEDTLS_RSA_C */
  27833. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  27834. if( keylen == 0 || key[keylen - 1] != '\0' )
  27835. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  27836. else
  27837. ret = mbedtls_pem_read_buffer( &pem,
  27838. "-----BEGIN PUBLIC KEY-----",
  27839. "-----END PUBLIC KEY-----",
  27840. key, NULL, 0, &len );
  27841. if( ret == 0 )
  27842. {
  27843. /*
  27844. * Was PEM encoded
  27845. */
  27846. p = pem.buf;
  27847. ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
  27848. mbedtls_pem_free( &pem );
  27849. return( ret );
  27850. }
  27851. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  27852. {
  27853. mbedtls_pem_free( &pem );
  27854. return( ret );
  27855. }
  27856. mbedtls_pem_free( &pem );
  27857. #endif /* MBEDTLS_PEM_PARSE_C */
  27858. #if defined(MBEDTLS_RSA_C)
  27859. if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
  27860. return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
  27861. if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
  27862. return( ret );
  27863. p = (unsigned char *)key;
  27864. ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
  27865. if( ret == 0 )
  27866. {
  27867. return( ret );
  27868. }
  27869. mbedtls_pk_free( ctx );
  27870. if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
  27871. {
  27872. return( ret );
  27873. }
  27874. #endif /* MBEDTLS_RSA_C */
  27875. p = (unsigned char *) key;
  27876. ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
  27877. return( ret );
  27878. }
  27879. #endif /* MBEDTLS_PK_PARSE_C */
  27880. /*
  27881. Amalgamated build undefines
  27882. */
  27883. #undef ADD
  27884. #undef BC
  27885. #undef BEFORE_COLON
  27886. #undef F
  27887. #undef F0
  27888. #undef F1
  27889. #undef F2
  27890. #undef F3
  27891. #undef F4
  27892. #undef F5
  27893. #undef FSb
  27894. #undef K
  27895. #undef KK
  27896. #undef P
  27897. #undef R
  27898. #undef ROTR
  27899. #undef S
  27900. #undef S0
  27901. #undef S1
  27902. #undef S2
  27903. #undef S3
  27904. #undef SAFE_SNPRINTF
  27905. #undef SHR
  27906. #undef close
  27907. #undef read
  27908. #undef supported_init
  27909. #undef write
  27910. /********* Start of file library/pkwrite.c ************/
  27911. /*
  27912. * Public Key layer for writing key files and structures
  27913. *
  27914. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  27915. * SPDX-License-Identifier: Apache-2.0
  27916. *
  27917. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  27918. * not use this file except in compliance with the License.
  27919. * You may obtain a copy of the License at
  27920. *
  27921. * http://www.apache.org/licenses/LICENSE-2.0
  27922. *
  27923. * Unless required by applicable law or agreed to in writing, software
  27924. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  27925. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27926. * See the License for the specific language governing permissions and
  27927. * limitations under the License.
  27928. *
  27929. * This file is part of mbed TLS (https://tls.mbed.org)
  27930. */
  27931. #if !defined(MBEDTLS_CONFIG_FILE)
  27932. #else
  27933. #endif
  27934. #if defined(MBEDTLS_PK_WRITE_C)
  27935. #include <string.h>
  27936. #if defined(MBEDTLS_RSA_C)
  27937. #endif
  27938. #if defined(MBEDTLS_ECP_C)
  27939. #endif
  27940. #if defined(MBEDTLS_ECDSA_C)
  27941. #endif
  27942. #if defined(MBEDTLS_PEM_WRITE_C)
  27943. #endif
  27944. #if defined(MBEDTLS_PLATFORM_C)
  27945. #else
  27946. #include <stdlib.h>
  27947. #define mbedtls_calloc calloc
  27948. #define mbedtls_free free
  27949. #endif
  27950. #if defined(MBEDTLS_RSA_C)
  27951. /*
  27952. * RSAPublicKey ::= SEQUENCE {
  27953. * modulus INTEGER, -- n
  27954. * publicExponent INTEGER -- e
  27955. * }
  27956. */
  27957. static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start,
  27958. mbedtls_rsa_context *rsa )
  27959. {
  27960. int ret;
  27961. size_t len = 0;
  27962. mbedtls_mpi T;
  27963. mbedtls_mpi_init( &T );
  27964. /* Export E */
  27965. if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &T ) ) != 0 ||
  27966. ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
  27967. goto end_of_export;
  27968. len += ret;
  27969. /* Export N */
  27970. if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL, NULL, NULL, NULL ) ) != 0 ||
  27971. ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
  27972. goto end_of_export;
  27973. len += ret;
  27974. end_of_export:
  27975. mbedtls_mpi_free( &T );
  27976. if( ret < 0 )
  27977. return( ret );
  27978. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  27979. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
  27980. MBEDTLS_ASN1_SEQUENCE ) );
  27981. return( (int) len );
  27982. }
  27983. #endif /* MBEDTLS_RSA_C */
  27984. #if defined(MBEDTLS_ECP_C)
  27985. /*
  27986. * EC public key is an EC point
  27987. */
  27988. static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
  27989. mbedtls_ecp_keypair *ec )
  27990. {
  27991. int ret;
  27992. size_t len = 0;
  27993. unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
  27994. if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
  27995. MBEDTLS_ECP_PF_UNCOMPRESSED,
  27996. &len, buf, sizeof( buf ) ) ) != 0 )
  27997. {
  27998. return( ret );
  27999. }
  28000. if( *p < start || (size_t)( *p - start ) < len )
  28001. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  28002. *p -= len;
  28003. memcpy( *p, buf, len );
  28004. return( (int) len );
  28005. }
  28006. /*
  28007. * ECParameters ::= CHOICE {
  28008. * namedCurve OBJECT IDENTIFIER
  28009. * }
  28010. */
  28011. static int pk_write_ec_param( unsigned char **p, unsigned char *start,
  28012. mbedtls_ecp_keypair *ec )
  28013. {
  28014. int ret;
  28015. size_t len = 0;
  28016. const char *oid;
  28017. size_t oid_len;
  28018. if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
  28019. return( ret );
  28020. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
  28021. return( (int) len );
  28022. }
  28023. #endif /* MBEDTLS_ECP_C */
  28024. int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
  28025. const mbedtls_pk_context *key )
  28026. {
  28027. int ret;
  28028. size_t len = 0;
  28029. #if defined(MBEDTLS_RSA_C)
  28030. if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
  28031. MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
  28032. else
  28033. #endif
  28034. #if defined(MBEDTLS_ECP_C)
  28035. if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
  28036. MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedtls_pk_ec( *key ) ) );
  28037. else
  28038. #endif
  28039. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  28040. return( (int) len );
  28041. }
  28042. int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
  28043. {
  28044. int ret;
  28045. unsigned char *c;
  28046. size_t len = 0, par_len = 0, oid_len;
  28047. const char *oid;
  28048. c = buf + size;
  28049. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
  28050. if( c - buf < 1 )
  28051. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  28052. /*
  28053. * SubjectPublicKeyInfo ::= SEQUENCE {
  28054. * algorithm AlgorithmIdentifier,
  28055. * subjectPublicKey BIT STRING }
  28056. */
  28057. *--c = 0;
  28058. len += 1;
  28059. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  28060. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
  28061. if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
  28062. &oid, &oid_len ) ) != 0 )
  28063. {
  28064. return( ret );
  28065. }
  28066. #if defined(MBEDTLS_ECP_C)
  28067. if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
  28068. {
  28069. MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
  28070. }
  28071. #endif
  28072. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
  28073. par_len ) );
  28074. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  28075. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
  28076. MBEDTLS_ASN1_SEQUENCE ) );
  28077. return( (int) len );
  28078. }
  28079. int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
  28080. {
  28081. int ret;
  28082. unsigned char *c = buf + size;
  28083. size_t len = 0;
  28084. #if defined(MBEDTLS_RSA_C)
  28085. if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
  28086. {
  28087. mbedtls_mpi T; /* Temporary holding the exported parameters */
  28088. mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
  28089. /*
  28090. * Export the parameters one after another to avoid simultaneous copies.
  28091. */
  28092. mbedtls_mpi_init( &T );
  28093. /* Export QP */
  28094. if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
  28095. ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
  28096. goto end_of_export;
  28097. len += ret;
  28098. /* Export DQ */
  28099. if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
  28100. ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
  28101. goto end_of_export;
  28102. len += ret;
  28103. /* Export DP */
  28104. if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
  28105. ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
  28106. goto end_of_export;
  28107. len += ret;
  28108. /* Export Q */
  28109. if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
  28110. &T, NULL, NULL ) ) != 0 ||
  28111. ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
  28112. goto end_of_export;
  28113. len += ret;
  28114. /* Export P */
  28115. if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
  28116. NULL, NULL, NULL ) ) != 0 ||
  28117. ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
  28118. goto end_of_export;
  28119. len += ret;
  28120. /* Export D */
  28121. if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
  28122. NULL, &T, NULL ) ) != 0 ||
  28123. ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
  28124. goto end_of_export;
  28125. len += ret;
  28126. /* Export E */
  28127. if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
  28128. NULL, NULL, &T ) ) != 0 ||
  28129. ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
  28130. goto end_of_export;
  28131. len += ret;
  28132. /* Export N */
  28133. if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
  28134. NULL, NULL, NULL ) ) != 0 ||
  28135. ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
  28136. goto end_of_export;
  28137. len += ret;
  28138. end_of_export:
  28139. mbedtls_mpi_free( &T );
  28140. if( ret < 0 )
  28141. return( ret );
  28142. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
  28143. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  28144. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
  28145. buf, MBEDTLS_ASN1_CONSTRUCTED |
  28146. MBEDTLS_ASN1_SEQUENCE ) );
  28147. }
  28148. else
  28149. #endif /* MBEDTLS_RSA_C */
  28150. #if defined(MBEDTLS_ECP_C)
  28151. if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
  28152. {
  28153. mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key );
  28154. size_t pub_len = 0, par_len = 0;
  28155. /*
  28156. * RFC 5915, or SEC1 Appendix C.4
  28157. *
  28158. * ECPrivateKey ::= SEQUENCE {
  28159. * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
  28160. * privateKey OCTET STRING,
  28161. * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
  28162. * publicKey [1] BIT STRING OPTIONAL
  28163. * }
  28164. */
  28165. /* publicKey */
  28166. MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
  28167. if( c - buf < 1 )
  28168. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  28169. *--c = 0;
  28170. pub_len += 1;
  28171. MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
  28172. MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
  28173. MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
  28174. MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
  28175. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
  28176. len += pub_len;
  28177. /* parameters */
  28178. MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
  28179. MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
  28180. MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
  28181. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
  28182. len += par_len;
  28183. /* privateKey: write as MPI then fix tag */
  28184. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &ec->d ) );
  28185. *c = MBEDTLS_ASN1_OCTET_STRING;
  28186. /* version */
  28187. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
  28188. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  28189. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
  28190. MBEDTLS_ASN1_SEQUENCE ) );
  28191. }
  28192. else
  28193. #endif /* MBEDTLS_ECP_C */
  28194. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  28195. return( (int) len );
  28196. }
  28197. #if defined(MBEDTLS_PEM_WRITE_C)
  28198. #define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
  28199. #define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
  28200. #define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
  28201. #define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
  28202. #define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
  28203. #define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
  28204. /*
  28205. * Max sizes of key per types. Shown as tag + len (+ content).
  28206. */
  28207. #if defined(MBEDTLS_RSA_C)
  28208. /*
  28209. * RSA public keys:
  28210. * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
  28211. * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
  28212. * + 1 + 1 + 9 (rsa oid)
  28213. * + 1 + 1 (params null)
  28214. * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
  28215. * RSAPublicKey ::= SEQUENCE { 1 + 3
  28216. * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
  28217. * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
  28218. * }
  28219. */
  28220. #define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
  28221. /*
  28222. * RSA private keys:
  28223. * RSAPrivateKey ::= SEQUENCE { 1 + 3
  28224. * version Version, 1 + 1 + 1
  28225. * modulus INTEGER, 1 + 3 + MPI_MAX + 1
  28226. * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
  28227. * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
  28228. * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
  28229. * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
  28230. * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
  28231. * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
  28232. * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
  28233. * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
  28234. * }
  28235. */
  28236. #define MPI_MAX_SIZE_2 MBEDTLS_MPI_MAX_SIZE / 2 + \
  28237. MBEDTLS_MPI_MAX_SIZE % 2
  28238. #define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
  28239. + 5 * MPI_MAX_SIZE_2
  28240. #else /* MBEDTLS_RSA_C */
  28241. #define RSA_PUB_DER_MAX_BYTES 0
  28242. #define RSA_PRV_DER_MAX_BYTES 0
  28243. #endif /* MBEDTLS_RSA_C */
  28244. #if defined(MBEDTLS_ECP_C)
  28245. /*
  28246. * EC public keys:
  28247. * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
  28248. * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
  28249. * + 1 + 1 + 7 (ec oid)
  28250. * + 1 + 1 + 9 (namedCurve oid)
  28251. * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
  28252. * + 1 (point format) [1]
  28253. * + 2 * ECP_MAX (coords) [1]
  28254. * }
  28255. */
  28256. #define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
  28257. /*
  28258. * EC private keys:
  28259. * ECPrivateKey ::= SEQUENCE { 1 + 2
  28260. * version INTEGER , 1 + 1 + 1
  28261. * privateKey OCTET STRING, 1 + 1 + ECP_MAX
  28262. * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
  28263. * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
  28264. * }
  28265. */
  28266. #define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
  28267. #else /* MBEDTLS_ECP_C */
  28268. #define ECP_PUB_DER_MAX_BYTES 0
  28269. #define ECP_PRV_DER_MAX_BYTES 0
  28270. #endif /* MBEDTLS_ECP_C */
  28271. #define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
  28272. RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
  28273. #define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
  28274. RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
  28275. int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
  28276. {
  28277. int ret;
  28278. unsigned char output_buf[PUB_DER_MAX_BYTES];
  28279. size_t olen = 0;
  28280. if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
  28281. sizeof(output_buf) ) ) < 0 )
  28282. {
  28283. return( ret );
  28284. }
  28285. if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
  28286. output_buf + sizeof(output_buf) - ret,
  28287. ret, buf, size, &olen ) ) != 0 )
  28288. {
  28289. return( ret );
  28290. }
  28291. return( 0 );
  28292. }
  28293. int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
  28294. {
  28295. int ret;
  28296. unsigned char output_buf[PRV_DER_MAX_BYTES];
  28297. const char *begin, *end;
  28298. size_t olen = 0;
  28299. if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
  28300. return( ret );
  28301. #if defined(MBEDTLS_RSA_C)
  28302. if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
  28303. {
  28304. begin = PEM_BEGIN_PRIVATE_KEY_RSA;
  28305. end = PEM_END_PRIVATE_KEY_RSA;
  28306. }
  28307. else
  28308. #endif
  28309. #if defined(MBEDTLS_ECP_C)
  28310. if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
  28311. {
  28312. begin = PEM_BEGIN_PRIVATE_KEY_EC;
  28313. end = PEM_END_PRIVATE_KEY_EC;
  28314. }
  28315. else
  28316. #endif
  28317. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  28318. if( ( ret = mbedtls_pem_write_buffer( begin, end,
  28319. output_buf + sizeof(output_buf) - ret,
  28320. ret, buf, size, &olen ) ) != 0 )
  28321. {
  28322. return( ret );
  28323. }
  28324. return( 0 );
  28325. }
  28326. #endif /* MBEDTLS_PEM_WRITE_C */
  28327. #endif /* MBEDTLS_PK_WRITE_C */
  28328. /*
  28329. Amalgamated build undefines
  28330. */
  28331. #undef ADD
  28332. #undef BC
  28333. #undef BEFORE_COLON
  28334. #undef F
  28335. #undef F0
  28336. #undef F1
  28337. #undef F2
  28338. #undef F3
  28339. #undef F4
  28340. #undef F5
  28341. #undef FSb
  28342. #undef K
  28343. #undef KK
  28344. #undef P
  28345. #undef R
  28346. #undef ROTR
  28347. #undef S
  28348. #undef S0
  28349. #undef S1
  28350. #undef S2
  28351. #undef S3
  28352. #undef SAFE_SNPRINTF
  28353. #undef SHR
  28354. #undef close
  28355. #undef read
  28356. #undef supported_init
  28357. #undef write
  28358. /********* Start of file library/platform.c ************/
  28359. /*
  28360. * Platform abstraction layer
  28361. *
  28362. * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
  28363. * SPDX-License-Identifier: Apache-2.0
  28364. *
  28365. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  28366. * not use this file except in compliance with the License.
  28367. * You may obtain a copy of the License at
  28368. *
  28369. * http://www.apache.org/licenses/LICENSE-2.0
  28370. *
  28371. * Unless required by applicable law or agreed to in writing, software
  28372. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  28373. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  28374. * See the License for the specific language governing permissions and
  28375. * limitations under the License.
  28376. *
  28377. * This file is part of mbed TLS (https://tls.mbed.org)
  28378. */
  28379. #if !defined(MBEDTLS_CONFIG_FILE)
  28380. #else
  28381. #endif
  28382. #if defined(MBEDTLS_PLATFORM_C)
  28383. #if defined(MBEDTLS_ENTROPY_NV_SEED) && \
  28384. !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
  28385. /* Implementation that should never be optimized out by the compiler */
  28386. /* zeroize was here */
  28387. #endif
  28388. #if defined(MBEDTLS_PLATFORM_MEMORY)
  28389. #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
  28390. static void *platform_calloc_uninit( size_t n, size_t size )
  28391. {
  28392. ((void) n);
  28393. ((void) size);
  28394. return( NULL );
  28395. }
  28396. #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
  28397. #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
  28398. #if !defined(MBEDTLS_PLATFORM_STD_FREE)
  28399. static void platform_free_uninit( void *ptr )
  28400. {
  28401. ((void) ptr);
  28402. }
  28403. #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
  28404. #endif /* !MBEDTLS_PLATFORM_STD_FREE */
  28405. void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
  28406. void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
  28407. int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
  28408. void (*free_func)( void * ) )
  28409. {
  28410. mbedtls_calloc = calloc_func;
  28411. mbedtls_free = free_func;
  28412. return( 0 );
  28413. }
  28414. #endif /* MBEDTLS_PLATFORM_MEMORY */
  28415. #if defined(_WIN32)
  28416. #include <stdarg.h>
  28417. int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
  28418. {
  28419. int ret;
  28420. va_list argp;
  28421. /* Avoid calling the invalid parameter handler by checking ourselves */
  28422. if( s == NULL || n == 0 || fmt == NULL )
  28423. return( -1 );
  28424. va_start( argp, fmt );
  28425. #if defined(_TRUNCATE) && !defined(__MINGW32__)
  28426. ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
  28427. #else
  28428. ret = _vsnprintf( s, n, fmt, argp );
  28429. if( ret < 0 || (size_t) ret == n )
  28430. {
  28431. s[n-1] = '\0';
  28432. ret = -1;
  28433. }
  28434. #endif
  28435. va_end( argp );
  28436. return( ret );
  28437. }
  28438. #endif
  28439. #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
  28440. #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
  28441. /*
  28442. * Make dummy function to prevent NULL pointer dereferences
  28443. */
  28444. static int platform_snprintf_uninit( char * s, size_t n,
  28445. const char * format, ... )
  28446. {
  28447. ((void) s);
  28448. ((void) n);
  28449. ((void) format);
  28450. return( 0 );
  28451. }
  28452. #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
  28453. #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
  28454. int (*mbedtls_snprintf)( char * s, size_t n,
  28455. const char * format,
  28456. ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
  28457. int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
  28458. const char * format,
  28459. ... ) )
  28460. {
  28461. mbedtls_snprintf = snprintf_func;
  28462. return( 0 );
  28463. }
  28464. #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  28465. #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
  28466. #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
  28467. /*
  28468. * Make dummy function to prevent NULL pointer dereferences
  28469. */
  28470. static int platform_printf_uninit( const char *format, ... )
  28471. {
  28472. ((void) format);
  28473. return( 0 );
  28474. }
  28475. #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
  28476. #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
  28477. int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
  28478. int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
  28479. {
  28480. mbedtls_printf = printf_func;
  28481. return( 0 );
  28482. }
  28483. #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
  28484. #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
  28485. #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
  28486. /*
  28487. * Make dummy function to prevent NULL pointer dereferences
  28488. */
  28489. static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
  28490. {
  28491. ((void) stream);
  28492. ((void) format);
  28493. return( 0 );
  28494. }
  28495. #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
  28496. #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
  28497. int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
  28498. MBEDTLS_PLATFORM_STD_FPRINTF;
  28499. int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
  28500. {
  28501. mbedtls_fprintf = fprintf_func;
  28502. return( 0 );
  28503. }
  28504. #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
  28505. #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
  28506. #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
  28507. /*
  28508. * Make dummy function to prevent NULL pointer dereferences
  28509. */
  28510. static void platform_exit_uninit( int status )
  28511. {
  28512. ((void) status);
  28513. }
  28514. #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
  28515. #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
  28516. void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
  28517. int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
  28518. {
  28519. mbedtls_exit = exit_func;
  28520. return( 0 );
  28521. }
  28522. #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
  28523. #if defined(MBEDTLS_HAVE_TIME)
  28524. #if defined(MBEDTLS_PLATFORM_TIME_ALT)
  28525. #if !defined(MBEDTLS_PLATFORM_STD_TIME)
  28526. /*
  28527. * Make dummy function to prevent NULL pointer dereferences
  28528. */
  28529. static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
  28530. {
  28531. ((void) timer);
  28532. return( 0 );
  28533. }
  28534. #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
  28535. #endif /* !MBEDTLS_PLATFORM_STD_TIME */
  28536. mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
  28537. int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
  28538. {
  28539. mbedtls_time = time_func;
  28540. return( 0 );
  28541. }
  28542. #endif /* MBEDTLS_PLATFORM_TIME_ALT */
  28543. #endif /* MBEDTLS_HAVE_TIME */
  28544. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  28545. #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
  28546. /* Default implementations for the platform independent seed functions use
  28547. * standard libc file functions to read from and write to a pre-defined filename
  28548. */
  28549. int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
  28550. {
  28551. FILE *file;
  28552. size_t n;
  28553. if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
  28554. return( -1 );
  28555. if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
  28556. {
  28557. fclose( file );
  28558. mbedtls_zeroize( buf, buf_len );
  28559. return( -1 );
  28560. }
  28561. fclose( file );
  28562. return( (int)n );
  28563. }
  28564. int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
  28565. {
  28566. FILE *file;
  28567. size_t n;
  28568. if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
  28569. return -1;
  28570. if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
  28571. {
  28572. fclose( file );
  28573. return -1;
  28574. }
  28575. fclose( file );
  28576. return( (int)n );
  28577. }
  28578. #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
  28579. #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
  28580. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
  28581. /*
  28582. * Make dummy function to prevent NULL pointer dereferences
  28583. */
  28584. static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
  28585. {
  28586. ((void) buf);
  28587. ((void) buf_len);
  28588. return( -1 );
  28589. }
  28590. #define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
  28591. #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
  28592. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
  28593. /*
  28594. * Make dummy function to prevent NULL pointer dereferences
  28595. */
  28596. static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
  28597. {
  28598. ((void) buf);
  28599. ((void) buf_len);
  28600. return( -1 );
  28601. }
  28602. #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
  28603. #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
  28604. int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
  28605. MBEDTLS_PLATFORM_STD_NV_SEED_READ;
  28606. int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
  28607. MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
  28608. int mbedtls_platform_set_nv_seed(
  28609. int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
  28610. int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
  28611. {
  28612. mbedtls_nv_seed_read = nv_seed_read_func;
  28613. mbedtls_nv_seed_write = nv_seed_write_func;
  28614. return( 0 );
  28615. }
  28616. #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
  28617. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  28618. #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
  28619. /*
  28620. * Placeholder platform setup that does nothing by default
  28621. */
  28622. int mbedtls_platform_setup( mbedtls_platform_context *ctx )
  28623. {
  28624. (void)ctx;
  28625. return( 0 );
  28626. }
  28627. /*
  28628. * Placeholder platform teardown that does nothing by default
  28629. */
  28630. void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
  28631. {
  28632. (void)ctx;
  28633. }
  28634. #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
  28635. #endif /* MBEDTLS_PLATFORM_C */
  28636. /*
  28637. Amalgamated build undefines
  28638. */
  28639. #undef ADD
  28640. #undef BC
  28641. #undef BEFORE_COLON
  28642. #undef F
  28643. #undef F0
  28644. #undef F1
  28645. #undef F2
  28646. #undef F3
  28647. #undef F4
  28648. #undef F5
  28649. #undef FSb
  28650. #undef K
  28651. #undef KK
  28652. #undef P
  28653. #undef R
  28654. #undef ROTR
  28655. #undef S
  28656. #undef S0
  28657. #undef S1
  28658. #undef S2
  28659. #undef S3
  28660. #undef SAFE_SNPRINTF
  28661. #undef SHR
  28662. #undef close
  28663. #undef read
  28664. #undef supported_init
  28665. #undef write
  28666. /********* Start of file library/ripemd160.c ************/
  28667. /*
  28668. * RIPE MD-160 implementation
  28669. *
  28670. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  28671. * SPDX-License-Identifier: Apache-2.0
  28672. *
  28673. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  28674. * not use this file except in compliance with the License.
  28675. * You may obtain a copy of the License at
  28676. *
  28677. * http://www.apache.org/licenses/LICENSE-2.0
  28678. *
  28679. * Unless required by applicable law or agreed to in writing, software
  28680. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  28681. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  28682. * See the License for the specific language governing permissions and
  28683. * limitations under the License.
  28684. *
  28685. * This file is part of mbed TLS (https://tls.mbed.org)
  28686. */
  28687. /*
  28688. * The RIPEMD-160 algorithm was designed by RIPE in 1996
  28689. * http://homes.esat.kuleuven.be/~bosselae/mbedtls_ripemd160.html
  28690. * http://ehash.iaik.tugraz.at/wiki/RIPEMD-160
  28691. */
  28692. #if !defined(MBEDTLS_CONFIG_FILE)
  28693. #else
  28694. #endif
  28695. #if defined(MBEDTLS_RIPEMD160_C)
  28696. #include <string.h>
  28697. #if defined(MBEDTLS_SELF_TEST)
  28698. #if defined(MBEDTLS_PLATFORM_C)
  28699. #else
  28700. #include <stdio.h>
  28701. #define mbedtls_printf printf
  28702. #endif /* MBEDTLS_PLATFORM_C */
  28703. #endif /* MBEDTLS_SELF_TEST */
  28704. #if !defined(MBEDTLS_RIPEMD160_ALT)
  28705. /*
  28706. * 32-bit integer manipulation macros (little endian)
  28707. */
  28708. #ifndef GET_UINT32_LE
  28709. #define GET_UINT32_LE(n,b,i) \
  28710. { \
  28711. (n) = ( (uint32_t) (b)[(i) ] ) \
  28712. | ( (uint32_t) (b)[(i) + 1] << 8 ) \
  28713. | ( (uint32_t) (b)[(i) + 2] << 16 ) \
  28714. | ( (uint32_t) (b)[(i) + 3] << 24 ); \
  28715. }
  28716. #endif
  28717. #ifndef PUT_UINT32_LE
  28718. #define PUT_UINT32_LE(n,b,i) \
  28719. { \
  28720. (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
  28721. (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
  28722. (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
  28723. (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
  28724. }
  28725. #endif
  28726. /* Implementation that should never be optimized out by the compiler */
  28727. /* zeroize was here */
  28728. void mbedtls_ripemd160_init( mbedtls_ripemd160_context *ctx )
  28729. {
  28730. memset( ctx, 0, sizeof( mbedtls_ripemd160_context ) );
  28731. }
  28732. void mbedtls_ripemd160_free( mbedtls_ripemd160_context *ctx )
  28733. {
  28734. if( ctx == NULL )
  28735. return;
  28736. mbedtls_zeroize( ctx, sizeof( mbedtls_ripemd160_context ) );
  28737. }
  28738. void mbedtls_ripemd160_clone( mbedtls_ripemd160_context *dst,
  28739. const mbedtls_ripemd160_context *src )
  28740. {
  28741. *dst = *src;
  28742. }
  28743. /*
  28744. * RIPEMD-160 context setup
  28745. */
  28746. int mbedtls_ripemd160_starts_ret( mbedtls_ripemd160_context *ctx )
  28747. {
  28748. ctx->total[0] = 0;
  28749. ctx->total[1] = 0;
  28750. ctx->state[0] = 0x67452301;
  28751. ctx->state[1] = 0xEFCDAB89;
  28752. ctx->state[2] = 0x98BADCFE;
  28753. ctx->state[3] = 0x10325476;
  28754. ctx->state[4] = 0xC3D2E1F0;
  28755. return( 0 );
  28756. }
  28757. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  28758. void mbedtls_ripemd160_starts( mbedtls_ripemd160_context *ctx )
  28759. {
  28760. mbedtls_ripemd160_starts_ret( ctx );
  28761. }
  28762. #endif
  28763. #if !defined(MBEDTLS_RIPEMD160_PROCESS_ALT)
  28764. /*
  28765. * Process one block
  28766. */
  28767. int mbedtls_internal_ripemd160_process( mbedtls_ripemd160_context *ctx,
  28768. const unsigned char data[64] )
  28769. {
  28770. uint32_t A, B, C, D, E, Ap, Bp, Cp, Dp, Ep, X[16];
  28771. GET_UINT32_LE( X[ 0], data, 0 );
  28772. GET_UINT32_LE( X[ 1], data, 4 );
  28773. GET_UINT32_LE( X[ 2], data, 8 );
  28774. GET_UINT32_LE( X[ 3], data, 12 );
  28775. GET_UINT32_LE( X[ 4], data, 16 );
  28776. GET_UINT32_LE( X[ 5], data, 20 );
  28777. GET_UINT32_LE( X[ 6], data, 24 );
  28778. GET_UINT32_LE( X[ 7], data, 28 );
  28779. GET_UINT32_LE( X[ 8], data, 32 );
  28780. GET_UINT32_LE( X[ 9], data, 36 );
  28781. GET_UINT32_LE( X[10], data, 40 );
  28782. GET_UINT32_LE( X[11], data, 44 );
  28783. GET_UINT32_LE( X[12], data, 48 );
  28784. GET_UINT32_LE( X[13], data, 52 );
  28785. GET_UINT32_LE( X[14], data, 56 );
  28786. GET_UINT32_LE( X[15], data, 60 );
  28787. A = Ap = ctx->state[0];
  28788. B = Bp = ctx->state[1];
  28789. C = Cp = ctx->state[2];
  28790. D = Dp = ctx->state[3];
  28791. E = Ep = ctx->state[4];
  28792. #define F1( x, y, z ) ( x ^ y ^ z )
  28793. #define F2( x, y, z ) ( ( x & y ) | ( ~x & z ) )
  28794. #define F3( x, y, z ) ( ( x | ~y ) ^ z )
  28795. #define F4( x, y, z ) ( ( x & z ) | ( y & ~z ) )
  28796. #define F5( x, y, z ) ( x ^ ( y | ~z ) )
  28797. #define S( x, n ) ( ( x << n ) | ( x >> (32 - n) ) )
  28798. #define P( a, b, c, d, e, r, s, f, k ) \
  28799. a += f( b, c, d ) + X[r] + k; \
  28800. a = S( a, s ) + e; \
  28801. c = S( c, 10 );
  28802. #define P2( a, b, c, d, e, r, s, rp, sp ) \
  28803. P( a, b, c, d, e, r, s, F, K ); \
  28804. P( a ## p, b ## p, c ## p, d ## p, e ## p, rp, sp, Fp, Kp );
  28805. #define F F1
  28806. #define K 0x00000000
  28807. #define Fp F5
  28808. #define Kp 0x50A28BE6
  28809. P2( A, B, C, D, E, 0, 11, 5, 8 );
  28810. P2( E, A, B, C, D, 1, 14, 14, 9 );
  28811. P2( D, E, A, B, C, 2, 15, 7, 9 );
  28812. P2( C, D, E, A, B, 3, 12, 0, 11 );
  28813. P2( B, C, D, E, A, 4, 5, 9, 13 );
  28814. P2( A, B, C, D, E, 5, 8, 2, 15 );
  28815. P2( E, A, B, C, D, 6, 7, 11, 15 );
  28816. P2( D, E, A, B, C, 7, 9, 4, 5 );
  28817. P2( C, D, E, A, B, 8, 11, 13, 7 );
  28818. P2( B, C, D, E, A, 9, 13, 6, 7 );
  28819. P2( A, B, C, D, E, 10, 14, 15, 8 );
  28820. P2( E, A, B, C, D, 11, 15, 8, 11 );
  28821. P2( D, E, A, B, C, 12, 6, 1, 14 );
  28822. P2( C, D, E, A, B, 13, 7, 10, 14 );
  28823. P2( B, C, D, E, A, 14, 9, 3, 12 );
  28824. P2( A, B, C, D, E, 15, 8, 12, 6 );
  28825. #undef F
  28826. #undef K
  28827. #undef Fp
  28828. #undef Kp
  28829. #define F F2
  28830. #define K 0x5A827999
  28831. #define Fp F4
  28832. #define Kp 0x5C4DD124
  28833. P2( E, A, B, C, D, 7, 7, 6, 9 );
  28834. P2( D, E, A, B, C, 4, 6, 11, 13 );
  28835. P2( C, D, E, A, B, 13, 8, 3, 15 );
  28836. P2( B, C, D, E, A, 1, 13, 7, 7 );
  28837. P2( A, B, C, D, E, 10, 11, 0, 12 );
  28838. P2( E, A, B, C, D, 6, 9, 13, 8 );
  28839. P2( D, E, A, B, C, 15, 7, 5, 9 );
  28840. P2( C, D, E, A, B, 3, 15, 10, 11 );
  28841. P2( B, C, D, E, A, 12, 7, 14, 7 );
  28842. P2( A, B, C, D, E, 0, 12, 15, 7 );
  28843. P2( E, A, B, C, D, 9, 15, 8, 12 );
  28844. P2( D, E, A, B, C, 5, 9, 12, 7 );
  28845. P2( C, D, E, A, B, 2, 11, 4, 6 );
  28846. P2( B, C, D, E, A, 14, 7, 9, 15 );
  28847. P2( A, B, C, D, E, 11, 13, 1, 13 );
  28848. P2( E, A, B, C, D, 8, 12, 2, 11 );
  28849. #undef F
  28850. #undef K
  28851. #undef Fp
  28852. #undef Kp
  28853. #define F F3
  28854. #define K 0x6ED9EBA1
  28855. #define Fp F3
  28856. #define Kp 0x6D703EF3
  28857. P2( D, E, A, B, C, 3, 11, 15, 9 );
  28858. P2( C, D, E, A, B, 10, 13, 5, 7 );
  28859. P2( B, C, D, E, A, 14, 6, 1, 15 );
  28860. P2( A, B, C, D, E, 4, 7, 3, 11 );
  28861. P2( E, A, B, C, D, 9, 14, 7, 8 );
  28862. P2( D, E, A, B, C, 15, 9, 14, 6 );
  28863. P2( C, D, E, A, B, 8, 13, 6, 6 );
  28864. P2( B, C, D, E, A, 1, 15, 9, 14 );
  28865. P2( A, B, C, D, E, 2, 14, 11, 12 );
  28866. P2( E, A, B, C, D, 7, 8, 8, 13 );
  28867. P2( D, E, A, B, C, 0, 13, 12, 5 );
  28868. P2( C, D, E, A, B, 6, 6, 2, 14 );
  28869. P2( B, C, D, E, A, 13, 5, 10, 13 );
  28870. P2( A, B, C, D, E, 11, 12, 0, 13 );
  28871. P2( E, A, B, C, D, 5, 7, 4, 7 );
  28872. P2( D, E, A, B, C, 12, 5, 13, 5 );
  28873. #undef F
  28874. #undef K
  28875. #undef Fp
  28876. #undef Kp
  28877. #define F F4
  28878. #define K 0x8F1BBCDC
  28879. #define Fp F2
  28880. #define Kp 0x7A6D76E9
  28881. P2( C, D, E, A, B, 1, 11, 8, 15 );
  28882. P2( B, C, D, E, A, 9, 12, 6, 5 );
  28883. P2( A, B, C, D, E, 11, 14, 4, 8 );
  28884. P2( E, A, B, C, D, 10, 15, 1, 11 );
  28885. P2( D, E, A, B, C, 0, 14, 3, 14 );
  28886. P2( C, D, E, A, B, 8, 15, 11, 14 );
  28887. P2( B, C, D, E, A, 12, 9, 15, 6 );
  28888. P2( A, B, C, D, E, 4, 8, 0, 14 );
  28889. P2( E, A, B, C, D, 13, 9, 5, 6 );
  28890. P2( D, E, A, B, C, 3, 14, 12, 9 );
  28891. P2( C, D, E, A, B, 7, 5, 2, 12 );
  28892. P2( B, C, D, E, A, 15, 6, 13, 9 );
  28893. P2( A, B, C, D, E, 14, 8, 9, 12 );
  28894. P2( E, A, B, C, D, 5, 6, 7, 5 );
  28895. P2( D, E, A, B, C, 6, 5, 10, 15 );
  28896. P2( C, D, E, A, B, 2, 12, 14, 8 );
  28897. #undef F
  28898. #undef K
  28899. #undef Fp
  28900. #undef Kp
  28901. #define F F5
  28902. #define K 0xA953FD4E
  28903. #define Fp F1
  28904. #define Kp 0x00000000
  28905. P2( B, C, D, E, A, 4, 9, 12, 8 );
  28906. P2( A, B, C, D, E, 0, 15, 15, 5 );
  28907. P2( E, A, B, C, D, 5, 5, 10, 12 );
  28908. P2( D, E, A, B, C, 9, 11, 4, 9 );
  28909. P2( C, D, E, A, B, 7, 6, 1, 12 );
  28910. P2( B, C, D, E, A, 12, 8, 5, 5 );
  28911. P2( A, B, C, D, E, 2, 13, 8, 14 );
  28912. P2( E, A, B, C, D, 10, 12, 7, 6 );
  28913. P2( D, E, A, B, C, 14, 5, 6, 8 );
  28914. P2( C, D, E, A, B, 1, 12, 2, 13 );
  28915. P2( B, C, D, E, A, 3, 13, 13, 6 );
  28916. P2( A, B, C, D, E, 8, 14, 14, 5 );
  28917. P2( E, A, B, C, D, 11, 11, 0, 15 );
  28918. P2( D, E, A, B, C, 6, 8, 3, 13 );
  28919. P2( C, D, E, A, B, 15, 5, 9, 11 );
  28920. P2( B, C, D, E, A, 13, 6, 11, 11 );
  28921. #undef F
  28922. #undef K
  28923. #undef Fp
  28924. #undef Kp
  28925. C = ctx->state[1] + C + Dp;
  28926. ctx->state[1] = ctx->state[2] + D + Ep;
  28927. ctx->state[2] = ctx->state[3] + E + Ap;
  28928. ctx->state[3] = ctx->state[4] + A + Bp;
  28929. ctx->state[4] = ctx->state[0] + B + Cp;
  28930. ctx->state[0] = C;
  28931. return( 0 );
  28932. }
  28933. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  28934. void mbedtls_ripemd160_process( mbedtls_ripemd160_context *ctx,
  28935. const unsigned char data[64] )
  28936. {
  28937. mbedtls_internal_ripemd160_process( ctx, data );
  28938. }
  28939. #endif
  28940. #endif /* !MBEDTLS_RIPEMD160_PROCESS_ALT */
  28941. /*
  28942. * RIPEMD-160 process buffer
  28943. */
  28944. int mbedtls_ripemd160_update_ret( mbedtls_ripemd160_context *ctx,
  28945. const unsigned char *input,
  28946. size_t ilen )
  28947. {
  28948. int ret;
  28949. size_t fill;
  28950. uint32_t left;
  28951. if( ilen == 0 )
  28952. return( 0 );
  28953. left = ctx->total[0] & 0x3F;
  28954. fill = 64 - left;
  28955. ctx->total[0] += (uint32_t) ilen;
  28956. ctx->total[0] &= 0xFFFFFFFF;
  28957. if( ctx->total[0] < (uint32_t) ilen )
  28958. ctx->total[1]++;
  28959. if( left && ilen >= fill )
  28960. {
  28961. memcpy( (void *) (ctx->buffer + left), input, fill );
  28962. if( ( ret = mbedtls_internal_ripemd160_process( ctx, ctx->buffer ) ) != 0 )
  28963. return( ret );
  28964. input += fill;
  28965. ilen -= fill;
  28966. left = 0;
  28967. }
  28968. while( ilen >= 64 )
  28969. {
  28970. if( ( ret = mbedtls_internal_ripemd160_process( ctx, input ) ) != 0 )
  28971. return( ret );
  28972. input += 64;
  28973. ilen -= 64;
  28974. }
  28975. if( ilen > 0 )
  28976. {
  28977. memcpy( (void *) (ctx->buffer + left), input, ilen );
  28978. }
  28979. return( 0 );
  28980. }
  28981. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  28982. void mbedtls_ripemd160_update( mbedtls_ripemd160_context *ctx,
  28983. const unsigned char *input,
  28984. size_t ilen )
  28985. {
  28986. mbedtls_ripemd160_update_ret( ctx, input, ilen );
  28987. }
  28988. #endif
  28989. static const unsigned char ripemd160_padding[64] =
  28990. {
  28991. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  28992. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  28993. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  28994. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  28995. };
  28996. /*
  28997. * RIPEMD-160 final digest
  28998. */
  28999. int mbedtls_ripemd160_finish_ret( mbedtls_ripemd160_context *ctx,
  29000. unsigned char output[20] )
  29001. {
  29002. int ret;
  29003. uint32_t last, padn;
  29004. uint32_t high, low;
  29005. unsigned char msglen[8];
  29006. high = ( ctx->total[0] >> 29 )
  29007. | ( ctx->total[1] << 3 );
  29008. low = ( ctx->total[0] << 3 );
  29009. PUT_UINT32_LE( low, msglen, 0 );
  29010. PUT_UINT32_LE( high, msglen, 4 );
  29011. last = ctx->total[0] & 0x3F;
  29012. padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  29013. ret = mbedtls_ripemd160_update_ret( ctx, ripemd160_padding, padn );
  29014. if( ret != 0 )
  29015. return( ret );
  29016. ret = mbedtls_ripemd160_update_ret( ctx, msglen, 8 );
  29017. if( ret != 0 )
  29018. return( ret );
  29019. PUT_UINT32_LE( ctx->state[0], output, 0 );
  29020. PUT_UINT32_LE( ctx->state[1], output, 4 );
  29021. PUT_UINT32_LE( ctx->state[2], output, 8 );
  29022. PUT_UINT32_LE( ctx->state[3], output, 12 );
  29023. PUT_UINT32_LE( ctx->state[4], output, 16 );
  29024. return( 0 );
  29025. }
  29026. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  29027. void mbedtls_ripemd160_finish( mbedtls_ripemd160_context *ctx,
  29028. unsigned char output[20] )
  29029. {
  29030. mbedtls_ripemd160_finish_ret( ctx, output );
  29031. }
  29032. #endif
  29033. #endif /* ! MBEDTLS_RIPEMD160_ALT */
  29034. /*
  29035. * output = RIPEMD-160( input buffer )
  29036. */
  29037. int mbedtls_ripemd160_ret( const unsigned char *input,
  29038. size_t ilen,
  29039. unsigned char output[20] )
  29040. {
  29041. int ret;
  29042. mbedtls_ripemd160_context ctx;
  29043. mbedtls_ripemd160_init( &ctx );
  29044. if( ( ret = mbedtls_ripemd160_starts_ret( &ctx ) ) != 0 )
  29045. goto exit;
  29046. if( ( ret = mbedtls_ripemd160_update_ret( &ctx, input, ilen ) ) != 0 )
  29047. goto exit;
  29048. if( ( ret = mbedtls_ripemd160_finish_ret( &ctx, output ) ) != 0 )
  29049. goto exit;
  29050. exit:
  29051. mbedtls_ripemd160_free( &ctx );
  29052. return( ret );
  29053. }
  29054. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  29055. void mbedtls_ripemd160( const unsigned char *input,
  29056. size_t ilen,
  29057. unsigned char output[20] )
  29058. {
  29059. mbedtls_ripemd160_ret( input, ilen, output );
  29060. }
  29061. #endif
  29062. #if defined(MBEDTLS_SELF_TEST)
  29063. /*
  29064. * Test vectors from the RIPEMD-160 paper and
  29065. * http://homes.esat.kuleuven.be/~bosselae/mbedtls_ripemd160.html#HMAC
  29066. */
  29067. #define TESTS 8
  29068. static const unsigned char ripemd160_test_str[TESTS][81] =
  29069. {
  29070. { "" },
  29071. { "a" },
  29072. { "abc" },
  29073. { "message digest" },
  29074. { "abcdefghijklmnopqrstuvwxyz" },
  29075. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
  29076. { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
  29077. { "12345678901234567890123456789012345678901234567890123456789012"
  29078. "345678901234567890" },
  29079. };
  29080. static const size_t ripemd160_test_strlen[TESTS] =
  29081. {
  29082. 0, 1, 3, 14, 26, 56, 62, 80
  29083. };
  29084. static const unsigned char ripemd160_test_md[TESTS][20] =
  29085. {
  29086. { 0x9c, 0x11, 0x85, 0xa5, 0xc5, 0xe9, 0xfc, 0x54, 0x61, 0x28,
  29087. 0x08, 0x97, 0x7e, 0xe8, 0xf5, 0x48, 0xb2, 0x25, 0x8d, 0x31 },
  29088. { 0x0b, 0xdc, 0x9d, 0x2d, 0x25, 0x6b, 0x3e, 0xe9, 0xda, 0xae,
  29089. 0x34, 0x7b, 0xe6, 0xf4, 0xdc, 0x83, 0x5a, 0x46, 0x7f, 0xfe },
  29090. { 0x8e, 0xb2, 0x08, 0xf7, 0xe0, 0x5d, 0x98, 0x7a, 0x9b, 0x04,
  29091. 0x4a, 0x8e, 0x98, 0xc6, 0xb0, 0x87, 0xf1, 0x5a, 0x0b, 0xfc },
  29092. { 0x5d, 0x06, 0x89, 0xef, 0x49, 0xd2, 0xfa, 0xe5, 0x72, 0xb8,
  29093. 0x81, 0xb1, 0x23, 0xa8, 0x5f, 0xfa, 0x21, 0x59, 0x5f, 0x36 },
  29094. { 0xf7, 0x1c, 0x27, 0x10, 0x9c, 0x69, 0x2c, 0x1b, 0x56, 0xbb,
  29095. 0xdc, 0xeb, 0x5b, 0x9d, 0x28, 0x65, 0xb3, 0x70, 0x8d, 0xbc },
  29096. { 0x12, 0xa0, 0x53, 0x38, 0x4a, 0x9c, 0x0c, 0x88, 0xe4, 0x05,
  29097. 0xa0, 0x6c, 0x27, 0xdc, 0xf4, 0x9a, 0xda, 0x62, 0xeb, 0x2b },
  29098. { 0xb0, 0xe2, 0x0b, 0x6e, 0x31, 0x16, 0x64, 0x02, 0x86, 0xed,
  29099. 0x3a, 0x87, 0xa5, 0x71, 0x30, 0x79, 0xb2, 0x1f, 0x51, 0x89 },
  29100. { 0x9b, 0x75, 0x2e, 0x45, 0x57, 0x3d, 0x4b, 0x39, 0xf4, 0xdb,
  29101. 0xd3, 0x32, 0x3c, 0xab, 0x82, 0xbf, 0x63, 0x32, 0x6b, 0xfb },
  29102. };
  29103. /*
  29104. * Checkup routine
  29105. */
  29106. int mbedtls_ripemd160_self_test( int verbose )
  29107. {
  29108. int i, ret = 0;
  29109. unsigned char output[20];
  29110. memset( output, 0, sizeof output );
  29111. for( i = 0; i < TESTS; i++ )
  29112. {
  29113. if( verbose != 0 )
  29114. mbedtls_printf( " RIPEMD-160 test #%d: ", i + 1 );
  29115. ret = mbedtls_ripemd160_ret( ripemd160_test_str[i],
  29116. ripemd160_test_strlen[i], output );
  29117. if( ret != 0 )
  29118. goto fail;
  29119. if( memcmp( output, ripemd160_test_md[i], 20 ) != 0 )
  29120. {
  29121. ret = 1;
  29122. goto fail;
  29123. }
  29124. if( verbose != 0 )
  29125. mbedtls_printf( "passed\n" );
  29126. }
  29127. if( verbose != 0 )
  29128. mbedtls_printf( "\n" );
  29129. return( 0 );
  29130. fail:
  29131. if( verbose != 0 )
  29132. mbedtls_printf( "failed\n" );
  29133. return( ret );
  29134. }
  29135. #endif /* MBEDTLS_SELF_TEST */
  29136. #endif /* MBEDTLS_RIPEMD160_C */
  29137. /*
  29138. Amalgamated build undefines
  29139. */
  29140. #undef ADD
  29141. #undef BC
  29142. #undef BEFORE_COLON
  29143. #undef F
  29144. #undef F0
  29145. #undef F1
  29146. #undef F2
  29147. #undef F3
  29148. #undef F4
  29149. #undef F5
  29150. #undef FSb
  29151. #undef K
  29152. #undef KK
  29153. #undef P
  29154. #undef R
  29155. #undef ROTR
  29156. #undef S
  29157. #undef S0
  29158. #undef S1
  29159. #undef S2
  29160. #undef S3
  29161. #undef SAFE_SNPRINTF
  29162. #undef SHR
  29163. #undef close
  29164. #undef read
  29165. #undef supported_init
  29166. #undef write
  29167. /********* Start of file library/rsa.c ************/
  29168. /*
  29169. * The RSA public-key cryptosystem
  29170. *
  29171. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  29172. * SPDX-License-Identifier: Apache-2.0
  29173. *
  29174. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  29175. * not use this file except in compliance with the License.
  29176. * You may obtain a copy of the License at
  29177. *
  29178. * http://www.apache.org/licenses/LICENSE-2.0
  29179. *
  29180. * Unless required by applicable law or agreed to in writing, software
  29181. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  29182. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  29183. * See the License for the specific language governing permissions and
  29184. * limitations under the License.
  29185. *
  29186. * This file is part of mbed TLS (https://tls.mbed.org)
  29187. */
  29188. /*
  29189. * The following sources were referenced in the design of this implementation
  29190. * of the RSA algorithm:
  29191. *
  29192. * [1] A method for obtaining digital signatures and public-key cryptosystems
  29193. * R Rivest, A Shamir, and L Adleman
  29194. * http://people.csail.mit.edu/rivest/pubs.html#RSA78
  29195. *
  29196. * [2] Handbook of Applied Cryptography - 1997, Chapter 8
  29197. * Menezes, van Oorschot and Vanstone
  29198. *
  29199. * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
  29200. * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
  29201. * Stefan Mangard
  29202. * https://arxiv.org/abs/1702.08719v2
  29203. *
  29204. */
  29205. #if !defined(MBEDTLS_CONFIG_FILE)
  29206. #else
  29207. #endif
  29208. #if defined(MBEDTLS_RSA_C)
  29209. #include <string.h>
  29210. #if defined(MBEDTLS_PKCS1_V21)
  29211. #endif
  29212. #if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
  29213. #include <stdlib.h>
  29214. #endif
  29215. #if defined(MBEDTLS_PLATFORM_C)
  29216. #else
  29217. #include <stdio.h>
  29218. #define mbedtls_printf printf
  29219. #define mbedtls_calloc calloc
  29220. #define mbedtls_free free
  29221. #endif
  29222. #if !defined(MBEDTLS_RSA_ALT)
  29223. /* Implementation that should never be optimized out by the compiler */
  29224. /* zeroize was here */
  29225. /* constant-time buffer comparison */
  29226. static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
  29227. {
  29228. size_t i;
  29229. const unsigned char *A = (const unsigned char *) a;
  29230. const unsigned char *B = (const unsigned char *) b;
  29231. unsigned char diff = 0;
  29232. for( i = 0; i < n; i++ )
  29233. diff |= A[i] ^ B[i];
  29234. return( diff );
  29235. }
  29236. int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
  29237. const mbedtls_mpi *N,
  29238. const mbedtls_mpi *P, const mbedtls_mpi *Q,
  29239. const mbedtls_mpi *D, const mbedtls_mpi *E )
  29240. {
  29241. int ret;
  29242. if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
  29243. ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
  29244. ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
  29245. ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
  29246. ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
  29247. {
  29248. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
  29249. }
  29250. if( N != NULL )
  29251. ctx->len = mbedtls_mpi_size( &ctx->N );
  29252. return( 0 );
  29253. }
  29254. int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
  29255. unsigned char const *N, size_t N_len,
  29256. unsigned char const *P, size_t P_len,
  29257. unsigned char const *Q, size_t Q_len,
  29258. unsigned char const *D, size_t D_len,
  29259. unsigned char const *E, size_t E_len )
  29260. {
  29261. int ret = 0;
  29262. if( N != NULL )
  29263. {
  29264. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
  29265. ctx->len = mbedtls_mpi_size( &ctx->N );
  29266. }
  29267. if( P != NULL )
  29268. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
  29269. if( Q != NULL )
  29270. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
  29271. if( D != NULL )
  29272. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
  29273. if( E != NULL )
  29274. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
  29275. cleanup:
  29276. if( ret != 0 )
  29277. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
  29278. return( 0 );
  29279. }
  29280. /*
  29281. * Checks whether the context fields are set in such a way
  29282. * that the RSA primitives will be able to execute without error.
  29283. * It does *not* make guarantees for consistency of the parameters.
  29284. */
  29285. static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
  29286. int blinding_needed )
  29287. {
  29288. #if !defined(MBEDTLS_RSA_NO_CRT)
  29289. /* blinding_needed is only used for NO_CRT to decide whether
  29290. * P,Q need to be present or not. */
  29291. ((void) blinding_needed);
  29292. #endif
  29293. if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
  29294. ctx->len > MBEDTLS_MPI_MAX_SIZE )
  29295. {
  29296. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  29297. }
  29298. /*
  29299. * 1. Modular exponentiation needs positive, odd moduli.
  29300. */
  29301. /* Modular exponentiation wrt. N is always used for
  29302. * RSA public key operations. */
  29303. if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
  29304. mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
  29305. {
  29306. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  29307. }
  29308. #if !defined(MBEDTLS_RSA_NO_CRT)
  29309. /* Modular exponentiation for P and Q is only
  29310. * used for private key operations and if CRT
  29311. * is used. */
  29312. if( is_priv &&
  29313. ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
  29314. mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
  29315. mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
  29316. mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
  29317. {
  29318. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  29319. }
  29320. #endif /* !MBEDTLS_RSA_NO_CRT */
  29321. /*
  29322. * 2. Exponents must be positive
  29323. */
  29324. /* Always need E for public key operations */
  29325. if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
  29326. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  29327. #if defined(MBEDTLS_RSA_NO_CRT)
  29328. /* For private key operations, use D or DP & DQ
  29329. * as (unblinded) exponents. */
  29330. if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
  29331. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  29332. #else
  29333. if( is_priv &&
  29334. ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
  29335. mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
  29336. {
  29337. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  29338. }
  29339. #endif /* MBEDTLS_RSA_NO_CRT */
  29340. /* Blinding shouldn't make exponents negative either,
  29341. * so check that P, Q >= 1 if that hasn't yet been
  29342. * done as part of 1. */
  29343. #if defined(MBEDTLS_RSA_NO_CRT)
  29344. if( is_priv && blinding_needed &&
  29345. ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
  29346. mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
  29347. {
  29348. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  29349. }
  29350. #endif
  29351. /* It wouldn't lead to an error if it wasn't satisfied,
  29352. * but check for QP >= 1 nonetheless. */
  29353. #if !defined(MBEDTLS_RSA_NO_CRT)
  29354. if( is_priv &&
  29355. mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
  29356. {
  29357. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  29358. }
  29359. #endif
  29360. return( 0 );
  29361. }
  29362. int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
  29363. {
  29364. int ret = 0;
  29365. const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
  29366. const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
  29367. const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
  29368. const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
  29369. const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
  29370. /*
  29371. * Check whether provided parameters are enough
  29372. * to deduce all others. The following incomplete
  29373. * parameter sets for private keys are supported:
  29374. *
  29375. * (1) P, Q missing.
  29376. * (2) D and potentially N missing.
  29377. *
  29378. */
  29379. const int n_missing = have_P && have_Q && have_D && have_E;
  29380. const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
  29381. const int d_missing = have_P && have_Q && !have_D && have_E;
  29382. const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
  29383. /* These three alternatives are mutually exclusive */
  29384. const int is_priv = n_missing || pq_missing || d_missing;
  29385. if( !is_priv && !is_pub )
  29386. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  29387. /*
  29388. * Step 1: Deduce N if P, Q are provided.
  29389. */
  29390. if( !have_N && have_P && have_Q )
  29391. {
  29392. if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
  29393. &ctx->Q ) ) != 0 )
  29394. {
  29395. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
  29396. }
  29397. ctx->len = mbedtls_mpi_size( &ctx->N );
  29398. }
  29399. /*
  29400. * Step 2: Deduce and verify all remaining core parameters.
  29401. */
  29402. if( pq_missing )
  29403. {
  29404. ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
  29405. &ctx->P, &ctx->Q );
  29406. if( ret != 0 )
  29407. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
  29408. }
  29409. else if( d_missing )
  29410. {
  29411. if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
  29412. &ctx->Q,
  29413. &ctx->E,
  29414. &ctx->D ) ) != 0 )
  29415. {
  29416. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
  29417. }
  29418. }
  29419. /*
  29420. * Step 3: Deduce all additional parameters specific
  29421. * to our current RSA implementation.
  29422. */
  29423. #if !defined(MBEDTLS_RSA_NO_CRT)
  29424. if( is_priv )
  29425. {
  29426. ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
  29427. &ctx->DP, &ctx->DQ, &ctx->QP );
  29428. if( ret != 0 )
  29429. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
  29430. }
  29431. #endif /* MBEDTLS_RSA_NO_CRT */
  29432. /*
  29433. * Step 3: Basic sanity checks
  29434. */
  29435. return( rsa_check_context( ctx, is_priv, 1 ) );
  29436. }
  29437. int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
  29438. unsigned char *N, size_t N_len,
  29439. unsigned char *P, size_t P_len,
  29440. unsigned char *Q, size_t Q_len,
  29441. unsigned char *D, size_t D_len,
  29442. unsigned char *E, size_t E_len )
  29443. {
  29444. int ret = 0;
  29445. /* Check if key is private or public */
  29446. const int is_priv =
  29447. mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
  29448. mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
  29449. mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
  29450. mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
  29451. mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
  29452. if( !is_priv )
  29453. {
  29454. /* If we're trying to export private parameters for a public key,
  29455. * something must be wrong. */
  29456. if( P != NULL || Q != NULL || D != NULL )
  29457. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  29458. }
  29459. if( N != NULL )
  29460. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
  29461. if( P != NULL )
  29462. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
  29463. if( Q != NULL )
  29464. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
  29465. if( D != NULL )
  29466. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
  29467. if( E != NULL )
  29468. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
  29469. cleanup:
  29470. return( ret );
  29471. }
  29472. int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
  29473. mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
  29474. mbedtls_mpi *D, mbedtls_mpi *E )
  29475. {
  29476. int ret;
  29477. /* Check if key is private or public */
  29478. int is_priv =
  29479. mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
  29480. mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
  29481. mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
  29482. mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
  29483. mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
  29484. if( !is_priv )
  29485. {
  29486. /* If we're trying to export private parameters for a public key,
  29487. * something must be wrong. */
  29488. if( P != NULL || Q != NULL || D != NULL )
  29489. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  29490. }
  29491. /* Export all requested core parameters. */
  29492. if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
  29493. ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
  29494. ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
  29495. ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
  29496. ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
  29497. {
  29498. return( ret );
  29499. }
  29500. return( 0 );
  29501. }
  29502. /*
  29503. * Export CRT parameters
  29504. * This must also be implemented if CRT is not used, for being able to
  29505. * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
  29506. * can be used in this case.
  29507. */
  29508. int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
  29509. mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
  29510. {
  29511. int ret;
  29512. /* Check if key is private or public */
  29513. int is_priv =
  29514. mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
  29515. mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
  29516. mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
  29517. mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
  29518. mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
  29519. if( !is_priv )
  29520. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  29521. #if !defined(MBEDTLS_RSA_NO_CRT)
  29522. /* Export all requested blinding parameters. */
  29523. if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
  29524. ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
  29525. ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
  29526. {
  29527. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
  29528. }
  29529. #else
  29530. if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
  29531. DP, DQ, QP ) ) != 0 )
  29532. {
  29533. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
  29534. }
  29535. #endif
  29536. return( 0 );
  29537. }
  29538. /*
  29539. * Initialize an RSA context
  29540. */
  29541. void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
  29542. int padding,
  29543. int hash_id )
  29544. {
  29545. memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
  29546. mbedtls_rsa_set_padding( ctx, padding, hash_id );
  29547. #if defined(MBEDTLS_THREADING_C)
  29548. mbedtls_mutex_init( &ctx->mutex );
  29549. #endif
  29550. }
  29551. /*
  29552. * Set padding for an existing RSA context
  29553. */
  29554. void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
  29555. {
  29556. ctx->padding = padding;
  29557. ctx->hash_id = hash_id;
  29558. }
  29559. /*
  29560. * Get length in bytes of RSA modulus
  29561. */
  29562. size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
  29563. {
  29564. return( ctx->len );
  29565. }
  29566. #if defined(MBEDTLS_GENPRIME)
  29567. /*
  29568. * Generate an RSA keypair
  29569. */
  29570. int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
  29571. int (*f_rng)(void *, unsigned char *, size_t),
  29572. void *p_rng,
  29573. unsigned int nbits, int exponent )
  29574. {
  29575. int ret;
  29576. mbedtls_mpi H, G;
  29577. if( f_rng == NULL || nbits < 128 || exponent < 3 )
  29578. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  29579. if( nbits % 2 )
  29580. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  29581. mbedtls_mpi_init( &H );
  29582. mbedtls_mpi_init( &G );
  29583. /*
  29584. * find primes P and Q with Q < P so that:
  29585. * GCD( E, (P-1)*(Q-1) ) == 1
  29586. */
  29587. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
  29588. do
  29589. {
  29590. MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
  29591. f_rng, p_rng ) );
  29592. MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
  29593. f_rng, p_rng ) );
  29594. if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
  29595. continue;
  29596. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
  29597. if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
  29598. continue;
  29599. if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
  29600. mbedtls_mpi_swap( &ctx->P, &ctx->Q );
  29601. /* Temporarily replace P,Q by P-1, Q-1 */
  29602. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
  29603. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
  29604. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
  29605. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
  29606. }
  29607. while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
  29608. /* Restore P,Q */
  29609. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
  29610. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
  29611. ctx->len = mbedtls_mpi_size( &ctx->N );
  29612. /*
  29613. * D = E^-1 mod ((P-1)*(Q-1))
  29614. * DP = D mod (P - 1)
  29615. * DQ = D mod (Q - 1)
  29616. * QP = Q^-1 mod P
  29617. */
  29618. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
  29619. #if !defined(MBEDTLS_RSA_NO_CRT)
  29620. MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
  29621. &ctx->DP, &ctx->DQ, &ctx->QP ) );
  29622. #endif /* MBEDTLS_RSA_NO_CRT */
  29623. /* Double-check */
  29624. MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
  29625. cleanup:
  29626. mbedtls_mpi_free( &H );
  29627. mbedtls_mpi_free( &G );
  29628. if( ret != 0 )
  29629. {
  29630. mbedtls_rsa_free( ctx );
  29631. return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
  29632. }
  29633. return( 0 );
  29634. }
  29635. #endif /* MBEDTLS_GENPRIME */
  29636. /*
  29637. * Check a public RSA key
  29638. */
  29639. int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
  29640. {
  29641. if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
  29642. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  29643. if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
  29644. {
  29645. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  29646. }
  29647. if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
  29648. mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
  29649. mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
  29650. {
  29651. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  29652. }
  29653. return( 0 );
  29654. }
  29655. /*
  29656. * Check for the consistency of all fields in an RSA private key context
  29657. */
  29658. int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
  29659. {
  29660. if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
  29661. rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
  29662. {
  29663. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  29664. }
  29665. if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
  29666. &ctx->D, &ctx->E, NULL, NULL ) != 0 )
  29667. {
  29668. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  29669. }
  29670. #if !defined(MBEDTLS_RSA_NO_CRT)
  29671. else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
  29672. &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
  29673. {
  29674. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  29675. }
  29676. #endif
  29677. return( 0 );
  29678. }
  29679. /*
  29680. * Check if contexts holding a public and private key match
  29681. */
  29682. int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
  29683. const mbedtls_rsa_context *prv )
  29684. {
  29685. if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
  29686. mbedtls_rsa_check_privkey( prv ) != 0 )
  29687. {
  29688. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  29689. }
  29690. if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
  29691. mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
  29692. {
  29693. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  29694. }
  29695. return( 0 );
  29696. }
  29697. /*
  29698. * Do an RSA public key operation
  29699. */
  29700. int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
  29701. const unsigned char *input,
  29702. unsigned char *output )
  29703. {
  29704. int ret;
  29705. size_t olen;
  29706. mbedtls_mpi T;
  29707. if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
  29708. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  29709. mbedtls_mpi_init( &T );
  29710. #if defined(MBEDTLS_THREADING_C)
  29711. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  29712. return( ret );
  29713. #endif
  29714. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
  29715. if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
  29716. {
  29717. ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  29718. goto cleanup;
  29719. }
  29720. olen = ctx->len;
  29721. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
  29722. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
  29723. cleanup:
  29724. #if defined(MBEDTLS_THREADING_C)
  29725. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  29726. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  29727. #endif
  29728. mbedtls_mpi_free( &T );
  29729. if( ret != 0 )
  29730. return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
  29731. return( 0 );
  29732. }
  29733. /*
  29734. * Generate or update blinding values, see section 10 of:
  29735. * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
  29736. * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
  29737. * Berlin Heidelberg, 1996. p. 104-113.
  29738. */
  29739. static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
  29740. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  29741. {
  29742. int ret, count = 0;
  29743. if( ctx->Vf.p != NULL )
  29744. {
  29745. /* We already have blinding values, just update them by squaring */
  29746. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
  29747. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
  29748. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
  29749. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
  29750. goto cleanup;
  29751. }
  29752. /* Unblinding value: Vf = random number, invertible mod N */
  29753. do {
  29754. if( count++ > 10 )
  29755. return( MBEDTLS_ERR_RSA_RNG_FAILED );
  29756. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
  29757. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
  29758. } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
  29759. /* Blinding value: Vi = Vf^(-e) mod N */
  29760. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
  29761. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN ) );
  29762. cleanup:
  29763. return( ret );
  29764. }
  29765. /*
  29766. * Exponent blinding supposed to prevent side-channel attacks using multiple
  29767. * traces of measurements to recover the RSA key. The more collisions are there,
  29768. * the more bits of the key can be recovered. See [3].
  29769. *
  29770. * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
  29771. * observations on avarage.
  29772. *
  29773. * For example with 28 byte blinding to achieve 2 collisions the adversary has
  29774. * to make 2^112 observations on avarage.
  29775. *
  29776. * (With the currently (as of 2017 April) known best algorithms breaking 2048
  29777. * bit RSA requires approximately as much time as trying out 2^112 random keys.
  29778. * Thus in this sense with 28 byte blinding the security is not reduced by
  29779. * side-channel attacks like the one in [3])
  29780. *
  29781. * This countermeasure does not help if the key recovery is possible with a
  29782. * single trace.
  29783. */
  29784. #define RSA_EXPONENT_BLINDING 28
  29785. /*
  29786. * Do an RSA private key operation
  29787. */
  29788. int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
  29789. int (*f_rng)(void *, unsigned char *, size_t),
  29790. void *p_rng,
  29791. const unsigned char *input,
  29792. unsigned char *output )
  29793. {
  29794. int ret;
  29795. size_t olen;
  29796. /* Temporary holding the result */
  29797. mbedtls_mpi T;
  29798. /* Temporaries holding P-1, Q-1 and the
  29799. * exponent blinding factor, respectively. */
  29800. mbedtls_mpi P1, Q1, R;
  29801. #if !defined(MBEDTLS_RSA_NO_CRT)
  29802. /* Temporaries holding the results mod p resp. mod q. */
  29803. mbedtls_mpi TP, TQ;
  29804. /* Temporaries holding the blinded exponents for
  29805. * the mod p resp. mod q computation (if used). */
  29806. mbedtls_mpi DP_blind, DQ_blind;
  29807. /* Pointers to actual exponents to be used - either the unblinded
  29808. * or the blinded ones, depending on the presence of a PRNG. */
  29809. mbedtls_mpi *DP = &ctx->DP;
  29810. mbedtls_mpi *DQ = &ctx->DQ;
  29811. #else
  29812. /* Temporary holding the blinded exponent (if used). */
  29813. mbedtls_mpi D_blind;
  29814. /* Pointer to actual exponent to be used - either the unblinded
  29815. * or the blinded one, depending on the presence of a PRNG. */
  29816. mbedtls_mpi *D = &ctx->D;
  29817. #endif /* MBEDTLS_RSA_NO_CRT */
  29818. /* Temporaries holding the initial input and the double
  29819. * checked result; should be the same in the end. */
  29820. mbedtls_mpi I, C;
  29821. if( rsa_check_context( ctx, 1 /* private key checks */,
  29822. f_rng != NULL /* blinding y/n */ ) != 0 )
  29823. {
  29824. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  29825. }
  29826. #if defined(MBEDTLS_THREADING_C)
  29827. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  29828. return( ret );
  29829. #endif
  29830. /* MPI Initialization */
  29831. mbedtls_mpi_init( &T );
  29832. mbedtls_mpi_init( &P1 );
  29833. mbedtls_mpi_init( &Q1 );
  29834. mbedtls_mpi_init( &R );
  29835. if( f_rng != NULL )
  29836. {
  29837. #if defined(MBEDTLS_RSA_NO_CRT)
  29838. mbedtls_mpi_init( &D_blind );
  29839. #else
  29840. mbedtls_mpi_init( &DP_blind );
  29841. mbedtls_mpi_init( &DQ_blind );
  29842. #endif
  29843. }
  29844. #if !defined(MBEDTLS_RSA_NO_CRT)
  29845. mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
  29846. #endif
  29847. mbedtls_mpi_init( &I );
  29848. mbedtls_mpi_init( &C );
  29849. /* End of MPI initialization */
  29850. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
  29851. if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
  29852. {
  29853. ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  29854. goto cleanup;
  29855. }
  29856. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
  29857. if( f_rng != NULL )
  29858. {
  29859. /*
  29860. * Blinding
  29861. * T = T * Vi mod N
  29862. */
  29863. MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
  29864. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
  29865. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
  29866. /*
  29867. * Exponent blinding
  29868. */
  29869. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
  29870. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
  29871. #if defined(MBEDTLS_RSA_NO_CRT)
  29872. /*
  29873. * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
  29874. */
  29875. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
  29876. f_rng, p_rng ) );
  29877. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
  29878. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
  29879. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
  29880. D = &D_blind;
  29881. #else
  29882. /*
  29883. * DP_blind = ( P - 1 ) * R + DP
  29884. */
  29885. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
  29886. f_rng, p_rng ) );
  29887. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
  29888. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
  29889. &ctx->DP ) );
  29890. DP = &DP_blind;
  29891. /*
  29892. * DQ_blind = ( Q - 1 ) * R + DQ
  29893. */
  29894. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
  29895. f_rng, p_rng ) );
  29896. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
  29897. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
  29898. &ctx->DQ ) );
  29899. DQ = &DQ_blind;
  29900. #endif /* MBEDTLS_RSA_NO_CRT */
  29901. }
  29902. #if defined(MBEDTLS_RSA_NO_CRT)
  29903. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
  29904. #else
  29905. /*
  29906. * Faster decryption using the CRT
  29907. *
  29908. * TP = input ^ dP mod P
  29909. * TQ = input ^ dQ mod Q
  29910. */
  29911. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
  29912. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
  29913. /*
  29914. * T = (TP - TQ) * (Q^-1 mod P) mod P
  29915. */
  29916. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
  29917. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
  29918. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
  29919. /*
  29920. * T = TQ + T * Q
  29921. */
  29922. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
  29923. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
  29924. #endif /* MBEDTLS_RSA_NO_CRT */
  29925. if( f_rng != NULL )
  29926. {
  29927. /*
  29928. * Unblind
  29929. * T = T * Vf mod N
  29930. */
  29931. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
  29932. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
  29933. }
  29934. /* Verify the result to prevent glitching attacks. */
  29935. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
  29936. &ctx->N, &ctx->RN ) );
  29937. if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
  29938. {
  29939. ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
  29940. goto cleanup;
  29941. }
  29942. olen = ctx->len;
  29943. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
  29944. cleanup:
  29945. #if defined(MBEDTLS_THREADING_C)
  29946. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  29947. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  29948. #endif
  29949. mbedtls_mpi_free( &P1 );
  29950. mbedtls_mpi_free( &Q1 );
  29951. mbedtls_mpi_free( &R );
  29952. if( f_rng != NULL )
  29953. {
  29954. #if defined(MBEDTLS_RSA_NO_CRT)
  29955. mbedtls_mpi_free( &D_blind );
  29956. #else
  29957. mbedtls_mpi_free( &DP_blind );
  29958. mbedtls_mpi_free( &DQ_blind );
  29959. #endif
  29960. }
  29961. mbedtls_mpi_free( &T );
  29962. #if !defined(MBEDTLS_RSA_NO_CRT)
  29963. mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
  29964. #endif
  29965. mbedtls_mpi_free( &C );
  29966. mbedtls_mpi_free( &I );
  29967. if( ret != 0 )
  29968. return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
  29969. return( 0 );
  29970. }
  29971. #if defined(MBEDTLS_PKCS1_V21)
  29972. /**
  29973. * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
  29974. *
  29975. * \param dst buffer to mask
  29976. * \param dlen length of destination buffer
  29977. * \param src source of the mask generation
  29978. * \param slen length of the source buffer
  29979. * \param md_ctx message digest context to use
  29980. */
  29981. static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
  29982. size_t slen, mbedtls_md_context_t *md_ctx )
  29983. {
  29984. unsigned char mask[MBEDTLS_MD_MAX_SIZE];
  29985. unsigned char counter[4];
  29986. unsigned char *p;
  29987. unsigned int hlen;
  29988. size_t i, use_len;
  29989. int ret = 0;
  29990. memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
  29991. memset( counter, 0, 4 );
  29992. hlen = mbedtls_md_get_size( md_ctx->md_info );
  29993. /* Generate and apply dbMask */
  29994. p = dst;
  29995. while( dlen > 0 )
  29996. {
  29997. use_len = hlen;
  29998. if( dlen < hlen )
  29999. use_len = dlen;
  30000. if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
  30001. goto exit;
  30002. if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
  30003. goto exit;
  30004. if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
  30005. goto exit;
  30006. if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
  30007. goto exit;
  30008. for( i = 0; i < use_len; ++i )
  30009. *p++ ^= mask[i];
  30010. counter[3]++;
  30011. dlen -= use_len;
  30012. }
  30013. exit:
  30014. mbedtls_zeroize( mask, sizeof( mask ) );
  30015. return( ret );
  30016. }
  30017. #endif /* MBEDTLS_PKCS1_V21 */
  30018. #if defined(MBEDTLS_PKCS1_V21)
  30019. /*
  30020. * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
  30021. */
  30022. int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
  30023. int (*f_rng)(void *, unsigned char *, size_t),
  30024. void *p_rng,
  30025. int mode,
  30026. const unsigned char *label, size_t label_len,
  30027. size_t ilen,
  30028. const unsigned char *input,
  30029. unsigned char *output )
  30030. {
  30031. size_t olen;
  30032. int ret;
  30033. unsigned char *p = output;
  30034. unsigned int hlen;
  30035. const mbedtls_md_info_t *md_info;
  30036. mbedtls_md_context_t md_ctx;
  30037. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
  30038. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30039. if( f_rng == NULL )
  30040. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30041. md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
  30042. if( md_info == NULL )
  30043. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30044. olen = ctx->len;
  30045. hlen = mbedtls_md_get_size( md_info );
  30046. /* first comparison checks for overflow */
  30047. if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
  30048. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30049. memset( output, 0, olen );
  30050. *p++ = 0;
  30051. /* Generate a random octet string seed */
  30052. if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
  30053. return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
  30054. p += hlen;
  30055. /* Construct DB */
  30056. if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
  30057. return( ret );
  30058. p += hlen;
  30059. p += olen - 2 * hlen - 2 - ilen;
  30060. *p++ = 1;
  30061. memcpy( p, input, ilen );
  30062. mbedtls_md_init( &md_ctx );
  30063. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
  30064. goto exit;
  30065. /* maskedDB: Apply dbMask to DB */
  30066. if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
  30067. &md_ctx ) ) != 0 )
  30068. goto exit;
  30069. /* maskedSeed: Apply seedMask to seed */
  30070. if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
  30071. &md_ctx ) ) != 0 )
  30072. goto exit;
  30073. exit:
  30074. mbedtls_md_free( &md_ctx );
  30075. if( ret != 0 )
  30076. return( ret );
  30077. return( ( mode == MBEDTLS_RSA_PUBLIC )
  30078. ? mbedtls_rsa_public( ctx, output, output )
  30079. : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
  30080. }
  30081. #endif /* MBEDTLS_PKCS1_V21 */
  30082. #if defined(MBEDTLS_PKCS1_V15)
  30083. /*
  30084. * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
  30085. */
  30086. int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
  30087. int (*f_rng)(void *, unsigned char *, size_t),
  30088. void *p_rng,
  30089. int mode, size_t ilen,
  30090. const unsigned char *input,
  30091. unsigned char *output )
  30092. {
  30093. size_t nb_pad, olen;
  30094. int ret;
  30095. unsigned char *p = output;
  30096. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
  30097. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30098. // We don't check p_rng because it won't be dereferenced here
  30099. if( f_rng == NULL || input == NULL || output == NULL )
  30100. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30101. olen = ctx->len;
  30102. /* first comparison checks for overflow */
  30103. if( ilen + 11 < ilen || olen < ilen + 11 )
  30104. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30105. nb_pad = olen - 3 - ilen;
  30106. *p++ = 0;
  30107. if( mode == MBEDTLS_RSA_PUBLIC )
  30108. {
  30109. *p++ = MBEDTLS_RSA_CRYPT;
  30110. while( nb_pad-- > 0 )
  30111. {
  30112. int rng_dl = 100;
  30113. do {
  30114. ret = f_rng( p_rng, p, 1 );
  30115. } while( *p == 0 && --rng_dl && ret == 0 );
  30116. /* Check if RNG failed to generate data */
  30117. if( rng_dl == 0 || ret != 0 )
  30118. return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
  30119. p++;
  30120. }
  30121. }
  30122. else
  30123. {
  30124. *p++ = MBEDTLS_RSA_SIGN;
  30125. while( nb_pad-- > 0 )
  30126. *p++ = 0xFF;
  30127. }
  30128. *p++ = 0;
  30129. memcpy( p, input, ilen );
  30130. return( ( mode == MBEDTLS_RSA_PUBLIC )
  30131. ? mbedtls_rsa_public( ctx, output, output )
  30132. : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
  30133. }
  30134. #endif /* MBEDTLS_PKCS1_V15 */
  30135. /*
  30136. * Add the message padding, then do an RSA operation
  30137. */
  30138. int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
  30139. int (*f_rng)(void *, unsigned char *, size_t),
  30140. void *p_rng,
  30141. int mode, size_t ilen,
  30142. const unsigned char *input,
  30143. unsigned char *output )
  30144. {
  30145. switch( ctx->padding )
  30146. {
  30147. #if defined(MBEDTLS_PKCS1_V15)
  30148. case MBEDTLS_RSA_PKCS_V15:
  30149. return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
  30150. input, output );
  30151. #endif
  30152. #if defined(MBEDTLS_PKCS1_V21)
  30153. case MBEDTLS_RSA_PKCS_V21:
  30154. return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
  30155. ilen, input, output );
  30156. #endif
  30157. default:
  30158. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  30159. }
  30160. }
  30161. #if defined(MBEDTLS_PKCS1_V21)
  30162. /*
  30163. * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
  30164. */
  30165. int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
  30166. int (*f_rng)(void *, unsigned char *, size_t),
  30167. void *p_rng,
  30168. int mode,
  30169. const unsigned char *label, size_t label_len,
  30170. size_t *olen,
  30171. const unsigned char *input,
  30172. unsigned char *output,
  30173. size_t output_max_len )
  30174. {
  30175. int ret;
  30176. size_t ilen, i, pad_len;
  30177. unsigned char *p, bad, pad_done;
  30178. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  30179. unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
  30180. unsigned int hlen;
  30181. const mbedtls_md_info_t *md_info;
  30182. mbedtls_md_context_t md_ctx;
  30183. /*
  30184. * Parameters sanity checks
  30185. */
  30186. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
  30187. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30188. ilen = ctx->len;
  30189. if( ilen < 16 || ilen > sizeof( buf ) )
  30190. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30191. md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
  30192. if( md_info == NULL )
  30193. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30194. hlen = mbedtls_md_get_size( md_info );
  30195. // checking for integer underflow
  30196. if( 2 * hlen + 2 > ilen )
  30197. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30198. /*
  30199. * RSA operation
  30200. */
  30201. ret = ( mode == MBEDTLS_RSA_PUBLIC )
  30202. ? mbedtls_rsa_public( ctx, input, buf )
  30203. : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
  30204. if( ret != 0 )
  30205. goto cleanup;
  30206. /*
  30207. * Unmask data and generate lHash
  30208. */
  30209. mbedtls_md_init( &md_ctx );
  30210. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
  30211. {
  30212. mbedtls_md_free( &md_ctx );
  30213. goto cleanup;
  30214. }
  30215. /* seed: Apply seedMask to maskedSeed */
  30216. if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
  30217. &md_ctx ) ) != 0 ||
  30218. /* DB: Apply dbMask to maskedDB */
  30219. ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
  30220. &md_ctx ) ) != 0 )
  30221. {
  30222. mbedtls_md_free( &md_ctx );
  30223. goto cleanup;
  30224. }
  30225. mbedtls_md_free( &md_ctx );
  30226. /* Generate lHash */
  30227. if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
  30228. goto cleanup;
  30229. /*
  30230. * Check contents, in "constant-time"
  30231. */
  30232. p = buf;
  30233. bad = 0;
  30234. bad |= *p++; /* First byte must be 0 */
  30235. p += hlen; /* Skip seed */
  30236. /* Check lHash */
  30237. for( i = 0; i < hlen; i++ )
  30238. bad |= lhash[i] ^ *p++;
  30239. /* Get zero-padding len, but always read till end of buffer
  30240. * (minus one, for the 01 byte) */
  30241. pad_len = 0;
  30242. pad_done = 0;
  30243. for( i = 0; i < ilen - 2 * hlen - 2; i++ )
  30244. {
  30245. pad_done |= p[i];
  30246. pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
  30247. }
  30248. p += pad_len;
  30249. bad |= *p++ ^ 0x01;
  30250. /*
  30251. * The only information "leaked" is whether the padding was correct or not
  30252. * (eg, no data is copied if it was not correct). This meets the
  30253. * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
  30254. * the different error conditions.
  30255. */
  30256. if( bad != 0 )
  30257. {
  30258. ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
  30259. goto cleanup;
  30260. }
  30261. if( ilen - ( p - buf ) > output_max_len )
  30262. {
  30263. ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
  30264. goto cleanup;
  30265. }
  30266. *olen = ilen - (p - buf);
  30267. memcpy( output, p, *olen );
  30268. ret = 0;
  30269. cleanup:
  30270. mbedtls_zeroize( buf, sizeof( buf ) );
  30271. mbedtls_zeroize( lhash, sizeof( lhash ) );
  30272. return( ret );
  30273. }
  30274. #endif /* MBEDTLS_PKCS1_V21 */
  30275. #if defined(MBEDTLS_PKCS1_V15)
  30276. /*
  30277. * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
  30278. */
  30279. int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
  30280. int (*f_rng)(void *, unsigned char *, size_t),
  30281. void *p_rng,
  30282. int mode, size_t *olen,
  30283. const unsigned char *input,
  30284. unsigned char *output,
  30285. size_t output_max_len)
  30286. {
  30287. int ret;
  30288. size_t ilen, pad_count = 0, i;
  30289. unsigned char *p, bad, pad_done = 0;
  30290. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  30291. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
  30292. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30293. ilen = ctx->len;
  30294. if( ilen < 16 || ilen > sizeof( buf ) )
  30295. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30296. ret = ( mode == MBEDTLS_RSA_PUBLIC )
  30297. ? mbedtls_rsa_public( ctx, input, buf )
  30298. : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
  30299. if( ret != 0 )
  30300. goto cleanup;
  30301. p = buf;
  30302. bad = 0;
  30303. /*
  30304. * Check and get padding len in "constant-time"
  30305. */
  30306. bad |= *p++; /* First byte must be 0 */
  30307. /* This test does not depend on secret data */
  30308. if( mode == MBEDTLS_RSA_PRIVATE )
  30309. {
  30310. bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
  30311. /* Get padding len, but always read till end of buffer
  30312. * (minus one, for the 00 byte) */
  30313. for( i = 0; i < ilen - 3; i++ )
  30314. {
  30315. pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
  30316. pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
  30317. }
  30318. p += pad_count;
  30319. bad |= *p++; /* Must be zero */
  30320. }
  30321. else
  30322. {
  30323. bad |= *p++ ^ MBEDTLS_RSA_SIGN;
  30324. /* Get padding len, but always read till end of buffer
  30325. * (minus one, for the 00 byte) */
  30326. for( i = 0; i < ilen - 3; i++ )
  30327. {
  30328. pad_done |= ( p[i] != 0xFF );
  30329. pad_count += ( pad_done == 0 );
  30330. }
  30331. p += pad_count;
  30332. bad |= *p++; /* Must be zero */
  30333. }
  30334. bad |= ( pad_count < 8 );
  30335. if( bad )
  30336. {
  30337. ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
  30338. goto cleanup;
  30339. }
  30340. if( ilen - ( p - buf ) > output_max_len )
  30341. {
  30342. ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
  30343. goto cleanup;
  30344. }
  30345. *olen = ilen - (p - buf);
  30346. memcpy( output, p, *olen );
  30347. ret = 0;
  30348. cleanup:
  30349. mbedtls_zeroize( buf, sizeof( buf ) );
  30350. return( ret );
  30351. }
  30352. #endif /* MBEDTLS_PKCS1_V15 */
  30353. /*
  30354. * Do an RSA operation, then remove the message padding
  30355. */
  30356. int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
  30357. int (*f_rng)(void *, unsigned char *, size_t),
  30358. void *p_rng,
  30359. int mode, size_t *olen,
  30360. const unsigned char *input,
  30361. unsigned char *output,
  30362. size_t output_max_len)
  30363. {
  30364. switch( ctx->padding )
  30365. {
  30366. #if defined(MBEDTLS_PKCS1_V15)
  30367. case MBEDTLS_RSA_PKCS_V15:
  30368. return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
  30369. input, output, output_max_len );
  30370. #endif
  30371. #if defined(MBEDTLS_PKCS1_V21)
  30372. case MBEDTLS_RSA_PKCS_V21:
  30373. return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
  30374. olen, input, output,
  30375. output_max_len );
  30376. #endif
  30377. default:
  30378. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  30379. }
  30380. }
  30381. #if defined(MBEDTLS_PKCS1_V21)
  30382. /*
  30383. * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
  30384. */
  30385. int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
  30386. int (*f_rng)(void *, unsigned char *, size_t),
  30387. void *p_rng,
  30388. int mode,
  30389. mbedtls_md_type_t md_alg,
  30390. unsigned int hashlen,
  30391. const unsigned char *hash,
  30392. unsigned char *sig )
  30393. {
  30394. size_t olen;
  30395. unsigned char *p = sig;
  30396. unsigned char salt[MBEDTLS_MD_MAX_SIZE];
  30397. unsigned int slen, hlen, offset = 0;
  30398. int ret;
  30399. size_t msb;
  30400. const mbedtls_md_info_t *md_info;
  30401. mbedtls_md_context_t md_ctx;
  30402. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
  30403. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30404. if( f_rng == NULL )
  30405. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30406. olen = ctx->len;
  30407. if( md_alg != MBEDTLS_MD_NONE )
  30408. {
  30409. /* Gather length of hash to sign */
  30410. md_info = mbedtls_md_info_from_type( md_alg );
  30411. if( md_info == NULL )
  30412. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30413. hashlen = mbedtls_md_get_size( md_info );
  30414. }
  30415. md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
  30416. if( md_info == NULL )
  30417. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30418. hlen = mbedtls_md_get_size( md_info );
  30419. slen = hlen;
  30420. if( olen < hlen + slen + 2 )
  30421. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30422. memset( sig, 0, olen );
  30423. /* Generate salt of length slen */
  30424. if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
  30425. return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
  30426. /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
  30427. msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
  30428. p += olen - hlen * 2 - 2;
  30429. *p++ = 0x01;
  30430. memcpy( p, salt, slen );
  30431. p += slen;
  30432. mbedtls_md_init( &md_ctx );
  30433. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
  30434. goto exit;
  30435. /* Generate H = Hash( M' ) */
  30436. if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
  30437. goto exit;
  30438. if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
  30439. goto exit;
  30440. if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
  30441. goto exit;
  30442. if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
  30443. goto exit;
  30444. if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
  30445. goto exit;
  30446. /* Compensate for boundary condition when applying mask */
  30447. if( msb % 8 == 0 )
  30448. offset = 1;
  30449. /* maskedDB: Apply dbMask to DB */
  30450. if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
  30451. &md_ctx ) ) != 0 )
  30452. goto exit;
  30453. msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
  30454. sig[0] &= 0xFF >> ( olen * 8 - msb );
  30455. p += hlen;
  30456. *p++ = 0xBC;
  30457. mbedtls_zeroize( salt, sizeof( salt ) );
  30458. exit:
  30459. mbedtls_md_free( &md_ctx );
  30460. if( ret != 0 )
  30461. return( ret );
  30462. return( ( mode == MBEDTLS_RSA_PUBLIC )
  30463. ? mbedtls_rsa_public( ctx, sig, sig )
  30464. : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
  30465. }
  30466. #endif /* MBEDTLS_PKCS1_V21 */
  30467. #if defined(MBEDTLS_PKCS1_V15)
  30468. /*
  30469. * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
  30470. */
  30471. /* Construct a PKCS v1.5 encoding of a hashed message
  30472. *
  30473. * This is used both for signature generation and verification.
  30474. *
  30475. * Parameters:
  30476. * - md_alg: Identifies the hash algorithm used to generate the given hash;
  30477. * MBEDTLS_MD_NONE if raw data is signed.
  30478. * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
  30479. * - hash: Buffer containing the hashed message or the raw data.
  30480. * - dst_len: Length of the encoded message.
  30481. * - dst: Buffer to hold the encoded message.
  30482. *
  30483. * Assumptions:
  30484. * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
  30485. * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
  30486. * - dst points to a buffer of size at least dst_len.
  30487. *
  30488. */
  30489. static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
  30490. unsigned int hashlen,
  30491. const unsigned char *hash,
  30492. size_t dst_len,
  30493. unsigned char *dst )
  30494. {
  30495. size_t oid_size = 0;
  30496. size_t nb_pad = dst_len;
  30497. unsigned char *p = dst;
  30498. const char *oid = NULL;
  30499. /* Are we signing hashed or raw data? */
  30500. if( md_alg != MBEDTLS_MD_NONE )
  30501. {
  30502. const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
  30503. if( md_info == NULL )
  30504. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30505. if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
  30506. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30507. hashlen = mbedtls_md_get_size( md_info );
  30508. /* Double-check that 8 + hashlen + oid_size can be used as a
  30509. * 1-byte ASN.1 length encoding and that there's no overflow. */
  30510. if( 8 + hashlen + oid_size >= 0x80 ||
  30511. 10 + hashlen < hashlen ||
  30512. 10 + hashlen + oid_size < 10 + hashlen )
  30513. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30514. /*
  30515. * Static bounds check:
  30516. * - Need 10 bytes for five tag-length pairs.
  30517. * (Insist on 1-byte length encodings to protect against variants of
  30518. * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
  30519. * - Need hashlen bytes for hash
  30520. * - Need oid_size bytes for hash alg OID.
  30521. */
  30522. if( nb_pad < 10 + hashlen + oid_size )
  30523. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30524. nb_pad -= 10 + hashlen + oid_size;
  30525. }
  30526. else
  30527. {
  30528. if( nb_pad < hashlen )
  30529. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30530. nb_pad -= hashlen;
  30531. }
  30532. /* Need space for signature header and padding delimiter (3 bytes),
  30533. * and 8 bytes for the minimal padding */
  30534. if( nb_pad < 3 + 8 )
  30535. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30536. nb_pad -= 3;
  30537. /* Now nb_pad is the amount of memory to be filled
  30538. * with padding, and at least 8 bytes long. */
  30539. /* Write signature header and padding */
  30540. *p++ = 0;
  30541. *p++ = MBEDTLS_RSA_SIGN;
  30542. memset( p, 0xFF, nb_pad );
  30543. p += nb_pad;
  30544. *p++ = 0;
  30545. /* Are we signing raw data? */
  30546. if( md_alg == MBEDTLS_MD_NONE )
  30547. {
  30548. memcpy( p, hash, hashlen );
  30549. return( 0 );
  30550. }
  30551. /* Signing hashed data, add corresponding ASN.1 structure
  30552. *
  30553. * DigestInfo ::= SEQUENCE {
  30554. * digestAlgorithm DigestAlgorithmIdentifier,
  30555. * digest Digest }
  30556. * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
  30557. * Digest ::= OCTET STRING
  30558. *
  30559. * Schematic:
  30560. * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
  30561. * TAG-NULL + LEN [ NULL ] ]
  30562. * TAG-OCTET + LEN [ HASH ] ]
  30563. */
  30564. *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
  30565. *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
  30566. *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
  30567. *p++ = (unsigned char)( 0x04 + oid_size );
  30568. *p++ = MBEDTLS_ASN1_OID;
  30569. *p++ = (unsigned char) oid_size;
  30570. memcpy( p, oid, oid_size );
  30571. p += oid_size;
  30572. *p++ = MBEDTLS_ASN1_NULL;
  30573. *p++ = 0x00;
  30574. *p++ = MBEDTLS_ASN1_OCTET_STRING;
  30575. *p++ = (unsigned char) hashlen;
  30576. memcpy( p, hash, hashlen );
  30577. p += hashlen;
  30578. /* Just a sanity-check, should be automatic
  30579. * after the initial bounds check. */
  30580. if( p != dst + dst_len )
  30581. {
  30582. mbedtls_zeroize( dst, dst_len );
  30583. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30584. }
  30585. return( 0 );
  30586. }
  30587. /*
  30588. * Do an RSA operation to sign the message digest
  30589. */
  30590. int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
  30591. int (*f_rng)(void *, unsigned char *, size_t),
  30592. void *p_rng,
  30593. int mode,
  30594. mbedtls_md_type_t md_alg,
  30595. unsigned int hashlen,
  30596. const unsigned char *hash,
  30597. unsigned char *sig )
  30598. {
  30599. int ret;
  30600. unsigned char *sig_try = NULL, *verif = NULL;
  30601. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
  30602. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30603. /*
  30604. * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
  30605. */
  30606. if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
  30607. ctx->len, sig ) ) != 0 )
  30608. return( ret );
  30609. /*
  30610. * Call respective RSA primitive
  30611. */
  30612. if( mode == MBEDTLS_RSA_PUBLIC )
  30613. {
  30614. /* Skip verification on a public key operation */
  30615. return( mbedtls_rsa_public( ctx, sig, sig ) );
  30616. }
  30617. /* Private key operation
  30618. *
  30619. * In order to prevent Lenstra's attack, make the signature in a
  30620. * temporary buffer and check it before returning it.
  30621. */
  30622. sig_try = mbedtls_calloc( 1, ctx->len );
  30623. if( sig_try == NULL )
  30624. return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
  30625. verif = mbedtls_calloc( 1, ctx->len );
  30626. if( verif == NULL )
  30627. {
  30628. mbedtls_free( sig_try );
  30629. return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
  30630. }
  30631. MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
  30632. MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
  30633. if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
  30634. {
  30635. ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
  30636. goto cleanup;
  30637. }
  30638. memcpy( sig, sig_try, ctx->len );
  30639. cleanup:
  30640. mbedtls_free( sig_try );
  30641. mbedtls_free( verif );
  30642. return( ret );
  30643. }
  30644. #endif /* MBEDTLS_PKCS1_V15 */
  30645. /*
  30646. * Do an RSA operation to sign the message digest
  30647. */
  30648. int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
  30649. int (*f_rng)(void *, unsigned char *, size_t),
  30650. void *p_rng,
  30651. int mode,
  30652. mbedtls_md_type_t md_alg,
  30653. unsigned int hashlen,
  30654. const unsigned char *hash,
  30655. unsigned char *sig )
  30656. {
  30657. switch( ctx->padding )
  30658. {
  30659. #if defined(MBEDTLS_PKCS1_V15)
  30660. case MBEDTLS_RSA_PKCS_V15:
  30661. return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
  30662. hashlen, hash, sig );
  30663. #endif
  30664. #if defined(MBEDTLS_PKCS1_V21)
  30665. case MBEDTLS_RSA_PKCS_V21:
  30666. return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
  30667. hashlen, hash, sig );
  30668. #endif
  30669. default:
  30670. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  30671. }
  30672. }
  30673. #if defined(MBEDTLS_PKCS1_V21)
  30674. /*
  30675. * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
  30676. */
  30677. int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
  30678. int (*f_rng)(void *, unsigned char *, size_t),
  30679. void *p_rng,
  30680. int mode,
  30681. mbedtls_md_type_t md_alg,
  30682. unsigned int hashlen,
  30683. const unsigned char *hash,
  30684. mbedtls_md_type_t mgf1_hash_id,
  30685. int expected_salt_len,
  30686. const unsigned char *sig )
  30687. {
  30688. int ret;
  30689. size_t siglen;
  30690. unsigned char *p;
  30691. unsigned char *hash_start;
  30692. unsigned char result[MBEDTLS_MD_MAX_SIZE];
  30693. unsigned char zeros[8];
  30694. unsigned int hlen;
  30695. size_t observed_salt_len, msb;
  30696. const mbedtls_md_info_t *md_info;
  30697. mbedtls_md_context_t md_ctx;
  30698. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  30699. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
  30700. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30701. siglen = ctx->len;
  30702. if( siglen < 16 || siglen > sizeof( buf ) )
  30703. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30704. ret = ( mode == MBEDTLS_RSA_PUBLIC )
  30705. ? mbedtls_rsa_public( ctx, sig, buf )
  30706. : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
  30707. if( ret != 0 )
  30708. return( ret );
  30709. p = buf;
  30710. if( buf[siglen - 1] != 0xBC )
  30711. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  30712. if( md_alg != MBEDTLS_MD_NONE )
  30713. {
  30714. /* Gather length of hash to sign */
  30715. md_info = mbedtls_md_info_from_type( md_alg );
  30716. if( md_info == NULL )
  30717. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30718. hashlen = mbedtls_md_get_size( md_info );
  30719. }
  30720. md_info = mbedtls_md_info_from_type( mgf1_hash_id );
  30721. if( md_info == NULL )
  30722. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30723. hlen = mbedtls_md_get_size( md_info );
  30724. memset( zeros, 0, 8 );
  30725. /*
  30726. * Note: EMSA-PSS verification is over the length of N - 1 bits
  30727. */
  30728. msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
  30729. if( buf[0] >> ( 8 - siglen * 8 + msb ) )
  30730. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30731. /* Compensate for boundary condition when applying mask */
  30732. if( msb % 8 == 0 )
  30733. {
  30734. p++;
  30735. siglen -= 1;
  30736. }
  30737. if( siglen < hlen + 2 )
  30738. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30739. hash_start = p + siglen - hlen - 1;
  30740. mbedtls_md_init( &md_ctx );
  30741. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
  30742. goto exit;
  30743. ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
  30744. if( ret != 0 )
  30745. goto exit;
  30746. buf[0] &= 0xFF >> ( siglen * 8 - msb );
  30747. while( p < hash_start - 1 && *p == 0 )
  30748. p++;
  30749. if( *p++ != 0x01 )
  30750. {
  30751. ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
  30752. goto exit;
  30753. }
  30754. observed_salt_len = hash_start - p;
  30755. if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
  30756. observed_salt_len != (size_t) expected_salt_len )
  30757. {
  30758. ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
  30759. goto exit;
  30760. }
  30761. /*
  30762. * Generate H = Hash( M' )
  30763. */
  30764. ret = mbedtls_md_starts( &md_ctx );
  30765. if ( ret != 0 )
  30766. goto exit;
  30767. ret = mbedtls_md_update( &md_ctx, zeros, 8 );
  30768. if ( ret != 0 )
  30769. goto exit;
  30770. ret = mbedtls_md_update( &md_ctx, hash, hashlen );
  30771. if ( ret != 0 )
  30772. goto exit;
  30773. ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
  30774. if ( ret != 0 )
  30775. goto exit;
  30776. ret = mbedtls_md_finish( &md_ctx, result );
  30777. if ( ret != 0 )
  30778. goto exit;
  30779. if( memcmp( hash_start, result, hlen ) != 0 )
  30780. {
  30781. ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
  30782. goto exit;
  30783. }
  30784. exit:
  30785. mbedtls_md_free( &md_ctx );
  30786. return( ret );
  30787. }
  30788. /*
  30789. * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
  30790. */
  30791. int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
  30792. int (*f_rng)(void *, unsigned char *, size_t),
  30793. void *p_rng,
  30794. int mode,
  30795. mbedtls_md_type_t md_alg,
  30796. unsigned int hashlen,
  30797. const unsigned char *hash,
  30798. const unsigned char *sig )
  30799. {
  30800. mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
  30801. ? (mbedtls_md_type_t) ctx->hash_id
  30802. : md_alg;
  30803. return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
  30804. md_alg, hashlen, hash,
  30805. mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
  30806. sig ) );
  30807. }
  30808. #endif /* MBEDTLS_PKCS1_V21 */
  30809. #if defined(MBEDTLS_PKCS1_V15)
  30810. /*
  30811. * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
  30812. */
  30813. int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
  30814. int (*f_rng)(void *, unsigned char *, size_t),
  30815. void *p_rng,
  30816. int mode,
  30817. mbedtls_md_type_t md_alg,
  30818. unsigned int hashlen,
  30819. const unsigned char *hash,
  30820. const unsigned char *sig )
  30821. {
  30822. int ret = 0;
  30823. const size_t sig_len = ctx->len;
  30824. unsigned char *encoded = NULL, *encoded_expected = NULL;
  30825. if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
  30826. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  30827. /*
  30828. * Prepare expected PKCS1 v1.5 encoding of hash.
  30829. */
  30830. if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
  30831. ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
  30832. {
  30833. ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
  30834. goto cleanup;
  30835. }
  30836. if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
  30837. encoded_expected ) ) != 0 )
  30838. goto cleanup;
  30839. /*
  30840. * Apply RSA primitive to get what should be PKCS1 encoded hash.
  30841. */
  30842. ret = ( mode == MBEDTLS_RSA_PUBLIC )
  30843. ? mbedtls_rsa_public( ctx, sig, encoded )
  30844. : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
  30845. if( ret != 0 )
  30846. goto cleanup;
  30847. /*
  30848. * Compare
  30849. */
  30850. if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
  30851. sig_len ) ) != 0 )
  30852. {
  30853. ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
  30854. goto cleanup;
  30855. }
  30856. cleanup:
  30857. if( encoded != NULL )
  30858. {
  30859. mbedtls_zeroize( encoded, sig_len );
  30860. mbedtls_free( encoded );
  30861. }
  30862. if( encoded_expected != NULL )
  30863. {
  30864. mbedtls_zeroize( encoded_expected, sig_len );
  30865. mbedtls_free( encoded_expected );
  30866. }
  30867. return( ret );
  30868. }
  30869. #endif /* MBEDTLS_PKCS1_V15 */
  30870. /*
  30871. * Do an RSA operation and check the message digest
  30872. */
  30873. int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
  30874. int (*f_rng)(void *, unsigned char *, size_t),
  30875. void *p_rng,
  30876. int mode,
  30877. mbedtls_md_type_t md_alg,
  30878. unsigned int hashlen,
  30879. const unsigned char *hash,
  30880. const unsigned char *sig )
  30881. {
  30882. switch( ctx->padding )
  30883. {
  30884. #if defined(MBEDTLS_PKCS1_V15)
  30885. case MBEDTLS_RSA_PKCS_V15:
  30886. return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
  30887. hashlen, hash, sig );
  30888. #endif
  30889. #if defined(MBEDTLS_PKCS1_V21)
  30890. case MBEDTLS_RSA_PKCS_V21:
  30891. return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
  30892. hashlen, hash, sig );
  30893. #endif
  30894. default:
  30895. return( MBEDTLS_ERR_RSA_INVALID_PADDING );
  30896. }
  30897. }
  30898. /*
  30899. * Copy the components of an RSA key
  30900. */
  30901. int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
  30902. {
  30903. int ret;
  30904. dst->ver = src->ver;
  30905. dst->len = src->len;
  30906. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
  30907. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
  30908. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
  30909. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
  30910. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
  30911. #if !defined(MBEDTLS_RSA_NO_CRT)
  30912. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
  30913. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
  30914. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
  30915. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
  30916. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
  30917. #endif
  30918. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
  30919. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
  30920. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
  30921. dst->padding = src->padding;
  30922. dst->hash_id = src->hash_id;
  30923. cleanup:
  30924. if( ret != 0 )
  30925. mbedtls_rsa_free( dst );
  30926. return( ret );
  30927. }
  30928. /*
  30929. * Free the components of an RSA key
  30930. */
  30931. void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
  30932. {
  30933. mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
  30934. mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
  30935. mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
  30936. mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
  30937. #if !defined(MBEDTLS_RSA_NO_CRT)
  30938. mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
  30939. mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
  30940. mbedtls_mpi_free( &ctx->DP );
  30941. #endif /* MBEDTLS_RSA_NO_CRT */
  30942. #if defined(MBEDTLS_THREADING_C)
  30943. mbedtls_mutex_free( &ctx->mutex );
  30944. #endif
  30945. }
  30946. #endif /* !MBEDTLS_RSA_ALT */
  30947. #if defined(MBEDTLS_SELF_TEST)
  30948. /*
  30949. * Example RSA-1024 keypair, for test purposes
  30950. */
  30951. #define KEY_LEN 128
  30952. #define RSA_N "9292758453063D803DD603D5E777D788" \
  30953. "8ED1D5BF35786190FA2F23EBC0848AEA" \
  30954. "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
  30955. "7130B9CED7ACDF54CFC7555AC14EEBAB" \
  30956. "93A89813FBF3C4F8066D2D800F7C38A8" \
  30957. "1AE31942917403FF4946B0A83D3D3E05" \
  30958. "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
  30959. "5E94BB77B07507233A0BC7BAC8F90F79"
  30960. #define RSA_E "10001"
  30961. #define RSA_D "24BF6185468786FDD303083D25E64EFC" \
  30962. "66CA472BC44D253102F8B4A9D3BFA750" \
  30963. "91386C0077937FE33FA3252D28855837" \
  30964. "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
  30965. "DF79C5CE07EE72C7F123142198164234" \
  30966. "CABB724CF78B8173B9F880FC86322407" \
  30967. "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
  30968. "071513A1E85B5DFA031F21ECAE91A34D"
  30969. #define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
  30970. "2C01CAD19EA484A87EA4377637E75500" \
  30971. "FCB2005C5C7DD6EC4AC023CDA285D796" \
  30972. "C3D9E75E1EFC42488BB4F1D13AC30A57"
  30973. #define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
  30974. "E211C2B9E5DB1ED0BF61D0D9899620F4" \
  30975. "910E4168387E3C30AA1E00C339A79508" \
  30976. "8452DD96A9A5EA5D9DCA68DA636032AF"
  30977. #define PT_LEN 24
  30978. #define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
  30979. "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
  30980. #if defined(MBEDTLS_PKCS1_V15)
  30981. static int myrand( void *rng_state, unsigned char *output, size_t len )
  30982. {
  30983. #if !defined(__OpenBSD__)
  30984. size_t i;
  30985. if( rng_state != NULL )
  30986. rng_state = NULL;
  30987. for( i = 0; i < len; ++i )
  30988. output[i] = rand();
  30989. #else
  30990. if( rng_state != NULL )
  30991. rng_state = NULL;
  30992. arc4random_buf( output, len );
  30993. #endif /* !OpenBSD */
  30994. return( 0 );
  30995. }
  30996. #endif /* MBEDTLS_PKCS1_V15 */
  30997. /*
  30998. * Checkup routine
  30999. */
  31000. int mbedtls_rsa_self_test( int verbose )
  31001. {
  31002. int ret = 0;
  31003. #if defined(MBEDTLS_PKCS1_V15)
  31004. size_t len;
  31005. mbedtls_rsa_context rsa;
  31006. unsigned char rsa_plaintext[PT_LEN];
  31007. unsigned char rsa_decrypted[PT_LEN];
  31008. unsigned char rsa_ciphertext[KEY_LEN];
  31009. #if defined(MBEDTLS_SHA1_C)
  31010. unsigned char sha1sum[20];
  31011. #endif
  31012. mbedtls_mpi K;
  31013. mbedtls_mpi_init( &K );
  31014. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
  31015. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
  31016. MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
  31017. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
  31018. MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
  31019. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
  31020. MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
  31021. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
  31022. MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
  31023. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
  31024. MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
  31025. MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
  31026. if( verbose != 0 )
  31027. mbedtls_printf( " RSA key validation: " );
  31028. if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
  31029. mbedtls_rsa_check_privkey( &rsa ) != 0 )
  31030. {
  31031. if( verbose != 0 )
  31032. mbedtls_printf( "failed\n" );
  31033. ret = 1;
  31034. goto cleanup;
  31035. }
  31036. if( verbose != 0 )
  31037. mbedtls_printf( "passed\n PKCS#1 encryption : " );
  31038. memcpy( rsa_plaintext, RSA_PT, PT_LEN );
  31039. if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
  31040. PT_LEN, rsa_plaintext,
  31041. rsa_ciphertext ) != 0 )
  31042. {
  31043. if( verbose != 0 )
  31044. mbedtls_printf( "failed\n" );
  31045. ret = 1;
  31046. goto cleanup;
  31047. }
  31048. if( verbose != 0 )
  31049. mbedtls_printf( "passed\n PKCS#1 decryption : " );
  31050. if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
  31051. &len, rsa_ciphertext, rsa_decrypted,
  31052. sizeof(rsa_decrypted) ) != 0 )
  31053. {
  31054. if( verbose != 0 )
  31055. mbedtls_printf( "failed\n" );
  31056. ret = 1;
  31057. goto cleanup;
  31058. }
  31059. if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
  31060. {
  31061. if( verbose != 0 )
  31062. mbedtls_printf( "failed\n" );
  31063. ret = 1;
  31064. goto cleanup;
  31065. }
  31066. if( verbose != 0 )
  31067. mbedtls_printf( "passed\n" );
  31068. #if defined(MBEDTLS_SHA1_C)
  31069. if( verbose != 0 )
  31070. mbedtls_printf( " PKCS#1 data sign : " );
  31071. if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
  31072. {
  31073. if( verbose != 0 )
  31074. mbedtls_printf( "failed\n" );
  31075. return( 1 );
  31076. }
  31077. if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
  31078. MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
  31079. sha1sum, rsa_ciphertext ) != 0 )
  31080. {
  31081. if( verbose != 0 )
  31082. mbedtls_printf( "failed\n" );
  31083. ret = 1;
  31084. goto cleanup;
  31085. }
  31086. if( verbose != 0 )
  31087. mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
  31088. if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
  31089. MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
  31090. sha1sum, rsa_ciphertext ) != 0 )
  31091. {
  31092. if( verbose != 0 )
  31093. mbedtls_printf( "failed\n" );
  31094. ret = 1;
  31095. goto cleanup;
  31096. }
  31097. if( verbose != 0 )
  31098. mbedtls_printf( "passed\n" );
  31099. #endif /* MBEDTLS_SHA1_C */
  31100. if( verbose != 0 )
  31101. mbedtls_printf( "\n" );
  31102. cleanup:
  31103. mbedtls_mpi_free( &K );
  31104. mbedtls_rsa_free( &rsa );
  31105. #else /* MBEDTLS_PKCS1_V15 */
  31106. ((void) verbose);
  31107. #endif /* MBEDTLS_PKCS1_V15 */
  31108. return( ret );
  31109. }
  31110. #endif /* MBEDTLS_SELF_TEST */
  31111. #endif /* MBEDTLS_RSA_C */
  31112. /*
  31113. Amalgamated build undefines
  31114. */
  31115. #undef ADD
  31116. #undef BC
  31117. #undef BEFORE_COLON
  31118. #undef F
  31119. #undef F0
  31120. #undef F1
  31121. #undef F2
  31122. #undef F3
  31123. #undef F4
  31124. #undef F5
  31125. #undef FSb
  31126. #undef K
  31127. #undef KK
  31128. #undef P
  31129. #undef R
  31130. #undef ROTR
  31131. #undef S
  31132. #undef S0
  31133. #undef S1
  31134. #undef S2
  31135. #undef S3
  31136. #undef SAFE_SNPRINTF
  31137. #undef SHR
  31138. #undef close
  31139. #undef read
  31140. #undef supported_init
  31141. #undef write
  31142. /********* Start of file library/rsa_internal.c ************/
  31143. /*
  31144. * Helper functions for the RSA module
  31145. *
  31146. * Copyright (C) 2006-2017, ARM Limited, All Rights Reserved
  31147. * SPDX-License-Identifier: Apache-2.0
  31148. *
  31149. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  31150. * not use this file except in compliance with the License.
  31151. * You may obtain a copy of the License at
  31152. *
  31153. * http://www.apache.org/licenses/LICENSE-2.0
  31154. *
  31155. * Unless required by applicable law or agreed to in writing, software
  31156. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  31157. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  31158. * See the License for the specific language governing permissions and
  31159. * limitations under the License.
  31160. *
  31161. * This file is part of mbed TLS (https://tls.mbed.org)
  31162. *
  31163. */
  31164. #if !defined(MBEDTLS_CONFIG_FILE)
  31165. #else
  31166. #endif
  31167. #if defined(MBEDTLS_RSA_C)
  31168. /*
  31169. * Compute RSA prime factors from public and private exponents
  31170. *
  31171. * Summary of algorithm:
  31172. * Setting F := lcm(P-1,Q-1), the idea is as follows:
  31173. *
  31174. * (a) For any 1 <= X < N with gcd(X,N)=1, we have X^F = 1 modulo N, so X^(F/2)
  31175. * is a square root of 1 in Z/NZ. Since Z/NZ ~= Z/PZ x Z/QZ by CRT and the
  31176. * square roots of 1 in Z/PZ and Z/QZ are +1 and -1, this leaves the four
  31177. * possibilities X^(F/2) = (+-1, +-1). If it happens that X^(F/2) = (-1,+1)
  31178. * or (+1,-1), then gcd(X^(F/2) + 1, N) will be equal to one of the prime
  31179. * factors of N.
  31180. *
  31181. * (b) If we don't know F/2 but (F/2) * K for some odd (!) K, then the same
  31182. * construction still applies since (-)^K is the identity on the set of
  31183. * roots of 1 in Z/NZ.
  31184. *
  31185. * The public and private key primitives (-)^E and (-)^D are mutually inverse
  31186. * bijections on Z/NZ if and only if (-)^(DE) is the identity on Z/NZ, i.e.
  31187. * if and only if DE - 1 is a multiple of F, say DE - 1 = F * L.
  31188. * Splitting L = 2^t * K with K odd, we have
  31189. *
  31190. * DE - 1 = FL = (F/2) * (2^(t+1)) * K,
  31191. *
  31192. * so (F / 2) * K is among the numbers
  31193. *
  31194. * (DE - 1) >> 1, (DE - 1) >> 2, ..., (DE - 1) >> ord
  31195. *
  31196. * where ord is the order of 2 in (DE - 1).
  31197. * We can therefore iterate through these numbers apply the construction
  31198. * of (a) and (b) above to attempt to factor N.
  31199. *
  31200. */
  31201. int mbedtls_rsa_deduce_primes( mbedtls_mpi const *N,
  31202. mbedtls_mpi const *E, mbedtls_mpi const *D,
  31203. mbedtls_mpi *P, mbedtls_mpi *Q )
  31204. {
  31205. int ret = 0;
  31206. uint16_t attempt; /* Number of current attempt */
  31207. uint16_t iter; /* Number of squares computed in the current attempt */
  31208. uint16_t order; /* Order of 2 in DE - 1 */
  31209. mbedtls_mpi T; /* Holds largest odd divisor of DE - 1 */
  31210. mbedtls_mpi K; /* Temporary holding the current candidate */
  31211. const unsigned char primes[] = { 2,
  31212. 3, 5, 7, 11, 13, 17, 19, 23,
  31213. 29, 31, 37, 41, 43, 47, 53, 59,
  31214. 61, 67, 71, 73, 79, 83, 89, 97,
  31215. 101, 103, 107, 109, 113, 127, 131, 137,
  31216. 139, 149, 151, 157, 163, 167, 173, 179,
  31217. 181, 191, 193, 197, 199, 211, 223, 227,
  31218. 229, 233, 239, 241, 251
  31219. };
  31220. const size_t num_primes = sizeof( primes ) / sizeof( *primes );
  31221. if( P == NULL || Q == NULL || P->p != NULL || Q->p != NULL )
  31222. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  31223. if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 ||
  31224. mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
  31225. mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
  31226. mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
  31227. mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
  31228. {
  31229. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  31230. }
  31231. /*
  31232. * Initializations and temporary changes
  31233. */
  31234. mbedtls_mpi_init( &K );
  31235. mbedtls_mpi_init( &T );
  31236. /* T := DE - 1 */
  31237. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, D, E ) );
  31238. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &T, &T, 1 ) );
  31239. if( ( order = (uint16_t) mbedtls_mpi_lsb( &T ) ) == 0 )
  31240. {
  31241. ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  31242. goto cleanup;
  31243. }
  31244. /* After this operation, T holds the largest odd divisor of DE - 1. */
  31245. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &T, order ) );
  31246. /*
  31247. * Actual work
  31248. */
  31249. /* Skip trying 2 if N == 1 mod 8 */
  31250. attempt = 0;
  31251. if( N->p[0] % 8 == 1 )
  31252. attempt = 1;
  31253. for( ; attempt < num_primes; ++attempt )
  31254. {
  31255. mbedtls_mpi_lset( &K, primes[attempt] );
  31256. /* Check if gcd(K,N) = 1 */
  31257. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
  31258. if( mbedtls_mpi_cmp_int( P, 1 ) != 0 )
  31259. continue;
  31260. /* Go through K^T + 1, K^(2T) + 1, K^(4T) + 1, ...
  31261. * and check whether they have nontrivial GCD with N. */
  31262. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &K, &K, &T, N,
  31263. Q /* temporarily use Q for storing Montgomery
  31264. * multiplication helper values */ ) );
  31265. for( iter = 1; iter <= order; ++iter )
  31266. {
  31267. /* If we reach 1 prematurely, there's no point
  31268. * in continuing to square K */
  31269. if( mbedtls_mpi_cmp_int( &K, 1 ) == 0 )
  31270. break;
  31271. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, 1 ) );
  31272. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
  31273. if( mbedtls_mpi_cmp_int( P, 1 ) == 1 &&
  31274. mbedtls_mpi_cmp_mpi( P, N ) == -1 )
  31275. {
  31276. /*
  31277. * Have found a nontrivial divisor P of N.
  31278. * Set Q := N / P.
  31279. */
  31280. MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( Q, NULL, N, P ) );
  31281. goto cleanup;
  31282. }
  31283. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
  31284. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &K ) );
  31285. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, N ) );
  31286. }
  31287. /*
  31288. * If we get here, then either we prematurely aborted the loop because
  31289. * we reached 1, or K holds primes[attempt]^(DE - 1) mod N, which must
  31290. * be 1 if D,E,N were consistent.
  31291. * Check if that's the case and abort if not, to avoid very long,
  31292. * yet eventually failing, computations if N,D,E were not sane.
  31293. */
  31294. if( mbedtls_mpi_cmp_int( &K, 1 ) != 0 )
  31295. {
  31296. break;
  31297. }
  31298. }
  31299. ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  31300. cleanup:
  31301. mbedtls_mpi_free( &K );
  31302. mbedtls_mpi_free( &T );
  31303. return( ret );
  31304. }
  31305. /*
  31306. * Given P, Q and the public exponent E, deduce D.
  31307. * This is essentially a modular inversion.
  31308. */
  31309. int mbedtls_rsa_deduce_private_exponent( mbedtls_mpi const *P,
  31310. mbedtls_mpi const *Q,
  31311. mbedtls_mpi const *E,
  31312. mbedtls_mpi *D )
  31313. {
  31314. int ret = 0;
  31315. mbedtls_mpi K, L;
  31316. if( D == NULL || mbedtls_mpi_cmp_int( D, 0 ) != 0 )
  31317. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  31318. if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
  31319. mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
  31320. mbedtls_mpi_cmp_int( E, 0 ) == 0 )
  31321. {
  31322. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  31323. }
  31324. mbedtls_mpi_init( &K );
  31325. mbedtls_mpi_init( &L );
  31326. /* Temporarily put K := P-1 and L := Q-1 */
  31327. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
  31328. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
  31329. /* Temporarily put D := gcd(P-1, Q-1) */
  31330. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( D, &K, &L ) );
  31331. /* K := LCM(P-1, Q-1) */
  31332. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &L ) );
  31333. MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &K, NULL, &K, D ) );
  31334. /* Compute modular inverse of E in LCM(P-1, Q-1) */
  31335. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( D, E, &K ) );
  31336. cleanup:
  31337. mbedtls_mpi_free( &K );
  31338. mbedtls_mpi_free( &L );
  31339. return( ret );
  31340. }
  31341. /*
  31342. * Check that RSA CRT parameters are in accordance with core parameters.
  31343. */
  31344. int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
  31345. const mbedtls_mpi *D, const mbedtls_mpi *DP,
  31346. const mbedtls_mpi *DQ, const mbedtls_mpi *QP )
  31347. {
  31348. int ret = 0;
  31349. mbedtls_mpi K, L;
  31350. mbedtls_mpi_init( &K );
  31351. mbedtls_mpi_init( &L );
  31352. /* Check that DP - D == 0 mod P - 1 */
  31353. if( DP != NULL )
  31354. {
  31355. if( P == NULL )
  31356. {
  31357. ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
  31358. goto cleanup;
  31359. }
  31360. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
  31361. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DP, D ) );
  31362. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
  31363. if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
  31364. {
  31365. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  31366. goto cleanup;
  31367. }
  31368. }
  31369. /* Check that DQ - D == 0 mod Q - 1 */
  31370. if( DQ != NULL )
  31371. {
  31372. if( Q == NULL )
  31373. {
  31374. ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
  31375. goto cleanup;
  31376. }
  31377. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
  31378. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DQ, D ) );
  31379. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
  31380. if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
  31381. {
  31382. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  31383. goto cleanup;
  31384. }
  31385. }
  31386. /* Check that QP * Q - 1 == 0 mod P */
  31387. if( QP != NULL )
  31388. {
  31389. if( P == NULL || Q == NULL )
  31390. {
  31391. ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
  31392. goto cleanup;
  31393. }
  31394. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, QP, Q ) );
  31395. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
  31396. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, P ) );
  31397. if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
  31398. {
  31399. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  31400. goto cleanup;
  31401. }
  31402. }
  31403. cleanup:
  31404. /* Wrap MPI error codes by RSA check failure error code */
  31405. if( ret != 0 &&
  31406. ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
  31407. ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
  31408. {
  31409. ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  31410. }
  31411. mbedtls_mpi_free( &K );
  31412. mbedtls_mpi_free( &L );
  31413. return( ret );
  31414. }
  31415. /*
  31416. * Check that core RSA parameters are sane.
  31417. */
  31418. int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
  31419. const mbedtls_mpi *Q, const mbedtls_mpi *D,
  31420. const mbedtls_mpi *E,
  31421. int (*f_rng)(void *, unsigned char *, size_t),
  31422. void *p_rng )
  31423. {
  31424. int ret = 0;
  31425. mbedtls_mpi K, L;
  31426. mbedtls_mpi_init( &K );
  31427. mbedtls_mpi_init( &L );
  31428. /*
  31429. * Step 1: If PRNG provided, check that P and Q are prime
  31430. */
  31431. #if defined(MBEDTLS_GENPRIME)
  31432. if( f_rng != NULL && P != NULL &&
  31433. ( ret = mbedtls_mpi_is_prime( P, f_rng, p_rng ) ) != 0 )
  31434. {
  31435. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  31436. goto cleanup;
  31437. }
  31438. if( f_rng != NULL && Q != NULL &&
  31439. ( ret = mbedtls_mpi_is_prime( Q, f_rng, p_rng ) ) != 0 )
  31440. {
  31441. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  31442. goto cleanup;
  31443. }
  31444. #else
  31445. ((void) f_rng);
  31446. ((void) p_rng);
  31447. #endif /* MBEDTLS_GENPRIME */
  31448. /*
  31449. * Step 2: Check that 1 < N = P * Q
  31450. */
  31451. if( P != NULL && Q != NULL && N != NULL )
  31452. {
  31453. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
  31454. if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 ||
  31455. mbedtls_mpi_cmp_mpi( &K, N ) != 0 )
  31456. {
  31457. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  31458. goto cleanup;
  31459. }
  31460. }
  31461. /*
  31462. * Step 3: Check and 1 < D, E < N if present.
  31463. */
  31464. if( N != NULL && D != NULL && E != NULL )
  31465. {
  31466. if ( mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
  31467. mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
  31468. mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
  31469. mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
  31470. {
  31471. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  31472. goto cleanup;
  31473. }
  31474. }
  31475. /*
  31476. * Step 4: Check that D, E are inverse modulo P-1 and Q-1
  31477. */
  31478. if( P != NULL && Q != NULL && D != NULL && E != NULL )
  31479. {
  31480. if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
  31481. mbedtls_mpi_cmp_int( Q, 1 ) <= 0 )
  31482. {
  31483. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  31484. goto cleanup;
  31485. }
  31486. /* Compute DE-1 mod P-1 */
  31487. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
  31488. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
  31489. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, P, 1 ) );
  31490. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
  31491. if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
  31492. {
  31493. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  31494. goto cleanup;
  31495. }
  31496. /* Compute DE-1 mod Q-1 */
  31497. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
  31498. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
  31499. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
  31500. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
  31501. if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
  31502. {
  31503. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  31504. goto cleanup;
  31505. }
  31506. }
  31507. cleanup:
  31508. mbedtls_mpi_free( &K );
  31509. mbedtls_mpi_free( &L );
  31510. /* Wrap MPI error codes by RSA check failure error code */
  31511. if( ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
  31512. {
  31513. ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  31514. }
  31515. return( ret );
  31516. }
  31517. int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
  31518. const mbedtls_mpi *D, mbedtls_mpi *DP,
  31519. mbedtls_mpi *DQ, mbedtls_mpi *QP )
  31520. {
  31521. int ret = 0;
  31522. mbedtls_mpi K;
  31523. mbedtls_mpi_init( &K );
  31524. /* DP = D mod P-1 */
  31525. if( DP != NULL )
  31526. {
  31527. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
  31528. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
  31529. }
  31530. /* DQ = D mod Q-1 */
  31531. if( DQ != NULL )
  31532. {
  31533. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
  31534. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
  31535. }
  31536. /* QP = Q^{-1} mod P */
  31537. if( QP != NULL )
  31538. {
  31539. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
  31540. }
  31541. cleanup:
  31542. mbedtls_mpi_free( &K );
  31543. return( ret );
  31544. }
  31545. #endif /* MBEDTLS_RSA_C */
  31546. /*
  31547. Amalgamated build undefines
  31548. */
  31549. #undef ADD
  31550. #undef BC
  31551. #undef BEFORE_COLON
  31552. #undef F
  31553. #undef F0
  31554. #undef F1
  31555. #undef F2
  31556. #undef F3
  31557. #undef F4
  31558. #undef F5
  31559. #undef FSb
  31560. #undef K
  31561. #undef KK
  31562. #undef P
  31563. #undef R
  31564. #undef ROTR
  31565. #undef S
  31566. #undef S0
  31567. #undef S1
  31568. #undef S2
  31569. #undef S3
  31570. #undef SAFE_SNPRINTF
  31571. #undef SHR
  31572. #undef close
  31573. #undef read
  31574. #undef supported_init
  31575. #undef write
  31576. /********* Start of file library/sha1.c ************/
  31577. /*
  31578. * FIPS-180-1 compliant SHA-1 implementation
  31579. *
  31580. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  31581. * SPDX-License-Identifier: Apache-2.0
  31582. *
  31583. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  31584. * not use this file except in compliance with the License.
  31585. * You may obtain a copy of the License at
  31586. *
  31587. * http://www.apache.org/licenses/LICENSE-2.0
  31588. *
  31589. * Unless required by applicable law or agreed to in writing, software
  31590. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  31591. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  31592. * See the License for the specific language governing permissions and
  31593. * limitations under the License.
  31594. *
  31595. * This file is part of mbed TLS (https://tls.mbed.org)
  31596. */
  31597. /*
  31598. * The SHA-1 standard was published by NIST in 1993.
  31599. *
  31600. * http://www.itl.nist.gov/fipspubs/fip180-1.htm
  31601. */
  31602. #if !defined(MBEDTLS_CONFIG_FILE)
  31603. #else
  31604. #endif
  31605. #if defined(MBEDTLS_SHA1_C)
  31606. #include <string.h>
  31607. #if defined(MBEDTLS_SELF_TEST)
  31608. #if defined(MBEDTLS_PLATFORM_C)
  31609. #else
  31610. #include <stdio.h>
  31611. #define mbedtls_printf printf
  31612. #endif /* MBEDTLS_PLATFORM_C */
  31613. #endif /* MBEDTLS_SELF_TEST */
  31614. #if !defined(MBEDTLS_SHA1_ALT)
  31615. /* Implementation that should never be optimized out by the compiler */
  31616. /* zeroize was here */
  31617. /*
  31618. * 32-bit integer manipulation macros (big endian)
  31619. */
  31620. #ifndef GET_UINT32_BE
  31621. #define GET_UINT32_BE(n,b,i) \
  31622. { \
  31623. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  31624. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  31625. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  31626. | ( (uint32_t) (b)[(i) + 3] ); \
  31627. }
  31628. #endif
  31629. #ifndef PUT_UINT32_BE
  31630. #define PUT_UINT32_BE(n,b,i) \
  31631. { \
  31632. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  31633. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  31634. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  31635. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  31636. }
  31637. #endif
  31638. void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
  31639. {
  31640. memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
  31641. }
  31642. void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
  31643. {
  31644. if( ctx == NULL )
  31645. return;
  31646. mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
  31647. }
  31648. void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
  31649. const mbedtls_sha1_context *src )
  31650. {
  31651. *dst = *src;
  31652. }
  31653. /*
  31654. * SHA-1 context setup
  31655. */
  31656. int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
  31657. {
  31658. ctx->total[0] = 0;
  31659. ctx->total[1] = 0;
  31660. ctx->state[0] = 0x67452301;
  31661. ctx->state[1] = 0xEFCDAB89;
  31662. ctx->state[2] = 0x98BADCFE;
  31663. ctx->state[3] = 0x10325476;
  31664. ctx->state[4] = 0xC3D2E1F0;
  31665. return( 0 );
  31666. }
  31667. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  31668. void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
  31669. {
  31670. mbedtls_sha1_starts_ret( ctx );
  31671. }
  31672. #endif
  31673. #if !defined(MBEDTLS_SHA1_PROCESS_ALT)
  31674. int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
  31675. const unsigned char data[64] )
  31676. {
  31677. uint32_t temp, W[16], A, B, C, D, E;
  31678. GET_UINT32_BE( W[ 0], data, 0 );
  31679. GET_UINT32_BE( W[ 1], data, 4 );
  31680. GET_UINT32_BE( W[ 2], data, 8 );
  31681. GET_UINT32_BE( W[ 3], data, 12 );
  31682. GET_UINT32_BE( W[ 4], data, 16 );
  31683. GET_UINT32_BE( W[ 5], data, 20 );
  31684. GET_UINT32_BE( W[ 6], data, 24 );
  31685. GET_UINT32_BE( W[ 7], data, 28 );
  31686. GET_UINT32_BE( W[ 8], data, 32 );
  31687. GET_UINT32_BE( W[ 9], data, 36 );
  31688. GET_UINT32_BE( W[10], data, 40 );
  31689. GET_UINT32_BE( W[11], data, 44 );
  31690. GET_UINT32_BE( W[12], data, 48 );
  31691. GET_UINT32_BE( W[13], data, 52 );
  31692. GET_UINT32_BE( W[14], data, 56 );
  31693. GET_UINT32_BE( W[15], data, 60 );
  31694. #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  31695. #define R(t) \
  31696. ( \
  31697. temp = W[( t - 3 ) & 0x0F] ^ W[( t - 8 ) & 0x0F] ^ \
  31698. W[( t - 14 ) & 0x0F] ^ W[ t & 0x0F], \
  31699. ( W[t & 0x0F] = S(temp,1) ) \
  31700. )
  31701. #define P(a,b,c,d,e,x) \
  31702. { \
  31703. e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
  31704. }
  31705. A = ctx->state[0];
  31706. B = ctx->state[1];
  31707. C = ctx->state[2];
  31708. D = ctx->state[3];
  31709. E = ctx->state[4];
  31710. #define F(x,y,z) (z ^ (x & (y ^ z)))
  31711. #define K 0x5A827999
  31712. P( A, B, C, D, E, W[0] );
  31713. P( E, A, B, C, D, W[1] );
  31714. P( D, E, A, B, C, W[2] );
  31715. P( C, D, E, A, B, W[3] );
  31716. P( B, C, D, E, A, W[4] );
  31717. P( A, B, C, D, E, W[5] );
  31718. P( E, A, B, C, D, W[6] );
  31719. P( D, E, A, B, C, W[7] );
  31720. P( C, D, E, A, B, W[8] );
  31721. P( B, C, D, E, A, W[9] );
  31722. P( A, B, C, D, E, W[10] );
  31723. P( E, A, B, C, D, W[11] );
  31724. P( D, E, A, B, C, W[12] );
  31725. P( C, D, E, A, B, W[13] );
  31726. P( B, C, D, E, A, W[14] );
  31727. P( A, B, C, D, E, W[15] );
  31728. P( E, A, B, C, D, R(16) );
  31729. P( D, E, A, B, C, R(17) );
  31730. P( C, D, E, A, B, R(18) );
  31731. P( B, C, D, E, A, R(19) );
  31732. #undef K
  31733. #undef F
  31734. #define F(x,y,z) (x ^ y ^ z)
  31735. #define K 0x6ED9EBA1
  31736. P( A, B, C, D, E, R(20) );
  31737. P( E, A, B, C, D, R(21) );
  31738. P( D, E, A, B, C, R(22) );
  31739. P( C, D, E, A, B, R(23) );
  31740. P( B, C, D, E, A, R(24) );
  31741. P( A, B, C, D, E, R(25) );
  31742. P( E, A, B, C, D, R(26) );
  31743. P( D, E, A, B, C, R(27) );
  31744. P( C, D, E, A, B, R(28) );
  31745. P( B, C, D, E, A, R(29) );
  31746. P( A, B, C, D, E, R(30) );
  31747. P( E, A, B, C, D, R(31) );
  31748. P( D, E, A, B, C, R(32) );
  31749. P( C, D, E, A, B, R(33) );
  31750. P( B, C, D, E, A, R(34) );
  31751. P( A, B, C, D, E, R(35) );
  31752. P( E, A, B, C, D, R(36) );
  31753. P( D, E, A, B, C, R(37) );
  31754. P( C, D, E, A, B, R(38) );
  31755. P( B, C, D, E, A, R(39) );
  31756. #undef K
  31757. #undef F
  31758. #define F(x,y,z) ((x & y) | (z & (x | y)))
  31759. #define K 0x8F1BBCDC
  31760. P( A, B, C, D, E, R(40) );
  31761. P( E, A, B, C, D, R(41) );
  31762. P( D, E, A, B, C, R(42) );
  31763. P( C, D, E, A, B, R(43) );
  31764. P( B, C, D, E, A, R(44) );
  31765. P( A, B, C, D, E, R(45) );
  31766. P( E, A, B, C, D, R(46) );
  31767. P( D, E, A, B, C, R(47) );
  31768. P( C, D, E, A, B, R(48) );
  31769. P( B, C, D, E, A, R(49) );
  31770. P( A, B, C, D, E, R(50) );
  31771. P( E, A, B, C, D, R(51) );
  31772. P( D, E, A, B, C, R(52) );
  31773. P( C, D, E, A, B, R(53) );
  31774. P( B, C, D, E, A, R(54) );
  31775. P( A, B, C, D, E, R(55) );
  31776. P( E, A, B, C, D, R(56) );
  31777. P( D, E, A, B, C, R(57) );
  31778. P( C, D, E, A, B, R(58) );
  31779. P( B, C, D, E, A, R(59) );
  31780. #undef K
  31781. #undef F
  31782. #define F(x,y,z) (x ^ y ^ z)
  31783. #define K 0xCA62C1D6
  31784. P( A, B, C, D, E, R(60) );
  31785. P( E, A, B, C, D, R(61) );
  31786. P( D, E, A, B, C, R(62) );
  31787. P( C, D, E, A, B, R(63) );
  31788. P( B, C, D, E, A, R(64) );
  31789. P( A, B, C, D, E, R(65) );
  31790. P( E, A, B, C, D, R(66) );
  31791. P( D, E, A, B, C, R(67) );
  31792. P( C, D, E, A, B, R(68) );
  31793. P( B, C, D, E, A, R(69) );
  31794. P( A, B, C, D, E, R(70) );
  31795. P( E, A, B, C, D, R(71) );
  31796. P( D, E, A, B, C, R(72) );
  31797. P( C, D, E, A, B, R(73) );
  31798. P( B, C, D, E, A, R(74) );
  31799. P( A, B, C, D, E, R(75) );
  31800. P( E, A, B, C, D, R(76) );
  31801. P( D, E, A, B, C, R(77) );
  31802. P( C, D, E, A, B, R(78) );
  31803. P( B, C, D, E, A, R(79) );
  31804. #undef K
  31805. #undef F
  31806. ctx->state[0] += A;
  31807. ctx->state[1] += B;
  31808. ctx->state[2] += C;
  31809. ctx->state[3] += D;
  31810. ctx->state[4] += E;
  31811. return( 0 );
  31812. }
  31813. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  31814. void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
  31815. const unsigned char data[64] )
  31816. {
  31817. mbedtls_internal_sha1_process( ctx, data );
  31818. }
  31819. #endif
  31820. #endif /* !MBEDTLS_SHA1_PROCESS_ALT */
  31821. /*
  31822. * SHA-1 process buffer
  31823. */
  31824. int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
  31825. const unsigned char *input,
  31826. size_t ilen )
  31827. {
  31828. int ret;
  31829. size_t fill;
  31830. uint32_t left;
  31831. if( ilen == 0 )
  31832. return( 0 );
  31833. left = ctx->total[0] & 0x3F;
  31834. fill = 64 - left;
  31835. ctx->total[0] += (uint32_t) ilen;
  31836. ctx->total[0] &= 0xFFFFFFFF;
  31837. if( ctx->total[0] < (uint32_t) ilen )
  31838. ctx->total[1]++;
  31839. if( left && ilen >= fill )
  31840. {
  31841. memcpy( (void *) (ctx->buffer + left), input, fill );
  31842. if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
  31843. return( ret );
  31844. input += fill;
  31845. ilen -= fill;
  31846. left = 0;
  31847. }
  31848. while( ilen >= 64 )
  31849. {
  31850. if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
  31851. return( ret );
  31852. input += 64;
  31853. ilen -= 64;
  31854. }
  31855. if( ilen > 0 )
  31856. memcpy( (void *) (ctx->buffer + left), input, ilen );
  31857. return( 0 );
  31858. }
  31859. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  31860. void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
  31861. const unsigned char *input,
  31862. size_t ilen )
  31863. {
  31864. mbedtls_sha1_update_ret( ctx, input, ilen );
  31865. }
  31866. #endif
  31867. static const unsigned char sha1_padding[64] =
  31868. {
  31869. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31870. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31871. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31872. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  31873. };
  31874. /*
  31875. * SHA-1 final digest
  31876. */
  31877. int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
  31878. unsigned char output[20] )
  31879. {
  31880. int ret;
  31881. uint32_t last, padn;
  31882. uint32_t high, low;
  31883. unsigned char msglen[8];
  31884. high = ( ctx->total[0] >> 29 )
  31885. | ( ctx->total[1] << 3 );
  31886. low = ( ctx->total[0] << 3 );
  31887. PUT_UINT32_BE( high, msglen, 0 );
  31888. PUT_UINT32_BE( low, msglen, 4 );
  31889. last = ctx->total[0] & 0x3F;
  31890. padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  31891. if( ( ret = mbedtls_sha1_update_ret( ctx, sha1_padding, padn ) ) != 0 )
  31892. return( ret );
  31893. if( ( ret = mbedtls_sha1_update_ret( ctx, msglen, 8 ) ) != 0 )
  31894. return( ret );
  31895. PUT_UINT32_BE( ctx->state[0], output, 0 );
  31896. PUT_UINT32_BE( ctx->state[1], output, 4 );
  31897. PUT_UINT32_BE( ctx->state[2], output, 8 );
  31898. PUT_UINT32_BE( ctx->state[3], output, 12 );
  31899. PUT_UINT32_BE( ctx->state[4], output, 16 );
  31900. return( 0 );
  31901. }
  31902. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  31903. void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
  31904. unsigned char output[20] )
  31905. {
  31906. mbedtls_sha1_finish_ret( ctx, output );
  31907. }
  31908. #endif
  31909. #endif /* !MBEDTLS_SHA1_ALT */
  31910. /*
  31911. * output = SHA-1( input buffer )
  31912. */
  31913. int mbedtls_sha1_ret( const unsigned char *input,
  31914. size_t ilen,
  31915. unsigned char output[20] )
  31916. {
  31917. int ret;
  31918. mbedtls_sha1_context ctx;
  31919. mbedtls_sha1_init( &ctx );
  31920. if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
  31921. goto exit;
  31922. if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
  31923. goto exit;
  31924. if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
  31925. goto exit;
  31926. exit:
  31927. mbedtls_sha1_free( &ctx );
  31928. return( ret );
  31929. }
  31930. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  31931. void mbedtls_sha1( const unsigned char *input,
  31932. size_t ilen,
  31933. unsigned char output[20] )
  31934. {
  31935. mbedtls_sha1_ret( input, ilen, output );
  31936. }
  31937. #endif
  31938. #if defined(MBEDTLS_SELF_TEST)
  31939. /*
  31940. * FIPS-180-1 test vectors
  31941. */
  31942. static const unsigned char sha1_test_buf[3][57] =
  31943. {
  31944. { "abc" },
  31945. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
  31946. { "" }
  31947. };
  31948. static const size_t sha1_test_buflen[3] =
  31949. {
  31950. 3, 56, 1000
  31951. };
  31952. static const unsigned char sha1_test_sum[3][20] =
  31953. {
  31954. { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
  31955. 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
  31956. { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
  31957. 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
  31958. { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
  31959. 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
  31960. };
  31961. /*
  31962. * Checkup routine
  31963. */
  31964. int mbedtls_sha1_self_test( int verbose )
  31965. {
  31966. int i, j, buflen, ret = 0;
  31967. unsigned char buf[1024];
  31968. unsigned char sha1sum[20];
  31969. mbedtls_sha1_context ctx;
  31970. mbedtls_sha1_init( &ctx );
  31971. /*
  31972. * SHA-1
  31973. */
  31974. for( i = 0; i < 3; i++ )
  31975. {
  31976. if( verbose != 0 )
  31977. mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
  31978. if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
  31979. goto fail;
  31980. if( i == 2 )
  31981. {
  31982. memset( buf, 'a', buflen = 1000 );
  31983. for( j = 0; j < 1000; j++ )
  31984. {
  31985. ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
  31986. if( ret != 0 )
  31987. goto fail;
  31988. }
  31989. }
  31990. else
  31991. {
  31992. ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
  31993. sha1_test_buflen[i] );
  31994. if( ret != 0 )
  31995. goto fail;
  31996. }
  31997. if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
  31998. goto fail;
  31999. if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
  32000. {
  32001. ret = 1;
  32002. goto fail;
  32003. }
  32004. if( verbose != 0 )
  32005. mbedtls_printf( "passed\n" );
  32006. }
  32007. if( verbose != 0 )
  32008. mbedtls_printf( "\n" );
  32009. goto exit;
  32010. fail:
  32011. if( verbose != 0 )
  32012. mbedtls_printf( "failed\n" );
  32013. exit:
  32014. mbedtls_sha1_free( &ctx );
  32015. return( ret );
  32016. }
  32017. #endif /* MBEDTLS_SELF_TEST */
  32018. #endif /* MBEDTLS_SHA1_C */
  32019. /*
  32020. Amalgamated build undefines
  32021. */
  32022. #undef ADD
  32023. #undef BC
  32024. #undef BEFORE_COLON
  32025. #undef F
  32026. #undef F0
  32027. #undef F1
  32028. #undef F2
  32029. #undef F3
  32030. #undef F4
  32031. #undef F5
  32032. #undef FSb
  32033. #undef K
  32034. #undef KK
  32035. #undef P
  32036. #undef R
  32037. #undef ROTR
  32038. #undef S
  32039. #undef S0
  32040. #undef S1
  32041. #undef S2
  32042. #undef S3
  32043. #undef SAFE_SNPRINTF
  32044. #undef SHR
  32045. #undef close
  32046. #undef read
  32047. #undef supported_init
  32048. #undef write
  32049. /********* Start of file library/sha256.c ************/
  32050. /*
  32051. * FIPS-180-2 compliant SHA-256 implementation
  32052. *
  32053. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  32054. * SPDX-License-Identifier: Apache-2.0
  32055. *
  32056. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  32057. * not use this file except in compliance with the License.
  32058. * You may obtain a copy of the License at
  32059. *
  32060. * http://www.apache.org/licenses/LICENSE-2.0
  32061. *
  32062. * Unless required by applicable law or agreed to in writing, software
  32063. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  32064. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  32065. * See the License for the specific language governing permissions and
  32066. * limitations under the License.
  32067. *
  32068. * This file is part of mbed TLS (https://tls.mbed.org)
  32069. */
  32070. /*
  32071. * The SHA-256 Secure Hash Standard was published by NIST in 2002.
  32072. *
  32073. * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
  32074. */
  32075. #if !defined(MBEDTLS_CONFIG_FILE)
  32076. #else
  32077. #endif
  32078. #if defined(MBEDTLS_SHA256_C)
  32079. #include <string.h>
  32080. #if defined(MBEDTLS_SELF_TEST)
  32081. #if defined(MBEDTLS_PLATFORM_C)
  32082. #else
  32083. #include <stdio.h>
  32084. #include <stdlib.h>
  32085. #define mbedtls_printf printf
  32086. #define mbedtls_calloc calloc
  32087. #define mbedtls_free free
  32088. #endif /* MBEDTLS_PLATFORM_C */
  32089. #endif /* MBEDTLS_SELF_TEST */
  32090. #if !defined(MBEDTLS_SHA256_ALT)
  32091. /* Implementation that should never be optimized out by the compiler */
  32092. /* zeroize was here */
  32093. /*
  32094. * 32-bit integer manipulation macros (big endian)
  32095. */
  32096. #ifndef GET_UINT32_BE
  32097. #define GET_UINT32_BE(n,b,i) \
  32098. do { \
  32099. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  32100. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  32101. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  32102. | ( (uint32_t) (b)[(i) + 3] ); \
  32103. } while( 0 )
  32104. #endif
  32105. #ifndef PUT_UINT32_BE
  32106. #define PUT_UINT32_BE(n,b,i) \
  32107. do { \
  32108. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  32109. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  32110. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  32111. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  32112. } while( 0 )
  32113. #endif
  32114. void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
  32115. {
  32116. memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
  32117. }
  32118. void mbedtls_sha256_free( mbedtls_sha256_context *ctx )
  32119. {
  32120. if( ctx == NULL )
  32121. return;
  32122. mbedtls_zeroize( ctx, sizeof( mbedtls_sha256_context ) );
  32123. }
  32124. void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
  32125. const mbedtls_sha256_context *src )
  32126. {
  32127. *dst = *src;
  32128. }
  32129. /*
  32130. * SHA-256 context setup
  32131. */
  32132. int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 )
  32133. {
  32134. ctx->total[0] = 0;
  32135. ctx->total[1] = 0;
  32136. if( is224 == 0 )
  32137. {
  32138. /* SHA-256 */
  32139. ctx->state[0] = 0x6A09E667;
  32140. ctx->state[1] = 0xBB67AE85;
  32141. ctx->state[2] = 0x3C6EF372;
  32142. ctx->state[3] = 0xA54FF53A;
  32143. ctx->state[4] = 0x510E527F;
  32144. ctx->state[5] = 0x9B05688C;
  32145. ctx->state[6] = 0x1F83D9AB;
  32146. ctx->state[7] = 0x5BE0CD19;
  32147. }
  32148. else
  32149. {
  32150. /* SHA-224 */
  32151. ctx->state[0] = 0xC1059ED8;
  32152. ctx->state[1] = 0x367CD507;
  32153. ctx->state[2] = 0x3070DD17;
  32154. ctx->state[3] = 0xF70E5939;
  32155. ctx->state[4] = 0xFFC00B31;
  32156. ctx->state[5] = 0x68581511;
  32157. ctx->state[6] = 0x64F98FA7;
  32158. ctx->state[7] = 0xBEFA4FA4;
  32159. }
  32160. ctx->is224 = is224;
  32161. return( 0 );
  32162. }
  32163. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  32164. void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
  32165. int is224 )
  32166. {
  32167. mbedtls_sha256_starts_ret( ctx, is224 );
  32168. }
  32169. #endif
  32170. #if !defined(MBEDTLS_SHA256_PROCESS_ALT)
  32171. static const uint32_t K[] =
  32172. {
  32173. 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
  32174. 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
  32175. 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
  32176. 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
  32177. 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
  32178. 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
  32179. 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
  32180. 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
  32181. 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
  32182. 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
  32183. 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
  32184. 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
  32185. 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
  32186. 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
  32187. 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
  32188. 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
  32189. };
  32190. #define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
  32191. #define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
  32192. #define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
  32193. #define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
  32194. #define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
  32195. #define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
  32196. #define F0(x,y,z) ((x & y) | (z & (x | y)))
  32197. #define F1(x,y,z) (z ^ (x & (y ^ z)))
  32198. #define R(t) \
  32199. ( \
  32200. W[t] = S1(W[t - 2]) + W[t - 7] + \
  32201. S0(W[t - 15]) + W[t - 16] \
  32202. )
  32203. #define P(a,b,c,d,e,f,g,h,x,K) \
  32204. { \
  32205. temp1 = h + S3(e) + F1(e,f,g) + K + x; \
  32206. temp2 = S2(a) + F0(a,b,c); \
  32207. d += temp1; h = temp1 + temp2; \
  32208. }
  32209. int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
  32210. const unsigned char data[64] )
  32211. {
  32212. uint32_t temp1, temp2, W[64];
  32213. uint32_t A[8];
  32214. unsigned int i;
  32215. for( i = 0; i < 8; i++ )
  32216. A[i] = ctx->state[i];
  32217. #if defined(MBEDTLS_SHA256_SMALLER)
  32218. for( i = 0; i < 64; i++ )
  32219. {
  32220. if( i < 16 )
  32221. GET_UINT32_BE( W[i], data, 4 * i );
  32222. else
  32223. R( i );
  32224. P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i], K[i] );
  32225. temp1 = A[7]; A[7] = A[6]; A[6] = A[5]; A[5] = A[4]; A[4] = A[3];
  32226. A[3] = A[2]; A[2] = A[1]; A[1] = A[0]; A[0] = temp1;
  32227. }
  32228. #else /* MBEDTLS_SHA256_SMALLER */
  32229. for( i = 0; i < 16; i++ )
  32230. GET_UINT32_BE( W[i], data, 4 * i );
  32231. for( i = 0; i < 16; i += 8 )
  32232. {
  32233. P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i+0], K[i+0] );
  32234. P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], W[i+1], K[i+1] );
  32235. P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], W[i+2], K[i+2] );
  32236. P( A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], W[i+3], K[i+3] );
  32237. P( A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], W[i+4], K[i+4] );
  32238. P( A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], W[i+5], K[i+5] );
  32239. P( A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], W[i+6], K[i+6] );
  32240. P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], W[i+7], K[i+7] );
  32241. }
  32242. for( i = 16; i < 64; i += 8 )
  32243. {
  32244. P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], R(i+0), K[i+0] );
  32245. P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], R(i+1), K[i+1] );
  32246. P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], R(i+2), K[i+2] );
  32247. P( A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], R(i+3), K[i+3] );
  32248. P( A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], R(i+4), K[i+4] );
  32249. P( A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], R(i+5), K[i+5] );
  32250. P( A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], R(i+6), K[i+6] );
  32251. P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], R(i+7), K[i+7] );
  32252. }
  32253. #endif /* MBEDTLS_SHA256_SMALLER */
  32254. for( i = 0; i < 8; i++ )
  32255. ctx->state[i] += A[i];
  32256. return( 0 );
  32257. }
  32258. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  32259. void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
  32260. const unsigned char data[64] )
  32261. {
  32262. mbedtls_internal_sha256_process( ctx, data );
  32263. }
  32264. #endif
  32265. #endif /* !MBEDTLS_SHA256_PROCESS_ALT */
  32266. /*
  32267. * SHA-256 process buffer
  32268. */
  32269. int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
  32270. const unsigned char *input,
  32271. size_t ilen )
  32272. {
  32273. int ret;
  32274. size_t fill;
  32275. uint32_t left;
  32276. if( ilen == 0 )
  32277. return( 0 );
  32278. left = ctx->total[0] & 0x3F;
  32279. fill = 64 - left;
  32280. ctx->total[0] += (uint32_t) ilen;
  32281. ctx->total[0] &= 0xFFFFFFFF;
  32282. if( ctx->total[0] < (uint32_t) ilen )
  32283. ctx->total[1]++;
  32284. if( left && ilen >= fill )
  32285. {
  32286. memcpy( (void *) (ctx->buffer + left), input, fill );
  32287. if( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 )
  32288. return( ret );
  32289. input += fill;
  32290. ilen -= fill;
  32291. left = 0;
  32292. }
  32293. while( ilen >= 64 )
  32294. {
  32295. if( ( ret = mbedtls_internal_sha256_process( ctx, input ) ) != 0 )
  32296. return( ret );
  32297. input += 64;
  32298. ilen -= 64;
  32299. }
  32300. if( ilen > 0 )
  32301. memcpy( (void *) (ctx->buffer + left), input, ilen );
  32302. return( 0 );
  32303. }
  32304. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  32305. void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
  32306. const unsigned char *input,
  32307. size_t ilen )
  32308. {
  32309. mbedtls_sha256_update_ret( ctx, input, ilen );
  32310. }
  32311. #endif
  32312. static const unsigned char sha256_padding[64] =
  32313. {
  32314. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32315. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32316. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32317. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  32318. };
  32319. /*
  32320. * SHA-256 final digest
  32321. */
  32322. int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
  32323. unsigned char output[32] )
  32324. {
  32325. int ret;
  32326. uint32_t last, padn;
  32327. uint32_t high, low;
  32328. unsigned char msglen[8];
  32329. high = ( ctx->total[0] >> 29 )
  32330. | ( ctx->total[1] << 3 );
  32331. low = ( ctx->total[0] << 3 );
  32332. PUT_UINT32_BE( high, msglen, 0 );
  32333. PUT_UINT32_BE( low, msglen, 4 );
  32334. last = ctx->total[0] & 0x3F;
  32335. padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  32336. if( ( ret = mbedtls_sha256_update_ret( ctx, sha256_padding, padn ) ) != 0 )
  32337. return( ret );
  32338. if( ( ret = mbedtls_sha256_update_ret( ctx, msglen, 8 ) ) != 0 )
  32339. return( ret );
  32340. PUT_UINT32_BE( ctx->state[0], output, 0 );
  32341. PUT_UINT32_BE( ctx->state[1], output, 4 );
  32342. PUT_UINT32_BE( ctx->state[2], output, 8 );
  32343. PUT_UINT32_BE( ctx->state[3], output, 12 );
  32344. PUT_UINT32_BE( ctx->state[4], output, 16 );
  32345. PUT_UINT32_BE( ctx->state[5], output, 20 );
  32346. PUT_UINT32_BE( ctx->state[6], output, 24 );
  32347. if( ctx->is224 == 0 )
  32348. PUT_UINT32_BE( ctx->state[7], output, 28 );
  32349. return( 0 );
  32350. }
  32351. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  32352. void mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
  32353. unsigned char output[32] )
  32354. {
  32355. mbedtls_sha256_finish_ret( ctx, output );
  32356. }
  32357. #endif
  32358. #endif /* !MBEDTLS_SHA256_ALT */
  32359. /*
  32360. * output = SHA-256( input buffer )
  32361. */
  32362. int mbedtls_sha256_ret( const unsigned char *input,
  32363. size_t ilen,
  32364. unsigned char output[32],
  32365. int is224 )
  32366. {
  32367. int ret;
  32368. mbedtls_sha256_context ctx;
  32369. mbedtls_sha256_init( &ctx );
  32370. if( ( ret = mbedtls_sha256_starts_ret( &ctx, is224 ) ) != 0 )
  32371. goto exit;
  32372. if( ( ret = mbedtls_sha256_update_ret( &ctx, input, ilen ) ) != 0 )
  32373. goto exit;
  32374. if( ( ret = mbedtls_sha256_finish_ret( &ctx, output ) ) != 0 )
  32375. goto exit;
  32376. exit:
  32377. mbedtls_sha256_free( &ctx );
  32378. return( ret );
  32379. }
  32380. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  32381. void mbedtls_sha256( const unsigned char *input,
  32382. size_t ilen,
  32383. unsigned char output[32],
  32384. int is224 )
  32385. {
  32386. mbedtls_sha256_ret( input, ilen, output, is224 );
  32387. }
  32388. #endif
  32389. #if defined(MBEDTLS_SELF_TEST)
  32390. /*
  32391. * FIPS-180-2 test vectors
  32392. */
  32393. static const unsigned char sha256_test_buf[3][57] =
  32394. {
  32395. { "abc" },
  32396. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
  32397. { "" }
  32398. };
  32399. static const size_t sha256_test_buflen[3] =
  32400. {
  32401. 3, 56, 1000
  32402. };
  32403. static const unsigned char sha256_test_sum[6][32] =
  32404. {
  32405. /*
  32406. * SHA-224 test vectors
  32407. */
  32408. { 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22,
  32409. 0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3,
  32410. 0x2A, 0xAD, 0xBC, 0xE4, 0xBD, 0xA0, 0xB3, 0xF7,
  32411. 0xE3, 0x6C, 0x9D, 0xA7 },
  32412. { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC,
  32413. 0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50,
  32414. 0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19,
  32415. 0x52, 0x52, 0x25, 0x25 },
  32416. { 0x20, 0x79, 0x46, 0x55, 0x98, 0x0C, 0x91, 0xD8,
  32417. 0xBB, 0xB4, 0xC1, 0xEA, 0x97, 0x61, 0x8A, 0x4B,
  32418. 0xF0, 0x3F, 0x42, 0x58, 0x19, 0x48, 0xB2, 0xEE,
  32419. 0x4E, 0xE7, 0xAD, 0x67 },
  32420. /*
  32421. * SHA-256 test vectors
  32422. */
  32423. { 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
  32424. 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
  32425. 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
  32426. 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD },
  32427. { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
  32428. 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
  32429. 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
  32430. 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 },
  32431. { 0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92,
  32432. 0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67,
  32433. 0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E,
  32434. 0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 }
  32435. };
  32436. /*
  32437. * Checkup routine
  32438. */
  32439. int mbedtls_sha256_self_test( int verbose )
  32440. {
  32441. int i, j, k, buflen, ret = 0;
  32442. unsigned char *buf;
  32443. unsigned char sha256sum[32];
  32444. mbedtls_sha256_context ctx;
  32445. buf = mbedtls_calloc( 1024, sizeof(unsigned char) );
  32446. if( NULL == buf )
  32447. {
  32448. if( verbose != 0 )
  32449. mbedtls_printf( "Buffer allocation failed\n" );
  32450. return( 1 );
  32451. }
  32452. mbedtls_sha256_init( &ctx );
  32453. for( i = 0; i < 6; i++ )
  32454. {
  32455. j = i % 3;
  32456. k = i < 3;
  32457. if( verbose != 0 )
  32458. mbedtls_printf( " SHA-%d test #%d: ", 256 - k * 32, j + 1 );
  32459. if( ( ret = mbedtls_sha256_starts_ret( &ctx, k ) ) != 0 )
  32460. goto fail;
  32461. if( j == 2 )
  32462. {
  32463. memset( buf, 'a', buflen = 1000 );
  32464. for( j = 0; j < 1000; j++ )
  32465. {
  32466. ret = mbedtls_sha256_update_ret( &ctx, buf, buflen );
  32467. if( ret != 0 )
  32468. goto fail;
  32469. }
  32470. }
  32471. else
  32472. {
  32473. ret = mbedtls_sha256_update_ret( &ctx, sha256_test_buf[j],
  32474. sha256_test_buflen[j] );
  32475. if( ret != 0 )
  32476. goto fail;
  32477. }
  32478. if( ( ret = mbedtls_sha256_finish_ret( &ctx, sha256sum ) ) != 0 )
  32479. goto fail;
  32480. if( memcmp( sha256sum, sha256_test_sum[i], 32 - k * 4 ) != 0 )
  32481. {
  32482. ret = 1;
  32483. goto fail;
  32484. }
  32485. if( verbose != 0 )
  32486. mbedtls_printf( "passed\n" );
  32487. }
  32488. if( verbose != 0 )
  32489. mbedtls_printf( "\n" );
  32490. goto exit;
  32491. fail:
  32492. if( verbose != 0 )
  32493. mbedtls_printf( "failed\n" );
  32494. exit:
  32495. mbedtls_sha256_free( &ctx );
  32496. mbedtls_free( buf );
  32497. return( ret );
  32498. }
  32499. #endif /* MBEDTLS_SELF_TEST */
  32500. #endif /* MBEDTLS_SHA256_C */
  32501. /*
  32502. Amalgamated build undefines
  32503. */
  32504. #undef ADD
  32505. #undef BC
  32506. #undef BEFORE_COLON
  32507. #undef F
  32508. #undef F0
  32509. #undef F1
  32510. #undef F2
  32511. #undef F3
  32512. #undef F4
  32513. #undef F5
  32514. #undef FSb
  32515. #undef K
  32516. #undef KK
  32517. #undef P
  32518. #undef R
  32519. #undef ROTR
  32520. #undef S
  32521. #undef S0
  32522. #undef S1
  32523. #undef S2
  32524. #undef S3
  32525. #undef SAFE_SNPRINTF
  32526. #undef SHR
  32527. #undef close
  32528. #undef read
  32529. #undef supported_init
  32530. #undef write
  32531. /********* Start of file library/sha512.c ************/
  32532. /*
  32533. * FIPS-180-2 compliant SHA-384/512 implementation
  32534. *
  32535. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  32536. * SPDX-License-Identifier: Apache-2.0
  32537. *
  32538. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  32539. * not use this file except in compliance with the License.
  32540. * You may obtain a copy of the License at
  32541. *
  32542. * http://www.apache.org/licenses/LICENSE-2.0
  32543. *
  32544. * Unless required by applicable law or agreed to in writing, software
  32545. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  32546. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  32547. * See the License for the specific language governing permissions and
  32548. * limitations under the License.
  32549. *
  32550. * This file is part of mbed TLS (https://tls.mbed.org)
  32551. */
  32552. /*
  32553. * The SHA-512 Secure Hash Standard was published by NIST in 2002.
  32554. *
  32555. * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
  32556. */
  32557. #if !defined(MBEDTLS_CONFIG_FILE)
  32558. #else
  32559. #endif
  32560. #if defined(MBEDTLS_SHA512_C)
  32561. #if defined(_MSC_VER) || defined(__WATCOMC__)
  32562. #define UL64(x) x##ui64
  32563. #else
  32564. #define UL64(x) x##ULL
  32565. #endif
  32566. #include <string.h>
  32567. #if defined(MBEDTLS_SELF_TEST)
  32568. #if defined(MBEDTLS_PLATFORM_C)
  32569. #else
  32570. #include <stdio.h>
  32571. #include <stdlib.h>
  32572. #define mbedtls_printf printf
  32573. #define mbedtls_calloc calloc
  32574. #define mbedtls_free free
  32575. #endif /* MBEDTLS_PLATFORM_C */
  32576. #endif /* MBEDTLS_SELF_TEST */
  32577. #if !defined(MBEDTLS_SHA512_ALT)
  32578. /* Implementation that should never be optimized out by the compiler */
  32579. /* zeroize was here */
  32580. /*
  32581. * 64-bit integer manipulation macros (big endian)
  32582. */
  32583. #ifndef GET_UINT64_BE
  32584. #define GET_UINT64_BE(n,b,i) \
  32585. { \
  32586. (n) = ( (uint64_t) (b)[(i) ] << 56 ) \
  32587. | ( (uint64_t) (b)[(i) + 1] << 48 ) \
  32588. | ( (uint64_t) (b)[(i) + 2] << 40 ) \
  32589. | ( (uint64_t) (b)[(i) + 3] << 32 ) \
  32590. | ( (uint64_t) (b)[(i) + 4] << 24 ) \
  32591. | ( (uint64_t) (b)[(i) + 5] << 16 ) \
  32592. | ( (uint64_t) (b)[(i) + 6] << 8 ) \
  32593. | ( (uint64_t) (b)[(i) + 7] ); \
  32594. }
  32595. #endif /* GET_UINT64_BE */
  32596. #ifndef PUT_UINT64_BE
  32597. #define PUT_UINT64_BE(n,b,i) \
  32598. { \
  32599. (b)[(i) ] = (unsigned char) ( (n) >> 56 ); \
  32600. (b)[(i) + 1] = (unsigned char) ( (n) >> 48 ); \
  32601. (b)[(i) + 2] = (unsigned char) ( (n) >> 40 ); \
  32602. (b)[(i) + 3] = (unsigned char) ( (n) >> 32 ); \
  32603. (b)[(i) + 4] = (unsigned char) ( (n) >> 24 ); \
  32604. (b)[(i) + 5] = (unsigned char) ( (n) >> 16 ); \
  32605. (b)[(i) + 6] = (unsigned char) ( (n) >> 8 ); \
  32606. (b)[(i) + 7] = (unsigned char) ( (n) ); \
  32607. }
  32608. #endif /* PUT_UINT64_BE */
  32609. void mbedtls_sha512_init( mbedtls_sha512_context *ctx )
  32610. {
  32611. memset( ctx, 0, sizeof( mbedtls_sha512_context ) );
  32612. }
  32613. void mbedtls_sha512_free( mbedtls_sha512_context *ctx )
  32614. {
  32615. if( ctx == NULL )
  32616. return;
  32617. mbedtls_zeroize( ctx, sizeof( mbedtls_sha512_context ) );
  32618. }
  32619. void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
  32620. const mbedtls_sha512_context *src )
  32621. {
  32622. *dst = *src;
  32623. }
  32624. /*
  32625. * SHA-512 context setup
  32626. */
  32627. int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 )
  32628. {
  32629. ctx->total[0] = 0;
  32630. ctx->total[1] = 0;
  32631. if( is384 == 0 )
  32632. {
  32633. /* SHA-512 */
  32634. ctx->state[0] = UL64(0x6A09E667F3BCC908);
  32635. ctx->state[1] = UL64(0xBB67AE8584CAA73B);
  32636. ctx->state[2] = UL64(0x3C6EF372FE94F82B);
  32637. ctx->state[3] = UL64(0xA54FF53A5F1D36F1);
  32638. ctx->state[4] = UL64(0x510E527FADE682D1);
  32639. ctx->state[5] = UL64(0x9B05688C2B3E6C1F);
  32640. ctx->state[6] = UL64(0x1F83D9ABFB41BD6B);
  32641. ctx->state[7] = UL64(0x5BE0CD19137E2179);
  32642. }
  32643. else
  32644. {
  32645. /* SHA-384 */
  32646. ctx->state[0] = UL64(0xCBBB9D5DC1059ED8);
  32647. ctx->state[1] = UL64(0x629A292A367CD507);
  32648. ctx->state[2] = UL64(0x9159015A3070DD17);
  32649. ctx->state[3] = UL64(0x152FECD8F70E5939);
  32650. ctx->state[4] = UL64(0x67332667FFC00B31);
  32651. ctx->state[5] = UL64(0x8EB44A8768581511);
  32652. ctx->state[6] = UL64(0xDB0C2E0D64F98FA7);
  32653. ctx->state[7] = UL64(0x47B5481DBEFA4FA4);
  32654. }
  32655. ctx->is384 = is384;
  32656. return( 0 );
  32657. }
  32658. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  32659. void mbedtls_sha512_starts( mbedtls_sha512_context *ctx,
  32660. int is384 )
  32661. {
  32662. mbedtls_sha512_starts_ret( ctx, is384 );
  32663. }
  32664. #endif
  32665. #if !defined(MBEDTLS_SHA512_PROCESS_ALT)
  32666. /*
  32667. * Round constants
  32668. */
  32669. #define K KK
  32670. static const uint64_t K[80] =
  32671. {
  32672. UL64(0x428A2F98D728AE22), UL64(0x7137449123EF65CD),
  32673. UL64(0xB5C0FBCFEC4D3B2F), UL64(0xE9B5DBA58189DBBC),
  32674. UL64(0x3956C25BF348B538), UL64(0x59F111F1B605D019),
  32675. UL64(0x923F82A4AF194F9B), UL64(0xAB1C5ED5DA6D8118),
  32676. UL64(0xD807AA98A3030242), UL64(0x12835B0145706FBE),
  32677. UL64(0x243185BE4EE4B28C), UL64(0x550C7DC3D5FFB4E2),
  32678. UL64(0x72BE5D74F27B896F), UL64(0x80DEB1FE3B1696B1),
  32679. UL64(0x9BDC06A725C71235), UL64(0xC19BF174CF692694),
  32680. UL64(0xE49B69C19EF14AD2), UL64(0xEFBE4786384F25E3),
  32681. UL64(0x0FC19DC68B8CD5B5), UL64(0x240CA1CC77AC9C65),
  32682. UL64(0x2DE92C6F592B0275), UL64(0x4A7484AA6EA6E483),
  32683. UL64(0x5CB0A9DCBD41FBD4), UL64(0x76F988DA831153B5),
  32684. UL64(0x983E5152EE66DFAB), UL64(0xA831C66D2DB43210),
  32685. UL64(0xB00327C898FB213F), UL64(0xBF597FC7BEEF0EE4),
  32686. UL64(0xC6E00BF33DA88FC2), UL64(0xD5A79147930AA725),
  32687. UL64(0x06CA6351E003826F), UL64(0x142929670A0E6E70),
  32688. UL64(0x27B70A8546D22FFC), UL64(0x2E1B21385C26C926),
  32689. UL64(0x4D2C6DFC5AC42AED), UL64(0x53380D139D95B3DF),
  32690. UL64(0x650A73548BAF63DE), UL64(0x766A0ABB3C77B2A8),
  32691. UL64(0x81C2C92E47EDAEE6), UL64(0x92722C851482353B),
  32692. UL64(0xA2BFE8A14CF10364), UL64(0xA81A664BBC423001),
  32693. UL64(0xC24B8B70D0F89791), UL64(0xC76C51A30654BE30),
  32694. UL64(0xD192E819D6EF5218), UL64(0xD69906245565A910),
  32695. UL64(0xF40E35855771202A), UL64(0x106AA07032BBD1B8),
  32696. UL64(0x19A4C116B8D2D0C8), UL64(0x1E376C085141AB53),
  32697. UL64(0x2748774CDF8EEB99), UL64(0x34B0BCB5E19B48A8),
  32698. UL64(0x391C0CB3C5C95A63), UL64(0x4ED8AA4AE3418ACB),
  32699. UL64(0x5B9CCA4F7763E373), UL64(0x682E6FF3D6B2B8A3),
  32700. UL64(0x748F82EE5DEFB2FC), UL64(0x78A5636F43172F60),
  32701. UL64(0x84C87814A1F0AB72), UL64(0x8CC702081A6439EC),
  32702. UL64(0x90BEFFFA23631E28), UL64(0xA4506CEBDE82BDE9),
  32703. UL64(0xBEF9A3F7B2C67915), UL64(0xC67178F2E372532B),
  32704. UL64(0xCA273ECEEA26619C), UL64(0xD186B8C721C0C207),
  32705. UL64(0xEADA7DD6CDE0EB1E), UL64(0xF57D4F7FEE6ED178),
  32706. UL64(0x06F067AA72176FBA), UL64(0x0A637DC5A2C898A6),
  32707. UL64(0x113F9804BEF90DAE), UL64(0x1B710B35131C471B),
  32708. UL64(0x28DB77F523047D84), UL64(0x32CAAB7B40C72493),
  32709. UL64(0x3C9EBE0A15C9BEBC), UL64(0x431D67C49C100D4C),
  32710. UL64(0x4CC5D4BECB3E42B6), UL64(0x597F299CFC657E2A),
  32711. UL64(0x5FCB6FAB3AD6FAEC), UL64(0x6C44198C4A475817)
  32712. };
  32713. int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
  32714. const unsigned char data[128] )
  32715. {
  32716. int i;
  32717. uint64_t temp1, temp2, W[80];
  32718. uint64_t A, B, C, D, E, F, G, H;
  32719. #define SHR(x,n) (x >> n)
  32720. #define ROTR(x,n) (SHR(x,n) | (x << (64 - n)))
  32721. #define S0(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7))
  32722. #define S1(x) (ROTR(x,19) ^ ROTR(x,61) ^ SHR(x, 6))
  32723. #define S2(x) (ROTR(x,28) ^ ROTR(x,34) ^ ROTR(x,39))
  32724. #define S3(x) (ROTR(x,14) ^ ROTR(x,18) ^ ROTR(x,41))
  32725. #define F0(x,y,z) ((x & y) | (z & (x | y)))
  32726. #define F1(x,y,z) (z ^ (x & (y ^ z)))
  32727. #define P(a,b,c,d,e,f,g,h,x,K) \
  32728. { \
  32729. temp1 = h + S3(e) + F1(e,f,g) + K + x; \
  32730. temp2 = S2(a) + F0(a,b,c); \
  32731. d += temp1; h = temp1 + temp2; \
  32732. }
  32733. for( i = 0; i < 16; i++ )
  32734. {
  32735. GET_UINT64_BE( W[i], data, i << 3 );
  32736. }
  32737. for( ; i < 80; i++ )
  32738. {
  32739. W[i] = S1(W[i - 2]) + W[i - 7] +
  32740. S0(W[i - 15]) + W[i - 16];
  32741. }
  32742. A = ctx->state[0];
  32743. B = ctx->state[1];
  32744. C = ctx->state[2];
  32745. D = ctx->state[3];
  32746. E = ctx->state[4];
  32747. F = ctx->state[5];
  32748. G = ctx->state[6];
  32749. H = ctx->state[7];
  32750. i = 0;
  32751. do
  32752. {
  32753. P( A, B, C, D, E, F, G, H, W[i], K[i] ); i++;
  32754. P( H, A, B, C, D, E, F, G, W[i], K[i] ); i++;
  32755. P( G, H, A, B, C, D, E, F, W[i], K[i] ); i++;
  32756. P( F, G, H, A, B, C, D, E, W[i], K[i] ); i++;
  32757. P( E, F, G, H, A, B, C, D, W[i], K[i] ); i++;
  32758. P( D, E, F, G, H, A, B, C, W[i], K[i] ); i++;
  32759. P( C, D, E, F, G, H, A, B, W[i], K[i] ); i++;
  32760. P( B, C, D, E, F, G, H, A, W[i], K[i] ); i++;
  32761. }
  32762. while( i < 80 );
  32763. ctx->state[0] += A;
  32764. ctx->state[1] += B;
  32765. ctx->state[2] += C;
  32766. ctx->state[3] += D;
  32767. ctx->state[4] += E;
  32768. ctx->state[5] += F;
  32769. ctx->state[6] += G;
  32770. ctx->state[7] += H;
  32771. return( 0 );
  32772. }
  32773. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  32774. void mbedtls_sha512_process( mbedtls_sha512_context *ctx,
  32775. const unsigned char data[128] )
  32776. {
  32777. mbedtls_internal_sha512_process( ctx, data );
  32778. }
  32779. #endif
  32780. #endif /* !MBEDTLS_SHA512_PROCESS_ALT */
  32781. /*
  32782. * SHA-512 process buffer
  32783. */
  32784. int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx,
  32785. const unsigned char *input,
  32786. size_t ilen )
  32787. {
  32788. int ret;
  32789. size_t fill;
  32790. unsigned int left;
  32791. if( ilen == 0 )
  32792. return( 0 );
  32793. left = (unsigned int) (ctx->total[0] & 0x7F);
  32794. fill = 128 - left;
  32795. ctx->total[0] += (uint64_t) ilen;
  32796. if( ctx->total[0] < (uint64_t) ilen )
  32797. ctx->total[1]++;
  32798. if( left && ilen >= fill )
  32799. {
  32800. memcpy( (void *) (ctx->buffer + left), input, fill );
  32801. if( ( ret = mbedtls_internal_sha512_process( ctx, ctx->buffer ) ) != 0 )
  32802. return( ret );
  32803. input += fill;
  32804. ilen -= fill;
  32805. left = 0;
  32806. }
  32807. while( ilen >= 128 )
  32808. {
  32809. if( ( ret = mbedtls_internal_sha512_process( ctx, input ) ) != 0 )
  32810. return( ret );
  32811. input += 128;
  32812. ilen -= 128;
  32813. }
  32814. if( ilen > 0 )
  32815. memcpy( (void *) (ctx->buffer + left), input, ilen );
  32816. return( 0 );
  32817. }
  32818. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  32819. void mbedtls_sha512_update( mbedtls_sha512_context *ctx,
  32820. const unsigned char *input,
  32821. size_t ilen )
  32822. {
  32823. mbedtls_sha512_update_ret( ctx, input, ilen );
  32824. }
  32825. #endif
  32826. static const unsigned char sha512_padding[128] =
  32827. {
  32828. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32829. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32830. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32831. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32832. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32833. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32834. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32835. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  32836. };
  32837. /*
  32838. * SHA-512 final digest
  32839. */
  32840. int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
  32841. unsigned char output[64] )
  32842. {
  32843. int ret;
  32844. size_t last, padn;
  32845. uint64_t high, low;
  32846. unsigned char msglen[16];
  32847. high = ( ctx->total[0] >> 61 )
  32848. | ( ctx->total[1] << 3 );
  32849. low = ( ctx->total[0] << 3 );
  32850. PUT_UINT64_BE( high, msglen, 0 );
  32851. PUT_UINT64_BE( low, msglen, 8 );
  32852. last = (size_t)( ctx->total[0] & 0x7F );
  32853. padn = ( last < 112 ) ? ( 112 - last ) : ( 240 - last );
  32854. if( ( ret = mbedtls_sha512_update_ret( ctx, sha512_padding, padn ) ) != 0 )
  32855. return( ret );
  32856. if( ( ret = mbedtls_sha512_update_ret( ctx, msglen, 16 ) ) != 0 )
  32857. return( ret );
  32858. PUT_UINT64_BE( ctx->state[0], output, 0 );
  32859. PUT_UINT64_BE( ctx->state[1], output, 8 );
  32860. PUT_UINT64_BE( ctx->state[2], output, 16 );
  32861. PUT_UINT64_BE( ctx->state[3], output, 24 );
  32862. PUT_UINT64_BE( ctx->state[4], output, 32 );
  32863. PUT_UINT64_BE( ctx->state[5], output, 40 );
  32864. if( ctx->is384 == 0 )
  32865. {
  32866. PUT_UINT64_BE( ctx->state[6], output, 48 );
  32867. PUT_UINT64_BE( ctx->state[7], output, 56 );
  32868. }
  32869. return( 0 );
  32870. }
  32871. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  32872. void mbedtls_sha512_finish( mbedtls_sha512_context *ctx,
  32873. unsigned char output[64] )
  32874. {
  32875. mbedtls_sha512_finish_ret( ctx, output );
  32876. }
  32877. #endif
  32878. #endif /* !MBEDTLS_SHA512_ALT */
  32879. /*
  32880. * output = SHA-512( input buffer )
  32881. */
  32882. int mbedtls_sha512_ret( const unsigned char *input,
  32883. size_t ilen,
  32884. unsigned char output[64],
  32885. int is384 )
  32886. {
  32887. int ret;
  32888. mbedtls_sha512_context ctx;
  32889. mbedtls_sha512_init( &ctx );
  32890. if( ( ret = mbedtls_sha512_starts_ret( &ctx, is384 ) ) != 0 )
  32891. goto exit;
  32892. if( ( ret = mbedtls_sha512_update_ret( &ctx, input, ilen ) ) != 0 )
  32893. goto exit;
  32894. if( ( ret = mbedtls_sha512_finish_ret( &ctx, output ) ) != 0 )
  32895. goto exit;
  32896. exit:
  32897. mbedtls_sha512_free( &ctx );
  32898. return( ret );
  32899. }
  32900. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  32901. void mbedtls_sha512( const unsigned char *input,
  32902. size_t ilen,
  32903. unsigned char output[64],
  32904. int is384 )
  32905. {
  32906. mbedtls_sha512_ret( input, ilen, output, is384 );
  32907. }
  32908. #endif
  32909. #if defined(MBEDTLS_SELF_TEST)
  32910. /*
  32911. * FIPS-180-2 test vectors
  32912. */
  32913. static const unsigned char sha512_test_buf[3][113] =
  32914. {
  32915. { "abc" },
  32916. { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
  32917. "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" },
  32918. { "" }
  32919. };
  32920. static const size_t sha512_test_buflen[3] =
  32921. {
  32922. 3, 112, 1000
  32923. };
  32924. static const unsigned char sha512_test_sum[6][64] =
  32925. {
  32926. /*
  32927. * SHA-384 test vectors
  32928. */
  32929. { 0xCB, 0x00, 0x75, 0x3F, 0x45, 0xA3, 0x5E, 0x8B,
  32930. 0xB5, 0xA0, 0x3D, 0x69, 0x9A, 0xC6, 0x50, 0x07,
  32931. 0x27, 0x2C, 0x32, 0xAB, 0x0E, 0xDE, 0xD1, 0x63,
  32932. 0x1A, 0x8B, 0x60, 0x5A, 0x43, 0xFF, 0x5B, 0xED,
  32933. 0x80, 0x86, 0x07, 0x2B, 0xA1, 0xE7, 0xCC, 0x23,
  32934. 0x58, 0xBA, 0xEC, 0xA1, 0x34, 0xC8, 0x25, 0xA7 },
  32935. { 0x09, 0x33, 0x0C, 0x33, 0xF7, 0x11, 0x47, 0xE8,
  32936. 0x3D, 0x19, 0x2F, 0xC7, 0x82, 0xCD, 0x1B, 0x47,
  32937. 0x53, 0x11, 0x1B, 0x17, 0x3B, 0x3B, 0x05, 0xD2,
  32938. 0x2F, 0xA0, 0x80, 0x86, 0xE3, 0xB0, 0xF7, 0x12,
  32939. 0xFC, 0xC7, 0xC7, 0x1A, 0x55, 0x7E, 0x2D, 0xB9,
  32940. 0x66, 0xC3, 0xE9, 0xFA, 0x91, 0x74, 0x60, 0x39 },
  32941. { 0x9D, 0x0E, 0x18, 0x09, 0x71, 0x64, 0x74, 0xCB,
  32942. 0x08, 0x6E, 0x83, 0x4E, 0x31, 0x0A, 0x4A, 0x1C,
  32943. 0xED, 0x14, 0x9E, 0x9C, 0x00, 0xF2, 0x48, 0x52,
  32944. 0x79, 0x72, 0xCE, 0xC5, 0x70, 0x4C, 0x2A, 0x5B,
  32945. 0x07, 0xB8, 0xB3, 0xDC, 0x38, 0xEC, 0xC4, 0xEB,
  32946. 0xAE, 0x97, 0xDD, 0xD8, 0x7F, 0x3D, 0x89, 0x85 },
  32947. /*
  32948. * SHA-512 test vectors
  32949. */
  32950. { 0xDD, 0xAF, 0x35, 0xA1, 0x93, 0x61, 0x7A, 0xBA,
  32951. 0xCC, 0x41, 0x73, 0x49, 0xAE, 0x20, 0x41, 0x31,
  32952. 0x12, 0xE6, 0xFA, 0x4E, 0x89, 0xA9, 0x7E, 0xA2,
  32953. 0x0A, 0x9E, 0xEE, 0xE6, 0x4B, 0x55, 0xD3, 0x9A,
  32954. 0x21, 0x92, 0x99, 0x2A, 0x27, 0x4F, 0xC1, 0xA8,
  32955. 0x36, 0xBA, 0x3C, 0x23, 0xA3, 0xFE, 0xEB, 0xBD,
  32956. 0x45, 0x4D, 0x44, 0x23, 0x64, 0x3C, 0xE8, 0x0E,
  32957. 0x2A, 0x9A, 0xC9, 0x4F, 0xA5, 0x4C, 0xA4, 0x9F },
  32958. { 0x8E, 0x95, 0x9B, 0x75, 0xDA, 0xE3, 0x13, 0xDA,
  32959. 0x8C, 0xF4, 0xF7, 0x28, 0x14, 0xFC, 0x14, 0x3F,
  32960. 0x8F, 0x77, 0x79, 0xC6, 0xEB, 0x9F, 0x7F, 0xA1,
  32961. 0x72, 0x99, 0xAE, 0xAD, 0xB6, 0x88, 0x90, 0x18,
  32962. 0x50, 0x1D, 0x28, 0x9E, 0x49, 0x00, 0xF7, 0xE4,
  32963. 0x33, 0x1B, 0x99, 0xDE, 0xC4, 0xB5, 0x43, 0x3A,
  32964. 0xC7, 0xD3, 0x29, 0xEE, 0xB6, 0xDD, 0x26, 0x54,
  32965. 0x5E, 0x96, 0xE5, 0x5B, 0x87, 0x4B, 0xE9, 0x09 },
  32966. { 0xE7, 0x18, 0x48, 0x3D, 0x0C, 0xE7, 0x69, 0x64,
  32967. 0x4E, 0x2E, 0x42, 0xC7, 0xBC, 0x15, 0xB4, 0x63,
  32968. 0x8E, 0x1F, 0x98, 0xB1, 0x3B, 0x20, 0x44, 0x28,
  32969. 0x56, 0x32, 0xA8, 0x03, 0xAF, 0xA9, 0x73, 0xEB,
  32970. 0xDE, 0x0F, 0xF2, 0x44, 0x87, 0x7E, 0xA6, 0x0A,
  32971. 0x4C, 0xB0, 0x43, 0x2C, 0xE5, 0x77, 0xC3, 0x1B,
  32972. 0xEB, 0x00, 0x9C, 0x5C, 0x2C, 0x49, 0xAA, 0x2E,
  32973. 0x4E, 0xAD, 0xB2, 0x17, 0xAD, 0x8C, 0xC0, 0x9B }
  32974. };
  32975. /*
  32976. * Checkup routine
  32977. */
  32978. int mbedtls_sha512_self_test( int verbose )
  32979. {
  32980. int i, j, k, buflen, ret = 0;
  32981. unsigned char *buf;
  32982. unsigned char sha512sum[64];
  32983. mbedtls_sha512_context ctx;
  32984. buf = mbedtls_calloc( 1024, sizeof(unsigned char) );
  32985. if( NULL == buf )
  32986. {
  32987. if( verbose != 0 )
  32988. mbedtls_printf( "Buffer allocation failed\n" );
  32989. return( 1 );
  32990. }
  32991. mbedtls_sha512_init( &ctx );
  32992. for( i = 0; i < 6; i++ )
  32993. {
  32994. j = i % 3;
  32995. k = i < 3;
  32996. if( verbose != 0 )
  32997. mbedtls_printf( " SHA-%d test #%d: ", 512 - k * 128, j + 1 );
  32998. if( ( ret = mbedtls_sha512_starts_ret( &ctx, k ) ) != 0 )
  32999. goto fail;
  33000. if( j == 2 )
  33001. {
  33002. memset( buf, 'a', buflen = 1000 );
  33003. for( j = 0; j < 1000; j++ )
  33004. {
  33005. ret = mbedtls_sha512_update_ret( &ctx, buf, buflen );
  33006. if( ret != 0 )
  33007. goto fail;
  33008. }
  33009. }
  33010. else
  33011. {
  33012. ret = mbedtls_sha512_update_ret( &ctx, sha512_test_buf[j],
  33013. sha512_test_buflen[j] );
  33014. if( ret != 0 )
  33015. goto fail;
  33016. }
  33017. if( ( ret = mbedtls_sha512_finish_ret( &ctx, sha512sum ) ) != 0 )
  33018. goto fail;
  33019. if( memcmp( sha512sum, sha512_test_sum[i], 64 - k * 16 ) != 0 )
  33020. {
  33021. ret = 1;
  33022. goto fail;
  33023. }
  33024. if( verbose != 0 )
  33025. mbedtls_printf( "passed\n" );
  33026. }
  33027. if( verbose != 0 )
  33028. mbedtls_printf( "\n" );
  33029. goto exit;
  33030. fail:
  33031. if( verbose != 0 )
  33032. mbedtls_printf( "failed\n" );
  33033. exit:
  33034. mbedtls_sha512_free( &ctx );
  33035. mbedtls_free( buf );
  33036. return( ret );
  33037. }
  33038. #endif /* MBEDTLS_SELF_TEST */
  33039. #endif /* MBEDTLS_SHA512_C */
  33040. /*
  33041. Amalgamated build undefines
  33042. */
  33043. #undef ADD
  33044. #undef BC
  33045. #undef BEFORE_COLON
  33046. #undef F
  33047. #undef F0
  33048. #undef F1
  33049. #undef F2
  33050. #undef F3
  33051. #undef F4
  33052. #undef F5
  33053. #undef FSb
  33054. #undef K
  33055. #undef KK
  33056. #undef P
  33057. #undef R
  33058. #undef ROTR
  33059. #undef S
  33060. #undef S0
  33061. #undef S1
  33062. #undef S2
  33063. #undef S3
  33064. #undef SAFE_SNPRINTF
  33065. #undef SHR
  33066. #undef close
  33067. #undef read
  33068. #undef supported_init
  33069. #undef write
  33070. /********* Start of file library/ssl_cache.c ************/
  33071. /*
  33072. * SSL session cache implementation
  33073. *
  33074. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  33075. * SPDX-License-Identifier: Apache-2.0
  33076. *
  33077. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  33078. * not use this file except in compliance with the License.
  33079. * You may obtain a copy of the License at
  33080. *
  33081. * http://www.apache.org/licenses/LICENSE-2.0
  33082. *
  33083. * Unless required by applicable law or agreed to in writing, software
  33084. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  33085. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  33086. * See the License for the specific language governing permissions and
  33087. * limitations under the License.
  33088. *
  33089. * This file is part of mbed TLS (https://tls.mbed.org)
  33090. */
  33091. /*
  33092. * These session callbacks use a simple chained list
  33093. * to store and retrieve the session information.
  33094. */
  33095. #if !defined(MBEDTLS_CONFIG_FILE)
  33096. #else
  33097. #endif
  33098. #if defined(MBEDTLS_SSL_CACHE_C)
  33099. #if defined(MBEDTLS_PLATFORM_C)
  33100. #else
  33101. #include <stdlib.h>
  33102. #define mbedtls_calloc calloc
  33103. #define mbedtls_free free
  33104. #endif
  33105. #include <string.h>
  33106. void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache )
  33107. {
  33108. memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) );
  33109. cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
  33110. cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
  33111. #if defined(MBEDTLS_THREADING_C)
  33112. mbedtls_mutex_init( &cache->mutex );
  33113. #endif
  33114. }
  33115. int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session )
  33116. {
  33117. int ret = 1;
  33118. #if defined(MBEDTLS_HAVE_TIME)
  33119. mbedtls_time_t t = mbedtls_time( NULL );
  33120. #endif
  33121. mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
  33122. mbedtls_ssl_cache_entry *cur, *entry;
  33123. #if defined(MBEDTLS_THREADING_C)
  33124. if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
  33125. return( 1 );
  33126. #endif
  33127. cur = cache->chain;
  33128. entry = NULL;
  33129. while( cur != NULL )
  33130. {
  33131. entry = cur;
  33132. cur = cur->next;
  33133. #if defined(MBEDTLS_HAVE_TIME)
  33134. if( cache->timeout != 0 &&
  33135. (int) ( t - entry->timestamp ) > cache->timeout )
  33136. continue;
  33137. #endif
  33138. if( session->ciphersuite != entry->session.ciphersuite ||
  33139. session->compression != entry->session.compression ||
  33140. session->id_len != entry->session.id_len )
  33141. continue;
  33142. if( memcmp( session->id, entry->session.id,
  33143. entry->session.id_len ) != 0 )
  33144. continue;
  33145. memcpy( session->master, entry->session.master, 48 );
  33146. session->verify_result = entry->session.verify_result;
  33147. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  33148. /*
  33149. * Restore peer certificate (without rest of the original chain)
  33150. */
  33151. if( entry->peer_cert.p != NULL )
  33152. {
  33153. if( ( session->peer_cert = mbedtls_calloc( 1,
  33154. sizeof(mbedtls_x509_crt) ) ) == NULL )
  33155. {
  33156. ret = 1;
  33157. goto exit;
  33158. }
  33159. mbedtls_x509_crt_init( session->peer_cert );
  33160. if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p,
  33161. entry->peer_cert.len ) != 0 )
  33162. {
  33163. mbedtls_free( session->peer_cert );
  33164. session->peer_cert = NULL;
  33165. ret = 1;
  33166. goto exit;
  33167. }
  33168. }
  33169. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  33170. ret = 0;
  33171. goto exit;
  33172. }
  33173. exit:
  33174. #if defined(MBEDTLS_THREADING_C)
  33175. if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
  33176. ret = 1;
  33177. #endif
  33178. return( ret );
  33179. }
  33180. int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session )
  33181. {
  33182. int ret = 1;
  33183. #if defined(MBEDTLS_HAVE_TIME)
  33184. mbedtls_time_t t = mbedtls_time( NULL ), oldest = 0;
  33185. mbedtls_ssl_cache_entry *old = NULL;
  33186. #endif
  33187. mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
  33188. mbedtls_ssl_cache_entry *cur, *prv;
  33189. int count = 0;
  33190. #if defined(MBEDTLS_THREADING_C)
  33191. if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 )
  33192. return( ret );
  33193. #endif
  33194. cur = cache->chain;
  33195. prv = NULL;
  33196. while( cur != NULL )
  33197. {
  33198. count++;
  33199. #if defined(MBEDTLS_HAVE_TIME)
  33200. if( cache->timeout != 0 &&
  33201. (int) ( t - cur->timestamp ) > cache->timeout )
  33202. {
  33203. cur->timestamp = t;
  33204. break; /* expired, reuse this slot, update timestamp */
  33205. }
  33206. #endif
  33207. if( memcmp( session->id, cur->session.id, cur->session.id_len ) == 0 )
  33208. break; /* client reconnected, keep timestamp for session id */
  33209. #if defined(MBEDTLS_HAVE_TIME)
  33210. if( oldest == 0 || cur->timestamp < oldest )
  33211. {
  33212. oldest = cur->timestamp;
  33213. old = cur;
  33214. }
  33215. #endif
  33216. prv = cur;
  33217. cur = cur->next;
  33218. }
  33219. if( cur == NULL )
  33220. {
  33221. #if defined(MBEDTLS_HAVE_TIME)
  33222. /*
  33223. * Reuse oldest entry if max_entries reached
  33224. */
  33225. if( count >= cache->max_entries )
  33226. {
  33227. if( old == NULL )
  33228. {
  33229. ret = 1;
  33230. goto exit;
  33231. }
  33232. cur = old;
  33233. }
  33234. #else /* MBEDTLS_HAVE_TIME */
  33235. /*
  33236. * Reuse first entry in chain if max_entries reached,
  33237. * but move to last place
  33238. */
  33239. if( count >= cache->max_entries )
  33240. {
  33241. if( cache->chain == NULL )
  33242. {
  33243. ret = 1;
  33244. goto exit;
  33245. }
  33246. cur = cache->chain;
  33247. cache->chain = cur->next;
  33248. cur->next = NULL;
  33249. prv->next = cur;
  33250. }
  33251. #endif /* MBEDTLS_HAVE_TIME */
  33252. else
  33253. {
  33254. /*
  33255. * max_entries not reached, create new entry
  33256. */
  33257. cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) );
  33258. if( cur == NULL )
  33259. {
  33260. ret = 1;
  33261. goto exit;
  33262. }
  33263. if( prv == NULL )
  33264. cache->chain = cur;
  33265. else
  33266. prv->next = cur;
  33267. }
  33268. #if defined(MBEDTLS_HAVE_TIME)
  33269. cur->timestamp = t;
  33270. #endif
  33271. }
  33272. memcpy( &cur->session, session, sizeof( mbedtls_ssl_session ) );
  33273. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  33274. /*
  33275. * If we're reusing an entry, free its certificate first
  33276. */
  33277. if( cur->peer_cert.p != NULL )
  33278. {
  33279. mbedtls_free( cur->peer_cert.p );
  33280. memset( &cur->peer_cert, 0, sizeof(mbedtls_x509_buf) );
  33281. }
  33282. /*
  33283. * Store peer certificate
  33284. */
  33285. if( session->peer_cert != NULL )
  33286. {
  33287. cur->peer_cert.p = mbedtls_calloc( 1, session->peer_cert->raw.len );
  33288. if( cur->peer_cert.p == NULL )
  33289. {
  33290. ret = 1;
  33291. goto exit;
  33292. }
  33293. memcpy( cur->peer_cert.p, session->peer_cert->raw.p,
  33294. session->peer_cert->raw.len );
  33295. cur->peer_cert.len = session->peer_cert->raw.len;
  33296. cur->session.peer_cert = NULL;
  33297. }
  33298. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  33299. ret = 0;
  33300. exit:
  33301. #if defined(MBEDTLS_THREADING_C)
  33302. if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
  33303. ret = 1;
  33304. #endif
  33305. return( ret );
  33306. }
  33307. #if defined(MBEDTLS_HAVE_TIME)
  33308. void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
  33309. {
  33310. if( timeout < 0 ) timeout = 0;
  33311. cache->timeout = timeout;
  33312. }
  33313. #endif /* MBEDTLS_HAVE_TIME */
  33314. void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
  33315. {
  33316. if( max < 0 ) max = 0;
  33317. cache->max_entries = max;
  33318. }
  33319. void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
  33320. {
  33321. mbedtls_ssl_cache_entry *cur, *prv;
  33322. cur = cache->chain;
  33323. while( cur != NULL )
  33324. {
  33325. prv = cur;
  33326. cur = cur->next;
  33327. mbedtls_ssl_session_free( &prv->session );
  33328. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  33329. mbedtls_free( prv->peer_cert.p );
  33330. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  33331. mbedtls_free( prv );
  33332. }
  33333. #if defined(MBEDTLS_THREADING_C)
  33334. mbedtls_mutex_free( &cache->mutex );
  33335. #endif
  33336. cache->chain = NULL;
  33337. }
  33338. #endif /* MBEDTLS_SSL_CACHE_C */
  33339. /*
  33340. Amalgamated build undefines
  33341. */
  33342. #undef ADD
  33343. #undef BC
  33344. #undef BEFORE_COLON
  33345. #undef F
  33346. #undef F0
  33347. #undef F1
  33348. #undef F2
  33349. #undef F3
  33350. #undef F4
  33351. #undef F5
  33352. #undef FSb
  33353. #undef K
  33354. #undef KK
  33355. #undef P
  33356. #undef R
  33357. #undef ROTR
  33358. #undef S
  33359. #undef S0
  33360. #undef S1
  33361. #undef S2
  33362. #undef S3
  33363. #undef SAFE_SNPRINTF
  33364. #undef SHR
  33365. #undef close
  33366. #undef read
  33367. #undef supported_init
  33368. #undef write
  33369. /********* Start of file library/ssl_ciphersuites.c ************/
  33370. /**
  33371. * \file ssl_ciphersuites.c
  33372. *
  33373. * \brief SSL ciphersuites for mbed TLS
  33374. *
  33375. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  33376. * SPDX-License-Identifier: Apache-2.0
  33377. *
  33378. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  33379. * not use this file except in compliance with the License.
  33380. * You may obtain a copy of the License at
  33381. *
  33382. * http://www.apache.org/licenses/LICENSE-2.0
  33383. *
  33384. * Unless required by applicable law or agreed to in writing, software
  33385. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  33386. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  33387. * See the License for the specific language governing permissions and
  33388. * limitations under the License.
  33389. *
  33390. * This file is part of mbed TLS (https://tls.mbed.org)
  33391. */
  33392. #if !defined(MBEDTLS_CONFIG_FILE)
  33393. #else
  33394. #endif
  33395. #if defined(MBEDTLS_SSL_TLS_C)
  33396. #if defined(MBEDTLS_PLATFORM_C)
  33397. #else
  33398. #include <stdlib.h>
  33399. #endif
  33400. #include <string.h>
  33401. /*
  33402. * Ordered from most preferred to least preferred in terms of security.
  33403. *
  33404. * Current rule (except rc4, weak and null which come last):
  33405. * 1. By key exchange:
  33406. * Forward-secure non-PSK > forward-secure PSK > ECJPAKE > other non-PSK > other PSK
  33407. * 2. By key length and cipher:
  33408. * AES-256 > Camellia-256 > AES-128 > Camellia-128 > 3DES
  33409. * 3. By cipher mode when relevant GCM > CCM > CBC > CCM_8
  33410. * 4. By hash function used when relevant
  33411. * 5. By key exchange/auth again: EC > non-EC
  33412. */
  33413. static const int ciphersuite_preference[] =
  33414. {
  33415. #if defined(MBEDTLS_SSL_CIPHERSUITES)
  33416. MBEDTLS_SSL_CIPHERSUITES,
  33417. #else
  33418. /* All AES-256 ephemeral suites */
  33419. MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  33420. MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  33421. MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
  33422. MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM,
  33423. MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CCM,
  33424. MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
  33425. MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
  33426. MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,
  33427. MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  33428. MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  33429. MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
  33430. MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8,
  33431. MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CCM_8,
  33432. /* All CAMELLIA-256 ephemeral suites */
  33433. MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384,
  33434. MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384,
  33435. MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384,
  33436. MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384,
  33437. MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384,
  33438. MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256,
  33439. MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA,
  33440. /* All AES-128 ephemeral suites */
  33441. MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  33442. MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  33443. MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
  33444. MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM,
  33445. MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CCM,
  33446. MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
  33447. MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
  33448. MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
  33449. MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  33450. MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  33451. MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
  33452. MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8,
  33453. MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CCM_8,
  33454. /* All CAMELLIA-128 ephemeral suites */
  33455. MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256,
  33456. MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256,
  33457. MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256,
  33458. MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256,
  33459. MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256,
  33460. MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256,
  33461. MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA,
  33462. /* All remaining >= 128-bit ephemeral suites */
  33463. MBEDTLS_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,
  33464. MBEDTLS_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
  33465. MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA,
  33466. /* The PSK ephemeral suites */
  33467. MBEDTLS_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384,
  33468. MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CCM,
  33469. MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384,
  33470. MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384,
  33471. MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA,
  33472. MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA,
  33473. MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384,
  33474. MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384,
  33475. MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384,
  33476. MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CCM_8,
  33477. MBEDTLS_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256,
  33478. MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CCM,
  33479. MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256,
  33480. MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256,
  33481. MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA,
  33482. MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA,
  33483. MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256,
  33484. MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256,
  33485. MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256,
  33486. MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CCM_8,
  33487. MBEDTLS_TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA,
  33488. MBEDTLS_TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA,
  33489. /* The ECJPAKE suite */
  33490. MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8,
  33491. /* All AES-256 suites */
  33492. MBEDTLS_TLS_RSA_WITH_AES_256_GCM_SHA384,
  33493. MBEDTLS_TLS_RSA_WITH_AES_256_CCM,
  33494. MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA256,
  33495. MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA,
  33496. MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384,
  33497. MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384,
  33498. MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA,
  33499. MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384,
  33500. MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384,
  33501. MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA,
  33502. MBEDTLS_TLS_RSA_WITH_AES_256_CCM_8,
  33503. /* All CAMELLIA-256 suites */
  33504. MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384,
  33505. MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256,
  33506. MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,
  33507. MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384,
  33508. MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384,
  33509. MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384,
  33510. MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384,
  33511. /* All AES-128 suites */
  33512. MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256,
  33513. MBEDTLS_TLS_RSA_WITH_AES_128_CCM,
  33514. MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256,
  33515. MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA,
  33516. MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256,
  33517. MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256,
  33518. MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,
  33519. MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256,
  33520. MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256,
  33521. MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
  33522. MBEDTLS_TLS_RSA_WITH_AES_128_CCM_8,
  33523. /* All CAMELLIA-128 suites */
  33524. MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256,
  33525. MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256,
  33526. MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA,
  33527. MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256,
  33528. MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256,
  33529. MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256,
  33530. MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256,
  33531. /* All remaining >= 128-bit suites */
  33532. MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA,
  33533. MBEDTLS_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA,
  33534. MBEDTLS_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,
  33535. /* The RSA PSK suites */
  33536. MBEDTLS_TLS_RSA_PSK_WITH_AES_256_GCM_SHA384,
  33537. MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA384,
  33538. MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA,
  33539. MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384,
  33540. MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384,
  33541. MBEDTLS_TLS_RSA_PSK_WITH_AES_128_GCM_SHA256,
  33542. MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA256,
  33543. MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA,
  33544. MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256,
  33545. MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256,
  33546. MBEDTLS_TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA,
  33547. /* The PSK suites */
  33548. MBEDTLS_TLS_PSK_WITH_AES_256_GCM_SHA384,
  33549. MBEDTLS_TLS_PSK_WITH_AES_256_CCM,
  33550. MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA384,
  33551. MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA,
  33552. MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384,
  33553. MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384,
  33554. MBEDTLS_TLS_PSK_WITH_AES_256_CCM_8,
  33555. MBEDTLS_TLS_PSK_WITH_AES_128_GCM_SHA256,
  33556. MBEDTLS_TLS_PSK_WITH_AES_128_CCM,
  33557. MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA256,
  33558. MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA,
  33559. MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256,
  33560. MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256,
  33561. MBEDTLS_TLS_PSK_WITH_AES_128_CCM_8,
  33562. MBEDTLS_TLS_PSK_WITH_3DES_EDE_CBC_SHA,
  33563. /* RC4 suites */
  33564. MBEDTLS_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
  33565. MBEDTLS_TLS_ECDHE_RSA_WITH_RC4_128_SHA,
  33566. MBEDTLS_TLS_ECDHE_PSK_WITH_RC4_128_SHA,
  33567. MBEDTLS_TLS_DHE_PSK_WITH_RC4_128_SHA,
  33568. MBEDTLS_TLS_RSA_WITH_RC4_128_SHA,
  33569. MBEDTLS_TLS_RSA_WITH_RC4_128_MD5,
  33570. MBEDTLS_TLS_ECDH_RSA_WITH_RC4_128_SHA,
  33571. MBEDTLS_TLS_ECDH_ECDSA_WITH_RC4_128_SHA,
  33572. MBEDTLS_TLS_RSA_PSK_WITH_RC4_128_SHA,
  33573. MBEDTLS_TLS_PSK_WITH_RC4_128_SHA,
  33574. /* Weak suites */
  33575. MBEDTLS_TLS_DHE_RSA_WITH_DES_CBC_SHA,
  33576. MBEDTLS_TLS_RSA_WITH_DES_CBC_SHA,
  33577. /* NULL suites */
  33578. MBEDTLS_TLS_ECDHE_ECDSA_WITH_NULL_SHA,
  33579. MBEDTLS_TLS_ECDHE_RSA_WITH_NULL_SHA,
  33580. MBEDTLS_TLS_ECDHE_PSK_WITH_NULL_SHA384,
  33581. MBEDTLS_TLS_ECDHE_PSK_WITH_NULL_SHA256,
  33582. MBEDTLS_TLS_ECDHE_PSK_WITH_NULL_SHA,
  33583. MBEDTLS_TLS_DHE_PSK_WITH_NULL_SHA384,
  33584. MBEDTLS_TLS_DHE_PSK_WITH_NULL_SHA256,
  33585. MBEDTLS_TLS_DHE_PSK_WITH_NULL_SHA,
  33586. MBEDTLS_TLS_RSA_WITH_NULL_SHA256,
  33587. MBEDTLS_TLS_RSA_WITH_NULL_SHA,
  33588. MBEDTLS_TLS_RSA_WITH_NULL_MD5,
  33589. MBEDTLS_TLS_ECDH_RSA_WITH_NULL_SHA,
  33590. MBEDTLS_TLS_ECDH_ECDSA_WITH_NULL_SHA,
  33591. MBEDTLS_TLS_RSA_PSK_WITH_NULL_SHA384,
  33592. MBEDTLS_TLS_RSA_PSK_WITH_NULL_SHA256,
  33593. MBEDTLS_TLS_RSA_PSK_WITH_NULL_SHA,
  33594. MBEDTLS_TLS_PSK_WITH_NULL_SHA384,
  33595. MBEDTLS_TLS_PSK_WITH_NULL_SHA256,
  33596. MBEDTLS_TLS_PSK_WITH_NULL_SHA,
  33597. #endif /* MBEDTLS_SSL_CIPHERSUITES */
  33598. 0
  33599. };
  33600. static const mbedtls_ssl_ciphersuite_t ciphersuite_definitions[] =
  33601. {
  33602. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  33603. #if defined(MBEDTLS_AES_C)
  33604. #if defined(MBEDTLS_SHA1_C)
  33605. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  33606. { MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, "TLS-ECDHE-ECDSA-WITH-AES-128-CBC-SHA",
  33607. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA,
  33608. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  33609. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33610. 0 },
  33611. { MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, "TLS-ECDHE-ECDSA-WITH-AES-256-CBC-SHA",
  33612. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA,
  33613. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  33614. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33615. 0 },
  33616. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  33617. #endif /* MBEDTLS_SHA1_C */
  33618. #if defined(MBEDTLS_SHA256_C)
  33619. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  33620. { MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, "TLS-ECDHE-ECDSA-WITH-AES-128-CBC-SHA256",
  33621. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA,
  33622. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33623. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33624. 0 },
  33625. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  33626. #if defined(MBEDTLS_GCM_C)
  33627. { MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, "TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256",
  33628. MBEDTLS_CIPHER_AES_128_GCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA,
  33629. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33630. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33631. 0 },
  33632. #endif /* MBEDTLS_GCM_C */
  33633. #endif /* MBEDTLS_SHA256_C */
  33634. #if defined(MBEDTLS_SHA512_C)
  33635. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  33636. { MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, "TLS-ECDHE-ECDSA-WITH-AES-256-CBC-SHA384",
  33637. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA,
  33638. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33639. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33640. 0 },
  33641. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  33642. #if defined(MBEDTLS_GCM_C)
  33643. { MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, "TLS-ECDHE-ECDSA-WITH-AES-256-GCM-SHA384",
  33644. MBEDTLS_CIPHER_AES_256_GCM, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA,
  33645. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33646. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33647. 0 },
  33648. #endif /* MBEDTLS_GCM_C */
  33649. #endif /* MBEDTLS_SHA512_C */
  33650. #if defined(MBEDTLS_CCM_C)
  33651. { MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM, "TLS-ECDHE-ECDSA-WITH-AES-256-CCM",
  33652. MBEDTLS_CIPHER_AES_256_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA,
  33653. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33654. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33655. 0 },
  33656. { MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8, "TLS-ECDHE-ECDSA-WITH-AES-256-CCM-8",
  33657. MBEDTLS_CIPHER_AES_256_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA,
  33658. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33659. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33660. MBEDTLS_CIPHERSUITE_SHORT_TAG },
  33661. { MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM, "TLS-ECDHE-ECDSA-WITH-AES-128-CCM",
  33662. MBEDTLS_CIPHER_AES_128_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA,
  33663. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33664. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33665. 0 },
  33666. { MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8, "TLS-ECDHE-ECDSA-WITH-AES-128-CCM-8",
  33667. MBEDTLS_CIPHER_AES_128_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA,
  33668. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33669. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33670. MBEDTLS_CIPHERSUITE_SHORT_TAG },
  33671. #endif /* MBEDTLS_CCM_C */
  33672. #endif /* MBEDTLS_AES_C */
  33673. #if defined(MBEDTLS_CAMELLIA_C)
  33674. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  33675. #if defined(MBEDTLS_SHA256_C)
  33676. { MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256, "TLS-ECDHE-ECDSA-WITH-CAMELLIA-128-CBC-SHA256",
  33677. MBEDTLS_CIPHER_CAMELLIA_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA,
  33678. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  33679. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33680. 0 },
  33681. #endif /* MBEDTLS_SHA256_C */
  33682. #if defined(MBEDTLS_SHA512_C)
  33683. { MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384, "TLS-ECDHE-ECDSA-WITH-CAMELLIA-256-CBC-SHA384",
  33684. MBEDTLS_CIPHER_CAMELLIA_256_CBC, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA,
  33685. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  33686. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33687. 0 },
  33688. #endif /* MBEDTLS_SHA512_C */
  33689. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  33690. #if defined(MBEDTLS_GCM_C)
  33691. #if defined(MBEDTLS_SHA256_C)
  33692. { MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256, "TLS-ECDHE-ECDSA-WITH-CAMELLIA-128-GCM-SHA256",
  33693. MBEDTLS_CIPHER_CAMELLIA_128_GCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA,
  33694. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33695. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33696. 0 },
  33697. #endif /* MBEDTLS_SHA256_C */
  33698. #if defined(MBEDTLS_SHA512_C)
  33699. { MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384, "TLS-ECDHE-ECDSA-WITH-CAMELLIA-256-GCM-SHA384",
  33700. MBEDTLS_CIPHER_CAMELLIA_256_GCM, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA,
  33701. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33702. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33703. 0 },
  33704. #endif /* MBEDTLS_SHA512_C */
  33705. #endif /* MBEDTLS_GCM_C */
  33706. #endif /* MBEDTLS_CAMELLIA_C */
  33707. #if defined(MBEDTLS_DES_C)
  33708. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  33709. #if defined(MBEDTLS_SHA1_C)
  33710. { MBEDTLS_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, "TLS-ECDHE-ECDSA-WITH-3DES-EDE-CBC-SHA",
  33711. MBEDTLS_CIPHER_DES_EDE3_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA,
  33712. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  33713. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33714. 0 },
  33715. #endif /* MBEDTLS_SHA1_C */
  33716. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  33717. #endif /* MBEDTLS_DES_C */
  33718. #if defined(MBEDTLS_ARC4_C)
  33719. #if defined(MBEDTLS_SHA1_C)
  33720. { MBEDTLS_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, "TLS-ECDHE-ECDSA-WITH-RC4-128-SHA",
  33721. MBEDTLS_CIPHER_ARC4_128, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA,
  33722. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  33723. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33724. MBEDTLS_CIPHERSUITE_NODTLS },
  33725. #endif /* MBEDTLS_SHA1_C */
  33726. #endif /* MBEDTLS_ARC4_C */
  33727. #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
  33728. #if defined(MBEDTLS_SHA1_C)
  33729. { MBEDTLS_TLS_ECDHE_ECDSA_WITH_NULL_SHA, "TLS-ECDHE-ECDSA-WITH-NULL-SHA",
  33730. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA,
  33731. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  33732. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33733. MBEDTLS_CIPHERSUITE_WEAK },
  33734. #endif /* MBEDTLS_SHA1_C */
  33735. #endif /* MBEDTLS_CIPHER_NULL_CIPHER */
  33736. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  33737. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
  33738. #if defined(MBEDTLS_AES_C)
  33739. #if defined(MBEDTLS_SHA1_C)
  33740. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  33741. { MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, "TLS-ECDHE-RSA-WITH-AES-128-CBC-SHA",
  33742. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDHE_RSA,
  33743. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  33744. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33745. 0 },
  33746. { MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, "TLS-ECDHE-RSA-WITH-AES-256-CBC-SHA",
  33747. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDHE_RSA,
  33748. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  33749. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33750. 0 },
  33751. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  33752. #endif /* MBEDTLS_SHA1_C */
  33753. #if defined(MBEDTLS_SHA256_C)
  33754. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  33755. { MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, "TLS-ECDHE-RSA-WITH-AES-128-CBC-SHA256",
  33756. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDHE_RSA,
  33757. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33758. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33759. 0 },
  33760. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  33761. #if defined(MBEDTLS_GCM_C)
  33762. { MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, "TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256",
  33763. MBEDTLS_CIPHER_AES_128_GCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDHE_RSA,
  33764. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33765. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33766. 0 },
  33767. #endif /* MBEDTLS_GCM_C */
  33768. #endif /* MBEDTLS_SHA256_C */
  33769. #if defined(MBEDTLS_SHA512_C)
  33770. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  33771. { MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, "TLS-ECDHE-RSA-WITH-AES-256-CBC-SHA384",
  33772. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDHE_RSA,
  33773. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33774. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33775. 0 },
  33776. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  33777. #if defined(MBEDTLS_GCM_C)
  33778. { MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, "TLS-ECDHE-RSA-WITH-AES-256-GCM-SHA384",
  33779. MBEDTLS_CIPHER_AES_256_GCM, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDHE_RSA,
  33780. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33781. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33782. 0 },
  33783. #endif /* MBEDTLS_GCM_C */
  33784. #endif /* MBEDTLS_SHA512_C */
  33785. #endif /* MBEDTLS_AES_C */
  33786. #if defined(MBEDTLS_CAMELLIA_C)
  33787. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  33788. #if defined(MBEDTLS_SHA256_C)
  33789. { MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256, "TLS-ECDHE-RSA-WITH-CAMELLIA-128-CBC-SHA256",
  33790. MBEDTLS_CIPHER_CAMELLIA_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDHE_RSA,
  33791. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  33792. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33793. 0 },
  33794. #endif /* MBEDTLS_SHA256_C */
  33795. #if defined(MBEDTLS_SHA512_C)
  33796. { MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384, "TLS-ECDHE-RSA-WITH-CAMELLIA-256-CBC-SHA384",
  33797. MBEDTLS_CIPHER_CAMELLIA_256_CBC, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDHE_RSA,
  33798. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  33799. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33800. 0 },
  33801. #endif /* MBEDTLS_SHA512_C */
  33802. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  33803. #if defined(MBEDTLS_GCM_C)
  33804. #if defined(MBEDTLS_SHA256_C)
  33805. { MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256, "TLS-ECDHE-RSA-WITH-CAMELLIA-128-GCM-SHA256",
  33806. MBEDTLS_CIPHER_CAMELLIA_128_GCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDHE_RSA,
  33807. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33808. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33809. 0 },
  33810. #endif /* MBEDTLS_SHA256_C */
  33811. #if defined(MBEDTLS_SHA512_C)
  33812. { MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384, "TLS-ECDHE-RSA-WITH-CAMELLIA-256-GCM-SHA384",
  33813. MBEDTLS_CIPHER_CAMELLIA_256_GCM, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDHE_RSA,
  33814. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33815. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33816. 0 },
  33817. #endif /* MBEDTLS_SHA512_C */
  33818. #endif /* MBEDTLS_GCM_C */
  33819. #endif /* MBEDTLS_CAMELLIA_C */
  33820. #if defined(MBEDTLS_DES_C)
  33821. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  33822. #if defined(MBEDTLS_SHA1_C)
  33823. { MBEDTLS_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, "TLS-ECDHE-RSA-WITH-3DES-EDE-CBC-SHA",
  33824. MBEDTLS_CIPHER_DES_EDE3_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDHE_RSA,
  33825. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  33826. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33827. 0 },
  33828. #endif /* MBEDTLS_SHA1_C */
  33829. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  33830. #endif /* MBEDTLS_DES_C */
  33831. #if defined(MBEDTLS_ARC4_C)
  33832. #if defined(MBEDTLS_SHA1_C)
  33833. { MBEDTLS_TLS_ECDHE_RSA_WITH_RC4_128_SHA, "TLS-ECDHE-RSA-WITH-RC4-128-SHA",
  33834. MBEDTLS_CIPHER_ARC4_128, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDHE_RSA,
  33835. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  33836. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33837. MBEDTLS_CIPHERSUITE_NODTLS },
  33838. #endif /* MBEDTLS_SHA1_C */
  33839. #endif /* MBEDTLS_ARC4_C */
  33840. #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
  33841. #if defined(MBEDTLS_SHA1_C)
  33842. { MBEDTLS_TLS_ECDHE_RSA_WITH_NULL_SHA, "TLS-ECDHE-RSA-WITH-NULL-SHA",
  33843. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDHE_RSA,
  33844. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  33845. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33846. MBEDTLS_CIPHERSUITE_WEAK },
  33847. #endif /* MBEDTLS_SHA1_C */
  33848. #endif /* MBEDTLS_CIPHER_NULL_CIPHER */
  33849. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
  33850. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
  33851. #if defined(MBEDTLS_AES_C)
  33852. #if defined(MBEDTLS_SHA512_C) && defined(MBEDTLS_GCM_C)
  33853. { MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, "TLS-DHE-RSA-WITH-AES-256-GCM-SHA384",
  33854. MBEDTLS_CIPHER_AES_256_GCM, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  33855. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33856. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33857. 0 },
  33858. #endif /* MBEDTLS_SHA512_C && MBEDTLS_GCM_C */
  33859. #if defined(MBEDTLS_SHA256_C)
  33860. #if defined(MBEDTLS_GCM_C)
  33861. { MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, "TLS-DHE-RSA-WITH-AES-128-GCM-SHA256",
  33862. MBEDTLS_CIPHER_AES_128_GCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  33863. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33864. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33865. 0 },
  33866. #endif /* MBEDTLS_GCM_C */
  33867. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  33868. { MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, "TLS-DHE-RSA-WITH-AES-128-CBC-SHA256",
  33869. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  33870. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33871. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33872. 0 },
  33873. { MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256",
  33874. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  33875. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33876. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33877. 0 },
  33878. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  33879. #endif /* MBEDTLS_SHA256_C */
  33880. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  33881. #if defined(MBEDTLS_SHA1_C)
  33882. { MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA, "TLS-DHE-RSA-WITH-AES-128-CBC-SHA",
  33883. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  33884. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  33885. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33886. 0 },
  33887. { MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA, "TLS-DHE-RSA-WITH-AES-256-CBC-SHA",
  33888. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  33889. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  33890. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33891. 0 },
  33892. #endif /* MBEDTLS_SHA1_C */
  33893. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  33894. #if defined(MBEDTLS_CCM_C)
  33895. { MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CCM, "TLS-DHE-RSA-WITH-AES-256-CCM",
  33896. MBEDTLS_CIPHER_AES_256_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  33897. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33898. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33899. 0 },
  33900. { MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CCM_8, "TLS-DHE-RSA-WITH-AES-256-CCM-8",
  33901. MBEDTLS_CIPHER_AES_256_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  33902. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33903. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33904. MBEDTLS_CIPHERSUITE_SHORT_TAG },
  33905. { MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CCM, "TLS-DHE-RSA-WITH-AES-128-CCM",
  33906. MBEDTLS_CIPHER_AES_128_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  33907. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33908. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33909. 0 },
  33910. { MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CCM_8, "TLS-DHE-RSA-WITH-AES-128-CCM-8",
  33911. MBEDTLS_CIPHER_AES_128_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  33912. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33913. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33914. MBEDTLS_CIPHERSUITE_SHORT_TAG },
  33915. #endif /* MBEDTLS_CCM_C */
  33916. #endif /* MBEDTLS_AES_C */
  33917. #if defined(MBEDTLS_CAMELLIA_C)
  33918. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  33919. #if defined(MBEDTLS_SHA256_C)
  33920. { MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256, "TLS-DHE-RSA-WITH-CAMELLIA-128-CBC-SHA256",
  33921. MBEDTLS_CIPHER_CAMELLIA_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  33922. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33923. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33924. 0 },
  33925. { MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256, "TLS-DHE-RSA-WITH-CAMELLIA-256-CBC-SHA256",
  33926. MBEDTLS_CIPHER_CAMELLIA_256_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  33927. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33928. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33929. 0 },
  33930. #endif /* MBEDTLS_SHA256_C */
  33931. #if defined(MBEDTLS_SHA1_C)
  33932. { MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA, "TLS-DHE-RSA-WITH-CAMELLIA-128-CBC-SHA",
  33933. MBEDTLS_CIPHER_CAMELLIA_128_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  33934. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  33935. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33936. 0 },
  33937. { MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA, "TLS-DHE-RSA-WITH-CAMELLIA-256-CBC-SHA",
  33938. MBEDTLS_CIPHER_CAMELLIA_256_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  33939. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  33940. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33941. 0 },
  33942. #endif /* MBEDTLS_SHA1_C */
  33943. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  33944. #if defined(MBEDTLS_GCM_C)
  33945. #if defined(MBEDTLS_SHA256_C)
  33946. { MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256, "TLS-DHE-RSA-WITH-CAMELLIA-128-GCM-SHA256",
  33947. MBEDTLS_CIPHER_CAMELLIA_128_GCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  33948. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33949. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33950. 0 },
  33951. #endif /* MBEDTLS_SHA256_C */
  33952. #if defined(MBEDTLS_SHA512_C)
  33953. { MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384, "TLS-DHE-RSA-WITH-CAMELLIA-256-GCM-SHA384",
  33954. MBEDTLS_CIPHER_CAMELLIA_256_GCM, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  33955. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33956. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33957. 0 },
  33958. #endif /* MBEDTLS_SHA512_C */
  33959. #endif /* MBEDTLS_GCM_C */
  33960. #endif /* MBEDTLS_CAMELLIA_C */
  33961. #if defined(MBEDTLS_DES_C)
  33962. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  33963. #if defined(MBEDTLS_SHA1_C)
  33964. { MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA, "TLS-DHE-RSA-WITH-3DES-EDE-CBC-SHA",
  33965. MBEDTLS_CIPHER_DES_EDE3_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  33966. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  33967. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33968. 0 },
  33969. #endif /* MBEDTLS_SHA1_C */
  33970. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  33971. #endif /* MBEDTLS_DES_C */
  33972. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
  33973. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  33974. #if defined(MBEDTLS_AES_C)
  33975. #if defined(MBEDTLS_SHA512_C) && defined(MBEDTLS_GCM_C)
  33976. { MBEDTLS_TLS_RSA_WITH_AES_256_GCM_SHA384, "TLS-RSA-WITH-AES-256-GCM-SHA384",
  33977. MBEDTLS_CIPHER_AES_256_GCM, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_RSA,
  33978. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33979. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33980. 0 },
  33981. #endif /* MBEDTLS_SHA512_C && MBEDTLS_GCM_C */
  33982. #if defined(MBEDTLS_SHA256_C)
  33983. #if defined(MBEDTLS_GCM_C)
  33984. { MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256, "TLS-RSA-WITH-AES-128-GCM-SHA256",
  33985. MBEDTLS_CIPHER_AES_128_GCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_RSA,
  33986. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33987. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33988. 0 },
  33989. #endif /* MBEDTLS_GCM_C */
  33990. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  33991. { MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256, "TLS-RSA-WITH-AES-128-CBC-SHA256",
  33992. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_RSA,
  33993. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33994. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33995. 0 },
  33996. { MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA256, "TLS-RSA-WITH-AES-256-CBC-SHA256",
  33997. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_RSA,
  33998. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  33999. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34000. 0 },
  34001. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34002. #endif /* MBEDTLS_SHA256_C */
  34003. #if defined(MBEDTLS_SHA1_C)
  34004. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34005. { MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA, "TLS-RSA-WITH-AES-128-CBC-SHA",
  34006. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_RSA,
  34007. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34008. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34009. 0 },
  34010. { MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA, "TLS-RSA-WITH-AES-256-CBC-SHA",
  34011. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_RSA,
  34012. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34013. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34014. 0 },
  34015. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34016. #endif /* MBEDTLS_SHA1_C */
  34017. #if defined(MBEDTLS_CCM_C)
  34018. { MBEDTLS_TLS_RSA_WITH_AES_256_CCM, "TLS-RSA-WITH-AES-256-CCM",
  34019. MBEDTLS_CIPHER_AES_256_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_RSA,
  34020. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34021. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34022. 0 },
  34023. { MBEDTLS_TLS_RSA_WITH_AES_256_CCM_8, "TLS-RSA-WITH-AES-256-CCM-8",
  34024. MBEDTLS_CIPHER_AES_256_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_RSA,
  34025. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34026. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34027. MBEDTLS_CIPHERSUITE_SHORT_TAG },
  34028. { MBEDTLS_TLS_RSA_WITH_AES_128_CCM, "TLS-RSA-WITH-AES-128-CCM",
  34029. MBEDTLS_CIPHER_AES_128_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_RSA,
  34030. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34031. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34032. 0 },
  34033. { MBEDTLS_TLS_RSA_WITH_AES_128_CCM_8, "TLS-RSA-WITH-AES-128-CCM-8",
  34034. MBEDTLS_CIPHER_AES_128_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_RSA,
  34035. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34036. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34037. MBEDTLS_CIPHERSUITE_SHORT_TAG },
  34038. #endif /* MBEDTLS_CCM_C */
  34039. #endif /* MBEDTLS_AES_C */
  34040. #if defined(MBEDTLS_CAMELLIA_C)
  34041. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34042. #if defined(MBEDTLS_SHA256_C)
  34043. { MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256, "TLS-RSA-WITH-CAMELLIA-128-CBC-SHA256",
  34044. MBEDTLS_CIPHER_CAMELLIA_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_RSA,
  34045. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34046. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34047. 0 },
  34048. { MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256, "TLS-RSA-WITH-CAMELLIA-256-CBC-SHA256",
  34049. MBEDTLS_CIPHER_CAMELLIA_256_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_RSA,
  34050. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34051. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34052. 0 },
  34053. #endif /* MBEDTLS_SHA256_C */
  34054. #if defined(MBEDTLS_SHA1_C)
  34055. { MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA, "TLS-RSA-WITH-CAMELLIA-128-CBC-SHA",
  34056. MBEDTLS_CIPHER_CAMELLIA_128_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_RSA,
  34057. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34058. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34059. 0 },
  34060. { MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA, "TLS-RSA-WITH-CAMELLIA-256-CBC-SHA",
  34061. MBEDTLS_CIPHER_CAMELLIA_256_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_RSA,
  34062. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34063. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34064. 0 },
  34065. #endif /* MBEDTLS_SHA1_C */
  34066. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34067. #if defined(MBEDTLS_GCM_C)
  34068. #if defined(MBEDTLS_SHA256_C)
  34069. { MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256, "TLS-RSA-WITH-CAMELLIA-128-GCM-SHA256",
  34070. MBEDTLS_CIPHER_CAMELLIA_128_GCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_RSA,
  34071. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34072. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34073. 0 },
  34074. #endif /* MBEDTLS_SHA256_C */
  34075. #if defined(MBEDTLS_SHA1_C)
  34076. { MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384, "TLS-RSA-WITH-CAMELLIA-256-GCM-SHA384",
  34077. MBEDTLS_CIPHER_CAMELLIA_256_GCM, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_RSA,
  34078. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34079. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34080. 0 },
  34081. #endif /* MBEDTLS_SHA1_C */
  34082. #endif /* MBEDTLS_GCM_C */
  34083. #endif /* MBEDTLS_CAMELLIA_C */
  34084. #if defined(MBEDTLS_DES_C)
  34085. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34086. #if defined(MBEDTLS_SHA1_C)
  34087. { MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA, "TLS-RSA-WITH-3DES-EDE-CBC-SHA",
  34088. MBEDTLS_CIPHER_DES_EDE3_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_RSA,
  34089. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34090. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34091. 0 },
  34092. #endif /* MBEDTLS_SHA1_C */
  34093. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34094. #endif /* MBEDTLS_DES_C */
  34095. #if defined(MBEDTLS_ARC4_C)
  34096. #if defined(MBEDTLS_MD5_C)
  34097. { MBEDTLS_TLS_RSA_WITH_RC4_128_MD5, "TLS-RSA-WITH-RC4-128-MD5",
  34098. MBEDTLS_CIPHER_ARC4_128, MBEDTLS_MD_MD5, MBEDTLS_KEY_EXCHANGE_RSA,
  34099. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34100. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34101. MBEDTLS_CIPHERSUITE_NODTLS },
  34102. #endif
  34103. #if defined(MBEDTLS_SHA1_C)
  34104. { MBEDTLS_TLS_RSA_WITH_RC4_128_SHA, "TLS-RSA-WITH-RC4-128-SHA",
  34105. MBEDTLS_CIPHER_ARC4_128, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_RSA,
  34106. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34107. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34108. MBEDTLS_CIPHERSUITE_NODTLS },
  34109. #endif
  34110. #endif /* MBEDTLS_ARC4_C */
  34111. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  34112. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED)
  34113. #if defined(MBEDTLS_AES_C)
  34114. #if defined(MBEDTLS_SHA1_C)
  34115. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34116. { MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, "TLS-ECDH-RSA-WITH-AES-128-CBC-SHA",
  34117. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDH_RSA,
  34118. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34119. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34120. 0 },
  34121. { MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA, "TLS-ECDH-RSA-WITH-AES-256-CBC-SHA",
  34122. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDH_RSA,
  34123. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34124. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34125. 0 },
  34126. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34127. #endif /* MBEDTLS_SHA1_C */
  34128. #if defined(MBEDTLS_SHA256_C)
  34129. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34130. { MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256, "TLS-ECDH-RSA-WITH-AES-128-CBC-SHA256",
  34131. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDH_RSA,
  34132. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34133. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34134. 0 },
  34135. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34136. #if defined(MBEDTLS_GCM_C)
  34137. { MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256, "TLS-ECDH-RSA-WITH-AES-128-GCM-SHA256",
  34138. MBEDTLS_CIPHER_AES_128_GCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDH_RSA,
  34139. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34140. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34141. 0 },
  34142. #endif /* MBEDTLS_GCM_C */
  34143. #endif /* MBEDTLS_SHA256_C */
  34144. #if defined(MBEDTLS_SHA512_C)
  34145. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34146. { MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384, "TLS-ECDH-RSA-WITH-AES-256-CBC-SHA384",
  34147. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDH_RSA,
  34148. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34149. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34150. 0 },
  34151. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34152. #if defined(MBEDTLS_GCM_C)
  34153. { MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384, "TLS-ECDH-RSA-WITH-AES-256-GCM-SHA384",
  34154. MBEDTLS_CIPHER_AES_256_GCM, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDH_RSA,
  34155. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34156. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34157. 0 },
  34158. #endif /* MBEDTLS_GCM_C */
  34159. #endif /* MBEDTLS_SHA512_C */
  34160. #endif /* MBEDTLS_AES_C */
  34161. #if defined(MBEDTLS_CAMELLIA_C)
  34162. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34163. #if defined(MBEDTLS_SHA256_C)
  34164. { MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256, "TLS-ECDH-RSA-WITH-CAMELLIA-128-CBC-SHA256",
  34165. MBEDTLS_CIPHER_CAMELLIA_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDH_RSA,
  34166. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34167. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34168. 0 },
  34169. #endif /* MBEDTLS_SHA256_C */
  34170. #if defined(MBEDTLS_SHA512_C)
  34171. { MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384, "TLS-ECDH-RSA-WITH-CAMELLIA-256-CBC-SHA384",
  34172. MBEDTLS_CIPHER_CAMELLIA_256_CBC, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDH_RSA,
  34173. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34174. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34175. 0 },
  34176. #endif /* MBEDTLS_SHA512_C */
  34177. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34178. #if defined(MBEDTLS_GCM_C)
  34179. #if defined(MBEDTLS_SHA256_C)
  34180. { MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256, "TLS-ECDH-RSA-WITH-CAMELLIA-128-GCM-SHA256",
  34181. MBEDTLS_CIPHER_CAMELLIA_128_GCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDH_RSA,
  34182. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34183. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34184. 0 },
  34185. #endif /* MBEDTLS_SHA256_C */
  34186. #if defined(MBEDTLS_SHA512_C)
  34187. { MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384, "TLS-ECDH-RSA-WITH-CAMELLIA-256-GCM-SHA384",
  34188. MBEDTLS_CIPHER_CAMELLIA_256_GCM, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDH_RSA,
  34189. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34190. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34191. 0 },
  34192. #endif /* MBEDTLS_SHA512_C */
  34193. #endif /* MBEDTLS_GCM_C */
  34194. #endif /* MBEDTLS_CAMELLIA_C */
  34195. #if defined(MBEDTLS_DES_C)
  34196. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34197. #if defined(MBEDTLS_SHA1_C)
  34198. { MBEDTLS_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, "TLS-ECDH-RSA-WITH-3DES-EDE-CBC-SHA",
  34199. MBEDTLS_CIPHER_DES_EDE3_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDH_RSA,
  34200. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34201. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34202. 0 },
  34203. #endif /* MBEDTLS_SHA1_C */
  34204. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34205. #endif /* MBEDTLS_DES_C */
  34206. #if defined(MBEDTLS_ARC4_C)
  34207. #if defined(MBEDTLS_SHA1_C)
  34208. { MBEDTLS_TLS_ECDH_RSA_WITH_RC4_128_SHA, "TLS-ECDH-RSA-WITH-RC4-128-SHA",
  34209. MBEDTLS_CIPHER_ARC4_128, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDH_RSA,
  34210. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34211. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34212. MBEDTLS_CIPHERSUITE_NODTLS },
  34213. #endif /* MBEDTLS_SHA1_C */
  34214. #endif /* MBEDTLS_ARC4_C */
  34215. #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
  34216. #if defined(MBEDTLS_SHA1_C)
  34217. { MBEDTLS_TLS_ECDH_RSA_WITH_NULL_SHA, "TLS-ECDH-RSA-WITH-NULL-SHA",
  34218. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDH_RSA,
  34219. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34220. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34221. MBEDTLS_CIPHERSUITE_WEAK },
  34222. #endif /* MBEDTLS_SHA1_C */
  34223. #endif /* MBEDTLS_CIPHER_NULL_CIPHER */
  34224. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED */
  34225. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  34226. #if defined(MBEDTLS_AES_C)
  34227. #if defined(MBEDTLS_SHA1_C)
  34228. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34229. { MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, "TLS-ECDH-ECDSA-WITH-AES-128-CBC-SHA",
  34230. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA,
  34231. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34232. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34233. 0 },
  34234. { MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA, "TLS-ECDH-ECDSA-WITH-AES-256-CBC-SHA",
  34235. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA,
  34236. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34237. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34238. 0 },
  34239. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34240. #endif /* MBEDTLS_SHA1_C */
  34241. #if defined(MBEDTLS_SHA256_C)
  34242. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34243. { MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256, "TLS-ECDH-ECDSA-WITH-AES-128-CBC-SHA256",
  34244. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA,
  34245. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34246. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34247. 0 },
  34248. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34249. #if defined(MBEDTLS_GCM_C)
  34250. { MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256, "TLS-ECDH-ECDSA-WITH-AES-128-GCM-SHA256",
  34251. MBEDTLS_CIPHER_AES_128_GCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA,
  34252. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34253. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34254. 0 },
  34255. #endif /* MBEDTLS_GCM_C */
  34256. #endif /* MBEDTLS_SHA256_C */
  34257. #if defined(MBEDTLS_SHA512_C)
  34258. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34259. { MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384, "TLS-ECDH-ECDSA-WITH-AES-256-CBC-SHA384",
  34260. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA,
  34261. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34262. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34263. 0 },
  34264. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34265. #if defined(MBEDTLS_GCM_C)
  34266. { MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384, "TLS-ECDH-ECDSA-WITH-AES-256-GCM-SHA384",
  34267. MBEDTLS_CIPHER_AES_256_GCM, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA,
  34268. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34269. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34270. 0 },
  34271. #endif /* MBEDTLS_GCM_C */
  34272. #endif /* MBEDTLS_SHA512_C */
  34273. #endif /* MBEDTLS_AES_C */
  34274. #if defined(MBEDTLS_CAMELLIA_C)
  34275. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34276. #if defined(MBEDTLS_SHA256_C)
  34277. { MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256, "TLS-ECDH-ECDSA-WITH-CAMELLIA-128-CBC-SHA256",
  34278. MBEDTLS_CIPHER_CAMELLIA_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA,
  34279. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34280. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34281. 0 },
  34282. #endif /* MBEDTLS_SHA256_C */
  34283. #if defined(MBEDTLS_SHA512_C)
  34284. { MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384, "TLS-ECDH-ECDSA-WITH-CAMELLIA-256-CBC-SHA384",
  34285. MBEDTLS_CIPHER_CAMELLIA_256_CBC, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA,
  34286. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34287. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34288. 0 },
  34289. #endif /* MBEDTLS_SHA512_C */
  34290. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34291. #if defined(MBEDTLS_GCM_C)
  34292. #if defined(MBEDTLS_SHA256_C)
  34293. { MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256, "TLS-ECDH-ECDSA-WITH-CAMELLIA-128-GCM-SHA256",
  34294. MBEDTLS_CIPHER_CAMELLIA_128_GCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA,
  34295. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34296. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34297. 0 },
  34298. #endif /* MBEDTLS_SHA256_C */
  34299. #if defined(MBEDTLS_SHA512_C)
  34300. { MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384, "TLS-ECDH-ECDSA-WITH-CAMELLIA-256-GCM-SHA384",
  34301. MBEDTLS_CIPHER_CAMELLIA_256_GCM, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA,
  34302. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34303. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34304. 0 },
  34305. #endif /* MBEDTLS_SHA512_C */
  34306. #endif /* MBEDTLS_GCM_C */
  34307. #endif /* MBEDTLS_CAMELLIA_C */
  34308. #if defined(MBEDTLS_DES_C)
  34309. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34310. #if defined(MBEDTLS_SHA1_C)
  34311. { MBEDTLS_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, "TLS-ECDH-ECDSA-WITH-3DES-EDE-CBC-SHA",
  34312. MBEDTLS_CIPHER_DES_EDE3_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA,
  34313. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34314. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34315. 0 },
  34316. #endif /* MBEDTLS_SHA1_C */
  34317. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34318. #endif /* MBEDTLS_DES_C */
  34319. #if defined(MBEDTLS_ARC4_C)
  34320. #if defined(MBEDTLS_SHA1_C)
  34321. { MBEDTLS_TLS_ECDH_ECDSA_WITH_RC4_128_SHA, "TLS-ECDH-ECDSA-WITH-RC4-128-SHA",
  34322. MBEDTLS_CIPHER_ARC4_128, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA,
  34323. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34324. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34325. MBEDTLS_CIPHERSUITE_NODTLS },
  34326. #endif /* MBEDTLS_SHA1_C */
  34327. #endif /* MBEDTLS_ARC4_C */
  34328. #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
  34329. #if defined(MBEDTLS_SHA1_C)
  34330. { MBEDTLS_TLS_ECDH_ECDSA_WITH_NULL_SHA, "TLS-ECDH-ECDSA-WITH-NULL-SHA",
  34331. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA,
  34332. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34333. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34334. MBEDTLS_CIPHERSUITE_WEAK },
  34335. #endif /* MBEDTLS_SHA1_C */
  34336. #endif /* MBEDTLS_CIPHER_NULL_CIPHER */
  34337. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  34338. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  34339. #if defined(MBEDTLS_AES_C)
  34340. #if defined(MBEDTLS_GCM_C)
  34341. #if defined(MBEDTLS_SHA256_C)
  34342. { MBEDTLS_TLS_PSK_WITH_AES_128_GCM_SHA256, "TLS-PSK-WITH-AES-128-GCM-SHA256",
  34343. MBEDTLS_CIPHER_AES_128_GCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_PSK,
  34344. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34345. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34346. 0 },
  34347. #endif /* MBEDTLS_SHA256_C */
  34348. #if defined(MBEDTLS_SHA512_C)
  34349. { MBEDTLS_TLS_PSK_WITH_AES_256_GCM_SHA384, "TLS-PSK-WITH-AES-256-GCM-SHA384",
  34350. MBEDTLS_CIPHER_AES_256_GCM, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_PSK,
  34351. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34352. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34353. 0 },
  34354. #endif /* MBEDTLS_SHA512_C */
  34355. #endif /* MBEDTLS_GCM_C */
  34356. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34357. #if defined(MBEDTLS_SHA256_C)
  34358. { MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA256, "TLS-PSK-WITH-AES-128-CBC-SHA256",
  34359. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_PSK,
  34360. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34361. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34362. 0 },
  34363. #endif /* MBEDTLS_SHA256_C */
  34364. #if defined(MBEDTLS_SHA512_C)
  34365. { MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA384, "TLS-PSK-WITH-AES-256-CBC-SHA384",
  34366. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_PSK,
  34367. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34368. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34369. 0 },
  34370. #endif /* MBEDTLS_SHA512_C */
  34371. #if defined(MBEDTLS_SHA1_C)
  34372. { MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA, "TLS-PSK-WITH-AES-128-CBC-SHA",
  34373. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_PSK,
  34374. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34375. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34376. 0 },
  34377. { MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA, "TLS-PSK-WITH-AES-256-CBC-SHA",
  34378. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_PSK,
  34379. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34380. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34381. 0 },
  34382. #endif /* MBEDTLS_SHA1_C */
  34383. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34384. #if defined(MBEDTLS_CCM_C)
  34385. { MBEDTLS_TLS_PSK_WITH_AES_256_CCM, "TLS-PSK-WITH-AES-256-CCM",
  34386. MBEDTLS_CIPHER_AES_256_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_PSK,
  34387. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34388. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34389. 0 },
  34390. { MBEDTLS_TLS_PSK_WITH_AES_256_CCM_8, "TLS-PSK-WITH-AES-256-CCM-8",
  34391. MBEDTLS_CIPHER_AES_256_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_PSK,
  34392. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34393. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34394. MBEDTLS_CIPHERSUITE_SHORT_TAG },
  34395. { MBEDTLS_TLS_PSK_WITH_AES_128_CCM, "TLS-PSK-WITH-AES-128-CCM",
  34396. MBEDTLS_CIPHER_AES_128_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_PSK,
  34397. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34398. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34399. 0 },
  34400. { MBEDTLS_TLS_PSK_WITH_AES_128_CCM_8, "TLS-PSK-WITH-AES-128-CCM-8",
  34401. MBEDTLS_CIPHER_AES_128_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_PSK,
  34402. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34403. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34404. MBEDTLS_CIPHERSUITE_SHORT_TAG },
  34405. #endif /* MBEDTLS_CCM_C */
  34406. #endif /* MBEDTLS_AES_C */
  34407. #if defined(MBEDTLS_CAMELLIA_C)
  34408. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34409. #if defined(MBEDTLS_SHA256_C)
  34410. { MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256, "TLS-PSK-WITH-CAMELLIA-128-CBC-SHA256",
  34411. MBEDTLS_CIPHER_CAMELLIA_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_PSK,
  34412. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34413. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34414. 0 },
  34415. #endif /* MBEDTLS_SHA256_C */
  34416. #if defined(MBEDTLS_SHA512_C)
  34417. { MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384, "TLS-PSK-WITH-CAMELLIA-256-CBC-SHA384",
  34418. MBEDTLS_CIPHER_CAMELLIA_256_CBC, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_PSK,
  34419. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34420. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34421. 0 },
  34422. #endif /* MBEDTLS_SHA512_C */
  34423. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34424. #if defined(MBEDTLS_GCM_C)
  34425. #if defined(MBEDTLS_SHA256_C)
  34426. { MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256, "TLS-PSK-WITH-CAMELLIA-128-GCM-SHA256",
  34427. MBEDTLS_CIPHER_CAMELLIA_128_GCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_PSK,
  34428. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34429. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34430. 0 },
  34431. #endif /* MBEDTLS_SHA256_C */
  34432. #if defined(MBEDTLS_SHA512_C)
  34433. { MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384, "TLS-PSK-WITH-CAMELLIA-256-GCM-SHA384",
  34434. MBEDTLS_CIPHER_CAMELLIA_256_GCM, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_PSK,
  34435. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34436. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34437. 0 },
  34438. #endif /* MBEDTLS_SHA512_C */
  34439. #endif /* MBEDTLS_GCM_C */
  34440. #endif /* MBEDTLS_CAMELLIA_C */
  34441. #if defined(MBEDTLS_DES_C)
  34442. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34443. #if defined(MBEDTLS_SHA1_C)
  34444. { MBEDTLS_TLS_PSK_WITH_3DES_EDE_CBC_SHA, "TLS-PSK-WITH-3DES-EDE-CBC-SHA",
  34445. MBEDTLS_CIPHER_DES_EDE3_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_PSK,
  34446. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34447. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34448. 0 },
  34449. #endif /* MBEDTLS_SHA1_C */
  34450. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34451. #endif /* MBEDTLS_DES_C */
  34452. #if defined(MBEDTLS_ARC4_C)
  34453. #if defined(MBEDTLS_SHA1_C)
  34454. { MBEDTLS_TLS_PSK_WITH_RC4_128_SHA, "TLS-PSK-WITH-RC4-128-SHA",
  34455. MBEDTLS_CIPHER_ARC4_128, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_PSK,
  34456. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34457. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34458. MBEDTLS_CIPHERSUITE_NODTLS },
  34459. #endif /* MBEDTLS_SHA1_C */
  34460. #endif /* MBEDTLS_ARC4_C */
  34461. #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
  34462. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  34463. #if defined(MBEDTLS_AES_C)
  34464. #if defined(MBEDTLS_GCM_C)
  34465. #if defined(MBEDTLS_SHA256_C)
  34466. { MBEDTLS_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256, "TLS-DHE-PSK-WITH-AES-128-GCM-SHA256",
  34467. MBEDTLS_CIPHER_AES_128_GCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34468. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34469. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34470. 0 },
  34471. #endif /* MBEDTLS_SHA256_C */
  34472. #if defined(MBEDTLS_SHA512_C)
  34473. { MBEDTLS_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384, "TLS-DHE-PSK-WITH-AES-256-GCM-SHA384",
  34474. MBEDTLS_CIPHER_AES_256_GCM, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34475. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34476. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34477. 0 },
  34478. #endif /* MBEDTLS_SHA512_C */
  34479. #endif /* MBEDTLS_GCM_C */
  34480. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34481. #if defined(MBEDTLS_SHA256_C)
  34482. { MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256, "TLS-DHE-PSK-WITH-AES-128-CBC-SHA256",
  34483. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34484. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34485. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34486. 0 },
  34487. #endif /* MBEDTLS_SHA256_C */
  34488. #if defined(MBEDTLS_SHA512_C)
  34489. { MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384, "TLS-DHE-PSK-WITH-AES-256-CBC-SHA384",
  34490. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34491. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34492. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34493. 0 },
  34494. #endif /* MBEDTLS_SHA512_C */
  34495. #if defined(MBEDTLS_SHA1_C)
  34496. { MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA, "TLS-DHE-PSK-WITH-AES-128-CBC-SHA",
  34497. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34498. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34499. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34500. 0 },
  34501. { MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA, "TLS-DHE-PSK-WITH-AES-256-CBC-SHA",
  34502. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34503. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34504. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34505. 0 },
  34506. #endif /* MBEDTLS_SHA1_C */
  34507. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34508. #if defined(MBEDTLS_CCM_C)
  34509. { MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CCM, "TLS-DHE-PSK-WITH-AES-256-CCM",
  34510. MBEDTLS_CIPHER_AES_256_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34511. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34512. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34513. 0 },
  34514. { MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CCM_8, "TLS-DHE-PSK-WITH-AES-256-CCM-8",
  34515. MBEDTLS_CIPHER_AES_256_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34516. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34517. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34518. MBEDTLS_CIPHERSUITE_SHORT_TAG },
  34519. { MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CCM, "TLS-DHE-PSK-WITH-AES-128-CCM",
  34520. MBEDTLS_CIPHER_AES_128_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34521. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34522. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34523. 0 },
  34524. { MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CCM_8, "TLS-DHE-PSK-WITH-AES-128-CCM-8",
  34525. MBEDTLS_CIPHER_AES_128_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34526. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34527. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34528. MBEDTLS_CIPHERSUITE_SHORT_TAG },
  34529. #endif /* MBEDTLS_CCM_C */
  34530. #endif /* MBEDTLS_AES_C */
  34531. #if defined(MBEDTLS_CAMELLIA_C)
  34532. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34533. #if defined(MBEDTLS_SHA256_C)
  34534. { MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256, "TLS-DHE-PSK-WITH-CAMELLIA-128-CBC-SHA256",
  34535. MBEDTLS_CIPHER_CAMELLIA_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34536. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34537. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34538. 0 },
  34539. #endif /* MBEDTLS_SHA256_C */
  34540. #if defined(MBEDTLS_SHA512_C)
  34541. { MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384, "TLS-DHE-PSK-WITH-CAMELLIA-256-CBC-SHA384",
  34542. MBEDTLS_CIPHER_CAMELLIA_256_CBC, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34543. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34544. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34545. 0 },
  34546. #endif /* MBEDTLS_SHA512_C */
  34547. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34548. #if defined(MBEDTLS_GCM_C)
  34549. #if defined(MBEDTLS_SHA256_C)
  34550. { MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256, "TLS-DHE-PSK-WITH-CAMELLIA-128-GCM-SHA256",
  34551. MBEDTLS_CIPHER_CAMELLIA_128_GCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34552. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34553. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34554. 0 },
  34555. #endif /* MBEDTLS_SHA256_C */
  34556. #if defined(MBEDTLS_SHA512_C)
  34557. { MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384, "TLS-DHE-PSK-WITH-CAMELLIA-256-GCM-SHA384",
  34558. MBEDTLS_CIPHER_CAMELLIA_256_GCM, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34559. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34560. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34561. 0 },
  34562. #endif /* MBEDTLS_SHA512_C */
  34563. #endif /* MBEDTLS_GCM_C */
  34564. #endif /* MBEDTLS_CAMELLIA_C */
  34565. #if defined(MBEDTLS_DES_C)
  34566. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34567. #if defined(MBEDTLS_SHA1_C)
  34568. { MBEDTLS_TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA, "TLS-DHE-PSK-WITH-3DES-EDE-CBC-SHA",
  34569. MBEDTLS_CIPHER_DES_EDE3_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34570. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34571. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34572. 0 },
  34573. #endif /* MBEDTLS_SHA1_C */
  34574. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34575. #endif /* MBEDTLS_DES_C */
  34576. #if defined(MBEDTLS_ARC4_C)
  34577. #if defined(MBEDTLS_SHA1_C)
  34578. { MBEDTLS_TLS_DHE_PSK_WITH_RC4_128_SHA, "TLS-DHE-PSK-WITH-RC4-128-SHA",
  34579. MBEDTLS_CIPHER_ARC4_128, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34580. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34581. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34582. MBEDTLS_CIPHERSUITE_NODTLS },
  34583. #endif /* MBEDTLS_SHA1_C */
  34584. #endif /* MBEDTLS_ARC4_C */
  34585. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  34586. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  34587. #if defined(MBEDTLS_AES_C)
  34588. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34589. #if defined(MBEDTLS_SHA256_C)
  34590. { MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256, "TLS-ECDHE-PSK-WITH-AES-128-CBC-SHA256",
  34591. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDHE_PSK,
  34592. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34593. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34594. 0 },
  34595. #endif /* MBEDTLS_SHA256_C */
  34596. #if defined(MBEDTLS_SHA512_C)
  34597. { MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384, "TLS-ECDHE-PSK-WITH-AES-256-CBC-SHA384",
  34598. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDHE_PSK,
  34599. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34600. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34601. 0 },
  34602. #endif /* MBEDTLS_SHA512_C */
  34603. #if defined(MBEDTLS_SHA1_C)
  34604. { MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA, "TLS-ECDHE-PSK-WITH-AES-128-CBC-SHA",
  34605. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDHE_PSK,
  34606. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34607. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34608. 0 },
  34609. { MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA, "TLS-ECDHE-PSK-WITH-AES-256-CBC-SHA",
  34610. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDHE_PSK,
  34611. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34612. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34613. 0 },
  34614. #endif /* MBEDTLS_SHA1_C */
  34615. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34616. #endif /* MBEDTLS_AES_C */
  34617. #if defined(MBEDTLS_CAMELLIA_C)
  34618. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34619. #if defined(MBEDTLS_SHA256_C)
  34620. { MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256, "TLS-ECDHE-PSK-WITH-CAMELLIA-128-CBC-SHA256",
  34621. MBEDTLS_CIPHER_CAMELLIA_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDHE_PSK,
  34622. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34623. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34624. 0 },
  34625. #endif /* MBEDTLS_SHA256_C */
  34626. #if defined(MBEDTLS_SHA512_C)
  34627. { MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384, "TLS-ECDHE-PSK-WITH-CAMELLIA-256-CBC-SHA384",
  34628. MBEDTLS_CIPHER_CAMELLIA_256_CBC, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDHE_PSK,
  34629. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34630. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34631. 0 },
  34632. #endif /* MBEDTLS_SHA512_C */
  34633. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34634. #endif /* MBEDTLS_CAMELLIA_C */
  34635. #if defined(MBEDTLS_DES_C)
  34636. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34637. #if defined(MBEDTLS_SHA1_C)
  34638. { MBEDTLS_TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA, "TLS-ECDHE-PSK-WITH-3DES-EDE-CBC-SHA",
  34639. MBEDTLS_CIPHER_DES_EDE3_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDHE_PSK,
  34640. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34641. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34642. 0 },
  34643. #endif /* MBEDTLS_SHA1_C */
  34644. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34645. #endif /* MBEDTLS_DES_C */
  34646. #if defined(MBEDTLS_ARC4_C)
  34647. #if defined(MBEDTLS_SHA1_C)
  34648. { MBEDTLS_TLS_ECDHE_PSK_WITH_RC4_128_SHA, "TLS-ECDHE-PSK-WITH-RC4-128-SHA",
  34649. MBEDTLS_CIPHER_ARC4_128, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDHE_PSK,
  34650. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34651. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34652. MBEDTLS_CIPHERSUITE_NODTLS },
  34653. #endif /* MBEDTLS_SHA1_C */
  34654. #endif /* MBEDTLS_ARC4_C */
  34655. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  34656. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  34657. #if defined(MBEDTLS_AES_C)
  34658. #if defined(MBEDTLS_GCM_C)
  34659. #if defined(MBEDTLS_SHA256_C)
  34660. { MBEDTLS_TLS_RSA_PSK_WITH_AES_128_GCM_SHA256, "TLS-RSA-PSK-WITH-AES-128-GCM-SHA256",
  34661. MBEDTLS_CIPHER_AES_128_GCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_RSA_PSK,
  34662. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34663. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34664. 0 },
  34665. #endif /* MBEDTLS_SHA256_C */
  34666. #if defined(MBEDTLS_SHA512_C)
  34667. { MBEDTLS_TLS_RSA_PSK_WITH_AES_256_GCM_SHA384, "TLS-RSA-PSK-WITH-AES-256-GCM-SHA384",
  34668. MBEDTLS_CIPHER_AES_256_GCM, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_RSA_PSK,
  34669. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34670. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34671. 0 },
  34672. #endif /* MBEDTLS_SHA512_C */
  34673. #endif /* MBEDTLS_GCM_C */
  34674. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34675. #if defined(MBEDTLS_SHA256_C)
  34676. { MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA256, "TLS-RSA-PSK-WITH-AES-128-CBC-SHA256",
  34677. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_RSA_PSK,
  34678. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34679. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34680. 0 },
  34681. #endif /* MBEDTLS_SHA256_C */
  34682. #if defined(MBEDTLS_SHA512_C)
  34683. { MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA384, "TLS-RSA-PSK-WITH-AES-256-CBC-SHA384",
  34684. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_RSA_PSK,
  34685. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34686. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34687. 0 },
  34688. #endif /* MBEDTLS_SHA512_C */
  34689. #if defined(MBEDTLS_SHA1_C)
  34690. { MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA, "TLS-RSA-PSK-WITH-AES-128-CBC-SHA",
  34691. MBEDTLS_CIPHER_AES_128_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_RSA_PSK,
  34692. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34693. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34694. 0 },
  34695. { MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA, "TLS-RSA-PSK-WITH-AES-256-CBC-SHA",
  34696. MBEDTLS_CIPHER_AES_256_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_RSA_PSK,
  34697. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34698. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34699. 0 },
  34700. #endif /* MBEDTLS_SHA1_C */
  34701. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34702. #endif /* MBEDTLS_AES_C */
  34703. #if defined(MBEDTLS_CAMELLIA_C)
  34704. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34705. #if defined(MBEDTLS_SHA256_C)
  34706. { MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256, "TLS-RSA-PSK-WITH-CAMELLIA-128-CBC-SHA256",
  34707. MBEDTLS_CIPHER_CAMELLIA_128_CBC, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_RSA_PSK,
  34708. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34709. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34710. 0 },
  34711. #endif /* MBEDTLS_SHA256_C */
  34712. #if defined(MBEDTLS_SHA512_C)
  34713. { MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384, "TLS-RSA-PSK-WITH-CAMELLIA-256-CBC-SHA384",
  34714. MBEDTLS_CIPHER_CAMELLIA_256_CBC, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_RSA_PSK,
  34715. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34716. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34717. 0 },
  34718. #endif /* MBEDTLS_SHA512_C */
  34719. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34720. #if defined(MBEDTLS_GCM_C)
  34721. #if defined(MBEDTLS_SHA256_C)
  34722. { MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256, "TLS-RSA-PSK-WITH-CAMELLIA-128-GCM-SHA256",
  34723. MBEDTLS_CIPHER_CAMELLIA_128_GCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_RSA_PSK,
  34724. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34725. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34726. 0 },
  34727. #endif /* MBEDTLS_SHA256_C */
  34728. #if defined(MBEDTLS_SHA512_C)
  34729. { MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384, "TLS-RSA-PSK-WITH-CAMELLIA-256-GCM-SHA384",
  34730. MBEDTLS_CIPHER_CAMELLIA_256_GCM, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_RSA_PSK,
  34731. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34732. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34733. 0 },
  34734. #endif /* MBEDTLS_SHA512_C */
  34735. #endif /* MBEDTLS_GCM_C */
  34736. #endif /* MBEDTLS_CAMELLIA_C */
  34737. #if defined(MBEDTLS_DES_C)
  34738. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34739. #if defined(MBEDTLS_SHA1_C)
  34740. { MBEDTLS_TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA, "TLS-RSA-PSK-WITH-3DES-EDE-CBC-SHA",
  34741. MBEDTLS_CIPHER_DES_EDE3_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_RSA_PSK,
  34742. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34743. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34744. 0 },
  34745. #endif /* MBEDTLS_SHA1_C */
  34746. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34747. #endif /* MBEDTLS_DES_C */
  34748. #if defined(MBEDTLS_ARC4_C)
  34749. #if defined(MBEDTLS_SHA1_C)
  34750. { MBEDTLS_TLS_RSA_PSK_WITH_RC4_128_SHA, "TLS-RSA-PSK-WITH-RC4-128-SHA",
  34751. MBEDTLS_CIPHER_ARC4_128, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_RSA_PSK,
  34752. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34753. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34754. MBEDTLS_CIPHERSUITE_NODTLS },
  34755. #endif /* MBEDTLS_SHA1_C */
  34756. #endif /* MBEDTLS_ARC4_C */
  34757. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  34758. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  34759. #if defined(MBEDTLS_AES_C)
  34760. #if defined(MBEDTLS_CCM_C)
  34761. { MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8, "TLS-ECJPAKE-WITH-AES-128-CCM-8",
  34762. MBEDTLS_CIPHER_AES_128_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECJPAKE,
  34763. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34764. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34765. MBEDTLS_CIPHERSUITE_SHORT_TAG },
  34766. #endif /* MBEDTLS_CCM_C */
  34767. #endif /* MBEDTLS_AES_C */
  34768. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  34769. #if defined(MBEDTLS_ENABLE_WEAK_CIPHERSUITES)
  34770. #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
  34771. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  34772. #if defined(MBEDTLS_MD5_C)
  34773. { MBEDTLS_TLS_RSA_WITH_NULL_MD5, "TLS-RSA-WITH-NULL-MD5",
  34774. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_MD5, MBEDTLS_KEY_EXCHANGE_RSA,
  34775. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34776. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34777. MBEDTLS_CIPHERSUITE_WEAK },
  34778. #endif
  34779. #if defined(MBEDTLS_SHA1_C)
  34780. { MBEDTLS_TLS_RSA_WITH_NULL_SHA, "TLS-RSA-WITH-NULL-SHA",
  34781. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_RSA,
  34782. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34783. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34784. MBEDTLS_CIPHERSUITE_WEAK },
  34785. #endif
  34786. #if defined(MBEDTLS_SHA256_C)
  34787. { MBEDTLS_TLS_RSA_WITH_NULL_SHA256, "TLS-RSA-WITH-NULL-SHA256",
  34788. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_RSA,
  34789. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34790. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34791. MBEDTLS_CIPHERSUITE_WEAK },
  34792. #endif
  34793. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  34794. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  34795. #if defined(MBEDTLS_SHA1_C)
  34796. { MBEDTLS_TLS_PSK_WITH_NULL_SHA, "TLS-PSK-WITH-NULL-SHA",
  34797. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_PSK,
  34798. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34799. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34800. MBEDTLS_CIPHERSUITE_WEAK },
  34801. #endif /* MBEDTLS_SHA1_C */
  34802. #if defined(MBEDTLS_SHA256_C)
  34803. { MBEDTLS_TLS_PSK_WITH_NULL_SHA256, "TLS-PSK-WITH-NULL-SHA256",
  34804. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_PSK,
  34805. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34806. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34807. MBEDTLS_CIPHERSUITE_WEAK },
  34808. #endif
  34809. #if defined(MBEDTLS_SHA512_C)
  34810. { MBEDTLS_TLS_PSK_WITH_NULL_SHA384, "TLS-PSK-WITH-NULL-SHA384",
  34811. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_PSK,
  34812. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34813. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34814. MBEDTLS_CIPHERSUITE_WEAK },
  34815. #endif
  34816. #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
  34817. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  34818. #if defined(MBEDTLS_SHA1_C)
  34819. { MBEDTLS_TLS_DHE_PSK_WITH_NULL_SHA, "TLS-DHE-PSK-WITH-NULL-SHA",
  34820. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34821. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34822. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34823. MBEDTLS_CIPHERSUITE_WEAK },
  34824. #endif /* MBEDTLS_SHA1_C */
  34825. #if defined(MBEDTLS_SHA256_C)
  34826. { MBEDTLS_TLS_DHE_PSK_WITH_NULL_SHA256, "TLS-DHE-PSK-WITH-NULL-SHA256",
  34827. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34828. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34829. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34830. MBEDTLS_CIPHERSUITE_WEAK },
  34831. #endif
  34832. #if defined(MBEDTLS_SHA512_C)
  34833. { MBEDTLS_TLS_DHE_PSK_WITH_NULL_SHA384, "TLS-DHE-PSK-WITH-NULL-SHA384",
  34834. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  34835. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34836. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34837. MBEDTLS_CIPHERSUITE_WEAK },
  34838. #endif
  34839. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  34840. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  34841. #if defined(MBEDTLS_SHA1_C)
  34842. { MBEDTLS_TLS_ECDHE_PSK_WITH_NULL_SHA, "TLS-ECDHE-PSK-WITH-NULL-SHA",
  34843. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_ECDHE_PSK,
  34844. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34845. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34846. MBEDTLS_CIPHERSUITE_WEAK },
  34847. #endif /* MBEDTLS_SHA1_C */
  34848. #if defined(MBEDTLS_SHA256_C)
  34849. { MBEDTLS_TLS_ECDHE_PSK_WITH_NULL_SHA256, "TLS-ECDHE-PSK-WITH-NULL-SHA256",
  34850. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDHE_PSK,
  34851. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34852. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34853. MBEDTLS_CIPHERSUITE_WEAK },
  34854. #endif
  34855. #if defined(MBEDTLS_SHA512_C)
  34856. { MBEDTLS_TLS_ECDHE_PSK_WITH_NULL_SHA384, "TLS-ECDHE-PSK-WITH-NULL-SHA384",
  34857. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_ECDHE_PSK,
  34858. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34859. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34860. MBEDTLS_CIPHERSUITE_WEAK },
  34861. #endif
  34862. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  34863. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  34864. #if defined(MBEDTLS_SHA1_C)
  34865. { MBEDTLS_TLS_RSA_PSK_WITH_NULL_SHA, "TLS-RSA-PSK-WITH-NULL-SHA",
  34866. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_RSA_PSK,
  34867. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34868. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34869. MBEDTLS_CIPHERSUITE_WEAK },
  34870. #endif /* MBEDTLS_SHA1_C */
  34871. #if defined(MBEDTLS_SHA256_C)
  34872. { MBEDTLS_TLS_RSA_PSK_WITH_NULL_SHA256, "TLS-RSA-PSK-WITH-NULL-SHA256",
  34873. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_RSA_PSK,
  34874. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34875. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34876. MBEDTLS_CIPHERSUITE_WEAK },
  34877. #endif
  34878. #if defined(MBEDTLS_SHA512_C)
  34879. { MBEDTLS_TLS_RSA_PSK_WITH_NULL_SHA384, "TLS-RSA-PSK-WITH-NULL-SHA384",
  34880. MBEDTLS_CIPHER_NULL, MBEDTLS_MD_SHA384, MBEDTLS_KEY_EXCHANGE_RSA_PSK,
  34881. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1,
  34882. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34883. MBEDTLS_CIPHERSUITE_WEAK },
  34884. #endif
  34885. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  34886. #endif /* MBEDTLS_CIPHER_NULL_CIPHER */
  34887. #if defined(MBEDTLS_DES_C)
  34888. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  34889. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
  34890. #if defined(MBEDTLS_SHA1_C)
  34891. { MBEDTLS_TLS_DHE_RSA_WITH_DES_CBC_SHA, "TLS-DHE-RSA-WITH-DES-CBC-SHA",
  34892. MBEDTLS_CIPHER_DES_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  34893. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34894. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34895. MBEDTLS_CIPHERSUITE_WEAK },
  34896. #endif /* MBEDTLS_SHA1_C */
  34897. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
  34898. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  34899. #if defined(MBEDTLS_SHA1_C)
  34900. { MBEDTLS_TLS_RSA_WITH_DES_CBC_SHA, "TLS-RSA-WITH-DES-CBC-SHA",
  34901. MBEDTLS_CIPHER_DES_CBC, MBEDTLS_MD_SHA1, MBEDTLS_KEY_EXCHANGE_RSA,
  34902. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0,
  34903. MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3,
  34904. MBEDTLS_CIPHERSUITE_WEAK },
  34905. #endif /* MBEDTLS_SHA1_C */
  34906. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  34907. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  34908. #endif /* MBEDTLS_DES_C */
  34909. #endif /* MBEDTLS_ENABLE_WEAK_CIPHERSUITES */
  34910. { 0, "",
  34911. MBEDTLS_CIPHER_NONE, MBEDTLS_MD_NONE, MBEDTLS_KEY_EXCHANGE_NONE,
  34912. 0, 0, 0, 0, 0 }
  34913. };
  34914. #if defined(MBEDTLS_SSL_CIPHERSUITES)
  34915. const int *mbedtls_ssl_list_ciphersuites( void )
  34916. {
  34917. return( ciphersuite_preference );
  34918. }
  34919. #else
  34920. #define MAX_CIPHERSUITES sizeof( ciphersuite_definitions ) / \
  34921. sizeof( ciphersuite_definitions[0] )
  34922. static int supported_ciphersuites[MAX_CIPHERSUITES];
  34923. static int supported_init = 0;
  34924. const int *mbedtls_ssl_list_ciphersuites( void )
  34925. {
  34926. /*
  34927. * On initial call filter out all ciphersuites not supported by current
  34928. * build based on presence in the ciphersuite_definitions.
  34929. */
  34930. if( supported_init == 0 )
  34931. {
  34932. const int *p;
  34933. int *q;
  34934. for( p = ciphersuite_preference, q = supported_ciphersuites;
  34935. *p != 0 && q < supported_ciphersuites + MAX_CIPHERSUITES - 1;
  34936. p++ )
  34937. {
  34938. #if defined(MBEDTLS_REMOVE_ARC4_CIPHERSUITES)
  34939. const mbedtls_ssl_ciphersuite_t *cs_info;
  34940. if( ( cs_info = mbedtls_ssl_ciphersuite_from_id( *p ) ) != NULL &&
  34941. cs_info->cipher != MBEDTLS_CIPHER_ARC4_128 )
  34942. #else
  34943. if( mbedtls_ssl_ciphersuite_from_id( *p ) != NULL )
  34944. #endif
  34945. *(q++) = *p;
  34946. }
  34947. *q = 0;
  34948. supported_init = 1;
  34949. }
  34950. return( supported_ciphersuites );
  34951. }
  34952. #endif /* MBEDTLS_SSL_CIPHERSUITES */
  34953. const mbedtls_ssl_ciphersuite_t *mbedtls_ssl_ciphersuite_from_string(
  34954. const char *ciphersuite_name )
  34955. {
  34956. const mbedtls_ssl_ciphersuite_t *cur = ciphersuite_definitions;
  34957. if( NULL == ciphersuite_name )
  34958. return( NULL );
  34959. while( cur->id != 0 )
  34960. {
  34961. if( 0 == strcmp( cur->name, ciphersuite_name ) )
  34962. return( cur );
  34963. cur++;
  34964. }
  34965. return( NULL );
  34966. }
  34967. const mbedtls_ssl_ciphersuite_t *mbedtls_ssl_ciphersuite_from_id( int ciphersuite )
  34968. {
  34969. const mbedtls_ssl_ciphersuite_t *cur = ciphersuite_definitions;
  34970. while( cur->id != 0 )
  34971. {
  34972. if( cur->id == ciphersuite )
  34973. return( cur );
  34974. cur++;
  34975. }
  34976. return( NULL );
  34977. }
  34978. const char *mbedtls_ssl_get_ciphersuite_name( const int ciphersuite_id )
  34979. {
  34980. const mbedtls_ssl_ciphersuite_t *cur;
  34981. cur = mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
  34982. if( cur == NULL )
  34983. return( "unknown" );
  34984. return( cur->name );
  34985. }
  34986. int mbedtls_ssl_get_ciphersuite_id( const char *ciphersuite_name )
  34987. {
  34988. const mbedtls_ssl_ciphersuite_t *cur;
  34989. cur = mbedtls_ssl_ciphersuite_from_string( ciphersuite_name );
  34990. if( cur == NULL )
  34991. return( 0 );
  34992. return( cur->id );
  34993. }
  34994. #if defined(MBEDTLS_PK_C)
  34995. mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg( const mbedtls_ssl_ciphersuite_t *info )
  34996. {
  34997. switch( info->key_exchange )
  34998. {
  34999. case MBEDTLS_KEY_EXCHANGE_RSA:
  35000. case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
  35001. case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
  35002. case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
  35003. return( MBEDTLS_PK_RSA );
  35004. case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
  35005. return( MBEDTLS_PK_ECDSA );
  35006. case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
  35007. case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
  35008. return( MBEDTLS_PK_ECKEY );
  35009. default:
  35010. return( MBEDTLS_PK_NONE );
  35011. }
  35012. }
  35013. mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_alg( const mbedtls_ssl_ciphersuite_t *info )
  35014. {
  35015. switch( info->key_exchange )
  35016. {
  35017. case MBEDTLS_KEY_EXCHANGE_RSA:
  35018. case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
  35019. case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
  35020. return( MBEDTLS_PK_RSA );
  35021. case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
  35022. return( MBEDTLS_PK_ECDSA );
  35023. default:
  35024. return( MBEDTLS_PK_NONE );
  35025. }
  35026. }
  35027. #endif /* MBEDTLS_PK_C */
  35028. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
  35029. int mbedtls_ssl_ciphersuite_uses_ec( const mbedtls_ssl_ciphersuite_t *info )
  35030. {
  35031. switch( info->key_exchange )
  35032. {
  35033. case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
  35034. case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
  35035. case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
  35036. case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
  35037. case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
  35038. return( 1 );
  35039. default:
  35040. return( 0 );
  35041. }
  35042. }
  35043. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
  35044. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  35045. int mbedtls_ssl_ciphersuite_uses_psk( const mbedtls_ssl_ciphersuite_t *info )
  35046. {
  35047. switch( info->key_exchange )
  35048. {
  35049. case MBEDTLS_KEY_EXCHANGE_PSK:
  35050. case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
  35051. case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
  35052. case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
  35053. return( 1 );
  35054. default:
  35055. return( 0 );
  35056. }
  35057. }
  35058. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  35059. #endif /* MBEDTLS_SSL_TLS_C */
  35060. /*
  35061. Amalgamated build undefines
  35062. */
  35063. #undef ADD
  35064. #undef BC
  35065. #undef BEFORE_COLON
  35066. #undef F
  35067. #undef F0
  35068. #undef F1
  35069. #undef F2
  35070. #undef F3
  35071. #undef F4
  35072. #undef F5
  35073. #undef FSb
  35074. #undef K
  35075. #undef KK
  35076. #undef P
  35077. #undef R
  35078. #undef ROTR
  35079. #undef S
  35080. #undef S0
  35081. #undef S1
  35082. #undef S2
  35083. #undef S3
  35084. #undef SAFE_SNPRINTF
  35085. #undef SHR
  35086. #undef close
  35087. #undef read
  35088. #undef supported_init
  35089. #undef write
  35090. /********* Start of file library/ssl_cli.c ************/
  35091. /*
  35092. * SSLv3/TLSv1 client-side functions
  35093. *
  35094. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  35095. * SPDX-License-Identifier: Apache-2.0
  35096. *
  35097. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  35098. * not use this file except in compliance with the License.
  35099. * You may obtain a copy of the License at
  35100. *
  35101. * http://www.apache.org/licenses/LICENSE-2.0
  35102. *
  35103. * Unless required by applicable law or agreed to in writing, software
  35104. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  35105. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  35106. * See the License for the specific language governing permissions and
  35107. * limitations under the License.
  35108. *
  35109. * This file is part of mbed TLS (https://tls.mbed.org)
  35110. */
  35111. #if !defined(MBEDTLS_CONFIG_FILE)
  35112. #else
  35113. #endif
  35114. #if defined(MBEDTLS_SSL_CLI_C)
  35115. #if defined(MBEDTLS_PLATFORM_C)
  35116. #else
  35117. #include <stdlib.h>
  35118. #define mbedtls_calloc calloc
  35119. #define mbedtls_free free
  35120. #endif
  35121. #include <string.h>
  35122. #include <stdint.h>
  35123. #if defined(MBEDTLS_HAVE_TIME)
  35124. #endif
  35125. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  35126. /* Implementation that should never be optimized out by the compiler */
  35127. /* zeroize was here */
  35128. #endif
  35129. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  35130. static void ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
  35131. unsigned char *buf,
  35132. size_t *olen )
  35133. {
  35134. unsigned char *p = buf;
  35135. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  35136. size_t hostname_len;
  35137. *olen = 0;
  35138. if( ssl->hostname == NULL )
  35139. return;
  35140. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s",
  35141. ssl->hostname ) );
  35142. hostname_len = strlen( ssl->hostname );
  35143. if( end < p || (size_t)( end - p ) < hostname_len + 9 )
  35144. {
  35145. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  35146. return;
  35147. }
  35148. /*
  35149. * Sect. 3, RFC 6066 (TLS Extensions Definitions)
  35150. *
  35151. * In order to provide any of the server names, clients MAY include an
  35152. * extension of type "server_name" in the (extended) client hello. The
  35153. * "extension_data" field of this extension SHALL contain
  35154. * "ServerNameList" where:
  35155. *
  35156. * struct {
  35157. * NameType name_type;
  35158. * select (name_type) {
  35159. * case host_name: HostName;
  35160. * } name;
  35161. * } ServerName;
  35162. *
  35163. * enum {
  35164. * host_name(0), (255)
  35165. * } NameType;
  35166. *
  35167. * opaque HostName<1..2^16-1>;
  35168. *
  35169. * struct {
  35170. * ServerName server_name_list<1..2^16-1>
  35171. * } ServerNameList;
  35172. *
  35173. */
  35174. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
  35175. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME ) & 0xFF );
  35176. *p++ = (unsigned char)( ( (hostname_len + 5) >> 8 ) & 0xFF );
  35177. *p++ = (unsigned char)( ( (hostname_len + 5) ) & 0xFF );
  35178. *p++ = (unsigned char)( ( (hostname_len + 3) >> 8 ) & 0xFF );
  35179. *p++ = (unsigned char)( ( (hostname_len + 3) ) & 0xFF );
  35180. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
  35181. *p++ = (unsigned char)( ( hostname_len >> 8 ) & 0xFF );
  35182. *p++ = (unsigned char)( ( hostname_len ) & 0xFF );
  35183. memcpy( p, ssl->hostname, hostname_len );
  35184. *olen = hostname_len + 9;
  35185. }
  35186. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  35187. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  35188. static void cli_ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
  35189. unsigned char *buf,
  35190. size_t *olen )
  35191. {
  35192. unsigned char *p = buf;
  35193. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  35194. *olen = 0;
  35195. /* We're always including an TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
  35196. * initial ClientHello, in which case also adding the renegotiation
  35197. * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
  35198. if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
  35199. return;
  35200. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
  35201. if( end < p || (size_t)( end - p ) < 5 + ssl->verify_data_len )
  35202. {
  35203. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  35204. return;
  35205. }
  35206. /*
  35207. * Secure renegotiation
  35208. */
  35209. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
  35210. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
  35211. *p++ = 0x00;
  35212. *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
  35213. *p++ = ssl->verify_data_len & 0xFF;
  35214. memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
  35215. *olen = 5 + ssl->verify_data_len;
  35216. }
  35217. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  35218. /*
  35219. * Only if we handle at least one key exchange that needs signatures.
  35220. */
  35221. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  35222. defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  35223. static void ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl,
  35224. unsigned char *buf,
  35225. size_t *olen )
  35226. {
  35227. unsigned char *p = buf;
  35228. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  35229. size_t sig_alg_len = 0;
  35230. const int *md;
  35231. #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C)
  35232. unsigned char *sig_alg_list = buf + 6;
  35233. #endif
  35234. *olen = 0;
  35235. if( ssl->conf->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
  35236. return;
  35237. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
  35238. for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
  35239. {
  35240. #if defined(MBEDTLS_ECDSA_C)
  35241. sig_alg_len += 2;
  35242. #endif
  35243. #if defined(MBEDTLS_RSA_C)
  35244. sig_alg_len += 2;
  35245. #endif
  35246. }
  35247. if( end < p || (size_t)( end - p ) < sig_alg_len + 6 )
  35248. {
  35249. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  35250. return;
  35251. }
  35252. /*
  35253. * Prepare signature_algorithms extension (TLS 1.2)
  35254. */
  35255. sig_alg_len = 0;
  35256. for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
  35257. {
  35258. #if defined(MBEDTLS_ECDSA_C)
  35259. sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md );
  35260. sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_ECDSA;
  35261. #endif
  35262. #if defined(MBEDTLS_RSA_C)
  35263. sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md );
  35264. sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_RSA;
  35265. #endif
  35266. }
  35267. /*
  35268. * enum {
  35269. * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
  35270. * sha512(6), (255)
  35271. * } HashAlgorithm;
  35272. *
  35273. * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
  35274. * SignatureAlgorithm;
  35275. *
  35276. * struct {
  35277. * HashAlgorithm hash;
  35278. * SignatureAlgorithm signature;
  35279. * } SignatureAndHashAlgorithm;
  35280. *
  35281. * SignatureAndHashAlgorithm
  35282. * supported_signature_algorithms<2..2^16-2>;
  35283. */
  35284. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
  35285. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SIG_ALG ) & 0xFF );
  35286. *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
  35287. *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
  35288. *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
  35289. *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
  35290. *olen = 6 + sig_alg_len;
  35291. }
  35292. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
  35293. MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
  35294. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  35295. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  35296. static void ssl_write_supported_elliptic_curves_ext( mbedtls_ssl_context *ssl,
  35297. unsigned char *buf,
  35298. size_t *olen )
  35299. {
  35300. unsigned char *p = buf;
  35301. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  35302. unsigned char *elliptic_curve_list = p + 6;
  35303. size_t elliptic_curve_len = 0;
  35304. const mbedtls_ecp_curve_info *info;
  35305. #if defined(MBEDTLS_ECP_C)
  35306. const mbedtls_ecp_group_id *grp_id;
  35307. #else
  35308. ((void) ssl);
  35309. #endif
  35310. *olen = 0;
  35311. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
  35312. #if defined(MBEDTLS_ECP_C)
  35313. for( grp_id = ssl->conf->curve_list; *grp_id != MBEDTLS_ECP_DP_NONE; grp_id++ )
  35314. #else
  35315. for( info = mbedtls_ecp_curve_list(); info->grp_id != MBEDTLS_ECP_DP_NONE; info++ )
  35316. #endif
  35317. {
  35318. #if defined(MBEDTLS_ECP_C)
  35319. info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
  35320. #endif
  35321. if( info == NULL )
  35322. {
  35323. MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid curve in ssl configuration" ) );
  35324. return;
  35325. }
  35326. elliptic_curve_len += 2;
  35327. }
  35328. if( end < p || (size_t)( end - p ) < 6 + elliptic_curve_len )
  35329. {
  35330. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  35331. return;
  35332. }
  35333. elliptic_curve_len = 0;
  35334. #if defined(MBEDTLS_ECP_C)
  35335. for( grp_id = ssl->conf->curve_list; *grp_id != MBEDTLS_ECP_DP_NONE; grp_id++ )
  35336. #else
  35337. for( info = mbedtls_ecp_curve_list(); info->grp_id != MBEDTLS_ECP_DP_NONE; info++ )
  35338. #endif
  35339. {
  35340. #if defined(MBEDTLS_ECP_C)
  35341. info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
  35342. #endif
  35343. elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
  35344. elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
  35345. }
  35346. if( elliptic_curve_len == 0 )
  35347. return;
  35348. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
  35349. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
  35350. *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
  35351. *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
  35352. *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
  35353. *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
  35354. *olen = 6 + elliptic_curve_len;
  35355. }
  35356. static void cli_ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
  35357. unsigned char *buf,
  35358. size_t *olen )
  35359. {
  35360. unsigned char *p = buf;
  35361. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  35362. *olen = 0;
  35363. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
  35364. if( end < p || (size_t)( end - p ) < 6 )
  35365. {
  35366. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  35367. return;
  35368. }
  35369. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
  35370. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
  35371. *p++ = 0x00;
  35372. *p++ = 2;
  35373. *p++ = 1;
  35374. *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
  35375. *olen = 6;
  35376. }
  35377. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  35378. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  35379. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  35380. static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
  35381. unsigned char *buf,
  35382. size_t *olen )
  35383. {
  35384. int ret;
  35385. unsigned char *p = buf;
  35386. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  35387. size_t kkpp_len;
  35388. *olen = 0;
  35389. /* Skip costly extension if we can't use EC J-PAKE anyway */
  35390. if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
  35391. return;
  35392. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding ecjpake_kkpp extension" ) );
  35393. if( end - p < 4 )
  35394. {
  35395. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  35396. return;
  35397. }
  35398. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF );
  35399. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF );
  35400. /*
  35401. * We may need to send ClientHello multiple times for Hello verification.
  35402. * We don't want to compute fresh values every time (both for performance
  35403. * and consistency reasons), so cache the extension content.
  35404. */
  35405. if( ssl->handshake->ecjpake_cache == NULL ||
  35406. ssl->handshake->ecjpake_cache_len == 0 )
  35407. {
  35408. MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) );
  35409. ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
  35410. p + 2, end - p - 2, &kkpp_len,
  35411. ssl->conf->f_rng, ssl->conf->p_rng );
  35412. if( ret != 0 )
  35413. {
  35414. MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
  35415. return;
  35416. }
  35417. ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
  35418. if( ssl->handshake->ecjpake_cache == NULL )
  35419. {
  35420. MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
  35421. return;
  35422. }
  35423. memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
  35424. ssl->handshake->ecjpake_cache_len = kkpp_len;
  35425. }
  35426. else
  35427. {
  35428. MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
  35429. kkpp_len = ssl->handshake->ecjpake_cache_len;
  35430. if( (size_t)( end - p - 2 ) < kkpp_len )
  35431. {
  35432. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  35433. return;
  35434. }
  35435. memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
  35436. }
  35437. *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF );
  35438. *p++ = (unsigned char)( ( kkpp_len ) & 0xFF );
  35439. *olen = kkpp_len + 4;
  35440. }
  35441. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  35442. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  35443. static void cli_ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
  35444. unsigned char *buf,
  35445. size_t *olen )
  35446. {
  35447. unsigned char *p = buf;
  35448. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  35449. *olen = 0;
  35450. if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ) {
  35451. return;
  35452. }
  35453. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
  35454. if( end < p || (size_t)( end - p ) < 5 )
  35455. {
  35456. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  35457. return;
  35458. }
  35459. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
  35460. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
  35461. *p++ = 0x00;
  35462. *p++ = 1;
  35463. *p++ = ssl->conf->mfl_code;
  35464. *olen = 5;
  35465. }
  35466. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  35467. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  35468. static void cli_ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
  35469. unsigned char *buf, size_t *olen )
  35470. {
  35471. unsigned char *p = buf;
  35472. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  35473. *olen = 0;
  35474. if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
  35475. {
  35476. return;
  35477. }
  35478. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
  35479. if( end < p || (size_t)( end - p ) < 4 )
  35480. {
  35481. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  35482. return;
  35483. }
  35484. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
  35485. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
  35486. *p++ = 0x00;
  35487. *p++ = 0x00;
  35488. *olen = 4;
  35489. }
  35490. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  35491. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  35492. static void cli_ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
  35493. unsigned char *buf, size_t *olen )
  35494. {
  35495. unsigned char *p = buf;
  35496. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  35497. *olen = 0;
  35498. if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
  35499. ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  35500. {
  35501. return;
  35502. }
  35503. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding encrypt_then_mac "
  35504. "extension" ) );
  35505. if( end < p || (size_t)( end - p ) < 4 )
  35506. {
  35507. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  35508. return;
  35509. }
  35510. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
  35511. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
  35512. *p++ = 0x00;
  35513. *p++ = 0x00;
  35514. *olen = 4;
  35515. }
  35516. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  35517. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  35518. static void cli_ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
  35519. unsigned char *buf, size_t *olen )
  35520. {
  35521. unsigned char *p = buf;
  35522. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  35523. *olen = 0;
  35524. if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
  35525. ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  35526. {
  35527. return;
  35528. }
  35529. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding extended_master_secret "
  35530. "extension" ) );
  35531. if( end < p || (size_t)( end - p ) < 4 )
  35532. {
  35533. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  35534. return;
  35535. }
  35536. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
  35537. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
  35538. *p++ = 0x00;
  35539. *p++ = 0x00;
  35540. *olen = 4;
  35541. }
  35542. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  35543. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  35544. static void cli_ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
  35545. unsigned char *buf, size_t *olen )
  35546. {
  35547. unsigned char *p = buf;
  35548. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  35549. size_t tlen = ssl->session_negotiate->ticket_len;
  35550. *olen = 0;
  35551. if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
  35552. {
  35553. return;
  35554. }
  35555. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
  35556. if( end < p || (size_t)( end - p ) < 4 + tlen )
  35557. {
  35558. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  35559. return;
  35560. }
  35561. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
  35562. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF );
  35563. *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
  35564. *p++ = (unsigned char)( ( tlen ) & 0xFF );
  35565. *olen = 4;
  35566. if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
  35567. {
  35568. return;
  35569. }
  35570. MBEDTLS_SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
  35571. memcpy( p, ssl->session_negotiate->ticket, tlen );
  35572. *olen += tlen;
  35573. }
  35574. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  35575. #if defined(MBEDTLS_SSL_ALPN)
  35576. static void cli_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
  35577. unsigned char *buf, size_t *olen )
  35578. {
  35579. unsigned char *p = buf;
  35580. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  35581. size_t alpnlen = 0;
  35582. const char **cur;
  35583. *olen = 0;
  35584. if( ssl->conf->alpn_list == NULL )
  35585. {
  35586. return;
  35587. }
  35588. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
  35589. for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
  35590. alpnlen += (unsigned char)( strlen( *cur ) & 0xFF ) + 1;
  35591. if( end < p || (size_t)( end - p ) < 6 + alpnlen )
  35592. {
  35593. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  35594. return;
  35595. }
  35596. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF );
  35597. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF );
  35598. /*
  35599. * opaque ProtocolName<1..2^8-1>;
  35600. *
  35601. * struct {
  35602. * ProtocolName protocol_name_list<2..2^16-1>
  35603. * } ProtocolNameList;
  35604. */
  35605. /* Skip writing extension and list length for now */
  35606. p += 4;
  35607. for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
  35608. {
  35609. *p = (unsigned char)( strlen( *cur ) & 0xFF );
  35610. memcpy( p + 1, *cur, *p );
  35611. p += 1 + *p;
  35612. }
  35613. *olen = p - buf;
  35614. /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
  35615. buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
  35616. buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
  35617. /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
  35618. buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
  35619. buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
  35620. }
  35621. #endif /* MBEDTLS_SSL_ALPN */
  35622. /*
  35623. * Generate random bytes for ClientHello
  35624. */
  35625. static int ssl_generate_random( mbedtls_ssl_context *ssl )
  35626. {
  35627. int ret;
  35628. unsigned char *p = ssl->handshake->randbytes;
  35629. #if defined(MBEDTLS_HAVE_TIME)
  35630. mbedtls_time_t t;
  35631. #endif
  35632. /*
  35633. * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1)
  35634. */
  35635. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  35636. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  35637. ssl->handshake->verify_cookie != NULL )
  35638. {
  35639. return( 0 );
  35640. }
  35641. #endif
  35642. #if defined(MBEDTLS_HAVE_TIME)
  35643. t = mbedtls_time( NULL );
  35644. *p++ = (unsigned char)( t >> 24 );
  35645. *p++ = (unsigned char)( t >> 16 );
  35646. *p++ = (unsigned char)( t >> 8 );
  35647. *p++ = (unsigned char)( t );
  35648. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
  35649. #else
  35650. if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
  35651. return( ret );
  35652. p += 4;
  35653. #endif /* MBEDTLS_HAVE_TIME */
  35654. if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
  35655. return( ret );
  35656. return( 0 );
  35657. }
  35658. static int ssl_write_client_hello( mbedtls_ssl_context *ssl )
  35659. {
  35660. int ret;
  35661. size_t i, n, olen, ext_len = 0;
  35662. unsigned char *buf;
  35663. unsigned char *p, *q;
  35664. unsigned char offer_compress;
  35665. const int *ciphersuites;
  35666. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  35667. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
  35668. if( ssl->conf->f_rng == NULL )
  35669. {
  35670. MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
  35671. return( MBEDTLS_ERR_SSL_NO_RNG );
  35672. }
  35673. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  35674. if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
  35675. #endif
  35676. {
  35677. ssl->major_ver = ssl->conf->min_major_ver;
  35678. ssl->minor_ver = ssl->conf->min_minor_ver;
  35679. }
  35680. if( ssl->conf->max_major_ver == 0 )
  35681. {
  35682. MBEDTLS_SSL_DEBUG_MSG( 1, ( "configured max major version is invalid, "
  35683. "consider using mbedtls_ssl_config_defaults()" ) );
  35684. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  35685. }
  35686. /*
  35687. * 0 . 0 handshake type
  35688. * 1 . 3 handshake length
  35689. * 4 . 5 highest version supported
  35690. * 6 . 9 current UNIX time
  35691. * 10 . 37 random bytes
  35692. */
  35693. buf = ssl->out_msg;
  35694. p = buf + 4;
  35695. mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver,
  35696. ssl->conf->transport, p );
  35697. p += 2;
  35698. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
  35699. buf[4], buf[5] ) );
  35700. if( ( ret = ssl_generate_random( ssl ) ) != 0 )
  35701. {
  35702. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
  35703. return( ret );
  35704. }
  35705. memcpy( p, ssl->handshake->randbytes, 32 );
  35706. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
  35707. p += 32;
  35708. /*
  35709. * 38 . 38 session id length
  35710. * 39 . 39+n session id
  35711. * 39+n . 39+n DTLS only: cookie length (1 byte)
  35712. * 40+n . .. DTSL only: cookie
  35713. * .. . .. ciphersuitelist length (2 bytes)
  35714. * .. . .. ciphersuitelist
  35715. * .. . .. compression methods length (1 byte)
  35716. * .. . .. compression methods
  35717. * .. . .. extensions length (2 bytes)
  35718. * .. . .. extensions
  35719. */
  35720. n = ssl->session_negotiate->id_len;
  35721. if( n < 16 || n > 32 ||
  35722. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  35723. ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
  35724. #endif
  35725. ssl->handshake->resume == 0 )
  35726. {
  35727. n = 0;
  35728. }
  35729. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  35730. /*
  35731. * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
  35732. * generate and include a Session ID in the TLS ClientHello."
  35733. */
  35734. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  35735. if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
  35736. #endif
  35737. {
  35738. if( ssl->session_negotiate->ticket != NULL &&
  35739. ssl->session_negotiate->ticket_len != 0 )
  35740. {
  35741. ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id, 32 );
  35742. if( ret != 0 )
  35743. return( ret );
  35744. ssl->session_negotiate->id_len = n = 32;
  35745. }
  35746. }
  35747. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  35748. *p++ = (unsigned char) n;
  35749. for( i = 0; i < n; i++ )
  35750. *p++ = ssl->session_negotiate->id[i];
  35751. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
  35752. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
  35753. /*
  35754. * DTLS cookie
  35755. */
  35756. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  35757. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  35758. {
  35759. if( ssl->handshake->verify_cookie == NULL )
  35760. {
  35761. MBEDTLS_SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
  35762. *p++ = 0;
  35763. }
  35764. else
  35765. {
  35766. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
  35767. ssl->handshake->verify_cookie,
  35768. ssl->handshake->verify_cookie_len );
  35769. *p++ = ssl->handshake->verify_cookie_len;
  35770. memcpy( p, ssl->handshake->verify_cookie,
  35771. ssl->handshake->verify_cookie_len );
  35772. p += ssl->handshake->verify_cookie_len;
  35773. }
  35774. }
  35775. #endif
  35776. /*
  35777. * Ciphersuite list
  35778. */
  35779. ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
  35780. /* Skip writing ciphersuite length for now */
  35781. n = 0;
  35782. q = p;
  35783. p += 2;
  35784. for( i = 0; ciphersuites[i] != 0; i++ )
  35785. {
  35786. ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
  35787. if( ciphersuite_info == NULL )
  35788. continue;
  35789. if( ciphersuite_info->min_minor_ver > ssl->conf->max_minor_ver ||
  35790. ciphersuite_info->max_minor_ver < ssl->conf->min_minor_ver )
  35791. continue;
  35792. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  35793. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  35794. ( ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
  35795. continue;
  35796. #endif
  35797. #if defined(MBEDTLS_ARC4_C)
  35798. if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
  35799. ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
  35800. continue;
  35801. #endif
  35802. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  35803. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
  35804. mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
  35805. continue;
  35806. #endif
  35807. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x",
  35808. ciphersuites[i] ) );
  35809. n++;
  35810. *p++ = (unsigned char)( ciphersuites[i] >> 8 );
  35811. *p++ = (unsigned char)( ciphersuites[i] );
  35812. }
  35813. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites (excluding SCSVs)", n ) );
  35814. /*
  35815. * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
  35816. */
  35817. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  35818. if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
  35819. #endif
  35820. {
  35821. MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
  35822. *p++ = (unsigned char)( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
  35823. *p++ = (unsigned char)( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO );
  35824. n++;
  35825. }
  35826. /* Some versions of OpenSSL don't handle it correctly if not at end */
  35827. #if defined(MBEDTLS_SSL_FALLBACK_SCSV)
  35828. if( ssl->conf->fallback == MBEDTLS_SSL_IS_FALLBACK )
  35829. {
  35830. MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
  35831. *p++ = (unsigned char)( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 );
  35832. *p++ = (unsigned char)( MBEDTLS_SSL_FALLBACK_SCSV_VALUE );
  35833. n++;
  35834. }
  35835. #endif
  35836. *q++ = (unsigned char)( n >> 7 );
  35837. *q++ = (unsigned char)( n << 1 );
  35838. #if defined(MBEDTLS_ZLIB_SUPPORT)
  35839. offer_compress = 1;
  35840. #else
  35841. offer_compress = 0;
  35842. #endif
  35843. /*
  35844. * We don't support compression with DTLS right now: is many records come
  35845. * in the same datagram, uncompressing one could overwrite the next one.
  35846. * We don't want to add complexity for handling that case unless there is
  35847. * an actual need for it.
  35848. */
  35849. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  35850. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  35851. offer_compress = 0;
  35852. #endif
  35853. if( offer_compress )
  35854. {
  35855. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
  35856. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
  35857. MBEDTLS_SSL_COMPRESS_DEFLATE, MBEDTLS_SSL_COMPRESS_NULL ) );
  35858. *p++ = 2;
  35859. *p++ = MBEDTLS_SSL_COMPRESS_DEFLATE;
  35860. *p++ = MBEDTLS_SSL_COMPRESS_NULL;
  35861. }
  35862. else
  35863. {
  35864. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
  35865. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d",
  35866. MBEDTLS_SSL_COMPRESS_NULL ) );
  35867. *p++ = 1;
  35868. *p++ = MBEDTLS_SSL_COMPRESS_NULL;
  35869. }
  35870. // First write extensions, then the total length
  35871. //
  35872. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  35873. ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
  35874. ext_len += olen;
  35875. #endif
  35876. /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
  35877. * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
  35878. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  35879. cli_ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
  35880. ext_len += olen;
  35881. #endif
  35882. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  35883. defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  35884. ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
  35885. ext_len += olen;
  35886. #endif
  35887. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  35888. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  35889. ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
  35890. ext_len += olen;
  35891. cli_ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
  35892. ext_len += olen;
  35893. #endif
  35894. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  35895. ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
  35896. ext_len += olen;
  35897. #endif
  35898. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  35899. cli_ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
  35900. ext_len += olen;
  35901. #endif
  35902. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  35903. cli_ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
  35904. ext_len += olen;
  35905. #endif
  35906. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  35907. cli_ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
  35908. ext_len += olen;
  35909. #endif
  35910. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  35911. cli_ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
  35912. ext_len += olen;
  35913. #endif
  35914. #if defined(MBEDTLS_SSL_ALPN)
  35915. cli_ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
  35916. ext_len += olen;
  35917. #endif
  35918. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  35919. cli_ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
  35920. ext_len += olen;
  35921. #endif
  35922. /* olen unused if all extensions are disabled */
  35923. ((void) olen);
  35924. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
  35925. ext_len ) );
  35926. if( ext_len > 0 )
  35927. {
  35928. *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
  35929. *p++ = (unsigned char)( ( ext_len ) & 0xFF );
  35930. p += ext_len;
  35931. }
  35932. ssl->out_msglen = p - buf;
  35933. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  35934. ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_HELLO;
  35935. ssl->state++;
  35936. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  35937. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  35938. mbedtls_ssl_send_flight_completed( ssl );
  35939. #endif
  35940. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  35941. {
  35942. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  35943. return( ret );
  35944. }
  35945. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
  35946. return( 0 );
  35947. }
  35948. static int cli_ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
  35949. const unsigned char *buf,
  35950. size_t len )
  35951. {
  35952. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  35953. if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
  35954. {
  35955. /* Check verify-data in constant-time. The length OTOH is no secret */
  35956. if( len != 1 + ssl->verify_data_len * 2 ||
  35957. buf[0] != ssl->verify_data_len * 2 ||
  35958. mbedtls_ssl_safer_memcmp( buf + 1,
  35959. ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
  35960. mbedtls_ssl_safer_memcmp( buf + 1 + ssl->verify_data_len,
  35961. ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
  35962. {
  35963. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
  35964. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  35965. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  35966. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  35967. }
  35968. }
  35969. else
  35970. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  35971. {
  35972. if( len != 1 || buf[0] != 0x00 )
  35973. {
  35974. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
  35975. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  35976. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  35977. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  35978. }
  35979. ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
  35980. }
  35981. return( 0 );
  35982. }
  35983. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  35984. static int cli_ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
  35985. const unsigned char *buf,
  35986. size_t len )
  35987. {
  35988. /*
  35989. * server should use the extension only if we did,
  35990. * and if so the server's value should match ours (and len is always 1)
  35991. */
  35992. if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
  35993. len != 1 ||
  35994. buf[0] != ssl->conf->mfl_code )
  35995. {
  35996. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching max fragment length extension" ) );
  35997. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  35998. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  35999. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36000. }
  36001. return( 0 );
  36002. }
  36003. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  36004. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  36005. static int cli_ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl,
  36006. const unsigned char *buf,
  36007. size_t len )
  36008. {
  36009. if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED ||
  36010. len != 0 )
  36011. {
  36012. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching truncated HMAC extension" ) );
  36013. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36014. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  36015. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36016. }
  36017. ((void) buf);
  36018. ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
  36019. return( 0 );
  36020. }
  36021. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  36022. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  36023. static int cli_ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
  36024. const unsigned char *buf,
  36025. size_t len )
  36026. {
  36027. if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
  36028. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
  36029. len != 0 )
  36030. {
  36031. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching encrypt-then-MAC extension" ) );
  36032. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36033. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  36034. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36035. }
  36036. ((void) buf);
  36037. ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
  36038. return( 0 );
  36039. }
  36040. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  36041. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  36042. static int cli_ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
  36043. const unsigned char *buf,
  36044. size_t len )
  36045. {
  36046. if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
  36047. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
  36048. len != 0 )
  36049. {
  36050. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching extended master secret extension" ) );
  36051. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36052. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  36053. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36054. }
  36055. ((void) buf);
  36056. ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
  36057. return( 0 );
  36058. }
  36059. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  36060. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  36061. static int cli_ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
  36062. const unsigned char *buf,
  36063. size_t len )
  36064. {
  36065. if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
  36066. len != 0 )
  36067. {
  36068. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching session ticket extension" ) );
  36069. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36070. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  36071. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36072. }
  36073. ((void) buf);
  36074. ssl->handshake->new_session_ticket = 1;
  36075. return( 0 );
  36076. }
  36077. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  36078. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  36079. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  36080. static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
  36081. const unsigned char *buf,
  36082. size_t len )
  36083. {
  36084. size_t list_size;
  36085. const unsigned char *p;
  36086. list_size = buf[0];
  36087. if( list_size + 1 != len )
  36088. {
  36089. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  36090. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36091. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  36092. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36093. }
  36094. p = buf + 1;
  36095. while( list_size > 0 )
  36096. {
  36097. if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
  36098. p[0] == MBEDTLS_ECP_PF_COMPRESSED )
  36099. {
  36100. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
  36101. ssl->handshake->ecdh_ctx.point_format = p[0];
  36102. #endif
  36103. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  36104. ssl->handshake->ecjpake_ctx.point_format = p[0];
  36105. #endif
  36106. MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
  36107. return( 0 );
  36108. }
  36109. list_size--;
  36110. p++;
  36111. }
  36112. MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
  36113. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36114. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  36115. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36116. }
  36117. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  36118. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  36119. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  36120. static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
  36121. const unsigned char *buf,
  36122. size_t len )
  36123. {
  36124. int ret;
  36125. if( ssl->transform_negotiate->ciphersuite_info->key_exchange !=
  36126. MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  36127. {
  36128. MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
  36129. return( 0 );
  36130. }
  36131. /* If we got here, we no longer need our cached extension */
  36132. mbedtls_free( ssl->handshake->ecjpake_cache );
  36133. ssl->handshake->ecjpake_cache = NULL;
  36134. ssl->handshake->ecjpake_cache_len = 0;
  36135. if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
  36136. buf, len ) ) != 0 )
  36137. {
  36138. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
  36139. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36140. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  36141. return( ret );
  36142. }
  36143. return( 0 );
  36144. }
  36145. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  36146. #if defined(MBEDTLS_SSL_ALPN)
  36147. static int cli_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
  36148. const unsigned char *buf, size_t len )
  36149. {
  36150. size_t list_len, name_len;
  36151. const char **p;
  36152. /* If we didn't send it, the server shouldn't send it */
  36153. if( ssl->conf->alpn_list == NULL )
  36154. {
  36155. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
  36156. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36157. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  36158. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36159. }
  36160. /*
  36161. * opaque ProtocolName<1..2^8-1>;
  36162. *
  36163. * struct {
  36164. * ProtocolName protocol_name_list<2..2^16-1>
  36165. * } ProtocolNameList;
  36166. *
  36167. * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
  36168. */
  36169. /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
  36170. if( len < 4 )
  36171. {
  36172. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36173. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  36174. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36175. }
  36176. list_len = ( buf[0] << 8 ) | buf[1];
  36177. if( list_len != len - 2 )
  36178. {
  36179. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36180. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  36181. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36182. }
  36183. name_len = buf[2];
  36184. if( name_len != list_len - 1 )
  36185. {
  36186. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36187. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  36188. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36189. }
  36190. /* Check that the server chosen protocol was in our list and save it */
  36191. for( p = ssl->conf->alpn_list; *p != NULL; p++ )
  36192. {
  36193. if( name_len == strlen( *p ) &&
  36194. memcmp( buf + 3, *p, name_len ) == 0 )
  36195. {
  36196. ssl->alpn_chosen = *p;
  36197. return( 0 );
  36198. }
  36199. }
  36200. MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
  36201. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36202. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  36203. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36204. }
  36205. #endif /* MBEDTLS_SSL_ALPN */
  36206. /*
  36207. * Parse HelloVerifyRequest. Only called after verifying the HS type.
  36208. */
  36209. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  36210. static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
  36211. {
  36212. const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  36213. int major_ver, minor_ver;
  36214. unsigned char cookie_len;
  36215. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
  36216. /*
  36217. * struct {
  36218. * ProtocolVersion server_version;
  36219. * opaque cookie<0..2^8-1>;
  36220. * } HelloVerifyRequest;
  36221. */
  36222. MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
  36223. mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
  36224. p += 2;
  36225. /*
  36226. * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
  36227. * even is lower than our min version.
  36228. */
  36229. if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
  36230. minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ||
  36231. major_ver > ssl->conf->max_major_ver ||
  36232. minor_ver > ssl->conf->max_minor_ver )
  36233. {
  36234. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
  36235. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36236. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
  36237. return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
  36238. }
  36239. cookie_len = *p++;
  36240. MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
  36241. if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
  36242. {
  36243. MBEDTLS_SSL_DEBUG_MSG( 1,
  36244. ( "cookie length does not match incoming message size" ) );
  36245. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36246. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  36247. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36248. }
  36249. mbedtls_free( ssl->handshake->verify_cookie );
  36250. ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
  36251. if( ssl->handshake->verify_cookie == NULL )
  36252. {
  36253. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
  36254. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  36255. }
  36256. memcpy( ssl->handshake->verify_cookie, p, cookie_len );
  36257. ssl->handshake->verify_cookie_len = cookie_len;
  36258. /* Start over at ClientHello */
  36259. ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
  36260. mbedtls_ssl_reset_checksum( ssl );
  36261. mbedtls_ssl_recv_flight_completed( ssl );
  36262. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
  36263. return( 0 );
  36264. }
  36265. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  36266. static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
  36267. {
  36268. int ret, i;
  36269. size_t n;
  36270. size_t ext_len;
  36271. unsigned char *buf, *ext;
  36272. unsigned char comp;
  36273. #if defined(MBEDTLS_ZLIB_SUPPORT)
  36274. int accept_comp;
  36275. #endif
  36276. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  36277. int renegotiation_info_seen = 0;
  36278. #endif
  36279. int handshake_failure = 0;
  36280. const mbedtls_ssl_ciphersuite_t *suite_info;
  36281. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
  36282. buf = ssl->in_msg;
  36283. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  36284. {
  36285. /* No alert on a read error. */
  36286. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  36287. return( ret );
  36288. }
  36289. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  36290. {
  36291. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  36292. if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
  36293. {
  36294. ssl->renego_records_seen++;
  36295. if( ssl->conf->renego_max_records >= 0 &&
  36296. ssl->renego_records_seen > ssl->conf->renego_max_records )
  36297. {
  36298. MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
  36299. "but not honored by server" ) );
  36300. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  36301. }
  36302. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
  36303. ssl->keep_current_message = 1;
  36304. return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
  36305. }
  36306. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  36307. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  36308. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36309. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  36310. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  36311. }
  36312. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  36313. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  36314. {
  36315. if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
  36316. {
  36317. MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
  36318. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
  36319. return( ssl_parse_hello_verify_request( ssl ) );
  36320. }
  36321. else
  36322. {
  36323. /* We made it through the verification process */
  36324. mbedtls_free( ssl->handshake->verify_cookie );
  36325. ssl->handshake->verify_cookie = NULL;
  36326. ssl->handshake->verify_cookie_len = 0;
  36327. }
  36328. }
  36329. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  36330. if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
  36331. buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
  36332. {
  36333. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  36334. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36335. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  36336. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36337. }
  36338. /*
  36339. * 0 . 1 server_version
  36340. * 2 . 33 random (maybe including 4 bytes of Unix time)
  36341. * 34 . 34 session_id length = n
  36342. * 35 . 34+n session_id
  36343. * 35+n . 36+n cipher_suite
  36344. * 37+n . 37+n compression_method
  36345. *
  36346. * 38+n . 39+n extensions length (optional)
  36347. * 40+n . .. extensions
  36348. */
  36349. buf += mbedtls_ssl_hs_hdr_len( ssl );
  36350. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
  36351. mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
  36352. ssl->conf->transport, buf + 0 );
  36353. if( ssl->major_ver < ssl->conf->min_major_ver ||
  36354. ssl->minor_ver < ssl->conf->min_minor_ver ||
  36355. ssl->major_ver > ssl->conf->max_major_ver ||
  36356. ssl->minor_ver > ssl->conf->max_minor_ver )
  36357. {
  36358. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server version out of bounds - "
  36359. " min: [%d:%d], server: [%d:%d], max: [%d:%d]",
  36360. ssl->conf->min_major_ver, ssl->conf->min_minor_ver,
  36361. ssl->major_ver, ssl->minor_ver,
  36362. ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) );
  36363. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36364. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
  36365. return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
  36366. }
  36367. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
  36368. ( (uint32_t) buf[2] << 24 ) |
  36369. ( (uint32_t) buf[3] << 16 ) |
  36370. ( (uint32_t) buf[4] << 8 ) |
  36371. ( (uint32_t) buf[5] ) ) );
  36372. memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
  36373. n = buf[34];
  36374. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
  36375. if( n > 32 )
  36376. {
  36377. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  36378. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36379. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  36380. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36381. }
  36382. if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
  36383. {
  36384. ext_len = ( ( buf[38 + n] << 8 )
  36385. | ( buf[39 + n] ) );
  36386. if( ( ext_len > 0 && ext_len < 4 ) ||
  36387. ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
  36388. {
  36389. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  36390. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36391. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  36392. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36393. }
  36394. }
  36395. else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
  36396. {
  36397. ext_len = 0;
  36398. }
  36399. else
  36400. {
  36401. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  36402. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36403. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  36404. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36405. }
  36406. /* ciphersuite (used later) */
  36407. i = ( buf[35 + n] << 8 ) | buf[36 + n];
  36408. /*
  36409. * Read and check compression
  36410. */
  36411. comp = buf[37 + n];
  36412. #if defined(MBEDTLS_ZLIB_SUPPORT)
  36413. /* See comments in ssl_write_client_hello() */
  36414. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  36415. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  36416. accept_comp = 0;
  36417. else
  36418. #endif
  36419. accept_comp = 1;
  36420. if( comp != MBEDTLS_SSL_COMPRESS_NULL &&
  36421. ( comp != MBEDTLS_SSL_COMPRESS_DEFLATE || accept_comp == 0 ) )
  36422. #else /* MBEDTLS_ZLIB_SUPPORT */
  36423. if( comp != MBEDTLS_SSL_COMPRESS_NULL )
  36424. #endif/* MBEDTLS_ZLIB_SUPPORT */
  36425. {
  36426. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server hello, bad compression: %d", comp ) );
  36427. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36428. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  36429. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  36430. }
  36431. /*
  36432. * Initialize update checksum functions
  36433. */
  36434. ssl->transform_negotiate->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
  36435. if( ssl->transform_negotiate->ciphersuite_info == NULL )
  36436. {
  36437. MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
  36438. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36439. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  36440. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  36441. }
  36442. mbedtls_ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
  36443. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
  36444. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
  36445. /*
  36446. * Check if the session can be resumed
  36447. */
  36448. if( ssl->handshake->resume == 0 || n == 0 ||
  36449. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  36450. ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
  36451. #endif
  36452. ssl->session_negotiate->ciphersuite != i ||
  36453. ssl->session_negotiate->compression != comp ||
  36454. ssl->session_negotiate->id_len != n ||
  36455. memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
  36456. {
  36457. ssl->state++;
  36458. ssl->handshake->resume = 0;
  36459. #if defined(MBEDTLS_HAVE_TIME)
  36460. ssl->session_negotiate->start = mbedtls_time( NULL );
  36461. #endif
  36462. ssl->session_negotiate->ciphersuite = i;
  36463. ssl->session_negotiate->compression = comp;
  36464. ssl->session_negotiate->id_len = n;
  36465. memcpy( ssl->session_negotiate->id, buf + 35, n );
  36466. }
  36467. else
  36468. {
  36469. ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
  36470. if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
  36471. {
  36472. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
  36473. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36474. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  36475. return( ret );
  36476. }
  36477. }
  36478. MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
  36479. ssl->handshake->resume ? "a" : "no" ) );
  36480. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", i ) );
  36481. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[37 + n] ) );
  36482. suite_info = mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite );
  36483. if( suite_info == NULL
  36484. #if defined(MBEDTLS_ARC4_C)
  36485. || ( ssl->conf->arc4_disabled &&
  36486. suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
  36487. #endif
  36488. )
  36489. {
  36490. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  36491. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36492. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  36493. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36494. }
  36495. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
  36496. i = 0;
  36497. while( 1 )
  36498. {
  36499. if( ssl->conf->ciphersuite_list[ssl->minor_ver][i] == 0 )
  36500. {
  36501. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  36502. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36503. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  36504. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36505. }
  36506. if( ssl->conf->ciphersuite_list[ssl->minor_ver][i++] ==
  36507. ssl->session_negotiate->ciphersuite )
  36508. {
  36509. break;
  36510. }
  36511. }
  36512. if( comp != MBEDTLS_SSL_COMPRESS_NULL
  36513. #if defined(MBEDTLS_ZLIB_SUPPORT)
  36514. && comp != MBEDTLS_SSL_COMPRESS_DEFLATE
  36515. #endif
  36516. )
  36517. {
  36518. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  36519. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36520. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  36521. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36522. }
  36523. ssl->session_negotiate->compression = comp;
  36524. ext = buf + 40 + n;
  36525. MBEDTLS_SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
  36526. while( ext_len )
  36527. {
  36528. unsigned int ext_id = ( ( ext[0] << 8 )
  36529. | ( ext[1] ) );
  36530. unsigned int ext_size = ( ( ext[2] << 8 )
  36531. | ( ext[3] ) );
  36532. if( ext_size + 4 > ext_len )
  36533. {
  36534. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  36535. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36536. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  36537. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36538. }
  36539. switch( ext_id )
  36540. {
  36541. case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
  36542. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
  36543. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  36544. renegotiation_info_seen = 1;
  36545. #endif
  36546. if( ( ret = cli_ssl_parse_renegotiation_info( ssl, ext + 4,
  36547. ext_size ) ) != 0 )
  36548. return( ret );
  36549. break;
  36550. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  36551. case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
  36552. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
  36553. if( ( ret = cli_ssl_parse_max_fragment_length_ext( ssl,
  36554. ext + 4, ext_size ) ) != 0 )
  36555. {
  36556. return( ret );
  36557. }
  36558. break;
  36559. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  36560. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  36561. case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
  36562. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
  36563. if( ( ret = cli_ssl_parse_truncated_hmac_ext( ssl,
  36564. ext + 4, ext_size ) ) != 0 )
  36565. {
  36566. return( ret );
  36567. }
  36568. break;
  36569. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  36570. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  36571. case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
  36572. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
  36573. if( ( ret = cli_ssl_parse_encrypt_then_mac_ext( ssl,
  36574. ext + 4, ext_size ) ) != 0 )
  36575. {
  36576. return( ret );
  36577. }
  36578. break;
  36579. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  36580. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  36581. case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
  36582. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
  36583. if( ( ret = cli_ssl_parse_extended_ms_ext( ssl,
  36584. ext + 4, ext_size ) ) != 0 )
  36585. {
  36586. return( ret );
  36587. }
  36588. break;
  36589. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  36590. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  36591. case MBEDTLS_TLS_EXT_SESSION_TICKET:
  36592. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
  36593. if( ( ret = cli_ssl_parse_session_ticket_ext( ssl,
  36594. ext + 4, ext_size ) ) != 0 )
  36595. {
  36596. return( ret );
  36597. }
  36598. break;
  36599. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  36600. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  36601. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  36602. case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
  36603. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
  36604. if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
  36605. ext + 4, ext_size ) ) != 0 )
  36606. {
  36607. return( ret );
  36608. }
  36609. break;
  36610. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  36611. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  36612. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  36613. case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
  36614. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
  36615. if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
  36616. ext + 4, ext_size ) ) != 0 )
  36617. {
  36618. return( ret );
  36619. }
  36620. break;
  36621. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  36622. #if defined(MBEDTLS_SSL_ALPN)
  36623. case MBEDTLS_TLS_EXT_ALPN:
  36624. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
  36625. if( ( ret = cli_ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
  36626. return( ret );
  36627. break;
  36628. #endif /* MBEDTLS_SSL_ALPN */
  36629. default:
  36630. MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
  36631. ext_id ) );
  36632. }
  36633. ext_len -= 4 + ext_size;
  36634. ext += 4 + ext_size;
  36635. if( ext_len > 0 && ext_len < 4 )
  36636. {
  36637. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  36638. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36639. }
  36640. }
  36641. /*
  36642. * Renegotiation security checks
  36643. */
  36644. if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  36645. ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
  36646. {
  36647. MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
  36648. handshake_failure = 1;
  36649. }
  36650. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  36651. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  36652. ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
  36653. renegotiation_info_seen == 0 )
  36654. {
  36655. MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
  36656. handshake_failure = 1;
  36657. }
  36658. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  36659. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  36660. ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
  36661. {
  36662. MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
  36663. handshake_failure = 1;
  36664. }
  36665. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  36666. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  36667. renegotiation_info_seen == 1 )
  36668. {
  36669. MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
  36670. handshake_failure = 1;
  36671. }
  36672. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  36673. if( handshake_failure == 1 )
  36674. {
  36675. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36676. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  36677. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  36678. }
  36679. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
  36680. return( 0 );
  36681. }
  36682. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  36683. defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  36684. static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl, unsigned char **p,
  36685. unsigned char *end )
  36686. {
  36687. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  36688. /*
  36689. * Ephemeral DH parameters:
  36690. *
  36691. * struct {
  36692. * opaque dh_p<1..2^16-1>;
  36693. * opaque dh_g<1..2^16-1>;
  36694. * opaque dh_Ys<1..2^16-1>;
  36695. * } ServerDHParams;
  36696. */
  36697. if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
  36698. {
  36699. MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
  36700. return( ret );
  36701. }
  36702. if( ssl->handshake->dhm_ctx.len * 8 < ssl->conf->dhm_min_bitlen )
  36703. {
  36704. MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %d < %d",
  36705. ssl->handshake->dhm_ctx.len * 8,
  36706. ssl->conf->dhm_min_bitlen ) );
  36707. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  36708. }
  36709. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
  36710. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
  36711. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
  36712. return( ret );
  36713. }
  36714. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  36715. MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  36716. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  36717. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  36718. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
  36719. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  36720. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  36721. static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
  36722. {
  36723. const mbedtls_ecp_curve_info *curve_info;
  36724. curve_info = mbedtls_ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
  36725. if( curve_info == NULL )
  36726. {
  36727. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  36728. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  36729. }
  36730. MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
  36731. #if defined(MBEDTLS_ECP_C)
  36732. if( mbedtls_ssl_check_curve( ssl, ssl->handshake->ecdh_ctx.grp.id ) != 0 )
  36733. #else
  36734. if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
  36735. ssl->handshake->ecdh_ctx.grp.nbits > 521 )
  36736. #endif
  36737. return( -1 );
  36738. MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
  36739. return( 0 );
  36740. }
  36741. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  36742. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  36743. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
  36744. MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  36745. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  36746. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  36747. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  36748. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  36749. static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
  36750. unsigned char **p,
  36751. unsigned char *end )
  36752. {
  36753. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  36754. /*
  36755. * Ephemeral ECDH parameters:
  36756. *
  36757. * struct {
  36758. * ECParameters curve_params;
  36759. * ECPoint public;
  36760. * } ServerECDHParams;
  36761. */
  36762. if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
  36763. (const unsigned char **) p, end ) ) != 0 )
  36764. {
  36765. MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
  36766. return( ret );
  36767. }
  36768. if( ssl_check_server_ecdh_params( ssl ) != 0 )
  36769. {
  36770. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
  36771. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  36772. }
  36773. return( ret );
  36774. }
  36775. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  36776. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  36777. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  36778. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  36779. static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
  36780. unsigned char **p,
  36781. unsigned char *end )
  36782. {
  36783. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  36784. size_t len;
  36785. ((void) ssl);
  36786. /*
  36787. * PSK parameters:
  36788. *
  36789. * opaque psk_identity_hint<0..2^16-1>;
  36790. */
  36791. if( (*p) > end - 2 )
  36792. {
  36793. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message "
  36794. "(psk_identity_hint length)" ) );
  36795. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  36796. }
  36797. len = (*p)[0] << 8 | (*p)[1];
  36798. *p += 2;
  36799. if( (*p) > end - len )
  36800. {
  36801. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message "
  36802. "(psk_identity_hint length)" ) );
  36803. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  36804. }
  36805. /*
  36806. * Note: we currently ignore the PKS identity hint, as we only allow one
  36807. * PSK to be provisionned on the client. This could be changed later if
  36808. * someone needs that feature.
  36809. */
  36810. *p += len;
  36811. ret = 0;
  36812. return( ret );
  36813. }
  36814. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  36815. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
  36816. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  36817. /*
  36818. * Generate a pre-master secret and encrypt it with the server's RSA key
  36819. */
  36820. static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
  36821. size_t offset, size_t *olen,
  36822. size_t pms_offset )
  36823. {
  36824. int ret;
  36825. size_t len_bytes = ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ? 0 : 2;
  36826. unsigned char *p = ssl->handshake->premaster + pms_offset;
  36827. if( offset + len_bytes > MBEDTLS_SSL_MAX_CONTENT_LEN )
  36828. {
  36829. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
  36830. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  36831. }
  36832. /*
  36833. * Generate (part of) the pre-master as
  36834. * struct {
  36835. * ProtocolVersion client_version;
  36836. * opaque random[46];
  36837. * } PreMasterSecret;
  36838. */
  36839. mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver,
  36840. ssl->conf->transport, p );
  36841. if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
  36842. {
  36843. MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
  36844. return( ret );
  36845. }
  36846. ssl->handshake->pmslen = 48;
  36847. if( ssl->session_negotiate->peer_cert == NULL )
  36848. {
  36849. MBEDTLS_SSL_DEBUG_MSG( 2, ( "certificate required" ) );
  36850. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  36851. }
  36852. /*
  36853. * Now write it out, encrypted
  36854. */
  36855. if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk,
  36856. MBEDTLS_PK_RSA ) )
  36857. {
  36858. MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
  36859. return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
  36860. }
  36861. if( ( ret = mbedtls_pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
  36862. p, ssl->handshake->pmslen,
  36863. ssl->out_msg + offset + len_bytes, olen,
  36864. MBEDTLS_SSL_MAX_CONTENT_LEN - offset - len_bytes,
  36865. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  36866. {
  36867. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
  36868. return( ret );
  36869. }
  36870. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  36871. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  36872. if( len_bytes == 2 )
  36873. {
  36874. ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
  36875. ssl->out_msg[offset+1] = (unsigned char)( *olen );
  36876. *olen += 2;
  36877. }
  36878. #endif
  36879. return( 0 );
  36880. }
  36881. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
  36882. MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  36883. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  36884. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  36885. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  36886. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  36887. static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
  36888. unsigned char **p,
  36889. unsigned char *end,
  36890. mbedtls_md_type_t *md_alg,
  36891. mbedtls_pk_type_t *pk_alg )
  36892. {
  36893. ((void) ssl);
  36894. *md_alg = MBEDTLS_MD_NONE;
  36895. *pk_alg = MBEDTLS_PK_NONE;
  36896. /* Only in TLS 1.2 */
  36897. if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
  36898. {
  36899. return( 0 );
  36900. }
  36901. if( (*p) + 2 > end )
  36902. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  36903. /*
  36904. * Get hash algorithm
  36905. */
  36906. if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) ) == MBEDTLS_MD_NONE )
  36907. {
  36908. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Server used unsupported "
  36909. "HashAlgorithm %d", *(p)[0] ) );
  36910. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  36911. }
  36912. /*
  36913. * Get signature algorithm
  36914. */
  36915. if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) ) == MBEDTLS_PK_NONE )
  36916. {
  36917. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server used unsupported "
  36918. "SignatureAlgorithm %d", (*p)[1] ) );
  36919. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  36920. }
  36921. /*
  36922. * Check if the hash is acceptable
  36923. */
  36924. if( mbedtls_ssl_check_sig_hash( ssl, *md_alg ) != 0 )
  36925. {
  36926. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server used HashAlgorithm %d that was not offered",
  36927. *(p)[0] ) );
  36928. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  36929. }
  36930. MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
  36931. MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
  36932. *p += 2;
  36933. return( 0 );
  36934. }
  36935. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  36936. MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  36937. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  36938. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  36939. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  36940. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  36941. static int cli_ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
  36942. {
  36943. int ret;
  36944. const mbedtls_ecp_keypair *peer_key;
  36945. if( ssl->session_negotiate->peer_cert == NULL )
  36946. {
  36947. MBEDTLS_SSL_DEBUG_MSG( 2, ( "certificate required" ) );
  36948. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  36949. }
  36950. if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk,
  36951. MBEDTLS_PK_ECKEY ) )
  36952. {
  36953. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
  36954. return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
  36955. }
  36956. peer_key = mbedtls_pk_ec( ssl->session_negotiate->peer_cert->pk );
  36957. if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
  36958. MBEDTLS_ECDH_THEIRS ) ) != 0 )
  36959. {
  36960. MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
  36961. return( ret );
  36962. }
  36963. if( ssl_check_server_ecdh_params( ssl ) != 0 )
  36964. {
  36965. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
  36966. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
  36967. }
  36968. return( ret );
  36969. }
  36970. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
  36971. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  36972. static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
  36973. {
  36974. int ret;
  36975. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  36976. ssl->transform_negotiate->ciphersuite_info;
  36977. unsigned char *p = NULL, *end = NULL;
  36978. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
  36979. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  36980. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
  36981. {
  36982. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
  36983. ssl->state++;
  36984. return( 0 );
  36985. }
  36986. ((void) p);
  36987. ((void) end);
  36988. #endif
  36989. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  36990. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  36991. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
  36992. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
  36993. {
  36994. if( ( ret = cli_ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
  36995. {
  36996. MBEDTLS_SSL_DEBUG_RET( 1, "cli_ssl_get_ecdh_params_from_cert", ret );
  36997. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  36998. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  36999. return( ret );
  37000. }
  37001. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
  37002. ssl->state++;
  37003. return( 0 );
  37004. }
  37005. ((void) p);
  37006. ((void) end);
  37007. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  37008. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  37009. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  37010. {
  37011. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  37012. return( ret );
  37013. }
  37014. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  37015. {
  37016. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  37017. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37018. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  37019. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  37020. }
  37021. /*
  37022. * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
  37023. * doesn't use a psk_identity_hint
  37024. */
  37025. if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
  37026. {
  37027. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  37028. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  37029. {
  37030. /* Current message is probably either
  37031. * CertificateRequest or ServerHelloDone */
  37032. ssl->keep_current_message = 1;
  37033. goto exit;
  37034. }
  37035. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key exchange message must "
  37036. "not be skipped" ) );
  37037. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37038. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  37039. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  37040. }
  37041. p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  37042. end = ssl->in_msg + ssl->in_hslen;
  37043. MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
  37044. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  37045. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  37046. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  37047. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  37048. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
  37049. {
  37050. if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
  37051. {
  37052. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  37053. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37054. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  37055. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  37056. }
  37057. } /* FALLTROUGH */
  37058. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  37059. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
  37060. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  37061. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  37062. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  37063. ; /* nothing more to do */
  37064. else
  37065. #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
  37066. MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  37067. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  37068. defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  37069. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
  37070. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
  37071. {
  37072. if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
  37073. {
  37074. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  37075. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37076. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  37077. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  37078. }
  37079. }
  37080. else
  37081. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  37082. MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  37083. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  37084. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
  37085. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  37086. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  37087. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  37088. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
  37089. {
  37090. if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
  37091. {
  37092. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  37093. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37094. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  37095. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  37096. }
  37097. }
  37098. else
  37099. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  37100. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
  37101. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  37102. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  37103. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  37104. {
  37105. ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
  37106. p, end - p );
  37107. if( ret != 0 )
  37108. {
  37109. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
  37110. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37111. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  37112. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  37113. }
  37114. }
  37115. else
  37116. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  37117. {
  37118. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  37119. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  37120. }
  37121. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED)
  37122. if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
  37123. {
  37124. size_t sig_len, hashlen;
  37125. unsigned char hash[64];
  37126. mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
  37127. mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
  37128. unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  37129. size_t params_len = p - params;
  37130. /*
  37131. * Handle the digitally-signed structure
  37132. */
  37133. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  37134. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  37135. {
  37136. if( ssl_parse_signature_algorithm( ssl, &p, end,
  37137. &md_alg, &pk_alg ) != 0 )
  37138. {
  37139. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  37140. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37141. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  37142. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  37143. }
  37144. if( pk_alg != mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
  37145. {
  37146. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  37147. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37148. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  37149. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  37150. }
  37151. }
  37152. else
  37153. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  37154. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  37155. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  37156. if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
  37157. {
  37158. pk_alg = mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
  37159. /* Default hash for ECDSA is SHA-1 */
  37160. if( pk_alg == MBEDTLS_PK_ECDSA && md_alg == MBEDTLS_MD_NONE )
  37161. md_alg = MBEDTLS_MD_SHA1;
  37162. }
  37163. else
  37164. #endif
  37165. {
  37166. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  37167. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  37168. }
  37169. /*
  37170. * Read signature
  37171. */
  37172. if( p > end - 2 )
  37173. {
  37174. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  37175. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37176. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  37177. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  37178. }
  37179. sig_len = ( p[0] << 8 ) | p[1];
  37180. p += 2;
  37181. if( p != end - sig_len )
  37182. {
  37183. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  37184. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37185. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  37186. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  37187. }
  37188. MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
  37189. /*
  37190. * Compute the hash that has been signed
  37191. */
  37192. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  37193. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  37194. if( md_alg == MBEDTLS_MD_NONE )
  37195. {
  37196. hashlen = 36;
  37197. ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash, params,
  37198. params_len );
  37199. if( ret != 0 )
  37200. return( ret );
  37201. }
  37202. else
  37203. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  37204. MBEDTLS_SSL_PROTO_TLS1_1 */
  37205. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  37206. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  37207. if( md_alg != MBEDTLS_MD_NONE )
  37208. {
  37209. /* Info from md_alg will be used instead */
  37210. hashlen = 0;
  37211. ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, params,
  37212. params_len, md_alg );
  37213. if( ret != 0 )
  37214. return( ret );
  37215. }
  37216. else
  37217. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
  37218. MBEDTLS_SSL_PROTO_TLS1_2 */
  37219. {
  37220. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  37221. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  37222. }
  37223. MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
  37224. (unsigned int) ( mbedtls_md_get_size( mbedtls_md_info_from_type( md_alg ) ) ) );
  37225. if( ssl->session_negotiate->peer_cert == NULL )
  37226. {
  37227. MBEDTLS_SSL_DEBUG_MSG( 2, ( "certificate required" ) );
  37228. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37229. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  37230. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  37231. }
  37232. /*
  37233. * Verify signature
  37234. */
  37235. if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
  37236. {
  37237. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  37238. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37239. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  37240. return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
  37241. }
  37242. if( ( ret = mbedtls_pk_verify( &ssl->session_negotiate->peer_cert->pk,
  37243. md_alg, hash, hashlen, p, sig_len ) ) != 0 )
  37244. {
  37245. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37246. MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
  37247. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
  37248. return( ret );
  37249. }
  37250. }
  37251. #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */
  37252. exit:
  37253. ssl->state++;
  37254. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
  37255. return( 0 );
  37256. }
  37257. #if ! defined(MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED)
  37258. static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
  37259. {
  37260. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  37261. ssl->transform_negotiate->ciphersuite_info;
  37262. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
  37263. if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
  37264. {
  37265. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
  37266. ssl->state++;
  37267. return( 0 );
  37268. }
  37269. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  37270. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  37271. }
  37272. #else /* MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */
  37273. static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
  37274. {
  37275. int ret;
  37276. unsigned char *buf;
  37277. size_t n = 0;
  37278. size_t cert_type_len = 0, dn_len = 0;
  37279. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  37280. ssl->transform_negotiate->ciphersuite_info;
  37281. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
  37282. if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
  37283. {
  37284. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
  37285. ssl->state++;
  37286. return( 0 );
  37287. }
  37288. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  37289. {
  37290. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  37291. return( ret );
  37292. }
  37293. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  37294. {
  37295. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  37296. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37297. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  37298. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  37299. }
  37300. ssl->state++;
  37301. ssl->client_auth = ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
  37302. MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
  37303. ssl->client_auth ? "a" : "no" ) );
  37304. if( ssl->client_auth == 0 )
  37305. {
  37306. /* Current message is probably the ServerHelloDone */
  37307. ssl->keep_current_message = 1;
  37308. goto exit;
  37309. }
  37310. /*
  37311. * struct {
  37312. * ClientCertificateType certificate_types<1..2^8-1>;
  37313. * SignatureAndHashAlgorithm
  37314. * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
  37315. * DistinguishedName certificate_authorities<0..2^16-1>;
  37316. * } CertificateRequest;
  37317. *
  37318. * Since we only support a single certificate on clients, let's just
  37319. * ignore all the information that's supposed to help us pick a
  37320. * certificate.
  37321. *
  37322. * We could check that our certificate matches the request, and bail out
  37323. * if it doesn't, but it's simpler to just send the certificate anyway,
  37324. * and give the server the opportunity to decide if it should terminate
  37325. * the connection when it doesn't like our certificate.
  37326. *
  37327. * Same goes for the hash in TLS 1.2's signature_algorithms: at this
  37328. * point we only have one hash available (see comments in
  37329. * write_certificate_verify), so let's just use what we have.
  37330. *
  37331. * However, we still minimally parse the message to check it is at least
  37332. * superficially sane.
  37333. */
  37334. buf = ssl->in_msg;
  37335. /* certificate_types */
  37336. cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
  37337. n = cert_type_len;
  37338. if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
  37339. {
  37340. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  37341. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37342. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  37343. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  37344. }
  37345. /* supported_signature_algorithms */
  37346. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  37347. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  37348. {
  37349. size_t sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
  37350. | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
  37351. #if defined(MBEDTLS_DEBUG_C)
  37352. unsigned char* sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
  37353. size_t i;
  37354. for( i = 0; i < sig_alg_len; i += 2 )
  37355. {
  37356. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Signature Algorithm found: %d"
  37357. ",%d", sig_alg[i], sig_alg[i + 1] ) );
  37358. }
  37359. #endif
  37360. n += 2 + sig_alg_len;
  37361. if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
  37362. {
  37363. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  37364. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37365. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  37366. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  37367. }
  37368. }
  37369. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  37370. /* certificate_authorities */
  37371. dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
  37372. | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
  37373. n += dn_len;
  37374. if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
  37375. {
  37376. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  37377. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37378. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  37379. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  37380. }
  37381. exit:
  37382. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
  37383. return( 0 );
  37384. }
  37385. #endif /* MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */
  37386. static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
  37387. {
  37388. int ret;
  37389. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
  37390. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  37391. {
  37392. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  37393. return( ret );
  37394. }
  37395. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  37396. {
  37397. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
  37398. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  37399. }
  37400. if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
  37401. ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
  37402. {
  37403. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
  37404. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37405. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  37406. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
  37407. }
  37408. ssl->state++;
  37409. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  37410. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  37411. mbedtls_ssl_recv_flight_completed( ssl );
  37412. #endif
  37413. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
  37414. return( 0 );
  37415. }
  37416. static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
  37417. {
  37418. int ret;
  37419. size_t i, n;
  37420. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  37421. ssl->transform_negotiate->ciphersuite_info;
  37422. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
  37423. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
  37424. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
  37425. {
  37426. /*
  37427. * DHM key exchange -- send G^X mod P
  37428. */
  37429. n = ssl->handshake->dhm_ctx.len;
  37430. ssl->out_msg[4] = (unsigned char)( n >> 8 );
  37431. ssl->out_msg[5] = (unsigned char)( n );
  37432. i = 6;
  37433. ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
  37434. (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
  37435. &ssl->out_msg[i], n,
  37436. ssl->conf->f_rng, ssl->conf->p_rng );
  37437. if( ret != 0 )
  37438. {
  37439. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
  37440. return( ret );
  37441. }
  37442. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
  37443. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
  37444. if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
  37445. ssl->handshake->premaster,
  37446. MBEDTLS_PREMASTER_SIZE,
  37447. &ssl->handshake->pmslen,
  37448. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  37449. {
  37450. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
  37451. return( ret );
  37452. }
  37453. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
  37454. }
  37455. else
  37456. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
  37457. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  37458. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  37459. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  37460. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  37461. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  37462. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
  37463. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
  37464. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
  37465. {
  37466. /*
  37467. * ECDH key exchange -- send client public value
  37468. */
  37469. i = 4;
  37470. ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
  37471. &n,
  37472. &ssl->out_msg[i], 1000,
  37473. ssl->conf->f_rng, ssl->conf->p_rng );
  37474. if( ret != 0 )
  37475. {
  37476. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
  37477. return( ret );
  37478. }
  37479. MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
  37480. if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
  37481. &ssl->handshake->pmslen,
  37482. ssl->handshake->premaster,
  37483. MBEDTLS_MPI_MAX_SIZE,
  37484. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  37485. {
  37486. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
  37487. return( ret );
  37488. }
  37489. MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
  37490. }
  37491. else
  37492. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  37493. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  37494. MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  37495. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  37496. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  37497. if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
  37498. {
  37499. /*
  37500. * opaque psk_identity<0..2^16-1>;
  37501. */
  37502. if( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL )
  37503. {
  37504. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for PSK" ) );
  37505. return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
  37506. }
  37507. i = 4;
  37508. n = ssl->conf->psk_identity_len;
  37509. if( i + 2 + n > MBEDTLS_SSL_MAX_CONTENT_LEN )
  37510. {
  37511. MBEDTLS_SSL_DEBUG_MSG( 1, ( "psk identity too long or "
  37512. "SSL buffer too short" ) );
  37513. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  37514. }
  37515. ssl->out_msg[i++] = (unsigned char)( n >> 8 );
  37516. ssl->out_msg[i++] = (unsigned char)( n );
  37517. memcpy( ssl->out_msg + i, ssl->conf->psk_identity, ssl->conf->psk_identity_len );
  37518. i += ssl->conf->psk_identity_len;
  37519. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  37520. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
  37521. {
  37522. n = 0;
  37523. }
  37524. else
  37525. #endif
  37526. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  37527. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  37528. {
  37529. if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
  37530. return( ret );
  37531. }
  37532. else
  37533. #endif
  37534. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  37535. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
  37536. {
  37537. /*
  37538. * ClientDiffieHellmanPublic public (DHM send G^X mod P)
  37539. */
  37540. n = ssl->handshake->dhm_ctx.len;
  37541. if( i + 2 + n > MBEDTLS_SSL_MAX_CONTENT_LEN )
  37542. {
  37543. MBEDTLS_SSL_DEBUG_MSG( 1, ( "psk identity or DHM size too long"
  37544. " or SSL buffer too short" ) );
  37545. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  37546. }
  37547. ssl->out_msg[i++] = (unsigned char)( n >> 8 );
  37548. ssl->out_msg[i++] = (unsigned char)( n );
  37549. ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
  37550. (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
  37551. &ssl->out_msg[i], n,
  37552. ssl->conf->f_rng, ssl->conf->p_rng );
  37553. if( ret != 0 )
  37554. {
  37555. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
  37556. return( ret );
  37557. }
  37558. }
  37559. else
  37560. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  37561. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  37562. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
  37563. {
  37564. /*
  37565. * ClientECDiffieHellmanPublic public;
  37566. */
  37567. ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
  37568. &ssl->out_msg[i], MBEDTLS_SSL_MAX_CONTENT_LEN - i,
  37569. ssl->conf->f_rng, ssl->conf->p_rng );
  37570. if( ret != 0 )
  37571. {
  37572. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
  37573. return( ret );
  37574. }
  37575. MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
  37576. }
  37577. else
  37578. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  37579. {
  37580. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  37581. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  37582. }
  37583. if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
  37584. ciphersuite_info->key_exchange ) ) != 0 )
  37585. {
  37586. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
  37587. return( ret );
  37588. }
  37589. }
  37590. else
  37591. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  37592. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  37593. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
  37594. {
  37595. i = 4;
  37596. if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
  37597. return( ret );
  37598. }
  37599. else
  37600. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  37601. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  37602. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  37603. {
  37604. i = 4;
  37605. ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
  37606. ssl->out_msg + i, MBEDTLS_SSL_MAX_CONTENT_LEN - i, &n,
  37607. ssl->conf->f_rng, ssl->conf->p_rng );
  37608. if( ret != 0 )
  37609. {
  37610. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
  37611. return( ret );
  37612. }
  37613. ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
  37614. ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
  37615. ssl->conf->f_rng, ssl->conf->p_rng );
  37616. if( ret != 0 )
  37617. {
  37618. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
  37619. return( ret );
  37620. }
  37621. }
  37622. else
  37623. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  37624. {
  37625. ((void) ciphersuite_info);
  37626. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  37627. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  37628. }
  37629. ssl->out_msglen = i + n;
  37630. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  37631. ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
  37632. ssl->state++;
  37633. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  37634. {
  37635. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  37636. return( ret );
  37637. }
  37638. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
  37639. return( 0 );
  37640. }
  37641. #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
  37642. !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
  37643. !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
  37644. !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
  37645. !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
  37646. !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  37647. static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
  37648. {
  37649. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  37650. ssl->transform_negotiate->ciphersuite_info;
  37651. int ret;
  37652. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
  37653. if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
  37654. {
  37655. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
  37656. return( ret );
  37657. }
  37658. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  37659. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  37660. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  37661. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  37662. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  37663. {
  37664. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
  37665. ssl->state++;
  37666. return( 0 );
  37667. }
  37668. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  37669. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  37670. }
  37671. #else
  37672. static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
  37673. {
  37674. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  37675. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  37676. ssl->transform_negotiate->ciphersuite_info;
  37677. size_t n = 0, offset = 0;
  37678. unsigned char hash[48];
  37679. unsigned char *hash_start = hash;
  37680. mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
  37681. unsigned int hashlen;
  37682. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
  37683. if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
  37684. {
  37685. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
  37686. return( ret );
  37687. }
  37688. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  37689. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  37690. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  37691. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  37692. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  37693. {
  37694. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
  37695. ssl->state++;
  37696. return( 0 );
  37697. }
  37698. if( ssl->client_auth == 0 || mbedtls_ssl_own_cert( ssl ) == NULL )
  37699. {
  37700. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
  37701. ssl->state++;
  37702. return( 0 );
  37703. }
  37704. if( mbedtls_ssl_own_key( ssl ) == NULL )
  37705. {
  37706. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
  37707. return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
  37708. }
  37709. /*
  37710. * Make an RSA signature of the handshake digests
  37711. */
  37712. ssl->handshake->calc_verify( ssl, hash );
  37713. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  37714. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  37715. if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
  37716. {
  37717. /*
  37718. * digitally-signed struct {
  37719. * opaque md5_hash[16];
  37720. * opaque sha_hash[20];
  37721. * };
  37722. *
  37723. * md5_hash
  37724. * MD5(handshake_messages);
  37725. *
  37726. * sha_hash
  37727. * SHA(handshake_messages);
  37728. */
  37729. hashlen = 36;
  37730. md_alg = MBEDTLS_MD_NONE;
  37731. /*
  37732. * For ECDSA, default hash is SHA-1 only
  37733. */
  37734. if( mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECDSA ) )
  37735. {
  37736. hash_start += 16;
  37737. hashlen -= 16;
  37738. md_alg = MBEDTLS_MD_SHA1;
  37739. }
  37740. }
  37741. else
  37742. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  37743. MBEDTLS_SSL_PROTO_TLS1_1 */
  37744. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  37745. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  37746. {
  37747. /*
  37748. * digitally-signed struct {
  37749. * opaque handshake_messages[handshake_messages_length];
  37750. * };
  37751. *
  37752. * Taking shortcut here. We assume that the server always allows the
  37753. * PRF Hash function and has sent it in the allowed signature
  37754. * algorithms list received in the Certificate Request message.
  37755. *
  37756. * Until we encounter a server that does not, we will take this
  37757. * shortcut.
  37758. *
  37759. * Reason: Otherwise we should have running hashes for SHA512 and SHA224
  37760. * in order to satisfy 'weird' needs from the server side.
  37761. */
  37762. if( ssl->transform_negotiate->ciphersuite_info->mac ==
  37763. MBEDTLS_MD_SHA384 )
  37764. {
  37765. md_alg = MBEDTLS_MD_SHA384;
  37766. ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
  37767. }
  37768. else
  37769. {
  37770. md_alg = MBEDTLS_MD_SHA256;
  37771. ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
  37772. }
  37773. ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
  37774. /* Info from md_alg will be used instead */
  37775. hashlen = 0;
  37776. offset = 2;
  37777. }
  37778. else
  37779. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  37780. {
  37781. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  37782. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  37783. }
  37784. if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ), md_alg, hash_start, hashlen,
  37785. ssl->out_msg + 6 + offset, &n,
  37786. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  37787. {
  37788. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
  37789. return( ret );
  37790. }
  37791. ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
  37792. ssl->out_msg[5 + offset] = (unsigned char)( n );
  37793. ssl->out_msglen = 6 + n + offset;
  37794. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  37795. ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
  37796. ssl->state++;
  37797. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  37798. {
  37799. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  37800. return( ret );
  37801. }
  37802. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
  37803. return( ret );
  37804. }
  37805. #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED &&
  37806. !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED &&
  37807. !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
  37808. !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
  37809. !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
  37810. !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  37811. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  37812. static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
  37813. {
  37814. int ret;
  37815. uint32_t lifetime;
  37816. size_t ticket_len;
  37817. unsigned char *ticket;
  37818. const unsigned char *msg;
  37819. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
  37820. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  37821. {
  37822. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  37823. return( ret );
  37824. }
  37825. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  37826. {
  37827. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
  37828. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37829. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  37830. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  37831. }
  37832. /*
  37833. * struct {
  37834. * uint32 ticket_lifetime_hint;
  37835. * opaque ticket<0..2^16-1>;
  37836. * } NewSessionTicket;
  37837. *
  37838. * 0 . 3 ticket_lifetime_hint
  37839. * 4 . 5 ticket_len (n)
  37840. * 6 . 5+n ticket content
  37841. */
  37842. if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
  37843. ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
  37844. {
  37845. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
  37846. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37847. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  37848. return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
  37849. }
  37850. msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  37851. lifetime = ( msg[0] << 24 ) | ( msg[1] << 16 ) |
  37852. ( msg[2] << 8 ) | ( msg[3] );
  37853. ticket_len = ( msg[4] << 8 ) | ( msg[5] );
  37854. if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
  37855. {
  37856. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
  37857. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37858. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  37859. return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
  37860. }
  37861. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
  37862. /* We're not waiting for a NewSessionTicket message any more */
  37863. ssl->handshake->new_session_ticket = 0;
  37864. ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
  37865. /*
  37866. * Zero-length ticket means the server changed his mind and doesn't want
  37867. * to send a ticket after all, so just forget it
  37868. */
  37869. if( ticket_len == 0 )
  37870. return( 0 );
  37871. mbedtls_zeroize( ssl->session_negotiate->ticket,
  37872. ssl->session_negotiate->ticket_len );
  37873. mbedtls_free( ssl->session_negotiate->ticket );
  37874. ssl->session_negotiate->ticket = NULL;
  37875. ssl->session_negotiate->ticket_len = 0;
  37876. if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
  37877. {
  37878. MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
  37879. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  37880. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  37881. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  37882. }
  37883. memcpy( ticket, msg + 6, ticket_len );
  37884. ssl->session_negotiate->ticket = ticket;
  37885. ssl->session_negotiate->ticket_len = ticket_len;
  37886. ssl->session_negotiate->ticket_lifetime = lifetime;
  37887. /*
  37888. * RFC 5077 section 3.4:
  37889. * "If the client receives a session ticket from the server, then it
  37890. * discards any Session ID that was sent in the ServerHello."
  37891. */
  37892. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
  37893. ssl->session_negotiate->id_len = 0;
  37894. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
  37895. return( 0 );
  37896. }
  37897. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  37898. /*
  37899. * SSL handshake -- client side -- single step
  37900. */
  37901. int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
  37902. {
  37903. int ret = 0;
  37904. if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
  37905. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  37906. MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
  37907. if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
  37908. return( ret );
  37909. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  37910. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  37911. ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
  37912. {
  37913. if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
  37914. return( ret );
  37915. }
  37916. #endif
  37917. /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
  37918. * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
  37919. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  37920. if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
  37921. ssl->handshake->new_session_ticket != 0 )
  37922. {
  37923. ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
  37924. }
  37925. #endif
  37926. switch( ssl->state )
  37927. {
  37928. case MBEDTLS_SSL_HELLO_REQUEST:
  37929. ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
  37930. break;
  37931. /*
  37932. * ==> ClientHello
  37933. */
  37934. case MBEDTLS_SSL_CLIENT_HELLO:
  37935. ret = ssl_write_client_hello( ssl );
  37936. break;
  37937. /*
  37938. * <== ServerHello
  37939. * Certificate
  37940. * ( ServerKeyExchange )
  37941. * ( CertificateRequest )
  37942. * ServerHelloDone
  37943. */
  37944. case MBEDTLS_SSL_SERVER_HELLO:
  37945. ret = ssl_parse_server_hello( ssl );
  37946. break;
  37947. case MBEDTLS_SSL_SERVER_CERTIFICATE:
  37948. ret = mbedtls_ssl_parse_certificate( ssl );
  37949. break;
  37950. case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
  37951. ret = ssl_parse_server_key_exchange( ssl );
  37952. break;
  37953. case MBEDTLS_SSL_CERTIFICATE_REQUEST:
  37954. ret = ssl_parse_certificate_request( ssl );
  37955. break;
  37956. case MBEDTLS_SSL_SERVER_HELLO_DONE:
  37957. ret = ssl_parse_server_hello_done( ssl );
  37958. break;
  37959. /*
  37960. * ==> ( Certificate/Alert )
  37961. * ClientKeyExchange
  37962. * ( CertificateVerify )
  37963. * ChangeCipherSpec
  37964. * Finished
  37965. */
  37966. case MBEDTLS_SSL_CLIENT_CERTIFICATE:
  37967. ret = mbedtls_ssl_write_certificate( ssl );
  37968. break;
  37969. case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
  37970. ret = ssl_write_client_key_exchange( ssl );
  37971. break;
  37972. case MBEDTLS_SSL_CERTIFICATE_VERIFY:
  37973. ret = ssl_write_certificate_verify( ssl );
  37974. break;
  37975. case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
  37976. ret = mbedtls_ssl_write_change_cipher_spec( ssl );
  37977. break;
  37978. case MBEDTLS_SSL_CLIENT_FINISHED:
  37979. ret = mbedtls_ssl_write_finished( ssl );
  37980. break;
  37981. /*
  37982. * <== ( NewSessionTicket )
  37983. * ChangeCipherSpec
  37984. * Finished
  37985. */
  37986. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  37987. case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
  37988. ret = ssl_parse_new_session_ticket( ssl );
  37989. break;
  37990. #endif
  37991. case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
  37992. ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
  37993. break;
  37994. case MBEDTLS_SSL_SERVER_FINISHED:
  37995. ret = mbedtls_ssl_parse_finished( ssl );
  37996. break;
  37997. case MBEDTLS_SSL_FLUSH_BUFFERS:
  37998. MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
  37999. ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
  38000. break;
  38001. case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
  38002. mbedtls_ssl_handshake_wrapup( ssl );
  38003. break;
  38004. default:
  38005. MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
  38006. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  38007. }
  38008. return( ret );
  38009. }
  38010. #endif /* MBEDTLS_SSL_CLI_C */
  38011. /*
  38012. Amalgamated build undefines
  38013. */
  38014. #undef ADD
  38015. #undef BC
  38016. #undef BEFORE_COLON
  38017. #undef F
  38018. #undef F0
  38019. #undef F1
  38020. #undef F2
  38021. #undef F3
  38022. #undef F4
  38023. #undef F5
  38024. #undef FSb
  38025. #undef K
  38026. #undef KK
  38027. #undef P
  38028. #undef R
  38029. #undef ROTR
  38030. #undef S
  38031. #undef S0
  38032. #undef S1
  38033. #undef S2
  38034. #undef S3
  38035. #undef SAFE_SNPRINTF
  38036. #undef SHR
  38037. #undef close
  38038. #undef read
  38039. #undef supported_init
  38040. #undef write
  38041. /********* Start of file library/ssl_cookie.c ************/
  38042. /*
  38043. * DTLS cookie callbacks implementation
  38044. *
  38045. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  38046. * SPDX-License-Identifier: Apache-2.0
  38047. *
  38048. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  38049. * not use this file except in compliance with the License.
  38050. * You may obtain a copy of the License at
  38051. *
  38052. * http://www.apache.org/licenses/LICENSE-2.0
  38053. *
  38054. * Unless required by applicable law or agreed to in writing, software
  38055. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  38056. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  38057. * See the License for the specific language governing permissions and
  38058. * limitations under the License.
  38059. *
  38060. * This file is part of mbed TLS (https://tls.mbed.org)
  38061. */
  38062. /*
  38063. * These session callbacks use a simple chained list
  38064. * to store and retrieve the session information.
  38065. */
  38066. #if !defined(MBEDTLS_CONFIG_FILE)
  38067. #else
  38068. #endif
  38069. #if defined(MBEDTLS_SSL_COOKIE_C)
  38070. #if defined(MBEDTLS_PLATFORM_C)
  38071. #else
  38072. #define mbedtls_calloc calloc
  38073. #define mbedtls_free free
  38074. #endif
  38075. #include <string.h>
  38076. /* Implementation that should never be optimized out by the compiler */
  38077. /* zeroize was here */
  38078. /*
  38079. * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
  38080. * available. Try SHA-256 first, 512 wastes resources since we need to stay
  38081. * with max 32 bytes of cookie for DTLS 1.0
  38082. */
  38083. #if defined(MBEDTLS_SHA256_C)
  38084. #define COOKIE_MD MBEDTLS_MD_SHA224
  38085. #define COOKIE_MD_OUTLEN 32
  38086. #define COOKIE_HMAC_LEN 28
  38087. #elif defined(MBEDTLS_SHA512_C)
  38088. #define COOKIE_MD MBEDTLS_MD_SHA384
  38089. #define COOKIE_MD_OUTLEN 48
  38090. #define COOKIE_HMAC_LEN 28
  38091. #elif defined(MBEDTLS_SHA1_C)
  38092. #define COOKIE_MD MBEDTLS_MD_SHA1
  38093. #define COOKIE_MD_OUTLEN 20
  38094. #define COOKIE_HMAC_LEN 20
  38095. #else
  38096. #error "DTLS hello verify needs SHA-1 or SHA-2"
  38097. #endif
  38098. /*
  38099. * Cookies are formed of a 4-bytes timestamp (or serial number) and
  38100. * an HMAC of timestemp and client ID.
  38101. */
  38102. #define COOKIE_LEN ( 4 + COOKIE_HMAC_LEN )
  38103. void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
  38104. {
  38105. mbedtls_md_init( &ctx->hmac_ctx );
  38106. #if !defined(MBEDTLS_HAVE_TIME)
  38107. ctx->serial = 0;
  38108. #endif
  38109. ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
  38110. #if defined(MBEDTLS_THREADING_C)
  38111. mbedtls_mutex_init( &ctx->mutex );
  38112. #endif
  38113. }
  38114. void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
  38115. {
  38116. ctx->timeout = delay;
  38117. }
  38118. void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
  38119. {
  38120. mbedtls_md_free( &ctx->hmac_ctx );
  38121. #if defined(MBEDTLS_THREADING_C)
  38122. mbedtls_mutex_free( &ctx->mutex );
  38123. #endif
  38124. mbedtls_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
  38125. }
  38126. int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
  38127. int (*f_rng)(void *, unsigned char *, size_t),
  38128. void *p_rng )
  38129. {
  38130. int ret;
  38131. unsigned char key[COOKIE_MD_OUTLEN];
  38132. if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
  38133. return( ret );
  38134. ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 );
  38135. if( ret != 0 )
  38136. return( ret );
  38137. ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
  38138. if( ret != 0 )
  38139. return( ret );
  38140. mbedtls_zeroize( key, sizeof( key ) );
  38141. return( 0 );
  38142. }
  38143. /*
  38144. * Generate the HMAC part of a cookie
  38145. */
  38146. static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
  38147. const unsigned char time[4],
  38148. unsigned char **p, unsigned char *end,
  38149. const unsigned char *cli_id, size_t cli_id_len )
  38150. {
  38151. unsigned char hmac_out[COOKIE_MD_OUTLEN];
  38152. if( (size_t)( end - *p ) < COOKIE_HMAC_LEN )
  38153. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  38154. if( mbedtls_md_hmac_reset( hmac_ctx ) != 0 ||
  38155. mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
  38156. mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
  38157. mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
  38158. {
  38159. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  38160. }
  38161. memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
  38162. *p += COOKIE_HMAC_LEN;
  38163. return( 0 );
  38164. }
  38165. /*
  38166. * Generate cookie for DTLS ClientHello verification
  38167. */
  38168. int mbedtls_ssl_cookie_write( void *p_ctx,
  38169. unsigned char **p, unsigned char *end,
  38170. const unsigned char *cli_id, size_t cli_id_len )
  38171. {
  38172. int ret;
  38173. mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
  38174. unsigned long t;
  38175. if( ctx == NULL || cli_id == NULL )
  38176. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  38177. if( (size_t)( end - *p ) < COOKIE_LEN )
  38178. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  38179. #if defined(MBEDTLS_HAVE_TIME)
  38180. t = (unsigned long) mbedtls_time( NULL );
  38181. #else
  38182. t = ctx->serial++;
  38183. #endif
  38184. (*p)[0] = (unsigned char)( t >> 24 );
  38185. (*p)[1] = (unsigned char)( t >> 16 );
  38186. (*p)[2] = (unsigned char)( t >> 8 );
  38187. (*p)[3] = (unsigned char)( t );
  38188. *p += 4;
  38189. #if defined(MBEDTLS_THREADING_C)
  38190. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  38191. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
  38192. #endif
  38193. ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
  38194. p, end, cli_id, cli_id_len );
  38195. #if defined(MBEDTLS_THREADING_C)
  38196. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  38197. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
  38198. MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  38199. #endif
  38200. return( ret );
  38201. }
  38202. /*
  38203. * Check a cookie
  38204. */
  38205. int mbedtls_ssl_cookie_check( void *p_ctx,
  38206. const unsigned char *cookie, size_t cookie_len,
  38207. const unsigned char *cli_id, size_t cli_id_len )
  38208. {
  38209. unsigned char ref_hmac[COOKIE_HMAC_LEN];
  38210. int ret = 0;
  38211. unsigned char *p = ref_hmac;
  38212. mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
  38213. unsigned long cur_time, cookie_time;
  38214. if( ctx == NULL || cli_id == NULL )
  38215. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  38216. if( cookie_len != COOKIE_LEN )
  38217. return( -1 );
  38218. #if defined(MBEDTLS_THREADING_C)
  38219. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  38220. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
  38221. #endif
  38222. if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
  38223. &p, p + sizeof( ref_hmac ),
  38224. cli_id, cli_id_len ) != 0 )
  38225. ret = -1;
  38226. #if defined(MBEDTLS_THREADING_C)
  38227. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  38228. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
  38229. MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  38230. #endif
  38231. if( ret != 0 )
  38232. return( ret );
  38233. if( mbedtls_ssl_safer_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
  38234. return( -1 );
  38235. #if defined(MBEDTLS_HAVE_TIME)
  38236. cur_time = (unsigned long) mbedtls_time( NULL );
  38237. #else
  38238. cur_time = ctx->serial;
  38239. #endif
  38240. cookie_time = ( (unsigned long) cookie[0] << 24 ) |
  38241. ( (unsigned long) cookie[1] << 16 ) |
  38242. ( (unsigned long) cookie[2] << 8 ) |
  38243. ( (unsigned long) cookie[3] );
  38244. if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
  38245. return( -1 );
  38246. return( 0 );
  38247. }
  38248. #endif /* MBEDTLS_SSL_COOKIE_C */
  38249. /*
  38250. Amalgamated build undefines
  38251. */
  38252. #undef ADD
  38253. #undef BC
  38254. #undef BEFORE_COLON
  38255. #undef F
  38256. #undef F0
  38257. #undef F1
  38258. #undef F2
  38259. #undef F3
  38260. #undef F4
  38261. #undef F5
  38262. #undef FSb
  38263. #undef K
  38264. #undef KK
  38265. #undef P
  38266. #undef R
  38267. #undef ROTR
  38268. #undef S
  38269. #undef S0
  38270. #undef S1
  38271. #undef S2
  38272. #undef S3
  38273. #undef SAFE_SNPRINTF
  38274. #undef SHR
  38275. #undef close
  38276. #undef read
  38277. #undef supported_init
  38278. #undef write
  38279. /********* Start of file library/ssl_srv.c ************/
  38280. /*
  38281. * SSLv3/TLSv1 server-side functions
  38282. *
  38283. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  38284. * SPDX-License-Identifier: Apache-2.0
  38285. *
  38286. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  38287. * not use this file except in compliance with the License.
  38288. * You may obtain a copy of the License at
  38289. *
  38290. * http://www.apache.org/licenses/LICENSE-2.0
  38291. *
  38292. * Unless required by applicable law or agreed to in writing, software
  38293. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  38294. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  38295. * See the License for the specific language governing permissions and
  38296. * limitations under the License.
  38297. *
  38298. * This file is part of mbed TLS (https://tls.mbed.org)
  38299. */
  38300. #if !defined(MBEDTLS_CONFIG_FILE)
  38301. #else
  38302. #endif
  38303. #if defined(MBEDTLS_SSL_SRV_C)
  38304. #if defined(MBEDTLS_PLATFORM_C)
  38305. #else
  38306. #include <stdlib.h>
  38307. #define mbedtls_calloc calloc
  38308. #define mbedtls_free free
  38309. #endif
  38310. #include <string.h>
  38311. #if defined(MBEDTLS_ECP_C)
  38312. #endif
  38313. #if defined(MBEDTLS_HAVE_TIME)
  38314. #endif
  38315. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  38316. /* Implementation that should never be optimized out by the compiler */
  38317. /* zeroize was here */
  38318. #endif
  38319. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  38320. int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl,
  38321. const unsigned char *info,
  38322. size_t ilen )
  38323. {
  38324. if( ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER )
  38325. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  38326. mbedtls_free( ssl->cli_id );
  38327. if( ( ssl->cli_id = mbedtls_calloc( 1, ilen ) ) == NULL )
  38328. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  38329. memcpy( ssl->cli_id, info, ilen );
  38330. ssl->cli_id_len = ilen;
  38331. return( 0 );
  38332. }
  38333. void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf,
  38334. mbedtls_ssl_cookie_write_t *f_cookie_write,
  38335. mbedtls_ssl_cookie_check_t *f_cookie_check,
  38336. void *p_cookie )
  38337. {
  38338. conf->f_cookie_write = f_cookie_write;
  38339. conf->f_cookie_check = f_cookie_check;
  38340. conf->p_cookie = p_cookie;
  38341. }
  38342. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  38343. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  38344. static int ssl_parse_servername_ext( mbedtls_ssl_context *ssl,
  38345. const unsigned char *buf,
  38346. size_t len )
  38347. {
  38348. int ret;
  38349. size_t servername_list_size, hostname_len;
  38350. const unsigned char *p;
  38351. MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
  38352. servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
  38353. if( servername_list_size + 2 != len )
  38354. {
  38355. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  38356. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38357. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  38358. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38359. }
  38360. p = buf + 2;
  38361. while( servername_list_size > 0 )
  38362. {
  38363. hostname_len = ( ( p[1] << 8 ) | p[2] );
  38364. if( hostname_len + 3 > servername_list_size )
  38365. {
  38366. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  38367. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38368. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  38369. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38370. }
  38371. if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
  38372. {
  38373. ret = ssl->conf->f_sni( ssl->conf->p_sni,
  38374. ssl, p + 3, hostname_len );
  38375. if( ret != 0 )
  38376. {
  38377. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
  38378. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38379. MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME );
  38380. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38381. }
  38382. return( 0 );
  38383. }
  38384. servername_list_size -= hostname_len + 3;
  38385. p += hostname_len + 3;
  38386. }
  38387. if( servername_list_size != 0 )
  38388. {
  38389. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  38390. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38391. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  38392. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38393. }
  38394. return( 0 );
  38395. }
  38396. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  38397. static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
  38398. const unsigned char *buf,
  38399. size_t len )
  38400. {
  38401. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  38402. if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
  38403. {
  38404. /* Check verify-data in constant-time. The length OTOH is no secret */
  38405. if( len != 1 + ssl->verify_data_len ||
  38406. buf[0] != ssl->verify_data_len ||
  38407. mbedtls_ssl_safer_memcmp( buf + 1, ssl->peer_verify_data,
  38408. ssl->verify_data_len ) != 0 )
  38409. {
  38410. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
  38411. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38412. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  38413. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38414. }
  38415. }
  38416. else
  38417. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  38418. {
  38419. if( len != 1 || buf[0] != 0x0 )
  38420. {
  38421. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
  38422. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38423. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  38424. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38425. }
  38426. ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
  38427. }
  38428. return( 0 );
  38429. }
  38430. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  38431. defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  38432. /*
  38433. * Status of the implementation of signature-algorithms extension:
  38434. *
  38435. * Currently, we are only considering the signature-algorithm extension
  38436. * to pick a ciphersuite which allows us to send the ServerKeyExchange
  38437. * message with a signature-hash combination that the user allows.
  38438. *
  38439. * We do *not* check whether all certificates in our certificate
  38440. * chain are signed with an allowed signature-hash pair.
  38441. * This needs to be done at a later stage.
  38442. *
  38443. */
  38444. static int ssl_parse_signature_algorithms_ext( mbedtls_ssl_context *ssl,
  38445. const unsigned char *buf,
  38446. size_t len )
  38447. {
  38448. size_t sig_alg_list_size;
  38449. const unsigned char *p;
  38450. const unsigned char *end = buf + len;
  38451. mbedtls_md_type_t md_cur;
  38452. mbedtls_pk_type_t sig_cur;
  38453. sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
  38454. if( sig_alg_list_size + 2 != len ||
  38455. sig_alg_list_size % 2 != 0 )
  38456. {
  38457. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  38458. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38459. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  38460. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38461. }
  38462. /* Currently we only guarantee signing the ServerKeyExchange message according
  38463. * to the constraints specified in this extension (see above), so it suffices
  38464. * to remember only one suitable hash for each possible signature algorithm.
  38465. *
  38466. * This will change when we also consider certificate signatures,
  38467. * in which case we will need to remember the whole signature-hash
  38468. * pair list from the extension.
  38469. */
  38470. for( p = buf + 2; p < end; p += 2 )
  38471. {
  38472. /* Silently ignore unknown signature or hash algorithms. */
  38473. if( ( sig_cur = mbedtls_ssl_pk_alg_from_sig( p[1] ) ) == MBEDTLS_PK_NONE )
  38474. {
  38475. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext"
  38476. " unknown sig alg encoding %d", p[1] ) );
  38477. continue;
  38478. }
  38479. /* Check if we support the hash the user proposes */
  38480. md_cur = mbedtls_ssl_md_alg_from_hash( p[0] );
  38481. if( md_cur == MBEDTLS_MD_NONE )
  38482. {
  38483. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
  38484. " unknown hash alg encoding %d", p[0] ) );
  38485. continue;
  38486. }
  38487. if( mbedtls_ssl_check_sig_hash( ssl, md_cur ) == 0 )
  38488. {
  38489. mbedtls_ssl_sig_hash_set_add( &ssl->handshake->hash_algs, sig_cur, md_cur );
  38490. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
  38491. " match sig %d and hash %d",
  38492. sig_cur, md_cur ) );
  38493. }
  38494. else
  38495. {
  38496. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: "
  38497. "hash alg %d not supported", md_cur ) );
  38498. }
  38499. }
  38500. return( 0 );
  38501. }
  38502. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
  38503. MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
  38504. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  38505. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  38506. static int ssl_parse_supported_elliptic_curves( mbedtls_ssl_context *ssl,
  38507. const unsigned char *buf,
  38508. size_t len )
  38509. {
  38510. size_t list_size, our_size;
  38511. const unsigned char *p;
  38512. const mbedtls_ecp_curve_info *curve_info, **curves;
  38513. list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
  38514. if( list_size + 2 != len ||
  38515. list_size % 2 != 0 )
  38516. {
  38517. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  38518. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38519. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  38520. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38521. }
  38522. /* Should never happen unless client duplicates the extension */
  38523. if( ssl->handshake->curves != NULL )
  38524. {
  38525. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  38526. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38527. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  38528. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38529. }
  38530. /* Don't allow our peer to make us allocate too much memory,
  38531. * and leave room for a final 0 */
  38532. our_size = list_size / 2 + 1;
  38533. if( our_size > MBEDTLS_ECP_DP_MAX )
  38534. our_size = MBEDTLS_ECP_DP_MAX;
  38535. if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL )
  38536. {
  38537. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38538. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  38539. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  38540. }
  38541. ssl->handshake->curves = curves;
  38542. p = buf + 2;
  38543. while( list_size > 0 && our_size > 1 )
  38544. {
  38545. curve_info = mbedtls_ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
  38546. if( curve_info != NULL )
  38547. {
  38548. *curves++ = curve_info;
  38549. our_size--;
  38550. }
  38551. list_size -= 2;
  38552. p += 2;
  38553. }
  38554. return( 0 );
  38555. }
  38556. static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl,
  38557. const unsigned char *buf,
  38558. size_t len )
  38559. {
  38560. size_t list_size;
  38561. const unsigned char *p;
  38562. list_size = buf[0];
  38563. if( list_size + 1 != len )
  38564. {
  38565. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  38566. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38567. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  38568. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38569. }
  38570. p = buf + 1;
  38571. while( list_size > 0 )
  38572. {
  38573. if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
  38574. p[0] == MBEDTLS_ECP_PF_COMPRESSED )
  38575. {
  38576. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
  38577. ssl->handshake->ecdh_ctx.point_format = p[0];
  38578. #endif
  38579. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  38580. ssl->handshake->ecjpake_ctx.point_format = p[0];
  38581. #endif
  38582. MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
  38583. return( 0 );
  38584. }
  38585. list_size--;
  38586. p++;
  38587. }
  38588. return( 0 );
  38589. }
  38590. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  38591. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  38592. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  38593. static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
  38594. const unsigned char *buf,
  38595. size_t len )
  38596. {
  38597. int ret;
  38598. if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
  38599. {
  38600. MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
  38601. return( 0 );
  38602. }
  38603. if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
  38604. buf, len ) ) != 0 )
  38605. {
  38606. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
  38607. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38608. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  38609. return( ret );
  38610. }
  38611. /* Only mark the extension as OK when we're sure it is */
  38612. ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
  38613. return( 0 );
  38614. }
  38615. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  38616. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  38617. static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
  38618. const unsigned char *buf,
  38619. size_t len )
  38620. {
  38621. if( len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID )
  38622. {
  38623. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  38624. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38625. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  38626. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38627. }
  38628. ssl->session_negotiate->mfl_code = buf[0];
  38629. return( 0 );
  38630. }
  38631. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  38632. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  38633. static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl,
  38634. const unsigned char *buf,
  38635. size_t len )
  38636. {
  38637. if( len != 0 )
  38638. {
  38639. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  38640. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38641. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  38642. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38643. }
  38644. ((void) buf);
  38645. if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
  38646. ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
  38647. return( 0 );
  38648. }
  38649. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  38650. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  38651. static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
  38652. const unsigned char *buf,
  38653. size_t len )
  38654. {
  38655. if( len != 0 )
  38656. {
  38657. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  38658. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38659. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  38660. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38661. }
  38662. ((void) buf);
  38663. if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
  38664. ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
  38665. {
  38666. ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
  38667. }
  38668. return( 0 );
  38669. }
  38670. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  38671. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  38672. static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
  38673. const unsigned char *buf,
  38674. size_t len )
  38675. {
  38676. if( len != 0 )
  38677. {
  38678. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  38679. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38680. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  38681. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38682. }
  38683. ((void) buf);
  38684. if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED &&
  38685. ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
  38686. {
  38687. ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
  38688. }
  38689. return( 0 );
  38690. }
  38691. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  38692. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  38693. static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
  38694. unsigned char *buf,
  38695. size_t len )
  38696. {
  38697. int ret;
  38698. mbedtls_ssl_session session;
  38699. mbedtls_ssl_session_init( &session );
  38700. if( ssl->conf->f_ticket_parse == NULL ||
  38701. ssl->conf->f_ticket_write == NULL )
  38702. {
  38703. return( 0 );
  38704. }
  38705. /* Remember the client asked us to send a new ticket */
  38706. ssl->handshake->new_session_ticket = 1;
  38707. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
  38708. if( len == 0 )
  38709. return( 0 );
  38710. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  38711. if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
  38712. {
  38713. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
  38714. return( 0 );
  38715. }
  38716. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  38717. /*
  38718. * Failures are ok: just ignore the ticket and proceed.
  38719. */
  38720. if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session,
  38721. buf, len ) ) != 0 )
  38722. {
  38723. mbedtls_ssl_session_free( &session );
  38724. if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
  38725. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) );
  38726. else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED )
  38727. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) );
  38728. else
  38729. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret );
  38730. return( 0 );
  38731. }
  38732. /*
  38733. * Keep the session ID sent by the client, since we MUST send it back to
  38734. * inform them we're accepting the ticket (RFC 5077 section 3.4)
  38735. */
  38736. session.id_len = ssl->session_negotiate->id_len;
  38737. memcpy( &session.id, ssl->session_negotiate->id, session.id_len );
  38738. mbedtls_ssl_session_free( ssl->session_negotiate );
  38739. memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) );
  38740. /* Zeroize instead of free as we copied the content */
  38741. mbedtls_zeroize( &session, sizeof( mbedtls_ssl_session ) );
  38742. MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
  38743. ssl->handshake->resume = 1;
  38744. /* Don't send a new ticket after all, this one is OK */
  38745. ssl->handshake->new_session_ticket = 0;
  38746. return( 0 );
  38747. }
  38748. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  38749. #if defined(MBEDTLS_SSL_ALPN)
  38750. static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
  38751. const unsigned char *buf, size_t len )
  38752. {
  38753. size_t list_len, cur_len, ours_len;
  38754. const unsigned char *theirs, *start, *end;
  38755. const char **ours;
  38756. /* If ALPN not configured, just ignore the extension */
  38757. if( ssl->conf->alpn_list == NULL )
  38758. return( 0 );
  38759. /*
  38760. * opaque ProtocolName<1..2^8-1>;
  38761. *
  38762. * struct {
  38763. * ProtocolName protocol_name_list<2..2^16-1>
  38764. * } ProtocolNameList;
  38765. */
  38766. /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
  38767. if( len < 4 )
  38768. {
  38769. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38770. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  38771. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38772. }
  38773. list_len = ( buf[0] << 8 ) | buf[1];
  38774. if( list_len != len - 2 )
  38775. {
  38776. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38777. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  38778. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38779. }
  38780. /*
  38781. * Validate peer's list (lengths)
  38782. */
  38783. start = buf + 2;
  38784. end = buf + len;
  38785. for( theirs = start; theirs != end; theirs += cur_len )
  38786. {
  38787. cur_len = *theirs++;
  38788. /* Current identifier must fit in list */
  38789. if( cur_len > (size_t)( end - theirs ) )
  38790. {
  38791. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38792. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  38793. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38794. }
  38795. /* Empty strings MUST NOT be included */
  38796. if( cur_len == 0 )
  38797. {
  38798. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38799. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  38800. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38801. }
  38802. }
  38803. /*
  38804. * Use our order of preference
  38805. */
  38806. for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ )
  38807. {
  38808. ours_len = strlen( *ours );
  38809. for( theirs = start; theirs != end; theirs += cur_len )
  38810. {
  38811. cur_len = *theirs++;
  38812. if( cur_len == ours_len &&
  38813. memcmp( theirs, *ours, cur_len ) == 0 )
  38814. {
  38815. ssl->alpn_chosen = *ours;
  38816. return( 0 );
  38817. }
  38818. }
  38819. }
  38820. /* If we get there, no match was found */
  38821. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  38822. MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
  38823. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  38824. }
  38825. #endif /* MBEDTLS_SSL_ALPN */
  38826. /*
  38827. * Auxiliary functions for ServerHello parsing and related actions
  38828. */
  38829. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  38830. /*
  38831. * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
  38832. */
  38833. #if defined(MBEDTLS_ECDSA_C)
  38834. static int ssl_check_key_curve( mbedtls_pk_context *pk,
  38835. const mbedtls_ecp_curve_info **curves )
  38836. {
  38837. const mbedtls_ecp_curve_info **crv = curves;
  38838. mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
  38839. while( *crv != NULL )
  38840. {
  38841. if( (*crv)->grp_id == grp_id )
  38842. return( 0 );
  38843. crv++;
  38844. }
  38845. return( -1 );
  38846. }
  38847. #endif /* MBEDTLS_ECDSA_C */
  38848. /*
  38849. * Try picking a certificate for this ciphersuite,
  38850. * return 0 on success and -1 on failure.
  38851. */
  38852. static int ssl_pick_cert( mbedtls_ssl_context *ssl,
  38853. const mbedtls_ssl_ciphersuite_t * ciphersuite_info )
  38854. {
  38855. mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
  38856. mbedtls_pk_type_t pk_alg =
  38857. mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
  38858. uint32_t flags;
  38859. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  38860. if( ssl->handshake->sni_key_cert != NULL )
  38861. list = ssl->handshake->sni_key_cert;
  38862. else
  38863. #endif
  38864. list = ssl->conf->key_cert;
  38865. if( pk_alg == MBEDTLS_PK_NONE )
  38866. return( 0 );
  38867. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
  38868. if( list == NULL )
  38869. {
  38870. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
  38871. return( -1 );
  38872. }
  38873. for( cur = list; cur != NULL; cur = cur->next )
  38874. {
  38875. MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
  38876. cur->cert );
  38877. if( ! mbedtls_pk_can_do( cur->key, pk_alg ) )
  38878. {
  38879. MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
  38880. continue;
  38881. }
  38882. /*
  38883. * This avoids sending the client a cert it'll reject based on
  38884. * keyUsage or other extensions.
  38885. *
  38886. * It also allows the user to provision different certificates for
  38887. * different uses based on keyUsage, eg if they want to avoid signing
  38888. * and decrypting with the same RSA key.
  38889. */
  38890. if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
  38891. MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
  38892. {
  38893. MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
  38894. "(extended) key usage extension" ) );
  38895. continue;
  38896. }
  38897. #if defined(MBEDTLS_ECDSA_C)
  38898. if( pk_alg == MBEDTLS_PK_ECDSA &&
  38899. ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 )
  38900. {
  38901. MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
  38902. continue;
  38903. }
  38904. #endif
  38905. /*
  38906. * Try to select a SHA-1 certificate for pre-1.2 clients, but still
  38907. * present them a SHA-higher cert rather than failing if it's the only
  38908. * one we got that satisfies the other conditions.
  38909. */
  38910. if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
  38911. cur->cert->sig_md != MBEDTLS_MD_SHA1 )
  38912. {
  38913. if( fallback == NULL )
  38914. fallback = cur;
  38915. {
  38916. MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
  38917. "sha-2 with pre-TLS 1.2 client" ) );
  38918. continue;
  38919. }
  38920. }
  38921. /* If we get there, we got a winner */
  38922. break;
  38923. }
  38924. if( cur == NULL )
  38925. cur = fallback;
  38926. /* Do not update ssl->handshake->key_cert unless there is a match */
  38927. if( cur != NULL )
  38928. {
  38929. ssl->handshake->key_cert = cur;
  38930. MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
  38931. ssl->handshake->key_cert->cert );
  38932. return( 0 );
  38933. }
  38934. return( -1 );
  38935. }
  38936. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  38937. /*
  38938. * Check if a given ciphersuite is suitable for use with our config/keys/etc
  38939. * Sets ciphersuite_info only if the suite matches.
  38940. */
  38941. static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
  38942. const mbedtls_ssl_ciphersuite_t **ciphersuite_info )
  38943. {
  38944. const mbedtls_ssl_ciphersuite_t *suite_info;
  38945. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  38946. defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  38947. mbedtls_pk_type_t sig_type;
  38948. #endif
  38949. suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
  38950. if( suite_info == NULL )
  38951. {
  38952. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  38953. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  38954. }
  38955. MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", suite_info->name ) );
  38956. if( suite_info->min_minor_ver > ssl->minor_ver ||
  38957. suite_info->max_minor_ver < ssl->minor_ver )
  38958. {
  38959. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
  38960. return( 0 );
  38961. }
  38962. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  38963. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  38964. ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
  38965. return( 0 );
  38966. #endif
  38967. #if defined(MBEDTLS_ARC4_C)
  38968. if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
  38969. suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
  38970. {
  38971. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
  38972. return( 0 );
  38973. }
  38974. #endif
  38975. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  38976. if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
  38977. ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
  38978. {
  38979. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
  38980. "not configured or ext missing" ) );
  38981. return( 0 );
  38982. }
  38983. #endif
  38984. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
  38985. if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
  38986. ( ssl->handshake->curves == NULL ||
  38987. ssl->handshake->curves[0] == NULL ) )
  38988. {
  38989. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
  38990. "no common elliptic curve" ) );
  38991. return( 0 );
  38992. }
  38993. #endif
  38994. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  38995. /* If the ciphersuite requires a pre-shared key and we don't
  38996. * have one, skip it now rather than failing later */
  38997. if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
  38998. ssl->conf->f_psk == NULL &&
  38999. ( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL ||
  39000. ssl->conf->psk_identity_len == 0 || ssl->conf->psk_len == 0 ) )
  39001. {
  39002. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
  39003. return( 0 );
  39004. }
  39005. #endif
  39006. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  39007. defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  39008. /* If the ciphersuite requires signing, check whether
  39009. * a suitable hash algorithm is present. */
  39010. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  39011. {
  39012. sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info );
  39013. if( sig_type != MBEDTLS_PK_NONE &&
  39014. mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE )
  39015. {
  39016. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
  39017. "for signature algorithm %d", sig_type ) );
  39018. return( 0 );
  39019. }
  39020. }
  39021. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
  39022. MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
  39023. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  39024. /*
  39025. * Final check: if ciphersuite requires us to have a
  39026. * certificate/key of a particular type:
  39027. * - select the appropriate certificate if we have one, or
  39028. * - try the next ciphersuite if we don't
  39029. * This must be done last since we modify the key_cert list.
  39030. */
  39031. if( ssl_pick_cert( ssl, suite_info ) != 0 )
  39032. {
  39033. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
  39034. "no suitable certificate" ) );
  39035. return( 0 );
  39036. }
  39037. #endif
  39038. *ciphersuite_info = suite_info;
  39039. return( 0 );
  39040. }
  39041. #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
  39042. static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl )
  39043. {
  39044. int ret, got_common_suite;
  39045. unsigned int i, j;
  39046. size_t n;
  39047. unsigned int ciph_len, sess_len, chal_len;
  39048. unsigned char *buf, *p;
  39049. const int *ciphersuites;
  39050. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  39051. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
  39052. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  39053. if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
  39054. {
  39055. MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
  39056. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39057. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  39058. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39059. }
  39060. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  39061. buf = ssl->in_hdr;
  39062. MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 );
  39063. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
  39064. buf[2] ) );
  39065. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
  39066. ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
  39067. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
  39068. buf[3], buf[4] ) );
  39069. /*
  39070. * SSLv2 Client Hello
  39071. *
  39072. * Record layer:
  39073. * 0 . 1 message length
  39074. *
  39075. * SSL layer:
  39076. * 2 . 2 message type
  39077. * 3 . 4 protocol version
  39078. */
  39079. if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO ||
  39080. buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 )
  39081. {
  39082. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39083. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39084. }
  39085. n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
  39086. if( n < 17 || n > 512 )
  39087. {
  39088. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39089. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39090. }
  39091. ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
  39092. ssl->minor_ver = ( buf[4] <= ssl->conf->max_minor_ver )
  39093. ? buf[4] : ssl->conf->max_minor_ver;
  39094. if( ssl->minor_ver < ssl->conf->min_minor_ver )
  39095. {
  39096. MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
  39097. " [%d:%d] < [%d:%d]",
  39098. ssl->major_ver, ssl->minor_ver,
  39099. ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
  39100. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39101. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
  39102. return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
  39103. }
  39104. ssl->handshake->max_major_ver = buf[3];
  39105. ssl->handshake->max_minor_ver = buf[4];
  39106. if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 )
  39107. {
  39108. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
  39109. return( ret );
  39110. }
  39111. ssl->handshake->update_checksum( ssl, buf + 2, n );
  39112. buf = ssl->in_msg;
  39113. n = ssl->in_left - 5;
  39114. /*
  39115. * 0 . 1 ciphersuitelist length
  39116. * 2 . 3 session id length
  39117. * 4 . 5 challenge length
  39118. * 6 . .. ciphersuitelist
  39119. * .. . .. session id
  39120. * .. . .. challenge
  39121. */
  39122. MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n );
  39123. ciph_len = ( buf[0] << 8 ) | buf[1];
  39124. sess_len = ( buf[2] << 8 ) | buf[3];
  39125. chal_len = ( buf[4] << 8 ) | buf[5];
  39126. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
  39127. ciph_len, sess_len, chal_len ) );
  39128. /*
  39129. * Make sure each parameter length is valid
  39130. */
  39131. if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
  39132. {
  39133. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39134. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39135. }
  39136. if( sess_len > 32 )
  39137. {
  39138. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39139. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39140. }
  39141. if( chal_len < 8 || chal_len > 32 )
  39142. {
  39143. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39144. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39145. }
  39146. if( n != 6 + ciph_len + sess_len + chal_len )
  39147. {
  39148. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39149. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39150. }
  39151. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
  39152. buf + 6, ciph_len );
  39153. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
  39154. buf + 6 + ciph_len, sess_len );
  39155. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge",
  39156. buf + 6 + ciph_len + sess_len, chal_len );
  39157. p = buf + 6 + ciph_len;
  39158. ssl->session_negotiate->id_len = sess_len;
  39159. memset( ssl->session_negotiate->id, 0,
  39160. sizeof( ssl->session_negotiate->id ) );
  39161. memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len );
  39162. p += sess_len;
  39163. memset( ssl->handshake->randbytes, 0, 64 );
  39164. memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
  39165. /*
  39166. * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
  39167. */
  39168. for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
  39169. {
  39170. if( p[0] == 0 && p[1] == 0 && p[2] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
  39171. {
  39172. MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
  39173. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  39174. if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
  39175. {
  39176. MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
  39177. "during renegotiation" ) );
  39178. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39179. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  39180. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39181. }
  39182. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  39183. ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
  39184. break;
  39185. }
  39186. }
  39187. #if defined(MBEDTLS_SSL_FALLBACK_SCSV)
  39188. for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
  39189. {
  39190. if( p[0] == 0 &&
  39191. p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
  39192. p[2] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
  39193. {
  39194. MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
  39195. if( ssl->minor_ver < ssl->conf->max_minor_ver )
  39196. {
  39197. MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
  39198. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39199. MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
  39200. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39201. }
  39202. break;
  39203. }
  39204. }
  39205. #endif /* MBEDTLS_SSL_FALLBACK_SCSV */
  39206. got_common_suite = 0;
  39207. ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
  39208. ciphersuite_info = NULL;
  39209. #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
  39210. for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
  39211. for( i = 0; ciphersuites[i] != 0; i++ )
  39212. #else
  39213. for( i = 0; ciphersuites[i] != 0; i++ )
  39214. for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
  39215. #endif
  39216. {
  39217. if( p[0] != 0 ||
  39218. p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
  39219. p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
  39220. continue;
  39221. got_common_suite = 1;
  39222. if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
  39223. &ciphersuite_info ) ) != 0 )
  39224. return( ret );
  39225. if( ciphersuite_info != NULL )
  39226. goto have_ciphersuite_v2;
  39227. }
  39228. if( got_common_suite )
  39229. {
  39230. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
  39231. "but none of them usable" ) );
  39232. return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
  39233. }
  39234. else
  39235. {
  39236. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
  39237. return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
  39238. }
  39239. have_ciphersuite_v2:
  39240. MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
  39241. ssl->session_negotiate->ciphersuite = ciphersuites[i];
  39242. ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
  39243. /*
  39244. * SSLv2 Client Hello relevant renegotiation security checks
  39245. */
  39246. if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  39247. ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
  39248. {
  39249. MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
  39250. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39251. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  39252. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39253. }
  39254. ssl->in_left = 0;
  39255. ssl->state++;
  39256. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
  39257. return( 0 );
  39258. }
  39259. #endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
  39260. /* This function doesn't alert on errors that happen early during
  39261. ClientHello parsing because they might indicate that the client is
  39262. not talking SSL/TLS at all and would not understand our alert. */
  39263. static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
  39264. {
  39265. int ret, got_common_suite;
  39266. size_t i, j;
  39267. size_t ciph_offset, comp_offset, ext_offset;
  39268. size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
  39269. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  39270. size_t cookie_offset, cookie_len;
  39271. #endif
  39272. unsigned char *buf, *p, *ext;
  39273. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  39274. int renegotiation_info_seen = 0;
  39275. #endif
  39276. int handshake_failure = 0;
  39277. const int *ciphersuites;
  39278. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  39279. int major, minor;
  39280. /* If there is no signature-algorithm extension present,
  39281. * we need to fall back to the default values for allowed
  39282. * signature-hash pairs. */
  39283. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  39284. defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  39285. int sig_hash_alg_ext_present = 0;
  39286. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
  39287. MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
  39288. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
  39289. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  39290. read_record_header:
  39291. #endif
  39292. /*
  39293. * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
  39294. * otherwise read it ourselves manually in order to support SSLv2
  39295. * ClientHello, which doesn't use the same record layer format.
  39296. */
  39297. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  39298. if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
  39299. #endif
  39300. {
  39301. if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
  39302. {
  39303. /* No alert on a read error. */
  39304. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
  39305. return( ret );
  39306. }
  39307. }
  39308. buf = ssl->in_hdr;
  39309. #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
  39310. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  39311. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
  39312. #endif
  39313. if( ( buf[0] & 0x80 ) != 0 )
  39314. return( ssl_parse_client_hello_v2( ssl ) );
  39315. #endif
  39316. MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_hdr_len( ssl ) );
  39317. /*
  39318. * SSLv3/TLS Client Hello
  39319. *
  39320. * Record layer:
  39321. * 0 . 0 message type
  39322. * 1 . 2 protocol version
  39323. * 3 . 11 DTLS: epoch + record sequence number
  39324. * 3 . 4 message length
  39325. */
  39326. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
  39327. buf[0] ) );
  39328. if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
  39329. {
  39330. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39331. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39332. }
  39333. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
  39334. ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
  39335. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
  39336. buf[1], buf[2] ) );
  39337. mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 );
  39338. /* According to RFC 5246 Appendix E.1, the version here is typically
  39339. * "{03,00}, the lowest version number supported by the client, [or] the
  39340. * value of ClientHello.client_version", so the only meaningful check here
  39341. * is the major version shouldn't be less than 3 */
  39342. if( major < MBEDTLS_SSL_MAJOR_VERSION_3 )
  39343. {
  39344. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39345. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39346. }
  39347. /* For DTLS if this is the initial handshake, remember the client sequence
  39348. * number to use it in our next message (RFC 6347 4.2.1) */
  39349. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  39350. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
  39351. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  39352. && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
  39353. #endif
  39354. )
  39355. {
  39356. /* Epoch should be 0 for initial handshakes */
  39357. if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
  39358. {
  39359. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39360. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39361. }
  39362. memcpy( ssl->out_ctr + 2, ssl->in_ctr + 2, 6 );
  39363. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  39364. if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
  39365. {
  39366. MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
  39367. ssl->next_record_offset = 0;
  39368. ssl->in_left = 0;
  39369. goto read_record_header;
  39370. }
  39371. /* No MAC to check yet, so we can update right now */
  39372. mbedtls_ssl_dtls_replay_update( ssl );
  39373. #endif
  39374. }
  39375. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  39376. msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
  39377. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  39378. if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
  39379. {
  39380. /* Set by mbedtls_ssl_read_record() */
  39381. msg_len = ssl->in_hslen;
  39382. }
  39383. else
  39384. #endif
  39385. {
  39386. if( msg_len > MBEDTLS_SSL_MAX_CONTENT_LEN )
  39387. {
  39388. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39389. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39390. }
  39391. if( ( ret = mbedtls_ssl_fetch_input( ssl,
  39392. mbedtls_ssl_hdr_len( ssl ) + msg_len ) ) != 0 )
  39393. {
  39394. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
  39395. return( ret );
  39396. }
  39397. /* Done reading this record, get ready for the next one */
  39398. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  39399. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  39400. ssl->next_record_offset = msg_len + mbedtls_ssl_hdr_len( ssl );
  39401. else
  39402. #endif
  39403. ssl->in_left = 0;
  39404. }
  39405. buf = ssl->in_msg;
  39406. MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
  39407. ssl->handshake->update_checksum( ssl, buf, msg_len );
  39408. /*
  39409. * Handshake layer:
  39410. * 0 . 0 handshake type
  39411. * 1 . 3 handshake length
  39412. * 4 . 5 DTLS only: message seqence number
  39413. * 6 . 8 DTLS only: fragment offset
  39414. * 9 . 11 DTLS only: fragment length
  39415. */
  39416. if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
  39417. {
  39418. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39419. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39420. }
  39421. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
  39422. if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
  39423. {
  39424. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39425. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39426. }
  39427. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
  39428. ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
  39429. /* We don't support fragmentation of ClientHello (yet?) */
  39430. if( buf[1] != 0 ||
  39431. msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
  39432. {
  39433. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39434. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39435. }
  39436. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  39437. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  39438. {
  39439. /*
  39440. * Copy the client's handshake message_seq on initial handshakes,
  39441. * check sequence number on renego.
  39442. */
  39443. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  39444. if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
  39445. {
  39446. /* This couldn't be done in ssl_prepare_handshake_record() */
  39447. unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
  39448. ssl->in_msg[5];
  39449. if( cli_msg_seq != ssl->handshake->in_msg_seq )
  39450. {
  39451. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
  39452. "%d (expected %d)", cli_msg_seq,
  39453. ssl->handshake->in_msg_seq ) );
  39454. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39455. }
  39456. ssl->handshake->in_msg_seq++;
  39457. }
  39458. else
  39459. #endif
  39460. {
  39461. unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
  39462. ssl->in_msg[5];
  39463. ssl->handshake->out_msg_seq = cli_msg_seq;
  39464. ssl->handshake->in_msg_seq = cli_msg_seq + 1;
  39465. }
  39466. /*
  39467. * For now we don't support fragmentation, so make sure
  39468. * fragment_offset == 0 and fragment_length == length
  39469. */
  39470. if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
  39471. memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
  39472. {
  39473. MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
  39474. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  39475. }
  39476. }
  39477. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  39478. buf += mbedtls_ssl_hs_hdr_len( ssl );
  39479. msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
  39480. /*
  39481. * ClientHello layer:
  39482. * 0 . 1 protocol version
  39483. * 2 . 33 random bytes (starting with 4 bytes of Unix time)
  39484. * 34 . 35 session id length (1 byte)
  39485. * 35 . 34+x session id
  39486. * 35+x . 35+x DTLS only: cookie length (1 byte)
  39487. * 36+x . .. DTLS only: cookie
  39488. * .. . .. ciphersuite list length (2 bytes)
  39489. * .. . .. ciphersuite list
  39490. * .. . .. compression alg. list length (1 byte)
  39491. * .. . .. compression alg. list
  39492. * .. . .. extensions length (2 bytes, optional)
  39493. * .. . .. extensions (optional)
  39494. */
  39495. /*
  39496. * Minimal length (with everything empty and extensions ommitted) is
  39497. * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
  39498. * read at least up to session id length without worrying.
  39499. */
  39500. if( msg_len < 38 )
  39501. {
  39502. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39503. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39504. }
  39505. /*
  39506. * Check and save the protocol version
  39507. */
  39508. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
  39509. mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
  39510. ssl->conf->transport, buf );
  39511. ssl->handshake->max_major_ver = ssl->major_ver;
  39512. ssl->handshake->max_minor_ver = ssl->minor_ver;
  39513. if( ssl->major_ver < ssl->conf->min_major_ver ||
  39514. ssl->minor_ver < ssl->conf->min_minor_ver )
  39515. {
  39516. MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
  39517. " [%d:%d] < [%d:%d]",
  39518. ssl->major_ver, ssl->minor_ver,
  39519. ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
  39520. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39521. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
  39522. return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
  39523. }
  39524. if( ssl->major_ver > ssl->conf->max_major_ver )
  39525. {
  39526. ssl->major_ver = ssl->conf->max_major_ver;
  39527. ssl->minor_ver = ssl->conf->max_minor_ver;
  39528. }
  39529. else if( ssl->minor_ver > ssl->conf->max_minor_ver )
  39530. ssl->minor_ver = ssl->conf->max_minor_ver;
  39531. /*
  39532. * Save client random (inc. Unix time)
  39533. */
  39534. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
  39535. memcpy( ssl->handshake->randbytes, buf + 2, 32 );
  39536. /*
  39537. * Check the session ID length and save session ID
  39538. */
  39539. sess_len = buf[34];
  39540. if( sess_len > sizeof( ssl->session_negotiate->id ) ||
  39541. sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
  39542. {
  39543. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39544. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39545. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  39546. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39547. }
  39548. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
  39549. ssl->session_negotiate->id_len = sess_len;
  39550. memset( ssl->session_negotiate->id, 0,
  39551. sizeof( ssl->session_negotiate->id ) );
  39552. memcpy( ssl->session_negotiate->id, buf + 35,
  39553. ssl->session_negotiate->id_len );
  39554. /*
  39555. * Check the cookie length and content
  39556. */
  39557. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  39558. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  39559. {
  39560. cookie_offset = 35 + sess_len;
  39561. cookie_len = buf[cookie_offset];
  39562. if( cookie_offset + 1 + cookie_len + 2 > msg_len )
  39563. {
  39564. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39565. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39566. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
  39567. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39568. }
  39569. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
  39570. buf + cookie_offset + 1, cookie_len );
  39571. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  39572. if( ssl->conf->f_cookie_check != NULL
  39573. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  39574. && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
  39575. #endif
  39576. )
  39577. {
  39578. if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
  39579. buf + cookie_offset + 1, cookie_len,
  39580. ssl->cli_id, ssl->cli_id_len ) != 0 )
  39581. {
  39582. MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
  39583. ssl->handshake->verify_cookie_len = 1;
  39584. }
  39585. else
  39586. {
  39587. MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
  39588. ssl->handshake->verify_cookie_len = 0;
  39589. }
  39590. }
  39591. else
  39592. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  39593. {
  39594. /* We know we didn't send a cookie, so it should be empty */
  39595. if( cookie_len != 0 )
  39596. {
  39597. /* This may be an attacker's probe, so don't send an alert */
  39598. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39599. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39600. }
  39601. MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
  39602. }
  39603. /*
  39604. * Check the ciphersuitelist length (will be parsed later)
  39605. */
  39606. ciph_offset = cookie_offset + 1 + cookie_len;
  39607. }
  39608. else
  39609. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  39610. ciph_offset = 35 + sess_len;
  39611. ciph_len = ( buf[ciph_offset + 0] << 8 )
  39612. | ( buf[ciph_offset + 1] );
  39613. if( ciph_len < 2 ||
  39614. ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
  39615. ( ciph_len % 2 ) != 0 )
  39616. {
  39617. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39618. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39619. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  39620. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39621. }
  39622. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
  39623. buf + ciph_offset + 2, ciph_len );
  39624. /*
  39625. * Check the compression algorithms length and pick one
  39626. */
  39627. comp_offset = ciph_offset + 2 + ciph_len;
  39628. comp_len = buf[comp_offset];
  39629. if( comp_len < 1 ||
  39630. comp_len > 16 ||
  39631. comp_len + comp_offset + 1 > msg_len )
  39632. {
  39633. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39634. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39635. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  39636. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39637. }
  39638. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
  39639. buf + comp_offset + 1, comp_len );
  39640. ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
  39641. #if defined(MBEDTLS_ZLIB_SUPPORT)
  39642. for( i = 0; i < comp_len; ++i )
  39643. {
  39644. if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE )
  39645. {
  39646. ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE;
  39647. break;
  39648. }
  39649. }
  39650. #endif
  39651. /* See comments in ssl_write_client_hello() */
  39652. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  39653. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  39654. ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
  39655. #endif
  39656. /* Do not parse the extensions if the protocol is SSLv3 */
  39657. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  39658. if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
  39659. {
  39660. #endif
  39661. /*
  39662. * Check the extension length
  39663. */
  39664. ext_offset = comp_offset + 1 + comp_len;
  39665. if( msg_len > ext_offset )
  39666. {
  39667. if( msg_len < ext_offset + 2 )
  39668. {
  39669. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39670. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39671. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  39672. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39673. }
  39674. ext_len = ( buf[ext_offset + 0] << 8 )
  39675. | ( buf[ext_offset + 1] );
  39676. if( ( ext_len > 0 && ext_len < 4 ) ||
  39677. msg_len != ext_offset + 2 + ext_len )
  39678. {
  39679. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39680. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39681. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  39682. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39683. }
  39684. }
  39685. else
  39686. ext_len = 0;
  39687. ext = buf + ext_offset + 2;
  39688. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
  39689. while( ext_len != 0 )
  39690. {
  39691. unsigned int ext_id = ( ( ext[0] << 8 )
  39692. | ( ext[1] ) );
  39693. unsigned int ext_size = ( ( ext[2] << 8 )
  39694. | ( ext[3] ) );
  39695. if( ext_size + 4 > ext_len )
  39696. {
  39697. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39698. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39699. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  39700. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39701. }
  39702. switch( ext_id )
  39703. {
  39704. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  39705. case MBEDTLS_TLS_EXT_SERVERNAME:
  39706. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
  39707. if( ssl->conf->f_sni == NULL )
  39708. break;
  39709. ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
  39710. if( ret != 0 )
  39711. return( ret );
  39712. break;
  39713. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  39714. case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
  39715. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
  39716. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  39717. renegotiation_info_seen = 1;
  39718. #endif
  39719. ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
  39720. if( ret != 0 )
  39721. return( ret );
  39722. break;
  39723. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  39724. defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  39725. case MBEDTLS_TLS_EXT_SIG_ALG:
  39726. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
  39727. ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
  39728. if( ret != 0 )
  39729. return( ret );
  39730. sig_hash_alg_ext_present = 1;
  39731. break;
  39732. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
  39733. MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
  39734. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  39735. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  39736. case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
  39737. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
  39738. ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
  39739. if( ret != 0 )
  39740. return( ret );
  39741. break;
  39742. case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
  39743. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
  39744. ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
  39745. ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
  39746. if( ret != 0 )
  39747. return( ret );
  39748. break;
  39749. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  39750. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  39751. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  39752. case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
  39753. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
  39754. ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
  39755. if( ret != 0 )
  39756. return( ret );
  39757. break;
  39758. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  39759. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  39760. case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
  39761. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
  39762. ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
  39763. if( ret != 0 )
  39764. return( ret );
  39765. break;
  39766. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  39767. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  39768. case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
  39769. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
  39770. ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
  39771. if( ret != 0 )
  39772. return( ret );
  39773. break;
  39774. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  39775. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  39776. case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
  39777. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
  39778. ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
  39779. if( ret != 0 )
  39780. return( ret );
  39781. break;
  39782. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  39783. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  39784. case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
  39785. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
  39786. ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
  39787. if( ret != 0 )
  39788. return( ret );
  39789. break;
  39790. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  39791. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  39792. case MBEDTLS_TLS_EXT_SESSION_TICKET:
  39793. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
  39794. ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
  39795. if( ret != 0 )
  39796. return( ret );
  39797. break;
  39798. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  39799. #if defined(MBEDTLS_SSL_ALPN)
  39800. case MBEDTLS_TLS_EXT_ALPN:
  39801. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
  39802. ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
  39803. if( ret != 0 )
  39804. return( ret );
  39805. break;
  39806. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  39807. default:
  39808. MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
  39809. ext_id ) );
  39810. }
  39811. ext_len -= 4 + ext_size;
  39812. ext += 4 + ext_size;
  39813. if( ext_len > 0 && ext_len < 4 )
  39814. {
  39815. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
  39816. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39817. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  39818. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39819. }
  39820. }
  39821. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  39822. }
  39823. #endif
  39824. #if defined(MBEDTLS_SSL_FALLBACK_SCSV)
  39825. for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
  39826. {
  39827. if( p[0] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
  39828. p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
  39829. {
  39830. MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) );
  39831. if( ssl->minor_ver < ssl->conf->max_minor_ver )
  39832. {
  39833. MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
  39834. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39835. MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
  39836. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39837. }
  39838. break;
  39839. }
  39840. }
  39841. #endif /* MBEDTLS_SSL_FALLBACK_SCSV */
  39842. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  39843. defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  39844. /*
  39845. * Try to fall back to default hash SHA1 if the client
  39846. * hasn't provided any preferred signature-hash combinations.
  39847. */
  39848. if( sig_hash_alg_ext_present == 0 )
  39849. {
  39850. mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
  39851. if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 )
  39852. md_default = MBEDTLS_MD_NONE;
  39853. mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
  39854. }
  39855. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
  39856. MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
  39857. /*
  39858. * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
  39859. */
  39860. for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
  39861. {
  39862. if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
  39863. {
  39864. MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
  39865. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  39866. if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
  39867. {
  39868. MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
  39869. "during renegotiation" ) );
  39870. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39871. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  39872. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39873. }
  39874. #endif
  39875. ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
  39876. break;
  39877. }
  39878. }
  39879. /*
  39880. * Renegotiation security checks
  39881. */
  39882. if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
  39883. ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
  39884. {
  39885. MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
  39886. handshake_failure = 1;
  39887. }
  39888. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  39889. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  39890. ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
  39891. renegotiation_info_seen == 0 )
  39892. {
  39893. MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
  39894. handshake_failure = 1;
  39895. }
  39896. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  39897. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  39898. ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
  39899. {
  39900. MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
  39901. handshake_failure = 1;
  39902. }
  39903. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  39904. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  39905. renegotiation_info_seen == 1 )
  39906. {
  39907. MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
  39908. handshake_failure = 1;
  39909. }
  39910. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  39911. if( handshake_failure == 1 )
  39912. {
  39913. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39914. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  39915. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  39916. }
  39917. /*
  39918. * Search for a matching ciphersuite
  39919. * (At the end because we need information from the EC-based extensions
  39920. * and certificate from the SNI callback triggered by the SNI extension.)
  39921. */
  39922. got_common_suite = 0;
  39923. ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
  39924. ciphersuite_info = NULL;
  39925. #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
  39926. for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
  39927. for( i = 0; ciphersuites[i] != 0; i++ )
  39928. #else
  39929. for( i = 0; ciphersuites[i] != 0; i++ )
  39930. for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
  39931. #endif
  39932. {
  39933. if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
  39934. p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
  39935. continue;
  39936. got_common_suite = 1;
  39937. if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
  39938. &ciphersuite_info ) ) != 0 )
  39939. return( ret );
  39940. if( ciphersuite_info != NULL )
  39941. goto have_ciphersuite;
  39942. }
  39943. if( got_common_suite )
  39944. {
  39945. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
  39946. "but none of them usable" ) );
  39947. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39948. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  39949. return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
  39950. }
  39951. else
  39952. {
  39953. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
  39954. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  39955. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  39956. return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
  39957. }
  39958. have_ciphersuite:
  39959. MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
  39960. ssl->session_negotiate->ciphersuite = ciphersuites[i];
  39961. ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
  39962. ssl->state++;
  39963. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  39964. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  39965. mbedtls_ssl_recv_flight_completed( ssl );
  39966. #endif
  39967. /* Debugging-only output for testsuite */
  39968. #if defined(MBEDTLS_DEBUG_C) && \
  39969. defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  39970. defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  39971. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  39972. {
  39973. mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info );
  39974. if( sig_alg != MBEDTLS_PK_NONE )
  39975. {
  39976. mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
  39977. sig_alg );
  39978. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
  39979. mbedtls_ssl_hash_from_md_alg( md_alg ) ) );
  39980. }
  39981. else
  39982. {
  39983. MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm "
  39984. "%d - should not happen", sig_alg ) );
  39985. }
  39986. }
  39987. #endif
  39988. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
  39989. return( 0 );
  39990. }
  39991. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  39992. static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
  39993. unsigned char *buf,
  39994. size_t *olen )
  39995. {
  39996. unsigned char *p = buf;
  39997. if( ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
  39998. {
  39999. *olen = 0;
  40000. return;
  40001. }
  40002. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
  40003. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
  40004. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
  40005. *p++ = 0x00;
  40006. *p++ = 0x00;
  40007. *olen = 4;
  40008. }
  40009. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  40010. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  40011. static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
  40012. unsigned char *buf,
  40013. size_t *olen )
  40014. {
  40015. unsigned char *p = buf;
  40016. const mbedtls_ssl_ciphersuite_t *suite = NULL;
  40017. const mbedtls_cipher_info_t *cipher = NULL;
  40018. if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
  40019. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  40020. {
  40021. *olen = 0;
  40022. return;
  40023. }
  40024. /*
  40025. * RFC 7366: "If a server receives an encrypt-then-MAC request extension
  40026. * from a client and then selects a stream or Authenticated Encryption
  40027. * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
  40028. * encrypt-then-MAC response extension back to the client."
  40029. */
  40030. if( ( suite = mbedtls_ssl_ciphersuite_from_id(
  40031. ssl->session_negotiate->ciphersuite ) ) == NULL ||
  40032. ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL ||
  40033. cipher->mode != MBEDTLS_MODE_CBC )
  40034. {
  40035. *olen = 0;
  40036. return;
  40037. }
  40038. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
  40039. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
  40040. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
  40041. *p++ = 0x00;
  40042. *p++ = 0x00;
  40043. *olen = 4;
  40044. }
  40045. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  40046. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  40047. static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
  40048. unsigned char *buf,
  40049. size_t *olen )
  40050. {
  40051. unsigned char *p = buf;
  40052. if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
  40053. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  40054. {
  40055. *olen = 0;
  40056. return;
  40057. }
  40058. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
  40059. "extension" ) );
  40060. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
  40061. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
  40062. *p++ = 0x00;
  40063. *p++ = 0x00;
  40064. *olen = 4;
  40065. }
  40066. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  40067. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  40068. static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
  40069. unsigned char *buf,
  40070. size_t *olen )
  40071. {
  40072. unsigned char *p = buf;
  40073. if( ssl->handshake->new_session_ticket == 0 )
  40074. {
  40075. *olen = 0;
  40076. return;
  40077. }
  40078. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
  40079. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
  40080. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF );
  40081. *p++ = 0x00;
  40082. *p++ = 0x00;
  40083. *olen = 4;
  40084. }
  40085. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  40086. static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
  40087. unsigned char *buf,
  40088. size_t *olen )
  40089. {
  40090. unsigned char *p = buf;
  40091. if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
  40092. {
  40093. *olen = 0;
  40094. return;
  40095. }
  40096. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
  40097. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
  40098. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
  40099. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  40100. if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
  40101. {
  40102. *p++ = 0x00;
  40103. *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
  40104. *p++ = ssl->verify_data_len * 2 & 0xFF;
  40105. memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
  40106. p += ssl->verify_data_len;
  40107. memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
  40108. p += ssl->verify_data_len;
  40109. }
  40110. else
  40111. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  40112. {
  40113. *p++ = 0x00;
  40114. *p++ = 0x01;
  40115. *p++ = 0x00;
  40116. }
  40117. *olen = p - buf;
  40118. }
  40119. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  40120. static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
  40121. unsigned char *buf,
  40122. size_t *olen )
  40123. {
  40124. unsigned char *p = buf;
  40125. if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
  40126. {
  40127. *olen = 0;
  40128. return;
  40129. }
  40130. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
  40131. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
  40132. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
  40133. *p++ = 0x00;
  40134. *p++ = 1;
  40135. *p++ = ssl->session_negotiate->mfl_code;
  40136. *olen = 5;
  40137. }
  40138. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  40139. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  40140. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  40141. static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
  40142. unsigned char *buf,
  40143. size_t *olen )
  40144. {
  40145. unsigned char *p = buf;
  40146. ((void) ssl);
  40147. if( ( ssl->handshake->cli_exts &
  40148. MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
  40149. {
  40150. *olen = 0;
  40151. return;
  40152. }
  40153. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
  40154. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
  40155. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
  40156. *p++ = 0x00;
  40157. *p++ = 2;
  40158. *p++ = 1;
  40159. *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
  40160. *olen = 6;
  40161. }
  40162. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  40163. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  40164. static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
  40165. unsigned char *buf,
  40166. size_t *olen )
  40167. {
  40168. int ret;
  40169. unsigned char *p = buf;
  40170. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  40171. size_t kkpp_len;
  40172. *olen = 0;
  40173. /* Skip costly computation if not needed */
  40174. if( ssl->transform_negotiate->ciphersuite_info->key_exchange !=
  40175. MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  40176. return;
  40177. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
  40178. if( end - p < 4 )
  40179. {
  40180. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  40181. return;
  40182. }
  40183. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF );
  40184. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF );
  40185. ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
  40186. p + 2, end - p - 2, &kkpp_len,
  40187. ssl->conf->f_rng, ssl->conf->p_rng );
  40188. if( ret != 0 )
  40189. {
  40190. MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
  40191. return;
  40192. }
  40193. *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF );
  40194. *p++ = (unsigned char)( ( kkpp_len ) & 0xFF );
  40195. *olen = kkpp_len + 4;
  40196. }
  40197. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  40198. #if defined(MBEDTLS_SSL_ALPN )
  40199. static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
  40200. unsigned char *buf, size_t *olen )
  40201. {
  40202. if( ssl->alpn_chosen == NULL )
  40203. {
  40204. *olen = 0;
  40205. return;
  40206. }
  40207. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
  40208. /*
  40209. * 0 . 1 ext identifier
  40210. * 2 . 3 ext length
  40211. * 4 . 5 protocol list length
  40212. * 6 . 6 protocol name length
  40213. * 7 . 7+n protocol name
  40214. */
  40215. buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF );
  40216. buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF );
  40217. *olen = 7 + strlen( ssl->alpn_chosen );
  40218. buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
  40219. buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
  40220. buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
  40221. buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
  40222. buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
  40223. memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
  40224. }
  40225. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
  40226. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  40227. static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
  40228. {
  40229. int ret;
  40230. unsigned char *p = ssl->out_msg + 4;
  40231. unsigned char *cookie_len_byte;
  40232. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
  40233. /*
  40234. * struct {
  40235. * ProtocolVersion server_version;
  40236. * opaque cookie<0..2^8-1>;
  40237. * } HelloVerifyRequest;
  40238. */
  40239. /* The RFC is not clear on this point, but sending the actual negotiated
  40240. * version looks like the most interoperable thing to do. */
  40241. mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
  40242. ssl->conf->transport, p );
  40243. MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
  40244. p += 2;
  40245. /* If we get here, f_cookie_check is not null */
  40246. if( ssl->conf->f_cookie_write == NULL )
  40247. {
  40248. MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
  40249. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  40250. }
  40251. /* Skip length byte until we know the length */
  40252. cookie_len_byte = p++;
  40253. if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie,
  40254. &p, ssl->out_buf + MBEDTLS_SSL_BUFFER_LEN,
  40255. ssl->cli_id, ssl->cli_id_len ) ) != 0 )
  40256. {
  40257. MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret );
  40258. return( ret );
  40259. }
  40260. *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
  40261. MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
  40262. ssl->out_msglen = p - ssl->out_msg;
  40263. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  40264. ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
  40265. ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
  40266. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  40267. {
  40268. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  40269. return( ret );
  40270. }
  40271. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
  40272. return( 0 );
  40273. }
  40274. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  40275. static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
  40276. {
  40277. #if defined(MBEDTLS_HAVE_TIME)
  40278. mbedtls_time_t t;
  40279. #endif
  40280. int ret;
  40281. size_t olen, ext_len = 0, n;
  40282. unsigned char *buf, *p;
  40283. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
  40284. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  40285. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  40286. ssl->handshake->verify_cookie_len != 0 )
  40287. {
  40288. MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
  40289. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
  40290. return( ssl_write_hello_verify_request( ssl ) );
  40291. }
  40292. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  40293. if( ssl->conf->f_rng == NULL )
  40294. {
  40295. MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
  40296. return( MBEDTLS_ERR_SSL_NO_RNG );
  40297. }
  40298. /*
  40299. * 0 . 0 handshake type
  40300. * 1 . 3 handshake length
  40301. * 4 . 5 protocol version
  40302. * 6 . 9 UNIX time()
  40303. * 10 . 37 random bytes
  40304. */
  40305. buf = ssl->out_msg;
  40306. p = buf + 4;
  40307. mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
  40308. ssl->conf->transport, p );
  40309. p += 2;
  40310. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
  40311. buf[4], buf[5] ) );
  40312. #if defined(MBEDTLS_HAVE_TIME)
  40313. t = mbedtls_time( NULL );
  40314. *p++ = (unsigned char)( t >> 24 );
  40315. *p++ = (unsigned char)( t >> 16 );
  40316. *p++ = (unsigned char)( t >> 8 );
  40317. *p++ = (unsigned char)( t );
  40318. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
  40319. #else
  40320. if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
  40321. return( ret );
  40322. p += 4;
  40323. #endif /* MBEDTLS_HAVE_TIME */
  40324. if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
  40325. return( ret );
  40326. p += 28;
  40327. memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
  40328. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
  40329. /*
  40330. * Resume is 0 by default, see ssl_handshake_init().
  40331. * It may be already set to 1 by ssl_parse_session_ticket_ext().
  40332. * If not, try looking up session ID in our cache.
  40333. */
  40334. if( ssl->handshake->resume == 0 &&
  40335. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  40336. ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE &&
  40337. #endif
  40338. ssl->session_negotiate->id_len != 0 &&
  40339. ssl->conf->f_get_cache != NULL &&
  40340. ssl->conf->f_get_cache( ssl->conf->p_cache, ssl->session_negotiate ) == 0 )
  40341. {
  40342. MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
  40343. ssl->handshake->resume = 1;
  40344. }
  40345. if( ssl->handshake->resume == 0 )
  40346. {
  40347. /*
  40348. * New session, create a new session id,
  40349. * unless we're about to issue a session ticket
  40350. */
  40351. ssl->state++;
  40352. #if defined(MBEDTLS_HAVE_TIME)
  40353. ssl->session_negotiate->start = mbedtls_time( NULL );
  40354. #endif
  40355. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  40356. if( ssl->handshake->new_session_ticket != 0 )
  40357. {
  40358. ssl->session_negotiate->id_len = n = 0;
  40359. memset( ssl->session_negotiate->id, 0, 32 );
  40360. }
  40361. else
  40362. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  40363. {
  40364. ssl->session_negotiate->id_len = n = 32;
  40365. if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id,
  40366. n ) ) != 0 )
  40367. return( ret );
  40368. }
  40369. }
  40370. else
  40371. {
  40372. /*
  40373. * Resuming a session
  40374. */
  40375. n = ssl->session_negotiate->id_len;
  40376. ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
  40377. if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
  40378. {
  40379. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
  40380. return( ret );
  40381. }
  40382. }
  40383. /*
  40384. * 38 . 38 session id length
  40385. * 39 . 38+n session id
  40386. * 39+n . 40+n chosen ciphersuite
  40387. * 41+n . 41+n chosen compression alg.
  40388. * 42+n . 43+n extensions length
  40389. * 44+n . 43+n+m extensions
  40390. */
  40391. *p++ = (unsigned char) ssl->session_negotiate->id_len;
  40392. memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
  40393. p += ssl->session_negotiate->id_len;
  40394. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
  40395. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
  40396. MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
  40397. ssl->handshake->resume ? "a" : "no" ) );
  40398. *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
  40399. *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
  40400. *p++ = (unsigned char)( ssl->session_negotiate->compression );
  40401. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
  40402. mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
  40403. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
  40404. ssl->session_negotiate->compression ) );
  40405. /* Do not write the extensions if the protocol is SSLv3 */
  40406. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  40407. if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
  40408. {
  40409. #endif
  40410. /*
  40411. * First write extensions, then the total length
  40412. */
  40413. ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
  40414. ext_len += olen;
  40415. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  40416. ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
  40417. ext_len += olen;
  40418. #endif
  40419. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  40420. ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
  40421. ext_len += olen;
  40422. #endif
  40423. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  40424. ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
  40425. ext_len += olen;
  40426. #endif
  40427. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  40428. ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
  40429. ext_len += olen;
  40430. #endif
  40431. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  40432. ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
  40433. ext_len += olen;
  40434. #endif
  40435. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  40436. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  40437. ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
  40438. ext_len += olen;
  40439. #endif
  40440. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  40441. ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
  40442. ext_len += olen;
  40443. #endif
  40444. #if defined(MBEDTLS_SSL_ALPN)
  40445. ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
  40446. ext_len += olen;
  40447. #endif
  40448. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
  40449. if( ext_len > 0 )
  40450. {
  40451. *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
  40452. *p++ = (unsigned char)( ( ext_len ) & 0xFF );
  40453. p += ext_len;
  40454. }
  40455. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  40456. }
  40457. #endif
  40458. ssl->out_msglen = p - buf;
  40459. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  40460. ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
  40461. ret = mbedtls_ssl_write_record( ssl );
  40462. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
  40463. return( ret );
  40464. }
  40465. #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
  40466. !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
  40467. !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
  40468. !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
  40469. !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
  40470. !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  40471. static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
  40472. {
  40473. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  40474. ssl->transform_negotiate->ciphersuite_info;
  40475. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
  40476. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  40477. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  40478. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  40479. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  40480. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  40481. {
  40482. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
  40483. ssl->state++;
  40484. return( 0 );
  40485. }
  40486. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  40487. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  40488. }
  40489. #else
  40490. static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
  40491. {
  40492. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  40493. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  40494. ssl->transform_negotiate->ciphersuite_info;
  40495. size_t dn_size, total_dn_size; /* excluding length bytes */
  40496. size_t ct_len, sa_len; /* including length bytes */
  40497. unsigned char *buf, *p;
  40498. const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  40499. const mbedtls_x509_crt *crt;
  40500. int authmode;
  40501. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
  40502. ssl->state++;
  40503. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  40504. if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
  40505. authmode = ssl->handshake->sni_authmode;
  40506. else
  40507. #endif
  40508. authmode = ssl->conf->authmode;
  40509. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  40510. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  40511. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  40512. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  40513. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ||
  40514. authmode == MBEDTLS_SSL_VERIFY_NONE )
  40515. {
  40516. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
  40517. return( 0 );
  40518. }
  40519. /*
  40520. * 0 . 0 handshake type
  40521. * 1 . 3 handshake length
  40522. * 4 . 4 cert type count
  40523. * 5 .. m-1 cert types
  40524. * m .. m+1 sig alg length (TLS 1.2 only)
  40525. * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
  40526. * n .. n+1 length of all DNs
  40527. * n+2 .. n+3 length of DN 1
  40528. * n+4 .. ... Distinguished Name #1
  40529. * ... .. ... length of DN 2, etc.
  40530. */
  40531. buf = ssl->out_msg;
  40532. p = buf + 4;
  40533. /*
  40534. * Supported certificate types
  40535. *
  40536. * ClientCertificateType certificate_types<1..2^8-1>;
  40537. * enum { (255) } ClientCertificateType;
  40538. */
  40539. ct_len = 0;
  40540. #if defined(MBEDTLS_RSA_C)
  40541. p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
  40542. #endif
  40543. #if defined(MBEDTLS_ECDSA_C)
  40544. p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
  40545. #endif
  40546. p[0] = (unsigned char) ct_len++;
  40547. p += ct_len;
  40548. sa_len = 0;
  40549. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  40550. /*
  40551. * Add signature_algorithms for verify (TLS 1.2)
  40552. *
  40553. * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
  40554. *
  40555. * struct {
  40556. * HashAlgorithm hash;
  40557. * SignatureAlgorithm signature;
  40558. * } SignatureAndHashAlgorithm;
  40559. *
  40560. * enum { (255) } HashAlgorithm;
  40561. * enum { (255) } SignatureAlgorithm;
  40562. */
  40563. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  40564. {
  40565. const int *cur;
  40566. /*
  40567. * Supported signature algorithms
  40568. */
  40569. for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
  40570. {
  40571. unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur );
  40572. if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) )
  40573. continue;
  40574. #if defined(MBEDTLS_RSA_C)
  40575. p[2 + sa_len++] = hash;
  40576. p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
  40577. #endif
  40578. #if defined(MBEDTLS_ECDSA_C)
  40579. p[2 + sa_len++] = hash;
  40580. p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
  40581. #endif
  40582. }
  40583. p[0] = (unsigned char)( sa_len >> 8 );
  40584. p[1] = (unsigned char)( sa_len );
  40585. sa_len += 2;
  40586. p += sa_len;
  40587. }
  40588. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  40589. /*
  40590. * DistinguishedName certificate_authorities<0..2^16-1>;
  40591. * opaque DistinguishedName<1..2^16-1>;
  40592. */
  40593. p += 2;
  40594. total_dn_size = 0;
  40595. if( ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED )
  40596. {
  40597. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  40598. if( ssl->handshake->sni_ca_chain != NULL )
  40599. crt = ssl->handshake->sni_ca_chain;
  40600. else
  40601. #endif
  40602. crt = ssl->conf->ca_chain;
  40603. while( crt != NULL && crt->version != 0 )
  40604. {
  40605. dn_size = crt->subject_raw.len;
  40606. if( end < p ||
  40607. (size_t)( end - p ) < dn_size ||
  40608. (size_t)( end - p ) < 2 + dn_size )
  40609. {
  40610. MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
  40611. break;
  40612. }
  40613. *p++ = (unsigned char)( dn_size >> 8 );
  40614. *p++ = (unsigned char)( dn_size );
  40615. memcpy( p, crt->subject_raw.p, dn_size );
  40616. p += dn_size;
  40617. MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
  40618. total_dn_size += 2 + dn_size;
  40619. crt = crt->next;
  40620. }
  40621. }
  40622. ssl->out_msglen = p - buf;
  40623. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  40624. ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
  40625. ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
  40626. ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
  40627. ret = mbedtls_ssl_write_record( ssl );
  40628. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
  40629. return( ret );
  40630. }
  40631. #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED &&
  40632. !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED &&
  40633. !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
  40634. !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
  40635. !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
  40636. !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  40637. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  40638. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  40639. static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
  40640. {
  40641. int ret;
  40642. if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) )
  40643. {
  40644. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
  40645. return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
  40646. }
  40647. if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
  40648. mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
  40649. MBEDTLS_ECDH_OURS ) ) != 0 )
  40650. {
  40651. MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
  40652. return( ret );
  40653. }
  40654. return( 0 );
  40655. }
  40656. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
  40657. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  40658. static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
  40659. {
  40660. int ret;
  40661. size_t n = 0;
  40662. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  40663. ssl->transform_negotiate->ciphersuite_info;
  40664. #if defined(MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED)
  40665. unsigned char *p = ssl->out_msg + 4;
  40666. size_t len;
  40667. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED)
  40668. unsigned char *dig_signed = p;
  40669. size_t dig_signed_len = 0;
  40670. #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */
  40671. #endif /* MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED */
  40672. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
  40673. /*
  40674. *
  40675. * Part 1: Extract static ECDH parameters and abort
  40676. * if ServerKeyExchange not needed.
  40677. *
  40678. */
  40679. /* For suites involving ECDH, extract DH parameters
  40680. * from certificate at this point. */
  40681. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED)
  40682. if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) )
  40683. {
  40684. ssl_get_ecdh_params_from_cert( ssl );
  40685. }
  40686. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED */
  40687. /* Key exchanges not involving ephemeral keys don't use
  40688. * ServerKeyExchange, so end here. */
  40689. #if defined(MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED)
  40690. if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) )
  40691. {
  40692. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
  40693. ssl->state++;
  40694. return( 0 );
  40695. }
  40696. #endif /* MBEDTLS_KEY_EXCHANGE__NON_PFS__ENABLED */
  40697. /*
  40698. *
  40699. * Part 2: Provide key exchange parameters for chosen ciphersuite.
  40700. *
  40701. */
  40702. /*
  40703. * - ECJPAKE key exchanges
  40704. */
  40705. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  40706. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  40707. {
  40708. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  40709. ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
  40710. p, end - p, &len, ssl->conf->f_rng, ssl->conf->p_rng );
  40711. if( ret != 0 )
  40712. {
  40713. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
  40714. return( ret );
  40715. }
  40716. p += len;
  40717. n += len;
  40718. }
  40719. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  40720. /*
  40721. * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
  40722. * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
  40723. * we use empty support identity hints here.
  40724. **/
  40725. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
  40726. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  40727. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  40728. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
  40729. {
  40730. *(p++) = 0x00;
  40731. *(p++) = 0x00;
  40732. n += 2;
  40733. }
  40734. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
  40735. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  40736. /*
  40737. * - DHE key exchanges
  40738. */
  40739. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__DHE_ENABLED)
  40740. if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) )
  40741. {
  40742. if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
  40743. {
  40744. MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
  40745. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  40746. }
  40747. /*
  40748. * Ephemeral DH parameters:
  40749. *
  40750. * struct {
  40751. * opaque dh_p<1..2^16-1>;
  40752. * opaque dh_g<1..2^16-1>;
  40753. * opaque dh_Ys<1..2^16-1>;
  40754. * } ServerDHParams;
  40755. */
  40756. if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx,
  40757. &ssl->conf->dhm_P,
  40758. &ssl->conf->dhm_G ) ) != 0 )
  40759. {
  40760. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret );
  40761. return( ret );
  40762. }
  40763. if( ( ret = mbedtls_dhm_make_params( &ssl->handshake->dhm_ctx,
  40764. (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
  40765. p, &len, ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  40766. {
  40767. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
  40768. return( ret );
  40769. }
  40770. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED)
  40771. dig_signed = p;
  40772. dig_signed_len = len;
  40773. #endif
  40774. p += len;
  40775. n += len;
  40776. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
  40777. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
  40778. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
  40779. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
  40780. }
  40781. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__DHE_ENABLED */
  40782. /*
  40783. * - ECDHE key exchanges
  40784. */
  40785. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
  40786. if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) )
  40787. {
  40788. /*
  40789. * Ephemeral ECDH parameters:
  40790. *
  40791. * struct {
  40792. * ECParameters curve_params;
  40793. * ECPoint public;
  40794. * } ServerECDHParams;
  40795. */
  40796. const mbedtls_ecp_curve_info **curve = NULL;
  40797. const mbedtls_ecp_group_id *gid;
  40798. /* Match our preference list against the offered curves */
  40799. for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
  40800. for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
  40801. if( (*curve)->grp_id == *gid )
  40802. goto curve_matching_done;
  40803. curve_matching_done:
  40804. if( curve == NULL || *curve == NULL )
  40805. {
  40806. MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
  40807. return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
  40808. }
  40809. MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
  40810. if( ( ret = mbedtls_ecp_group_load( &ssl->handshake->ecdh_ctx.grp,
  40811. (*curve)->grp_id ) ) != 0 )
  40812. {
  40813. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
  40814. return( ret );
  40815. }
  40816. if( ( ret = mbedtls_ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
  40817. p, MBEDTLS_SSL_MAX_CONTENT_LEN - n,
  40818. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  40819. {
  40820. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
  40821. return( ret );
  40822. }
  40823. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED)
  40824. dig_signed = p;
  40825. dig_signed_len = len;
  40826. #endif
  40827. p += len;
  40828. n += len;
  40829. MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
  40830. }
  40831. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
  40832. /*
  40833. *
  40834. * Part 3: For key exchanges involving the server signing the
  40835. * exchange parameters, compute and add the signature here.
  40836. *
  40837. */
  40838. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED)
  40839. if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
  40840. {
  40841. size_t signature_len = 0;
  40842. unsigned int hashlen = 0;
  40843. unsigned char hash[64];
  40844. /*
  40845. * 3.1: Choose hash algorithm:
  40846. * A: For TLS 1.2, obey signature-hash-algorithm extension
  40847. * to choose appropriate hash.
  40848. * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1
  40849. * (RFC 4492, Sec. 5.4)
  40850. * C: Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
  40851. */
  40852. mbedtls_md_type_t md_alg;
  40853. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  40854. mbedtls_pk_type_t sig_alg =
  40855. mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
  40856. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  40857. {
  40858. /* A: For TLS 1.2, obey signature-hash-algorithm extension
  40859. * (RFC 5246, Sec. 7.4.1.4.1). */
  40860. if( sig_alg == MBEDTLS_PK_NONE ||
  40861. ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
  40862. sig_alg ) ) == MBEDTLS_MD_NONE )
  40863. {
  40864. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  40865. /* (... because we choose a cipher suite
  40866. * only if there is a matching hash.) */
  40867. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  40868. }
  40869. }
  40870. else
  40871. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  40872. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  40873. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  40874. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
  40875. {
  40876. /* B: Default hash SHA1 */
  40877. md_alg = MBEDTLS_MD_SHA1;
  40878. }
  40879. else
  40880. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  40881. MBEDTLS_SSL_PROTO_TLS1_1 */
  40882. {
  40883. /* C: MD5 + SHA1 */
  40884. md_alg = MBEDTLS_MD_NONE;
  40885. }
  40886. MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %d for signing", md_alg ) );
  40887. /*
  40888. * 3.2: Compute the hash to be signed
  40889. */
  40890. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  40891. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  40892. if( md_alg == MBEDTLS_MD_NONE )
  40893. {
  40894. hashlen = 36;
  40895. ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash,
  40896. dig_signed,
  40897. dig_signed_len );
  40898. if( ret != 0 )
  40899. return( ret );
  40900. }
  40901. else
  40902. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  40903. MBEDTLS_SSL_PROTO_TLS1_1 */
  40904. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  40905. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  40906. if( md_alg != MBEDTLS_MD_NONE )
  40907. {
  40908. /* Info from md_alg will be used instead */
  40909. hashlen = 0;
  40910. ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash,
  40911. dig_signed,
  40912. dig_signed_len,
  40913. md_alg );
  40914. if( ret != 0 )
  40915. return( ret );
  40916. }
  40917. else
  40918. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
  40919. MBEDTLS_SSL_PROTO_TLS1_2 */
  40920. {
  40921. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  40922. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  40923. }
  40924. MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
  40925. (unsigned int) ( mbedtls_md_get_size( mbedtls_md_info_from_type( md_alg ) ) ) );
  40926. /*
  40927. * 3.3: Compute and add the signature
  40928. */
  40929. if( mbedtls_ssl_own_key( ssl ) == NULL )
  40930. {
  40931. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
  40932. return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
  40933. }
  40934. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  40935. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  40936. {
  40937. /*
  40938. * For TLS 1.2, we need to specify signature and hash algorithm
  40939. * explicitly through a prefix to the signature.
  40940. *
  40941. * struct {
  40942. * HashAlgorithm hash;
  40943. * SignatureAlgorithm signature;
  40944. * } SignatureAndHashAlgorithm;
  40945. *
  40946. * struct {
  40947. * SignatureAndHashAlgorithm algorithm;
  40948. * opaque signature<0..2^16-1>;
  40949. * } DigitallySigned;
  40950. *
  40951. */
  40952. *(p++) = mbedtls_ssl_hash_from_md_alg( md_alg );
  40953. *(p++) = mbedtls_ssl_sig_from_pk_alg( sig_alg );
  40954. n += 2;
  40955. }
  40956. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  40957. if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ), md_alg, hash, hashlen,
  40958. p + 2 , &signature_len, ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  40959. {
  40960. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
  40961. return( ret );
  40962. }
  40963. *(p++) = (unsigned char)( signature_len >> 8 );
  40964. *(p++) = (unsigned char)( signature_len );
  40965. n += 2;
  40966. MBEDTLS_SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
  40967. n += signature_len;
  40968. }
  40969. #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */
  40970. /* Done with actual work; add header and send. */
  40971. ssl->out_msglen = 4 + n;
  40972. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  40973. ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
  40974. ssl->state++;
  40975. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  40976. {
  40977. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  40978. return( ret );
  40979. }
  40980. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
  40981. return( 0 );
  40982. }
  40983. static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
  40984. {
  40985. int ret;
  40986. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
  40987. ssl->out_msglen = 4;
  40988. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  40989. ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
  40990. ssl->state++;
  40991. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  40992. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  40993. mbedtls_ssl_send_flight_completed( ssl );
  40994. #endif
  40995. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  40996. {
  40997. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  40998. return( ret );
  40999. }
  41000. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
  41001. return( 0 );
  41002. }
  41003. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  41004. defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  41005. static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
  41006. const unsigned char *end )
  41007. {
  41008. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  41009. size_t n;
  41010. /*
  41011. * Receive G^Y mod P, premaster = (G^Y)^X mod P
  41012. */
  41013. if( *p + 2 > end )
  41014. {
  41015. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
  41016. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
  41017. }
  41018. n = ( (*p)[0] << 8 ) | (*p)[1];
  41019. *p += 2;
  41020. if( *p + n > end )
  41021. {
  41022. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
  41023. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
  41024. }
  41025. if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
  41026. {
  41027. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
  41028. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
  41029. }
  41030. *p += n;
  41031. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
  41032. return( ret );
  41033. }
  41034. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  41035. MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  41036. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
  41037. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  41038. static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
  41039. const unsigned char *p,
  41040. const unsigned char *end,
  41041. size_t pms_offset )
  41042. {
  41043. int ret;
  41044. size_t len = mbedtls_pk_get_len( mbedtls_ssl_own_key( ssl ) );
  41045. unsigned char *pms = ssl->handshake->premaster + pms_offset;
  41046. unsigned char ver[2];
  41047. unsigned char fake_pms[48], peer_pms[48];
  41048. unsigned char mask;
  41049. size_t i, peer_pmslen;
  41050. unsigned int diff;
  41051. if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_RSA ) )
  41052. {
  41053. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
  41054. return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
  41055. }
  41056. /*
  41057. * Decrypt the premaster using own private RSA key
  41058. */
  41059. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  41060. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  41061. if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
  41062. {
  41063. if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
  41064. *p++ != ( ( len ) & 0xFF ) )
  41065. {
  41066. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
  41067. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
  41068. }
  41069. }
  41070. #endif
  41071. if( p + len != end )
  41072. {
  41073. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
  41074. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
  41075. }
  41076. mbedtls_ssl_write_version( ssl->handshake->max_major_ver,
  41077. ssl->handshake->max_minor_ver,
  41078. ssl->conf->transport, ver );
  41079. /*
  41080. * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
  41081. * must not cause the connection to end immediately; instead, send a
  41082. * bad_record_mac later in the handshake.
  41083. * Also, avoid data-dependant branches here to protect against
  41084. * timing-based variants.
  41085. */
  41086. ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
  41087. if( ret != 0 )
  41088. return( ret );
  41089. ret = mbedtls_pk_decrypt( mbedtls_ssl_own_key( ssl ), p, len,
  41090. peer_pms, &peer_pmslen,
  41091. sizeof( peer_pms ),
  41092. ssl->conf->f_rng, ssl->conf->p_rng );
  41093. diff = (unsigned int) ret;
  41094. diff |= peer_pmslen ^ 48;
  41095. diff |= peer_pms[0] ^ ver[0];
  41096. diff |= peer_pms[1] ^ ver[1];
  41097. #if defined(MBEDTLS_SSL_DEBUG_ALL)
  41098. if( diff != 0 )
  41099. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
  41100. #endif
  41101. if( sizeof( ssl->handshake->premaster ) < pms_offset ||
  41102. sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
  41103. {
  41104. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  41105. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  41106. }
  41107. ssl->handshake->pmslen = 48;
  41108. /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
  41109. /* MSVC has a warning about unary minus on unsigned, but this is
  41110. * well-defined and precisely what we want to do here */
  41111. #if defined(_MSC_VER)
  41112. #pragma warning( push )
  41113. #pragma warning( disable : 4146 )
  41114. #endif
  41115. mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) );
  41116. #if defined(_MSC_VER)
  41117. #pragma warning( pop )
  41118. #endif
  41119. for( i = 0; i < ssl->handshake->pmslen; i++ )
  41120. pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
  41121. return( 0 );
  41122. }
  41123. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
  41124. MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  41125. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  41126. static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
  41127. const unsigned char *end )
  41128. {
  41129. int ret = 0;
  41130. size_t n;
  41131. if( ssl->conf->f_psk == NULL &&
  41132. ( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL ||
  41133. ssl->conf->psk_identity_len == 0 || ssl->conf->psk_len == 0 ) )
  41134. {
  41135. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
  41136. return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
  41137. }
  41138. /*
  41139. * Receive client pre-shared key identity name
  41140. */
  41141. if( end - *p < 2 )
  41142. {
  41143. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
  41144. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
  41145. }
  41146. n = ( (*p)[0] << 8 ) | (*p)[1];
  41147. *p += 2;
  41148. if( n < 1 || n > 65535 || n > (size_t) ( end - *p ) )
  41149. {
  41150. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
  41151. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
  41152. }
  41153. if( ssl->conf->f_psk != NULL )
  41154. {
  41155. if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
  41156. ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
  41157. }
  41158. else
  41159. {
  41160. /* Identity is not a big secret since clients send it in the clear,
  41161. * but treat it carefully anyway, just in case */
  41162. if( n != ssl->conf->psk_identity_len ||
  41163. mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
  41164. {
  41165. ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
  41166. }
  41167. }
  41168. if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
  41169. {
  41170. MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
  41171. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  41172. MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY );
  41173. return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
  41174. }
  41175. *p += n;
  41176. return( 0 );
  41177. }
  41178. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  41179. static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
  41180. {
  41181. int ret;
  41182. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  41183. unsigned char *p, *end;
  41184. ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
  41185. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
  41186. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  41187. {
  41188. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  41189. return( ret );
  41190. }
  41191. p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  41192. end = ssl->in_msg + ssl->in_hslen;
  41193. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  41194. {
  41195. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
  41196. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
  41197. }
  41198. if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
  41199. {
  41200. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
  41201. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
  41202. }
  41203. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
  41204. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
  41205. {
  41206. if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
  41207. {
  41208. MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
  41209. return( ret );
  41210. }
  41211. if( p != end )
  41212. {
  41213. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
  41214. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
  41215. }
  41216. if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
  41217. ssl->handshake->premaster,
  41218. MBEDTLS_PREMASTER_SIZE,
  41219. &ssl->handshake->pmslen,
  41220. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  41221. {
  41222. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
  41223. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
  41224. }
  41225. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
  41226. }
  41227. else
  41228. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
  41229. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  41230. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  41231. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  41232. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  41233. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  41234. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
  41235. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
  41236. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
  41237. {
  41238. if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
  41239. p, end - p) ) != 0 )
  41240. {
  41241. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
  41242. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
  41243. }
  41244. MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
  41245. if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
  41246. &ssl->handshake->pmslen,
  41247. ssl->handshake->premaster,
  41248. MBEDTLS_MPI_MAX_SIZE,
  41249. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  41250. {
  41251. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
  41252. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
  41253. }
  41254. MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
  41255. }
  41256. else
  41257. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  41258. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  41259. MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  41260. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  41261. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  41262. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
  41263. {
  41264. if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
  41265. {
  41266. MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
  41267. return( ret );
  41268. }
  41269. if( p != end )
  41270. {
  41271. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
  41272. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
  41273. }
  41274. if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
  41275. ciphersuite_info->key_exchange ) ) != 0 )
  41276. {
  41277. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
  41278. return( ret );
  41279. }
  41280. }
  41281. else
  41282. #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
  41283. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  41284. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  41285. {
  41286. if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
  41287. {
  41288. MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
  41289. return( ret );
  41290. }
  41291. if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
  41292. {
  41293. MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
  41294. return( ret );
  41295. }
  41296. if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
  41297. ciphersuite_info->key_exchange ) ) != 0 )
  41298. {
  41299. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
  41300. return( ret );
  41301. }
  41302. }
  41303. else
  41304. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  41305. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  41306. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
  41307. {
  41308. if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
  41309. {
  41310. MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
  41311. return( ret );
  41312. }
  41313. if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
  41314. {
  41315. MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
  41316. return( ret );
  41317. }
  41318. if( p != end )
  41319. {
  41320. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
  41321. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
  41322. }
  41323. if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
  41324. ciphersuite_info->key_exchange ) ) != 0 )
  41325. {
  41326. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
  41327. return( ret );
  41328. }
  41329. }
  41330. else
  41331. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  41332. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  41333. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
  41334. {
  41335. if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
  41336. {
  41337. MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
  41338. return( ret );
  41339. }
  41340. if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
  41341. p, end - p ) ) != 0 )
  41342. {
  41343. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
  41344. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
  41345. }
  41346. MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
  41347. if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
  41348. ciphersuite_info->key_exchange ) ) != 0 )
  41349. {
  41350. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
  41351. return( ret );
  41352. }
  41353. }
  41354. else
  41355. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  41356. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  41357. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
  41358. {
  41359. if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
  41360. {
  41361. MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
  41362. return( ret );
  41363. }
  41364. }
  41365. else
  41366. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  41367. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  41368. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  41369. {
  41370. ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
  41371. p, end - p );
  41372. if( ret != 0 )
  41373. {
  41374. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
  41375. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  41376. }
  41377. ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
  41378. ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
  41379. ssl->conf->f_rng, ssl->conf->p_rng );
  41380. if( ret != 0 )
  41381. {
  41382. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
  41383. return( ret );
  41384. }
  41385. }
  41386. else
  41387. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  41388. {
  41389. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  41390. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  41391. }
  41392. if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
  41393. {
  41394. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
  41395. return( ret );
  41396. }
  41397. ssl->state++;
  41398. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
  41399. return( 0 );
  41400. }
  41401. #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
  41402. !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
  41403. !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
  41404. !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
  41405. !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
  41406. !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  41407. static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
  41408. {
  41409. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  41410. ssl->transform_negotiate->ciphersuite_info;
  41411. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
  41412. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  41413. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  41414. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  41415. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  41416. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  41417. {
  41418. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
  41419. ssl->state++;
  41420. return( 0 );
  41421. }
  41422. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  41423. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  41424. }
  41425. #else
  41426. static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
  41427. {
  41428. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  41429. size_t i, sig_len;
  41430. unsigned char hash[48];
  41431. unsigned char *hash_start = hash;
  41432. size_t hashlen;
  41433. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  41434. mbedtls_pk_type_t pk_alg;
  41435. #endif
  41436. mbedtls_md_type_t md_alg;
  41437. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  41438. ssl->transform_negotiate->ciphersuite_info;
  41439. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
  41440. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  41441. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  41442. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  41443. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  41444. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ||
  41445. ssl->session_negotiate->peer_cert == NULL )
  41446. {
  41447. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
  41448. ssl->state++;
  41449. return( 0 );
  41450. }
  41451. /* Read the message without adding it to the checksum */
  41452. do {
  41453. if( ( ret = mbedtls_ssl_read_record_layer( ssl ) ) != 0 )
  41454. {
  41455. MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
  41456. return( ret );
  41457. }
  41458. ret = mbedtls_ssl_handle_message_type( ssl );
  41459. } while( MBEDTLS_ERR_SSL_NON_FATAL == ret );
  41460. if( 0 != ret )
  41461. {
  41462. MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
  41463. return( ret );
  41464. }
  41465. ssl->state++;
  41466. /* Process the message contents */
  41467. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
  41468. ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
  41469. {
  41470. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
  41471. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
  41472. }
  41473. i = mbedtls_ssl_hs_hdr_len( ssl );
  41474. /*
  41475. * struct {
  41476. * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
  41477. * opaque signature<0..2^16-1>;
  41478. * } DigitallySigned;
  41479. */
  41480. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  41481. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  41482. if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
  41483. {
  41484. md_alg = MBEDTLS_MD_NONE;
  41485. hashlen = 36;
  41486. /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
  41487. if( mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk,
  41488. MBEDTLS_PK_ECDSA ) )
  41489. {
  41490. hash_start += 16;
  41491. hashlen -= 16;
  41492. md_alg = MBEDTLS_MD_SHA1;
  41493. }
  41494. }
  41495. else
  41496. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 ||
  41497. MBEDTLS_SSL_PROTO_TLS1_1 */
  41498. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  41499. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  41500. {
  41501. if( i + 2 > ssl->in_hslen )
  41502. {
  41503. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
  41504. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
  41505. }
  41506. /*
  41507. * Hash
  41508. */
  41509. md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] );
  41510. if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) )
  41511. {
  41512. MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
  41513. " for verify message" ) );
  41514. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
  41515. }
  41516. #if !defined(MBEDTLS_MD_SHA1)
  41517. if( MBEDTLS_MD_SHA1 == md_alg )
  41518. hash_start += 16;
  41519. #endif
  41520. /* Info from md_alg will be used instead */
  41521. hashlen = 0;
  41522. i++;
  41523. /*
  41524. * Signature
  41525. */
  41526. if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
  41527. == MBEDTLS_PK_NONE )
  41528. {
  41529. MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
  41530. " for verify message" ) );
  41531. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
  41532. }
  41533. /*
  41534. * Check the certificate's key type matches the signature alg
  41535. */
  41536. if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
  41537. {
  41538. MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
  41539. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
  41540. }
  41541. i++;
  41542. }
  41543. else
  41544. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  41545. {
  41546. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  41547. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  41548. }
  41549. if( i + 2 > ssl->in_hslen )
  41550. {
  41551. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
  41552. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
  41553. }
  41554. sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
  41555. i += 2;
  41556. if( i + sig_len != ssl->in_hslen )
  41557. {
  41558. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
  41559. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
  41560. }
  41561. /* Calculate hash and verify signature */
  41562. ssl->handshake->calc_verify( ssl, hash );
  41563. if( ( ret = mbedtls_pk_verify( &ssl->session_negotiate->peer_cert->pk,
  41564. md_alg, hash_start, hashlen,
  41565. ssl->in_msg + i, sig_len ) ) != 0 )
  41566. {
  41567. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
  41568. return( ret );
  41569. }
  41570. mbedtls_ssl_update_handshake_status( ssl );
  41571. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
  41572. return( ret );
  41573. }
  41574. #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED &&
  41575. !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED &&
  41576. !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
  41577. !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
  41578. !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
  41579. !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  41580. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  41581. static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
  41582. {
  41583. int ret;
  41584. size_t tlen;
  41585. uint32_t lifetime;
  41586. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
  41587. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  41588. ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
  41589. /*
  41590. * struct {
  41591. * uint32 ticket_lifetime_hint;
  41592. * opaque ticket<0..2^16-1>;
  41593. * } NewSessionTicket;
  41594. *
  41595. * 4 . 7 ticket_lifetime_hint (0 = unspecified)
  41596. * 8 . 9 ticket_len (n)
  41597. * 10 . 9+n ticket content
  41598. */
  41599. if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
  41600. ssl->session_negotiate,
  41601. ssl->out_msg + 10,
  41602. ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN,
  41603. &tlen, &lifetime ) ) != 0 )
  41604. {
  41605. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
  41606. tlen = 0;
  41607. }
  41608. ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
  41609. ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
  41610. ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
  41611. ssl->out_msg[7] = ( lifetime ) & 0xFF;
  41612. ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
  41613. ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
  41614. ssl->out_msglen = 10 + tlen;
  41615. /*
  41616. * Morally equivalent to updating ssl->state, but NewSessionTicket and
  41617. * ChangeCipherSpec share the same state.
  41618. */
  41619. ssl->handshake->new_session_ticket = 0;
  41620. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  41621. {
  41622. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  41623. return( ret );
  41624. }
  41625. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
  41626. return( 0 );
  41627. }
  41628. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  41629. /*
  41630. * SSL handshake -- server side -- single step
  41631. */
  41632. int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
  41633. {
  41634. int ret = 0;
  41635. if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
  41636. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  41637. MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
  41638. if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
  41639. return( ret );
  41640. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  41641. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  41642. ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
  41643. {
  41644. if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
  41645. return( ret );
  41646. }
  41647. #endif
  41648. switch( ssl->state )
  41649. {
  41650. case MBEDTLS_SSL_HELLO_REQUEST:
  41651. ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
  41652. break;
  41653. /*
  41654. * <== ClientHello
  41655. */
  41656. case MBEDTLS_SSL_CLIENT_HELLO:
  41657. ret = ssl_parse_client_hello( ssl );
  41658. break;
  41659. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  41660. case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
  41661. return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
  41662. #endif
  41663. /*
  41664. * ==> ServerHello
  41665. * Certificate
  41666. * ( ServerKeyExchange )
  41667. * ( CertificateRequest )
  41668. * ServerHelloDone
  41669. */
  41670. case MBEDTLS_SSL_SERVER_HELLO:
  41671. ret = ssl_write_server_hello( ssl );
  41672. break;
  41673. case MBEDTLS_SSL_SERVER_CERTIFICATE:
  41674. ret = mbedtls_ssl_write_certificate( ssl );
  41675. break;
  41676. case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
  41677. ret = ssl_write_server_key_exchange( ssl );
  41678. break;
  41679. case MBEDTLS_SSL_CERTIFICATE_REQUEST:
  41680. ret = ssl_write_certificate_request( ssl );
  41681. break;
  41682. case MBEDTLS_SSL_SERVER_HELLO_DONE:
  41683. ret = ssl_write_server_hello_done( ssl );
  41684. break;
  41685. /*
  41686. * <== ( Certificate/Alert )
  41687. * ClientKeyExchange
  41688. * ( CertificateVerify )
  41689. * ChangeCipherSpec
  41690. * Finished
  41691. */
  41692. case MBEDTLS_SSL_CLIENT_CERTIFICATE:
  41693. ret = mbedtls_ssl_parse_certificate( ssl );
  41694. break;
  41695. case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
  41696. ret = ssl_parse_client_key_exchange( ssl );
  41697. break;
  41698. case MBEDTLS_SSL_CERTIFICATE_VERIFY:
  41699. ret = ssl_parse_certificate_verify( ssl );
  41700. break;
  41701. case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
  41702. ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
  41703. break;
  41704. case MBEDTLS_SSL_CLIENT_FINISHED:
  41705. ret = mbedtls_ssl_parse_finished( ssl );
  41706. break;
  41707. /*
  41708. * ==> ( NewSessionTicket )
  41709. * ChangeCipherSpec
  41710. * Finished
  41711. */
  41712. case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
  41713. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  41714. if( ssl->handshake->new_session_ticket != 0 )
  41715. ret = ssl_write_new_session_ticket( ssl );
  41716. else
  41717. #endif
  41718. ret = mbedtls_ssl_write_change_cipher_spec( ssl );
  41719. break;
  41720. case MBEDTLS_SSL_SERVER_FINISHED:
  41721. ret = mbedtls_ssl_write_finished( ssl );
  41722. break;
  41723. case MBEDTLS_SSL_FLUSH_BUFFERS:
  41724. MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
  41725. ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
  41726. break;
  41727. case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
  41728. mbedtls_ssl_handshake_wrapup( ssl );
  41729. break;
  41730. default:
  41731. MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
  41732. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  41733. }
  41734. return( ret );
  41735. }
  41736. #endif /* MBEDTLS_SSL_SRV_C */
  41737. /*
  41738. Amalgamated build undefines
  41739. */
  41740. #undef ADD
  41741. #undef BC
  41742. #undef BEFORE_COLON
  41743. #undef F
  41744. #undef F0
  41745. #undef F1
  41746. #undef F2
  41747. #undef F3
  41748. #undef F4
  41749. #undef F5
  41750. #undef FSb
  41751. #undef K
  41752. #undef KK
  41753. #undef P
  41754. #undef R
  41755. #undef ROTR
  41756. #undef S
  41757. #undef S0
  41758. #undef S1
  41759. #undef S2
  41760. #undef S3
  41761. #undef SAFE_SNPRINTF
  41762. #undef SHR
  41763. #undef close
  41764. #undef read
  41765. #undef supported_init
  41766. #undef write
  41767. /********* Start of file library/ssl_ticket.c ************/
  41768. /*
  41769. * TLS server tickets callbacks implementation
  41770. *
  41771. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  41772. * SPDX-License-Identifier: Apache-2.0
  41773. *
  41774. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  41775. * not use this file except in compliance with the License.
  41776. * You may obtain a copy of the License at
  41777. *
  41778. * http://www.apache.org/licenses/LICENSE-2.0
  41779. *
  41780. * Unless required by applicable law or agreed to in writing, software
  41781. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  41782. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  41783. * See the License for the specific language governing permissions and
  41784. * limitations under the License.
  41785. *
  41786. * This file is part of mbed TLS (https://tls.mbed.org)
  41787. */
  41788. #if !defined(MBEDTLS_CONFIG_FILE)
  41789. #else
  41790. #endif
  41791. #if defined(MBEDTLS_SSL_TICKET_C)
  41792. #if defined(MBEDTLS_PLATFORM_C)
  41793. #else
  41794. #include <stdlib.h>
  41795. #define mbedtls_calloc calloc
  41796. #define mbedtls_free free
  41797. #endif
  41798. #include <string.h>
  41799. /* Implementation that should never be optimized out by the compiler */
  41800. /* zeroize was here */
  41801. /*
  41802. * Initialze context
  41803. */
  41804. void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx )
  41805. {
  41806. memset( ctx, 0, sizeof( mbedtls_ssl_ticket_context ) );
  41807. #if defined(MBEDTLS_THREADING_C)
  41808. mbedtls_mutex_init( &ctx->mutex );
  41809. #endif
  41810. }
  41811. #define MAX_KEY_BYTES 32 /* 256 bits */
  41812. /*
  41813. * Generate/update a key
  41814. */
  41815. static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx,
  41816. unsigned char index )
  41817. {
  41818. int ret;
  41819. unsigned char buf[MAX_KEY_BYTES];
  41820. mbedtls_ssl_ticket_key *key = ctx->keys + index;
  41821. #if defined(MBEDTLS_HAVE_TIME)
  41822. key->generation_time = (uint32_t) mbedtls_time( NULL );
  41823. #endif
  41824. if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 )
  41825. return( ret );
  41826. if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 )
  41827. return( ret );
  41828. /* With GCM and CCM, same context can encrypt & decrypt */
  41829. ret = mbedtls_cipher_setkey( &key->ctx, buf,
  41830. mbedtls_cipher_get_key_bitlen( &key->ctx ),
  41831. MBEDTLS_ENCRYPT );
  41832. mbedtls_zeroize( buf, sizeof( buf ) );
  41833. return( ret );
  41834. }
  41835. /*
  41836. * Rotate/generate keys if necessary
  41837. */
  41838. static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx )
  41839. {
  41840. #if !defined(MBEDTLS_HAVE_TIME)
  41841. ((void) ctx);
  41842. #else
  41843. if( ctx->ticket_lifetime != 0 )
  41844. {
  41845. uint32_t current_time = (uint32_t) mbedtls_time( NULL );
  41846. uint32_t key_time = ctx->keys[ctx->active].generation_time;
  41847. if( current_time > key_time &&
  41848. current_time - key_time < ctx->ticket_lifetime )
  41849. {
  41850. return( 0 );
  41851. }
  41852. ctx->active = 1 - ctx->active;
  41853. return( ssl_ticket_gen_key( ctx, ctx->active ) );
  41854. }
  41855. else
  41856. #endif /* MBEDTLS_HAVE_TIME */
  41857. return( 0 );
  41858. }
  41859. /*
  41860. * Setup context for actual use
  41861. */
  41862. int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx,
  41863. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  41864. mbedtls_cipher_type_t cipher,
  41865. uint32_t lifetime )
  41866. {
  41867. int ret;
  41868. const mbedtls_cipher_info_t *cipher_info;
  41869. ctx->f_rng = f_rng;
  41870. ctx->p_rng = p_rng;
  41871. ctx->ticket_lifetime = lifetime;
  41872. cipher_info = mbedtls_cipher_info_from_type( cipher);
  41873. if( cipher_info == NULL )
  41874. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  41875. if( cipher_info->mode != MBEDTLS_MODE_GCM &&
  41876. cipher_info->mode != MBEDTLS_MODE_CCM )
  41877. {
  41878. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  41879. }
  41880. if( cipher_info->key_bitlen > 8 * MAX_KEY_BYTES )
  41881. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  41882. if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 ||
  41883. ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 )
  41884. {
  41885. return( ret );
  41886. }
  41887. if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 ||
  41888. ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 )
  41889. {
  41890. return( ret );
  41891. }
  41892. return( 0 );
  41893. }
  41894. /*
  41895. * Serialize a session in the following format:
  41896. * 0 . n-1 session structure, n = sizeof(mbedtls_ssl_session)
  41897. * n . n+2 peer_cert length = m (0 if no certificate)
  41898. * n+3 . n+2+m peer cert ASN.1
  41899. */
  41900. static int ssl_save_session( const mbedtls_ssl_session *session,
  41901. unsigned char *buf, size_t buf_len,
  41902. size_t *olen )
  41903. {
  41904. unsigned char *p = buf;
  41905. size_t left = buf_len;
  41906. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  41907. size_t cert_len;
  41908. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  41909. if( left < sizeof( mbedtls_ssl_session ) )
  41910. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  41911. memcpy( p, session, sizeof( mbedtls_ssl_session ) );
  41912. p += sizeof( mbedtls_ssl_session );
  41913. left -= sizeof( mbedtls_ssl_session );
  41914. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  41915. if( session->peer_cert == NULL )
  41916. cert_len = 0;
  41917. else
  41918. cert_len = session->peer_cert->raw.len;
  41919. if( left < 3 + cert_len )
  41920. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  41921. *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
  41922. *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
  41923. *p++ = (unsigned char)( cert_len & 0xFF );
  41924. if( session->peer_cert != NULL )
  41925. memcpy( p, session->peer_cert->raw.p, cert_len );
  41926. p += cert_len;
  41927. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  41928. *olen = p - buf;
  41929. return( 0 );
  41930. }
  41931. /*
  41932. * Unserialise session, see ssl_save_session()
  41933. */
  41934. static int ssl_load_session( mbedtls_ssl_session *session,
  41935. const unsigned char *buf, size_t len )
  41936. {
  41937. const unsigned char *p = buf;
  41938. const unsigned char * const end = buf + len;
  41939. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  41940. size_t cert_len;
  41941. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  41942. if( p + sizeof( mbedtls_ssl_session ) > end )
  41943. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  41944. memcpy( session, p, sizeof( mbedtls_ssl_session ) );
  41945. p += sizeof( mbedtls_ssl_session );
  41946. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  41947. if( p + 3 > end )
  41948. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  41949. cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
  41950. p += 3;
  41951. if( cert_len == 0 )
  41952. {
  41953. session->peer_cert = NULL;
  41954. }
  41955. else
  41956. {
  41957. int ret;
  41958. if( p + cert_len > end )
  41959. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  41960. session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
  41961. if( session->peer_cert == NULL )
  41962. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  41963. mbedtls_x509_crt_init( session->peer_cert );
  41964. if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
  41965. p, cert_len ) ) != 0 )
  41966. {
  41967. mbedtls_x509_crt_free( session->peer_cert );
  41968. mbedtls_free( session->peer_cert );
  41969. session->peer_cert = NULL;
  41970. return( ret );
  41971. }
  41972. p += cert_len;
  41973. }
  41974. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  41975. if( p != end )
  41976. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  41977. return( 0 );
  41978. }
  41979. /*
  41980. * Create session ticket, with the following structure:
  41981. *
  41982. * struct {
  41983. * opaque key_name[4];
  41984. * opaque iv[12];
  41985. * opaque encrypted_state<0..2^16-1>;
  41986. * opaque tag[16];
  41987. * } ticket;
  41988. *
  41989. * The key_name, iv, and length of encrypted_state are the additional
  41990. * authenticated data.
  41991. */
  41992. int mbedtls_ssl_ticket_write( void *p_ticket,
  41993. const mbedtls_ssl_session *session,
  41994. unsigned char *start,
  41995. const unsigned char *end,
  41996. size_t *tlen,
  41997. uint32_t *ticket_lifetime )
  41998. {
  41999. int ret;
  42000. mbedtls_ssl_ticket_context *ctx = p_ticket;
  42001. mbedtls_ssl_ticket_key *key;
  42002. unsigned char *key_name = start;
  42003. unsigned char *iv = start + 4;
  42004. unsigned char *state_len_bytes = iv + 12;
  42005. unsigned char *state = state_len_bytes + 2;
  42006. unsigned char *tag;
  42007. size_t clear_len, ciph_len;
  42008. *tlen = 0;
  42009. if( ctx == NULL || ctx->f_rng == NULL )
  42010. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  42011. /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag,
  42012. * in addition to session itself, that will be checked when writing it. */
  42013. if( end - start < 4 + 12 + 2 + 16 )
  42014. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  42015. #if defined(MBEDTLS_THREADING_C)
  42016. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  42017. return( ret );
  42018. #endif
  42019. if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
  42020. goto cleanup;
  42021. key = &ctx->keys[ctx->active];
  42022. *ticket_lifetime = ctx->ticket_lifetime;
  42023. memcpy( key_name, key->name, 4 );
  42024. if( ( ret = ctx->f_rng( ctx->p_rng, iv, 12 ) ) != 0 )
  42025. goto cleanup;
  42026. /* Dump session state */
  42027. if( ( ret = ssl_save_session( session,
  42028. state, end - state, &clear_len ) ) != 0 ||
  42029. (unsigned long) clear_len > 65535 )
  42030. {
  42031. goto cleanup;
  42032. }
  42033. state_len_bytes[0] = ( clear_len >> 8 ) & 0xff;
  42034. state_len_bytes[1] = ( clear_len ) & 0xff;
  42035. /* Encrypt and authenticate */
  42036. tag = state + clear_len;
  42037. if( ( ret = mbedtls_cipher_auth_encrypt( &key->ctx,
  42038. iv, 12, key_name, 4 + 12 + 2,
  42039. state, clear_len, state, &ciph_len, tag, 16 ) ) != 0 )
  42040. {
  42041. goto cleanup;
  42042. }
  42043. if( ciph_len != clear_len )
  42044. {
  42045. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  42046. goto cleanup;
  42047. }
  42048. *tlen = 4 + 12 + 2 + 16 + ciph_len;
  42049. cleanup:
  42050. #if defined(MBEDTLS_THREADING_C)
  42051. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  42052. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  42053. #endif
  42054. return( ret );
  42055. }
  42056. /*
  42057. * Select key based on name
  42058. */
  42059. static mbedtls_ssl_ticket_key *ssl_ticket_select_key(
  42060. mbedtls_ssl_ticket_context *ctx,
  42061. const unsigned char name[4] )
  42062. {
  42063. unsigned char i;
  42064. for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ )
  42065. if( memcmp( name, ctx->keys[i].name, 4 ) == 0 )
  42066. return( &ctx->keys[i] );
  42067. return( NULL );
  42068. }
  42069. /*
  42070. * Load session ticket (see mbedtls_ssl_ticket_write for structure)
  42071. */
  42072. int mbedtls_ssl_ticket_parse( void *p_ticket,
  42073. mbedtls_ssl_session *session,
  42074. unsigned char *buf,
  42075. size_t len )
  42076. {
  42077. int ret;
  42078. mbedtls_ssl_ticket_context *ctx = p_ticket;
  42079. mbedtls_ssl_ticket_key *key;
  42080. unsigned char *key_name = buf;
  42081. unsigned char *iv = buf + 4;
  42082. unsigned char *enc_len_p = iv + 12;
  42083. unsigned char *ticket = enc_len_p + 2;
  42084. unsigned char *tag;
  42085. size_t enc_len, clear_len;
  42086. if( ctx == NULL || ctx->f_rng == NULL )
  42087. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  42088. /* See mbedtls_ssl_ticket_write() */
  42089. if( len < 4 + 12 + 2 + 16 )
  42090. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  42091. #if defined(MBEDTLS_THREADING_C)
  42092. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  42093. return( ret );
  42094. #endif
  42095. if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
  42096. goto cleanup;
  42097. enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
  42098. tag = ticket + enc_len;
  42099. if( len != 4 + 12 + 2 + enc_len + 16 )
  42100. {
  42101. ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  42102. goto cleanup;
  42103. }
  42104. /* Select key */
  42105. if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL )
  42106. {
  42107. /* We can't know for sure but this is a likely option unless we're
  42108. * under attack - this is only informative anyway */
  42109. ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
  42110. goto cleanup;
  42111. }
  42112. /* Decrypt and authenticate */
  42113. if( ( ret = mbedtls_cipher_auth_decrypt( &key->ctx, iv, 12,
  42114. key_name, 4 + 12 + 2, ticket, enc_len,
  42115. ticket, &clear_len, tag, 16 ) ) != 0 )
  42116. {
  42117. if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
  42118. ret = MBEDTLS_ERR_SSL_INVALID_MAC;
  42119. goto cleanup;
  42120. }
  42121. if( clear_len != enc_len )
  42122. {
  42123. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  42124. goto cleanup;
  42125. }
  42126. /* Actually load session */
  42127. if( ( ret = ssl_load_session( session, ticket, clear_len ) ) != 0 )
  42128. goto cleanup;
  42129. #if defined(MBEDTLS_HAVE_TIME)
  42130. {
  42131. /* Check for expiration */
  42132. mbedtls_time_t current_time = mbedtls_time( NULL );
  42133. if( current_time < session->start ||
  42134. (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime )
  42135. {
  42136. ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
  42137. goto cleanup;
  42138. }
  42139. }
  42140. #endif
  42141. cleanup:
  42142. #if defined(MBEDTLS_THREADING_C)
  42143. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  42144. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  42145. #endif
  42146. return( ret );
  42147. }
  42148. /*
  42149. * Free context
  42150. */
  42151. void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx )
  42152. {
  42153. mbedtls_cipher_free( &ctx->keys[0].ctx );
  42154. mbedtls_cipher_free( &ctx->keys[1].ctx );
  42155. #if defined(MBEDTLS_THREADING_C)
  42156. mbedtls_mutex_free( &ctx->mutex );
  42157. #endif
  42158. mbedtls_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) );
  42159. }
  42160. #endif /* MBEDTLS_SSL_TICKET_C */
  42161. /*
  42162. Amalgamated build undefines
  42163. */
  42164. #undef ADD
  42165. #undef BC
  42166. #undef BEFORE_COLON
  42167. #undef F
  42168. #undef F0
  42169. #undef F1
  42170. #undef F2
  42171. #undef F3
  42172. #undef F4
  42173. #undef F5
  42174. #undef FSb
  42175. #undef K
  42176. #undef KK
  42177. #undef P
  42178. #undef R
  42179. #undef ROTR
  42180. #undef S
  42181. #undef S0
  42182. #undef S1
  42183. #undef S2
  42184. #undef S3
  42185. #undef SAFE_SNPRINTF
  42186. #undef SHR
  42187. #undef close
  42188. #undef read
  42189. #undef supported_init
  42190. #undef write
  42191. /********* Start of file library/ssl_tls.c ************/
  42192. /*
  42193. * SSLv3/TLSv1 shared functions
  42194. *
  42195. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  42196. * SPDX-License-Identifier: Apache-2.0
  42197. *
  42198. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  42199. * not use this file except in compliance with the License.
  42200. * You may obtain a copy of the License at
  42201. *
  42202. * http://www.apache.org/licenses/LICENSE-2.0
  42203. *
  42204. * Unless required by applicable law or agreed to in writing, software
  42205. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  42206. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  42207. * See the License for the specific language governing permissions and
  42208. * limitations under the License.
  42209. *
  42210. * This file is part of mbed TLS (https://tls.mbed.org)
  42211. */
  42212. /*
  42213. * The SSL 3.0 specification was drafted by Netscape in 1996,
  42214. * and became an IETF standard in 1999.
  42215. *
  42216. * http://wp.netscape.com/eng/ssl3/
  42217. * http://www.ietf.org/rfc/rfc2246.txt
  42218. * http://www.ietf.org/rfc/rfc4346.txt
  42219. */
  42220. #if !defined(MBEDTLS_CONFIG_FILE)
  42221. #else
  42222. #endif
  42223. #if defined(MBEDTLS_SSL_TLS_C)
  42224. #if defined(MBEDTLS_PLATFORM_C)
  42225. #else
  42226. #include <stdlib.h>
  42227. #define mbedtls_calloc calloc
  42228. #define mbedtls_free free
  42229. #endif
  42230. #include <string.h>
  42231. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  42232. #endif
  42233. /* Implementation that should never be optimized out by the compiler */
  42234. /* zeroize was here */
  42235. /* Length of the "epoch" field in the record header */
  42236. static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
  42237. {
  42238. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  42239. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  42240. return( 2 );
  42241. #else
  42242. ((void) ssl);
  42243. #endif
  42244. return( 0 );
  42245. }
  42246. /*
  42247. * Start a timer.
  42248. * Passing millisecs = 0 cancels a running timer.
  42249. */
  42250. static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
  42251. {
  42252. if( ssl->f_set_timer == NULL )
  42253. return;
  42254. MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
  42255. ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
  42256. }
  42257. /*
  42258. * Return -1 is timer is expired, 0 if it isn't.
  42259. */
  42260. static int ssl_check_timer( mbedtls_ssl_context *ssl )
  42261. {
  42262. if( ssl->f_get_timer == NULL )
  42263. return( 0 );
  42264. if( ssl->f_get_timer( ssl->p_timer ) == 2 )
  42265. {
  42266. MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
  42267. return( -1 );
  42268. }
  42269. return( 0 );
  42270. }
  42271. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  42272. /*
  42273. * Double the retransmit timeout value, within the allowed range,
  42274. * returning -1 if the maximum value has already been reached.
  42275. */
  42276. static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
  42277. {
  42278. uint32_t new_timeout;
  42279. if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
  42280. return( -1 );
  42281. new_timeout = 2 * ssl->handshake->retransmit_timeout;
  42282. /* Avoid arithmetic overflow and range overflow */
  42283. if( new_timeout < ssl->handshake->retransmit_timeout ||
  42284. new_timeout > ssl->conf->hs_timeout_max )
  42285. {
  42286. new_timeout = ssl->conf->hs_timeout_max;
  42287. }
  42288. ssl->handshake->retransmit_timeout = new_timeout;
  42289. MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
  42290. ssl->handshake->retransmit_timeout ) );
  42291. return( 0 );
  42292. }
  42293. static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
  42294. {
  42295. ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
  42296. MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
  42297. ssl->handshake->retransmit_timeout ) );
  42298. }
  42299. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  42300. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  42301. /*
  42302. * Convert max_fragment_length codes to length.
  42303. * RFC 6066 says:
  42304. * enum{
  42305. * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
  42306. * } MaxFragmentLength;
  42307. * and we add 0 -> extension unused
  42308. */
  42309. static unsigned int mfl_code_to_length[MBEDTLS_SSL_MAX_FRAG_LEN_INVALID] =
  42310. {
  42311. MBEDTLS_SSL_MAX_CONTENT_LEN, /* MBEDTLS_SSL_MAX_FRAG_LEN_NONE */
  42312. 512, /* MBEDTLS_SSL_MAX_FRAG_LEN_512 */
  42313. 1024, /* MBEDTLS_SSL_MAX_FRAG_LEN_1024 */
  42314. 2048, /* MBEDTLS_SSL_MAX_FRAG_LEN_2048 */
  42315. 4096, /* MBEDTLS_SSL_MAX_FRAG_LEN_4096 */
  42316. };
  42317. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  42318. #if defined(MBEDTLS_SSL_CLI_C)
  42319. static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
  42320. {
  42321. mbedtls_ssl_session_free( dst );
  42322. memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
  42323. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  42324. if( src->peer_cert != NULL )
  42325. {
  42326. int ret;
  42327. dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
  42328. if( dst->peer_cert == NULL )
  42329. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  42330. mbedtls_x509_crt_init( dst->peer_cert );
  42331. if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
  42332. src->peer_cert->raw.len ) ) != 0 )
  42333. {
  42334. mbedtls_free( dst->peer_cert );
  42335. dst->peer_cert = NULL;
  42336. return( ret );
  42337. }
  42338. }
  42339. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  42340. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
  42341. if( src->ticket != NULL )
  42342. {
  42343. dst->ticket = mbedtls_calloc( 1, src->ticket_len );
  42344. if( dst->ticket == NULL )
  42345. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  42346. memcpy( dst->ticket, src->ticket, src->ticket_len );
  42347. }
  42348. #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
  42349. return( 0 );
  42350. }
  42351. #endif /* MBEDTLS_SSL_CLI_C */
  42352. #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
  42353. int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
  42354. const unsigned char *key_enc, const unsigned char *key_dec,
  42355. size_t keylen,
  42356. const unsigned char *iv_enc, const unsigned char *iv_dec,
  42357. size_t ivlen,
  42358. const unsigned char *mac_enc, const unsigned char *mac_dec,
  42359. size_t maclen ) = NULL;
  42360. int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
  42361. int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
  42362. int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
  42363. int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
  42364. int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
  42365. #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
  42366. /*
  42367. * Key material generation
  42368. */
  42369. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  42370. static int ssl3_prf( const unsigned char *secret, size_t slen,
  42371. const char *label,
  42372. const unsigned char *random, size_t rlen,
  42373. unsigned char *dstbuf, size_t dlen )
  42374. {
  42375. int ret = 0;
  42376. size_t i;
  42377. mbedtls_md5_context md5;
  42378. mbedtls_sha1_context sha1;
  42379. unsigned char padding[16];
  42380. unsigned char sha1sum[20];
  42381. ((void)label);
  42382. mbedtls_md5_init( &md5 );
  42383. mbedtls_sha1_init( &sha1 );
  42384. /*
  42385. * SSLv3:
  42386. * block =
  42387. * MD5( secret + SHA1( 'A' + secret + random ) ) +
  42388. * MD5( secret + SHA1( 'BB' + secret + random ) ) +
  42389. * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
  42390. * ...
  42391. */
  42392. for( i = 0; i < dlen / 16; i++ )
  42393. {
  42394. memset( padding, (unsigned char) ('A' + i), 1 + i );
  42395. if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
  42396. goto exit;
  42397. if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
  42398. goto exit;
  42399. if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
  42400. goto exit;
  42401. if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
  42402. goto exit;
  42403. if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
  42404. goto exit;
  42405. if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
  42406. goto exit;
  42407. if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
  42408. goto exit;
  42409. if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
  42410. goto exit;
  42411. if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
  42412. goto exit;
  42413. }
  42414. exit:
  42415. mbedtls_md5_free( &md5 );
  42416. mbedtls_sha1_free( &sha1 );
  42417. mbedtls_zeroize( padding, sizeof( padding ) );
  42418. mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
  42419. return( ret );
  42420. }
  42421. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  42422. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
  42423. static int tls1_prf( const unsigned char *secret, size_t slen,
  42424. const char *label,
  42425. const unsigned char *random, size_t rlen,
  42426. unsigned char *dstbuf, size_t dlen )
  42427. {
  42428. size_t nb, hs;
  42429. size_t i, j, k;
  42430. const unsigned char *S1, *S2;
  42431. unsigned char tmp[128];
  42432. unsigned char h_i[20];
  42433. const mbedtls_md_info_t *md_info;
  42434. mbedtls_md_context_t md_ctx;
  42435. int ret;
  42436. mbedtls_md_init( &md_ctx );
  42437. if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
  42438. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  42439. hs = ( slen + 1 ) / 2;
  42440. S1 = secret;
  42441. S2 = secret + slen - hs;
  42442. nb = strlen( label );
  42443. memcpy( tmp + 20, label, nb );
  42444. memcpy( tmp + 20 + nb, random, rlen );
  42445. nb += rlen;
  42446. /*
  42447. * First compute P_md5(secret,label+random)[0..dlen]
  42448. */
  42449. if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
  42450. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  42451. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
  42452. return( ret );
  42453. mbedtls_md_hmac_starts( &md_ctx, S1, hs );
  42454. mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
  42455. mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
  42456. for( i = 0; i < dlen; i += 16 )
  42457. {
  42458. mbedtls_md_hmac_reset ( &md_ctx );
  42459. mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
  42460. mbedtls_md_hmac_finish( &md_ctx, h_i );
  42461. mbedtls_md_hmac_reset ( &md_ctx );
  42462. mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
  42463. mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
  42464. k = ( i + 16 > dlen ) ? dlen % 16 : 16;
  42465. for( j = 0; j < k; j++ )
  42466. dstbuf[i + j] = h_i[j];
  42467. }
  42468. mbedtls_md_free( &md_ctx );
  42469. /*
  42470. * XOR out with P_sha1(secret,label+random)[0..dlen]
  42471. */
  42472. if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
  42473. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  42474. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
  42475. return( ret );
  42476. mbedtls_md_hmac_starts( &md_ctx, S2, hs );
  42477. mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
  42478. mbedtls_md_hmac_finish( &md_ctx, tmp );
  42479. for( i = 0; i < dlen; i += 20 )
  42480. {
  42481. mbedtls_md_hmac_reset ( &md_ctx );
  42482. mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
  42483. mbedtls_md_hmac_finish( &md_ctx, h_i );
  42484. mbedtls_md_hmac_reset ( &md_ctx );
  42485. mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
  42486. mbedtls_md_hmac_finish( &md_ctx, tmp );
  42487. k = ( i + 20 > dlen ) ? dlen % 20 : 20;
  42488. for( j = 0; j < k; j++ )
  42489. dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
  42490. }
  42491. mbedtls_md_free( &md_ctx );
  42492. mbedtls_zeroize( tmp, sizeof( tmp ) );
  42493. mbedtls_zeroize( h_i, sizeof( h_i ) );
  42494. return( 0 );
  42495. }
  42496. #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
  42497. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  42498. static int tls_prf_generic( mbedtls_md_type_t md_type,
  42499. const unsigned char *secret, size_t slen,
  42500. const char *label,
  42501. const unsigned char *random, size_t rlen,
  42502. unsigned char *dstbuf, size_t dlen )
  42503. {
  42504. size_t nb;
  42505. size_t i, j, k, md_len;
  42506. unsigned char tmp[128];
  42507. unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
  42508. const mbedtls_md_info_t *md_info;
  42509. mbedtls_md_context_t md_ctx;
  42510. int ret;
  42511. mbedtls_md_init( &md_ctx );
  42512. if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
  42513. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  42514. md_len = mbedtls_md_get_size( md_info );
  42515. if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
  42516. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  42517. nb = strlen( label );
  42518. memcpy( tmp + md_len, label, nb );
  42519. memcpy( tmp + md_len + nb, random, rlen );
  42520. nb += rlen;
  42521. /*
  42522. * Compute P_<hash>(secret, label + random)[0..dlen]
  42523. */
  42524. if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
  42525. return( ret );
  42526. mbedtls_md_hmac_starts( &md_ctx, secret, slen );
  42527. mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
  42528. mbedtls_md_hmac_finish( &md_ctx, tmp );
  42529. for( i = 0; i < dlen; i += md_len )
  42530. {
  42531. mbedtls_md_hmac_reset ( &md_ctx );
  42532. mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
  42533. mbedtls_md_hmac_finish( &md_ctx, h_i );
  42534. mbedtls_md_hmac_reset ( &md_ctx );
  42535. mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
  42536. mbedtls_md_hmac_finish( &md_ctx, tmp );
  42537. k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
  42538. for( j = 0; j < k; j++ )
  42539. dstbuf[i + j] = h_i[j];
  42540. }
  42541. mbedtls_md_free( &md_ctx );
  42542. mbedtls_zeroize( tmp, sizeof( tmp ) );
  42543. mbedtls_zeroize( h_i, sizeof( h_i ) );
  42544. return( 0 );
  42545. }
  42546. #if defined(MBEDTLS_SHA256_C)
  42547. static int tls_prf_sha256( const unsigned char *secret, size_t slen,
  42548. const char *label,
  42549. const unsigned char *random, size_t rlen,
  42550. unsigned char *dstbuf, size_t dlen )
  42551. {
  42552. return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
  42553. label, random, rlen, dstbuf, dlen ) );
  42554. }
  42555. #endif /* MBEDTLS_SHA256_C */
  42556. #if defined(MBEDTLS_SHA512_C)
  42557. static int tls_prf_sha384( const unsigned char *secret, size_t slen,
  42558. const char *label,
  42559. const unsigned char *random, size_t rlen,
  42560. unsigned char *dstbuf, size_t dlen )
  42561. {
  42562. return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
  42563. label, random, rlen, dstbuf, dlen ) );
  42564. }
  42565. #endif /* MBEDTLS_SHA512_C */
  42566. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  42567. static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
  42568. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  42569. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  42570. static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
  42571. #endif
  42572. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  42573. static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
  42574. static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
  42575. #endif
  42576. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
  42577. static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
  42578. static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
  42579. #endif
  42580. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  42581. #if defined(MBEDTLS_SHA256_C)
  42582. static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
  42583. static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
  42584. static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
  42585. #endif
  42586. #if defined(MBEDTLS_SHA512_C)
  42587. static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
  42588. static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
  42589. static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
  42590. #endif
  42591. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  42592. int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
  42593. {
  42594. int ret = 0;
  42595. unsigned char tmp[64];
  42596. unsigned char keyblk[256];
  42597. unsigned char *key1;
  42598. unsigned char *key2;
  42599. unsigned char *mac_enc;
  42600. unsigned char *mac_dec;
  42601. size_t mac_key_len;
  42602. size_t iv_copy_len;
  42603. const mbedtls_cipher_info_t *cipher_info;
  42604. const mbedtls_md_info_t *md_info;
  42605. mbedtls_ssl_session *session = ssl->session_negotiate;
  42606. mbedtls_ssl_transform *transform = ssl->transform_negotiate;
  42607. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  42608. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
  42609. cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
  42610. if( cipher_info == NULL )
  42611. {
  42612. MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
  42613. transform->ciphersuite_info->cipher ) );
  42614. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  42615. }
  42616. md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
  42617. if( md_info == NULL )
  42618. {
  42619. MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
  42620. transform->ciphersuite_info->mac ) );
  42621. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  42622. }
  42623. /*
  42624. * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
  42625. */
  42626. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  42627. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  42628. {
  42629. handshake->tls_prf = ssl3_prf;
  42630. handshake->calc_verify = ssl_calc_verify_ssl;
  42631. handshake->calc_finished = ssl_calc_finished_ssl;
  42632. }
  42633. else
  42634. #endif
  42635. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
  42636. if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
  42637. {
  42638. handshake->tls_prf = tls1_prf;
  42639. handshake->calc_verify = ssl_calc_verify_tls;
  42640. handshake->calc_finished = ssl_calc_finished_tls;
  42641. }
  42642. else
  42643. #endif
  42644. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  42645. #if defined(MBEDTLS_SHA512_C)
  42646. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
  42647. transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
  42648. {
  42649. handshake->tls_prf = tls_prf_sha384;
  42650. handshake->calc_verify = ssl_calc_verify_tls_sha384;
  42651. handshake->calc_finished = ssl_calc_finished_tls_sha384;
  42652. }
  42653. else
  42654. #endif
  42655. #if defined(MBEDTLS_SHA256_C)
  42656. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  42657. {
  42658. handshake->tls_prf = tls_prf_sha256;
  42659. handshake->calc_verify = ssl_calc_verify_tls_sha256;
  42660. handshake->calc_finished = ssl_calc_finished_tls_sha256;
  42661. }
  42662. else
  42663. #endif
  42664. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  42665. {
  42666. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  42667. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  42668. }
  42669. /*
  42670. * SSLv3:
  42671. * master =
  42672. * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
  42673. * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
  42674. * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
  42675. *
  42676. * TLSv1+:
  42677. * master = PRF( premaster, "master secret", randbytes )[0..47]
  42678. */
  42679. if( handshake->resume == 0 )
  42680. {
  42681. MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
  42682. handshake->pmslen );
  42683. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  42684. if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
  42685. {
  42686. unsigned char session_hash[48];
  42687. size_t hash_len;
  42688. MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
  42689. ssl->handshake->calc_verify( ssl, session_hash );
  42690. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  42691. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  42692. {
  42693. #if defined(MBEDTLS_SHA512_C)
  42694. if( ssl->transform_negotiate->ciphersuite_info->mac ==
  42695. MBEDTLS_MD_SHA384 )
  42696. {
  42697. hash_len = 48;
  42698. }
  42699. else
  42700. #endif
  42701. hash_len = 32;
  42702. }
  42703. else
  42704. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  42705. hash_len = 36;
  42706. MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
  42707. ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
  42708. "extended master secret",
  42709. session_hash, hash_len,
  42710. session->master, 48 );
  42711. if( ret != 0 )
  42712. {
  42713. MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
  42714. return( ret );
  42715. }
  42716. }
  42717. else
  42718. #endif
  42719. ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
  42720. "master secret",
  42721. handshake->randbytes, 64,
  42722. session->master, 48 );
  42723. if( ret != 0 )
  42724. {
  42725. MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
  42726. return( ret );
  42727. }
  42728. mbedtls_zeroize( handshake->premaster, sizeof(handshake->premaster) );
  42729. }
  42730. else
  42731. MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
  42732. /*
  42733. * Swap the client and server random values.
  42734. */
  42735. memcpy( tmp, handshake->randbytes, 64 );
  42736. memcpy( handshake->randbytes, tmp + 32, 32 );
  42737. memcpy( handshake->randbytes + 32, tmp, 32 );
  42738. mbedtls_zeroize( tmp, sizeof( tmp ) );
  42739. /*
  42740. * SSLv3:
  42741. * key block =
  42742. * MD5( master + SHA1( 'A' + master + randbytes ) ) +
  42743. * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
  42744. * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
  42745. * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
  42746. * ...
  42747. *
  42748. * TLSv1:
  42749. * key block = PRF( master, "key expansion", randbytes )
  42750. */
  42751. ret = handshake->tls_prf( session->master, 48, "key expansion",
  42752. handshake->randbytes, 64, keyblk, 256 );
  42753. if( ret != 0 )
  42754. {
  42755. MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
  42756. return( ret );
  42757. }
  42758. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
  42759. mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
  42760. MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
  42761. MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
  42762. MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
  42763. mbedtls_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
  42764. /*
  42765. * Determine the appropriate key, IV and MAC length.
  42766. */
  42767. transform->keylen = cipher_info->key_bitlen / 8;
  42768. if( cipher_info->mode == MBEDTLS_MODE_GCM ||
  42769. cipher_info->mode == MBEDTLS_MODE_CCM )
  42770. {
  42771. transform->maclen = 0;
  42772. mac_key_len = 0;
  42773. transform->ivlen = 12;
  42774. transform->fixed_ivlen = 4;
  42775. /* Minimum length is expicit IV + tag */
  42776. transform->minlen = transform->ivlen - transform->fixed_ivlen
  42777. + ( transform->ciphersuite_info->flags &
  42778. MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
  42779. }
  42780. else
  42781. {
  42782. /* Initialize HMAC contexts */
  42783. if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
  42784. ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
  42785. {
  42786. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
  42787. return( ret );
  42788. }
  42789. /* Get MAC length */
  42790. mac_key_len = mbedtls_md_get_size( md_info );
  42791. transform->maclen = mac_key_len;
  42792. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  42793. /*
  42794. * If HMAC is to be truncated, we shall keep the leftmost bytes,
  42795. * (rfc 6066 page 13 or rfc 2104 section 4),
  42796. * so we only need to adjust the length here.
  42797. */
  42798. if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
  42799. {
  42800. transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
  42801. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
  42802. /* Fall back to old, non-compliant version of the truncated
  42803. * HMAC implementation which also truncates the key
  42804. * (Mbed TLS versions from 1.3 to 2.6.0) */
  42805. mac_key_len = transform->maclen;
  42806. #endif
  42807. }
  42808. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  42809. /* IV length */
  42810. transform->ivlen = cipher_info->iv_size;
  42811. /* Minimum length */
  42812. if( cipher_info->mode == MBEDTLS_MODE_STREAM )
  42813. transform->minlen = transform->maclen;
  42814. else
  42815. {
  42816. /*
  42817. * GenericBlockCipher:
  42818. * 1. if EtM is in use: one block plus MAC
  42819. * otherwise: * first multiple of blocklen greater than maclen
  42820. * 2. IV except for SSL3 and TLS 1.0
  42821. */
  42822. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  42823. if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
  42824. {
  42825. transform->minlen = transform->maclen
  42826. + cipher_info->block_size;
  42827. }
  42828. else
  42829. #endif
  42830. {
  42831. transform->minlen = transform->maclen
  42832. + cipher_info->block_size
  42833. - transform->maclen % cipher_info->block_size;
  42834. }
  42835. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
  42836. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
  42837. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
  42838. ; /* No need to adjust minlen */
  42839. else
  42840. #endif
  42841. #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
  42842. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
  42843. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  42844. {
  42845. transform->minlen += transform->ivlen;
  42846. }
  42847. else
  42848. #endif
  42849. {
  42850. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  42851. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  42852. }
  42853. }
  42854. }
  42855. MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
  42856. transform->keylen, transform->minlen, transform->ivlen,
  42857. transform->maclen ) );
  42858. /*
  42859. * Finally setup the cipher contexts, IVs and MAC secrets.
  42860. */
  42861. #if defined(MBEDTLS_SSL_CLI_C)
  42862. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
  42863. {
  42864. key1 = keyblk + mac_key_len * 2;
  42865. key2 = keyblk + mac_key_len * 2 + transform->keylen;
  42866. mac_enc = keyblk;
  42867. mac_dec = keyblk + mac_key_len;
  42868. /*
  42869. * This is not used in TLS v1.1.
  42870. */
  42871. iv_copy_len = ( transform->fixed_ivlen ) ?
  42872. transform->fixed_ivlen : transform->ivlen;
  42873. memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
  42874. memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
  42875. iv_copy_len );
  42876. }
  42877. else
  42878. #endif /* MBEDTLS_SSL_CLI_C */
  42879. #if defined(MBEDTLS_SSL_SRV_C)
  42880. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
  42881. {
  42882. key1 = keyblk + mac_key_len * 2 + transform->keylen;
  42883. key2 = keyblk + mac_key_len * 2;
  42884. mac_enc = keyblk + mac_key_len;
  42885. mac_dec = keyblk;
  42886. /*
  42887. * This is not used in TLS v1.1.
  42888. */
  42889. iv_copy_len = ( transform->fixed_ivlen ) ?
  42890. transform->fixed_ivlen : transform->ivlen;
  42891. memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
  42892. memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
  42893. iv_copy_len );
  42894. }
  42895. else
  42896. #endif /* MBEDTLS_SSL_SRV_C */
  42897. {
  42898. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  42899. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  42900. }
  42901. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  42902. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  42903. {
  42904. if( mac_key_len > sizeof transform->mac_enc )
  42905. {
  42906. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  42907. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  42908. }
  42909. memcpy( transform->mac_enc, mac_enc, mac_key_len );
  42910. memcpy( transform->mac_dec, mac_dec, mac_key_len );
  42911. }
  42912. else
  42913. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  42914. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  42915. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  42916. if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
  42917. {
  42918. mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
  42919. mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
  42920. }
  42921. else
  42922. #endif
  42923. {
  42924. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  42925. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  42926. }
  42927. #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
  42928. if( mbedtls_ssl_hw_record_init != NULL )
  42929. {
  42930. int ret = 0;
  42931. MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
  42932. if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
  42933. transform->iv_enc, transform->iv_dec,
  42934. iv_copy_len,
  42935. mac_enc, mac_dec,
  42936. mac_key_len ) ) != 0 )
  42937. {
  42938. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
  42939. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  42940. }
  42941. }
  42942. #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
  42943. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  42944. if( ssl->conf->f_export_keys != NULL )
  42945. {
  42946. ssl->conf->f_export_keys( ssl->conf->p_export_keys,
  42947. session->master, keyblk,
  42948. mac_key_len, transform->keylen,
  42949. iv_copy_len );
  42950. }
  42951. #endif
  42952. if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
  42953. cipher_info ) ) != 0 )
  42954. {
  42955. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
  42956. return( ret );
  42957. }
  42958. if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
  42959. cipher_info ) ) != 0 )
  42960. {
  42961. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
  42962. return( ret );
  42963. }
  42964. if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
  42965. cipher_info->key_bitlen,
  42966. MBEDTLS_ENCRYPT ) ) != 0 )
  42967. {
  42968. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
  42969. return( ret );
  42970. }
  42971. if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
  42972. cipher_info->key_bitlen,
  42973. MBEDTLS_DECRYPT ) ) != 0 )
  42974. {
  42975. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
  42976. return( ret );
  42977. }
  42978. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  42979. if( cipher_info->mode == MBEDTLS_MODE_CBC )
  42980. {
  42981. if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
  42982. MBEDTLS_PADDING_NONE ) ) != 0 )
  42983. {
  42984. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
  42985. return( ret );
  42986. }
  42987. if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
  42988. MBEDTLS_PADDING_NONE ) ) != 0 )
  42989. {
  42990. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
  42991. return( ret );
  42992. }
  42993. }
  42994. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  42995. mbedtls_zeroize( keyblk, sizeof( keyblk ) );
  42996. #if defined(MBEDTLS_ZLIB_SUPPORT)
  42997. // Initialize compression
  42998. //
  42999. if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
  43000. {
  43001. if( ssl->compress_buf == NULL )
  43002. {
  43003. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
  43004. ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_BUFFER_LEN );
  43005. if( ssl->compress_buf == NULL )
  43006. {
  43007. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
  43008. MBEDTLS_SSL_BUFFER_LEN ) );
  43009. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  43010. }
  43011. }
  43012. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
  43013. memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
  43014. memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
  43015. if( deflateInit( &transform->ctx_deflate,
  43016. Z_DEFAULT_COMPRESSION ) != Z_OK ||
  43017. inflateInit( &transform->ctx_inflate ) != Z_OK )
  43018. {
  43019. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
  43020. return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
  43021. }
  43022. }
  43023. #endif /* MBEDTLS_ZLIB_SUPPORT */
  43024. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
  43025. return( 0 );
  43026. }
  43027. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  43028. void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
  43029. {
  43030. mbedtls_md5_context md5;
  43031. mbedtls_sha1_context sha1;
  43032. unsigned char pad_1[48];
  43033. unsigned char pad_2[48];
  43034. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
  43035. mbedtls_md5_init( &md5 );
  43036. mbedtls_sha1_init( &sha1 );
  43037. mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
  43038. mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
  43039. memset( pad_1, 0x36, 48 );
  43040. memset( pad_2, 0x5C, 48 );
  43041. mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
  43042. mbedtls_md5_update_ret( &md5, pad_1, 48 );
  43043. mbedtls_md5_finish_ret( &md5, hash );
  43044. mbedtls_md5_starts_ret( &md5 );
  43045. mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
  43046. mbedtls_md5_update_ret( &md5, pad_2, 48 );
  43047. mbedtls_md5_update_ret( &md5, hash, 16 );
  43048. mbedtls_md5_finish_ret( &md5, hash );
  43049. mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
  43050. mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
  43051. mbedtls_sha1_finish_ret( &sha1, hash + 16 );
  43052. mbedtls_sha1_starts_ret( &sha1 );
  43053. mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
  43054. mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
  43055. mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
  43056. mbedtls_sha1_finish_ret( &sha1, hash + 16 );
  43057. MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
  43058. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
  43059. mbedtls_md5_free( &md5 );
  43060. mbedtls_sha1_free( &sha1 );
  43061. return;
  43062. }
  43063. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  43064. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
  43065. void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
  43066. {
  43067. mbedtls_md5_context md5;
  43068. mbedtls_sha1_context sha1;
  43069. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
  43070. mbedtls_md5_init( &md5 );
  43071. mbedtls_sha1_init( &sha1 );
  43072. mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
  43073. mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
  43074. mbedtls_md5_finish_ret( &md5, hash );
  43075. mbedtls_sha1_finish_ret( &sha1, hash + 16 );
  43076. MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
  43077. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
  43078. mbedtls_md5_free( &md5 );
  43079. mbedtls_sha1_free( &sha1 );
  43080. return;
  43081. }
  43082. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
  43083. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  43084. #if defined(MBEDTLS_SHA256_C)
  43085. void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
  43086. {
  43087. mbedtls_sha256_context sha256;
  43088. mbedtls_sha256_init( &sha256 );
  43089. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
  43090. mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
  43091. mbedtls_sha256_finish_ret( &sha256, hash );
  43092. MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
  43093. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
  43094. mbedtls_sha256_free( &sha256 );
  43095. return;
  43096. }
  43097. #endif /* MBEDTLS_SHA256_C */
  43098. #if defined(MBEDTLS_SHA512_C)
  43099. void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
  43100. {
  43101. mbedtls_sha512_context sha512;
  43102. mbedtls_sha512_init( &sha512 );
  43103. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
  43104. mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
  43105. mbedtls_sha512_finish_ret( &sha512, hash );
  43106. MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
  43107. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
  43108. mbedtls_sha512_free( &sha512 );
  43109. return;
  43110. }
  43111. #endif /* MBEDTLS_SHA512_C */
  43112. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  43113. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  43114. int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
  43115. {
  43116. unsigned char *p = ssl->handshake->premaster;
  43117. unsigned char *end = p + sizeof( ssl->handshake->premaster );
  43118. const unsigned char *psk = ssl->conf->psk;
  43119. size_t psk_len = ssl->conf->psk_len;
  43120. /* If the psk callback was called, use its result */
  43121. if( ssl->handshake->psk != NULL )
  43122. {
  43123. psk = ssl->handshake->psk;
  43124. psk_len = ssl->handshake->psk_len;
  43125. }
  43126. /*
  43127. * PMS = struct {
  43128. * opaque other_secret<0..2^16-1>;
  43129. * opaque psk<0..2^16-1>;
  43130. * };
  43131. * with "other_secret" depending on the particular key exchange
  43132. */
  43133. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  43134. if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
  43135. {
  43136. if( end - p < 2 )
  43137. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  43138. *(p++) = (unsigned char)( psk_len >> 8 );
  43139. *(p++) = (unsigned char)( psk_len );
  43140. if( end < p || (size_t)( end - p ) < psk_len )
  43141. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  43142. memset( p, 0, psk_len );
  43143. p += psk_len;
  43144. }
  43145. else
  43146. #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
  43147. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  43148. if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  43149. {
  43150. /*
  43151. * other_secret already set by the ClientKeyExchange message,
  43152. * and is 48 bytes long
  43153. */
  43154. *p++ = 0;
  43155. *p++ = 48;
  43156. p += 48;
  43157. }
  43158. else
  43159. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  43160. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  43161. if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
  43162. {
  43163. int ret;
  43164. size_t len;
  43165. /* Write length only when we know the actual value */
  43166. if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
  43167. p + 2, end - ( p + 2 ), &len,
  43168. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  43169. {
  43170. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
  43171. return( ret );
  43172. }
  43173. *(p++) = (unsigned char)( len >> 8 );
  43174. *(p++) = (unsigned char)( len );
  43175. p += len;
  43176. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
  43177. }
  43178. else
  43179. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  43180. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  43181. if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
  43182. {
  43183. int ret;
  43184. size_t zlen;
  43185. if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
  43186. p + 2, end - ( p + 2 ),
  43187. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  43188. {
  43189. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
  43190. return( ret );
  43191. }
  43192. *(p++) = (unsigned char)( zlen >> 8 );
  43193. *(p++) = (unsigned char)( zlen );
  43194. p += zlen;
  43195. MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
  43196. }
  43197. else
  43198. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  43199. {
  43200. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  43201. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  43202. }
  43203. /* opaque psk<0..2^16-1>; */
  43204. if( end - p < 2 )
  43205. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  43206. *(p++) = (unsigned char)( psk_len >> 8 );
  43207. *(p++) = (unsigned char)( psk_len );
  43208. if( end < p || (size_t)( end - p ) < psk_len )
  43209. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  43210. memcpy( p, psk, psk_len );
  43211. p += psk_len;
  43212. ssl->handshake->pmslen = p - ssl->handshake->premaster;
  43213. return( 0 );
  43214. }
  43215. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  43216. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  43217. /*
  43218. * SSLv3.0 MAC functions
  43219. */
  43220. #define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
  43221. static void ssl_mac( mbedtls_md_context_t *md_ctx,
  43222. const unsigned char *secret,
  43223. const unsigned char *buf, size_t len,
  43224. const unsigned char *ctr, int type,
  43225. unsigned char out[SSL_MAC_MAX_BYTES] )
  43226. {
  43227. unsigned char header[11];
  43228. unsigned char padding[48];
  43229. int padlen;
  43230. int md_size = mbedtls_md_get_size( md_ctx->md_info );
  43231. int md_type = mbedtls_md_get_type( md_ctx->md_info );
  43232. /* Only MD5 and SHA-1 supported */
  43233. if( md_type == MBEDTLS_MD_MD5 )
  43234. padlen = 48;
  43235. else
  43236. padlen = 40;
  43237. memcpy( header, ctr, 8 );
  43238. header[ 8] = (unsigned char) type;
  43239. header[ 9] = (unsigned char)( len >> 8 );
  43240. header[10] = (unsigned char)( len );
  43241. memset( padding, 0x36, padlen );
  43242. mbedtls_md_starts( md_ctx );
  43243. mbedtls_md_update( md_ctx, secret, md_size );
  43244. mbedtls_md_update( md_ctx, padding, padlen );
  43245. mbedtls_md_update( md_ctx, header, 11 );
  43246. mbedtls_md_update( md_ctx, buf, len );
  43247. mbedtls_md_finish( md_ctx, out );
  43248. memset( padding, 0x5C, padlen );
  43249. mbedtls_md_starts( md_ctx );
  43250. mbedtls_md_update( md_ctx, secret, md_size );
  43251. mbedtls_md_update( md_ctx, padding, padlen );
  43252. mbedtls_md_update( md_ctx, out, md_size );
  43253. mbedtls_md_finish( md_ctx, out );
  43254. }
  43255. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  43256. #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
  43257. ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
  43258. ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) ) )
  43259. #define SSL_SOME_MODES_USE_MAC
  43260. #endif
  43261. /*
  43262. * Encryption/decryption functions
  43263. */
  43264. static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
  43265. {
  43266. mbedtls_cipher_mode_t mode;
  43267. int auth_done = 0;
  43268. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
  43269. if( ssl->session_out == NULL || ssl->transform_out == NULL )
  43270. {
  43271. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  43272. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  43273. }
  43274. mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
  43275. MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
  43276. ssl->out_msg, ssl->out_msglen );
  43277. if( ssl->out_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
  43278. {
  43279. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
  43280. (unsigned) ssl->out_msglen,
  43281. MBEDTLS_SSL_MAX_CONTENT_LEN ) );
  43282. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  43283. }
  43284. /*
  43285. * Add MAC before if needed
  43286. */
  43287. #if defined(SSL_SOME_MODES_USE_MAC)
  43288. if( mode == MBEDTLS_MODE_STREAM ||
  43289. ( mode == MBEDTLS_MODE_CBC
  43290. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  43291. && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
  43292. #endif
  43293. ) )
  43294. {
  43295. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  43296. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  43297. {
  43298. unsigned char mac[SSL_MAC_MAX_BYTES];
  43299. ssl_mac( &ssl->transform_out->md_ctx_enc,
  43300. ssl->transform_out->mac_enc,
  43301. ssl->out_msg, ssl->out_msglen,
  43302. ssl->out_ctr, ssl->out_msgtype,
  43303. mac );
  43304. memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
  43305. }
  43306. else
  43307. #endif
  43308. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  43309. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  43310. if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
  43311. {
  43312. unsigned char mac[MBEDTLS_SSL_MAC_ADD];
  43313. mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
  43314. mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
  43315. mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
  43316. mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
  43317. ssl->out_msg, ssl->out_msglen );
  43318. mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
  43319. mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
  43320. memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
  43321. }
  43322. else
  43323. #endif
  43324. {
  43325. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  43326. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  43327. }
  43328. MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
  43329. ssl->out_msg + ssl->out_msglen,
  43330. ssl->transform_out->maclen );
  43331. ssl->out_msglen += ssl->transform_out->maclen;
  43332. auth_done++;
  43333. }
  43334. #endif /* AEAD not the only option */
  43335. /*
  43336. * Encrypt
  43337. */
  43338. #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
  43339. if( mode == MBEDTLS_MODE_STREAM )
  43340. {
  43341. int ret;
  43342. size_t olen = 0;
  43343. MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
  43344. "including %d bytes of padding",
  43345. ssl->out_msglen, 0 ) );
  43346. if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
  43347. ssl->transform_out->iv_enc,
  43348. ssl->transform_out->ivlen,
  43349. ssl->out_msg, ssl->out_msglen,
  43350. ssl->out_msg, &olen ) ) != 0 )
  43351. {
  43352. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
  43353. return( ret );
  43354. }
  43355. if( ssl->out_msglen != olen )
  43356. {
  43357. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  43358. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  43359. }
  43360. }
  43361. else
  43362. #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
  43363. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
  43364. if( mode == MBEDTLS_MODE_GCM ||
  43365. mode == MBEDTLS_MODE_CCM )
  43366. {
  43367. int ret;
  43368. size_t enc_msglen, olen;
  43369. unsigned char *enc_msg;
  43370. unsigned char add_data[13];
  43371. unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
  43372. MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
  43373. memcpy( add_data, ssl->out_ctr, 8 );
  43374. add_data[8] = ssl->out_msgtype;
  43375. mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
  43376. ssl->conf->transport, add_data + 9 );
  43377. add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
  43378. add_data[12] = ssl->out_msglen & 0xFF;
  43379. MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
  43380. add_data, 13 );
  43381. /*
  43382. * Generate IV
  43383. */
  43384. if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
  43385. {
  43386. /* Reminder if we ever add an AEAD mode with a different size */
  43387. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  43388. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  43389. }
  43390. memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
  43391. ssl->out_ctr, 8 );
  43392. memcpy( ssl->out_iv, ssl->out_ctr, 8 );
  43393. MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
  43394. ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
  43395. /*
  43396. * Fix pointer positions and message length with added IV
  43397. */
  43398. enc_msg = ssl->out_msg;
  43399. enc_msglen = ssl->out_msglen;
  43400. ssl->out_msglen += ssl->transform_out->ivlen -
  43401. ssl->transform_out->fixed_ivlen;
  43402. MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
  43403. "including %d bytes of padding",
  43404. ssl->out_msglen, 0 ) );
  43405. /*
  43406. * Encrypt and authenticate
  43407. */
  43408. if( ( ret = mbedtls_cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
  43409. ssl->transform_out->iv_enc,
  43410. ssl->transform_out->ivlen,
  43411. add_data, 13,
  43412. enc_msg, enc_msglen,
  43413. enc_msg, &olen,
  43414. enc_msg + enc_msglen, taglen ) ) != 0 )
  43415. {
  43416. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
  43417. return( ret );
  43418. }
  43419. if( olen != enc_msglen )
  43420. {
  43421. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  43422. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  43423. }
  43424. ssl->out_msglen += taglen;
  43425. auth_done++;
  43426. MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
  43427. }
  43428. else
  43429. #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
  43430. #if defined(MBEDTLS_CIPHER_MODE_CBC) && \
  43431. ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) )
  43432. if( mode == MBEDTLS_MODE_CBC )
  43433. {
  43434. int ret;
  43435. unsigned char *enc_msg;
  43436. size_t enc_msglen, padlen, olen = 0, i;
  43437. padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
  43438. ssl->transform_out->ivlen;
  43439. if( padlen == ssl->transform_out->ivlen )
  43440. padlen = 0;
  43441. for( i = 0; i <= padlen; i++ )
  43442. ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
  43443. ssl->out_msglen += padlen + 1;
  43444. enc_msglen = ssl->out_msglen;
  43445. enc_msg = ssl->out_msg;
  43446. #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
  43447. /*
  43448. * Prepend per-record IV for block cipher in TLS v1.1 and up as per
  43449. * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
  43450. */
  43451. if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
  43452. {
  43453. /*
  43454. * Generate IV
  43455. */
  43456. ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
  43457. ssl->transform_out->ivlen );
  43458. if( ret != 0 )
  43459. return( ret );
  43460. memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
  43461. ssl->transform_out->ivlen );
  43462. /*
  43463. * Fix pointer positions and message length with added IV
  43464. */
  43465. enc_msg = ssl->out_msg;
  43466. enc_msglen = ssl->out_msglen;
  43467. ssl->out_msglen += ssl->transform_out->ivlen;
  43468. }
  43469. #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
  43470. MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
  43471. "including %d bytes of IV and %d bytes of padding",
  43472. ssl->out_msglen, ssl->transform_out->ivlen,
  43473. padlen + 1 ) );
  43474. if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
  43475. ssl->transform_out->iv_enc,
  43476. ssl->transform_out->ivlen,
  43477. enc_msg, enc_msglen,
  43478. enc_msg, &olen ) ) != 0 )
  43479. {
  43480. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
  43481. return( ret );
  43482. }
  43483. if( enc_msglen != olen )
  43484. {
  43485. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  43486. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  43487. }
  43488. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
  43489. if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
  43490. {
  43491. /*
  43492. * Save IV in SSL3 and TLS1
  43493. */
  43494. memcpy( ssl->transform_out->iv_enc,
  43495. ssl->transform_out->cipher_ctx_enc.iv,
  43496. ssl->transform_out->ivlen );
  43497. }
  43498. #endif
  43499. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  43500. if( auth_done == 0 )
  43501. {
  43502. /*
  43503. * MAC(MAC_write_key, seq_num +
  43504. * TLSCipherText.type +
  43505. * TLSCipherText.version +
  43506. * length_of( (IV +) ENC(...) ) +
  43507. * IV + // except for TLS 1.0
  43508. * ENC(content + padding + padding_length));
  43509. */
  43510. unsigned char pseudo_hdr[13];
  43511. MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
  43512. memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
  43513. memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
  43514. pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
  43515. pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
  43516. MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
  43517. mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
  43518. mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
  43519. ssl->out_iv, ssl->out_msglen );
  43520. mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
  43521. ssl->out_iv + ssl->out_msglen );
  43522. mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
  43523. ssl->out_msglen += ssl->transform_out->maclen;
  43524. auth_done++;
  43525. }
  43526. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  43527. }
  43528. else
  43529. #endif /* MBEDTLS_CIPHER_MODE_CBC &&
  43530. ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C ) */
  43531. {
  43532. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  43533. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  43534. }
  43535. /* Make extra sure authentication was performed, exactly once */
  43536. if( auth_done != 1 )
  43537. {
  43538. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  43539. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  43540. }
  43541. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
  43542. return( 0 );
  43543. }
  43544. static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
  43545. {
  43546. size_t i;
  43547. mbedtls_cipher_mode_t mode;
  43548. int auth_done = 0;
  43549. #if defined(SSL_SOME_MODES_USE_MAC)
  43550. size_t padlen = 0, correct = 1;
  43551. #endif
  43552. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
  43553. if( ssl->session_in == NULL || ssl->transform_in == NULL )
  43554. {
  43555. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  43556. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  43557. }
  43558. mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
  43559. if( ssl->in_msglen < ssl->transform_in->minlen )
  43560. {
  43561. MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
  43562. ssl->in_msglen, ssl->transform_in->minlen ) );
  43563. return( MBEDTLS_ERR_SSL_INVALID_MAC );
  43564. }
  43565. #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
  43566. if( mode == MBEDTLS_MODE_STREAM )
  43567. {
  43568. int ret;
  43569. size_t olen = 0;
  43570. padlen = 0;
  43571. if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
  43572. ssl->transform_in->iv_dec,
  43573. ssl->transform_in->ivlen,
  43574. ssl->in_msg, ssl->in_msglen,
  43575. ssl->in_msg, &olen ) ) != 0 )
  43576. {
  43577. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
  43578. return( ret );
  43579. }
  43580. if( ssl->in_msglen != olen )
  43581. {
  43582. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  43583. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  43584. }
  43585. }
  43586. else
  43587. #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
  43588. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
  43589. if( mode == MBEDTLS_MODE_GCM ||
  43590. mode == MBEDTLS_MODE_CCM )
  43591. {
  43592. int ret;
  43593. size_t dec_msglen, olen;
  43594. unsigned char *dec_msg;
  43595. unsigned char *dec_msg_result;
  43596. unsigned char add_data[13];
  43597. unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
  43598. MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
  43599. size_t explicit_iv_len = ssl->transform_in->ivlen -
  43600. ssl->transform_in->fixed_ivlen;
  43601. if( ssl->in_msglen < explicit_iv_len + taglen )
  43602. {
  43603. MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
  43604. "+ taglen (%d)", ssl->in_msglen,
  43605. explicit_iv_len, taglen ) );
  43606. return( MBEDTLS_ERR_SSL_INVALID_MAC );
  43607. }
  43608. dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
  43609. dec_msg = ssl->in_msg;
  43610. dec_msg_result = ssl->in_msg;
  43611. ssl->in_msglen = dec_msglen;
  43612. memcpy( add_data, ssl->in_ctr, 8 );
  43613. add_data[8] = ssl->in_msgtype;
  43614. mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
  43615. ssl->conf->transport, add_data + 9 );
  43616. add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
  43617. add_data[12] = ssl->in_msglen & 0xFF;
  43618. MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
  43619. add_data, 13 );
  43620. memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
  43621. ssl->in_iv,
  43622. ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
  43623. MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
  43624. ssl->transform_in->ivlen );
  43625. MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
  43626. /*
  43627. * Decrypt and authenticate
  43628. */
  43629. if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
  43630. ssl->transform_in->iv_dec,
  43631. ssl->transform_in->ivlen,
  43632. add_data, 13,
  43633. dec_msg, dec_msglen,
  43634. dec_msg_result, &olen,
  43635. dec_msg + dec_msglen, taglen ) ) != 0 )
  43636. {
  43637. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
  43638. if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
  43639. return( MBEDTLS_ERR_SSL_INVALID_MAC );
  43640. return( ret );
  43641. }
  43642. auth_done++;
  43643. if( olen != dec_msglen )
  43644. {
  43645. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  43646. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  43647. }
  43648. }
  43649. else
  43650. #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
  43651. #if defined(MBEDTLS_CIPHER_MODE_CBC) && \
  43652. ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) )
  43653. if( mode == MBEDTLS_MODE_CBC )
  43654. {
  43655. /*
  43656. * Decrypt and check the padding
  43657. */
  43658. int ret;
  43659. unsigned char *dec_msg;
  43660. unsigned char *dec_msg_result;
  43661. size_t dec_msglen;
  43662. size_t minlen = 0;
  43663. size_t olen = 0;
  43664. /*
  43665. * Check immediate ciphertext sanity
  43666. */
  43667. #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
  43668. if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
  43669. minlen += ssl->transform_in->ivlen;
  43670. #endif
  43671. if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
  43672. ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
  43673. {
  43674. MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
  43675. "+ 1 ) ( + expl IV )", ssl->in_msglen,
  43676. ssl->transform_in->ivlen,
  43677. ssl->transform_in->maclen ) );
  43678. return( MBEDTLS_ERR_SSL_INVALID_MAC );
  43679. }
  43680. dec_msglen = ssl->in_msglen;
  43681. dec_msg = ssl->in_msg;
  43682. dec_msg_result = ssl->in_msg;
  43683. /*
  43684. * Authenticate before decrypt if enabled
  43685. */
  43686. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  43687. if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
  43688. {
  43689. unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
  43690. unsigned char pseudo_hdr[13];
  43691. MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
  43692. dec_msglen -= ssl->transform_in->maclen;
  43693. ssl->in_msglen -= ssl->transform_in->maclen;
  43694. memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
  43695. memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
  43696. pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
  43697. pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
  43698. MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
  43699. mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
  43700. mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
  43701. ssl->in_iv, ssl->in_msglen );
  43702. mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
  43703. mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
  43704. MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
  43705. ssl->transform_in->maclen );
  43706. MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
  43707. ssl->transform_in->maclen );
  43708. if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
  43709. ssl->transform_in->maclen ) != 0 )
  43710. {
  43711. MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
  43712. return( MBEDTLS_ERR_SSL_INVALID_MAC );
  43713. }
  43714. auth_done++;
  43715. }
  43716. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  43717. /*
  43718. * Check length sanity
  43719. */
  43720. if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
  43721. {
  43722. MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
  43723. ssl->in_msglen, ssl->transform_in->ivlen ) );
  43724. return( MBEDTLS_ERR_SSL_INVALID_MAC );
  43725. }
  43726. #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
  43727. /*
  43728. * Initialize for prepended IV for block cipher in TLS v1.1 and up
  43729. */
  43730. if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
  43731. {
  43732. dec_msglen -= ssl->transform_in->ivlen;
  43733. ssl->in_msglen -= ssl->transform_in->ivlen;
  43734. for( i = 0; i < ssl->transform_in->ivlen; i++ )
  43735. ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
  43736. }
  43737. #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
  43738. if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
  43739. ssl->transform_in->iv_dec,
  43740. ssl->transform_in->ivlen,
  43741. dec_msg, dec_msglen,
  43742. dec_msg_result, &olen ) ) != 0 )
  43743. {
  43744. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
  43745. return( ret );
  43746. }
  43747. if( dec_msglen != olen )
  43748. {
  43749. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  43750. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  43751. }
  43752. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
  43753. if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
  43754. {
  43755. /*
  43756. * Save IV in SSL3 and TLS1
  43757. */
  43758. memcpy( ssl->transform_in->iv_dec,
  43759. ssl->transform_in->cipher_ctx_dec.iv,
  43760. ssl->transform_in->ivlen );
  43761. }
  43762. #endif
  43763. padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
  43764. if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
  43765. auth_done == 0 )
  43766. {
  43767. #if defined(MBEDTLS_SSL_DEBUG_ALL)
  43768. MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
  43769. ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
  43770. #endif
  43771. padlen = 0;
  43772. correct = 0;
  43773. }
  43774. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  43775. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  43776. {
  43777. if( padlen > ssl->transform_in->ivlen )
  43778. {
  43779. #if defined(MBEDTLS_SSL_DEBUG_ALL)
  43780. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
  43781. "should be no more than %d",
  43782. padlen, ssl->transform_in->ivlen ) );
  43783. #endif
  43784. correct = 0;
  43785. }
  43786. }
  43787. else
  43788. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  43789. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  43790. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  43791. if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
  43792. {
  43793. /*
  43794. * TLSv1+: always check the padding up to the first failure
  43795. * and fake check up to 256 bytes of padding
  43796. */
  43797. size_t pad_count = 0, real_count = 1;
  43798. size_t padding_idx = ssl->in_msglen - padlen - 1;
  43799. /*
  43800. * Padding is guaranteed to be incorrect if:
  43801. * 1. padlen >= ssl->in_msglen
  43802. *
  43803. * 2. padding_idx >= MBEDTLS_SSL_MAX_CONTENT_LEN +
  43804. * ssl->transform_in->maclen
  43805. *
  43806. * In both cases we reset padding_idx to a safe value (0) to
  43807. * prevent out-of-buffer reads.
  43808. */
  43809. correct &= ( ssl->in_msglen >= padlen + 1 );
  43810. correct &= ( padding_idx < MBEDTLS_SSL_MAX_CONTENT_LEN +
  43811. ssl->transform_in->maclen );
  43812. padding_idx *= correct;
  43813. for( i = 1; i <= 256; i++ )
  43814. {
  43815. real_count &= ( i <= padlen );
  43816. pad_count += real_count *
  43817. ( ssl->in_msg[padding_idx + i] == padlen - 1 );
  43818. }
  43819. correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
  43820. #if defined(MBEDTLS_SSL_DEBUG_ALL)
  43821. if( padlen > 0 && correct == 0 )
  43822. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
  43823. #endif
  43824. padlen &= correct * 0x1FF;
  43825. }
  43826. else
  43827. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
  43828. MBEDTLS_SSL_PROTO_TLS1_2 */
  43829. {
  43830. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  43831. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  43832. }
  43833. ssl->in_msglen -= padlen;
  43834. }
  43835. else
  43836. #endif /* MBEDTLS_CIPHER_MODE_CBC &&
  43837. ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C ) */
  43838. {
  43839. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  43840. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  43841. }
  43842. MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
  43843. ssl->in_msg, ssl->in_msglen );
  43844. /*
  43845. * Authenticate if not done yet.
  43846. * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
  43847. */
  43848. #if defined(SSL_SOME_MODES_USE_MAC)
  43849. if( auth_done == 0 )
  43850. {
  43851. unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
  43852. ssl->in_msglen -= ssl->transform_in->maclen;
  43853. ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
  43854. ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
  43855. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  43856. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  43857. {
  43858. ssl_mac( &ssl->transform_in->md_ctx_dec,
  43859. ssl->transform_in->mac_dec,
  43860. ssl->in_msg, ssl->in_msglen,
  43861. ssl->in_ctr, ssl->in_msgtype,
  43862. mac_expect );
  43863. }
  43864. else
  43865. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  43866. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  43867. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  43868. if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
  43869. {
  43870. /*
  43871. * Process MAC and always update for padlen afterwards to make
  43872. * total time independent of padlen
  43873. *
  43874. * extra_run compensates MAC check for padlen
  43875. *
  43876. * Known timing attacks:
  43877. * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
  43878. *
  43879. * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
  43880. * correctly. (We round down instead of up, so -56 is the correct
  43881. * value for our calculations instead of -55)
  43882. */
  43883. size_t j, extra_run = 0;
  43884. extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
  43885. ( 13 + ssl->in_msglen + 8 ) / 64;
  43886. extra_run &= correct * 0xFF;
  43887. mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
  43888. mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
  43889. mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
  43890. mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
  43891. ssl->in_msglen );
  43892. mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
  43893. /* Call mbedtls_md_process at least once due to cache attacks */
  43894. for( j = 0; j < extra_run + 1; j++ )
  43895. mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
  43896. mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
  43897. }
  43898. else
  43899. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
  43900. MBEDTLS_SSL_PROTO_TLS1_2 */
  43901. {
  43902. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  43903. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  43904. }
  43905. MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
  43906. MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
  43907. ssl->transform_in->maclen );
  43908. if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
  43909. ssl->transform_in->maclen ) != 0 )
  43910. {
  43911. #if defined(MBEDTLS_SSL_DEBUG_ALL)
  43912. MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
  43913. #endif
  43914. correct = 0;
  43915. }
  43916. auth_done++;
  43917. /*
  43918. * Finally check the correct flag
  43919. */
  43920. if( correct == 0 )
  43921. return( MBEDTLS_ERR_SSL_INVALID_MAC );
  43922. }
  43923. #endif /* SSL_SOME_MODES_USE_MAC */
  43924. /* Make extra sure authentication was performed, exactly once */
  43925. if( auth_done != 1 )
  43926. {
  43927. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  43928. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  43929. }
  43930. if( ssl->in_msglen == 0 )
  43931. {
  43932. ssl->nb_zero++;
  43933. /*
  43934. * Three or more empty messages may be a DoS attack
  43935. * (excessive CPU consumption).
  43936. */
  43937. if( ssl->nb_zero > 3 )
  43938. {
  43939. MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
  43940. "messages, possible DoS attack" ) );
  43941. return( MBEDTLS_ERR_SSL_INVALID_MAC );
  43942. }
  43943. }
  43944. else
  43945. ssl->nb_zero = 0;
  43946. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  43947. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  43948. {
  43949. ; /* in_ctr read from peer, not maintained internally */
  43950. }
  43951. else
  43952. #endif
  43953. {
  43954. for( i = 8; i > ssl_ep_len( ssl ); i-- )
  43955. if( ++ssl->in_ctr[i - 1] != 0 )
  43956. break;
  43957. /* The loop goes to its end iff the counter is wrapping */
  43958. if( i == ssl_ep_len( ssl ) )
  43959. {
  43960. MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
  43961. return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
  43962. }
  43963. }
  43964. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
  43965. return( 0 );
  43966. }
  43967. #undef MAC_NONE
  43968. #undef MAC_PLAINTEXT
  43969. #undef MAC_CIPHERTEXT
  43970. #if defined(MBEDTLS_ZLIB_SUPPORT)
  43971. /*
  43972. * Compression/decompression functions
  43973. */
  43974. static int ssl_compress_buf( mbedtls_ssl_context *ssl )
  43975. {
  43976. int ret;
  43977. unsigned char *msg_post = ssl->out_msg;
  43978. size_t len_pre = ssl->out_msglen;
  43979. unsigned char *msg_pre = ssl->compress_buf;
  43980. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
  43981. if( len_pre == 0 )
  43982. return( 0 );
  43983. memcpy( msg_pre, ssl->out_msg, len_pre );
  43984. MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
  43985. ssl->out_msglen ) );
  43986. MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
  43987. ssl->out_msg, ssl->out_msglen );
  43988. ssl->transform_out->ctx_deflate.next_in = msg_pre;
  43989. ssl->transform_out->ctx_deflate.avail_in = len_pre;
  43990. ssl->transform_out->ctx_deflate.next_out = msg_post;
  43991. ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_BUFFER_LEN;
  43992. ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
  43993. if( ret != Z_OK )
  43994. {
  43995. MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
  43996. return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
  43997. }
  43998. ssl->out_msglen = MBEDTLS_SSL_BUFFER_LEN -
  43999. ssl->transform_out->ctx_deflate.avail_out;
  44000. MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
  44001. ssl->out_msglen ) );
  44002. MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
  44003. ssl->out_msg, ssl->out_msglen );
  44004. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
  44005. return( 0 );
  44006. }
  44007. static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
  44008. {
  44009. int ret;
  44010. unsigned char *msg_post = ssl->in_msg;
  44011. size_t len_pre = ssl->in_msglen;
  44012. unsigned char *msg_pre = ssl->compress_buf;
  44013. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
  44014. if( len_pre == 0 )
  44015. return( 0 );
  44016. memcpy( msg_pre, ssl->in_msg, len_pre );
  44017. MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
  44018. ssl->in_msglen ) );
  44019. MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
  44020. ssl->in_msg, ssl->in_msglen );
  44021. ssl->transform_in->ctx_inflate.next_in = msg_pre;
  44022. ssl->transform_in->ctx_inflate.avail_in = len_pre;
  44023. ssl->transform_in->ctx_inflate.next_out = msg_post;
  44024. ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_MAX_CONTENT_LEN;
  44025. ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
  44026. if( ret != Z_OK )
  44027. {
  44028. MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
  44029. return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
  44030. }
  44031. ssl->in_msglen = MBEDTLS_SSL_MAX_CONTENT_LEN -
  44032. ssl->transform_in->ctx_inflate.avail_out;
  44033. MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
  44034. ssl->in_msglen ) );
  44035. MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
  44036. ssl->in_msg, ssl->in_msglen );
  44037. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
  44038. return( 0 );
  44039. }
  44040. #endif /* MBEDTLS_ZLIB_SUPPORT */
  44041. #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
  44042. static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
  44043. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  44044. static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
  44045. {
  44046. /* If renegotiation is not enforced, retransmit until we would reach max
  44047. * timeout if we were using the usual handshake doubling scheme */
  44048. if( ssl->conf->renego_max_records < 0 )
  44049. {
  44050. uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
  44051. unsigned char doublings = 1;
  44052. while( ratio != 0 )
  44053. {
  44054. ++doublings;
  44055. ratio >>= 1;
  44056. }
  44057. if( ++ssl->renego_records_seen > doublings )
  44058. {
  44059. MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
  44060. return( 0 );
  44061. }
  44062. }
  44063. return( ssl_write_hello_request( ssl ) );
  44064. }
  44065. #endif
  44066. #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
  44067. /*
  44068. * Fill the input message buffer by appending data to it.
  44069. * The amount of data already fetched is in ssl->in_left.
  44070. *
  44071. * If we return 0, is it guaranteed that (at least) nb_want bytes are
  44072. * available (from this read and/or a previous one). Otherwise, an error code
  44073. * is returned (possibly EOF or WANT_READ).
  44074. *
  44075. * With stream transport (TLS) on success ssl->in_left == nb_want, but
  44076. * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
  44077. * since we always read a whole datagram at once.
  44078. *
  44079. * For DTLS, it is up to the caller to set ssl->next_record_offset when
  44080. * they're done reading a record.
  44081. */
  44082. int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
  44083. {
  44084. int ret;
  44085. size_t len;
  44086. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
  44087. if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
  44088. {
  44089. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
  44090. "or mbedtls_ssl_set_bio()" ) );
  44091. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  44092. }
  44093. if( nb_want > MBEDTLS_SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
  44094. {
  44095. MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
  44096. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  44097. }
  44098. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  44099. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  44100. {
  44101. uint32_t timeout;
  44102. /* Just to be sure */
  44103. if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
  44104. {
  44105. MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
  44106. "mbedtls_ssl_set_timer_cb() for DTLS" ) );
  44107. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  44108. }
  44109. /*
  44110. * The point is, we need to always read a full datagram at once, so we
  44111. * sometimes read more then requested, and handle the additional data.
  44112. * It could be the rest of the current record (while fetching the
  44113. * header) and/or some other records in the same datagram.
  44114. */
  44115. /*
  44116. * Move to the next record in the already read datagram if applicable
  44117. */
  44118. if( ssl->next_record_offset != 0 )
  44119. {
  44120. if( ssl->in_left < ssl->next_record_offset )
  44121. {
  44122. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  44123. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  44124. }
  44125. ssl->in_left -= ssl->next_record_offset;
  44126. if( ssl->in_left != 0 )
  44127. {
  44128. MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
  44129. ssl->next_record_offset ) );
  44130. memmove( ssl->in_hdr,
  44131. ssl->in_hdr + ssl->next_record_offset,
  44132. ssl->in_left );
  44133. }
  44134. ssl->next_record_offset = 0;
  44135. }
  44136. MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
  44137. ssl->in_left, nb_want ) );
  44138. /*
  44139. * Done if we already have enough data.
  44140. */
  44141. if( nb_want <= ssl->in_left)
  44142. {
  44143. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
  44144. return( 0 );
  44145. }
  44146. /*
  44147. * A record can't be split accross datagrams. If we need to read but
  44148. * are not at the beginning of a new record, the caller did something
  44149. * wrong.
  44150. */
  44151. if( ssl->in_left != 0 )
  44152. {
  44153. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  44154. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  44155. }
  44156. /*
  44157. * Don't even try to read if time's out already.
  44158. * This avoids by-passing the timer when repeatedly receiving messages
  44159. * that will end up being dropped.
  44160. */
  44161. if( ssl_check_timer( ssl ) != 0 )
  44162. ret = MBEDTLS_ERR_SSL_TIMEOUT;
  44163. else
  44164. {
  44165. len = MBEDTLS_SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
  44166. if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
  44167. timeout = ssl->handshake->retransmit_timeout;
  44168. else
  44169. timeout = ssl->conf->read_timeout;
  44170. MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
  44171. if( ssl->f_recv_timeout != NULL )
  44172. ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
  44173. timeout );
  44174. else
  44175. ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
  44176. MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
  44177. if( ret == 0 )
  44178. return( MBEDTLS_ERR_SSL_CONN_EOF );
  44179. }
  44180. if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
  44181. {
  44182. MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
  44183. ssl_set_timer( ssl, 0 );
  44184. if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
  44185. {
  44186. if( ssl_double_retransmit_timeout( ssl ) != 0 )
  44187. {
  44188. MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
  44189. return( MBEDTLS_ERR_SSL_TIMEOUT );
  44190. }
  44191. if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
  44192. {
  44193. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
  44194. return( ret );
  44195. }
  44196. return( MBEDTLS_ERR_SSL_WANT_READ );
  44197. }
  44198. #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
  44199. else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
  44200. ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
  44201. {
  44202. if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
  44203. {
  44204. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
  44205. return( ret );
  44206. }
  44207. return( MBEDTLS_ERR_SSL_WANT_READ );
  44208. }
  44209. #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
  44210. }
  44211. if( ret < 0 )
  44212. return( ret );
  44213. ssl->in_left = ret;
  44214. }
  44215. else
  44216. #endif
  44217. {
  44218. MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
  44219. ssl->in_left, nb_want ) );
  44220. while( ssl->in_left < nb_want )
  44221. {
  44222. len = nb_want - ssl->in_left;
  44223. if( ssl_check_timer( ssl ) != 0 )
  44224. ret = MBEDTLS_ERR_SSL_TIMEOUT;
  44225. else
  44226. {
  44227. if( ssl->f_recv_timeout != NULL )
  44228. {
  44229. ret = ssl->f_recv_timeout( ssl->p_bio,
  44230. ssl->in_hdr + ssl->in_left, len,
  44231. ssl->conf->read_timeout );
  44232. }
  44233. else
  44234. {
  44235. ret = ssl->f_recv( ssl->p_bio,
  44236. ssl->in_hdr + ssl->in_left, len );
  44237. }
  44238. }
  44239. MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
  44240. ssl->in_left, nb_want ) );
  44241. MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
  44242. if( ret == 0 )
  44243. return( MBEDTLS_ERR_SSL_CONN_EOF );
  44244. if( ret < 0 )
  44245. return( ret );
  44246. ssl->in_left += ret;
  44247. }
  44248. }
  44249. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
  44250. return( 0 );
  44251. }
  44252. /*
  44253. * Flush any data not yet written
  44254. */
  44255. int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
  44256. {
  44257. int ret;
  44258. unsigned char *buf, i;
  44259. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
  44260. if( ssl->f_send == NULL )
  44261. {
  44262. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
  44263. "or mbedtls_ssl_set_bio()" ) );
  44264. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  44265. }
  44266. /* Avoid incrementing counter if data is flushed */
  44267. if( ssl->out_left == 0 )
  44268. {
  44269. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
  44270. return( 0 );
  44271. }
  44272. while( ssl->out_left > 0 )
  44273. {
  44274. MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
  44275. mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
  44276. buf = ssl->out_hdr + mbedtls_ssl_hdr_len( ssl ) +
  44277. ssl->out_msglen - ssl->out_left;
  44278. ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
  44279. MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
  44280. if( ret <= 0 )
  44281. return( ret );
  44282. ssl->out_left -= ret;
  44283. }
  44284. for( i = 8; i > ssl_ep_len( ssl ); i-- )
  44285. if( ++ssl->out_ctr[i - 1] != 0 )
  44286. break;
  44287. /* The loop goes to its end iff the counter is wrapping */
  44288. if( i == ssl_ep_len( ssl ) )
  44289. {
  44290. MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
  44291. return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
  44292. }
  44293. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
  44294. return( 0 );
  44295. }
  44296. /*
  44297. * Functions to handle the DTLS retransmission state machine
  44298. */
  44299. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  44300. /*
  44301. * Append current handshake message to current outgoing flight
  44302. */
  44303. static int ssl_flight_append( mbedtls_ssl_context *ssl )
  44304. {
  44305. mbedtls_ssl_flight_item *msg;
  44306. /* Allocate space for current message */
  44307. if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
  44308. {
  44309. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
  44310. sizeof( mbedtls_ssl_flight_item ) ) );
  44311. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  44312. }
  44313. if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
  44314. {
  44315. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
  44316. mbedtls_free( msg );
  44317. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  44318. }
  44319. /* Copy current handshake message with headers */
  44320. memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
  44321. msg->len = ssl->out_msglen;
  44322. msg->type = ssl->out_msgtype;
  44323. msg->next = NULL;
  44324. /* Append to the current flight */
  44325. if( ssl->handshake->flight == NULL )
  44326. ssl->handshake->flight = msg;
  44327. else
  44328. {
  44329. mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
  44330. while( cur->next != NULL )
  44331. cur = cur->next;
  44332. cur->next = msg;
  44333. }
  44334. return( 0 );
  44335. }
  44336. /*
  44337. * Free the current flight of handshake messages
  44338. */
  44339. static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
  44340. {
  44341. mbedtls_ssl_flight_item *cur = flight;
  44342. mbedtls_ssl_flight_item *next;
  44343. while( cur != NULL )
  44344. {
  44345. next = cur->next;
  44346. mbedtls_free( cur->p );
  44347. mbedtls_free( cur );
  44348. cur = next;
  44349. }
  44350. }
  44351. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  44352. static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
  44353. #endif
  44354. /*
  44355. * Swap transform_out and out_ctr with the alternative ones
  44356. */
  44357. static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
  44358. {
  44359. mbedtls_ssl_transform *tmp_transform;
  44360. unsigned char tmp_out_ctr[8];
  44361. if( ssl->transform_out == ssl->handshake->alt_transform_out )
  44362. {
  44363. MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
  44364. return;
  44365. }
  44366. MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
  44367. /* Swap transforms */
  44368. tmp_transform = ssl->transform_out;
  44369. ssl->transform_out = ssl->handshake->alt_transform_out;
  44370. ssl->handshake->alt_transform_out = tmp_transform;
  44371. /* Swap epoch + sequence_number */
  44372. memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
  44373. memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
  44374. memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
  44375. /* Adjust to the newly activated transform */
  44376. if( ssl->transform_out != NULL &&
  44377. ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
  44378. {
  44379. ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
  44380. ssl->transform_out->fixed_ivlen;
  44381. }
  44382. else
  44383. ssl->out_msg = ssl->out_iv;
  44384. #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
  44385. if( mbedtls_ssl_hw_record_activate != NULL )
  44386. {
  44387. if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
  44388. {
  44389. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
  44390. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  44391. }
  44392. }
  44393. #endif
  44394. }
  44395. /*
  44396. * Retransmit the current flight of messages.
  44397. *
  44398. * Need to remember the current message in case flush_output returns
  44399. * WANT_WRITE, causing us to exit this function and come back later.
  44400. * This function must be called until state is no longer SENDING.
  44401. */
  44402. int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
  44403. {
  44404. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
  44405. if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
  44406. {
  44407. MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
  44408. ssl->handshake->cur_msg = ssl->handshake->flight;
  44409. ssl_swap_epochs( ssl );
  44410. ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
  44411. }
  44412. while( ssl->handshake->cur_msg != NULL )
  44413. {
  44414. int ret;
  44415. mbedtls_ssl_flight_item *cur = ssl->handshake->cur_msg;
  44416. /* Swap epochs before sending Finished: we can't do it after
  44417. * sending ChangeCipherSpec, in case write returns WANT_READ.
  44418. * Must be done before copying, may change out_msg pointer */
  44419. if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
  44420. cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
  44421. {
  44422. ssl_swap_epochs( ssl );
  44423. }
  44424. memcpy( ssl->out_msg, cur->p, cur->len );
  44425. ssl->out_msglen = cur->len;
  44426. ssl->out_msgtype = cur->type;
  44427. ssl->handshake->cur_msg = cur->next;
  44428. MBEDTLS_SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
  44429. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  44430. {
  44431. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  44432. return( ret );
  44433. }
  44434. }
  44435. if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
  44436. ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
  44437. else
  44438. {
  44439. ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
  44440. ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
  44441. }
  44442. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
  44443. return( 0 );
  44444. }
  44445. /*
  44446. * To be called when the last message of an incoming flight is received.
  44447. */
  44448. void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
  44449. {
  44450. /* We won't need to resend that one any more */
  44451. ssl_flight_free( ssl->handshake->flight );
  44452. ssl->handshake->flight = NULL;
  44453. ssl->handshake->cur_msg = NULL;
  44454. /* The next incoming flight will start with this msg_seq */
  44455. ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
  44456. /* Cancel timer */
  44457. ssl_set_timer( ssl, 0 );
  44458. if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
  44459. ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
  44460. {
  44461. ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
  44462. }
  44463. else
  44464. ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
  44465. }
  44466. /*
  44467. * To be called when the last message of an outgoing flight is send.
  44468. */
  44469. void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
  44470. {
  44471. ssl_reset_retransmit_timeout( ssl );
  44472. ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
  44473. if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
  44474. ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
  44475. {
  44476. ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
  44477. }
  44478. else
  44479. ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
  44480. }
  44481. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  44482. /*
  44483. * Record layer functions
  44484. */
  44485. /*
  44486. * Write current record.
  44487. * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
  44488. */
  44489. int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
  44490. {
  44491. int ret, done = 0, out_msg_type;
  44492. size_t len = ssl->out_msglen;
  44493. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
  44494. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  44495. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  44496. ssl->handshake != NULL &&
  44497. ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
  44498. {
  44499. ; /* Skip special handshake treatment when resending */
  44500. }
  44501. else
  44502. #endif
  44503. if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
  44504. {
  44505. out_msg_type = ssl->out_msg[0];
  44506. if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
  44507. ssl->handshake == NULL )
  44508. {
  44509. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  44510. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  44511. }
  44512. ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
  44513. ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
  44514. ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
  44515. /*
  44516. * DTLS has additional fields in the Handshake layer,
  44517. * between the length field and the actual payload:
  44518. * uint16 message_seq;
  44519. * uint24 fragment_offset;
  44520. * uint24 fragment_length;
  44521. */
  44522. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  44523. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  44524. {
  44525. /* Make room for the additional DTLS fields */
  44526. if( MBEDTLS_SSL_MAX_CONTENT_LEN - ssl->out_msglen < 8 )
  44527. {
  44528. MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
  44529. "size %u, maximum %u",
  44530. (unsigned) ( ssl->in_hslen - 4 ),
  44531. (unsigned) ( MBEDTLS_SSL_MAX_CONTENT_LEN - 12 ) ) );
  44532. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  44533. }
  44534. memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
  44535. ssl->out_msglen += 8;
  44536. len += 8;
  44537. /* Write message_seq and update it, except for HelloRequest */
  44538. if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
  44539. {
  44540. ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
  44541. ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
  44542. ++( ssl->handshake->out_msg_seq );
  44543. }
  44544. else
  44545. {
  44546. ssl->out_msg[4] = 0;
  44547. ssl->out_msg[5] = 0;
  44548. }
  44549. /* We don't fragment, so frag_offset = 0 and frag_len = len */
  44550. memset( ssl->out_msg + 6, 0x00, 3 );
  44551. memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
  44552. }
  44553. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  44554. if( out_msg_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
  44555. ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
  44556. }
  44557. /* Save handshake and CCS messages for resending */
  44558. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  44559. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  44560. ssl->handshake != NULL &&
  44561. ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING &&
  44562. ( ssl->out_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ||
  44563. ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) )
  44564. {
  44565. if( ( ret = ssl_flight_append( ssl ) ) != 0 )
  44566. {
  44567. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
  44568. return( ret );
  44569. }
  44570. }
  44571. #endif
  44572. #if defined(MBEDTLS_ZLIB_SUPPORT)
  44573. if( ssl->transform_out != NULL &&
  44574. ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
  44575. {
  44576. if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
  44577. {
  44578. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
  44579. return( ret );
  44580. }
  44581. len = ssl->out_msglen;
  44582. }
  44583. #endif /*MBEDTLS_ZLIB_SUPPORT */
  44584. #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
  44585. if( mbedtls_ssl_hw_record_write != NULL )
  44586. {
  44587. MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
  44588. ret = mbedtls_ssl_hw_record_write( ssl );
  44589. if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
  44590. {
  44591. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
  44592. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  44593. }
  44594. if( ret == 0 )
  44595. done = 1;
  44596. }
  44597. #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
  44598. if( !done )
  44599. {
  44600. ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
  44601. mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
  44602. ssl->conf->transport, ssl->out_hdr + 1 );
  44603. ssl->out_len[0] = (unsigned char)( len >> 8 );
  44604. ssl->out_len[1] = (unsigned char)( len );
  44605. if( ssl->transform_out != NULL )
  44606. {
  44607. if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
  44608. {
  44609. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
  44610. return( ret );
  44611. }
  44612. len = ssl->out_msglen;
  44613. ssl->out_len[0] = (unsigned char)( len >> 8 );
  44614. ssl->out_len[1] = (unsigned char)( len );
  44615. }
  44616. ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
  44617. MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
  44618. "version = [%d:%d], msglen = %d",
  44619. ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
  44620. ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
  44621. MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
  44622. ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
  44623. }
  44624. if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
  44625. {
  44626. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
  44627. return( ret );
  44628. }
  44629. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
  44630. return( 0 );
  44631. }
  44632. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  44633. /*
  44634. * Mark bits in bitmask (used for DTLS HS reassembly)
  44635. */
  44636. static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
  44637. {
  44638. unsigned int start_bits, end_bits;
  44639. start_bits = 8 - ( offset % 8 );
  44640. if( start_bits != 8 )
  44641. {
  44642. size_t first_byte_idx = offset / 8;
  44643. /* Special case */
  44644. if( len <= start_bits )
  44645. {
  44646. for( ; len != 0; len-- )
  44647. mask[first_byte_idx] |= 1 << ( start_bits - len );
  44648. /* Avoid potential issues with offset or len becoming invalid */
  44649. return;
  44650. }
  44651. offset += start_bits; /* Now offset % 8 == 0 */
  44652. len -= start_bits;
  44653. for( ; start_bits != 0; start_bits-- )
  44654. mask[first_byte_idx] |= 1 << ( start_bits - 1 );
  44655. }
  44656. end_bits = len % 8;
  44657. if( end_bits != 0 )
  44658. {
  44659. size_t last_byte_idx = ( offset + len ) / 8;
  44660. len -= end_bits; /* Now len % 8 == 0 */
  44661. for( ; end_bits != 0; end_bits-- )
  44662. mask[last_byte_idx] |= 1 << ( 8 - end_bits );
  44663. }
  44664. memset( mask + offset / 8, 0xFF, len / 8 );
  44665. }
  44666. /*
  44667. * Check that bitmask is full
  44668. */
  44669. static int ssl_bitmask_check( unsigned char *mask, size_t len )
  44670. {
  44671. size_t i;
  44672. for( i = 0; i < len / 8; i++ )
  44673. if( mask[i] != 0xFF )
  44674. return( -1 );
  44675. for( i = 0; i < len % 8; i++ )
  44676. if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
  44677. return( -1 );
  44678. return( 0 );
  44679. }
  44680. /*
  44681. * Reassemble fragmented DTLS handshake messages.
  44682. *
  44683. * Use a temporary buffer for reassembly, divided in two parts:
  44684. * - the first holds the reassembled message (including handshake header),
  44685. * - the second holds a bitmask indicating which parts of the message
  44686. * (excluding headers) have been received so far.
  44687. */
  44688. static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
  44689. {
  44690. unsigned char *msg, *bitmask;
  44691. size_t frag_len, frag_off;
  44692. size_t msg_len = ssl->in_hslen - 12; /* Without headers */
  44693. if( ssl->handshake == NULL )
  44694. {
  44695. MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
  44696. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  44697. }
  44698. /*
  44699. * For first fragment, check size and allocate buffer
  44700. */
  44701. if( ssl->handshake->hs_msg == NULL )
  44702. {
  44703. size_t alloc_len;
  44704. MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
  44705. msg_len ) );
  44706. if( ssl->in_hslen > MBEDTLS_SSL_MAX_CONTENT_LEN )
  44707. {
  44708. MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
  44709. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  44710. }
  44711. /* The bitmask needs one bit per byte of message excluding header */
  44712. alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
  44713. ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
  44714. if( ssl->handshake->hs_msg == NULL )
  44715. {
  44716. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
  44717. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  44718. }
  44719. /* Prepare final header: copy msg_type, length and message_seq,
  44720. * then add standardised fragment_offset and fragment_length */
  44721. memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
  44722. memset( ssl->handshake->hs_msg + 6, 0, 3 );
  44723. memcpy( ssl->handshake->hs_msg + 9,
  44724. ssl->handshake->hs_msg + 1, 3 );
  44725. }
  44726. else
  44727. {
  44728. /* Make sure msg_type and length are consistent */
  44729. if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
  44730. {
  44731. MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
  44732. return( MBEDTLS_ERR_SSL_INVALID_RECORD );
  44733. }
  44734. }
  44735. msg = ssl->handshake->hs_msg + 12;
  44736. bitmask = msg + msg_len;
  44737. /*
  44738. * Check and copy current fragment
  44739. */
  44740. frag_off = ( ssl->in_msg[6] << 16 ) |
  44741. ( ssl->in_msg[7] << 8 ) |
  44742. ssl->in_msg[8];
  44743. frag_len = ( ssl->in_msg[9] << 16 ) |
  44744. ( ssl->in_msg[10] << 8 ) |
  44745. ssl->in_msg[11];
  44746. if( frag_off + frag_len > msg_len )
  44747. {
  44748. MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
  44749. frag_off, frag_len, msg_len ) );
  44750. return( MBEDTLS_ERR_SSL_INVALID_RECORD );
  44751. }
  44752. if( frag_len + 12 > ssl->in_msglen )
  44753. {
  44754. MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
  44755. frag_len, ssl->in_msglen ) );
  44756. return( MBEDTLS_ERR_SSL_INVALID_RECORD );
  44757. }
  44758. MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
  44759. frag_off, frag_len ) );
  44760. memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
  44761. ssl_bitmask_set( bitmask, frag_off, frag_len );
  44762. /*
  44763. * Do we have the complete message by now?
  44764. * If yes, finalize it, else ask to read the next record.
  44765. */
  44766. if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
  44767. {
  44768. MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
  44769. return( MBEDTLS_ERR_SSL_WANT_READ );
  44770. }
  44771. MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
  44772. if( frag_len + 12 < ssl->in_msglen )
  44773. {
  44774. /*
  44775. * We'got more handshake messages in the same record.
  44776. * This case is not handled now because no know implementation does
  44777. * that and it's hard to test, so we prefer to fail cleanly for now.
  44778. */
  44779. MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
  44780. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  44781. }
  44782. if( ssl->in_left > ssl->next_record_offset )
  44783. {
  44784. /*
  44785. * We've got more data in the buffer after the current record,
  44786. * that we don't want to overwrite. Move it before writing the
  44787. * reassembled message, and adjust in_left and next_record_offset.
  44788. */
  44789. unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
  44790. unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
  44791. size_t remain_len = ssl->in_left - ssl->next_record_offset;
  44792. /* First compute and check new lengths */
  44793. ssl->next_record_offset = new_remain - ssl->in_hdr;
  44794. ssl->in_left = ssl->next_record_offset + remain_len;
  44795. if( ssl->in_left > MBEDTLS_SSL_BUFFER_LEN -
  44796. (size_t)( ssl->in_hdr - ssl->in_buf ) )
  44797. {
  44798. MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
  44799. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  44800. }
  44801. memmove( new_remain, cur_remain, remain_len );
  44802. }
  44803. memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
  44804. mbedtls_free( ssl->handshake->hs_msg );
  44805. ssl->handshake->hs_msg = NULL;
  44806. MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
  44807. ssl->in_msg, ssl->in_hslen );
  44808. return( 0 );
  44809. }
  44810. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  44811. int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
  44812. {
  44813. if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
  44814. {
  44815. MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
  44816. ssl->in_msglen ) );
  44817. return( MBEDTLS_ERR_SSL_INVALID_RECORD );
  44818. }
  44819. ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
  44820. ( ssl->in_msg[1] << 16 ) |
  44821. ( ssl->in_msg[2] << 8 ) |
  44822. ssl->in_msg[3] );
  44823. MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
  44824. " %d, type = %d, hslen = %d",
  44825. ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
  44826. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  44827. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  44828. {
  44829. int ret;
  44830. unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
  44831. /* ssl->handshake is NULL when receiving ClientHello for renego */
  44832. if( ssl->handshake != NULL &&
  44833. recv_msg_seq != ssl->handshake->in_msg_seq )
  44834. {
  44835. /* Retransmit only on last message from previous flight, to avoid
  44836. * too many retransmissions.
  44837. * Besides, No sane server ever retransmits HelloVerifyRequest */
  44838. if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
  44839. ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
  44840. {
  44841. MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
  44842. "message_seq = %d, start_of_flight = %d",
  44843. recv_msg_seq,
  44844. ssl->handshake->in_flight_start_seq ) );
  44845. if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
  44846. {
  44847. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
  44848. return( ret );
  44849. }
  44850. }
  44851. else
  44852. {
  44853. MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
  44854. "message_seq = %d, expected = %d",
  44855. recv_msg_seq,
  44856. ssl->handshake->in_msg_seq ) );
  44857. }
  44858. return( MBEDTLS_ERR_SSL_WANT_READ );
  44859. }
  44860. /* Wait until message completion to increment in_msg_seq */
  44861. /* Reassemble if current message is fragmented or reassembly is
  44862. * already in progress */
  44863. if( ssl->in_msglen < ssl->in_hslen ||
  44864. memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
  44865. memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
  44866. ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
  44867. {
  44868. MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
  44869. if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
  44870. {
  44871. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
  44872. return( ret );
  44873. }
  44874. }
  44875. }
  44876. else
  44877. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  44878. /* With TLS we don't handle fragmentation (for now) */
  44879. if( ssl->in_msglen < ssl->in_hslen )
  44880. {
  44881. MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
  44882. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  44883. }
  44884. return( 0 );
  44885. }
  44886. void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
  44887. {
  44888. if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
  44889. ssl->handshake != NULL )
  44890. {
  44891. ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
  44892. }
  44893. /* Handshake message is complete, increment counter */
  44894. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  44895. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  44896. ssl->handshake != NULL )
  44897. {
  44898. ssl->handshake->in_msg_seq++;
  44899. }
  44900. #endif
  44901. }
  44902. /*
  44903. * DTLS anti-replay: RFC 6347 4.1.2.6
  44904. *
  44905. * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
  44906. * Bit n is set iff record number in_window_top - n has been seen.
  44907. *
  44908. * Usually, in_window_top is the last record number seen and the lsb of
  44909. * in_window is set. The only exception is the initial state (record number 0
  44910. * not seen yet).
  44911. */
  44912. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  44913. static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
  44914. {
  44915. ssl->in_window_top = 0;
  44916. ssl->in_window = 0;
  44917. }
  44918. static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
  44919. {
  44920. return( ( (uint64_t) buf[0] << 40 ) |
  44921. ( (uint64_t) buf[1] << 32 ) |
  44922. ( (uint64_t) buf[2] << 24 ) |
  44923. ( (uint64_t) buf[3] << 16 ) |
  44924. ( (uint64_t) buf[4] << 8 ) |
  44925. ( (uint64_t) buf[5] ) );
  44926. }
  44927. /*
  44928. * Return 0 if sequence number is acceptable, -1 otherwise
  44929. */
  44930. int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
  44931. {
  44932. uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
  44933. uint64_t bit;
  44934. if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
  44935. return( 0 );
  44936. if( rec_seqnum > ssl->in_window_top )
  44937. return( 0 );
  44938. bit = ssl->in_window_top - rec_seqnum;
  44939. if( bit >= 64 )
  44940. return( -1 );
  44941. if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
  44942. return( -1 );
  44943. return( 0 );
  44944. }
  44945. /*
  44946. * Update replay window on new validated record
  44947. */
  44948. void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
  44949. {
  44950. uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
  44951. if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
  44952. return;
  44953. if( rec_seqnum > ssl->in_window_top )
  44954. {
  44955. /* Update window_top and the contents of the window */
  44956. uint64_t shift = rec_seqnum - ssl->in_window_top;
  44957. if( shift >= 64 )
  44958. ssl->in_window = 1;
  44959. else
  44960. {
  44961. ssl->in_window <<= shift;
  44962. ssl->in_window |= 1;
  44963. }
  44964. ssl->in_window_top = rec_seqnum;
  44965. }
  44966. else
  44967. {
  44968. /* Mark that number as seen in the current window */
  44969. uint64_t bit = ssl->in_window_top - rec_seqnum;
  44970. if( bit < 64 ) /* Always true, but be extra sure */
  44971. ssl->in_window |= (uint64_t) 1 << bit;
  44972. }
  44973. }
  44974. #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
  44975. #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
  44976. /* Forward declaration */
  44977. static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
  44978. /*
  44979. * Without any SSL context, check if a datagram looks like a ClientHello with
  44980. * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
  44981. * Both input and output include full DTLS headers.
  44982. *
  44983. * - if cookie is valid, return 0
  44984. * - if ClientHello looks superficially valid but cookie is not,
  44985. * fill obuf and set olen, then
  44986. * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
  44987. * - otherwise return a specific error code
  44988. */
  44989. static int ssl_check_dtls_clihlo_cookie(
  44990. mbedtls_ssl_cookie_write_t *f_cookie_write,
  44991. mbedtls_ssl_cookie_check_t *f_cookie_check,
  44992. void *p_cookie,
  44993. const unsigned char *cli_id, size_t cli_id_len,
  44994. const unsigned char *in, size_t in_len,
  44995. unsigned char *obuf, size_t buf_len, size_t *olen )
  44996. {
  44997. size_t sid_len, cookie_len;
  44998. unsigned char *p;
  44999. if( f_cookie_write == NULL || f_cookie_check == NULL )
  45000. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  45001. /*
  45002. * Structure of ClientHello with record and handshake headers,
  45003. * and expected values. We don't need to check a lot, more checks will be
  45004. * done when actually parsing the ClientHello - skipping those checks
  45005. * avoids code duplication and does not make cookie forging any easier.
  45006. *
  45007. * 0-0 ContentType type; copied, must be handshake
  45008. * 1-2 ProtocolVersion version; copied
  45009. * 3-4 uint16 epoch; copied, must be 0
  45010. * 5-10 uint48 sequence_number; copied
  45011. * 11-12 uint16 length; (ignored)
  45012. *
  45013. * 13-13 HandshakeType msg_type; (ignored)
  45014. * 14-16 uint24 length; (ignored)
  45015. * 17-18 uint16 message_seq; copied
  45016. * 19-21 uint24 fragment_offset; copied, must be 0
  45017. * 22-24 uint24 fragment_length; (ignored)
  45018. *
  45019. * 25-26 ProtocolVersion client_version; (ignored)
  45020. * 27-58 Random random; (ignored)
  45021. * 59-xx SessionID session_id; 1 byte len + sid_len content
  45022. * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
  45023. * ...
  45024. *
  45025. * Minimum length is 61 bytes.
  45026. */
  45027. if( in_len < 61 ||
  45028. in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
  45029. in[3] != 0 || in[4] != 0 ||
  45030. in[19] != 0 || in[20] != 0 || in[21] != 0 )
  45031. {
  45032. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  45033. }
  45034. sid_len = in[59];
  45035. if( sid_len > in_len - 61 )
  45036. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  45037. cookie_len = in[60 + sid_len];
  45038. if( cookie_len > in_len - 60 )
  45039. return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
  45040. if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
  45041. cli_id, cli_id_len ) == 0 )
  45042. {
  45043. /* Valid cookie */
  45044. return( 0 );
  45045. }
  45046. /*
  45047. * If we get here, we've got an invalid cookie, let's prepare HVR.
  45048. *
  45049. * 0-0 ContentType type; copied
  45050. * 1-2 ProtocolVersion version; copied
  45051. * 3-4 uint16 epoch; copied
  45052. * 5-10 uint48 sequence_number; copied
  45053. * 11-12 uint16 length; olen - 13
  45054. *
  45055. * 13-13 HandshakeType msg_type; hello_verify_request
  45056. * 14-16 uint24 length; olen - 25
  45057. * 17-18 uint16 message_seq; copied
  45058. * 19-21 uint24 fragment_offset; copied
  45059. * 22-24 uint24 fragment_length; olen - 25
  45060. *
  45061. * 25-26 ProtocolVersion server_version; 0xfe 0xff
  45062. * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
  45063. *
  45064. * Minimum length is 28.
  45065. */
  45066. if( buf_len < 28 )
  45067. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  45068. /* Copy most fields and adapt others */
  45069. memcpy( obuf, in, 25 );
  45070. obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
  45071. obuf[25] = 0xfe;
  45072. obuf[26] = 0xff;
  45073. /* Generate and write actual cookie */
  45074. p = obuf + 28;
  45075. if( f_cookie_write( p_cookie,
  45076. &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
  45077. {
  45078. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  45079. }
  45080. *olen = p - obuf;
  45081. /* Go back and fill length fields */
  45082. obuf[27] = (unsigned char)( *olen - 28 );
  45083. obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
  45084. obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
  45085. obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
  45086. obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
  45087. obuf[12] = (unsigned char)( ( *olen - 13 ) );
  45088. return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
  45089. }
  45090. /*
  45091. * Handle possible client reconnect with the same UDP quadruplet
  45092. * (RFC 6347 Section 4.2.8).
  45093. *
  45094. * Called by ssl_parse_record_header() in case we receive an epoch 0 record
  45095. * that looks like a ClientHello.
  45096. *
  45097. * - if the input looks like a ClientHello without cookies,
  45098. * send back HelloVerifyRequest, then
  45099. * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
  45100. * - if the input looks like a ClientHello with a valid cookie,
  45101. * reset the session of the current context, and
  45102. * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
  45103. * - if anything goes wrong, return a specific error code
  45104. *
  45105. * mbedtls_ssl_read_record() will ignore the record if anything else than
  45106. * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
  45107. * cannot not return 0.
  45108. */
  45109. static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
  45110. {
  45111. int ret;
  45112. size_t len;
  45113. ret = ssl_check_dtls_clihlo_cookie(
  45114. ssl->conf->f_cookie_write,
  45115. ssl->conf->f_cookie_check,
  45116. ssl->conf->p_cookie,
  45117. ssl->cli_id, ssl->cli_id_len,
  45118. ssl->in_buf, ssl->in_left,
  45119. ssl->out_buf, MBEDTLS_SSL_MAX_CONTENT_LEN, &len );
  45120. MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
  45121. if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
  45122. {
  45123. /* Don't check write errors as we can't do anything here.
  45124. * If the error is permanent we'll catch it later,
  45125. * if it's not, then hopefully it'll work next time. */
  45126. (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
  45127. return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
  45128. }
  45129. if( ret == 0 )
  45130. {
  45131. /* Got a valid cookie, partially reset context */
  45132. if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
  45133. {
  45134. MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
  45135. return( ret );
  45136. }
  45137. return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
  45138. }
  45139. return( ret );
  45140. }
  45141. #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
  45142. /*
  45143. * ContentType type;
  45144. * ProtocolVersion version;
  45145. * uint16 epoch; // DTLS only
  45146. * uint48 sequence_number; // DTLS only
  45147. * uint16 length;
  45148. *
  45149. * Return 0 if header looks sane (and, for DTLS, the record is expected)
  45150. * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
  45151. * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
  45152. *
  45153. * With DTLS, mbedtls_ssl_read_record() will:
  45154. * 1. proceed with the record if this function returns 0
  45155. * 2. drop only the current record if this function returns UNEXPECTED_RECORD
  45156. * 3. return CLIENT_RECONNECT if this function return that value
  45157. * 4. drop the whole datagram if this function returns anything else.
  45158. * Point 2 is needed when the peer is resending, and we have already received
  45159. * the first record from a datagram but are still waiting for the others.
  45160. */
  45161. static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
  45162. {
  45163. int major_ver, minor_ver;
  45164. MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) );
  45165. ssl->in_msgtype = ssl->in_hdr[0];
  45166. ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
  45167. mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
  45168. MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
  45169. "version = [%d:%d], msglen = %d",
  45170. ssl->in_msgtype,
  45171. major_ver, minor_ver, ssl->in_msglen ) );
  45172. /* Check record type */
  45173. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
  45174. ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
  45175. ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
  45176. ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
  45177. {
  45178. MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
  45179. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  45180. /* Silently ignore invalid DTLS records as recommended by RFC 6347
  45181. * Section 4.1.2.7 */
  45182. if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  45183. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  45184. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  45185. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  45186. return( MBEDTLS_ERR_SSL_INVALID_RECORD );
  45187. }
  45188. /* Check version */
  45189. if( major_ver != ssl->major_ver )
  45190. {
  45191. MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
  45192. return( MBEDTLS_ERR_SSL_INVALID_RECORD );
  45193. }
  45194. if( minor_ver > ssl->conf->max_minor_ver )
  45195. {
  45196. MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
  45197. return( MBEDTLS_ERR_SSL_INVALID_RECORD );
  45198. }
  45199. /* Check length against the size of our buffer */
  45200. if( ssl->in_msglen > MBEDTLS_SSL_BUFFER_LEN
  45201. - (size_t)( ssl->in_msg - ssl->in_buf ) )
  45202. {
  45203. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
  45204. return( MBEDTLS_ERR_SSL_INVALID_RECORD );
  45205. }
  45206. /* Check length against bounds of the current transform and version */
  45207. if( ssl->transform_in == NULL )
  45208. {
  45209. if( ssl->in_msglen < 1 ||
  45210. ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
  45211. {
  45212. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
  45213. return( MBEDTLS_ERR_SSL_INVALID_RECORD );
  45214. }
  45215. }
  45216. else
  45217. {
  45218. if( ssl->in_msglen < ssl->transform_in->minlen )
  45219. {
  45220. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
  45221. return( MBEDTLS_ERR_SSL_INVALID_RECORD );
  45222. }
  45223. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  45224. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
  45225. ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_MAX_CONTENT_LEN )
  45226. {
  45227. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
  45228. return( MBEDTLS_ERR_SSL_INVALID_RECORD );
  45229. }
  45230. #endif
  45231. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  45232. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  45233. /*
  45234. * TLS encrypted messages can have up to 256 bytes of padding
  45235. */
  45236. if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
  45237. ssl->in_msglen > ssl->transform_in->minlen +
  45238. MBEDTLS_SSL_MAX_CONTENT_LEN + 256 )
  45239. {
  45240. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
  45241. return( MBEDTLS_ERR_SSL_INVALID_RECORD );
  45242. }
  45243. #endif
  45244. }
  45245. /*
  45246. * DTLS-related tests done last, because most of them may result in
  45247. * silently dropping the record (but not the whole datagram), and we only
  45248. * want to consider that after ensuring that the "basic" fields (type,
  45249. * version, length) are sane.
  45250. */
  45251. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  45252. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  45253. {
  45254. unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
  45255. /* Drop unexpected ChangeCipherSpec messages */
  45256. if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
  45257. ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
  45258. ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
  45259. {
  45260. MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
  45261. return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
  45262. }
  45263. /* Drop unexpected ApplicationData records,
  45264. * except at the beginning of renegotiations */
  45265. if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
  45266. ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
  45267. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  45268. && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  45269. ssl->state == MBEDTLS_SSL_SERVER_HELLO )
  45270. #endif
  45271. )
  45272. {
  45273. MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
  45274. return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
  45275. }
  45276. /* Check epoch (and sequence number) with DTLS */
  45277. if( rec_epoch != ssl->in_epoch )
  45278. {
  45279. MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
  45280. "expected %d, received %d",
  45281. ssl->in_epoch, rec_epoch ) );
  45282. #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
  45283. /*
  45284. * Check for an epoch 0 ClientHello. We can't use in_msg here to
  45285. * access the first byte of record content (handshake type), as we
  45286. * have an active transform (possibly iv_len != 0), so use the
  45287. * fact that the record header len is 13 instead.
  45288. */
  45289. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
  45290. ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
  45291. rec_epoch == 0 &&
  45292. ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
  45293. ssl->in_left > 13 &&
  45294. ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
  45295. {
  45296. MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
  45297. "from the same port" ) );
  45298. return( ssl_handle_possible_reconnect( ssl ) );
  45299. }
  45300. else
  45301. #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
  45302. return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
  45303. }
  45304. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  45305. /* Replay detection only works for the current epoch */
  45306. if( rec_epoch == ssl->in_epoch &&
  45307. mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
  45308. {
  45309. MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
  45310. return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
  45311. }
  45312. #endif
  45313. }
  45314. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  45315. return( 0 );
  45316. }
  45317. /*
  45318. * If applicable, decrypt (and decompress) record content
  45319. */
  45320. static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
  45321. {
  45322. int ret, done = 0;
  45323. MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
  45324. ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
  45325. #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
  45326. if( mbedtls_ssl_hw_record_read != NULL )
  45327. {
  45328. MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
  45329. ret = mbedtls_ssl_hw_record_read( ssl );
  45330. if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
  45331. {
  45332. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
  45333. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  45334. }
  45335. if( ret == 0 )
  45336. done = 1;
  45337. }
  45338. #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
  45339. if( !done && ssl->transform_in != NULL )
  45340. {
  45341. if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
  45342. {
  45343. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
  45344. return( ret );
  45345. }
  45346. MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
  45347. ssl->in_msg, ssl->in_msglen );
  45348. if( ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
  45349. {
  45350. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
  45351. return( MBEDTLS_ERR_SSL_INVALID_RECORD );
  45352. }
  45353. }
  45354. #if defined(MBEDTLS_ZLIB_SUPPORT)
  45355. if( ssl->transform_in != NULL &&
  45356. ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
  45357. {
  45358. if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
  45359. {
  45360. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
  45361. return( ret );
  45362. }
  45363. }
  45364. #endif /* MBEDTLS_ZLIB_SUPPORT */
  45365. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  45366. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  45367. {
  45368. mbedtls_ssl_dtls_replay_update( ssl );
  45369. }
  45370. #endif
  45371. return( 0 );
  45372. }
  45373. static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
  45374. /*
  45375. * Read a record.
  45376. *
  45377. * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
  45378. * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
  45379. *
  45380. */
  45381. int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
  45382. {
  45383. int ret;
  45384. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
  45385. if( ssl->keep_current_message == 0 )
  45386. {
  45387. do {
  45388. if( ( ret = mbedtls_ssl_read_record_layer( ssl ) ) != 0 )
  45389. {
  45390. MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
  45391. return( ret );
  45392. }
  45393. ret = mbedtls_ssl_handle_message_type( ssl );
  45394. } while( MBEDTLS_ERR_SSL_NON_FATAL == ret );
  45395. if( 0 != ret )
  45396. {
  45397. MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
  45398. return( ret );
  45399. }
  45400. if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
  45401. {
  45402. mbedtls_ssl_update_handshake_status( ssl );
  45403. }
  45404. }
  45405. else
  45406. {
  45407. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
  45408. ssl->keep_current_message = 0;
  45409. }
  45410. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
  45411. return( 0 );
  45412. }
  45413. int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
  45414. {
  45415. int ret;
  45416. /*
  45417. * Step A
  45418. *
  45419. * Consume last content-layer message and potentially
  45420. * update in_msglen which keeps track of the contents'
  45421. * consumption state.
  45422. *
  45423. * (1) Handshake messages:
  45424. * Remove last handshake message, move content
  45425. * and adapt in_msglen.
  45426. *
  45427. * (2) Alert messages:
  45428. * Consume whole record content, in_msglen = 0.
  45429. *
  45430. * NOTE: This needs to be fixed, since like for
  45431. * handshake messages it is allowed to have
  45432. * multiple alerts witin a single record.
  45433. * Internal reference IOTSSL-1321.
  45434. *
  45435. * (3) Change cipher spec:
  45436. * Consume whole record content, in_msglen = 0.
  45437. *
  45438. * (4) Application data:
  45439. * Don't do anything - the record layer provides
  45440. * the application data as a stream transport
  45441. * and consumes through mbedtls_ssl_read only.
  45442. *
  45443. */
  45444. /* Case (1): Handshake messages */
  45445. if( ssl->in_hslen != 0 )
  45446. {
  45447. /* Hard assertion to be sure that no application data
  45448. * is in flight, as corrupting ssl->in_msglen during
  45449. * ssl->in_offt != NULL is fatal. */
  45450. if( ssl->in_offt != NULL )
  45451. {
  45452. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  45453. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  45454. }
  45455. /*
  45456. * Get next Handshake message in the current record
  45457. */
  45458. /* Notes:
  45459. * (1) in_hslen is *NOT* necessarily the size of the
  45460. * current handshake content: If DTLS handshake
  45461. * fragmentation is used, that's the fragment
  45462. * size instead. Using the total handshake message
  45463. * size here is FAULTY and should be changed at
  45464. * some point. Internal reference IOTSSL-1414.
  45465. * (2) While it doesn't seem to cause problems, one
  45466. * has to be very careful not to assume that in_hslen
  45467. * is always <= in_msglen in a sensible communication.
  45468. * Again, it's wrong for DTLS handshake fragmentation.
  45469. * The following check is therefore mandatory, and
  45470. * should not be treated as a silently corrected assertion.
  45471. * Additionally, ssl->in_hslen might be arbitrarily out of
  45472. * bounds after handling a DTLS message with an unexpected
  45473. * sequence number, see mbedtls_ssl_prepare_handshake_record.
  45474. */
  45475. if( ssl->in_hslen < ssl->in_msglen )
  45476. {
  45477. ssl->in_msglen -= ssl->in_hslen;
  45478. memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
  45479. ssl->in_msglen );
  45480. MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
  45481. ssl->in_msg, ssl->in_msglen );
  45482. }
  45483. else
  45484. {
  45485. ssl->in_msglen = 0;
  45486. }
  45487. ssl->in_hslen = 0;
  45488. }
  45489. /* Case (4): Application data */
  45490. else if( ssl->in_offt != NULL )
  45491. {
  45492. return( 0 );
  45493. }
  45494. /* Everything else (CCS & Alerts) */
  45495. else
  45496. {
  45497. ssl->in_msglen = 0;
  45498. }
  45499. /*
  45500. * Step B
  45501. *
  45502. * Fetch and decode new record if current one is fully consumed.
  45503. *
  45504. */
  45505. if( ssl->in_msglen > 0 )
  45506. {
  45507. /* There's something left to be processed in the current record. */
  45508. return( 0 );
  45509. }
  45510. /* Need to fetch a new record */
  45511. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  45512. read_record_header:
  45513. #endif
  45514. /* Current record either fully processed or to be discarded. */
  45515. if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
  45516. {
  45517. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
  45518. return( ret );
  45519. }
  45520. if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
  45521. {
  45522. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  45523. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  45524. ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
  45525. {
  45526. if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
  45527. {
  45528. /* Skip unexpected record (but not whole datagram) */
  45529. ssl->next_record_offset = ssl->in_msglen
  45530. + mbedtls_ssl_hdr_len( ssl );
  45531. MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
  45532. "(header)" ) );
  45533. }
  45534. else
  45535. {
  45536. /* Skip invalid record and the rest of the datagram */
  45537. ssl->next_record_offset = 0;
  45538. ssl->in_left = 0;
  45539. MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
  45540. "(header)" ) );
  45541. }
  45542. /* Get next record */
  45543. goto read_record_header;
  45544. }
  45545. #endif
  45546. return( ret );
  45547. }
  45548. /*
  45549. * Read and optionally decrypt the message contents
  45550. */
  45551. if( ( ret = mbedtls_ssl_fetch_input( ssl,
  45552. mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
  45553. {
  45554. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
  45555. return( ret );
  45556. }
  45557. /* Done reading this record, get ready for the next one */
  45558. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  45559. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  45560. ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
  45561. else
  45562. #endif
  45563. ssl->in_left = 0;
  45564. if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
  45565. {
  45566. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  45567. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  45568. {
  45569. /* Silently discard invalid records */
  45570. if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
  45571. ret == MBEDTLS_ERR_SSL_INVALID_MAC )
  45572. {
  45573. /* Except when waiting for Finished as a bad mac here
  45574. * probably means something went wrong in the handshake
  45575. * (eg wrong psk used, mitm downgrade attempt, etc.) */
  45576. if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
  45577. ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
  45578. {
  45579. #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
  45580. if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
  45581. {
  45582. mbedtls_ssl_send_alert_message( ssl,
  45583. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  45584. MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
  45585. }
  45586. #endif
  45587. return( ret );
  45588. }
  45589. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  45590. if( ssl->conf->badmac_limit != 0 &&
  45591. ++ssl->badmac_seen >= ssl->conf->badmac_limit )
  45592. {
  45593. MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
  45594. return( MBEDTLS_ERR_SSL_INVALID_MAC );
  45595. }
  45596. #endif
  45597. /* As above, invalid records cause
  45598. * dismissal of the whole datagram. */
  45599. ssl->next_record_offset = 0;
  45600. ssl->in_left = 0;
  45601. MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
  45602. goto read_record_header;
  45603. }
  45604. return( ret );
  45605. }
  45606. else
  45607. #endif
  45608. {
  45609. /* Error out (and send alert) on invalid records */
  45610. #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
  45611. if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
  45612. {
  45613. mbedtls_ssl_send_alert_message( ssl,
  45614. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  45615. MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
  45616. }
  45617. #endif
  45618. return( ret );
  45619. }
  45620. }
  45621. /*
  45622. * When we sent the last flight of the handshake, we MUST respond to a
  45623. * retransmit of the peer's previous flight with a retransmit. (In
  45624. * practice, only the Finished message will make it, other messages
  45625. * including CCS use the old transform so they're dropped as invalid.)
  45626. *
  45627. * If the record we received is not a handshake message, however, it
  45628. * means the peer received our last flight so we can clean up
  45629. * handshake info.
  45630. *
  45631. * This check needs to be done before prepare_handshake() due to an edge
  45632. * case: if the client immediately requests renegotiation, this
  45633. * finishes the current handshake first, avoiding the new ClientHello
  45634. * being mistaken for an ancient message in the current handshake.
  45635. */
  45636. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  45637. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  45638. ssl->handshake != NULL &&
  45639. ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
  45640. {
  45641. if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
  45642. ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
  45643. {
  45644. MBEDTLS_SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
  45645. if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
  45646. {
  45647. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
  45648. return( ret );
  45649. }
  45650. return( MBEDTLS_ERR_SSL_WANT_READ );
  45651. }
  45652. else
  45653. {
  45654. ssl_handshake_wrapup_free_hs_transform( ssl );
  45655. }
  45656. }
  45657. #endif
  45658. return( 0 );
  45659. }
  45660. int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
  45661. {
  45662. int ret;
  45663. /*
  45664. * Handle particular types of records
  45665. */
  45666. if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
  45667. {
  45668. if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
  45669. {
  45670. return( ret );
  45671. }
  45672. }
  45673. if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
  45674. {
  45675. MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
  45676. ssl->in_msg[0], ssl->in_msg[1] ) );
  45677. /*
  45678. * Ignore non-fatal alerts, except close_notify and no_renegotiation
  45679. */
  45680. if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
  45681. {
  45682. MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
  45683. ssl->in_msg[1] ) );
  45684. return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
  45685. }
  45686. if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
  45687. ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
  45688. {
  45689. MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
  45690. return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
  45691. }
  45692. #if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
  45693. if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
  45694. ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
  45695. {
  45696. MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
  45697. /* Will be handled when trying to parse ServerHello */
  45698. return( 0 );
  45699. }
  45700. #endif
  45701. #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
  45702. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
  45703. ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
  45704. ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
  45705. ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
  45706. {
  45707. MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
  45708. /* Will be handled in mbedtls_ssl_parse_certificate() */
  45709. return( 0 );
  45710. }
  45711. #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
  45712. /* Silently ignore: fetch new message */
  45713. return MBEDTLS_ERR_SSL_NON_FATAL;
  45714. }
  45715. return( 0 );
  45716. }
  45717. int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
  45718. {
  45719. int ret;
  45720. if( ( ret = mbedtls_ssl_send_alert_message( ssl,
  45721. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  45722. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
  45723. {
  45724. return( ret );
  45725. }
  45726. return( 0 );
  45727. }
  45728. int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
  45729. unsigned char level,
  45730. unsigned char message )
  45731. {
  45732. int ret;
  45733. if( ssl == NULL || ssl->conf == NULL )
  45734. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  45735. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
  45736. MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
  45737. ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
  45738. ssl->out_msglen = 2;
  45739. ssl->out_msg[0] = level;
  45740. ssl->out_msg[1] = message;
  45741. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  45742. {
  45743. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  45744. return( ret );
  45745. }
  45746. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
  45747. return( 0 );
  45748. }
  45749. /*
  45750. * Handshake functions
  45751. */
  45752. #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
  45753. !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
  45754. !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
  45755. !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
  45756. !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
  45757. !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
  45758. !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  45759. /* No certificate support -> dummy functions */
  45760. int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
  45761. {
  45762. const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
  45763. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
  45764. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  45765. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  45766. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  45767. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  45768. {
  45769. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
  45770. ssl->state++;
  45771. return( 0 );
  45772. }
  45773. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  45774. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  45775. }
  45776. int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
  45777. {
  45778. const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
  45779. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
  45780. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  45781. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  45782. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  45783. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  45784. {
  45785. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
  45786. ssl->state++;
  45787. return( 0 );
  45788. }
  45789. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  45790. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  45791. }
  45792. #else
  45793. /* Some certificate support -> implement write and parse */
  45794. int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
  45795. {
  45796. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  45797. size_t i, n;
  45798. const mbedtls_x509_crt *crt;
  45799. const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
  45800. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
  45801. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  45802. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  45803. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  45804. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  45805. {
  45806. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
  45807. ssl->state++;
  45808. return( 0 );
  45809. }
  45810. #if defined(MBEDTLS_SSL_CLI_C)
  45811. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
  45812. {
  45813. if( ssl->client_auth == 0 )
  45814. {
  45815. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
  45816. ssl->state++;
  45817. return( 0 );
  45818. }
  45819. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  45820. /*
  45821. * If using SSLv3 and got no cert, send an Alert message
  45822. * (otherwise an empty Certificate message will be sent).
  45823. */
  45824. if( mbedtls_ssl_own_cert( ssl ) == NULL &&
  45825. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  45826. {
  45827. ssl->out_msglen = 2;
  45828. ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
  45829. ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
  45830. ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
  45831. MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
  45832. goto write_msg;
  45833. }
  45834. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  45835. }
  45836. #endif /* MBEDTLS_SSL_CLI_C */
  45837. #if defined(MBEDTLS_SSL_SRV_C)
  45838. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
  45839. {
  45840. if( mbedtls_ssl_own_cert( ssl ) == NULL )
  45841. {
  45842. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
  45843. return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
  45844. }
  45845. }
  45846. #endif
  45847. MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
  45848. /*
  45849. * 0 . 0 handshake type
  45850. * 1 . 3 handshake length
  45851. * 4 . 6 length of all certs
  45852. * 7 . 9 length of cert. 1
  45853. * 10 . n-1 peer certificate
  45854. * n . n+2 length of cert. 2
  45855. * n+3 . ... upper level cert, etc.
  45856. */
  45857. i = 7;
  45858. crt = mbedtls_ssl_own_cert( ssl );
  45859. while( crt != NULL )
  45860. {
  45861. n = crt->raw.len;
  45862. if( n > MBEDTLS_SSL_MAX_CONTENT_LEN - 3 - i )
  45863. {
  45864. MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
  45865. i + 3 + n, MBEDTLS_SSL_MAX_CONTENT_LEN ) );
  45866. return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
  45867. }
  45868. ssl->out_msg[i ] = (unsigned char)( n >> 16 );
  45869. ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
  45870. ssl->out_msg[i + 2] = (unsigned char)( n );
  45871. i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
  45872. i += n; crt = crt->next;
  45873. }
  45874. ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
  45875. ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
  45876. ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
  45877. ssl->out_msglen = i;
  45878. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  45879. ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
  45880. #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
  45881. write_msg:
  45882. #endif
  45883. ssl->state++;
  45884. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  45885. {
  45886. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  45887. return( ret );
  45888. }
  45889. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
  45890. return( ret );
  45891. }
  45892. int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
  45893. {
  45894. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  45895. size_t i, n;
  45896. const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
  45897. int authmode = ssl->conf->authmode;
  45898. uint8_t alert;
  45899. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
  45900. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  45901. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  45902. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  45903. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  45904. {
  45905. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
  45906. ssl->state++;
  45907. return( 0 );
  45908. }
  45909. #if defined(MBEDTLS_SSL_SRV_C)
  45910. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
  45911. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  45912. {
  45913. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
  45914. ssl->state++;
  45915. return( 0 );
  45916. }
  45917. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  45918. if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
  45919. authmode = ssl->handshake->sni_authmode;
  45920. #endif
  45921. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
  45922. authmode == MBEDTLS_SSL_VERIFY_NONE )
  45923. {
  45924. ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
  45925. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
  45926. ssl->state++;
  45927. return( 0 );
  45928. }
  45929. #endif
  45930. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  45931. {
  45932. /* mbedtls_ssl_read_record may have sent an alert already. We
  45933. let it decide whether to alert. */
  45934. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  45935. return( ret );
  45936. }
  45937. ssl->state++;
  45938. #if defined(MBEDTLS_SSL_SRV_C)
  45939. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  45940. /*
  45941. * Check if the client sent an empty certificate
  45942. */
  45943. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
  45944. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  45945. {
  45946. if( ssl->in_msglen == 2 &&
  45947. ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
  45948. ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
  45949. ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
  45950. {
  45951. MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
  45952. /* The client was asked for a certificate but didn't send
  45953. one. The client should know what's going on, so we
  45954. don't send an alert. */
  45955. ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
  45956. if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
  45957. return( 0 );
  45958. else
  45959. return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
  45960. }
  45961. }
  45962. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  45963. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  45964. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  45965. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
  45966. ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
  45967. {
  45968. if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
  45969. ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
  45970. ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
  45971. memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
  45972. {
  45973. MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
  45974. /* The client was asked for a certificate but didn't send
  45975. one. The client should know what's going on, so we
  45976. don't send an alert. */
  45977. ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
  45978. if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
  45979. return( 0 );
  45980. else
  45981. return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
  45982. }
  45983. }
  45984. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
  45985. MBEDTLS_SSL_PROTO_TLS1_2 */
  45986. #endif /* MBEDTLS_SSL_SRV_C */
  45987. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  45988. {
  45989. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
  45990. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  45991. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  45992. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  45993. }
  45994. if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
  45995. ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
  45996. {
  45997. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
  45998. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  45999. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  46000. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
  46001. }
  46002. i = mbedtls_ssl_hs_hdr_len( ssl );
  46003. /*
  46004. * Same message structure as in mbedtls_ssl_write_certificate()
  46005. */
  46006. n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
  46007. if( ssl->in_msg[i] != 0 ||
  46008. ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
  46009. {
  46010. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
  46011. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  46012. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  46013. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
  46014. }
  46015. /* In case we tried to reuse a session but it failed */
  46016. if( ssl->session_negotiate->peer_cert != NULL )
  46017. {
  46018. mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
  46019. mbedtls_free( ssl->session_negotiate->peer_cert );
  46020. }
  46021. if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
  46022. sizeof( mbedtls_x509_crt ) ) ) == NULL )
  46023. {
  46024. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
  46025. sizeof( mbedtls_x509_crt ) ) );
  46026. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  46027. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  46028. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  46029. }
  46030. mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
  46031. i += 3;
  46032. while( i < ssl->in_hslen )
  46033. {
  46034. if( ssl->in_msg[i] != 0 )
  46035. {
  46036. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
  46037. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  46038. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  46039. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
  46040. }
  46041. n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
  46042. | (unsigned int) ssl->in_msg[i + 2];
  46043. i += 3;
  46044. if( n < 128 || i + n > ssl->in_hslen )
  46045. {
  46046. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
  46047. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  46048. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  46049. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
  46050. }
  46051. ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
  46052. ssl->in_msg + i, n );
  46053. switch( ret )
  46054. {
  46055. case 0: /*ok*/
  46056. case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
  46057. /* Ignore certificate with an unknown algorithm: maybe a
  46058. prior certificate was already trusted. */
  46059. break;
  46060. case MBEDTLS_ERR_X509_ALLOC_FAILED:
  46061. alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
  46062. goto crt_parse_der_failed;
  46063. case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
  46064. alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
  46065. goto crt_parse_der_failed;
  46066. default:
  46067. alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
  46068. crt_parse_der_failed:
  46069. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
  46070. MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
  46071. return( ret );
  46072. }
  46073. i += n;
  46074. }
  46075. MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
  46076. /*
  46077. * On client, make sure the server cert doesn't change during renego to
  46078. * avoid "triple handshake" attack: https://secure-resumption.com/
  46079. */
  46080. #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
  46081. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
  46082. ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
  46083. {
  46084. if( ssl->session->peer_cert == NULL )
  46085. {
  46086. MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
  46087. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  46088. MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
  46089. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
  46090. }
  46091. if( ssl->session->peer_cert->raw.len !=
  46092. ssl->session_negotiate->peer_cert->raw.len ||
  46093. memcmp( ssl->session->peer_cert->raw.p,
  46094. ssl->session_negotiate->peer_cert->raw.p,
  46095. ssl->session->peer_cert->raw.len ) != 0 )
  46096. {
  46097. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
  46098. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  46099. MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
  46100. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
  46101. }
  46102. }
  46103. #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
  46104. if( authmode != MBEDTLS_SSL_VERIFY_NONE )
  46105. {
  46106. mbedtls_x509_crt *ca_chain;
  46107. mbedtls_x509_crl *ca_crl;
  46108. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  46109. if( ssl->handshake->sni_ca_chain != NULL )
  46110. {
  46111. ca_chain = ssl->handshake->sni_ca_chain;
  46112. ca_crl = ssl->handshake->sni_ca_crl;
  46113. }
  46114. else
  46115. #endif
  46116. {
  46117. ca_chain = ssl->conf->ca_chain;
  46118. ca_crl = ssl->conf->ca_crl;
  46119. }
  46120. /*
  46121. * Main check: verify certificate
  46122. */
  46123. ret = mbedtls_x509_crt_verify_with_profile(
  46124. ssl->session_negotiate->peer_cert,
  46125. ca_chain, ca_crl,
  46126. ssl->conf->cert_profile,
  46127. ssl->hostname,
  46128. &ssl->session_negotiate->verify_result,
  46129. ssl->conf->f_vrfy, ssl->conf->p_vrfy );
  46130. if( ret != 0 )
  46131. {
  46132. MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
  46133. }
  46134. /*
  46135. * Secondary checks: always done, but change 'ret' only if it was 0
  46136. */
  46137. #if defined(MBEDTLS_ECP_C)
  46138. {
  46139. const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
  46140. /* If certificate uses an EC key, make sure the curve is OK */
  46141. if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
  46142. mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
  46143. {
  46144. ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
  46145. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
  46146. if( ret == 0 )
  46147. ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
  46148. }
  46149. }
  46150. #endif /* MBEDTLS_ECP_C */
  46151. if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
  46152. ciphersuite_info,
  46153. ! ssl->conf->endpoint,
  46154. &ssl->session_negotiate->verify_result ) != 0 )
  46155. {
  46156. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
  46157. if( ret == 0 )
  46158. ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
  46159. }
  46160. /* mbedtls_x509_crt_verify_with_profile is supposed to report a
  46161. * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
  46162. * with details encoded in the verification flags. All other kinds
  46163. * of error codes, including those from the user provided f_vrfy
  46164. * functions, are treated as fatal and lead to a failure of
  46165. * ssl_parse_certificate even if verification was optional. */
  46166. if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
  46167. ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
  46168. ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
  46169. {
  46170. ret = 0;
  46171. }
  46172. if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
  46173. {
  46174. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
  46175. ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
  46176. }
  46177. if( ret != 0 )
  46178. {
  46179. /* The certificate may have been rejected for several reasons.
  46180. Pick one and send the corresponding alert. Which alert to send
  46181. may be a subject of debate in some cases. */
  46182. if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
  46183. alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
  46184. else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
  46185. alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
  46186. else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
  46187. alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
  46188. else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
  46189. alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
  46190. else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
  46191. alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
  46192. else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
  46193. alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
  46194. else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
  46195. alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
  46196. else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
  46197. alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
  46198. else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
  46199. alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
  46200. else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
  46201. alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
  46202. else
  46203. alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
  46204. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  46205. alert );
  46206. }
  46207. #if defined(MBEDTLS_DEBUG_C)
  46208. if( ssl->session_negotiate->verify_result != 0 )
  46209. {
  46210. MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
  46211. ssl->session_negotiate->verify_result ) );
  46212. }
  46213. else
  46214. {
  46215. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
  46216. }
  46217. #endif /* MBEDTLS_DEBUG_C */
  46218. }
  46219. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
  46220. return( ret );
  46221. }
  46222. #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
  46223. !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
  46224. !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
  46225. !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
  46226. !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
  46227. !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
  46228. !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  46229. int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
  46230. {
  46231. int ret;
  46232. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
  46233. ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
  46234. ssl->out_msglen = 1;
  46235. ssl->out_msg[0] = 1;
  46236. ssl->state++;
  46237. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  46238. {
  46239. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  46240. return( ret );
  46241. }
  46242. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
  46243. return( 0 );
  46244. }
  46245. int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
  46246. {
  46247. int ret;
  46248. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
  46249. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  46250. {
  46251. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  46252. return( ret );
  46253. }
  46254. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
  46255. {
  46256. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
  46257. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  46258. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  46259. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  46260. }
  46261. if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
  46262. {
  46263. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
  46264. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  46265. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  46266. return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
  46267. }
  46268. /*
  46269. * Switch to our negotiated transform and session parameters for inbound
  46270. * data.
  46271. */
  46272. MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
  46273. ssl->transform_in = ssl->transform_negotiate;
  46274. ssl->session_in = ssl->session_negotiate;
  46275. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  46276. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  46277. {
  46278. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  46279. ssl_dtls_replay_reset( ssl );
  46280. #endif
  46281. /* Increment epoch */
  46282. if( ++ssl->in_epoch == 0 )
  46283. {
  46284. MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
  46285. /* This is highly unlikely to happen for legitimate reasons, so
  46286. treat it as an attack and don't send an alert. */
  46287. return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
  46288. }
  46289. }
  46290. else
  46291. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  46292. memset( ssl->in_ctr, 0, 8 );
  46293. /*
  46294. * Set the in_msg pointer to the correct location based on IV length
  46295. */
  46296. if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
  46297. {
  46298. ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
  46299. ssl->transform_negotiate->fixed_ivlen;
  46300. }
  46301. else
  46302. ssl->in_msg = ssl->in_iv;
  46303. #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
  46304. if( mbedtls_ssl_hw_record_activate != NULL )
  46305. {
  46306. if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
  46307. {
  46308. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
  46309. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  46310. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  46311. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  46312. }
  46313. }
  46314. #endif
  46315. ssl->state++;
  46316. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
  46317. return( 0 );
  46318. }
  46319. void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
  46320. const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
  46321. {
  46322. ((void) ciphersuite_info);
  46323. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  46324. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  46325. if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
  46326. ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
  46327. else
  46328. #endif
  46329. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  46330. #if defined(MBEDTLS_SHA512_C)
  46331. if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
  46332. ssl->handshake->update_checksum = ssl_update_checksum_sha384;
  46333. else
  46334. #endif
  46335. #if defined(MBEDTLS_SHA256_C)
  46336. if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
  46337. ssl->handshake->update_checksum = ssl_update_checksum_sha256;
  46338. else
  46339. #endif
  46340. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  46341. {
  46342. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  46343. return;
  46344. }
  46345. }
  46346. void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
  46347. {
  46348. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  46349. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  46350. mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
  46351. mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
  46352. #endif
  46353. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  46354. #if defined(MBEDTLS_SHA256_C)
  46355. mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
  46356. #endif
  46357. #if defined(MBEDTLS_SHA512_C)
  46358. mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
  46359. #endif
  46360. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  46361. }
  46362. static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
  46363. const unsigned char *buf, size_t len )
  46364. {
  46365. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  46366. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  46367. mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
  46368. mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
  46369. #endif
  46370. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  46371. #if defined(MBEDTLS_SHA256_C)
  46372. mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
  46373. #endif
  46374. #if defined(MBEDTLS_SHA512_C)
  46375. mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
  46376. #endif
  46377. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  46378. }
  46379. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  46380. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  46381. static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
  46382. const unsigned char *buf, size_t len )
  46383. {
  46384. mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
  46385. mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
  46386. }
  46387. #endif
  46388. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  46389. #if defined(MBEDTLS_SHA256_C)
  46390. static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
  46391. const unsigned char *buf, size_t len )
  46392. {
  46393. mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
  46394. }
  46395. #endif
  46396. #if defined(MBEDTLS_SHA512_C)
  46397. static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
  46398. const unsigned char *buf, size_t len )
  46399. {
  46400. mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
  46401. }
  46402. #endif
  46403. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  46404. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  46405. static void ssl_calc_finished_ssl(
  46406. mbedtls_ssl_context *ssl, unsigned char *buf, int from )
  46407. {
  46408. const char *sender;
  46409. mbedtls_md5_context md5;
  46410. mbedtls_sha1_context sha1;
  46411. unsigned char padbuf[48];
  46412. unsigned char md5sum[16];
  46413. unsigned char sha1sum[20];
  46414. mbedtls_ssl_session *session = ssl->session_negotiate;
  46415. if( !session )
  46416. session = ssl->session;
  46417. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
  46418. mbedtls_md5_init( &md5 );
  46419. mbedtls_sha1_init( &sha1 );
  46420. mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
  46421. mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
  46422. /*
  46423. * SSLv3:
  46424. * hash =
  46425. * MD5( master + pad2 +
  46426. * MD5( handshake + sender + master + pad1 ) )
  46427. * + SHA1( master + pad2 +
  46428. * SHA1( handshake + sender + master + pad1 ) )
  46429. */
  46430. #if !defined(MBEDTLS_MD5_ALT)
  46431. MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
  46432. md5.state, sizeof( md5.state ) );
  46433. #endif
  46434. #if !defined(MBEDTLS_SHA1_ALT)
  46435. MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
  46436. sha1.state, sizeof( sha1.state ) );
  46437. #endif
  46438. sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
  46439. : "SRVR";
  46440. memset( padbuf, 0x36, 48 );
  46441. mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
  46442. mbedtls_md5_update_ret( &md5, session->master, 48 );
  46443. mbedtls_md5_update_ret( &md5, padbuf, 48 );
  46444. mbedtls_md5_finish_ret( &md5, md5sum );
  46445. mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
  46446. mbedtls_sha1_update_ret( &sha1, session->master, 48 );
  46447. mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
  46448. mbedtls_sha1_finish_ret( &sha1, sha1sum );
  46449. memset( padbuf, 0x5C, 48 );
  46450. mbedtls_md5_starts_ret( &md5 );
  46451. mbedtls_md5_update_ret( &md5, session->master, 48 );
  46452. mbedtls_md5_update_ret( &md5, padbuf, 48 );
  46453. mbedtls_md5_update_ret( &md5, md5sum, 16 );
  46454. mbedtls_md5_finish_ret( &md5, buf );
  46455. mbedtls_sha1_starts_ret( &sha1 );
  46456. mbedtls_sha1_update_ret( &sha1, session->master, 48 );
  46457. mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
  46458. mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
  46459. mbedtls_sha1_finish_ret( &sha1, buf + 16 );
  46460. MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
  46461. mbedtls_md5_free( &md5 );
  46462. mbedtls_sha1_free( &sha1 );
  46463. mbedtls_zeroize( padbuf, sizeof( padbuf ) );
  46464. mbedtls_zeroize( md5sum, sizeof( md5sum ) );
  46465. mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
  46466. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
  46467. }
  46468. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  46469. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
  46470. static void ssl_calc_finished_tls(
  46471. mbedtls_ssl_context *ssl, unsigned char *buf, int from )
  46472. {
  46473. int len = 12;
  46474. const char *sender;
  46475. mbedtls_md5_context md5;
  46476. mbedtls_sha1_context sha1;
  46477. unsigned char padbuf[36];
  46478. mbedtls_ssl_session *session = ssl->session_negotiate;
  46479. if( !session )
  46480. session = ssl->session;
  46481. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
  46482. mbedtls_md5_init( &md5 );
  46483. mbedtls_sha1_init( &sha1 );
  46484. mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
  46485. mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
  46486. /*
  46487. * TLSv1:
  46488. * hash = PRF( master, finished_label,
  46489. * MD5( handshake ) + SHA1( handshake ) )[0..11]
  46490. */
  46491. #if !defined(MBEDTLS_MD5_ALT)
  46492. MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
  46493. md5.state, sizeof( md5.state ) );
  46494. #endif
  46495. #if !defined(MBEDTLS_SHA1_ALT)
  46496. MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
  46497. sha1.state, sizeof( sha1.state ) );
  46498. #endif
  46499. sender = ( from == MBEDTLS_SSL_IS_CLIENT )
  46500. ? "client finished"
  46501. : "server finished";
  46502. mbedtls_md5_finish_ret( &md5, padbuf );
  46503. mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
  46504. ssl->handshake->tls_prf( session->master, 48, sender,
  46505. padbuf, 36, buf, len );
  46506. MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
  46507. mbedtls_md5_free( &md5 );
  46508. mbedtls_sha1_free( &sha1 );
  46509. mbedtls_zeroize( padbuf, sizeof( padbuf ) );
  46510. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
  46511. }
  46512. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
  46513. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  46514. #if defined(MBEDTLS_SHA256_C)
  46515. static void ssl_calc_finished_tls_sha256(
  46516. mbedtls_ssl_context *ssl, unsigned char *buf, int from )
  46517. {
  46518. int len = 12;
  46519. const char *sender;
  46520. mbedtls_sha256_context sha256;
  46521. unsigned char padbuf[32];
  46522. mbedtls_ssl_session *session = ssl->session_negotiate;
  46523. if( !session )
  46524. session = ssl->session;
  46525. mbedtls_sha256_init( &sha256 );
  46526. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
  46527. mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
  46528. /*
  46529. * TLSv1.2:
  46530. * hash = PRF( master, finished_label,
  46531. * Hash( handshake ) )[0.11]
  46532. */
  46533. #if !defined(MBEDTLS_SHA256_ALT)
  46534. MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
  46535. sha256.state, sizeof( sha256.state ) );
  46536. #endif
  46537. sender = ( from == MBEDTLS_SSL_IS_CLIENT )
  46538. ? "client finished"
  46539. : "server finished";
  46540. mbedtls_sha256_finish_ret( &sha256, padbuf );
  46541. ssl->handshake->tls_prf( session->master, 48, sender,
  46542. padbuf, 32, buf, len );
  46543. MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
  46544. mbedtls_sha256_free( &sha256 );
  46545. mbedtls_zeroize( padbuf, sizeof( padbuf ) );
  46546. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
  46547. }
  46548. #endif /* MBEDTLS_SHA256_C */
  46549. #if defined(MBEDTLS_SHA512_C)
  46550. static void ssl_calc_finished_tls_sha384(
  46551. mbedtls_ssl_context *ssl, unsigned char *buf, int from )
  46552. {
  46553. int len = 12;
  46554. const char *sender;
  46555. mbedtls_sha512_context sha512;
  46556. unsigned char padbuf[48];
  46557. mbedtls_ssl_session *session = ssl->session_negotiate;
  46558. if( !session )
  46559. session = ssl->session;
  46560. mbedtls_sha512_init( &sha512 );
  46561. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
  46562. mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
  46563. /*
  46564. * TLSv1.2:
  46565. * hash = PRF( master, finished_label,
  46566. * Hash( handshake ) )[0.11]
  46567. */
  46568. #if !defined(MBEDTLS_SHA512_ALT)
  46569. MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
  46570. sha512.state, sizeof( sha512.state ) );
  46571. #endif
  46572. sender = ( from == MBEDTLS_SSL_IS_CLIENT )
  46573. ? "client finished"
  46574. : "server finished";
  46575. mbedtls_sha512_finish_ret( &sha512, padbuf );
  46576. ssl->handshake->tls_prf( session->master, 48, sender,
  46577. padbuf, 48, buf, len );
  46578. MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
  46579. mbedtls_sha512_free( &sha512 );
  46580. mbedtls_zeroize( padbuf, sizeof( padbuf ) );
  46581. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
  46582. }
  46583. #endif /* MBEDTLS_SHA512_C */
  46584. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  46585. static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
  46586. {
  46587. MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
  46588. /*
  46589. * Free our handshake params
  46590. */
  46591. mbedtls_ssl_handshake_free( ssl->handshake );
  46592. mbedtls_free( ssl->handshake );
  46593. ssl->handshake = NULL;
  46594. /*
  46595. * Free the previous transform and swith in the current one
  46596. */
  46597. if( ssl->transform )
  46598. {
  46599. mbedtls_ssl_transform_free( ssl->transform );
  46600. mbedtls_free( ssl->transform );
  46601. }
  46602. ssl->transform = ssl->transform_negotiate;
  46603. ssl->transform_negotiate = NULL;
  46604. MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
  46605. }
  46606. void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
  46607. {
  46608. int resume = ssl->handshake->resume;
  46609. MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
  46610. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  46611. if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
  46612. {
  46613. ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
  46614. ssl->renego_records_seen = 0;
  46615. }
  46616. #endif
  46617. /*
  46618. * Free the previous session and switch in the current one
  46619. */
  46620. if( ssl->session )
  46621. {
  46622. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  46623. /* RFC 7366 3.1: keep the EtM state */
  46624. ssl->session_negotiate->encrypt_then_mac =
  46625. ssl->session->encrypt_then_mac;
  46626. #endif
  46627. mbedtls_ssl_session_free( ssl->session );
  46628. mbedtls_free( ssl->session );
  46629. }
  46630. ssl->session = ssl->session_negotiate;
  46631. ssl->session_negotiate = NULL;
  46632. /*
  46633. * Add cache entry
  46634. */
  46635. if( ssl->conf->f_set_cache != NULL &&
  46636. ssl->session->id_len != 0 &&
  46637. resume == 0 )
  46638. {
  46639. if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
  46640. MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
  46641. }
  46642. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  46643. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  46644. ssl->handshake->flight != NULL )
  46645. {
  46646. /* Cancel handshake timer */
  46647. ssl_set_timer( ssl, 0 );
  46648. /* Keep last flight around in case we need to resend it:
  46649. * we need the handshake and transform structures for that */
  46650. MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
  46651. }
  46652. else
  46653. #endif
  46654. ssl_handshake_wrapup_free_hs_transform( ssl );
  46655. ssl->state++;
  46656. MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
  46657. }
  46658. int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
  46659. {
  46660. int ret, hash_len;
  46661. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
  46662. /*
  46663. * Set the out_msg pointer to the correct location based on IV length
  46664. */
  46665. if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
  46666. {
  46667. ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
  46668. ssl->transform_negotiate->fixed_ivlen;
  46669. }
  46670. else
  46671. ssl->out_msg = ssl->out_iv;
  46672. ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
  46673. /*
  46674. * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
  46675. * may define some other value. Currently (early 2016), no defined
  46676. * ciphersuite does this (and this is unlikely to change as activity has
  46677. * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
  46678. */
  46679. hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
  46680. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  46681. ssl->verify_data_len = hash_len;
  46682. memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
  46683. #endif
  46684. ssl->out_msglen = 4 + hash_len;
  46685. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  46686. ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
  46687. /*
  46688. * In case of session resuming, invert the client and server
  46689. * ChangeCipherSpec messages order.
  46690. */
  46691. if( ssl->handshake->resume != 0 )
  46692. {
  46693. #if defined(MBEDTLS_SSL_CLI_C)
  46694. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
  46695. ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
  46696. #endif
  46697. #if defined(MBEDTLS_SSL_SRV_C)
  46698. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
  46699. ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
  46700. #endif
  46701. }
  46702. else
  46703. ssl->state++;
  46704. /*
  46705. * Switch to our negotiated transform and session parameters for outbound
  46706. * data.
  46707. */
  46708. MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
  46709. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  46710. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  46711. {
  46712. unsigned char i;
  46713. /* Remember current epoch settings for resending */
  46714. ssl->handshake->alt_transform_out = ssl->transform_out;
  46715. memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
  46716. /* Set sequence_number to zero */
  46717. memset( ssl->out_ctr + 2, 0, 6 );
  46718. /* Increment epoch */
  46719. for( i = 2; i > 0; i-- )
  46720. if( ++ssl->out_ctr[i - 1] != 0 )
  46721. break;
  46722. /* The loop goes to its end iff the counter is wrapping */
  46723. if( i == 0 )
  46724. {
  46725. MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
  46726. return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
  46727. }
  46728. }
  46729. else
  46730. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  46731. memset( ssl->out_ctr, 0, 8 );
  46732. ssl->transform_out = ssl->transform_negotiate;
  46733. ssl->session_out = ssl->session_negotiate;
  46734. #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
  46735. if( mbedtls_ssl_hw_record_activate != NULL )
  46736. {
  46737. if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
  46738. {
  46739. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
  46740. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  46741. }
  46742. }
  46743. #endif
  46744. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  46745. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  46746. mbedtls_ssl_send_flight_completed( ssl );
  46747. #endif
  46748. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  46749. {
  46750. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  46751. return( ret );
  46752. }
  46753. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
  46754. return( 0 );
  46755. }
  46756. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  46757. #define SSL_MAX_HASH_LEN 36
  46758. #else
  46759. #define SSL_MAX_HASH_LEN 12
  46760. #endif
  46761. int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
  46762. {
  46763. int ret;
  46764. unsigned int hash_len;
  46765. unsigned char buf[SSL_MAX_HASH_LEN];
  46766. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
  46767. ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
  46768. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  46769. {
  46770. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  46771. return( ret );
  46772. }
  46773. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  46774. {
  46775. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
  46776. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  46777. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  46778. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  46779. }
  46780. /* There is currently no ciphersuite using another length with TLS 1.2 */
  46781. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  46782. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  46783. hash_len = 36;
  46784. else
  46785. #endif
  46786. hash_len = 12;
  46787. if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
  46788. ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
  46789. {
  46790. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
  46791. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  46792. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  46793. return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
  46794. }
  46795. if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
  46796. buf, hash_len ) != 0 )
  46797. {
  46798. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
  46799. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  46800. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  46801. return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
  46802. }
  46803. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  46804. ssl->verify_data_len = hash_len;
  46805. memcpy( ssl->peer_verify_data, buf, hash_len );
  46806. #endif
  46807. if( ssl->handshake->resume != 0 )
  46808. {
  46809. #if defined(MBEDTLS_SSL_CLI_C)
  46810. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
  46811. ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
  46812. #endif
  46813. #if defined(MBEDTLS_SSL_SRV_C)
  46814. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
  46815. ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
  46816. #endif
  46817. }
  46818. else
  46819. ssl->state++;
  46820. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  46821. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  46822. mbedtls_ssl_recv_flight_completed( ssl );
  46823. #endif
  46824. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
  46825. return( 0 );
  46826. }
  46827. static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
  46828. {
  46829. memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
  46830. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  46831. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  46832. mbedtls_md5_init( &handshake->fin_md5 );
  46833. mbedtls_sha1_init( &handshake->fin_sha1 );
  46834. mbedtls_md5_starts_ret( &handshake->fin_md5 );
  46835. mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
  46836. #endif
  46837. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  46838. #if defined(MBEDTLS_SHA256_C)
  46839. mbedtls_sha256_init( &handshake->fin_sha256 );
  46840. mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
  46841. #endif
  46842. #if defined(MBEDTLS_SHA512_C)
  46843. mbedtls_sha512_init( &handshake->fin_sha512 );
  46844. mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
  46845. #endif
  46846. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  46847. handshake->update_checksum = ssl_update_checksum_start;
  46848. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  46849. defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  46850. mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
  46851. #endif
  46852. #if defined(MBEDTLS_DHM_C)
  46853. mbedtls_dhm_init( &handshake->dhm_ctx );
  46854. #endif
  46855. #if defined(MBEDTLS_ECDH_C)
  46856. mbedtls_ecdh_init( &handshake->ecdh_ctx );
  46857. #endif
  46858. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  46859. mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
  46860. #if defined(MBEDTLS_SSL_CLI_C)
  46861. handshake->ecjpake_cache = NULL;
  46862. handshake->ecjpake_cache_len = 0;
  46863. #endif
  46864. #endif
  46865. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  46866. handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
  46867. #endif
  46868. }
  46869. static void ssl_transform_init( mbedtls_ssl_transform *transform )
  46870. {
  46871. memset( transform, 0, sizeof(mbedtls_ssl_transform) );
  46872. mbedtls_cipher_init( &transform->cipher_ctx_enc );
  46873. mbedtls_cipher_init( &transform->cipher_ctx_dec );
  46874. mbedtls_md_init( &transform->md_ctx_enc );
  46875. mbedtls_md_init( &transform->md_ctx_dec );
  46876. }
  46877. void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
  46878. {
  46879. memset( session, 0, sizeof(mbedtls_ssl_session) );
  46880. }
  46881. static int ssl_handshake_init( mbedtls_ssl_context *ssl )
  46882. {
  46883. /* Clear old handshake information if present */
  46884. if( ssl->transform_negotiate )
  46885. mbedtls_ssl_transform_free( ssl->transform_negotiate );
  46886. if( ssl->session_negotiate )
  46887. mbedtls_ssl_session_free( ssl->session_negotiate );
  46888. if( ssl->handshake )
  46889. mbedtls_ssl_handshake_free( ssl->handshake );
  46890. /*
  46891. * Either the pointers are now NULL or cleared properly and can be freed.
  46892. * Now allocate missing structures.
  46893. */
  46894. if( ssl->transform_negotiate == NULL )
  46895. {
  46896. ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
  46897. }
  46898. if( ssl->session_negotiate == NULL )
  46899. {
  46900. ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
  46901. }
  46902. if( ssl->handshake == NULL )
  46903. {
  46904. ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
  46905. }
  46906. /* All pointers should exist and can be directly freed without issue */
  46907. if( ssl->handshake == NULL ||
  46908. ssl->transform_negotiate == NULL ||
  46909. ssl->session_negotiate == NULL )
  46910. {
  46911. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
  46912. mbedtls_free( ssl->handshake );
  46913. mbedtls_free( ssl->transform_negotiate );
  46914. mbedtls_free( ssl->session_negotiate );
  46915. ssl->handshake = NULL;
  46916. ssl->transform_negotiate = NULL;
  46917. ssl->session_negotiate = NULL;
  46918. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  46919. }
  46920. /* Initialize structures */
  46921. mbedtls_ssl_session_init( ssl->session_negotiate );
  46922. ssl_transform_init( ssl->transform_negotiate );
  46923. ssl_handshake_params_init( ssl->handshake );
  46924. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  46925. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  46926. {
  46927. ssl->handshake->alt_transform_out = ssl->transform_out;
  46928. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
  46929. ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
  46930. else
  46931. ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
  46932. ssl_set_timer( ssl, 0 );
  46933. }
  46934. #endif
  46935. return( 0 );
  46936. }
  46937. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
  46938. /* Dummy cookie callbacks for defaults */
  46939. static int ssl_cookie_write_dummy( void *ctx,
  46940. unsigned char **p, unsigned char *end,
  46941. const unsigned char *cli_id, size_t cli_id_len )
  46942. {
  46943. ((void) ctx);
  46944. ((void) p);
  46945. ((void) end);
  46946. ((void) cli_id);
  46947. ((void) cli_id_len);
  46948. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  46949. }
  46950. static int ssl_cookie_check_dummy( void *ctx,
  46951. const unsigned char *cookie, size_t cookie_len,
  46952. const unsigned char *cli_id, size_t cli_id_len )
  46953. {
  46954. ((void) ctx);
  46955. ((void) cookie);
  46956. ((void) cookie_len);
  46957. ((void) cli_id);
  46958. ((void) cli_id_len);
  46959. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  46960. }
  46961. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
  46962. /*
  46963. * Initialize an SSL context
  46964. */
  46965. void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
  46966. {
  46967. memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
  46968. }
  46969. /*
  46970. * Setup an SSL context
  46971. */
  46972. int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
  46973. const mbedtls_ssl_config *conf )
  46974. {
  46975. int ret;
  46976. const size_t len = MBEDTLS_SSL_BUFFER_LEN;
  46977. ssl->conf = conf;
  46978. /*
  46979. * Prepare base structures
  46980. */
  46981. if( ( ssl-> in_buf = mbedtls_calloc( 1, len ) ) == NULL ||
  46982. ( ssl->out_buf = mbedtls_calloc( 1, len ) ) == NULL )
  46983. {
  46984. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", len ) );
  46985. mbedtls_free( ssl->in_buf );
  46986. ssl->in_buf = NULL;
  46987. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  46988. }
  46989. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  46990. if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  46991. {
  46992. ssl->out_hdr = ssl->out_buf;
  46993. ssl->out_ctr = ssl->out_buf + 3;
  46994. ssl->out_len = ssl->out_buf + 11;
  46995. ssl->out_iv = ssl->out_buf + 13;
  46996. ssl->out_msg = ssl->out_buf + 13;
  46997. ssl->in_hdr = ssl->in_buf;
  46998. ssl->in_ctr = ssl->in_buf + 3;
  46999. ssl->in_len = ssl->in_buf + 11;
  47000. ssl->in_iv = ssl->in_buf + 13;
  47001. ssl->in_msg = ssl->in_buf + 13;
  47002. }
  47003. else
  47004. #endif
  47005. {
  47006. ssl->out_ctr = ssl->out_buf;
  47007. ssl->out_hdr = ssl->out_buf + 8;
  47008. ssl->out_len = ssl->out_buf + 11;
  47009. ssl->out_iv = ssl->out_buf + 13;
  47010. ssl->out_msg = ssl->out_buf + 13;
  47011. ssl->in_ctr = ssl->in_buf;
  47012. ssl->in_hdr = ssl->in_buf + 8;
  47013. ssl->in_len = ssl->in_buf + 11;
  47014. ssl->in_iv = ssl->in_buf + 13;
  47015. ssl->in_msg = ssl->in_buf + 13;
  47016. }
  47017. if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
  47018. return( ret );
  47019. return( 0 );
  47020. }
  47021. /*
  47022. * Reset an initialized and used SSL context for re-use while retaining
  47023. * all application-set variables, function pointers and data.
  47024. *
  47025. * If partial is non-zero, keep data in the input buffer and client ID.
  47026. * (Use when a DTLS client reconnects from the same port.)
  47027. */
  47028. static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
  47029. {
  47030. int ret;
  47031. ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
  47032. /* Cancel any possibly running timer */
  47033. ssl_set_timer( ssl, 0 );
  47034. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  47035. ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
  47036. ssl->renego_records_seen = 0;
  47037. ssl->verify_data_len = 0;
  47038. memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
  47039. memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
  47040. #endif
  47041. ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
  47042. ssl->in_offt = NULL;
  47043. ssl->in_msg = ssl->in_buf + 13;
  47044. ssl->in_msgtype = 0;
  47045. ssl->in_msglen = 0;
  47046. if( partial == 0 )
  47047. ssl->in_left = 0;
  47048. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  47049. ssl->next_record_offset = 0;
  47050. ssl->in_epoch = 0;
  47051. #endif
  47052. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  47053. ssl_dtls_replay_reset( ssl );
  47054. #endif
  47055. ssl->in_hslen = 0;
  47056. ssl->nb_zero = 0;
  47057. ssl->keep_current_message = 0;
  47058. ssl->out_msg = ssl->out_buf + 13;
  47059. ssl->out_msgtype = 0;
  47060. ssl->out_msglen = 0;
  47061. ssl->out_left = 0;
  47062. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  47063. if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
  47064. ssl->split_done = 0;
  47065. #endif
  47066. ssl->transform_in = NULL;
  47067. ssl->transform_out = NULL;
  47068. memset( ssl->out_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
  47069. if( partial == 0 )
  47070. memset( ssl->in_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
  47071. #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
  47072. if( mbedtls_ssl_hw_record_reset != NULL )
  47073. {
  47074. MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
  47075. if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
  47076. {
  47077. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
  47078. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  47079. }
  47080. }
  47081. #endif
  47082. if( ssl->transform )
  47083. {
  47084. mbedtls_ssl_transform_free( ssl->transform );
  47085. mbedtls_free( ssl->transform );
  47086. ssl->transform = NULL;
  47087. }
  47088. if( ssl->session )
  47089. {
  47090. mbedtls_ssl_session_free( ssl->session );
  47091. mbedtls_free( ssl->session );
  47092. ssl->session = NULL;
  47093. }
  47094. #if defined(MBEDTLS_SSL_ALPN)
  47095. ssl->alpn_chosen = NULL;
  47096. #endif
  47097. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
  47098. if( partial == 0 )
  47099. {
  47100. mbedtls_free( ssl->cli_id );
  47101. ssl->cli_id = NULL;
  47102. ssl->cli_id_len = 0;
  47103. }
  47104. #endif
  47105. if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
  47106. return( ret );
  47107. return( 0 );
  47108. }
  47109. /*
  47110. * Reset an initialized and used SSL context for re-use while retaining
  47111. * all application-set variables, function pointers and data.
  47112. */
  47113. int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
  47114. {
  47115. return( ssl_session_reset_int( ssl, 0 ) );
  47116. }
  47117. /*
  47118. * SSL set accessors
  47119. */
  47120. void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
  47121. {
  47122. conf->endpoint = endpoint;
  47123. }
  47124. void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
  47125. {
  47126. conf->transport = transport;
  47127. }
  47128. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  47129. void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
  47130. {
  47131. conf->anti_replay = mode;
  47132. }
  47133. #endif
  47134. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  47135. void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
  47136. {
  47137. conf->badmac_limit = limit;
  47138. }
  47139. #endif
  47140. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  47141. void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
  47142. {
  47143. conf->hs_timeout_min = min;
  47144. conf->hs_timeout_max = max;
  47145. }
  47146. #endif
  47147. void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
  47148. {
  47149. conf->authmode = authmode;
  47150. }
  47151. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  47152. void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
  47153. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  47154. void *p_vrfy )
  47155. {
  47156. conf->f_vrfy = f_vrfy;
  47157. conf->p_vrfy = p_vrfy;
  47158. }
  47159. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  47160. void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
  47161. int (*f_rng)(void *, unsigned char *, size_t),
  47162. void *p_rng )
  47163. {
  47164. conf->f_rng = f_rng;
  47165. conf->p_rng = p_rng;
  47166. }
  47167. void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
  47168. void (*f_dbg)(void *, int, const char *, int, const char *),
  47169. void *p_dbg )
  47170. {
  47171. conf->f_dbg = f_dbg;
  47172. conf->p_dbg = p_dbg;
  47173. }
  47174. void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
  47175. void *p_bio,
  47176. mbedtls_ssl_send_t *f_send,
  47177. mbedtls_ssl_recv_t *f_recv,
  47178. mbedtls_ssl_recv_timeout_t *f_recv_timeout )
  47179. {
  47180. ssl->p_bio = p_bio;
  47181. ssl->f_send = f_send;
  47182. ssl->f_recv = f_recv;
  47183. ssl->f_recv_timeout = f_recv_timeout;
  47184. }
  47185. void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
  47186. {
  47187. conf->read_timeout = timeout;
  47188. }
  47189. void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
  47190. void *p_timer,
  47191. mbedtls_ssl_set_timer_t *f_set_timer,
  47192. mbedtls_ssl_get_timer_t *f_get_timer )
  47193. {
  47194. ssl->p_timer = p_timer;
  47195. ssl->f_set_timer = f_set_timer;
  47196. ssl->f_get_timer = f_get_timer;
  47197. /* Make sure we start with no timer running */
  47198. ssl_set_timer( ssl, 0 );
  47199. }
  47200. #if defined(MBEDTLS_SSL_SRV_C)
  47201. void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
  47202. void *p_cache,
  47203. int (*f_get_cache)(void *, mbedtls_ssl_session *),
  47204. int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
  47205. {
  47206. conf->p_cache = p_cache;
  47207. conf->f_get_cache = f_get_cache;
  47208. conf->f_set_cache = f_set_cache;
  47209. }
  47210. #endif /* MBEDTLS_SSL_SRV_C */
  47211. #if defined(MBEDTLS_SSL_CLI_C)
  47212. int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
  47213. {
  47214. int ret;
  47215. if( ssl == NULL ||
  47216. session == NULL ||
  47217. ssl->session_negotiate == NULL ||
  47218. ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
  47219. {
  47220. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  47221. }
  47222. if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
  47223. return( ret );
  47224. ssl->handshake->resume = 1;
  47225. return( 0 );
  47226. }
  47227. #endif /* MBEDTLS_SSL_CLI_C */
  47228. void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
  47229. const int *ciphersuites )
  47230. {
  47231. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
  47232. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
  47233. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
  47234. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
  47235. }
  47236. void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
  47237. const int *ciphersuites,
  47238. int major, int minor )
  47239. {
  47240. if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
  47241. return;
  47242. if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
  47243. return;
  47244. conf->ciphersuite_list[minor] = ciphersuites;
  47245. }
  47246. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  47247. void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
  47248. const mbedtls_x509_crt_profile *profile )
  47249. {
  47250. conf->cert_profile = profile;
  47251. }
  47252. /* Append a new keycert entry to a (possibly empty) list */
  47253. static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
  47254. mbedtls_x509_crt *cert,
  47255. mbedtls_pk_context *key )
  47256. {
  47257. mbedtls_ssl_key_cert *new;
  47258. new = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
  47259. if( new == NULL )
  47260. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  47261. new->cert = cert;
  47262. new->key = key;
  47263. new->next = NULL;
  47264. /* Update head is the list was null, else add to the end */
  47265. if( *head == NULL )
  47266. {
  47267. *head = new;
  47268. }
  47269. else
  47270. {
  47271. mbedtls_ssl_key_cert *cur = *head;
  47272. while( cur->next != NULL )
  47273. cur = cur->next;
  47274. cur->next = new;
  47275. }
  47276. return( 0 );
  47277. }
  47278. int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
  47279. mbedtls_x509_crt *own_cert,
  47280. mbedtls_pk_context *pk_key )
  47281. {
  47282. return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
  47283. }
  47284. void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
  47285. mbedtls_x509_crt *ca_chain,
  47286. mbedtls_x509_crl *ca_crl )
  47287. {
  47288. conf->ca_chain = ca_chain;
  47289. conf->ca_crl = ca_crl;
  47290. }
  47291. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  47292. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  47293. int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
  47294. mbedtls_x509_crt *own_cert,
  47295. mbedtls_pk_context *pk_key )
  47296. {
  47297. return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
  47298. own_cert, pk_key ) );
  47299. }
  47300. void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
  47301. mbedtls_x509_crt *ca_chain,
  47302. mbedtls_x509_crl *ca_crl )
  47303. {
  47304. ssl->handshake->sni_ca_chain = ca_chain;
  47305. ssl->handshake->sni_ca_crl = ca_crl;
  47306. }
  47307. void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
  47308. int authmode )
  47309. {
  47310. ssl->handshake->sni_authmode = authmode;
  47311. }
  47312. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  47313. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  47314. /*
  47315. * Set EC J-PAKE password for current handshake
  47316. */
  47317. int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
  47318. const unsigned char *pw,
  47319. size_t pw_len )
  47320. {
  47321. mbedtls_ecjpake_role role;
  47322. if( ssl->handshake == NULL || ssl->conf == NULL )
  47323. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  47324. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
  47325. role = MBEDTLS_ECJPAKE_SERVER;
  47326. else
  47327. role = MBEDTLS_ECJPAKE_CLIENT;
  47328. return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
  47329. role,
  47330. MBEDTLS_MD_SHA256,
  47331. MBEDTLS_ECP_DP_SECP256R1,
  47332. pw, pw_len ) );
  47333. }
  47334. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  47335. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  47336. int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
  47337. const unsigned char *psk, size_t psk_len,
  47338. const unsigned char *psk_identity, size_t psk_identity_len )
  47339. {
  47340. if( psk == NULL || psk_identity == NULL )
  47341. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  47342. if( psk_len > MBEDTLS_PSK_MAX_LEN )
  47343. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  47344. /* Identity len will be encoded on two bytes */
  47345. if( ( psk_identity_len >> 16 ) != 0 ||
  47346. psk_identity_len > MBEDTLS_SSL_MAX_CONTENT_LEN )
  47347. {
  47348. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  47349. }
  47350. if( conf->psk != NULL )
  47351. {
  47352. mbedtls_zeroize( conf->psk, conf->psk_len );
  47353. mbedtls_free( conf->psk );
  47354. conf->psk = NULL;
  47355. conf->psk_len = 0;
  47356. }
  47357. if( conf->psk_identity != NULL )
  47358. {
  47359. mbedtls_free( conf->psk_identity );
  47360. conf->psk_identity = NULL;
  47361. conf->psk_identity_len = 0;
  47362. }
  47363. if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
  47364. ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
  47365. {
  47366. mbedtls_free( conf->psk );
  47367. mbedtls_free( conf->psk_identity );
  47368. conf->psk = NULL;
  47369. conf->psk_identity = NULL;
  47370. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  47371. }
  47372. conf->psk_len = psk_len;
  47373. conf->psk_identity_len = psk_identity_len;
  47374. memcpy( conf->psk, psk, conf->psk_len );
  47375. memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
  47376. return( 0 );
  47377. }
  47378. int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
  47379. const unsigned char *psk, size_t psk_len )
  47380. {
  47381. if( psk == NULL || ssl->handshake == NULL )
  47382. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  47383. if( psk_len > MBEDTLS_PSK_MAX_LEN )
  47384. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  47385. if( ssl->handshake->psk != NULL )
  47386. {
  47387. mbedtls_zeroize( ssl->handshake->psk, ssl->handshake->psk_len );
  47388. mbedtls_free( ssl->handshake->psk );
  47389. ssl->handshake->psk_len = 0;
  47390. }
  47391. if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
  47392. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  47393. ssl->handshake->psk_len = psk_len;
  47394. memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
  47395. return( 0 );
  47396. }
  47397. void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
  47398. int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
  47399. size_t),
  47400. void *p_psk )
  47401. {
  47402. conf->f_psk = f_psk;
  47403. conf->p_psk = p_psk;
  47404. }
  47405. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  47406. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
  47407. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  47408. int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
  47409. {
  47410. int ret;
  47411. if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
  47412. ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
  47413. {
  47414. mbedtls_mpi_free( &conf->dhm_P );
  47415. mbedtls_mpi_free( &conf->dhm_G );
  47416. return( ret );
  47417. }
  47418. return( 0 );
  47419. }
  47420. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  47421. int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
  47422. const unsigned char *dhm_P, size_t P_len,
  47423. const unsigned char *dhm_G, size_t G_len )
  47424. {
  47425. int ret;
  47426. if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
  47427. ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
  47428. {
  47429. mbedtls_mpi_free( &conf->dhm_P );
  47430. mbedtls_mpi_free( &conf->dhm_G );
  47431. return( ret );
  47432. }
  47433. return( 0 );
  47434. }
  47435. int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
  47436. {
  47437. int ret;
  47438. if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
  47439. ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
  47440. {
  47441. mbedtls_mpi_free( &conf->dhm_P );
  47442. mbedtls_mpi_free( &conf->dhm_G );
  47443. return( ret );
  47444. }
  47445. return( 0 );
  47446. }
  47447. #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
  47448. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
  47449. /*
  47450. * Set the minimum length for Diffie-Hellman parameters
  47451. */
  47452. void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
  47453. unsigned int bitlen )
  47454. {
  47455. conf->dhm_min_bitlen = bitlen;
  47456. }
  47457. #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
  47458. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  47459. /*
  47460. * Set allowed/preferred hashes for handshake signatures
  47461. */
  47462. void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
  47463. const int *hashes )
  47464. {
  47465. conf->sig_hashes = hashes;
  47466. }
  47467. #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
  47468. #if defined(MBEDTLS_ECP_C)
  47469. /*
  47470. * Set the allowed elliptic curves
  47471. */
  47472. void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
  47473. const mbedtls_ecp_group_id *curve_list )
  47474. {
  47475. conf->curve_list = curve_list;
  47476. }
  47477. #endif /* MBEDTLS_ECP_C */
  47478. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  47479. int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
  47480. {
  47481. /* Initialize to suppress unnecessary compiler warning */
  47482. size_t hostname_len = 0;
  47483. /* Check if new hostname is valid before
  47484. * making any change to current one */
  47485. if( hostname != NULL )
  47486. {
  47487. hostname_len = strlen( hostname );
  47488. if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
  47489. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  47490. }
  47491. /* Now it's clear that we will overwrite the old hostname,
  47492. * so we can free it safely */
  47493. if( ssl->hostname != NULL )
  47494. {
  47495. mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
  47496. mbedtls_free( ssl->hostname );
  47497. }
  47498. /* Passing NULL as hostname shall clear the old one */
  47499. if( hostname == NULL )
  47500. {
  47501. ssl->hostname = NULL;
  47502. }
  47503. else
  47504. {
  47505. ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
  47506. if( ssl->hostname == NULL )
  47507. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  47508. memcpy( ssl->hostname, hostname, hostname_len );
  47509. ssl->hostname[hostname_len] = '\0';
  47510. }
  47511. return( 0 );
  47512. }
  47513. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  47514. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  47515. void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
  47516. int (*f_sni)(void *, mbedtls_ssl_context *,
  47517. const unsigned char *, size_t),
  47518. void *p_sni )
  47519. {
  47520. conf->f_sni = f_sni;
  47521. conf->p_sni = p_sni;
  47522. }
  47523. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  47524. #if defined(MBEDTLS_SSL_ALPN)
  47525. int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
  47526. {
  47527. size_t cur_len, tot_len;
  47528. const char **p;
  47529. /*
  47530. * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
  47531. * MUST NOT be truncated."
  47532. * We check lengths now rather than later.
  47533. */
  47534. tot_len = 0;
  47535. for( p = protos; *p != NULL; p++ )
  47536. {
  47537. cur_len = strlen( *p );
  47538. tot_len += cur_len;
  47539. if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
  47540. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  47541. }
  47542. conf->alpn_list = protos;
  47543. return( 0 );
  47544. }
  47545. const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
  47546. {
  47547. return( ssl->alpn_chosen );
  47548. }
  47549. #endif /* MBEDTLS_SSL_ALPN */
  47550. void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
  47551. {
  47552. conf->max_major_ver = major;
  47553. conf->max_minor_ver = minor;
  47554. }
  47555. void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
  47556. {
  47557. conf->min_major_ver = major;
  47558. conf->min_minor_ver = minor;
  47559. }
  47560. #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
  47561. void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
  47562. {
  47563. conf->fallback = fallback;
  47564. }
  47565. #endif
  47566. #if defined(MBEDTLS_SSL_SRV_C)
  47567. void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
  47568. char cert_req_ca_list )
  47569. {
  47570. conf->cert_req_ca_list = cert_req_ca_list;
  47571. }
  47572. #endif
  47573. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  47574. void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
  47575. {
  47576. conf->encrypt_then_mac = etm;
  47577. }
  47578. #endif
  47579. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  47580. void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
  47581. {
  47582. conf->extended_ms = ems;
  47583. }
  47584. #endif
  47585. #if defined(MBEDTLS_ARC4_C)
  47586. void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
  47587. {
  47588. conf->arc4_disabled = arc4;
  47589. }
  47590. #endif
  47591. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  47592. int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
  47593. {
  47594. if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
  47595. mfl_code_to_length[mfl_code] > MBEDTLS_SSL_MAX_CONTENT_LEN )
  47596. {
  47597. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  47598. }
  47599. conf->mfl_code = mfl_code;
  47600. return( 0 );
  47601. }
  47602. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  47603. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  47604. void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
  47605. {
  47606. conf->trunc_hmac = truncate;
  47607. }
  47608. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  47609. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  47610. void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
  47611. {
  47612. conf->cbc_record_splitting = split;
  47613. }
  47614. #endif
  47615. void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
  47616. {
  47617. conf->allow_legacy_renegotiation = allow_legacy;
  47618. }
  47619. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  47620. void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
  47621. {
  47622. conf->disable_renegotiation = renegotiation;
  47623. }
  47624. void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
  47625. {
  47626. conf->renego_max_records = max_records;
  47627. }
  47628. void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
  47629. const unsigned char period[8] )
  47630. {
  47631. memcpy( conf->renego_period, period, 8 );
  47632. }
  47633. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  47634. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  47635. #if defined(MBEDTLS_SSL_CLI_C)
  47636. void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
  47637. {
  47638. conf->session_tickets = use_tickets;
  47639. }
  47640. #endif
  47641. #if defined(MBEDTLS_SSL_SRV_C)
  47642. void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
  47643. mbedtls_ssl_ticket_write_t *f_ticket_write,
  47644. mbedtls_ssl_ticket_parse_t *f_ticket_parse,
  47645. void *p_ticket )
  47646. {
  47647. conf->f_ticket_write = f_ticket_write;
  47648. conf->f_ticket_parse = f_ticket_parse;
  47649. conf->p_ticket = p_ticket;
  47650. }
  47651. #endif
  47652. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  47653. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  47654. void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
  47655. mbedtls_ssl_export_keys_t *f_export_keys,
  47656. void *p_export_keys )
  47657. {
  47658. conf->f_export_keys = f_export_keys;
  47659. conf->p_export_keys = p_export_keys;
  47660. }
  47661. #endif
  47662. /*
  47663. * SSL get accessors
  47664. */
  47665. size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
  47666. {
  47667. return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
  47668. }
  47669. uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
  47670. {
  47671. if( ssl->session != NULL )
  47672. return( ssl->session->verify_result );
  47673. if( ssl->session_negotiate != NULL )
  47674. return( ssl->session_negotiate->verify_result );
  47675. return( 0xFFFFFFFF );
  47676. }
  47677. const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
  47678. {
  47679. if( ssl == NULL || ssl->session == NULL )
  47680. return( NULL );
  47681. return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
  47682. }
  47683. const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
  47684. {
  47685. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  47686. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  47687. {
  47688. switch( ssl->minor_ver )
  47689. {
  47690. case MBEDTLS_SSL_MINOR_VERSION_2:
  47691. return( "DTLSv1.0" );
  47692. case MBEDTLS_SSL_MINOR_VERSION_3:
  47693. return( "DTLSv1.2" );
  47694. default:
  47695. return( "unknown (DTLS)" );
  47696. }
  47697. }
  47698. #endif
  47699. switch( ssl->minor_ver )
  47700. {
  47701. case MBEDTLS_SSL_MINOR_VERSION_0:
  47702. return( "SSLv3.0" );
  47703. case MBEDTLS_SSL_MINOR_VERSION_1:
  47704. return( "TLSv1.0" );
  47705. case MBEDTLS_SSL_MINOR_VERSION_2:
  47706. return( "TLSv1.1" );
  47707. case MBEDTLS_SSL_MINOR_VERSION_3:
  47708. return( "TLSv1.2" );
  47709. default:
  47710. return( "unknown" );
  47711. }
  47712. }
  47713. int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
  47714. {
  47715. size_t transform_expansion;
  47716. const mbedtls_ssl_transform *transform = ssl->transform_out;
  47717. #if defined(MBEDTLS_ZLIB_SUPPORT)
  47718. if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
  47719. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  47720. #endif
  47721. if( transform == NULL )
  47722. return( (int) mbedtls_ssl_hdr_len( ssl ) );
  47723. switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
  47724. {
  47725. case MBEDTLS_MODE_GCM:
  47726. case MBEDTLS_MODE_CCM:
  47727. case MBEDTLS_MODE_STREAM:
  47728. transform_expansion = transform->minlen;
  47729. break;
  47730. case MBEDTLS_MODE_CBC:
  47731. transform_expansion = transform->maclen
  47732. + mbedtls_cipher_get_block_size( &transform->cipher_ctx_enc );
  47733. break;
  47734. default:
  47735. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  47736. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  47737. }
  47738. return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
  47739. }
  47740. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  47741. size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
  47742. {
  47743. size_t max_len;
  47744. /*
  47745. * Assume mfl_code is correct since it was checked when set
  47746. */
  47747. max_len = mfl_code_to_length[ssl->conf->mfl_code];
  47748. /*
  47749. * Check if a smaller max length was negotiated
  47750. */
  47751. if( ssl->session_out != NULL &&
  47752. mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
  47753. {
  47754. max_len = mfl_code_to_length[ssl->session_out->mfl_code];
  47755. }
  47756. return max_len;
  47757. }
  47758. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  47759. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  47760. const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
  47761. {
  47762. if( ssl == NULL || ssl->session == NULL )
  47763. return( NULL );
  47764. return( ssl->session->peer_cert );
  47765. }
  47766. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  47767. #if defined(MBEDTLS_SSL_CLI_C)
  47768. int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
  47769. {
  47770. if( ssl == NULL ||
  47771. dst == NULL ||
  47772. ssl->session == NULL ||
  47773. ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
  47774. {
  47775. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  47776. }
  47777. return( ssl_session_copy( dst, ssl->session ) );
  47778. }
  47779. #endif /* MBEDTLS_SSL_CLI_C */
  47780. /*
  47781. * Perform a single step of the SSL handshake
  47782. */
  47783. int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
  47784. {
  47785. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  47786. if( ssl == NULL || ssl->conf == NULL )
  47787. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  47788. #if defined(MBEDTLS_SSL_CLI_C)
  47789. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
  47790. ret = mbedtls_ssl_handshake_client_step( ssl );
  47791. #endif
  47792. #if defined(MBEDTLS_SSL_SRV_C)
  47793. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
  47794. ret = mbedtls_ssl_handshake_server_step( ssl );
  47795. #endif
  47796. return( ret );
  47797. }
  47798. /*
  47799. * Perform the SSL handshake
  47800. */
  47801. int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
  47802. {
  47803. int ret = 0;
  47804. if( ssl == NULL || ssl->conf == NULL )
  47805. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  47806. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
  47807. while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
  47808. {
  47809. ret = mbedtls_ssl_handshake_step( ssl );
  47810. if( ret != 0 )
  47811. break;
  47812. }
  47813. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
  47814. return( ret );
  47815. }
  47816. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  47817. #if defined(MBEDTLS_SSL_SRV_C)
  47818. /*
  47819. * Write HelloRequest to request renegotiation on server
  47820. */
  47821. static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
  47822. {
  47823. int ret;
  47824. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
  47825. ssl->out_msglen = 4;
  47826. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  47827. ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
  47828. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  47829. {
  47830. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  47831. return( ret );
  47832. }
  47833. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
  47834. return( 0 );
  47835. }
  47836. #endif /* MBEDTLS_SSL_SRV_C */
  47837. /*
  47838. * Actually renegotiate current connection, triggered by either:
  47839. * - any side: calling mbedtls_ssl_renegotiate(),
  47840. * - client: receiving a HelloRequest during mbedtls_ssl_read(),
  47841. * - server: receiving any handshake message on server during mbedtls_ssl_read() after
  47842. * the initial handshake is completed.
  47843. * If the handshake doesn't complete due to waiting for I/O, it will continue
  47844. * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
  47845. */
  47846. static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
  47847. {
  47848. int ret;
  47849. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
  47850. if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
  47851. return( ret );
  47852. /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
  47853. * the ServerHello will have message_seq = 1" */
  47854. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  47855. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  47856. ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
  47857. {
  47858. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
  47859. ssl->handshake->out_msg_seq = 1;
  47860. else
  47861. ssl->handshake->in_msg_seq = 1;
  47862. }
  47863. #endif
  47864. ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
  47865. ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
  47866. if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
  47867. {
  47868. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
  47869. return( ret );
  47870. }
  47871. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
  47872. return( 0 );
  47873. }
  47874. /*
  47875. * Renegotiate current connection on client,
  47876. * or request renegotiation on server
  47877. */
  47878. int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
  47879. {
  47880. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  47881. if( ssl == NULL || ssl->conf == NULL )
  47882. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  47883. #if defined(MBEDTLS_SSL_SRV_C)
  47884. /* On server, just send the request */
  47885. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
  47886. {
  47887. if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
  47888. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  47889. ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
  47890. /* Did we already try/start sending HelloRequest? */
  47891. if( ssl->out_left != 0 )
  47892. return( mbedtls_ssl_flush_output( ssl ) );
  47893. return( ssl_write_hello_request( ssl ) );
  47894. }
  47895. #endif /* MBEDTLS_SSL_SRV_C */
  47896. #if defined(MBEDTLS_SSL_CLI_C)
  47897. /*
  47898. * On client, either start the renegotiation process or,
  47899. * if already in progress, continue the handshake
  47900. */
  47901. if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
  47902. {
  47903. if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
  47904. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  47905. if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
  47906. {
  47907. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
  47908. return( ret );
  47909. }
  47910. }
  47911. else
  47912. {
  47913. if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
  47914. {
  47915. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
  47916. return( ret );
  47917. }
  47918. }
  47919. #endif /* MBEDTLS_SSL_CLI_C */
  47920. return( ret );
  47921. }
  47922. /*
  47923. * Check record counters and renegotiate if they're above the limit.
  47924. */
  47925. static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
  47926. {
  47927. size_t ep_len = ssl_ep_len( ssl );
  47928. int in_ctr_cmp;
  47929. int out_ctr_cmp;
  47930. if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
  47931. ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
  47932. ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
  47933. {
  47934. return( 0 );
  47935. }
  47936. in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
  47937. ssl->conf->renego_period + ep_len, 8 - ep_len );
  47938. out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
  47939. ssl->conf->renego_period + ep_len, 8 - ep_len );
  47940. if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
  47941. {
  47942. return( 0 );
  47943. }
  47944. MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
  47945. return( mbedtls_ssl_renegotiate( ssl ) );
  47946. }
  47947. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  47948. /*
  47949. * Receive application data decrypted from the SSL layer
  47950. */
  47951. int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
  47952. {
  47953. int ret;
  47954. size_t n;
  47955. if( ssl == NULL || ssl->conf == NULL )
  47956. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  47957. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
  47958. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  47959. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  47960. {
  47961. if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
  47962. return( ret );
  47963. if( ssl->handshake != NULL &&
  47964. ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
  47965. {
  47966. if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
  47967. return( ret );
  47968. }
  47969. }
  47970. #endif
  47971. /*
  47972. * Check if renegotiation is necessary and/or handshake is
  47973. * in process. If yes, perform/continue, and fall through
  47974. * if an unexpected packet is received while the client
  47975. * is waiting for the ServerHello.
  47976. *
  47977. * (There is no equivalent to the last condition on
  47978. * the server-side as it is not treated as within
  47979. * a handshake while waiting for the ClientHello
  47980. * after a renegotiation request.)
  47981. */
  47982. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  47983. ret = ssl_check_ctr_renegotiate( ssl );
  47984. if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
  47985. ret != 0 )
  47986. {
  47987. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
  47988. return( ret );
  47989. }
  47990. #endif
  47991. if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
  47992. {
  47993. ret = mbedtls_ssl_handshake( ssl );
  47994. if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
  47995. ret != 0 )
  47996. {
  47997. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
  47998. return( ret );
  47999. }
  48000. }
  48001. /*
  48002. * TODO
  48003. *
  48004. * The logic should be streamlined here:
  48005. *
  48006. * Instead of
  48007. *
  48008. * - Manually checking whether ssl->in_offt is NULL
  48009. * - Fetching a new record if yes
  48010. * - Setting ssl->in_offt if one finds an application record
  48011. * - Resetting keep_current_message after handling the application data
  48012. *
  48013. * one should
  48014. *
  48015. * - Adapt read_record to set ssl->in_offt automatically
  48016. * when a new application data record is processed.
  48017. * - Always call mbedtls_ssl_read_record here.
  48018. *
  48019. * This way, the logic of ssl_read would be much clearer:
  48020. *
  48021. * (1) Always call record layer and see what kind of record is on
  48022. * and have it ready for consumption (in particular, in_offt
  48023. * properly set for application data records).
  48024. * (2) If it's application data (either freshly fetched
  48025. * or something already being partially processed),
  48026. * serve the read request from it.
  48027. * (3) If it's something different from application data,
  48028. * handle it accordingly, e.g. potentially start a
  48029. * renegotiation.
  48030. *
  48031. * This will also remove the need to manually reset
  48032. * ssl->keep_current_message = 0 below.
  48033. *
  48034. */
  48035. if( ssl->in_offt == NULL )
  48036. {
  48037. /* Start timer if not already running */
  48038. if( ssl->f_get_timer != NULL &&
  48039. ssl->f_get_timer( ssl->p_timer ) == -1 )
  48040. {
  48041. ssl_set_timer( ssl, ssl->conf->read_timeout );
  48042. }
  48043. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  48044. {
  48045. if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
  48046. return( 0 );
  48047. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  48048. return( ret );
  48049. }
  48050. if( ssl->in_msglen == 0 &&
  48051. ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
  48052. {
  48053. /*
  48054. * OpenSSL sends empty messages to randomize the IV
  48055. */
  48056. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  48057. {
  48058. if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
  48059. return( 0 );
  48060. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  48061. return( ret );
  48062. }
  48063. }
  48064. if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
  48065. {
  48066. MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
  48067. /*
  48068. * - For client-side, expect SERVER_HELLO_REQUEST.
  48069. * - For server-side, expect CLIENT_HELLO.
  48070. * - Fail (TLS) or silently drop record (DTLS) in other cases.
  48071. */
  48072. #if defined(MBEDTLS_SSL_CLI_C)
  48073. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
  48074. ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
  48075. ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
  48076. {
  48077. MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
  48078. /* With DTLS, drop the packet (probably from last handshake) */
  48079. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  48080. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  48081. return( MBEDTLS_ERR_SSL_WANT_READ );
  48082. #endif
  48083. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  48084. }
  48085. #endif /* MBEDTLS_SSL_CLI_C */
  48086. #if defined(MBEDTLS_SSL_SRV_C)
  48087. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
  48088. ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
  48089. {
  48090. MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
  48091. /* With DTLS, drop the packet (probably from last handshake) */
  48092. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  48093. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  48094. return( MBEDTLS_ERR_SSL_WANT_READ );
  48095. #endif
  48096. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  48097. }
  48098. #endif /* MBEDTLS_SSL_SRV_C */
  48099. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  48100. /* Determine whether renegotiation attempt should be accepted */
  48101. if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
  48102. ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  48103. ssl->conf->allow_legacy_renegotiation ==
  48104. MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
  48105. {
  48106. /*
  48107. * Accept renegotiation request
  48108. */
  48109. /* DTLS clients need to know renego is server-initiated */
  48110. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  48111. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  48112. ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
  48113. {
  48114. ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
  48115. }
  48116. #endif
  48117. ret = ssl_start_renegotiation( ssl );
  48118. if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
  48119. ret != 0 )
  48120. {
  48121. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
  48122. return( ret );
  48123. }
  48124. }
  48125. else
  48126. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  48127. {
  48128. /*
  48129. * Refuse renegotiation
  48130. */
  48131. MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
  48132. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  48133. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  48134. {
  48135. /* SSLv3 does not have a "no_renegotiation" warning, so
  48136. we send a fatal alert and abort the connection. */
  48137. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  48138. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  48139. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  48140. }
  48141. else
  48142. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  48143. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  48144. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  48145. if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
  48146. {
  48147. if( ( ret = mbedtls_ssl_send_alert_message( ssl,
  48148. MBEDTLS_SSL_ALERT_LEVEL_WARNING,
  48149. MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
  48150. {
  48151. return( ret );
  48152. }
  48153. }
  48154. else
  48155. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
  48156. MBEDTLS_SSL_PROTO_TLS1_2 */
  48157. {
  48158. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  48159. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  48160. }
  48161. }
  48162. return( MBEDTLS_ERR_SSL_WANT_READ );
  48163. }
  48164. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  48165. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
  48166. {
  48167. if( ssl->conf->renego_max_records >= 0 )
  48168. {
  48169. if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
  48170. {
  48171. MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
  48172. "but not honored by client" ) );
  48173. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  48174. }
  48175. }
  48176. }
  48177. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  48178. /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
  48179. if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
  48180. {
  48181. MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
  48182. return( MBEDTLS_ERR_SSL_WANT_READ );
  48183. }
  48184. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
  48185. {
  48186. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
  48187. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  48188. }
  48189. ssl->in_offt = ssl->in_msg;
  48190. /* We're going to return something now, cancel timer,
  48191. * except if handshake (renegotiation) is in progress */
  48192. if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
  48193. ssl_set_timer( ssl, 0 );
  48194. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  48195. /* If we requested renego but received AppData, resend HelloRequest.
  48196. * Do it now, after setting in_offt, to avoid taking this branch
  48197. * again if ssl_write_hello_request() returns WANT_WRITE */
  48198. #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
  48199. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
  48200. ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
  48201. {
  48202. if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
  48203. {
  48204. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
  48205. return( ret );
  48206. }
  48207. }
  48208. #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
  48209. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  48210. }
  48211. n = ( len < ssl->in_msglen )
  48212. ? len : ssl->in_msglen;
  48213. memcpy( buf, ssl->in_offt, n );
  48214. ssl->in_msglen -= n;
  48215. if( ssl->in_msglen == 0 )
  48216. {
  48217. /* all bytes consumed */
  48218. ssl->in_offt = NULL;
  48219. ssl->keep_current_message = 0;
  48220. }
  48221. else
  48222. {
  48223. /* more data available */
  48224. ssl->in_offt += n;
  48225. }
  48226. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
  48227. return( (int) n );
  48228. }
  48229. /*
  48230. * Send application data to be encrypted by the SSL layer,
  48231. * taking care of max fragment length and buffer size
  48232. */
  48233. static int ssl_write_real( mbedtls_ssl_context *ssl,
  48234. const unsigned char *buf, size_t len )
  48235. {
  48236. int ret;
  48237. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  48238. size_t max_len = mbedtls_ssl_get_max_frag_len( ssl );
  48239. #else
  48240. size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
  48241. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  48242. if( len > max_len )
  48243. {
  48244. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  48245. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  48246. {
  48247. MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
  48248. "maximum fragment length: %d > %d",
  48249. len, max_len ) );
  48250. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  48251. }
  48252. else
  48253. #endif
  48254. len = max_len;
  48255. }
  48256. if( ssl->out_left != 0 )
  48257. {
  48258. if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
  48259. {
  48260. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
  48261. return( ret );
  48262. }
  48263. }
  48264. else
  48265. {
  48266. ssl->out_msglen = len;
  48267. ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
  48268. memcpy( ssl->out_msg, buf, len );
  48269. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  48270. {
  48271. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  48272. return( ret );
  48273. }
  48274. }
  48275. return( (int) len );
  48276. }
  48277. /*
  48278. * Write application data, doing 1/n-1 splitting if necessary.
  48279. *
  48280. * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
  48281. * then the caller will call us again with the same arguments, so
  48282. * remember whether we already did the split or not.
  48283. */
  48284. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  48285. static int ssl_write_split( mbedtls_ssl_context *ssl,
  48286. const unsigned char *buf, size_t len )
  48287. {
  48288. int ret;
  48289. if( ssl->conf->cbc_record_splitting ==
  48290. MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
  48291. len <= 1 ||
  48292. ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
  48293. mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
  48294. != MBEDTLS_MODE_CBC )
  48295. {
  48296. return( ssl_write_real( ssl, buf, len ) );
  48297. }
  48298. if( ssl->split_done == 0 )
  48299. {
  48300. if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
  48301. return( ret );
  48302. ssl->split_done = 1;
  48303. }
  48304. if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
  48305. return( ret );
  48306. ssl->split_done = 0;
  48307. return( ret + 1 );
  48308. }
  48309. #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
  48310. /*
  48311. * Write application data (public-facing wrapper)
  48312. */
  48313. int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
  48314. {
  48315. int ret;
  48316. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
  48317. if( ssl == NULL || ssl->conf == NULL )
  48318. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  48319. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  48320. if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
  48321. {
  48322. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
  48323. return( ret );
  48324. }
  48325. #endif
  48326. if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
  48327. {
  48328. if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
  48329. {
  48330. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
  48331. return( ret );
  48332. }
  48333. }
  48334. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  48335. ret = ssl_write_split( ssl, buf, len );
  48336. #else
  48337. ret = ssl_write_real( ssl, buf, len );
  48338. #endif
  48339. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
  48340. return( ret );
  48341. }
  48342. /*
  48343. * Notify the peer that the connection is being closed
  48344. */
  48345. int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
  48346. {
  48347. int ret;
  48348. if( ssl == NULL || ssl->conf == NULL )
  48349. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  48350. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
  48351. if( ssl->out_left != 0 )
  48352. return( mbedtls_ssl_flush_output( ssl ) );
  48353. if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
  48354. {
  48355. if( ( ret = mbedtls_ssl_send_alert_message( ssl,
  48356. MBEDTLS_SSL_ALERT_LEVEL_WARNING,
  48357. MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
  48358. {
  48359. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
  48360. return( ret );
  48361. }
  48362. }
  48363. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
  48364. return( 0 );
  48365. }
  48366. void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
  48367. {
  48368. if( transform == NULL )
  48369. return;
  48370. #if defined(MBEDTLS_ZLIB_SUPPORT)
  48371. deflateEnd( &transform->ctx_deflate );
  48372. inflateEnd( &transform->ctx_inflate );
  48373. #endif
  48374. mbedtls_cipher_free( &transform->cipher_ctx_enc );
  48375. mbedtls_cipher_free( &transform->cipher_ctx_dec );
  48376. mbedtls_md_free( &transform->md_ctx_enc );
  48377. mbedtls_md_free( &transform->md_ctx_dec );
  48378. mbedtls_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
  48379. }
  48380. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  48381. static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
  48382. {
  48383. mbedtls_ssl_key_cert *cur = key_cert, *next;
  48384. while( cur != NULL )
  48385. {
  48386. next = cur->next;
  48387. mbedtls_free( cur );
  48388. cur = next;
  48389. }
  48390. }
  48391. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  48392. void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake )
  48393. {
  48394. if( handshake == NULL )
  48395. return;
  48396. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  48397. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  48398. mbedtls_md5_free( &handshake->fin_md5 );
  48399. mbedtls_sha1_free( &handshake->fin_sha1 );
  48400. #endif
  48401. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  48402. #if defined(MBEDTLS_SHA256_C)
  48403. mbedtls_sha256_free( &handshake->fin_sha256 );
  48404. #endif
  48405. #if defined(MBEDTLS_SHA512_C)
  48406. mbedtls_sha512_free( &handshake->fin_sha512 );
  48407. #endif
  48408. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  48409. #if defined(MBEDTLS_DHM_C)
  48410. mbedtls_dhm_free( &handshake->dhm_ctx );
  48411. #endif
  48412. #if defined(MBEDTLS_ECDH_C)
  48413. mbedtls_ecdh_free( &handshake->ecdh_ctx );
  48414. #endif
  48415. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  48416. mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
  48417. #if defined(MBEDTLS_SSL_CLI_C)
  48418. mbedtls_free( handshake->ecjpake_cache );
  48419. handshake->ecjpake_cache = NULL;
  48420. handshake->ecjpake_cache_len = 0;
  48421. #endif
  48422. #endif
  48423. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  48424. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  48425. /* explicit void pointer cast for buggy MS compiler */
  48426. mbedtls_free( (void *) handshake->curves );
  48427. #endif
  48428. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  48429. if( handshake->psk != NULL )
  48430. {
  48431. mbedtls_zeroize( handshake->psk, handshake->psk_len );
  48432. mbedtls_free( handshake->psk );
  48433. }
  48434. #endif
  48435. #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
  48436. defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  48437. /*
  48438. * Free only the linked list wrapper, not the keys themselves
  48439. * since the belong to the SNI callback
  48440. */
  48441. if( handshake->sni_key_cert != NULL )
  48442. {
  48443. mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
  48444. while( cur != NULL )
  48445. {
  48446. next = cur->next;
  48447. mbedtls_free( cur );
  48448. cur = next;
  48449. }
  48450. }
  48451. #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
  48452. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  48453. mbedtls_free( handshake->verify_cookie );
  48454. mbedtls_free( handshake->hs_msg );
  48455. ssl_flight_free( handshake->flight );
  48456. #endif
  48457. mbedtls_zeroize( handshake, sizeof( mbedtls_ssl_handshake_params ) );
  48458. }
  48459. void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
  48460. {
  48461. if( session == NULL )
  48462. return;
  48463. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  48464. if( session->peer_cert != NULL )
  48465. {
  48466. mbedtls_x509_crt_free( session->peer_cert );
  48467. mbedtls_free( session->peer_cert );
  48468. }
  48469. #endif
  48470. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
  48471. mbedtls_free( session->ticket );
  48472. #endif
  48473. mbedtls_zeroize( session, sizeof( mbedtls_ssl_session ) );
  48474. }
  48475. /*
  48476. * Free an SSL context
  48477. */
  48478. void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
  48479. {
  48480. if( ssl == NULL )
  48481. return;
  48482. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
  48483. if( ssl->out_buf != NULL )
  48484. {
  48485. mbedtls_zeroize( ssl->out_buf, MBEDTLS_SSL_BUFFER_LEN );
  48486. mbedtls_free( ssl->out_buf );
  48487. }
  48488. if( ssl->in_buf != NULL )
  48489. {
  48490. mbedtls_zeroize( ssl->in_buf, MBEDTLS_SSL_BUFFER_LEN );
  48491. mbedtls_free( ssl->in_buf );
  48492. }
  48493. #if defined(MBEDTLS_ZLIB_SUPPORT)
  48494. if( ssl->compress_buf != NULL )
  48495. {
  48496. mbedtls_zeroize( ssl->compress_buf, MBEDTLS_SSL_BUFFER_LEN );
  48497. mbedtls_free( ssl->compress_buf );
  48498. }
  48499. #endif
  48500. if( ssl->transform )
  48501. {
  48502. mbedtls_ssl_transform_free( ssl->transform );
  48503. mbedtls_free( ssl->transform );
  48504. }
  48505. if( ssl->handshake )
  48506. {
  48507. mbedtls_ssl_handshake_free( ssl->handshake );
  48508. mbedtls_ssl_transform_free( ssl->transform_negotiate );
  48509. mbedtls_ssl_session_free( ssl->session_negotiate );
  48510. mbedtls_free( ssl->handshake );
  48511. mbedtls_free( ssl->transform_negotiate );
  48512. mbedtls_free( ssl->session_negotiate );
  48513. }
  48514. if( ssl->session )
  48515. {
  48516. mbedtls_ssl_session_free( ssl->session );
  48517. mbedtls_free( ssl->session );
  48518. }
  48519. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  48520. if( ssl->hostname != NULL )
  48521. {
  48522. mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
  48523. mbedtls_free( ssl->hostname );
  48524. }
  48525. #endif
  48526. #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
  48527. if( mbedtls_ssl_hw_record_finish != NULL )
  48528. {
  48529. MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
  48530. mbedtls_ssl_hw_record_finish( ssl );
  48531. }
  48532. #endif
  48533. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
  48534. mbedtls_free( ssl->cli_id );
  48535. #endif
  48536. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
  48537. /* Actually clear after last debug message */
  48538. mbedtls_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
  48539. }
  48540. /*
  48541. * Initialze mbedtls_ssl_config
  48542. */
  48543. void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
  48544. {
  48545. memset( conf, 0, sizeof( mbedtls_ssl_config ) );
  48546. }
  48547. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  48548. static int ssl_preset_default_hashes[] = {
  48549. #if defined(MBEDTLS_SHA512_C)
  48550. MBEDTLS_MD_SHA512,
  48551. MBEDTLS_MD_SHA384,
  48552. #endif
  48553. #if defined(MBEDTLS_SHA256_C)
  48554. MBEDTLS_MD_SHA256,
  48555. MBEDTLS_MD_SHA224,
  48556. #endif
  48557. #if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
  48558. MBEDTLS_MD_SHA1,
  48559. #endif
  48560. MBEDTLS_MD_NONE
  48561. };
  48562. #endif
  48563. static int ssl_preset_suiteb_ciphersuites[] = {
  48564. MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  48565. MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  48566. 0
  48567. };
  48568. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  48569. static int ssl_preset_suiteb_hashes[] = {
  48570. MBEDTLS_MD_SHA256,
  48571. MBEDTLS_MD_SHA384,
  48572. MBEDTLS_MD_NONE
  48573. };
  48574. #endif
  48575. #if defined(MBEDTLS_ECP_C)
  48576. static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
  48577. MBEDTLS_ECP_DP_SECP256R1,
  48578. MBEDTLS_ECP_DP_SECP384R1,
  48579. MBEDTLS_ECP_DP_NONE
  48580. };
  48581. #endif
  48582. /*
  48583. * Load default in mbedtls_ssl_config
  48584. */
  48585. int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
  48586. int endpoint, int transport, int preset )
  48587. {
  48588. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
  48589. int ret;
  48590. #endif
  48591. /* Use the functions here so that they are covered in tests,
  48592. * but otherwise access member directly for efficiency */
  48593. mbedtls_ssl_conf_endpoint( conf, endpoint );
  48594. mbedtls_ssl_conf_transport( conf, transport );
  48595. /*
  48596. * Things that are common to all presets
  48597. */
  48598. #if defined(MBEDTLS_SSL_CLI_C)
  48599. if( endpoint == MBEDTLS_SSL_IS_CLIENT )
  48600. {
  48601. conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
  48602. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  48603. conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
  48604. #endif
  48605. }
  48606. #endif
  48607. #if defined(MBEDTLS_ARC4_C)
  48608. conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
  48609. #endif
  48610. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  48611. conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
  48612. #endif
  48613. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  48614. conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
  48615. #endif
  48616. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  48617. conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
  48618. #endif
  48619. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
  48620. conf->f_cookie_write = ssl_cookie_write_dummy;
  48621. conf->f_cookie_check = ssl_cookie_check_dummy;
  48622. #endif
  48623. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  48624. conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
  48625. #endif
  48626. #if defined(MBEDTLS_SSL_SRV_C)
  48627. conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
  48628. #endif
  48629. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  48630. conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
  48631. conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
  48632. #endif
  48633. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  48634. conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
  48635. memset( conf->renego_period, 0x00, 2 );
  48636. memset( conf->renego_period + 2, 0xFF, 6 );
  48637. #endif
  48638. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
  48639. if( endpoint == MBEDTLS_SSL_IS_SERVER )
  48640. {
  48641. const unsigned char dhm_p[] =
  48642. MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
  48643. const unsigned char dhm_g[] =
  48644. MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
  48645. if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
  48646. dhm_p, sizeof( dhm_p ),
  48647. dhm_g, sizeof( dhm_g ) ) ) != 0 )
  48648. {
  48649. return( ret );
  48650. }
  48651. }
  48652. #endif
  48653. /*
  48654. * Preset-specific defaults
  48655. */
  48656. switch( preset )
  48657. {
  48658. /*
  48659. * NSA Suite B
  48660. */
  48661. case MBEDTLS_SSL_PRESET_SUITEB:
  48662. conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
  48663. conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
  48664. conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
  48665. conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
  48666. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
  48667. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
  48668. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
  48669. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
  48670. ssl_preset_suiteb_ciphersuites;
  48671. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  48672. conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
  48673. #endif
  48674. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  48675. conf->sig_hashes = ssl_preset_suiteb_hashes;
  48676. #endif
  48677. #if defined(MBEDTLS_ECP_C)
  48678. conf->curve_list = ssl_preset_suiteb_curves;
  48679. #endif
  48680. break;
  48681. /*
  48682. * Default
  48683. */
  48684. default:
  48685. conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
  48686. MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
  48687. MBEDTLS_SSL_MIN_MAJOR_VERSION :
  48688. MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
  48689. conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
  48690. MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
  48691. MBEDTLS_SSL_MIN_MINOR_VERSION :
  48692. MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
  48693. conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
  48694. conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
  48695. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  48696. if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  48697. conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
  48698. #endif
  48699. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
  48700. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
  48701. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
  48702. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
  48703. mbedtls_ssl_list_ciphersuites();
  48704. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  48705. conf->cert_profile = &mbedtls_x509_crt_profile_default;
  48706. #endif
  48707. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  48708. conf->sig_hashes = ssl_preset_default_hashes;
  48709. #endif
  48710. #if defined(MBEDTLS_ECP_C)
  48711. conf->curve_list = mbedtls_ecp_grp_id_list();
  48712. #endif
  48713. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
  48714. conf->dhm_min_bitlen = 1024;
  48715. #endif
  48716. }
  48717. return( 0 );
  48718. }
  48719. /*
  48720. * Free mbedtls_ssl_config
  48721. */
  48722. void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
  48723. {
  48724. #if defined(MBEDTLS_DHM_C)
  48725. mbedtls_mpi_free( &conf->dhm_P );
  48726. mbedtls_mpi_free( &conf->dhm_G );
  48727. #endif
  48728. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  48729. if( conf->psk != NULL )
  48730. {
  48731. mbedtls_zeroize( conf->psk, conf->psk_len );
  48732. mbedtls_zeroize( conf->psk_identity, conf->psk_identity_len );
  48733. mbedtls_free( conf->psk );
  48734. mbedtls_free( conf->psk_identity );
  48735. conf->psk_len = 0;
  48736. conf->psk_identity_len = 0;
  48737. }
  48738. #endif
  48739. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  48740. ssl_key_cert_free( conf->key_cert );
  48741. #endif
  48742. mbedtls_zeroize( conf, sizeof( mbedtls_ssl_config ) );
  48743. }
  48744. #if defined(MBEDTLS_PK_C) && \
  48745. ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
  48746. /*
  48747. * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
  48748. */
  48749. unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
  48750. {
  48751. #if defined(MBEDTLS_RSA_C)
  48752. if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
  48753. return( MBEDTLS_SSL_SIG_RSA );
  48754. #endif
  48755. #if defined(MBEDTLS_ECDSA_C)
  48756. if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
  48757. return( MBEDTLS_SSL_SIG_ECDSA );
  48758. #endif
  48759. return( MBEDTLS_SSL_SIG_ANON );
  48760. }
  48761. unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
  48762. {
  48763. switch( type ) {
  48764. case MBEDTLS_PK_RSA:
  48765. return( MBEDTLS_SSL_SIG_RSA );
  48766. case MBEDTLS_PK_ECDSA:
  48767. case MBEDTLS_PK_ECKEY:
  48768. return( MBEDTLS_SSL_SIG_ECDSA );
  48769. default:
  48770. return( MBEDTLS_SSL_SIG_ANON );
  48771. }
  48772. }
  48773. mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
  48774. {
  48775. switch( sig )
  48776. {
  48777. #if defined(MBEDTLS_RSA_C)
  48778. case MBEDTLS_SSL_SIG_RSA:
  48779. return( MBEDTLS_PK_RSA );
  48780. #endif
  48781. #if defined(MBEDTLS_ECDSA_C)
  48782. case MBEDTLS_SSL_SIG_ECDSA:
  48783. return( MBEDTLS_PK_ECDSA );
  48784. #endif
  48785. default:
  48786. return( MBEDTLS_PK_NONE );
  48787. }
  48788. }
  48789. #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
  48790. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  48791. defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  48792. /* Find an entry in a signature-hash set matching a given hash algorithm. */
  48793. mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
  48794. mbedtls_pk_type_t sig_alg )
  48795. {
  48796. switch( sig_alg )
  48797. {
  48798. case MBEDTLS_PK_RSA:
  48799. return( set->rsa );
  48800. case MBEDTLS_PK_ECDSA:
  48801. return( set->ecdsa );
  48802. default:
  48803. return( MBEDTLS_MD_NONE );
  48804. }
  48805. }
  48806. /* Add a signature-hash-pair to a signature-hash set */
  48807. void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
  48808. mbedtls_pk_type_t sig_alg,
  48809. mbedtls_md_type_t md_alg )
  48810. {
  48811. switch( sig_alg )
  48812. {
  48813. case MBEDTLS_PK_RSA:
  48814. if( set->rsa == MBEDTLS_MD_NONE )
  48815. set->rsa = md_alg;
  48816. break;
  48817. case MBEDTLS_PK_ECDSA:
  48818. if( set->ecdsa == MBEDTLS_MD_NONE )
  48819. set->ecdsa = md_alg;
  48820. break;
  48821. default:
  48822. break;
  48823. }
  48824. }
  48825. /* Allow exactly one hash algorithm for each signature. */
  48826. void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
  48827. mbedtls_md_type_t md_alg )
  48828. {
  48829. set->rsa = md_alg;
  48830. set->ecdsa = md_alg;
  48831. }
  48832. #endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
  48833. MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
  48834. /*
  48835. * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
  48836. */
  48837. mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
  48838. {
  48839. switch( hash )
  48840. {
  48841. #if defined(MBEDTLS_MD5_C)
  48842. case MBEDTLS_SSL_HASH_MD5:
  48843. return( MBEDTLS_MD_MD5 );
  48844. #endif
  48845. #if defined(MBEDTLS_SHA1_C)
  48846. case MBEDTLS_SSL_HASH_SHA1:
  48847. return( MBEDTLS_MD_SHA1 );
  48848. #endif
  48849. #if defined(MBEDTLS_SHA256_C)
  48850. case MBEDTLS_SSL_HASH_SHA224:
  48851. return( MBEDTLS_MD_SHA224 );
  48852. case MBEDTLS_SSL_HASH_SHA256:
  48853. return( MBEDTLS_MD_SHA256 );
  48854. #endif
  48855. #if defined(MBEDTLS_SHA512_C)
  48856. case MBEDTLS_SSL_HASH_SHA384:
  48857. return( MBEDTLS_MD_SHA384 );
  48858. case MBEDTLS_SSL_HASH_SHA512:
  48859. return( MBEDTLS_MD_SHA512 );
  48860. #endif
  48861. default:
  48862. return( MBEDTLS_MD_NONE );
  48863. }
  48864. }
  48865. /*
  48866. * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
  48867. */
  48868. unsigned char mbedtls_ssl_hash_from_md_alg( int md )
  48869. {
  48870. switch( md )
  48871. {
  48872. #if defined(MBEDTLS_MD5_C)
  48873. case MBEDTLS_MD_MD5:
  48874. return( MBEDTLS_SSL_HASH_MD5 );
  48875. #endif
  48876. #if defined(MBEDTLS_SHA1_C)
  48877. case MBEDTLS_MD_SHA1:
  48878. return( MBEDTLS_SSL_HASH_SHA1 );
  48879. #endif
  48880. #if defined(MBEDTLS_SHA256_C)
  48881. case MBEDTLS_MD_SHA224:
  48882. return( MBEDTLS_SSL_HASH_SHA224 );
  48883. case MBEDTLS_MD_SHA256:
  48884. return( MBEDTLS_SSL_HASH_SHA256 );
  48885. #endif
  48886. #if defined(MBEDTLS_SHA512_C)
  48887. case MBEDTLS_MD_SHA384:
  48888. return( MBEDTLS_SSL_HASH_SHA384 );
  48889. case MBEDTLS_MD_SHA512:
  48890. return( MBEDTLS_SSL_HASH_SHA512 );
  48891. #endif
  48892. default:
  48893. return( MBEDTLS_SSL_HASH_NONE );
  48894. }
  48895. }
  48896. #if defined(MBEDTLS_ECP_C)
  48897. /*
  48898. * Check if a curve proposed by the peer is in our list.
  48899. * Return 0 if we're willing to use it, -1 otherwise.
  48900. */
  48901. int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
  48902. {
  48903. const mbedtls_ecp_group_id *gid;
  48904. if( ssl->conf->curve_list == NULL )
  48905. return( -1 );
  48906. for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
  48907. if( *gid == grp_id )
  48908. return( 0 );
  48909. return( -1 );
  48910. }
  48911. #endif /* MBEDTLS_ECP_C */
  48912. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  48913. /*
  48914. * Check if a hash proposed by the peer is in our list.
  48915. * Return 0 if we're willing to use it, -1 otherwise.
  48916. */
  48917. int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
  48918. mbedtls_md_type_t md )
  48919. {
  48920. const int *cur;
  48921. if( ssl->conf->sig_hashes == NULL )
  48922. return( -1 );
  48923. for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
  48924. if( *cur == (int) md )
  48925. return( 0 );
  48926. return( -1 );
  48927. }
  48928. #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
  48929. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  48930. int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
  48931. const mbedtls_ssl_ciphersuite_t *ciphersuite,
  48932. int cert_endpoint,
  48933. uint32_t *flags )
  48934. {
  48935. int ret = 0;
  48936. #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
  48937. int usage = 0;
  48938. #endif
  48939. #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
  48940. const char *ext_oid;
  48941. size_t ext_len;
  48942. #endif
  48943. #if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
  48944. !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
  48945. ((void) cert);
  48946. ((void) cert_endpoint);
  48947. ((void) flags);
  48948. #endif
  48949. #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
  48950. if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
  48951. {
  48952. /* Server part of the key exchange */
  48953. switch( ciphersuite->key_exchange )
  48954. {
  48955. case MBEDTLS_KEY_EXCHANGE_RSA:
  48956. case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
  48957. usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
  48958. break;
  48959. case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
  48960. case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
  48961. case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
  48962. usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
  48963. break;
  48964. case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
  48965. case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
  48966. usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
  48967. break;
  48968. /* Don't use default: we want warnings when adding new values */
  48969. case MBEDTLS_KEY_EXCHANGE_NONE:
  48970. case MBEDTLS_KEY_EXCHANGE_PSK:
  48971. case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
  48972. case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
  48973. case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
  48974. usage = 0;
  48975. }
  48976. }
  48977. else
  48978. {
  48979. /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
  48980. usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
  48981. }
  48982. if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
  48983. {
  48984. *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
  48985. ret = -1;
  48986. }
  48987. #else
  48988. ((void) ciphersuite);
  48989. #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
  48990. #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
  48991. if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
  48992. {
  48993. ext_oid = MBEDTLS_OID_SERVER_AUTH;
  48994. ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
  48995. }
  48996. else
  48997. {
  48998. ext_oid = MBEDTLS_OID_CLIENT_AUTH;
  48999. ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
  49000. }
  49001. if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
  49002. {
  49003. *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
  49004. ret = -1;
  49005. }
  49006. #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
  49007. return( ret );
  49008. }
  49009. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  49010. /*
  49011. * Convert version numbers to/from wire format
  49012. * and, for DTLS, to/from TLS equivalent.
  49013. *
  49014. * For TLS this is the identity.
  49015. * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
  49016. * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
  49017. * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
  49018. */
  49019. void mbedtls_ssl_write_version( int major, int minor, int transport,
  49020. unsigned char ver[2] )
  49021. {
  49022. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  49023. if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  49024. {
  49025. if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
  49026. --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
  49027. ver[0] = (unsigned char)( 255 - ( major - 2 ) );
  49028. ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
  49029. }
  49030. else
  49031. #else
  49032. ((void) transport);
  49033. #endif
  49034. {
  49035. ver[0] = (unsigned char) major;
  49036. ver[1] = (unsigned char) minor;
  49037. }
  49038. }
  49039. void mbedtls_ssl_read_version( int *major, int *minor, int transport,
  49040. const unsigned char ver[2] )
  49041. {
  49042. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  49043. if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  49044. {
  49045. *major = 255 - ver[0] + 2;
  49046. *minor = 255 - ver[1] + 1;
  49047. if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
  49048. ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
  49049. }
  49050. else
  49051. #else
  49052. ((void) transport);
  49053. #endif
  49054. {
  49055. *major = ver[0];
  49056. *minor = ver[1];
  49057. }
  49058. }
  49059. int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
  49060. {
  49061. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  49062. if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
  49063. return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
  49064. switch( md )
  49065. {
  49066. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
  49067. #if defined(MBEDTLS_MD5_C)
  49068. case MBEDTLS_SSL_HASH_MD5:
  49069. return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
  49070. #endif
  49071. #if defined(MBEDTLS_SHA1_C)
  49072. case MBEDTLS_SSL_HASH_SHA1:
  49073. ssl->handshake->calc_verify = ssl_calc_verify_tls;
  49074. break;
  49075. #endif
  49076. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
  49077. #if defined(MBEDTLS_SHA512_C)
  49078. case MBEDTLS_SSL_HASH_SHA384:
  49079. ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
  49080. break;
  49081. #endif
  49082. #if defined(MBEDTLS_SHA256_C)
  49083. case MBEDTLS_SSL_HASH_SHA256:
  49084. ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
  49085. break;
  49086. #endif
  49087. default:
  49088. return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
  49089. }
  49090. return 0;
  49091. #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
  49092. (void) ssl;
  49093. (void) md;
  49094. return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
  49095. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  49096. }
  49097. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  49098. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  49099. int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
  49100. unsigned char *output,
  49101. unsigned char *data, size_t data_len )
  49102. {
  49103. int ret = 0;
  49104. mbedtls_md5_context mbedtls_md5;
  49105. mbedtls_sha1_context mbedtls_sha1;
  49106. mbedtls_md5_init( &mbedtls_md5 );
  49107. mbedtls_sha1_init( &mbedtls_sha1 );
  49108. /*
  49109. * digitally-signed struct {
  49110. * opaque md5_hash[16];
  49111. * opaque sha_hash[20];
  49112. * };
  49113. *
  49114. * md5_hash
  49115. * MD5(ClientHello.random + ServerHello.random
  49116. * + ServerParams);
  49117. * sha_hash
  49118. * SHA(ClientHello.random + ServerHello.random
  49119. * + ServerParams);
  49120. */
  49121. if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
  49122. {
  49123. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
  49124. goto exit;
  49125. }
  49126. if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
  49127. ssl->handshake->randbytes, 64 ) ) != 0 )
  49128. {
  49129. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
  49130. goto exit;
  49131. }
  49132. if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
  49133. {
  49134. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
  49135. goto exit;
  49136. }
  49137. if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
  49138. {
  49139. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
  49140. goto exit;
  49141. }
  49142. if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
  49143. {
  49144. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
  49145. goto exit;
  49146. }
  49147. if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
  49148. ssl->handshake->randbytes, 64 ) ) != 0 )
  49149. {
  49150. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
  49151. goto exit;
  49152. }
  49153. if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
  49154. data_len ) ) != 0 )
  49155. {
  49156. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
  49157. goto exit;
  49158. }
  49159. if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
  49160. output + 16 ) ) != 0 )
  49161. {
  49162. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
  49163. goto exit;
  49164. }
  49165. exit:
  49166. mbedtls_md5_free( &mbedtls_md5 );
  49167. mbedtls_sha1_free( &mbedtls_sha1 );
  49168. if( ret != 0 )
  49169. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  49170. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  49171. return( ret );
  49172. }
  49173. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  49174. MBEDTLS_SSL_PROTO_TLS1_1 */
  49175. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  49176. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  49177. int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
  49178. unsigned char *output,
  49179. unsigned char *data, size_t data_len,
  49180. mbedtls_md_type_t md_alg )
  49181. {
  49182. int ret = 0;
  49183. mbedtls_md_context_t ctx;
  49184. const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
  49185. mbedtls_md_init( &ctx );
  49186. /*
  49187. * digitally-signed struct {
  49188. * opaque client_random[32];
  49189. * opaque server_random[32];
  49190. * ServerDHParams params;
  49191. * };
  49192. */
  49193. if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
  49194. {
  49195. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
  49196. goto exit;
  49197. }
  49198. if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
  49199. {
  49200. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
  49201. goto exit;
  49202. }
  49203. if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
  49204. {
  49205. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
  49206. goto exit;
  49207. }
  49208. if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
  49209. {
  49210. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
  49211. goto exit;
  49212. }
  49213. if( ( ret = mbedtls_md_finish( &ctx, output ) ) != 0 )
  49214. {
  49215. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
  49216. goto exit;
  49217. }
  49218. exit:
  49219. mbedtls_md_free( &ctx );
  49220. if( ret != 0 )
  49221. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  49222. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  49223. return( ret );
  49224. }
  49225. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
  49226. MBEDTLS_SSL_PROTO_TLS1_2 */
  49227. #endif /* MBEDTLS_SSL_TLS_C */
  49228. /*
  49229. Amalgamated build undefines
  49230. */
  49231. #undef ADD
  49232. #undef BC
  49233. #undef BEFORE_COLON
  49234. #undef F
  49235. #undef F0
  49236. #undef F1
  49237. #undef F2
  49238. #undef F3
  49239. #undef F4
  49240. #undef F5
  49241. #undef FSb
  49242. #undef K
  49243. #undef KK
  49244. #undef P
  49245. #undef R
  49246. #undef ROTR
  49247. #undef S
  49248. #undef S0
  49249. #undef S1
  49250. #undef S2
  49251. #undef S3
  49252. #undef SAFE_SNPRINTF
  49253. #undef SHR
  49254. #undef close
  49255. #undef read
  49256. #undef supported_init
  49257. #undef write
  49258. /********* Start of file library/threading.c ************/
  49259. /*
  49260. * Threading abstraction layer
  49261. *
  49262. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  49263. * SPDX-License-Identifier: Apache-2.0
  49264. *
  49265. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  49266. * not use this file except in compliance with the License.
  49267. * You may obtain a copy of the License at
  49268. *
  49269. * http://www.apache.org/licenses/LICENSE-2.0
  49270. *
  49271. * Unless required by applicable law or agreed to in writing, software
  49272. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  49273. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  49274. * See the License for the specific language governing permissions and
  49275. * limitations under the License.
  49276. *
  49277. * This file is part of mbed TLS (https://tls.mbed.org)
  49278. */
  49279. #if !defined(MBEDTLS_CONFIG_FILE)
  49280. #else
  49281. #endif
  49282. #if defined(MBEDTLS_THREADING_C)
  49283. #if defined(MBEDTLS_THREADING_PTHREAD)
  49284. static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
  49285. {
  49286. if( mutex == NULL )
  49287. return;
  49288. mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
  49289. }
  49290. static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
  49291. {
  49292. if( mutex == NULL || !mutex->is_valid )
  49293. return;
  49294. (void) pthread_mutex_destroy( &mutex->mutex );
  49295. mutex->is_valid = 0;
  49296. }
  49297. static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
  49298. {
  49299. if( mutex == NULL || ! mutex->is_valid )
  49300. return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
  49301. if( pthread_mutex_lock( &mutex->mutex ) != 0 )
  49302. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  49303. return( 0 );
  49304. }
  49305. static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
  49306. {
  49307. if( mutex == NULL || ! mutex->is_valid )
  49308. return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
  49309. if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
  49310. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  49311. return( 0 );
  49312. }
  49313. void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
  49314. void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
  49315. int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
  49316. int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
  49317. /*
  49318. * With phtreads we can statically initialize mutexes
  49319. */
  49320. #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
  49321. #endif /* MBEDTLS_THREADING_PTHREAD */
  49322. #if defined(MBEDTLS_THREADING_ALT)
  49323. static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
  49324. {
  49325. ((void) mutex );
  49326. return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
  49327. }
  49328. static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
  49329. {
  49330. ((void) mutex );
  49331. return;
  49332. }
  49333. void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
  49334. void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
  49335. int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
  49336. int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
  49337. /*
  49338. * Set functions pointers and initialize global mutexes
  49339. */
  49340. void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
  49341. void (*mutex_free)( mbedtls_threading_mutex_t * ),
  49342. int (*mutex_lock)( mbedtls_threading_mutex_t * ),
  49343. int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
  49344. {
  49345. mbedtls_mutex_init = mutex_init;
  49346. mbedtls_mutex_free = mutex_free;
  49347. mbedtls_mutex_lock = mutex_lock;
  49348. mbedtls_mutex_unlock = mutex_unlock;
  49349. mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
  49350. mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
  49351. }
  49352. /*
  49353. * Free global mutexes
  49354. */
  49355. void mbedtls_threading_free_alt( void )
  49356. {
  49357. mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
  49358. mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
  49359. }
  49360. #endif /* MBEDTLS_THREADING_ALT */
  49361. /*
  49362. * Define global mutexes
  49363. */
  49364. #ifndef MUTEX_INIT
  49365. #define MUTEX_INIT
  49366. #endif
  49367. mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
  49368. mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
  49369. #endif /* MBEDTLS_THREADING_C */
  49370. /*
  49371. Amalgamated build undefines
  49372. */
  49373. #undef ADD
  49374. #undef BC
  49375. #undef BEFORE_COLON
  49376. #undef F
  49377. #undef F0
  49378. #undef F1
  49379. #undef F2
  49380. #undef F3
  49381. #undef F4
  49382. #undef F5
  49383. #undef FSb
  49384. #undef K
  49385. #undef KK
  49386. #undef P
  49387. #undef R
  49388. #undef ROTR
  49389. #undef S
  49390. #undef S0
  49391. #undef S1
  49392. #undef S2
  49393. #undef S3
  49394. #undef SAFE_SNPRINTF
  49395. #undef SHR
  49396. #undef close
  49397. #undef read
  49398. #undef supported_init
  49399. #undef write
  49400. /********* Start of file library/timing.c ************/
  49401. /*
  49402. * Portable interface to the CPU cycle counter
  49403. *
  49404. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  49405. * SPDX-License-Identifier: Apache-2.0
  49406. *
  49407. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  49408. * not use this file except in compliance with the License.
  49409. * You may obtain a copy of the License at
  49410. *
  49411. * http://www.apache.org/licenses/LICENSE-2.0
  49412. *
  49413. * Unless required by applicable law or agreed to in writing, software
  49414. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  49415. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  49416. * See the License for the specific language governing permissions and
  49417. * limitations under the License.
  49418. *
  49419. * This file is part of mbed TLS (https://tls.mbed.org)
  49420. */
  49421. #if !defined(MBEDTLS_CONFIG_FILE)
  49422. #else
  49423. #endif
  49424. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_PLATFORM_C)
  49425. #else
  49426. #include <stdio.h>
  49427. #define mbedtls_printf printf
  49428. #endif
  49429. #if defined(MBEDTLS_TIMING_C)
  49430. #if !defined(MBEDTLS_TIMING_ALT)
  49431. #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
  49432. !defined(__APPLE__) && !defined(_WIN32)
  49433. #error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h"
  49434. #endif
  49435. #ifndef asm
  49436. #define asm __asm
  49437. #endif
  49438. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  49439. #include <windows.h>
  49440. #include <winbase.h>
  49441. struct _hr_time
  49442. {
  49443. LARGE_INTEGER start;
  49444. };
  49445. #else
  49446. #include <unistd.h>
  49447. #include <sys/types.h>
  49448. #include <sys/time.h>
  49449. #include <signal.h>
  49450. #include <time.h>
  49451. struct _hr_time
  49452. {
  49453. struct timeval start;
  49454. };
  49455. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  49456. #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
  49457. ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
  49458. #define HAVE_HARDCLOCK
  49459. unsigned long mbedtls_timing_hardclock( void )
  49460. {
  49461. unsigned long tsc;
  49462. __asm rdtsc
  49463. __asm mov [tsc], eax
  49464. return( tsc );
  49465. }
  49466. #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
  49467. ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
  49468. /* some versions of mingw-64 have 32-bit longs even on x84_64 */
  49469. #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
  49470. defined(__GNUC__) && ( defined(__i386__) || ( \
  49471. ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
  49472. #define HAVE_HARDCLOCK
  49473. unsigned long mbedtls_timing_hardclock( void )
  49474. {
  49475. unsigned long lo, hi;
  49476. asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
  49477. return( lo );
  49478. }
  49479. #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
  49480. __GNUC__ && __i386__ */
  49481. #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
  49482. defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
  49483. #define HAVE_HARDCLOCK
  49484. unsigned long mbedtls_timing_hardclock( void )
  49485. {
  49486. unsigned long lo, hi;
  49487. asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
  49488. return( lo | ( hi << 32 ) );
  49489. }
  49490. #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
  49491. __GNUC__ && ( __amd64__ || __x86_64__ ) */
  49492. #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
  49493. defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
  49494. #define HAVE_HARDCLOCK
  49495. unsigned long mbedtls_timing_hardclock( void )
  49496. {
  49497. unsigned long tbl, tbu0, tbu1;
  49498. do
  49499. {
  49500. asm volatile( "mftbu %0" : "=r" (tbu0) );
  49501. asm volatile( "mftb %0" : "=r" (tbl ) );
  49502. asm volatile( "mftbu %0" : "=r" (tbu1) );
  49503. }
  49504. while( tbu0 != tbu1 );
  49505. return( tbl );
  49506. }
  49507. #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
  49508. __GNUC__ && ( __powerpc__ || __ppc__ ) */
  49509. #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
  49510. defined(__GNUC__) && defined(__sparc64__)
  49511. #if defined(__OpenBSD__)
  49512. #warning OpenBSD does not allow access to tick register using software version instead
  49513. #else
  49514. #define HAVE_HARDCLOCK
  49515. unsigned long mbedtls_timing_hardclock( void )
  49516. {
  49517. unsigned long tick;
  49518. asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
  49519. return( tick );
  49520. }
  49521. #endif /* __OpenBSD__ */
  49522. #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
  49523. __GNUC__ && __sparc64__ */
  49524. #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
  49525. defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
  49526. #define HAVE_HARDCLOCK
  49527. unsigned long mbedtls_timing_hardclock( void )
  49528. {
  49529. unsigned long tick;
  49530. asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
  49531. asm volatile( "mov %%g1, %0" : "=r" (tick) );
  49532. return( tick );
  49533. }
  49534. #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
  49535. __GNUC__ && __sparc__ && !__sparc64__ */
  49536. #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
  49537. defined(__GNUC__) && defined(__alpha__)
  49538. #define HAVE_HARDCLOCK
  49539. unsigned long mbedtls_timing_hardclock( void )
  49540. {
  49541. unsigned long cc;
  49542. asm volatile( "rpcc %0" : "=r" (cc) );
  49543. return( cc & 0xFFFFFFFF );
  49544. }
  49545. #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
  49546. __GNUC__ && __alpha__ */
  49547. #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
  49548. defined(__GNUC__) && defined(__ia64__)
  49549. #define HAVE_HARDCLOCK
  49550. unsigned long mbedtls_timing_hardclock( void )
  49551. {
  49552. unsigned long itc;
  49553. asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
  49554. return( itc );
  49555. }
  49556. #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
  49557. __GNUC__ && __ia64__ */
  49558. #if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
  49559. !defined(EFIX64) && !defined(EFI32)
  49560. #define HAVE_HARDCLOCK
  49561. unsigned long mbedtls_timing_hardclock( void )
  49562. {
  49563. LARGE_INTEGER offset;
  49564. QueryPerformanceCounter( &offset );
  49565. return( (unsigned long)( offset.QuadPart ) );
  49566. }
  49567. #endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
  49568. #if !defined(HAVE_HARDCLOCK)
  49569. #define HAVE_HARDCLOCK
  49570. static int hardclock_init = 0;
  49571. static struct timeval tv_init;
  49572. unsigned long mbedtls_timing_hardclock( void )
  49573. {
  49574. struct timeval tv_cur;
  49575. if( hardclock_init == 0 )
  49576. {
  49577. gettimeofday( &tv_init, NULL );
  49578. hardclock_init = 1;
  49579. }
  49580. gettimeofday( &tv_cur, NULL );
  49581. return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
  49582. + ( tv_cur.tv_usec - tv_init.tv_usec ) );
  49583. }
  49584. #endif /* !HAVE_HARDCLOCK */
  49585. volatile int mbedtls_timing_alarmed = 0;
  49586. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  49587. unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
  49588. {
  49589. struct _hr_time *t = (struct _hr_time *) val;
  49590. if( reset )
  49591. {
  49592. QueryPerformanceCounter( &t->start );
  49593. return( 0 );
  49594. }
  49595. else
  49596. {
  49597. unsigned long delta;
  49598. LARGE_INTEGER now, hfreq;
  49599. QueryPerformanceCounter( &now );
  49600. QueryPerformanceFrequency( &hfreq );
  49601. delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul
  49602. / hfreq.QuadPart );
  49603. return( delta );
  49604. }
  49605. }
  49606. /* It's OK to use a global because alarm() is supposed to be global anyway */
  49607. static DWORD alarmMs;
  49608. static DWORD WINAPI TimerProc( LPVOID TimerContext )
  49609. {
  49610. ((void) TimerContext);
  49611. Sleep( alarmMs );
  49612. mbedtls_timing_alarmed = 1;
  49613. return( TRUE );
  49614. }
  49615. void mbedtls_set_alarm( int seconds )
  49616. {
  49617. DWORD ThreadId;
  49618. if( seconds == 0 )
  49619. {
  49620. /* No need to create a thread for this simple case.
  49621. * Also, this shorcut is more reliable at least on MinGW32 */
  49622. mbedtls_timing_alarmed = 1;
  49623. return;
  49624. }
  49625. mbedtls_timing_alarmed = 0;
  49626. alarmMs = seconds * 1000;
  49627. CloseHandle( CreateThread( NULL, 0, TimerProc, NULL, 0, &ThreadId ) );
  49628. }
  49629. #else /* _WIN32 && !EFIX64 && !EFI32 */
  49630. unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
  49631. {
  49632. struct _hr_time *t = (struct _hr_time *) val;
  49633. if( reset )
  49634. {
  49635. gettimeofday( &t->start, NULL );
  49636. return( 0 );
  49637. }
  49638. else
  49639. {
  49640. unsigned long delta;
  49641. struct timeval now;
  49642. gettimeofday( &now, NULL );
  49643. delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
  49644. + ( now.tv_usec - t->start.tv_usec ) / 1000;
  49645. return( delta );
  49646. }
  49647. }
  49648. static void sighandler( int signum )
  49649. {
  49650. mbedtls_timing_alarmed = 1;
  49651. signal( signum, sighandler );
  49652. }
  49653. void mbedtls_set_alarm( int seconds )
  49654. {
  49655. mbedtls_timing_alarmed = 0;
  49656. signal( SIGALRM, sighandler );
  49657. alarm( seconds );
  49658. if( seconds == 0 )
  49659. {
  49660. /* alarm(0) cancelled any previous pending alarm, but the
  49661. handler won't fire, so raise the flag straight away. */
  49662. mbedtls_timing_alarmed = 1;
  49663. }
  49664. }
  49665. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  49666. /*
  49667. * Set delays to watch
  49668. */
  49669. void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
  49670. {
  49671. mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
  49672. ctx->int_ms = int_ms;
  49673. ctx->fin_ms = fin_ms;
  49674. if( fin_ms != 0 )
  49675. (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
  49676. }
  49677. /*
  49678. * Get number of delays expired
  49679. */
  49680. int mbedtls_timing_get_delay( void *data )
  49681. {
  49682. mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
  49683. unsigned long elapsed_ms;
  49684. if( ctx->fin_ms == 0 )
  49685. return( -1 );
  49686. elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
  49687. if( elapsed_ms >= ctx->fin_ms )
  49688. return( 2 );
  49689. if( elapsed_ms >= ctx->int_ms )
  49690. return( 1 );
  49691. return( 0 );
  49692. }
  49693. #endif /* !MBEDTLS_TIMING_ALT */
  49694. #if defined(MBEDTLS_SELF_TEST)
  49695. /*
  49696. * Busy-waits for the given number of milliseconds.
  49697. * Used for testing mbedtls_timing_hardclock.
  49698. */
  49699. static void busy_msleep( unsigned long msec )
  49700. {
  49701. struct mbedtls_timing_hr_time hires;
  49702. unsigned long i = 0; /* for busy-waiting */
  49703. volatile unsigned long j; /* to prevent optimisation */
  49704. (void) mbedtls_timing_get_timer( &hires, 1 );
  49705. while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
  49706. i++;
  49707. j = i;
  49708. (void) j;
  49709. }
  49710. #define FAIL do \
  49711. { \
  49712. if( verbose != 0 ) \
  49713. { \
  49714. mbedtls_printf( "failed at line %d\n", __LINE__ ); \
  49715. mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
  49716. cycles, ratio, millisecs, secs, hardfail, \
  49717. (unsigned long) a, (unsigned long) b ); \
  49718. mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \
  49719. mbedtls_timing_get_timer( &hires, 0 ), \
  49720. mbedtls_timing_get_timer( &ctx.timer, 0 ), \
  49721. mbedtls_timing_get_delay( &ctx ) ); \
  49722. } \
  49723. return( 1 ); \
  49724. } while( 0 )
  49725. /*
  49726. * Checkup routine
  49727. *
  49728. * Warning: this is work in progress, some tests may not be reliable enough
  49729. * yet! False positives may happen.
  49730. */
  49731. int mbedtls_timing_self_test( int verbose )
  49732. {
  49733. unsigned long cycles = 0, ratio = 0;
  49734. unsigned long millisecs = 0, secs = 0;
  49735. int hardfail = 0;
  49736. struct mbedtls_timing_hr_time hires;
  49737. uint32_t a = 0, b = 0;
  49738. mbedtls_timing_delay_context ctx;
  49739. if( verbose != 0 )
  49740. mbedtls_printf( " TIMING tests note: will take some time!\n" );
  49741. if( verbose != 0 )
  49742. mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
  49743. {
  49744. secs = 1;
  49745. (void) mbedtls_timing_get_timer( &hires, 1 );
  49746. mbedtls_set_alarm( (int) secs );
  49747. while( !mbedtls_timing_alarmed )
  49748. ;
  49749. millisecs = mbedtls_timing_get_timer( &hires, 0 );
  49750. /* For some reason on Windows it looks like alarm has an extra delay
  49751. * (maybe related to creating a new thread). Allow some room here. */
  49752. if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
  49753. FAIL;
  49754. }
  49755. if( verbose != 0 )
  49756. mbedtls_printf( "passed\n" );
  49757. if( verbose != 0 )
  49758. mbedtls_printf( " TIMING test #2 (set/get_delay ): " );
  49759. {
  49760. a = 800;
  49761. b = 400;
  49762. mbedtls_timing_set_delay( &ctx, a, a + b ); /* T = 0 */
  49763. busy_msleep( a - a / 4 ); /* T = a - a/4 */
  49764. if( mbedtls_timing_get_delay( &ctx ) != 0 )
  49765. FAIL;
  49766. busy_msleep( a / 4 + b / 4 ); /* T = a + b/4 */
  49767. if( mbedtls_timing_get_delay( &ctx ) != 1 )
  49768. FAIL;
  49769. busy_msleep( b ); /* T = a + b + b/4 */
  49770. if( mbedtls_timing_get_delay( &ctx ) != 2 )
  49771. FAIL;
  49772. }
  49773. mbedtls_timing_set_delay( &ctx, 0, 0 );
  49774. busy_msleep( 200 );
  49775. if( mbedtls_timing_get_delay( &ctx ) != -1 )
  49776. FAIL;
  49777. if( verbose != 0 )
  49778. mbedtls_printf( "passed\n" );
  49779. if( verbose != 0 )
  49780. mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " );
  49781. /*
  49782. * Allow one failure for possible counter wrapping.
  49783. * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
  49784. * since the whole test is about 10ms, it shouldn't happen twice in a row.
  49785. */
  49786. hard_test:
  49787. if( hardfail > 1 )
  49788. {
  49789. if( verbose != 0 )
  49790. mbedtls_printf( "failed (ignored)\n" );
  49791. goto hard_test_done;
  49792. }
  49793. /* Get a reference ratio cycles/ms */
  49794. millisecs = 1;
  49795. cycles = mbedtls_timing_hardclock();
  49796. busy_msleep( millisecs );
  49797. cycles = mbedtls_timing_hardclock() - cycles;
  49798. ratio = cycles / millisecs;
  49799. /* Check that the ratio is mostly constant */
  49800. for( millisecs = 2; millisecs <= 4; millisecs++ )
  49801. {
  49802. cycles = mbedtls_timing_hardclock();
  49803. busy_msleep( millisecs );
  49804. cycles = mbedtls_timing_hardclock() - cycles;
  49805. /* Allow variation up to 20% */
  49806. if( cycles / millisecs < ratio - ratio / 5 ||
  49807. cycles / millisecs > ratio + ratio / 5 )
  49808. {
  49809. hardfail++;
  49810. goto hard_test;
  49811. }
  49812. }
  49813. if( verbose != 0 )
  49814. mbedtls_printf( "passed\n" );
  49815. hard_test_done:
  49816. if( verbose != 0 )
  49817. mbedtls_printf( "\n" );
  49818. return( 0 );
  49819. }
  49820. #endif /* MBEDTLS_SELF_TEST */
  49821. #endif /* MBEDTLS_TIMING_C */
  49822. /*
  49823. Amalgamated build undefines
  49824. */
  49825. #undef ADD
  49826. #undef BC
  49827. #undef BEFORE_COLON
  49828. #undef F
  49829. #undef F0
  49830. #undef F1
  49831. #undef F2
  49832. #undef F3
  49833. #undef F4
  49834. #undef F5
  49835. #undef FSb
  49836. #undef K
  49837. #undef KK
  49838. #undef P
  49839. #undef R
  49840. #undef ROTR
  49841. #undef S
  49842. #undef S0
  49843. #undef S1
  49844. #undef S2
  49845. #undef S3
  49846. #undef SAFE_SNPRINTF
  49847. #undef SHR
  49848. #undef close
  49849. #undef read
  49850. #undef supported_init
  49851. #undef write
  49852. /********* Start of file library/version.c ************/
  49853. /*
  49854. * Version information
  49855. *
  49856. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  49857. * SPDX-License-Identifier: Apache-2.0
  49858. *
  49859. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  49860. * not use this file except in compliance with the License.
  49861. * You may obtain a copy of the License at
  49862. *
  49863. * http://www.apache.org/licenses/LICENSE-2.0
  49864. *
  49865. * Unless required by applicable law or agreed to in writing, software
  49866. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  49867. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  49868. * See the License for the specific language governing permissions and
  49869. * limitations under the License.
  49870. *
  49871. * This file is part of mbed TLS (https://tls.mbed.org)
  49872. */
  49873. #if !defined(MBEDTLS_CONFIG_FILE)
  49874. #else
  49875. #endif
  49876. #if defined(MBEDTLS_VERSION_C)
  49877. #include <string.h>
  49878. unsigned int mbedtls_version_get_number( void )
  49879. {
  49880. return( MBEDTLS_VERSION_NUMBER );
  49881. }
  49882. void mbedtls_version_get_string( char *string )
  49883. {
  49884. memcpy( string, MBEDTLS_VERSION_STRING,
  49885. sizeof( MBEDTLS_VERSION_STRING ) );
  49886. }
  49887. void mbedtls_version_get_string_full( char *string )
  49888. {
  49889. memcpy( string, MBEDTLS_VERSION_STRING_FULL,
  49890. sizeof( MBEDTLS_VERSION_STRING_FULL ) );
  49891. }
  49892. #endif /* MBEDTLS_VERSION_C */
  49893. /*
  49894. Amalgamated build undefines
  49895. */
  49896. #undef ADD
  49897. #undef BC
  49898. #undef BEFORE_COLON
  49899. #undef F
  49900. #undef F0
  49901. #undef F1
  49902. #undef F2
  49903. #undef F3
  49904. #undef F4
  49905. #undef F5
  49906. #undef FSb
  49907. #undef K
  49908. #undef KK
  49909. #undef P
  49910. #undef R
  49911. #undef ROTR
  49912. #undef S
  49913. #undef S0
  49914. #undef S1
  49915. #undef S2
  49916. #undef S3
  49917. #undef SAFE_SNPRINTF
  49918. #undef SHR
  49919. #undef close
  49920. #undef read
  49921. #undef supported_init
  49922. #undef write
  49923. /********* Start of file library/version_features.c ************/
  49924. /*
  49925. * Version feature information
  49926. *
  49927. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  49928. * SPDX-License-Identifier: Apache-2.0
  49929. *
  49930. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  49931. * not use this file except in compliance with the License.
  49932. * You may obtain a copy of the License at
  49933. *
  49934. * http://www.apache.org/licenses/LICENSE-2.0
  49935. *
  49936. * Unless required by applicable law or agreed to in writing, software
  49937. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  49938. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  49939. * See the License for the specific language governing permissions and
  49940. * limitations under the License.
  49941. *
  49942. * This file is part of mbed TLS (https://tls.mbed.org)
  49943. */
  49944. #if !defined(MBEDTLS_CONFIG_FILE)
  49945. #else
  49946. #endif
  49947. #if defined(MBEDTLS_VERSION_C)
  49948. #include <string.h>
  49949. static const char *features[] = {
  49950. #if defined(MBEDTLS_VERSION_FEATURES)
  49951. #if defined(MBEDTLS_HAVE_ASM)
  49952. "MBEDTLS_HAVE_ASM",
  49953. #endif /* MBEDTLS_HAVE_ASM */
  49954. #if defined(MBEDTLS_NO_UDBL_DIVISION)
  49955. "MBEDTLS_NO_UDBL_DIVISION",
  49956. #endif /* MBEDTLS_NO_UDBL_DIVISION */
  49957. #if defined(MBEDTLS_HAVE_SSE2)
  49958. "MBEDTLS_HAVE_SSE2",
  49959. #endif /* MBEDTLS_HAVE_SSE2 */
  49960. #if defined(MBEDTLS_HAVE_TIME)
  49961. "MBEDTLS_HAVE_TIME",
  49962. #endif /* MBEDTLS_HAVE_TIME */
  49963. #if defined(MBEDTLS_HAVE_TIME_DATE)
  49964. "MBEDTLS_HAVE_TIME_DATE",
  49965. #endif /* MBEDTLS_HAVE_TIME_DATE */
  49966. #if defined(MBEDTLS_PLATFORM_MEMORY)
  49967. "MBEDTLS_PLATFORM_MEMORY",
  49968. #endif /* MBEDTLS_PLATFORM_MEMORY */
  49969. #if defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
  49970. "MBEDTLS_PLATFORM_NO_STD_FUNCTIONS",
  49971. #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
  49972. #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
  49973. "MBEDTLS_PLATFORM_EXIT_ALT",
  49974. #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
  49975. #if defined(MBEDTLS_PLATFORM_TIME_ALT)
  49976. "MBEDTLS_PLATFORM_TIME_ALT",
  49977. #endif /* MBEDTLS_PLATFORM_TIME_ALT */
  49978. #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
  49979. "MBEDTLS_PLATFORM_FPRINTF_ALT",
  49980. #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
  49981. #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
  49982. "MBEDTLS_PLATFORM_PRINTF_ALT",
  49983. #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
  49984. #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
  49985. "MBEDTLS_PLATFORM_SNPRINTF_ALT",
  49986. #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  49987. #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
  49988. "MBEDTLS_PLATFORM_NV_SEED_ALT",
  49989. #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
  49990. #if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
  49991. "MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT",
  49992. #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
  49993. #if defined(MBEDTLS_DEPRECATED_WARNING)
  49994. "MBEDTLS_DEPRECATED_WARNING",
  49995. #endif /* MBEDTLS_DEPRECATED_WARNING */
  49996. #if defined(MBEDTLS_DEPRECATED_REMOVED)
  49997. "MBEDTLS_DEPRECATED_REMOVED",
  49998. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  49999. #if defined(MBEDTLS_TIMING_ALT)
  50000. "MBEDTLS_TIMING_ALT",
  50001. #endif /* MBEDTLS_TIMING_ALT */
  50002. #if defined(MBEDTLS_AES_ALT)
  50003. "MBEDTLS_AES_ALT",
  50004. #endif /* MBEDTLS_AES_ALT */
  50005. #if defined(MBEDTLS_ARC4_ALT)
  50006. "MBEDTLS_ARC4_ALT",
  50007. #endif /* MBEDTLS_ARC4_ALT */
  50008. #if defined(MBEDTLS_BLOWFISH_ALT)
  50009. "MBEDTLS_BLOWFISH_ALT",
  50010. #endif /* MBEDTLS_BLOWFISH_ALT */
  50011. #if defined(MBEDTLS_CAMELLIA_ALT)
  50012. "MBEDTLS_CAMELLIA_ALT",
  50013. #endif /* MBEDTLS_CAMELLIA_ALT */
  50014. #if defined(MBEDTLS_CCM_ALT)
  50015. "MBEDTLS_CCM_ALT",
  50016. #endif /* MBEDTLS_CCM_ALT */
  50017. #if defined(MBEDTLS_CMAC_ALT)
  50018. "MBEDTLS_CMAC_ALT",
  50019. #endif /* MBEDTLS_CMAC_ALT */
  50020. #if defined(MBEDTLS_DES_ALT)
  50021. "MBEDTLS_DES_ALT",
  50022. #endif /* MBEDTLS_DES_ALT */
  50023. #if defined(MBEDTLS_DHM_ALT)
  50024. "MBEDTLS_DHM_ALT",
  50025. #endif /* MBEDTLS_DHM_ALT */
  50026. #if defined(MBEDTLS_ECJPAKE_ALT)
  50027. "MBEDTLS_ECJPAKE_ALT",
  50028. #endif /* MBEDTLS_ECJPAKE_ALT */
  50029. #if defined(MBEDTLS_GCM_ALT)
  50030. "MBEDTLS_GCM_ALT",
  50031. #endif /* MBEDTLS_GCM_ALT */
  50032. #if defined(MBEDTLS_MD2_ALT)
  50033. "MBEDTLS_MD2_ALT",
  50034. #endif /* MBEDTLS_MD2_ALT */
  50035. #if defined(MBEDTLS_MD4_ALT)
  50036. "MBEDTLS_MD4_ALT",
  50037. #endif /* MBEDTLS_MD4_ALT */
  50038. #if defined(MBEDTLS_MD5_ALT)
  50039. "MBEDTLS_MD5_ALT",
  50040. #endif /* MBEDTLS_MD5_ALT */
  50041. #if defined(MBEDTLS_RIPEMD160_ALT)
  50042. "MBEDTLS_RIPEMD160_ALT",
  50043. #endif /* MBEDTLS_RIPEMD160_ALT */
  50044. #if defined(MBEDTLS_RSA_ALT)
  50045. "MBEDTLS_RSA_ALT",
  50046. #endif /* MBEDTLS_RSA_ALT */
  50047. #if defined(MBEDTLS_SHA1_ALT)
  50048. "MBEDTLS_SHA1_ALT",
  50049. #endif /* MBEDTLS_SHA1_ALT */
  50050. #if defined(MBEDTLS_SHA256_ALT)
  50051. "MBEDTLS_SHA256_ALT",
  50052. #endif /* MBEDTLS_SHA256_ALT */
  50053. #if defined(MBEDTLS_SHA512_ALT)
  50054. "MBEDTLS_SHA512_ALT",
  50055. #endif /* MBEDTLS_SHA512_ALT */
  50056. #if defined(MBEDTLS_XTEA_ALT)
  50057. "MBEDTLS_XTEA_ALT",
  50058. #endif /* MBEDTLS_XTEA_ALT */
  50059. #if defined(MBEDTLS_ECP_ALT)
  50060. "MBEDTLS_ECP_ALT",
  50061. #endif /* MBEDTLS_ECP_ALT */
  50062. #if defined(MBEDTLS_MD2_PROCESS_ALT)
  50063. "MBEDTLS_MD2_PROCESS_ALT",
  50064. #endif /* MBEDTLS_MD2_PROCESS_ALT */
  50065. #if defined(MBEDTLS_MD4_PROCESS_ALT)
  50066. "MBEDTLS_MD4_PROCESS_ALT",
  50067. #endif /* MBEDTLS_MD4_PROCESS_ALT */
  50068. #if defined(MBEDTLS_MD5_PROCESS_ALT)
  50069. "MBEDTLS_MD5_PROCESS_ALT",
  50070. #endif /* MBEDTLS_MD5_PROCESS_ALT */
  50071. #if defined(MBEDTLS_RIPEMD160_PROCESS_ALT)
  50072. "MBEDTLS_RIPEMD160_PROCESS_ALT",
  50073. #endif /* MBEDTLS_RIPEMD160_PROCESS_ALT */
  50074. #if defined(MBEDTLS_SHA1_PROCESS_ALT)
  50075. "MBEDTLS_SHA1_PROCESS_ALT",
  50076. #endif /* MBEDTLS_SHA1_PROCESS_ALT */
  50077. #if defined(MBEDTLS_SHA256_PROCESS_ALT)
  50078. "MBEDTLS_SHA256_PROCESS_ALT",
  50079. #endif /* MBEDTLS_SHA256_PROCESS_ALT */
  50080. #if defined(MBEDTLS_SHA512_PROCESS_ALT)
  50081. "MBEDTLS_SHA512_PROCESS_ALT",
  50082. #endif /* MBEDTLS_SHA512_PROCESS_ALT */
  50083. #if defined(MBEDTLS_DES_SETKEY_ALT)
  50084. "MBEDTLS_DES_SETKEY_ALT",
  50085. #endif /* MBEDTLS_DES_SETKEY_ALT */
  50086. #if defined(MBEDTLS_DES_CRYPT_ECB_ALT)
  50087. "MBEDTLS_DES_CRYPT_ECB_ALT",
  50088. #endif /* MBEDTLS_DES_CRYPT_ECB_ALT */
  50089. #if defined(MBEDTLS_DES3_CRYPT_ECB_ALT)
  50090. "MBEDTLS_DES3_CRYPT_ECB_ALT",
  50091. #endif /* MBEDTLS_DES3_CRYPT_ECB_ALT */
  50092. #if defined(MBEDTLS_AES_SETKEY_ENC_ALT)
  50093. "MBEDTLS_AES_SETKEY_ENC_ALT",
  50094. #endif /* MBEDTLS_AES_SETKEY_ENC_ALT */
  50095. #if defined(MBEDTLS_AES_SETKEY_DEC_ALT)
  50096. "MBEDTLS_AES_SETKEY_DEC_ALT",
  50097. #endif /* MBEDTLS_AES_SETKEY_DEC_ALT */
  50098. #if defined(MBEDTLS_AES_ENCRYPT_ALT)
  50099. "MBEDTLS_AES_ENCRYPT_ALT",
  50100. #endif /* MBEDTLS_AES_ENCRYPT_ALT */
  50101. #if defined(MBEDTLS_AES_DECRYPT_ALT)
  50102. "MBEDTLS_AES_DECRYPT_ALT",
  50103. #endif /* MBEDTLS_AES_DECRYPT_ALT */
  50104. #if defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
  50105. "MBEDTLS_ECDH_GEN_PUBLIC_ALT",
  50106. #endif /* MBEDTLS_ECDH_GEN_PUBLIC_ALT */
  50107. #if defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
  50108. "MBEDTLS_ECDH_COMPUTE_SHARED_ALT",
  50109. #endif /* MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
  50110. #if defined(MBEDTLS_ECDSA_VERIFY_ALT)
  50111. "MBEDTLS_ECDSA_VERIFY_ALT",
  50112. #endif /* MBEDTLS_ECDSA_VERIFY_ALT */
  50113. #if defined(MBEDTLS_ECDSA_SIGN_ALT)
  50114. "MBEDTLS_ECDSA_SIGN_ALT",
  50115. #endif /* MBEDTLS_ECDSA_SIGN_ALT */
  50116. #if defined(MBEDTLS_ECDSA_GENKEY_ALT)
  50117. "MBEDTLS_ECDSA_GENKEY_ALT",
  50118. #endif /* MBEDTLS_ECDSA_GENKEY_ALT */
  50119. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  50120. "MBEDTLS_ECP_INTERNAL_ALT",
  50121. #endif /* MBEDTLS_ECP_INTERNAL_ALT */
  50122. #if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
  50123. "MBEDTLS_ECP_RANDOMIZE_JAC_ALT",
  50124. #endif /* MBEDTLS_ECP_RANDOMIZE_JAC_ALT */
  50125. #if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
  50126. "MBEDTLS_ECP_ADD_MIXED_ALT",
  50127. #endif /* MBEDTLS_ECP_ADD_MIXED_ALT */
  50128. #if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
  50129. "MBEDTLS_ECP_DOUBLE_JAC_ALT",
  50130. #endif /* MBEDTLS_ECP_DOUBLE_JAC_ALT */
  50131. #if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
  50132. "MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT",
  50133. #endif /* MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT */
  50134. #if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
  50135. "MBEDTLS_ECP_NORMALIZE_JAC_ALT",
  50136. #endif /* MBEDTLS_ECP_NORMALIZE_JAC_ALT */
  50137. #if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
  50138. "MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT",
  50139. #endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */
  50140. #if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
  50141. "MBEDTLS_ECP_RANDOMIZE_MXZ_ALT",
  50142. #endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */
  50143. #if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
  50144. "MBEDTLS_ECP_NORMALIZE_MXZ_ALT",
  50145. #endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */
  50146. #if defined(MBEDTLS_TEST_NULL_ENTROPY)
  50147. "MBEDTLS_TEST_NULL_ENTROPY",
  50148. #endif /* MBEDTLS_TEST_NULL_ENTROPY */
  50149. #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
  50150. "MBEDTLS_ENTROPY_HARDWARE_ALT",
  50151. #endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
  50152. #if defined(MBEDTLS_AES_ROM_TABLES)
  50153. "MBEDTLS_AES_ROM_TABLES",
  50154. #endif /* MBEDTLS_AES_ROM_TABLES */
  50155. #if defined(MBEDTLS_CAMELLIA_SMALL_MEMORY)
  50156. "MBEDTLS_CAMELLIA_SMALL_MEMORY",
  50157. #endif /* MBEDTLS_CAMELLIA_SMALL_MEMORY */
  50158. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  50159. "MBEDTLS_CIPHER_MODE_CBC",
  50160. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  50161. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  50162. "MBEDTLS_CIPHER_MODE_CFB",
  50163. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  50164. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  50165. "MBEDTLS_CIPHER_MODE_CTR",
  50166. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  50167. #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
  50168. "MBEDTLS_CIPHER_NULL_CIPHER",
  50169. #endif /* MBEDTLS_CIPHER_NULL_CIPHER */
  50170. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  50171. "MBEDTLS_CIPHER_PADDING_PKCS7",
  50172. #endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
  50173. #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
  50174. "MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS",
  50175. #endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
  50176. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
  50177. "MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN",
  50178. #endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
  50179. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
  50180. "MBEDTLS_CIPHER_PADDING_ZEROS",
  50181. #endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
  50182. #if defined(MBEDTLS_ENABLE_WEAK_CIPHERSUITES)
  50183. "MBEDTLS_ENABLE_WEAK_CIPHERSUITES",
  50184. #endif /* MBEDTLS_ENABLE_WEAK_CIPHERSUITES */
  50185. #if defined(MBEDTLS_REMOVE_ARC4_CIPHERSUITES)
  50186. "MBEDTLS_REMOVE_ARC4_CIPHERSUITES",
  50187. #endif /* MBEDTLS_REMOVE_ARC4_CIPHERSUITES */
  50188. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
  50189. "MBEDTLS_ECP_DP_SECP192R1_ENABLED",
  50190. #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
  50191. #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
  50192. "MBEDTLS_ECP_DP_SECP224R1_ENABLED",
  50193. #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
  50194. #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
  50195. "MBEDTLS_ECP_DP_SECP256R1_ENABLED",
  50196. #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
  50197. #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
  50198. "MBEDTLS_ECP_DP_SECP384R1_ENABLED",
  50199. #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
  50200. #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
  50201. "MBEDTLS_ECP_DP_SECP521R1_ENABLED",
  50202. #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
  50203. #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
  50204. "MBEDTLS_ECP_DP_SECP192K1_ENABLED",
  50205. #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
  50206. #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
  50207. "MBEDTLS_ECP_DP_SECP224K1_ENABLED",
  50208. #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
  50209. #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
  50210. "MBEDTLS_ECP_DP_SECP256K1_ENABLED",
  50211. #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
  50212. #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
  50213. "MBEDTLS_ECP_DP_BP256R1_ENABLED",
  50214. #endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
  50215. #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
  50216. "MBEDTLS_ECP_DP_BP384R1_ENABLED",
  50217. #endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
  50218. #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
  50219. "MBEDTLS_ECP_DP_BP512R1_ENABLED",
  50220. #endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
  50221. #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
  50222. "MBEDTLS_ECP_DP_CURVE25519_ENABLED",
  50223. #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
  50224. #if defined(MBEDTLS_ECP_NIST_OPTIM)
  50225. "MBEDTLS_ECP_NIST_OPTIM",
  50226. #endif /* MBEDTLS_ECP_NIST_OPTIM */
  50227. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  50228. "MBEDTLS_ECDSA_DETERMINISTIC",
  50229. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  50230. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  50231. "MBEDTLS_KEY_EXCHANGE_PSK_ENABLED",
  50232. #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
  50233. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  50234. "MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED",
  50235. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  50236. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  50237. "MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED",
  50238. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  50239. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  50240. "MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED",
  50241. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  50242. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  50243. "MBEDTLS_KEY_EXCHANGE_RSA_ENABLED",
  50244. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  50245. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
  50246. "MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED",
  50247. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
  50248. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
  50249. "MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED",
  50250. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
  50251. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  50252. "MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED",
  50253. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  50254. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  50255. "MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED",
  50256. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  50257. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED)
  50258. "MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED",
  50259. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED */
  50260. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  50261. "MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED",
  50262. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  50263. #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
  50264. "MBEDTLS_PK_PARSE_EC_EXTENDED",
  50265. #endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
  50266. #if defined(MBEDTLS_ERROR_STRERROR_DUMMY)
  50267. "MBEDTLS_ERROR_STRERROR_DUMMY",
  50268. #endif /* MBEDTLS_ERROR_STRERROR_DUMMY */
  50269. #if defined(MBEDTLS_GENPRIME)
  50270. "MBEDTLS_GENPRIME",
  50271. #endif /* MBEDTLS_GENPRIME */
  50272. #if defined(MBEDTLS_FS_IO)
  50273. "MBEDTLS_FS_IO",
  50274. #endif /* MBEDTLS_FS_IO */
  50275. #if defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
  50276. "MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES",
  50277. #endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
  50278. #if defined(MBEDTLS_NO_PLATFORM_ENTROPY)
  50279. "MBEDTLS_NO_PLATFORM_ENTROPY",
  50280. #endif /* MBEDTLS_NO_PLATFORM_ENTROPY */
  50281. #if defined(MBEDTLS_ENTROPY_FORCE_SHA256)
  50282. "MBEDTLS_ENTROPY_FORCE_SHA256",
  50283. #endif /* MBEDTLS_ENTROPY_FORCE_SHA256 */
  50284. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  50285. "MBEDTLS_ENTROPY_NV_SEED",
  50286. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  50287. #if defined(MBEDTLS_MEMORY_DEBUG)
  50288. "MBEDTLS_MEMORY_DEBUG",
  50289. #endif /* MBEDTLS_MEMORY_DEBUG */
  50290. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  50291. "MBEDTLS_MEMORY_BACKTRACE",
  50292. #endif /* MBEDTLS_MEMORY_BACKTRACE */
  50293. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  50294. "MBEDTLS_PK_RSA_ALT_SUPPORT",
  50295. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  50296. #if defined(MBEDTLS_PKCS1_V15)
  50297. "MBEDTLS_PKCS1_V15",
  50298. #endif /* MBEDTLS_PKCS1_V15 */
  50299. #if defined(MBEDTLS_PKCS1_V21)
  50300. "MBEDTLS_PKCS1_V21",
  50301. #endif /* MBEDTLS_PKCS1_V21 */
  50302. #if defined(MBEDTLS_RSA_NO_CRT)
  50303. "MBEDTLS_RSA_NO_CRT",
  50304. #endif /* MBEDTLS_RSA_NO_CRT */
  50305. #if defined(MBEDTLS_SELF_TEST)
  50306. "MBEDTLS_SELF_TEST",
  50307. #endif /* MBEDTLS_SELF_TEST */
  50308. #if defined(MBEDTLS_SHA256_SMALLER)
  50309. "MBEDTLS_SHA256_SMALLER",
  50310. #endif /* MBEDTLS_SHA256_SMALLER */
  50311. #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
  50312. "MBEDTLS_SSL_ALL_ALERT_MESSAGES",
  50313. #endif /* MBEDTLS_SSL_ALL_ALERT_MESSAGES */
  50314. #if defined(MBEDTLS_SSL_DEBUG_ALL)
  50315. "MBEDTLS_SSL_DEBUG_ALL",
  50316. #endif /* MBEDTLS_SSL_DEBUG_ALL */
  50317. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  50318. "MBEDTLS_SSL_ENCRYPT_THEN_MAC",
  50319. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  50320. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  50321. "MBEDTLS_SSL_EXTENDED_MASTER_SECRET",
  50322. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  50323. #if defined(MBEDTLS_SSL_FALLBACK_SCSV)
  50324. "MBEDTLS_SSL_FALLBACK_SCSV",
  50325. #endif /* MBEDTLS_SSL_FALLBACK_SCSV */
  50326. #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
  50327. "MBEDTLS_SSL_HW_RECORD_ACCEL",
  50328. #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
  50329. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  50330. "MBEDTLS_SSL_CBC_RECORD_SPLITTING",
  50331. #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
  50332. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  50333. "MBEDTLS_SSL_RENEGOTIATION",
  50334. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  50335. #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
  50336. "MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO",
  50337. #endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
  50338. #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
  50339. "MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE",
  50340. #endif /* MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE */
  50341. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  50342. "MBEDTLS_SSL_MAX_FRAGMENT_LENGTH",
  50343. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  50344. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  50345. "MBEDTLS_SSL_PROTO_SSL3",
  50346. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  50347. #if defined(MBEDTLS_SSL_PROTO_TLS1)
  50348. "MBEDTLS_SSL_PROTO_TLS1",
  50349. #endif /* MBEDTLS_SSL_PROTO_TLS1 */
  50350. #if defined(MBEDTLS_SSL_PROTO_TLS1_1)
  50351. "MBEDTLS_SSL_PROTO_TLS1_1",
  50352. #endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
  50353. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  50354. "MBEDTLS_SSL_PROTO_TLS1_2",
  50355. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  50356. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  50357. "MBEDTLS_SSL_PROTO_DTLS",
  50358. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  50359. #if defined(MBEDTLS_SSL_ALPN)
  50360. "MBEDTLS_SSL_ALPN",
  50361. #endif /* MBEDTLS_SSL_ALPN */
  50362. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  50363. "MBEDTLS_SSL_DTLS_ANTI_REPLAY",
  50364. #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
  50365. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  50366. "MBEDTLS_SSL_DTLS_HELLO_VERIFY",
  50367. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  50368. #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
  50369. "MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE",
  50370. #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE */
  50371. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  50372. "MBEDTLS_SSL_DTLS_BADMAC_LIMIT",
  50373. #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
  50374. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  50375. "MBEDTLS_SSL_SESSION_TICKETS",
  50376. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  50377. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  50378. "MBEDTLS_SSL_EXPORT_KEYS",
  50379. #endif /* MBEDTLS_SSL_EXPORT_KEYS */
  50380. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  50381. "MBEDTLS_SSL_SERVER_NAME_INDICATION",
  50382. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  50383. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  50384. "MBEDTLS_SSL_TRUNCATED_HMAC",
  50385. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  50386. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
  50387. "MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT",
  50388. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT */
  50389. #if defined(MBEDTLS_THREADING_ALT)
  50390. "MBEDTLS_THREADING_ALT",
  50391. #endif /* MBEDTLS_THREADING_ALT */
  50392. #if defined(MBEDTLS_THREADING_PTHREAD)
  50393. "MBEDTLS_THREADING_PTHREAD",
  50394. #endif /* MBEDTLS_THREADING_PTHREAD */
  50395. #if defined(MBEDTLS_VERSION_FEATURES)
  50396. "MBEDTLS_VERSION_FEATURES",
  50397. #endif /* MBEDTLS_VERSION_FEATURES */
  50398. #if defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
  50399. "MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3",
  50400. #endif /* MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3 */
  50401. #if defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
  50402. "MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION",
  50403. #endif /* MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION */
  50404. #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
  50405. "MBEDTLS_X509_CHECK_KEY_USAGE",
  50406. #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
  50407. #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
  50408. "MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE",
  50409. #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
  50410. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  50411. "MBEDTLS_X509_RSASSA_PSS_SUPPORT",
  50412. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  50413. #if defined(MBEDTLS_ZLIB_SUPPORT)
  50414. "MBEDTLS_ZLIB_SUPPORT",
  50415. #endif /* MBEDTLS_ZLIB_SUPPORT */
  50416. #if defined(MBEDTLS_AESNI_C)
  50417. "MBEDTLS_AESNI_C",
  50418. #endif /* MBEDTLS_AESNI_C */
  50419. #if defined(MBEDTLS_AES_C)
  50420. "MBEDTLS_AES_C",
  50421. #endif /* MBEDTLS_AES_C */
  50422. #if defined(MBEDTLS_ARC4_C)
  50423. "MBEDTLS_ARC4_C",
  50424. #endif /* MBEDTLS_ARC4_C */
  50425. #if defined(MBEDTLS_ASN1_PARSE_C)
  50426. "MBEDTLS_ASN1_PARSE_C",
  50427. #endif /* MBEDTLS_ASN1_PARSE_C */
  50428. #if defined(MBEDTLS_ASN1_WRITE_C)
  50429. "MBEDTLS_ASN1_WRITE_C",
  50430. #endif /* MBEDTLS_ASN1_WRITE_C */
  50431. #if defined(MBEDTLS_BASE64_C)
  50432. "MBEDTLS_BASE64_C",
  50433. #endif /* MBEDTLS_BASE64_C */
  50434. #if defined(MBEDTLS_BIGNUM_C)
  50435. "MBEDTLS_BIGNUM_C",
  50436. #endif /* MBEDTLS_BIGNUM_C */
  50437. #if defined(MBEDTLS_BLOWFISH_C)
  50438. "MBEDTLS_BLOWFISH_C",
  50439. #endif /* MBEDTLS_BLOWFISH_C */
  50440. #if defined(MBEDTLS_CAMELLIA_C)
  50441. "MBEDTLS_CAMELLIA_C",
  50442. #endif /* MBEDTLS_CAMELLIA_C */
  50443. #if defined(MBEDTLS_CCM_C)
  50444. "MBEDTLS_CCM_C",
  50445. #endif /* MBEDTLS_CCM_C */
  50446. #if defined(MBEDTLS_CERTS_C)
  50447. "MBEDTLS_CERTS_C",
  50448. #endif /* MBEDTLS_CERTS_C */
  50449. #if defined(MBEDTLS_CIPHER_C)
  50450. "MBEDTLS_CIPHER_C",
  50451. #endif /* MBEDTLS_CIPHER_C */
  50452. #if defined(MBEDTLS_CMAC_C)
  50453. "MBEDTLS_CMAC_C",
  50454. #endif /* MBEDTLS_CMAC_C */
  50455. #if defined(MBEDTLS_CTR_DRBG_C)
  50456. "MBEDTLS_CTR_DRBG_C",
  50457. #endif /* MBEDTLS_CTR_DRBG_C */
  50458. #if defined(MBEDTLS_DEBUG_C)
  50459. "MBEDTLS_DEBUG_C",
  50460. #endif /* MBEDTLS_DEBUG_C */
  50461. #if defined(MBEDTLS_DES_C)
  50462. "MBEDTLS_DES_C",
  50463. #endif /* MBEDTLS_DES_C */
  50464. #if defined(MBEDTLS_DHM_C)
  50465. "MBEDTLS_DHM_C",
  50466. #endif /* MBEDTLS_DHM_C */
  50467. #if defined(MBEDTLS_ECDH_C)
  50468. "MBEDTLS_ECDH_C",
  50469. #endif /* MBEDTLS_ECDH_C */
  50470. #if defined(MBEDTLS_ECDSA_C)
  50471. "MBEDTLS_ECDSA_C",
  50472. #endif /* MBEDTLS_ECDSA_C */
  50473. #if defined(MBEDTLS_ECJPAKE_C)
  50474. "MBEDTLS_ECJPAKE_C",
  50475. #endif /* MBEDTLS_ECJPAKE_C */
  50476. #if defined(MBEDTLS_ECP_C)
  50477. "MBEDTLS_ECP_C",
  50478. #endif /* MBEDTLS_ECP_C */
  50479. #if defined(MBEDTLS_ENTROPY_C)
  50480. "MBEDTLS_ENTROPY_C",
  50481. #endif /* MBEDTLS_ENTROPY_C */
  50482. #if defined(MBEDTLS_ERROR_C)
  50483. "MBEDTLS_ERROR_C",
  50484. #endif /* MBEDTLS_ERROR_C */
  50485. #if defined(MBEDTLS_GCM_C)
  50486. "MBEDTLS_GCM_C",
  50487. #endif /* MBEDTLS_GCM_C */
  50488. #if defined(MBEDTLS_HAVEGE_C)
  50489. "MBEDTLS_HAVEGE_C",
  50490. #endif /* MBEDTLS_HAVEGE_C */
  50491. #if defined(MBEDTLS_HMAC_DRBG_C)
  50492. "MBEDTLS_HMAC_DRBG_C",
  50493. #endif /* MBEDTLS_HMAC_DRBG_C */
  50494. #if defined(MBEDTLS_MD_C)
  50495. "MBEDTLS_MD_C",
  50496. #endif /* MBEDTLS_MD_C */
  50497. #if defined(MBEDTLS_MD2_C)
  50498. "MBEDTLS_MD2_C",
  50499. #endif /* MBEDTLS_MD2_C */
  50500. #if defined(MBEDTLS_MD4_C)
  50501. "MBEDTLS_MD4_C",
  50502. #endif /* MBEDTLS_MD4_C */
  50503. #if defined(MBEDTLS_MD5_C)
  50504. "MBEDTLS_MD5_C",
  50505. #endif /* MBEDTLS_MD5_C */
  50506. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  50507. "MBEDTLS_MEMORY_BUFFER_ALLOC_C",
  50508. #endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
  50509. #if defined(MBEDTLS_NET_C)
  50510. "MBEDTLS_NET_C",
  50511. #endif /* MBEDTLS_NET_C */
  50512. #if defined(MBEDTLS_OID_C)
  50513. "MBEDTLS_OID_C",
  50514. #endif /* MBEDTLS_OID_C */
  50515. #if defined(MBEDTLS_PADLOCK_C)
  50516. "MBEDTLS_PADLOCK_C",
  50517. #endif /* MBEDTLS_PADLOCK_C */
  50518. #if defined(MBEDTLS_PEM_PARSE_C)
  50519. "MBEDTLS_PEM_PARSE_C",
  50520. #endif /* MBEDTLS_PEM_PARSE_C */
  50521. #if defined(MBEDTLS_PEM_WRITE_C)
  50522. "MBEDTLS_PEM_WRITE_C",
  50523. #endif /* MBEDTLS_PEM_WRITE_C */
  50524. #if defined(MBEDTLS_PK_C)
  50525. "MBEDTLS_PK_C",
  50526. #endif /* MBEDTLS_PK_C */
  50527. #if defined(MBEDTLS_PK_PARSE_C)
  50528. "MBEDTLS_PK_PARSE_C",
  50529. #endif /* MBEDTLS_PK_PARSE_C */
  50530. #if defined(MBEDTLS_PK_WRITE_C)
  50531. "MBEDTLS_PK_WRITE_C",
  50532. #endif /* MBEDTLS_PK_WRITE_C */
  50533. #if defined(MBEDTLS_PKCS5_C)
  50534. "MBEDTLS_PKCS5_C",
  50535. #endif /* MBEDTLS_PKCS5_C */
  50536. #if defined(MBEDTLS_PKCS11_C)
  50537. "MBEDTLS_PKCS11_C",
  50538. #endif /* MBEDTLS_PKCS11_C */
  50539. #if defined(MBEDTLS_PKCS12_C)
  50540. "MBEDTLS_PKCS12_C",
  50541. #endif /* MBEDTLS_PKCS12_C */
  50542. #if defined(MBEDTLS_PLATFORM_C)
  50543. "MBEDTLS_PLATFORM_C",
  50544. #endif /* MBEDTLS_PLATFORM_C */
  50545. #if defined(MBEDTLS_RIPEMD160_C)
  50546. "MBEDTLS_RIPEMD160_C",
  50547. #endif /* MBEDTLS_RIPEMD160_C */
  50548. #if defined(MBEDTLS_RSA_C)
  50549. "MBEDTLS_RSA_C",
  50550. #endif /* MBEDTLS_RSA_C */
  50551. #if defined(MBEDTLS_SHA1_C)
  50552. "MBEDTLS_SHA1_C",
  50553. #endif /* MBEDTLS_SHA1_C */
  50554. #if defined(MBEDTLS_SHA256_C)
  50555. "MBEDTLS_SHA256_C",
  50556. #endif /* MBEDTLS_SHA256_C */
  50557. #if defined(MBEDTLS_SHA512_C)
  50558. "MBEDTLS_SHA512_C",
  50559. #endif /* MBEDTLS_SHA512_C */
  50560. #if defined(MBEDTLS_SSL_CACHE_C)
  50561. "MBEDTLS_SSL_CACHE_C",
  50562. #endif /* MBEDTLS_SSL_CACHE_C */
  50563. #if defined(MBEDTLS_SSL_COOKIE_C)
  50564. "MBEDTLS_SSL_COOKIE_C",
  50565. #endif /* MBEDTLS_SSL_COOKIE_C */
  50566. #if defined(MBEDTLS_SSL_TICKET_C)
  50567. "MBEDTLS_SSL_TICKET_C",
  50568. #endif /* MBEDTLS_SSL_TICKET_C */
  50569. #if defined(MBEDTLS_SSL_CLI_C)
  50570. "MBEDTLS_SSL_CLI_C",
  50571. #endif /* MBEDTLS_SSL_CLI_C */
  50572. #if defined(MBEDTLS_SSL_SRV_C)
  50573. "MBEDTLS_SSL_SRV_C",
  50574. #endif /* MBEDTLS_SSL_SRV_C */
  50575. #if defined(MBEDTLS_SSL_TLS_C)
  50576. "MBEDTLS_SSL_TLS_C",
  50577. #endif /* MBEDTLS_SSL_TLS_C */
  50578. #if defined(MBEDTLS_THREADING_C)
  50579. "MBEDTLS_THREADING_C",
  50580. #endif /* MBEDTLS_THREADING_C */
  50581. #if defined(MBEDTLS_TIMING_C)
  50582. "MBEDTLS_TIMING_C",
  50583. #endif /* MBEDTLS_TIMING_C */
  50584. #if defined(MBEDTLS_VERSION_C)
  50585. "MBEDTLS_VERSION_C",
  50586. #endif /* MBEDTLS_VERSION_C */
  50587. #if defined(MBEDTLS_X509_USE_C)
  50588. "MBEDTLS_X509_USE_C",
  50589. #endif /* MBEDTLS_X509_USE_C */
  50590. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  50591. "MBEDTLS_X509_CRT_PARSE_C",
  50592. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  50593. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  50594. "MBEDTLS_X509_CRL_PARSE_C",
  50595. #endif /* MBEDTLS_X509_CRL_PARSE_C */
  50596. #if defined(MBEDTLS_X509_CSR_PARSE_C)
  50597. "MBEDTLS_X509_CSR_PARSE_C",
  50598. #endif /* MBEDTLS_X509_CSR_PARSE_C */
  50599. #if defined(MBEDTLS_X509_CREATE_C)
  50600. "MBEDTLS_X509_CREATE_C",
  50601. #endif /* MBEDTLS_X509_CREATE_C */
  50602. #if defined(MBEDTLS_X509_CRT_WRITE_C)
  50603. "MBEDTLS_X509_CRT_WRITE_C",
  50604. #endif /* MBEDTLS_X509_CRT_WRITE_C */
  50605. #if defined(MBEDTLS_X509_CSR_WRITE_C)
  50606. "MBEDTLS_X509_CSR_WRITE_C",
  50607. #endif /* MBEDTLS_X509_CSR_WRITE_C */
  50608. #if defined(MBEDTLS_XTEA_C)
  50609. "MBEDTLS_XTEA_C",
  50610. #endif /* MBEDTLS_XTEA_C */
  50611. #endif /* MBEDTLS_VERSION_FEATURES */
  50612. NULL
  50613. };
  50614. int mbedtls_version_check_feature( const char *feature )
  50615. {
  50616. const char **idx = features;
  50617. if( *idx == NULL )
  50618. return( -2 );
  50619. if( feature == NULL )
  50620. return( -1 );
  50621. while( *idx != NULL )
  50622. {
  50623. if( !strcmp( *idx, feature ) )
  50624. return( 0 );
  50625. idx++;
  50626. }
  50627. return( -1 );
  50628. }
  50629. #endif /* MBEDTLS_VERSION_C */
  50630. /*
  50631. Amalgamated build undefines
  50632. */
  50633. #undef ADD
  50634. #undef BC
  50635. #undef BEFORE_COLON
  50636. #undef F
  50637. #undef F0
  50638. #undef F1
  50639. #undef F2
  50640. #undef F3
  50641. #undef F4
  50642. #undef F5
  50643. #undef FSb
  50644. #undef K
  50645. #undef KK
  50646. #undef P
  50647. #undef R
  50648. #undef ROTR
  50649. #undef S
  50650. #undef S0
  50651. #undef S1
  50652. #undef S2
  50653. #undef S3
  50654. #undef SAFE_SNPRINTF
  50655. #undef SHR
  50656. #undef close
  50657. #undef read
  50658. #undef supported_init
  50659. #undef write
  50660. /********* Start of file library/x509.c ************/
  50661. /*
  50662. * X.509 common functions for parsing and verification
  50663. *
  50664. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  50665. * SPDX-License-Identifier: Apache-2.0
  50666. *
  50667. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  50668. * not use this file except in compliance with the License.
  50669. * You may obtain a copy of the License at
  50670. *
  50671. * http://www.apache.org/licenses/LICENSE-2.0
  50672. *
  50673. * Unless required by applicable law or agreed to in writing, software
  50674. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  50675. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  50676. * See the License for the specific language governing permissions and
  50677. * limitations under the License.
  50678. *
  50679. * This file is part of mbed TLS (https://tls.mbed.org)
  50680. */
  50681. /*
  50682. * The ITU-T X.509 standard defines a certificate format for PKI.
  50683. *
  50684. * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
  50685. * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
  50686. * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
  50687. *
  50688. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
  50689. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
  50690. */
  50691. #if !defined(MBEDTLS_CONFIG_FILE)
  50692. #else
  50693. #endif
  50694. #if defined(MBEDTLS_X509_USE_C)
  50695. #include <stdio.h>
  50696. #include <string.h>
  50697. #if defined(MBEDTLS_PEM_PARSE_C)
  50698. #endif
  50699. #if defined(MBEDTLS_PLATFORM_C)
  50700. #else
  50701. #include <stdio.h>
  50702. #include <stdlib.h>
  50703. #define mbedtls_free free
  50704. #define mbedtls_calloc calloc
  50705. #define mbedtls_printf printf
  50706. #define mbedtls_snprintf snprintf
  50707. #endif
  50708. #if defined(MBEDTLS_HAVE_TIME)
  50709. #endif
  50710. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  50711. #include <windows.h>
  50712. #else
  50713. #include <time.h>
  50714. #endif
  50715. #if defined(MBEDTLS_FS_IO)
  50716. #include <stdio.h>
  50717. #if !defined(_WIN32)
  50718. #include <sys/types.h>
  50719. #include <sys/stat.h>
  50720. #include <dirent.h>
  50721. #endif
  50722. #endif
  50723. #define CHECK(code) if( ( ret = code ) != 0 ){ return( ret ); }
  50724. #define CHECK_RANGE(min, max, val) if( val < min || val > max ){ return( ret ); }
  50725. /*
  50726. * CertificateSerialNumber ::= INTEGER
  50727. */
  50728. int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
  50729. mbedtls_x509_buf *serial )
  50730. {
  50731. int ret;
  50732. if( ( end - *p ) < 1 )
  50733. return( MBEDTLS_ERR_X509_INVALID_SERIAL +
  50734. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  50735. if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
  50736. **p != MBEDTLS_ASN1_INTEGER )
  50737. return( MBEDTLS_ERR_X509_INVALID_SERIAL +
  50738. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  50739. serial->tag = *(*p)++;
  50740. if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
  50741. return( MBEDTLS_ERR_X509_INVALID_SERIAL + ret );
  50742. serial->p = *p;
  50743. *p += serial->len;
  50744. return( 0 );
  50745. }
  50746. /* Get an algorithm identifier without parameters (eg for signatures)
  50747. *
  50748. * AlgorithmIdentifier ::= SEQUENCE {
  50749. * algorithm OBJECT IDENTIFIER,
  50750. * parameters ANY DEFINED BY algorithm OPTIONAL }
  50751. */
  50752. int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
  50753. mbedtls_x509_buf *alg )
  50754. {
  50755. int ret;
  50756. if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
  50757. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  50758. return( 0 );
  50759. }
  50760. /*
  50761. * Parse an algorithm identifier with (optional) paramaters
  50762. */
  50763. int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
  50764. mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
  50765. {
  50766. int ret;
  50767. if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
  50768. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  50769. return( 0 );
  50770. }
  50771. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  50772. /*
  50773. * HashAlgorithm ::= AlgorithmIdentifier
  50774. *
  50775. * AlgorithmIdentifier ::= SEQUENCE {
  50776. * algorithm OBJECT IDENTIFIER,
  50777. * parameters ANY DEFINED BY algorithm OPTIONAL }
  50778. *
  50779. * For HashAlgorithm, parameters MUST be NULL or absent.
  50780. */
  50781. static int x509_get_hash_alg( const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg )
  50782. {
  50783. int ret;
  50784. unsigned char *p;
  50785. const unsigned char *end;
  50786. mbedtls_x509_buf md_oid;
  50787. size_t len;
  50788. /* Make sure we got a SEQUENCE and setup bounds */
  50789. if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
  50790. return( MBEDTLS_ERR_X509_INVALID_ALG +
  50791. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  50792. p = (unsigned char *) alg->p;
  50793. end = p + alg->len;
  50794. if( p >= end )
  50795. return( MBEDTLS_ERR_X509_INVALID_ALG +
  50796. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  50797. /* Parse md_oid */
  50798. md_oid.tag = *p;
  50799. if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
  50800. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  50801. md_oid.p = p;
  50802. p += md_oid.len;
  50803. /* Get md_alg from md_oid */
  50804. if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
  50805. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  50806. /* Make sure params is absent of NULL */
  50807. if( p == end )
  50808. return( 0 );
  50809. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
  50810. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  50811. if( p != end )
  50812. return( MBEDTLS_ERR_X509_INVALID_ALG +
  50813. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  50814. return( 0 );
  50815. }
  50816. /*
  50817. * RSASSA-PSS-params ::= SEQUENCE {
  50818. * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
  50819. * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
  50820. * saltLength [2] INTEGER DEFAULT 20,
  50821. * trailerField [3] INTEGER DEFAULT 1 }
  50822. * -- Note that the tags in this Sequence are explicit.
  50823. *
  50824. * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
  50825. * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
  50826. * option. Enfore this at parsing time.
  50827. */
  50828. int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
  50829. mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
  50830. int *salt_len )
  50831. {
  50832. int ret;
  50833. unsigned char *p;
  50834. const unsigned char *end, *end2;
  50835. size_t len;
  50836. mbedtls_x509_buf alg_id, alg_params;
  50837. /* First set everything to defaults */
  50838. *md_alg = MBEDTLS_MD_SHA1;
  50839. *mgf_md = MBEDTLS_MD_SHA1;
  50840. *salt_len = 20;
  50841. /* Make sure params is a SEQUENCE and setup bounds */
  50842. if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
  50843. return( MBEDTLS_ERR_X509_INVALID_ALG +
  50844. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  50845. p = (unsigned char *) params->p;
  50846. end = p + params->len;
  50847. if( p == end )
  50848. return( 0 );
  50849. /*
  50850. * HashAlgorithm
  50851. */
  50852. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  50853. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
  50854. {
  50855. end2 = p + len;
  50856. /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
  50857. if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
  50858. return( ret );
  50859. if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
  50860. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  50861. if( p != end2 )
  50862. return( MBEDTLS_ERR_X509_INVALID_ALG +
  50863. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  50864. }
  50865. else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  50866. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  50867. if( p == end )
  50868. return( 0 );
  50869. /*
  50870. * MaskGenAlgorithm
  50871. */
  50872. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  50873. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
  50874. {
  50875. end2 = p + len;
  50876. /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
  50877. if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
  50878. return( ret );
  50879. /* Only MFG1 is recognised for now */
  50880. if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
  50881. return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE +
  50882. MBEDTLS_ERR_OID_NOT_FOUND );
  50883. /* Parse HashAlgorithm */
  50884. if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
  50885. return( ret );
  50886. if( p != end2 )
  50887. return( MBEDTLS_ERR_X509_INVALID_ALG +
  50888. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  50889. }
  50890. else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  50891. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  50892. if( p == end )
  50893. return( 0 );
  50894. /*
  50895. * salt_len
  50896. */
  50897. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  50898. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
  50899. {
  50900. end2 = p + len;
  50901. if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
  50902. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  50903. if( p != end2 )
  50904. return( MBEDTLS_ERR_X509_INVALID_ALG +
  50905. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  50906. }
  50907. else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  50908. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  50909. if( p == end )
  50910. return( 0 );
  50911. /*
  50912. * trailer_field (if present, must be 1)
  50913. */
  50914. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  50915. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
  50916. {
  50917. int trailer_field;
  50918. end2 = p + len;
  50919. if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
  50920. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  50921. if( p != end2 )
  50922. return( MBEDTLS_ERR_X509_INVALID_ALG +
  50923. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  50924. if( trailer_field != 1 )
  50925. return( MBEDTLS_ERR_X509_INVALID_ALG );
  50926. }
  50927. else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  50928. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  50929. if( p != end )
  50930. return( MBEDTLS_ERR_X509_INVALID_ALG +
  50931. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  50932. return( 0 );
  50933. }
  50934. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  50935. /*
  50936. * AttributeTypeAndValue ::= SEQUENCE {
  50937. * type AttributeType,
  50938. * value AttributeValue }
  50939. *
  50940. * AttributeType ::= OBJECT IDENTIFIER
  50941. *
  50942. * AttributeValue ::= ANY DEFINED BY AttributeType
  50943. */
  50944. static int x509_get_attr_type_value( unsigned char **p,
  50945. const unsigned char *end,
  50946. mbedtls_x509_name *cur )
  50947. {
  50948. int ret;
  50949. size_t len;
  50950. mbedtls_x509_buf *oid;
  50951. mbedtls_x509_buf *val;
  50952. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  50953. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  50954. return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
  50955. if( ( end - *p ) < 1 )
  50956. return( MBEDTLS_ERR_X509_INVALID_NAME +
  50957. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  50958. oid = &cur->oid;
  50959. oid->tag = **p;
  50960. if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 )
  50961. return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
  50962. oid->p = *p;
  50963. *p += oid->len;
  50964. if( ( end - *p ) < 1 )
  50965. return( MBEDTLS_ERR_X509_INVALID_NAME +
  50966. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  50967. if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
  50968. **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
  50969. **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
  50970. **p != MBEDTLS_ASN1_BIT_STRING )
  50971. return( MBEDTLS_ERR_X509_INVALID_NAME +
  50972. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  50973. val = &cur->val;
  50974. val->tag = *(*p)++;
  50975. if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 )
  50976. return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
  50977. val->p = *p;
  50978. *p += val->len;
  50979. cur->next = NULL;
  50980. return( 0 );
  50981. }
  50982. /*
  50983. * Name ::= CHOICE { -- only one possibility for now --
  50984. * rdnSequence RDNSequence }
  50985. *
  50986. * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
  50987. *
  50988. * RelativeDistinguishedName ::=
  50989. * SET OF AttributeTypeAndValue
  50990. *
  50991. * AttributeTypeAndValue ::= SEQUENCE {
  50992. * type AttributeType,
  50993. * value AttributeValue }
  50994. *
  50995. * AttributeType ::= OBJECT IDENTIFIER
  50996. *
  50997. * AttributeValue ::= ANY DEFINED BY AttributeType
  50998. *
  50999. * The data structure is optimized for the common case where each RDN has only
  51000. * one element, which is represented as a list of AttributeTypeAndValue.
  51001. * For the general case we still use a flat list, but we mark elements of the
  51002. * same set so that they are "merged" together in the functions that consume
  51003. * this list, eg mbedtls_x509_dn_gets().
  51004. */
  51005. int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
  51006. mbedtls_x509_name *cur )
  51007. {
  51008. int ret;
  51009. size_t set_len;
  51010. const unsigned char *end_set;
  51011. /* don't use recursion, we'd risk stack overflow if not optimized */
  51012. while( 1 )
  51013. {
  51014. /*
  51015. * parse SET
  51016. */
  51017. if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len,
  51018. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 )
  51019. return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
  51020. end_set = *p + set_len;
  51021. while( 1 )
  51022. {
  51023. if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
  51024. return( ret );
  51025. if( *p == end_set )
  51026. break;
  51027. /* Mark this item as being no the only one in a set */
  51028. cur->next_merged = 1;
  51029. cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
  51030. if( cur->next == NULL )
  51031. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  51032. cur = cur->next;
  51033. }
  51034. /*
  51035. * continue until end of SEQUENCE is reached
  51036. */
  51037. if( *p == end )
  51038. return( 0 );
  51039. cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
  51040. if( cur->next == NULL )
  51041. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  51042. cur = cur->next;
  51043. }
  51044. }
  51045. static int x509_parse_int( unsigned char **p, size_t n, int *res )
  51046. {
  51047. *res = 0;
  51048. for( ; n > 0; --n )
  51049. {
  51050. if( ( **p < '0') || ( **p > '9' ) )
  51051. return ( MBEDTLS_ERR_X509_INVALID_DATE );
  51052. *res *= 10;
  51053. *res += ( *(*p)++ - '0' );
  51054. }
  51055. return( 0 );
  51056. }
  51057. static int x509_date_is_valid(const mbedtls_x509_time *t )
  51058. {
  51059. int ret = MBEDTLS_ERR_X509_INVALID_DATE;
  51060. int month_len;
  51061. CHECK_RANGE( 0, 9999, t->year );
  51062. CHECK_RANGE( 0, 23, t->hour );
  51063. CHECK_RANGE( 0, 59, t->min );
  51064. CHECK_RANGE( 0, 59, t->sec );
  51065. switch( t->mon )
  51066. {
  51067. case 1: case 3: case 5: case 7: case 8: case 10: case 12:
  51068. month_len = 31;
  51069. break;
  51070. case 4: case 6: case 9: case 11:
  51071. month_len = 30;
  51072. break;
  51073. case 2:
  51074. if( ( !( t->year % 4 ) && t->year % 100 ) ||
  51075. !( t->year % 400 ) )
  51076. month_len = 29;
  51077. else
  51078. month_len = 28;
  51079. break;
  51080. default:
  51081. return( ret );
  51082. }
  51083. CHECK_RANGE( 1, month_len, t->day );
  51084. return( 0 );
  51085. }
  51086. /*
  51087. * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
  51088. * field.
  51089. */
  51090. static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen,
  51091. mbedtls_x509_time *tm )
  51092. {
  51093. int ret;
  51094. /*
  51095. * Minimum length is 10 or 12 depending on yearlen
  51096. */
  51097. if ( len < yearlen + 8 )
  51098. return ( MBEDTLS_ERR_X509_INVALID_DATE );
  51099. len -= yearlen + 8;
  51100. /*
  51101. * Parse year, month, day, hour, minute
  51102. */
  51103. CHECK( x509_parse_int( p, yearlen, &tm->year ) );
  51104. if ( 2 == yearlen )
  51105. {
  51106. if ( tm->year < 50 )
  51107. tm->year += 100;
  51108. tm->year += 1900;
  51109. }
  51110. CHECK( x509_parse_int( p, 2, &tm->mon ) );
  51111. CHECK( x509_parse_int( p, 2, &tm->day ) );
  51112. CHECK( x509_parse_int( p, 2, &tm->hour ) );
  51113. CHECK( x509_parse_int( p, 2, &tm->min ) );
  51114. /*
  51115. * Parse seconds if present
  51116. */
  51117. if ( len >= 2 )
  51118. {
  51119. CHECK( x509_parse_int( p, 2, &tm->sec ) );
  51120. len -= 2;
  51121. }
  51122. else
  51123. return ( MBEDTLS_ERR_X509_INVALID_DATE );
  51124. /*
  51125. * Parse trailing 'Z' if present
  51126. */
  51127. if ( 1 == len && 'Z' == **p )
  51128. {
  51129. (*p)++;
  51130. len--;
  51131. }
  51132. /*
  51133. * We should have parsed all characters at this point
  51134. */
  51135. if ( 0 != len )
  51136. return ( MBEDTLS_ERR_X509_INVALID_DATE );
  51137. CHECK( x509_date_is_valid( tm ) );
  51138. return ( 0 );
  51139. }
  51140. /*
  51141. * Time ::= CHOICE {
  51142. * utcTime UTCTime,
  51143. * generalTime GeneralizedTime }
  51144. */
  51145. int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
  51146. mbedtls_x509_time *tm )
  51147. {
  51148. int ret;
  51149. size_t len, year_len;
  51150. unsigned char tag;
  51151. if( ( end - *p ) < 1 )
  51152. return( MBEDTLS_ERR_X509_INVALID_DATE +
  51153. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  51154. tag = **p;
  51155. if( tag == MBEDTLS_ASN1_UTC_TIME )
  51156. year_len = 2;
  51157. else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
  51158. year_len = 4;
  51159. else
  51160. return( MBEDTLS_ERR_X509_INVALID_DATE +
  51161. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  51162. (*p)++;
  51163. ret = mbedtls_asn1_get_len( p, end, &len );
  51164. if( ret != 0 )
  51165. return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
  51166. return x509_parse_time( p, len, year_len, tm );
  51167. }
  51168. int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
  51169. {
  51170. int ret;
  51171. size_t len;
  51172. int tag_type;
  51173. if( ( end - *p ) < 1 )
  51174. return( MBEDTLS_ERR_X509_INVALID_SIGNATURE +
  51175. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  51176. tag_type = **p;
  51177. if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
  51178. return( MBEDTLS_ERR_X509_INVALID_SIGNATURE + ret );
  51179. sig->tag = tag_type;
  51180. sig->len = len;
  51181. sig->p = *p;
  51182. *p += len;
  51183. return( 0 );
  51184. }
  51185. /*
  51186. * Get signature algorithm from alg OID and optional parameters
  51187. */
  51188. int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
  51189. mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
  51190. void **sig_opts )
  51191. {
  51192. int ret;
  51193. if( *sig_opts != NULL )
  51194. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  51195. if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
  51196. return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + ret );
  51197. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  51198. if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
  51199. {
  51200. mbedtls_pk_rsassa_pss_options *pss_opts;
  51201. pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
  51202. if( pss_opts == NULL )
  51203. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  51204. ret = mbedtls_x509_get_rsassa_pss_params( sig_params,
  51205. md_alg,
  51206. &pss_opts->mgf1_hash_id,
  51207. &pss_opts->expected_salt_len );
  51208. if( ret != 0 )
  51209. {
  51210. mbedtls_free( pss_opts );
  51211. return( ret );
  51212. }
  51213. *sig_opts = (void *) pss_opts;
  51214. }
  51215. else
  51216. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  51217. {
  51218. /* Make sure parameters are absent or NULL */
  51219. if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
  51220. sig_params->len != 0 )
  51221. return( MBEDTLS_ERR_X509_INVALID_ALG );
  51222. }
  51223. return( 0 );
  51224. }
  51225. /*
  51226. * X.509 Extensions (No parsing of extensions, pointer should
  51227. * be either manually updated or extensions should be parsed!)
  51228. */
  51229. int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
  51230. mbedtls_x509_buf *ext, int tag )
  51231. {
  51232. int ret;
  51233. size_t len;
  51234. if( *p == end )
  51235. return( 0 );
  51236. ext->tag = **p;
  51237. if( ( ret = mbedtls_asn1_get_tag( p, end, &ext->len,
  51238. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag ) ) != 0 )
  51239. return( ret );
  51240. ext->p = *p;
  51241. end = *p + ext->len;
  51242. /*
  51243. * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
  51244. *
  51245. * Extension ::= SEQUENCE {
  51246. * extnID OBJECT IDENTIFIER,
  51247. * critical BOOLEAN DEFAULT FALSE,
  51248. * extnValue OCTET STRING }
  51249. */
  51250. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  51251. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  51252. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  51253. if( end != *p + len )
  51254. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  51255. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  51256. return( 0 );
  51257. }
  51258. /*
  51259. * Store the name in printable form into buf; no more
  51260. * than size characters will be written
  51261. */
  51262. int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
  51263. {
  51264. int ret;
  51265. size_t i, n;
  51266. unsigned char c, merge = 0;
  51267. const mbedtls_x509_name *name;
  51268. const char *short_name = NULL;
  51269. char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
  51270. memset( s, 0, sizeof( s ) );
  51271. name = dn;
  51272. p = buf;
  51273. n = size;
  51274. while( name != NULL )
  51275. {
  51276. if( !name->oid.p )
  51277. {
  51278. name = name->next;
  51279. continue;
  51280. }
  51281. if( name != dn )
  51282. {
  51283. ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
  51284. MBEDTLS_X509_SAFE_SNPRINTF;
  51285. }
  51286. ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
  51287. if( ret == 0 )
  51288. ret = mbedtls_snprintf( p, n, "%s=", short_name );
  51289. else
  51290. ret = mbedtls_snprintf( p, n, "\?\?=" );
  51291. MBEDTLS_X509_SAFE_SNPRINTF;
  51292. for( i = 0; i < name->val.len; i++ )
  51293. {
  51294. if( i >= sizeof( s ) - 1 )
  51295. break;
  51296. c = name->val.p[i];
  51297. if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
  51298. s[i] = '?';
  51299. else s[i] = c;
  51300. }
  51301. s[i] = '\0';
  51302. ret = mbedtls_snprintf( p, n, "%s", s );
  51303. MBEDTLS_X509_SAFE_SNPRINTF;
  51304. merge = name->next_merged;
  51305. name = name->next;
  51306. }
  51307. return( (int) ( size - n ) );
  51308. }
  51309. /*
  51310. * Store the serial in printable form into buf; no more
  51311. * than size characters will be written
  51312. */
  51313. int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial )
  51314. {
  51315. int ret;
  51316. size_t i, n, nr;
  51317. char *p;
  51318. p = buf;
  51319. n = size;
  51320. nr = ( serial->len <= 32 )
  51321. ? serial->len : 28;
  51322. for( i = 0; i < nr; i++ )
  51323. {
  51324. if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
  51325. continue;
  51326. ret = mbedtls_snprintf( p, n, "%02X%s",
  51327. serial->p[i], ( i < nr - 1 ) ? ":" : "" );
  51328. MBEDTLS_X509_SAFE_SNPRINTF;
  51329. }
  51330. if( nr != serial->len )
  51331. {
  51332. ret = mbedtls_snprintf( p, n, "...." );
  51333. MBEDTLS_X509_SAFE_SNPRINTF;
  51334. }
  51335. return( (int) ( size - n ) );
  51336. }
  51337. /*
  51338. * Helper for writing signature algorithms
  51339. */
  51340. int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
  51341. mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
  51342. const void *sig_opts )
  51343. {
  51344. int ret;
  51345. char *p = buf;
  51346. size_t n = size;
  51347. const char *desc = NULL;
  51348. ret = mbedtls_oid_get_sig_alg_desc( sig_oid, &desc );
  51349. if( ret != 0 )
  51350. ret = mbedtls_snprintf( p, n, "???" );
  51351. else
  51352. ret = mbedtls_snprintf( p, n, "%s", desc );
  51353. MBEDTLS_X509_SAFE_SNPRINTF;
  51354. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  51355. if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
  51356. {
  51357. const mbedtls_pk_rsassa_pss_options *pss_opts;
  51358. const mbedtls_md_info_t *md_info, *mgf_md_info;
  51359. pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
  51360. md_info = mbedtls_md_info_from_type( md_alg );
  51361. mgf_md_info = mbedtls_md_info_from_type( pss_opts->mgf1_hash_id );
  51362. ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
  51363. md_info ? mbedtls_md_get_name( md_info ) : "???",
  51364. mgf_md_info ? mbedtls_md_get_name( mgf_md_info ) : "???",
  51365. pss_opts->expected_salt_len );
  51366. MBEDTLS_X509_SAFE_SNPRINTF;
  51367. }
  51368. #else
  51369. ((void) pk_alg);
  51370. ((void) md_alg);
  51371. ((void) sig_opts);
  51372. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  51373. return( (int)( size - n ) );
  51374. }
  51375. /*
  51376. * Helper for writing "RSA key size", "EC key size", etc
  51377. */
  51378. int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name )
  51379. {
  51380. char *p = buf;
  51381. size_t n = buf_size;
  51382. int ret;
  51383. ret = mbedtls_snprintf( p, n, "%s key size", name );
  51384. MBEDTLS_X509_SAFE_SNPRINTF;
  51385. return( 0 );
  51386. }
  51387. #if defined(MBEDTLS_HAVE_TIME_DATE)
  51388. /*
  51389. * Set the time structure to the current time.
  51390. * Return 0 on success, non-zero on failure.
  51391. */
  51392. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  51393. static int x509_get_current_time( mbedtls_x509_time *now )
  51394. {
  51395. SYSTEMTIME st;
  51396. GetSystemTime( &st );
  51397. now->year = st.wYear;
  51398. now->mon = st.wMonth;
  51399. now->day = st.wDay;
  51400. now->hour = st.wHour;
  51401. now->min = st.wMinute;
  51402. now->sec = st.wSecond;
  51403. return( 0 );
  51404. }
  51405. #else
  51406. static int x509_get_current_time( mbedtls_x509_time *now )
  51407. {
  51408. struct tm *lt;
  51409. mbedtls_time_t tt;
  51410. int ret = 0;
  51411. #if defined(MBEDTLS_THREADING_C)
  51412. if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 )
  51413. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  51414. #endif
  51415. tt = mbedtls_time( NULL );
  51416. lt = gmtime( &tt );
  51417. if( lt == NULL )
  51418. ret = -1;
  51419. else
  51420. {
  51421. now->year = lt->tm_year + 1900;
  51422. now->mon = lt->tm_mon + 1;
  51423. now->day = lt->tm_mday;
  51424. now->hour = lt->tm_hour;
  51425. now->min = lt->tm_min;
  51426. now->sec = lt->tm_sec;
  51427. }
  51428. #if defined(MBEDTLS_THREADING_C)
  51429. if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 )
  51430. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  51431. #endif
  51432. return( ret );
  51433. }
  51434. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  51435. /*
  51436. * Return 0 if before <= after, 1 otherwise
  51437. */
  51438. static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after )
  51439. {
  51440. if( before->year > after->year )
  51441. return( 1 );
  51442. if( before->year == after->year &&
  51443. before->mon > after->mon )
  51444. return( 1 );
  51445. if( before->year == after->year &&
  51446. before->mon == after->mon &&
  51447. before->day > after->day )
  51448. return( 1 );
  51449. if( before->year == after->year &&
  51450. before->mon == after->mon &&
  51451. before->day == after->day &&
  51452. before->hour > after->hour )
  51453. return( 1 );
  51454. if( before->year == after->year &&
  51455. before->mon == after->mon &&
  51456. before->day == after->day &&
  51457. before->hour == after->hour &&
  51458. before->min > after->min )
  51459. return( 1 );
  51460. if( before->year == after->year &&
  51461. before->mon == after->mon &&
  51462. before->day == after->day &&
  51463. before->hour == after->hour &&
  51464. before->min == after->min &&
  51465. before->sec > after->sec )
  51466. return( 1 );
  51467. return( 0 );
  51468. }
  51469. int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
  51470. {
  51471. mbedtls_x509_time now;
  51472. if( x509_get_current_time( &now ) != 0 )
  51473. return( 1 );
  51474. return( x509_check_time( &now, to ) );
  51475. }
  51476. int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
  51477. {
  51478. mbedtls_x509_time now;
  51479. if( x509_get_current_time( &now ) != 0 )
  51480. return( 1 );
  51481. return( x509_check_time( from, &now ) );
  51482. }
  51483. #else /* MBEDTLS_HAVE_TIME_DATE */
  51484. int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
  51485. {
  51486. ((void) to);
  51487. return( 0 );
  51488. }
  51489. int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
  51490. {
  51491. ((void) from);
  51492. return( 0 );
  51493. }
  51494. #endif /* MBEDTLS_HAVE_TIME_DATE */
  51495. #if defined(MBEDTLS_SELF_TEST)
  51496. /*
  51497. * Checkup routine
  51498. */
  51499. int mbedtls_x509_self_test( int verbose )
  51500. {
  51501. #if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
  51502. int ret;
  51503. uint32_t flags;
  51504. mbedtls_x509_crt cacert;
  51505. mbedtls_x509_crt clicert;
  51506. if( verbose != 0 )
  51507. mbedtls_printf( " X.509 certificate load: " );
  51508. mbedtls_x509_crt_init( &clicert );
  51509. ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
  51510. mbedtls_test_cli_crt_len );
  51511. if( ret != 0 )
  51512. {
  51513. if( verbose != 0 )
  51514. mbedtls_printf( "failed\n" );
  51515. return( ret );
  51516. }
  51517. mbedtls_x509_crt_init( &cacert );
  51518. ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_ca_crt,
  51519. mbedtls_test_ca_crt_len );
  51520. if( ret != 0 )
  51521. {
  51522. if( verbose != 0 )
  51523. mbedtls_printf( "failed\n" );
  51524. return( ret );
  51525. }
  51526. if( verbose != 0 )
  51527. mbedtls_printf( "passed\n X.509 signature verify: ");
  51528. ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
  51529. if( ret != 0 )
  51530. {
  51531. if( verbose != 0 )
  51532. mbedtls_printf( "failed\n" );
  51533. return( ret );
  51534. }
  51535. if( verbose != 0 )
  51536. mbedtls_printf( "passed\n\n");
  51537. mbedtls_x509_crt_free( &cacert );
  51538. mbedtls_x509_crt_free( &clicert );
  51539. return( 0 );
  51540. #else
  51541. ((void) verbose);
  51542. return( 0 );
  51543. #endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA1_C */
  51544. }
  51545. #endif /* MBEDTLS_SELF_TEST */
  51546. #endif /* MBEDTLS_X509_USE_C */
  51547. /*
  51548. Amalgamated build undefines
  51549. */
  51550. #undef ADD
  51551. #undef BC
  51552. #undef BEFORE_COLON
  51553. #undef F
  51554. #undef F0
  51555. #undef F1
  51556. #undef F2
  51557. #undef F3
  51558. #undef F4
  51559. #undef F5
  51560. #undef FSb
  51561. #undef K
  51562. #undef KK
  51563. #undef P
  51564. #undef R
  51565. #undef ROTR
  51566. #undef S
  51567. #undef S0
  51568. #undef S1
  51569. #undef S2
  51570. #undef S3
  51571. #undef SAFE_SNPRINTF
  51572. #undef SHR
  51573. #undef close
  51574. #undef read
  51575. #undef supported_init
  51576. #undef write
  51577. /********* Start of file library/x509_create.c ************/
  51578. /*
  51579. * X.509 base functions for creating certificates / CSRs
  51580. *
  51581. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  51582. * SPDX-License-Identifier: Apache-2.0
  51583. *
  51584. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  51585. * not use this file except in compliance with the License.
  51586. * You may obtain a copy of the License at
  51587. *
  51588. * http://www.apache.org/licenses/LICENSE-2.0
  51589. *
  51590. * Unless required by applicable law or agreed to in writing, software
  51591. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  51592. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  51593. * See the License for the specific language governing permissions and
  51594. * limitations under the License.
  51595. *
  51596. * This file is part of mbed TLS (https://tls.mbed.org)
  51597. */
  51598. #if !defined(MBEDTLS_CONFIG_FILE)
  51599. #else
  51600. #endif
  51601. #if defined(MBEDTLS_X509_CREATE_C)
  51602. #include <string.h>
  51603. typedef struct {
  51604. const char *name;
  51605. size_t name_len;
  51606. const char*oid;
  51607. } x509_attr_descriptor_t;
  51608. #define ADD_STRLEN( s ) s, sizeof( s ) - 1
  51609. static const x509_attr_descriptor_t x509_attrs[] =
  51610. {
  51611. { ADD_STRLEN( "CN" ), MBEDTLS_OID_AT_CN },
  51612. { ADD_STRLEN( "commonName" ), MBEDTLS_OID_AT_CN },
  51613. { ADD_STRLEN( "C" ), MBEDTLS_OID_AT_COUNTRY },
  51614. { ADD_STRLEN( "countryName" ), MBEDTLS_OID_AT_COUNTRY },
  51615. { ADD_STRLEN( "O" ), MBEDTLS_OID_AT_ORGANIZATION },
  51616. { ADD_STRLEN( "organizationName" ), MBEDTLS_OID_AT_ORGANIZATION },
  51617. { ADD_STRLEN( "L" ), MBEDTLS_OID_AT_LOCALITY },
  51618. { ADD_STRLEN( "locality" ), MBEDTLS_OID_AT_LOCALITY },
  51619. { ADD_STRLEN( "R" ), MBEDTLS_OID_PKCS9_EMAIL },
  51620. { ADD_STRLEN( "OU" ), MBEDTLS_OID_AT_ORG_UNIT },
  51621. { ADD_STRLEN( "organizationalUnitName" ), MBEDTLS_OID_AT_ORG_UNIT },
  51622. { ADD_STRLEN( "ST" ), MBEDTLS_OID_AT_STATE },
  51623. { ADD_STRLEN( "stateOrProvinceName" ), MBEDTLS_OID_AT_STATE },
  51624. { ADD_STRLEN( "emailAddress" ), MBEDTLS_OID_PKCS9_EMAIL },
  51625. { ADD_STRLEN( "serialNumber" ), MBEDTLS_OID_AT_SERIAL_NUMBER },
  51626. { ADD_STRLEN( "postalAddress" ), MBEDTLS_OID_AT_POSTAL_ADDRESS },
  51627. { ADD_STRLEN( "postalCode" ), MBEDTLS_OID_AT_POSTAL_CODE },
  51628. { ADD_STRLEN( "dnQualifier" ), MBEDTLS_OID_AT_DN_QUALIFIER },
  51629. { ADD_STRLEN( "title" ), MBEDTLS_OID_AT_TITLE },
  51630. { ADD_STRLEN( "surName" ), MBEDTLS_OID_AT_SUR_NAME },
  51631. { ADD_STRLEN( "SN" ), MBEDTLS_OID_AT_SUR_NAME },
  51632. { ADD_STRLEN( "givenName" ), MBEDTLS_OID_AT_GIVEN_NAME },
  51633. { ADD_STRLEN( "GN" ), MBEDTLS_OID_AT_GIVEN_NAME },
  51634. { ADD_STRLEN( "initials" ), MBEDTLS_OID_AT_INITIALS },
  51635. { ADD_STRLEN( "pseudonym" ), MBEDTLS_OID_AT_PSEUDONYM },
  51636. { ADD_STRLEN( "generationQualifier" ), MBEDTLS_OID_AT_GENERATION_QUALIFIER },
  51637. { ADD_STRLEN( "domainComponent" ), MBEDTLS_OID_DOMAIN_COMPONENT },
  51638. { ADD_STRLEN( "DC" ), MBEDTLS_OID_DOMAIN_COMPONENT },
  51639. { NULL, 0, NULL }
  51640. };
  51641. static const char *x509_at_oid_from_name( const char *name, size_t name_len )
  51642. {
  51643. const x509_attr_descriptor_t *cur;
  51644. for( cur = x509_attrs; cur->name != NULL; cur++ )
  51645. if( cur->name_len == name_len &&
  51646. strncmp( cur->name, name, name_len ) == 0 )
  51647. break;
  51648. return( cur->oid );
  51649. }
  51650. int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *name )
  51651. {
  51652. int ret = 0;
  51653. const char *s = name, *c = s;
  51654. const char *end = s + strlen( s );
  51655. const char *oid = NULL;
  51656. int in_tag = 1;
  51657. char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
  51658. char *d = data;
  51659. /* Clear existing chain if present */
  51660. mbedtls_asn1_free_named_data_list( head );
  51661. while( c <= end )
  51662. {
  51663. if( in_tag && *c == '=' )
  51664. {
  51665. if( ( oid = x509_at_oid_from_name( s, c - s ) ) == NULL )
  51666. {
  51667. ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
  51668. goto exit;
  51669. }
  51670. s = c + 1;
  51671. in_tag = 0;
  51672. d = data;
  51673. }
  51674. if( !in_tag && *c == '\\' && c != end )
  51675. {
  51676. c++;
  51677. /* Check for valid escaped characters */
  51678. if( c == end || *c != ',' )
  51679. {
  51680. ret = MBEDTLS_ERR_X509_INVALID_NAME;
  51681. goto exit;
  51682. }
  51683. }
  51684. else if( !in_tag && ( *c == ',' || c == end ) )
  51685. {
  51686. if( mbedtls_asn1_store_named_data( head, oid, strlen( oid ),
  51687. (unsigned char *) data,
  51688. d - data ) == NULL )
  51689. {
  51690. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  51691. }
  51692. while( c < end && *(c + 1) == ' ' )
  51693. c++;
  51694. s = c + 1;
  51695. in_tag = 1;
  51696. }
  51697. if( !in_tag && s != c + 1 )
  51698. {
  51699. *(d++) = *c;
  51700. if( d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE )
  51701. {
  51702. ret = MBEDTLS_ERR_X509_INVALID_NAME;
  51703. goto exit;
  51704. }
  51705. }
  51706. c++;
  51707. }
  51708. exit:
  51709. return( ret );
  51710. }
  51711. /* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
  51712. * to store the critical boolean for us
  51713. */
  51714. int mbedtls_x509_set_extension( mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
  51715. int critical, const unsigned char *val, size_t val_len )
  51716. {
  51717. mbedtls_asn1_named_data *cur;
  51718. if( ( cur = mbedtls_asn1_store_named_data( head, oid, oid_len,
  51719. NULL, val_len + 1 ) ) == NULL )
  51720. {
  51721. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  51722. }
  51723. cur->val.p[0] = critical;
  51724. memcpy( cur->val.p + 1, val, val_len );
  51725. return( 0 );
  51726. }
  51727. /*
  51728. * RelativeDistinguishedName ::=
  51729. * SET OF AttributeTypeAndValue
  51730. *
  51731. * AttributeTypeAndValue ::= SEQUENCE {
  51732. * type AttributeType,
  51733. * value AttributeValue }
  51734. *
  51735. * AttributeType ::= OBJECT IDENTIFIER
  51736. *
  51737. * AttributeValue ::= ANY DEFINED BY AttributeType
  51738. */
  51739. static int x509_write_name( unsigned char **p, unsigned char *start,
  51740. const char *oid, size_t oid_len,
  51741. const unsigned char *name, size_t name_len )
  51742. {
  51743. int ret;
  51744. size_t len = 0;
  51745. // Write PrintableString for all except MBEDTLS_OID_PKCS9_EMAIL
  51746. //
  51747. if( MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_EMAIL ) == oid_len &&
  51748. memcmp( oid, MBEDTLS_OID_PKCS9_EMAIL, oid_len ) == 0 )
  51749. {
  51750. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_ia5_string( p, start,
  51751. (const char *) name,
  51752. name_len ) );
  51753. }
  51754. else
  51755. {
  51756. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_printable_string( p, start,
  51757. (const char *) name,
  51758. name_len ) );
  51759. }
  51760. // Write OID
  51761. //
  51762. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
  51763. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  51764. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
  51765. MBEDTLS_ASN1_SEQUENCE ) );
  51766. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  51767. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
  51768. MBEDTLS_ASN1_SET ) );
  51769. return( (int) len );
  51770. }
  51771. int mbedtls_x509_write_names( unsigned char **p, unsigned char *start,
  51772. mbedtls_asn1_named_data *first )
  51773. {
  51774. int ret;
  51775. size_t len = 0;
  51776. mbedtls_asn1_named_data *cur = first;
  51777. while( cur != NULL )
  51778. {
  51779. MBEDTLS_ASN1_CHK_ADD( len, x509_write_name( p, start, (char *) cur->oid.p,
  51780. cur->oid.len,
  51781. cur->val.p, cur->val.len ) );
  51782. cur = cur->next;
  51783. }
  51784. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  51785. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
  51786. MBEDTLS_ASN1_SEQUENCE ) );
  51787. return( (int) len );
  51788. }
  51789. int mbedtls_x509_write_sig( unsigned char **p, unsigned char *start,
  51790. const char *oid, size_t oid_len,
  51791. unsigned char *sig, size_t size )
  51792. {
  51793. int ret;
  51794. size_t len = 0;
  51795. if( *p < start || (size_t)( *p - start ) < size )
  51796. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  51797. len = size;
  51798. (*p) -= len;
  51799. memcpy( *p, sig, len );
  51800. if( *p - start < 1 )
  51801. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  51802. *--(*p) = 0;
  51803. len += 1;
  51804. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  51805. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
  51806. // Write OID
  51807. //
  51808. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( p, start, oid,
  51809. oid_len, 0 ) );
  51810. return( (int) len );
  51811. }
  51812. static int x509_write_extension( unsigned char **p, unsigned char *start,
  51813. mbedtls_asn1_named_data *ext )
  51814. {
  51815. int ret;
  51816. size_t len = 0;
  51817. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->val.p + 1,
  51818. ext->val.len - 1 ) );
  51819. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->val.len - 1 ) );
  51820. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
  51821. if( ext->val.p[0] != 0 )
  51822. {
  51823. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( p, start, 1 ) );
  51824. }
  51825. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->oid.p,
  51826. ext->oid.len ) );
  51827. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->oid.len ) );
  51828. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
  51829. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  51830. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
  51831. MBEDTLS_ASN1_SEQUENCE ) );
  51832. return( (int) len );
  51833. }
  51834. /*
  51835. * Extension ::= SEQUENCE {
  51836. * extnID OBJECT IDENTIFIER,
  51837. * critical BOOLEAN DEFAULT FALSE,
  51838. * extnValue OCTET STRING
  51839. * -- contains the DER encoding of an ASN.1 value
  51840. * -- corresponding to the extension type identified
  51841. * -- by extnID
  51842. * }
  51843. */
  51844. int mbedtls_x509_write_extensions( unsigned char **p, unsigned char *start,
  51845. mbedtls_asn1_named_data *first )
  51846. {
  51847. int ret;
  51848. size_t len = 0;
  51849. mbedtls_asn1_named_data *cur_ext = first;
  51850. while( cur_ext != NULL )
  51851. {
  51852. MBEDTLS_ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) );
  51853. cur_ext = cur_ext->next;
  51854. }
  51855. return( (int) len );
  51856. }
  51857. #endif /* MBEDTLS_X509_CREATE_C */
  51858. /*
  51859. Amalgamated build undefines
  51860. */
  51861. #undef ADD
  51862. #undef BC
  51863. #undef BEFORE_COLON
  51864. #undef F
  51865. #undef F0
  51866. #undef F1
  51867. #undef F2
  51868. #undef F3
  51869. #undef F4
  51870. #undef F5
  51871. #undef FSb
  51872. #undef K
  51873. #undef KK
  51874. #undef P
  51875. #undef R
  51876. #undef ROTR
  51877. #undef S
  51878. #undef S0
  51879. #undef S1
  51880. #undef S2
  51881. #undef S3
  51882. #undef SAFE_SNPRINTF
  51883. #undef SHR
  51884. #undef close
  51885. #undef read
  51886. #undef supported_init
  51887. #undef write
  51888. /********* Start of file library/x509_crl.c ************/
  51889. /*
  51890. * X.509 Certidicate Revocation List (CRL) parsing
  51891. *
  51892. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  51893. * SPDX-License-Identifier: Apache-2.0
  51894. *
  51895. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  51896. * not use this file except in compliance with the License.
  51897. * You may obtain a copy of the License at
  51898. *
  51899. * http://www.apache.org/licenses/LICENSE-2.0
  51900. *
  51901. * Unless required by applicable law or agreed to in writing, software
  51902. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  51903. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  51904. * See the License for the specific language governing permissions and
  51905. * limitations under the License.
  51906. *
  51907. * This file is part of mbed TLS (https://tls.mbed.org)
  51908. */
  51909. /*
  51910. * The ITU-T X.509 standard defines a certificate format for PKI.
  51911. *
  51912. * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
  51913. * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
  51914. * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
  51915. *
  51916. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
  51917. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
  51918. */
  51919. #if !defined(MBEDTLS_CONFIG_FILE)
  51920. #else
  51921. #endif
  51922. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  51923. #include <string.h>
  51924. #if defined(MBEDTLS_PEM_PARSE_C)
  51925. #endif
  51926. #if defined(MBEDTLS_PLATFORM_C)
  51927. #else
  51928. #include <stdlib.h>
  51929. #include <stdio.h>
  51930. #define mbedtls_free free
  51931. #define mbedtls_calloc calloc
  51932. #define mbedtls_snprintf snprintf
  51933. #endif
  51934. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  51935. #include <windows.h>
  51936. #else
  51937. #include <time.h>
  51938. #endif
  51939. #if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
  51940. #include <stdio.h>
  51941. #endif
  51942. /* Implementation that should never be optimized out by the compiler */
  51943. /* zeroize was here */
  51944. /*
  51945. * Version ::= INTEGER { v1(0), v2(1) }
  51946. */
  51947. static int x509_crl_get_version( unsigned char **p,
  51948. const unsigned char *end,
  51949. int *ver )
  51950. {
  51951. int ret;
  51952. if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
  51953. {
  51954. if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  51955. {
  51956. *ver = 0;
  51957. return( 0 );
  51958. }
  51959. return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
  51960. }
  51961. return( 0 );
  51962. }
  51963. /*
  51964. * X.509 CRL v2 extensions
  51965. *
  51966. * We currently don't parse any extension's content, but we do check that the
  51967. * list of extensions is well-formed and abort on critical extensions (that
  51968. * are unsupported as we don't support any extension so far)
  51969. */
  51970. static int x509_get_crl_ext( unsigned char **p,
  51971. const unsigned char *end,
  51972. mbedtls_x509_buf *ext )
  51973. {
  51974. int ret;
  51975. /*
  51976. * crlExtensions [0] EXPLICIT Extensions OPTIONAL
  51977. * -- if present, version MUST be v2
  51978. */
  51979. if( ( ret = mbedtls_x509_get_ext( p, end, ext, 0 ) ) != 0 )
  51980. {
  51981. if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  51982. return( 0 );
  51983. return( ret );
  51984. }
  51985. while( *p < end )
  51986. {
  51987. /*
  51988. * Extension ::= SEQUENCE {
  51989. * extnID OBJECT IDENTIFIER,
  51990. * critical BOOLEAN DEFAULT FALSE,
  51991. * extnValue OCTET STRING }
  51992. */
  51993. int is_critical = 0;
  51994. const unsigned char *end_ext_data;
  51995. size_t len;
  51996. /* Get enclosing sequence tag */
  51997. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  51998. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  51999. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  52000. end_ext_data = *p + len;
  52001. /* Get OID (currently ignored) */
  52002. if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
  52003. MBEDTLS_ASN1_OID ) ) != 0 )
  52004. {
  52005. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  52006. }
  52007. *p += len;
  52008. /* Get optional critical */
  52009. if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data,
  52010. &is_critical ) ) != 0 &&
  52011. ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
  52012. {
  52013. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  52014. }
  52015. /* Data should be octet string type */
  52016. if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
  52017. MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  52018. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  52019. /* Ignore data so far and just check its length */
  52020. *p += len;
  52021. if( *p != end_ext_data )
  52022. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  52023. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  52024. /* Abort on (unsupported) critical extensions */
  52025. if( is_critical )
  52026. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  52027. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  52028. }
  52029. if( *p != end )
  52030. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  52031. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  52032. return( 0 );
  52033. }
  52034. /*
  52035. * X.509 CRL v2 entry extensions (no extensions parsed yet.)
  52036. */
  52037. static int x509_get_crl_entry_ext( unsigned char **p,
  52038. const unsigned char *end,
  52039. mbedtls_x509_buf *ext )
  52040. {
  52041. int ret;
  52042. size_t len = 0;
  52043. /* OPTIONAL */
  52044. if( end <= *p )
  52045. return( 0 );
  52046. ext->tag = **p;
  52047. ext->p = *p;
  52048. /*
  52049. * Get CRL-entry extension sequence header
  52050. * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
  52051. */
  52052. if( ( ret = mbedtls_asn1_get_tag( p, end, &ext->len,
  52053. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  52054. {
  52055. if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  52056. {
  52057. ext->p = NULL;
  52058. return( 0 );
  52059. }
  52060. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  52061. }
  52062. end = *p + ext->len;
  52063. if( end != *p + ext->len )
  52064. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  52065. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  52066. while( *p < end )
  52067. {
  52068. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  52069. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  52070. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  52071. *p += len;
  52072. }
  52073. if( *p != end )
  52074. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  52075. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  52076. return( 0 );
  52077. }
  52078. /*
  52079. * X.509 CRL Entries
  52080. */
  52081. static int x509_get_entries( unsigned char **p,
  52082. const unsigned char *end,
  52083. mbedtls_x509_crl_entry *entry )
  52084. {
  52085. int ret;
  52086. size_t entry_len;
  52087. mbedtls_x509_crl_entry *cur_entry = entry;
  52088. if( *p == end )
  52089. return( 0 );
  52090. if( ( ret = mbedtls_asn1_get_tag( p, end, &entry_len,
  52091. MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ) ) != 0 )
  52092. {
  52093. if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  52094. return( 0 );
  52095. return( ret );
  52096. }
  52097. end = *p + entry_len;
  52098. while( *p < end )
  52099. {
  52100. size_t len2;
  52101. const unsigned char *end2;
  52102. if( ( ret = mbedtls_asn1_get_tag( p, end, &len2,
  52103. MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ) ) != 0 )
  52104. {
  52105. return( ret );
  52106. }
  52107. cur_entry->raw.tag = **p;
  52108. cur_entry->raw.p = *p;
  52109. cur_entry->raw.len = len2;
  52110. end2 = *p + len2;
  52111. if( ( ret = mbedtls_x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
  52112. return( ret );
  52113. if( ( ret = mbedtls_x509_get_time( p, end2,
  52114. &cur_entry->revocation_date ) ) != 0 )
  52115. return( ret );
  52116. if( ( ret = x509_get_crl_entry_ext( p, end2,
  52117. &cur_entry->entry_ext ) ) != 0 )
  52118. return( ret );
  52119. if( *p < end )
  52120. {
  52121. cur_entry->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl_entry ) );
  52122. if( cur_entry->next == NULL )
  52123. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  52124. cur_entry = cur_entry->next;
  52125. }
  52126. }
  52127. return( 0 );
  52128. }
  52129. /*
  52130. * Parse one CRLs in DER format and append it to the chained list
  52131. */
  52132. int mbedtls_x509_crl_parse_der( mbedtls_x509_crl *chain,
  52133. const unsigned char *buf, size_t buflen )
  52134. {
  52135. int ret;
  52136. size_t len;
  52137. unsigned char *p = NULL, *end = NULL;
  52138. mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
  52139. mbedtls_x509_crl *crl = chain;
  52140. /*
  52141. * Check for valid input
  52142. */
  52143. if( crl == NULL || buf == NULL )
  52144. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  52145. memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
  52146. memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
  52147. memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
  52148. /*
  52149. * Add new CRL on the end of the chain if needed.
  52150. */
  52151. while( crl->version != 0 && crl->next != NULL )
  52152. crl = crl->next;
  52153. if( crl->version != 0 && crl->next == NULL )
  52154. {
  52155. crl->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) );
  52156. if( crl->next == NULL )
  52157. {
  52158. mbedtls_x509_crl_free( crl );
  52159. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  52160. }
  52161. mbedtls_x509_crl_init( crl->next );
  52162. crl = crl->next;
  52163. }
  52164. /*
  52165. * Copy raw DER-encoded CRL
  52166. */
  52167. if( buflen == 0 )
  52168. return( MBEDTLS_ERR_X509_INVALID_FORMAT );
  52169. p = mbedtls_calloc( 1, buflen );
  52170. if( p == NULL )
  52171. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  52172. memcpy( p, buf, buflen );
  52173. crl->raw.p = p;
  52174. crl->raw.len = buflen;
  52175. end = p + buflen;
  52176. /*
  52177. * CertificateList ::= SEQUENCE {
  52178. * tbsCertList TBSCertList,
  52179. * signatureAlgorithm AlgorithmIdentifier,
  52180. * signatureValue BIT STRING }
  52181. */
  52182. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  52183. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  52184. {
  52185. mbedtls_x509_crl_free( crl );
  52186. return( MBEDTLS_ERR_X509_INVALID_FORMAT );
  52187. }
  52188. if( len != (size_t) ( end - p ) )
  52189. {
  52190. mbedtls_x509_crl_free( crl );
  52191. return( MBEDTLS_ERR_X509_INVALID_FORMAT +
  52192. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  52193. }
  52194. /*
  52195. * TBSCertList ::= SEQUENCE {
  52196. */
  52197. crl->tbs.p = p;
  52198. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  52199. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  52200. {
  52201. mbedtls_x509_crl_free( crl );
  52202. return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
  52203. }
  52204. end = p + len;
  52205. crl->tbs.len = end - crl->tbs.p;
  52206. /*
  52207. * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
  52208. * -- if present, MUST be v2
  52209. *
  52210. * signature AlgorithmIdentifier
  52211. */
  52212. if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
  52213. ( ret = mbedtls_x509_get_alg( &p, end, &crl->sig_oid, &sig_params1 ) ) != 0 )
  52214. {
  52215. mbedtls_x509_crl_free( crl );
  52216. return( ret );
  52217. }
  52218. if( crl->version < 0 || crl->version > 1 )
  52219. {
  52220. mbedtls_x509_crl_free( crl );
  52221. return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
  52222. }
  52223. crl->version++;
  52224. if( ( ret = mbedtls_x509_get_sig_alg( &crl->sig_oid, &sig_params1,
  52225. &crl->sig_md, &crl->sig_pk,
  52226. &crl->sig_opts ) ) != 0 )
  52227. {
  52228. mbedtls_x509_crl_free( crl );
  52229. return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG );
  52230. }
  52231. /*
  52232. * issuer Name
  52233. */
  52234. crl->issuer_raw.p = p;
  52235. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  52236. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  52237. {
  52238. mbedtls_x509_crl_free( crl );
  52239. return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
  52240. }
  52241. if( ( ret = mbedtls_x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
  52242. {
  52243. mbedtls_x509_crl_free( crl );
  52244. return( ret );
  52245. }
  52246. crl->issuer_raw.len = p - crl->issuer_raw.p;
  52247. /*
  52248. * thisUpdate Time
  52249. * nextUpdate Time OPTIONAL
  52250. */
  52251. if( ( ret = mbedtls_x509_get_time( &p, end, &crl->this_update ) ) != 0 )
  52252. {
  52253. mbedtls_x509_crl_free( crl );
  52254. return( ret );
  52255. }
  52256. if( ( ret = mbedtls_x509_get_time( &p, end, &crl->next_update ) ) != 0 )
  52257. {
  52258. if( ret != ( MBEDTLS_ERR_X509_INVALID_DATE +
  52259. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) &&
  52260. ret != ( MBEDTLS_ERR_X509_INVALID_DATE +
  52261. MBEDTLS_ERR_ASN1_OUT_OF_DATA ) )
  52262. {
  52263. mbedtls_x509_crl_free( crl );
  52264. return( ret );
  52265. }
  52266. }
  52267. /*
  52268. * revokedCertificates SEQUENCE OF SEQUENCE {
  52269. * userCertificate CertificateSerialNumber,
  52270. * revocationDate Time,
  52271. * crlEntryExtensions Extensions OPTIONAL
  52272. * -- if present, MUST be v2
  52273. * } OPTIONAL
  52274. */
  52275. if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
  52276. {
  52277. mbedtls_x509_crl_free( crl );
  52278. return( ret );
  52279. }
  52280. /*
  52281. * crlExtensions EXPLICIT Extensions OPTIONAL
  52282. * -- if present, MUST be v2
  52283. */
  52284. if( crl->version == 2 )
  52285. {
  52286. ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
  52287. if( ret != 0 )
  52288. {
  52289. mbedtls_x509_crl_free( crl );
  52290. return( ret );
  52291. }
  52292. }
  52293. if( p != end )
  52294. {
  52295. mbedtls_x509_crl_free( crl );
  52296. return( MBEDTLS_ERR_X509_INVALID_FORMAT +
  52297. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  52298. }
  52299. end = crl->raw.p + crl->raw.len;
  52300. /*
  52301. * signatureAlgorithm AlgorithmIdentifier,
  52302. * signatureValue BIT STRING
  52303. */
  52304. if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
  52305. {
  52306. mbedtls_x509_crl_free( crl );
  52307. return( ret );
  52308. }
  52309. if( crl->sig_oid.len != sig_oid2.len ||
  52310. memcmp( crl->sig_oid.p, sig_oid2.p, crl->sig_oid.len ) != 0 ||
  52311. sig_params1.len != sig_params2.len ||
  52312. ( sig_params1.len != 0 &&
  52313. memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
  52314. {
  52315. mbedtls_x509_crl_free( crl );
  52316. return( MBEDTLS_ERR_X509_SIG_MISMATCH );
  52317. }
  52318. if( ( ret = mbedtls_x509_get_sig( &p, end, &crl->sig ) ) != 0 )
  52319. {
  52320. mbedtls_x509_crl_free( crl );
  52321. return( ret );
  52322. }
  52323. if( p != end )
  52324. {
  52325. mbedtls_x509_crl_free( crl );
  52326. return( MBEDTLS_ERR_X509_INVALID_FORMAT +
  52327. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  52328. }
  52329. return( 0 );
  52330. }
  52331. /*
  52332. * Parse one or more CRLs and add them to the chained list
  52333. */
  52334. int mbedtls_x509_crl_parse( mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen )
  52335. {
  52336. #if defined(MBEDTLS_PEM_PARSE_C)
  52337. int ret;
  52338. size_t use_len;
  52339. mbedtls_pem_context pem;
  52340. int is_pem = 0;
  52341. if( chain == NULL || buf == NULL )
  52342. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  52343. do
  52344. {
  52345. mbedtls_pem_init( &pem );
  52346. // Avoid calling mbedtls_pem_read_buffer() on non-null-terminated
  52347. // string
  52348. if( buflen == 0 || buf[buflen - 1] != '\0' )
  52349. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  52350. else
  52351. ret = mbedtls_pem_read_buffer( &pem,
  52352. "-----BEGIN X509 CRL-----",
  52353. "-----END X509 CRL-----",
  52354. buf, NULL, 0, &use_len );
  52355. if( ret == 0 )
  52356. {
  52357. /*
  52358. * Was PEM encoded
  52359. */
  52360. is_pem = 1;
  52361. buflen -= use_len;
  52362. buf += use_len;
  52363. if( ( ret = mbedtls_x509_crl_parse_der( chain,
  52364. pem.buf, pem.buflen ) ) != 0 )
  52365. {
  52366. mbedtls_pem_free( &pem );
  52367. return( ret );
  52368. }
  52369. }
  52370. else if( is_pem )
  52371. {
  52372. mbedtls_pem_free( &pem );
  52373. return( ret );
  52374. }
  52375. mbedtls_pem_free( &pem );
  52376. }
  52377. /* In the PEM case, buflen is 1 at the end, for the terminated NULL byte.
  52378. * And a valid CRL cannot be less than 1 byte anyway. */
  52379. while( is_pem && buflen > 1 );
  52380. if( is_pem )
  52381. return( 0 );
  52382. else
  52383. #endif /* MBEDTLS_PEM_PARSE_C */
  52384. return( mbedtls_x509_crl_parse_der( chain, buf, buflen ) );
  52385. }
  52386. #if defined(MBEDTLS_FS_IO)
  52387. /*
  52388. * Load one or more CRLs and add them to the chained list
  52389. */
  52390. int mbedtls_x509_crl_parse_file( mbedtls_x509_crl *chain, const char *path )
  52391. {
  52392. int ret;
  52393. size_t n;
  52394. unsigned char *buf;
  52395. if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
  52396. return( ret );
  52397. ret = mbedtls_x509_crl_parse( chain, buf, n );
  52398. mbedtls_zeroize( buf, n );
  52399. mbedtls_free( buf );
  52400. return( ret );
  52401. }
  52402. #endif /* MBEDTLS_FS_IO */
  52403. /*
  52404. * Return an informational string about the certificate.
  52405. */
  52406. #define BEFORE_COLON 14
  52407. #define BC "14"
  52408. /*
  52409. * Return an informational string about the CRL.
  52410. */
  52411. int mbedtls_x509_crl_info( char *buf, size_t size, const char *prefix,
  52412. const mbedtls_x509_crl *crl )
  52413. {
  52414. int ret;
  52415. size_t n;
  52416. char *p;
  52417. const mbedtls_x509_crl_entry *entry;
  52418. p = buf;
  52419. n = size;
  52420. ret = mbedtls_snprintf( p, n, "%sCRL version : %d",
  52421. prefix, crl->version );
  52422. MBEDTLS_X509_SAFE_SNPRINTF;
  52423. ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
  52424. MBEDTLS_X509_SAFE_SNPRINTF;
  52425. ret = mbedtls_x509_dn_gets( p, n, &crl->issuer );
  52426. MBEDTLS_X509_SAFE_SNPRINTF;
  52427. ret = mbedtls_snprintf( p, n, "\n%sthis update : " \
  52428. "%04d-%02d-%02d %02d:%02d:%02d", prefix,
  52429. crl->this_update.year, crl->this_update.mon,
  52430. crl->this_update.day, crl->this_update.hour,
  52431. crl->this_update.min, crl->this_update.sec );
  52432. MBEDTLS_X509_SAFE_SNPRINTF;
  52433. ret = mbedtls_snprintf( p, n, "\n%snext update : " \
  52434. "%04d-%02d-%02d %02d:%02d:%02d", prefix,
  52435. crl->next_update.year, crl->next_update.mon,
  52436. crl->next_update.day, crl->next_update.hour,
  52437. crl->next_update.min, crl->next_update.sec );
  52438. MBEDTLS_X509_SAFE_SNPRINTF;
  52439. entry = &crl->entry;
  52440. ret = mbedtls_snprintf( p, n, "\n%sRevoked certificates:",
  52441. prefix );
  52442. MBEDTLS_X509_SAFE_SNPRINTF;
  52443. while( entry != NULL && entry->raw.len != 0 )
  52444. {
  52445. ret = mbedtls_snprintf( p, n, "\n%sserial number: ",
  52446. prefix );
  52447. MBEDTLS_X509_SAFE_SNPRINTF;
  52448. ret = mbedtls_x509_serial_gets( p, n, &entry->serial );
  52449. MBEDTLS_X509_SAFE_SNPRINTF;
  52450. ret = mbedtls_snprintf( p, n, " revocation date: " \
  52451. "%04d-%02d-%02d %02d:%02d:%02d",
  52452. entry->revocation_date.year, entry->revocation_date.mon,
  52453. entry->revocation_date.day, entry->revocation_date.hour,
  52454. entry->revocation_date.min, entry->revocation_date.sec );
  52455. MBEDTLS_X509_SAFE_SNPRINTF;
  52456. entry = entry->next;
  52457. }
  52458. ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
  52459. MBEDTLS_X509_SAFE_SNPRINTF;
  52460. ret = mbedtls_x509_sig_alg_gets( p, n, &crl->sig_oid, crl->sig_pk, crl->sig_md,
  52461. crl->sig_opts );
  52462. MBEDTLS_X509_SAFE_SNPRINTF;
  52463. ret = mbedtls_snprintf( p, n, "\n" );
  52464. MBEDTLS_X509_SAFE_SNPRINTF;
  52465. return( (int) ( size - n ) );
  52466. }
  52467. /*
  52468. * Initialize a CRL chain
  52469. */
  52470. void mbedtls_x509_crl_init( mbedtls_x509_crl *crl )
  52471. {
  52472. memset( crl, 0, sizeof(mbedtls_x509_crl) );
  52473. }
  52474. /*
  52475. * Unallocate all CRL data
  52476. */
  52477. void mbedtls_x509_crl_free( mbedtls_x509_crl *crl )
  52478. {
  52479. mbedtls_x509_crl *crl_cur = crl;
  52480. mbedtls_x509_crl *crl_prv;
  52481. mbedtls_x509_name *name_cur;
  52482. mbedtls_x509_name *name_prv;
  52483. mbedtls_x509_crl_entry *entry_cur;
  52484. mbedtls_x509_crl_entry *entry_prv;
  52485. if( crl == NULL )
  52486. return;
  52487. do
  52488. {
  52489. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  52490. mbedtls_free( crl_cur->sig_opts );
  52491. #endif
  52492. name_cur = crl_cur->issuer.next;
  52493. while( name_cur != NULL )
  52494. {
  52495. name_prv = name_cur;
  52496. name_cur = name_cur->next;
  52497. mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
  52498. mbedtls_free( name_prv );
  52499. }
  52500. entry_cur = crl_cur->entry.next;
  52501. while( entry_cur != NULL )
  52502. {
  52503. entry_prv = entry_cur;
  52504. entry_cur = entry_cur->next;
  52505. mbedtls_zeroize( entry_prv, sizeof( mbedtls_x509_crl_entry ) );
  52506. mbedtls_free( entry_prv );
  52507. }
  52508. if( crl_cur->raw.p != NULL )
  52509. {
  52510. mbedtls_zeroize( crl_cur->raw.p, crl_cur->raw.len );
  52511. mbedtls_free( crl_cur->raw.p );
  52512. }
  52513. crl_cur = crl_cur->next;
  52514. }
  52515. while( crl_cur != NULL );
  52516. crl_cur = crl;
  52517. do
  52518. {
  52519. crl_prv = crl_cur;
  52520. crl_cur = crl_cur->next;
  52521. mbedtls_zeroize( crl_prv, sizeof( mbedtls_x509_crl ) );
  52522. if( crl_prv != crl )
  52523. mbedtls_free( crl_prv );
  52524. }
  52525. while( crl_cur != NULL );
  52526. }
  52527. #endif /* MBEDTLS_X509_CRL_PARSE_C */
  52528. /*
  52529. Amalgamated build undefines
  52530. */
  52531. #undef ADD
  52532. #undef BC
  52533. #undef BEFORE_COLON
  52534. #undef F
  52535. #undef F0
  52536. #undef F1
  52537. #undef F2
  52538. #undef F3
  52539. #undef F4
  52540. #undef F5
  52541. #undef FSb
  52542. #undef K
  52543. #undef KK
  52544. #undef P
  52545. #undef R
  52546. #undef ROTR
  52547. #undef S
  52548. #undef S0
  52549. #undef S1
  52550. #undef S2
  52551. #undef S3
  52552. #undef SAFE_SNPRINTF
  52553. #undef SHR
  52554. #undef close
  52555. #undef read
  52556. #undef supported_init
  52557. #undef write
  52558. /********* Start of file library/x509_crt.c ************/
  52559. /*
  52560. * X.509 certificate parsing and verification
  52561. *
  52562. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  52563. * SPDX-License-Identifier: Apache-2.0
  52564. *
  52565. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  52566. * not use this file except in compliance with the License.
  52567. * You may obtain a copy of the License at
  52568. *
  52569. * http://www.apache.org/licenses/LICENSE-2.0
  52570. *
  52571. * Unless required by applicable law or agreed to in writing, software
  52572. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  52573. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  52574. * See the License for the specific language governing permissions and
  52575. * limitations under the License.
  52576. *
  52577. * This file is part of mbed TLS (https://tls.mbed.org)
  52578. */
  52579. /*
  52580. * The ITU-T X.509 standard defines a certificate format for PKI.
  52581. *
  52582. * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
  52583. * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
  52584. * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
  52585. *
  52586. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
  52587. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
  52588. */
  52589. #if !defined(MBEDTLS_CONFIG_FILE)
  52590. #else
  52591. #endif
  52592. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  52593. #include <stdio.h>
  52594. #include <string.h>
  52595. #if defined(MBEDTLS_PEM_PARSE_C)
  52596. #endif
  52597. #if defined(MBEDTLS_PLATFORM_C)
  52598. #else
  52599. #include <stdlib.h>
  52600. #define mbedtls_free free
  52601. #define mbedtls_calloc calloc
  52602. #define mbedtls_snprintf snprintf
  52603. #endif
  52604. #if defined(MBEDTLS_THREADING_C)
  52605. #endif
  52606. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  52607. #include <windows.h>
  52608. #else
  52609. #include <time.h>
  52610. #endif
  52611. #if defined(MBEDTLS_FS_IO)
  52612. #include <stdio.h>
  52613. #if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
  52614. #include <sys/types.h>
  52615. #include <sys/stat.h>
  52616. #include <dirent.h>
  52617. #endif /* !_WIN32 || EFIX64 || EFI32 */
  52618. #endif
  52619. /* Implementation that should never be optimized out by the compiler */
  52620. /* zeroize was here */
  52621. /*
  52622. * Default profile
  52623. */
  52624. const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
  52625. {
  52626. #if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
  52627. /* Allow SHA-1 (weak, but still safe in controlled environments) */
  52628. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
  52629. #endif
  52630. /* Only SHA-2 hashes */
  52631. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
  52632. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
  52633. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
  52634. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
  52635. 0xFFFFFFF, /* Any PK alg */
  52636. 0xFFFFFFF, /* Any curve */
  52637. 2048,
  52638. };
  52639. /*
  52640. * Next-default profile
  52641. */
  52642. const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
  52643. {
  52644. /* Hashes from SHA-256 and above */
  52645. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
  52646. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
  52647. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
  52648. 0xFFFFFFF, /* Any PK alg */
  52649. #if defined(MBEDTLS_ECP_C)
  52650. /* Curves at or above 128-bit security level */
  52651. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
  52652. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
  52653. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
  52654. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
  52655. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
  52656. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
  52657. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
  52658. #else
  52659. 0,
  52660. #endif
  52661. 2048,
  52662. };
  52663. /*
  52664. * NSA Suite B Profile
  52665. */
  52666. const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
  52667. {
  52668. /* Only SHA-256 and 384 */
  52669. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
  52670. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
  52671. /* Only ECDSA */
  52672. MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
  52673. MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
  52674. #if defined(MBEDTLS_ECP_C)
  52675. /* Only NIST P-256 and P-384 */
  52676. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
  52677. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
  52678. #else
  52679. 0,
  52680. #endif
  52681. 0,
  52682. };
  52683. /*
  52684. * Check md_alg against profile
  52685. * Return 0 if md_alg acceptable for this profile, -1 otherwise
  52686. */
  52687. static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
  52688. mbedtls_md_type_t md_alg )
  52689. {
  52690. if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
  52691. return( 0 );
  52692. return( -1 );
  52693. }
  52694. /*
  52695. * Check pk_alg against profile
  52696. * Return 0 if pk_alg acceptable for this profile, -1 otherwise
  52697. */
  52698. static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
  52699. mbedtls_pk_type_t pk_alg )
  52700. {
  52701. if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
  52702. return( 0 );
  52703. return( -1 );
  52704. }
  52705. /*
  52706. * Check key against profile
  52707. * Return 0 if pk_alg acceptable for this profile, -1 otherwise
  52708. */
  52709. static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
  52710. mbedtls_pk_type_t pk_alg,
  52711. const mbedtls_pk_context *pk )
  52712. {
  52713. #if defined(MBEDTLS_RSA_C)
  52714. if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
  52715. {
  52716. if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
  52717. return( 0 );
  52718. return( -1 );
  52719. }
  52720. #endif
  52721. #if defined(MBEDTLS_ECP_C)
  52722. if( pk_alg == MBEDTLS_PK_ECDSA ||
  52723. pk_alg == MBEDTLS_PK_ECKEY ||
  52724. pk_alg == MBEDTLS_PK_ECKEY_DH )
  52725. {
  52726. mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
  52727. if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
  52728. return( 0 );
  52729. return( -1 );
  52730. }
  52731. #endif
  52732. return( -1 );
  52733. }
  52734. /*
  52735. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  52736. */
  52737. static int x509_get_version( unsigned char **p,
  52738. const unsigned char *end,
  52739. int *ver )
  52740. {
  52741. int ret;
  52742. size_t len;
  52743. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  52744. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
  52745. {
  52746. if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  52747. {
  52748. *ver = 0;
  52749. return( 0 );
  52750. }
  52751. return( ret );
  52752. }
  52753. end = *p + len;
  52754. if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
  52755. return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
  52756. if( *p != end )
  52757. return( MBEDTLS_ERR_X509_INVALID_VERSION +
  52758. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  52759. return( 0 );
  52760. }
  52761. /*
  52762. * Validity ::= SEQUENCE {
  52763. * notBefore Time,
  52764. * notAfter Time }
  52765. */
  52766. static int x509_get_dates( unsigned char **p,
  52767. const unsigned char *end,
  52768. mbedtls_x509_time *from,
  52769. mbedtls_x509_time *to )
  52770. {
  52771. int ret;
  52772. size_t len;
  52773. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  52774. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  52775. return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
  52776. end = *p + len;
  52777. if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
  52778. return( ret );
  52779. if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
  52780. return( ret );
  52781. if( *p != end )
  52782. return( MBEDTLS_ERR_X509_INVALID_DATE +
  52783. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  52784. return( 0 );
  52785. }
  52786. /*
  52787. * X.509 v2/v3 unique identifier (not parsed)
  52788. */
  52789. static int x509_get_uid( unsigned char **p,
  52790. const unsigned char *end,
  52791. mbedtls_x509_buf *uid, int n )
  52792. {
  52793. int ret;
  52794. if( *p == end )
  52795. return( 0 );
  52796. uid->tag = **p;
  52797. if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
  52798. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
  52799. {
  52800. if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  52801. return( 0 );
  52802. return( ret );
  52803. }
  52804. uid->p = *p;
  52805. *p += uid->len;
  52806. return( 0 );
  52807. }
  52808. static int x509_get_basic_constraints( unsigned char **p,
  52809. const unsigned char *end,
  52810. int *ca_istrue,
  52811. int *max_pathlen )
  52812. {
  52813. int ret;
  52814. size_t len;
  52815. /*
  52816. * BasicConstraints ::= SEQUENCE {
  52817. * cA BOOLEAN DEFAULT FALSE,
  52818. * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
  52819. */
  52820. *ca_istrue = 0; /* DEFAULT FALSE */
  52821. *max_pathlen = 0; /* endless */
  52822. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  52823. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  52824. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  52825. if( *p == end )
  52826. return( 0 );
  52827. if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
  52828. {
  52829. if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  52830. ret = mbedtls_asn1_get_int( p, end, ca_istrue );
  52831. if( ret != 0 )
  52832. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  52833. if( *ca_istrue != 0 )
  52834. *ca_istrue = 1;
  52835. }
  52836. if( *p == end )
  52837. return( 0 );
  52838. if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
  52839. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  52840. if( *p != end )
  52841. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  52842. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  52843. (*max_pathlen)++;
  52844. return( 0 );
  52845. }
  52846. static int x509_get_ns_cert_type( unsigned char **p,
  52847. const unsigned char *end,
  52848. unsigned char *ns_cert_type)
  52849. {
  52850. int ret;
  52851. mbedtls_x509_bitstring bs = { 0, 0, NULL };
  52852. if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
  52853. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  52854. if( bs.len != 1 )
  52855. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  52856. MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  52857. /* Get actual bitstring */
  52858. *ns_cert_type = *bs.p;
  52859. return( 0 );
  52860. }
  52861. static int x509_get_key_usage( unsigned char **p,
  52862. const unsigned char *end,
  52863. unsigned int *key_usage)
  52864. {
  52865. int ret;
  52866. size_t i;
  52867. mbedtls_x509_bitstring bs = { 0, 0, NULL };
  52868. if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
  52869. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  52870. if( bs.len < 1 )
  52871. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  52872. MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  52873. /* Get actual bitstring */
  52874. *key_usage = 0;
  52875. for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
  52876. {
  52877. *key_usage |= (unsigned int) bs.p[i] << (8*i);
  52878. }
  52879. return( 0 );
  52880. }
  52881. /*
  52882. * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
  52883. *
  52884. * KeyPurposeId ::= OBJECT IDENTIFIER
  52885. */
  52886. static int x509_get_ext_key_usage( unsigned char **p,
  52887. const unsigned char *end,
  52888. mbedtls_x509_sequence *ext_key_usage)
  52889. {
  52890. int ret;
  52891. if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
  52892. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  52893. /* Sequence length must be >= 1 */
  52894. if( ext_key_usage->buf.p == NULL )
  52895. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  52896. MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  52897. return( 0 );
  52898. }
  52899. /*
  52900. * SubjectAltName ::= GeneralNames
  52901. *
  52902. * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
  52903. *
  52904. * GeneralName ::= CHOICE {
  52905. * otherName [0] OtherName,
  52906. * rfc822Name [1] IA5String,
  52907. * dNSName [2] IA5String,
  52908. * x400Address [3] ORAddress,
  52909. * directoryName [4] Name,
  52910. * ediPartyName [5] EDIPartyName,
  52911. * uniformResourceIdentifier [6] IA5String,
  52912. * iPAddress [7] OCTET STRING,
  52913. * registeredID [8] OBJECT IDENTIFIER }
  52914. *
  52915. * OtherName ::= SEQUENCE {
  52916. * type-id OBJECT IDENTIFIER,
  52917. * value [0] EXPLICIT ANY DEFINED BY type-id }
  52918. *
  52919. * EDIPartyName ::= SEQUENCE {
  52920. * nameAssigner [0] DirectoryString OPTIONAL,
  52921. * partyName [1] DirectoryString }
  52922. *
  52923. * NOTE: we only parse and use dNSName at this point.
  52924. */
  52925. static int x509_get_subject_alt_name( unsigned char **p,
  52926. const unsigned char *end,
  52927. mbedtls_x509_sequence *subject_alt_name )
  52928. {
  52929. int ret;
  52930. size_t len, tag_len;
  52931. mbedtls_asn1_buf *buf;
  52932. unsigned char tag;
  52933. mbedtls_asn1_sequence *cur = subject_alt_name;
  52934. /* Get main sequence tag */
  52935. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  52936. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  52937. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  52938. if( *p + len != end )
  52939. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  52940. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  52941. while( *p < end )
  52942. {
  52943. if( ( end - *p ) < 1 )
  52944. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  52945. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  52946. tag = **p;
  52947. (*p)++;
  52948. if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
  52949. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  52950. if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
  52951. MBEDTLS_ASN1_CONTEXT_SPECIFIC )
  52952. {
  52953. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  52954. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  52955. }
  52956. /* Skip everything but DNS name */
  52957. if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
  52958. {
  52959. *p += tag_len;
  52960. continue;
  52961. }
  52962. /* Allocate and assign next pointer */
  52963. if( cur->buf.p != NULL )
  52964. {
  52965. if( cur->next != NULL )
  52966. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
  52967. cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
  52968. if( cur->next == NULL )
  52969. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  52970. MBEDTLS_ERR_ASN1_ALLOC_FAILED );
  52971. cur = cur->next;
  52972. }
  52973. buf = &(cur->buf);
  52974. buf->tag = tag;
  52975. buf->p = *p;
  52976. buf->len = tag_len;
  52977. *p += buf->len;
  52978. }
  52979. /* Set final sequence entry's next pointer to NULL */
  52980. cur->next = NULL;
  52981. if( *p != end )
  52982. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  52983. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  52984. return( 0 );
  52985. }
  52986. /*
  52987. * X.509 v3 extensions
  52988. *
  52989. */
  52990. static int x509_get_crt_ext( unsigned char **p,
  52991. const unsigned char *end,
  52992. mbedtls_x509_crt *crt )
  52993. {
  52994. int ret;
  52995. size_t len;
  52996. unsigned char *end_ext_data, *end_ext_octet;
  52997. if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
  52998. {
  52999. if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  53000. return( 0 );
  53001. return( ret );
  53002. }
  53003. while( *p < end )
  53004. {
  53005. /*
  53006. * Extension ::= SEQUENCE {
  53007. * extnID OBJECT IDENTIFIER,
  53008. * critical BOOLEAN DEFAULT FALSE,
  53009. * extnValue OCTET STRING }
  53010. */
  53011. mbedtls_x509_buf extn_oid = {0, 0, NULL};
  53012. int is_critical = 0; /* DEFAULT FALSE */
  53013. int ext_type = 0;
  53014. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  53015. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  53016. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  53017. end_ext_data = *p + len;
  53018. /* Get extension ID */
  53019. extn_oid.tag = **p;
  53020. if( ( ret = mbedtls_asn1_get_tag( p, end, &extn_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
  53021. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  53022. extn_oid.p = *p;
  53023. *p += extn_oid.len;
  53024. if( ( end - *p ) < 1 )
  53025. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  53026. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  53027. /* Get optional critical */
  53028. if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
  53029. ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
  53030. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  53031. /* Data should be octet string type */
  53032. if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
  53033. MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  53034. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  53035. end_ext_octet = *p + len;
  53036. if( end_ext_octet != end_ext_data )
  53037. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  53038. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  53039. /*
  53040. * Detect supported extensions
  53041. */
  53042. ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
  53043. if( ret != 0 )
  53044. {
  53045. /* No parser found, skip extension */
  53046. *p = end_ext_octet;
  53047. #if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
  53048. if( is_critical )
  53049. {
  53050. /* Data is marked as critical: fail */
  53051. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  53052. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  53053. }
  53054. #endif
  53055. continue;
  53056. }
  53057. /* Forbid repeated extensions */
  53058. if( ( crt->ext_types & ext_type ) != 0 )
  53059. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
  53060. crt->ext_types |= ext_type;
  53061. switch( ext_type )
  53062. {
  53063. case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
  53064. /* Parse basic constraints */
  53065. if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
  53066. &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
  53067. return( ret );
  53068. break;
  53069. case MBEDTLS_X509_EXT_KEY_USAGE:
  53070. /* Parse key usage */
  53071. if( ( ret = x509_get_key_usage( p, end_ext_octet,
  53072. &crt->key_usage ) ) != 0 )
  53073. return( ret );
  53074. break;
  53075. case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
  53076. /* Parse extended key usage */
  53077. if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
  53078. &crt->ext_key_usage ) ) != 0 )
  53079. return( ret );
  53080. break;
  53081. case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
  53082. /* Parse subject alt name */
  53083. if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
  53084. &crt->subject_alt_names ) ) != 0 )
  53085. return( ret );
  53086. break;
  53087. case MBEDTLS_X509_EXT_NS_CERT_TYPE:
  53088. /* Parse netscape certificate type */
  53089. if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
  53090. &crt->ns_cert_type ) ) != 0 )
  53091. return( ret );
  53092. break;
  53093. default:
  53094. return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
  53095. }
  53096. }
  53097. if( *p != end )
  53098. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  53099. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  53100. return( 0 );
  53101. }
  53102. /*
  53103. * Parse and fill a single X.509 certificate in DER format
  53104. */
  53105. static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
  53106. size_t buflen )
  53107. {
  53108. int ret;
  53109. size_t len;
  53110. unsigned char *p, *end, *crt_end;
  53111. mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
  53112. memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
  53113. memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
  53114. memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
  53115. /*
  53116. * Check for valid input
  53117. */
  53118. if( crt == NULL || buf == NULL )
  53119. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  53120. // Use the original buffer until we figure out actual length
  53121. p = (unsigned char*) buf;
  53122. len = buflen;
  53123. end = p + len;
  53124. /*
  53125. * Certificate ::= SEQUENCE {
  53126. * tbsCertificate TBSCertificate,
  53127. * signatureAlgorithm AlgorithmIdentifier,
  53128. * signatureValue BIT STRING }
  53129. */
  53130. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  53131. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  53132. {
  53133. mbedtls_x509_crt_free( crt );
  53134. return( MBEDTLS_ERR_X509_INVALID_FORMAT );
  53135. }
  53136. if( len > (size_t) ( end - p ) )
  53137. {
  53138. mbedtls_x509_crt_free( crt );
  53139. return( MBEDTLS_ERR_X509_INVALID_FORMAT +
  53140. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  53141. }
  53142. crt_end = p + len;
  53143. // Create and populate a new buffer for the raw field
  53144. crt->raw.len = crt_end - buf;
  53145. crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
  53146. if( p == NULL )
  53147. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  53148. memcpy( p, buf, crt->raw.len );
  53149. // Direct pointers to the new buffer
  53150. p += crt->raw.len - len;
  53151. end = crt_end = p + len;
  53152. /*
  53153. * TBSCertificate ::= SEQUENCE {
  53154. */
  53155. crt->tbs.p = p;
  53156. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  53157. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  53158. {
  53159. mbedtls_x509_crt_free( crt );
  53160. return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
  53161. }
  53162. end = p + len;
  53163. crt->tbs.len = end - crt->tbs.p;
  53164. /*
  53165. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  53166. *
  53167. * CertificateSerialNumber ::= INTEGER
  53168. *
  53169. * signature AlgorithmIdentifier
  53170. */
  53171. if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
  53172. ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
  53173. ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
  53174. &sig_params1 ) ) != 0 )
  53175. {
  53176. mbedtls_x509_crt_free( crt );
  53177. return( ret );
  53178. }
  53179. if( crt->version < 0 || crt->version > 2 )
  53180. {
  53181. mbedtls_x509_crt_free( crt );
  53182. return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
  53183. }
  53184. crt->version++;
  53185. if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
  53186. &crt->sig_md, &crt->sig_pk,
  53187. &crt->sig_opts ) ) != 0 )
  53188. {
  53189. mbedtls_x509_crt_free( crt );
  53190. return( ret );
  53191. }
  53192. /*
  53193. * issuer Name
  53194. */
  53195. crt->issuer_raw.p = p;
  53196. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  53197. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  53198. {
  53199. mbedtls_x509_crt_free( crt );
  53200. return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
  53201. }
  53202. if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
  53203. {
  53204. mbedtls_x509_crt_free( crt );
  53205. return( ret );
  53206. }
  53207. crt->issuer_raw.len = p - crt->issuer_raw.p;
  53208. /*
  53209. * Validity ::= SEQUENCE {
  53210. * notBefore Time,
  53211. * notAfter Time }
  53212. *
  53213. */
  53214. if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
  53215. &crt->valid_to ) ) != 0 )
  53216. {
  53217. mbedtls_x509_crt_free( crt );
  53218. return( ret );
  53219. }
  53220. /*
  53221. * subject Name
  53222. */
  53223. crt->subject_raw.p = p;
  53224. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  53225. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  53226. {
  53227. mbedtls_x509_crt_free( crt );
  53228. return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
  53229. }
  53230. if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
  53231. {
  53232. mbedtls_x509_crt_free( crt );
  53233. return( ret );
  53234. }
  53235. crt->subject_raw.len = p - crt->subject_raw.p;
  53236. /*
  53237. * SubjectPublicKeyInfo
  53238. */
  53239. if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
  53240. {
  53241. mbedtls_x509_crt_free( crt );
  53242. return( ret );
  53243. }
  53244. /*
  53245. * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
  53246. * -- If present, version shall be v2 or v3
  53247. * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
  53248. * -- If present, version shall be v2 or v3
  53249. * extensions [3] EXPLICIT Extensions OPTIONAL
  53250. * -- If present, version shall be v3
  53251. */
  53252. if( crt->version == 2 || crt->version == 3 )
  53253. {
  53254. ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
  53255. if( ret != 0 )
  53256. {
  53257. mbedtls_x509_crt_free( crt );
  53258. return( ret );
  53259. }
  53260. }
  53261. if( crt->version == 2 || crt->version == 3 )
  53262. {
  53263. ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
  53264. if( ret != 0 )
  53265. {
  53266. mbedtls_x509_crt_free( crt );
  53267. return( ret );
  53268. }
  53269. }
  53270. #if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
  53271. if( crt->version == 3 )
  53272. #endif
  53273. {
  53274. ret = x509_get_crt_ext( &p, end, crt );
  53275. if( ret != 0 )
  53276. {
  53277. mbedtls_x509_crt_free( crt );
  53278. return( ret );
  53279. }
  53280. }
  53281. if( p != end )
  53282. {
  53283. mbedtls_x509_crt_free( crt );
  53284. return( MBEDTLS_ERR_X509_INVALID_FORMAT +
  53285. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  53286. }
  53287. end = crt_end;
  53288. /*
  53289. * }
  53290. * -- end of TBSCertificate
  53291. *
  53292. * signatureAlgorithm AlgorithmIdentifier,
  53293. * signatureValue BIT STRING
  53294. */
  53295. if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
  53296. {
  53297. mbedtls_x509_crt_free( crt );
  53298. return( ret );
  53299. }
  53300. if( crt->sig_oid.len != sig_oid2.len ||
  53301. memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
  53302. sig_params1.len != sig_params2.len ||
  53303. ( sig_params1.len != 0 &&
  53304. memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
  53305. {
  53306. mbedtls_x509_crt_free( crt );
  53307. return( MBEDTLS_ERR_X509_SIG_MISMATCH );
  53308. }
  53309. if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
  53310. {
  53311. mbedtls_x509_crt_free( crt );
  53312. return( ret );
  53313. }
  53314. if( p != end )
  53315. {
  53316. mbedtls_x509_crt_free( crt );
  53317. return( MBEDTLS_ERR_X509_INVALID_FORMAT +
  53318. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  53319. }
  53320. return( 0 );
  53321. }
  53322. /*
  53323. * Parse one X.509 certificate in DER format from a buffer and add them to a
  53324. * chained list
  53325. */
  53326. int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
  53327. size_t buflen )
  53328. {
  53329. int ret;
  53330. mbedtls_x509_crt *crt = chain, *prev = NULL;
  53331. /*
  53332. * Check for valid input
  53333. */
  53334. if( crt == NULL || buf == NULL )
  53335. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  53336. while( crt->version != 0 && crt->next != NULL )
  53337. {
  53338. prev = crt;
  53339. crt = crt->next;
  53340. }
  53341. /*
  53342. * Add new certificate on the end of the chain if needed.
  53343. */
  53344. if( crt->version != 0 && crt->next == NULL )
  53345. {
  53346. crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
  53347. if( crt->next == NULL )
  53348. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  53349. prev = crt;
  53350. mbedtls_x509_crt_init( crt->next );
  53351. crt = crt->next;
  53352. }
  53353. if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
  53354. {
  53355. if( prev )
  53356. prev->next = NULL;
  53357. if( crt != chain )
  53358. mbedtls_free( crt );
  53359. return( ret );
  53360. }
  53361. return( 0 );
  53362. }
  53363. /*
  53364. * Parse one or more PEM certificates from a buffer and add them to the chained
  53365. * list
  53366. */
  53367. int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
  53368. {
  53369. #if defined(MBEDTLS_PEM_PARSE_C)
  53370. int success = 0, first_error = 0, total_failed = 0;
  53371. int buf_format = MBEDTLS_X509_FORMAT_DER;
  53372. #endif
  53373. /*
  53374. * Check for valid input
  53375. */
  53376. if( chain == NULL || buf == NULL )
  53377. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  53378. /*
  53379. * Determine buffer content. Buffer contains either one DER certificate or
  53380. * one or more PEM certificates.
  53381. */
  53382. #if defined(MBEDTLS_PEM_PARSE_C)
  53383. if( buflen != 0 && buf[buflen - 1] == '\0' &&
  53384. strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
  53385. {
  53386. buf_format = MBEDTLS_X509_FORMAT_PEM;
  53387. }
  53388. if( buf_format == MBEDTLS_X509_FORMAT_DER )
  53389. return mbedtls_x509_crt_parse_der( chain, buf, buflen );
  53390. #else
  53391. return mbedtls_x509_crt_parse_der( chain, buf, buflen );
  53392. #endif
  53393. #if defined(MBEDTLS_PEM_PARSE_C)
  53394. if( buf_format == MBEDTLS_X509_FORMAT_PEM )
  53395. {
  53396. int ret;
  53397. mbedtls_pem_context pem;
  53398. /* 1 rather than 0 since the terminating NULL byte is counted in */
  53399. while( buflen > 1 )
  53400. {
  53401. size_t use_len;
  53402. mbedtls_pem_init( &pem );
  53403. /* If we get there, we know the string is null-terminated */
  53404. ret = mbedtls_pem_read_buffer( &pem,
  53405. "-----BEGIN CERTIFICATE-----",
  53406. "-----END CERTIFICATE-----",
  53407. buf, NULL, 0, &use_len );
  53408. if( ret == 0 )
  53409. {
  53410. /*
  53411. * Was PEM encoded
  53412. */
  53413. buflen -= use_len;
  53414. buf += use_len;
  53415. }
  53416. else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
  53417. {
  53418. return( ret );
  53419. }
  53420. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  53421. {
  53422. mbedtls_pem_free( &pem );
  53423. /*
  53424. * PEM header and footer were found
  53425. */
  53426. buflen -= use_len;
  53427. buf += use_len;
  53428. if( first_error == 0 )
  53429. first_error = ret;
  53430. total_failed++;
  53431. continue;
  53432. }
  53433. else
  53434. break;
  53435. ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
  53436. mbedtls_pem_free( &pem );
  53437. if( ret != 0 )
  53438. {
  53439. /*
  53440. * Quit parsing on a memory error
  53441. */
  53442. if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
  53443. return( ret );
  53444. if( first_error == 0 )
  53445. first_error = ret;
  53446. total_failed++;
  53447. continue;
  53448. }
  53449. success = 1;
  53450. }
  53451. }
  53452. if( success )
  53453. return( total_failed );
  53454. else if( first_error )
  53455. return( first_error );
  53456. else
  53457. return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
  53458. #endif /* MBEDTLS_PEM_PARSE_C */
  53459. }
  53460. #if defined(MBEDTLS_FS_IO)
  53461. /*
  53462. * Load one or more certificates and add them to the chained list
  53463. */
  53464. int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
  53465. {
  53466. int ret;
  53467. size_t n;
  53468. unsigned char *buf;
  53469. if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
  53470. return( ret );
  53471. ret = mbedtls_x509_crt_parse( chain, buf, n );
  53472. mbedtls_zeroize( buf, n );
  53473. mbedtls_free( buf );
  53474. return( ret );
  53475. }
  53476. int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
  53477. {
  53478. int ret = 0;
  53479. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  53480. int w_ret;
  53481. WCHAR szDir[MAX_PATH];
  53482. char filename[MAX_PATH];
  53483. char *p;
  53484. size_t len = strlen( path );
  53485. WIN32_FIND_DATAW file_data;
  53486. HANDLE hFind;
  53487. if( len > MAX_PATH - 3 )
  53488. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  53489. memset( szDir, 0, sizeof(szDir) );
  53490. memset( filename, 0, MAX_PATH );
  53491. memcpy( filename, path, len );
  53492. filename[len++] = '\\';
  53493. p = filename + len;
  53494. filename[len++] = '*';
  53495. w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
  53496. MAX_PATH - 3 );
  53497. if( w_ret == 0 )
  53498. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  53499. hFind = FindFirstFileW( szDir, &file_data );
  53500. if( hFind == INVALID_HANDLE_VALUE )
  53501. return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
  53502. len = MAX_PATH - len;
  53503. do
  53504. {
  53505. memset( p, 0, len );
  53506. if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
  53507. continue;
  53508. w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
  53509. lstrlenW( file_data.cFileName ),
  53510. p, (int) len - 1,
  53511. NULL, NULL );
  53512. if( w_ret == 0 )
  53513. {
  53514. ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
  53515. goto cleanup;
  53516. }
  53517. w_ret = mbedtls_x509_crt_parse_file( chain, filename );
  53518. if( w_ret < 0 )
  53519. ret++;
  53520. else
  53521. ret += w_ret;
  53522. }
  53523. while( FindNextFileW( hFind, &file_data ) != 0 );
  53524. if( GetLastError() != ERROR_NO_MORE_FILES )
  53525. ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
  53526. cleanup:
  53527. FindClose( hFind );
  53528. #else /* _WIN32 */
  53529. int t_ret;
  53530. int snp_ret;
  53531. struct stat sb;
  53532. struct dirent *entry;
  53533. char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
  53534. DIR *dir = opendir( path );
  53535. if( dir == NULL )
  53536. return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
  53537. #if defined(MBEDTLS_THREADING_C)
  53538. if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
  53539. {
  53540. closedir( dir );
  53541. return( ret );
  53542. }
  53543. #endif /* MBEDTLS_THREADING_C */
  53544. while( ( entry = readdir( dir ) ) != NULL )
  53545. {
  53546. snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
  53547. "%s/%s", path, entry->d_name );
  53548. if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
  53549. {
  53550. ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
  53551. goto cleanup;
  53552. }
  53553. else if( stat( entry_name, &sb ) == -1 )
  53554. {
  53555. ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
  53556. goto cleanup;
  53557. }
  53558. if( !S_ISREG( sb.st_mode ) )
  53559. continue;
  53560. // Ignore parse errors
  53561. //
  53562. t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
  53563. if( t_ret < 0 )
  53564. ret++;
  53565. else
  53566. ret += t_ret;
  53567. }
  53568. cleanup:
  53569. closedir( dir );
  53570. #if defined(MBEDTLS_THREADING_C)
  53571. if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
  53572. ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
  53573. #endif /* MBEDTLS_THREADING_C */
  53574. #endif /* _WIN32 */
  53575. return( ret );
  53576. }
  53577. #endif /* MBEDTLS_FS_IO */
  53578. static int x509_info_subject_alt_name( char **buf, size_t *size,
  53579. const mbedtls_x509_sequence *subject_alt_name )
  53580. {
  53581. size_t i;
  53582. size_t n = *size;
  53583. char *p = *buf;
  53584. const mbedtls_x509_sequence *cur = subject_alt_name;
  53585. const char *sep = "";
  53586. size_t sep_len = 0;
  53587. while( cur != NULL )
  53588. {
  53589. if( cur->buf.len + sep_len >= n )
  53590. {
  53591. *p = '\0';
  53592. return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
  53593. }
  53594. n -= cur->buf.len + sep_len;
  53595. for( i = 0; i < sep_len; i++ )
  53596. *p++ = sep[i];
  53597. for( i = 0; i < cur->buf.len; i++ )
  53598. *p++ = cur->buf.p[i];
  53599. sep = ", ";
  53600. sep_len = 2;
  53601. cur = cur->next;
  53602. }
  53603. *p = '\0';
  53604. *size = n;
  53605. *buf = p;
  53606. return( 0 );
  53607. }
  53608. #define PRINT_ITEM(i) \
  53609. { \
  53610. ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
  53611. MBEDTLS_X509_SAFE_SNPRINTF; \
  53612. sep = ", "; \
  53613. }
  53614. #define CERT_TYPE(type,name) \
  53615. if( ns_cert_type & type ) \
  53616. PRINT_ITEM( name );
  53617. static int x509_info_cert_type( char **buf, size_t *size,
  53618. unsigned char ns_cert_type )
  53619. {
  53620. int ret;
  53621. size_t n = *size;
  53622. char *p = *buf;
  53623. const char *sep = "";
  53624. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
  53625. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
  53626. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
  53627. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
  53628. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
  53629. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
  53630. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
  53631. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
  53632. *size = n;
  53633. *buf = p;
  53634. return( 0 );
  53635. }
  53636. #define KEY_USAGE(code,name) \
  53637. if( key_usage & code ) \
  53638. PRINT_ITEM( name );
  53639. static int x509_info_key_usage( char **buf, size_t *size,
  53640. unsigned int key_usage )
  53641. {
  53642. int ret;
  53643. size_t n = *size;
  53644. char *p = *buf;
  53645. const char *sep = "";
  53646. KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
  53647. KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
  53648. KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
  53649. KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
  53650. KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
  53651. KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
  53652. KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
  53653. KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
  53654. KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
  53655. *size = n;
  53656. *buf = p;
  53657. return( 0 );
  53658. }
  53659. static int x509_info_ext_key_usage( char **buf, size_t *size,
  53660. const mbedtls_x509_sequence *extended_key_usage )
  53661. {
  53662. int ret;
  53663. const char *desc;
  53664. size_t n = *size;
  53665. char *p = *buf;
  53666. const mbedtls_x509_sequence *cur = extended_key_usage;
  53667. const char *sep = "";
  53668. while( cur != NULL )
  53669. {
  53670. if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
  53671. desc = "???";
  53672. ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
  53673. MBEDTLS_X509_SAFE_SNPRINTF;
  53674. sep = ", ";
  53675. cur = cur->next;
  53676. }
  53677. *size = n;
  53678. *buf = p;
  53679. return( 0 );
  53680. }
  53681. /*
  53682. * Return an informational string about the certificate.
  53683. */
  53684. #define BEFORE_COLON 18
  53685. #define BC "18"
  53686. int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
  53687. const mbedtls_x509_crt *crt )
  53688. {
  53689. int ret;
  53690. size_t n;
  53691. char *p;
  53692. char key_size_str[BEFORE_COLON];
  53693. p = buf;
  53694. n = size;
  53695. if( NULL == crt )
  53696. {
  53697. ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
  53698. MBEDTLS_X509_SAFE_SNPRINTF;
  53699. return( (int) ( size - n ) );
  53700. }
  53701. ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
  53702. prefix, crt->version );
  53703. MBEDTLS_X509_SAFE_SNPRINTF;
  53704. ret = mbedtls_snprintf( p, n, "%sserial number : ",
  53705. prefix );
  53706. MBEDTLS_X509_SAFE_SNPRINTF;
  53707. ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
  53708. MBEDTLS_X509_SAFE_SNPRINTF;
  53709. ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
  53710. MBEDTLS_X509_SAFE_SNPRINTF;
  53711. ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
  53712. MBEDTLS_X509_SAFE_SNPRINTF;
  53713. ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
  53714. MBEDTLS_X509_SAFE_SNPRINTF;
  53715. ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
  53716. MBEDTLS_X509_SAFE_SNPRINTF;
  53717. ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
  53718. "%04d-%02d-%02d %02d:%02d:%02d", prefix,
  53719. crt->valid_from.year, crt->valid_from.mon,
  53720. crt->valid_from.day, crt->valid_from.hour,
  53721. crt->valid_from.min, crt->valid_from.sec );
  53722. MBEDTLS_X509_SAFE_SNPRINTF;
  53723. ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
  53724. "%04d-%02d-%02d %02d:%02d:%02d", prefix,
  53725. crt->valid_to.year, crt->valid_to.mon,
  53726. crt->valid_to.day, crt->valid_to.hour,
  53727. crt->valid_to.min, crt->valid_to.sec );
  53728. MBEDTLS_X509_SAFE_SNPRINTF;
  53729. ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
  53730. MBEDTLS_X509_SAFE_SNPRINTF;
  53731. ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
  53732. crt->sig_md, crt->sig_opts );
  53733. MBEDTLS_X509_SAFE_SNPRINTF;
  53734. /* Key size */
  53735. if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
  53736. mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
  53737. {
  53738. return( ret );
  53739. }
  53740. ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
  53741. (int) mbedtls_pk_get_bitlen( &crt->pk ) );
  53742. MBEDTLS_X509_SAFE_SNPRINTF;
  53743. /*
  53744. * Optional extensions
  53745. */
  53746. if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
  53747. {
  53748. ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
  53749. crt->ca_istrue ? "true" : "false" );
  53750. MBEDTLS_X509_SAFE_SNPRINTF;
  53751. if( crt->max_pathlen > 0 )
  53752. {
  53753. ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
  53754. MBEDTLS_X509_SAFE_SNPRINTF;
  53755. }
  53756. }
  53757. if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
  53758. {
  53759. ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
  53760. MBEDTLS_X509_SAFE_SNPRINTF;
  53761. if( ( ret = x509_info_subject_alt_name( &p, &n,
  53762. &crt->subject_alt_names ) ) != 0 )
  53763. return( ret );
  53764. }
  53765. if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
  53766. {
  53767. ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
  53768. MBEDTLS_X509_SAFE_SNPRINTF;
  53769. if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
  53770. return( ret );
  53771. }
  53772. if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
  53773. {
  53774. ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
  53775. MBEDTLS_X509_SAFE_SNPRINTF;
  53776. if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
  53777. return( ret );
  53778. }
  53779. if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
  53780. {
  53781. ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
  53782. MBEDTLS_X509_SAFE_SNPRINTF;
  53783. if( ( ret = x509_info_ext_key_usage( &p, &n,
  53784. &crt->ext_key_usage ) ) != 0 )
  53785. return( ret );
  53786. }
  53787. ret = mbedtls_snprintf( p, n, "\n" );
  53788. MBEDTLS_X509_SAFE_SNPRINTF;
  53789. return( (int) ( size - n ) );
  53790. }
  53791. struct x509_crt_verify_string {
  53792. int code;
  53793. const char *string;
  53794. };
  53795. static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
  53796. { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
  53797. { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
  53798. { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
  53799. { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
  53800. { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
  53801. { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
  53802. { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
  53803. { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
  53804. { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
  53805. { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
  53806. { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
  53807. { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
  53808. { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
  53809. { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
  53810. { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
  53811. { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
  53812. { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
  53813. { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
  53814. { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
  53815. { MBEDTLS_X509_BADCRL_BAD_KEY, "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." },
  53816. { 0, NULL }
  53817. };
  53818. int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
  53819. uint32_t flags )
  53820. {
  53821. int ret;
  53822. const struct x509_crt_verify_string *cur;
  53823. char *p = buf;
  53824. size_t n = size;
  53825. for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
  53826. {
  53827. if( ( flags & cur->code ) == 0 )
  53828. continue;
  53829. ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
  53830. MBEDTLS_X509_SAFE_SNPRINTF;
  53831. flags ^= cur->code;
  53832. }
  53833. if( flags != 0 )
  53834. {
  53835. ret = mbedtls_snprintf( p, n, "%sUnknown reason "
  53836. "(this should not happen)\n", prefix );
  53837. MBEDTLS_X509_SAFE_SNPRINTF;
  53838. }
  53839. return( (int) ( size - n ) );
  53840. }
  53841. #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
  53842. int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
  53843. unsigned int usage )
  53844. {
  53845. unsigned int usage_must, usage_may;
  53846. unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
  53847. | MBEDTLS_X509_KU_DECIPHER_ONLY;
  53848. if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
  53849. return( 0 );
  53850. usage_must = usage & ~may_mask;
  53851. if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
  53852. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  53853. usage_may = usage & may_mask;
  53854. if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
  53855. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  53856. return( 0 );
  53857. }
  53858. #endif
  53859. #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
  53860. int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
  53861. const char *usage_oid,
  53862. size_t usage_len )
  53863. {
  53864. const mbedtls_x509_sequence *cur;
  53865. /* Extension is not mandatory, absent means no restriction */
  53866. if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
  53867. return( 0 );
  53868. /*
  53869. * Look for the requested usage (or wildcard ANY) in our list
  53870. */
  53871. for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
  53872. {
  53873. const mbedtls_x509_buf *cur_oid = &cur->buf;
  53874. if( cur_oid->len == usage_len &&
  53875. memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
  53876. {
  53877. return( 0 );
  53878. }
  53879. if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
  53880. return( 0 );
  53881. }
  53882. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  53883. }
  53884. #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
  53885. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  53886. /*
  53887. * Return 1 if the certificate is revoked, or 0 otherwise.
  53888. */
  53889. int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
  53890. {
  53891. const mbedtls_x509_crl_entry *cur = &crl->entry;
  53892. while( cur != NULL && cur->serial.len != 0 )
  53893. {
  53894. if( crt->serial.len == cur->serial.len &&
  53895. memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
  53896. {
  53897. if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
  53898. return( 1 );
  53899. }
  53900. cur = cur->next;
  53901. }
  53902. return( 0 );
  53903. }
  53904. /*
  53905. * Check that the given certificate is not revoked according to the CRL.
  53906. * Skip validation is no CRL for the given CA is present.
  53907. */
  53908. static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
  53909. mbedtls_x509_crl *crl_list,
  53910. const mbedtls_x509_crt_profile *profile )
  53911. {
  53912. int flags = 0;
  53913. unsigned char hash[MBEDTLS_MD_MAX_SIZE];
  53914. const mbedtls_md_info_t *md_info;
  53915. if( ca == NULL )
  53916. return( flags );
  53917. while( crl_list != NULL )
  53918. {
  53919. if( crl_list->version == 0 ||
  53920. crl_list->issuer_raw.len != ca->subject_raw.len ||
  53921. memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
  53922. crl_list->issuer_raw.len ) != 0 )
  53923. {
  53924. crl_list = crl_list->next;
  53925. continue;
  53926. }
  53927. /*
  53928. * Check if the CA is configured to sign CRLs
  53929. */
  53930. #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
  53931. if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
  53932. {
  53933. flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
  53934. break;
  53935. }
  53936. #endif
  53937. /*
  53938. * Check if CRL is correctly signed by the trusted CA
  53939. */
  53940. if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
  53941. flags |= MBEDTLS_X509_BADCRL_BAD_MD;
  53942. if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
  53943. flags |= MBEDTLS_X509_BADCRL_BAD_PK;
  53944. md_info = mbedtls_md_info_from_type( crl_list->sig_md );
  53945. if( md_info == NULL )
  53946. {
  53947. /*
  53948. * Cannot check 'unknown' hash
  53949. */
  53950. flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
  53951. break;
  53952. }
  53953. mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
  53954. if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 )
  53955. flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
  53956. if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
  53957. crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
  53958. crl_list->sig.p, crl_list->sig.len ) != 0 )
  53959. {
  53960. flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
  53961. break;
  53962. }
  53963. /*
  53964. * Check for validity of CRL (Do not drop out)
  53965. */
  53966. if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
  53967. flags |= MBEDTLS_X509_BADCRL_EXPIRED;
  53968. if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
  53969. flags |= MBEDTLS_X509_BADCRL_FUTURE;
  53970. /*
  53971. * Check if certificate is revoked
  53972. */
  53973. if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
  53974. {
  53975. flags |= MBEDTLS_X509_BADCERT_REVOKED;
  53976. break;
  53977. }
  53978. crl_list = crl_list->next;
  53979. }
  53980. return( flags );
  53981. }
  53982. #endif /* MBEDTLS_X509_CRL_PARSE_C */
  53983. /*
  53984. * Like memcmp, but case-insensitive and always returns -1 if different
  53985. */
  53986. static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
  53987. {
  53988. size_t i;
  53989. unsigned char diff;
  53990. const unsigned char *n1 = s1, *n2 = s2;
  53991. for( i = 0; i < len; i++ )
  53992. {
  53993. diff = n1[i] ^ n2[i];
  53994. if( diff == 0 )
  53995. continue;
  53996. if( diff == 32 &&
  53997. ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
  53998. ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
  53999. {
  54000. continue;
  54001. }
  54002. return( -1 );
  54003. }
  54004. return( 0 );
  54005. }
  54006. /*
  54007. * Return 0 if name matches wildcard, -1 otherwise
  54008. */
  54009. static int x509_check_wildcard( const char *cn, mbedtls_x509_buf *name )
  54010. {
  54011. size_t i;
  54012. size_t cn_idx = 0, cn_len = strlen( cn );
  54013. if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
  54014. return( 0 );
  54015. for( i = 0; i < cn_len; ++i )
  54016. {
  54017. if( cn[i] == '.' )
  54018. {
  54019. cn_idx = i;
  54020. break;
  54021. }
  54022. }
  54023. if( cn_idx == 0 )
  54024. return( -1 );
  54025. if( cn_len - cn_idx == name->len - 1 &&
  54026. x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
  54027. {
  54028. return( 0 );
  54029. }
  54030. return( -1 );
  54031. }
  54032. /*
  54033. * Compare two X.509 strings, case-insensitive, and allowing for some encoding
  54034. * variations (but not all).
  54035. *
  54036. * Return 0 if equal, -1 otherwise.
  54037. */
  54038. static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
  54039. {
  54040. if( a->tag == b->tag &&
  54041. a->len == b->len &&
  54042. memcmp( a->p, b->p, b->len ) == 0 )
  54043. {
  54044. return( 0 );
  54045. }
  54046. if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
  54047. ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
  54048. a->len == b->len &&
  54049. x509_memcasecmp( a->p, b->p, b->len ) == 0 )
  54050. {
  54051. return( 0 );
  54052. }
  54053. return( -1 );
  54054. }
  54055. /*
  54056. * Compare two X.509 Names (aka rdnSequence).
  54057. *
  54058. * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
  54059. * we sometimes return unequal when the full algorithm would return equal,
  54060. * but never the other way. (In particular, we don't do Unicode normalisation
  54061. * or space folding.)
  54062. *
  54063. * Return 0 if equal, -1 otherwise.
  54064. */
  54065. static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
  54066. {
  54067. /* Avoid recursion, it might not be optimised by the compiler */
  54068. while( a != NULL || b != NULL )
  54069. {
  54070. if( a == NULL || b == NULL )
  54071. return( -1 );
  54072. /* type */
  54073. if( a->oid.tag != b->oid.tag ||
  54074. a->oid.len != b->oid.len ||
  54075. memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
  54076. {
  54077. return( -1 );
  54078. }
  54079. /* value */
  54080. if( x509_string_cmp( &a->val, &b->val ) != 0 )
  54081. return( -1 );
  54082. /* structure of the list of sets */
  54083. if( a->next_merged != b->next_merged )
  54084. return( -1 );
  54085. a = a->next;
  54086. b = b->next;
  54087. }
  54088. /* a == NULL == b */
  54089. return( 0 );
  54090. }
  54091. /*
  54092. * Check if 'parent' is a suitable parent (signing CA) for 'child'.
  54093. * Return 0 if yes, -1 if not.
  54094. *
  54095. * top means parent is a locally-trusted certificate
  54096. * bottom means child is the end entity cert
  54097. */
  54098. static int x509_crt_check_parent( const mbedtls_x509_crt *child,
  54099. const mbedtls_x509_crt *parent,
  54100. int top, int bottom )
  54101. {
  54102. int need_ca_bit;
  54103. /* Parent must be the issuer */
  54104. if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
  54105. return( -1 );
  54106. /* Parent must have the basicConstraints CA bit set as a general rule */
  54107. need_ca_bit = 1;
  54108. /* Exception: v1/v2 certificates that are locally trusted. */
  54109. if( top && parent->version < 3 )
  54110. need_ca_bit = 0;
  54111. /* Exception: self-signed end-entity certs that are locally trusted. */
  54112. if( top && bottom &&
  54113. child->raw.len == parent->raw.len &&
  54114. memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
  54115. {
  54116. need_ca_bit = 0;
  54117. }
  54118. if( need_ca_bit && ! parent->ca_istrue )
  54119. return( -1 );
  54120. #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
  54121. if( need_ca_bit &&
  54122. mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
  54123. {
  54124. return( -1 );
  54125. }
  54126. #endif
  54127. return( 0 );
  54128. }
  54129. static int x509_crt_verify_top(
  54130. mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
  54131. mbedtls_x509_crl *ca_crl,
  54132. const mbedtls_x509_crt_profile *profile,
  54133. int path_cnt, int self_cnt, uint32_t *flags,
  54134. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  54135. void *p_vrfy )
  54136. {
  54137. int ret;
  54138. uint32_t ca_flags = 0;
  54139. int check_path_cnt;
  54140. unsigned char hash[MBEDTLS_MD_MAX_SIZE];
  54141. const mbedtls_md_info_t *md_info;
  54142. mbedtls_x509_crt *future_past_ca = NULL;
  54143. if( mbedtls_x509_time_is_past( &child->valid_to ) )
  54144. *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
  54145. if( mbedtls_x509_time_is_future( &child->valid_from ) )
  54146. *flags |= MBEDTLS_X509_BADCERT_FUTURE;
  54147. if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
  54148. *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
  54149. if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
  54150. *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
  54151. /*
  54152. * Child is the top of the chain. Check against the trust_ca list.
  54153. */
  54154. *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
  54155. md_info = mbedtls_md_info_from_type( child->sig_md );
  54156. if( md_info == NULL )
  54157. {
  54158. /*
  54159. * Cannot check 'unknown', no need to try any CA
  54160. */
  54161. trust_ca = NULL;
  54162. }
  54163. else
  54164. mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
  54165. for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
  54166. {
  54167. if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
  54168. continue;
  54169. check_path_cnt = path_cnt + 1;
  54170. /*
  54171. * Reduce check_path_cnt to check against if top of the chain is
  54172. * the same as the trusted CA
  54173. */
  54174. if( child->subject_raw.len == trust_ca->subject_raw.len &&
  54175. memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
  54176. child->issuer_raw.len ) == 0 )
  54177. {
  54178. check_path_cnt--;
  54179. }
  54180. /* Self signed certificates do not count towards the limit */
  54181. if( trust_ca->max_pathlen > 0 &&
  54182. trust_ca->max_pathlen < check_path_cnt - self_cnt )
  54183. {
  54184. continue;
  54185. }
  54186. if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
  54187. child->sig_md, hash, mbedtls_md_get_size( md_info ),
  54188. child->sig.p, child->sig.len ) != 0 )
  54189. {
  54190. continue;
  54191. }
  54192. if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) ||
  54193. mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
  54194. {
  54195. if ( future_past_ca == NULL )
  54196. future_past_ca = trust_ca;
  54197. continue;
  54198. }
  54199. break;
  54200. }
  54201. if( trust_ca != NULL || ( trust_ca = future_past_ca ) != NULL )
  54202. {
  54203. /*
  54204. * Top of chain is signed by a trusted CA
  54205. */
  54206. *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
  54207. if( x509_profile_check_key( profile, child->sig_pk, &trust_ca->pk ) != 0 )
  54208. *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
  54209. }
  54210. /*
  54211. * If top of chain is not the same as the trusted CA send a verify request
  54212. * to the callback for any issues with validity and CRL presence for the
  54213. * trusted CA certificate.
  54214. */
  54215. if( trust_ca != NULL &&
  54216. ( child->subject_raw.len != trust_ca->subject_raw.len ||
  54217. memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
  54218. child->issuer_raw.len ) != 0 ) )
  54219. {
  54220. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  54221. /* Check trusted CA's CRL for the chain's top crt */
  54222. *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl, profile );
  54223. #else
  54224. ((void) ca_crl);
  54225. #endif
  54226. if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) )
  54227. ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED;
  54228. if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
  54229. ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
  54230. if( NULL != f_vrfy )
  54231. {
  54232. if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
  54233. &ca_flags ) ) != 0 )
  54234. {
  54235. return( ret );
  54236. }
  54237. }
  54238. }
  54239. /* Call callback on top cert */
  54240. if( NULL != f_vrfy )
  54241. {
  54242. if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
  54243. return( ret );
  54244. }
  54245. *flags |= ca_flags;
  54246. return( 0 );
  54247. }
  54248. static int x509_crt_verify_child(
  54249. mbedtls_x509_crt *child, mbedtls_x509_crt *parent,
  54250. mbedtls_x509_crt *trust_ca, mbedtls_x509_crl *ca_crl,
  54251. const mbedtls_x509_crt_profile *profile,
  54252. int path_cnt, int self_cnt, uint32_t *flags,
  54253. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  54254. void *p_vrfy )
  54255. {
  54256. int ret;
  54257. uint32_t parent_flags = 0;
  54258. unsigned char hash[MBEDTLS_MD_MAX_SIZE];
  54259. mbedtls_x509_crt *grandparent;
  54260. const mbedtls_md_info_t *md_info;
  54261. /* Counting intermediate self signed certificates */
  54262. if( ( path_cnt != 0 ) && x509_name_cmp( &child->issuer, &child->subject ) == 0 )
  54263. self_cnt++;
  54264. /* path_cnt is 0 for the first intermediate CA */
  54265. if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
  54266. {
  54267. /* return immediately as the goal is to avoid unbounded recursion */
  54268. return( MBEDTLS_ERR_X509_FATAL_ERROR );
  54269. }
  54270. if( mbedtls_x509_time_is_past( &child->valid_to ) )
  54271. *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
  54272. if( mbedtls_x509_time_is_future( &child->valid_from ) )
  54273. *flags |= MBEDTLS_X509_BADCERT_FUTURE;
  54274. if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
  54275. *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
  54276. if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
  54277. *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
  54278. md_info = mbedtls_md_info_from_type( child->sig_md );
  54279. if( md_info == NULL )
  54280. {
  54281. /*
  54282. * Cannot check 'unknown' hash
  54283. */
  54284. *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
  54285. }
  54286. else
  54287. {
  54288. mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
  54289. if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 )
  54290. *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
  54291. if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
  54292. child->sig_md, hash, mbedtls_md_get_size( md_info ),
  54293. child->sig.p, child->sig.len ) != 0 )
  54294. {
  54295. *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
  54296. }
  54297. }
  54298. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  54299. /* Check trusted CA's CRL for the given crt */
  54300. *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile );
  54301. #endif
  54302. /* Look for a grandparent in trusted CAs */
  54303. for( grandparent = trust_ca;
  54304. grandparent != NULL;
  54305. grandparent = grandparent->next )
  54306. {
  54307. if( x509_crt_check_parent( parent, grandparent,
  54308. 0, path_cnt == 0 ) == 0 )
  54309. break;
  54310. }
  54311. if( grandparent != NULL )
  54312. {
  54313. ret = x509_crt_verify_top( parent, grandparent, ca_crl, profile,
  54314. path_cnt + 1, self_cnt, &parent_flags, f_vrfy, p_vrfy );
  54315. if( ret != 0 )
  54316. return( ret );
  54317. }
  54318. else
  54319. {
  54320. /* Look for a grandparent upwards the chain */
  54321. for( grandparent = parent->next;
  54322. grandparent != NULL;
  54323. grandparent = grandparent->next )
  54324. {
  54325. /* +2 because the current step is not yet accounted for
  54326. * and because max_pathlen is one higher than it should be.
  54327. * Also self signed certificates do not count to the limit. */
  54328. if( grandparent->max_pathlen > 0 &&
  54329. grandparent->max_pathlen < 2 + path_cnt - self_cnt )
  54330. {
  54331. continue;
  54332. }
  54333. if( x509_crt_check_parent( parent, grandparent,
  54334. 0, path_cnt == 0 ) == 0 )
  54335. break;
  54336. }
  54337. /* Is our parent part of the chain or at the top? */
  54338. if( grandparent != NULL )
  54339. {
  54340. ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
  54341. profile, path_cnt + 1, self_cnt, &parent_flags,
  54342. f_vrfy, p_vrfy );
  54343. if( ret != 0 )
  54344. return( ret );
  54345. }
  54346. else
  54347. {
  54348. ret = x509_crt_verify_top( parent, trust_ca, ca_crl, profile,
  54349. path_cnt + 1, self_cnt, &parent_flags,
  54350. f_vrfy, p_vrfy );
  54351. if( ret != 0 )
  54352. return( ret );
  54353. }
  54354. }
  54355. /* child is verified to be a child of the parent, call verify callback */
  54356. if( NULL != f_vrfy )
  54357. if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
  54358. return( ret );
  54359. *flags |= parent_flags;
  54360. return( 0 );
  54361. }
  54362. /*
  54363. * Verify the certificate validity
  54364. */
  54365. int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
  54366. mbedtls_x509_crt *trust_ca,
  54367. mbedtls_x509_crl *ca_crl,
  54368. const char *cn, uint32_t *flags,
  54369. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  54370. void *p_vrfy )
  54371. {
  54372. return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
  54373. &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
  54374. }
  54375. /*
  54376. * Verify the certificate validity, with profile
  54377. */
  54378. int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
  54379. mbedtls_x509_crt *trust_ca,
  54380. mbedtls_x509_crl *ca_crl,
  54381. const mbedtls_x509_crt_profile *profile,
  54382. const char *cn, uint32_t *flags,
  54383. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  54384. void *p_vrfy )
  54385. {
  54386. size_t cn_len;
  54387. int ret;
  54388. int pathlen = 0, selfsigned = 0;
  54389. mbedtls_x509_crt *parent;
  54390. mbedtls_x509_name *name;
  54391. mbedtls_x509_sequence *cur = NULL;
  54392. mbedtls_pk_type_t pk_type;
  54393. *flags = 0;
  54394. if( profile == NULL )
  54395. {
  54396. ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  54397. goto exit;
  54398. }
  54399. if( cn != NULL )
  54400. {
  54401. name = &crt->subject;
  54402. cn_len = strlen( cn );
  54403. if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
  54404. {
  54405. cur = &crt->subject_alt_names;
  54406. while( cur != NULL )
  54407. {
  54408. if( cur->buf.len == cn_len &&
  54409. x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
  54410. break;
  54411. if( cur->buf.len > 2 &&
  54412. memcmp( cur->buf.p, "*.", 2 ) == 0 &&
  54413. x509_check_wildcard( cn, &cur->buf ) == 0 )
  54414. {
  54415. break;
  54416. }
  54417. cur = cur->next;
  54418. }
  54419. if( cur == NULL )
  54420. *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
  54421. }
  54422. else
  54423. {
  54424. while( name != NULL )
  54425. {
  54426. if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
  54427. {
  54428. if( name->val.len == cn_len &&
  54429. x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
  54430. break;
  54431. if( name->val.len > 2 &&
  54432. memcmp( name->val.p, "*.", 2 ) == 0 &&
  54433. x509_check_wildcard( cn, &name->val ) == 0 )
  54434. break;
  54435. }
  54436. name = name->next;
  54437. }
  54438. if( name == NULL )
  54439. *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
  54440. }
  54441. }
  54442. /* Check the type and size of the key */
  54443. pk_type = mbedtls_pk_get_type( &crt->pk );
  54444. if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
  54445. *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
  54446. if( x509_profile_check_key( profile, pk_type, &crt->pk ) != 0 )
  54447. *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
  54448. /* Look for a parent in trusted CAs */
  54449. for( parent = trust_ca; parent != NULL; parent = parent->next )
  54450. {
  54451. if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
  54452. break;
  54453. }
  54454. if( parent != NULL )
  54455. {
  54456. ret = x509_crt_verify_top( crt, parent, ca_crl, profile,
  54457. pathlen, selfsigned, flags, f_vrfy, p_vrfy );
  54458. if( ret != 0 )
  54459. goto exit;
  54460. }
  54461. else
  54462. {
  54463. /* Look for a parent upwards the chain */
  54464. for( parent = crt->next; parent != NULL; parent = parent->next )
  54465. if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
  54466. break;
  54467. /* Are we part of the chain or at the top? */
  54468. if( parent != NULL )
  54469. {
  54470. ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, profile,
  54471. pathlen, selfsigned, flags, f_vrfy, p_vrfy );
  54472. if( ret != 0 )
  54473. goto exit;
  54474. }
  54475. else
  54476. {
  54477. ret = x509_crt_verify_top( crt, trust_ca, ca_crl, profile,
  54478. pathlen, selfsigned, flags, f_vrfy, p_vrfy );
  54479. if( ret != 0 )
  54480. goto exit;
  54481. }
  54482. }
  54483. exit:
  54484. /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
  54485. * the SSL module for authmode optional, but non-zero return from the
  54486. * callback means a fatal error so it shouldn't be ignored */
  54487. if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
  54488. ret = MBEDTLS_ERR_X509_FATAL_ERROR;
  54489. if( ret != 0 )
  54490. {
  54491. *flags = (uint32_t) -1;
  54492. return( ret );
  54493. }
  54494. if( *flags != 0 )
  54495. return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
  54496. return( 0 );
  54497. }
  54498. /*
  54499. * Initialize a certificate chain
  54500. */
  54501. void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
  54502. {
  54503. memset( crt, 0, sizeof(mbedtls_x509_crt) );
  54504. }
  54505. /*
  54506. * Unallocate all certificate data
  54507. */
  54508. void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
  54509. {
  54510. mbedtls_x509_crt *cert_cur = crt;
  54511. mbedtls_x509_crt *cert_prv;
  54512. mbedtls_x509_name *name_cur;
  54513. mbedtls_x509_name *name_prv;
  54514. mbedtls_x509_sequence *seq_cur;
  54515. mbedtls_x509_sequence *seq_prv;
  54516. if( crt == NULL )
  54517. return;
  54518. do
  54519. {
  54520. mbedtls_pk_free( &cert_cur->pk );
  54521. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  54522. mbedtls_free( cert_cur->sig_opts );
  54523. #endif
  54524. name_cur = cert_cur->issuer.next;
  54525. while( name_cur != NULL )
  54526. {
  54527. name_prv = name_cur;
  54528. name_cur = name_cur->next;
  54529. mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
  54530. mbedtls_free( name_prv );
  54531. }
  54532. name_cur = cert_cur->subject.next;
  54533. while( name_cur != NULL )
  54534. {
  54535. name_prv = name_cur;
  54536. name_cur = name_cur->next;
  54537. mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
  54538. mbedtls_free( name_prv );
  54539. }
  54540. seq_cur = cert_cur->ext_key_usage.next;
  54541. while( seq_cur != NULL )
  54542. {
  54543. seq_prv = seq_cur;
  54544. seq_cur = seq_cur->next;
  54545. mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
  54546. mbedtls_free( seq_prv );
  54547. }
  54548. seq_cur = cert_cur->subject_alt_names.next;
  54549. while( seq_cur != NULL )
  54550. {
  54551. seq_prv = seq_cur;
  54552. seq_cur = seq_cur->next;
  54553. mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
  54554. mbedtls_free( seq_prv );
  54555. }
  54556. if( cert_cur->raw.p != NULL )
  54557. {
  54558. mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
  54559. mbedtls_free( cert_cur->raw.p );
  54560. }
  54561. cert_cur = cert_cur->next;
  54562. }
  54563. while( cert_cur != NULL );
  54564. cert_cur = crt;
  54565. do
  54566. {
  54567. cert_prv = cert_cur;
  54568. cert_cur = cert_cur->next;
  54569. mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
  54570. if( cert_prv != crt )
  54571. mbedtls_free( cert_prv );
  54572. }
  54573. while( cert_cur != NULL );
  54574. }
  54575. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  54576. /*
  54577. Amalgamated build undefines
  54578. */
  54579. #undef ADD
  54580. #undef BC
  54581. #undef BEFORE_COLON
  54582. #undef F
  54583. #undef F0
  54584. #undef F1
  54585. #undef F2
  54586. #undef F3
  54587. #undef F4
  54588. #undef F5
  54589. #undef FSb
  54590. #undef K
  54591. #undef KK
  54592. #undef P
  54593. #undef R
  54594. #undef ROTR
  54595. #undef S
  54596. #undef S0
  54597. #undef S1
  54598. #undef S2
  54599. #undef S3
  54600. #undef SAFE_SNPRINTF
  54601. #undef SHR
  54602. #undef close
  54603. #undef read
  54604. #undef supported_init
  54605. #undef write
  54606. /********* Start of file library/x509_csr.c ************/
  54607. /*
  54608. * X.509 Certificate Signing Request (CSR) parsing
  54609. *
  54610. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  54611. * SPDX-License-Identifier: Apache-2.0
  54612. *
  54613. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  54614. * not use this file except in compliance with the License.
  54615. * You may obtain a copy of the License at
  54616. *
  54617. * http://www.apache.org/licenses/LICENSE-2.0
  54618. *
  54619. * Unless required by applicable law or agreed to in writing, software
  54620. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  54621. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  54622. * See the License for the specific language governing permissions and
  54623. * limitations under the License.
  54624. *
  54625. * This file is part of mbed TLS (https://tls.mbed.org)
  54626. */
  54627. /*
  54628. * The ITU-T X.509 standard defines a certificate format for PKI.
  54629. *
  54630. * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
  54631. * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
  54632. * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
  54633. *
  54634. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
  54635. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
  54636. */
  54637. #if !defined(MBEDTLS_CONFIG_FILE)
  54638. #else
  54639. #endif
  54640. #if defined(MBEDTLS_X509_CSR_PARSE_C)
  54641. #include <string.h>
  54642. #if defined(MBEDTLS_PEM_PARSE_C)
  54643. #endif
  54644. #if defined(MBEDTLS_PLATFORM_C)
  54645. #else
  54646. #include <stdlib.h>
  54647. #include <stdio.h>
  54648. #define mbedtls_free free
  54649. #define mbedtls_calloc calloc
  54650. #define mbedtls_snprintf snprintf
  54651. #endif
  54652. #if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
  54653. #include <stdio.h>
  54654. #endif
  54655. /* Implementation that should never be optimized out by the compiler */
  54656. /* zeroize was here */
  54657. /*
  54658. * Version ::= INTEGER { v1(0) }
  54659. */
  54660. static int x509_csr_get_version( unsigned char **p,
  54661. const unsigned char *end,
  54662. int *ver )
  54663. {
  54664. int ret;
  54665. if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
  54666. {
  54667. if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  54668. {
  54669. *ver = 0;
  54670. return( 0 );
  54671. }
  54672. return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
  54673. }
  54674. return( 0 );
  54675. }
  54676. /*
  54677. * Parse a CSR in DER format
  54678. */
  54679. int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
  54680. const unsigned char *buf, size_t buflen )
  54681. {
  54682. int ret;
  54683. size_t len;
  54684. unsigned char *p, *end;
  54685. mbedtls_x509_buf sig_params;
  54686. memset( &sig_params, 0, sizeof( mbedtls_x509_buf ) );
  54687. /*
  54688. * Check for valid input
  54689. */
  54690. if( csr == NULL || buf == NULL || buflen == 0 )
  54691. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  54692. mbedtls_x509_csr_init( csr );
  54693. /*
  54694. * first copy the raw DER data
  54695. */
  54696. p = mbedtls_calloc( 1, len = buflen );
  54697. if( p == NULL )
  54698. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  54699. memcpy( p, buf, buflen );
  54700. csr->raw.p = p;
  54701. csr->raw.len = len;
  54702. end = p + len;
  54703. /*
  54704. * CertificationRequest ::= SEQUENCE {
  54705. * certificationRequestInfo CertificationRequestInfo,
  54706. * signatureAlgorithm AlgorithmIdentifier,
  54707. * signature BIT STRING
  54708. * }
  54709. */
  54710. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  54711. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  54712. {
  54713. mbedtls_x509_csr_free( csr );
  54714. return( MBEDTLS_ERR_X509_INVALID_FORMAT );
  54715. }
  54716. if( len != (size_t) ( end - p ) )
  54717. {
  54718. mbedtls_x509_csr_free( csr );
  54719. return( MBEDTLS_ERR_X509_INVALID_FORMAT +
  54720. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  54721. }
  54722. /*
  54723. * CertificationRequestInfo ::= SEQUENCE {
  54724. */
  54725. csr->cri.p = p;
  54726. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  54727. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  54728. {
  54729. mbedtls_x509_csr_free( csr );
  54730. return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
  54731. }
  54732. end = p + len;
  54733. csr->cri.len = end - csr->cri.p;
  54734. /*
  54735. * Version ::= INTEGER { v1(0) }
  54736. */
  54737. if( ( ret = x509_csr_get_version( &p, end, &csr->version ) ) != 0 )
  54738. {
  54739. mbedtls_x509_csr_free( csr );
  54740. return( ret );
  54741. }
  54742. if( csr->version != 0 )
  54743. {
  54744. mbedtls_x509_csr_free( csr );
  54745. return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
  54746. }
  54747. csr->version++;
  54748. /*
  54749. * subject Name
  54750. */
  54751. csr->subject_raw.p = p;
  54752. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  54753. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  54754. {
  54755. mbedtls_x509_csr_free( csr );
  54756. return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
  54757. }
  54758. if( ( ret = mbedtls_x509_get_name( &p, p + len, &csr->subject ) ) != 0 )
  54759. {
  54760. mbedtls_x509_csr_free( csr );
  54761. return( ret );
  54762. }
  54763. csr->subject_raw.len = p - csr->subject_raw.p;
  54764. /*
  54765. * subjectPKInfo SubjectPublicKeyInfo
  54766. */
  54767. if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &csr->pk ) ) != 0 )
  54768. {
  54769. mbedtls_x509_csr_free( csr );
  54770. return( ret );
  54771. }
  54772. /*
  54773. * attributes [0] Attributes
  54774. *
  54775. * The list of possible attributes is open-ended, though RFC 2985
  54776. * (PKCS#9) defines a few in section 5.4. We currently don't support any,
  54777. * so we just ignore them. This is a safe thing to do as the worst thing
  54778. * that could happen is that we issue a certificate that does not match
  54779. * the requester's expectations - this cannot cause a violation of our
  54780. * signature policies.
  54781. */
  54782. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  54783. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
  54784. {
  54785. mbedtls_x509_csr_free( csr );
  54786. return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
  54787. }
  54788. p += len;
  54789. end = csr->raw.p + csr->raw.len;
  54790. /*
  54791. * signatureAlgorithm AlgorithmIdentifier,
  54792. * signature BIT STRING
  54793. */
  54794. if( ( ret = mbedtls_x509_get_alg( &p, end, &csr->sig_oid, &sig_params ) ) != 0 )
  54795. {
  54796. mbedtls_x509_csr_free( csr );
  54797. return( ret );
  54798. }
  54799. if( ( ret = mbedtls_x509_get_sig_alg( &csr->sig_oid, &sig_params,
  54800. &csr->sig_md, &csr->sig_pk,
  54801. &csr->sig_opts ) ) != 0 )
  54802. {
  54803. mbedtls_x509_csr_free( csr );
  54804. return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG );
  54805. }
  54806. if( ( ret = mbedtls_x509_get_sig( &p, end, &csr->sig ) ) != 0 )
  54807. {
  54808. mbedtls_x509_csr_free( csr );
  54809. return( ret );
  54810. }
  54811. if( p != end )
  54812. {
  54813. mbedtls_x509_csr_free( csr );
  54814. return( MBEDTLS_ERR_X509_INVALID_FORMAT +
  54815. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  54816. }
  54817. return( 0 );
  54818. }
  54819. /*
  54820. * Parse a CSR, allowing for PEM or raw DER encoding
  54821. */
  54822. int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen )
  54823. {
  54824. #if defined(MBEDTLS_PEM_PARSE_C)
  54825. int ret;
  54826. size_t use_len;
  54827. mbedtls_pem_context pem;
  54828. #endif
  54829. /*
  54830. * Check for valid input
  54831. */
  54832. if( csr == NULL || buf == NULL || buflen == 0 )
  54833. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  54834. #if defined(MBEDTLS_PEM_PARSE_C)
  54835. mbedtls_pem_init( &pem );
  54836. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  54837. if( buf[buflen - 1] != '\0' )
  54838. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  54839. else
  54840. ret = mbedtls_pem_read_buffer( &pem,
  54841. "-----BEGIN CERTIFICATE REQUEST-----",
  54842. "-----END CERTIFICATE REQUEST-----",
  54843. buf, NULL, 0, &use_len );
  54844. if( ret == 0 )
  54845. {
  54846. /*
  54847. * Was PEM encoded, parse the result
  54848. */
  54849. if( ( ret = mbedtls_x509_csr_parse_der( csr, pem.buf, pem.buflen ) ) != 0 )
  54850. return( ret );
  54851. mbedtls_pem_free( &pem );
  54852. return( 0 );
  54853. }
  54854. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  54855. {
  54856. mbedtls_pem_free( &pem );
  54857. return( ret );
  54858. }
  54859. else
  54860. #endif /* MBEDTLS_PEM_PARSE_C */
  54861. return( mbedtls_x509_csr_parse_der( csr, buf, buflen ) );
  54862. }
  54863. #if defined(MBEDTLS_FS_IO)
  54864. /*
  54865. * Load a CSR into the structure
  54866. */
  54867. int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path )
  54868. {
  54869. int ret;
  54870. size_t n;
  54871. unsigned char *buf;
  54872. if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
  54873. return( ret );
  54874. ret = mbedtls_x509_csr_parse( csr, buf, n );
  54875. mbedtls_zeroize( buf, n );
  54876. mbedtls_free( buf );
  54877. return( ret );
  54878. }
  54879. #endif /* MBEDTLS_FS_IO */
  54880. #define BEFORE_COLON 14
  54881. #define BC "14"
  54882. /*
  54883. * Return an informational string about the CSR.
  54884. */
  54885. int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix,
  54886. const mbedtls_x509_csr *csr )
  54887. {
  54888. int ret;
  54889. size_t n;
  54890. char *p;
  54891. char key_size_str[BEFORE_COLON];
  54892. p = buf;
  54893. n = size;
  54894. ret = mbedtls_snprintf( p, n, "%sCSR version : %d",
  54895. prefix, csr->version );
  54896. MBEDTLS_X509_SAFE_SNPRINTF;
  54897. ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
  54898. MBEDTLS_X509_SAFE_SNPRINTF;
  54899. ret = mbedtls_x509_dn_gets( p, n, &csr->subject );
  54900. MBEDTLS_X509_SAFE_SNPRINTF;
  54901. ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
  54902. MBEDTLS_X509_SAFE_SNPRINTF;
  54903. ret = mbedtls_x509_sig_alg_gets( p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md,
  54904. csr->sig_opts );
  54905. MBEDTLS_X509_SAFE_SNPRINTF;
  54906. if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
  54907. mbedtls_pk_get_name( &csr->pk ) ) ) != 0 )
  54908. {
  54909. return( ret );
  54910. }
  54911. ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
  54912. (int) mbedtls_pk_get_bitlen( &csr->pk ) );
  54913. MBEDTLS_X509_SAFE_SNPRINTF;
  54914. return( (int) ( size - n ) );
  54915. }
  54916. /*
  54917. * Initialize a CSR
  54918. */
  54919. void mbedtls_x509_csr_init( mbedtls_x509_csr *csr )
  54920. {
  54921. memset( csr, 0, sizeof(mbedtls_x509_csr) );
  54922. }
  54923. /*
  54924. * Unallocate all CSR data
  54925. */
  54926. void mbedtls_x509_csr_free( mbedtls_x509_csr *csr )
  54927. {
  54928. mbedtls_x509_name *name_cur;
  54929. mbedtls_x509_name *name_prv;
  54930. if( csr == NULL )
  54931. return;
  54932. mbedtls_pk_free( &csr->pk );
  54933. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  54934. mbedtls_free( csr->sig_opts );
  54935. #endif
  54936. name_cur = csr->subject.next;
  54937. while( name_cur != NULL )
  54938. {
  54939. name_prv = name_cur;
  54940. name_cur = name_cur->next;
  54941. mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
  54942. mbedtls_free( name_prv );
  54943. }
  54944. if( csr->raw.p != NULL )
  54945. {
  54946. mbedtls_zeroize( csr->raw.p, csr->raw.len );
  54947. mbedtls_free( csr->raw.p );
  54948. }
  54949. mbedtls_zeroize( csr, sizeof( mbedtls_x509_csr ) );
  54950. }
  54951. #endif /* MBEDTLS_X509_CSR_PARSE_C */
  54952. /*
  54953. Amalgamated build undefines
  54954. */
  54955. #undef ADD
  54956. #undef BC
  54957. #undef BEFORE_COLON
  54958. #undef F
  54959. #undef F0
  54960. #undef F1
  54961. #undef F2
  54962. #undef F3
  54963. #undef F4
  54964. #undef F5
  54965. #undef FSb
  54966. #undef K
  54967. #undef KK
  54968. #undef P
  54969. #undef R
  54970. #undef ROTR
  54971. #undef S
  54972. #undef S0
  54973. #undef S1
  54974. #undef S2
  54975. #undef S3
  54976. #undef SAFE_SNPRINTF
  54977. #undef SHR
  54978. #undef close
  54979. #undef read
  54980. #undef supported_init
  54981. #undef write
  54982. /********* Start of file library/x509write_crt.c ************/
  54983. /*
  54984. * X.509 certificate writing
  54985. *
  54986. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  54987. * SPDX-License-Identifier: Apache-2.0
  54988. *
  54989. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  54990. * not use this file except in compliance with the License.
  54991. * You may obtain a copy of the License at
  54992. *
  54993. * http://www.apache.org/licenses/LICENSE-2.0
  54994. *
  54995. * Unless required by applicable law or agreed to in writing, software
  54996. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  54997. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  54998. * See the License for the specific language governing permissions and
  54999. * limitations under the License.
  55000. *
  55001. * This file is part of mbed TLS (https://tls.mbed.org)
  55002. */
  55003. /*
  55004. * References:
  55005. * - certificates: RFC 5280, updated by RFC 6818
  55006. * - CSRs: PKCS#10 v1.7 aka RFC 2986
  55007. * - attributes: PKCS#9 v2.0 aka RFC 2985
  55008. */
  55009. #if !defined(MBEDTLS_CONFIG_FILE)
  55010. #else
  55011. #endif
  55012. #if defined(MBEDTLS_X509_CRT_WRITE_C)
  55013. #include <string.h>
  55014. #if defined(MBEDTLS_PEM_WRITE_C)
  55015. #endif /* MBEDTLS_PEM_WRITE_C */
  55016. /* Implementation that should never be optimized out by the compiler */
  55017. /* zeroize was here */
  55018. void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx )
  55019. {
  55020. memset( ctx, 0, sizeof( mbedtls_x509write_cert ) );
  55021. mbedtls_mpi_init( &ctx->serial );
  55022. ctx->version = MBEDTLS_X509_CRT_VERSION_3;
  55023. }
  55024. void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx )
  55025. {
  55026. mbedtls_mpi_free( &ctx->serial );
  55027. mbedtls_asn1_free_named_data_list( &ctx->subject );
  55028. mbedtls_asn1_free_named_data_list( &ctx->issuer );
  55029. mbedtls_asn1_free_named_data_list( &ctx->extensions );
  55030. mbedtls_zeroize( ctx, sizeof( mbedtls_x509write_cert ) );
  55031. }
  55032. void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version )
  55033. {
  55034. ctx->version = version;
  55035. }
  55036. void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg )
  55037. {
  55038. ctx->md_alg = md_alg;
  55039. }
  55040. void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key )
  55041. {
  55042. ctx->subject_key = key;
  55043. }
  55044. void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key )
  55045. {
  55046. ctx->issuer_key = key;
  55047. }
  55048. int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
  55049. const char *subject_name )
  55050. {
  55051. return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
  55052. }
  55053. int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
  55054. const char *issuer_name )
  55055. {
  55056. return mbedtls_x509_string_to_names( &ctx->issuer, issuer_name );
  55057. }
  55058. int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial )
  55059. {
  55060. int ret;
  55061. if( ( ret = mbedtls_mpi_copy( &ctx->serial, serial ) ) != 0 )
  55062. return( ret );
  55063. return( 0 );
  55064. }
  55065. int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
  55066. const char *not_after )
  55067. {
  55068. if( strlen( not_before ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 ||
  55069. strlen( not_after ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 )
  55070. {
  55071. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  55072. }
  55073. strncpy( ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN );
  55074. strncpy( ctx->not_after , not_after , MBEDTLS_X509_RFC5280_UTC_TIME_LEN );
  55075. ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
  55076. ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
  55077. return( 0 );
  55078. }
  55079. int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
  55080. const char *oid, size_t oid_len,
  55081. int critical,
  55082. const unsigned char *val, size_t val_len )
  55083. {
  55084. return mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
  55085. critical, val, val_len );
  55086. }
  55087. int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
  55088. int is_ca, int max_pathlen )
  55089. {
  55090. int ret;
  55091. unsigned char buf[9];
  55092. unsigned char *c = buf + sizeof(buf);
  55093. size_t len = 0;
  55094. memset( buf, 0, sizeof(buf) );
  55095. if( is_ca && max_pathlen > 127 )
  55096. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  55097. if( is_ca )
  55098. {
  55099. if( max_pathlen >= 0 )
  55100. {
  55101. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, max_pathlen ) );
  55102. }
  55103. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( &c, buf, 1 ) );
  55104. }
  55105. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  55106. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
  55107. MBEDTLS_ASN1_SEQUENCE ) );
  55108. return mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
  55109. MBEDTLS_OID_SIZE( MBEDTLS_OID_BASIC_CONSTRAINTS ),
  55110. 0, buf + sizeof(buf) - len, len );
  55111. }
  55112. #if defined(MBEDTLS_SHA1_C)
  55113. int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx )
  55114. {
  55115. int ret;
  55116. unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
  55117. unsigned char *c = buf + sizeof(buf);
  55118. size_t len = 0;
  55119. memset( buf, 0, sizeof(buf) );
  55120. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, ctx->subject_key ) );
  55121. ret = mbedtls_sha1_ret( buf + sizeof( buf ) - len, len,
  55122. buf + sizeof( buf ) - 20 );
  55123. if( ret != 0 )
  55124. return( ret );
  55125. c = buf + sizeof( buf ) - 20;
  55126. len = 20;
  55127. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  55128. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_OCTET_STRING ) );
  55129. return mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
  55130. MBEDTLS_OID_SIZE( MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER ),
  55131. 0, buf + sizeof(buf) - len, len );
  55132. }
  55133. int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx )
  55134. {
  55135. int ret;
  55136. unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
  55137. unsigned char *c = buf + sizeof( buf );
  55138. size_t len = 0;
  55139. memset( buf, 0, sizeof(buf) );
  55140. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, ctx->issuer_key ) );
  55141. ret = mbedtls_sha1_ret( buf + sizeof( buf ) - len, len,
  55142. buf + sizeof( buf ) - 20 );
  55143. if( ret != 0 )
  55144. return( ret );
  55145. c = buf + sizeof( buf ) - 20;
  55146. len = 20;
  55147. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  55148. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0 ) );
  55149. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  55150. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
  55151. MBEDTLS_ASN1_SEQUENCE ) );
  55152. return mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
  55153. MBEDTLS_OID_SIZE( MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER ),
  55154. 0, buf + sizeof( buf ) - len, len );
  55155. }
  55156. #endif /* MBEDTLS_SHA1_C */
  55157. int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
  55158. unsigned int key_usage )
  55159. {
  55160. unsigned char buf[4], ku;
  55161. unsigned char *c;
  55162. int ret;
  55163. /* We currently only support 7 bits, from 0x80 to 0x02 */
  55164. if( ( key_usage & ~0xfe ) != 0 )
  55165. return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
  55166. c = buf + 4;
  55167. ku = (unsigned char) key_usage;
  55168. if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &ku, 7 ) ) != 4 )
  55169. return( ret );
  55170. ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
  55171. MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
  55172. 1, buf, 4 );
  55173. if( ret != 0 )
  55174. return( ret );
  55175. return( 0 );
  55176. }
  55177. int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
  55178. unsigned char ns_cert_type )
  55179. {
  55180. unsigned char buf[4];
  55181. unsigned char *c;
  55182. int ret;
  55183. c = buf + 4;
  55184. if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
  55185. return( ret );
  55186. ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
  55187. MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
  55188. 0, buf, 4 );
  55189. if( ret != 0 )
  55190. return( ret );
  55191. return( 0 );
  55192. }
  55193. static int x509_write_time( unsigned char **p, unsigned char *start,
  55194. const char *t, size_t size )
  55195. {
  55196. int ret;
  55197. size_t len = 0;
  55198. /*
  55199. * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
  55200. */
  55201. if( t[0] == '2' && t[1] == '0' && t[2] < '5' )
  55202. {
  55203. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
  55204. (const unsigned char *) t + 2,
  55205. size - 2 ) );
  55206. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  55207. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_UTC_TIME ) );
  55208. }
  55209. else
  55210. {
  55211. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
  55212. (const unsigned char *) t,
  55213. size ) );
  55214. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  55215. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_GENERALIZED_TIME ) );
  55216. }
  55217. return( (int) len );
  55218. }
  55219. int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
  55220. int (*f_rng)(void *, unsigned char *, size_t),
  55221. void *p_rng )
  55222. {
  55223. int ret;
  55224. const char *sig_oid;
  55225. size_t sig_oid_len = 0;
  55226. unsigned char *c, *c2;
  55227. unsigned char hash[64];
  55228. unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
  55229. unsigned char tmp_buf[2048];
  55230. size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
  55231. size_t len = 0;
  55232. mbedtls_pk_type_t pk_alg;
  55233. /*
  55234. * Prepare data to be signed in tmp_buf
  55235. */
  55236. c = tmp_buf + sizeof( tmp_buf );
  55237. /* Signature algorithm needed in TBS, and later for actual signature */
  55238. /* There's no direct way of extracting a signature algorithm
  55239. * (represented as an element of mbedtls_pk_type_t) from a PK instance. */
  55240. if( mbedtls_pk_can_do( ctx->issuer_key, MBEDTLS_PK_RSA ) )
  55241. pk_alg = MBEDTLS_PK_RSA;
  55242. else if( mbedtls_pk_can_do( ctx->issuer_key, MBEDTLS_PK_ECDSA ) )
  55243. pk_alg = MBEDTLS_PK_ECDSA;
  55244. else
  55245. return( MBEDTLS_ERR_X509_INVALID_ALG );
  55246. if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
  55247. &sig_oid, &sig_oid_len ) ) != 0 )
  55248. {
  55249. return( ret );
  55250. }
  55251. /*
  55252. * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
  55253. */
  55254. /* Only for v3 */
  55255. if( ctx->version == MBEDTLS_X509_CRT_VERSION_3 )
  55256. {
  55257. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
  55258. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  55259. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  55260. MBEDTLS_ASN1_SEQUENCE ) );
  55261. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  55262. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC |
  55263. MBEDTLS_ASN1_CONSTRUCTED | 3 ) );
  55264. }
  55265. /*
  55266. * SubjectPublicKeyInfo
  55267. */
  55268. MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_pk_write_pubkey_der( ctx->subject_key,
  55269. tmp_buf, c - tmp_buf ) );
  55270. c -= pub_len;
  55271. len += pub_len;
  55272. /*
  55273. * Subject ::= Name
  55274. */
  55275. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, tmp_buf, ctx->subject ) );
  55276. /*
  55277. * Validity ::= SEQUENCE {
  55278. * notBefore Time,
  55279. * notAfter Time }
  55280. */
  55281. sub_len = 0;
  55282. MBEDTLS_ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_after,
  55283. MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) );
  55284. MBEDTLS_ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_before,
  55285. MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) );
  55286. len += sub_len;
  55287. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, sub_len ) );
  55288. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  55289. MBEDTLS_ASN1_SEQUENCE ) );
  55290. /*
  55291. * Issuer ::= Name
  55292. */
  55293. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, tmp_buf, ctx->issuer ) );
  55294. /*
  55295. * Signature ::= AlgorithmIdentifier
  55296. */
  55297. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, tmp_buf,
  55298. sig_oid, strlen( sig_oid ), 0 ) );
  55299. /*
  55300. * Serial ::= INTEGER
  55301. */
  55302. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, tmp_buf, &ctx->serial ) );
  55303. /*
  55304. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  55305. */
  55306. /* Can be omitted for v1 */
  55307. if( ctx->version != MBEDTLS_X509_CRT_VERSION_1 )
  55308. {
  55309. sub_len = 0;
  55310. MBEDTLS_ASN1_CHK_ADD( sub_len, mbedtls_asn1_write_int( &c, tmp_buf, ctx->version ) );
  55311. len += sub_len;
  55312. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, sub_len ) );
  55313. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC |
  55314. MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
  55315. }
  55316. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  55317. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  55318. MBEDTLS_ASN1_SEQUENCE ) );
  55319. /*
  55320. * Make signature
  55321. */
  55322. if( ( ret = mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c,
  55323. len, hash ) ) != 0 )
  55324. {
  55325. return( ret );
  55326. }
  55327. if( ( ret = mbedtls_pk_sign( ctx->issuer_key, ctx->md_alg, hash, 0, sig, &sig_len,
  55328. f_rng, p_rng ) ) != 0 )
  55329. {
  55330. return( ret );
  55331. }
  55332. /*
  55333. * Write data to output buffer
  55334. */
  55335. c2 = buf + size;
  55336. MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, buf,
  55337. sig_oid, sig_oid_len, sig, sig_len ) );
  55338. if( len > (size_t)( c2 - buf ) )
  55339. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  55340. c2 -= len;
  55341. memcpy( c2, c, len );
  55342. len += sig_and_oid_len;
  55343. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c2, buf, len ) );
  55344. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c2, buf, MBEDTLS_ASN1_CONSTRUCTED |
  55345. MBEDTLS_ASN1_SEQUENCE ) );
  55346. return( (int) len );
  55347. }
  55348. #define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
  55349. #define PEM_END_CRT "-----END CERTIFICATE-----\n"
  55350. #if defined(MBEDTLS_PEM_WRITE_C)
  55351. int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *crt, unsigned char *buf, size_t size,
  55352. int (*f_rng)(void *, unsigned char *, size_t),
  55353. void *p_rng )
  55354. {
  55355. int ret;
  55356. unsigned char output_buf[4096];
  55357. size_t olen = 0;
  55358. if( ( ret = mbedtls_x509write_crt_der( crt, output_buf, sizeof(output_buf),
  55359. f_rng, p_rng ) ) < 0 )
  55360. {
  55361. return( ret );
  55362. }
  55363. if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT,
  55364. output_buf + sizeof(output_buf) - ret,
  55365. ret, buf, size, &olen ) ) != 0 )
  55366. {
  55367. return( ret );
  55368. }
  55369. return( 0 );
  55370. }
  55371. #endif /* MBEDTLS_PEM_WRITE_C */
  55372. #endif /* MBEDTLS_X509_CRT_WRITE_C */
  55373. /*
  55374. Amalgamated build undefines
  55375. */
  55376. #undef ADD
  55377. #undef BC
  55378. #undef BEFORE_COLON
  55379. #undef F
  55380. #undef F0
  55381. #undef F1
  55382. #undef F2
  55383. #undef F3
  55384. #undef F4
  55385. #undef F5
  55386. #undef FSb
  55387. #undef K
  55388. #undef KK
  55389. #undef P
  55390. #undef R
  55391. #undef ROTR
  55392. #undef S
  55393. #undef S0
  55394. #undef S1
  55395. #undef S2
  55396. #undef S3
  55397. #undef SAFE_SNPRINTF
  55398. #undef SHR
  55399. #undef close
  55400. #undef read
  55401. #undef supported_init
  55402. #undef write
  55403. /********* Start of file library/x509write_csr.c ************/
  55404. /*
  55405. * X.509 Certificate Signing Request writing
  55406. *
  55407. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  55408. * SPDX-License-Identifier: Apache-2.0
  55409. *
  55410. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  55411. * not use this file except in compliance with the License.
  55412. * You may obtain a copy of the License at
  55413. *
  55414. * http://www.apache.org/licenses/LICENSE-2.0
  55415. *
  55416. * Unless required by applicable law or agreed to in writing, software
  55417. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  55418. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  55419. * See the License for the specific language governing permissions and
  55420. * limitations under the License.
  55421. *
  55422. * This file is part of mbed TLS (https://tls.mbed.org)
  55423. */
  55424. /*
  55425. * References:
  55426. * - CSRs: PKCS#10 v1.7 aka RFC 2986
  55427. * - attributes: PKCS#9 v2.0 aka RFC 2985
  55428. */
  55429. #if !defined(MBEDTLS_CONFIG_FILE)
  55430. #else
  55431. #endif
  55432. #if defined(MBEDTLS_X509_CSR_WRITE_C)
  55433. #include <string.h>
  55434. #include <stdlib.h>
  55435. #if defined(MBEDTLS_PEM_WRITE_C)
  55436. #endif
  55437. /* Implementation that should never be optimized out by the compiler */
  55438. /* zeroize was here */
  55439. void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx )
  55440. {
  55441. memset( ctx, 0, sizeof( mbedtls_x509write_csr ) );
  55442. }
  55443. void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx )
  55444. {
  55445. mbedtls_asn1_free_named_data_list( &ctx->subject );
  55446. mbedtls_asn1_free_named_data_list( &ctx->extensions );
  55447. mbedtls_zeroize( ctx, sizeof( mbedtls_x509write_csr ) );
  55448. }
  55449. void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg )
  55450. {
  55451. ctx->md_alg = md_alg;
  55452. }
  55453. void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key )
  55454. {
  55455. ctx->key = key;
  55456. }
  55457. int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
  55458. const char *subject_name )
  55459. {
  55460. return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
  55461. }
  55462. int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
  55463. const char *oid, size_t oid_len,
  55464. const unsigned char *val, size_t val_len )
  55465. {
  55466. return mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
  55467. 0, val, val_len );
  55468. }
  55469. int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage )
  55470. {
  55471. unsigned char buf[4];
  55472. unsigned char *c;
  55473. int ret;
  55474. c = buf + 4;
  55475. if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &key_usage, 7 ) ) != 4 )
  55476. return( ret );
  55477. ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
  55478. MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
  55479. buf, 4 );
  55480. if( ret != 0 )
  55481. return( ret );
  55482. return( 0 );
  55483. }
  55484. int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
  55485. unsigned char ns_cert_type )
  55486. {
  55487. unsigned char buf[4];
  55488. unsigned char *c;
  55489. int ret;
  55490. c = buf + 4;
  55491. if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
  55492. return( ret );
  55493. ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
  55494. MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
  55495. buf, 4 );
  55496. if( ret != 0 )
  55497. return( ret );
  55498. return( 0 );
  55499. }
  55500. int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  55501. int (*f_rng)(void *, unsigned char *, size_t),
  55502. void *p_rng )
  55503. {
  55504. int ret;
  55505. const char *sig_oid;
  55506. size_t sig_oid_len = 0;
  55507. unsigned char *c, *c2;
  55508. unsigned char hash[64];
  55509. unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
  55510. unsigned char tmp_buf[2048];
  55511. size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
  55512. size_t len = 0;
  55513. mbedtls_pk_type_t pk_alg;
  55514. /*
  55515. * Prepare data to be signed in tmp_buf
  55516. */
  55517. c = tmp_buf + sizeof( tmp_buf );
  55518. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
  55519. if( len )
  55520. {
  55521. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  55522. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  55523. MBEDTLS_ASN1_SEQUENCE ) );
  55524. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  55525. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  55526. MBEDTLS_ASN1_SET ) );
  55527. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( &c, tmp_buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
  55528. MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_CSR_EXT_REQ ) ) );
  55529. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  55530. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  55531. MBEDTLS_ASN1_SEQUENCE ) );
  55532. }
  55533. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  55534. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  55535. MBEDTLS_ASN1_CONTEXT_SPECIFIC ) );
  55536. MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_pk_write_pubkey_der( ctx->key,
  55537. tmp_buf, c - tmp_buf ) );
  55538. c -= pub_len;
  55539. len += pub_len;
  55540. /*
  55541. * Subject ::= Name
  55542. */
  55543. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, tmp_buf, ctx->subject ) );
  55544. /*
  55545. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  55546. */
  55547. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, tmp_buf, 0 ) );
  55548. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  55549. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  55550. MBEDTLS_ASN1_SEQUENCE ) );
  55551. /*
  55552. * Prepare signature
  55553. */
  55554. mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
  55555. if( ( ret = mbedtls_pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
  55556. f_rng, p_rng ) ) != 0 )
  55557. {
  55558. return( ret );
  55559. }
  55560. if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_RSA ) )
  55561. pk_alg = MBEDTLS_PK_RSA;
  55562. else if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_ECDSA ) )
  55563. pk_alg = MBEDTLS_PK_ECDSA;
  55564. else
  55565. return( MBEDTLS_ERR_X509_INVALID_ALG );
  55566. if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
  55567. &sig_oid, &sig_oid_len ) ) != 0 )
  55568. {
  55569. return( ret );
  55570. }
  55571. /*
  55572. * Write data to output buffer
  55573. */
  55574. c2 = buf + size;
  55575. MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, buf,
  55576. sig_oid, sig_oid_len, sig, sig_len ) );
  55577. if( len > (size_t)( c2 - buf ) )
  55578. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  55579. c2 -= len;
  55580. memcpy( c2, c, len );
  55581. len += sig_and_oid_len;
  55582. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c2, buf, len ) );
  55583. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c2, buf, MBEDTLS_ASN1_CONSTRUCTED |
  55584. MBEDTLS_ASN1_SEQUENCE ) );
  55585. return( (int) len );
  55586. }
  55587. #define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
  55588. #define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
  55589. #if defined(MBEDTLS_PEM_WRITE_C)
  55590. int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  55591. int (*f_rng)(void *, unsigned char *, size_t),
  55592. void *p_rng )
  55593. {
  55594. int ret;
  55595. unsigned char output_buf[4096];
  55596. size_t olen = 0;
  55597. if( ( ret = mbedtls_x509write_csr_der( ctx, output_buf, sizeof(output_buf),
  55598. f_rng, p_rng ) ) < 0 )
  55599. {
  55600. return( ret );
  55601. }
  55602. if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CSR, PEM_END_CSR,
  55603. output_buf + sizeof(output_buf) - ret,
  55604. ret, buf, size, &olen ) ) != 0 )
  55605. {
  55606. return( ret );
  55607. }
  55608. return( 0 );
  55609. }
  55610. #endif /* MBEDTLS_PEM_WRITE_C */
  55611. #endif /* MBEDTLS_X509_CSR_WRITE_C */
  55612. /*
  55613. Amalgamated build undefines
  55614. */
  55615. #undef ADD
  55616. #undef BC
  55617. #undef BEFORE_COLON
  55618. #undef F
  55619. #undef F0
  55620. #undef F1
  55621. #undef F2
  55622. #undef F3
  55623. #undef F4
  55624. #undef F5
  55625. #undef FSb
  55626. #undef K
  55627. #undef KK
  55628. #undef P
  55629. #undef R
  55630. #undef ROTR
  55631. #undef S
  55632. #undef S0
  55633. #undef S1
  55634. #undef S2
  55635. #undef S3
  55636. #undef SAFE_SNPRINTF
  55637. #undef SHR
  55638. #undef close
  55639. #undef read
  55640. #undef supported_init
  55641. #undef write
  55642. /********* Start of file library/xtea.c ************/
  55643. /*
  55644. * An 32-bit implementation of the XTEA algorithm
  55645. *
  55646. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  55647. * SPDX-License-Identifier: Apache-2.0
  55648. *
  55649. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  55650. * not use this file except in compliance with the License.
  55651. * You may obtain a copy of the License at
  55652. *
  55653. * http://www.apache.org/licenses/LICENSE-2.0
  55654. *
  55655. * Unless required by applicable law or agreed to in writing, software
  55656. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  55657. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  55658. * See the License for the specific language governing permissions and
  55659. * limitations under the License.
  55660. *
  55661. * This file is part of mbed TLS (https://tls.mbed.org)
  55662. */
  55663. #if !defined(MBEDTLS_CONFIG_FILE)
  55664. #else
  55665. #endif
  55666. #if defined(MBEDTLS_XTEA_C)
  55667. #include <string.h>
  55668. #if defined(MBEDTLS_SELF_TEST)
  55669. #if defined(MBEDTLS_PLATFORM_C)
  55670. #else
  55671. #include <stdio.h>
  55672. #define mbedtls_printf printf
  55673. #endif /* MBEDTLS_PLATFORM_C */
  55674. #endif /* MBEDTLS_SELF_TEST */
  55675. #if !defined(MBEDTLS_XTEA_ALT)
  55676. /* Implementation that should never be optimized out by the compiler */
  55677. /* zeroize was here */
  55678. /*
  55679. * 32-bit integer manipulation macros (big endian)
  55680. */
  55681. #ifndef GET_UINT32_BE
  55682. #define GET_UINT32_BE(n,b,i) \
  55683. { \
  55684. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  55685. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  55686. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  55687. | ( (uint32_t) (b)[(i) + 3] ); \
  55688. }
  55689. #endif
  55690. #ifndef PUT_UINT32_BE
  55691. #define PUT_UINT32_BE(n,b,i) \
  55692. { \
  55693. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  55694. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  55695. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  55696. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  55697. }
  55698. #endif
  55699. void mbedtls_xtea_init( mbedtls_xtea_context *ctx )
  55700. {
  55701. memset( ctx, 0, sizeof( mbedtls_xtea_context ) );
  55702. }
  55703. void mbedtls_xtea_free( mbedtls_xtea_context *ctx )
  55704. {
  55705. if( ctx == NULL )
  55706. return;
  55707. mbedtls_zeroize( ctx, sizeof( mbedtls_xtea_context ) );
  55708. }
  55709. /*
  55710. * XTEA key schedule
  55711. */
  55712. void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] )
  55713. {
  55714. int i;
  55715. memset( ctx, 0, sizeof(mbedtls_xtea_context) );
  55716. for( i = 0; i < 4; i++ )
  55717. {
  55718. GET_UINT32_BE( ctx->k[i], key, i << 2 );
  55719. }
  55720. }
  55721. /*
  55722. * XTEA encrypt function
  55723. */
  55724. int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, int mode,
  55725. const unsigned char input[8], unsigned char output[8])
  55726. {
  55727. uint32_t *k, v0, v1, i;
  55728. k = ctx->k;
  55729. GET_UINT32_BE( v0, input, 0 );
  55730. GET_UINT32_BE( v1, input, 4 );
  55731. if( mode == MBEDTLS_XTEA_ENCRYPT )
  55732. {
  55733. uint32_t sum = 0, delta = 0x9E3779B9;
  55734. for( i = 0; i < 32; i++ )
  55735. {
  55736. v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  55737. sum += delta;
  55738. v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  55739. }
  55740. }
  55741. else /* MBEDTLS_XTEA_DECRYPT */
  55742. {
  55743. uint32_t delta = 0x9E3779B9, sum = delta * 32;
  55744. for( i = 0; i < 32; i++ )
  55745. {
  55746. v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  55747. sum -= delta;
  55748. v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  55749. }
  55750. }
  55751. PUT_UINT32_BE( v0, output, 0 );
  55752. PUT_UINT32_BE( v1, output, 4 );
  55753. return( 0 );
  55754. }
  55755. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  55756. /*
  55757. * XTEA-CBC buffer encryption/decryption
  55758. */
  55759. int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, int mode, size_t length,
  55760. unsigned char iv[8], const unsigned char *input,
  55761. unsigned char *output)
  55762. {
  55763. int i;
  55764. unsigned char temp[8];
  55765. if( length % 8 )
  55766. return( MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH );
  55767. if( mode == MBEDTLS_XTEA_DECRYPT )
  55768. {
  55769. while( length > 0 )
  55770. {
  55771. memcpy( temp, input, 8 );
  55772. mbedtls_xtea_crypt_ecb( ctx, mode, input, output );
  55773. for( i = 0; i < 8; i++ )
  55774. output[i] = (unsigned char)( output[i] ^ iv[i] );
  55775. memcpy( iv, temp, 8 );
  55776. input += 8;
  55777. output += 8;
  55778. length -= 8;
  55779. }
  55780. }
  55781. else
  55782. {
  55783. while( length > 0 )
  55784. {
  55785. for( i = 0; i < 8; i++ )
  55786. output[i] = (unsigned char)( input[i] ^ iv[i] );
  55787. mbedtls_xtea_crypt_ecb( ctx, mode, output, output );
  55788. memcpy( iv, output, 8 );
  55789. input += 8;
  55790. output += 8;
  55791. length -= 8;
  55792. }
  55793. }
  55794. return( 0 );
  55795. }
  55796. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  55797. #endif /* !MBEDTLS_XTEA_ALT */
  55798. #if defined(MBEDTLS_SELF_TEST)
  55799. /*
  55800. * XTEA tests vectors (non-official)
  55801. */
  55802. static const unsigned char xtea_test_key[6][16] =
  55803. {
  55804. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  55805. 0x0c, 0x0d, 0x0e, 0x0f },
  55806. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  55807. 0x0c, 0x0d, 0x0e, 0x0f },
  55808. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  55809. 0x0c, 0x0d, 0x0e, 0x0f },
  55810. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  55811. 0x00, 0x00, 0x00, 0x00 },
  55812. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  55813. 0x00, 0x00, 0x00, 0x00 },
  55814. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  55815. 0x00, 0x00, 0x00, 0x00 }
  55816. };
  55817. static const unsigned char xtea_test_pt[6][8] =
  55818. {
  55819. { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
  55820. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  55821. { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
  55822. { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
  55823. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  55824. { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
  55825. };
  55826. static const unsigned char xtea_test_ct[6][8] =
  55827. {
  55828. { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
  55829. { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
  55830. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  55831. { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
  55832. { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
  55833. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
  55834. };
  55835. /*
  55836. * Checkup routine
  55837. */
  55838. int mbedtls_xtea_self_test( int verbose )
  55839. {
  55840. int i, ret = 0;
  55841. unsigned char buf[8];
  55842. mbedtls_xtea_context ctx;
  55843. mbedtls_xtea_init( &ctx );
  55844. for( i = 0; i < 6; i++ )
  55845. {
  55846. if( verbose != 0 )
  55847. mbedtls_printf( " XTEA test #%d: ", i + 1 );
  55848. memcpy( buf, xtea_test_pt[i], 8 );
  55849. mbedtls_xtea_setup( &ctx, xtea_test_key[i] );
  55850. mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf );
  55851. if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
  55852. {
  55853. if( verbose != 0 )
  55854. mbedtls_printf( "failed\n" );
  55855. ret = 1;
  55856. goto exit;
  55857. }
  55858. if( verbose != 0 )
  55859. mbedtls_printf( "passed\n" );
  55860. }
  55861. if( verbose != 0 )
  55862. mbedtls_printf( "\n" );
  55863. exit:
  55864. mbedtls_xtea_free( &ctx );
  55865. return( ret );
  55866. }
  55867. #endif /* MBEDTLS_SELF_TEST */
  55868. #endif /* MBEDTLS_XTEA_C */
  55869. /*
  55870. Amalgamated build undefines
  55871. */
  55872. #undef ADD
  55873. #undef BC
  55874. #undef BEFORE_COLON
  55875. #undef F
  55876. #undef F0
  55877. #undef F1
  55878. #undef F2
  55879. #undef F3
  55880. #undef F4
  55881. #undef F5
  55882. #undef FSb
  55883. #undef K
  55884. #undef KK
  55885. #undef P
  55886. #undef R
  55887. #undef ROTR
  55888. #undef S
  55889. #undef S0
  55890. #undef S1
  55891. #undef S2
  55892. #undef S3
  55893. #undef SAFE_SNPRINTF
  55894. #undef SHR
  55895. #undef close
  55896. #undef read
  55897. #undef supported_init
  55898. #undef write
  55899. #endif /* ME_COM_MBEDTLS */