mbedtls.h 738 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211
  1. /*
  2. * MbedTLS Source Code Library Header
  3. */
  4. #include "me.h"
  5. #if ME_COM_MBEDTLS
  6. #if defined(MBEDTLS_CONFIG_FILE)
  7. #include MBEDTLS_CONFIG_FILE
  8. #endif
  9. /********* Start of file include/mbedtls/config.h ************/
  10. /**
  11. * \file config.h
  12. *
  13. * \brief Configuration options (set of defines)
  14. *
  15. * This set of compile-time options may be used to enable
  16. * or disable features selectively, and reduce the global
  17. * memory footprint.
  18. *
  19. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  20. * SPDX-License-Identifier: Apache-2.0
  21. *
  22. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  23. * not use this file except in compliance with the License.
  24. * You may obtain a copy of the License at
  25. *
  26. * http://www.apache.org/licenses/LICENSE-2.0
  27. *
  28. * Unless required by applicable law or agreed to in writing, software
  29. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  30. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  31. * See the License for the specific language governing permissions and
  32. * limitations under the License.
  33. *
  34. * This file is part of mbed TLS (https://tls.mbed.org)
  35. */
  36. #ifndef MBEDTLS_CONFIG_H
  37. #define MBEDTLS_CONFIG_H
  38. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
  39. #define _CRT_SECURE_NO_DEPRECATE 1
  40. #endif
  41. /**
  42. * \name SECTION: System support
  43. *
  44. * This section sets system specific settings.
  45. * \{
  46. */
  47. /**
  48. * \def MBEDTLS_HAVE_ASM
  49. *
  50. * The compiler has support for asm().
  51. *
  52. * Requires support for asm() in compiler.
  53. *
  54. * Used in:
  55. * library/timing.c
  56. * library/padlock.c
  57. * include/mbedtls/bn_mul.h
  58. *
  59. * Comment to disable the use of assembly code.
  60. */
  61. #define MBEDTLS_HAVE_ASM
  62. /**
  63. * \def MBEDTLS_HAVE_SSE2
  64. *
  65. * CPU supports SSE2 instruction set.
  66. *
  67. * Uncomment if the CPU supports SSE2 (IA-32 specific).
  68. */
  69. //#define MBEDTLS_HAVE_SSE2
  70. /**
  71. * \def MBEDTLS_HAVE_TIME
  72. *
  73. * System has time.h and time().
  74. * The time does not need to be correct, only time differences are used,
  75. * by contrast with MBEDTLS_HAVE_TIME_DATE
  76. *
  77. * Comment if your system does not support time functions
  78. */
  79. #define MBEDTLS_HAVE_TIME
  80. /**
  81. * \def MBEDTLS_HAVE_TIME_DATE
  82. *
  83. * System has time.h and time(), gmtime() and the clock is correct.
  84. * The time needs to be correct (not necesarily very accurate, but at least
  85. * the date should be correct). This is used to verify the validity period of
  86. * X.509 certificates.
  87. *
  88. * Comment if your system does not have a correct clock.
  89. */
  90. #define MBEDTLS_HAVE_TIME_DATE
  91. /**
  92. * \def MBEDTLS_PLATFORM_MEMORY
  93. *
  94. * Enable the memory allocation layer.
  95. *
  96. * By default mbed TLS uses the system-provided calloc() and free().
  97. * This allows different allocators (self-implemented or provided) to be
  98. * provided to the platform abstraction layer.
  99. *
  100. * Enabling MBEDTLS_PLATFORM_MEMORY without the
  101. * MBEDTLS_PLATFORM_{FREE,CALLOC}_MACROs will provide
  102. * "mbedtls_platform_set_calloc_free()" allowing you to set an alternative calloc() and
  103. * free() function pointer at runtime.
  104. *
  105. * Enabling MBEDTLS_PLATFORM_MEMORY and specifying
  106. * MBEDTLS_PLATFORM_{CALLOC,FREE}_MACROs will allow you to specify the
  107. * alternate function at compile time.
  108. *
  109. * Requires: MBEDTLS_PLATFORM_C
  110. *
  111. * Enable this layer to allow use of alternative memory allocators.
  112. */
  113. //#define MBEDTLS_PLATFORM_MEMORY
  114. /**
  115. * \def MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
  116. *
  117. * Do not assign standard functions in the platform layer (e.g. calloc() to
  118. * MBEDTLS_PLATFORM_STD_CALLOC and printf() to MBEDTLS_PLATFORM_STD_PRINTF)
  119. *
  120. * This makes sure there are no linking errors on platforms that do not support
  121. * these functions. You will HAVE to provide alternatives, either at runtime
  122. * via the platform_set_xxx() functions or at compile time by setting
  123. * the MBEDTLS_PLATFORM_STD_XXX defines, or enabling a
  124. * MBEDTLS_PLATFORM_XXX_MACRO.
  125. *
  126. * Requires: MBEDTLS_PLATFORM_C
  127. *
  128. * Uncomment to prevent default assignment of standard functions in the
  129. * platform layer.
  130. */
  131. //#define MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
  132. /**
  133. * \def MBEDTLS_PLATFORM_EXIT_ALT
  134. *
  135. * MBEDTLS_PLATFORM_XXX_ALT: Uncomment a macro to let mbed TLS support the
  136. * function in the platform abstraction layer.
  137. *
  138. * Example: In case you uncomment MBEDTLS_PLATFORM_PRINTF_ALT, mbed TLS will
  139. * provide a function "mbedtls_platform_set_printf()" that allows you to set an
  140. * alternative printf function pointer.
  141. *
  142. * All these define require MBEDTLS_PLATFORM_C to be defined!
  143. *
  144. * \note MBEDTLS_PLATFORM_SNPRINTF_ALT is required on Windows;
  145. * it will be enabled automatically by check_config.h
  146. *
  147. * \warning MBEDTLS_PLATFORM_XXX_ALT cannot be defined at the same time as
  148. * MBEDTLS_PLATFORM_XXX_MACRO!
  149. *
  150. * Uncomment a macro to enable alternate implementation of specific base
  151. * platform function
  152. */
  153. //#define MBEDTLS_PLATFORM_EXIT_ALT
  154. //#define MBEDTLS_PLATFORM_FPRINTF_ALT
  155. //#define MBEDTLS_PLATFORM_PRINTF_ALT
  156. //#define MBEDTLS_PLATFORM_SNPRINTF_ALT
  157. /**
  158. * \def MBEDTLS_DEPRECATED_WARNING
  159. *
  160. * Mark deprecated functions so that they generate a warning if used.
  161. * Functions deprecated in one version will usually be removed in the next
  162. * version. You can enable this to help you prepare the transition to a new
  163. * major version by making sure your code is not using these functions.
  164. *
  165. * This only works with GCC and Clang. With other compilers, you may want to
  166. * use MBEDTLS_DEPRECATED_REMOVED
  167. *
  168. * Uncomment to get warnings on using deprecated functions.
  169. */
  170. //#define MBEDTLS_DEPRECATED_WARNING
  171. /**
  172. * \def MBEDTLS_DEPRECATED_REMOVED
  173. *
  174. * Remove deprecated functions so that they generate an error if used.
  175. * Functions deprecated in one version will usually be removed in the next
  176. * version. You can enable this to help you prepare the transition to a new
  177. * major version by making sure your code is not using these functions.
  178. *
  179. * Uncomment to get errors on using deprecated functions.
  180. */
  181. //#define MBEDTLS_DEPRECATED_REMOVED
  182. /* \} name SECTION: System support */
  183. /**
  184. * \name SECTION: mbed TLS feature support
  185. *
  186. * This section sets support for features that are or are not needed
  187. * within the modules that are enabled.
  188. * \{
  189. */
  190. /**
  191. * \def MBEDTLS_TIMING_ALT
  192. *
  193. * Uncomment to provide your own alternate implementation for mbedtls_timing_hardclock(),
  194. * mbedtls_timing_get_timer(), mbedtls_set_alarm(), mbedtls_set/get_delay()
  195. *
  196. * Only works if you have MBEDTLS_TIMING_C enabled.
  197. *
  198. * You will need to provide a header "timing_alt.h" and an implementation at
  199. * compile time.
  200. */
  201. //#define MBEDTLS_TIMING_ALT
  202. /**
  203. * \def MBEDTLS_AES_ALT
  204. *
  205. * MBEDTLS__MODULE_NAME__ALT: Uncomment a macro to let mbed TLS use your
  206. * alternate core implementation of a symmetric crypto or hash module (e.g.
  207. * platform specific assembly optimized implementations). Keep in mind that
  208. * the function prototypes should remain the same.
  209. *
  210. * This replaces the whole module. If you only want to replace one of the
  211. * functions, use one of the MBEDTLS__FUNCTION_NAME__ALT flags.
  212. *
  213. * Example: In case you uncomment MBEDTLS_AES_ALT, mbed TLS will no longer
  214. * provide the "struct mbedtls_aes_context" definition and omit the base function
  215. * declarations and implementations. "aes_alt.h" will be included from
  216. * "aes.h" to include the new function definitions.
  217. *
  218. * Uncomment a macro to enable alternate implementation of the corresponding
  219. * module.
  220. */
  221. //#define MBEDTLS_AES_ALT
  222. //#define MBEDTLS_ARC4_ALT
  223. //#define MBEDTLS_BLOWFISH_ALT
  224. //#define MBEDTLS_CAMELLIA_ALT
  225. //#define MBEDTLS_DES_ALT
  226. //#define MBEDTLS_XTEA_ALT
  227. //#define MBEDTLS_MD2_ALT
  228. //#define MBEDTLS_MD4_ALT
  229. //#define MBEDTLS_MD5_ALT
  230. //#define MBEDTLS_RIPEMD160_ALT
  231. //#define MBEDTLS_SHA1_ALT
  232. //#define MBEDTLS_SHA256_ALT
  233. //#define MBEDTLS_SHA512_ALT
  234. /**
  235. * \def MBEDTLS_MD2_PROCESS_ALT
  236. *
  237. * MBEDTLS__FUNCTION_NAME__ALT: Uncomment a macro to let mbed TLS use you
  238. * alternate core implementation of symmetric crypto or hash function. Keep in
  239. * mind that function prototypes should remain the same.
  240. *
  241. * This replaces only one function. The header file from mbed TLS is still
  242. * used, in contrast to the MBEDTLS__MODULE_NAME__ALT flags.
  243. *
  244. * Example: In case you uncomment MBEDTLS_SHA256_PROCESS_ALT, mbed TLS will
  245. * no longer provide the mbedtls_sha1_process() function, but it will still provide
  246. * the other function (using your mbedtls_sha1_process() function) and the definition
  247. * of mbedtls_sha1_context, so your implementation of mbedtls_sha1_process must be compatible
  248. * with this definition.
  249. *
  250. * Note: if you use the AES_xxx_ALT macros, then is is recommended to also set
  251. * MBEDTLS_AES_ROM_TABLES in order to help the linker garbage-collect the AES
  252. * tables.
  253. *
  254. * Uncomment a macro to enable alternate implementation of the corresponding
  255. * function.
  256. */
  257. //#define MBEDTLS_MD2_PROCESS_ALT
  258. //#define MBEDTLS_MD4_PROCESS_ALT
  259. //#define MBEDTLS_MD5_PROCESS_ALT
  260. //#define MBEDTLS_RIPEMD160_PROCESS_ALT
  261. //#define MBEDTLS_SHA1_PROCESS_ALT
  262. //#define MBEDTLS_SHA256_PROCESS_ALT
  263. //#define MBEDTLS_SHA512_PROCESS_ALT
  264. //#define MBEDTLS_DES_SETKEY_ALT
  265. //#define MBEDTLS_DES_CRYPT_ECB_ALT
  266. //#define MBEDTLS_DES3_CRYPT_ECB_ALT
  267. //#define MBEDTLS_AES_SETKEY_ENC_ALT
  268. //#define MBEDTLS_AES_SETKEY_DEC_ALT
  269. //#define MBEDTLS_AES_ENCRYPT_ALT
  270. //#define MBEDTLS_AES_DECRYPT_ALT
  271. /**
  272. * \def MBEDTLS_ENTROPY_HARDWARE_ALT
  273. *
  274. * Uncomment this macro to let mbed TLS use your own implementation of a
  275. * hardware entropy collector.
  276. *
  277. * Your function must be called \c mbedtls_hardware_poll(), have the same
  278. * prototype as declared in entropy_poll.h, and accept NULL as first argument.
  279. *
  280. * Uncomment to use your own hardware entropy collector.
  281. */
  282. //#define MBEDTLS_ENTROPY_HARDWARE_ALT
  283. /**
  284. * \def MBEDTLS_AES_ROM_TABLES
  285. *
  286. * Store the AES tables in ROM.
  287. *
  288. * Uncomment this macro to store the AES tables in ROM.
  289. */
  290. //#define MBEDTLS_AES_ROM_TABLES
  291. /**
  292. * \def MBEDTLS_CAMELLIA_SMALL_MEMORY
  293. *
  294. * Use less ROM for the Camellia implementation (saves about 768 bytes).
  295. *
  296. * Uncomment this macro to use less memory for Camellia.
  297. */
  298. //#define MBEDTLS_CAMELLIA_SMALL_MEMORY
  299. /**
  300. * \def MBEDTLS_CIPHER_MODE_CBC
  301. *
  302. * Enable Cipher Block Chaining mode (CBC) for symmetric ciphers.
  303. */
  304. #define MBEDTLS_CIPHER_MODE_CBC
  305. /**
  306. * \def MBEDTLS_CIPHER_MODE_CFB
  307. *
  308. * Enable Cipher Feedback mode (CFB) for symmetric ciphers.
  309. */
  310. #define MBEDTLS_CIPHER_MODE_CFB
  311. /**
  312. * \def MBEDTLS_CIPHER_MODE_CTR
  313. *
  314. * Enable Counter Block Cipher mode (CTR) for symmetric ciphers.
  315. */
  316. #define MBEDTLS_CIPHER_MODE_CTR
  317. /**
  318. * \def MBEDTLS_CIPHER_NULL_CIPHER
  319. *
  320. * Enable NULL cipher.
  321. * Warning: Only do so when you know what you are doing. This allows for
  322. * encryption or channels without any security!
  323. *
  324. * Requires MBEDTLS_ENABLE_WEAK_CIPHERSUITES as well to enable
  325. * the following ciphersuites:
  326. * MBEDTLS_TLS_ECDH_ECDSA_WITH_NULL_SHA
  327. * MBEDTLS_TLS_ECDH_RSA_WITH_NULL_SHA
  328. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_NULL_SHA
  329. * MBEDTLS_TLS_ECDHE_RSA_WITH_NULL_SHA
  330. * MBEDTLS_TLS_ECDHE_PSK_WITH_NULL_SHA384
  331. * MBEDTLS_TLS_ECDHE_PSK_WITH_NULL_SHA256
  332. * MBEDTLS_TLS_ECDHE_PSK_WITH_NULL_SHA
  333. * MBEDTLS_TLS_DHE_PSK_WITH_NULL_SHA384
  334. * MBEDTLS_TLS_DHE_PSK_WITH_NULL_SHA256
  335. * MBEDTLS_TLS_DHE_PSK_WITH_NULL_SHA
  336. * MBEDTLS_TLS_RSA_WITH_NULL_SHA256
  337. * MBEDTLS_TLS_RSA_WITH_NULL_SHA
  338. * MBEDTLS_TLS_RSA_WITH_NULL_MD5
  339. * MBEDTLS_TLS_RSA_PSK_WITH_NULL_SHA384
  340. * MBEDTLS_TLS_RSA_PSK_WITH_NULL_SHA256
  341. * MBEDTLS_TLS_RSA_PSK_WITH_NULL_SHA
  342. * MBEDTLS_TLS_PSK_WITH_NULL_SHA384
  343. * MBEDTLS_TLS_PSK_WITH_NULL_SHA256
  344. * MBEDTLS_TLS_PSK_WITH_NULL_SHA
  345. *
  346. * Uncomment this macro to enable the NULL cipher and ciphersuites
  347. */
  348. //#define MBEDTLS_CIPHER_NULL_CIPHER
  349. /**
  350. * \def MBEDTLS_CIPHER_PADDING_PKCS7
  351. *
  352. * MBEDTLS_CIPHER_PADDING_XXX: Uncomment or comment macros to add support for
  353. * specific padding modes in the cipher layer with cipher modes that support
  354. * padding (e.g. CBC)
  355. *
  356. * If you disable all padding modes, only full blocks can be used with CBC.
  357. *
  358. * Enable padding modes in the cipher layer.
  359. */
  360. #define MBEDTLS_CIPHER_PADDING_PKCS7
  361. #define MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS
  362. #define MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN
  363. #define MBEDTLS_CIPHER_PADDING_ZEROS
  364. /**
  365. * \def MBEDTLS_ENABLE_WEAK_CIPHERSUITES
  366. *
  367. * Enable weak ciphersuites in SSL / TLS.
  368. * Warning: Only do so when you know what you are doing. This allows for
  369. * channels with virtually no security at all!
  370. *
  371. * This enables the following ciphersuites:
  372. * MBEDTLS_TLS_RSA_WITH_DES_CBC_SHA
  373. * MBEDTLS_TLS_DHE_RSA_WITH_DES_CBC_SHA
  374. *
  375. * Uncomment this macro to enable weak ciphersuites
  376. */
  377. //#define MBEDTLS_ENABLE_WEAK_CIPHERSUITES
  378. /**
  379. * \def MBEDTLS_REMOVE_ARC4_CIPHERSUITES
  380. *
  381. * Remove RC4 ciphersuites by default in SSL / TLS.
  382. * This flag removes the ciphersuites based on RC4 from the default list as
  383. * returned by mbedtls_ssl_list_ciphersuites(). However, it is still possible to
  384. * enable (some of) them with mbedtls_ssl_conf_ciphersuites() by including them
  385. * explicitly.
  386. *
  387. * Uncomment this macro to remove RC4 ciphersuites by default.
  388. */
  389. #define MBEDTLS_REMOVE_ARC4_CIPHERSUITES
  390. /**
  391. * \def MBEDTLS_ECP_DP_SECP192R1_ENABLED
  392. *
  393. * MBEDTLS_ECP_XXXX_ENABLED: Enables specific curves within the Elliptic Curve
  394. * module. By default all supported curves are enabled.
  395. *
  396. * Comment macros to disable the curve and functions for it
  397. */
  398. #define MBEDTLS_ECP_DP_SECP192R1_ENABLED
  399. #define MBEDTLS_ECP_DP_SECP224R1_ENABLED
  400. #define MBEDTLS_ECP_DP_SECP256R1_ENABLED
  401. #define MBEDTLS_ECP_DP_SECP384R1_ENABLED
  402. #define MBEDTLS_ECP_DP_SECP521R1_ENABLED
  403. #define MBEDTLS_ECP_DP_SECP192K1_ENABLED
  404. #define MBEDTLS_ECP_DP_SECP224K1_ENABLED
  405. #define MBEDTLS_ECP_DP_SECP256K1_ENABLED
  406. #define MBEDTLS_ECP_DP_BP256R1_ENABLED
  407. #define MBEDTLS_ECP_DP_BP384R1_ENABLED
  408. #define MBEDTLS_ECP_DP_BP512R1_ENABLED
  409. #define MBEDTLS_ECP_DP_CURVE25519_ENABLED
  410. /**
  411. * \def MBEDTLS_ECP_NIST_OPTIM
  412. *
  413. * Enable specific 'modulo p' routines for each NIST prime.
  414. * Depending on the prime and architecture, makes operations 4 to 8 times
  415. * faster on the corresponding curve.
  416. *
  417. * Comment this macro to disable NIST curves optimisation.
  418. */
  419. #define MBEDTLS_ECP_NIST_OPTIM
  420. /**
  421. * \def MBEDTLS_ECDSA_DETERMINISTIC
  422. *
  423. * Enable deterministic ECDSA (RFC 6979).
  424. * Standard ECDSA is "fragile" in the sense that lack of entropy when signing
  425. * may result in a compromise of the long-term signing key. This is avoided by
  426. * the deterministic variant.
  427. *
  428. * Requires: MBEDTLS_HMAC_DRBG_C
  429. *
  430. * Comment this macro to disable deterministic ECDSA.
  431. */
  432. #define MBEDTLS_ECDSA_DETERMINISTIC
  433. /**
  434. * \def MBEDTLS_KEY_EXCHANGE_PSK_ENABLED
  435. *
  436. * Enable the PSK based ciphersuite modes in SSL / TLS.
  437. *
  438. * This enables the following ciphersuites (if other requisites are
  439. * enabled as well):
  440. * MBEDTLS_TLS_PSK_WITH_AES_256_GCM_SHA384
  441. * MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA384
  442. * MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA
  443. * MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384
  444. * MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384
  445. * MBEDTLS_TLS_PSK_WITH_AES_128_GCM_SHA256
  446. * MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA256
  447. * MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA
  448. * MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256
  449. * MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256
  450. * MBEDTLS_TLS_PSK_WITH_3DES_EDE_CBC_SHA
  451. * MBEDTLS_TLS_PSK_WITH_RC4_128_SHA
  452. */
  453. #define MBEDTLS_KEY_EXCHANGE_PSK_ENABLED
  454. /**
  455. * \def MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED
  456. *
  457. * Enable the DHE-PSK based ciphersuite modes in SSL / TLS.
  458. *
  459. * Requires: MBEDTLS_DHM_C
  460. *
  461. * This enables the following ciphersuites (if other requisites are
  462. * enabled as well):
  463. * MBEDTLS_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384
  464. * MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384
  465. * MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA
  466. * MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384
  467. * MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384
  468. * MBEDTLS_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256
  469. * MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256
  470. * MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA
  471. * MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256
  472. * MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256
  473. * MBEDTLS_TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA
  474. * MBEDTLS_TLS_DHE_PSK_WITH_RC4_128_SHA
  475. */
  476. #define MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED
  477. /**
  478. * \def MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED
  479. *
  480. * Enable the ECDHE-PSK based ciphersuite modes in SSL / TLS.
  481. *
  482. * Requires: MBEDTLS_ECDH_C
  483. *
  484. * This enables the following ciphersuites (if other requisites are
  485. * enabled as well):
  486. * MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384
  487. * MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA
  488. * MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384
  489. * MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256
  490. * MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA
  491. * MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256
  492. * MBEDTLS_TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA
  493. * MBEDTLS_TLS_ECDHE_PSK_WITH_RC4_128_SHA
  494. */
  495. #define MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED
  496. /**
  497. * \def MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
  498. *
  499. * Enable the RSA-PSK based ciphersuite modes in SSL / TLS.
  500. *
  501. * Requires: MBEDTLS_RSA_C, MBEDTLS_PKCS1_V15,
  502. * MBEDTLS_X509_CRT_PARSE_C
  503. *
  504. * This enables the following ciphersuites (if other requisites are
  505. * enabled as well):
  506. * MBEDTLS_TLS_RSA_PSK_WITH_AES_256_GCM_SHA384
  507. * MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA384
  508. * MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA
  509. * MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384
  510. * MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384
  511. * MBEDTLS_TLS_RSA_PSK_WITH_AES_128_GCM_SHA256
  512. * MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA256
  513. * MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA
  514. * MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256
  515. * MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256
  516. * MBEDTLS_TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA
  517. * MBEDTLS_TLS_RSA_PSK_WITH_RC4_128_SHA
  518. */
  519. #define MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
  520. /**
  521. * \def MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
  522. *
  523. * Enable the RSA-only based ciphersuite modes in SSL / TLS.
  524. *
  525. * Requires: MBEDTLS_RSA_C, MBEDTLS_PKCS1_V15,
  526. * MBEDTLS_X509_CRT_PARSE_C
  527. *
  528. * This enables the following ciphersuites (if other requisites are
  529. * enabled as well):
  530. * MBEDTLS_TLS_RSA_WITH_AES_256_GCM_SHA384
  531. * MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA256
  532. * MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA
  533. * MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384
  534. * MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256
  535. * MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA
  536. * MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256
  537. * MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256
  538. * MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA
  539. * MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256
  540. * MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256
  541. * MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA
  542. * MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA
  543. * MBEDTLS_TLS_RSA_WITH_RC4_128_SHA
  544. * MBEDTLS_TLS_RSA_WITH_RC4_128_MD5
  545. */
  546. #define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
  547. /**
  548. * \def MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
  549. *
  550. * Enable the DHE-RSA based ciphersuite modes in SSL / TLS.
  551. *
  552. * Requires: MBEDTLS_DHM_C, MBEDTLS_RSA_C, MBEDTLS_PKCS1_V15,
  553. * MBEDTLS_X509_CRT_PARSE_C
  554. *
  555. * This enables the following ciphersuites (if other requisites are
  556. * enabled as well):
  557. * MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
  558. * MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
  559. * MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA
  560. * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384
  561. * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256
  562. * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA
  563. * MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
  564. * MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
  565. * MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  566. * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256
  567. * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256
  568. * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA
  569. * MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA
  570. */
  571. #define MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
  572. /**
  573. * \def MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
  574. *
  575. * Enable the ECDHE-RSA based ciphersuite modes in SSL / TLS.
  576. *
  577. * Requires: MBEDTLS_ECDH_C, MBEDTLS_RSA_C, MBEDTLS_PKCS1_V15,
  578. * MBEDTLS_X509_CRT_PARSE_C
  579. *
  580. * This enables the following ciphersuites (if other requisites are
  581. * enabled as well):
  582. * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
  583. * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
  584. * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
  585. * MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384
  586. * MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384
  587. * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  588. * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
  589. * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
  590. * MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256
  591. * MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256
  592. * MBEDTLS_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
  593. * MBEDTLS_TLS_ECDHE_RSA_WITH_RC4_128_SHA
  594. */
  595. #define MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
  596. /**
  597. * \def MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
  598. *
  599. * Enable the ECDHE-ECDSA based ciphersuite modes in SSL / TLS.
  600. *
  601. * Requires: MBEDTLS_ECDH_C, MBEDTLS_ECDSA_C, MBEDTLS_X509_CRT_PARSE_C,
  602. *
  603. * This enables the following ciphersuites (if other requisites are
  604. * enabled as well):
  605. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
  606. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
  607. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
  608. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384
  609. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384
  610. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
  611. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
  612. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
  613. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256
  614. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256
  615. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
  616. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
  617. */
  618. #define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
  619. /**
  620. * \def MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED
  621. *
  622. * Enable the ECDH-ECDSA based ciphersuite modes in SSL / TLS.
  623. *
  624. * Requires: MBEDTLS_ECDH_C, MBEDTLS_X509_CRT_PARSE_C
  625. *
  626. * This enables the following ciphersuites (if other requisites are
  627. * enabled as well):
  628. * MBEDTLS_TLS_ECDH_ECDSA_WITH_RC4_128_SHA
  629. * MBEDTLS_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
  630. * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
  631. * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
  632. * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
  633. * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
  634. * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
  635. * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
  636. * MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256
  637. * MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384
  638. * MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256
  639. * MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384
  640. */
  641. #define MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED
  642. /**
  643. * \def MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
  644. *
  645. * Enable the ECDH-RSA based ciphersuite modes in SSL / TLS.
  646. *
  647. * Requires: MBEDTLS_ECDH_C, MBEDTLS_X509_CRT_PARSE_C
  648. *
  649. * This enables the following ciphersuites (if other requisites are
  650. * enabled as well):
  651. * MBEDTLS_TLS_ECDH_RSA_WITH_RC4_128_SHA
  652. * MBEDTLS_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
  653. * MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
  654. * MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
  655. * MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
  656. * MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
  657. * MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
  658. * MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
  659. * MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256
  660. * MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384
  661. * MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256
  662. * MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384
  663. */
  664. #define MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
  665. /**
  666. * \def MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
  667. *
  668. * Enable the ECJPAKE based ciphersuite modes in SSL / TLS.
  669. *
  670. * \warning This is currently experimental. EC J-PAKE support is based on the
  671. * Thread v1.0.0 specification; incompatible changes to the specification
  672. * might still happen. For this reason, this is disabled by default.
  673. *
  674. * Requires: MBEDTLS_ECJPAKE_C
  675. * MBEDTLS_SHA256_C
  676. * MBEDTLS_ECP_DP_SECP256R1_ENABLED
  677. *
  678. * This enables the following ciphersuites (if other requisites are
  679. * enabled as well):
  680. * MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8
  681. */
  682. //#define MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
  683. /**
  684. * \def MBEDTLS_PK_PARSE_EC_EXTENDED
  685. *
  686. * Enhance support for reading EC keys using variants of SEC1 not allowed by
  687. * RFC 5915 and RFC 5480.
  688. *
  689. * Currently this means parsing the SpecifiedECDomain choice of EC
  690. * parameters (only known groups are supported, not arbitrary domains, to
  691. * avoid validation issues).
  692. *
  693. * Disable if you only need to support RFC 5915 + 5480 key formats.
  694. */
  695. #define MBEDTLS_PK_PARSE_EC_EXTENDED
  696. /**
  697. * \def MBEDTLS_ERROR_STRERROR_DUMMY
  698. *
  699. * Enable a dummy error function to make use of mbedtls_strerror() in
  700. * third party libraries easier when MBEDTLS_ERROR_C is disabled
  701. * (no effect when MBEDTLS_ERROR_C is enabled).
  702. *
  703. * You can safely disable this if MBEDTLS_ERROR_C is enabled, or if you're
  704. * not using mbedtls_strerror() or error_strerror() in your application.
  705. *
  706. * Disable if you run into name conflicts and want to really remove the
  707. * mbedtls_strerror()
  708. */
  709. #define MBEDTLS_ERROR_STRERROR_DUMMY
  710. /**
  711. * \def MBEDTLS_GENPRIME
  712. *
  713. * Enable the prime-number generation code.
  714. *
  715. * Requires: MBEDTLS_BIGNUM_C
  716. */
  717. #define MBEDTLS_GENPRIME
  718. /**
  719. * \def MBEDTLS_FS_IO
  720. *
  721. * Enable functions that use the filesystem.
  722. */
  723. #define MBEDTLS_FS_IO
  724. /**
  725. * \def MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
  726. *
  727. * Do not add default entropy sources. These are the platform specific,
  728. * mbedtls_timing_hardclock and HAVEGE based poll functions.
  729. *
  730. * This is useful to have more control over the added entropy sources in an
  731. * application.
  732. *
  733. * Uncomment this macro to prevent loading of default entropy functions.
  734. */
  735. //#define MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
  736. /**
  737. * \def MBEDTLS_NO_PLATFORM_ENTROPY
  738. *
  739. * Do not use built-in platform entropy functions.
  740. * This is useful if your platform does not support
  741. * standards like the /dev/urandom or Windows CryptoAPI.
  742. *
  743. * Uncomment this macro to disable the built-in platform entropy functions.
  744. */
  745. //#define MBEDTLS_NO_PLATFORM_ENTROPY
  746. /**
  747. * \def MBEDTLS_ENTROPY_FORCE_SHA256
  748. *
  749. * Force the entropy accumulator to use a SHA-256 accumulator instead of the
  750. * default SHA-512 based one (if both are available).
  751. *
  752. * Requires: MBEDTLS_SHA256_C
  753. *
  754. * On 32-bit systems SHA-256 can be much faster than SHA-512. Use this option
  755. * if you have performance concerns.
  756. *
  757. * This option is only useful if both MBEDTLS_SHA256_C and
  758. * MBEDTLS_SHA512_C are defined. Otherwise the available hash module is used.
  759. */
  760. //#define MBEDTLS_ENTROPY_FORCE_SHA256
  761. /**
  762. * \def MBEDTLS_MEMORY_DEBUG
  763. *
  764. * Enable debugging of buffer allocator memory issues. Automatically prints
  765. * (to stderr) all (fatal) messages on memory allocation issues. Enables
  766. * function for 'debug output' of allocated memory.
  767. *
  768. * Requires: MBEDTLS_MEMORY_BUFFER_ALLOC_C
  769. *
  770. * Uncomment this macro to let the buffer allocator print out error messages.
  771. */
  772. //#define MBEDTLS_MEMORY_DEBUG
  773. /**
  774. * \def MBEDTLS_MEMORY_BACKTRACE
  775. *
  776. * Include backtrace information with each allocated block.
  777. *
  778. * Requires: MBEDTLS_MEMORY_BUFFER_ALLOC_C
  779. * GLIBC-compatible backtrace() an backtrace_symbols() support
  780. *
  781. * Uncomment this macro to include backtrace information
  782. */
  783. //#define MBEDTLS_MEMORY_BACKTRACE
  784. /**
  785. * \def MBEDTLS_PK_RSA_ALT_SUPPORT
  786. *
  787. * Support external private RSA keys (eg from a HSM) in the PK layer.
  788. *
  789. * Comment this macro to disable support for external private RSA keys.
  790. */
  791. #define MBEDTLS_PK_RSA_ALT_SUPPORT
  792. /**
  793. * \def MBEDTLS_PKCS1_V15
  794. *
  795. * Enable support for PKCS#1 v1.5 encoding.
  796. *
  797. * Requires: MBEDTLS_RSA_C
  798. *
  799. * This enables support for PKCS#1 v1.5 operations.
  800. */
  801. #define MBEDTLS_PKCS1_V15
  802. /**
  803. * \def MBEDTLS_PKCS1_V21
  804. *
  805. * Enable support for PKCS#1 v2.1 encoding.
  806. *
  807. * Requires: MBEDTLS_MD_C, MBEDTLS_RSA_C
  808. *
  809. * This enables support for RSAES-OAEP and RSASSA-PSS operations.
  810. */
  811. #define MBEDTLS_PKCS1_V21
  812. /**
  813. * \def MBEDTLS_RSA_NO_CRT
  814. *
  815. * Do not use the Chinese Remainder Theorem for the RSA private operation.
  816. *
  817. * Uncomment this macro to disable the use of CRT in RSA.
  818. *
  819. */
  820. //#define MBEDTLS_RSA_NO_CRT
  821. /**
  822. * \def MBEDTLS_SELF_TEST
  823. *
  824. * Enable the checkup functions (*_self_test).
  825. */
  826. #define MBEDTLS_SELF_TEST
  827. /**
  828. * \def MBEDTLS_SHA256_SMALLER
  829. *
  830. * Enable an implementation of SHA-256 that has lower ROM footprint but also
  831. * lower performance.
  832. *
  833. * The default implementation is meant to be a reasonnable compromise between
  834. * performance and size. This version optimizes more aggressively for size at
  835. * the expense of performance. Eg on Cortex-M4 it reduces the size of
  836. * mbedtls_sha256_process() from ~2KB to ~0.5KB for a performance hit of about
  837. * 30%.
  838. *
  839. * Uncomment to enable the smaller implementation of SHA256.
  840. */
  841. //#define MBEDTLS_SHA256_SMALLER
  842. /**
  843. * \def MBEDTLS_SSL_AEAD_RANDOM_IV
  844. *
  845. * Generate a random IV rather than using the record sequence number as a
  846. * nonce for ciphersuites using and AEAD algorithm (GCM or CCM).
  847. *
  848. * Using the sequence number is generally recommended.
  849. *
  850. * Uncomment this macro to always use random IVs with AEAD ciphersuites.
  851. */
  852. //#define MBEDTLS_SSL_AEAD_RANDOM_IV
  853. /**
  854. * \def MBEDTLS_SSL_ALL_ALERT_MESSAGES
  855. *
  856. * Enable sending of alert messages in case of encountered errors as per RFC.
  857. * If you choose not to send the alert messages, mbed TLS can still communicate
  858. * with other servers, only debugging of failures is harder.
  859. *
  860. * The advantage of not sending alert messages, is that no information is given
  861. * about reasons for failures thus preventing adversaries of gaining intel.
  862. *
  863. * Enable sending of all alert messages
  864. */
  865. #define MBEDTLS_SSL_ALL_ALERT_MESSAGES
  866. /**
  867. * \def MBEDTLS_SSL_DEBUG_ALL
  868. *
  869. * Enable the debug messages in SSL module for all issues.
  870. * Debug messages have been disabled in some places to prevent timing
  871. * attacks due to (unbalanced) debugging function calls.
  872. *
  873. * If you need all error reporting you should enable this during debugging,
  874. * but remove this for production servers that should log as well.
  875. *
  876. * Uncomment this macro to report all debug messages on errors introducing
  877. * a timing side-channel.
  878. *
  879. */
  880. //#define MBEDTLS_SSL_DEBUG_ALL
  881. /** \def MBEDTLS_SSL_ENCRYPT_THEN_MAC
  882. *
  883. * Enable support for Encrypt-then-MAC, RFC 7366.
  884. *
  885. * This allows peers that both support it to use a more robust protection for
  886. * ciphersuites using CBC, providing deep resistance against timing attacks
  887. * on the padding or underlying cipher.
  888. *
  889. * This only affects CBC ciphersuites, and is useless if none is defined.
  890. *
  891. * Requires: MBEDTLS_SSL_PROTO_TLS1 or
  892. * MBEDTLS_SSL_PROTO_TLS1_1 or
  893. * MBEDTLS_SSL_PROTO_TLS1_2
  894. *
  895. * Comment this macro to disable support for Encrypt-then-MAC
  896. */
  897. #define MBEDTLS_SSL_ENCRYPT_THEN_MAC
  898. /** \def MBEDTLS_SSL_EXTENDED_MASTER_SECRET
  899. *
  900. * Enable support for Extended Master Secret, aka Session Hash
  901. * (draft-ietf-tls-session-hash-02).
  902. *
  903. * This was introduced as "the proper fix" to the Triple Handshake familiy of
  904. * attacks, but it is recommended to always use it (even if you disable
  905. * renegotiation), since it actually fixes a more fundamental issue in the
  906. * original SSL/TLS design, and has implications beyond Triple Handshake.
  907. *
  908. * Requires: MBEDTLS_SSL_PROTO_TLS1 or
  909. * MBEDTLS_SSL_PROTO_TLS1_1 or
  910. * MBEDTLS_SSL_PROTO_TLS1_2
  911. *
  912. * Comment this macro to disable support for Extended Master Secret.
  913. */
  914. #define MBEDTLS_SSL_EXTENDED_MASTER_SECRET
  915. /**
  916. * \def MBEDTLS_SSL_FALLBACK_SCSV
  917. *
  918. * Enable support for FALLBACK_SCSV (draft-ietf-tls-downgrade-scsv-00).
  919. *
  920. * For servers, it is recommended to always enable this, unless you support
  921. * only one version of TLS, or know for sure that none of your clients
  922. * implements a fallback strategy.
  923. *
  924. * For clients, you only need this if you're using a fallback strategy, which
  925. * is not recommended in the first place, unless you absolutely need it to
  926. * interoperate with buggy (version-intolerant) servers.
  927. *
  928. * Comment this macro to disable support for FALLBACK_SCSV
  929. */
  930. #define MBEDTLS_SSL_FALLBACK_SCSV
  931. /**
  932. * \def MBEDTLS_SSL_HW_RECORD_ACCEL
  933. *
  934. * Enable hooking functions in SSL module for hardware acceleration of
  935. * individual records.
  936. *
  937. * Uncomment this macro to enable hooking functions.
  938. */
  939. //#define MBEDTLS_SSL_HW_RECORD_ACCEL
  940. /**
  941. * \def MBEDTLS_SSL_CBC_RECORD_SPLITTING
  942. *
  943. * Enable 1/n-1 record splitting for CBC mode in SSLv3 and TLS 1.0.
  944. *
  945. * This is a countermeasure to the BEAST attack, which also minimizes the risk
  946. * of interoperability issues compared to sending 0-length records.
  947. *
  948. * Comment this macro to disable 1/n-1 record splitting.
  949. */
  950. #define MBEDTLS_SSL_CBC_RECORD_SPLITTING
  951. /**
  952. * \def MBEDTLS_SSL_RENEGOTIATION
  953. *
  954. * Disable support for TLS renegotiation.
  955. *
  956. * The two main uses of renegotiation are (1) refresh keys on long-lived
  957. * connections and (2) client authentication after the initial handshake.
  958. * If you don't need renegotiation, it's probably better to disable it, since
  959. * it has been associated with security issues in the past and is easy to
  960. * misuse/misunderstand.
  961. *
  962. * Comment this to disable support for renegotiation.
  963. */
  964. #define MBEDTLS_SSL_RENEGOTIATION
  965. /**
  966. * \def MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO
  967. *
  968. * Enable support for receiving and parsing SSLv2 Client Hello messages for the
  969. * SSL Server module (MBEDTLS_SSL_SRV_C).
  970. *
  971. * Uncomment this macro to enable support for SSLv2 Client Hello messages.
  972. */
  973. //#define MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO
  974. /**
  975. * \def MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE
  976. *
  977. * Pick the ciphersuite according to the client's preferences rather than ours
  978. * in the SSL Server module (MBEDTLS_SSL_SRV_C).
  979. *
  980. * Uncomment this macro to respect client's ciphersuite order
  981. */
  982. //#define MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE
  983. /**
  984. * \def MBEDTLS_SSL_MAX_FRAGMENT_LENGTH
  985. *
  986. * Enable support for RFC 6066 max_fragment_length extension in SSL.
  987. *
  988. * Comment this macro to disable support for the max_fragment_length extension
  989. */
  990. #define MBEDTLS_SSL_MAX_FRAGMENT_LENGTH
  991. /**
  992. * \def MBEDTLS_SSL_PROTO_SSL3
  993. *
  994. * Enable support for SSL 3.0.
  995. *
  996. * Requires: MBEDTLS_MD5_C
  997. * MBEDTLS_SHA1_C
  998. *
  999. * Comment this macro to disable support for SSL 3.0
  1000. */
  1001. #define MBEDTLS_SSL_PROTO_SSL3
  1002. /**
  1003. * \def MBEDTLS_SSL_PROTO_TLS1
  1004. *
  1005. * Enable support for TLS 1.0.
  1006. *
  1007. * Requires: MBEDTLS_MD5_C
  1008. * MBEDTLS_SHA1_C
  1009. *
  1010. * Comment this macro to disable support for TLS 1.0
  1011. */
  1012. #define MBEDTLS_SSL_PROTO_TLS1
  1013. /**
  1014. * \def MBEDTLS_SSL_PROTO_TLS1_1
  1015. *
  1016. * Enable support for TLS 1.1 (and DTLS 1.0 if DTLS is enabled).
  1017. *
  1018. * Requires: MBEDTLS_MD5_C
  1019. * MBEDTLS_SHA1_C
  1020. *
  1021. * Comment this macro to disable support for TLS 1.1 / DTLS 1.0
  1022. */
  1023. #define MBEDTLS_SSL_PROTO_TLS1_1
  1024. /**
  1025. * \def MBEDTLS_SSL_PROTO_TLS1_2
  1026. *
  1027. * Enable support for TLS 1.2 (and DTLS 1.2 if DTLS is enabled).
  1028. *
  1029. * Requires: MBEDTLS_SHA1_C or MBEDTLS_SHA256_C or MBEDTLS_SHA512_C
  1030. * (Depends on ciphersuites)
  1031. *
  1032. * Comment this macro to disable support for TLS 1.2 / DTLS 1.2
  1033. */
  1034. #define MBEDTLS_SSL_PROTO_TLS1_2
  1035. /**
  1036. * \def MBEDTLS_SSL_PROTO_DTLS
  1037. *
  1038. * Enable support for DTLS (all available versions).
  1039. *
  1040. * Enable this and MBEDTLS_SSL_PROTO_TLS1_1 to enable DTLS 1.0,
  1041. * and/or this and MBEDTLS_SSL_PROTO_TLS1_2 to enable DTLS 1.2.
  1042. *
  1043. * Requires: MBEDTLS_SSL_PROTO_TLS1_1
  1044. * or MBEDTLS_SSL_PROTO_TLS1_2
  1045. *
  1046. * Comment this macro to disable support for DTLS
  1047. */
  1048. #define MBEDTLS_SSL_PROTO_DTLS
  1049. /**
  1050. * \def MBEDTLS_SSL_ALPN
  1051. *
  1052. * Enable support for RFC 7301 Application Layer Protocol Negotiation.
  1053. *
  1054. * Comment this macro to disable support for ALPN.
  1055. */
  1056. #define MBEDTLS_SSL_ALPN
  1057. /**
  1058. * \def MBEDTLS_SSL_DTLS_ANTI_REPLAY
  1059. *
  1060. * Enable support for the anti-replay mechanism in DTLS.
  1061. *
  1062. * Requires: MBEDTLS_SSL_TLS_C
  1063. * MBEDTLS_SSL_PROTO_DTLS
  1064. *
  1065. * \warning Disabling this is often a security risk!
  1066. * See mbedtls_ssl_conf_dtls_anti_replay() for details.
  1067. *
  1068. * Comment this to disable anti-replay in DTLS.
  1069. */
  1070. #define MBEDTLS_SSL_DTLS_ANTI_REPLAY
  1071. /**
  1072. * \def MBEDTLS_SSL_DTLS_HELLO_VERIFY
  1073. *
  1074. * Enable support for HelloVerifyRequest on DTLS servers.
  1075. *
  1076. * This feature is highly recommended to prevent DTLS servers being used as
  1077. * amplifiers in DoS attacks against other hosts. It should always be enabled
  1078. * unless you know for sure amplification cannot be a problem in the
  1079. * environment in which your server operates.
  1080. *
  1081. * \warning Disabling this can ba a security risk! (see above)
  1082. *
  1083. * Requires: MBEDTLS_SSL_PROTO_DTLS
  1084. *
  1085. * Comment this to disable support for HelloVerifyRequest.
  1086. */
  1087. #define MBEDTLS_SSL_DTLS_HELLO_VERIFY
  1088. /**
  1089. * \def MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE
  1090. *
  1091. * Enable server-side support for clients that reconnect from the same port.
  1092. *
  1093. * Some clients unexpectedly close the connection and try to reconnect using the
  1094. * same source port. This needs special support from the server to handle the
  1095. * new connection securely, as described in section 4.2.8 of RFC 6347. This
  1096. * flag enables that support.
  1097. *
  1098. * Requires: MBEDTLS_SSL_DTLS_HELLO_VERIFY
  1099. *
  1100. * Comment this to disable support for clients reusing the source port.
  1101. */
  1102. #define MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE
  1103. /**
  1104. * \def MBEDTLS_SSL_DTLS_BADMAC_LIMIT
  1105. *
  1106. * Enable support for a limit of records with bad MAC.
  1107. *
  1108. * See mbedtls_ssl_conf_dtls_badmac_limit().
  1109. *
  1110. * Requires: MBEDTLS_SSL_PROTO_DTLS
  1111. */
  1112. #define MBEDTLS_SSL_DTLS_BADMAC_LIMIT
  1113. /**
  1114. * \def MBEDTLS_SSL_SESSION_TICKETS
  1115. *
  1116. * Enable support for RFC 5077 session tickets in SSL.
  1117. * Client-side, provides full support for session tickets (maintainance of a
  1118. * session store remains the responsibility of the application, though).
  1119. * Server-side, you also need to provide callbacks for writing and parsing
  1120. * tickets, including authenticated encryption and key management. Example
  1121. * callbacks are provided by MBEDTLS_SSL_TICKET_C.
  1122. *
  1123. * Comment this macro to disable support for SSL session tickets
  1124. */
  1125. #define MBEDTLS_SSL_SESSION_TICKETS
  1126. /**
  1127. * \def MBEDTLS_SSL_EXPORT_KEYS
  1128. *
  1129. * Enable support for exporting key block and master secret.
  1130. * This is required for certain users of TLS, e.g. EAP-TLS.
  1131. *
  1132. * Comment this macro to disable support for key export
  1133. */
  1134. #define MBEDTLS_SSL_EXPORT_KEYS
  1135. /**
  1136. * \def MBEDTLS_SSL_SERVER_NAME_INDICATION
  1137. *
  1138. * Enable support for RFC 6066 server name indication (SNI) in SSL.
  1139. *
  1140. * Requires: MBEDTLS_X509_CRT_PARSE_C
  1141. *
  1142. * Comment this macro to disable support for server name indication in SSL
  1143. */
  1144. #define MBEDTLS_SSL_SERVER_NAME_INDICATION
  1145. /**
  1146. * \def MBEDTLS_SSL_TRUNCATED_HMAC
  1147. *
  1148. * Enable support for RFC 6066 truncated HMAC in SSL.
  1149. *
  1150. * Comment this macro to disable support for truncated HMAC in SSL
  1151. */
  1152. #define MBEDTLS_SSL_TRUNCATED_HMAC
  1153. /**
  1154. * \def MBEDTLS_THREADING_ALT
  1155. *
  1156. * Provide your own alternate threading implementation.
  1157. *
  1158. * Requires: MBEDTLS_THREADING_C
  1159. *
  1160. * Uncomment this to allow your own alternate threading implementation.
  1161. */
  1162. //#define MBEDTLS_THREADING_ALT
  1163. /**
  1164. * \def MBEDTLS_THREADING_PTHREAD
  1165. *
  1166. * Enable the pthread wrapper layer for the threading layer.
  1167. *
  1168. * Requires: MBEDTLS_THREADING_C
  1169. *
  1170. * Uncomment this to enable pthread mutexes.
  1171. */
  1172. //#define MBEDTLS_THREADING_PTHREAD
  1173. /**
  1174. * \def MBEDTLS_VERSION_FEATURES
  1175. *
  1176. * Allow run-time checking of compile-time enabled features. Thus allowing users
  1177. * to check at run-time if the library is for instance compiled with threading
  1178. * support via mbedtls_version_check_feature().
  1179. *
  1180. * Requires: MBEDTLS_VERSION_C
  1181. *
  1182. * Comment this to disable run-time checking and save ROM space
  1183. */
  1184. #define MBEDTLS_VERSION_FEATURES
  1185. /**
  1186. * \def MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3
  1187. *
  1188. * If set, the X509 parser will not break-off when parsing an X509 certificate
  1189. * and encountering an extension in a v1 or v2 certificate.
  1190. *
  1191. * Uncomment to prevent an error.
  1192. */
  1193. //#define MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3
  1194. /**
  1195. * \def MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
  1196. *
  1197. * If set, the X509 parser will not break-off when parsing an X509 certificate
  1198. * and encountering an unknown critical extension.
  1199. *
  1200. * \warning Depending on your PKI use, enabling this can be a security risk!
  1201. *
  1202. * Uncomment to prevent an error.
  1203. */
  1204. //#define MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
  1205. /**
  1206. * \def MBEDTLS_X509_CHECK_KEY_USAGE
  1207. *
  1208. * Enable verification of the keyUsage extension (CA and leaf certificates).
  1209. *
  1210. * Disabling this avoids problems with mis-issued and/or misused
  1211. * (intermediate) CA and leaf certificates.
  1212. *
  1213. * \warning Depending on your PKI use, disabling this can be a security risk!
  1214. *
  1215. * Comment to skip keyUsage checking for both CA and leaf certificates.
  1216. */
  1217. #define MBEDTLS_X509_CHECK_KEY_USAGE
  1218. /**
  1219. * \def MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE
  1220. *
  1221. * Enable verification of the extendedKeyUsage extension (leaf certificates).
  1222. *
  1223. * Disabling this avoids problems with mis-issued and/or misused certificates.
  1224. *
  1225. * \warning Depending on your PKI use, disabling this can be a security risk!
  1226. *
  1227. * Comment to skip extendedKeyUsage checking for certificates.
  1228. */
  1229. #define MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE
  1230. /**
  1231. * \def MBEDTLS_X509_RSASSA_PSS_SUPPORT
  1232. *
  1233. * Enable parsing and verification of X.509 certificates, CRLs and CSRS
  1234. * signed with RSASSA-PSS (aka PKCS#1 v2.1).
  1235. *
  1236. * Comment this macro to disallow using RSASSA-PSS in certificates.
  1237. */
  1238. #define MBEDTLS_X509_RSASSA_PSS_SUPPORT
  1239. /**
  1240. * \def MBEDTLS_ZLIB_SUPPORT
  1241. *
  1242. * If set, the SSL/TLS module uses ZLIB to support compression and
  1243. * decompression of packet data.
  1244. *
  1245. * \warning TLS-level compression MAY REDUCE SECURITY! See for example the
  1246. * CRIME attack. Before enabling this option, you should examine with care if
  1247. * CRIME or similar exploits may be a applicable to your use case.
  1248. *
  1249. * \note Currently compression can't be used with DTLS.
  1250. *
  1251. * Used in: library/ssl_tls.c
  1252. * library/ssl_cli.c
  1253. * library/ssl_srv.c
  1254. *
  1255. * This feature requires zlib library and headers to be present.
  1256. *
  1257. * Uncomment to enable use of ZLIB
  1258. */
  1259. //#define MBEDTLS_ZLIB_SUPPORT
  1260. /* \} name SECTION: mbed TLS feature support */
  1261. /**
  1262. * \name SECTION: mbed TLS modules
  1263. *
  1264. * This section enables or disables entire modules in mbed TLS
  1265. * \{
  1266. */
  1267. /**
  1268. * \def MBEDTLS_AESNI_C
  1269. *
  1270. * Enable AES-NI support on x86-64.
  1271. *
  1272. * Module: library/aesni.c
  1273. * Caller: library/aes.c
  1274. *
  1275. * Requires: MBEDTLS_HAVE_ASM
  1276. *
  1277. * This modules adds support for the AES-NI instructions on x86-64
  1278. */
  1279. #define MBEDTLS_AESNI_C
  1280. /**
  1281. * \def MBEDTLS_AES_C
  1282. *
  1283. * Enable the AES block cipher.
  1284. *
  1285. * Module: library/aes.c
  1286. * Caller: library/ssl_tls.c
  1287. * library/pem.c
  1288. * library/ctr_drbg.c
  1289. *
  1290. * This module enables the following ciphersuites (if other requisites are
  1291. * enabled as well):
  1292. * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
  1293. * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
  1294. * MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
  1295. * MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
  1296. * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
  1297. * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
  1298. * MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
  1299. * MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
  1300. * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
  1301. * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
  1302. * MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
  1303. * MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
  1304. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
  1305. * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
  1306. * MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
  1307. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
  1308. * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
  1309. * MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
  1310. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
  1311. * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
  1312. * MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA
  1313. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
  1314. * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  1315. * MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
  1316. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
  1317. * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
  1318. * MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
  1319. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
  1320. * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
  1321. * MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  1322. * MBEDTLS_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384
  1323. * MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384
  1324. * MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384
  1325. * MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA
  1326. * MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA
  1327. * MBEDTLS_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256
  1328. * MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256
  1329. * MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256
  1330. * MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA
  1331. * MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA
  1332. * MBEDTLS_TLS_RSA_WITH_AES_256_GCM_SHA384
  1333. * MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA256
  1334. * MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA
  1335. * MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256
  1336. * MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256
  1337. * MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA
  1338. * MBEDTLS_TLS_RSA_PSK_WITH_AES_256_GCM_SHA384
  1339. * MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA384
  1340. * MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA
  1341. * MBEDTLS_TLS_RSA_PSK_WITH_AES_128_GCM_SHA256
  1342. * MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA256
  1343. * MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA
  1344. * MBEDTLS_TLS_PSK_WITH_AES_256_GCM_SHA384
  1345. * MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA384
  1346. * MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA
  1347. * MBEDTLS_TLS_PSK_WITH_AES_128_GCM_SHA256
  1348. * MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA256
  1349. * MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA
  1350. *
  1351. * PEM_PARSE uses AES for decrypting encrypted keys.
  1352. */
  1353. #define MBEDTLS_AES_C
  1354. /**
  1355. * \def MBEDTLS_ARC4_C
  1356. *
  1357. * Enable the ARCFOUR stream cipher.
  1358. *
  1359. * Module: library/arc4.c
  1360. * Caller: library/ssl_tls.c
  1361. *
  1362. * This module enables the following ciphersuites (if other requisites are
  1363. * enabled as well):
  1364. * MBEDTLS_TLS_ECDH_ECDSA_WITH_RC4_128_SHA
  1365. * MBEDTLS_TLS_ECDH_RSA_WITH_RC4_128_SHA
  1366. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
  1367. * MBEDTLS_TLS_ECDHE_RSA_WITH_RC4_128_SHA
  1368. * MBEDTLS_TLS_ECDHE_PSK_WITH_RC4_128_SHA
  1369. * MBEDTLS_TLS_DHE_PSK_WITH_RC4_128_SHA
  1370. * MBEDTLS_TLS_RSA_WITH_RC4_128_SHA
  1371. * MBEDTLS_TLS_RSA_WITH_RC4_128_MD5
  1372. * MBEDTLS_TLS_RSA_PSK_WITH_RC4_128_SHA
  1373. * MBEDTLS_TLS_PSK_WITH_RC4_128_SHA
  1374. */
  1375. #define MBEDTLS_ARC4_C
  1376. /**
  1377. * \def MBEDTLS_ASN1_PARSE_C
  1378. *
  1379. * Enable the generic ASN1 parser.
  1380. *
  1381. * Module: library/asn1.c
  1382. * Caller: library/x509.c
  1383. * library/dhm.c
  1384. * library/pkcs12.c
  1385. * library/pkcs5.c
  1386. * library/pkparse.c
  1387. */
  1388. #define MBEDTLS_ASN1_PARSE_C
  1389. /**
  1390. * \def MBEDTLS_ASN1_WRITE_C
  1391. *
  1392. * Enable the generic ASN1 writer.
  1393. *
  1394. * Module: library/asn1write.c
  1395. * Caller: library/ecdsa.c
  1396. * library/pkwrite.c
  1397. * library/x509_create.c
  1398. * library/x509write_crt.c
  1399. * library/mbedtls_x509write_csr.c
  1400. */
  1401. #define MBEDTLS_ASN1_WRITE_C
  1402. /**
  1403. * \def MBEDTLS_BASE64_C
  1404. *
  1405. * Enable the Base64 module.
  1406. *
  1407. * Module: library/base64.c
  1408. * Caller: library/pem.c
  1409. *
  1410. * This module is required for PEM support (required by X.509).
  1411. */
  1412. #define MBEDTLS_BASE64_C
  1413. /**
  1414. * \def MBEDTLS_BIGNUM_C
  1415. *
  1416. * Enable the multi-precision integer library.
  1417. *
  1418. * Module: library/bignum.c
  1419. * Caller: library/dhm.c
  1420. * library/ecp.c
  1421. * library/ecdsa.c
  1422. * library/rsa.c
  1423. * library/ssl_tls.c
  1424. *
  1425. * This module is required for RSA, DHM and ECC (ECDH, ECDSA) support.
  1426. */
  1427. #define MBEDTLS_BIGNUM_C
  1428. /**
  1429. * \def MBEDTLS_BLOWFISH_C
  1430. *
  1431. * Enable the Blowfish block cipher.
  1432. *
  1433. * Module: library/blowfish.c
  1434. */
  1435. #define MBEDTLS_BLOWFISH_C
  1436. /**
  1437. * \def MBEDTLS_CAMELLIA_C
  1438. *
  1439. * Enable the Camellia block cipher.
  1440. *
  1441. * Module: library/camellia.c
  1442. * Caller: library/ssl_tls.c
  1443. *
  1444. * This module enables the following ciphersuites (if other requisites are
  1445. * enabled as well):
  1446. * MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256
  1447. * MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384
  1448. * MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256
  1449. * MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384
  1450. * MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256
  1451. * MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384
  1452. * MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256
  1453. * MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384
  1454. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384
  1455. * MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384
  1456. * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384
  1457. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384
  1458. * MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384
  1459. * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256
  1460. * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA
  1461. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256
  1462. * MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256
  1463. * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256
  1464. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256
  1465. * MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256
  1466. * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256
  1467. * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA
  1468. * MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384
  1469. * MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384
  1470. * MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384
  1471. * MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256
  1472. * MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256
  1473. * MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256
  1474. * MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384
  1475. * MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256
  1476. * MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA
  1477. * MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256
  1478. * MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256
  1479. * MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA
  1480. * MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384
  1481. * MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384
  1482. * MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256
  1483. * MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256
  1484. * MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384
  1485. * MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384
  1486. * MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256
  1487. * MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256
  1488. */
  1489. #define MBEDTLS_CAMELLIA_C
  1490. /**
  1491. * \def MBEDTLS_CCM_C
  1492. *
  1493. * Enable the Counter with CBC-MAC (CCM) mode for 128-bit block cipher.
  1494. *
  1495. * Module: library/ccm.c
  1496. *
  1497. * Requires: MBEDTLS_AES_C or MBEDTLS_CAMELLIA_C
  1498. *
  1499. * This module enables the AES-CCM ciphersuites, if other requisites are
  1500. * enabled as well.
  1501. */
  1502. #define MBEDTLS_CCM_C
  1503. /**
  1504. * \def MBEDTLS_CERTS_C
  1505. *
  1506. * Enable the test certificates.
  1507. *
  1508. * Module: library/certs.c
  1509. * Caller:
  1510. *
  1511. * This module is used for testing (ssl_client/server).
  1512. */
  1513. #define MBEDTLS_CERTS_C
  1514. /**
  1515. * \def MBEDTLS_CIPHER_C
  1516. *
  1517. * Enable the generic cipher layer.
  1518. *
  1519. * Module: library/cipher.c
  1520. * Caller: library/ssl_tls.c
  1521. *
  1522. * Uncomment to enable generic cipher wrappers.
  1523. */
  1524. #define MBEDTLS_CIPHER_C
  1525. /**
  1526. * \def MBEDTLS_CTR_DRBG_C
  1527. *
  1528. * Enable the CTR_DRBG AES-256-based random generator.
  1529. *
  1530. * Module: library/ctr_drbg.c
  1531. * Caller:
  1532. *
  1533. * Requires: MBEDTLS_AES_C
  1534. *
  1535. * This module provides the CTR_DRBG AES-256 random number generator.
  1536. */
  1537. #define MBEDTLS_CTR_DRBG_C
  1538. /**
  1539. * \def MBEDTLS_DEBUG_C
  1540. *
  1541. * Enable the debug functions.
  1542. *
  1543. * Module: library/debug.c
  1544. * Caller: library/ssl_cli.c
  1545. * library/ssl_srv.c
  1546. * library/ssl_tls.c
  1547. *
  1548. * This module provides debugging functions.
  1549. */
  1550. #define MBEDTLS_DEBUG_C
  1551. /**
  1552. * \def MBEDTLS_DES_C
  1553. *
  1554. * Enable the DES block cipher.
  1555. *
  1556. * Module: library/des.c
  1557. * Caller: library/pem.c
  1558. * library/ssl_tls.c
  1559. *
  1560. * This module enables the following ciphersuites (if other requisites are
  1561. * enabled as well):
  1562. * MBEDTLS_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
  1563. * MBEDTLS_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
  1564. * MBEDTLS_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
  1565. * MBEDTLS_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
  1566. * MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA
  1567. * MBEDTLS_TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA
  1568. * MBEDTLS_TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA
  1569. * MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA
  1570. * MBEDTLS_TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA
  1571. * MBEDTLS_TLS_PSK_WITH_3DES_EDE_CBC_SHA
  1572. *
  1573. * PEM_PARSE uses DES/3DES for decrypting encrypted keys.
  1574. */
  1575. #define MBEDTLS_DES_C
  1576. /**
  1577. * \def MBEDTLS_DHM_C
  1578. *
  1579. * Enable the Diffie-Hellman-Merkle module.
  1580. *
  1581. * Module: library/dhm.c
  1582. * Caller: library/ssl_cli.c
  1583. * library/ssl_srv.c
  1584. *
  1585. * This module is used by the following key exchanges:
  1586. * DHE-RSA, DHE-PSK
  1587. */
  1588. #define MBEDTLS_DHM_C
  1589. /**
  1590. * \def MBEDTLS_ECDH_C
  1591. *
  1592. * Enable the elliptic curve Diffie-Hellman library.
  1593. *
  1594. * Module: library/ecdh.c
  1595. * Caller: library/ssl_cli.c
  1596. * library/ssl_srv.c
  1597. *
  1598. * This module is used by the following key exchanges:
  1599. * ECDHE-ECDSA, ECDHE-RSA, DHE-PSK
  1600. *
  1601. * Requires: MBEDTLS_ECP_C
  1602. */
  1603. #define MBEDTLS_ECDH_C
  1604. /**
  1605. * \def MBEDTLS_ECDSA_C
  1606. *
  1607. * Enable the elliptic curve DSA library.
  1608. *
  1609. * Module: library/ecdsa.c
  1610. * Caller:
  1611. *
  1612. * This module is used by the following key exchanges:
  1613. * ECDHE-ECDSA
  1614. *
  1615. * Requires: MBEDTLS_ECP_C, MBEDTLS_ASN1_WRITE_C, MBEDTLS_ASN1_PARSE_C
  1616. */
  1617. #define MBEDTLS_ECDSA_C
  1618. /**
  1619. * \def MBEDTLS_ECJPAKE_C
  1620. *
  1621. * Enable the elliptic curve J-PAKE library.
  1622. *
  1623. * \warning This is currently experimental. EC J-PAKE support is based on the
  1624. * Thread v1.0.0 specification; incompatible changes to the specification
  1625. * might still happen. For this reason, this is disabled by default.
  1626. *
  1627. * Module: library/ecjpake.c
  1628. * Caller:
  1629. *
  1630. * This module is used by the following key exchanges:
  1631. * ECJPAKE
  1632. *
  1633. * Requires: MBEDTLS_ECP_C, MBEDTLS_MD_C
  1634. */
  1635. //#define MBEDTLS_ECJPAKE_C
  1636. /**
  1637. * \def MBEDTLS_ECP_C
  1638. *
  1639. * Enable the elliptic curve over GF(p) library.
  1640. *
  1641. * Module: library/ecp.c
  1642. * Caller: library/ecdh.c
  1643. * library/ecdsa.c
  1644. * library/ecjpake.c
  1645. *
  1646. * Requires: MBEDTLS_BIGNUM_C and at least one MBEDTLS_ECP_DP_XXX_ENABLED
  1647. */
  1648. #define MBEDTLS_ECP_C
  1649. /**
  1650. * \def MBEDTLS_ENTROPY_C
  1651. *
  1652. * Enable the platform-specific entropy code.
  1653. *
  1654. * Module: library/entropy.c
  1655. * Caller:
  1656. *
  1657. * Requires: MBEDTLS_SHA512_C or MBEDTLS_SHA256_C
  1658. *
  1659. * This module provides a generic entropy pool
  1660. */
  1661. #define MBEDTLS_ENTROPY_C
  1662. /**
  1663. * \def MBEDTLS_ERROR_C
  1664. *
  1665. * Enable error code to error string conversion.
  1666. *
  1667. * Module: library/error.c
  1668. * Caller:
  1669. *
  1670. * This module enables mbedtls_strerror().
  1671. */
  1672. #define MBEDTLS_ERROR_C
  1673. /**
  1674. * \def MBEDTLS_GCM_C
  1675. *
  1676. * Enable the Galois/Counter Mode (GCM) for AES.
  1677. *
  1678. * Module: library/gcm.c
  1679. *
  1680. * Requires: MBEDTLS_AES_C or MBEDTLS_CAMELLIA_C
  1681. *
  1682. * This module enables the AES-GCM and CAMELLIA-GCM ciphersuites, if other
  1683. * requisites are enabled as well.
  1684. */
  1685. #define MBEDTLS_GCM_C
  1686. /**
  1687. * \def MBEDTLS_HAVEGE_C
  1688. *
  1689. * Enable the HAVEGE random generator.
  1690. *
  1691. * Warning: the HAVEGE random generator is not suitable for virtualized
  1692. * environments
  1693. *
  1694. * Warning: the HAVEGE random generator is dependent on timing and specific
  1695. * processor traits. It is therefore not advised to use HAVEGE as
  1696. * your applications primary random generator or primary entropy pool
  1697. * input. As a secondary input to your entropy pool, it IS able add
  1698. * the (limited) extra entropy it provides.
  1699. *
  1700. * Module: library/havege.c
  1701. * Caller:
  1702. *
  1703. * Requires: MBEDTLS_TIMING_C
  1704. *
  1705. * Uncomment to enable the HAVEGE random generator.
  1706. */
  1707. //#define MBEDTLS_HAVEGE_C
  1708. /**
  1709. * \def MBEDTLS_HMAC_DRBG_C
  1710. *
  1711. * Enable the HMAC_DRBG random generator.
  1712. *
  1713. * Module: library/hmac_drbg.c
  1714. * Caller:
  1715. *
  1716. * Requires: MBEDTLS_MD_C
  1717. *
  1718. * Uncomment to enable the HMAC_DRBG random number geerator.
  1719. */
  1720. #define MBEDTLS_HMAC_DRBG_C
  1721. /**
  1722. * \def MBEDTLS_MD_C
  1723. *
  1724. * Enable the generic message digest layer.
  1725. *
  1726. * Module: library/mbedtls_md.c
  1727. * Caller:
  1728. *
  1729. * Uncomment to enable generic message digest wrappers.
  1730. */
  1731. #define MBEDTLS_MD_C
  1732. /**
  1733. * \def MBEDTLS_MD2_C
  1734. *
  1735. * Enable the MD2 hash algorithm.
  1736. *
  1737. * Module: library/mbedtls_md2.c
  1738. * Caller:
  1739. *
  1740. * Uncomment to enable support for (rare) MD2-signed X.509 certs.
  1741. */
  1742. //#define MBEDTLS_MD2_C
  1743. /**
  1744. * \def MBEDTLS_MD4_C
  1745. *
  1746. * Enable the MD4 hash algorithm.
  1747. *
  1748. * Module: library/mbedtls_md4.c
  1749. * Caller:
  1750. *
  1751. * Uncomment to enable support for (rare) MD4-signed X.509 certs.
  1752. */
  1753. //#define MBEDTLS_MD4_C
  1754. /**
  1755. * \def MBEDTLS_MD5_C
  1756. *
  1757. * Enable the MD5 hash algorithm.
  1758. *
  1759. * Module: library/mbedtls_md5.c
  1760. * Caller: library/mbedtls_md.c
  1761. * library/pem.c
  1762. * library/ssl_tls.c
  1763. *
  1764. * This module is required for SSL/TLS and X.509.
  1765. * PEM_PARSE uses MD5 for decrypting encrypted keys.
  1766. */
  1767. #define MBEDTLS_MD5_C
  1768. /**
  1769. * \def MBEDTLS_MEMORY_BUFFER_ALLOC_C
  1770. *
  1771. * Enable the buffer allocator implementation that makes use of a (stack)
  1772. * based buffer to 'allocate' dynamic memory. (replaces calloc() and free()
  1773. * calls)
  1774. *
  1775. * Module: library/memory_buffer_alloc.c
  1776. *
  1777. * Requires: MBEDTLS_PLATFORM_C
  1778. * MBEDTLS_PLATFORM_MEMORY (to use it within mbed TLS)
  1779. *
  1780. * Enable this module to enable the buffer memory allocator.
  1781. */
  1782. //#define MBEDTLS_MEMORY_BUFFER_ALLOC_C
  1783. /**
  1784. * \def MBEDTLS_NET_C
  1785. *
  1786. * Enable the TCP/IP networking routines.
  1787. *
  1788. * Module: library/net.c
  1789. *
  1790. * This module provides TCP/IP networking routines.
  1791. */
  1792. #define MBEDTLS_NET_C
  1793. /**
  1794. * \def MBEDTLS_OID_C
  1795. *
  1796. * Enable the OID database.
  1797. *
  1798. * Module: library/oid.c
  1799. * Caller: library/asn1write.c
  1800. * library/pkcs5.c
  1801. * library/pkparse.c
  1802. * library/pkwrite.c
  1803. * library/rsa.c
  1804. * library/x509.c
  1805. * library/x509_create.c
  1806. * library/mbedtls_x509_crl.c
  1807. * library/mbedtls_x509_crt.c
  1808. * library/mbedtls_x509_csr.c
  1809. * library/x509write_crt.c
  1810. * library/mbedtls_x509write_csr.c
  1811. *
  1812. * This modules translates between OIDs and internal values.
  1813. */
  1814. #define MBEDTLS_OID_C
  1815. /**
  1816. * \def MBEDTLS_PADLOCK_C
  1817. *
  1818. * Enable VIA Padlock support on x86.
  1819. *
  1820. * Module: library/padlock.c
  1821. * Caller: library/aes.c
  1822. *
  1823. * Requires: MBEDTLS_HAVE_ASM
  1824. *
  1825. * This modules adds support for the VIA PadLock on x86.
  1826. */
  1827. #define MBEDTLS_PADLOCK_C
  1828. /**
  1829. * \def MBEDTLS_PEM_PARSE_C
  1830. *
  1831. * Enable PEM decoding / parsing.
  1832. *
  1833. * Module: library/pem.c
  1834. * Caller: library/dhm.c
  1835. * library/pkparse.c
  1836. * library/mbedtls_x509_crl.c
  1837. * library/mbedtls_x509_crt.c
  1838. * library/mbedtls_x509_csr.c
  1839. *
  1840. * Requires: MBEDTLS_BASE64_C
  1841. *
  1842. * This modules adds support for decoding / parsing PEM files.
  1843. */
  1844. #define MBEDTLS_PEM_PARSE_C
  1845. /**
  1846. * \def MBEDTLS_PEM_WRITE_C
  1847. *
  1848. * Enable PEM encoding / writing.
  1849. *
  1850. * Module: library/pem.c
  1851. * Caller: library/pkwrite.c
  1852. * library/x509write_crt.c
  1853. * library/mbedtls_x509write_csr.c
  1854. *
  1855. * Requires: MBEDTLS_BASE64_C
  1856. *
  1857. * This modules adds support for encoding / writing PEM files.
  1858. */
  1859. #define MBEDTLS_PEM_WRITE_C
  1860. /**
  1861. * \def MBEDTLS_PK_C
  1862. *
  1863. * Enable the generic public (asymetric) key layer.
  1864. *
  1865. * Module: library/pk.c
  1866. * Caller: library/ssl_tls.c
  1867. * library/ssl_cli.c
  1868. * library/ssl_srv.c
  1869. *
  1870. * Requires: MBEDTLS_RSA_C or MBEDTLS_ECP_C
  1871. *
  1872. * Uncomment to enable generic public key wrappers.
  1873. */
  1874. #define MBEDTLS_PK_C
  1875. /**
  1876. * \def MBEDTLS_PK_PARSE_C
  1877. *
  1878. * Enable the generic public (asymetric) key parser.
  1879. *
  1880. * Module: library/pkparse.c
  1881. * Caller: library/mbedtls_x509_crt.c
  1882. * library/mbedtls_x509_csr.c
  1883. *
  1884. * Requires: MBEDTLS_PK_C
  1885. *
  1886. * Uncomment to enable generic public key parse functions.
  1887. */
  1888. #define MBEDTLS_PK_PARSE_C
  1889. /**
  1890. * \def MBEDTLS_PK_WRITE_C
  1891. *
  1892. * Enable the generic public (asymetric) key writer.
  1893. *
  1894. * Module: library/pkwrite.c
  1895. * Caller: library/x509write.c
  1896. *
  1897. * Requires: MBEDTLS_PK_C
  1898. *
  1899. * Uncomment to enable generic public key write functions.
  1900. */
  1901. #define MBEDTLS_PK_WRITE_C
  1902. /**
  1903. * \def MBEDTLS_PKCS5_C
  1904. *
  1905. * Enable PKCS#5 functions.
  1906. *
  1907. * Module: library/pkcs5.c
  1908. *
  1909. * Requires: MBEDTLS_MD_C
  1910. *
  1911. * This module adds support for the PKCS#5 functions.
  1912. */
  1913. #define MBEDTLS_PKCS5_C
  1914. /**
  1915. * \def MBEDTLS_PKCS11_C
  1916. *
  1917. * Enable wrapper for PKCS#11 smartcard support.
  1918. *
  1919. * Module: library/pkcs11.c
  1920. * Caller: library/pk.c
  1921. *
  1922. * Requires: MBEDTLS_PK_C
  1923. *
  1924. * This module enables SSL/TLS PKCS #11 smartcard support.
  1925. * Requires the presence of the PKCS#11 helper library (libpkcs11-helper)
  1926. */
  1927. //#define MBEDTLS_PKCS11_C
  1928. /**
  1929. * \def MBEDTLS_PKCS12_C
  1930. *
  1931. * Enable PKCS#12 PBE functions.
  1932. * Adds algorithms for parsing PKCS#8 encrypted private keys
  1933. *
  1934. * Module: library/pkcs12.c
  1935. * Caller: library/pkparse.c
  1936. *
  1937. * Requires: MBEDTLS_ASN1_PARSE_C, MBEDTLS_CIPHER_C, MBEDTLS_MD_C
  1938. * Can use: MBEDTLS_ARC4_C
  1939. *
  1940. * This module enables PKCS#12 functions.
  1941. */
  1942. #define MBEDTLS_PKCS12_C
  1943. /**
  1944. * \def MBEDTLS_PLATFORM_C
  1945. *
  1946. * Enable the platform abstraction layer that allows you to re-assign
  1947. * functions like calloc(), free(), snprintf(), printf(), fprintf(), exit().
  1948. *
  1949. * Enabling MBEDTLS_PLATFORM_C enables to use of MBEDTLS_PLATFORM_XXX_ALT
  1950. * or MBEDTLS_PLATFORM_XXX_MACRO directives, allowing the functions mentioned
  1951. * above to be specified at runtime or compile time respectively.
  1952. *
  1953. * \note This abstraction layer must be enabled on Windows (including MSYS2)
  1954. * as other module rely on it for a fixed snprintf implementation.
  1955. *
  1956. * Module: library/platform.c
  1957. * Caller: Most other .c files
  1958. *
  1959. * This module enables abstraction of common (libc) functions.
  1960. */
  1961. #define MBEDTLS_PLATFORM_C
  1962. /**
  1963. * \def MBEDTLS_RIPEMD160_C
  1964. *
  1965. * Enable the RIPEMD-160 hash algorithm.
  1966. *
  1967. * Module: library/mbedtls_ripemd160.c
  1968. * Caller: library/mbedtls_md.c
  1969. *
  1970. */
  1971. #define MBEDTLS_RIPEMD160_C
  1972. /**
  1973. * \def MBEDTLS_RSA_C
  1974. *
  1975. * Enable the RSA public-key cryptosystem.
  1976. *
  1977. * Module: library/rsa.c
  1978. * Caller: library/ssl_cli.c
  1979. * library/ssl_srv.c
  1980. * library/ssl_tls.c
  1981. * library/x509.c
  1982. *
  1983. * This module is used by the following key exchanges:
  1984. * RSA, DHE-RSA, ECDHE-RSA, RSA-PSK
  1985. *
  1986. * Requires: MBEDTLS_BIGNUM_C, MBEDTLS_OID_C
  1987. */
  1988. #define MBEDTLS_RSA_C
  1989. /**
  1990. * \def MBEDTLS_SHA1_C
  1991. *
  1992. * Enable the SHA1 cryptographic hash algorithm.
  1993. *
  1994. * Module: library/mbedtls_sha1.c
  1995. * Caller: library/mbedtls_md.c
  1996. * library/ssl_cli.c
  1997. * library/ssl_srv.c
  1998. * library/ssl_tls.c
  1999. * library/x509write_crt.c
  2000. *
  2001. * This module is required for SSL/TLS and SHA1-signed certificates.
  2002. */
  2003. #define MBEDTLS_SHA1_C
  2004. /**
  2005. * \def MBEDTLS_SHA256_C
  2006. *
  2007. * Enable the SHA-224 and SHA-256 cryptographic hash algorithms.
  2008. *
  2009. * Module: library/mbedtls_sha256.c
  2010. * Caller: library/entropy.c
  2011. * library/mbedtls_md.c
  2012. * library/ssl_cli.c
  2013. * library/ssl_srv.c
  2014. * library/ssl_tls.c
  2015. *
  2016. * This module adds support for SHA-224 and SHA-256.
  2017. * This module is required for the SSL/TLS 1.2 PRF function.
  2018. */
  2019. #define MBEDTLS_SHA256_C
  2020. /**
  2021. * \def MBEDTLS_SHA512_C
  2022. *
  2023. * Enable the SHA-384 and SHA-512 cryptographic hash algorithms.
  2024. *
  2025. * Module: library/mbedtls_sha512.c
  2026. * Caller: library/entropy.c
  2027. * library/mbedtls_md.c
  2028. * library/ssl_cli.c
  2029. * library/ssl_srv.c
  2030. *
  2031. * This module adds support for SHA-384 and SHA-512.
  2032. */
  2033. #define MBEDTLS_SHA512_C
  2034. /**
  2035. * \def MBEDTLS_SSL_CACHE_C
  2036. *
  2037. * Enable simple SSL cache implementation.
  2038. *
  2039. * Module: library/ssl_cache.c
  2040. * Caller:
  2041. *
  2042. * Requires: MBEDTLS_SSL_CACHE_C
  2043. */
  2044. #define MBEDTLS_SSL_CACHE_C
  2045. /**
  2046. * \def MBEDTLS_SSL_COOKIE_C
  2047. *
  2048. * Enable basic implementation of DTLS cookies for hello verification.
  2049. *
  2050. * Module: library/ssl_cookie.c
  2051. * Caller:
  2052. */
  2053. #define MBEDTLS_SSL_COOKIE_C
  2054. /**
  2055. * \def MBEDTLS_SSL_TICKET_C
  2056. *
  2057. * Enable an implementation of TLS server-side callbacks for session tickets.
  2058. *
  2059. * Module: library/ssl_ticket.c
  2060. * Caller:
  2061. *
  2062. * Requires: MBEDTLS_CIPHER_C
  2063. */
  2064. #define MBEDTLS_SSL_TICKET_C
  2065. /**
  2066. * \def MBEDTLS_SSL_CLI_C
  2067. *
  2068. * Enable the SSL/TLS client code.
  2069. *
  2070. * Module: library/ssl_cli.c
  2071. * Caller:
  2072. *
  2073. * Requires: MBEDTLS_SSL_TLS_C
  2074. *
  2075. * This module is required for SSL/TLS client support.
  2076. */
  2077. #define MBEDTLS_SSL_CLI_C
  2078. /**
  2079. * \def MBEDTLS_SSL_SRV_C
  2080. *
  2081. * Enable the SSL/TLS server code.
  2082. *
  2083. * Module: library/ssl_srv.c
  2084. * Caller:
  2085. *
  2086. * Requires: MBEDTLS_SSL_TLS_C
  2087. *
  2088. * This module is required for SSL/TLS server support.
  2089. */
  2090. #define MBEDTLS_SSL_SRV_C
  2091. /**
  2092. * \def MBEDTLS_SSL_TLS_C
  2093. *
  2094. * Enable the generic SSL/TLS code.
  2095. *
  2096. * Module: library/ssl_tls.c
  2097. * Caller: library/ssl_cli.c
  2098. * library/ssl_srv.c
  2099. *
  2100. * Requires: MBEDTLS_CIPHER_C, MBEDTLS_MD_C
  2101. * and at least one of the MBEDTLS_SSL_PROTO_XXX defines
  2102. *
  2103. * This module is required for SSL/TLS.
  2104. */
  2105. #define MBEDTLS_SSL_TLS_C
  2106. /**
  2107. * \def MBEDTLS_THREADING_C
  2108. *
  2109. * Enable the threading abstraction layer.
  2110. * By default mbed TLS assumes it is used in a non-threaded environment or that
  2111. * contexts are not shared between threads. If you do intend to use contexts
  2112. * between threads, you will need to enable this layer to prevent race
  2113. * conditions.
  2114. *
  2115. * Module: library/threading.c
  2116. *
  2117. * This allows different threading implementations (self-implemented or
  2118. * provided).
  2119. *
  2120. * You will have to enable either MBEDTLS_THREADING_ALT or
  2121. * MBEDTLS_THREADING_PTHREAD.
  2122. *
  2123. * Enable this layer to allow use of mutexes within mbed TLS
  2124. */
  2125. //#define MBEDTLS_THREADING_C
  2126. /**
  2127. * \def MBEDTLS_TIMING_C
  2128. *
  2129. * Enable the portable timing interface.
  2130. *
  2131. * Module: library/timing.c
  2132. * Caller: library/havege.c
  2133. *
  2134. * This module is used by the HAVEGE random number generator.
  2135. */
  2136. #define MBEDTLS_TIMING_C
  2137. /**
  2138. * \def MBEDTLS_VERSION_C
  2139. *
  2140. * Enable run-time version information.
  2141. *
  2142. * Module: library/version.c
  2143. *
  2144. * This module provides run-time version information.
  2145. */
  2146. #define MBEDTLS_VERSION_C
  2147. /**
  2148. * \def MBEDTLS_X509_USE_C
  2149. *
  2150. * Enable X.509 core for using certificates.
  2151. *
  2152. * Module: library/x509.c
  2153. * Caller: library/mbedtls_x509_crl.c
  2154. * library/mbedtls_x509_crt.c
  2155. * library/mbedtls_x509_csr.c
  2156. *
  2157. * Requires: MBEDTLS_ASN1_PARSE_C, MBEDTLS_BIGNUM_C, MBEDTLS_OID_C,
  2158. * MBEDTLS_PK_PARSE_C
  2159. *
  2160. * This module is required for the X.509 parsing modules.
  2161. */
  2162. #define MBEDTLS_X509_USE_C
  2163. /**
  2164. * \def MBEDTLS_X509_CRT_PARSE_C
  2165. *
  2166. * Enable X.509 certificate parsing.
  2167. *
  2168. * Module: library/mbedtls_x509_crt.c
  2169. * Caller: library/ssl_cli.c
  2170. * library/ssl_srv.c
  2171. * library/ssl_tls.c
  2172. *
  2173. * Requires: MBEDTLS_X509_USE_C
  2174. *
  2175. * This module is required for X.509 certificate parsing.
  2176. */
  2177. #define MBEDTLS_X509_CRT_PARSE_C
  2178. /**
  2179. * \def MBEDTLS_X509_CRL_PARSE_C
  2180. *
  2181. * Enable X.509 CRL parsing.
  2182. *
  2183. * Module: library/mbedtls_x509_crl.c
  2184. * Caller: library/mbedtls_x509_crt.c
  2185. *
  2186. * Requires: MBEDTLS_X509_USE_C
  2187. *
  2188. * This module is required for X.509 CRL parsing.
  2189. */
  2190. #define MBEDTLS_X509_CRL_PARSE_C
  2191. /**
  2192. * \def MBEDTLS_X509_CSR_PARSE_C
  2193. *
  2194. * Enable X.509 Certificate Signing Request (CSR) parsing.
  2195. *
  2196. * Module: library/mbedtls_x509_csr.c
  2197. * Caller: library/x509_crt_write.c
  2198. *
  2199. * Requires: MBEDTLS_X509_USE_C
  2200. *
  2201. * This module is used for reading X.509 certificate request.
  2202. */
  2203. #define MBEDTLS_X509_CSR_PARSE_C
  2204. /**
  2205. * \def MBEDTLS_X509_CREATE_C
  2206. *
  2207. * Enable X.509 core for creating certificates.
  2208. *
  2209. * Module: library/x509_create.c
  2210. *
  2211. * Requires: MBEDTLS_BIGNUM_C, MBEDTLS_OID_C, MBEDTLS_PK_WRITE_C
  2212. *
  2213. * This module is the basis for creating X.509 certificates and CSRs.
  2214. */
  2215. #define MBEDTLS_X509_CREATE_C
  2216. /**
  2217. * \def MBEDTLS_X509_CRT_WRITE_C
  2218. *
  2219. * Enable creating X.509 certificates.
  2220. *
  2221. * Module: library/x509_crt_write.c
  2222. *
  2223. * Requires: MBEDTLS_X509_CREATE_C
  2224. *
  2225. * This module is required for X.509 certificate creation.
  2226. */
  2227. #define MBEDTLS_X509_CRT_WRITE_C
  2228. /**
  2229. * \def MBEDTLS_X509_CSR_WRITE_C
  2230. *
  2231. * Enable creating X.509 Certificate Signing Requests (CSR).
  2232. *
  2233. * Module: library/x509_csr_write.c
  2234. *
  2235. * Requires: MBEDTLS_X509_CREATE_C
  2236. *
  2237. * This module is required for X.509 certificate request writing.
  2238. */
  2239. #define MBEDTLS_X509_CSR_WRITE_C
  2240. /**
  2241. * \def MBEDTLS_XTEA_C
  2242. *
  2243. * Enable the XTEA block cipher.
  2244. *
  2245. * Module: library/xtea.c
  2246. * Caller:
  2247. */
  2248. #define MBEDTLS_XTEA_C
  2249. /* \} name SECTION: mbed TLS modules */
  2250. /**
  2251. * \name SECTION: Module configuration options
  2252. *
  2253. * This section allows for the setting of module specific sizes and
  2254. * configuration options. The default values are already present in the
  2255. * relevant header files and should suffice for the regular use cases.
  2256. *
  2257. * Our advice is to enable options and change their values here
  2258. * only if you have a good reason and know the consequences.
  2259. *
  2260. * Please check the respective header file for documentation on these
  2261. * parameters (to prevent duplicate documentation).
  2262. * \{
  2263. */
  2264. /* MPI / BIGNUM options */
  2265. //#define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
  2266. //#define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
  2267. /* CTR_DRBG options */
  2268. //#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
  2269. //#define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
  2270. //#define MBEDTLS_CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
  2271. //#define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
  2272. //#define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
  2273. /* HMAC_DRBG options */
  2274. //#define MBEDTLS_HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
  2275. //#define MBEDTLS_HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
  2276. //#define MBEDTLS_HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
  2277. //#define MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
  2278. /* ECP options */
  2279. //#define MBEDTLS_ECP_MAX_BITS 521 /**< Maximum bit size of groups */
  2280. //#define MBEDTLS_ECP_WINDOW_SIZE 6 /**< Maximum window size used */
  2281. //#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */
  2282. /* Entropy options */
  2283. //#define MBEDTLS_ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
  2284. //#define MBEDTLS_ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */
  2285. /* Memory buffer allocator options */
  2286. //#define MBEDTLS_MEMORY_ALIGN_MULTIPLE 4 /**< Align on multiples of this value */
  2287. /* Platform options */
  2288. //#define MBEDTLS_PLATFORM_STD_MEM_HDR <stdlib.h> /**< Header to include if MBEDTLS_PLATFORM_NO_STD_FUNCTIONS is defined. Don't define if no header is needed. */
  2289. //#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator to use, can be undefined */
  2290. //#define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use, can be undefined */
  2291. //#define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default exit to use, can be undefined */
  2292. //#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use, can be undefined */
  2293. //#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< Default printf to use, can be undefined */
  2294. /* Note: your snprintf must correclty zero-terminate the buffer! */
  2295. //#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use, can be undefined */
  2296. /* To Use Function Macros MBEDTLS_PLATFORM_C must be enabled */
  2297. /* MBEDTLS_PLATFORM_XXX_MACRO and MBEDTLS_PLATFORM_XXX_ALT cannot both be defined */
  2298. //#define MBEDTLS_PLATFORM_CALLOC_MACRO calloc /**< Default allocator macro to use, can be undefined */
  2299. //#define MBEDTLS_PLATFORM_FREE_MACRO free /**< Default free macro to use, can be undefined */
  2300. //#define MBEDTLS_PLATFORM_EXIT_MACRO exit /**< Default exit macro to use, can be undefined */
  2301. //#define MBEDTLS_PLATFORM_FPRINTF_MACRO fprintf /**< Default fprintf macro to use, can be undefined */
  2302. //#define MBEDTLS_PLATFORM_PRINTF_MACRO printf /**< Default printf macro to use, can be undefined */
  2303. /* Note: your snprintf must correclty zero-terminate the buffer! */
  2304. //#define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf /**< Default snprintf macro to use, can be undefined */
  2305. /* SSL Cache options */
  2306. //#define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT 86400 /**< 1 day */
  2307. //#define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /**< Maximum entries in cache */
  2308. /* SSL options */
  2309. //#define MBEDTLS_SSL_MAX_CONTENT_LEN 16384 /**< Maxium fragment length in bytes, determines the size of each of the two internal I/O buffers */
  2310. //#define MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME 86400 /**< Lifetime of session tickets (if enabled) */
  2311. //#define MBEDTLS_PSK_MAX_LEN 32 /**< Max size of TLS pre-shared keys, in bytes (default 256 bits) */
  2312. //#define MBEDTLS_SSL_COOKIE_TIMEOUT 60 /**< Default expiration delay of DTLS cookies, in seconds if HAVE_TIME, or in number of cookies issued */
  2313. /**
  2314. * Complete list of ciphersuites to use, in order of preference.
  2315. *
  2316. * \warning No dependency checking is done on that field! This option can only
  2317. * be used to restrict the set of available ciphersuites. It is your
  2318. * responsibility to make sure the needed modules are active.
  2319. *
  2320. * Use this to save a few hundred bytes of ROM (default ordering of all
  2321. * available ciphersuites) and a few to a few hundred bytes of RAM.
  2322. *
  2323. * The value below is only an example, not the default.
  2324. */
  2325. //#define MBEDTLS_SSL_CIPHERSUITES MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
  2326. /* X509 options */
  2327. //#define MBEDTLS_X509_MAX_INTERMEDIATE_CA 8 /**< Maximum number of intermediate CAs in a verification chain. */
  2328. /* \} name SECTION: Module configuration options */
  2329. #if defined(TARGET_LIKE_MBED)
  2330. #endif
  2331. /*
  2332. * Allow user to override any previous default.
  2333. *
  2334. * Use two macro names for that, as:
  2335. * - with yotta the prefix YOTTA_CFG_ is forced
  2336. * - without yotta is looks weird to have a YOTTA prefix.
  2337. */
  2338. #if defined(YOTTA_CFG_MBEDTLS_USER_CONFIG_FILE)
  2339. #include YOTTA_CFG_MBEDTLS_USER_CONFIG_FILE
  2340. #elif defined(MBEDTLS_USER_CONFIG_FILE)
  2341. #include MBEDTLS_USER_CONFIG_FILE
  2342. #endif
  2343. #endif /* MBEDTLS_CONFIG_H */
  2344. /********* Start of file include/mbedtls/check_config.h ************/
  2345. /**
  2346. * \file check_config.h
  2347. *
  2348. * \brief Consistency checks for configuration options
  2349. *
  2350. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  2351. * SPDX-License-Identifier: Apache-2.0
  2352. *
  2353. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  2354. * not use this file except in compliance with the License.
  2355. * You may obtain a copy of the License at
  2356. *
  2357. * http://www.apache.org/licenses/LICENSE-2.0
  2358. *
  2359. * Unless required by applicable law or agreed to in writing, software
  2360. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  2361. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2362. * See the License for the specific language governing permissions and
  2363. * limitations under the License.
  2364. *
  2365. * This file is part of mbed TLS (https://tls.mbed.org)
  2366. */
  2367. /*
  2368. * It is recommended to include this file from your config.h
  2369. * in order to catch dependency issues early.
  2370. */
  2371. #ifndef MBEDTLS_CHECK_CONFIG_H
  2372. #define MBEDTLS_CHECK_CONFIG_H
  2373. /*
  2374. * We assume CHAR_BIT is 8 in many places. In practice, this is true on our
  2375. * target platforms, so not an issue, but let's just be extra sure.
  2376. */
  2377. #include <limits.h>
  2378. #if CHAR_BIT != 8
  2379. #error "mbed TLS requires a platform with 8-bit chars"
  2380. #endif
  2381. #if defined(_WIN32)
  2382. #if !defined(MBEDTLS_PLATFORM_C)
  2383. #error "MBEDTLS_PLATFORM_C is required on Windows"
  2384. #endif
  2385. /* Fix the config here. Not convenient to put an #ifdef _WIN32 in config.h as
  2386. * it would confuse config.pl. */
  2387. #if !defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) && \
  2388. !defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
  2389. #define MBEDTLS_PLATFORM_SNPRINTF_ALT
  2390. #endif
  2391. #endif /* _WIN32 */
  2392. #if defined(TARGET_LIKE_MBED) && \
  2393. ( defined(MBEDTLS_NET_C) || defined(MBEDTLS_TIMING_C) )
  2394. #error "The NET and TIMING modules are not available for mbed OS - please use the network and timing functions provided by mbed OS"
  2395. #endif
  2396. #if defined(MBEDTLS_DEPRECATED_WARNING) && \
  2397. !defined(__GNUC__) && !defined(__clang__)
  2398. #error "MBEDTLS_DEPRECATED_WARNING only works with GCC and Clang"
  2399. #endif
  2400. #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_HAVE_TIME)
  2401. #error "MBEDTLS_HAVE_TIME_DATE without MBEDTLS_HAVE_TIME does not make sense"
  2402. #endif
  2403. #if defined(MBEDTLS_AESNI_C) && !defined(MBEDTLS_HAVE_ASM)
  2404. #error "MBEDTLS_AESNI_C defined, but not all prerequisites"
  2405. #endif
  2406. #if defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_AES_C)
  2407. #error "MBEDTLS_CTR_DRBG_C defined, but not all prerequisites"
  2408. #endif
  2409. #if defined(MBEDTLS_DHM_C) && !defined(MBEDTLS_BIGNUM_C)
  2410. #error "MBEDTLS_DHM_C defined, but not all prerequisites"
  2411. #endif
  2412. #if defined(MBEDTLS_ECDH_C) && !defined(MBEDTLS_ECP_C)
  2413. #error "MBEDTLS_ECDH_C defined, but not all prerequisites"
  2414. #endif
  2415. #if defined(MBEDTLS_ECDSA_C) && \
  2416. ( !defined(MBEDTLS_ECP_C) || \
  2417. !defined(MBEDTLS_ASN1_PARSE_C) || \
  2418. !defined(MBEDTLS_ASN1_WRITE_C) )
  2419. #error "MBEDTLS_ECDSA_C defined, but not all prerequisites"
  2420. #endif
  2421. #if defined(MBEDTLS_ECJPAKE_C) && \
  2422. ( !defined(MBEDTLS_ECP_C) || !defined(MBEDTLS_MD_C) )
  2423. #error "MBEDTLS_ECJPAKE_C defined, but not all prerequisites"
  2424. #endif
  2425. #if defined(MBEDTLS_ECDSA_DETERMINISTIC) && !defined(MBEDTLS_HMAC_DRBG_C)
  2426. #error "MBEDTLS_ECDSA_DETERMINISTIC defined, but not all prerequisites"
  2427. #endif
  2428. #if defined(MBEDTLS_ECP_C) && ( !defined(MBEDTLS_BIGNUM_C) || ( \
  2429. !defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) && \
  2430. !defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) && \
  2431. !defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) && \
  2432. !defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) && \
  2433. !defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) && \
  2434. !defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) && \
  2435. !defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) && \
  2436. !defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) && \
  2437. !defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) && \
  2438. !defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) && \
  2439. !defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) ) )
  2440. #error "MBEDTLS_ECP_C defined, but not all prerequisites"
  2441. #endif
  2442. #if defined(MBEDTLS_ENTROPY_C) && (!defined(MBEDTLS_SHA512_C) && \
  2443. !defined(MBEDTLS_SHA256_C))
  2444. #error "MBEDTLS_ENTROPY_C defined, but not all prerequisites"
  2445. #endif
  2446. #if defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_SHA512_C) && \
  2447. defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) && (MBEDTLS_CTR_DRBG_ENTROPY_LEN > 64)
  2448. #error "MBEDTLS_CTR_DRBG_ENTROPY_LEN value too high"
  2449. #endif
  2450. #if defined(MBEDTLS_ENTROPY_C) && \
  2451. ( !defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_ENTROPY_FORCE_SHA256) ) \
  2452. && defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) && (MBEDTLS_CTR_DRBG_ENTROPY_LEN > 32)
  2453. #error "MBEDTLS_CTR_DRBG_ENTROPY_LEN value too high"
  2454. #endif
  2455. #if defined(MBEDTLS_ENTROPY_C) && \
  2456. defined(MBEDTLS_ENTROPY_FORCE_SHA256) && !defined(MBEDTLS_SHA256_C)
  2457. #error "MBEDTLS_ENTROPY_FORCE_SHA256 defined, but not all prerequisites"
  2458. #endif
  2459. #if defined(MBEDTLS_GCM_C) && ( \
  2460. !defined(MBEDTLS_AES_C) && !defined(MBEDTLS_CAMELLIA_C) )
  2461. #error "MBEDTLS_GCM_C defined, but not all prerequisites"
  2462. #endif
  2463. #if defined(MBEDTLS_HAVEGE_C) && !defined(MBEDTLS_TIMING_C)
  2464. #error "MBEDTLS_HAVEGE_C defined, but not all prerequisites"
  2465. #endif
  2466. #if defined(MBEDTLS_HMAC_DRBG_C) && !defined(MBEDTLS_MD_C)
  2467. #error "MBEDTLS_HMAC_DRBG_C defined, but not all prerequisites"
  2468. #endif
  2469. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) && \
  2470. ( !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) )
  2471. #error "MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED defined, but not all prerequisites"
  2472. #endif
  2473. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
  2474. ( !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) )
  2475. #error "MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED defined, but not all prerequisites"
  2476. #endif
  2477. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) && !defined(MBEDTLS_DHM_C)
  2478. #error "MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED defined, but not all prerequisites"
  2479. #endif
  2480. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) && \
  2481. !defined(MBEDTLS_ECDH_C)
  2482. #error "MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED defined, but not all prerequisites"
  2483. #endif
  2484. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
  2485. ( !defined(MBEDTLS_DHM_C) || !defined(MBEDTLS_RSA_C) || \
  2486. !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_PKCS1_V15) )
  2487. #error "MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED defined, but not all prerequisites"
  2488. #endif
  2489. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
  2490. ( !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_RSA_C) || \
  2491. !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_PKCS1_V15) )
  2492. #error "MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED defined, but not all prerequisites"
  2493. #endif
  2494. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
  2495. ( !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_ECDSA_C) || \
  2496. !defined(MBEDTLS_X509_CRT_PARSE_C) )
  2497. #error "MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED defined, but not all prerequisites"
  2498. #endif
  2499. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
  2500. ( !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
  2501. !defined(MBEDTLS_PKCS1_V15) )
  2502. #error "MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED defined, but not all prerequisites"
  2503. #endif
  2504. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
  2505. ( !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
  2506. !defined(MBEDTLS_PKCS1_V15) )
  2507. #error "MBEDTLS_KEY_EXCHANGE_RSA_ENABLED defined, but not all prerequisites"
  2508. #endif
  2509. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
  2510. ( !defined(MBEDTLS_ECJPAKE_C) || !defined(MBEDTLS_SHA256_C) || \
  2511. !defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) )
  2512. #error "MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED defined, but not all prerequisites"
  2513. #endif
  2514. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
  2515. ( !defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_PLATFORM_MEMORY) )
  2516. #error "MBEDTLS_MEMORY_BUFFER_ALLOC_C defined, but not all prerequisites"
  2517. #endif
  2518. #if defined(MBEDTLS_PADLOCK_C) && !defined(MBEDTLS_HAVE_ASM)
  2519. #error "MBEDTLS_PADLOCK_C defined, but not all prerequisites"
  2520. #endif
  2521. #if defined(MBEDTLS_PEM_PARSE_C) && !defined(MBEDTLS_BASE64_C)
  2522. #error "MBEDTLS_PEM_PARSE_C defined, but not all prerequisites"
  2523. #endif
  2524. #if defined(MBEDTLS_PEM_WRITE_C) && !defined(MBEDTLS_BASE64_C)
  2525. #error "MBEDTLS_PEM_WRITE_C defined, but not all prerequisites"
  2526. #endif
  2527. #if defined(MBEDTLS_PK_C) && \
  2528. ( !defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_ECP_C) )
  2529. #error "MBEDTLS_PK_C defined, but not all prerequisites"
  2530. #endif
  2531. #if defined(MBEDTLS_PK_PARSE_C) && !defined(MBEDTLS_PK_C)
  2532. #error "MBEDTLS_PK_PARSE_C defined, but not all prerequisites"
  2533. #endif
  2534. #if defined(MBEDTLS_PK_WRITE_C) && !defined(MBEDTLS_PK_C)
  2535. #error "MBEDTLS_PK_WRITE_C defined, but not all prerequisites"
  2536. #endif
  2537. #if defined(MBEDTLS_PKCS11_C) && !defined(MBEDTLS_PK_C)
  2538. #error "MBEDTLS_PKCS11_C defined, but not all prerequisites"
  2539. #endif
  2540. #if defined(MBEDTLS_PLATFORM_EXIT_ALT) && !defined(MBEDTLS_PLATFORM_C)
  2541. #error "MBEDTLS_PLATFORM_EXIT_ALT defined, but not all prerequisites"
  2542. #endif
  2543. #if defined(MBEDTLS_PLATFORM_EXIT_MACRO) && !defined(MBEDTLS_PLATFORM_C)
  2544. #error "MBEDTLS_PLATFORM_EXIT_MACRO defined, but not all prerequisites"
  2545. #endif
  2546. #if defined(MBEDTLS_PLATFORM_EXIT_MACRO) &&\
  2547. ( defined(MBEDTLS_PLATFORM_STD_EXIT) ||\
  2548. defined(MBEDTLS_PLATFORM_EXIT_ALT) )
  2549. #error "MBEDTLS_PLATFORM_EXIT_MACRO and MBEDTLS_PLATFORM_STD_EXIT/MBEDTLS_PLATFORM_EXIT_ALT cannot be defined simultaneously"
  2550. #endif
  2551. #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT) && !defined(MBEDTLS_PLATFORM_C)
  2552. #error "MBEDTLS_PLATFORM_FPRINTF_ALT defined, but not all prerequisites"
  2553. #endif
  2554. #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO) && !defined(MBEDTLS_PLATFORM_C)
  2555. #error "MBEDTLS_PLATFORM_FPRINTF_MACRO defined, but not all prerequisites"
  2556. #endif
  2557. #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO) &&\
  2558. ( defined(MBEDTLS_PLATFORM_STD_FPRINTF) ||\
  2559. defined(MBEDTLS_PLATFORM_FPRINTF_ALT) )
  2560. #error "MBEDTLS_PLATFORM_FPRINTF_MACRO and MBEDTLS_PLATFORM_STD_FPRINTF/MBEDTLS_PLATFORM_FPRINTF_ALT cannot be defined simultaneously"
  2561. #endif
  2562. #if defined(MBEDTLS_PLATFORM_FREE_MACRO) &&\
  2563. ( !defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_PLATFORM_MEMORY) )
  2564. #error "MBEDTLS_PLATFORM_FREE_MACRO defined, but not all prerequisites"
  2565. #endif
  2566. #if defined(MBEDTLS_PLATFORM_FREE_MACRO) &&\
  2567. defined(MBEDTLS_PLATFORM_STD_FREE)
  2568. #error "MBEDTLS_PLATFORM_FREE_MACRO and MBEDTLS_PLATFORM_STD_FREE cannot be defined simultaneously"
  2569. #endif
  2570. #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && !defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
  2571. #error "MBEDTLS_PLATFORM_CALLOC_MACRO must be defined if MBEDTLS_PLATFORM_FREE_MACRO is"
  2572. #endif
  2573. #if defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&\
  2574. ( !defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_PLATFORM_MEMORY) )
  2575. #error "MBEDTLS_PLATFORM_CALLOC_MACRO defined, but not all prerequisites"
  2576. #endif
  2577. #if defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&\
  2578. defined(MBEDTLS_PLATFORM_STD_CALLOC)
  2579. #error "MBEDTLS_PLATFORM_CALLOC_MACRO and MBEDTLS_PLATFORM_STD_CALLOC cannot be defined simultaneously"
  2580. #endif
  2581. #if defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && !defined(MBEDTLS_PLATFORM_FREE_MACRO)
  2582. #error "MBEDTLS_PLATFORM_FREE_MACRO must be defined if MBEDTLS_PLATFORM_CALLOC_MACRO is"
  2583. #endif
  2584. #if defined(MBEDTLS_PLATFORM_MEMORY) && !defined(MBEDTLS_PLATFORM_C)
  2585. #error "MBEDTLS_PLATFORM_MEMORY defined, but not all prerequisites"
  2586. #endif
  2587. #if defined(MBEDTLS_PLATFORM_PRINTF_ALT) && !defined(MBEDTLS_PLATFORM_C)
  2588. #error "MBEDTLS_PLATFORM_PRINTF_ALT defined, but not all prerequisites"
  2589. #endif
  2590. #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO) && !defined(MBEDTLS_PLATFORM_C)
  2591. #error "MBEDTLS_PLATFORM_PRINTF_MACRO defined, but not all prerequisites"
  2592. #endif
  2593. #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO) &&\
  2594. ( defined(MBEDTLS_PLATFORM_STD_PRINTF) ||\
  2595. defined(MBEDTLS_PLATFORM_PRINTF_ALT) )
  2596. #error "MBEDTLS_PLATFORM_PRINTF_MACRO and MBEDTLS_PLATFORM_STD_PRINTF/MBEDTLS_PLATFORM_PRINTF_ALT cannot be defined simultaneously"
  2597. #endif
  2598. #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) && !defined(MBEDTLS_PLATFORM_C)
  2599. #error "MBEDTLS_PLATFORM_SNPRINTF_ALT defined, but not all prerequisites"
  2600. #endif
  2601. #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO) && !defined(MBEDTLS_PLATFORM_C)
  2602. #error "MBEDTLS_PLATFORM_SNPRINTF_MACRO defined, but not all prerequisites"
  2603. #endif
  2604. #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO) &&\
  2605. ( defined(MBEDTLS_PLATFORM_STD_SNPRINTF) ||\
  2606. defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) )
  2607. #error "MBEDTLS_PLATFORM_SNPRINTF_MACRO and MBEDTLS_PLATFORM_STD_SNPRINTF/MBEDTLS_PLATFORM_SNPRINTF_ALT cannot be defined simultaneously"
  2608. #endif
  2609. #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR) &&\
  2610. !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
  2611. #error "MBEDTLS_PLATFORM_STD_MEM_HDR defined, but not all prerequisites"
  2612. #endif
  2613. #if defined(MBEDTLS_PLATFORM_STD_CALLOC) && !defined(MBEDTLS_PLATFORM_MEMORY)
  2614. #error "MBEDTLS_PLATFORM_STD_CALLOC defined, but not all prerequisites"
  2615. #endif
  2616. #if defined(MBEDTLS_PLATFORM_STD_CALLOC) && !defined(MBEDTLS_PLATFORM_MEMORY)
  2617. #error "MBEDTLS_PLATFORM_STD_CALLOC defined, but not all prerequisites"
  2618. #endif
  2619. #if defined(MBEDTLS_PLATFORM_STD_FREE) && !defined(MBEDTLS_PLATFORM_MEMORY)
  2620. #error "MBEDTLS_PLATFORM_STD_FREE defined, but not all prerequisites"
  2621. #endif
  2622. #if defined(MBEDTLS_PLATFORM_STD_EXIT) &&\
  2623. !defined(MBEDTLS_PLATFORM_EXIT_ALT)
  2624. #error "MBEDTLS_PLATFORM_STD_EXIT defined, but not all prerequisites"
  2625. #endif
  2626. #if defined(MBEDTLS_PLATFORM_STD_FPRINTF) &&\
  2627. !defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
  2628. #error "MBEDTLS_PLATFORM_STD_FPRINTF defined, but not all prerequisites"
  2629. #endif
  2630. #if defined(MBEDTLS_PLATFORM_STD_PRINTF) &&\
  2631. !defined(MBEDTLS_PLATFORM_PRINTF_ALT)
  2632. #error "MBEDTLS_PLATFORM_STD_PRINTF defined, but not all prerequisites"
  2633. #endif
  2634. #if defined(MBEDTLS_PLATFORM_STD_SNPRINTF) &&\
  2635. !defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
  2636. #error "MBEDTLS_PLATFORM_STD_SNPRINTF defined, but not all prerequisites"
  2637. #endif
  2638. #if defined(MBEDTLS_RSA_C) && ( !defined(MBEDTLS_BIGNUM_C) || \
  2639. !defined(MBEDTLS_OID_C) )
  2640. #error "MBEDTLS_RSA_C defined, but not all prerequisites"
  2641. #endif
  2642. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
  2643. ( !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_PKCS1_V21) )
  2644. #error "MBEDTLS_X509_RSASSA_PSS_SUPPORT defined, but not all prerequisites"
  2645. #endif
  2646. #if defined(MBEDTLS_SSL_PROTO_SSL3) && ( !defined(MBEDTLS_MD5_C) || \
  2647. !defined(MBEDTLS_SHA1_C) )
  2648. #error "MBEDTLS_SSL_PROTO_SSL3 defined, but not all prerequisites"
  2649. #endif
  2650. #if defined(MBEDTLS_SSL_PROTO_TLS1) && ( !defined(MBEDTLS_MD5_C) || \
  2651. !defined(MBEDTLS_SHA1_C) )
  2652. #error "MBEDTLS_SSL_PROTO_TLS1 defined, but not all prerequisites"
  2653. #endif
  2654. #if defined(MBEDTLS_SSL_PROTO_TLS1_1) && ( !defined(MBEDTLS_MD5_C) || \
  2655. !defined(MBEDTLS_SHA1_C) )
  2656. #error "MBEDTLS_SSL_PROTO_TLS1_1 defined, but not all prerequisites"
  2657. #endif
  2658. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && ( !defined(MBEDTLS_SHA1_C) && \
  2659. !defined(MBEDTLS_SHA256_C) && !defined(MBEDTLS_SHA512_C) )
  2660. #error "MBEDTLS_SSL_PROTO_TLS1_2 defined, but not all prerequisites"
  2661. #endif
  2662. #if defined(MBEDTLS_SSL_PROTO_DTLS) && \
  2663. !defined(MBEDTLS_SSL_PROTO_TLS1_1) && \
  2664. !defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2665. #error "MBEDTLS_SSL_PROTO_DTLS defined, but not all prerequisites"
  2666. #endif
  2667. #if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_TLS_C)
  2668. #error "MBEDTLS_SSL_CLI_C defined, but not all prerequisites"
  2669. #endif
  2670. #if defined(MBEDTLS_SSL_TLS_C) && ( !defined(MBEDTLS_CIPHER_C) || \
  2671. !defined(MBEDTLS_MD_C) )
  2672. #error "MBEDTLS_SSL_TLS_C defined, but not all prerequisites"
  2673. #endif
  2674. #if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_TLS_C)
  2675. #error "MBEDTLS_SSL_SRV_C defined, but not all prerequisites"
  2676. #endif
  2677. #if defined(MBEDTLS_SSL_TLS_C) && (!defined(MBEDTLS_SSL_PROTO_SSL3) && \
  2678. !defined(MBEDTLS_SSL_PROTO_TLS1) && !defined(MBEDTLS_SSL_PROTO_TLS1_1) && \
  2679. !defined(MBEDTLS_SSL_PROTO_TLS1_2))
  2680. #error "MBEDTLS_SSL_TLS_C defined, but no protocols are active"
  2681. #endif
  2682. #if defined(MBEDTLS_SSL_TLS_C) && (defined(MBEDTLS_SSL_PROTO_SSL3) && \
  2683. defined(MBEDTLS_SSL_PROTO_TLS1_1) && !defined(MBEDTLS_SSL_PROTO_TLS1))
  2684. #error "Illegal protocol selection"
  2685. #endif
  2686. #if defined(MBEDTLS_SSL_TLS_C) && (defined(MBEDTLS_SSL_PROTO_TLS1) && \
  2687. defined(MBEDTLS_SSL_PROTO_TLS1_2) && !defined(MBEDTLS_SSL_PROTO_TLS1_1))
  2688. #error "Illegal protocol selection"
  2689. #endif
  2690. #if defined(MBEDTLS_SSL_TLS_C) && (defined(MBEDTLS_SSL_PROTO_SSL3) && \
  2691. defined(MBEDTLS_SSL_PROTO_TLS1_2) && (!defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2692. !defined(MBEDTLS_SSL_PROTO_TLS1_1)))
  2693. #error "Illegal protocol selection"
  2694. #endif
  2695. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && !defined(MBEDTLS_SSL_PROTO_DTLS)
  2696. #error "MBEDTLS_SSL_DTLS_HELLO_VERIFY defined, but not all prerequisites"
  2697. #endif
  2698. #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && \
  2699. !defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  2700. #error "MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE defined, but not all prerequisites"
  2701. #endif
  2702. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
  2703. ( !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) )
  2704. #error "MBEDTLS_SSL_DTLS_ANTI_REPLAY defined, but not all prerequisites"
  2705. #endif
  2706. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
  2707. ( !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) )
  2708. #error "MBEDTLS_SSL_DTLS_BADMAC_LIMIT defined, but not all prerequisites"
  2709. #endif
  2710. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
  2711. !defined(MBEDTLS_SSL_PROTO_TLS1) && \
  2712. !defined(MBEDTLS_SSL_PROTO_TLS1_1) && \
  2713. !defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2714. #error "MBEDTLS_SSL_ENCRYPT_THEN_MAC defined, but not all prerequsites"
  2715. #endif
  2716. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
  2717. !defined(MBEDTLS_SSL_PROTO_TLS1) && \
  2718. !defined(MBEDTLS_SSL_PROTO_TLS1_1) && \
  2719. !defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2720. #error "MBEDTLS_SSL_EXTENDED_MASTER_SECRET defined, but not all prerequsites"
  2721. #endif
  2722. #if defined(MBEDTLS_SSL_TICKET_C) && !defined(MBEDTLS_CIPHER_C)
  2723. #error "MBEDTLS_SSL_TICKET_C defined, but not all prerequisites"
  2724. #endif
  2725. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) && \
  2726. !defined(MBEDTLS_SSL_PROTO_SSL3) && !defined(MBEDTLS_SSL_PROTO_TLS1)
  2727. #error "MBEDTLS_SSL_CBC_RECORD_SPLITTING defined, but not all prerequisites"
  2728. #endif
  2729. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
  2730. !defined(MBEDTLS_X509_CRT_PARSE_C)
  2731. #error "MBEDTLS_SSL_SERVER_NAME_INDICATION defined, but not all prerequisites"
  2732. #endif
  2733. #if defined(MBEDTLS_THREADING_PTHREAD)
  2734. #if !defined(MBEDTLS_THREADING_C) || defined(MBEDTLS_THREADING_IMPL)
  2735. #error "MBEDTLS_THREADING_PTHREAD defined, but not all prerequisites"
  2736. #endif
  2737. #define MBEDTLS_THREADING_IMPL
  2738. #endif
  2739. #if defined(MBEDTLS_THREADING_ALT)
  2740. #if !defined(MBEDTLS_THREADING_C) || defined(MBEDTLS_THREADING_IMPL)
  2741. #error "MBEDTLS_THREADING_ALT defined, but not all prerequisites"
  2742. #endif
  2743. #define MBEDTLS_THREADING_IMPL
  2744. #endif
  2745. #if defined(MBEDTLS_THREADING_C) && !defined(MBEDTLS_THREADING_IMPL)
  2746. #error "MBEDTLS_THREADING_C defined, single threading implementation required"
  2747. #endif
  2748. #undef MBEDTLS_THREADING_IMPL
  2749. #if defined(MBEDTLS_VERSION_FEATURES) && !defined(MBEDTLS_VERSION_C)
  2750. #error "MBEDTLS_VERSION_FEATURES defined, but not all prerequisites"
  2751. #endif
  2752. #if defined(MBEDTLS_X509_USE_C) && ( !defined(MBEDTLS_BIGNUM_C) || \
  2753. !defined(MBEDTLS_OID_C) || !defined(MBEDTLS_ASN1_PARSE_C) || \
  2754. !defined(MBEDTLS_PK_PARSE_C) )
  2755. #error "MBEDTLS_X509_USE_C defined, but not all prerequisites"
  2756. #endif
  2757. #if defined(MBEDTLS_X509_CREATE_C) && ( !defined(MBEDTLS_BIGNUM_C) || \
  2758. !defined(MBEDTLS_OID_C) || !defined(MBEDTLS_ASN1_WRITE_C) || \
  2759. !defined(MBEDTLS_PK_WRITE_C) )
  2760. #error "MBEDTLS_X509_CREATE_C defined, but not all prerequisites"
  2761. #endif
  2762. #if defined(MBEDTLS_X509_CRT_PARSE_C) && ( !defined(MBEDTLS_X509_USE_C) )
  2763. #error "MBEDTLS_X509_CRT_PARSE_C defined, but not all prerequisites"
  2764. #endif
  2765. #if defined(MBEDTLS_X509_CRL_PARSE_C) && ( !defined(MBEDTLS_X509_USE_C) )
  2766. #error "MBEDTLS_X509_CRL_PARSE_C defined, but not all prerequisites"
  2767. #endif
  2768. #if defined(MBEDTLS_X509_CSR_PARSE_C) && ( !defined(MBEDTLS_X509_USE_C) )
  2769. #error "MBEDTLS_X509_CSR_PARSE_C defined, but not all prerequisites"
  2770. #endif
  2771. #if defined(MBEDTLS_X509_CRT_WRITE_C) && ( !defined(MBEDTLS_X509_CREATE_C) )
  2772. #error "MBEDTLS_X509_CRT_WRITE_C defined, but not all prerequisites"
  2773. #endif
  2774. #if defined(MBEDTLS_X509_CSR_WRITE_C) && ( !defined(MBEDTLS_X509_CREATE_C) )
  2775. #error "MBEDTLS_X509_CSR_WRITE_C defined, but not all prerequisites"
  2776. #endif
  2777. /*
  2778. * Avoid warning from -pedantic. This is a convenient place for this
  2779. * workaround since this is included by every single file before the
  2780. * #if defined(MBEDTLS_xxx_C) that results in emtpy translation units.
  2781. */
  2782. typedef int mbedtls_iso_c_forbids_empty_translation_units;
  2783. #endif /* MBEDTLS_CHECK_CONFIG_H */
  2784. /********* Start of file include/mbedtls/platform.h ************/
  2785. /**
  2786. * \file platform.h
  2787. *
  2788. * \brief mbed TLS Platform abstraction layer
  2789. *
  2790. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  2791. * SPDX-License-Identifier: Apache-2.0
  2792. *
  2793. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  2794. * not use this file except in compliance with the License.
  2795. * You may obtain a copy of the License at
  2796. *
  2797. * http://www.apache.org/licenses/LICENSE-2.0
  2798. *
  2799. * Unless required by applicable law or agreed to in writing, software
  2800. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  2801. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2802. * See the License for the specific language governing permissions and
  2803. * limitations under the License.
  2804. *
  2805. * This file is part of mbed TLS (https://tls.mbed.org)
  2806. */
  2807. #ifndef MBEDTLS_PLATFORM_H
  2808. #define MBEDTLS_PLATFORM_H
  2809. #if !defined(MBEDTLS_CONFIG_FILE)
  2810. #else
  2811. #endif
  2812. #ifdef __cplusplus
  2813. extern "C" {
  2814. #endif
  2815. /**
  2816. * \name SECTION: Module settings
  2817. *
  2818. * The configuration options you can set for this module are in this section.
  2819. * Either change them in config.h or define them on the compiler command line.
  2820. * \{
  2821. */
  2822. #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
  2823. #include <stdio.h>
  2824. #include <stdlib.h>
  2825. #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
  2826. #if defined(_WIN32)
  2827. #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< Default snprintf to use */
  2828. #else
  2829. #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */
  2830. #endif
  2831. #endif
  2832. #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
  2833. #define MBEDTLS_PLATFORM_STD_PRINTF printf /**< Default printf to use */
  2834. #endif
  2835. #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
  2836. #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
  2837. #endif
  2838. #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
  2839. #define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator to use */
  2840. #endif
  2841. #if !defined(MBEDTLS_PLATFORM_STD_FREE)
  2842. #define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use */
  2843. #endif
  2844. #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
  2845. #define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default free to use */
  2846. #endif
  2847. #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
  2848. #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
  2849. #include MBEDTLS_PLATFORM_STD_MEM_HDR
  2850. #endif
  2851. #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
  2852. /* \} name SECTION: Module settings */
  2853. /*
  2854. * The function pointers for calloc and free
  2855. */
  2856. #if defined(MBEDTLS_PLATFORM_MEMORY)
  2857. #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
  2858. defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
  2859. #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
  2860. #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
  2861. #else
  2862. /* For size_t */
  2863. #include <stddef.h>
  2864. extern void * (*mbedtls_calloc)( size_t n, size_t size );
  2865. extern void (*mbedtls_free)( void *ptr );
  2866. /**
  2867. * \brief Set your own memory implementation function pointers
  2868. *
  2869. * \param calloc_func the calloc function implementation
  2870. * \param free_func the free function implementation
  2871. *
  2872. * \return 0 if successful
  2873. */
  2874. int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
  2875. void (*free_func)( void * ) );
  2876. #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
  2877. #else /* !MBEDTLS_PLATFORM_MEMORY */
  2878. #define mbedtls_free free
  2879. #define mbedtls_calloc calloc
  2880. #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
  2881. /*
  2882. * The function pointers for fprintf
  2883. */
  2884. #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
  2885. /* We need FILE * */
  2886. #include <stdio.h>
  2887. extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
  2888. /**
  2889. * \brief Set your own fprintf function pointer
  2890. *
  2891. * \param fprintf_func the fprintf function implementation
  2892. *
  2893. * \return 0
  2894. */
  2895. int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
  2896. ... ) );
  2897. #else
  2898. #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
  2899. #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
  2900. #else
  2901. #define mbedtls_fprintf fprintf
  2902. #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
  2903. #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
  2904. /*
  2905. * The function pointers for printf
  2906. */
  2907. #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
  2908. extern int (*mbedtls_printf)( const char *format, ... );
  2909. /**
  2910. * \brief Set your own printf function pointer
  2911. *
  2912. * \param printf_func the printf function implementation
  2913. *
  2914. * \return 0
  2915. */
  2916. int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
  2917. #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
  2918. #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
  2919. #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
  2920. #else
  2921. #define mbedtls_printf printf
  2922. #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
  2923. #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
  2924. /*
  2925. * The function pointers for snprintf
  2926. *
  2927. * The snprintf implementation should conform to C99:
  2928. * - it *must* always correctly zero-terminate the buffer
  2929. * (except when n == 0, then it must leave the buffer untouched)
  2930. * - however it is acceptable to return -1 instead of the required length when
  2931. * the destination buffer is too short.
  2932. */
  2933. #if defined(_WIN32)
  2934. /* For Windows (inc. MSYS2), we provide our own fixed implementation */
  2935. int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
  2936. #endif
  2937. #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
  2938. extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
  2939. /**
  2940. * \brief Set your own snprintf function pointer
  2941. *
  2942. * \param snprintf_func the snprintf function implementation
  2943. *
  2944. * \return 0
  2945. */
  2946. int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
  2947. const char * format, ... ) );
  2948. #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  2949. #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
  2950. #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
  2951. #else
  2952. #define mbedtls_snprintf snprintf
  2953. #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
  2954. #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  2955. /*
  2956. * The function pointers for exit
  2957. */
  2958. #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
  2959. extern void (*mbedtls_exit)( int status );
  2960. /**
  2961. * \brief Set your own exit function pointer
  2962. *
  2963. * \param exit_func the exit function implementation
  2964. *
  2965. * \return 0
  2966. */
  2967. int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
  2968. #else
  2969. #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
  2970. #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
  2971. #else
  2972. #define mbedtls_exit exit
  2973. #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
  2974. #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
  2975. #ifdef __cplusplus
  2976. }
  2977. #endif
  2978. #endif /* platform.h */
  2979. /********* Start of file include/mbedtls/threading.h ************/
  2980. /**
  2981. * \file threading.h
  2982. *
  2983. * \brief Threading abstraction layer
  2984. *
  2985. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  2986. * SPDX-License-Identifier: Apache-2.0
  2987. *
  2988. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  2989. * not use this file except in compliance with the License.
  2990. * You may obtain a copy of the License at
  2991. *
  2992. * http://www.apache.org/licenses/LICENSE-2.0
  2993. *
  2994. * Unless required by applicable law or agreed to in writing, software
  2995. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  2996. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2997. * See the License for the specific language governing permissions and
  2998. * limitations under the License.
  2999. *
  3000. * This file is part of mbed TLS (https://tls.mbed.org)
  3001. */
  3002. #ifndef MBEDTLS_THREADING_H
  3003. #define MBEDTLS_THREADING_H
  3004. #if !defined(MBEDTLS_CONFIG_FILE)
  3005. #else
  3006. #endif
  3007. #include <stdlib.h>
  3008. #ifdef __cplusplus
  3009. extern "C" {
  3010. #endif
  3011. #define MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE -0x001A /**< The selected feature is not available. */
  3012. #define MBEDTLS_ERR_THREADING_BAD_INPUT_DATA -0x001C /**< Bad input parameters to function. */
  3013. #define MBEDTLS_ERR_THREADING_MUTEX_ERROR -0x001E /**< Locking / unlocking / free failed with error code. */
  3014. #if defined(MBEDTLS_THREADING_PTHREAD)
  3015. #include <pthread.h>
  3016. typedef struct
  3017. {
  3018. pthread_mutex_t mutex;
  3019. char is_valid;
  3020. } mbedtls_threading_mutex_t;
  3021. #endif
  3022. #if defined(MBEDTLS_THREADING_ALT)
  3023. /* You should define the mbedtls_threading_mutex_t type in your header */
  3024. /**
  3025. * \brief Set your alternate threading implementation function
  3026. * pointers and initialize global mutexes. If used, this
  3027. * function must be called once in the main thread before any
  3028. * other mbed TLS function is called, and
  3029. * mbedtls_threading_free_alt() must be called once in the main
  3030. * thread after all other mbed TLS functions.
  3031. *
  3032. * \note mutex_init() and mutex_free() don't return a status code.
  3033. * If mutex_init() fails, it should leave its argument (the
  3034. * mutex) in a state such that mutex_lock() will fail when
  3035. * called with this argument.
  3036. *
  3037. * \param mutex_init the init function implementation
  3038. * \param mutex_free the free function implementation
  3039. * \param mutex_lock the lock function implementation
  3040. * \param mutex_unlock the unlock function implementation
  3041. */
  3042. void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
  3043. void (*mutex_free)( mbedtls_threading_mutex_t * ),
  3044. int (*mutex_lock)( mbedtls_threading_mutex_t * ),
  3045. int (*mutex_unlock)( mbedtls_threading_mutex_t * ) );
  3046. /**
  3047. * \brief Free global mutexes.
  3048. */
  3049. void mbedtls_threading_free_alt( void );
  3050. #endif /* MBEDTLS_THREADING_ALT */
  3051. #if defined(MBEDTLS_THREADING_C)
  3052. /*
  3053. * The function pointers for mutex_init, mutex_free, mutex_ and mutex_unlock
  3054. *
  3055. * All these functions are expected to work or the result will be undefined.
  3056. */
  3057. extern void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t *mutex );
  3058. extern void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t *mutex );
  3059. extern int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t *mutex );
  3060. extern int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t *mutex );
  3061. /*
  3062. * Global mutexes
  3063. */
  3064. extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex;
  3065. extern mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex;
  3066. #endif
  3067. #ifdef __cplusplus
  3068. }
  3069. #endif
  3070. #endif /* threading.h */
  3071. /********* Start of file include/mbedtls/bignum.h ************/
  3072. /**
  3073. * \file bignum.h
  3074. *
  3075. * \brief Multi-precision integer library
  3076. *
  3077. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  3078. * SPDX-License-Identifier: Apache-2.0
  3079. *
  3080. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  3081. * not use this file except in compliance with the License.
  3082. * You may obtain a copy of the License at
  3083. *
  3084. * http://www.apache.org/licenses/LICENSE-2.0
  3085. *
  3086. * Unless required by applicable law or agreed to in writing, software
  3087. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  3088. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3089. * See the License for the specific language governing permissions and
  3090. * limitations under the License.
  3091. *
  3092. * This file is part of mbed TLS (https://tls.mbed.org)
  3093. */
  3094. #ifndef MBEDTLS_BIGNUM_H
  3095. #define MBEDTLS_BIGNUM_H
  3096. #if !defined(MBEDTLS_CONFIG_FILE)
  3097. #else
  3098. #endif
  3099. #include <stddef.h>
  3100. #if !VXWORKS
  3101. #include <stdint.h>
  3102. #endif
  3103. #if defined(MBEDTLS_FS_IO)
  3104. #include <stdio.h>
  3105. #endif
  3106. #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
  3107. #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
  3108. #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
  3109. #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
  3110. #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
  3111. #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
  3112. #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
  3113. #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 /**< Memory allocation failed. */
  3114. #define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
  3115. /*
  3116. * Maximum size MPIs are allowed to grow to in number of limbs.
  3117. */
  3118. #define MBEDTLS_MPI_MAX_LIMBS 10000
  3119. #if !defined(MBEDTLS_MPI_WINDOW_SIZE)
  3120. /*
  3121. * Maximum window size used for modular exponentiation. Default: 6
  3122. * Minimum value: 1. Maximum value: 6.
  3123. *
  3124. * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
  3125. * for the sliding window calculation. (So 64 by default)
  3126. *
  3127. * Reduction in size, reduces speed.
  3128. */
  3129. #define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
  3130. #endif /* !MBEDTLS_MPI_WINDOW_SIZE */
  3131. #if !defined(MBEDTLS_MPI_MAX_SIZE)
  3132. /*
  3133. * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
  3134. * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
  3135. *
  3136. * Note: Calculations can results temporarily in larger MPIs. So the number
  3137. * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
  3138. */
  3139. #define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
  3140. #endif /* !MBEDTLS_MPI_MAX_SIZE */
  3141. #define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
  3142. /*
  3143. * When reading from files with mbedtls_mpi_read_file() and writing to files with
  3144. * mbedtls_mpi_write_file() the buffer should have space
  3145. * for a (short) label, the MPI (in the provided radix), the newline
  3146. * characters and the '\0'.
  3147. *
  3148. * By default we assume at least a 10 char label, a minimum radix of 10
  3149. * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
  3150. * Autosized at compile time for at least a 10 char label, a minimum radix
  3151. * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
  3152. *
  3153. * This used to be statically sized to 1250 for a maximum of 4096 bit
  3154. * numbers (1234 decimal chars).
  3155. *
  3156. * Calculate using the formula:
  3157. * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
  3158. * LabelSize + 6
  3159. */
  3160. #define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
  3161. #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
  3162. #define MBEDTLS_MPI_RW_BUFFER_SIZE ( ((MBEDTLS_MPI_MAX_BITS_SCALE100 + MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
  3163. /*
  3164. * Define the base integer type, architecture-wise.
  3165. *
  3166. * 32-bit integers can be forced on 64-bit arches (eg. for testing purposes)
  3167. * by defining MBEDTLS_HAVE_INT32 and undefining MBEDTLS_HAVE_ASM
  3168. */
  3169. #if ( ! defined(MBEDTLS_HAVE_INT32) && \
  3170. defined(_MSC_VER) && defined(_M_AMD64) )
  3171. #define MBEDTLS_HAVE_INT64
  3172. typedef int64_t mbedtls_mpi_sint;
  3173. typedef uint64_t mbedtls_mpi_uint;
  3174. #else
  3175. #if ( ! defined(MBEDTLS_HAVE_INT32) && \
  3176. defined(__GNUC__) && ( \
  3177. defined(__amd64__) || defined(__x86_64__) || \
  3178. defined(__ppc64__) || defined(__powerpc64__) || \
  3179. defined(__ia64__) || defined(__alpha__) || \
  3180. (defined(__sparc__) && defined(__arch64__)) || \
  3181. defined(__s390x__) || defined(__mips64) ) )
  3182. #define MBEDTLS_HAVE_INT64
  3183. typedef int64_t mbedtls_mpi_sint;
  3184. typedef uint64_t mbedtls_mpi_uint;
  3185. /* mbedtls_t_udbl defined as 128-bit unsigned int */
  3186. typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
  3187. #define MBEDTLS_HAVE_UDBL
  3188. #else
  3189. #define MBEDTLS_HAVE_INT32
  3190. typedef int32_t mbedtls_mpi_sint;
  3191. typedef uint32_t mbedtls_mpi_uint;
  3192. typedef uint64_t mbedtls_t_udbl;
  3193. #define MBEDTLS_HAVE_UDBL
  3194. #endif /* !MBEDTLS_HAVE_INT32 && __GNUC__ && 64-bit platform */
  3195. #endif /* !MBEDTLS_HAVE_INT32 && _MSC_VER && _M_AMD64 */
  3196. #ifdef __cplusplus
  3197. extern "C" {
  3198. #endif
  3199. /**
  3200. * \brief MPI structure
  3201. */
  3202. typedef struct
  3203. {
  3204. int s; /*!< integer sign */
  3205. size_t n; /*!< total # of limbs */
  3206. mbedtls_mpi_uint *p; /*!< pointer to limbs */
  3207. }
  3208. mbedtls_mpi;
  3209. /**
  3210. * \brief Initialize one MPI (make internal references valid)
  3211. * This just makes it ready to be set or freed,
  3212. * but does not define a value for the MPI.
  3213. *
  3214. * \param X One MPI to initialize.
  3215. */
  3216. void mbedtls_mpi_init( mbedtls_mpi *X );
  3217. /**
  3218. * \brief Unallocate one MPI
  3219. *
  3220. * \param X One MPI to unallocate.
  3221. */
  3222. void mbedtls_mpi_free( mbedtls_mpi *X );
  3223. /**
  3224. * \brief Enlarge to the specified number of limbs
  3225. *
  3226. * \param X MPI to grow
  3227. * \param nblimbs The target number of limbs
  3228. *
  3229. * \return 0 if successful,
  3230. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  3231. */
  3232. int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
  3233. /**
  3234. * \brief Resize down, keeping at least the specified number of limbs
  3235. *
  3236. * \param X MPI to shrink
  3237. * \param nblimbs The minimum number of limbs to keep
  3238. *
  3239. * \return 0 if successful,
  3240. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  3241. */
  3242. int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
  3243. /**
  3244. * \brief Copy the contents of Y into X
  3245. *
  3246. * \param X Destination MPI
  3247. * \param Y Source MPI
  3248. *
  3249. * \return 0 if successful,
  3250. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  3251. */
  3252. int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
  3253. /**
  3254. * \brief Swap the contents of X and Y
  3255. *
  3256. * \param X First MPI value
  3257. * \param Y Second MPI value
  3258. */
  3259. void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
  3260. /**
  3261. * \brief Safe conditional assignement X = Y if assign is 1
  3262. *
  3263. * \param X MPI to conditionally assign to
  3264. * \param Y Value to be assigned
  3265. * \param assign 1: perform the assignment, 0: keep X's original value
  3266. *
  3267. * \return 0 if successful,
  3268. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  3269. *
  3270. * \note This function is equivalent to
  3271. * if( assign ) mbedtls_mpi_copy( X, Y );
  3272. * except that it avoids leaking any information about whether
  3273. * the assignment was done or not (the above code may leak
  3274. * information through branch prediction and/or memory access
  3275. * patterns analysis).
  3276. */
  3277. int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
  3278. /**
  3279. * \brief Safe conditional swap X <-> Y if swap is 1
  3280. *
  3281. * \param X First mbedtls_mpi value
  3282. * \param Y Second mbedtls_mpi value
  3283. * \param assign 1: perform the swap, 0: keep X and Y's original values
  3284. *
  3285. * \return 0 if successful,
  3286. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  3287. *
  3288. * \note This function is equivalent to
  3289. * if( assign ) mbedtls_mpi_swap( X, Y );
  3290. * except that it avoids leaking any information about whether
  3291. * the assignment was done or not (the above code may leak
  3292. * information through branch prediction and/or memory access
  3293. * patterns analysis).
  3294. */
  3295. int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
  3296. /**
  3297. * \brief Set value from integer
  3298. *
  3299. * \param X MPI to set
  3300. * \param z Value to use
  3301. *
  3302. * \return 0 if successful,
  3303. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  3304. */
  3305. int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
  3306. /**
  3307. * \brief Get a specific bit from X
  3308. *
  3309. * \param X MPI to use
  3310. * \param pos Zero-based index of the bit in X
  3311. *
  3312. * \return Either a 0 or a 1
  3313. */
  3314. int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
  3315. /**
  3316. * \brief Set a bit of X to a specific value of 0 or 1
  3317. *
  3318. * \note Will grow X if necessary to set a bit to 1 in a not yet
  3319. * existing limb. Will not grow if bit should be set to 0
  3320. *
  3321. * \param X MPI to use
  3322. * \param pos Zero-based index of the bit in X
  3323. * \param val The value to set the bit to (0 or 1)
  3324. *
  3325. * \return 0 if successful,
  3326. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  3327. * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
  3328. */
  3329. int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
  3330. /**
  3331. * \brief Return the number of zero-bits before the least significant
  3332. * '1' bit
  3333. *
  3334. * Note: Thus also the zero-based index of the least significant '1' bit
  3335. *
  3336. * \param X MPI to use
  3337. */
  3338. size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
  3339. /**
  3340. * \brief Return the number of bits up to and including the most
  3341. * significant '1' bit'
  3342. *
  3343. * Note: Thus also the one-based index of the most significant '1' bit
  3344. *
  3345. * \param X MPI to use
  3346. */
  3347. size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
  3348. /**
  3349. * \brief Return the total size in bytes
  3350. *
  3351. * \param X MPI to use
  3352. */
  3353. size_t mbedtls_mpi_size( const mbedtls_mpi *X );
  3354. /**
  3355. * \brief Import from an ASCII string
  3356. *
  3357. * \param X Destination MPI
  3358. * \param radix Input numeric base
  3359. * \param s Null-terminated string buffer
  3360. *
  3361. * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
  3362. */
  3363. int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
  3364. /**
  3365. * \brief Export into an ASCII string
  3366. *
  3367. * \param X Source MPI
  3368. * \param radix Output numeric base
  3369. * \param buf Buffer to write the string to
  3370. * \param buflen Length of buf
  3371. * \param olen Length of the string written, including final NUL byte
  3372. *
  3373. * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code.
  3374. * *olen is always updated to reflect the amount
  3375. * of data that has (or would have) been written.
  3376. *
  3377. * \note Call this function with buflen = 0 to obtain the
  3378. * minimum required buffer size in *olen.
  3379. */
  3380. int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
  3381. char *buf, size_t buflen, size_t *olen );
  3382. #if defined(MBEDTLS_FS_IO)
  3383. /**
  3384. * \brief Read X from an opened file
  3385. *
  3386. * \param X Destination MPI
  3387. * \param radix Input numeric base
  3388. * \param fin Input file handle
  3389. *
  3390. * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
  3391. * the file read buffer is too small or a
  3392. * MBEDTLS_ERR_MPI_XXX error code
  3393. */
  3394. int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
  3395. /**
  3396. * \brief Write X into an opened file, or stdout if fout is NULL
  3397. *
  3398. * \param p Prefix, can be NULL
  3399. * \param X Source MPI
  3400. * \param radix Output numeric base
  3401. * \param fout Output file handle (can be NULL)
  3402. *
  3403. * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
  3404. *
  3405. * \note Set fout == NULL to print X on the console.
  3406. */
  3407. int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
  3408. #endif /* MBEDTLS_FS_IO */
  3409. /**
  3410. * \brief Import X from unsigned binary data, big endian
  3411. *
  3412. * \param X Destination MPI
  3413. * \param buf Input buffer
  3414. * \param buflen Input buffer size
  3415. *
  3416. * \return 0 if successful,
  3417. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  3418. */
  3419. int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
  3420. /**
  3421. * \brief Export X into unsigned binary data, big endian.
  3422. * Always fills the whole buffer, which will start with zeros
  3423. * if the number is smaller.
  3424. *
  3425. * \param X Source MPI
  3426. * \param buf Output buffer
  3427. * \param buflen Output buffer size
  3428. *
  3429. * \return 0 if successful,
  3430. * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
  3431. */
  3432. int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
  3433. /**
  3434. * \brief Left-shift: X <<= count
  3435. *
  3436. * \param X MPI to shift
  3437. * \param count Amount to shift
  3438. *
  3439. * \return 0 if successful,
  3440. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  3441. */
  3442. int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
  3443. /**
  3444. * \brief Right-shift: X >>= count
  3445. *
  3446. * \param X MPI to shift
  3447. * \param count Amount to shift
  3448. *
  3449. * \return 0 if successful,
  3450. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  3451. */
  3452. int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
  3453. /**
  3454. * \brief Compare unsigned values
  3455. *
  3456. * \param X Left-hand MPI
  3457. * \param Y Right-hand MPI
  3458. *
  3459. * \return 1 if |X| is greater than |Y|,
  3460. * -1 if |X| is lesser than |Y| or
  3461. * 0 if |X| is equal to |Y|
  3462. */
  3463. int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
  3464. /**
  3465. * \brief Compare signed values
  3466. *
  3467. * \param X Left-hand MPI
  3468. * \param Y Right-hand MPI
  3469. *
  3470. * \return 1 if X is greater than Y,
  3471. * -1 if X is lesser than Y or
  3472. * 0 if X is equal to Y
  3473. */
  3474. int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
  3475. /**
  3476. * \brief Compare signed values
  3477. *
  3478. * \param X Left-hand MPI
  3479. * \param z The integer value to compare to
  3480. *
  3481. * \return 1 if X is greater than z,
  3482. * -1 if X is lesser than z or
  3483. * 0 if X is equal to z
  3484. */
  3485. int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
  3486. /**
  3487. * \brief Unsigned addition: X = |A| + |B|
  3488. *
  3489. * \param X Destination MPI
  3490. * \param A Left-hand MPI
  3491. * \param B Right-hand MPI
  3492. *
  3493. * \return 0 if successful,
  3494. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  3495. */
  3496. int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
  3497. /**
  3498. * \brief Unsigned subtraction: X = |A| - |B|
  3499. *
  3500. * \param X Destination MPI
  3501. * \param A Left-hand MPI
  3502. * \param B Right-hand MPI
  3503. *
  3504. * \return 0 if successful,
  3505. * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
  3506. */
  3507. int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
  3508. /**
  3509. * \brief Signed addition: X = A + B
  3510. *
  3511. * \param X Destination MPI
  3512. * \param A Left-hand MPI
  3513. * \param B Right-hand MPI
  3514. *
  3515. * \return 0 if successful,
  3516. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  3517. */
  3518. int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
  3519. /**
  3520. * \brief Signed subtraction: X = A - B
  3521. *
  3522. * \param X Destination MPI
  3523. * \param A Left-hand MPI
  3524. * \param B Right-hand MPI
  3525. *
  3526. * \return 0 if successful,
  3527. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  3528. */
  3529. int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
  3530. /**
  3531. * \brief Signed addition: X = A + b
  3532. *
  3533. * \param X Destination MPI
  3534. * \param A Left-hand MPI
  3535. * \param b The integer value to add
  3536. *
  3537. * \return 0 if successful,
  3538. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  3539. */
  3540. int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
  3541. /**
  3542. * \brief Signed subtraction: X = A - b
  3543. *
  3544. * \param X Destination MPI
  3545. * \param A Left-hand MPI
  3546. * \param b The integer value to subtract
  3547. *
  3548. * \return 0 if successful,
  3549. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  3550. */
  3551. int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
  3552. /**
  3553. * \brief Baseline multiplication: X = A * B
  3554. *
  3555. * \param X Destination MPI
  3556. * \param A Left-hand MPI
  3557. * \param B Right-hand MPI
  3558. *
  3559. * \return 0 if successful,
  3560. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  3561. */
  3562. int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
  3563. /**
  3564. * \brief Baseline multiplication: X = A * b
  3565. *
  3566. * \param X Destination MPI
  3567. * \param A Left-hand MPI
  3568. * \param b The unsigned integer value to multiply with
  3569. *
  3570. * \note b is unsigned
  3571. *
  3572. * \return 0 if successful,
  3573. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  3574. */
  3575. int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
  3576. /**
  3577. * \brief Division by mbedtls_mpi: A = Q * B + R
  3578. *
  3579. * \param Q Destination MPI for the quotient
  3580. * \param R Destination MPI for the rest value
  3581. * \param A Left-hand MPI
  3582. * \param B Right-hand MPI
  3583. *
  3584. * \return 0 if successful,
  3585. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  3586. * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0
  3587. *
  3588. * \note Either Q or R can be NULL.
  3589. */
  3590. int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
  3591. /**
  3592. * \brief Division by int: A = Q * b + R
  3593. *
  3594. * \param Q Destination MPI for the quotient
  3595. * \param R Destination MPI for the rest value
  3596. * \param A Left-hand MPI
  3597. * \param b Integer to divide by
  3598. *
  3599. * \return 0 if successful,
  3600. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  3601. * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0
  3602. *
  3603. * \note Either Q or R can be NULL.
  3604. */
  3605. int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b );
  3606. /**
  3607. * \brief Modulo: R = A mod B
  3608. *
  3609. * \param R Destination MPI for the rest value
  3610. * \param A Left-hand MPI
  3611. * \param B Right-hand MPI
  3612. *
  3613. * \return 0 if successful,
  3614. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  3615. * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0,
  3616. * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
  3617. */
  3618. int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
  3619. /**
  3620. * \brief Modulo: r = A mod b
  3621. *
  3622. * \param r Destination mbedtls_mpi_uint
  3623. * \param A Left-hand MPI
  3624. * \param b Integer to divide by
  3625. *
  3626. * \return 0 if successful,
  3627. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  3628. * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0,
  3629. * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
  3630. */
  3631. int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
  3632. /**
  3633. * \brief Sliding-window exponentiation: X = A^E mod N
  3634. *
  3635. * \param X Destination MPI
  3636. * \param A Left-hand MPI
  3637. * \param E Exponent MPI
  3638. * \param N Modular MPI
  3639. * \param _RR Speed-up MPI used for recalculations
  3640. *
  3641. * \return 0 if successful,
  3642. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  3643. * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
  3644. * if E is negative
  3645. *
  3646. * \note _RR is used to avoid re-computing R*R mod N across
  3647. * multiple calls, which speeds up things a bit. It can
  3648. * be set to NULL if the extra performance is unneeded.
  3649. */
  3650. int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR );
  3651. /**
  3652. * \brief Fill an MPI X with size bytes of random
  3653. *
  3654. * \param X Destination MPI
  3655. * \param size Size in bytes
  3656. * \param f_rng RNG function
  3657. * \param p_rng RNG parameter
  3658. *
  3659. * \return 0 if successful,
  3660. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  3661. */
  3662. int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
  3663. int (*f_rng)(void *, unsigned char *, size_t),
  3664. void *p_rng );
  3665. /**
  3666. * \brief Greatest common divisor: G = gcd(A, B)
  3667. *
  3668. * \param G Destination MPI
  3669. * \param A Left-hand MPI
  3670. * \param B Right-hand MPI
  3671. *
  3672. * \return 0 if successful,
  3673. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  3674. */
  3675. int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
  3676. /**
  3677. * \brief Modular inverse: X = A^-1 mod N
  3678. *
  3679. * \param X Destination MPI
  3680. * \param A Left-hand MPI
  3681. * \param N Right-hand MPI
  3682. *
  3683. * \return 0 if successful,
  3684. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  3685. * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
  3686. MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
  3687. */
  3688. int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
  3689. /**
  3690. * \brief Miller-Rabin primality test
  3691. *
  3692. * \param X MPI to check
  3693. * \param f_rng RNG function
  3694. * \param p_rng RNG parameter
  3695. *
  3696. * \return 0 if successful (probably prime),
  3697. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  3698. * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
  3699. */
  3700. int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
  3701. int (*f_rng)(void *, unsigned char *, size_t),
  3702. void *p_rng );
  3703. /**
  3704. * \brief Prime number generation
  3705. *
  3706. * \param X Destination MPI
  3707. * \param nbits Required size of X in bits
  3708. * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
  3709. * \param dh_flag If 1, then (X-1)/2 will be prime too
  3710. * \param f_rng RNG function
  3711. * \param p_rng RNG parameter
  3712. *
  3713. * \return 0 if successful (probably prime),
  3714. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  3715. * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
  3716. */
  3717. int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
  3718. int (*f_rng)(void *, unsigned char *, size_t),
  3719. void *p_rng );
  3720. /**
  3721. * \brief Checkup routine
  3722. *
  3723. * \return 0 if successful, or 1 if the test failed
  3724. */
  3725. int mbedtls_mpi_self_test( int verbose );
  3726. #ifdef __cplusplus
  3727. }
  3728. #endif
  3729. #endif /* bignum.h */
  3730. /********* Start of file include/mbedtls/net.h ************/
  3731. /**
  3732. * \file net.h
  3733. *
  3734. * \brief Network communication functions
  3735. *
  3736. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  3737. * SPDX-License-Identifier: Apache-2.0
  3738. *
  3739. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  3740. * not use this file except in compliance with the License.
  3741. * You may obtain a copy of the License at
  3742. *
  3743. * http://www.apache.org/licenses/LICENSE-2.0
  3744. *
  3745. * Unless required by applicable law or agreed to in writing, software
  3746. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  3747. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3748. * See the License for the specific language governing permissions and
  3749. * limitations under the License.
  3750. *
  3751. * This file is part of mbed TLS (https://tls.mbed.org)
  3752. */
  3753. #ifndef MBEDTLS_NET_H
  3754. #define MBEDTLS_NET_H
  3755. #if !defined(MBEDTLS_CONFIG_FILE)
  3756. #else
  3757. #endif
  3758. #include <stddef.h>
  3759. #if !VXWORKS
  3760. #include <stdint.h>
  3761. #endif
  3762. #define MBEDTLS_ERR_NET_SOCKET_FAILED -0x0042 /**< Failed to open a socket. */
  3763. #define MBEDTLS_ERR_NET_CONNECT_FAILED -0x0044 /**< The connection to the given server / port failed. */
  3764. #define MBEDTLS_ERR_NET_BIND_FAILED -0x0046 /**< Binding of the socket failed. */
  3765. #define MBEDTLS_ERR_NET_LISTEN_FAILED -0x0048 /**< Could not listen on the socket. */
  3766. #define MBEDTLS_ERR_NET_ACCEPT_FAILED -0x004A /**< Could not accept the incoming connection. */
  3767. #define MBEDTLS_ERR_NET_RECV_FAILED -0x004C /**< Reading information from the socket failed. */
  3768. #define MBEDTLS_ERR_NET_SEND_FAILED -0x004E /**< Sending information through the socket failed. */
  3769. #define MBEDTLS_ERR_NET_CONN_RESET -0x0050 /**< Connection was reset by peer. */
  3770. #define MBEDTLS_ERR_NET_UNKNOWN_HOST -0x0052 /**< Failed to get an IP address for the given hostname. */
  3771. #define MBEDTLS_ERR_NET_BUFFER_TOO_SMALL -0x0043 /**< Buffer is too small to hold the data. */
  3772. #define MBEDTLS_ERR_NET_INVALID_CONTEXT -0x0045 /**< The context is invalid, eg because it was free()ed. */
  3773. #define MBEDTLS_NET_LISTEN_BACKLOG 10 /**< The backlog that listen() should use. */
  3774. #define MBEDTLS_NET_PROTO_TCP 0 /**< The TCP transport protocol */
  3775. #define MBEDTLS_NET_PROTO_UDP 1 /**< The UDP transport protocol */
  3776. #ifdef __cplusplus
  3777. extern "C" {
  3778. #endif
  3779. /**
  3780. * Wrapper type for sockets.
  3781. *
  3782. * Currently backed by just a file descriptor, but might be more in the future
  3783. * (eg two file descriptors for combined IPv4 + IPv6 support, or additional
  3784. * structures for hand-made UDP demultiplexing).
  3785. */
  3786. typedef struct
  3787. {
  3788. int fd; /**< The underlying file descriptor */
  3789. }
  3790. mbedtls_net_context;
  3791. /**
  3792. * \brief Initialize a context
  3793. * Just makes the context ready to be used or freed safely.
  3794. *
  3795. * \param ctx Context to initialize
  3796. */
  3797. void mbedtls_net_init( mbedtls_net_context *ctx );
  3798. /**
  3799. * \brief Initiate a connection with host:port in the given protocol
  3800. *
  3801. * \param ctx Socket to use
  3802. * \param host Host to connect to
  3803. * \param port Port to connect to
  3804. * \param proto Protocol: MBEDTLS_NET_PROTO_TCP or MBEDTLS_NET_PROTO_UDP
  3805. *
  3806. * \return 0 if successful, or one of:
  3807. * MBEDTLS_ERR_NET_SOCKET_FAILED,
  3808. * MBEDTLS_ERR_NET_UNKNOWN_HOST,
  3809. * MBEDTLS_ERR_NET_CONNECT_FAILED
  3810. *
  3811. * \note Sets the socket in connected mode even with UDP.
  3812. */
  3813. int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char *port, int proto );
  3814. /**
  3815. * \brief Create a receiving socket on bind_ip:port in the chosen
  3816. * protocol. If bind_ip == NULL, all interfaces are bound.
  3817. *
  3818. * \param ctx Socket to use
  3819. * \param bind_ip IP to bind to, can be NULL
  3820. * \param port Port number to use
  3821. * \param proto Protocol: MBEDTLS_NET_PROTO_TCP or MBEDTLS_NET_PROTO_UDP
  3822. *
  3823. * \return 0 if successful, or one of:
  3824. * MBEDTLS_ERR_NET_SOCKET_FAILED,
  3825. * MBEDTLS_ERR_NET_BIND_FAILED,
  3826. * MBEDTLS_ERR_NET_LISTEN_FAILED
  3827. *
  3828. * \note Regardless of the protocol, opens the sockets and binds it.
  3829. * In addition, make the socket listening if protocol is TCP.
  3830. */
  3831. int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto );
  3832. /**
  3833. * \brief Accept a connection from a remote client
  3834. *
  3835. * \param bind_ctx Relevant socket
  3836. * \param client_ctx Will contain the connected client socket
  3837. * \param client_ip Will contain the client IP address
  3838. * \param buf_size Size of the client_ip buffer
  3839. * \param ip_len Will receive the size of the client IP written
  3840. *
  3841. * \return 0 if successful, or
  3842. * MBEDTLS_ERR_NET_ACCEPT_FAILED, or
  3843. * MBEDTLS_ERR_NET_BUFFER_TOO_SMALL if buf_size is too small,
  3844. * MBEDTLS_ERR_SSL_WANT_READ if bind_fd was set to
  3845. * non-blocking and accept() would block.
  3846. */
  3847. int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
  3848. mbedtls_net_context *client_ctx,
  3849. void *client_ip, size_t buf_size, size_t *ip_len );
  3850. /**
  3851. * \brief Set the socket blocking
  3852. *
  3853. * \param ctx Socket to set
  3854. *
  3855. * \return 0 if successful, or a non-zero error code
  3856. */
  3857. int mbedtls_net_set_block( mbedtls_net_context *ctx );
  3858. /**
  3859. * \brief Set the socket non-blocking
  3860. *
  3861. * \param ctx Socket to set
  3862. *
  3863. * \return 0 if successful, or a non-zero error code
  3864. */
  3865. int mbedtls_net_set_nonblock( mbedtls_net_context *ctx );
  3866. /**
  3867. * \brief Portable usleep helper
  3868. *
  3869. * \param usec Amount of microseconds to sleep
  3870. *
  3871. * \note Real amount of time slept will not be less than
  3872. * select()'s timeout granularity (typically, 10ms).
  3873. */
  3874. void mbedtls_net_usleep( unsigned long usec );
  3875. /**
  3876. * \brief Read at most 'len' characters. If no error occurs,
  3877. * the actual amount read is returned.
  3878. *
  3879. * \param ctx Socket
  3880. * \param buf The buffer to write to
  3881. * \param len Maximum length of the buffer
  3882. *
  3883. * \return the number of bytes received,
  3884. * or a non-zero error code; with a non-blocking socket,
  3885. * MBEDTLS_ERR_SSL_WANT_READ indicates read() would block.
  3886. */
  3887. int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len );
  3888. /**
  3889. * \brief Write at most 'len' characters. If no error occurs,
  3890. * the actual amount read is returned.
  3891. *
  3892. * \param ctx Socket
  3893. * \param buf The buffer to read from
  3894. * \param len The length of the buffer
  3895. *
  3896. * \return the number of bytes sent,
  3897. * or a non-zero error code; with a non-blocking socket,
  3898. * MBEDTLS_ERR_SSL_WANT_WRITE indicates write() would block.
  3899. */
  3900. int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len );
  3901. /**
  3902. * \brief Read at most 'len' characters, blocking for at most
  3903. * 'timeout' seconds. If no error occurs, the actual amount
  3904. * read is returned.
  3905. *
  3906. * \param ctx Socket
  3907. * \param buf The buffer to write to
  3908. * \param len Maximum length of the buffer
  3909. * \param timeout Maximum number of milliseconds to wait for data
  3910. * 0 means no timeout (wait forever)
  3911. *
  3912. * \return the number of bytes received,
  3913. * or a non-zero error code:
  3914. * MBEDTLS_ERR_SSL_TIMEOUT if the operation timed out,
  3915. * MBEDTLS_ERR_SSL_WANT_READ if interrupted by a signal.
  3916. *
  3917. * \note This function will block (until data becomes available or
  3918. * timeout is reached) even if the socket is set to
  3919. * non-blocking. Handling timeouts with non-blocking reads
  3920. * requires a different strategy.
  3921. */
  3922. int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
  3923. uint32_t timeout );
  3924. /**
  3925. * \brief Gracefully shutdown the connection and free associated data
  3926. *
  3927. * \param ctx The context to free
  3928. */
  3929. void mbedtls_net_free( mbedtls_net_context *ctx );
  3930. #ifdef __cplusplus
  3931. }
  3932. #endif
  3933. #endif /* net.h */
  3934. /********* Start of file include/mbedtls/dhm.h ************/
  3935. /**
  3936. * \file dhm.h
  3937. *
  3938. * \brief Diffie-Hellman-Merkle key exchange
  3939. *
  3940. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  3941. * SPDX-License-Identifier: Apache-2.0
  3942. *
  3943. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  3944. * not use this file except in compliance with the License.
  3945. * You may obtain a copy of the License at
  3946. *
  3947. * http://www.apache.org/licenses/LICENSE-2.0
  3948. *
  3949. * Unless required by applicable law or agreed to in writing, software
  3950. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  3951. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3952. * See the License for the specific language governing permissions and
  3953. * limitations under the License.
  3954. *
  3955. * This file is part of mbed TLS (https://tls.mbed.org)
  3956. */
  3957. #ifndef MBEDTLS_DHM_H
  3958. #define MBEDTLS_DHM_H
  3959. /*
  3960. * DHM Error codes
  3961. */
  3962. #define MBEDTLS_ERR_DHM_BAD_INPUT_DATA -0x3080 /**< Bad input parameters to function. */
  3963. #define MBEDTLS_ERR_DHM_READ_PARAMS_FAILED -0x3100 /**< Reading of the DHM parameters failed. */
  3964. #define MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED -0x3180 /**< Making of the DHM parameters failed. */
  3965. #define MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED -0x3200 /**< Reading of the public values failed. */
  3966. #define MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED -0x3280 /**< Making of the public value failed. */
  3967. #define MBEDTLS_ERR_DHM_CALC_SECRET_FAILED -0x3300 /**< Calculation of the DHM secret failed. */
  3968. #define MBEDTLS_ERR_DHM_INVALID_FORMAT -0x3380 /**< The ASN.1 data is not formatted correctly. */
  3969. #define MBEDTLS_ERR_DHM_ALLOC_FAILED -0x3400 /**< Allocation of memory failed. */
  3970. #define MBEDTLS_ERR_DHM_FILE_IO_ERROR -0x3480 /**< Read/write of file failed. */
  3971. /**
  3972. * RFC 3526 defines a number of standardized Diffie-Hellman groups
  3973. * for IKE.
  3974. * RFC 5114 defines a number of standardized Diffie-Hellman groups
  3975. * that can be used.
  3976. *
  3977. * Some are included here for convenience.
  3978. *
  3979. * Included are:
  3980. * RFC 3526 3. 2048-bit MODP Group
  3981. * RFC 3526 4. 3072-bit MODP Group
  3982. * RFC 3526 5. 4096-bit MODP Group
  3983. * RFC 5114 2.2. 2048-bit MODP Group with 224-bit Prime Order Subgroup
  3984. */
  3985. #define MBEDTLS_DHM_RFC3526_MODP_2048_P \
  3986. "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
  3987. "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
  3988. "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \
  3989. "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
  3990. "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \
  3991. "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \
  3992. "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \
  3993. "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \
  3994. "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \
  3995. "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \
  3996. "15728E5A8AACAA68FFFFFFFFFFFFFFFF"
  3997. #define MBEDTLS_DHM_RFC3526_MODP_2048_G "02"
  3998. #define MBEDTLS_DHM_RFC3526_MODP_3072_P \
  3999. "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
  4000. "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
  4001. "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \
  4002. "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
  4003. "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \
  4004. "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \
  4005. "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \
  4006. "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \
  4007. "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \
  4008. "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \
  4009. "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" \
  4010. "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" \
  4011. "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" \
  4012. "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" \
  4013. "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" \
  4014. "43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF"
  4015. #define MBEDTLS_DHM_RFC3526_MODP_3072_G "02"
  4016. #define MBEDTLS_DHM_RFC3526_MODP_4096_P \
  4017. "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
  4018. "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
  4019. "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \
  4020. "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
  4021. "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \
  4022. "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \
  4023. "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \
  4024. "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \
  4025. "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \
  4026. "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \
  4027. "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" \
  4028. "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" \
  4029. "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" \
  4030. "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" \
  4031. "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" \
  4032. "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" \
  4033. "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" \
  4034. "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" \
  4035. "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" \
  4036. "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" \
  4037. "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199" \
  4038. "FFFFFFFFFFFFFFFF"
  4039. #define MBEDTLS_DHM_RFC3526_MODP_4096_G "02"
  4040. #define MBEDTLS_DHM_RFC5114_MODP_2048_P \
  4041. "AD107E1E9123A9D0D660FAA79559C51FA20D64E5683B9FD1" \
  4042. "B54B1597B61D0A75E6FA141DF95A56DBAF9A3C407BA1DF15" \
  4043. "EB3D688A309C180E1DE6B85A1274A0A66D3F8152AD6AC212" \
  4044. "9037C9EDEFDA4DF8D91E8FEF55B7394B7AD5B7D0B6C12207" \
  4045. "C9F98D11ED34DBF6C6BA0B2C8BBC27BE6A00E0A0B9C49708" \
  4046. "B3BF8A317091883681286130BC8985DB1602E714415D9330" \
  4047. "278273C7DE31EFDC7310F7121FD5A07415987D9ADC0A486D" \
  4048. "CDF93ACC44328387315D75E198C641A480CD86A1B9E587E8" \
  4049. "BE60E69CC928B2B9C52172E413042E9B23F10B0E16E79763" \
  4050. "C9B53DCF4BA80A29E3FB73C16B8E75B97EF363E2FFA31F71" \
  4051. "CF9DE5384E71B81C0AC4DFFE0C10E64F"
  4052. #define MBEDTLS_DHM_RFC5114_MODP_2048_G \
  4053. "AC4032EF4F2D9AE39DF30B5C8FFDAC506CDEBE7B89998CAF"\
  4054. "74866A08CFE4FFE3A6824A4E10B9A6F0DD921F01A70C4AFA"\
  4055. "AB739D7700C29F52C57DB17C620A8652BE5E9001A8D66AD7"\
  4056. "C17669101999024AF4D027275AC1348BB8A762D0521BC98A"\
  4057. "E247150422EA1ED409939D54DA7460CDB5F6C6B250717CBE"\
  4058. "F180EB34118E98D119529A45D6F834566E3025E316A330EF"\
  4059. "BB77A86F0C1AB15B051AE3D428C8F8ACB70A8137150B8EEB"\
  4060. "10E183EDD19963DDD9E263E4770589EF6AA21E7F5F2FF381"\
  4061. "B539CCE3409D13CD566AFBB48D6C019181E1BCFE94B30269"\
  4062. "EDFE72FE9B6AA4BD7B5A0F1C71CFFF4C19C418E1F6EC0179"\
  4063. "81BC087F2A7065B384B890D3191F2BFA"
  4064. #ifdef __cplusplus
  4065. extern "C" {
  4066. #endif
  4067. /**
  4068. * \brief DHM context structure
  4069. */
  4070. typedef struct
  4071. {
  4072. size_t len; /*!< size(P) in chars */
  4073. mbedtls_mpi P; /*!< prime modulus */
  4074. mbedtls_mpi G; /*!< generator */
  4075. mbedtls_mpi X; /*!< secret value */
  4076. mbedtls_mpi GX; /*!< self = G^X mod P */
  4077. mbedtls_mpi GY; /*!< peer = G^Y mod P */
  4078. mbedtls_mpi K; /*!< key = GY^X mod P */
  4079. mbedtls_mpi RP; /*!< cached R^2 mod P */
  4080. mbedtls_mpi Vi; /*!< blinding value */
  4081. mbedtls_mpi Vf; /*!< un-blinding value */
  4082. mbedtls_mpi pX; /*!< previous X */
  4083. }
  4084. mbedtls_dhm_context;
  4085. /**
  4086. * \brief Initialize DHM context
  4087. *
  4088. * \param ctx DHM context to be initialized
  4089. */
  4090. void mbedtls_dhm_init( mbedtls_dhm_context *ctx );
  4091. /**
  4092. * \brief Parse the ServerKeyExchange parameters
  4093. *
  4094. * \param ctx DHM context
  4095. * \param p &(start of input buffer)
  4096. * \param end end of buffer
  4097. *
  4098. * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
  4099. */
  4100. int mbedtls_dhm_read_params( mbedtls_dhm_context *ctx,
  4101. unsigned char **p,
  4102. const unsigned char *end );
  4103. /**
  4104. * \brief Setup and write the ServerKeyExchange parameters
  4105. *
  4106. * \param ctx DHM context
  4107. * \param x_size private value size in bytes
  4108. * \param output destination buffer
  4109. * \param olen number of chars written
  4110. * \param f_rng RNG function
  4111. * \param p_rng RNG parameter
  4112. *
  4113. * \note This function assumes that ctx->P and ctx->G
  4114. * have already been properly set (for example
  4115. * using mbedtls_mpi_read_string or mbedtls_mpi_read_binary).
  4116. *
  4117. * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
  4118. */
  4119. int mbedtls_dhm_make_params( mbedtls_dhm_context *ctx, int x_size,
  4120. unsigned char *output, size_t *olen,
  4121. int (*f_rng)(void *, unsigned char *, size_t),
  4122. void *p_rng );
  4123. /**
  4124. * \brief Import the peer's public value G^Y
  4125. *
  4126. * \param ctx DHM context
  4127. * \param input input buffer
  4128. * \param ilen size of buffer
  4129. *
  4130. * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
  4131. */
  4132. int mbedtls_dhm_read_public( mbedtls_dhm_context *ctx,
  4133. const unsigned char *input, size_t ilen );
  4134. /**
  4135. * \brief Create own private value X and export G^X
  4136. *
  4137. * \param ctx DHM context
  4138. * \param x_size private value size in bytes
  4139. * \param output destination buffer
  4140. * \param olen must be equal to ctx->P.len
  4141. * \param f_rng RNG function
  4142. * \param p_rng RNG parameter
  4143. *
  4144. * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
  4145. */
  4146. int mbedtls_dhm_make_public( mbedtls_dhm_context *ctx, int x_size,
  4147. unsigned char *output, size_t olen,
  4148. int (*f_rng)(void *, unsigned char *, size_t),
  4149. void *p_rng );
  4150. /**
  4151. * \brief Derive and export the shared secret (G^Y)^X mod P
  4152. *
  4153. * \param ctx DHM context
  4154. * \param output destination buffer
  4155. * \param output_size size of the destination buffer
  4156. * \param olen on exit, holds the actual number of bytes written
  4157. * \param f_rng RNG function, for blinding purposes
  4158. * \param p_rng RNG parameter
  4159. *
  4160. * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
  4161. *
  4162. * \note If non-NULL, f_rng is used to blind the input as
  4163. * countermeasure against timing attacks. Blinding is
  4164. * automatically used if and only if our secret value X is
  4165. * re-used and costs nothing otherwise, so it is recommended
  4166. * to always pass a non-NULL f_rng argument.
  4167. */
  4168. int mbedtls_dhm_calc_secret( mbedtls_dhm_context *ctx,
  4169. unsigned char *output, size_t output_size, size_t *olen,
  4170. int (*f_rng)(void *, unsigned char *, size_t),
  4171. void *p_rng );
  4172. /**
  4173. * \brief Free and clear the components of a DHM key
  4174. *
  4175. * \param ctx DHM context to free and clear
  4176. */
  4177. void mbedtls_dhm_free( mbedtls_dhm_context *ctx );
  4178. #if defined(MBEDTLS_ASN1_PARSE_C)
  4179. /** \ingroup x509_module */
  4180. /**
  4181. * \brief Parse DHM parameters in PEM or DER format
  4182. *
  4183. * \param dhm DHM context to be initialized
  4184. * \param dhmin input buffer
  4185. * \param dhminlen size of the buffer
  4186. * (including the terminating null byte for PEM data)
  4187. *
  4188. * \return 0 if successful, or a specific DHM or PEM error code
  4189. */
  4190. int mbedtls_dhm_parse_dhm( mbedtls_dhm_context *dhm, const unsigned char *dhmin,
  4191. size_t dhminlen );
  4192. #if defined(MBEDTLS_FS_IO)
  4193. /** \ingroup x509_module */
  4194. /**
  4195. * \brief Load and parse DHM parameters
  4196. *
  4197. * \param dhm DHM context to be initialized
  4198. * \param path filename to read the DHM Parameters from
  4199. *
  4200. * \return 0 if successful, or a specific DHM or PEM error code
  4201. */
  4202. int mbedtls_dhm_parse_dhmfile( mbedtls_dhm_context *dhm, const char *path );
  4203. #endif /* MBEDTLS_FS_IO */
  4204. #endif /* MBEDTLS_ASN1_PARSE_C */
  4205. /**
  4206. * \brief Checkup routine
  4207. *
  4208. * \return 0 if successful, or 1 if the test failed
  4209. */
  4210. int mbedtls_dhm_self_test( int verbose );
  4211. #ifdef __cplusplus
  4212. }
  4213. #endif
  4214. #endif /* dhm.h */
  4215. /********* Start of file include/mbedtls/error.h ************/
  4216. /**
  4217. * \file error.h
  4218. *
  4219. * \brief Error to string translation
  4220. *
  4221. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  4222. * SPDX-License-Identifier: Apache-2.0
  4223. *
  4224. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  4225. * not use this file except in compliance with the License.
  4226. * You may obtain a copy of the License at
  4227. *
  4228. * http://www.apache.org/licenses/LICENSE-2.0
  4229. *
  4230. * Unless required by applicable law or agreed to in writing, software
  4231. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  4232. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4233. * See the License for the specific language governing permissions and
  4234. * limitations under the License.
  4235. *
  4236. * This file is part of mbed TLS (https://tls.mbed.org)
  4237. */
  4238. #ifndef MBEDTLS_ERROR_H
  4239. #define MBEDTLS_ERROR_H
  4240. #include <stddef.h>
  4241. /**
  4242. * Error code layout.
  4243. *
  4244. * Currently we try to keep all error codes within the negative space of 16
  4245. * bits signed integers to support all platforms (-0x0001 - -0x7FFF). In
  4246. * addition we'd like to give two layers of information on the error if
  4247. * possible.
  4248. *
  4249. * For that purpose the error codes are segmented in the following manner:
  4250. *
  4251. * 16 bit error code bit-segmentation
  4252. *
  4253. * 1 bit - Unused (sign bit)
  4254. * 3 bits - High level module ID
  4255. * 5 bits - Module-dependent error code
  4256. * 7 bits - Low level module errors
  4257. *
  4258. * For historical reasons, low-level error codes are divided in even and odd,
  4259. * even codes were assigned first, and -1 is reserved for other errors.
  4260. *
  4261. * Low-level module errors (0x0002-0x007E, 0x0003-0x007F)
  4262. *
  4263. * Module Nr Codes assigned
  4264. * MPI 7 0x0002-0x0010
  4265. * GCM 2 0x0012-0x0014
  4266. * BLOWFISH 2 0x0016-0x0018
  4267. * THREADING 3 0x001A-0x001E
  4268. * AES 2 0x0020-0x0022
  4269. * CAMELLIA 2 0x0024-0x0026
  4270. * XTEA 1 0x0028-0x0028
  4271. * BASE64 2 0x002A-0x002C
  4272. * OID 1 0x002E-0x002E 0x000B-0x000B
  4273. * PADLOCK 1 0x0030-0x0030
  4274. * DES 1 0x0032-0x0032
  4275. * CTR_DBRG 4 0x0034-0x003A
  4276. * ENTROPY 3 0x003C-0x0040 0x003D-0x003F
  4277. * NET 11 0x0042-0x0052 0x0043-0x0045
  4278. * ASN1 7 0x0060-0x006C
  4279. * PBKDF2 1 0x007C-0x007C
  4280. * HMAC_DRBG 4 0x0003-0x0009
  4281. * CCM 2 0x000D-0x000F
  4282. *
  4283. * High-level module nr (3 bits - 0x0...-0x7...)
  4284. * Name ID Nr of Errors
  4285. * PEM 1 9
  4286. * PKCS#12 1 4 (Started from top)
  4287. * X509 2 19
  4288. * PKCS5 2 4 (Started from top)
  4289. * DHM 3 9
  4290. * PK 3 14 (Started from top)
  4291. * RSA 4 9
  4292. * ECP 4 8 (Started from top)
  4293. * MD 5 4
  4294. * CIPHER 6 6
  4295. * SSL 6 17 (Started from top)
  4296. * SSL 7 31
  4297. *
  4298. * Module dependent error code (5 bits 0x.00.-0x.F8.)
  4299. */
  4300. #ifdef __cplusplus
  4301. extern "C" {
  4302. #endif
  4303. /**
  4304. * \brief Translate a mbed TLS error code into a string representation,
  4305. * Result is truncated if necessary and always includes a terminating
  4306. * null byte.
  4307. *
  4308. * \param errnum error code
  4309. * \param buffer buffer to place representation in
  4310. * \param buflen length of the buffer
  4311. */
  4312. void mbedtls_strerror( int errnum, char *buffer, size_t buflen );
  4313. #ifdef __cplusplus
  4314. }
  4315. #endif
  4316. #endif /* error.h */
  4317. /********* Start of file include/mbedtls/md.h ************/
  4318. /**
  4319. * \file md.h
  4320. *
  4321. * \brief Generic message digest wrapper
  4322. *
  4323. * \author Adriaan de Jong <dejong@fox-it.com>
  4324. *
  4325. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  4326. * SPDX-License-Identifier: Apache-2.0
  4327. *
  4328. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  4329. * not use this file except in compliance with the License.
  4330. * You may obtain a copy of the License at
  4331. *
  4332. * http://www.apache.org/licenses/LICENSE-2.0
  4333. *
  4334. * Unless required by applicable law or agreed to in writing, software
  4335. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  4336. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4337. * See the License for the specific language governing permissions and
  4338. * limitations under the License.
  4339. *
  4340. * This file is part of mbed TLS (https://tls.mbed.org)
  4341. */
  4342. #ifndef MBEDTLS_MD_H
  4343. #define MBEDTLS_MD_H
  4344. #include <stddef.h>
  4345. #define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
  4346. #define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
  4347. #define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
  4348. #define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */
  4349. #ifdef __cplusplus
  4350. extern "C" {
  4351. #endif
  4352. typedef enum {
  4353. MBEDTLS_MD_NONE=0,
  4354. MBEDTLS_MD_MD2,
  4355. MBEDTLS_MD_MD4,
  4356. MBEDTLS_MD_MD5,
  4357. MBEDTLS_MD_SHA1,
  4358. MBEDTLS_MD_SHA224,
  4359. MBEDTLS_MD_SHA256,
  4360. MBEDTLS_MD_SHA384,
  4361. MBEDTLS_MD_SHA512,
  4362. MBEDTLS_MD_RIPEMD160,
  4363. } mbedtls_md_type_t;
  4364. #if defined(MBEDTLS_SHA512_C)
  4365. #define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
  4366. #else
  4367. #define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
  4368. #endif
  4369. /**
  4370. * Opaque struct defined in md_internal.h
  4371. */
  4372. typedef struct mbedtls_md_info_t mbedtls_md_info_t;
  4373. /**
  4374. * Generic message digest context.
  4375. */
  4376. typedef struct {
  4377. /** Information about the associated message digest */
  4378. const mbedtls_md_info_t *md_info;
  4379. /** Digest-specific context */
  4380. void *md_ctx;
  4381. /** HMAC part of the context */
  4382. void *hmac_ctx;
  4383. } mbedtls_md_context_t;
  4384. /**
  4385. * \brief Returns the list of digests supported by the generic digest module.
  4386. *
  4387. * \return a statically allocated array of digests, the last entry
  4388. * is 0.
  4389. */
  4390. const int *mbedtls_md_list( void );
  4391. /**
  4392. * \brief Returns the message digest information associated with the
  4393. * given digest name.
  4394. *
  4395. * \param md_name Name of the digest to search for.
  4396. *
  4397. * \return The message digest information associated with md_name or
  4398. * NULL if not found.
  4399. */
  4400. const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name );
  4401. /**
  4402. * \brief Returns the message digest information associated with the
  4403. * given digest type.
  4404. *
  4405. * \param md_type type of digest to search for.
  4406. *
  4407. * \return The message digest information associated with md_type or
  4408. * NULL if not found.
  4409. */
  4410. const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type );
  4411. /**
  4412. * \brief Initialize a md_context (as NONE)
  4413. * This should always be called first.
  4414. * Prepares the context for mbedtls_md_setup() or mbedtls_md_free().
  4415. */
  4416. void mbedtls_md_init( mbedtls_md_context_t *ctx );
  4417. /**
  4418. * \brief Free and clear the internal structures of ctx.
  4419. * Can be called at any time after mbedtls_md_init().
  4420. * Mandatory once mbedtls_md_setup() has been called.
  4421. */
  4422. void mbedtls_md_free( mbedtls_md_context_t *ctx );
  4423. #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
  4424. #if defined(MBEDTLS_DEPRECATED_WARNING)
  4425. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  4426. #else
  4427. #define MBEDTLS_DEPRECATED
  4428. #endif
  4429. /**
  4430. * \brief Select MD to use and allocate internal structures.
  4431. * Should be called after mbedtls_md_init() or mbedtls_md_free().
  4432. * Makes it necessary to call mbedtls_md_free() later.
  4433. *
  4434. * \deprecated Superseded by mbedtls_md_setup() in 2.0.0
  4435. *
  4436. * \param ctx Context to set up.
  4437. * \param md_info Message digest to use.
  4438. *
  4439. * \returns \c 0 on success,
  4440. * \c MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure,
  4441. * \c MBEDTLS_ERR_MD_ALLOC_FAILED memory allocation failure.
  4442. */
  4443. int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info ) MBEDTLS_DEPRECATED;
  4444. #undef MBEDTLS_DEPRECATED
  4445. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  4446. /**
  4447. * \brief Select MD to use and allocate internal structures.
  4448. * Should be called after mbedtls_md_init() or mbedtls_md_free().
  4449. * Makes it necessary to call mbedtls_md_free() later.
  4450. *
  4451. * \param ctx Context to set up.
  4452. * \param md_info Message digest to use.
  4453. * \param hmac 0 to save some memory if HMAC will not be used,
  4454. * non-zero is HMAC is going to be used with this context.
  4455. *
  4456. * \returns \c 0 on success,
  4457. * \c MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure,
  4458. * \c MBEDTLS_ERR_MD_ALLOC_FAILED memory allocation failure.
  4459. */
  4460. int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac );
  4461. /**
  4462. * \brief Clone the state of an MD context
  4463. *
  4464. * \note The two contexts must have been setup to the same type
  4465. * (cloning from SHA-256 to SHA-512 make no sense).
  4466. *
  4467. * \warning Only clones the MD state, not the HMAC state! (for now)
  4468. *
  4469. * \param dst The destination context
  4470. * \param src The context to be cloned
  4471. *
  4472. * \return \c 0 on success,
  4473. * \c MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure.
  4474. */
  4475. int mbedtls_md_clone( mbedtls_md_context_t *dst,
  4476. const mbedtls_md_context_t *src );
  4477. /**
  4478. * \brief Returns the size of the message digest output.
  4479. *
  4480. * \param md_info message digest info
  4481. *
  4482. * \return size of the message digest output in bytes.
  4483. */
  4484. unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info );
  4485. /**
  4486. * \brief Returns the type of the message digest output.
  4487. *
  4488. * \param md_info message digest info
  4489. *
  4490. * \return type of the message digest output.
  4491. */
  4492. mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info );
  4493. /**
  4494. * \brief Returns the name of the message digest output.
  4495. *
  4496. * \param md_info message digest info
  4497. *
  4498. * \return name of the message digest output.
  4499. */
  4500. const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info );
  4501. /**
  4502. * \brief Prepare the context to digest a new message.
  4503. * Generally called after mbedtls_md_setup() or mbedtls_md_finish().
  4504. * Followed by mbedtls_md_update().
  4505. *
  4506. * \param ctx generic message digest context.
  4507. *
  4508. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  4509. * verification fails.
  4510. */
  4511. int mbedtls_md_starts( mbedtls_md_context_t *ctx );
  4512. /**
  4513. * \brief Generic message digest process buffer
  4514. * Called between mbedtls_md_starts() and mbedtls_md_finish().
  4515. * May be called repeatedly.
  4516. *
  4517. * \param ctx Generic message digest context
  4518. * \param input buffer holding the datal
  4519. * \param ilen length of the input data
  4520. *
  4521. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  4522. * verification fails.
  4523. */
  4524. int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen );
  4525. /**
  4526. * \brief Generic message digest final digest
  4527. * Called after mbedtls_md_update().
  4528. * Usually followed by mbedtls_md_free() or mbedtls_md_starts().
  4529. *
  4530. * \param ctx Generic message digest context
  4531. * \param output Generic message digest checksum result
  4532. *
  4533. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  4534. * verification fails.
  4535. */
  4536. int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output );
  4537. /**
  4538. * \brief Output = message_digest( input buffer )
  4539. *
  4540. * \param md_info message digest info
  4541. * \param input buffer holding the data
  4542. * \param ilen length of the input data
  4543. * \param output Generic message digest checksum result
  4544. *
  4545. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  4546. * verification fails.
  4547. */
  4548. int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
  4549. unsigned char *output );
  4550. #if defined(MBEDTLS_FS_IO)
  4551. /**
  4552. * \brief Output = message_digest( file contents )
  4553. *
  4554. * \param md_info message digest info
  4555. * \param path input file name
  4556. * \param output generic message digest checksum result
  4557. *
  4558. * \return 0 if successful,
  4559. * MBEDTLS_ERR_MD_FILE_IO_ERROR if file input failed,
  4560. * MBEDTLS_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
  4561. */
  4562. int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path,
  4563. unsigned char *output );
  4564. #endif /* MBEDTLS_FS_IO */
  4565. /**
  4566. * \brief Set HMAC key and prepare to authenticate a new message.
  4567. * Usually called after mbedtls_md_setup() or mbedtls_md_hmac_finish().
  4568. *
  4569. * \param ctx HMAC context
  4570. * \param key HMAC secret key
  4571. * \param keylen length of the HMAC key in bytes
  4572. *
  4573. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  4574. * verification fails.
  4575. */
  4576. int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key,
  4577. size_t keylen );
  4578. /**
  4579. * \brief Generic HMAC process buffer.
  4580. * Called between mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
  4581. * and mbedtls_md_hmac_finish().
  4582. * May be called repeatedly.
  4583. *
  4584. * \param ctx HMAC context
  4585. * \param input buffer holding the data
  4586. * \param ilen length of the input data
  4587. *
  4588. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  4589. * verification fails.
  4590. */
  4591. int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input,
  4592. size_t ilen );
  4593. /**
  4594. * \brief Output HMAC.
  4595. * Called after mbedtls_md_hmac_update().
  4596. * Usually followed my mbedtls_md_hmac_reset(), mbedtls_md_hmac_starts(),
  4597. * or mbedtls_md_free().
  4598. *
  4599. * \param ctx HMAC context
  4600. * \param output Generic HMAC checksum result
  4601. *
  4602. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  4603. * verification fails.
  4604. */
  4605. int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output);
  4606. /**
  4607. * \brief Prepare to authenticate a new message with the same key.
  4608. * Called after mbedtls_md_hmac_finish() and before mbedtls_md_hmac_update().
  4609. *
  4610. * \param ctx HMAC context to be reset
  4611. *
  4612. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  4613. * verification fails.
  4614. */
  4615. int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx );
  4616. /**
  4617. * \brief Output = Generic_HMAC( hmac key, input buffer )
  4618. *
  4619. * \param md_info message digest info
  4620. * \param key HMAC secret key
  4621. * \param keylen length of the HMAC key in bytes
  4622. * \param input buffer holding the data
  4623. * \param ilen length of the input data
  4624. * \param output Generic HMAC-result
  4625. *
  4626. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  4627. * verification fails.
  4628. */
  4629. int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
  4630. const unsigned char *input, size_t ilen,
  4631. unsigned char *output );
  4632. /* Internal use */
  4633. int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data );
  4634. #ifdef __cplusplus
  4635. }
  4636. #endif
  4637. #endif /* MBEDTLS_MD_H */
  4638. /********* Start of file include/mbedtls/md_internal.h ************/
  4639. /**
  4640. * \file md_internal.h
  4641. *
  4642. * \brief Message digest wrappers.
  4643. *
  4644. * \warning This in an internal header. Do not include directly.
  4645. *
  4646. * \author Adriaan de Jong <dejong@fox-it.com>
  4647. *
  4648. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  4649. * SPDX-License-Identifier: Apache-2.0
  4650. *
  4651. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  4652. * not use this file except in compliance with the License.
  4653. * You may obtain a copy of the License at
  4654. *
  4655. * http://www.apache.org/licenses/LICENSE-2.0
  4656. *
  4657. * Unless required by applicable law or agreed to in writing, software
  4658. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  4659. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4660. * See the License for the specific language governing permissions and
  4661. * limitations under the License.
  4662. *
  4663. * This file is part of mbed TLS (https://tls.mbed.org)
  4664. */
  4665. #ifndef MBEDTLS_MD_WRAP_H
  4666. #define MBEDTLS_MD_WRAP_H
  4667. #if !defined(MBEDTLS_CONFIG_FILE)
  4668. #else
  4669. #endif
  4670. #ifdef __cplusplus
  4671. extern "C" {
  4672. #endif
  4673. /**
  4674. * Message digest information.
  4675. * Allows message digest functions to be called in a generic way.
  4676. */
  4677. struct mbedtls_md_info_t
  4678. {
  4679. /** Digest identifier */
  4680. mbedtls_md_type_t type;
  4681. /** Name of the message digest */
  4682. const char * name;
  4683. /** Output length of the digest function in bytes */
  4684. int size;
  4685. /** Block length of the digest function in bytes */
  4686. int block_size;
  4687. /** Digest initialisation function */
  4688. void (*starts_func)( void *ctx );
  4689. /** Digest update function */
  4690. void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
  4691. /** Digest finalisation function */
  4692. void (*finish_func)( void *ctx, unsigned char *output );
  4693. /** Generic digest function */
  4694. void (*digest_func)( const unsigned char *input, size_t ilen,
  4695. unsigned char *output );
  4696. /** Allocate a new context */
  4697. void * (*ctx_alloc_func)( void );
  4698. /** Free the given context */
  4699. void (*ctx_free_func)( void *ctx );
  4700. /** Clone state from a context */
  4701. void (*clone_func)( void *dst, const void *src );
  4702. /** Internal use only */
  4703. void (*process_func)( void *ctx, const unsigned char *input );
  4704. };
  4705. #if defined(MBEDTLS_MD2_C)
  4706. extern const mbedtls_md_info_t mbedtls_md2_info;
  4707. #endif
  4708. #if defined(MBEDTLS_MD4_C)
  4709. extern const mbedtls_md_info_t mbedtls_md4_info;
  4710. #endif
  4711. #if defined(MBEDTLS_MD5_C)
  4712. extern const mbedtls_md_info_t mbedtls_md5_info;
  4713. #endif
  4714. #if defined(MBEDTLS_RIPEMD160_C)
  4715. extern const mbedtls_md_info_t mbedtls_ripemd160_info;
  4716. #endif
  4717. #if defined(MBEDTLS_SHA1_C)
  4718. extern const mbedtls_md_info_t mbedtls_sha1_info;
  4719. #endif
  4720. #if defined(MBEDTLS_SHA256_C)
  4721. extern const mbedtls_md_info_t mbedtls_sha224_info;
  4722. extern const mbedtls_md_info_t mbedtls_sha256_info;
  4723. #endif
  4724. #if defined(MBEDTLS_SHA512_C)
  4725. extern const mbedtls_md_info_t mbedtls_sha384_info;
  4726. extern const mbedtls_md_info_t mbedtls_sha512_info;
  4727. #endif
  4728. #ifdef __cplusplus
  4729. }
  4730. #endif
  4731. #endif /* MBEDTLS_MD_WRAP_H */
  4732. /********* Start of file include/mbedtls/md5.h ************/
  4733. /**
  4734. * \file md5.h
  4735. *
  4736. * \brief MD5 message digest algorithm (hash function)
  4737. *
  4738. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  4739. * SPDX-License-Identifier: Apache-2.0
  4740. *
  4741. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  4742. * not use this file except in compliance with the License.
  4743. * You may obtain a copy of the License at
  4744. *
  4745. * http://www.apache.org/licenses/LICENSE-2.0
  4746. *
  4747. * Unless required by applicable law or agreed to in writing, software
  4748. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  4749. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4750. * See the License for the specific language governing permissions and
  4751. * limitations under the License.
  4752. *
  4753. * This file is part of mbed TLS (https://tls.mbed.org)
  4754. */
  4755. #ifndef MBEDTLS_MD5_H
  4756. #define MBEDTLS_MD5_H
  4757. #if !defined(MBEDTLS_CONFIG_FILE)
  4758. #else
  4759. #endif
  4760. #include <stddef.h>
  4761. #if !VXWORKS
  4762. #include <stdint.h>
  4763. #endif
  4764. #if !defined(MBEDTLS_MD5_ALT)
  4765. // Regular implementation
  4766. //
  4767. #ifdef __cplusplus
  4768. extern "C" {
  4769. #endif
  4770. /**
  4771. * \brief MD5 context structure
  4772. */
  4773. typedef struct
  4774. {
  4775. uint32_t total[2]; /*!< number of bytes processed */
  4776. uint32_t state[4]; /*!< intermediate digest state */
  4777. unsigned char buffer[64]; /*!< data block being processed */
  4778. }
  4779. mbedtls_md5_context;
  4780. /**
  4781. * \brief Initialize MD5 context
  4782. *
  4783. * \param ctx MD5 context to be initialized
  4784. */
  4785. void mbedtls_md5_init( mbedtls_md5_context *ctx );
  4786. /**
  4787. * \brief Clear MD5 context
  4788. *
  4789. * \param ctx MD5 context to be cleared
  4790. */
  4791. void mbedtls_md5_free( mbedtls_md5_context *ctx );
  4792. /**
  4793. * \brief Clone (the state of) an MD5 context
  4794. *
  4795. * \param dst The destination context
  4796. * \param src The context to be cloned
  4797. */
  4798. void mbedtls_md5_clone( mbedtls_md5_context *dst,
  4799. const mbedtls_md5_context *src );
  4800. /**
  4801. * \brief MD5 context setup
  4802. *
  4803. * \param ctx context to be initialized
  4804. */
  4805. void mbedtls_md5_starts( mbedtls_md5_context *ctx );
  4806. /**
  4807. * \brief MD5 process buffer
  4808. *
  4809. * \param ctx MD5 context
  4810. * \param input buffer holding the data
  4811. * \param ilen length of the input data
  4812. */
  4813. void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen );
  4814. /**
  4815. * \brief MD5 final digest
  4816. *
  4817. * \param ctx MD5 context
  4818. * \param output MD5 checksum result
  4819. */
  4820. void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] );
  4821. /* Internal use */
  4822. void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[64] );
  4823. #ifdef __cplusplus
  4824. }
  4825. #endif
  4826. #else /* MBEDTLS_MD5_ALT */
  4827. #endif /* MBEDTLS_MD5_ALT */
  4828. #ifdef __cplusplus
  4829. extern "C" {
  4830. #endif
  4831. /**
  4832. * \brief Output = MD5( input buffer )
  4833. *
  4834. * \param input buffer holding the data
  4835. * \param ilen length of the input data
  4836. * \param output MD5 checksum result
  4837. */
  4838. void mbedtls_md5( const unsigned char *input, size_t ilen, unsigned char output[16] );
  4839. /**
  4840. * \brief Checkup routine
  4841. *
  4842. * \return 0 if successful, or 1 if the test failed
  4843. */
  4844. int mbedtls_md5_self_test( int verbose );
  4845. #ifdef __cplusplus
  4846. }
  4847. #endif
  4848. #endif /* mbedtls_md5.h */
  4849. /********* Start of file include/mbedtls/md2.h ************/
  4850. /**
  4851. * \file md2.h
  4852. *
  4853. * \brief MD2 message digest algorithm (hash function)
  4854. *
  4855. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  4856. * SPDX-License-Identifier: Apache-2.0
  4857. *
  4858. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  4859. * not use this file except in compliance with the License.
  4860. * You may obtain a copy of the License at
  4861. *
  4862. * http://www.apache.org/licenses/LICENSE-2.0
  4863. *
  4864. * Unless required by applicable law or agreed to in writing, software
  4865. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  4866. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4867. * See the License for the specific language governing permissions and
  4868. * limitations under the License.
  4869. *
  4870. * This file is part of mbed TLS (https://tls.mbed.org)
  4871. */
  4872. #ifndef MBEDTLS_MD2_H
  4873. #define MBEDTLS_MD2_H
  4874. #if !defined(MBEDTLS_CONFIG_FILE)
  4875. #else
  4876. #endif
  4877. #include <stddef.h>
  4878. #if !defined(MBEDTLS_MD2_ALT)
  4879. // Regular implementation
  4880. //
  4881. #ifdef __cplusplus
  4882. extern "C" {
  4883. #endif
  4884. /**
  4885. * \brief MD2 context structure
  4886. */
  4887. typedef struct
  4888. {
  4889. unsigned char cksum[16]; /*!< checksum of the data block */
  4890. unsigned char state[48]; /*!< intermediate digest state */
  4891. unsigned char buffer[16]; /*!< data block being processed */
  4892. size_t left; /*!< amount of data in buffer */
  4893. }
  4894. mbedtls_md2_context;
  4895. /**
  4896. * \brief Initialize MD2 context
  4897. *
  4898. * \param ctx MD2 context to be initialized
  4899. */
  4900. void mbedtls_md2_init( mbedtls_md2_context *ctx );
  4901. /**
  4902. * \brief Clear MD2 context
  4903. *
  4904. * \param ctx MD2 context to be cleared
  4905. */
  4906. void mbedtls_md2_free( mbedtls_md2_context *ctx );
  4907. /**
  4908. * \brief Clone (the state of) an MD2 context
  4909. *
  4910. * \param dst The destination context
  4911. * \param src The context to be cloned
  4912. */
  4913. void mbedtls_md2_clone( mbedtls_md2_context *dst,
  4914. const mbedtls_md2_context *src );
  4915. /**
  4916. * \brief MD2 context setup
  4917. *
  4918. * \param ctx context to be initialized
  4919. */
  4920. void mbedtls_md2_starts( mbedtls_md2_context *ctx );
  4921. /**
  4922. * \brief MD2 process buffer
  4923. *
  4924. * \param ctx MD2 context
  4925. * \param input buffer holding the data
  4926. * \param ilen length of the input data
  4927. */
  4928. void mbedtls_md2_update( mbedtls_md2_context *ctx, const unsigned char *input, size_t ilen );
  4929. /**
  4930. * \brief MD2 final digest
  4931. *
  4932. * \param ctx MD2 context
  4933. * \param output MD2 checksum result
  4934. */
  4935. void mbedtls_md2_finish( mbedtls_md2_context *ctx, unsigned char output[16] );
  4936. #ifdef __cplusplus
  4937. }
  4938. #endif
  4939. #else /* MBEDTLS_MD2_ALT */
  4940. #endif /* MBEDTLS_MD2_ALT */
  4941. #ifdef __cplusplus
  4942. extern "C" {
  4943. #endif
  4944. /**
  4945. * \brief Output = MD2( input buffer )
  4946. *
  4947. * \param input buffer holding the data
  4948. * \param ilen length of the input data
  4949. * \param output MD2 checksum result
  4950. */
  4951. void mbedtls_md2( const unsigned char *input, size_t ilen, unsigned char output[16] );
  4952. /**
  4953. * \brief Checkup routine
  4954. *
  4955. * \return 0 if successful, or 1 if the test failed
  4956. */
  4957. int mbedtls_md2_self_test( int verbose );
  4958. /* Internal use */
  4959. void mbedtls_md2_process( mbedtls_md2_context *ctx );
  4960. #ifdef __cplusplus
  4961. }
  4962. #endif
  4963. #endif /* mbedtls_md2.h */
  4964. /********* Start of file include/mbedtls/md4.h ************/
  4965. /**
  4966. * \file md4.h
  4967. *
  4968. * \brief MD4 message digest algorithm (hash function)
  4969. *
  4970. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  4971. * SPDX-License-Identifier: Apache-2.0
  4972. *
  4973. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  4974. * not use this file except in compliance with the License.
  4975. * You may obtain a copy of the License at
  4976. *
  4977. * http://www.apache.org/licenses/LICENSE-2.0
  4978. *
  4979. * Unless required by applicable law or agreed to in writing, software
  4980. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  4981. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4982. * See the License for the specific language governing permissions and
  4983. * limitations under the License.
  4984. *
  4985. * This file is part of mbed TLS (https://tls.mbed.org)
  4986. */
  4987. #ifndef MBEDTLS_MD4_H
  4988. #define MBEDTLS_MD4_H
  4989. #if !defined(MBEDTLS_CONFIG_FILE)
  4990. #else
  4991. #endif
  4992. #include <stddef.h>
  4993. #if !VXWORKS
  4994. #include <stdint.h>
  4995. #endif
  4996. #if !defined(MBEDTLS_MD4_ALT)
  4997. // Regular implementation
  4998. //
  4999. #ifdef __cplusplus
  5000. extern "C" {
  5001. #endif
  5002. /**
  5003. * \brief MD4 context structure
  5004. */
  5005. typedef struct
  5006. {
  5007. uint32_t total[2]; /*!< number of bytes processed */
  5008. uint32_t state[4]; /*!< intermediate digest state */
  5009. unsigned char buffer[64]; /*!< data block being processed */
  5010. }
  5011. mbedtls_md4_context;
  5012. /**
  5013. * \brief Initialize MD4 context
  5014. *
  5015. * \param ctx MD4 context to be initialized
  5016. */
  5017. void mbedtls_md4_init( mbedtls_md4_context *ctx );
  5018. /**
  5019. * \brief Clear MD4 context
  5020. *
  5021. * \param ctx MD4 context to be cleared
  5022. */
  5023. void mbedtls_md4_free( mbedtls_md4_context *ctx );
  5024. /**
  5025. * \brief Clone (the state of) an MD4 context
  5026. *
  5027. * \param dst The destination context
  5028. * \param src The context to be cloned
  5029. */
  5030. void mbedtls_md4_clone( mbedtls_md4_context *dst,
  5031. const mbedtls_md4_context *src );
  5032. /**
  5033. * \brief MD4 context setup
  5034. *
  5035. * \param ctx context to be initialized
  5036. */
  5037. void mbedtls_md4_starts( mbedtls_md4_context *ctx );
  5038. /**
  5039. * \brief MD4 process buffer
  5040. *
  5041. * \param ctx MD4 context
  5042. * \param input buffer holding the data
  5043. * \param ilen length of the input data
  5044. */
  5045. void mbedtls_md4_update( mbedtls_md4_context *ctx, const unsigned char *input, size_t ilen );
  5046. /**
  5047. * \brief MD4 final digest
  5048. *
  5049. * \param ctx MD4 context
  5050. * \param output MD4 checksum result
  5051. */
  5052. void mbedtls_md4_finish( mbedtls_md4_context *ctx, unsigned char output[16] );
  5053. #ifdef __cplusplus
  5054. }
  5055. #endif
  5056. #else /* MBEDTLS_MD4_ALT */
  5057. #endif /* MBEDTLS_MD4_ALT */
  5058. #ifdef __cplusplus
  5059. extern "C" {
  5060. #endif
  5061. /**
  5062. * \brief Output = MD4( input buffer )
  5063. *
  5064. * \param input buffer holding the data
  5065. * \param ilen length of the input data
  5066. * \param output MD4 checksum result
  5067. */
  5068. void mbedtls_md4( const unsigned char *input, size_t ilen, unsigned char output[16] );
  5069. /**
  5070. * \brief Checkup routine
  5071. *
  5072. * \return 0 if successful, or 1 if the test failed
  5073. */
  5074. int mbedtls_md4_self_test( int verbose );
  5075. /* Internal use */
  5076. void mbedtls_md4_process( mbedtls_md4_context *ctx, const unsigned char data[64] );
  5077. #ifdef __cplusplus
  5078. }
  5079. #endif
  5080. #endif /* mbedtls_md4.h */
  5081. /********* Start of file include/mbedtls/rsa.h ************/
  5082. /**
  5083. * \file rsa.h
  5084. *
  5085. * \brief The RSA public-key cryptosystem
  5086. *
  5087. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5088. * SPDX-License-Identifier: Apache-2.0
  5089. *
  5090. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  5091. * not use this file except in compliance with the License.
  5092. * You may obtain a copy of the License at
  5093. *
  5094. * http://www.apache.org/licenses/LICENSE-2.0
  5095. *
  5096. * Unless required by applicable law or agreed to in writing, software
  5097. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  5098. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5099. * See the License for the specific language governing permissions and
  5100. * limitations under the License.
  5101. *
  5102. * This file is part of mbed TLS (https://tls.mbed.org)
  5103. */
  5104. #ifndef MBEDTLS_RSA_H
  5105. #define MBEDTLS_RSA_H
  5106. #if !defined(MBEDTLS_CONFIG_FILE)
  5107. #else
  5108. #endif
  5109. #if defined(MBEDTLS_THREADING_C)
  5110. #endif
  5111. /*
  5112. * RSA Error codes
  5113. */
  5114. #define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
  5115. #define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
  5116. #define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
  5117. #define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the library's validity check. */
  5118. #define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
  5119. #define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
  5120. #define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
  5121. #define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
  5122. #define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
  5123. /*
  5124. * RSA constants
  5125. */
  5126. #define MBEDTLS_RSA_PUBLIC 0
  5127. #define MBEDTLS_RSA_PRIVATE 1
  5128. #define MBEDTLS_RSA_PKCS_V15 0
  5129. #define MBEDTLS_RSA_PKCS_V21 1
  5130. #define MBEDTLS_RSA_SIGN 1
  5131. #define MBEDTLS_RSA_CRYPT 2
  5132. #define MBEDTLS_RSA_SALT_LEN_ANY -1
  5133. /*
  5134. * The above constants may be used even if the RSA module is compile out,
  5135. * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
  5136. */
  5137. #if defined(MBEDTLS_RSA_C)
  5138. #ifdef __cplusplus
  5139. extern "C" {
  5140. #endif
  5141. /**
  5142. * \brief RSA context structure
  5143. */
  5144. typedef struct
  5145. {
  5146. int ver; /*!< always 0 */
  5147. size_t len; /*!< size(N) in chars */
  5148. mbedtls_mpi N; /*!< public modulus */
  5149. mbedtls_mpi E; /*!< public exponent */
  5150. mbedtls_mpi D; /*!< private exponent */
  5151. mbedtls_mpi P; /*!< 1st prime factor */
  5152. mbedtls_mpi Q; /*!< 2nd prime factor */
  5153. mbedtls_mpi DP; /*!< D % (P - 1) */
  5154. mbedtls_mpi DQ; /*!< D % (Q - 1) */
  5155. mbedtls_mpi QP; /*!< 1 / (Q % P) */
  5156. mbedtls_mpi RN; /*!< cached R^2 mod N */
  5157. mbedtls_mpi RP; /*!< cached R^2 mod P */
  5158. mbedtls_mpi RQ; /*!< cached R^2 mod Q */
  5159. mbedtls_mpi Vi; /*!< cached blinding value */
  5160. mbedtls_mpi Vf; /*!< cached un-blinding value */
  5161. int padding; /*!< MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
  5162. RSA_PKCS_v21 for OAEP/PSS */
  5163. int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
  5164. specified in the mbedtls_md.h header file
  5165. for the EME-OAEP and EMSA-PSS
  5166. encoding */
  5167. #if defined(MBEDTLS_THREADING_C)
  5168. mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
  5169. #endif
  5170. }
  5171. mbedtls_rsa_context;
  5172. /**
  5173. * \brief Initialize an RSA context
  5174. *
  5175. * Note: Set padding to MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
  5176. * encryption scheme and the RSASSA-PSS signature scheme.
  5177. *
  5178. * \param ctx RSA context to be initialized
  5179. * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
  5180. * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier
  5181. *
  5182. * \note The hash_id parameter is actually ignored
  5183. * when using MBEDTLS_RSA_PKCS_V15 padding.
  5184. *
  5185. * \note Choice of padding mode is strictly enforced for private key
  5186. * operations, since there might be security concerns in
  5187. * mixing padding modes. For public key operations it's merely
  5188. * a default value, which can be overriden by calling specific
  5189. * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
  5190. *
  5191. * \note The chosen hash is always used for OEAP encryption.
  5192. * For PSS signatures, it's always used for making signatures,
  5193. * but can be overriden (and always is, if set to
  5194. * MBEDTLS_MD_NONE) for verifying them.
  5195. */
  5196. void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
  5197. int padding,
  5198. int hash_id);
  5199. /**
  5200. * \brief Set padding for an already initialized RSA context
  5201. * See \c mbedtls_rsa_init() for details.
  5202. *
  5203. * \param ctx RSA context to be set
  5204. * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
  5205. * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier
  5206. */
  5207. void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id);
  5208. /**
  5209. * \brief Generate an RSA keypair
  5210. *
  5211. * \param ctx RSA context that will hold the key
  5212. * \param f_rng RNG function
  5213. * \param p_rng RNG parameter
  5214. * \param nbits size of the public key in bits
  5215. * \param exponent public exponent (e.g., 65537)
  5216. *
  5217. * \note mbedtls_rsa_init() must be called beforehand to setup
  5218. * the RSA context.
  5219. *
  5220. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  5221. */
  5222. int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
  5223. int (*f_rng)(void *, unsigned char *, size_t),
  5224. void *p_rng,
  5225. unsigned int nbits, int exponent );
  5226. /**
  5227. * \brief Check a public RSA key
  5228. *
  5229. * \param ctx RSA context to be checked
  5230. *
  5231. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  5232. */
  5233. int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
  5234. /**
  5235. * \brief Check a private RSA key
  5236. *
  5237. * \param ctx RSA context to be checked
  5238. *
  5239. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  5240. */
  5241. int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
  5242. /**
  5243. * \brief Check a public-private RSA key pair.
  5244. * Check each of the contexts, and make sure they match.
  5245. *
  5246. * \param pub RSA context holding the public key
  5247. * \param prv RSA context holding the private key
  5248. *
  5249. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  5250. */
  5251. int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv );
  5252. /**
  5253. * \brief Do an RSA public key operation
  5254. *
  5255. * \param ctx RSA context
  5256. * \param input input buffer
  5257. * \param output output buffer
  5258. *
  5259. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  5260. *
  5261. * \note This function does NOT take care of message
  5262. * padding. Also, be sure to set input[0] = 0 or assure that
  5263. * input is smaller than N.
  5264. *
  5265. * \note The input and output buffers must be large
  5266. * enough (eg. 128 bytes if RSA-1024 is used).
  5267. */
  5268. int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
  5269. const unsigned char *input,
  5270. unsigned char *output );
  5271. /**
  5272. * \brief Do an RSA private key operation
  5273. *
  5274. * \param ctx RSA context
  5275. * \param f_rng RNG function (Needed for blinding)
  5276. * \param p_rng RNG parameter
  5277. * \param input input buffer
  5278. * \param output output buffer
  5279. *
  5280. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  5281. *
  5282. * \note The input and output buffers must be large
  5283. * enough (eg. 128 bytes if RSA-1024 is used).
  5284. */
  5285. int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
  5286. int (*f_rng)(void *, unsigned char *, size_t),
  5287. void *p_rng,
  5288. const unsigned char *input,
  5289. unsigned char *output );
  5290. /**
  5291. * \brief Generic wrapper to perform a PKCS#1 encryption using the
  5292. * mode from the context. Add the message padding, then do an
  5293. * RSA operation.
  5294. *
  5295. * \param ctx RSA context
  5296. * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
  5297. * and MBEDTLS_RSA_PRIVATE)
  5298. * \param p_rng RNG parameter
  5299. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  5300. * \param ilen contains the plaintext length
  5301. * \param input buffer holding the data to be encrypted
  5302. * \param output buffer that will hold the ciphertext
  5303. *
  5304. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  5305. *
  5306. * \note The output buffer must be as large as the size
  5307. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  5308. */
  5309. int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
  5310. int (*f_rng)(void *, unsigned char *, size_t),
  5311. void *p_rng,
  5312. int mode, size_t ilen,
  5313. const unsigned char *input,
  5314. unsigned char *output );
  5315. /**
  5316. * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
  5317. *
  5318. * \param ctx RSA context
  5319. * \param f_rng RNG function (Needed for padding and MBEDTLS_RSA_PRIVATE)
  5320. * \param p_rng RNG parameter
  5321. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  5322. * \param ilen contains the plaintext length
  5323. * \param input buffer holding the data to be encrypted
  5324. * \param output buffer that will hold the ciphertext
  5325. *
  5326. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  5327. *
  5328. * \note The output buffer must be as large as the size
  5329. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  5330. */
  5331. int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
  5332. int (*f_rng)(void *, unsigned char *, size_t),
  5333. void *p_rng,
  5334. int mode, size_t ilen,
  5335. const unsigned char *input,
  5336. unsigned char *output );
  5337. /**
  5338. * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
  5339. *
  5340. * \param ctx RSA context
  5341. * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
  5342. * and MBEDTLS_RSA_PRIVATE)
  5343. * \param p_rng RNG parameter
  5344. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  5345. * \param label buffer holding the custom label to use
  5346. * \param label_len contains the label length
  5347. * \param ilen contains the plaintext length
  5348. * \param input buffer holding the data to be encrypted
  5349. * \param output buffer that will hold the ciphertext
  5350. *
  5351. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  5352. *
  5353. * \note The output buffer must be as large as the size
  5354. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  5355. */
  5356. int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
  5357. int (*f_rng)(void *, unsigned char *, size_t),
  5358. void *p_rng,
  5359. int mode,
  5360. const unsigned char *label, size_t label_len,
  5361. size_t ilen,
  5362. const unsigned char *input,
  5363. unsigned char *output );
  5364. /**
  5365. * \brief Generic wrapper to perform a PKCS#1 decryption using the
  5366. * mode from the context. Do an RSA operation, then remove
  5367. * the message padding
  5368. *
  5369. * \param ctx RSA context
  5370. * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
  5371. * \param p_rng RNG parameter
  5372. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  5373. * \param olen will contain the plaintext length
  5374. * \param input buffer holding the encrypted data
  5375. * \param output buffer that will hold the plaintext
  5376. * \param output_max_len maximum length of the output buffer
  5377. *
  5378. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  5379. *
  5380. * \note The output buffer must be as large as the size
  5381. * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
  5382. * an error is thrown.
  5383. */
  5384. int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
  5385. int (*f_rng)(void *, unsigned char *, size_t),
  5386. void *p_rng,
  5387. int mode, size_t *olen,
  5388. const unsigned char *input,
  5389. unsigned char *output,
  5390. size_t output_max_len );
  5391. /**
  5392. * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
  5393. *
  5394. * \param ctx RSA context
  5395. * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
  5396. * \param p_rng RNG parameter
  5397. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  5398. * \param olen will contain the plaintext length
  5399. * \param input buffer holding the encrypted data
  5400. * \param output buffer that will hold the plaintext
  5401. * \param output_max_len maximum length of the output buffer
  5402. *
  5403. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  5404. *
  5405. * \note The output buffer must be as large as the size
  5406. * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
  5407. * an error is thrown.
  5408. */
  5409. int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
  5410. int (*f_rng)(void *, unsigned char *, size_t),
  5411. void *p_rng,
  5412. int mode, size_t *olen,
  5413. const unsigned char *input,
  5414. unsigned char *output,
  5415. size_t output_max_len );
  5416. /**
  5417. * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
  5418. *
  5419. * \param ctx RSA context
  5420. * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
  5421. * \param p_rng RNG parameter
  5422. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  5423. * \param label buffer holding the custom label to use
  5424. * \param label_len contains the label length
  5425. * \param olen will contain the plaintext length
  5426. * \param input buffer holding the encrypted data
  5427. * \param output buffer that will hold the plaintext
  5428. * \param output_max_len maximum length of the output buffer
  5429. *
  5430. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  5431. *
  5432. * \note The output buffer must be as large as the size
  5433. * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
  5434. * an error is thrown.
  5435. */
  5436. int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
  5437. int (*f_rng)(void *, unsigned char *, size_t),
  5438. void *p_rng,
  5439. int mode,
  5440. const unsigned char *label, size_t label_len,
  5441. size_t *olen,
  5442. const unsigned char *input,
  5443. unsigned char *output,
  5444. size_t output_max_len );
  5445. /**
  5446. * \brief Generic wrapper to perform a PKCS#1 signature using the
  5447. * mode from the context. Do a private RSA operation to sign
  5448. * a message digest
  5449. *
  5450. * \param ctx RSA context
  5451. * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
  5452. * MBEDTLS_RSA_PRIVATE)
  5453. * \param p_rng RNG parameter
  5454. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  5455. * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
  5456. * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
  5457. * \param hash buffer holding the message digest
  5458. * \param sig buffer that will hold the ciphertext
  5459. *
  5460. * \return 0 if the signing operation was successful,
  5461. * or an MBEDTLS_ERR_RSA_XXX error code
  5462. *
  5463. * \note The "sig" buffer must be as large as the size
  5464. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  5465. *
  5466. * \note In case of PKCS#1 v2.1 encoding, see comments on
  5467. * \note \c mbedtls_rsa_rsassa_pss_sign() for details on md_alg and hash_id.
  5468. */
  5469. int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
  5470. int (*f_rng)(void *, unsigned char *, size_t),
  5471. void *p_rng,
  5472. int mode,
  5473. mbedtls_md_type_t md_alg,
  5474. unsigned int hashlen,
  5475. const unsigned char *hash,
  5476. unsigned char *sig );
  5477. /**
  5478. * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
  5479. *
  5480. * \param ctx RSA context
  5481. * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
  5482. * \param p_rng RNG parameter
  5483. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  5484. * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
  5485. * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
  5486. * \param hash buffer holding the message digest
  5487. * \param sig buffer that will hold the ciphertext
  5488. *
  5489. * \return 0 if the signing operation was successful,
  5490. * or an MBEDTLS_ERR_RSA_XXX error code
  5491. *
  5492. * \note The "sig" buffer must be as large as the size
  5493. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  5494. */
  5495. int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
  5496. int (*f_rng)(void *, unsigned char *, size_t),
  5497. void *p_rng,
  5498. int mode,
  5499. mbedtls_md_type_t md_alg,
  5500. unsigned int hashlen,
  5501. const unsigned char *hash,
  5502. unsigned char *sig );
  5503. /**
  5504. * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
  5505. *
  5506. * \param ctx RSA context
  5507. * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
  5508. * MBEDTLS_RSA_PRIVATE)
  5509. * \param p_rng RNG parameter
  5510. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  5511. * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
  5512. * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
  5513. * \param hash buffer holding the message digest
  5514. * \param sig buffer that will hold the ciphertext
  5515. *
  5516. * \return 0 if the signing operation was successful,
  5517. * or an MBEDTLS_ERR_RSA_XXX error code
  5518. *
  5519. * \note The "sig" buffer must be as large as the size
  5520. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  5521. *
  5522. * \note The hash_id in the RSA context is the one used for the
  5523. * encoding. md_alg in the function call is the type of hash
  5524. * that is encoded. According to RFC 3447 it is advised to
  5525. * keep both hashes the same.
  5526. */
  5527. int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
  5528. int (*f_rng)(void *, unsigned char *, size_t),
  5529. void *p_rng,
  5530. int mode,
  5531. mbedtls_md_type_t md_alg,
  5532. unsigned int hashlen,
  5533. const unsigned char *hash,
  5534. unsigned char *sig );
  5535. /**
  5536. * \brief Generic wrapper to perform a PKCS#1 verification using the
  5537. * mode from the context. Do a public RSA operation and check
  5538. * the message digest
  5539. *
  5540. * \param ctx points to an RSA public key
  5541. * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
  5542. * \param p_rng RNG parameter
  5543. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  5544. * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
  5545. * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
  5546. * \param hash buffer holding the message digest
  5547. * \param sig buffer holding the ciphertext
  5548. *
  5549. * \return 0 if the verify operation was successful,
  5550. * or an MBEDTLS_ERR_RSA_XXX error code
  5551. *
  5552. * \note The "sig" buffer must be as large as the size
  5553. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  5554. *
  5555. * \note In case of PKCS#1 v2.1 encoding, see comments on
  5556. * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
  5557. */
  5558. int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
  5559. int (*f_rng)(void *, unsigned char *, size_t),
  5560. void *p_rng,
  5561. int mode,
  5562. mbedtls_md_type_t md_alg,
  5563. unsigned int hashlen,
  5564. const unsigned char *hash,
  5565. const unsigned char *sig );
  5566. /**
  5567. * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
  5568. *
  5569. * \param ctx points to an RSA public key
  5570. * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
  5571. * \param p_rng RNG parameter
  5572. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  5573. * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
  5574. * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
  5575. * \param hash buffer holding the message digest
  5576. * \param sig buffer holding the ciphertext
  5577. *
  5578. * \return 0 if the verify operation was successful,
  5579. * or an MBEDTLS_ERR_RSA_XXX error code
  5580. *
  5581. * \note The "sig" buffer must be as large as the size
  5582. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  5583. */
  5584. int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
  5585. int (*f_rng)(void *, unsigned char *, size_t),
  5586. void *p_rng,
  5587. int mode,
  5588. mbedtls_md_type_t md_alg,
  5589. unsigned int hashlen,
  5590. const unsigned char *hash,
  5591. const unsigned char *sig );
  5592. /**
  5593. * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
  5594. * (This is the "simple" version.)
  5595. *
  5596. * \param ctx points to an RSA public key
  5597. * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
  5598. * \param p_rng RNG parameter
  5599. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  5600. * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
  5601. * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
  5602. * \param hash buffer holding the message digest
  5603. * \param sig buffer holding the ciphertext
  5604. *
  5605. * \return 0 if the verify operation was successful,
  5606. * or an MBEDTLS_ERR_RSA_XXX error code
  5607. *
  5608. * \note The "sig" buffer must be as large as the size
  5609. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  5610. *
  5611. * \note The hash_id in the RSA context is the one used for the
  5612. * verification. md_alg in the function call is the type of
  5613. * hash that is verified. According to RFC 3447 it is advised to
  5614. * keep both hashes the same. If hash_id in the RSA context is
  5615. * unset, the md_alg from the function call is used.
  5616. */
  5617. int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
  5618. int (*f_rng)(void *, unsigned char *, size_t),
  5619. void *p_rng,
  5620. int mode,
  5621. mbedtls_md_type_t md_alg,
  5622. unsigned int hashlen,
  5623. const unsigned char *hash,
  5624. const unsigned char *sig );
  5625. /**
  5626. * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
  5627. * (This is the version with "full" options.)
  5628. *
  5629. * \param ctx points to an RSA public key
  5630. * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
  5631. * \param p_rng RNG parameter
  5632. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  5633. * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
  5634. * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
  5635. * \param hash buffer holding the message digest
  5636. * \param mgf1_hash_id message digest used for mask generation
  5637. * \param expected_salt_len Length of the salt used in padding, use
  5638. * MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
  5639. * \param sig buffer holding the ciphertext
  5640. *
  5641. * \return 0 if the verify operation was successful,
  5642. * or an MBEDTLS_ERR_RSA_XXX error code
  5643. *
  5644. * \note The "sig" buffer must be as large as the size
  5645. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  5646. *
  5647. * \note The hash_id in the RSA context is ignored.
  5648. */
  5649. int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
  5650. int (*f_rng)(void *, unsigned char *, size_t),
  5651. void *p_rng,
  5652. int mode,
  5653. mbedtls_md_type_t md_alg,
  5654. unsigned int hashlen,
  5655. const unsigned char *hash,
  5656. mbedtls_md_type_t mgf1_hash_id,
  5657. int expected_salt_len,
  5658. const unsigned char *sig );
  5659. /**
  5660. * \brief Copy the components of an RSA context
  5661. *
  5662. * \param dst Destination context
  5663. * \param src Source context
  5664. *
  5665. * \return 0 on success,
  5666. * MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
  5667. */
  5668. int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
  5669. /**
  5670. * \brief Free the components of an RSA key
  5671. *
  5672. * \param ctx RSA Context to free
  5673. */
  5674. void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
  5675. /**
  5676. * \brief Checkup routine
  5677. *
  5678. * \return 0 if successful, or 1 if the test failed
  5679. */
  5680. int mbedtls_rsa_self_test( int verbose );
  5681. #ifdef __cplusplus
  5682. }
  5683. #endif
  5684. #endif /* MBEDTLS_RSA_C */
  5685. #endif /* rsa.h */
  5686. /********* Start of file include/mbedtls/asn1.h ************/
  5687. /**
  5688. * \file asn1.h
  5689. *
  5690. * \brief Generic ASN.1 parsing
  5691. *
  5692. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5693. * SPDX-License-Identifier: Apache-2.0
  5694. *
  5695. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  5696. * not use this file except in compliance with the License.
  5697. * You may obtain a copy of the License at
  5698. *
  5699. * http://www.apache.org/licenses/LICENSE-2.0
  5700. *
  5701. * Unless required by applicable law or agreed to in writing, software
  5702. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  5703. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5704. * See the License for the specific language governing permissions and
  5705. * limitations under the License.
  5706. *
  5707. * This file is part of mbed TLS (https://tls.mbed.org)
  5708. */
  5709. #ifndef MBEDTLS_ASN1_H
  5710. #define MBEDTLS_ASN1_H
  5711. #if !defined(MBEDTLS_CONFIG_FILE)
  5712. #else
  5713. #endif
  5714. #include <stddef.h>
  5715. #if defined(MBEDTLS_BIGNUM_C)
  5716. #endif
  5717. /**
  5718. * \addtogroup asn1_module
  5719. * \{
  5720. */
  5721. /**
  5722. * \name ASN1 Error codes
  5723. * These error codes are OR'ed to X509 error codes for
  5724. * higher error granularity.
  5725. * ASN1 is a standard to specify data structures.
  5726. * \{
  5727. */
  5728. #define MBEDTLS_ERR_ASN1_OUT_OF_DATA -0x0060 /**< Out of data when parsing an ASN1 data structure. */
  5729. #define MBEDTLS_ERR_ASN1_UNEXPECTED_TAG -0x0062 /**< ASN1 tag was of an unexpected value. */
  5730. #define MBEDTLS_ERR_ASN1_INVALID_LENGTH -0x0064 /**< Error when trying to determine the length or invalid length. */
  5731. #define MBEDTLS_ERR_ASN1_LENGTH_MISMATCH -0x0066 /**< Actual length differs from expected length. */
  5732. #define MBEDTLS_ERR_ASN1_INVALID_DATA -0x0068 /**< Data is invalid. (not used) */
  5733. #define MBEDTLS_ERR_ASN1_ALLOC_FAILED -0x006A /**< Memory allocation failed */
  5734. #define MBEDTLS_ERR_ASN1_BUF_TOO_SMALL -0x006C /**< Buffer too small when writing ASN.1 data structure. */
  5735. /* \} name */
  5736. /**
  5737. * \name DER constants
  5738. * These constants comply with DER encoded the ANS1 type tags.
  5739. * DER encoding uses hexadecimal representation.
  5740. * An example DER sequence is:\n
  5741. * - 0x02 -- tag indicating INTEGER
  5742. * - 0x01 -- length in octets
  5743. * - 0x05 -- value
  5744. * Such sequences are typically read into \c ::mbedtls_x509_buf.
  5745. * \{
  5746. */
  5747. #define MBEDTLS_ASN1_BOOLEAN 0x01
  5748. #define MBEDTLS_ASN1_INTEGER 0x02
  5749. #define MBEDTLS_ASN1_BIT_STRING 0x03
  5750. #define MBEDTLS_ASN1_OCTET_STRING 0x04
  5751. #define MBEDTLS_ASN1_NULL 0x05
  5752. #define MBEDTLS_ASN1_OID 0x06
  5753. #define MBEDTLS_ASN1_UTF8_STRING 0x0C
  5754. #define MBEDTLS_ASN1_SEQUENCE 0x10
  5755. #define MBEDTLS_ASN1_SET 0x11
  5756. #define MBEDTLS_ASN1_PRINTABLE_STRING 0x13
  5757. #define MBEDTLS_ASN1_T61_STRING 0x14
  5758. #define MBEDTLS_ASN1_IA5_STRING 0x16
  5759. #define MBEDTLS_ASN1_UTC_TIME 0x17
  5760. #define MBEDTLS_ASN1_GENERALIZED_TIME 0x18
  5761. #define MBEDTLS_ASN1_UNIVERSAL_STRING 0x1C
  5762. #define MBEDTLS_ASN1_BMP_STRING 0x1E
  5763. #define MBEDTLS_ASN1_PRIMITIVE 0x00
  5764. #define MBEDTLS_ASN1_CONSTRUCTED 0x20
  5765. #define MBEDTLS_ASN1_CONTEXT_SPECIFIC 0x80
  5766. /* \} name */
  5767. /* \} addtogroup asn1_module */
  5768. /** Returns the size of the binary string, without the trailing \\0 */
  5769. #define MBEDTLS_OID_SIZE(x) (sizeof(x) - 1)
  5770. /**
  5771. * Compares an mbedtls_asn1_buf structure to a reference OID.
  5772. *
  5773. * Only works for 'defined' oid_str values (MBEDTLS_OID_HMAC_SHA1), you cannot use a
  5774. * 'unsigned char *oid' here!
  5775. */
  5776. #define MBEDTLS_OID_CMP(oid_str, oid_buf) \
  5777. ( ( MBEDTLS_OID_SIZE(oid_str) != (oid_buf)->len ) || \
  5778. memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) != 0 )
  5779. #ifdef __cplusplus
  5780. extern "C" {
  5781. #endif
  5782. /**
  5783. * \name Functions to parse ASN.1 data structures
  5784. * \{
  5785. */
  5786. /**
  5787. * Type-length-value structure that allows for ASN1 using DER.
  5788. */
  5789. typedef struct mbedtls_asn1_buf
  5790. {
  5791. int tag; /**< ASN1 type, e.g. MBEDTLS_ASN1_UTF8_STRING. */
  5792. size_t len; /**< ASN1 length, in octets. */
  5793. unsigned char *p; /**< ASN1 data, e.g. in ASCII. */
  5794. }
  5795. mbedtls_asn1_buf;
  5796. /**
  5797. * Container for ASN1 bit strings.
  5798. */
  5799. typedef struct mbedtls_asn1_bitstring
  5800. {
  5801. size_t len; /**< ASN1 length, in octets. */
  5802. unsigned char unused_bits; /**< Number of unused bits at the end of the string */
  5803. unsigned char *p; /**< Raw ASN1 data for the bit string */
  5804. }
  5805. mbedtls_asn1_bitstring;
  5806. /**
  5807. * Container for a sequence of ASN.1 items
  5808. */
  5809. typedef struct mbedtls_asn1_sequence
  5810. {
  5811. mbedtls_asn1_buf buf; /**< Buffer containing the given ASN.1 item. */
  5812. struct mbedtls_asn1_sequence *next; /**< The next entry in the sequence. */
  5813. }
  5814. mbedtls_asn1_sequence;
  5815. /**
  5816. * Container for a sequence or list of 'named' ASN.1 data items
  5817. */
  5818. typedef struct mbedtls_asn1_named_data
  5819. {
  5820. mbedtls_asn1_buf oid; /**< The object identifier. */
  5821. mbedtls_asn1_buf val; /**< The named value. */
  5822. struct mbedtls_asn1_named_data *next; /**< The next entry in the sequence. */
  5823. unsigned char next_merged; /**< Merge next item into the current one? */
  5824. }
  5825. mbedtls_asn1_named_data;
  5826. /**
  5827. * \brief Get the length of an ASN.1 element.
  5828. * Updates the pointer to immediately behind the length.
  5829. *
  5830. * \param p The position in the ASN.1 data
  5831. * \param end End of data
  5832. * \param len The variable that will receive the value
  5833. *
  5834. * \return 0 if successful, MBEDTLS_ERR_ASN1_OUT_OF_DATA on reaching
  5835. * end of data, MBEDTLS_ERR_ASN1_INVALID_LENGTH if length is
  5836. * unparseable.
  5837. */
  5838. int mbedtls_asn1_get_len( unsigned char **p,
  5839. const unsigned char *end,
  5840. size_t *len );
  5841. /**
  5842. * \brief Get the tag and length of the tag. Check for the requested tag.
  5843. * Updates the pointer to immediately behind the tag and length.
  5844. *
  5845. * \param p The position in the ASN.1 data
  5846. * \param end End of data
  5847. * \param len The variable that will receive the length
  5848. * \param tag The expected tag
  5849. *
  5850. * \return 0 if successful, MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if tag did
  5851. * not match requested tag, or another specific ASN.1 error code.
  5852. */
  5853. int mbedtls_asn1_get_tag( unsigned char **p,
  5854. const unsigned char *end,
  5855. size_t *len, int tag );
  5856. /**
  5857. * \brief Retrieve a boolean ASN.1 tag and its value.
  5858. * Updates the pointer to immediately behind the full tag.
  5859. *
  5860. * \param p The position in the ASN.1 data
  5861. * \param end End of data
  5862. * \param val The variable that will receive the value
  5863. *
  5864. * \return 0 if successful or a specific ASN.1 error code.
  5865. */
  5866. int mbedtls_asn1_get_bool( unsigned char **p,
  5867. const unsigned char *end,
  5868. int *val );
  5869. /**
  5870. * \brief Retrieve an integer ASN.1 tag and its value.
  5871. * Updates the pointer to immediately behind the full tag.
  5872. *
  5873. * \param p The position in the ASN.1 data
  5874. * \param end End of data
  5875. * \param val The variable that will receive the value
  5876. *
  5877. * \return 0 if successful or a specific ASN.1 error code.
  5878. */
  5879. int mbedtls_asn1_get_int( unsigned char **p,
  5880. const unsigned char *end,
  5881. int *val );
  5882. /**
  5883. * \brief Retrieve a bitstring ASN.1 tag and its value.
  5884. * Updates the pointer to immediately behind the full tag.
  5885. *
  5886. * \param p The position in the ASN.1 data
  5887. * \param end End of data
  5888. * \param bs The variable that will receive the value
  5889. *
  5890. * \return 0 if successful or a specific ASN.1 error code.
  5891. */
  5892. int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end,
  5893. mbedtls_asn1_bitstring *bs);
  5894. /**
  5895. * \brief Retrieve a bitstring ASN.1 tag without unused bits and its
  5896. * value.
  5897. * Updates the pointer to the beginning of the bit/octet string.
  5898. *
  5899. * \param p The position in the ASN.1 data
  5900. * \param end End of data
  5901. * \param len Length of the actual bit/octect string in bytes
  5902. *
  5903. * \return 0 if successful or a specific ASN.1 error code.
  5904. */
  5905. int mbedtls_asn1_get_bitstring_null( unsigned char **p, const unsigned char *end,
  5906. size_t *len );
  5907. /**
  5908. * \brief Parses and splits an ASN.1 "SEQUENCE OF <tag>"
  5909. * Updated the pointer to immediately behind the full sequence tag.
  5910. *
  5911. * \param p The position in the ASN.1 data
  5912. * \param end End of data
  5913. * \param cur First variable in the chain to fill
  5914. * \param tag Type of sequence
  5915. *
  5916. * \return 0 if successful or a specific ASN.1 error code.
  5917. */
  5918. int mbedtls_asn1_get_sequence_of( unsigned char **p,
  5919. const unsigned char *end,
  5920. mbedtls_asn1_sequence *cur,
  5921. int tag);
  5922. #if defined(MBEDTLS_BIGNUM_C)
  5923. /**
  5924. * \brief Retrieve a MPI value from an integer ASN.1 tag.
  5925. * Updates the pointer to immediately behind the full tag.
  5926. *
  5927. * \param p The position in the ASN.1 data
  5928. * \param end End of data
  5929. * \param X The MPI that will receive the value
  5930. *
  5931. * \return 0 if successful or a specific ASN.1 or MPI error code.
  5932. */
  5933. int mbedtls_asn1_get_mpi( unsigned char **p,
  5934. const unsigned char *end,
  5935. mbedtls_mpi *X );
  5936. #endif /* MBEDTLS_BIGNUM_C */
  5937. /**
  5938. * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence.
  5939. * Updates the pointer to immediately behind the full
  5940. * AlgorithmIdentifier.
  5941. *
  5942. * \param p The position in the ASN.1 data
  5943. * \param end End of data
  5944. * \param alg The buffer to receive the OID
  5945. * \param params The buffer to receive the params (if any)
  5946. *
  5947. * \return 0 if successful or a specific ASN.1 or MPI error code.
  5948. */
  5949. int mbedtls_asn1_get_alg( unsigned char **p,
  5950. const unsigned char *end,
  5951. mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params );
  5952. /**
  5953. * \brief Retrieve an AlgorithmIdentifier ASN.1 sequence with NULL or no
  5954. * params.
  5955. * Updates the pointer to immediately behind the full
  5956. * AlgorithmIdentifier.
  5957. *
  5958. * \param p The position in the ASN.1 data
  5959. * \param end End of data
  5960. * \param alg The buffer to receive the OID
  5961. *
  5962. * \return 0 if successful or a specific ASN.1 or MPI error code.
  5963. */
  5964. int mbedtls_asn1_get_alg_null( unsigned char **p,
  5965. const unsigned char *end,
  5966. mbedtls_asn1_buf *alg );
  5967. /**
  5968. * \brief Find a specific named_data entry in a sequence or list based on
  5969. * the OID.
  5970. *
  5971. * \param list The list to seek through
  5972. * \param oid The OID to look for
  5973. * \param len Size of the OID
  5974. *
  5975. * \return NULL if not found, or a pointer to the existing entry.
  5976. */
  5977. mbedtls_asn1_named_data *mbedtls_asn1_find_named_data( mbedtls_asn1_named_data *list,
  5978. const char *oid, size_t len );
  5979. /**
  5980. * \brief Free a mbedtls_asn1_named_data entry
  5981. *
  5982. * \param entry The named data entry to free
  5983. */
  5984. void mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *entry );
  5985. /**
  5986. * \brief Free all entries in a mbedtls_asn1_named_data list
  5987. * Head will be set to NULL
  5988. *
  5989. * \param head Pointer to the head of the list of named data entries to free
  5990. */
  5991. void mbedtls_asn1_free_named_data_list( mbedtls_asn1_named_data **head );
  5992. #ifdef __cplusplus
  5993. }
  5994. #endif
  5995. #endif /* asn1.h */
  5996. /********* Start of file include/mbedtls/ecp.h ************/
  5997. /**
  5998. * \file ecp.h
  5999. *
  6000. * \brief Elliptic curves over GF(p)
  6001. *
  6002. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  6003. * SPDX-License-Identifier: Apache-2.0
  6004. *
  6005. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6006. * not use this file except in compliance with the License.
  6007. * You may obtain a copy of the License at
  6008. *
  6009. * http://www.apache.org/licenses/LICENSE-2.0
  6010. *
  6011. * Unless required by applicable law or agreed to in writing, software
  6012. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  6013. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6014. * See the License for the specific language governing permissions and
  6015. * limitations under the License.
  6016. *
  6017. * This file is part of mbed TLS (https://tls.mbed.org)
  6018. */
  6019. #ifndef MBEDTLS_ECP_H
  6020. #define MBEDTLS_ECP_H
  6021. /*
  6022. * ECP error codes
  6023. */
  6024. #define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */
  6025. #define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */
  6026. #define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< Requested curve not available. */
  6027. #define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */
  6028. #define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */
  6029. #define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as (ephemeral) key, failed. */
  6030. #define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */
  6031. #define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< Signature is valid but shorter than the user-supplied length. */
  6032. #ifdef __cplusplus
  6033. extern "C" {
  6034. #endif
  6035. /**
  6036. * Domain parameters (curve, subgroup and generator) identifiers.
  6037. *
  6038. * Only curves over prime fields are supported.
  6039. *
  6040. * \warning This library does not support validation of arbitrary domain
  6041. * parameters. Therefore, only well-known domain parameters from trusted
  6042. * sources should be used. See mbedtls_ecp_group_load().
  6043. */
  6044. typedef enum
  6045. {
  6046. MBEDTLS_ECP_DP_NONE = 0,
  6047. MBEDTLS_ECP_DP_SECP192R1, /*!< 192-bits NIST curve */
  6048. MBEDTLS_ECP_DP_SECP224R1, /*!< 224-bits NIST curve */
  6049. MBEDTLS_ECP_DP_SECP256R1, /*!< 256-bits NIST curve */
  6050. MBEDTLS_ECP_DP_SECP384R1, /*!< 384-bits NIST curve */
  6051. MBEDTLS_ECP_DP_SECP521R1, /*!< 521-bits NIST curve */
  6052. MBEDTLS_ECP_DP_BP256R1, /*!< 256-bits Brainpool curve */
  6053. MBEDTLS_ECP_DP_BP384R1, /*!< 384-bits Brainpool curve */
  6054. MBEDTLS_ECP_DP_BP512R1, /*!< 512-bits Brainpool curve */
  6055. MBEDTLS_ECP_DP_CURVE25519, /*!< Curve25519 */
  6056. MBEDTLS_ECP_DP_SECP192K1, /*!< 192-bits "Koblitz" curve */
  6057. MBEDTLS_ECP_DP_SECP224K1, /*!< 224-bits "Koblitz" curve */
  6058. MBEDTLS_ECP_DP_SECP256K1, /*!< 256-bits "Koblitz" curve */
  6059. } mbedtls_ecp_group_id;
  6060. /**
  6061. * Number of supported curves (plus one for NONE).
  6062. *
  6063. * (Montgomery curves excluded for now.)
  6064. */
  6065. #define MBEDTLS_ECP_DP_MAX 12
  6066. /**
  6067. * Curve information for use by other modules
  6068. */
  6069. typedef struct
  6070. {
  6071. mbedtls_ecp_group_id grp_id; /*!< Internal identifier */
  6072. uint16_t tls_id; /*!< TLS NamedCurve identifier */
  6073. uint16_t bit_size; /*!< Curve size in bits */
  6074. const char *name; /*!< Human-friendly name */
  6075. } mbedtls_ecp_curve_info;
  6076. /**
  6077. * \brief ECP point structure (jacobian coordinates)
  6078. *
  6079. * \note All functions expect and return points satisfying
  6080. * the following condition: Z == 0 or Z == 1. (Other
  6081. * values of Z are used by internal functions only.)
  6082. * The point is zero, or "at infinity", if Z == 0.
  6083. * Otherwise, X and Y are its standard (affine) coordinates.
  6084. */
  6085. typedef struct
  6086. {
  6087. mbedtls_mpi X; /*!< the point's X coordinate */
  6088. mbedtls_mpi Y; /*!< the point's Y coordinate */
  6089. mbedtls_mpi Z; /*!< the point's Z coordinate */
  6090. }
  6091. mbedtls_ecp_point;
  6092. /**
  6093. * \brief ECP group structure
  6094. *
  6095. * We consider two types of curves equations:
  6096. * 1. Short Weierstrass y^2 = x^3 + A x + B mod P (SEC1 + RFC 4492)
  6097. * 2. Montgomery, y^2 = x^3 + A x^2 + x mod P (Curve25519 + draft)
  6098. * In both cases, a generator G for a prime-order subgroup is fixed. In the
  6099. * short weierstrass, this subgroup is actually the whole curve, and its
  6100. * cardinal is denoted by N.
  6101. *
  6102. * In the case of Short Weierstrass curves, our code requires that N is an odd
  6103. * prime. (Use odd in mbedtls_ecp_mul() and prime in mbedtls_ecdsa_sign() for blinding.)
  6104. *
  6105. * In the case of Montgomery curves, we don't store A but (A + 2) / 4 which is
  6106. * the quantity actually used in the formulas. Also, nbits is not the size of N
  6107. * but the required size for private keys.
  6108. *
  6109. * If modp is NULL, reduction modulo P is done using a generic algorithm.
  6110. * Otherwise, it must point to a function that takes an mbedtls_mpi in the range
  6111. * 0..2^(2*pbits)-1 and transforms it in-place in an integer of little more
  6112. * than pbits, so that the integer may be efficiently brought in the 0..P-1
  6113. * range by a few additions or substractions. It must return 0 on success and
  6114. * non-zero on failure.
  6115. */
  6116. typedef struct
  6117. {
  6118. mbedtls_ecp_group_id id; /*!< internal group identifier */
  6119. mbedtls_mpi P; /*!< prime modulus of the base field */
  6120. mbedtls_mpi A; /*!< 1. A in the equation, or 2. (A + 2) / 4 */
  6121. mbedtls_mpi B; /*!< 1. B in the equation, or 2. unused */
  6122. mbedtls_ecp_point G; /*!< generator of the (sub)group used */
  6123. mbedtls_mpi N; /*!< 1. the order of G, or 2. unused */
  6124. size_t pbits; /*!< number of bits in P */
  6125. size_t nbits; /*!< number of bits in 1. P, or 2. private keys */
  6126. unsigned int h; /*!< internal: 1 if the constants are static */
  6127. int (*modp)(mbedtls_mpi *); /*!< function for fast reduction mod P */
  6128. int (*t_pre)(mbedtls_ecp_point *, void *); /*!< unused */
  6129. int (*t_post)(mbedtls_ecp_point *, void *); /*!< unused */
  6130. void *t_data; /*!< unused */
  6131. mbedtls_ecp_point *T; /*!< pre-computed points for ecp_mul_comb() */
  6132. size_t T_size; /*!< number for pre-computed points */
  6133. }
  6134. mbedtls_ecp_group;
  6135. /**
  6136. * \brief ECP key pair structure
  6137. *
  6138. * A generic key pair that could be used for ECDSA, fixed ECDH, etc.
  6139. *
  6140. * \note Members purposefully in the same order as struc mbedtls_ecdsa_context.
  6141. */
  6142. typedef struct
  6143. {
  6144. mbedtls_ecp_group grp; /*!< Elliptic curve and base point */
  6145. mbedtls_mpi d; /*!< our secret value */
  6146. mbedtls_ecp_point Q; /*!< our public value */
  6147. }
  6148. mbedtls_ecp_keypair;
  6149. /**
  6150. * \name SECTION: Module settings
  6151. *
  6152. * The configuration options you can set for this module are in this section.
  6153. * Either change them in config.h or define them on the compiler command line.
  6154. * \{
  6155. */
  6156. #if !defined(MBEDTLS_ECP_MAX_BITS)
  6157. /**
  6158. * Maximum size of the groups (that is, of N and P)
  6159. */
  6160. #define MBEDTLS_ECP_MAX_BITS 521 /**< Maximum bit size of groups */
  6161. #endif
  6162. #define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
  6163. #define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
  6164. #if !defined(MBEDTLS_ECP_WINDOW_SIZE)
  6165. /*
  6166. * Maximum "window" size used for point multiplication.
  6167. * Default: 6.
  6168. * Minimum value: 2. Maximum value: 7.
  6169. *
  6170. * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
  6171. * points used for point multiplication. This value is directly tied to EC
  6172. * peak memory usage, so decreasing it by one should roughly cut memory usage
  6173. * by two (if large curves are in use).
  6174. *
  6175. * Reduction in size may reduce speed, but larger curves are impacted first.
  6176. * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
  6177. * w-size: 6 5 4 3 2
  6178. * 521 145 141 135 120 97
  6179. * 384 214 209 198 177 146
  6180. * 256 320 320 303 262 226
  6181. * 224 475 475 453 398 342
  6182. * 192 640 640 633 587 476
  6183. */
  6184. #define MBEDTLS_ECP_WINDOW_SIZE 6 /**< Maximum window size used */
  6185. #endif /* MBEDTLS_ECP_WINDOW_SIZE */
  6186. #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
  6187. /*
  6188. * Trade memory for speed on fixed-point multiplication.
  6189. *
  6190. * This speeds up repeated multiplication of the generator (that is, the
  6191. * multiplication in ECDSA signatures, and half of the multiplications in
  6192. * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
  6193. *
  6194. * The cost is increasing EC peak memory usage by a factor roughly 2.
  6195. *
  6196. * Change this value to 0 to reduce peak memory usage.
  6197. */
  6198. #define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */
  6199. #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
  6200. /* \} name SECTION: Module settings */
  6201. /*
  6202. * Point formats, from RFC 4492's enum ECPointFormat
  6203. */
  6204. #define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
  6205. #define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format */
  6206. /*
  6207. * Some other constants from RFC 4492
  6208. */
  6209. #define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */
  6210. /**
  6211. * \brief Get the list of supported curves in order of preferrence
  6212. * (full information)
  6213. *
  6214. * \return A statically allocated array, the last entry is 0.
  6215. */
  6216. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
  6217. /**
  6218. * \brief Get the list of supported curves in order of preferrence
  6219. * (grp_id only)
  6220. *
  6221. * \return A statically allocated array,
  6222. * terminated with MBEDTLS_ECP_DP_NONE.
  6223. */
  6224. const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
  6225. /**
  6226. * \brief Get curve information from an internal group identifier
  6227. *
  6228. * \param grp_id A MBEDTLS_ECP_DP_XXX value
  6229. *
  6230. * \return The associated curve information or NULL
  6231. */
  6232. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id );
  6233. /**
  6234. * \brief Get curve information from a TLS NamedCurve value
  6235. *
  6236. * \param tls_id A MBEDTLS_ECP_DP_XXX value
  6237. *
  6238. * \return The associated curve information or NULL
  6239. */
  6240. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
  6241. /**
  6242. * \brief Get curve information from a human-readable name
  6243. *
  6244. * \param name The name
  6245. *
  6246. * \return The associated curve information or NULL
  6247. */
  6248. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
  6249. /**
  6250. * \brief Initialize a point (as zero)
  6251. */
  6252. void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
  6253. /**
  6254. * \brief Initialize a group (to something meaningless)
  6255. */
  6256. void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
  6257. /**
  6258. * \brief Initialize a key pair (as an invalid one)
  6259. */
  6260. void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
  6261. /**
  6262. * \brief Free the components of a point
  6263. */
  6264. void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
  6265. /**
  6266. * \brief Free the components of an ECP group
  6267. */
  6268. void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
  6269. /**
  6270. * \brief Free the components of a key pair
  6271. */
  6272. void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
  6273. /**
  6274. * \brief Copy the contents of point Q into P
  6275. *
  6276. * \param P Destination point
  6277. * \param Q Source point
  6278. *
  6279. * \return 0 if successful,
  6280. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  6281. */
  6282. int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
  6283. /**
  6284. * \brief Copy the contents of a group object
  6285. *
  6286. * \param dst Destination group
  6287. * \param src Source group
  6288. *
  6289. * \return 0 if successful,
  6290. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  6291. */
  6292. int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src );
  6293. /**
  6294. * \brief Set a point to zero
  6295. *
  6296. * \param pt Destination point
  6297. *
  6298. * \return 0 if successful,
  6299. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  6300. */
  6301. int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
  6302. /**
  6303. * \brief Tell if a point is zero
  6304. *
  6305. * \param pt Point to test
  6306. *
  6307. * \return 1 if point is zero, 0 otherwise
  6308. */
  6309. int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
  6310. /**
  6311. * \brief Compare two points
  6312. *
  6313. * \note This assumes the points are normalized. Otherwise,
  6314. * they may compare as "not equal" even if they are.
  6315. *
  6316. * \param P First point to compare
  6317. * \param Q Second point to compare
  6318. *
  6319. * \return 0 if the points are equal,
  6320. * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise
  6321. */
  6322. int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
  6323. const mbedtls_ecp_point *Q );
  6324. /**
  6325. * \brief Import a non-zero point from two ASCII strings
  6326. *
  6327. * \param P Destination point
  6328. * \param radix Input numeric base
  6329. * \param x First affine coordinate as a null-terminated string
  6330. * \param y Second affine coordinate as a null-terminated string
  6331. *
  6332. * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
  6333. */
  6334. int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
  6335. const char *x, const char *y );
  6336. /**
  6337. * \brief Export a point into unsigned binary data
  6338. *
  6339. * \param grp Group to which the point should belong
  6340. * \param P Point to export
  6341. * \param format Point format, should be a MBEDTLS_ECP_PF_XXX macro
  6342. * \param olen Length of the actual output
  6343. * \param buf Output buffer
  6344. * \param buflen Length of the output buffer
  6345. *
  6346. * \return 0 if successful,
  6347. * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
  6348. * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
  6349. */
  6350. int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
  6351. int format, size_t *olen,
  6352. unsigned char *buf, size_t buflen );
  6353. /**
  6354. * \brief Import a point from unsigned binary data
  6355. *
  6356. * \param grp Group to which the point should belong
  6357. * \param P Point to import
  6358. * \param buf Input buffer
  6359. * \param ilen Actual length of input
  6360. *
  6361. * \return 0 if successful,
  6362. * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid,
  6363. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  6364. * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
  6365. * is not implemented.
  6366. *
  6367. * \note This function does NOT check that the point actually
  6368. * belongs to the given group, see mbedtls_ecp_check_pubkey() for
  6369. * that.
  6370. */
  6371. int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
  6372. const unsigned char *buf, size_t ilen );
  6373. /**
  6374. * \brief Import a point from a TLS ECPoint record
  6375. *
  6376. * \param grp ECP group used
  6377. * \param pt Destination point
  6378. * \param buf $(Start of input buffer)
  6379. * \param len Buffer length
  6380. *
  6381. * \note buf is updated to point right after the ECPoint on exit
  6382. *
  6383. * \return 0 if successful,
  6384. * MBEDTLS_ERR_MPI_XXX if initialization failed
  6385. * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
  6386. */
  6387. int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
  6388. const unsigned char **buf, size_t len );
  6389. /**
  6390. * \brief Export a point as a TLS ECPoint record
  6391. *
  6392. * \param grp ECP group used
  6393. * \param pt Point to export
  6394. * \param format Export format
  6395. * \param olen length of data written
  6396. * \param buf Buffer to write to
  6397. * \param blen Buffer length
  6398. *
  6399. * \return 0 if successful,
  6400. * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
  6401. * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
  6402. */
  6403. int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
  6404. int format, size_t *olen,
  6405. unsigned char *buf, size_t blen );
  6406. /**
  6407. * \brief Set a group using well-known domain parameters
  6408. *
  6409. * \param grp Destination group
  6410. * \param index Index in the list of well-known domain parameters
  6411. *
  6412. * \return 0 if successful,
  6413. * MBEDTLS_ERR_MPI_XXX if initialization failed
  6414. * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups
  6415. *
  6416. * \note Index should be a value of RFC 4492's enum NamedCurve,
  6417. * usually in the form of a MBEDTLS_ECP_DP_XXX macro.
  6418. */
  6419. int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id index );
  6420. /**
  6421. * \brief Set a group from a TLS ECParameters record
  6422. *
  6423. * \param grp Destination group
  6424. * \param buf &(Start of input buffer)
  6425. * \param len Buffer length
  6426. *
  6427. * \note buf is updated to point right after ECParameters on exit
  6428. *
  6429. * \return 0 if successful,
  6430. * MBEDTLS_ERR_MPI_XXX if initialization failed
  6431. * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
  6432. */
  6433. int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len );
  6434. /**
  6435. * \brief Write the TLS ECParameters record for a group
  6436. *
  6437. * \param grp ECP group used
  6438. * \param olen Number of bytes actually written
  6439. * \param buf Buffer to write to
  6440. * \param blen Buffer length
  6441. *
  6442. * \return 0 if successful,
  6443. * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
  6444. */
  6445. int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
  6446. unsigned char *buf, size_t blen );
  6447. /**
  6448. * \brief Multiplication by an integer: R = m * P
  6449. * (Not thread-safe to use same group in multiple threads)
  6450. *
  6451. * \note In order to prevent timing attacks, this function
  6452. * executes the exact same sequence of (base field)
  6453. * operations for any valid m. It avoids any if-branch or
  6454. * array index depending on the value of m.
  6455. *
  6456. * \note If f_rng is not NULL, it is used to randomize intermediate
  6457. * results in order to prevent potential timing attacks
  6458. * targeting these results. It is recommended to always
  6459. * provide a non-NULL f_rng (the overhead is negligible).
  6460. *
  6461. * \param grp ECP group
  6462. * \param R Destination point
  6463. * \param m Integer by which to multiply
  6464. * \param P Point to multiply
  6465. * \param f_rng RNG function (see notes)
  6466. * \param p_rng RNG parameter
  6467. *
  6468. * \return 0 if successful,
  6469. * MBEDTLS_ERR_ECP_INVALID_KEY if m is not a valid privkey
  6470. * or P is not a valid pubkey,
  6471. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  6472. */
  6473. int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  6474. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  6475. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  6476. /**
  6477. * \brief Multiplication and addition of two points by integers:
  6478. * R = m * P + n * Q
  6479. * (Not thread-safe to use same group in multiple threads)
  6480. *
  6481. * \note In contrast to mbedtls_ecp_mul(), this function does not guarantee
  6482. * a constant execution flow and timing.
  6483. *
  6484. * \param grp ECP group
  6485. * \param R Destination point
  6486. * \param m Integer by which to multiply P
  6487. * \param P Point to multiply by m
  6488. * \param n Integer by which to multiply Q
  6489. * \param Q Point to be multiplied by n
  6490. *
  6491. * \return 0 if successful,
  6492. * MBEDTLS_ERR_ECP_INVALID_KEY if m or n is not a valid privkey
  6493. * or P or Q is not a valid pubkey,
  6494. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  6495. */
  6496. int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  6497. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  6498. const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
  6499. /**
  6500. * \brief Check that a point is a valid public key on this curve
  6501. *
  6502. * \param grp Curve/group the point should belong to
  6503. * \param pt Point to check
  6504. *
  6505. * \return 0 if point is a valid public key,
  6506. * MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
  6507. *
  6508. * \note This function only checks the point is non-zero, has valid
  6509. * coordinates and lies on the curve, but not that it is
  6510. * indeed a multiple of G. This is additional check is more
  6511. * expensive, isn't required by standards, and shouldn't be
  6512. * necessary if the group used has a small cofactor. In
  6513. * particular, it is useless for the NIST groups which all
  6514. * have a cofactor of 1.
  6515. *
  6516. * \note Uses bare components rather than an mbedtls_ecp_keypair structure
  6517. * in order to ease use with other structures such as
  6518. * mbedtls_ecdh_context of mbedtls_ecdsa_context.
  6519. */
  6520. int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt );
  6521. /**
  6522. * \brief Check that an mbedtls_mpi is a valid private key for this curve
  6523. *
  6524. * \param grp Group used
  6525. * \param d Integer to check
  6526. *
  6527. * \return 0 if point is a valid private key,
  6528. * MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
  6529. *
  6530. * \note Uses bare components rather than an mbedtls_ecp_keypair structure
  6531. * in order to ease use with other structures such as
  6532. * mbedtls_ecdh_context of mbedtls_ecdsa_context.
  6533. */
  6534. int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d );
  6535. /**
  6536. * \brief Generate a keypair with configurable base point
  6537. *
  6538. * \param grp ECP group
  6539. * \param G Chosen base point
  6540. * \param d Destination MPI (secret part)
  6541. * \param Q Destination point (public part)
  6542. * \param f_rng RNG function
  6543. * \param p_rng RNG parameter
  6544. *
  6545. * \return 0 if successful,
  6546. * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
  6547. *
  6548. * \note Uses bare components rather than an mbedtls_ecp_keypair structure
  6549. * in order to ease use with other structures such as
  6550. * mbedtls_ecdh_context of mbedtls_ecdsa_context.
  6551. */
  6552. int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
  6553. const mbedtls_ecp_point *G,
  6554. mbedtls_mpi *d, mbedtls_ecp_point *Q,
  6555. int (*f_rng)(void *, unsigned char *, size_t),
  6556. void *p_rng );
  6557. /**
  6558. * \brief Generate a keypair
  6559. *
  6560. * \param grp ECP group
  6561. * \param d Destination MPI (secret part)
  6562. * \param Q Destination point (public part)
  6563. * \param f_rng RNG function
  6564. * \param p_rng RNG parameter
  6565. *
  6566. * \return 0 if successful,
  6567. * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
  6568. *
  6569. * \note Uses bare components rather than an mbedtls_ecp_keypair structure
  6570. * in order to ease use with other structures such as
  6571. * mbedtls_ecdh_context of mbedtls_ecdsa_context.
  6572. */
  6573. int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
  6574. int (*f_rng)(void *, unsigned char *, size_t),
  6575. void *p_rng );
  6576. /**
  6577. * \brief Generate a keypair
  6578. *
  6579. * \param grp_id ECP group identifier
  6580. * \param key Destination keypair
  6581. * \param f_rng RNG function
  6582. * \param p_rng RNG parameter
  6583. *
  6584. * \return 0 if successful,
  6585. * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
  6586. */
  6587. int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
  6588. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  6589. /**
  6590. * \brief Check a public-private key pair
  6591. *
  6592. * \param pub Keypair structure holding a public key
  6593. * \param prv Keypair structure holding a private (plus public) key
  6594. *
  6595. * \return 0 if successful (keys are valid and match), or
  6596. * MBEDTLS_ERR_ECP_BAD_INPUT_DATA, or
  6597. * a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX code.
  6598. */
  6599. int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv );
  6600. #if defined(MBEDTLS_SELF_TEST)
  6601. /**
  6602. * \brief Checkup routine
  6603. *
  6604. * \return 0 if successful, or 1 if a test failed
  6605. */
  6606. int mbedtls_ecp_self_test( int verbose );
  6607. #endif
  6608. #ifdef __cplusplus
  6609. }
  6610. #endif
  6611. #endif /* ecp.h */
  6612. /********* Start of file include/mbedtls/ecdsa.h ************/
  6613. /**
  6614. * \file ecdsa.h
  6615. *
  6616. * \brief Elliptic curve DSA
  6617. *
  6618. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  6619. * SPDX-License-Identifier: Apache-2.0
  6620. *
  6621. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6622. * not use this file except in compliance with the License.
  6623. * You may obtain a copy of the License at
  6624. *
  6625. * http://www.apache.org/licenses/LICENSE-2.0
  6626. *
  6627. * Unless required by applicable law or agreed to in writing, software
  6628. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  6629. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6630. * See the License for the specific language governing permissions and
  6631. * limitations under the License.
  6632. *
  6633. * This file is part of mbed TLS (https://tls.mbed.org)
  6634. */
  6635. #ifndef MBEDTLS_ECDSA_H
  6636. #define MBEDTLS_ECDSA_H
  6637. /*
  6638. * RFC 4492 page 20:
  6639. *
  6640. * Ecdsa-Sig-Value ::= SEQUENCE {
  6641. * r INTEGER,
  6642. * s INTEGER
  6643. * }
  6644. *
  6645. * Size is at most
  6646. * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
  6647. * twice that + 1 (tag) + 2 (len) for the sequence
  6648. * (assuming ECP_MAX_BYTES is less than 126 for r and s,
  6649. * and less than 124 (total len <= 255) for the sequence)
  6650. */
  6651. #if MBEDTLS_ECP_MAX_BYTES > 124
  6652. #error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN"
  6653. #endif
  6654. /** Maximum size of an ECDSA signature in bytes */
  6655. #define MBEDTLS_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + MBEDTLS_ECP_MAX_BYTES ) )
  6656. /**
  6657. * \brief ECDSA context structure
  6658. */
  6659. typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
  6660. #ifdef __cplusplus
  6661. extern "C" {
  6662. #endif
  6663. /**
  6664. * \brief Compute ECDSA signature of a previously hashed message
  6665. *
  6666. * \note The deterministic version is usually prefered.
  6667. *
  6668. * \param grp ECP group
  6669. * \param r First output integer
  6670. * \param s Second output integer
  6671. * \param d Private signing key
  6672. * \param buf Message hash
  6673. * \param blen Length of buf
  6674. * \param f_rng RNG function
  6675. * \param p_rng RNG parameter
  6676. *
  6677. * \return 0 if successful,
  6678. * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
  6679. */
  6680. int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
  6681. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  6682. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  6683. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  6684. /**
  6685. * \brief Compute ECDSA signature of a previously hashed message,
  6686. * deterministic version (RFC 6979).
  6687. *
  6688. * \param grp ECP group
  6689. * \param r First output integer
  6690. * \param s Second output integer
  6691. * \param d Private signing key
  6692. * \param buf Message hash
  6693. * \param blen Length of buf
  6694. * \param md_alg MD algorithm used to hash the message
  6695. *
  6696. * \return 0 if successful,
  6697. * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
  6698. */
  6699. int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
  6700. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  6701. mbedtls_md_type_t md_alg );
  6702. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  6703. /**
  6704. * \brief Verify ECDSA signature of a previously hashed message
  6705. *
  6706. * \param grp ECP group
  6707. * \param buf Message hash
  6708. * \param blen Length of buf
  6709. * \param Q Public key to use for verification
  6710. * \param r First integer of the signature
  6711. * \param s Second integer of the signature
  6712. *
  6713. * \return 0 if successful,
  6714. * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid
  6715. * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
  6716. */
  6717. int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
  6718. const unsigned char *buf, size_t blen,
  6719. const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s);
  6720. /**
  6721. * \brief Compute ECDSA signature and write it to buffer,
  6722. * serialized as defined in RFC 4492 page 20.
  6723. * (Not thread-safe to use same context in multiple threads)
  6724. *
  6725. * \note The deterministice version (RFC 6979) is used if
  6726. * MBEDTLS_ECDSA_DETERMINISTIC is defined.
  6727. *
  6728. * \param ctx ECDSA context
  6729. * \param md_alg Algorithm that was used to hash the message
  6730. * \param hash Message hash
  6731. * \param hlen Length of hash
  6732. * \param sig Buffer that will hold the signature
  6733. * \param slen Length of the signature written
  6734. * \param f_rng RNG function
  6735. * \param p_rng RNG parameter
  6736. *
  6737. * \note The "sig" buffer must be at least as large as twice the
  6738. * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit
  6739. * curve is used). MBEDTLS_ECDSA_MAX_LEN is always safe.
  6740. *
  6741. * \return 0 if successful,
  6742. * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or
  6743. * MBEDTLS_ERR_ASN1_XXX error code
  6744. */
  6745. int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
  6746. const unsigned char *hash, size_t hlen,
  6747. unsigned char *sig, size_t *slen,
  6748. int (*f_rng)(void *, unsigned char *, size_t),
  6749. void *p_rng );
  6750. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  6751. #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
  6752. #if defined(MBEDTLS_DEPRECATED_WARNING)
  6753. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  6754. #else
  6755. #define MBEDTLS_DEPRECATED
  6756. #endif
  6757. /**
  6758. * \brief Compute ECDSA signature and write it to buffer,
  6759. * serialized as defined in RFC 4492 page 20.
  6760. * Deterministic version, RFC 6979.
  6761. * (Not thread-safe to use same context in multiple threads)
  6762. *
  6763. * \deprecated Superseded by mbedtls_ecdsa_write_signature() in 2.0.0
  6764. *
  6765. * \param ctx ECDSA context
  6766. * \param hash Message hash
  6767. * \param hlen Length of hash
  6768. * \param sig Buffer that will hold the signature
  6769. * \param slen Length of the signature written
  6770. * \param md_alg MD algorithm used to hash the message
  6771. *
  6772. * \note The "sig" buffer must be at least as large as twice the
  6773. * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit
  6774. * curve is used). MBEDTLS_ECDSA_MAX_LEN is always safe.
  6775. *
  6776. * \return 0 if successful,
  6777. * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or
  6778. * MBEDTLS_ERR_ASN1_XXX error code
  6779. */
  6780. int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
  6781. const unsigned char *hash, size_t hlen,
  6782. unsigned char *sig, size_t *slen,
  6783. mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
  6784. #undef MBEDTLS_DEPRECATED
  6785. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  6786. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  6787. /**
  6788. * \brief Read and verify an ECDSA signature
  6789. *
  6790. * \param ctx ECDSA context
  6791. * \param hash Message hash
  6792. * \param hlen Size of hash
  6793. * \param sig Signature to read and verify
  6794. * \param slen Size of sig
  6795. *
  6796. * \return 0 if successful,
  6797. * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
  6798. * MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if the signature is
  6799. * valid but its actual length is less than siglen,
  6800. * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX error code
  6801. */
  6802. int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
  6803. const unsigned char *hash, size_t hlen,
  6804. const unsigned char *sig, size_t slen );
  6805. /**
  6806. * \brief Generate an ECDSA keypair on the given curve
  6807. *
  6808. * \param ctx ECDSA context in which the keypair should be stored
  6809. * \param gid Group (elliptic curve) to use. One of the various
  6810. * MBEDTLS_ECP_DP_XXX macros depending on configuration.
  6811. * \param f_rng RNG function
  6812. * \param p_rng RNG parameter
  6813. *
  6814. * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code.
  6815. */
  6816. int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
  6817. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  6818. /**
  6819. * \brief Set an ECDSA context from an EC key pair
  6820. *
  6821. * \param ctx ECDSA context to set
  6822. * \param key EC key to use
  6823. *
  6824. * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code.
  6825. */
  6826. int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key );
  6827. /**
  6828. * \brief Initialize context
  6829. *
  6830. * \param ctx Context to initialize
  6831. */
  6832. void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
  6833. /**
  6834. * \brief Free context
  6835. *
  6836. * \param ctx Context to free
  6837. */
  6838. void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
  6839. #ifdef __cplusplus
  6840. }
  6841. #endif
  6842. #endif /* ecdsa.h */
  6843. /********* Start of file include/mbedtls/ecjpake.h ************/
  6844. /**
  6845. * \file ecjpake.h
  6846. *
  6847. * \brief Elliptic curve J-PAKE
  6848. *
  6849. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  6850. * SPDX-License-Identifier: Apache-2.0
  6851. *
  6852. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6853. * not use this file except in compliance with the License.
  6854. * You may obtain a copy of the License at
  6855. *
  6856. * http://www.apache.org/licenses/LICENSE-2.0
  6857. *
  6858. * Unless required by applicable law or agreed to in writing, software
  6859. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  6860. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6861. * See the License for the specific language governing permissions and
  6862. * limitations under the License.
  6863. *
  6864. * This file is part of mbed TLS (https://tls.mbed.org)
  6865. */
  6866. #ifndef MBEDTLS_ECJPAKE_H
  6867. #define MBEDTLS_ECJPAKE_H
  6868. /*
  6869. * J-PAKE is a password-authenticated key exchange that allows deriving a
  6870. * strong shared secret from a (potentially low entropy) pre-shared
  6871. * passphrase, with forward secrecy and mutual authentication.
  6872. * https://en.wikipedia.org/wiki/Password_Authenticated_Key_Exchange_by_Juggling
  6873. *
  6874. * This file implements the Elliptic Curve variant of J-PAKE,
  6875. * as defined in Chapter 7.4 of the Thread v1.0 Specification,
  6876. * available to members of the Thread Group http://threadgroup.org/
  6877. *
  6878. * As the J-PAKE algorithm is inherently symmetric, so is our API.
  6879. * Each party needs to send its first round message, in any order, to the
  6880. * other party, then each sends its second round message, in any order.
  6881. * The payloads are serialized in a way suitable for use in TLS, but could
  6882. * also be use outside TLS.
  6883. */
  6884. #ifdef __cplusplus
  6885. extern "C" {
  6886. #endif
  6887. /**
  6888. * Roles in the EC J-PAKE exchange
  6889. */
  6890. typedef enum {
  6891. MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */
  6892. MBEDTLS_ECJPAKE_SERVER, /**< Server */
  6893. } mbedtls_ecjpake_role;
  6894. /**
  6895. * EC J-PAKE context structure.
  6896. *
  6897. * J-PAKE is a symmetric protocol, except for the identifiers used in
  6898. * Zero-Knowledge Proofs, and the serialization of the second message
  6899. * (KeyExchange) as defined by the Thread spec.
  6900. *
  6901. * In order to benefit from this symmetry, we choose a different naming
  6902. * convetion from the Thread v1.0 spec. Correspondance is indicated in the
  6903. * description as a pair C: client name, S: server name
  6904. */
  6905. typedef struct
  6906. {
  6907. const mbedtls_md_info_t *md_info; /**< Hash to use */
  6908. mbedtls_ecp_group grp; /**< Elliptic curve */
  6909. mbedtls_ecjpake_role role; /**< Are we client or server? */
  6910. int point_format; /**< Format for point export */
  6911. mbedtls_ecp_point Xm1; /**< My public key 1 C: X1, S: X3 */
  6912. mbedtls_ecp_point Xm2; /**< My public key 2 C: X2, S: X4 */
  6913. mbedtls_ecp_point Xp1; /**< Peer public key 1 C: X3, S: X1 */
  6914. mbedtls_ecp_point Xp2; /**< Peer public key 2 C: X4, S: X2 */
  6915. mbedtls_ecp_point Xp; /**< Peer public key C: Xs, S: Xc */
  6916. mbedtls_mpi xm1; /**< My private key 1 C: x1, S: x3 */
  6917. mbedtls_mpi xm2; /**< My private key 2 C: x2, S: x4 */
  6918. mbedtls_mpi s; /**< Pre-shared secret (passphrase) */
  6919. } mbedtls_ecjpake_context;
  6920. /**
  6921. * \brief Initialize a context
  6922. * (just makes it ready for setup() or free()).
  6923. *
  6924. * \param ctx context to initialize
  6925. */
  6926. void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx );
  6927. /**
  6928. * \brief Set up a context for use
  6929. *
  6930. * \note Currently the only values for hash/curve allowed by the
  6931. * standard are MBEDTLS_MD_SHA256/MBEDTLS_ECP_DP_SECP256R1.
  6932. *
  6933. * \param ctx context to set up
  6934. * \param role Our role: client or server
  6935. * \param hash hash function to use (MBEDTLS_MD_XXX)
  6936. * \param curve elliptic curve identifier (MBEDTLS_ECP_DP_XXX)
  6937. * \param secret pre-shared secret (passphrase)
  6938. * \param len length of the shared secret
  6939. *
  6940. * \return 0 if successfull,
  6941. * a negative error code otherwise
  6942. */
  6943. int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx,
  6944. mbedtls_ecjpake_role role,
  6945. mbedtls_md_type_t hash,
  6946. mbedtls_ecp_group_id curve,
  6947. const unsigned char *secret,
  6948. size_t len );
  6949. /*
  6950. * \brief Check if a context is ready for use
  6951. *
  6952. * \param ctx Context to check
  6953. *
  6954. * \return 0 if the context is ready for use,
  6955. * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise
  6956. */
  6957. int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx );
  6958. /**
  6959. * \brief Generate and write the first round message
  6960. * (TLS: contents of the Client/ServerHello extension,
  6961. * excluding extension type and length bytes)
  6962. *
  6963. * \param ctx Context to use
  6964. * \param buf Buffer to write the contents to
  6965. * \param len Buffer size
  6966. * \param olen Will be updated with the number of bytes written
  6967. * \param f_rng RNG function
  6968. * \param p_rng RNG parameter
  6969. *
  6970. * \return 0 if successfull,
  6971. * a negative error code otherwise
  6972. */
  6973. int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx,
  6974. unsigned char *buf, size_t len, size_t *olen,
  6975. int (*f_rng)(void *, unsigned char *, size_t),
  6976. void *p_rng );
  6977. /**
  6978. * \brief Read and process the first round message
  6979. * (TLS: contents of the Client/ServerHello extension,
  6980. * excluding extension type and length bytes)
  6981. *
  6982. * \param ctx Context to use
  6983. * \param buf Pointer to extension contents
  6984. * \param len Extension length
  6985. *
  6986. * \return 0 if successfull,
  6987. * a negative error code otherwise
  6988. */
  6989. int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx,
  6990. const unsigned char *buf,
  6991. size_t len );
  6992. /**
  6993. * \brief Generate and write the second round message
  6994. * (TLS: contents of the Client/ServerKeyExchange)
  6995. *
  6996. * \param ctx Context to use
  6997. * \param buf Buffer to write the contents to
  6998. * \param len Buffer size
  6999. * \param olen Will be updated with the number of bytes written
  7000. * \param f_rng RNG function
  7001. * \param p_rng RNG parameter
  7002. *
  7003. * \return 0 if successfull,
  7004. * a negative error code otherwise
  7005. */
  7006. int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx,
  7007. unsigned char *buf, size_t len, size_t *olen,
  7008. int (*f_rng)(void *, unsigned char *, size_t),
  7009. void *p_rng );
  7010. /**
  7011. * \brief Read and process the second round message
  7012. * (TLS: contents of the Client/ServerKeyExchange)
  7013. *
  7014. * \param ctx Context to use
  7015. * \param buf Pointer to the message
  7016. * \param len Message length
  7017. *
  7018. * \return 0 if successfull,
  7019. * a negative error code otherwise
  7020. */
  7021. int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx,
  7022. const unsigned char *buf,
  7023. size_t len );
  7024. /**
  7025. * \brief Derive the shared secret
  7026. * (TLS: Pre-Master Secret)
  7027. *
  7028. * \param ctx Context to use
  7029. * \param buf Buffer to write the contents to
  7030. * \param len Buffer size
  7031. * \param olen Will be updated with the number of bytes written
  7032. * \param f_rng RNG function
  7033. * \param p_rng RNG parameter
  7034. *
  7035. * \return 0 if successfull,
  7036. * a negative error code otherwise
  7037. */
  7038. int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx,
  7039. unsigned char *buf, size_t len, size_t *olen,
  7040. int (*f_rng)(void *, unsigned char *, size_t),
  7041. void *p_rng );
  7042. /**
  7043. * \brief Free a context's content
  7044. *
  7045. * \param ctx context to free
  7046. */
  7047. void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx );
  7048. #if defined(MBEDTLS_SELF_TEST)
  7049. /**
  7050. * \brief Checkup routine
  7051. *
  7052. * \return 0 if successful, or 1 if a test failed
  7053. */
  7054. int mbedtls_ecjpake_self_test( int verbose );
  7055. #endif
  7056. #ifdef __cplusplus
  7057. }
  7058. #endif
  7059. #endif /* ecjpake.h */
  7060. /********* Start of file include/mbedtls/pk.h ************/
  7061. /**
  7062. * \file pk.h
  7063. *
  7064. * \brief Public Key abstraction layer
  7065. *
  7066. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7067. * SPDX-License-Identifier: Apache-2.0
  7068. *
  7069. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  7070. * not use this file except in compliance with the License.
  7071. * You may obtain a copy of the License at
  7072. *
  7073. * http://www.apache.org/licenses/LICENSE-2.0
  7074. *
  7075. * Unless required by applicable law or agreed to in writing, software
  7076. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  7077. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  7078. * See the License for the specific language governing permissions and
  7079. * limitations under the License.
  7080. *
  7081. * This file is part of mbed TLS (https://tls.mbed.org)
  7082. */
  7083. #ifndef MBEDTLS_PK_H
  7084. #define MBEDTLS_PK_H
  7085. #if !defined(MBEDTLS_CONFIG_FILE)
  7086. #else
  7087. #endif
  7088. #if defined(MBEDTLS_RSA_C)
  7089. #endif
  7090. #if defined(MBEDTLS_ECP_C)
  7091. #endif
  7092. #if defined(MBEDTLS_ECDSA_C)
  7093. #endif
  7094. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  7095. !defined(inline) && !defined(__cplusplus)
  7096. #define inline __inline
  7097. #endif
  7098. #define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80 /**< Memory allocation failed. */
  7099. #define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00 /**< Type mismatch, eg attempt to encrypt with an ECDSA key */
  7100. #define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80 /**< Bad input parameters to function. */
  7101. #define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00 /**< Read/write of file failed. */
  7102. #define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80 /**< Unsupported key version */
  7103. #define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00 /**< Invalid key tag or value. */
  7104. #define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80 /**< Key algorithm is unsupported (only RSA and EC are supported). */
  7105. #define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00 /**< Private key password can't be empty. */
  7106. #define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80 /**< Given private key password does not allow for correct decryption. */
  7107. #define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00 /**< The pubkey tag or value is invalid (only RSA and EC are supported). */
  7108. #define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80 /**< The algorithm tag or value is invalid. */
  7109. #define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00 /**< Elliptic curve is unsupported (only NIST curves are supported). */
  7110. #define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980 /**< Unavailable feature, e.g. RSA disabled for RSA key. */
  7111. #define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900 /**< The signature is valid but its length is less than expected. */
  7112. #ifdef __cplusplus
  7113. extern "C" {
  7114. #endif
  7115. /**
  7116. * \brief Public key types
  7117. */
  7118. typedef enum {
  7119. MBEDTLS_PK_NONE=0,
  7120. MBEDTLS_PK_RSA,
  7121. MBEDTLS_PK_ECKEY,
  7122. MBEDTLS_PK_ECKEY_DH,
  7123. MBEDTLS_PK_ECDSA,
  7124. MBEDTLS_PK_RSA_ALT,
  7125. MBEDTLS_PK_RSASSA_PSS,
  7126. } mbedtls_pk_type_t;
  7127. /**
  7128. * \brief Options for RSASSA-PSS signature verification.
  7129. * See \c mbedtls_rsa_rsassa_pss_verify_ext()
  7130. */
  7131. typedef struct
  7132. {
  7133. mbedtls_md_type_t mgf1_hash_id;
  7134. int expected_salt_len;
  7135. } mbedtls_pk_rsassa_pss_options;
  7136. /**
  7137. * \brief Types for interfacing with the debug module
  7138. */
  7139. typedef enum
  7140. {
  7141. MBEDTLS_PK_DEBUG_NONE = 0,
  7142. MBEDTLS_PK_DEBUG_MPI,
  7143. MBEDTLS_PK_DEBUG_ECP,
  7144. } mbedtls_pk_debug_type;
  7145. /**
  7146. * \brief Item to send to the debug module
  7147. */
  7148. typedef struct
  7149. {
  7150. mbedtls_pk_debug_type type;
  7151. const char *name;
  7152. void *value;
  7153. } mbedtls_pk_debug_item;
  7154. /** Maximum number of item send for debugging, plus 1 */
  7155. #define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
  7156. /**
  7157. * \brief Public key information and operations
  7158. */
  7159. typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
  7160. /**
  7161. * \brief Public key container
  7162. */
  7163. typedef struct
  7164. {
  7165. const mbedtls_pk_info_t * pk_info; /**< Public key informations */
  7166. void * pk_ctx; /**< Underlying public key context */
  7167. } mbedtls_pk_context;
  7168. #if defined(MBEDTLS_RSA_C)
  7169. /**
  7170. * Quick access to an RSA context inside a PK context.
  7171. *
  7172. * \warning You must make sure the PK context actually holds an RSA context
  7173. * before using this function!
  7174. */
  7175. static inline mbedtls_rsa_context *mbedtls_pk_rsa( const mbedtls_pk_context pk )
  7176. {
  7177. return( (mbedtls_rsa_context *) (pk).pk_ctx );
  7178. }
  7179. #endif /* MBEDTLS_RSA_C */
  7180. #if defined(MBEDTLS_ECP_C)
  7181. /**
  7182. * Quick access to an EC context inside a PK context.
  7183. *
  7184. * \warning You must make sure the PK context actually holds an EC context
  7185. * before using this function!
  7186. */
  7187. static inline mbedtls_ecp_keypair *mbedtls_pk_ec( const mbedtls_pk_context pk )
  7188. {
  7189. return( (mbedtls_ecp_keypair *) (pk).pk_ctx );
  7190. }
  7191. #endif /* MBEDTLS_ECP_C */
  7192. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  7193. /**
  7194. * \brief Types for RSA-alt abstraction
  7195. */
  7196. typedef int (*mbedtls_pk_rsa_alt_decrypt_func)( void *ctx, int mode, size_t *olen,
  7197. const unsigned char *input, unsigned char *output,
  7198. size_t output_max_len );
  7199. typedef int (*mbedtls_pk_rsa_alt_sign_func)( void *ctx,
  7200. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  7201. int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
  7202. const unsigned char *hash, unsigned char *sig );
  7203. typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx );
  7204. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  7205. /**
  7206. * \brief Return information associated with the given PK type
  7207. *
  7208. * \param pk_type PK type to search for.
  7209. *
  7210. * \return The PK info associated with the type or NULL if not found.
  7211. */
  7212. const mbedtls_pk_info_t *mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type );
  7213. /**
  7214. * \brief Initialize a mbedtls_pk_context (as NONE)
  7215. */
  7216. void mbedtls_pk_init( mbedtls_pk_context *ctx );
  7217. /**
  7218. * \brief Free a mbedtls_pk_context
  7219. */
  7220. void mbedtls_pk_free( mbedtls_pk_context *ctx );
  7221. /**
  7222. * \brief Initialize a PK context with the information given
  7223. * and allocates the type-specific PK subcontext.
  7224. *
  7225. * \param ctx Context to initialize. Must be empty (type NONE).
  7226. * \param info Information to use
  7227. *
  7228. * \return 0 on success,
  7229. * MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input,
  7230. * MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
  7231. *
  7232. * \note For contexts holding an RSA-alt key, use
  7233. * \c mbedtls_pk_setup_rsa_alt() instead.
  7234. */
  7235. int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
  7236. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  7237. /**
  7238. * \brief Initialize an RSA-alt context
  7239. *
  7240. * \param ctx Context to initialize. Must be empty (type NONE).
  7241. * \param key RSA key pointer
  7242. * \param decrypt_func Decryption function
  7243. * \param sign_func Signing function
  7244. * \param key_len_func Function returning key length in bytes
  7245. *
  7246. * \return 0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the
  7247. * context wasn't already initialized as RSA_ALT.
  7248. *
  7249. * \note This function replaces \c mbedtls_pk_setup() for RSA-alt.
  7250. */
  7251. int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
  7252. mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
  7253. mbedtls_pk_rsa_alt_sign_func sign_func,
  7254. mbedtls_pk_rsa_alt_key_len_func key_len_func );
  7255. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  7256. /**
  7257. * \brief Get the size in bits of the underlying key
  7258. *
  7259. * \param ctx Context to use
  7260. *
  7261. * \return Key size in bits, or 0 on error
  7262. */
  7263. size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx );
  7264. /**
  7265. * \brief Get the length in bytes of the underlying key
  7266. * \param ctx Context to use
  7267. *
  7268. * \return Key length in bytes, or 0 on error
  7269. */
  7270. static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
  7271. {
  7272. return( ( mbedtls_pk_get_bitlen( ctx ) + 7 ) / 8 );
  7273. }
  7274. /**
  7275. * \brief Tell if a context can do the operation given by type
  7276. *
  7277. * \param ctx Context to test
  7278. * \param type Target type
  7279. *
  7280. * \return 0 if context can't do the operations,
  7281. * 1 otherwise.
  7282. */
  7283. int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type );
  7284. /**
  7285. * \brief Verify signature (including padding if relevant).
  7286. *
  7287. * \param ctx PK context to use
  7288. * \param md_alg Hash algorithm used (see notes)
  7289. * \param hash Hash of the message to sign
  7290. * \param hash_len Hash length or 0 (see notes)
  7291. * \param sig Signature to verify
  7292. * \param sig_len Signature length
  7293. *
  7294. * \return 0 on success (signature is valid),
  7295. * MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if the signature is
  7296. * valid but its actual length is less than sig_len,
  7297. * or a specific error code.
  7298. *
  7299. * \note For RSA keys, the default padding type is PKCS#1 v1.5.
  7300. * Use \c mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, ... )
  7301. * to verify RSASSA_PSS signatures.
  7302. *
  7303. * \note If hash_len is 0, then the length associated with md_alg
  7304. * is used instead, or an error returned if it is invalid.
  7305. *
  7306. * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
  7307. */
  7308. int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  7309. const unsigned char *hash, size_t hash_len,
  7310. const unsigned char *sig, size_t sig_len );
  7311. /**
  7312. * \brief Verify signature, with options.
  7313. * (Includes verification of the padding depending on type.)
  7314. *
  7315. * \param type Signature type (inc. possible padding type) to verify
  7316. * \param options Pointer to type-specific options, or NULL
  7317. * \param ctx PK context to use
  7318. * \param md_alg Hash algorithm used (see notes)
  7319. * \param hash Hash of the message to sign
  7320. * \param hash_len Hash length or 0 (see notes)
  7321. * \param sig Signature to verify
  7322. * \param sig_len Signature length
  7323. *
  7324. * \return 0 on success (signature is valid),
  7325. * MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be
  7326. * used for this type of signatures,
  7327. * MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if the signature is
  7328. * valid but its actual length is less than sig_len,
  7329. * or a specific error code.
  7330. *
  7331. * \note If hash_len is 0, then the length associated with md_alg
  7332. * is used instead, or an error returned if it is invalid.
  7333. *
  7334. * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
  7335. *
  7336. * \note If type is MBEDTLS_PK_RSASSA_PSS, then options must point
  7337. * to a mbedtls_pk_rsassa_pss_options structure,
  7338. * otherwise it must be NULL.
  7339. */
  7340. int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
  7341. mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  7342. const unsigned char *hash, size_t hash_len,
  7343. const unsigned char *sig, size_t sig_len );
  7344. /**
  7345. * \brief Make signature, including padding if relevant.
  7346. *
  7347. * \param ctx PK context to use - must hold a private key
  7348. * \param md_alg Hash algorithm used (see notes)
  7349. * \param hash Hash of the message to sign
  7350. * \param hash_len Hash length or 0 (see notes)
  7351. * \param sig Place to write the signature
  7352. * \param sig_len Number of bytes written
  7353. * \param f_rng RNG function
  7354. * \param p_rng RNG parameter
  7355. *
  7356. * \return 0 on success, or a specific error code.
  7357. *
  7358. * \note For RSA keys, the default padding type is PKCS#1 v1.5.
  7359. * There is no interface in the PK module to make RSASSA-PSS
  7360. * signatures yet.
  7361. *
  7362. * \note If hash_len is 0, then the length associated with md_alg
  7363. * is used instead, or an error returned if it is invalid.
  7364. *
  7365. * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
  7366. * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
  7367. */
  7368. int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  7369. const unsigned char *hash, size_t hash_len,
  7370. unsigned char *sig, size_t *sig_len,
  7371. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  7372. /**
  7373. * \brief Decrypt message (including padding if relevant).
  7374. *
  7375. * \param ctx PK context to use - must hold a private key
  7376. * \param input Input to decrypt
  7377. * \param ilen Input size
  7378. * \param output Decrypted output
  7379. * \param olen Decrypted message length
  7380. * \param osize Size of the output buffer
  7381. * \param f_rng RNG function
  7382. * \param p_rng RNG parameter
  7383. *
  7384. * \note For RSA keys, the default padding type is PKCS#1 v1.5.
  7385. *
  7386. * \return 0 on success, or a specific error code.
  7387. */
  7388. int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
  7389. const unsigned char *input, size_t ilen,
  7390. unsigned char *output, size_t *olen, size_t osize,
  7391. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  7392. /**
  7393. * \brief Encrypt message (including padding if relevant).
  7394. *
  7395. * \param ctx PK context to use
  7396. * \param input Message to encrypt
  7397. * \param ilen Message size
  7398. * \param output Encrypted output
  7399. * \param olen Encrypted output length
  7400. * \param osize Size of the output buffer
  7401. * \param f_rng RNG function
  7402. * \param p_rng RNG parameter
  7403. *
  7404. * \note For RSA keys, the default padding type is PKCS#1 v1.5.
  7405. *
  7406. * \return 0 on success, or a specific error code.
  7407. */
  7408. int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
  7409. const unsigned char *input, size_t ilen,
  7410. unsigned char *output, size_t *olen, size_t osize,
  7411. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  7412. /**
  7413. * \brief Check if a public-private pair of keys matches.
  7414. *
  7415. * \param pub Context holding a public key.
  7416. * \param prv Context holding a private (and public) key.
  7417. *
  7418. * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
  7419. */
  7420. int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );
  7421. /**
  7422. * \brief Export debug information
  7423. *
  7424. * \param ctx Context to use
  7425. * \param items Place to write debug items
  7426. *
  7427. * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
  7428. */
  7429. int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items );
  7430. /**
  7431. * \brief Access the type name
  7432. *
  7433. * \param ctx Context to use
  7434. *
  7435. * \return Type name on success, or "invalid PK"
  7436. */
  7437. const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
  7438. /**
  7439. * \brief Get the key type
  7440. *
  7441. * \param ctx Context to use
  7442. *
  7443. * \return Type on success, or MBEDTLS_PK_NONE
  7444. */
  7445. mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx );
  7446. #if defined(MBEDTLS_PK_PARSE_C)
  7447. /** \ingroup pk_module */
  7448. /**
  7449. * \brief Parse a private key in PEM or DER format
  7450. *
  7451. * \param ctx key to be initialized
  7452. * \param key input buffer
  7453. * \param keylen size of the buffer
  7454. * (including the terminating null byte for PEM data)
  7455. * \param pwd password for decryption (optional)
  7456. * \param pwdlen size of the password
  7457. *
  7458. * \note On entry, ctx must be empty, either freshly initialised
  7459. * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
  7460. * specific key type, check the result with mbedtls_pk_can_do().
  7461. *
  7462. * \note The key is also checked for correctness.
  7463. *
  7464. * \return 0 if successful, or a specific PK or PEM error code
  7465. */
  7466. int mbedtls_pk_parse_key( mbedtls_pk_context *ctx,
  7467. const unsigned char *key, size_t keylen,
  7468. const unsigned char *pwd, size_t pwdlen );
  7469. /** \ingroup pk_module */
  7470. /**
  7471. * \brief Parse a public key in PEM or DER format
  7472. *
  7473. * \param ctx key to be initialized
  7474. * \param key input buffer
  7475. * \param keylen size of the buffer
  7476. * (including the terminating null byte for PEM data)
  7477. *
  7478. * \note On entry, ctx must be empty, either freshly initialised
  7479. * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
  7480. * specific key type, check the result with mbedtls_pk_can_do().
  7481. *
  7482. * \note The key is also checked for correctness.
  7483. *
  7484. * \return 0 if successful, or a specific PK or PEM error code
  7485. */
  7486. int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
  7487. const unsigned char *key, size_t keylen );
  7488. #if defined(MBEDTLS_FS_IO)
  7489. /** \ingroup pk_module */
  7490. /**
  7491. * \brief Load and parse a private key
  7492. *
  7493. * \param ctx key to be initialized
  7494. * \param path filename to read the private key from
  7495. * \param password password to decrypt the file (can be NULL)
  7496. *
  7497. * \note On entry, ctx must be empty, either freshly initialised
  7498. * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
  7499. * specific key type, check the result with mbedtls_pk_can_do().
  7500. *
  7501. * \note The key is also checked for correctness.
  7502. *
  7503. * \return 0 if successful, or a specific PK or PEM error code
  7504. */
  7505. int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
  7506. const char *path, const char *password );
  7507. /** \ingroup pk_module */
  7508. /**
  7509. * \brief Load and parse a public key
  7510. *
  7511. * \param ctx key to be initialized
  7512. * \param path filename to read the private key from
  7513. *
  7514. * \note On entry, ctx must be empty, either freshly initialised
  7515. * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
  7516. * specific key type, check the result with mbedtls_pk_can_do().
  7517. *
  7518. * \note The key is also checked for correctness.
  7519. *
  7520. * \return 0 if successful, or a specific PK or PEM error code
  7521. */
  7522. int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path );
  7523. #endif /* MBEDTLS_FS_IO */
  7524. #endif /* MBEDTLS_PK_PARSE_C */
  7525. #if defined(MBEDTLS_PK_WRITE_C)
  7526. /**
  7527. * \brief Write a private key to a PKCS#1 or SEC1 DER structure
  7528. * Note: data is written at the end of the buffer! Use the
  7529. * return value to determine where you should start
  7530. * using the buffer
  7531. *
  7532. * \param ctx private to write away
  7533. * \param buf buffer to write to
  7534. * \param size size of the buffer
  7535. *
  7536. * \return length of data written if successful, or a specific
  7537. * error code
  7538. */
  7539. int mbedtls_pk_write_key_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
  7540. /**
  7541. * \brief Write a public key to a SubjectPublicKeyInfo DER structure
  7542. * Note: data is written at the end of the buffer! Use the
  7543. * return value to determine where you should start
  7544. * using the buffer
  7545. *
  7546. * \param ctx public key to write away
  7547. * \param buf buffer to write to
  7548. * \param size size of the buffer
  7549. *
  7550. * \return length of data written if successful, or a specific
  7551. * error code
  7552. */
  7553. int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
  7554. #if defined(MBEDTLS_PEM_WRITE_C)
  7555. /**
  7556. * \brief Write a public key to a PEM string
  7557. *
  7558. * \param ctx public key to write away
  7559. * \param buf buffer to write to
  7560. * \param size size of the buffer
  7561. *
  7562. * \return 0 if successful, or a specific error code
  7563. */
  7564. int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
  7565. /**
  7566. * \brief Write a private key to a PKCS#1 or SEC1 PEM string
  7567. *
  7568. * \param ctx private to write away
  7569. * \param buf buffer to write to
  7570. * \param size size of the buffer
  7571. *
  7572. * \return 0 if successful, or a specific error code
  7573. */
  7574. int mbedtls_pk_write_key_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
  7575. #endif /* MBEDTLS_PEM_WRITE_C */
  7576. #endif /* MBEDTLS_PK_WRITE_C */
  7577. /*
  7578. * WARNING: Low-level functions. You probably do not want to use these unless
  7579. * you are certain you do ;)
  7580. */
  7581. #if defined(MBEDTLS_PK_PARSE_C)
  7582. /**
  7583. * \brief Parse a SubjectPublicKeyInfo DER structure
  7584. *
  7585. * \param p the position in the ASN.1 data
  7586. * \param end end of the buffer
  7587. * \param pk the key to fill
  7588. *
  7589. * \return 0 if successful, or a specific PK error code
  7590. */
  7591. int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
  7592. mbedtls_pk_context *pk );
  7593. #endif /* MBEDTLS_PK_PARSE_C */
  7594. #if defined(MBEDTLS_PK_WRITE_C)
  7595. /**
  7596. * \brief Write a subjectPublicKey to ASN.1 data
  7597. * Note: function works backwards in data buffer
  7598. *
  7599. * \param p reference to current position pointer
  7600. * \param start start of the buffer (for bounds-checking)
  7601. * \param key public key to write away
  7602. *
  7603. * \return the length written or a negative error code
  7604. */
  7605. int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
  7606. const mbedtls_pk_context *key );
  7607. #endif /* MBEDTLS_PK_WRITE_C */
  7608. /*
  7609. * Internal module functions. You probably do not want to use these unless you
  7610. * know you do.
  7611. */
  7612. #if defined(MBEDTLS_FS_IO)
  7613. int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
  7614. #endif
  7615. #ifdef __cplusplus
  7616. }
  7617. #endif
  7618. #endif /* MBEDTLS_PK_H */
  7619. /********* Start of file include/mbedtls/pk_internal.h ************/
  7620. /**
  7621. * \file pk.h
  7622. *
  7623. * \brief Public Key abstraction layer: wrapper functions
  7624. *
  7625. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7626. * SPDX-License-Identifier: Apache-2.0
  7627. *
  7628. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  7629. * not use this file except in compliance with the License.
  7630. * You may obtain a copy of the License at
  7631. *
  7632. * http://www.apache.org/licenses/LICENSE-2.0
  7633. *
  7634. * Unless required by applicable law or agreed to in writing, software
  7635. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  7636. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  7637. * See the License for the specific language governing permissions and
  7638. * limitations under the License.
  7639. *
  7640. * This file is part of mbed TLS (https://tls.mbed.org)
  7641. */
  7642. #ifndef MBEDTLS_PK_WRAP_H
  7643. #define MBEDTLS_PK_WRAP_H
  7644. #if !defined(MBEDTLS_CONFIG_FILE)
  7645. #else
  7646. #endif
  7647. struct mbedtls_pk_info_t
  7648. {
  7649. /** Public key type */
  7650. mbedtls_pk_type_t type;
  7651. /** Type name */
  7652. const char *name;
  7653. /** Get key size in bits */
  7654. size_t (*get_bitlen)( const void * );
  7655. /** Tell if the context implements this type (e.g. ECKEY can do ECDSA) */
  7656. int (*can_do)( mbedtls_pk_type_t type );
  7657. /** Verify signature */
  7658. int (*verify_func)( void *ctx, mbedtls_md_type_t md_alg,
  7659. const unsigned char *hash, size_t hash_len,
  7660. const unsigned char *sig, size_t sig_len );
  7661. /** Make signature */
  7662. int (*sign_func)( void *ctx, mbedtls_md_type_t md_alg,
  7663. const unsigned char *hash, size_t hash_len,
  7664. unsigned char *sig, size_t *sig_len,
  7665. int (*f_rng)(void *, unsigned char *, size_t),
  7666. void *p_rng );
  7667. /** Decrypt message */
  7668. int (*decrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
  7669. unsigned char *output, size_t *olen, size_t osize,
  7670. int (*f_rng)(void *, unsigned char *, size_t),
  7671. void *p_rng );
  7672. /** Encrypt message */
  7673. int (*encrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
  7674. unsigned char *output, size_t *olen, size_t osize,
  7675. int (*f_rng)(void *, unsigned char *, size_t),
  7676. void *p_rng );
  7677. /** Check public-private key pair */
  7678. int (*check_pair_func)( const void *pub, const void *prv );
  7679. /** Allocate a new context */
  7680. void * (*ctx_alloc_func)( void );
  7681. /** Free the given context */
  7682. void (*ctx_free_func)( void *ctx );
  7683. /** Interface with the debug module */
  7684. void (*debug_func)( const void *ctx, mbedtls_pk_debug_item *items );
  7685. };
  7686. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  7687. /* Container for RSA-alt */
  7688. typedef struct
  7689. {
  7690. void *key;
  7691. mbedtls_pk_rsa_alt_decrypt_func decrypt_func;
  7692. mbedtls_pk_rsa_alt_sign_func sign_func;
  7693. mbedtls_pk_rsa_alt_key_len_func key_len_func;
  7694. } mbedtls_rsa_alt_context;
  7695. #endif
  7696. #if defined(MBEDTLS_RSA_C)
  7697. extern const mbedtls_pk_info_t mbedtls_rsa_info;
  7698. #endif
  7699. #if defined(MBEDTLS_ECP_C)
  7700. extern const mbedtls_pk_info_t mbedtls_eckey_info;
  7701. extern const mbedtls_pk_info_t mbedtls_eckeydh_info;
  7702. #endif
  7703. #if defined(MBEDTLS_ECDSA_C)
  7704. extern const mbedtls_pk_info_t mbedtls_ecdsa_info;
  7705. #endif
  7706. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  7707. extern const mbedtls_pk_info_t mbedtls_rsa_alt_info;
  7708. #endif
  7709. #endif /* MBEDTLS_PK_WRAP_H */
  7710. /********* Start of file include/mbedtls/x509.h ************/
  7711. /**
  7712. * \file x509.h
  7713. *
  7714. * \brief X.509 generic defines and structures
  7715. *
  7716. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7717. * SPDX-License-Identifier: Apache-2.0
  7718. *
  7719. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  7720. * not use this file except in compliance with the License.
  7721. * You may obtain a copy of the License at
  7722. *
  7723. * http://www.apache.org/licenses/LICENSE-2.0
  7724. *
  7725. * Unless required by applicable law or agreed to in writing, software
  7726. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  7727. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  7728. * See the License for the specific language governing permissions and
  7729. * limitations under the License.
  7730. *
  7731. * This file is part of mbed TLS (https://tls.mbed.org)
  7732. */
  7733. #ifndef MBEDTLS_X509_H
  7734. #define MBEDTLS_X509_H
  7735. #if !defined(MBEDTLS_CONFIG_FILE)
  7736. #else
  7737. #endif
  7738. #if defined(MBEDTLS_RSA_C)
  7739. #endif
  7740. /**
  7741. * \addtogroup x509_module
  7742. * \{
  7743. */
  7744. #if !defined(MBEDTLS_X509_MAX_INTERMEDIATE_CA)
  7745. /**
  7746. * Maximum number of intermediate CAs in a verification chain.
  7747. * That is, maximum length of the chain, excluding the end-entity certificate
  7748. * and the trusted root certificate.
  7749. *
  7750. * Set this to a low value to prevent an adversary from making you waste
  7751. * resources verifying an overlong certificate chain.
  7752. */
  7753. #define MBEDTLS_X509_MAX_INTERMEDIATE_CA 8
  7754. #endif
  7755. /**
  7756. * \name X509 Error codes
  7757. * \{
  7758. */
  7759. #define MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE -0x2080 /**< Unavailable feature, e.g. RSA hashing/encryption combination. */
  7760. #define MBEDTLS_ERR_X509_UNKNOWN_OID -0x2100 /**< Requested OID is unknown. */
  7761. #define MBEDTLS_ERR_X509_INVALID_FORMAT -0x2180 /**< The CRT/CRL/CSR format is invalid, e.g. different type expected. */
  7762. #define MBEDTLS_ERR_X509_INVALID_VERSION -0x2200 /**< The CRT/CRL/CSR version element is invalid. */
  7763. #define MBEDTLS_ERR_X509_INVALID_SERIAL -0x2280 /**< The serial tag or value is invalid. */
  7764. #define MBEDTLS_ERR_X509_INVALID_ALG -0x2300 /**< The algorithm tag or value is invalid. */
  7765. #define MBEDTLS_ERR_X509_INVALID_NAME -0x2380 /**< The name tag or value is invalid. */
  7766. #define MBEDTLS_ERR_X509_INVALID_DATE -0x2400 /**< The date tag or value is invalid. */
  7767. #define MBEDTLS_ERR_X509_INVALID_SIGNATURE -0x2480 /**< The signature tag or value invalid. */
  7768. #define MBEDTLS_ERR_X509_INVALID_EXTENSIONS -0x2500 /**< The extension tag or value is invalid. */
  7769. #define MBEDTLS_ERR_X509_UNKNOWN_VERSION -0x2580 /**< CRT/CRL/CSR has an unsupported version number. */
  7770. #define MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG -0x2600 /**< Signature algorithm (oid) is unsupported. */
  7771. #define MBEDTLS_ERR_X509_SIG_MISMATCH -0x2680 /**< Signature algorithms do not match. (see \c ::mbedtls_x509_crt sig_oid) */
  7772. #define MBEDTLS_ERR_X509_CERT_VERIFY_FAILED -0x2700 /**< Certificate verification failed, e.g. CRL, CA or signature check failed. */
  7773. #define MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT -0x2780 /**< Format not recognized as DER or PEM. */
  7774. #define MBEDTLS_ERR_X509_BAD_INPUT_DATA -0x2800 /**< Input invalid. */
  7775. #define MBEDTLS_ERR_X509_ALLOC_FAILED -0x2880 /**< Allocation of memory failed. */
  7776. #define MBEDTLS_ERR_X509_FILE_IO_ERROR -0x2900 /**< Read/write of file failed. */
  7777. #define MBEDTLS_ERR_X509_BUFFER_TOO_SMALL -0x2980 /**< Destination buffer is too small. */
  7778. /* \} name */
  7779. /**
  7780. * \name X509 Verify codes
  7781. * \{
  7782. */
  7783. /* Reminder: update x509_crt_verify_strings[] in library/x509_crt.c */
  7784. #define MBEDTLS_X509_BADCERT_EXPIRED 0x01 /**< The certificate validity has expired. */
  7785. #define MBEDTLS_X509_BADCERT_REVOKED 0x02 /**< The certificate has been revoked (is on a CRL). */
  7786. #define MBEDTLS_X509_BADCERT_CN_MISMATCH 0x04 /**< The certificate Common Name (CN) does not match with the expected CN. */
  7787. #define MBEDTLS_X509_BADCERT_NOT_TRUSTED 0x08 /**< The certificate is not correctly signed by the trusted CA. */
  7788. #define MBEDTLS_X509_BADCRL_NOT_TRUSTED 0x10 /**< The CRL is not correctly signed by the trusted CA. */
  7789. #define MBEDTLS_X509_BADCRL_EXPIRED 0x20 /**< The CRL is expired. */
  7790. #define MBEDTLS_X509_BADCERT_MISSING 0x40 /**< Certificate was missing. */
  7791. #define MBEDTLS_X509_BADCERT_SKIP_VERIFY 0x80 /**< Certificate verification was skipped. */
  7792. #define MBEDTLS_X509_BADCERT_OTHER 0x0100 /**< Other reason (can be used by verify callback) */
  7793. #define MBEDTLS_X509_BADCERT_FUTURE 0x0200 /**< The certificate validity starts in the future. */
  7794. #define MBEDTLS_X509_BADCRL_FUTURE 0x0400 /**< The CRL is from the future */
  7795. #define MBEDTLS_X509_BADCERT_KEY_USAGE 0x0800 /**< Usage does not match the keyUsage extension. */
  7796. #define MBEDTLS_X509_BADCERT_EXT_KEY_USAGE 0x1000 /**< Usage does not match the extendedKeyUsage extension. */
  7797. #define MBEDTLS_X509_BADCERT_NS_CERT_TYPE 0x2000 /**< Usage does not match the nsCertType extension. */
  7798. #define MBEDTLS_X509_BADCERT_BAD_MD 0x4000 /**< The certificate is signed with an unacceptable hash. */
  7799. #define MBEDTLS_X509_BADCERT_BAD_PK 0x8000 /**< The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA). */
  7800. #define MBEDTLS_X509_BADCERT_BAD_KEY 0x010000 /**< The certificate is signed with an unacceptable key (eg bad curve, RSA too short). */
  7801. #define MBEDTLS_X509_BADCRL_BAD_MD 0x020000 /**< The CRL is signed with an unacceptable hash. */
  7802. #define MBEDTLS_X509_BADCRL_BAD_PK 0x040000 /**< The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA). */
  7803. #define MBEDTLS_X509_BADCRL_BAD_KEY 0x080000 /**< The CRL is signed with an unacceptable key (eg bad curve, RSA too short). */
  7804. /* \} name */
  7805. /* \} addtogroup x509_module */
  7806. /*
  7807. * X.509 v3 Key Usage Extension flags
  7808. * Reminder: update x509_info_key_usage() when adding new flags.
  7809. */
  7810. #define MBEDTLS_X509_KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
  7811. #define MBEDTLS_X509_KU_NON_REPUDIATION (0x40) /* bit 1 */
  7812. #define MBEDTLS_X509_KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */
  7813. #define MBEDTLS_X509_KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */
  7814. #define MBEDTLS_X509_KU_KEY_AGREEMENT (0x08) /* bit 4 */
  7815. #define MBEDTLS_X509_KU_KEY_CERT_SIGN (0x04) /* bit 5 */
  7816. #define MBEDTLS_X509_KU_CRL_SIGN (0x02) /* bit 6 */
  7817. #define MBEDTLS_X509_KU_ENCIPHER_ONLY (0x01) /* bit 7 */
  7818. #define MBEDTLS_X509_KU_DECIPHER_ONLY (0x8000) /* bit 8 */
  7819. /*
  7820. * Netscape certificate types
  7821. * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
  7822. */
  7823. #define MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */
  7824. #define MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */
  7825. #define MBEDTLS_X509_NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */
  7826. #define MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */
  7827. #define MBEDTLS_X509_NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */
  7828. #define MBEDTLS_X509_NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */
  7829. #define MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */
  7830. #define MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */
  7831. /*
  7832. * X.509 extension types
  7833. *
  7834. * Comments refer to the status for using certificates. Status can be
  7835. * different for writing certificates or reading CRLs or CSRs.
  7836. */
  7837. #define MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0)
  7838. #define MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER (1 << 1)
  7839. #define MBEDTLS_X509_EXT_KEY_USAGE (1 << 2)
  7840. #define MBEDTLS_X509_EXT_CERTIFICATE_POLICIES (1 << 3)
  7841. #define MBEDTLS_X509_EXT_POLICY_MAPPINGS (1 << 4)
  7842. #define MBEDTLS_X509_EXT_SUBJECT_ALT_NAME (1 << 5) /* Supported (DNS) */
  7843. #define MBEDTLS_X509_EXT_ISSUER_ALT_NAME (1 << 6)
  7844. #define MBEDTLS_X509_EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7)
  7845. #define MBEDTLS_X509_EXT_BASIC_CONSTRAINTS (1 << 8) /* Supported */
  7846. #define MBEDTLS_X509_EXT_NAME_CONSTRAINTS (1 << 9)
  7847. #define MBEDTLS_X509_EXT_POLICY_CONSTRAINTS (1 << 10)
  7848. #define MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE (1 << 11)
  7849. #define MBEDTLS_X509_EXT_CRL_DISTRIBUTION_POINTS (1 << 12)
  7850. #define MBEDTLS_X509_EXT_INIHIBIT_ANYPOLICY (1 << 13)
  7851. #define MBEDTLS_X509_EXT_FRESHEST_CRL (1 << 14)
  7852. #define MBEDTLS_X509_EXT_NS_CERT_TYPE (1 << 16) /* Parsed (and then ?) */
  7853. /*
  7854. * Storage format identifiers
  7855. * Recognized formats: PEM and DER
  7856. */
  7857. #define MBEDTLS_X509_FORMAT_DER 1
  7858. #define MBEDTLS_X509_FORMAT_PEM 2
  7859. #define MBEDTLS_X509_MAX_DN_NAME_SIZE 256 /**< Maximum value size of a DN entry */
  7860. #ifdef __cplusplus
  7861. extern "C" {
  7862. #endif
  7863. /**
  7864. * \addtogroup x509_module
  7865. * \{ */
  7866. /**
  7867. * \name Structures for parsing X.509 certificates, CRLs and CSRs
  7868. * \{
  7869. */
  7870. /**
  7871. * Type-length-value structure that allows for ASN1 using DER.
  7872. */
  7873. typedef mbedtls_asn1_buf mbedtls_x509_buf;
  7874. /**
  7875. * Container for ASN1 bit strings.
  7876. */
  7877. typedef mbedtls_asn1_bitstring mbedtls_x509_bitstring;
  7878. /**
  7879. * Container for ASN1 named information objects.
  7880. * It allows for Relative Distinguished Names (e.g. cn=localhost,ou=code,etc.).
  7881. */
  7882. typedef mbedtls_asn1_named_data mbedtls_x509_name;
  7883. /**
  7884. * Container for a sequence of ASN.1 items
  7885. */
  7886. typedef mbedtls_asn1_sequence mbedtls_x509_sequence;
  7887. /** Container for date and time (precision in seconds). */
  7888. typedef struct mbedtls_x509_time
  7889. {
  7890. int year, mon, day; /**< Date. */
  7891. int hour, min, sec; /**< Time. */
  7892. }
  7893. mbedtls_x509_time;
  7894. /** \} name Structures for parsing X.509 certificates, CRLs and CSRs */
  7895. /** \} addtogroup x509_module */
  7896. /**
  7897. * \brief Store the certificate DN in printable form into buf;
  7898. * no more than size characters will be written.
  7899. *
  7900. * \param buf Buffer to write to
  7901. * \param size Maximum size of buffer
  7902. * \param dn The X509 name to represent
  7903. *
  7904. * \return The length of the string written (not including the
  7905. * terminated nul byte), or a negative error code.
  7906. */
  7907. int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn );
  7908. /**
  7909. * \brief Store the certificate serial in printable form into buf;
  7910. * no more than size characters will be written.
  7911. *
  7912. * \param buf Buffer to write to
  7913. * \param size Maximum size of buffer
  7914. * \param serial The X509 serial to represent
  7915. *
  7916. * \return The length of the string written (not including the
  7917. * terminated nul byte), or a negative error code.
  7918. */
  7919. int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial );
  7920. /**
  7921. * \brief Check a given mbedtls_x509_time against the system time
  7922. * and tell if it's in the past.
  7923. *
  7924. * \note Intended usage is "if( is_past( valid_to ) ) ERROR".
  7925. * Hence the return value of 1 if on internal errors.
  7926. *
  7927. * \param time mbedtls_x509_time to check
  7928. *
  7929. * \return 1 if the given time is in the past or an error occured,
  7930. * 0 otherwise.
  7931. */
  7932. int mbedtls_x509_time_is_past( const mbedtls_x509_time *time );
  7933. /**
  7934. * \brief Check a given mbedtls_x509_time against the system time
  7935. * and tell if it's in the future.
  7936. *
  7937. * \note Intended usage is "if( is_future( valid_from ) ) ERROR".
  7938. * Hence the return value of 1 if on internal errors.
  7939. *
  7940. * \param time mbedtls_x509_time to check
  7941. *
  7942. * \return 1 if the given time is in the future or an error occured,
  7943. * 0 otherwise.
  7944. */
  7945. int mbedtls_x509_time_is_future( const mbedtls_x509_time *time );
  7946. /**
  7947. * \brief Checkup routine
  7948. *
  7949. * \return 0 if successful, or 1 if the test failed
  7950. */
  7951. int mbedtls_x509_self_test( int verbose );
  7952. /*
  7953. * Internal module functions. You probably do not want to use these unless you
  7954. * know you do.
  7955. */
  7956. int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
  7957. mbedtls_x509_name *cur );
  7958. int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
  7959. mbedtls_x509_buf *alg );
  7960. int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
  7961. mbedtls_x509_buf *alg, mbedtls_x509_buf *params );
  7962. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  7963. int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
  7964. mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
  7965. int *salt_len );
  7966. #endif
  7967. int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig );
  7968. int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
  7969. mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
  7970. void **sig_opts );
  7971. int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
  7972. mbedtls_x509_time *time );
  7973. int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
  7974. mbedtls_x509_buf *serial );
  7975. int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
  7976. mbedtls_x509_buf *ext, int tag );
  7977. int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
  7978. mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
  7979. const void *sig_opts );
  7980. int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name );
  7981. int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *name );
  7982. int mbedtls_x509_set_extension( mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
  7983. int critical, const unsigned char *val,
  7984. size_t val_len );
  7985. int mbedtls_x509_write_extensions( unsigned char **p, unsigned char *start,
  7986. mbedtls_asn1_named_data *first );
  7987. int mbedtls_x509_write_names( unsigned char **p, unsigned char *start,
  7988. mbedtls_asn1_named_data *first );
  7989. int mbedtls_x509_write_sig( unsigned char **p, unsigned char *start,
  7990. const char *oid, size_t oid_len,
  7991. unsigned char *sig, size_t size );
  7992. #define MBEDTLS_X509_SAFE_SNPRINTF \
  7993. do { \
  7994. if( ret < 0 || (size_t) ret >= n ) \
  7995. return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL ); \
  7996. \
  7997. n -= (size_t) ret; \
  7998. p += (size_t) ret; \
  7999. } while( 0 )
  8000. #ifdef __cplusplus
  8001. }
  8002. #endif
  8003. #endif /* x509.h */
  8004. /********* Start of file include/mbedtls/x509_crl.h ************/
  8005. /**
  8006. * \file x509_crl.h
  8007. *
  8008. * \brief X.509 certificate revocation list parsing
  8009. *
  8010. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  8011. * SPDX-License-Identifier: Apache-2.0
  8012. *
  8013. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8014. * not use this file except in compliance with the License.
  8015. * You may obtain a copy of the License at
  8016. *
  8017. * http://www.apache.org/licenses/LICENSE-2.0
  8018. *
  8019. * Unless required by applicable law or agreed to in writing, software
  8020. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  8021. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8022. * See the License for the specific language governing permissions and
  8023. * limitations under the License.
  8024. *
  8025. * This file is part of mbed TLS (https://tls.mbed.org)
  8026. */
  8027. #ifndef MBEDTLS_X509_CRL_H
  8028. #define MBEDTLS_X509_CRL_H
  8029. #if !defined(MBEDTLS_CONFIG_FILE)
  8030. #else
  8031. #endif
  8032. #ifdef __cplusplus
  8033. extern "C" {
  8034. #endif
  8035. /**
  8036. * \addtogroup x509_module
  8037. * \{ */
  8038. /**
  8039. * \name Structures and functions for parsing CRLs
  8040. * \{
  8041. */
  8042. /**
  8043. * Certificate revocation list entry.
  8044. * Contains the CA-specific serial numbers and revocation dates.
  8045. */
  8046. typedef struct mbedtls_x509_crl_entry
  8047. {
  8048. mbedtls_x509_buf raw;
  8049. mbedtls_x509_buf serial;
  8050. mbedtls_x509_time revocation_date;
  8051. mbedtls_x509_buf entry_ext;
  8052. struct mbedtls_x509_crl_entry *next;
  8053. }
  8054. mbedtls_x509_crl_entry;
  8055. /**
  8056. * Certificate revocation list structure.
  8057. * Every CRL may have multiple entries.
  8058. */
  8059. typedef struct mbedtls_x509_crl
  8060. {
  8061. mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
  8062. mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
  8063. int version; /**< CRL version (1=v1, 2=v2) */
  8064. mbedtls_x509_buf sig_oid; /**< CRL signature type identifier */
  8065. mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). */
  8066. mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
  8067. mbedtls_x509_time this_update;
  8068. mbedtls_x509_time next_update;
  8069. mbedtls_x509_crl_entry entry; /**< The CRL entries containing the certificate revocation times for this CA. */
  8070. mbedtls_x509_buf crl_ext;
  8071. mbedtls_x509_buf sig_oid2;
  8072. mbedtls_x509_buf sig;
  8073. mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
  8074. mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
  8075. void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
  8076. struct mbedtls_x509_crl *next;
  8077. }
  8078. mbedtls_x509_crl;
  8079. /**
  8080. * \brief Parse a DER-encoded CRL and append it to the chained list
  8081. *
  8082. * \param chain points to the start of the chain
  8083. * \param buf buffer holding the CRL data in DER format
  8084. * \param buflen size of the buffer
  8085. * (including the terminating null byte for PEM data)
  8086. *
  8087. * \return 0 if successful, or a specific X509 or PEM error code
  8088. */
  8089. int mbedtls_x509_crl_parse_der( mbedtls_x509_crl *chain,
  8090. const unsigned char *buf, size_t buflen );
  8091. /**
  8092. * \brief Parse one or more CRLs and append them to the chained list
  8093. *
  8094. * \note Mutliple CRLs are accepted only if using PEM format
  8095. *
  8096. * \param chain points to the start of the chain
  8097. * \param buf buffer holding the CRL data in PEM or DER format
  8098. * \param buflen size of the buffer
  8099. * (including the terminating null byte for PEM data)
  8100. *
  8101. * \return 0 if successful, or a specific X509 or PEM error code
  8102. */
  8103. int mbedtls_x509_crl_parse( mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen );
  8104. #if defined(MBEDTLS_FS_IO)
  8105. /**
  8106. * \brief Load one or more CRLs and append them to the chained list
  8107. *
  8108. * \note Mutliple CRLs are accepted only if using PEM format
  8109. *
  8110. * \param chain points to the start of the chain
  8111. * \param path filename to read the CRLs from (in PEM or DER encoding)
  8112. *
  8113. * \return 0 if successful, or a specific X509 or PEM error code
  8114. */
  8115. int mbedtls_x509_crl_parse_file( mbedtls_x509_crl *chain, const char *path );
  8116. #endif /* MBEDTLS_FS_IO */
  8117. /**
  8118. * \brief Returns an informational string about the CRL.
  8119. *
  8120. * \param buf Buffer to write to
  8121. * \param size Maximum size of buffer
  8122. * \param prefix A line prefix
  8123. * \param crl The X509 CRL to represent
  8124. *
  8125. * \return The length of the string written (not including the
  8126. * terminated nul byte), or a negative error code.
  8127. */
  8128. int mbedtls_x509_crl_info( char *buf, size_t size, const char *prefix,
  8129. const mbedtls_x509_crl *crl );
  8130. /**
  8131. * \brief Initialize a CRL (chain)
  8132. *
  8133. * \param crl CRL chain to initialize
  8134. */
  8135. void mbedtls_x509_crl_init( mbedtls_x509_crl *crl );
  8136. /**
  8137. * \brief Unallocate all CRL data
  8138. *
  8139. * \param crl CRL chain to free
  8140. */
  8141. void mbedtls_x509_crl_free( mbedtls_x509_crl *crl );
  8142. /* \} name */
  8143. /* \} addtogroup x509_module */
  8144. #ifdef __cplusplus
  8145. }
  8146. #endif
  8147. #endif /* mbedtls_x509_crl.h */
  8148. /********* Start of file include/mbedtls/x509_crt.h ************/
  8149. /**
  8150. * \file x509_crt.h
  8151. *
  8152. * \brief X.509 certificate parsing and writing
  8153. *
  8154. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  8155. * SPDX-License-Identifier: Apache-2.0
  8156. *
  8157. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8158. * not use this file except in compliance with the License.
  8159. * You may obtain a copy of the License at
  8160. *
  8161. * http://www.apache.org/licenses/LICENSE-2.0
  8162. *
  8163. * Unless required by applicable law or agreed to in writing, software
  8164. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  8165. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8166. * See the License for the specific language governing permissions and
  8167. * limitations under the License.
  8168. *
  8169. * This file is part of mbed TLS (https://tls.mbed.org)
  8170. */
  8171. #ifndef MBEDTLS_X509_CRT_H
  8172. #define MBEDTLS_X509_CRT_H
  8173. #if !defined(MBEDTLS_CONFIG_FILE)
  8174. #else
  8175. #endif
  8176. /**
  8177. * \addtogroup x509_module
  8178. * \{
  8179. */
  8180. #ifdef __cplusplus
  8181. extern "C" {
  8182. #endif
  8183. /**
  8184. * \name Structures and functions for parsing and writing X.509 certificates
  8185. * \{
  8186. */
  8187. /**
  8188. * Container for an X.509 certificate. The certificate may be chained.
  8189. */
  8190. typedef struct mbedtls_x509_crt
  8191. {
  8192. mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
  8193. mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
  8194. int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
  8195. mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
  8196. mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
  8197. mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
  8198. mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
  8199. mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
  8200. mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
  8201. mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
  8202. mbedtls_x509_time valid_to; /**< End time of certificate validity. */
  8203. mbedtls_pk_context pk; /**< Container for the public key context. */
  8204. mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
  8205. mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
  8206. mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
  8207. mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
  8208. int ext_types; /**< Bit string containing detected and parsed extensions */
  8209. int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
  8210. int max_pathlen; /**< Optional Basic Constraint extension value: The maximum path length to the root certificate. Path length is 1 higher than RFC 5280 'meaning', so 1+ */
  8211. unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
  8212. mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
  8213. unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
  8214. mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
  8215. mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
  8216. mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
  8217. void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
  8218. struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
  8219. }
  8220. mbedtls_x509_crt;
  8221. /**
  8222. * Build flag from an algorithm/curve identifier (pk, md, ecp)
  8223. * Since 0 is always XXX_NONE, ignore it.
  8224. */
  8225. #define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( id - 1 ) )
  8226. /**
  8227. * Security profile for certificate verification.
  8228. *
  8229. * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
  8230. */
  8231. typedef struct
  8232. {
  8233. uint32_t allowed_mds; /**< MDs for signatures */
  8234. uint32_t allowed_pks; /**< PK algs for signatures */
  8235. uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
  8236. uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
  8237. }
  8238. mbedtls_x509_crt_profile;
  8239. #define MBEDTLS_X509_CRT_VERSION_1 0
  8240. #define MBEDTLS_X509_CRT_VERSION_2 1
  8241. #define MBEDTLS_X509_CRT_VERSION_3 2
  8242. #define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
  8243. #define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
  8244. /**
  8245. * Container for writing a certificate (CRT)
  8246. */
  8247. typedef struct mbedtls_x509write_cert
  8248. {
  8249. int version;
  8250. mbedtls_mpi serial;
  8251. mbedtls_pk_context *subject_key;
  8252. mbedtls_pk_context *issuer_key;
  8253. mbedtls_asn1_named_data *subject;
  8254. mbedtls_asn1_named_data *issuer;
  8255. mbedtls_md_type_t md_alg;
  8256. char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
  8257. char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
  8258. mbedtls_asn1_named_data *extensions;
  8259. }
  8260. mbedtls_x509write_cert;
  8261. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  8262. /**
  8263. * Default security profile. Should provide a good balance between security
  8264. * and compatibility with current deployments.
  8265. */
  8266. extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
  8267. /**
  8268. * Expected next default profile. Recommended for new deployments.
  8269. * Currently targets a 128-bit security level, except for RSA-2048.
  8270. */
  8271. extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
  8272. /**
  8273. * NSA Suite B profile.
  8274. */
  8275. extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
  8276. /**
  8277. * \brief Parse a single DER formatted certificate and add it
  8278. * to the chained list.
  8279. *
  8280. * \param chain points to the start of the chain
  8281. * \param buf buffer holding the certificate DER data
  8282. * \param buflen size of the buffer
  8283. *
  8284. * \return 0 if successful, or a specific X509 or PEM error code
  8285. */
  8286. int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
  8287. size_t buflen );
  8288. /**
  8289. * \brief Parse one or more certificates and add them
  8290. * to the chained list. Parses permissively. If some
  8291. * certificates can be parsed, the result is the number
  8292. * of failed certificates it encountered. If none complete
  8293. * correctly, the first error is returned.
  8294. *
  8295. * \param chain points to the start of the chain
  8296. * \param buf buffer holding the certificate data in PEM or DER format
  8297. * \param buflen size of the buffer
  8298. * (including the terminating null byte for PEM data)
  8299. *
  8300. * \return 0 if all certificates parsed successfully, a positive number
  8301. * if partly successful or a specific X509 or PEM error code
  8302. */
  8303. int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
  8304. #if defined(MBEDTLS_FS_IO)
  8305. /**
  8306. * \brief Load one or more certificates and add them
  8307. * to the chained list. Parses permissively. If some
  8308. * certificates can be parsed, the result is the number
  8309. * of failed certificates it encountered. If none complete
  8310. * correctly, the first error is returned.
  8311. *
  8312. * \param chain points to the start of the chain
  8313. * \param path filename to read the certificates from
  8314. *
  8315. * \return 0 if all certificates parsed successfully, a positive number
  8316. * if partly successful or a specific X509 or PEM error code
  8317. */
  8318. int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
  8319. /**
  8320. * \brief Load one or more certificate files from a path and add them
  8321. * to the chained list. Parses permissively. If some
  8322. * certificates can be parsed, the result is the number
  8323. * of failed certificates it encountered. If none complete
  8324. * correctly, the first error is returned.
  8325. *
  8326. * \param chain points to the start of the chain
  8327. * \param path directory / folder to read the certificate files from
  8328. *
  8329. * \return 0 if all certificates parsed successfully, a positive number
  8330. * if partly successful or a specific X509 or PEM error code
  8331. */
  8332. int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
  8333. #endif /* MBEDTLS_FS_IO */
  8334. /**
  8335. * \brief Returns an informational string about the
  8336. * certificate.
  8337. *
  8338. * \param buf Buffer to write to
  8339. * \param size Maximum size of buffer
  8340. * \param prefix A line prefix
  8341. * \param crt The X509 certificate to represent
  8342. *
  8343. * \return The length of the string written (not including the
  8344. * terminated nul byte), or a negative error code.
  8345. */
  8346. int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
  8347. const mbedtls_x509_crt *crt );
  8348. /**
  8349. * \brief Returns an informational string about the
  8350. * verification status of a certificate.
  8351. *
  8352. * \param buf Buffer to write to
  8353. * \param size Maximum size of buffer
  8354. * \param prefix A line prefix
  8355. * \param flags Verification flags created by mbedtls_x509_crt_verify()
  8356. *
  8357. * \return The length of the string written (not including the
  8358. * terminated nul byte), or a negative error code.
  8359. */
  8360. int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
  8361. uint32_t flags );
  8362. /**
  8363. * \brief Verify the certificate signature
  8364. *
  8365. * The verify callback is a user-supplied callback that
  8366. * can clear / modify / add flags for a certificate. If set,
  8367. * the verification callback is called for each
  8368. * certificate in the chain (from the trust-ca down to the
  8369. * presented crt). The parameters for the callback are:
  8370. * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
  8371. * int *flags). With the flags representing current flags for
  8372. * that specific certificate and the certificate depth from
  8373. * the bottom (Peer cert depth = 0).
  8374. *
  8375. * All flags left after returning from the callback
  8376. * are also returned to the application. The function should
  8377. * return 0 for anything but a fatal error.
  8378. *
  8379. * \note In case verification failed, the results can be displayed
  8380. * using \c mbedtls_x509_crt_verify_info()
  8381. *
  8382. * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
  8383. * default security profile.
  8384. *
  8385. * \param crt a certificate to be verified
  8386. * \param trust_ca the trusted CA chain
  8387. * \param ca_crl the CRL chain for trusted CA's
  8388. * \param cn expected Common Name (can be set to
  8389. * NULL if the CN must not be verified)
  8390. * \param flags result of the verification
  8391. * \param f_vrfy verification function
  8392. * \param p_vrfy verification parameter
  8393. *
  8394. * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
  8395. * in which case *flags will have one or more
  8396. * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
  8397. * set,
  8398. * or another error in case of a fatal error encountered
  8399. * during the verification process.
  8400. */
  8401. int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
  8402. mbedtls_x509_crt *trust_ca,
  8403. mbedtls_x509_crl *ca_crl,
  8404. const char *cn, uint32_t *flags,
  8405. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  8406. void *p_vrfy );
  8407. /**
  8408. * \brief Verify the certificate signature according to profile
  8409. *
  8410. * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
  8411. * security profile.
  8412. *
  8413. * \note The restrictions on keys (RSA minimum size, allowed curves
  8414. * for ECDSA) apply to all certificates: trusted root,
  8415. * intermediate CAs if any, and end entity certificate.
  8416. *
  8417. * \param crt a certificate to be verified
  8418. * \param trust_ca the trusted CA chain
  8419. * \param ca_crl the CRL chain for trusted CA's
  8420. * \param profile security profile for verification
  8421. * \param cn expected Common Name (can be set to
  8422. * NULL if the CN must not be verified)
  8423. * \param flags result of the verification
  8424. * \param f_vrfy verification function
  8425. * \param p_vrfy verification parameter
  8426. *
  8427. * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
  8428. * in which case *flags will have one or more
  8429. * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
  8430. * set,
  8431. * or another error in case of a fatal error encountered
  8432. * during the verification process.
  8433. */
  8434. int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
  8435. mbedtls_x509_crt *trust_ca,
  8436. mbedtls_x509_crl *ca_crl,
  8437. const mbedtls_x509_crt_profile *profile,
  8438. const char *cn, uint32_t *flags,
  8439. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  8440. void *p_vrfy );
  8441. #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
  8442. /**
  8443. * \brief Check usage of certificate against keyUsage extension.
  8444. *
  8445. * \param crt Leaf certificate used.
  8446. * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
  8447. * before using the certificate to perform an RSA key
  8448. * exchange).
  8449. *
  8450. * \note Except for decipherOnly and encipherOnly, a bit set in the
  8451. * usage argument means this bit MUST be set in the
  8452. * certificate. For decipherOnly and encipherOnly, it means
  8453. * that bit MAY be set.
  8454. *
  8455. * \return 0 is these uses of the certificate are allowed,
  8456. * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
  8457. * is present but does not match the usage argument.
  8458. *
  8459. * \note You should only call this function on leaf certificates, on
  8460. * (intermediate) CAs the keyUsage extension is automatically
  8461. * checked by \c mbedtls_x509_crt_verify().
  8462. */
  8463. int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
  8464. unsigned int usage );
  8465. #endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
  8466. #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
  8467. /**
  8468. * \brief Check usage of certificate against extentedJeyUsage.
  8469. *
  8470. * \param crt Leaf certificate used.
  8471. * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or MBEDTLS_OID_CLIENT_AUTH).
  8472. * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
  8473. *
  8474. * \return 0 if this use of the certificate is allowed,
  8475. * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
  8476. *
  8477. * \note Usually only makes sense on leaf certificates.
  8478. */
  8479. int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
  8480. const char *usage_oid,
  8481. size_t usage_len );
  8482. #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) */
  8483. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  8484. /**
  8485. * \brief Verify the certificate revocation status
  8486. *
  8487. * \param crt a certificate to be verified
  8488. * \param crl the CRL to verify against
  8489. *
  8490. * \return 1 if the certificate is revoked, 0 otherwise
  8491. *
  8492. */
  8493. int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
  8494. #endif /* MBEDTLS_X509_CRL_PARSE_C */
  8495. /**
  8496. * \brief Initialize a certificate (chain)
  8497. *
  8498. * \param crt Certificate chain to initialize
  8499. */
  8500. void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
  8501. /**
  8502. * \brief Unallocate all certificate data
  8503. *
  8504. * \param crt Certificate chain to free
  8505. */
  8506. void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
  8507. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  8508. /* \} name */
  8509. /* \} addtogroup x509_module */
  8510. #if defined(MBEDTLS_X509_CRT_WRITE_C)
  8511. /**
  8512. * \brief Initialize a CRT writing context
  8513. *
  8514. * \param ctx CRT context to initialize
  8515. */
  8516. void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
  8517. /**
  8518. * \brief Set the verion for a Certificate
  8519. * Default: MBEDTLS_X509_CRT_VERSION_3
  8520. *
  8521. * \param ctx CRT context to use
  8522. * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
  8523. * MBEDTLS_X509_CRT_VERSION_3)
  8524. */
  8525. void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
  8526. /**
  8527. * \brief Set the serial number for a Certificate.
  8528. *
  8529. * \param ctx CRT context to use
  8530. * \param serial serial number to set
  8531. *
  8532. * \return 0 if successful
  8533. */
  8534. int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
  8535. /**
  8536. * \brief Set the validity period for a Certificate
  8537. * Timestamps should be in string format for UTC timezone
  8538. * i.e. "YYYYMMDDhhmmss"
  8539. * e.g. "20131231235959" for December 31st 2013
  8540. * at 23:59:59
  8541. *
  8542. * \param ctx CRT context to use
  8543. * \param not_before not_before timestamp
  8544. * \param not_after not_after timestamp
  8545. *
  8546. * \return 0 if timestamp was parsed successfully, or
  8547. * a specific error code
  8548. */
  8549. int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
  8550. const char *not_after );
  8551. /**
  8552. * \brief Set the issuer name for a Certificate
  8553. * Issuer names should contain a comma-separated list
  8554. * of OID types and values:
  8555. * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
  8556. *
  8557. * \param ctx CRT context to use
  8558. * \param issuer_name issuer name to set
  8559. *
  8560. * \return 0 if issuer name was parsed successfully, or
  8561. * a specific error code
  8562. */
  8563. int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
  8564. const char *issuer_name );
  8565. /**
  8566. * \brief Set the subject name for a Certificate
  8567. * Subject names should contain a comma-separated list
  8568. * of OID types and values:
  8569. * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
  8570. *
  8571. * \param ctx CRT context to use
  8572. * \param subject_name subject name to set
  8573. *
  8574. * \return 0 if subject name was parsed successfully, or
  8575. * a specific error code
  8576. */
  8577. int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
  8578. const char *subject_name );
  8579. /**
  8580. * \brief Set the subject public key for the certificate
  8581. *
  8582. * \param ctx CRT context to use
  8583. * \param key public key to include
  8584. */
  8585. void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
  8586. /**
  8587. * \brief Set the issuer key used for signing the certificate
  8588. *
  8589. * \param ctx CRT context to use
  8590. * \param key private key to sign with
  8591. */
  8592. void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
  8593. /**
  8594. * \brief Set the MD algorithm to use for the signature
  8595. * (e.g. MBEDTLS_MD_SHA1)
  8596. *
  8597. * \param ctx CRT context to use
  8598. * \param md_alg MD algorithm to use
  8599. */
  8600. void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
  8601. /**
  8602. * \brief Generic function to add to or replace an extension in the
  8603. * CRT
  8604. *
  8605. * \param ctx CRT context to use
  8606. * \param oid OID of the extension
  8607. * \param oid_len length of the OID
  8608. * \param critical if the extension is critical (per the RFC's definition)
  8609. * \param val value of the extension OCTET STRING
  8610. * \param val_len length of the value data
  8611. *
  8612. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  8613. */
  8614. int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
  8615. const char *oid, size_t oid_len,
  8616. int critical,
  8617. const unsigned char *val, size_t val_len );
  8618. /**
  8619. * \brief Set the basicConstraints extension for a CRT
  8620. *
  8621. * \param ctx CRT context to use
  8622. * \param is_ca is this a CA certificate
  8623. * \param max_pathlen maximum length of certificate chains below this
  8624. * certificate (only for CA certificates, -1 is
  8625. * inlimited)
  8626. *
  8627. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  8628. */
  8629. int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
  8630. int is_ca, int max_pathlen );
  8631. #if defined(MBEDTLS_SHA1_C)
  8632. /**
  8633. * \brief Set the subjectKeyIdentifier extension for a CRT
  8634. * Requires that mbedtls_x509write_crt_set_subject_key() has been
  8635. * called before
  8636. *
  8637. * \param ctx CRT context to use
  8638. *
  8639. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  8640. */
  8641. int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
  8642. /**
  8643. * \brief Set the authorityKeyIdentifier extension for a CRT
  8644. * Requires that mbedtls_x509write_crt_set_issuer_key() has been
  8645. * called before
  8646. *
  8647. * \param ctx CRT context to use
  8648. *
  8649. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  8650. */
  8651. int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
  8652. #endif /* MBEDTLS_SHA1_C */
  8653. /**
  8654. * \brief Set the Key Usage Extension flags
  8655. * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
  8656. *
  8657. * \param ctx CRT context to use
  8658. * \param key_usage key usage flags to set
  8659. *
  8660. * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  8661. */
  8662. int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
  8663. unsigned int key_usage );
  8664. /**
  8665. * \brief Set the Netscape Cert Type flags
  8666. * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
  8667. *
  8668. * \param ctx CRT context to use
  8669. * \param ns_cert_type Netscape Cert Type flags to set
  8670. *
  8671. * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  8672. */
  8673. int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
  8674. unsigned char ns_cert_type );
  8675. /**
  8676. * \brief Free the contents of a CRT write context
  8677. *
  8678. * \param ctx CRT context to free
  8679. */
  8680. void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
  8681. /**
  8682. * \brief Write a built up certificate to a X509 DER structure
  8683. * Note: data is written at the end of the buffer! Use the
  8684. * return value to determine where you should start
  8685. * using the buffer
  8686. *
  8687. * \param ctx certificate to write away
  8688. * \param buf buffer to write to
  8689. * \param size size of the buffer
  8690. * \param f_rng RNG function (for signature, see note)
  8691. * \param p_rng RNG parameter
  8692. *
  8693. * \return length of data written if successful, or a specific
  8694. * error code
  8695. *
  8696. * \note f_rng may be NULL if RSA is used for signature and the
  8697. * signature is made offline (otherwise f_rng is desirable
  8698. * for countermeasures against timing attacks).
  8699. * ECDSA signatures always require a non-NULL f_rng.
  8700. */
  8701. int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
  8702. int (*f_rng)(void *, unsigned char *, size_t),
  8703. void *p_rng );
  8704. #if defined(MBEDTLS_PEM_WRITE_C)
  8705. /**
  8706. * \brief Write a built up certificate to a X509 PEM string
  8707. *
  8708. * \param ctx certificate to write away
  8709. * \param buf buffer to write to
  8710. * \param size size of the buffer
  8711. * \param f_rng RNG function (for signature, see note)
  8712. * \param p_rng RNG parameter
  8713. *
  8714. * \return 0 if successful, or a specific error code
  8715. *
  8716. * \note f_rng may be NULL if RSA is used for signature and the
  8717. * signature is made offline (otherwise f_rng is desirable
  8718. * for countermeasures against timing attacks).
  8719. * ECDSA signatures always require a non-NULL f_rng.
  8720. */
  8721. int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
  8722. int (*f_rng)(void *, unsigned char *, size_t),
  8723. void *p_rng );
  8724. #endif /* MBEDTLS_PEM_WRITE_C */
  8725. #endif /* MBEDTLS_X509_CRT_WRITE_C */
  8726. #ifdef __cplusplus
  8727. }
  8728. #endif
  8729. #endif /* mbedtls_x509_crt.h */
  8730. /********* Start of file include/mbedtls/x509_csr.h ************/
  8731. /**
  8732. * \file x509_csr.h
  8733. *
  8734. * \brief X.509 certificate signing request parsing and writing
  8735. *
  8736. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  8737. * SPDX-License-Identifier: Apache-2.0
  8738. *
  8739. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8740. * not use this file except in compliance with the License.
  8741. * You may obtain a copy of the License at
  8742. *
  8743. * http://www.apache.org/licenses/LICENSE-2.0
  8744. *
  8745. * Unless required by applicable law or agreed to in writing, software
  8746. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  8747. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8748. * See the License for the specific language governing permissions and
  8749. * limitations under the License.
  8750. *
  8751. * This file is part of mbed TLS (https://tls.mbed.org)
  8752. */
  8753. #ifndef MBEDTLS_X509_CSR_H
  8754. #define MBEDTLS_X509_CSR_H
  8755. #if !defined(MBEDTLS_CONFIG_FILE)
  8756. #else
  8757. #endif
  8758. #ifdef __cplusplus
  8759. extern "C" {
  8760. #endif
  8761. /**
  8762. * \addtogroup x509_module
  8763. * \{ */
  8764. /**
  8765. * \name Structures and functions for X.509 Certificate Signing Requests (CSR)
  8766. * \{
  8767. */
  8768. /**
  8769. * Certificate Signing Request (CSR) structure.
  8770. */
  8771. typedef struct mbedtls_x509_csr
  8772. {
  8773. mbedtls_x509_buf raw; /**< The raw CSR data (DER). */
  8774. mbedtls_x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */
  8775. int version; /**< CSR version (1=v1). */
  8776. mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). */
  8777. mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
  8778. mbedtls_pk_context pk; /**< Container for the public key context. */
  8779. mbedtls_x509_buf sig_oid;
  8780. mbedtls_x509_buf sig;
  8781. mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
  8782. mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
  8783. void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
  8784. }
  8785. mbedtls_x509_csr;
  8786. /**
  8787. * Container for writing a CSR
  8788. */
  8789. typedef struct mbedtls_x509write_csr
  8790. {
  8791. mbedtls_pk_context *key;
  8792. mbedtls_asn1_named_data *subject;
  8793. mbedtls_md_type_t md_alg;
  8794. mbedtls_asn1_named_data *extensions;
  8795. }
  8796. mbedtls_x509write_csr;
  8797. #if defined(MBEDTLS_X509_CSR_PARSE_C)
  8798. /**
  8799. * \brief Load a Certificate Signing Request (CSR) in DER format
  8800. *
  8801. * \param csr CSR context to fill
  8802. * \param buf buffer holding the CRL data
  8803. * \param buflen size of the buffer
  8804. *
  8805. * \return 0 if successful, or a specific X509 error code
  8806. */
  8807. int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
  8808. const unsigned char *buf, size_t buflen );
  8809. /**
  8810. * \brief Load a Certificate Signing Request (CSR), DER or PEM format
  8811. *
  8812. * \param csr CSR context to fill
  8813. * \param buf buffer holding the CRL data
  8814. * \param buflen size of the buffer
  8815. * (including the terminating null byte for PEM data)
  8816. *
  8817. * \return 0 if successful, or a specific X509 or PEM error code
  8818. */
  8819. int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen );
  8820. #if defined(MBEDTLS_FS_IO)
  8821. /**
  8822. * \brief Load a Certificate Signing Request (CSR)
  8823. *
  8824. * \param csr CSR context to fill
  8825. * \param path filename to read the CSR from
  8826. *
  8827. * \return 0 if successful, or a specific X509 or PEM error code
  8828. */
  8829. int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path );
  8830. #endif /* MBEDTLS_FS_IO */
  8831. /**
  8832. * \brief Returns an informational string about the
  8833. * CSR.
  8834. *
  8835. * \param buf Buffer to write to
  8836. * \param size Maximum size of buffer
  8837. * \param prefix A line prefix
  8838. * \param csr The X509 CSR to represent
  8839. *
  8840. * \return The length of the string written (not including the
  8841. * terminated nul byte), or a negative error code.
  8842. */
  8843. int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix,
  8844. const mbedtls_x509_csr *csr );
  8845. /**
  8846. * \brief Initialize a CSR
  8847. *
  8848. * \param csr CSR to initialize
  8849. */
  8850. void mbedtls_x509_csr_init( mbedtls_x509_csr *csr );
  8851. /**
  8852. * \brief Unallocate all CSR data
  8853. *
  8854. * \param csr CSR to free
  8855. */
  8856. void mbedtls_x509_csr_free( mbedtls_x509_csr *csr );
  8857. #endif /* MBEDTLS_X509_CSR_PARSE_C */
  8858. /* \} name */
  8859. /* \} addtogroup x509_module */
  8860. #if defined(MBEDTLS_X509_CSR_WRITE_C)
  8861. /**
  8862. * \brief Initialize a CSR context
  8863. *
  8864. * \param ctx CSR context to initialize
  8865. */
  8866. void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx );
  8867. /**
  8868. * \brief Set the subject name for a CSR
  8869. * Subject names should contain a comma-separated list
  8870. * of OID types and values:
  8871. * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
  8872. *
  8873. * \param ctx CSR context to use
  8874. * \param subject_name subject name to set
  8875. *
  8876. * \return 0 if subject name was parsed successfully, or
  8877. * a specific error code
  8878. */
  8879. int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
  8880. const char *subject_name );
  8881. /**
  8882. * \brief Set the key for a CSR (public key will be included,
  8883. * private key used to sign the CSR when writing it)
  8884. *
  8885. * \param ctx CSR context to use
  8886. * \param key Asymetric key to include
  8887. */
  8888. void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key );
  8889. /**
  8890. * \brief Set the MD algorithm to use for the signature
  8891. * (e.g. MBEDTLS_MD_SHA1)
  8892. *
  8893. * \param ctx CSR context to use
  8894. * \param md_alg MD algorithm to use
  8895. */
  8896. void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg );
  8897. /**
  8898. * \brief Set the Key Usage Extension flags
  8899. * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
  8900. *
  8901. * \param ctx CSR context to use
  8902. * \param key_usage key usage flags to set
  8903. *
  8904. * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  8905. */
  8906. int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage );
  8907. /**
  8908. * \brief Set the Netscape Cert Type flags
  8909. * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
  8910. *
  8911. * \param ctx CSR context to use
  8912. * \param ns_cert_type Netscape Cert Type flags to set
  8913. *
  8914. * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  8915. */
  8916. int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
  8917. unsigned char ns_cert_type );
  8918. /**
  8919. * \brief Generic function to add to or replace an extension in the
  8920. * CSR
  8921. *
  8922. * \param ctx CSR context to use
  8923. * \param oid OID of the extension
  8924. * \param oid_len length of the OID
  8925. * \param val value of the extension OCTET STRING
  8926. * \param val_len length of the value data
  8927. *
  8928. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  8929. */
  8930. int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
  8931. const char *oid, size_t oid_len,
  8932. const unsigned char *val, size_t val_len );
  8933. /**
  8934. * \brief Free the contents of a CSR context
  8935. *
  8936. * \param ctx CSR context to free
  8937. */
  8938. void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx );
  8939. /**
  8940. * \brief Write a CSR (Certificate Signing Request) to a
  8941. * DER structure
  8942. * Note: data is written at the end of the buffer! Use the
  8943. * return value to determine where you should start
  8944. * using the buffer
  8945. *
  8946. * \param ctx CSR to write away
  8947. * \param buf buffer to write to
  8948. * \param size size of the buffer
  8949. * \param f_rng RNG function (for signature, see note)
  8950. * \param p_rng RNG parameter
  8951. *
  8952. * \return length of data written if successful, or a specific
  8953. * error code
  8954. *
  8955. * \note f_rng may be NULL if RSA is used for signature and the
  8956. * signature is made offline (otherwise f_rng is desirable
  8957. * for countermeasures against timing attacks).
  8958. * ECDSA signatures always require a non-NULL f_rng.
  8959. */
  8960. int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  8961. int (*f_rng)(void *, unsigned char *, size_t),
  8962. void *p_rng );
  8963. #if defined(MBEDTLS_PEM_WRITE_C)
  8964. /**
  8965. * \brief Write a CSR (Certificate Signing Request) to a
  8966. * PEM string
  8967. *
  8968. * \param ctx CSR to write away
  8969. * \param buf buffer to write to
  8970. * \param size size of the buffer
  8971. * \param f_rng RNG function (for signature, see note)
  8972. * \param p_rng RNG parameter
  8973. *
  8974. * \return 0 if successful, or a specific error code
  8975. *
  8976. * \note f_rng may be NULL if RSA is used for signature and the
  8977. * signature is made offline (otherwise f_rng is desirable
  8978. * for couermeasures against timing attacks).
  8979. * ECDSA signatures always require a non-NULL f_rng.
  8980. */
  8981. int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  8982. int (*f_rng)(void *, unsigned char *, size_t),
  8983. void *p_rng );
  8984. #endif /* MBEDTLS_PEM_WRITE_C */
  8985. #endif /* MBEDTLS_X509_CSR_WRITE_C */
  8986. #ifdef __cplusplus
  8987. }
  8988. #endif
  8989. #endif /* mbedtls_x509_csr.h */
  8990. /********* Start of file include/mbedtls/cipher.h ************/
  8991. /**
  8992. * \file cipher.h
  8993. *
  8994. * \brief Generic cipher wrapper.
  8995. *
  8996. * \author Adriaan de Jong <dejong@fox-it.com>
  8997. *
  8998. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  8999. * SPDX-License-Identifier: Apache-2.0
  9000. *
  9001. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9002. * not use this file except in compliance with the License.
  9003. * You may obtain a copy of the License at
  9004. *
  9005. * http://www.apache.org/licenses/LICENSE-2.0
  9006. *
  9007. * Unless required by applicable law or agreed to in writing, software
  9008. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9009. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9010. * See the License for the specific language governing permissions and
  9011. * limitations under the License.
  9012. *
  9013. * This file is part of mbed TLS (https://tls.mbed.org)
  9014. */
  9015. #ifndef MBEDTLS_CIPHER_H
  9016. #define MBEDTLS_CIPHER_H
  9017. #if !defined(MBEDTLS_CONFIG_FILE)
  9018. #else
  9019. #endif
  9020. #include <stddef.h>
  9021. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
  9022. #define MBEDTLS_CIPHER_MODE_AEAD
  9023. #endif
  9024. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  9025. #define MBEDTLS_CIPHER_MODE_WITH_PADDING
  9026. #endif
  9027. #if defined(MBEDTLS_ARC4_C)
  9028. #define MBEDTLS_CIPHER_MODE_STREAM
  9029. #endif
  9030. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  9031. !defined(inline) && !defined(__cplusplus)
  9032. #define inline __inline
  9033. #endif
  9034. #define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */
  9035. #define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters to function. */
  9036. #define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */
  9037. #define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */
  9038. #define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */
  9039. #define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300 /**< Authentication failed (for AEAD modes). */
  9040. #define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01 /**< Cipher accepts IVs of variable length */
  9041. #define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02 /**< Cipher accepts keys of variable length */
  9042. #ifdef __cplusplus
  9043. extern "C" {
  9044. #endif
  9045. typedef enum {
  9046. MBEDTLS_CIPHER_ID_NONE = 0,
  9047. MBEDTLS_CIPHER_ID_NULL,
  9048. MBEDTLS_CIPHER_ID_AES,
  9049. MBEDTLS_CIPHER_ID_DES,
  9050. MBEDTLS_CIPHER_ID_3DES,
  9051. MBEDTLS_CIPHER_ID_CAMELLIA,
  9052. MBEDTLS_CIPHER_ID_BLOWFISH,
  9053. MBEDTLS_CIPHER_ID_ARC4,
  9054. } mbedtls_cipher_id_t;
  9055. typedef enum {
  9056. MBEDTLS_CIPHER_NONE = 0,
  9057. MBEDTLS_CIPHER_NULL,
  9058. MBEDTLS_CIPHER_AES_128_ECB,
  9059. MBEDTLS_CIPHER_AES_192_ECB,
  9060. MBEDTLS_CIPHER_AES_256_ECB,
  9061. MBEDTLS_CIPHER_AES_128_CBC,
  9062. MBEDTLS_CIPHER_AES_192_CBC,
  9063. MBEDTLS_CIPHER_AES_256_CBC,
  9064. MBEDTLS_CIPHER_AES_128_CFB128,
  9065. MBEDTLS_CIPHER_AES_192_CFB128,
  9066. MBEDTLS_CIPHER_AES_256_CFB128,
  9067. MBEDTLS_CIPHER_AES_128_CTR,
  9068. MBEDTLS_CIPHER_AES_192_CTR,
  9069. MBEDTLS_CIPHER_AES_256_CTR,
  9070. MBEDTLS_CIPHER_AES_128_GCM,
  9071. MBEDTLS_CIPHER_AES_192_GCM,
  9072. MBEDTLS_CIPHER_AES_256_GCM,
  9073. MBEDTLS_CIPHER_CAMELLIA_128_ECB,
  9074. MBEDTLS_CIPHER_CAMELLIA_192_ECB,
  9075. MBEDTLS_CIPHER_CAMELLIA_256_ECB,
  9076. MBEDTLS_CIPHER_CAMELLIA_128_CBC,
  9077. MBEDTLS_CIPHER_CAMELLIA_192_CBC,
  9078. MBEDTLS_CIPHER_CAMELLIA_256_CBC,
  9079. MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
  9080. MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
  9081. MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
  9082. MBEDTLS_CIPHER_CAMELLIA_128_CTR,
  9083. MBEDTLS_CIPHER_CAMELLIA_192_CTR,
  9084. MBEDTLS_CIPHER_CAMELLIA_256_CTR,
  9085. MBEDTLS_CIPHER_CAMELLIA_128_GCM,
  9086. MBEDTLS_CIPHER_CAMELLIA_192_GCM,
  9087. MBEDTLS_CIPHER_CAMELLIA_256_GCM,
  9088. MBEDTLS_CIPHER_DES_ECB,
  9089. MBEDTLS_CIPHER_DES_CBC,
  9090. MBEDTLS_CIPHER_DES_EDE_ECB,
  9091. MBEDTLS_CIPHER_DES_EDE_CBC,
  9092. MBEDTLS_CIPHER_DES_EDE3_ECB,
  9093. MBEDTLS_CIPHER_DES_EDE3_CBC,
  9094. MBEDTLS_CIPHER_BLOWFISH_ECB,
  9095. MBEDTLS_CIPHER_BLOWFISH_CBC,
  9096. MBEDTLS_CIPHER_BLOWFISH_CFB64,
  9097. MBEDTLS_CIPHER_BLOWFISH_CTR,
  9098. MBEDTLS_CIPHER_ARC4_128,
  9099. MBEDTLS_CIPHER_AES_128_CCM,
  9100. MBEDTLS_CIPHER_AES_192_CCM,
  9101. MBEDTLS_CIPHER_AES_256_CCM,
  9102. MBEDTLS_CIPHER_CAMELLIA_128_CCM,
  9103. MBEDTLS_CIPHER_CAMELLIA_192_CCM,
  9104. MBEDTLS_CIPHER_CAMELLIA_256_CCM,
  9105. } mbedtls_cipher_type_t;
  9106. typedef enum {
  9107. MBEDTLS_MODE_NONE = 0,
  9108. MBEDTLS_MODE_ECB,
  9109. MBEDTLS_MODE_CBC,
  9110. MBEDTLS_MODE_CFB,
  9111. MBEDTLS_MODE_OFB, /* Unused! */
  9112. MBEDTLS_MODE_CTR,
  9113. MBEDTLS_MODE_GCM,
  9114. MBEDTLS_MODE_STREAM,
  9115. MBEDTLS_MODE_CCM,
  9116. } mbedtls_cipher_mode_t;
  9117. typedef enum {
  9118. MBEDTLS_PADDING_PKCS7 = 0, /**< PKCS7 padding (default) */
  9119. MBEDTLS_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding */
  9120. MBEDTLS_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding */
  9121. MBEDTLS_PADDING_ZEROS, /**< zero padding (not reversible!) */
  9122. MBEDTLS_PADDING_NONE, /**< never pad (full blocks only) */
  9123. } mbedtls_cipher_padding_t;
  9124. typedef enum {
  9125. MBEDTLS_OPERATION_NONE = -1,
  9126. MBEDTLS_DECRYPT = 0,
  9127. MBEDTLS_ENCRYPT,
  9128. } mbedtls_operation_t;
  9129. enum {
  9130. /** Undefined key length */
  9131. MBEDTLS_KEY_LENGTH_NONE = 0,
  9132. /** Key length, in bits (including parity), for DES keys */
  9133. MBEDTLS_KEY_LENGTH_DES = 64,
  9134. /** Key length, in bits (including parity), for DES in two key EDE */
  9135. MBEDTLS_KEY_LENGTH_DES_EDE = 128,
  9136. /** Key length, in bits (including parity), for DES in three-key EDE */
  9137. MBEDTLS_KEY_LENGTH_DES_EDE3 = 192,
  9138. };
  9139. /** Maximum length of any IV, in bytes */
  9140. #define MBEDTLS_MAX_IV_LENGTH 16
  9141. /** Maximum block size of any cipher, in bytes */
  9142. #define MBEDTLS_MAX_BLOCK_LENGTH 16
  9143. /**
  9144. * Base cipher information (opaque struct).
  9145. */
  9146. typedef struct mbedtls_cipher_base_t mbedtls_cipher_base_t;
  9147. /**
  9148. * Cipher information. Allows cipher functions to be called in a generic way.
  9149. */
  9150. typedef struct {
  9151. /** Full cipher identifier (e.g. MBEDTLS_CIPHER_AES_256_CBC) */
  9152. mbedtls_cipher_type_t type;
  9153. /** Cipher mode (e.g. MBEDTLS_MODE_CBC) */
  9154. mbedtls_cipher_mode_t mode;
  9155. /** Cipher key length, in bits (default length for variable sized ciphers)
  9156. * (Includes parity bits for ciphers like DES) */
  9157. unsigned int key_bitlen;
  9158. /** Name of the cipher */
  9159. const char * name;
  9160. /** IV/NONCE size, in bytes.
  9161. * For cipher that accept many sizes: recommended size */
  9162. unsigned int iv_size;
  9163. /** Flags for variable IV size, variable key size, etc. */
  9164. int flags;
  9165. /** block size, in bytes */
  9166. unsigned int block_size;
  9167. /** Base cipher information and functions */
  9168. const mbedtls_cipher_base_t *base;
  9169. } mbedtls_cipher_info_t;
  9170. /**
  9171. * Generic cipher context.
  9172. */
  9173. typedef struct {
  9174. /** Information about the associated cipher */
  9175. const mbedtls_cipher_info_t *cipher_info;
  9176. /** Key length to use */
  9177. int key_bitlen;
  9178. /** Operation that the context's key has been initialised for */
  9179. mbedtls_operation_t operation;
  9180. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  9181. /** Padding functions to use, if relevant for cipher mode */
  9182. void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
  9183. int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
  9184. #endif
  9185. /** Buffer for data that hasn't been encrypted yet */
  9186. unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH];
  9187. /** Number of bytes that still need processing */
  9188. size_t unprocessed_len;
  9189. /** Current IV or NONCE_COUNTER for CTR-mode */
  9190. unsigned char iv[MBEDTLS_MAX_IV_LENGTH];
  9191. /** IV size in bytes (for ciphers with variable-length IVs) */
  9192. size_t iv_size;
  9193. /** Cipher-specific context */
  9194. void *cipher_ctx;
  9195. } mbedtls_cipher_context_t;
  9196. /**
  9197. * \brief Returns the list of ciphers supported by the generic cipher module.
  9198. *
  9199. * \return a statically allocated array of ciphers, the last entry
  9200. * is 0.
  9201. */
  9202. const int *mbedtls_cipher_list( void );
  9203. /**
  9204. * \brief Returns the cipher information structure associated
  9205. * with the given cipher name.
  9206. *
  9207. * \param cipher_name Name of the cipher to search for.
  9208. *
  9209. * \return the cipher information structure associated with the
  9210. * given cipher_name, or NULL if not found.
  9211. */
  9212. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name );
  9213. /**
  9214. * \brief Returns the cipher information structure associated
  9215. * with the given cipher type.
  9216. *
  9217. * \param cipher_type Type of the cipher to search for.
  9218. *
  9219. * \return the cipher information structure associated with the
  9220. * given cipher_type, or NULL if not found.
  9221. */
  9222. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type );
  9223. /**
  9224. * \brief Returns the cipher information structure associated
  9225. * with the given cipher id, key size and mode.
  9226. *
  9227. * \param cipher_id Id of the cipher to search for
  9228. * (e.g. MBEDTLS_CIPHER_ID_AES)
  9229. * \param key_bitlen Length of the key in bits
  9230. * \param mode Cipher mode (e.g. MBEDTLS_MODE_CBC)
  9231. *
  9232. * \return the cipher information structure associated with the
  9233. * given cipher_type, or NULL if not found.
  9234. */
  9235. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
  9236. int key_bitlen,
  9237. const mbedtls_cipher_mode_t mode );
  9238. /**
  9239. * \brief Initialize a cipher_context (as NONE)
  9240. */
  9241. void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx );
  9242. /**
  9243. * \brief Free and clear the cipher-specific context of ctx.
  9244. * Freeing ctx itself remains the responsibility of the
  9245. * caller.
  9246. */
  9247. void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx );
  9248. /**
  9249. * \brief Initialises and fills the cipher context structure with
  9250. * the appropriate values.
  9251. *
  9252. * \note Currently also clears structure. In future versions you
  9253. * will be required to call mbedtls_cipher_init() on the structure
  9254. * first.
  9255. *
  9256. * \param ctx context to initialise. May not be NULL.
  9257. * \param cipher_info cipher to use.
  9258. *
  9259. * \return 0 on success,
  9260. * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on parameter failure,
  9261. * MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
  9262. * cipher-specific context failed.
  9263. */
  9264. int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info );
  9265. /**
  9266. * \brief Returns the block size of the given cipher.
  9267. *
  9268. * \param ctx cipher's context. Must have been initialised.
  9269. *
  9270. * \return size of the cipher's blocks, or 0 if ctx has not been
  9271. * initialised.
  9272. */
  9273. static inline unsigned int mbedtls_cipher_get_block_size( const mbedtls_cipher_context_t *ctx )
  9274. {
  9275. if( NULL == ctx || NULL == ctx->cipher_info )
  9276. return 0;
  9277. return ctx->cipher_info->block_size;
  9278. }
  9279. /**
  9280. * \brief Returns the mode of operation for the cipher.
  9281. * (e.g. MBEDTLS_MODE_CBC)
  9282. *
  9283. * \param ctx cipher's context. Must have been initialised.
  9284. *
  9285. * \return mode of operation, or MBEDTLS_MODE_NONE if ctx
  9286. * has not been initialised.
  9287. */
  9288. static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode( const mbedtls_cipher_context_t *ctx )
  9289. {
  9290. if( NULL == ctx || NULL == ctx->cipher_info )
  9291. return MBEDTLS_MODE_NONE;
  9292. return ctx->cipher_info->mode;
  9293. }
  9294. /**
  9295. * \brief Returns the size of the cipher's IV/NONCE in bytes.
  9296. *
  9297. * \param ctx cipher's context. Must have been initialised.
  9298. *
  9299. * \return If IV has not been set yet: (recommended) IV size
  9300. * (0 for ciphers not using IV/NONCE).
  9301. * If IV has already been set: actual size.
  9302. */
  9303. static inline int mbedtls_cipher_get_iv_size( const mbedtls_cipher_context_t *ctx )
  9304. {
  9305. if( NULL == ctx || NULL == ctx->cipher_info )
  9306. return 0;
  9307. if( ctx->iv_size != 0 )
  9308. return (int) ctx->iv_size;
  9309. return (int) ctx->cipher_info->iv_size;
  9310. }
  9311. /**
  9312. * \brief Returns the type of the given cipher.
  9313. *
  9314. * \param ctx cipher's context. Must have been initialised.
  9315. *
  9316. * \return type of the cipher, or MBEDTLS_CIPHER_NONE if ctx has
  9317. * not been initialised.
  9318. */
  9319. static inline mbedtls_cipher_type_t mbedtls_cipher_get_type( const mbedtls_cipher_context_t *ctx )
  9320. {
  9321. if( NULL == ctx || NULL == ctx->cipher_info )
  9322. return MBEDTLS_CIPHER_NONE;
  9323. return ctx->cipher_info->type;
  9324. }
  9325. /**
  9326. * \brief Returns the name of the given cipher, as a string.
  9327. *
  9328. * \param ctx cipher's context. Must have been initialised.
  9329. *
  9330. * \return name of the cipher, or NULL if ctx was not initialised.
  9331. */
  9332. static inline const char *mbedtls_cipher_get_name( const mbedtls_cipher_context_t *ctx )
  9333. {
  9334. if( NULL == ctx || NULL == ctx->cipher_info )
  9335. return 0;
  9336. return ctx->cipher_info->name;
  9337. }
  9338. /**
  9339. * \brief Returns the key length of the cipher.
  9340. *
  9341. * \param ctx cipher's context. Must have been initialised.
  9342. *
  9343. * \return cipher's key length, in bits, or
  9344. * MBEDTLS_KEY_LENGTH_NONE if ctx has not been
  9345. * initialised.
  9346. */
  9347. static inline int mbedtls_cipher_get_key_bitlen( const mbedtls_cipher_context_t *ctx )
  9348. {
  9349. if( NULL == ctx || NULL == ctx->cipher_info )
  9350. return MBEDTLS_KEY_LENGTH_NONE;
  9351. return (int) ctx->cipher_info->key_bitlen;
  9352. }
  9353. /**
  9354. * \brief Returns the operation of the given cipher.
  9355. *
  9356. * \param ctx cipher's context. Must have been initialised.
  9357. *
  9358. * \return operation (MBEDTLS_ENCRYPT or MBEDTLS_DECRYPT),
  9359. * or MBEDTLS_OPERATION_NONE if ctx has not been
  9360. * initialised.
  9361. */
  9362. static inline mbedtls_operation_t mbedtls_cipher_get_operation( const mbedtls_cipher_context_t *ctx )
  9363. {
  9364. if( NULL == ctx || NULL == ctx->cipher_info )
  9365. return MBEDTLS_OPERATION_NONE;
  9366. return ctx->operation;
  9367. }
  9368. /**
  9369. * \brief Set the key to use with the given context.
  9370. *
  9371. * \param ctx generic cipher context. May not be NULL. Must have been
  9372. * initialised using cipher_context_from_type or
  9373. * cipher_context_from_string.
  9374. * \param key The key to use.
  9375. * \param key_bitlen key length to use, in bits.
  9376. * \param operation Operation that the key will be used for, either
  9377. * MBEDTLS_ENCRYPT or MBEDTLS_DECRYPT.
  9378. *
  9379. * \returns 0 on success, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if
  9380. * parameter verification fails or a cipher specific
  9381. * error code.
  9382. */
  9383. int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
  9384. int key_bitlen, const mbedtls_operation_t operation );
  9385. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  9386. /**
  9387. * \brief Set padding mode, for cipher modes that use padding.
  9388. * (Default: PKCS7 padding.)
  9389. *
  9390. * \param ctx generic cipher context
  9391. * \param mode padding mode
  9392. *
  9393. * \returns 0 on success, MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
  9394. * if selected padding mode is not supported, or
  9395. * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode
  9396. * does not support padding.
  9397. */
  9398. int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode );
  9399. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  9400. /**
  9401. * \brief Set the initialization vector (IV) or nonce
  9402. *
  9403. * \param ctx generic cipher context
  9404. * \param iv IV to use (or NONCE_COUNTER for CTR-mode ciphers)
  9405. * \param iv_len IV length for ciphers with variable-size IV;
  9406. * discarded by ciphers with fixed-size IV.
  9407. *
  9408. * \returns 0 on success, or MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
  9409. *
  9410. * \note Some ciphers don't use IVs nor NONCE. For these
  9411. * ciphers, this function has no effect.
  9412. */
  9413. int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
  9414. const unsigned char *iv, size_t iv_len );
  9415. /**
  9416. * \brief Finish preparation of the given context
  9417. *
  9418. * \param ctx generic cipher context
  9419. *
  9420. * \returns 0 on success, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
  9421. * if parameter verification fails.
  9422. */
  9423. int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx );
  9424. #if defined(MBEDTLS_GCM_C)
  9425. /**
  9426. * \brief Add additional data (for AEAD ciphers).
  9427. * Currently only supported with GCM.
  9428. * Must be called exactly once, after mbedtls_cipher_reset().
  9429. *
  9430. * \param ctx generic cipher context
  9431. * \param ad Additional data to use.
  9432. * \param ad_len Length of ad.
  9433. *
  9434. * \return 0 on success, or a specific error code.
  9435. */
  9436. int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
  9437. const unsigned char *ad, size_t ad_len );
  9438. #endif /* MBEDTLS_GCM_C */
  9439. /**
  9440. * \brief Generic cipher update function. Encrypts/decrypts
  9441. * using the given cipher context. Writes as many block
  9442. * size'd blocks of data as possible to output. Any data
  9443. * that cannot be written immediately will either be added
  9444. * to the next block, or flushed when cipher_final is
  9445. * called.
  9446. * Exception: for MBEDTLS_MODE_ECB, expects single block
  9447. * in size (e.g. 16 bytes for AES)
  9448. *
  9449. * \param ctx generic cipher context
  9450. * \param input buffer holding the input data
  9451. * \param ilen length of the input data
  9452. * \param output buffer for the output data. Should be able to hold at
  9453. * least ilen + block_size. Cannot be the same buffer as
  9454. * input!
  9455. * \param olen length of the output data, will be filled with the
  9456. * actual number of bytes written.
  9457. *
  9458. * \returns 0 on success, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if
  9459. * parameter verification fails,
  9460. * MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE on an
  9461. * unsupported mode for a cipher or a cipher specific
  9462. * error code.
  9463. *
  9464. * \note If the underlying cipher is GCM, all calls to this
  9465. * function, except the last one before mbedtls_cipher_finish(),
  9466. * must have ilen a multiple of the block size.
  9467. */
  9468. int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
  9469. size_t ilen, unsigned char *output, size_t *olen );
  9470. /**
  9471. * \brief Generic cipher finalisation function. If data still
  9472. * needs to be flushed from an incomplete block, data
  9473. * contained within it will be padded with the size of
  9474. * the last block, and written to the output buffer.
  9475. *
  9476. * \param ctx Generic cipher context
  9477. * \param output buffer to write data to. Needs block_size available.
  9478. * \param olen length of the data written to the output buffer.
  9479. *
  9480. * \returns 0 on success, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if
  9481. * parameter verification fails,
  9482. * MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption
  9483. * expected a full block but was not provided one,
  9484. * MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
  9485. * while decrypting or a cipher specific error code.
  9486. */
  9487. int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
  9488. unsigned char *output, size_t *olen );
  9489. #if defined(MBEDTLS_GCM_C)
  9490. /**
  9491. * \brief Write tag for AEAD ciphers.
  9492. * Currently only supported with GCM.
  9493. * Must be called after mbedtls_cipher_finish().
  9494. *
  9495. * \param ctx Generic cipher context
  9496. * \param tag buffer to write the tag
  9497. * \param tag_len Length of the tag to write
  9498. *
  9499. * \return 0 on success, or a specific error code.
  9500. */
  9501. int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
  9502. unsigned char *tag, size_t tag_len );
  9503. /**
  9504. * \brief Check tag for AEAD ciphers.
  9505. * Currently only supported with GCM.
  9506. * Must be called after mbedtls_cipher_finish().
  9507. *
  9508. * \param ctx Generic cipher context
  9509. * \param tag Buffer holding the tag
  9510. * \param tag_len Length of the tag to check
  9511. *
  9512. * \return 0 on success, or a specific error code.
  9513. */
  9514. int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
  9515. const unsigned char *tag, size_t tag_len );
  9516. #endif /* MBEDTLS_GCM_C */
  9517. /**
  9518. * \brief Generic all-in-one encryption/decryption
  9519. * (for all ciphers except AEAD constructs).
  9520. *
  9521. * \param ctx generic cipher context
  9522. * \param iv IV to use (or NONCE_COUNTER for CTR-mode ciphers)
  9523. * \param iv_len IV length for ciphers with variable-size IV;
  9524. * discarded by ciphers with fixed-size IV.
  9525. * \param input buffer holding the input data
  9526. * \param ilen length of the input data
  9527. * \param output buffer for the output data. Should be able to hold at
  9528. * least ilen + block_size. Cannot be the same buffer as
  9529. * input!
  9530. * \param olen length of the output data, will be filled with the
  9531. * actual number of bytes written.
  9532. *
  9533. * \note Some ciphers don't use IVs nor NONCE. For these
  9534. * ciphers, use iv = NULL and iv_len = 0.
  9535. *
  9536. * \returns 0 on success, or
  9537. * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
  9538. * MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption
  9539. * expected a full block but was not provided one, or
  9540. * MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
  9541. * while decrypting, or
  9542. * a cipher specific error code.
  9543. */
  9544. int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
  9545. const unsigned char *iv, size_t iv_len,
  9546. const unsigned char *input, size_t ilen,
  9547. unsigned char *output, size_t *olen );
  9548. #if defined(MBEDTLS_CIPHER_MODE_AEAD)
  9549. /**
  9550. * \brief Generic autenticated encryption (AEAD ciphers).
  9551. *
  9552. * \param ctx generic cipher context
  9553. * \param iv IV to use (or NONCE_COUNTER for CTR-mode ciphers)
  9554. * \param iv_len IV length for ciphers with variable-size IV;
  9555. * discarded by ciphers with fixed-size IV.
  9556. * \param ad Additional data to authenticate.
  9557. * \param ad_len Length of ad.
  9558. * \param input buffer holding the input data
  9559. * \param ilen length of the input data
  9560. * \param output buffer for the output data.
  9561. * Should be able to hold at least ilen.
  9562. * \param olen length of the output data, will be filled with the
  9563. * actual number of bytes written.
  9564. * \param tag buffer for the authentication tag
  9565. * \param tag_len desired tag length
  9566. *
  9567. * \returns 0 on success, or
  9568. * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
  9569. * a cipher specific error code.
  9570. */
  9571. int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
  9572. const unsigned char *iv, size_t iv_len,
  9573. const unsigned char *ad, size_t ad_len,
  9574. const unsigned char *input, size_t ilen,
  9575. unsigned char *output, size_t *olen,
  9576. unsigned char *tag, size_t tag_len );
  9577. /**
  9578. * \brief Generic autenticated decryption (AEAD ciphers).
  9579. *
  9580. * \param ctx generic cipher context
  9581. * \param iv IV to use (or NONCE_COUNTER for CTR-mode ciphers)
  9582. * \param iv_len IV length for ciphers with variable-size IV;
  9583. * discarded by ciphers with fixed-size IV.
  9584. * \param ad Additional data to be authenticated.
  9585. * \param ad_len Length of ad.
  9586. * \param input buffer holding the input data
  9587. * \param ilen length of the input data
  9588. * \param output buffer for the output data.
  9589. * Should be able to hold at least ilen.
  9590. * \param olen length of the output data, will be filled with the
  9591. * actual number of bytes written.
  9592. * \param tag buffer holding the authentication tag
  9593. * \param tag_len length of the authentication tag
  9594. *
  9595. * \returns 0 on success, or
  9596. * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
  9597. * MBEDTLS_ERR_CIPHER_AUTH_FAILED if data isn't authentic,
  9598. * or a cipher specific error code.
  9599. *
  9600. * \note If the data is not authentic, then the output buffer
  9601. * is zeroed out to prevent the unauthentic plaintext to
  9602. * be used by mistake, making this interface safer.
  9603. */
  9604. int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
  9605. const unsigned char *iv, size_t iv_len,
  9606. const unsigned char *ad, size_t ad_len,
  9607. const unsigned char *input, size_t ilen,
  9608. unsigned char *output, size_t *olen,
  9609. const unsigned char *tag, size_t tag_len );
  9610. #endif /* MBEDTLS_CIPHER_MODE_AEAD */
  9611. #ifdef __cplusplus
  9612. }
  9613. #endif
  9614. #endif /* MBEDTLS_CIPHER_H */
  9615. /********* Start of file include/mbedtls/cipher_internal.h ************/
  9616. /**
  9617. * \file cipher_internal.h
  9618. *
  9619. * \brief Cipher wrappers.
  9620. *
  9621. * \author Adriaan de Jong <dejong@fox-it.com>
  9622. *
  9623. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  9624. * SPDX-License-Identifier: Apache-2.0
  9625. *
  9626. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9627. * not use this file except in compliance with the License.
  9628. * You may obtain a copy of the License at
  9629. *
  9630. * http://www.apache.org/licenses/LICENSE-2.0
  9631. *
  9632. * Unless required by applicable law or agreed to in writing, software
  9633. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9634. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9635. * See the License for the specific language governing permissions and
  9636. * limitations under the License.
  9637. *
  9638. * This file is part of mbed TLS (https://tls.mbed.org)
  9639. */
  9640. #ifndef MBEDTLS_CIPHER_WRAP_H
  9641. #define MBEDTLS_CIPHER_WRAP_H
  9642. #if !defined(MBEDTLS_CONFIG_FILE)
  9643. #else
  9644. #endif
  9645. #ifdef __cplusplus
  9646. extern "C" {
  9647. #endif
  9648. /**
  9649. * Base cipher information. The non-mode specific functions and values.
  9650. */
  9651. struct mbedtls_cipher_base_t
  9652. {
  9653. /** Base Cipher type (e.g. MBEDTLS_CIPHER_ID_AES) */
  9654. mbedtls_cipher_id_t cipher;
  9655. /** Encrypt using ECB */
  9656. int (*ecb_func)( void *ctx, mbedtls_operation_t mode,
  9657. const unsigned char *input, unsigned char *output );
  9658. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  9659. /** Encrypt using CBC */
  9660. int (*cbc_func)( void *ctx, mbedtls_operation_t mode, size_t length,
  9661. unsigned char *iv, const unsigned char *input,
  9662. unsigned char *output );
  9663. #endif
  9664. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  9665. /** Encrypt using CFB (Full length) */
  9666. int (*cfb_func)( void *ctx, mbedtls_operation_t mode, size_t length, size_t *iv_off,
  9667. unsigned char *iv, const unsigned char *input,
  9668. unsigned char *output );
  9669. #endif
  9670. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  9671. /** Encrypt using CTR */
  9672. int (*ctr_func)( void *ctx, size_t length, size_t *nc_off,
  9673. unsigned char *nonce_counter, unsigned char *stream_block,
  9674. const unsigned char *input, unsigned char *output );
  9675. #endif
  9676. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  9677. /** Encrypt using STREAM */
  9678. int (*stream_func)( void *ctx, size_t length,
  9679. const unsigned char *input, unsigned char *output );
  9680. #endif
  9681. /** Set key for encryption purposes */
  9682. int (*setkey_enc_func)( void *ctx, const unsigned char *key,
  9683. unsigned int key_bitlen );
  9684. /** Set key for decryption purposes */
  9685. int (*setkey_dec_func)( void *ctx, const unsigned char *key,
  9686. unsigned int key_bitlen);
  9687. /** Allocate a new context */
  9688. void * (*ctx_alloc_func)( void );
  9689. /** Free the given context */
  9690. void (*ctx_free_func)( void *ctx );
  9691. };
  9692. typedef struct
  9693. {
  9694. mbedtls_cipher_type_t type;
  9695. const mbedtls_cipher_info_t *info;
  9696. } mbedtls_cipher_definition_t;
  9697. extern const mbedtls_cipher_definition_t mbedtls_cipher_definitions[];
  9698. extern int mbedtls_cipher_supported[];
  9699. #ifdef __cplusplus
  9700. }
  9701. #endif
  9702. #endif /* MBEDTLS_CIPHER_WRAP_H */
  9703. /********* Start of file include/mbedtls/ssl_ciphersuites.h ************/
  9704. /**
  9705. * \file ssl_ciphersuites.h
  9706. *
  9707. * \brief SSL Ciphersuites for mbed TLS
  9708. *
  9709. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  9710. * SPDX-License-Identifier: Apache-2.0
  9711. *
  9712. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9713. * not use this file except in compliance with the License.
  9714. * You may obtain a copy of the License at
  9715. *
  9716. * http://www.apache.org/licenses/LICENSE-2.0
  9717. *
  9718. * Unless required by applicable law or agreed to in writing, software
  9719. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9720. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9721. * See the License for the specific language governing permissions and
  9722. * limitations under the License.
  9723. *
  9724. * This file is part of mbed TLS (https://tls.mbed.org)
  9725. */
  9726. #ifndef MBEDTLS_SSL_CIPHERSUITES_H
  9727. #define MBEDTLS_SSL_CIPHERSUITES_H
  9728. #ifdef __cplusplus
  9729. extern "C" {
  9730. #endif
  9731. /*
  9732. * Supported ciphersuites (Official IANA names)
  9733. */
  9734. #define MBEDTLS_TLS_RSA_WITH_NULL_MD5 0x01 /**< Weak! */
  9735. #define MBEDTLS_TLS_RSA_WITH_NULL_SHA 0x02 /**< Weak! */
  9736. #define MBEDTLS_TLS_RSA_WITH_RC4_128_MD5 0x04
  9737. #define MBEDTLS_TLS_RSA_WITH_RC4_128_SHA 0x05
  9738. #define MBEDTLS_TLS_RSA_WITH_DES_CBC_SHA 0x09 /**< Weak! Not in TLS 1.2 */
  9739. #define MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA 0x0A
  9740. #define MBEDTLS_TLS_DHE_RSA_WITH_DES_CBC_SHA 0x15 /**< Weak! Not in TLS 1.2 */
  9741. #define MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA 0x16
  9742. #define MBEDTLS_TLS_PSK_WITH_NULL_SHA 0x2C /**< Weak! */
  9743. #define MBEDTLS_TLS_DHE_PSK_WITH_NULL_SHA 0x2D /**< Weak! */
  9744. #define MBEDTLS_TLS_RSA_PSK_WITH_NULL_SHA 0x2E /**< Weak! */
  9745. #define MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA 0x2F
  9746. #define MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA 0x33
  9747. #define MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA 0x35
  9748. #define MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA 0x39
  9749. #define MBEDTLS_TLS_RSA_WITH_NULL_SHA256 0x3B /**< Weak! */
  9750. #define MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256 0x3C /**< TLS 1.2 */
  9751. #define MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA256 0x3D /**< TLS 1.2 */
  9752. #define MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA 0x41
  9753. #define MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA 0x45
  9754. #define MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 0x67 /**< TLS 1.2 */
  9755. #define MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 0x6B /**< TLS 1.2 */
  9756. #define MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA 0x84
  9757. #define MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA 0x88
  9758. #define MBEDTLS_TLS_PSK_WITH_RC4_128_SHA 0x8A
  9759. #define MBEDTLS_TLS_PSK_WITH_3DES_EDE_CBC_SHA 0x8B
  9760. #define MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA 0x8C
  9761. #define MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA 0x8D
  9762. #define MBEDTLS_TLS_DHE_PSK_WITH_RC4_128_SHA 0x8E
  9763. #define MBEDTLS_TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA 0x8F
  9764. #define MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA 0x90
  9765. #define MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA 0x91
  9766. #define MBEDTLS_TLS_RSA_PSK_WITH_RC4_128_SHA 0x92
  9767. #define MBEDTLS_TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA 0x93
  9768. #define MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA 0x94
  9769. #define MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA 0x95
  9770. #define MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256 0x9C /**< TLS 1.2 */
  9771. #define MBEDTLS_TLS_RSA_WITH_AES_256_GCM_SHA384 0x9D /**< TLS 1.2 */
  9772. #define MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 0x9E /**< TLS 1.2 */
  9773. #define MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 0x9F /**< TLS 1.2 */
  9774. #define MBEDTLS_TLS_PSK_WITH_AES_128_GCM_SHA256 0xA8 /**< TLS 1.2 */
  9775. #define MBEDTLS_TLS_PSK_WITH_AES_256_GCM_SHA384 0xA9 /**< TLS 1.2 */
  9776. #define MBEDTLS_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 0xAA /**< TLS 1.2 */
  9777. #define MBEDTLS_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 0xAB /**< TLS 1.2 */
  9778. #define MBEDTLS_TLS_RSA_PSK_WITH_AES_128_GCM_SHA256 0xAC /**< TLS 1.2 */
  9779. #define MBEDTLS_TLS_RSA_PSK_WITH_AES_256_GCM_SHA384 0xAD /**< TLS 1.2 */
  9780. #define MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA256 0xAE
  9781. #define MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA384 0xAF
  9782. #define MBEDTLS_TLS_PSK_WITH_NULL_SHA256 0xB0 /**< Weak! */
  9783. #define MBEDTLS_TLS_PSK_WITH_NULL_SHA384 0xB1 /**< Weak! */
  9784. #define MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 0xB2
  9785. #define MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 0xB3
  9786. #define MBEDTLS_TLS_DHE_PSK_WITH_NULL_SHA256 0xB4 /**< Weak! */
  9787. #define MBEDTLS_TLS_DHE_PSK_WITH_NULL_SHA384 0xB5 /**< Weak! */
  9788. #define MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA256 0xB6
  9789. #define MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA384 0xB7
  9790. #define MBEDTLS_TLS_RSA_PSK_WITH_NULL_SHA256 0xB8 /**< Weak! */
  9791. #define MBEDTLS_TLS_RSA_PSK_WITH_NULL_SHA384 0xB9 /**< Weak! */
  9792. #define MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 0xBA /**< TLS 1.2 */
  9793. #define MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 0xBE /**< TLS 1.2 */
  9794. #define MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 0xC0 /**< TLS 1.2 */
  9795. #define MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 0xC4 /**< TLS 1.2 */
  9796. #define MBEDTLS_TLS_ECDH_ECDSA_WITH_NULL_SHA 0xC001 /**< Weak! */
  9797. #define MBEDTLS_TLS_ECDH_ECDSA_WITH_RC4_128_SHA 0xC002 /**< Not in SSL3! */
  9798. #define MBEDTLS_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA 0xC003 /**< Not in SSL3! */
  9799. #define MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA 0xC004 /**< Not in SSL3! */
  9800. #define MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA 0xC005 /**< Not in SSL3! */
  9801. #define MBEDTLS_TLS_ECDHE_ECDSA_WITH_NULL_SHA 0xC006 /**< Weak! */
  9802. #define MBEDTLS_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA 0xC007 /**< Not in SSL3! */
  9803. #define MBEDTLS_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA 0xC008 /**< Not in SSL3! */
  9804. #define MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA 0xC009 /**< Not in SSL3! */
  9805. #define MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA 0xC00A /**< Not in SSL3! */
  9806. #define MBEDTLS_TLS_ECDH_RSA_WITH_NULL_SHA 0xC00B /**< Weak! */
  9807. #define MBEDTLS_TLS_ECDH_RSA_WITH_RC4_128_SHA 0xC00C /**< Not in SSL3! */
  9808. #define MBEDTLS_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA 0xC00D /**< Not in SSL3! */
  9809. #define MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA 0xC00E /**< Not in SSL3! */
  9810. #define MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA 0xC00F /**< Not in SSL3! */
  9811. #define MBEDTLS_TLS_ECDHE_RSA_WITH_NULL_SHA 0xC010 /**< Weak! */
  9812. #define MBEDTLS_TLS_ECDHE_RSA_WITH_RC4_128_SHA 0xC011 /**< Not in SSL3! */
  9813. #define MBEDTLS_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA 0xC012 /**< Not in SSL3! */
  9814. #define MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA 0xC013 /**< Not in SSL3! */
  9815. #define MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA 0xC014 /**< Not in SSL3! */
  9816. #define MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 0xC023 /**< TLS 1.2 */
  9817. #define MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 0xC024 /**< TLS 1.2 */
  9818. #define MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 0xC025 /**< TLS 1.2 */
  9819. #define MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 0xC026 /**< TLS 1.2 */
  9820. #define MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 0xC027 /**< TLS 1.2 */
  9821. #define MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 0xC028 /**< TLS 1.2 */
  9822. #define MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 0xC029 /**< TLS 1.2 */
  9823. #define MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 0xC02A /**< TLS 1.2 */
  9824. #define MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 0xC02B /**< TLS 1.2 */
  9825. #define MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 0xC02C /**< TLS 1.2 */
  9826. #define MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 0xC02D /**< TLS 1.2 */
  9827. #define MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 0xC02E /**< TLS 1.2 */
  9828. #define MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 0xC02F /**< TLS 1.2 */
  9829. #define MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 0xC030 /**< TLS 1.2 */
  9830. #define MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 0xC031 /**< TLS 1.2 */
  9831. #define MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 0xC032 /**< TLS 1.2 */
  9832. #define MBEDTLS_TLS_ECDHE_PSK_WITH_RC4_128_SHA 0xC033 /**< Not in SSL3! */
  9833. #define MBEDTLS_TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA 0xC034 /**< Not in SSL3! */
  9834. #define MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA 0xC035 /**< Not in SSL3! */
  9835. #define MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA 0xC036 /**< Not in SSL3! */
  9836. #define MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 0xC037 /**< Not in SSL3! */
  9837. #define MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384 0xC038 /**< Not in SSL3! */
  9838. #define MBEDTLS_TLS_ECDHE_PSK_WITH_NULL_SHA 0xC039 /**< Weak! No SSL3! */
  9839. #define MBEDTLS_TLS_ECDHE_PSK_WITH_NULL_SHA256 0xC03A /**< Weak! No SSL3! */
  9840. #define MBEDTLS_TLS_ECDHE_PSK_WITH_NULL_SHA384 0xC03B /**< Weak! No SSL3! */
  9841. #define MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256 0xC072 /**< Not in SSL3! */
  9842. #define MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384 0xC073 /**< Not in SSL3! */
  9843. #define MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256 0xC074 /**< Not in SSL3! */
  9844. #define MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384 0xC075 /**< Not in SSL3! */
  9845. #define MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 0xC076 /**< Not in SSL3! */
  9846. #define MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384 0xC077 /**< Not in SSL3! */
  9847. #define MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256 0xC078 /**< Not in SSL3! */
  9848. #define MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384 0xC079 /**< Not in SSL3! */
  9849. #define MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256 0xC07A /**< TLS 1.2 */
  9850. #define MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384 0xC07B /**< TLS 1.2 */
  9851. #define MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256 0xC07C /**< TLS 1.2 */
  9852. #define MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384 0xC07D /**< TLS 1.2 */
  9853. #define MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256 0xC086 /**< TLS 1.2 */
  9854. #define MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384 0xC087 /**< TLS 1.2 */
  9855. #define MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256 0xC088 /**< TLS 1.2 */
  9856. #define MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384 0xC089 /**< TLS 1.2 */
  9857. #define MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256 0xC08A /**< TLS 1.2 */
  9858. #define MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384 0xC08B /**< TLS 1.2 */
  9859. #define MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256 0xC08C /**< TLS 1.2 */
  9860. #define MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384 0xC08D /**< TLS 1.2 */
  9861. #define MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256 0xC08E /**< TLS 1.2 */
  9862. #define MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384 0xC08F /**< TLS 1.2 */
  9863. #define MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256 0xC090 /**< TLS 1.2 */
  9864. #define MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384 0xC091 /**< TLS 1.2 */
  9865. #define MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256 0xC092 /**< TLS 1.2 */
  9866. #define MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384 0xC093 /**< TLS 1.2 */
  9867. #define MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256 0xC094
  9868. #define MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384 0xC095
  9869. #define MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256 0xC096
  9870. #define MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384 0xC097
  9871. #define MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256 0xC098
  9872. #define MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384 0xC099
  9873. #define MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256 0xC09A /**< Not in SSL3! */
  9874. #define MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384 0xC09B /**< Not in SSL3! */
  9875. #define MBEDTLS_TLS_RSA_WITH_AES_128_CCM 0xC09C /**< TLS 1.2 */
  9876. #define MBEDTLS_TLS_RSA_WITH_AES_256_CCM 0xC09D /**< TLS 1.2 */
  9877. #define MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CCM 0xC09E /**< TLS 1.2 */
  9878. #define MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CCM 0xC09F /**< TLS 1.2 */
  9879. #define MBEDTLS_TLS_RSA_WITH_AES_128_CCM_8 0xC0A0 /**< TLS 1.2 */
  9880. #define MBEDTLS_TLS_RSA_WITH_AES_256_CCM_8 0xC0A1 /**< TLS 1.2 */
  9881. #define MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CCM_8 0xC0A2 /**< TLS 1.2 */
  9882. #define MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CCM_8 0xC0A3 /**< TLS 1.2 */
  9883. #define MBEDTLS_TLS_PSK_WITH_AES_128_CCM 0xC0A4 /**< TLS 1.2 */
  9884. #define MBEDTLS_TLS_PSK_WITH_AES_256_CCM 0xC0A5 /**< TLS 1.2 */
  9885. #define MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CCM 0xC0A6 /**< TLS 1.2 */
  9886. #define MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CCM 0xC0A7 /**< TLS 1.2 */
  9887. #define MBEDTLS_TLS_PSK_WITH_AES_128_CCM_8 0xC0A8 /**< TLS 1.2 */
  9888. #define MBEDTLS_TLS_PSK_WITH_AES_256_CCM_8 0xC0A9 /**< TLS 1.2 */
  9889. #define MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CCM_8 0xC0AA /**< TLS 1.2 */
  9890. #define MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CCM_8 0xC0AB /**< TLS 1.2 */
  9891. /* The last two are named with PSK_DHE in the RFC, which looks like a typo */
  9892. #define MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM 0xC0AC /**< TLS 1.2 */
  9893. #define MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM 0xC0AD /**< TLS 1.2 */
  9894. #define MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 0xC0AE /**< TLS 1.2 */
  9895. #define MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 0xC0AF /**< TLS 1.2 */
  9896. #define MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8 0xC0FF /**< experimental */
  9897. /* Reminder: update mbedtls_ssl_premaster_secret when adding a new key exchange.
  9898. * Reminder: update MBEDTLS_KEY_EXCHANGE__xxx below
  9899. */
  9900. typedef enum {
  9901. MBEDTLS_KEY_EXCHANGE_NONE = 0,
  9902. MBEDTLS_KEY_EXCHANGE_RSA,
  9903. MBEDTLS_KEY_EXCHANGE_DHE_RSA,
  9904. MBEDTLS_KEY_EXCHANGE_ECDHE_RSA,
  9905. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA,
  9906. MBEDTLS_KEY_EXCHANGE_PSK,
  9907. MBEDTLS_KEY_EXCHANGE_DHE_PSK,
  9908. MBEDTLS_KEY_EXCHANGE_RSA_PSK,
  9909. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK,
  9910. MBEDTLS_KEY_EXCHANGE_ECDH_RSA,
  9911. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA,
  9912. MBEDTLS_KEY_EXCHANGE_ECJPAKE,
  9913. } mbedtls_key_exchange_type_t;
  9914. /* Key exchanges using a certificate */
  9915. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
  9916. defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  9917. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  9918. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  9919. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) || \
  9920. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  9921. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  9922. #define MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED
  9923. #endif
  9924. /* Key exchanges using a PSK */
  9925. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
  9926. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) || \
  9927. defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
  9928. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  9929. #define MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
  9930. #endif
  9931. /* Key exchanges using a ECDHE */
  9932. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  9933. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  9934. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  9935. #define MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED
  9936. #endif
  9937. typedef struct mbedtls_ssl_ciphersuite_t mbedtls_ssl_ciphersuite_t;
  9938. #define MBEDTLS_CIPHERSUITE_WEAK 0x01 /**< Weak ciphersuite flag */
  9939. #define MBEDTLS_CIPHERSUITE_SHORT_TAG 0x02 /**< Short authentication tag,
  9940. eg for CCM_8 */
  9941. #define MBEDTLS_CIPHERSUITE_NODTLS 0x04 /**< Can't be used with DTLS */
  9942. /**
  9943. * \brief This structure is used for storing ciphersuite information
  9944. */
  9945. struct mbedtls_ssl_ciphersuite_t
  9946. {
  9947. int id;
  9948. const char * name;
  9949. mbedtls_cipher_type_t cipher;
  9950. mbedtls_md_type_t mac;
  9951. mbedtls_key_exchange_type_t key_exchange;
  9952. int min_major_ver;
  9953. int min_minor_ver;
  9954. int max_major_ver;
  9955. int max_minor_ver;
  9956. unsigned char flags;
  9957. };
  9958. const int *mbedtls_ssl_list_ciphersuites( void );
  9959. const mbedtls_ssl_ciphersuite_t *mbedtls_ssl_ciphersuite_from_string( const char *ciphersuite_name );
  9960. const mbedtls_ssl_ciphersuite_t *mbedtls_ssl_ciphersuite_from_id( int ciphersuite_id );
  9961. #if defined(MBEDTLS_PK_C)
  9962. mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg( const mbedtls_ssl_ciphersuite_t *info );
  9963. #endif
  9964. int mbedtls_ssl_ciphersuite_uses_ec( const mbedtls_ssl_ciphersuite_t *info );
  9965. int mbedtls_ssl_ciphersuite_uses_psk( const mbedtls_ssl_ciphersuite_t *info );
  9966. #ifdef __cplusplus
  9967. }
  9968. #endif
  9969. #endif /* ssl_ciphersuites.h */
  9970. /********* Start of file include/mbedtls/ecdh.h ************/
  9971. /**
  9972. * \file ecdh.h
  9973. *
  9974. * \brief Elliptic curve Diffie-Hellman
  9975. *
  9976. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  9977. * SPDX-License-Identifier: Apache-2.0
  9978. *
  9979. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9980. * not use this file except in compliance with the License.
  9981. * You may obtain a copy of the License at
  9982. *
  9983. * http://www.apache.org/licenses/LICENSE-2.0
  9984. *
  9985. * Unless required by applicable law or agreed to in writing, software
  9986. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9987. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9988. * See the License for the specific language governing permissions and
  9989. * limitations under the License.
  9990. *
  9991. * This file is part of mbed TLS (https://tls.mbed.org)
  9992. */
  9993. #ifndef MBEDTLS_ECDH_H
  9994. #define MBEDTLS_ECDH_H
  9995. #ifdef __cplusplus
  9996. extern "C" {
  9997. #endif
  9998. /**
  9999. * When importing from an EC key, select if it is our key or the peer's key
  10000. */
  10001. typedef enum
  10002. {
  10003. MBEDTLS_ECDH_OURS,
  10004. MBEDTLS_ECDH_THEIRS,
  10005. } mbedtls_ecdh_side;
  10006. /**
  10007. * \brief ECDH context structure
  10008. */
  10009. typedef struct
  10010. {
  10011. mbedtls_ecp_group grp; /*!< elliptic curve used */
  10012. mbedtls_mpi d; /*!< our secret value (private key) */
  10013. mbedtls_ecp_point Q; /*!< our public value (public key) */
  10014. mbedtls_ecp_point Qp; /*!< peer's public value (public key) */
  10015. mbedtls_mpi z; /*!< shared secret */
  10016. int point_format; /*!< format for point export in TLS messages */
  10017. mbedtls_ecp_point Vi; /*!< blinding value (for later) */
  10018. mbedtls_ecp_point Vf; /*!< un-blinding value (for later) */
  10019. mbedtls_mpi _d; /*!< previous d (for later) */
  10020. }
  10021. mbedtls_ecdh_context;
  10022. /**
  10023. * \brief Generate a public key.
  10024. * Raw function that only does the core computation.
  10025. *
  10026. * \param grp ECP group
  10027. * \param d Destination MPI (secret exponent, aka private key)
  10028. * \param Q Destination point (public key)
  10029. * \param f_rng RNG function
  10030. * \param p_rng RNG parameter
  10031. *
  10032. * \return 0 if successful,
  10033. * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
  10034. */
  10035. int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
  10036. int (*f_rng)(void *, unsigned char *, size_t),
  10037. void *p_rng );
  10038. /**
  10039. * \brief Compute shared secret
  10040. * Raw function that only does the core computation.
  10041. *
  10042. * \param grp ECP group
  10043. * \param z Destination MPI (shared secret)
  10044. * \param Q Public key from other party
  10045. * \param d Our secret exponent (private key)
  10046. * \param f_rng RNG function (see notes)
  10047. * \param p_rng RNG parameter
  10048. *
  10049. * \return 0 if successful,
  10050. * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
  10051. *
  10052. * \note If f_rng is not NULL, it is used to implement
  10053. * countermeasures against potential elaborate timing
  10054. * attacks, see \c mbedtls_ecp_mul() for details.
  10055. */
  10056. int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
  10057. const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
  10058. int (*f_rng)(void *, unsigned char *, size_t),
  10059. void *p_rng );
  10060. /**
  10061. * \brief Initialize context
  10062. *
  10063. * \param ctx Context to initialize
  10064. */
  10065. void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
  10066. /**
  10067. * \brief Free context
  10068. *
  10069. * \param ctx Context to free
  10070. */
  10071. void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
  10072. /**
  10073. * \brief Generate a public key and a TLS ServerKeyExchange payload.
  10074. * (First function used by a TLS server for ECDHE.)
  10075. *
  10076. * \param ctx ECDH context
  10077. * \param olen number of chars written
  10078. * \param buf destination buffer
  10079. * \param blen length of buffer
  10080. * \param f_rng RNG function
  10081. * \param p_rng RNG parameter
  10082. *
  10083. * \note This function assumes that ctx->grp has already been
  10084. * properly set (for example using mbedtls_ecp_group_load).
  10085. *
  10086. * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
  10087. */
  10088. int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
  10089. unsigned char *buf, size_t blen,
  10090. int (*f_rng)(void *, unsigned char *, size_t),
  10091. void *p_rng );
  10092. /**
  10093. * \brief Parse and procress a TLS ServerKeyExhange payload.
  10094. * (First function used by a TLS client for ECDHE.)
  10095. *
  10096. * \param ctx ECDH context
  10097. * \param buf pointer to start of input buffer
  10098. * \param end one past end of buffer
  10099. *
  10100. * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
  10101. */
  10102. int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
  10103. const unsigned char **buf, const unsigned char *end );
  10104. /**
  10105. * \brief Setup an ECDH context from an EC key.
  10106. * (Used by clients and servers in place of the
  10107. * ServerKeyEchange for static ECDH: import ECDH parameters
  10108. * from a certificate's EC key information.)
  10109. *
  10110. * \param ctx ECDH constext to set
  10111. * \param key EC key to use
  10112. * \param side Is it our key (1) or the peer's key (0) ?
  10113. *
  10114. * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
  10115. */
  10116. int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
  10117. mbedtls_ecdh_side side );
  10118. /**
  10119. * \brief Generate a public key and a TLS ClientKeyExchange payload.
  10120. * (Second function used by a TLS client for ECDH(E).)
  10121. *
  10122. * \param ctx ECDH context
  10123. * \param olen number of bytes actually written
  10124. * \param buf destination buffer
  10125. * \param blen size of destination buffer
  10126. * \param f_rng RNG function
  10127. * \param p_rng RNG parameter
  10128. *
  10129. * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
  10130. */
  10131. int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
  10132. unsigned char *buf, size_t blen,
  10133. int (*f_rng)(void *, unsigned char *, size_t),
  10134. void *p_rng );
  10135. /**
  10136. * \brief Parse and process a TLS ClientKeyExchange payload.
  10137. * (Second function used by a TLS server for ECDH(E).)
  10138. *
  10139. * \param ctx ECDH context
  10140. * \param buf start of input buffer
  10141. * \param blen length of input buffer
  10142. *
  10143. * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
  10144. */
  10145. int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
  10146. const unsigned char *buf, size_t blen );
  10147. /**
  10148. * \brief Derive and export the shared secret.
  10149. * (Last function used by both TLS client en servers.)
  10150. *
  10151. * \param ctx ECDH context
  10152. * \param olen number of bytes written
  10153. * \param buf destination buffer
  10154. * \param blen buffer length
  10155. * \param f_rng RNG function, see notes for \c mbedtls_ecdh_compute_shared()
  10156. * \param p_rng RNG parameter
  10157. *
  10158. * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
  10159. */
  10160. int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
  10161. unsigned char *buf, size_t blen,
  10162. int (*f_rng)(void *, unsigned char *, size_t),
  10163. void *p_rng );
  10164. #ifdef __cplusplus
  10165. }
  10166. #endif
  10167. #endif /* ecdh.h */
  10168. /********* Start of file include/mbedtls/sha1.h ************/
  10169. /**
  10170. * \file sha1.h
  10171. *
  10172. * \brief SHA-1 cryptographic hash function
  10173. *
  10174. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  10175. * SPDX-License-Identifier: Apache-2.0
  10176. *
  10177. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10178. * not use this file except in compliance with the License.
  10179. * You may obtain a copy of the License at
  10180. *
  10181. * http://www.apache.org/licenses/LICENSE-2.0
  10182. *
  10183. * Unless required by applicable law or agreed to in writing, software
  10184. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  10185. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10186. * See the License for the specific language governing permissions and
  10187. * limitations under the License.
  10188. *
  10189. * This file is part of mbed TLS (https://tls.mbed.org)
  10190. */
  10191. #ifndef MBEDTLS_SHA1_H
  10192. #define MBEDTLS_SHA1_H
  10193. #if !defined(MBEDTLS_CONFIG_FILE)
  10194. #else
  10195. #endif
  10196. #include <stddef.h>
  10197. #if !VXWORKS
  10198. #include <stdint.h>
  10199. #endif
  10200. #if !defined(MBEDTLS_SHA1_ALT)
  10201. // Regular implementation
  10202. //
  10203. #ifdef __cplusplus
  10204. extern "C" {
  10205. #endif
  10206. /**
  10207. * \brief SHA-1 context structure
  10208. */
  10209. typedef struct
  10210. {
  10211. uint32_t total[2]; /*!< number of bytes processed */
  10212. uint32_t state[5]; /*!< intermediate digest state */
  10213. unsigned char buffer[64]; /*!< data block being processed */
  10214. }
  10215. mbedtls_sha1_context;
  10216. /**
  10217. * \brief Initialize SHA-1 context
  10218. *
  10219. * \param ctx SHA-1 context to be initialized
  10220. */
  10221. void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
  10222. /**
  10223. * \brief Clear SHA-1 context
  10224. *
  10225. * \param ctx SHA-1 context to be cleared
  10226. */
  10227. void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
  10228. /**
  10229. * \brief Clone (the state of) a SHA-1 context
  10230. *
  10231. * \param dst The destination context
  10232. * \param src The context to be cloned
  10233. */
  10234. void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
  10235. const mbedtls_sha1_context *src );
  10236. /**
  10237. * \brief SHA-1 context setup
  10238. *
  10239. * \param ctx context to be initialized
  10240. */
  10241. void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
  10242. /**
  10243. * \brief SHA-1 process buffer
  10244. *
  10245. * \param ctx SHA-1 context
  10246. * \param input buffer holding the data
  10247. * \param ilen length of the input data
  10248. */
  10249. void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen );
  10250. /**
  10251. * \brief SHA-1 final digest
  10252. *
  10253. * \param ctx SHA-1 context
  10254. * \param output SHA-1 checksum result
  10255. */
  10256. void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] );
  10257. /* Internal use */
  10258. void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] );
  10259. #ifdef __cplusplus
  10260. }
  10261. #endif
  10262. #else /* MBEDTLS_SHA1_ALT */
  10263. #endif /* MBEDTLS_SHA1_ALT */
  10264. #ifdef __cplusplus
  10265. extern "C" {
  10266. #endif
  10267. /**
  10268. * \brief Output = SHA-1( input buffer )
  10269. *
  10270. * \param input buffer holding the data
  10271. * \param ilen length of the input data
  10272. * \param output SHA-1 checksum result
  10273. */
  10274. void mbedtls_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] );
  10275. /**
  10276. * \brief Checkup routine
  10277. *
  10278. * \return 0 if successful, or 1 if the test failed
  10279. */
  10280. int mbedtls_sha1_self_test( int verbose );
  10281. #ifdef __cplusplus
  10282. }
  10283. #endif
  10284. #endif /* mbedtls_sha1.h */
  10285. /********* Start of file include/mbedtls/sha256.h ************/
  10286. /**
  10287. * \file sha256.h
  10288. *
  10289. * \brief SHA-224 and SHA-256 cryptographic hash function
  10290. *
  10291. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  10292. * SPDX-License-Identifier: Apache-2.0
  10293. *
  10294. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10295. * not use this file except in compliance with the License.
  10296. * You may obtain a copy of the License at
  10297. *
  10298. * http://www.apache.org/licenses/LICENSE-2.0
  10299. *
  10300. * Unless required by applicable law or agreed to in writing, software
  10301. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  10302. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10303. * See the License for the specific language governing permissions and
  10304. * limitations under the License.
  10305. *
  10306. * This file is part of mbed TLS (https://tls.mbed.org)
  10307. */
  10308. #ifndef MBEDTLS_SHA256_H
  10309. #define MBEDTLS_SHA256_H
  10310. #if !defined(MBEDTLS_CONFIG_FILE)
  10311. #else
  10312. #endif
  10313. #include <stddef.h>
  10314. #if !VXWORKS
  10315. #include <stdint.h>
  10316. #endif
  10317. #if !defined(MBEDTLS_SHA256_ALT)
  10318. // Regular implementation
  10319. //
  10320. #ifdef __cplusplus
  10321. extern "C" {
  10322. #endif
  10323. /**
  10324. * \brief SHA-256 context structure
  10325. */
  10326. typedef struct
  10327. {
  10328. uint32_t total[2]; /*!< number of bytes processed */
  10329. uint32_t state[8]; /*!< intermediate digest state */
  10330. unsigned char buffer[64]; /*!< data block being processed */
  10331. int is224; /*!< 0 => SHA-256, else SHA-224 */
  10332. }
  10333. mbedtls_sha256_context;
  10334. /**
  10335. * \brief Initialize SHA-256 context
  10336. *
  10337. * \param ctx SHA-256 context to be initialized
  10338. */
  10339. void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
  10340. /**
  10341. * \brief Clear SHA-256 context
  10342. *
  10343. * \param ctx SHA-256 context to be cleared
  10344. */
  10345. void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
  10346. /**
  10347. * \brief Clone (the state of) a SHA-256 context
  10348. *
  10349. * \param dst The destination context
  10350. * \param src The context to be cloned
  10351. */
  10352. void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
  10353. const mbedtls_sha256_context *src );
  10354. /**
  10355. * \brief SHA-256 context setup
  10356. *
  10357. * \param ctx context to be initialized
  10358. * \param is224 0 = use SHA256, 1 = use SHA224
  10359. */
  10360. void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 );
  10361. /**
  10362. * \brief SHA-256 process buffer
  10363. *
  10364. * \param ctx SHA-256 context
  10365. * \param input buffer holding the data
  10366. * \param ilen length of the input data
  10367. */
  10368. void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input,
  10369. size_t ilen );
  10370. /**
  10371. * \brief SHA-256 final digest
  10372. *
  10373. * \param ctx SHA-256 context
  10374. * \param output SHA-224/256 checksum result
  10375. */
  10376. void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] );
  10377. /* Internal use */
  10378. void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] );
  10379. #ifdef __cplusplus
  10380. }
  10381. #endif
  10382. #else /* MBEDTLS_SHA256_ALT */
  10383. #endif /* MBEDTLS_SHA256_ALT */
  10384. #ifdef __cplusplus
  10385. extern "C" {
  10386. #endif
  10387. /**
  10388. * \brief Output = SHA-256( input buffer )
  10389. *
  10390. * \param input buffer holding the data
  10391. * \param ilen length of the input data
  10392. * \param output SHA-224/256 checksum result
  10393. * \param is224 0 = use SHA256, 1 = use SHA224
  10394. */
  10395. void mbedtls_sha256( const unsigned char *input, size_t ilen,
  10396. unsigned char output[32], int is224 );
  10397. /**
  10398. * \brief Checkup routine
  10399. *
  10400. * \return 0 if successful, or 1 if the test failed
  10401. */
  10402. int mbedtls_sha256_self_test( int verbose );
  10403. #ifdef __cplusplus
  10404. }
  10405. #endif
  10406. #endif /* mbedtls_sha256.h */
  10407. /********* Start of file include/mbedtls/sha512.h ************/
  10408. /**
  10409. * \file sha512.h
  10410. *
  10411. * \brief SHA-384 and SHA-512 cryptographic hash function
  10412. *
  10413. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  10414. * SPDX-License-Identifier: Apache-2.0
  10415. *
  10416. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10417. * not use this file except in compliance with the License.
  10418. * You may obtain a copy of the License at
  10419. *
  10420. * http://www.apache.org/licenses/LICENSE-2.0
  10421. *
  10422. * Unless required by applicable law or agreed to in writing, software
  10423. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  10424. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10425. * See the License for the specific language governing permissions and
  10426. * limitations under the License.
  10427. *
  10428. * This file is part of mbed TLS (https://tls.mbed.org)
  10429. */
  10430. #ifndef MBEDTLS_SHA512_H
  10431. #define MBEDTLS_SHA512_H
  10432. #if !defined(MBEDTLS_CONFIG_FILE)
  10433. #else
  10434. #endif
  10435. #include <stddef.h>
  10436. #if !VXWORKS
  10437. #include <stdint.h>
  10438. #endif
  10439. #if !defined(MBEDTLS_SHA512_ALT)
  10440. // Regular implementation
  10441. //
  10442. #ifdef __cplusplus
  10443. extern "C" {
  10444. #endif
  10445. /**
  10446. * \brief SHA-512 context structure
  10447. */
  10448. typedef struct
  10449. {
  10450. uint64_t total[2]; /*!< number of bytes processed */
  10451. uint64_t state[8]; /*!< intermediate digest state */
  10452. unsigned char buffer[128]; /*!< data block being processed */
  10453. int is384; /*!< 0 => SHA-512, else SHA-384 */
  10454. }
  10455. mbedtls_sha512_context;
  10456. /**
  10457. * \brief Initialize SHA-512 context
  10458. *
  10459. * \param ctx SHA-512 context to be initialized
  10460. */
  10461. void mbedtls_sha512_init( mbedtls_sha512_context *ctx );
  10462. /**
  10463. * \brief Clear SHA-512 context
  10464. *
  10465. * \param ctx SHA-512 context to be cleared
  10466. */
  10467. void mbedtls_sha512_free( mbedtls_sha512_context *ctx );
  10468. /**
  10469. * \brief Clone (the state of) a SHA-512 context
  10470. *
  10471. * \param dst The destination context
  10472. * \param src The context to be cloned
  10473. */
  10474. void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
  10475. const mbedtls_sha512_context *src );
  10476. /**
  10477. * \brief SHA-512 context setup
  10478. *
  10479. * \param ctx context to be initialized
  10480. * \param is384 0 = use SHA512, 1 = use SHA384
  10481. */
  10482. void mbedtls_sha512_starts( mbedtls_sha512_context *ctx, int is384 );
  10483. /**
  10484. * \brief SHA-512 process buffer
  10485. *
  10486. * \param ctx SHA-512 context
  10487. * \param input buffer holding the data
  10488. * \param ilen length of the input data
  10489. */
  10490. void mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *input,
  10491. size_t ilen );
  10492. /**
  10493. * \brief SHA-512 final digest
  10494. *
  10495. * \param ctx SHA-512 context
  10496. * \param output SHA-384/512 checksum result
  10497. */
  10498. void mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char output[64] );
  10499. #ifdef __cplusplus
  10500. }
  10501. #endif
  10502. #else /* MBEDTLS_SHA512_ALT */
  10503. #endif /* MBEDTLS_SHA512_ALT */
  10504. #ifdef __cplusplus
  10505. extern "C" {
  10506. #endif
  10507. /**
  10508. * \brief Output = SHA-512( input buffer )
  10509. *
  10510. * \param input buffer holding the data
  10511. * \param ilen length of the input data
  10512. * \param output SHA-384/512 checksum result
  10513. * \param is384 0 = use SHA512, 1 = use SHA384
  10514. */
  10515. void mbedtls_sha512( const unsigned char *input, size_t ilen,
  10516. unsigned char output[64], int is384 );
  10517. /**
  10518. * \brief Checkup routine
  10519. *
  10520. * \return 0 if successful, or 1 if the test failed
  10521. */
  10522. int mbedtls_sha512_self_test( int verbose );
  10523. /* Internal use */
  10524. void mbedtls_sha512_process( mbedtls_sha512_context *ctx, const unsigned char data[128] );
  10525. #ifdef __cplusplus
  10526. }
  10527. #endif
  10528. #endif /* mbedtls_sha512.h */
  10529. /********* Start of file include/mbedtls/aes.h ************/
  10530. /**
  10531. * \file aes.h
  10532. *
  10533. * \brief AES block cipher
  10534. *
  10535. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  10536. * SPDX-License-Identifier: Apache-2.0
  10537. *
  10538. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10539. * not use this file except in compliance with the License.
  10540. * You may obtain a copy of the License at
  10541. *
  10542. * http://www.apache.org/licenses/LICENSE-2.0
  10543. *
  10544. * Unless required by applicable law or agreed to in writing, software
  10545. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  10546. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10547. * See the License for the specific language governing permissions and
  10548. * limitations under the License.
  10549. *
  10550. * This file is part of mbed TLS (https://tls.mbed.org)
  10551. */
  10552. #ifndef MBEDTLS_AES_H
  10553. #define MBEDTLS_AES_H
  10554. #if !defined(MBEDTLS_CONFIG_FILE)
  10555. #else
  10556. #endif
  10557. #include <stddef.h>
  10558. #if !VXWORKS
  10559. #include <stdint.h>
  10560. #endif
  10561. /* padlock.c and aesni.c rely on these values! */
  10562. #define MBEDTLS_AES_ENCRYPT 1
  10563. #define MBEDTLS_AES_DECRYPT 0
  10564. #define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
  10565. #define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
  10566. #if !defined(MBEDTLS_AES_ALT)
  10567. // Regular implementation
  10568. //
  10569. #ifdef __cplusplus
  10570. extern "C" {
  10571. #endif
  10572. /**
  10573. * \brief AES context structure
  10574. *
  10575. * \note buf is able to hold 32 extra bytes, which can be used:
  10576. * - for alignment purposes if VIA padlock is used, and/or
  10577. * - to simplify key expansion in the 256-bit case by
  10578. * generating an extra round key
  10579. */
  10580. typedef struct
  10581. {
  10582. int nr; /*!< number of rounds */
  10583. uint32_t *rk; /*!< AES round keys */
  10584. uint32_t buf[68]; /*!< unaligned data */
  10585. }
  10586. mbedtls_aes_context;
  10587. /**
  10588. * \brief Initialize AES context
  10589. *
  10590. * \param ctx AES context to be initialized
  10591. */
  10592. void mbedtls_aes_init( mbedtls_aes_context *ctx );
  10593. /**
  10594. * \brief Clear AES context
  10595. *
  10596. * \param ctx AES context to be cleared
  10597. */
  10598. void mbedtls_aes_free( mbedtls_aes_context *ctx );
  10599. /**
  10600. * \brief AES key schedule (encryption)
  10601. *
  10602. * \param ctx AES context to be initialized
  10603. * \param key encryption key
  10604. * \param keybits must be 128, 192 or 256
  10605. *
  10606. * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
  10607. */
  10608. int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
  10609. unsigned int keybits );
  10610. /**
  10611. * \brief AES key schedule (decryption)
  10612. *
  10613. * \param ctx AES context to be initialized
  10614. * \param key decryption key
  10615. * \param keybits must be 128, 192 or 256
  10616. *
  10617. * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
  10618. */
  10619. int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
  10620. unsigned int keybits );
  10621. /**
  10622. * \brief AES-ECB block encryption/decryption
  10623. *
  10624. * \param ctx AES context
  10625. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  10626. * \param input 16-byte input block
  10627. * \param output 16-byte output block
  10628. *
  10629. * \return 0 if successful
  10630. */
  10631. int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
  10632. int mode,
  10633. const unsigned char input[16],
  10634. unsigned char output[16] );
  10635. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  10636. /**
  10637. * \brief AES-CBC buffer encryption/decryption
  10638. * Length should be a multiple of the block
  10639. * size (16 bytes)
  10640. *
  10641. * \note Upon exit, the content of the IV is updated so that you can
  10642. * call the function same function again on the following
  10643. * block(s) of data and get the same result as if it was
  10644. * encrypted in one call. This allows a "streaming" usage.
  10645. * If on the other hand you need to retain the contents of the
  10646. * IV, you should either save it manually or use the cipher
  10647. * module instead.
  10648. *
  10649. * \param ctx AES context
  10650. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  10651. * \param length length of the input data
  10652. * \param iv initialization vector (updated after use)
  10653. * \param input buffer holding the input data
  10654. * \param output buffer holding the output data
  10655. *
  10656. * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
  10657. */
  10658. int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
  10659. int mode,
  10660. size_t length,
  10661. unsigned char iv[16],
  10662. const unsigned char *input,
  10663. unsigned char *output );
  10664. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  10665. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  10666. /**
  10667. * \brief AES-CFB128 buffer encryption/decryption.
  10668. *
  10669. * Note: Due to the nature of CFB you should use the same key schedule for
  10670. * both encryption and decryption. So a context initialized with
  10671. * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
  10672. *
  10673. * \note Upon exit, the content of the IV is updated so that you can
  10674. * call the function same function again on the following
  10675. * block(s) of data and get the same result as if it was
  10676. * encrypted in one call. This allows a "streaming" usage.
  10677. * If on the other hand you need to retain the contents of the
  10678. * IV, you should either save it manually or use the cipher
  10679. * module instead.
  10680. *
  10681. * \param ctx AES context
  10682. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  10683. * \param length length of the input data
  10684. * \param iv_off offset in IV (updated after use)
  10685. * \param iv initialization vector (updated after use)
  10686. * \param input buffer holding the input data
  10687. * \param output buffer holding the output data
  10688. *
  10689. * \return 0 if successful
  10690. */
  10691. int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
  10692. int mode,
  10693. size_t length,
  10694. size_t *iv_off,
  10695. unsigned char iv[16],
  10696. const unsigned char *input,
  10697. unsigned char *output );
  10698. /**
  10699. * \brief AES-CFB8 buffer encryption/decryption.
  10700. *
  10701. * Note: Due to the nature of CFB you should use the same key schedule for
  10702. * both encryption and decryption. So a context initialized with
  10703. * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
  10704. *
  10705. * \note Upon exit, the content of the IV is updated so that you can
  10706. * call the function same function again on the following
  10707. * block(s) of data and get the same result as if it was
  10708. * encrypted in one call. This allows a "streaming" usage.
  10709. * If on the other hand you need to retain the contents of the
  10710. * IV, you should either save it manually or use the cipher
  10711. * module instead.
  10712. *
  10713. * \param ctx AES context
  10714. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  10715. * \param length length of the input data
  10716. * \param iv initialization vector (updated after use)
  10717. * \param input buffer holding the input data
  10718. * \param output buffer holding the output data
  10719. *
  10720. * \return 0 if successful
  10721. */
  10722. int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
  10723. int mode,
  10724. size_t length,
  10725. unsigned char iv[16],
  10726. const unsigned char *input,
  10727. unsigned char *output );
  10728. #endif /*MBEDTLS_CIPHER_MODE_CFB */
  10729. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  10730. /**
  10731. * \brief AES-CTR buffer encryption/decryption
  10732. *
  10733. * Warning: You have to keep the maximum use of your counter in mind!
  10734. *
  10735. * Note: Due to the nature of CTR you should use the same key schedule for
  10736. * both encryption and decryption. So a context initialized with
  10737. * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
  10738. *
  10739. * \param ctx AES context
  10740. * \param length The length of the data
  10741. * \param nc_off The offset in the current stream_block (for resuming
  10742. * within current cipher stream). The offset pointer to
  10743. * should be 0 at the start of a stream.
  10744. * \param nonce_counter The 128-bit nonce and counter.
  10745. * \param stream_block The saved stream-block for resuming. Is overwritten
  10746. * by the function.
  10747. * \param input The input data stream
  10748. * \param output The output data stream
  10749. *
  10750. * \return 0 if successful
  10751. */
  10752. int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
  10753. size_t length,
  10754. size_t *nc_off,
  10755. unsigned char nonce_counter[16],
  10756. unsigned char stream_block[16],
  10757. const unsigned char *input,
  10758. unsigned char *output );
  10759. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  10760. /**
  10761. * \brief Internal AES block encryption function
  10762. * (Only exposed to allow overriding it,
  10763. * see MBEDTLS_AES_ENCRYPT_ALT)
  10764. *
  10765. * \param ctx AES context
  10766. * \param input Plaintext block
  10767. * \param output Output (ciphertext) block
  10768. */
  10769. void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
  10770. const unsigned char input[16],
  10771. unsigned char output[16] );
  10772. /**
  10773. * \brief Internal AES block decryption function
  10774. * (Only exposed to allow overriding it,
  10775. * see MBEDTLS_AES_DECRYPT_ALT)
  10776. *
  10777. * \param ctx AES context
  10778. * \param input Ciphertext block
  10779. * \param output Output (plaintext) block
  10780. */
  10781. void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
  10782. const unsigned char input[16],
  10783. unsigned char output[16] );
  10784. #ifdef __cplusplus
  10785. }
  10786. #endif
  10787. #else /* MBEDTLS_AES_ALT */
  10788. #endif /* MBEDTLS_AES_ALT */
  10789. #ifdef __cplusplus
  10790. extern "C" {
  10791. #endif
  10792. /**
  10793. * \brief Checkup routine
  10794. *
  10795. * \return 0 if successful, or 1 if the test failed
  10796. */
  10797. int mbedtls_aes_self_test( int verbose );
  10798. #ifdef __cplusplus
  10799. }
  10800. #endif
  10801. #endif /* aes.h */
  10802. /********* Start of file include/mbedtls/aesni.h ************/
  10803. /**
  10804. * \file aesni.h
  10805. *
  10806. * \brief AES-NI for hardware AES acceleration on some Intel processors
  10807. *
  10808. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  10809. * SPDX-License-Identifier: Apache-2.0
  10810. *
  10811. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10812. * not use this file except in compliance with the License.
  10813. * You may obtain a copy of the License at
  10814. *
  10815. * http://www.apache.org/licenses/LICENSE-2.0
  10816. *
  10817. * Unless required by applicable law or agreed to in writing, software
  10818. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  10819. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10820. * See the License for the specific language governing permissions and
  10821. * limitations under the License.
  10822. *
  10823. * This file is part of mbed TLS (https://tls.mbed.org)
  10824. */
  10825. #ifndef MBEDTLS_AESNI_H
  10826. #define MBEDTLS_AESNI_H
  10827. #define MBEDTLS_AESNI_AES 0x02000000u
  10828. #define MBEDTLS_AESNI_CLMUL 0x00000002u
  10829. #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
  10830. ( defined(__amd64__) || defined(__x86_64__) ) && \
  10831. ! defined(MBEDTLS_HAVE_X86_64)
  10832. #define MBEDTLS_HAVE_X86_64
  10833. #endif
  10834. #if defined(MBEDTLS_HAVE_X86_64)
  10835. #ifdef __cplusplus
  10836. extern "C" {
  10837. #endif
  10838. /**
  10839. * \brief AES-NI features detection routine
  10840. *
  10841. * \param what The feature to detect
  10842. * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
  10843. *
  10844. * \return 1 if CPU has support for the feature, 0 otherwise
  10845. */
  10846. int mbedtls_aesni_has_support( unsigned int what );
  10847. /**
  10848. * \brief AES-NI AES-ECB block en(de)cryption
  10849. *
  10850. * \param ctx AES context
  10851. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  10852. * \param input 16-byte input block
  10853. * \param output 16-byte output block
  10854. *
  10855. * \return 0 on success (cannot fail)
  10856. */
  10857. int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx,
  10858. int mode,
  10859. const unsigned char input[16],
  10860. unsigned char output[16] );
  10861. /**
  10862. * \brief GCM multiplication: c = a * b in GF(2^128)
  10863. *
  10864. * \param c Result
  10865. * \param a First operand
  10866. * \param b Second operand
  10867. *
  10868. * \note Both operands and result are bit strings interpreted as
  10869. * elements of GF(2^128) as per the GCM spec.
  10870. */
  10871. void mbedtls_aesni_gcm_mult( unsigned char c[16],
  10872. const unsigned char a[16],
  10873. const unsigned char b[16] );
  10874. /**
  10875. * \brief Compute decryption round keys from encryption round keys
  10876. *
  10877. * \param invkey Round keys for the equivalent inverse cipher
  10878. * \param fwdkey Original round keys (for encryption)
  10879. * \param nr Number of rounds (that is, number of round keys minus one)
  10880. */
  10881. void mbedtls_aesni_inverse_key( unsigned char *invkey,
  10882. const unsigned char *fwdkey, int nr );
  10883. /**
  10884. * \brief Perform key expansion (for encryption)
  10885. *
  10886. * \param rk Destination buffer where the round keys are written
  10887. * \param key Encryption key
  10888. * \param bits Key size in bits (must be 128, 192 or 256)
  10889. *
  10890. * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
  10891. */
  10892. int mbedtls_aesni_setkey_enc( unsigned char *rk,
  10893. const unsigned char *key,
  10894. size_t bits );
  10895. #ifdef __cplusplus
  10896. }
  10897. #endif
  10898. #endif /* MBEDTLS_HAVE_X86_64 */
  10899. #endif /* MBEDTLS_AESNI_H */
  10900. /********* Start of file include/mbedtls/arc4.h ************/
  10901. /**
  10902. * \file arc4.h
  10903. *
  10904. * \brief The ARCFOUR stream cipher
  10905. *
  10906. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  10907. * SPDX-License-Identifier: Apache-2.0
  10908. *
  10909. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10910. * not use this file except in compliance with the License.
  10911. * You may obtain a copy of the License at
  10912. *
  10913. * http://www.apache.org/licenses/LICENSE-2.0
  10914. *
  10915. * Unless required by applicable law or agreed to in writing, software
  10916. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  10917. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10918. * See the License for the specific language governing permissions and
  10919. * limitations under the License.
  10920. *
  10921. * This file is part of mbed TLS (https://tls.mbed.org)
  10922. */
  10923. #ifndef MBEDTLS_ARC4_H
  10924. #define MBEDTLS_ARC4_H
  10925. #if !defined(MBEDTLS_CONFIG_FILE)
  10926. #else
  10927. #endif
  10928. #include <stddef.h>
  10929. #if !defined(MBEDTLS_ARC4_ALT)
  10930. // Regular implementation
  10931. //
  10932. #ifdef __cplusplus
  10933. extern "C" {
  10934. #endif
  10935. /**
  10936. * \brief ARC4 context structure
  10937. */
  10938. typedef struct
  10939. {
  10940. int x; /*!< permutation index */
  10941. int y; /*!< permutation index */
  10942. unsigned char m[256]; /*!< permutation table */
  10943. }
  10944. mbedtls_arc4_context;
  10945. /**
  10946. * \brief Initialize ARC4 context
  10947. *
  10948. * \param ctx ARC4 context to be initialized
  10949. */
  10950. void mbedtls_arc4_init( mbedtls_arc4_context *ctx );
  10951. /**
  10952. * \brief Clear ARC4 context
  10953. *
  10954. * \param ctx ARC4 context to be cleared
  10955. */
  10956. void mbedtls_arc4_free( mbedtls_arc4_context *ctx );
  10957. /**
  10958. * \brief ARC4 key schedule
  10959. *
  10960. * \param ctx ARC4 context to be setup
  10961. * \param key the secret key
  10962. * \param keylen length of the key, in bytes
  10963. */
  10964. void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key,
  10965. unsigned int keylen );
  10966. /**
  10967. * \brief ARC4 cipher function
  10968. *
  10969. * \param ctx ARC4 context
  10970. * \param length length of the input data
  10971. * \param input buffer holding the input data
  10972. * \param output buffer for the output data
  10973. *
  10974. * \return 0 if successful
  10975. */
  10976. int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned char *input,
  10977. unsigned char *output );
  10978. #ifdef __cplusplus
  10979. }
  10980. #endif
  10981. #else /* MBEDTLS_ARC4_ALT */
  10982. #endif /* MBEDTLS_ARC4_ALT */
  10983. #ifdef __cplusplus
  10984. extern "C" {
  10985. #endif
  10986. /**
  10987. * \brief Checkup routine
  10988. *
  10989. * \return 0 if successful, or 1 if the test failed
  10990. */
  10991. int mbedtls_arc4_self_test( int verbose );
  10992. #ifdef __cplusplus
  10993. }
  10994. #endif
  10995. #endif /* arc4.h */
  10996. /********* Start of file include/mbedtls/base64.h ************/
  10997. /**
  10998. * \file base64.h
  10999. *
  11000. * \brief RFC 1521 base64 encoding/decoding
  11001. *
  11002. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  11003. * SPDX-License-Identifier: Apache-2.0
  11004. *
  11005. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11006. * not use this file except in compliance with the License.
  11007. * You may obtain a copy of the License at
  11008. *
  11009. * http://www.apache.org/licenses/LICENSE-2.0
  11010. *
  11011. * Unless required by applicable law or agreed to in writing, software
  11012. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11013. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11014. * See the License for the specific language governing permissions and
  11015. * limitations under the License.
  11016. *
  11017. * This file is part of mbed TLS (https://tls.mbed.org)
  11018. */
  11019. #ifndef MBEDTLS_BASE64_H
  11020. #define MBEDTLS_BASE64_H
  11021. #include <stddef.h>
  11022. #define MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL -0x002A /**< Output buffer too small. */
  11023. #define MBEDTLS_ERR_BASE64_INVALID_CHARACTER -0x002C /**< Invalid character in input. */
  11024. #ifdef __cplusplus
  11025. extern "C" {
  11026. #endif
  11027. /**
  11028. * \brief Encode a buffer into base64 format
  11029. *
  11030. * \param dst destination buffer
  11031. * \param dlen size of the destination buffer
  11032. * \param olen number of bytes written
  11033. * \param src source buffer
  11034. * \param slen amount of data to be encoded
  11035. *
  11036. * \return 0 if successful, or MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL.
  11037. * *olen is always updated to reflect the amount
  11038. * of data that has (or would have) been written.
  11039. * If that length cannot be represented, then no data is
  11040. * written to the buffer and *olen is set to the maximum
  11041. * length representable as a size_t.
  11042. *
  11043. * \note Call this function with dlen = 0 to obtain the
  11044. * required buffer size in *olen
  11045. */
  11046. int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
  11047. const unsigned char *src, size_t slen );
  11048. /**
  11049. * \brief Decode a base64-formatted buffer
  11050. *
  11051. * \param dst destination buffer (can be NULL for checking size)
  11052. * \param dlen size of the destination buffer
  11053. * \param olen number of bytes written
  11054. * \param src source buffer
  11055. * \param slen amount of data to be decoded
  11056. *
  11057. * \return 0 if successful, MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL, or
  11058. * MBEDTLS_ERR_BASE64_INVALID_CHARACTER if the input data is
  11059. * not correct. *olen is always updated to reflect the amount
  11060. * of data that has (or would have) been written.
  11061. *
  11062. * \note Call this function with *dst = NULL or dlen = 0 to obtain
  11063. * the required buffer size in *olen
  11064. */
  11065. int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
  11066. const unsigned char *src, size_t slen );
  11067. /**
  11068. * \brief Checkup routine
  11069. *
  11070. * \return 0 if successful, or 1 if the test failed
  11071. */
  11072. int mbedtls_base64_self_test( int verbose );
  11073. #ifdef __cplusplus
  11074. }
  11075. #endif
  11076. #endif /* base64.h */
  11077. /********* Start of file include/mbedtls/bn_mul.h ************/
  11078. /**
  11079. * \file bn_mul.h
  11080. *
  11081. * \brief Multi-precision integer library
  11082. *
  11083. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  11084. * SPDX-License-Identifier: Apache-2.0
  11085. *
  11086. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11087. * not use this file except in compliance with the License.
  11088. * You may obtain a copy of the License at
  11089. *
  11090. * http://www.apache.org/licenses/LICENSE-2.0
  11091. *
  11092. * Unless required by applicable law or agreed to in writing, software
  11093. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11094. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11095. * See the License for the specific language governing permissions and
  11096. * limitations under the License.
  11097. *
  11098. * This file is part of mbed TLS (https://tls.mbed.org)
  11099. */
  11100. /*
  11101. * Multiply source vector [s] with b, add result
  11102. * to destination vector [d] and set carry c.
  11103. *
  11104. * Currently supports:
  11105. *
  11106. * . IA-32 (386+) . AMD64 / EM64T
  11107. * . IA-32 (SSE2) . Motorola 68000
  11108. * . PowerPC, 32-bit . MicroBlaze
  11109. * . PowerPC, 64-bit . TriCore
  11110. * . SPARC v8 . ARM v3+
  11111. * . Alpha . MIPS32
  11112. * . C, longlong . C, generic
  11113. */
  11114. #ifndef MBEDTLS_BN_MUL_H
  11115. #define MBEDTLS_BN_MUL_H
  11116. #if defined(MBEDTLS_HAVE_ASM)
  11117. #ifndef asm
  11118. #define asm __asm
  11119. #endif
  11120. /* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */
  11121. #if defined(__GNUC__) && \
  11122. ( !defined(__ARMCC_VERSION) || __ARMCC_VERSION >= 6000000 )
  11123. #if defined(__i386__)
  11124. #define MULADDC_INIT \
  11125. asm( \
  11126. "movl %%ebx, %0 \n\t" \
  11127. "movl %5, %%esi \n\t" \
  11128. "movl %6, %%edi \n\t" \
  11129. "movl %7, %%ecx \n\t" \
  11130. "movl %8, %%ebx \n\t"
  11131. #define MULADDC_CORE \
  11132. "lodsl \n\t" \
  11133. "mull %%ebx \n\t" \
  11134. "addl %%ecx, %%eax \n\t" \
  11135. "adcl $0, %%edx \n\t" \
  11136. "addl (%%edi), %%eax \n\t" \
  11137. "adcl $0, %%edx \n\t" \
  11138. "movl %%edx, %%ecx \n\t" \
  11139. "stosl \n\t"
  11140. #if defined(MBEDTLS_HAVE_SSE2)
  11141. #define MULADDC_HUIT \
  11142. "movd %%ecx, %%mm1 \n\t" \
  11143. "movd %%ebx, %%mm0 \n\t" \
  11144. "movd (%%edi), %%mm3 \n\t" \
  11145. "paddq %%mm3, %%mm1 \n\t" \
  11146. "movd (%%esi), %%mm2 \n\t" \
  11147. "pmuludq %%mm0, %%mm2 \n\t" \
  11148. "movd 4(%%esi), %%mm4 \n\t" \
  11149. "pmuludq %%mm0, %%mm4 \n\t" \
  11150. "movd 8(%%esi), %%mm6 \n\t" \
  11151. "pmuludq %%mm0, %%mm6 \n\t" \
  11152. "movd 12(%%esi), %%mm7 \n\t" \
  11153. "pmuludq %%mm0, %%mm7 \n\t" \
  11154. "paddq %%mm2, %%mm1 \n\t" \
  11155. "movd 4(%%edi), %%mm3 \n\t" \
  11156. "paddq %%mm4, %%mm3 \n\t" \
  11157. "movd 8(%%edi), %%mm5 \n\t" \
  11158. "paddq %%mm6, %%mm5 \n\t" \
  11159. "movd 12(%%edi), %%mm4 \n\t" \
  11160. "paddq %%mm4, %%mm7 \n\t" \
  11161. "movd %%mm1, (%%edi) \n\t" \
  11162. "movd 16(%%esi), %%mm2 \n\t" \
  11163. "pmuludq %%mm0, %%mm2 \n\t" \
  11164. "psrlq $32, %%mm1 \n\t" \
  11165. "movd 20(%%esi), %%mm4 \n\t" \
  11166. "pmuludq %%mm0, %%mm4 \n\t" \
  11167. "paddq %%mm3, %%mm1 \n\t" \
  11168. "movd 24(%%esi), %%mm6 \n\t" \
  11169. "pmuludq %%mm0, %%mm6 \n\t" \
  11170. "movd %%mm1, 4(%%edi) \n\t" \
  11171. "psrlq $32, %%mm1 \n\t" \
  11172. "movd 28(%%esi), %%mm3 \n\t" \
  11173. "pmuludq %%mm0, %%mm3 \n\t" \
  11174. "paddq %%mm5, %%mm1 \n\t" \
  11175. "movd 16(%%edi), %%mm5 \n\t" \
  11176. "paddq %%mm5, %%mm2 \n\t" \
  11177. "movd %%mm1, 8(%%edi) \n\t" \
  11178. "psrlq $32, %%mm1 \n\t" \
  11179. "paddq %%mm7, %%mm1 \n\t" \
  11180. "movd 20(%%edi), %%mm5 \n\t" \
  11181. "paddq %%mm5, %%mm4 \n\t" \
  11182. "movd %%mm1, 12(%%edi) \n\t" \
  11183. "psrlq $32, %%mm1 \n\t" \
  11184. "paddq %%mm2, %%mm1 \n\t" \
  11185. "movd 24(%%edi), %%mm5 \n\t" \
  11186. "paddq %%mm5, %%mm6 \n\t" \
  11187. "movd %%mm1, 16(%%edi) \n\t" \
  11188. "psrlq $32, %%mm1 \n\t" \
  11189. "paddq %%mm4, %%mm1 \n\t" \
  11190. "movd 28(%%edi), %%mm5 \n\t" \
  11191. "paddq %%mm5, %%mm3 \n\t" \
  11192. "movd %%mm1, 20(%%edi) \n\t" \
  11193. "psrlq $32, %%mm1 \n\t" \
  11194. "paddq %%mm6, %%mm1 \n\t" \
  11195. "movd %%mm1, 24(%%edi) \n\t" \
  11196. "psrlq $32, %%mm1 \n\t" \
  11197. "paddq %%mm3, %%mm1 \n\t" \
  11198. "movd %%mm1, 28(%%edi) \n\t" \
  11199. "addl $32, %%edi \n\t" \
  11200. "addl $32, %%esi \n\t" \
  11201. "psrlq $32, %%mm1 \n\t" \
  11202. "movd %%mm1, %%ecx \n\t"
  11203. #define MULADDC_STOP \
  11204. "emms \n\t" \
  11205. "movl %4, %%ebx \n\t" \
  11206. "movl %%ecx, %1 \n\t" \
  11207. "movl %%edi, %2 \n\t" \
  11208. "movl %%esi, %3 \n\t" \
  11209. : "=m" (t), "=m" (c), "=m" (d), "=m" (s) \
  11210. : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \
  11211. : "eax", "ecx", "edx", "esi", "edi" \
  11212. );
  11213. #else
  11214. #define MULADDC_STOP \
  11215. "movl %4, %%ebx \n\t" \
  11216. "movl %%ecx, %1 \n\t" \
  11217. "movl %%edi, %2 \n\t" \
  11218. "movl %%esi, %3 \n\t" \
  11219. : "=m" (t), "=m" (c), "=m" (d), "=m" (s) \
  11220. : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \
  11221. : "eax", "ecx", "edx", "esi", "edi" \
  11222. );
  11223. #endif /* SSE2 */
  11224. #endif /* i386 */
  11225. #if defined(__amd64__) || defined (__x86_64__)
  11226. #define MULADDC_INIT \
  11227. asm( \
  11228. "movq %3, %%rsi \n\t" \
  11229. "movq %4, %%rdi \n\t" \
  11230. "movq %5, %%rcx \n\t" \
  11231. "movq %6, %%rbx \n\t" \
  11232. "xorq %%r8, %%r8 \n\t"
  11233. #define MULADDC_CORE \
  11234. "movq (%%rsi), %%rax \n\t" \
  11235. "mulq %%rbx \n\t" \
  11236. "addq $8, %%rsi \n\t" \
  11237. "addq %%rcx, %%rax \n\t" \
  11238. "movq %%r8, %%rcx \n\t" \
  11239. "adcq $0, %%rdx \n\t" \
  11240. "nop \n\t" \
  11241. "addq %%rax, (%%rdi) \n\t" \
  11242. "adcq %%rdx, %%rcx \n\t" \
  11243. "addq $8, %%rdi \n\t"
  11244. #define MULADDC_STOP \
  11245. "movq %%rcx, %0 \n\t" \
  11246. "movq %%rdi, %1 \n\t" \
  11247. "movq %%rsi, %2 \n\t" \
  11248. : "=m" (c), "=m" (d), "=m" (s) \
  11249. : "m" (s), "m" (d), "m" (c), "m" (b) \
  11250. : "rax", "rcx", "rdx", "rbx", "rsi", "rdi", "r8" \
  11251. );
  11252. #endif /* AMD64 */
  11253. #if defined(__mc68020__) || defined(__mcpu32__)
  11254. #define MULADDC_INIT \
  11255. asm( \
  11256. "movl %3, %%a2 \n\t" \
  11257. "movl %4, %%a3 \n\t" \
  11258. "movl %5, %%d3 \n\t" \
  11259. "movl %6, %%d2 \n\t" \
  11260. "moveq #0, %%d0 \n\t"
  11261. #define MULADDC_CORE \
  11262. "movel %%a2@+, %%d1 \n\t" \
  11263. "mulul %%d2, %%d4:%%d1 \n\t" \
  11264. "addl %%d3, %%d1 \n\t" \
  11265. "addxl %%d0, %%d4 \n\t" \
  11266. "moveq #0, %%d3 \n\t" \
  11267. "addl %%d1, %%a3@+ \n\t" \
  11268. "addxl %%d4, %%d3 \n\t"
  11269. #define MULADDC_STOP \
  11270. "movl %%d3, %0 \n\t" \
  11271. "movl %%a3, %1 \n\t" \
  11272. "movl %%a2, %2 \n\t" \
  11273. : "=m" (c), "=m" (d), "=m" (s) \
  11274. : "m" (s), "m" (d), "m" (c), "m" (b) \
  11275. : "d0", "d1", "d2", "d3", "d4", "a2", "a3" \
  11276. );
  11277. #define MULADDC_HUIT \
  11278. "movel %%a2@+, %%d1 \n\t" \
  11279. "mulul %%d2, %%d4:%%d1 \n\t" \
  11280. "addxl %%d3, %%d1 \n\t" \
  11281. "addxl %%d0, %%d4 \n\t" \
  11282. "addl %%d1, %%a3@+ \n\t" \
  11283. "movel %%a2@+, %%d1 \n\t" \
  11284. "mulul %%d2, %%d3:%%d1 \n\t" \
  11285. "addxl %%d4, %%d1 \n\t" \
  11286. "addxl %%d0, %%d3 \n\t" \
  11287. "addl %%d1, %%a3@+ \n\t" \
  11288. "movel %%a2@+, %%d1 \n\t" \
  11289. "mulul %%d2, %%d4:%%d1 \n\t" \
  11290. "addxl %%d3, %%d1 \n\t" \
  11291. "addxl %%d0, %%d4 \n\t" \
  11292. "addl %%d1, %%a3@+ \n\t" \
  11293. "movel %%a2@+, %%d1 \n\t" \
  11294. "mulul %%d2, %%d3:%%d1 \n\t" \
  11295. "addxl %%d4, %%d1 \n\t" \
  11296. "addxl %%d0, %%d3 \n\t" \
  11297. "addl %%d1, %%a3@+ \n\t" \
  11298. "movel %%a2@+, %%d1 \n\t" \
  11299. "mulul %%d2, %%d4:%%d1 \n\t" \
  11300. "addxl %%d3, %%d1 \n\t" \
  11301. "addxl %%d0, %%d4 \n\t" \
  11302. "addl %%d1, %%a3@+ \n\t" \
  11303. "movel %%a2@+, %%d1 \n\t" \
  11304. "mulul %%d2, %%d3:%%d1 \n\t" \
  11305. "addxl %%d4, %%d1 \n\t" \
  11306. "addxl %%d0, %%d3 \n\t" \
  11307. "addl %%d1, %%a3@+ \n\t" \
  11308. "movel %%a2@+, %%d1 \n\t" \
  11309. "mulul %%d2, %%d4:%%d1 \n\t" \
  11310. "addxl %%d3, %%d1 \n\t" \
  11311. "addxl %%d0, %%d4 \n\t" \
  11312. "addl %%d1, %%a3@+ \n\t" \
  11313. "movel %%a2@+, %%d1 \n\t" \
  11314. "mulul %%d2, %%d3:%%d1 \n\t" \
  11315. "addxl %%d4, %%d1 \n\t" \
  11316. "addxl %%d0, %%d3 \n\t" \
  11317. "addl %%d1, %%a3@+ \n\t" \
  11318. "addxl %%d0, %%d3 \n\t"
  11319. #endif /* MC68000 */
  11320. #if defined(__powerpc64__) || defined(__ppc64__)
  11321. #if defined(__MACH__) && defined(__APPLE__)
  11322. #define MULADDC_INIT \
  11323. asm( \
  11324. "ld r3, %3 \n\t" \
  11325. "ld r4, %4 \n\t" \
  11326. "ld r5, %5 \n\t" \
  11327. "ld r6, %6 \n\t" \
  11328. "addi r3, r3, -8 \n\t" \
  11329. "addi r4, r4, -8 \n\t" \
  11330. "addic r5, r5, 0 \n\t"
  11331. #define MULADDC_CORE \
  11332. "ldu r7, 8(r3) \n\t" \
  11333. "mulld r8, r7, r6 \n\t" \
  11334. "mulhdu r9, r7, r6 \n\t" \
  11335. "adde r8, r8, r5 \n\t" \
  11336. "ld r7, 8(r4) \n\t" \
  11337. "addze r5, r9 \n\t" \
  11338. "addc r8, r8, r7 \n\t" \
  11339. "stdu r8, 8(r4) \n\t"
  11340. #define MULADDC_STOP \
  11341. "addze r5, r5 \n\t" \
  11342. "addi r4, r4, 8 \n\t" \
  11343. "addi r3, r3, 8 \n\t" \
  11344. "std r5, %0 \n\t" \
  11345. "std r4, %1 \n\t" \
  11346. "std r3, %2 \n\t" \
  11347. : "=m" (c), "=m" (d), "=m" (s) \
  11348. : "m" (s), "m" (d), "m" (c), "m" (b) \
  11349. : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
  11350. );
  11351. #else /* __MACH__ && __APPLE__ */
  11352. #define MULADDC_INIT \
  11353. asm( \
  11354. "ld %%r3, %3 \n\t" \
  11355. "ld %%r4, %4 \n\t" \
  11356. "ld %%r5, %5 \n\t" \
  11357. "ld %%r6, %6 \n\t" \
  11358. "addi %%r3, %%r3, -8 \n\t" \
  11359. "addi %%r4, %%r4, -8 \n\t" \
  11360. "addic %%r5, %%r5, 0 \n\t"
  11361. #define MULADDC_CORE \
  11362. "ldu %%r7, 8(%%r3) \n\t" \
  11363. "mulld %%r8, %%r7, %%r6 \n\t" \
  11364. "mulhdu %%r9, %%r7, %%r6 \n\t" \
  11365. "adde %%r8, %%r8, %%r5 \n\t" \
  11366. "ld %%r7, 8(%%r4) \n\t" \
  11367. "addze %%r5, %%r9 \n\t" \
  11368. "addc %%r8, %%r8, %%r7 \n\t" \
  11369. "stdu %%r8, 8(%%r4) \n\t"
  11370. #define MULADDC_STOP \
  11371. "addze %%r5, %%r5 \n\t" \
  11372. "addi %%r4, %%r4, 8 \n\t" \
  11373. "addi %%r3, %%r3, 8 \n\t" \
  11374. "std %%r5, %0 \n\t" \
  11375. "std %%r4, %1 \n\t" \
  11376. "std %%r3, %2 \n\t" \
  11377. : "=m" (c), "=m" (d), "=m" (s) \
  11378. : "m" (s), "m" (d), "m" (c), "m" (b) \
  11379. : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
  11380. );
  11381. #endif /* __MACH__ && __APPLE__ */
  11382. #elif defined(__powerpc__) || defined(__ppc__) /* end PPC64/begin PPC32 */
  11383. #if defined(__MACH__) && defined(__APPLE__)
  11384. #define MULADDC_INIT \
  11385. asm( \
  11386. "lwz r3, %3 \n\t" \
  11387. "lwz r4, %4 \n\t" \
  11388. "lwz r5, %5 \n\t" \
  11389. "lwz r6, %6 \n\t" \
  11390. "addi r3, r3, -4 \n\t" \
  11391. "addi r4, r4, -4 \n\t" \
  11392. "addic r5, r5, 0 \n\t"
  11393. #define MULADDC_CORE \
  11394. "lwzu r7, 4(r3) \n\t" \
  11395. "mullw r8, r7, r6 \n\t" \
  11396. "mulhwu r9, r7, r6 \n\t" \
  11397. "adde r8, r8, r5 \n\t" \
  11398. "lwz r7, 4(r4) \n\t" \
  11399. "addze r5, r9 \n\t" \
  11400. "addc r8, r8, r7 \n\t" \
  11401. "stwu r8, 4(r4) \n\t"
  11402. #define MULADDC_STOP \
  11403. "addze r5, r5 \n\t" \
  11404. "addi r4, r4, 4 \n\t" \
  11405. "addi r3, r3, 4 \n\t" \
  11406. "stw r5, %0 \n\t" \
  11407. "stw r4, %1 \n\t" \
  11408. "stw r3, %2 \n\t" \
  11409. : "=m" (c), "=m" (d), "=m" (s) \
  11410. : "m" (s), "m" (d), "m" (c), "m" (b) \
  11411. : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
  11412. );
  11413. #else /* __MACH__ && __APPLE__ */
  11414. #define MULADDC_INIT \
  11415. asm( \
  11416. "lwz %%r3, %3 \n\t" \
  11417. "lwz %%r4, %4 \n\t" \
  11418. "lwz %%r5, %5 \n\t" \
  11419. "lwz %%r6, %6 \n\t" \
  11420. "addi %%r3, %%r3, -4 \n\t" \
  11421. "addi %%r4, %%r4, -4 \n\t" \
  11422. "addic %%r5, %%r5, 0 \n\t"
  11423. #define MULADDC_CORE \
  11424. "lwzu %%r7, 4(%%r3) \n\t" \
  11425. "mullw %%r8, %%r7, %%r6 \n\t" \
  11426. "mulhwu %%r9, %%r7, %%r6 \n\t" \
  11427. "adde %%r8, %%r8, %%r5 \n\t" \
  11428. "lwz %%r7, 4(%%r4) \n\t" \
  11429. "addze %%r5, %%r9 \n\t" \
  11430. "addc %%r8, %%r8, %%r7 \n\t" \
  11431. "stwu %%r8, 4(%%r4) \n\t"
  11432. #define MULADDC_STOP \
  11433. "addze %%r5, %%r5 \n\t" \
  11434. "addi %%r4, %%r4, 4 \n\t" \
  11435. "addi %%r3, %%r3, 4 \n\t" \
  11436. "stw %%r5, %0 \n\t" \
  11437. "stw %%r4, %1 \n\t" \
  11438. "stw %%r3, %2 \n\t" \
  11439. : "=m" (c), "=m" (d), "=m" (s) \
  11440. : "m" (s), "m" (d), "m" (c), "m" (b) \
  11441. : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
  11442. );
  11443. #endif /* __MACH__ && __APPLE__ */
  11444. #endif /* PPC32 */
  11445. /*
  11446. * The Sparc(64) assembly is reported to be broken.
  11447. * Disable it for now, until we're able to fix it.
  11448. */
  11449. #if 0 && defined(__sparc__)
  11450. #if defined(__sparc64__)
  11451. #define MULADDC_INIT \
  11452. asm( \
  11453. "ldx %3, %%o0 \n\t" \
  11454. "ldx %4, %%o1 \n\t" \
  11455. "ld %5, %%o2 \n\t" \
  11456. "ld %6, %%o3 \n\t"
  11457. #define MULADDC_CORE \
  11458. "ld [%%o0], %%o4 \n\t" \
  11459. "inc 4, %%o0 \n\t" \
  11460. "ld [%%o1], %%o5 \n\t" \
  11461. "umul %%o3, %%o4, %%o4 \n\t" \
  11462. "addcc %%o4, %%o2, %%o4 \n\t" \
  11463. "rd %%y, %%g1 \n\t" \
  11464. "addx %%g1, 0, %%g1 \n\t" \
  11465. "addcc %%o4, %%o5, %%o4 \n\t" \
  11466. "st %%o4, [%%o1] \n\t" \
  11467. "addx %%g1, 0, %%o2 \n\t" \
  11468. "inc 4, %%o1 \n\t"
  11469. #define MULADDC_STOP \
  11470. "st %%o2, %0 \n\t" \
  11471. "stx %%o1, %1 \n\t" \
  11472. "stx %%o0, %2 \n\t" \
  11473. : "=m" (c), "=m" (d), "=m" (s) \
  11474. : "m" (s), "m" (d), "m" (c), "m" (b) \
  11475. : "g1", "o0", "o1", "o2", "o3", "o4", \
  11476. "o5" \
  11477. );
  11478. #else /* __sparc64__ */
  11479. #define MULADDC_INIT \
  11480. asm( \
  11481. "ld %3, %%o0 \n\t" \
  11482. "ld %4, %%o1 \n\t" \
  11483. "ld %5, %%o2 \n\t" \
  11484. "ld %6, %%o3 \n\t"
  11485. #define MULADDC_CORE \
  11486. "ld [%%o0], %%o4 \n\t" \
  11487. "inc 4, %%o0 \n\t" \
  11488. "ld [%%o1], %%o5 \n\t" \
  11489. "umul %%o3, %%o4, %%o4 \n\t" \
  11490. "addcc %%o4, %%o2, %%o4 \n\t" \
  11491. "rd %%y, %%g1 \n\t" \
  11492. "addx %%g1, 0, %%g1 \n\t" \
  11493. "addcc %%o4, %%o5, %%o4 \n\t" \
  11494. "st %%o4, [%%o1] \n\t" \
  11495. "addx %%g1, 0, %%o2 \n\t" \
  11496. "inc 4, %%o1 \n\t"
  11497. #define MULADDC_STOP \
  11498. "st %%o2, %0 \n\t" \
  11499. "st %%o1, %1 \n\t" \
  11500. "st %%o0, %2 \n\t" \
  11501. : "=m" (c), "=m" (d), "=m" (s) \
  11502. : "m" (s), "m" (d), "m" (c), "m" (b) \
  11503. : "g1", "o0", "o1", "o2", "o3", "o4", \
  11504. "o5" \
  11505. );
  11506. #endif /* __sparc64__ */
  11507. #endif /* __sparc__ */
  11508. #if defined(__microblaze__) || defined(microblaze)
  11509. #define MULADDC_INIT \
  11510. asm( \
  11511. "lwi r3, %3 \n\t" \
  11512. "lwi r4, %4 \n\t" \
  11513. "lwi r5, %5 \n\t" \
  11514. "lwi r6, %6 \n\t" \
  11515. "andi r7, r6, 0xffff \n\t" \
  11516. "bsrli r6, r6, 16 \n\t"
  11517. #define MULADDC_CORE \
  11518. "lhui r8, r3, 0 \n\t" \
  11519. "addi r3, r3, 2 \n\t" \
  11520. "lhui r9, r3, 0 \n\t" \
  11521. "addi r3, r3, 2 \n\t" \
  11522. "mul r10, r9, r6 \n\t" \
  11523. "mul r11, r8, r7 \n\t" \
  11524. "mul r12, r9, r7 \n\t" \
  11525. "mul r13, r8, r6 \n\t" \
  11526. "bsrli r8, r10, 16 \n\t" \
  11527. "bsrli r9, r11, 16 \n\t" \
  11528. "add r13, r13, r8 \n\t" \
  11529. "add r13, r13, r9 \n\t" \
  11530. "bslli r10, r10, 16 \n\t" \
  11531. "bslli r11, r11, 16 \n\t" \
  11532. "add r12, r12, r10 \n\t" \
  11533. "addc r13, r13, r0 \n\t" \
  11534. "add r12, r12, r11 \n\t" \
  11535. "addc r13, r13, r0 \n\t" \
  11536. "lwi r10, r4, 0 \n\t" \
  11537. "add r12, r12, r10 \n\t" \
  11538. "addc r13, r13, r0 \n\t" \
  11539. "add r12, r12, r5 \n\t" \
  11540. "addc r5, r13, r0 \n\t" \
  11541. "swi r12, r4, 0 \n\t" \
  11542. "addi r4, r4, 4 \n\t"
  11543. #define MULADDC_STOP \
  11544. "swi r5, %0 \n\t" \
  11545. "swi r4, %1 \n\t" \
  11546. "swi r3, %2 \n\t" \
  11547. : "=m" (c), "=m" (d), "=m" (s) \
  11548. : "m" (s), "m" (d), "m" (c), "m" (b) \
  11549. : "r3", "r4" "r5", "r6", "r7", "r8", \
  11550. "r9", "r10", "r11", "r12", "r13" \
  11551. );
  11552. #endif /* MicroBlaze */
  11553. #if defined(__tricore__)
  11554. #define MULADDC_INIT \
  11555. asm( \
  11556. "ld.a %%a2, %3 \n\t" \
  11557. "ld.a %%a3, %4 \n\t" \
  11558. "ld.w %%d4, %5 \n\t" \
  11559. "ld.w %%d1, %6 \n\t" \
  11560. "xor %%d5, %%d5 \n\t"
  11561. #define MULADDC_CORE \
  11562. "ld.w %%d0, [%%a2+] \n\t" \
  11563. "madd.u %%e2, %%e4, %%d0, %%d1 \n\t" \
  11564. "ld.w %%d0, [%%a3] \n\t" \
  11565. "addx %%d2, %%d2, %%d0 \n\t" \
  11566. "addc %%d3, %%d3, 0 \n\t" \
  11567. "mov %%d4, %%d3 \n\t" \
  11568. "st.w [%%a3+], %%d2 \n\t"
  11569. #define MULADDC_STOP \
  11570. "st.w %0, %%d4 \n\t" \
  11571. "st.a %1, %%a3 \n\t" \
  11572. "st.a %2, %%a2 \n\t" \
  11573. : "=m" (c), "=m" (d), "=m" (s) \
  11574. : "m" (s), "m" (d), "m" (c), "m" (b) \
  11575. : "d0", "d1", "e2", "d4", "a2", "a3" \
  11576. );
  11577. #endif /* TriCore */
  11578. #if defined(__arm__)
  11579. #if defined(__thumb__) && !defined(__thumb2__)
  11580. #define MULADDC_INIT \
  11581. asm( \
  11582. "ldr r0, %3 \n\t" \
  11583. "ldr r1, %4 \n\t" \
  11584. "ldr r2, %5 \n\t" \
  11585. "ldr r3, %6 \n\t" \
  11586. "lsr r7, r3, #16 \n\t" \
  11587. "mov r9, r7 \n\t" \
  11588. "lsl r7, r3, #16 \n\t" \
  11589. "lsr r7, r7, #16 \n\t" \
  11590. "mov r8, r7 \n\t"
  11591. #define MULADDC_CORE \
  11592. "ldmia r0!, {r6} \n\t" \
  11593. "lsr r7, r6, #16 \n\t" \
  11594. "lsl r6, r6, #16 \n\t" \
  11595. "lsr r6, r6, #16 \n\t" \
  11596. "mov r4, r8 \n\t" \
  11597. "mul r4, r6 \n\t" \
  11598. "mov r3, r9 \n\t" \
  11599. "mul r6, r3 \n\t" \
  11600. "mov r5, r9 \n\t" \
  11601. "mul r5, r7 \n\t" \
  11602. "mov r3, r8 \n\t" \
  11603. "mul r7, r3 \n\t" \
  11604. "lsr r3, r6, #16 \n\t" \
  11605. "add r5, r5, r3 \n\t" \
  11606. "lsr r3, r7, #16 \n\t" \
  11607. "add r5, r5, r3 \n\t" \
  11608. "add r4, r4, r2 \n\t" \
  11609. "mov r2, #0 \n\t" \
  11610. "adc r5, r2 \n\t" \
  11611. "lsl r3, r6, #16 \n\t" \
  11612. "add r4, r4, r3 \n\t" \
  11613. "adc r5, r2 \n\t" \
  11614. "lsl r3, r7, #16 \n\t" \
  11615. "add r4, r4, r3 \n\t" \
  11616. "adc r5, r2 \n\t" \
  11617. "ldr r3, [r1] \n\t" \
  11618. "add r4, r4, r3 \n\t" \
  11619. "adc r2, r5 \n\t" \
  11620. "stmia r1!, {r4} \n\t"
  11621. #define MULADDC_STOP \
  11622. "str r2, %0 \n\t" \
  11623. "str r1, %1 \n\t" \
  11624. "str r0, %2 \n\t" \
  11625. : "=m" (c), "=m" (d), "=m" (s) \
  11626. : "m" (s), "m" (d), "m" (c), "m" (b) \
  11627. : "r0", "r1", "r2", "r3", "r4", "r5", \
  11628. "r6", "r7", "r8", "r9", "cc" \
  11629. );
  11630. #else
  11631. #define MULADDC_INIT \
  11632. asm( \
  11633. "ldr r0, %3 \n\t" \
  11634. "ldr r1, %4 \n\t" \
  11635. "ldr r2, %5 \n\t" \
  11636. "ldr r3, %6 \n\t"
  11637. #define MULADDC_CORE \
  11638. "ldr r4, [r0], #4 \n\t" \
  11639. "mov r5, #0 \n\t" \
  11640. "ldr r6, [r1] \n\t" \
  11641. "umlal r2, r5, r3, r4 \n\t" \
  11642. "adds r7, r6, r2 \n\t" \
  11643. "adc r2, r5, #0 \n\t" \
  11644. "str r7, [r1], #4 \n\t"
  11645. #define MULADDC_STOP \
  11646. "str r2, %0 \n\t" \
  11647. "str r1, %1 \n\t" \
  11648. "str r0, %2 \n\t" \
  11649. : "=m" (c), "=m" (d), "=m" (s) \
  11650. : "m" (s), "m" (d), "m" (c), "m" (b) \
  11651. : "r0", "r1", "r2", "r3", "r4", "r5", \
  11652. "r6", "r7", "cc" \
  11653. );
  11654. #endif /* Thumb */
  11655. #endif /* ARMv3 */
  11656. #if defined(__alpha__)
  11657. #define MULADDC_INIT \
  11658. asm( \
  11659. "ldq $1, %3 \n\t" \
  11660. "ldq $2, %4 \n\t" \
  11661. "ldq $3, %5 \n\t" \
  11662. "ldq $4, %6 \n\t"
  11663. #define MULADDC_CORE \
  11664. "ldq $6, 0($1) \n\t" \
  11665. "addq $1, 8, $1 \n\t" \
  11666. "mulq $6, $4, $7 \n\t" \
  11667. "umulh $6, $4, $6 \n\t" \
  11668. "addq $7, $3, $7 \n\t" \
  11669. "cmpult $7, $3, $3 \n\t" \
  11670. "ldq $5, 0($2) \n\t" \
  11671. "addq $7, $5, $7 \n\t" \
  11672. "cmpult $7, $5, $5 \n\t" \
  11673. "stq $7, 0($2) \n\t" \
  11674. "addq $2, 8, $2 \n\t" \
  11675. "addq $6, $3, $3 \n\t" \
  11676. "addq $5, $3, $3 \n\t"
  11677. #define MULADDC_STOP \
  11678. "stq $3, %0 \n\t" \
  11679. "stq $2, %1 \n\t" \
  11680. "stq $1, %2 \n\t" \
  11681. : "=m" (c), "=m" (d), "=m" (s) \
  11682. : "m" (s), "m" (d), "m" (c), "m" (b) \
  11683. : "$1", "$2", "$3", "$4", "$5", "$6", "$7" \
  11684. );
  11685. #endif /* Alpha */
  11686. #if defined(__mips__) && !defined(__mips64)
  11687. #define MULADDC_INIT \
  11688. asm( \
  11689. "lw $10, %3 \n\t" \
  11690. "lw $11, %4 \n\t" \
  11691. "lw $12, %5 \n\t" \
  11692. "lw $13, %6 \n\t"
  11693. #define MULADDC_CORE \
  11694. "lw $14, 0($10) \n\t" \
  11695. "multu $13, $14 \n\t" \
  11696. "addi $10, $10, 4 \n\t" \
  11697. "mflo $14 \n\t" \
  11698. "mfhi $9 \n\t" \
  11699. "addu $14, $12, $14 \n\t" \
  11700. "lw $15, 0($11) \n\t" \
  11701. "sltu $12, $14, $12 \n\t" \
  11702. "addu $15, $14, $15 \n\t" \
  11703. "sltu $14, $15, $14 \n\t" \
  11704. "addu $12, $12, $9 \n\t" \
  11705. "sw $15, 0($11) \n\t" \
  11706. "addu $12, $12, $14 \n\t" \
  11707. "addi $11, $11, 4 \n\t"
  11708. #define MULADDC_STOP \
  11709. "sw $12, %0 \n\t" \
  11710. "sw $11, %1 \n\t" \
  11711. "sw $10, %2 \n\t" \
  11712. : "=m" (c), "=m" (d), "=m" (s) \
  11713. : "m" (s), "m" (d), "m" (c), "m" (b) \
  11714. : "$9", "$10", "$11", "$12", "$13", "$14", "$15" \
  11715. );
  11716. #endif /* MIPS */
  11717. #endif /* GNUC */
  11718. #if (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
  11719. #define MULADDC_INIT \
  11720. __asm mov esi, s \
  11721. __asm mov edi, d \
  11722. __asm mov ecx, c \
  11723. __asm mov ebx, b
  11724. #define MULADDC_CORE \
  11725. __asm lodsd \
  11726. __asm mul ebx \
  11727. __asm add eax, ecx \
  11728. __asm adc edx, 0 \
  11729. __asm add eax, [edi] \
  11730. __asm adc edx, 0 \
  11731. __asm mov ecx, edx \
  11732. __asm stosd
  11733. #if defined(MBEDTLS_HAVE_SSE2)
  11734. #define EMIT __asm _emit
  11735. #define MULADDC_HUIT \
  11736. EMIT 0x0F EMIT 0x6E EMIT 0xC9 \
  11737. EMIT 0x0F EMIT 0x6E EMIT 0xC3 \
  11738. EMIT 0x0F EMIT 0x6E EMIT 0x1F \
  11739. EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
  11740. EMIT 0x0F EMIT 0x6E EMIT 0x16 \
  11741. EMIT 0x0F EMIT 0xF4 EMIT 0xD0 \
  11742. EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x04 \
  11743. EMIT 0x0F EMIT 0xF4 EMIT 0xE0 \
  11744. EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x08 \
  11745. EMIT 0x0F EMIT 0xF4 EMIT 0xF0 \
  11746. EMIT 0x0F EMIT 0x6E EMIT 0x7E EMIT 0x0C \
  11747. EMIT 0x0F EMIT 0xF4 EMIT 0xF8 \
  11748. EMIT 0x0F EMIT 0xD4 EMIT 0xCA \
  11749. EMIT 0x0F EMIT 0x6E EMIT 0x5F EMIT 0x04 \
  11750. EMIT 0x0F EMIT 0xD4 EMIT 0xDC \
  11751. EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x08 \
  11752. EMIT 0x0F EMIT 0xD4 EMIT 0xEE \
  11753. EMIT 0x0F EMIT 0x6E EMIT 0x67 EMIT 0x0C \
  11754. EMIT 0x0F EMIT 0xD4 EMIT 0xFC \
  11755. EMIT 0x0F EMIT 0x7E EMIT 0x0F \
  11756. EMIT 0x0F EMIT 0x6E EMIT 0x56 EMIT 0x10 \
  11757. EMIT 0x0F EMIT 0xF4 EMIT 0xD0 \
  11758. EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
  11759. EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x14 \
  11760. EMIT 0x0F EMIT 0xF4 EMIT 0xE0 \
  11761. EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
  11762. EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x18 \
  11763. EMIT 0x0F EMIT 0xF4 EMIT 0xF0 \
  11764. EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x04 \
  11765. EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
  11766. EMIT 0x0F EMIT 0x6E EMIT 0x5E EMIT 0x1C \
  11767. EMIT 0x0F EMIT 0xF4 EMIT 0xD8 \
  11768. EMIT 0x0F EMIT 0xD4 EMIT 0xCD \
  11769. EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x10 \
  11770. EMIT 0x0F EMIT 0xD4 EMIT 0xD5 \
  11771. EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x08 \
  11772. EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
  11773. EMIT 0x0F EMIT 0xD4 EMIT 0xCF \
  11774. EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x14 \
  11775. EMIT 0x0F EMIT 0xD4 EMIT 0xE5 \
  11776. EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x0C \
  11777. EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
  11778. EMIT 0x0F EMIT 0xD4 EMIT 0xCA \
  11779. EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x18 \
  11780. EMIT 0x0F EMIT 0xD4 EMIT 0xF5 \
  11781. EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x10 \
  11782. EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
  11783. EMIT 0x0F EMIT 0xD4 EMIT 0xCC \
  11784. EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x1C \
  11785. EMIT 0x0F EMIT 0xD4 EMIT 0xDD \
  11786. EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x14 \
  11787. EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
  11788. EMIT 0x0F EMIT 0xD4 EMIT 0xCE \
  11789. EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x18 \
  11790. EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
  11791. EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
  11792. EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x1C \
  11793. EMIT 0x83 EMIT 0xC7 EMIT 0x20 \
  11794. EMIT 0x83 EMIT 0xC6 EMIT 0x20 \
  11795. EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
  11796. EMIT 0x0F EMIT 0x7E EMIT 0xC9
  11797. #define MULADDC_STOP \
  11798. EMIT 0x0F EMIT 0x77 \
  11799. __asm mov c, ecx \
  11800. __asm mov d, edi \
  11801. __asm mov s, esi \
  11802. #else
  11803. #define MULADDC_STOP \
  11804. __asm mov c, ecx \
  11805. __asm mov d, edi \
  11806. __asm mov s, esi \
  11807. #endif /* SSE2 */
  11808. #endif /* MSVC */
  11809. #endif /* MBEDTLS_HAVE_ASM */
  11810. #if !defined(MULADDC_CORE)
  11811. #if defined(MBEDTLS_HAVE_UDBL)
  11812. #define MULADDC_INIT \
  11813. { \
  11814. mbedtls_t_udbl r; \
  11815. mbedtls_mpi_uint r0, r1;
  11816. #define MULADDC_CORE \
  11817. r = *(s++) * (mbedtls_t_udbl) b; \
  11818. r0 = (mbedtls_mpi_uint) r; \
  11819. r1 = (mbedtls_mpi_uint)( r >> biL ); \
  11820. r0 += c; r1 += (r0 < c); \
  11821. r0 += *d; r1 += (r0 < *d); \
  11822. c = r1; *(d++) = r0;
  11823. #define MULADDC_STOP \
  11824. }
  11825. #else
  11826. #define MULADDC_INIT \
  11827. { \
  11828. mbedtls_mpi_uint s0, s1, b0, b1; \
  11829. mbedtls_mpi_uint r0, r1, rx, ry; \
  11830. b0 = ( b << biH ) >> biH; \
  11831. b1 = ( b >> biH );
  11832. #define MULADDC_CORE \
  11833. s0 = ( *s << biH ) >> biH; \
  11834. s1 = ( *s >> biH ); s++; \
  11835. rx = s0 * b1; r0 = s0 * b0; \
  11836. ry = s1 * b0; r1 = s1 * b1; \
  11837. r1 += ( rx >> biH ); \
  11838. r1 += ( ry >> biH ); \
  11839. rx <<= biH; ry <<= biH; \
  11840. r0 += rx; r1 += (r0 < rx); \
  11841. r0 += ry; r1 += (r0 < ry); \
  11842. r0 += c; r1 += (r0 < c); \
  11843. r0 += *d; r1 += (r0 < *d); \
  11844. c = r1; *(d++) = r0;
  11845. #define MULADDC_STOP \
  11846. }
  11847. #endif /* C (generic) */
  11848. #endif /* C (longlong) */
  11849. #endif /* bn_mul.h */
  11850. /********* Start of file include/mbedtls/camellia.h ************/
  11851. /**
  11852. * \file camellia.h
  11853. *
  11854. * \brief Camellia block cipher
  11855. *
  11856. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  11857. * SPDX-License-Identifier: Apache-2.0
  11858. *
  11859. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11860. * not use this file except in compliance with the License.
  11861. * You may obtain a copy of the License at
  11862. *
  11863. * http://www.apache.org/licenses/LICENSE-2.0
  11864. *
  11865. * Unless required by applicable law or agreed to in writing, software
  11866. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11867. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11868. * See the License for the specific language governing permissions and
  11869. * limitations under the License.
  11870. *
  11871. * This file is part of mbed TLS (https://tls.mbed.org)
  11872. */
  11873. #ifndef MBEDTLS_CAMELLIA_H
  11874. #define MBEDTLS_CAMELLIA_H
  11875. #if !defined(MBEDTLS_CONFIG_FILE)
  11876. #else
  11877. #endif
  11878. #include <stddef.h>
  11879. #if !VXWORKS
  11880. #include <stdint.h>
  11881. #endif
  11882. #define MBEDTLS_CAMELLIA_ENCRYPT 1
  11883. #define MBEDTLS_CAMELLIA_DECRYPT 0
  11884. #define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */
  11885. #define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
  11886. #if !defined(MBEDTLS_CAMELLIA_ALT)
  11887. // Regular implementation
  11888. //
  11889. #ifdef __cplusplus
  11890. extern "C" {
  11891. #endif
  11892. /**
  11893. * \brief CAMELLIA context structure
  11894. */
  11895. typedef struct
  11896. {
  11897. int nr; /*!< number of rounds */
  11898. uint32_t rk[68]; /*!< CAMELLIA round keys */
  11899. }
  11900. mbedtls_camellia_context;
  11901. /**
  11902. * \brief Initialize CAMELLIA context
  11903. *
  11904. * \param ctx CAMELLIA context to be initialized
  11905. */
  11906. void mbedtls_camellia_init( mbedtls_camellia_context *ctx );
  11907. /**
  11908. * \brief Clear CAMELLIA context
  11909. *
  11910. * \param ctx CAMELLIA context to be cleared
  11911. */
  11912. void mbedtls_camellia_free( mbedtls_camellia_context *ctx );
  11913. /**
  11914. * \brief CAMELLIA key schedule (encryption)
  11915. *
  11916. * \param ctx CAMELLIA context to be initialized
  11917. * \param key encryption key
  11918. * \param keybits must be 128, 192 or 256
  11919. *
  11920. * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
  11921. */
  11922. int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned char *key,
  11923. unsigned int keybits );
  11924. /**
  11925. * \brief CAMELLIA key schedule (decryption)
  11926. *
  11927. * \param ctx CAMELLIA context to be initialized
  11928. * \param key decryption key
  11929. * \param keybits must be 128, 192 or 256
  11930. *
  11931. * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
  11932. */
  11933. int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, const unsigned char *key,
  11934. unsigned int keybits );
  11935. /**
  11936. * \brief CAMELLIA-ECB block encryption/decryption
  11937. *
  11938. * \param ctx CAMELLIA context
  11939. * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
  11940. * \param input 16-byte input block
  11941. * \param output 16-byte output block
  11942. *
  11943. * \return 0 if successful
  11944. */
  11945. int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
  11946. int mode,
  11947. const unsigned char input[16],
  11948. unsigned char output[16] );
  11949. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  11950. /**
  11951. * \brief CAMELLIA-CBC buffer encryption/decryption
  11952. * Length should be a multiple of the block
  11953. * size (16 bytes)
  11954. *
  11955. * \note Upon exit, the content of the IV is updated so that you can
  11956. * call the function same function again on the following
  11957. * block(s) of data and get the same result as if it was
  11958. * encrypted in one call. This allows a "streaming" usage.
  11959. * If on the other hand you need to retain the contents of the
  11960. * IV, you should either save it manually or use the cipher
  11961. * module instead.
  11962. *
  11963. * \param ctx CAMELLIA context
  11964. * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
  11965. * \param length length of the input data
  11966. * \param iv initialization vector (updated after use)
  11967. * \param input buffer holding the input data
  11968. * \param output buffer holding the output data
  11969. *
  11970. * \return 0 if successful, or
  11971. * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
  11972. */
  11973. int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
  11974. int mode,
  11975. size_t length,
  11976. unsigned char iv[16],
  11977. const unsigned char *input,
  11978. unsigned char *output );
  11979. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  11980. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  11981. /**
  11982. * \brief CAMELLIA-CFB128 buffer encryption/decryption
  11983. *
  11984. * Note: Due to the nature of CFB you should use the same key schedule for
  11985. * both encryption and decryption. So a context initialized with
  11986. * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT.
  11987. *
  11988. * \note Upon exit, the content of the IV is updated so that you can
  11989. * call the function same function again on the following
  11990. * block(s) of data and get the same result as if it was
  11991. * encrypted in one call. This allows a "streaming" usage.
  11992. * If on the other hand you need to retain the contents of the
  11993. * IV, you should either save it manually or use the cipher
  11994. * module instead.
  11995. *
  11996. * \param ctx CAMELLIA context
  11997. * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
  11998. * \param length length of the input data
  11999. * \param iv_off offset in IV (updated after use)
  12000. * \param iv initialization vector (updated after use)
  12001. * \param input buffer holding the input data
  12002. * \param output buffer holding the output data
  12003. *
  12004. * \return 0 if successful, or
  12005. * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
  12006. */
  12007. int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
  12008. int mode,
  12009. size_t length,
  12010. size_t *iv_off,
  12011. unsigned char iv[16],
  12012. const unsigned char *input,
  12013. unsigned char *output );
  12014. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  12015. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  12016. /**
  12017. * \brief CAMELLIA-CTR buffer encryption/decryption
  12018. *
  12019. * Warning: You have to keep the maximum use of your counter in mind!
  12020. *
  12021. * Note: Due to the nature of CTR you should use the same key schedule for
  12022. * both encryption and decryption. So a context initialized with
  12023. * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and MBEDTLS_CAMELLIA_DECRYPT.
  12024. *
  12025. * \param ctx CAMELLIA context
  12026. * \param length The length of the data
  12027. * \param nc_off The offset in the current stream_block (for resuming
  12028. * within current cipher stream). The offset pointer to
  12029. * should be 0 at the start of a stream.
  12030. * \param nonce_counter The 128-bit nonce and counter.
  12031. * \param stream_block The saved stream-block for resuming. Is overwritten
  12032. * by the function.
  12033. * \param input The input data stream
  12034. * \param output The output data stream
  12035. *
  12036. * \return 0 if successful
  12037. */
  12038. int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
  12039. size_t length,
  12040. size_t *nc_off,
  12041. unsigned char nonce_counter[16],
  12042. unsigned char stream_block[16],
  12043. const unsigned char *input,
  12044. unsigned char *output );
  12045. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  12046. #ifdef __cplusplus
  12047. }
  12048. #endif
  12049. #else /* MBEDTLS_CAMELLIA_ALT */
  12050. #endif /* MBEDTLS_CAMELLIA_ALT */
  12051. #ifdef __cplusplus
  12052. extern "C" {
  12053. #endif
  12054. /**
  12055. * \brief Checkup routine
  12056. *
  12057. * \return 0 if successful, or 1 if the test failed
  12058. */
  12059. int mbedtls_camellia_self_test( int verbose );
  12060. #ifdef __cplusplus
  12061. }
  12062. #endif
  12063. #endif /* camellia.h */
  12064. /********* Start of file include/mbedtls/ctr_drbg.h ************/
  12065. /**
  12066. * \file ctr_drbg.h
  12067. *
  12068. * \brief CTR_DRBG based on AES-256 (NIST SP 800-90)
  12069. *
  12070. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  12071. * SPDX-License-Identifier: Apache-2.0
  12072. *
  12073. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  12074. * not use this file except in compliance with the License.
  12075. * You may obtain a copy of the License at
  12076. *
  12077. * http://www.apache.org/licenses/LICENSE-2.0
  12078. *
  12079. * Unless required by applicable law or agreed to in writing, software
  12080. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12081. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12082. * See the License for the specific language governing permissions and
  12083. * limitations under the License.
  12084. *
  12085. * This file is part of mbed TLS (https://tls.mbed.org)
  12086. */
  12087. #ifndef MBEDTLS_CTR_DRBG_H
  12088. #define MBEDTLS_CTR_DRBG_H
  12089. #if defined(MBEDTLS_THREADING_C)
  12090. #endif
  12091. #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
  12092. #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< Too many random requested in single call. */
  12093. #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< Input too large (Entropy + additional). */
  12094. #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read/write error in file. */
  12095. #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16 /**< Block size used by the cipher */
  12096. #define MBEDTLS_CTR_DRBG_KEYSIZE 32 /**< Key size used by the cipher */
  12097. #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
  12098. #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
  12099. /**< The seed length (counter + AES key) */
  12100. /**
  12101. * \name SECTION: Module settings
  12102. *
  12103. * The configuration options you can set for this module are in this section.
  12104. * Either change them in config.h or define them on the compiler command line.
  12105. * \{
  12106. */
  12107. #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
  12108. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
  12109. #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
  12110. #else
  12111. #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
  12112. #endif
  12113. #endif
  12114. #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
  12115. #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
  12116. #endif
  12117. #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
  12118. #define MBEDTLS_CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
  12119. #endif
  12120. #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
  12121. #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
  12122. #endif
  12123. #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
  12124. #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
  12125. #endif
  12126. /* \} name SECTION: Module settings */
  12127. #define MBEDTLS_CTR_DRBG_PR_OFF 0 /**< No prediction resistance */
  12128. #define MBEDTLS_CTR_DRBG_PR_ON 1 /**< Prediction resistance enabled */
  12129. #ifdef __cplusplus
  12130. extern "C" {
  12131. #endif
  12132. /**
  12133. * \brief CTR_DRBG context structure
  12134. */
  12135. typedef struct
  12136. {
  12137. unsigned char counter[16]; /*!< counter (V) */
  12138. int reseed_counter; /*!< reseed counter */
  12139. int prediction_resistance; /*!< enable prediction resistance (Automatic
  12140. reseed before every random generation) */
  12141. size_t entropy_len; /*!< amount of entropy grabbed on each
  12142. (re)seed */
  12143. int reseed_interval; /*!< reseed interval */
  12144. mbedtls_aes_context aes_ctx; /*!< AES context */
  12145. /*
  12146. * Callbacks (Entropy)
  12147. */
  12148. int (*f_entropy)(void *, unsigned char *, size_t);
  12149. void *p_entropy; /*!< context for the entropy function */
  12150. #if defined(MBEDTLS_THREADING_C)
  12151. mbedtls_threading_mutex_t mutex;
  12152. #endif
  12153. }
  12154. mbedtls_ctr_drbg_context;
  12155. /**
  12156. * \brief CTR_DRBG context initialization
  12157. * Makes the context ready for mbedtls_ctr_drbg_seed() or
  12158. * mbedtls_ctr_drbg_free().
  12159. *
  12160. * \param ctx CTR_DRBG context to be initialized
  12161. */
  12162. void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
  12163. /**
  12164. * \brief CTR_DRBG initial seeding
  12165. * Seed and setup entropy source for future reseeds.
  12166. *
  12167. * Note: Personalization data can be provided in addition to the more generic
  12168. * entropy source to make this instantiation as unique as possible.
  12169. *
  12170. * \param ctx CTR_DRBG context to be seeded
  12171. * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
  12172. * length)
  12173. * \param p_entropy Entropy context
  12174. * \param custom Personalization data (Device specific identifiers)
  12175. * (Can be NULL)
  12176. * \param len Length of personalization data
  12177. *
  12178. * \return 0 if successful, or
  12179. * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
  12180. */
  12181. int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
  12182. int (*f_entropy)(void *, unsigned char *, size_t),
  12183. void *p_entropy,
  12184. const unsigned char *custom,
  12185. size_t len );
  12186. /**
  12187. * \brief Clear CTR_CRBG context data
  12188. *
  12189. * \param ctx CTR_DRBG context to clear
  12190. */
  12191. void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
  12192. /**
  12193. * \brief Enable / disable prediction resistance (Default: Off)
  12194. *
  12195. * Note: If enabled, entropy is used for ctx->entropy_len before each call!
  12196. * Only use this if you have ample supply of good entropy!
  12197. *
  12198. * \param ctx CTR_DRBG context
  12199. * \param resistance MBEDTLS_CTR_DRBG_PR_ON or MBEDTLS_CTR_DRBG_PR_OFF
  12200. */
  12201. void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
  12202. int resistance );
  12203. /**
  12204. * \brief Set the amount of entropy grabbed on each (re)seed
  12205. * (Default: MBEDTLS_CTR_DRBG_ENTROPY_LEN)
  12206. *
  12207. * \param ctx CTR_DRBG context
  12208. * \param len Amount of entropy to grab
  12209. */
  12210. void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
  12211. size_t len );
  12212. /**
  12213. * \brief Set the reseed interval
  12214. * (Default: MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
  12215. *
  12216. * \param ctx CTR_DRBG context
  12217. * \param interval Reseed interval
  12218. */
  12219. void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
  12220. int interval );
  12221. /**
  12222. * \brief CTR_DRBG reseeding (extracts data from entropy source)
  12223. *
  12224. * \param ctx CTR_DRBG context
  12225. * \param additional Additional data to add to state (Can be NULL)
  12226. * \param len Length of additional data
  12227. *
  12228. * \return 0 if successful, or
  12229. * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
  12230. */
  12231. int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
  12232. const unsigned char *additional, size_t len );
  12233. /**
  12234. * \brief CTR_DRBG update state
  12235. *
  12236. * \param ctx CTR_DRBG context
  12237. * \param additional Additional data to update state with
  12238. * \param add_len Length of additional data
  12239. *
  12240. * \note If add_len is greater than MBEDTLS_CTR_DRBG_MAX_SEED_INPUT,
  12241. * only the first MBEDTLS_CTR_DRBG_MAX_SEED_INPUT bytes are used,
  12242. * the remaining ones are silently discarded.
  12243. */
  12244. void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
  12245. const unsigned char *additional, size_t add_len );
  12246. /**
  12247. * \brief CTR_DRBG generate random with additional update input
  12248. *
  12249. * Note: Automatically reseeds if reseed_counter is reached.
  12250. *
  12251. * \param p_rng CTR_DRBG context
  12252. * \param output Buffer to fill
  12253. * \param output_len Length of the buffer
  12254. * \param additional Additional data to update with (Can be NULL)
  12255. * \param add_len Length of additional data
  12256. *
  12257. * \return 0 if successful, or
  12258. * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
  12259. * MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG
  12260. */
  12261. int mbedtls_ctr_drbg_random_with_add( void *p_rng,
  12262. unsigned char *output, size_t output_len,
  12263. const unsigned char *additional, size_t add_len );
  12264. /**
  12265. * \brief CTR_DRBG generate random
  12266. *
  12267. * Note: Automatically reseeds if reseed_counter is reached.
  12268. *
  12269. * \param p_rng CTR_DRBG context
  12270. * \param output Buffer to fill
  12271. * \param output_len Length of the buffer
  12272. *
  12273. * \return 0 if successful, or
  12274. * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
  12275. * MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG
  12276. */
  12277. int mbedtls_ctr_drbg_random( void *p_rng,
  12278. unsigned char *output, size_t output_len );
  12279. #if defined(MBEDTLS_FS_IO)
  12280. /**
  12281. * \brief Write a seed file
  12282. *
  12283. * \param ctx CTR_DRBG context
  12284. * \param path Name of the file
  12285. *
  12286. * \return 0 if successful,
  12287. * MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error, or
  12288. * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
  12289. */
  12290. int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
  12291. /**
  12292. * \brief Read and update a seed file. Seed is added to this
  12293. * instance
  12294. *
  12295. * \param ctx CTR_DRBG context
  12296. * \param path Name of the file
  12297. *
  12298. * \return 0 if successful,
  12299. * MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error,
  12300. * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
  12301. * MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG
  12302. */
  12303. int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
  12304. #endif /* MBEDTLS_FS_IO */
  12305. /**
  12306. * \brief Checkup routine
  12307. *
  12308. * \return 0 if successful, or 1 if the test failed
  12309. */
  12310. int mbedtls_ctr_drbg_self_test( int verbose );
  12311. /* Internal functions (do not call directly) */
  12312. int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
  12313. int (*)(void *, unsigned char *, size_t), void *,
  12314. const unsigned char *, size_t, size_t );
  12315. #ifdef __cplusplus
  12316. }
  12317. #endif
  12318. #endif /* ctr_drbg.h */
  12319. /********* Start of file include/mbedtls/des.h ************/
  12320. /**
  12321. * \file des.h
  12322. *
  12323. * \brief DES block cipher
  12324. *
  12325. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  12326. * SPDX-License-Identifier: Apache-2.0
  12327. *
  12328. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  12329. * not use this file except in compliance with the License.
  12330. * You may obtain a copy of the License at
  12331. *
  12332. * http://www.apache.org/licenses/LICENSE-2.0
  12333. *
  12334. * Unless required by applicable law or agreed to in writing, software
  12335. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12336. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12337. * See the License for the specific language governing permissions and
  12338. * limitations under the License.
  12339. *
  12340. * This file is part of mbed TLS (https://tls.mbed.org)
  12341. */
  12342. #ifndef MBEDTLS_DES_H
  12343. #define MBEDTLS_DES_H
  12344. #if !defined(MBEDTLS_CONFIG_FILE)
  12345. #else
  12346. #endif
  12347. #include <stddef.h>
  12348. #if !VXWORKS
  12349. #include <stdint.h>
  12350. #endif
  12351. #define MBEDTLS_DES_ENCRYPT 1
  12352. #define MBEDTLS_DES_DECRYPT 0
  12353. #define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
  12354. #define MBEDTLS_DES_KEY_SIZE 8
  12355. #if !defined(MBEDTLS_DES_ALT)
  12356. // Regular implementation
  12357. //
  12358. #ifdef __cplusplus
  12359. extern "C" {
  12360. #endif
  12361. /**
  12362. * \brief DES context structure
  12363. */
  12364. typedef struct
  12365. {
  12366. uint32_t sk[32]; /*!< DES subkeys */
  12367. }
  12368. mbedtls_des_context;
  12369. /**
  12370. * \brief Triple-DES context structure
  12371. */
  12372. typedef struct
  12373. {
  12374. uint32_t sk[96]; /*!< 3DES subkeys */
  12375. }
  12376. mbedtls_des3_context;
  12377. /**
  12378. * \brief Initialize DES context
  12379. *
  12380. * \param ctx DES context to be initialized
  12381. */
  12382. void mbedtls_des_init( mbedtls_des_context *ctx );
  12383. /**
  12384. * \brief Clear DES context
  12385. *
  12386. * \param ctx DES context to be cleared
  12387. */
  12388. void mbedtls_des_free( mbedtls_des_context *ctx );
  12389. /**
  12390. * \brief Initialize Triple-DES context
  12391. *
  12392. * \param ctx DES3 context to be initialized
  12393. */
  12394. void mbedtls_des3_init( mbedtls_des3_context *ctx );
  12395. /**
  12396. * \brief Clear Triple-DES context
  12397. *
  12398. * \param ctx DES3 context to be cleared
  12399. */
  12400. void mbedtls_des3_free( mbedtls_des3_context *ctx );
  12401. /**
  12402. * \brief Set key parity on the given key to odd.
  12403. *
  12404. * DES keys are 56 bits long, but each byte is padded with
  12405. * a parity bit to allow verification.
  12406. *
  12407. * \param key 8-byte secret key
  12408. */
  12409. void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
  12410. /**
  12411. * \brief Check that key parity on the given key is odd.
  12412. *
  12413. * DES keys are 56 bits long, but each byte is padded with
  12414. * a parity bit to allow verification.
  12415. *
  12416. * \param key 8-byte secret key
  12417. *
  12418. * \return 0 is parity was ok, 1 if parity was not correct.
  12419. */
  12420. int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
  12421. /**
  12422. * \brief Check that key is not a weak or semi-weak DES key
  12423. *
  12424. * \param key 8-byte secret key
  12425. *
  12426. * \return 0 if no weak key was found, 1 if a weak key was identified.
  12427. */
  12428. int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
  12429. /**
  12430. * \brief DES key schedule (56-bit, encryption)
  12431. *
  12432. * \param ctx DES context to be initialized
  12433. * \param key 8-byte secret key
  12434. *
  12435. * \return 0
  12436. */
  12437. int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
  12438. /**
  12439. * \brief DES key schedule (56-bit, decryption)
  12440. *
  12441. * \param ctx DES context to be initialized
  12442. * \param key 8-byte secret key
  12443. *
  12444. * \return 0
  12445. */
  12446. int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
  12447. /**
  12448. * \brief Triple-DES key schedule (112-bit, encryption)
  12449. *
  12450. * \param ctx 3DES context to be initialized
  12451. * \param key 16-byte secret key
  12452. *
  12453. * \return 0
  12454. */
  12455. int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
  12456. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
  12457. /**
  12458. * \brief Triple-DES key schedule (112-bit, decryption)
  12459. *
  12460. * \param ctx 3DES context to be initialized
  12461. * \param key 16-byte secret key
  12462. *
  12463. * \return 0
  12464. */
  12465. int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
  12466. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
  12467. /**
  12468. * \brief Triple-DES key schedule (168-bit, encryption)
  12469. *
  12470. * \param ctx 3DES context to be initialized
  12471. * \param key 24-byte secret key
  12472. *
  12473. * \return 0
  12474. */
  12475. int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
  12476. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
  12477. /**
  12478. * \brief Triple-DES key schedule (168-bit, decryption)
  12479. *
  12480. * \param ctx 3DES context to be initialized
  12481. * \param key 24-byte secret key
  12482. *
  12483. * \return 0
  12484. */
  12485. int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
  12486. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
  12487. /**
  12488. * \brief DES-ECB block encryption/decryption
  12489. *
  12490. * \param ctx DES context
  12491. * \param input 64-bit input block
  12492. * \param output 64-bit output block
  12493. *
  12494. * \return 0 if successful
  12495. */
  12496. int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
  12497. const unsigned char input[8],
  12498. unsigned char output[8] );
  12499. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  12500. /**
  12501. * \brief DES-CBC buffer encryption/decryption
  12502. *
  12503. * \note Upon exit, the content of the IV is updated so that you can
  12504. * call the function same function again on the following
  12505. * block(s) of data and get the same result as if it was
  12506. * encrypted in one call. This allows a "streaming" usage.
  12507. * If on the other hand you need to retain the contents of the
  12508. * IV, you should either save it manually or use the cipher
  12509. * module instead.
  12510. *
  12511. * \param ctx DES context
  12512. * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
  12513. * \param length length of the input data
  12514. * \param iv initialization vector (updated after use)
  12515. * \param input buffer holding the input data
  12516. * \param output buffer holding the output data
  12517. */
  12518. int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
  12519. int mode,
  12520. size_t length,
  12521. unsigned char iv[8],
  12522. const unsigned char *input,
  12523. unsigned char *output );
  12524. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  12525. /**
  12526. * \brief 3DES-ECB block encryption/decryption
  12527. *
  12528. * \param ctx 3DES context
  12529. * \param input 64-bit input block
  12530. * \param output 64-bit output block
  12531. *
  12532. * \return 0 if successful
  12533. */
  12534. int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
  12535. const unsigned char input[8],
  12536. unsigned char output[8] );
  12537. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  12538. /**
  12539. * \brief 3DES-CBC buffer encryption/decryption
  12540. *
  12541. * \note Upon exit, the content of the IV is updated so that you can
  12542. * call the function same function again on the following
  12543. * block(s) of data and get the same result as if it was
  12544. * encrypted in one call. This allows a "streaming" usage.
  12545. * If on the other hand you need to retain the contents of the
  12546. * IV, you should either save it manually or use the cipher
  12547. * module instead.
  12548. *
  12549. * \param ctx 3DES context
  12550. * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
  12551. * \param length length of the input data
  12552. * \param iv initialization vector (updated after use)
  12553. * \param input buffer holding the input data
  12554. * \param output buffer holding the output data
  12555. *
  12556. * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
  12557. */
  12558. int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
  12559. int mode,
  12560. size_t length,
  12561. unsigned char iv[8],
  12562. const unsigned char *input,
  12563. unsigned char *output );
  12564. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  12565. /**
  12566. * \brief Internal function for key expansion.
  12567. * (Only exposed to allow overriding it,
  12568. * see MBEDTLS_DES_SETKEY_ALT)
  12569. *
  12570. * \param SK Round keys
  12571. * \param key Base key
  12572. */
  12573. void mbedtls_des_setkey( uint32_t SK[32],
  12574. const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
  12575. #ifdef __cplusplus
  12576. }
  12577. #endif
  12578. #else /* MBEDTLS_DES_ALT */
  12579. #endif /* MBEDTLS_DES_ALT */
  12580. #ifdef __cplusplus
  12581. extern "C" {
  12582. #endif
  12583. /**
  12584. * \brief Checkup routine
  12585. *
  12586. * \return 0 if successful, or 1 if the test failed
  12587. */
  12588. int mbedtls_des_self_test( int verbose );
  12589. #ifdef __cplusplus
  12590. }
  12591. #endif
  12592. #endif /* des.h */
  12593. /********* Start of file include/mbedtls/entropy.h ************/
  12594. /**
  12595. * \file entropy.h
  12596. *
  12597. * \brief Entropy accumulator implementation
  12598. *
  12599. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  12600. * SPDX-License-Identifier: Apache-2.0
  12601. *
  12602. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  12603. * not use this file except in compliance with the License.
  12604. * You may obtain a copy of the License at
  12605. *
  12606. * http://www.apache.org/licenses/LICENSE-2.0
  12607. *
  12608. * Unless required by applicable law or agreed to in writing, software
  12609. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12610. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12611. * See the License for the specific language governing permissions and
  12612. * limitations under the License.
  12613. *
  12614. * This file is part of mbed TLS (https://tls.mbed.org)
  12615. */
  12616. #ifndef MBEDTLS_ENTROPY_H
  12617. #define MBEDTLS_ENTROPY_H
  12618. #if !defined(MBEDTLS_CONFIG_FILE)
  12619. #else
  12620. #endif
  12621. #include <stddef.h>
  12622. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
  12623. #define MBEDTLS_ENTROPY_SHA512_ACCUMULATOR
  12624. #else
  12625. #if defined(MBEDTLS_SHA256_C)
  12626. #define MBEDTLS_ENTROPY_SHA256_ACCUMULATOR
  12627. #endif
  12628. #endif
  12629. #if defined(MBEDTLS_THREADING_C)
  12630. #endif
  12631. #if defined(MBEDTLS_HAVEGE_C)
  12632. #endif
  12633. #define MBEDTLS_ERR_ENTROPY_SOURCE_FAILED -0x003C /**< Critical entropy source failure. */
  12634. #define MBEDTLS_ERR_ENTROPY_MAX_SOURCES -0x003E /**< No more sources can be added. */
  12635. #define MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 /**< No sources have been added to poll. */
  12636. #define MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE -0x003D /**< No strong sources have been added to poll. */
  12637. #define MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR -0x003F /**< Read/write error in file. */
  12638. /**
  12639. * \name SECTION: Module settings
  12640. *
  12641. * The configuration options you can set for this module are in this section.
  12642. * Either change them in config.h or define them on the compiler command line.
  12643. * \{
  12644. */
  12645. #if !defined(MBEDTLS_ENTROPY_MAX_SOURCES)
  12646. #define MBEDTLS_ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
  12647. #endif
  12648. #if !defined(MBEDTLS_ENTROPY_MAX_GATHER)
  12649. #define MBEDTLS_ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */
  12650. #endif
  12651. /* \} name SECTION: Module settings */
  12652. #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
  12653. #define MBEDTLS_ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
  12654. #else
  12655. #define MBEDTLS_ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */
  12656. #endif
  12657. #define MBEDTLS_ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */
  12658. #define MBEDTLS_ENTROPY_SOURCE_MANUAL MBEDTLS_ENTROPY_MAX_SOURCES
  12659. #define MBEDTLS_ENTROPY_SOURCE_STRONG 1 /**< Entropy source is strong */
  12660. #define MBEDTLS_ENTROPY_SOURCE_WEAK 0 /**< Entropy source is weak */
  12661. #ifdef __cplusplus
  12662. extern "C" {
  12663. #endif
  12664. /**
  12665. * \brief Entropy poll callback pointer
  12666. *
  12667. * \param data Callback-specific data pointer
  12668. * \param output Data to fill
  12669. * \param len Maximum size to provide
  12670. * \param olen The actual amount of bytes put into the buffer (Can be 0)
  12671. *
  12672. * \return 0 if no critical failures occurred,
  12673. * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise
  12674. */
  12675. typedef int (*mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output, size_t len,
  12676. size_t *olen);
  12677. /**
  12678. * \brief Entropy source state
  12679. */
  12680. typedef struct
  12681. {
  12682. mbedtls_entropy_f_source_ptr f_source; /**< The entropy source callback */
  12683. void * p_source; /**< The callback data pointer */
  12684. size_t size; /**< Amount received in bytes */
  12685. size_t threshold; /**< Minimum bytes required before release */
  12686. int strong; /**< Is the source strong? */
  12687. }
  12688. mbedtls_entropy_source_state;
  12689. /**
  12690. * \brief Entropy context structure
  12691. */
  12692. typedef struct
  12693. {
  12694. #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
  12695. mbedtls_sha512_context accumulator;
  12696. #else
  12697. mbedtls_sha256_context accumulator;
  12698. #endif
  12699. int source_count;
  12700. mbedtls_entropy_source_state source[MBEDTLS_ENTROPY_MAX_SOURCES];
  12701. #if defined(MBEDTLS_HAVEGE_C)
  12702. mbedtls_havege_state havege_data;
  12703. #endif
  12704. #if defined(MBEDTLS_THREADING_C)
  12705. mbedtls_threading_mutex_t mutex; /*!< mutex */
  12706. #endif
  12707. }
  12708. mbedtls_entropy_context;
  12709. /**
  12710. * \brief Initialize the context
  12711. *
  12712. * \param ctx Entropy context to initialize
  12713. */
  12714. void mbedtls_entropy_init( mbedtls_entropy_context *ctx );
  12715. /**
  12716. * \brief Free the data in the context
  12717. *
  12718. * \param ctx Entropy context to free
  12719. */
  12720. void mbedtls_entropy_free( mbedtls_entropy_context *ctx );
  12721. /**
  12722. * \brief Adds an entropy source to poll
  12723. * (Thread-safe if MBEDTLS_THREADING_C is enabled)
  12724. *
  12725. * \param ctx Entropy context
  12726. * \param f_source Entropy function
  12727. * \param p_source Function data
  12728. * \param threshold Minimum required from source before entropy is released
  12729. * ( with mbedtls_entropy_func() ) (in bytes)
  12730. * \param strong MBEDTLS_ENTROPY_SOURCE_STRONG or
  12731. * MBEDTSL_ENTROPY_SOURCE_WEAK.
  12732. * At least one strong source needs to be added.
  12733. * Weaker sources (such as the cycle counter) can be used as
  12734. * a complement.
  12735. *
  12736. * \return 0 if successful or MBEDTLS_ERR_ENTROPY_MAX_SOURCES
  12737. */
  12738. int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
  12739. mbedtls_entropy_f_source_ptr f_source, void *p_source,
  12740. size_t threshold, int strong );
  12741. /**
  12742. * \brief Trigger an extra gather poll for the accumulator
  12743. * (Thread-safe if MBEDTLS_THREADING_C is enabled)
  12744. *
  12745. * \param ctx Entropy context
  12746. *
  12747. * \return 0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
  12748. */
  12749. int mbedtls_entropy_gather( mbedtls_entropy_context *ctx );
  12750. /**
  12751. * \brief Retrieve entropy from the accumulator
  12752. * (Maximum length: MBEDTLS_ENTROPY_BLOCK_SIZE)
  12753. * (Thread-safe if MBEDTLS_THREADING_C is enabled)
  12754. *
  12755. * \param data Entropy context
  12756. * \param output Buffer to fill
  12757. * \param len Number of bytes desired, must be at most MBEDTLS_ENTROPY_BLOCK_SIZE
  12758. *
  12759. * \return 0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
  12760. */
  12761. int mbedtls_entropy_func( void *data, unsigned char *output, size_t len );
  12762. /**
  12763. * \brief Add data to the accumulator manually
  12764. * (Thread-safe if MBEDTLS_THREADING_C is enabled)
  12765. *
  12766. * \param ctx Entropy context
  12767. * \param data Data to add
  12768. * \param len Length of data
  12769. *
  12770. * \return 0 if successful
  12771. */
  12772. int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
  12773. const unsigned char *data, size_t len );
  12774. #if defined(MBEDTLS_FS_IO)
  12775. /**
  12776. * \brief Write a seed file
  12777. *
  12778. * \param ctx Entropy context
  12779. * \param path Name of the file
  12780. *
  12781. * \return 0 if successful,
  12782. * MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error, or
  12783. * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
  12784. */
  12785. int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path );
  12786. /**
  12787. * \brief Read and update a seed file. Seed is added to this
  12788. * instance. No more than MBEDTLS_ENTROPY_MAX_SEED_SIZE bytes are
  12789. * read from the seed file. The rest is ignored.
  12790. *
  12791. * \param ctx Entropy context
  12792. * \param path Name of the file
  12793. *
  12794. * \return 0 if successful,
  12795. * MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error,
  12796. * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
  12797. */
  12798. int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path );
  12799. #endif /* MBEDTLS_FS_IO */
  12800. #if defined(MBEDTLS_SELF_TEST)
  12801. /**
  12802. * \brief Checkup routine
  12803. *
  12804. * \return 0 if successful, or 1 if a test failed
  12805. */
  12806. int mbedtls_entropy_self_test( int verbose );
  12807. #endif /* MBEDTLS_SELF_TEST */
  12808. #ifdef __cplusplus
  12809. }
  12810. #endif
  12811. #endif /* entropy.h */
  12812. /********* Start of file include/mbedtls/entropy_poll.h ************/
  12813. /**
  12814. * \file entropy_poll.h
  12815. *
  12816. * \brief Platform-specific and custom entropy polling functions
  12817. *
  12818. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  12819. * SPDX-License-Identifier: Apache-2.0
  12820. *
  12821. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  12822. * not use this file except in compliance with the License.
  12823. * You may obtain a copy of the License at
  12824. *
  12825. * http://www.apache.org/licenses/LICENSE-2.0
  12826. *
  12827. * Unless required by applicable law or agreed to in writing, software
  12828. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12829. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12830. * See the License for the specific language governing permissions and
  12831. * limitations under the License.
  12832. *
  12833. * This file is part of mbed TLS (https://tls.mbed.org)
  12834. */
  12835. #ifndef MBEDTLS_ENTROPY_POLL_H
  12836. #define MBEDTLS_ENTROPY_POLL_H
  12837. #if !defined(MBEDTLS_CONFIG_FILE)
  12838. #else
  12839. #endif
  12840. #include <stddef.h>
  12841. #ifdef __cplusplus
  12842. extern "C" {
  12843. #endif
  12844. /*
  12845. * Default thresholds for built-in sources, in bytes
  12846. */
  12847. #define MBEDTLS_ENTROPY_MIN_PLATFORM 32 /**< Minimum for platform source */
  12848. #define MBEDTLS_ENTROPY_MIN_HAVEGE 32 /**< Minimum for HAVEGE */
  12849. #define MBEDTLS_ENTROPY_MIN_HARDCLOCK 4 /**< Minimum for mbedtls_timing_hardclock() */
  12850. #define MBEDTLS_ENTROPY_MIN_HARDWARE 32 /**< Minimum for the hardware source */
  12851. #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
  12852. /**
  12853. * \brief Platform-specific entropy poll callback
  12854. */
  12855. int mbedtls_platform_entropy_poll( void *data,
  12856. unsigned char *output, size_t len, size_t *olen );
  12857. #endif
  12858. #if defined(MBEDTLS_HAVEGE_C)
  12859. /**
  12860. * \brief HAVEGE based entropy poll callback
  12861. *
  12862. * Requires an HAVEGE state as its data pointer.
  12863. */
  12864. int mbedtls_havege_poll( void *data,
  12865. unsigned char *output, size_t len, size_t *olen );
  12866. #endif
  12867. #if defined(MBEDTLS_TIMING_C)
  12868. /**
  12869. * \brief mbedtls_timing_hardclock-based entropy poll callback
  12870. */
  12871. int mbedtls_hardclock_poll( void *data,
  12872. unsigned char *output, size_t len, size_t *olen );
  12873. #endif
  12874. #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
  12875. /**
  12876. * \brief Entropy poll callback for a hardware source
  12877. *
  12878. * \warning This is not provided by mbed TLS!
  12879. * See \c MBEDTLS_ENTROPY_HARDWARE_ALT in config.h.
  12880. *
  12881. * \note This must accept NULL as its first argument.
  12882. */
  12883. int mbedtls_hardware_poll( void *data,
  12884. unsigned char *output, size_t len, size_t *olen );
  12885. #endif
  12886. #ifdef __cplusplus
  12887. }
  12888. #endif
  12889. #endif /* entropy_poll.h */
  12890. /********* Start of file include/mbedtls/havege.h ************/
  12891. /**
  12892. * \file havege.h
  12893. *
  12894. * \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion
  12895. *
  12896. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  12897. * SPDX-License-Identifier: Apache-2.0
  12898. *
  12899. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  12900. * not use this file except in compliance with the License.
  12901. * You may obtain a copy of the License at
  12902. *
  12903. * http://www.apache.org/licenses/LICENSE-2.0
  12904. *
  12905. * Unless required by applicable law or agreed to in writing, software
  12906. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12907. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12908. * See the License for the specific language governing permissions and
  12909. * limitations under the License.
  12910. *
  12911. * This file is part of mbed TLS (https://tls.mbed.org)
  12912. */
  12913. #ifndef MBEDTLS_HAVEGE_H
  12914. #define MBEDTLS_HAVEGE_H
  12915. #include <stddef.h>
  12916. #define MBEDTLS_HAVEGE_COLLECT_SIZE 1024
  12917. #ifdef __cplusplus
  12918. extern "C" {
  12919. #endif
  12920. /**
  12921. * \brief HAVEGE state structure
  12922. */
  12923. typedef struct
  12924. {
  12925. int PT1, PT2, offset[2];
  12926. int pool[MBEDTLS_HAVEGE_COLLECT_SIZE];
  12927. int WALK[8192];
  12928. }
  12929. mbedtls_havege_state;
  12930. /**
  12931. * \brief HAVEGE initialization
  12932. *
  12933. * \param hs HAVEGE state to be initialized
  12934. */
  12935. void mbedtls_havege_init( mbedtls_havege_state *hs );
  12936. /**
  12937. * \brief Clear HAVEGE state
  12938. *
  12939. * \param hs HAVEGE state to be cleared
  12940. */
  12941. void mbedtls_havege_free( mbedtls_havege_state *hs );
  12942. /**
  12943. * \brief HAVEGE rand function
  12944. *
  12945. * \param p_rng A HAVEGE state
  12946. * \param output Buffer to fill
  12947. * \param len Length of buffer
  12948. *
  12949. * \return 0
  12950. */
  12951. int mbedtls_havege_random( void *p_rng, unsigned char *output, size_t len );
  12952. #ifdef __cplusplus
  12953. }
  12954. #endif
  12955. #endif /* havege.h */
  12956. /********* Start of file include/mbedtls/memory_buffer_alloc.h ************/
  12957. /**
  12958. * \file memory_buffer_alloc.h
  12959. *
  12960. * \brief Buffer-based memory allocator
  12961. *
  12962. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  12963. * SPDX-License-Identifier: Apache-2.0
  12964. *
  12965. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  12966. * not use this file except in compliance with the License.
  12967. * You may obtain a copy of the License at
  12968. *
  12969. * http://www.apache.org/licenses/LICENSE-2.0
  12970. *
  12971. * Unless required by applicable law or agreed to in writing, software
  12972. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12973. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12974. * See the License for the specific language governing permissions and
  12975. * limitations under the License.
  12976. *
  12977. * This file is part of mbed TLS (https://tls.mbed.org)
  12978. */
  12979. #ifndef MBEDTLS_MEMORY_BUFFER_ALLOC_H
  12980. #define MBEDTLS_MEMORY_BUFFER_ALLOC_H
  12981. #if !defined(MBEDTLS_CONFIG_FILE)
  12982. #else
  12983. #endif
  12984. #include <stddef.h>
  12985. /**
  12986. * \name SECTION: Module settings
  12987. *
  12988. * The configuration options you can set for this module are in this section.
  12989. * Either change them in config.h or define them on the compiler command line.
  12990. * \{
  12991. */
  12992. #if !defined(MBEDTLS_MEMORY_ALIGN_MULTIPLE)
  12993. #define MBEDTLS_MEMORY_ALIGN_MULTIPLE 4 /**< Align on multiples of this value */
  12994. #endif
  12995. /* \} name SECTION: Module settings */
  12996. #define MBEDTLS_MEMORY_VERIFY_NONE 0
  12997. #define MBEDTLS_MEMORY_VERIFY_ALLOC (1 << 0)
  12998. #define MBEDTLS_MEMORY_VERIFY_FREE (1 << 1)
  12999. #define MBEDTLS_MEMORY_VERIFY_ALWAYS (MBEDTLS_MEMORY_VERIFY_ALLOC | MBEDTLS_MEMORY_VERIFY_FREE)
  13000. #ifdef __cplusplus
  13001. extern "C" {
  13002. #endif
  13003. /**
  13004. * \brief Initialize use of stack-based memory allocator.
  13005. * The stack-based allocator does memory management inside the
  13006. * presented buffer and does not call calloc() and free().
  13007. * It sets the global mbedtls_calloc() and mbedtls_free() pointers
  13008. * to its own functions.
  13009. * (Provided mbedtls_calloc() and mbedtls_free() are thread-safe if
  13010. * MBEDTLS_THREADING_C is defined)
  13011. *
  13012. * \note This code is not optimized and provides a straight-forward
  13013. * implementation of a stack-based memory allocator.
  13014. *
  13015. * \param buf buffer to use as heap
  13016. * \param len size of the buffer
  13017. */
  13018. void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len );
  13019. /**
  13020. * \brief Free the mutex for thread-safety and clear remaining memory
  13021. */
  13022. void mbedtls_memory_buffer_alloc_free( void );
  13023. /**
  13024. * \brief Determine when the allocator should automatically verify the state
  13025. * of the entire chain of headers / meta-data.
  13026. * (Default: MBEDTLS_MEMORY_VERIFY_NONE)
  13027. *
  13028. * \param verify One of MBEDTLS_MEMORY_VERIFY_NONE, MBEDTLS_MEMORY_VERIFY_ALLOC,
  13029. * MBEDTLS_MEMORY_VERIFY_FREE or MBEDTLS_MEMORY_VERIFY_ALWAYS
  13030. */
  13031. void mbedtls_memory_buffer_set_verify( int verify );
  13032. #if defined(MBEDTLS_MEMORY_DEBUG)
  13033. /**
  13034. * \brief Print out the status of the allocated memory (primarily for use
  13035. * after a program should have de-allocated all memory)
  13036. * Prints out a list of 'still allocated' blocks and their stack
  13037. * trace if MBEDTLS_MEMORY_BACKTRACE is defined.
  13038. */
  13039. void mbedtls_memory_buffer_alloc_status( void );
  13040. /**
  13041. * \brief Get the peak heap usage so far
  13042. *
  13043. * \param max_used Peak number of bytes reauested by the application
  13044. * \param max_blocks Peak number of blocks reauested by the application
  13045. */
  13046. void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks );
  13047. /**
  13048. * \brief Reset peak statistics
  13049. */
  13050. void mbedtls_memory_buffer_alloc_max_reset( void );
  13051. /**
  13052. * \brief Get the current heap usage
  13053. *
  13054. * \param cur_used Number of bytes reauested by the application
  13055. * \param cur_blocks Number of blocks reauested by the application
  13056. */
  13057. void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks );
  13058. #endif /* MBEDTLS_MEMORY_DEBUG */
  13059. /**
  13060. * \brief Verifies that all headers in the memory buffer are correct
  13061. * and contain sane values. Helps debug buffer-overflow errors.
  13062. *
  13063. * Prints out first failure if MBEDTLS_MEMORY_DEBUG is defined.
  13064. * Prints out full header information if MBEDTLS_MEMORY_DEBUG
  13065. * is defined. (Includes stack trace information for each block if
  13066. * MBEDTLS_MEMORY_BACKTRACE is defined as well).
  13067. *
  13068. * \return 0 if verified, 1 otherwise
  13069. */
  13070. int mbedtls_memory_buffer_alloc_verify( void );
  13071. #if defined(MBEDTLS_SELF_TEST)
  13072. /**
  13073. * \brief Checkup routine
  13074. *
  13075. * \return 0 if successful, or 1 if a test failed
  13076. */
  13077. int mbedtls_memory_buffer_alloc_self_test( int verbose );
  13078. #endif
  13079. #ifdef __cplusplus
  13080. }
  13081. #endif
  13082. #endif /* memory_buffer_alloc.h */
  13083. /********* Start of file include/mbedtls/padlock.h ************/
  13084. /**
  13085. * \file padlock.h
  13086. *
  13087. * \brief VIA PadLock ACE for HW encryption/decryption supported by some
  13088. * processors
  13089. *
  13090. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  13091. * SPDX-License-Identifier: Apache-2.0
  13092. *
  13093. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  13094. * not use this file except in compliance with the License.
  13095. * You may obtain a copy of the License at
  13096. *
  13097. * http://www.apache.org/licenses/LICENSE-2.0
  13098. *
  13099. * Unless required by applicable law or agreed to in writing, software
  13100. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13101. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13102. * See the License for the specific language governing permissions and
  13103. * limitations under the License.
  13104. *
  13105. * This file is part of mbed TLS (https://tls.mbed.org)
  13106. */
  13107. #ifndef MBEDTLS_PADLOCK_H
  13108. #define MBEDTLS_PADLOCK_H
  13109. #define MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED -0x0030 /**< Input data should be aligned. */
  13110. #if defined(__has_feature)
  13111. #if __has_feature(address_sanitizer)
  13112. #define MBEDTLS_HAVE_ASAN
  13113. #endif
  13114. #endif
  13115. /* Some versions of ASan result in errors about not enough registers */
  13116. #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && defined(__i386__) && \
  13117. !defined(MBEDTLS_HAVE_ASAN)
  13118. #ifndef MBEDTLS_HAVE_X86
  13119. #define MBEDTLS_HAVE_X86
  13120. #endif
  13121. #if !VXWORKS
  13122. #include <stdint.h>
  13123. #endif
  13124. #define MBEDTLS_PADLOCK_RNG 0x000C
  13125. #define MBEDTLS_PADLOCK_ACE 0x00C0
  13126. #define MBEDTLS_PADLOCK_PHE 0x0C00
  13127. #define MBEDTLS_PADLOCK_PMM 0x3000
  13128. #define MBEDTLS_PADLOCK_ALIGN16(x) (uint32_t *) (16 + ((int32_t) x & ~15))
  13129. #ifdef __cplusplus
  13130. extern "C" {
  13131. #endif
  13132. /**
  13133. * \brief PadLock detection routine
  13134. *
  13135. * \param feature The feature to detect
  13136. *
  13137. * \return 1 if CPU has support for the feature, 0 otherwise
  13138. */
  13139. int mbedtls_padlock_has_support( int feature );
  13140. /**
  13141. * \brief PadLock AES-ECB block en(de)cryption
  13142. *
  13143. * \param ctx AES context
  13144. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  13145. * \param input 16-byte input block
  13146. * \param output 16-byte output block
  13147. *
  13148. * \return 0 if success, 1 if operation failed
  13149. */
  13150. int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx,
  13151. int mode,
  13152. const unsigned char input[16],
  13153. unsigned char output[16] );
  13154. /**
  13155. * \brief PadLock AES-CBC buffer en(de)cryption
  13156. *
  13157. * \param ctx AES context
  13158. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  13159. * \param length length of the input data
  13160. * \param iv initialization vector (updated after use)
  13161. * \param input buffer holding the input data
  13162. * \param output buffer holding the output data
  13163. *
  13164. * \return 0 if success, 1 if operation failed
  13165. */
  13166. int mbedtls_padlock_xcryptcbc( mbedtls_aes_context *ctx,
  13167. int mode,
  13168. size_t length,
  13169. unsigned char iv[16],
  13170. const unsigned char *input,
  13171. unsigned char *output );
  13172. #ifdef __cplusplus
  13173. }
  13174. #endif
  13175. #endif /* HAVE_X86 */
  13176. #endif /* padlock.h */
  13177. /********* Start of file include/mbedtls/timing.h ************/
  13178. /**
  13179. * \file timing.h
  13180. *
  13181. * \brief Portable interface to the CPU cycle counter
  13182. *
  13183. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  13184. * SPDX-License-Identifier: Apache-2.0
  13185. *
  13186. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  13187. * not use this file except in compliance with the License.
  13188. * You may obtain a copy of the License at
  13189. *
  13190. * http://www.apache.org/licenses/LICENSE-2.0
  13191. *
  13192. * Unless required by applicable law or agreed to in writing, software
  13193. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13194. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13195. * See the License for the specific language governing permissions and
  13196. * limitations under the License.
  13197. *
  13198. * This file is part of mbed TLS (https://tls.mbed.org)
  13199. */
  13200. #ifndef MBEDTLS_TIMING_H
  13201. #define MBEDTLS_TIMING_H
  13202. #if !defined(MBEDTLS_CONFIG_FILE)
  13203. #else
  13204. #endif
  13205. #if !defined(MBEDTLS_TIMING_ALT)
  13206. // Regular implementation
  13207. //
  13208. #if !VXWORKS
  13209. #include <stdint.h>
  13210. #endif
  13211. #ifdef __cplusplus
  13212. extern "C" {
  13213. #endif
  13214. /**
  13215. * \brief timer structure
  13216. */
  13217. struct mbedtls_timing_hr_time
  13218. {
  13219. unsigned char opaque[32];
  13220. };
  13221. /**
  13222. * \brief Context for mbedtls_timing_set/get_delay()
  13223. */
  13224. typedef struct
  13225. {
  13226. struct mbedtls_timing_hr_time timer;
  13227. uint32_t int_ms;
  13228. uint32_t fin_ms;
  13229. } mbedtls_timing_delay_context;
  13230. extern volatile int mbedtls_timing_alarmed;
  13231. /**
  13232. * \brief Return the CPU cycle counter value
  13233. *
  13234. * \warning This is only a best effort! Do not rely on this!
  13235. * In particular, it is known to be unreliable on virtual
  13236. * machines.
  13237. */
  13238. unsigned long mbedtls_timing_hardclock( void );
  13239. /**
  13240. * \brief Return the elapsed time in milliseconds
  13241. *
  13242. * \param val points to a timer structure
  13243. * \param reset if set to 1, the timer is restarted
  13244. */
  13245. unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset );
  13246. /**
  13247. * \brief Setup an alarm clock
  13248. *
  13249. * \param seconds delay before the "mbedtls_timing_alarmed" flag is set
  13250. *
  13251. * \warning Only one alarm at a time is supported. In a threaded
  13252. * context, this means one for the whole process, not one per
  13253. * thread.
  13254. */
  13255. void mbedtls_set_alarm( int seconds );
  13256. /**
  13257. * \brief Set a pair of delays to watch
  13258. * (See \c mbedtls_timing_get_delay().)
  13259. *
  13260. * \param data Pointer to timing data
  13261. * Must point to a valid \c mbedtls_timing_delay_context struct.
  13262. * \param int_ms First (intermediate) delay in milliseconds.
  13263. * \param fin_ms Second (final) delay in milliseconds.
  13264. * Pass 0 to cancel the current delay.
  13265. */
  13266. void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms );
  13267. /**
  13268. * \brief Get the status of delays
  13269. * (Memory helper: number of delays passed.)
  13270. *
  13271. * \param data Pointer to timing data
  13272. * Must point to a valid \c mbedtls_timing_delay_context struct.
  13273. *
  13274. * \return -1 if cancelled (fin_ms = 0)
  13275. * 0 if none of the delays are passed,
  13276. * 1 if only the intermediate delay is passed,
  13277. * 2 if the final delay is passed.
  13278. */
  13279. int mbedtls_timing_get_delay( void *data );
  13280. #ifdef __cplusplus
  13281. }
  13282. #endif
  13283. #else /* MBEDTLS_TIMING_ALT */
  13284. #endif /* MBEDTLS_TIMING_ALT */
  13285. #ifdef __cplusplus
  13286. extern "C" {
  13287. #endif
  13288. #if defined(MBEDTLS_SELF_TEST)
  13289. /**
  13290. * \brief Checkup routine
  13291. *
  13292. * \return 0 if successful, or 1 if a test failed
  13293. */
  13294. int mbedtls_timing_self_test( int verbose );
  13295. #endif
  13296. #ifdef __cplusplus
  13297. }
  13298. #endif
  13299. #endif /* timing.h */
  13300. /********* Start of file include/mbedtls/xtea.h ************/
  13301. /**
  13302. * \file xtea.h
  13303. *
  13304. * \brief XTEA block cipher (32-bit)
  13305. *
  13306. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  13307. * SPDX-License-Identifier: Apache-2.0
  13308. *
  13309. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  13310. * not use this file except in compliance with the License.
  13311. * You may obtain a copy of the License at
  13312. *
  13313. * http://www.apache.org/licenses/LICENSE-2.0
  13314. *
  13315. * Unless required by applicable law or agreed to in writing, software
  13316. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13317. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13318. * See the License for the specific language governing permissions and
  13319. * limitations under the License.
  13320. *
  13321. * This file is part of mbed TLS (https://tls.mbed.org)
  13322. */
  13323. #ifndef MBEDTLS_XTEA_H
  13324. #define MBEDTLS_XTEA_H
  13325. #if !defined(MBEDTLS_CONFIG_FILE)
  13326. #else
  13327. #endif
  13328. #include <stddef.h>
  13329. #if !VXWORKS
  13330. #include <stdint.h>
  13331. #endif
  13332. #define MBEDTLS_XTEA_ENCRYPT 1
  13333. #define MBEDTLS_XTEA_DECRYPT 0
  13334. #define MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH -0x0028 /**< The data input has an invalid length. */
  13335. #if !defined(MBEDTLS_XTEA_ALT)
  13336. // Regular implementation
  13337. //
  13338. #ifdef __cplusplus
  13339. extern "C" {
  13340. #endif
  13341. /**
  13342. * \brief XTEA context structure
  13343. */
  13344. typedef struct
  13345. {
  13346. uint32_t k[4]; /*!< key */
  13347. }
  13348. mbedtls_xtea_context;
  13349. /**
  13350. * \brief Initialize XTEA context
  13351. *
  13352. * \param ctx XTEA context to be initialized
  13353. */
  13354. void mbedtls_xtea_init( mbedtls_xtea_context *ctx );
  13355. /**
  13356. * \brief Clear XTEA context
  13357. *
  13358. * \param ctx XTEA context to be cleared
  13359. */
  13360. void mbedtls_xtea_free( mbedtls_xtea_context *ctx );
  13361. /**
  13362. * \brief XTEA key schedule
  13363. *
  13364. * \param ctx XTEA context to be initialized
  13365. * \param key the secret key
  13366. */
  13367. void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] );
  13368. /**
  13369. * \brief XTEA cipher function
  13370. *
  13371. * \param ctx XTEA context
  13372. * \param mode MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT
  13373. * \param input 8-byte input block
  13374. * \param output 8-byte output block
  13375. *
  13376. * \return 0 if successful
  13377. */
  13378. int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx,
  13379. int mode,
  13380. const unsigned char input[8],
  13381. unsigned char output[8] );
  13382. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  13383. /**
  13384. * \brief XTEA CBC cipher function
  13385. *
  13386. * \param ctx XTEA context
  13387. * \param mode MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT
  13388. * \param length the length of input, multiple of 8
  13389. * \param iv initialization vector for CBC mode
  13390. * \param input input block
  13391. * \param output output block
  13392. *
  13393. * \return 0 if successful,
  13394. * MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH if the length % 8 != 0
  13395. */
  13396. int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx,
  13397. int mode,
  13398. size_t length,
  13399. unsigned char iv[8],
  13400. const unsigned char *input,
  13401. unsigned char *output);
  13402. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  13403. #ifdef __cplusplus
  13404. }
  13405. #endif
  13406. #else /* MBEDTLS_XTEA_ALT */
  13407. #endif /* MBEDTLS_XTEA_ALT */
  13408. #ifdef __cplusplus
  13409. extern "C" {
  13410. #endif
  13411. /**
  13412. * \brief Checkup routine
  13413. *
  13414. * \return 0 if successful, or 1 if the test failed
  13415. */
  13416. int mbedtls_xtea_self_test( int verbose );
  13417. #ifdef __cplusplus
  13418. }
  13419. #endif
  13420. #endif /* xtea.h */
  13421. /********* Start of file include/mbedtls/ssl.h ************/
  13422. /**
  13423. * \file ssl.h
  13424. *
  13425. * \brief SSL/TLS functions.
  13426. *
  13427. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  13428. * SPDX-License-Identifier: Apache-2.0
  13429. *
  13430. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  13431. * not use this file except in compliance with the License.
  13432. * You may obtain a copy of the License at
  13433. *
  13434. * http://www.apache.org/licenses/LICENSE-2.0
  13435. *
  13436. * Unless required by applicable law or agreed to in writing, software
  13437. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13438. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13439. * See the License for the specific language governing permissions and
  13440. * limitations under the License.
  13441. *
  13442. * This file is part of mbed TLS (https://tls.mbed.org)
  13443. */
  13444. #ifndef MBEDTLS_SSL_H
  13445. #define MBEDTLS_SSL_H
  13446. #if !defined(MBEDTLS_CONFIG_FILE)
  13447. #else
  13448. #endif
  13449. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  13450. #endif
  13451. #if defined(MBEDTLS_DHM_C)
  13452. #endif
  13453. #if defined(MBEDTLS_ECDH_C)
  13454. #endif
  13455. #if defined(MBEDTLS_ZLIB_SUPPORT)
  13456. #endif
  13457. #if defined(MBEDTLS_HAVE_TIME)
  13458. #include <time.h>
  13459. #endif
  13460. /*
  13461. * SSL Error codes
  13462. */
  13463. #define MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE -0x7080 /**< The requested feature is not available. */
  13464. #define MBEDTLS_ERR_SSL_BAD_INPUT_DATA -0x7100 /**< Bad input parameters to function. */
  13465. #define MBEDTLS_ERR_SSL_INVALID_MAC -0x7180 /**< Verification of the message MAC failed. */
  13466. #define MBEDTLS_ERR_SSL_INVALID_RECORD -0x7200 /**< An invalid SSL record was received. */
  13467. #define MBEDTLS_ERR_SSL_CONN_EOF -0x7280 /**< The connection indicated an EOF. */
  13468. #define MBEDTLS_ERR_SSL_UNKNOWN_CIPHER -0x7300 /**< An unknown cipher was received. */
  13469. #define MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN -0x7380 /**< The server has no ciphersuites in common with the client. */
  13470. #define MBEDTLS_ERR_SSL_NO_RNG -0x7400 /**< No RNG was provided to the SSL module. */
  13471. #define MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE -0x7480 /**< No client certification received from the client, but required by the authentication mode. */
  13472. #define MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE -0x7500 /**< Our own certificate(s) is/are too large to send in an SSL message. */
  13473. #define MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED -0x7580 /**< The own certificate is not set, but needed by the server. */
  13474. #define MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED -0x7600 /**< The own private key or pre-shared key is not set, but needed. */
  13475. #define MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED -0x7680 /**< No CA Chain is set, but required to operate. */
  13476. #define MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE -0x7700 /**< An unexpected message was received from our peer. */
  13477. #define MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE -0x7780 /**< A fatal alert message was received from our peer. */
  13478. #define MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED -0x7800 /**< Verification of our peer failed. */
  13479. #define MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY -0x7880 /**< The peer notified us that the connection is going to be closed. */
  13480. #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO -0x7900 /**< Processing of the ClientHello handshake message failed. */
  13481. #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO -0x7980 /**< Processing of the ServerHello handshake message failed. */
  13482. #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE -0x7A00 /**< Processing of the Certificate handshake message failed. */
  13483. #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST -0x7A80 /**< Processing of the CertificateRequest handshake message failed. */
  13484. #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE -0x7B00 /**< Processing of the ServerKeyExchange handshake message failed. */
  13485. #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE -0x7B80 /**< Processing of the ServerHelloDone handshake message failed. */
  13486. #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE -0x7C00 /**< Processing of the ClientKeyExchange handshake message failed. */
  13487. #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP -0x7C80 /**< Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Read Public. */
  13488. #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS -0x7D00 /**< Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Calculate Secret. */
  13489. #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY -0x7D80 /**< Processing of the CertificateVerify handshake message failed. */
  13490. #define MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC -0x7E00 /**< Processing of the ChangeCipherSpec handshake message failed. */
  13491. #define MBEDTLS_ERR_SSL_BAD_HS_FINISHED -0x7E80 /**< Processing of the Finished handshake message failed. */
  13492. #define MBEDTLS_ERR_SSL_ALLOC_FAILED -0x7F00 /**< Memory allocation failed */
  13493. #define MBEDTLS_ERR_SSL_HW_ACCEL_FAILED -0x7F80 /**< Hardware acceleration function returned with error */
  13494. #define MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH -0x6F80 /**< Hardware acceleration function skipped / left alone data */
  13495. #define MBEDTLS_ERR_SSL_COMPRESSION_FAILED -0x6F00 /**< Processing of the compression / decompression failed */
  13496. #define MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION -0x6E80 /**< Handshake protocol not within min/max boundaries */
  13497. #define MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET -0x6E00 /**< Processing of the NewSessionTicket handshake message failed. */
  13498. #define MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED -0x6D80 /**< Session ticket has expired. */
  13499. #define MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH -0x6D00 /**< Public key type mismatch (eg, asked for RSA key exchange and presented EC key) */
  13500. #define MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY -0x6C80 /**< Unknown identity received (eg, PSK identity) */
  13501. #define MBEDTLS_ERR_SSL_INTERNAL_ERROR -0x6C00 /**< Internal error (eg, unexpected failure in lower-level module) */
  13502. #define MBEDTLS_ERR_SSL_COUNTER_WRAPPING -0x6B80 /**< A counter would wrap (eg, too many messages exchanged). */
  13503. #define MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO -0x6B00 /**< Unexpected message at ServerHello in renegotiation. */
  13504. #define MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED -0x6A80 /**< DTLS client must retry for hello verification */
  13505. #define MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL -0x6A00 /**< A buffer is too small to receive or write a message */
  13506. #define MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE -0x6980 /**< None of the common ciphersuites is usable (eg, no suitable certificate, see debug messages). */
  13507. #define MBEDTLS_ERR_SSL_WANT_READ -0x6900 /**< Connection requires a read call. */
  13508. #define MBEDTLS_ERR_SSL_WANT_WRITE -0x6880 /**< Connection requires a write call. */
  13509. #define MBEDTLS_ERR_SSL_TIMEOUT -0x6800 /**< The operation timed out. */
  13510. #define MBEDTLS_ERR_SSL_CLIENT_RECONNECT -0x6780 /**< The client initiated a reconnect from the same port. */
  13511. #define MBEDTLS_ERR_SSL_UNEXPECTED_RECORD -0x6700 /**< Record header looks valid but is not expected. */
  13512. /*
  13513. * Various constants
  13514. */
  13515. #define MBEDTLS_SSL_MAJOR_VERSION_3 3
  13516. #define MBEDTLS_SSL_MINOR_VERSION_0 0 /*!< SSL v3.0 */
  13517. #define MBEDTLS_SSL_MINOR_VERSION_1 1 /*!< TLS v1.0 */
  13518. #define MBEDTLS_SSL_MINOR_VERSION_2 2 /*!< TLS v1.1 */
  13519. #define MBEDTLS_SSL_MINOR_VERSION_3 3 /*!< TLS v1.2 */
  13520. #define MBEDTLS_SSL_TRANSPORT_STREAM 0 /*!< TLS */
  13521. #define MBEDTLS_SSL_TRANSPORT_DATAGRAM 1 /*!< DTLS */
  13522. #define MBEDTLS_SSL_MAX_HOST_NAME_LEN 255 /*!< Maximum host name defined in RFC 1035 */
  13523. /* RFC 6066 section 4, see also mfl_code_to_length in ssl_tls.c
  13524. * NONE must be zero so that memset()ing structure to zero works */
  13525. #define MBEDTLS_SSL_MAX_FRAG_LEN_NONE 0 /*!< don't use this extension */
  13526. #define MBEDTLS_SSL_MAX_FRAG_LEN_512 1 /*!< MaxFragmentLength 2^9 */
  13527. #define MBEDTLS_SSL_MAX_FRAG_LEN_1024 2 /*!< MaxFragmentLength 2^10 */
  13528. #define MBEDTLS_SSL_MAX_FRAG_LEN_2048 3 /*!< MaxFragmentLength 2^11 */
  13529. #define MBEDTLS_SSL_MAX_FRAG_LEN_4096 4 /*!< MaxFragmentLength 2^12 */
  13530. #define MBEDTLS_SSL_MAX_FRAG_LEN_INVALID 5 /*!< first invalid value */
  13531. #define MBEDTLS_SSL_IS_CLIENT 0
  13532. #define MBEDTLS_SSL_IS_SERVER 1
  13533. #define MBEDTLS_SSL_IS_NOT_FALLBACK 0
  13534. #define MBEDTLS_SSL_IS_FALLBACK 1
  13535. #define MBEDTLS_SSL_EXTENDED_MS_DISABLED 0
  13536. #define MBEDTLS_SSL_EXTENDED_MS_ENABLED 1
  13537. #define MBEDTLS_SSL_ETM_DISABLED 0
  13538. #define MBEDTLS_SSL_ETM_ENABLED 1
  13539. #define MBEDTLS_SSL_COMPRESS_NULL 0
  13540. #define MBEDTLS_SSL_COMPRESS_DEFLATE 1
  13541. #define MBEDTLS_SSL_VERIFY_NONE 0
  13542. #define MBEDTLS_SSL_VERIFY_OPTIONAL 1
  13543. #define MBEDTLS_SSL_VERIFY_REQUIRED 2
  13544. #define MBEDTLS_SSL_VERIFY_UNSET 3 /* Used only for sni_authmode */
  13545. #define MBEDTLS_SSL_LEGACY_RENEGOTIATION 0
  13546. #define MBEDTLS_SSL_SECURE_RENEGOTIATION 1
  13547. #define MBEDTLS_SSL_RENEGOTIATION_DISABLED 0
  13548. #define MBEDTLS_SSL_RENEGOTIATION_ENABLED 1
  13549. #define MBEDTLS_SSL_ANTI_REPLAY_DISABLED 0
  13550. #define MBEDTLS_SSL_ANTI_REPLAY_ENABLED 1
  13551. #define MBEDTLS_SSL_RENEGOTIATION_NOT_ENFORCED -1
  13552. #define MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT 16
  13553. #define MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION 0
  13554. #define MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION 1
  13555. #define MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE 2
  13556. #define MBEDTLS_SSL_TRUNC_HMAC_DISABLED 0
  13557. #define MBEDTLS_SSL_TRUNC_HMAC_ENABLED 1
  13558. #define MBEDTLS_SSL_TRUNCATED_HMAC_LEN 10 /* 80 bits, rfc 6066 section 7 */
  13559. #define MBEDTLS_SSL_SESSION_TICKETS_DISABLED 0
  13560. #define MBEDTLS_SSL_SESSION_TICKETS_ENABLED 1
  13561. #define MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED 0
  13562. #define MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED 1
  13563. #define MBEDTLS_SSL_ARC4_ENABLED 0
  13564. #define MBEDTLS_SSL_ARC4_DISABLED 1
  13565. #define MBEDTLS_SSL_PRESET_DEFAULT 0
  13566. #define MBEDTLS_SSL_PRESET_SUITEB 2
  13567. /*
  13568. * Default range for DTLS retransmission timer value, in milliseconds.
  13569. * RFC 6347 4.2.4.1 says from 1 second to 60 seconds.
  13570. */
  13571. #define MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN 1000
  13572. #define MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX 60000
  13573. /**
  13574. * \name SECTION: Module settings
  13575. *
  13576. * The configuration options you can set for this module are in this section.
  13577. * Either change them in config.h or define them on the compiler command line.
  13578. * \{
  13579. */
  13580. #if !defined(MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME)
  13581. #define MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME 86400 /**< Lifetime of session tickets (if enabled) */
  13582. #endif
  13583. /*
  13584. * Maxium fragment length in bytes,
  13585. * determines the size of each of the two internal I/O buffers.
  13586. *
  13587. * Note: the RFC defines the default size of SSL / TLS messages. If you
  13588. * change the value here, other clients / servers may not be able to
  13589. * communicate with you anymore. Only change this value if you control
  13590. * both sides of the connection and have it reduced at both sides, or
  13591. * if you're using the Max Fragment Length extension and you know all your
  13592. * peers are using it too!
  13593. */
  13594. #if !defined(MBEDTLS_SSL_MAX_CONTENT_LEN)
  13595. #define MBEDTLS_SSL_MAX_CONTENT_LEN 16384 /**< Size of the input / output buffer */
  13596. #endif
  13597. /* \} name SECTION: Module settings */
  13598. /*
  13599. * Length of the verify data for secure renegotiation
  13600. */
  13601. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  13602. #define MBEDTLS_SSL_VERIFY_DATA_MAX_LEN 36
  13603. #else
  13604. #define MBEDTLS_SSL_VERIFY_DATA_MAX_LEN 12
  13605. #endif
  13606. /*
  13607. * Signaling ciphersuite values (SCSV)
  13608. */
  13609. #define MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO 0xFF /**< renegotiation info ext */
  13610. #define MBEDTLS_SSL_FALLBACK_SCSV_VALUE 0x5600 /**< draft-ietf-tls-downgrade-scsv-00 */
  13611. /*
  13612. * Supported Signature and Hash algorithms (For TLS 1.2)
  13613. * RFC 5246 section 7.4.1.4.1
  13614. */
  13615. #define MBEDTLS_SSL_HASH_NONE 0
  13616. #define MBEDTLS_SSL_HASH_MD5 1
  13617. #define MBEDTLS_SSL_HASH_SHA1 2
  13618. #define MBEDTLS_SSL_HASH_SHA224 3
  13619. #define MBEDTLS_SSL_HASH_SHA256 4
  13620. #define MBEDTLS_SSL_HASH_SHA384 5
  13621. #define MBEDTLS_SSL_HASH_SHA512 6
  13622. #define MBEDTLS_SSL_SIG_ANON 0
  13623. #define MBEDTLS_SSL_SIG_RSA 1
  13624. #define MBEDTLS_SSL_SIG_ECDSA 3
  13625. /*
  13626. * Client Certificate Types
  13627. * RFC 5246 section 7.4.4 plus RFC 4492 section 5.5
  13628. */
  13629. #define MBEDTLS_SSL_CERT_TYPE_RSA_SIGN 1
  13630. #define MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN 64
  13631. /*
  13632. * Message, alert and handshake types
  13633. */
  13634. #define MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC 20
  13635. #define MBEDTLS_SSL_MSG_ALERT 21
  13636. #define MBEDTLS_SSL_MSG_HANDSHAKE 22
  13637. #define MBEDTLS_SSL_MSG_APPLICATION_DATA 23
  13638. #define MBEDTLS_SSL_ALERT_LEVEL_WARNING 1
  13639. #define MBEDTLS_SSL_ALERT_LEVEL_FATAL 2
  13640. #define MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY 0 /* 0x00 */
  13641. #define MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE 10 /* 0x0A */
  13642. #define MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC 20 /* 0x14 */
  13643. #define MBEDTLS_SSL_ALERT_MSG_DECRYPTION_FAILED 21 /* 0x15 */
  13644. #define MBEDTLS_SSL_ALERT_MSG_RECORD_OVERFLOW 22 /* 0x16 */
  13645. #define MBEDTLS_SSL_ALERT_MSG_DECOMPRESSION_FAILURE 30 /* 0x1E */
  13646. #define MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE 40 /* 0x28 */
  13647. #define MBEDTLS_SSL_ALERT_MSG_NO_CERT 41 /* 0x29 */
  13648. #define MBEDTLS_SSL_ALERT_MSG_BAD_CERT 42 /* 0x2A */
  13649. #define MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT 43 /* 0x2B */
  13650. #define MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED 44 /* 0x2C */
  13651. #define MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED 45 /* 0x2D */
  13652. #define MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN 46 /* 0x2E */
  13653. #define MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER 47 /* 0x2F */
  13654. #define MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA 48 /* 0x30 */
  13655. #define MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED 49 /* 0x31 */
  13656. #define MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR 50 /* 0x32 */
  13657. #define MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR 51 /* 0x33 */
  13658. #define MBEDTLS_SSL_ALERT_MSG_EXPORT_RESTRICTION 60 /* 0x3C */
  13659. #define MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION 70 /* 0x46 */
  13660. #define MBEDTLS_SSL_ALERT_MSG_INSUFFICIENT_SECURITY 71 /* 0x47 */
  13661. #define MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR 80 /* 0x50 */
  13662. #define MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK 86 /* 0x56 */
  13663. #define MBEDTLS_SSL_ALERT_MSG_USER_CANCELED 90 /* 0x5A */
  13664. #define MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION 100 /* 0x64 */
  13665. #define MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT 110 /* 0x6E */
  13666. #define MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME 112 /* 0x70 */
  13667. #define MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY 115 /* 0x73 */
  13668. #define MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL 120 /* 0x78 */
  13669. #define MBEDTLS_SSL_HS_HELLO_REQUEST 0
  13670. #define MBEDTLS_SSL_HS_CLIENT_HELLO 1
  13671. #define MBEDTLS_SSL_HS_SERVER_HELLO 2
  13672. #define MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST 3
  13673. #define MBEDTLS_SSL_HS_NEW_SESSION_TICKET 4
  13674. #define MBEDTLS_SSL_HS_CERTIFICATE 11
  13675. #define MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE 12
  13676. #define MBEDTLS_SSL_HS_CERTIFICATE_REQUEST 13
  13677. #define MBEDTLS_SSL_HS_SERVER_HELLO_DONE 14
  13678. #define MBEDTLS_SSL_HS_CERTIFICATE_VERIFY 15
  13679. #define MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE 16
  13680. #define MBEDTLS_SSL_HS_FINISHED 20
  13681. /*
  13682. * TLS extensions
  13683. */
  13684. #define MBEDTLS_TLS_EXT_SERVERNAME 0
  13685. #define MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME 0
  13686. #define MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH 1
  13687. #define MBEDTLS_TLS_EXT_TRUNCATED_HMAC 4
  13688. #define MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES 10
  13689. #define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS 11
  13690. #define MBEDTLS_TLS_EXT_SIG_ALG 13
  13691. #define MBEDTLS_TLS_EXT_ALPN 16
  13692. #define MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC 22 /* 0x16 */
  13693. #define MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET 0x0017 /* 23 */
  13694. #define MBEDTLS_TLS_EXT_SESSION_TICKET 35
  13695. #define MBEDTLS_TLS_EXT_ECJPAKE_KKPP 256 /* experimental */
  13696. #define MBEDTLS_TLS_EXT_RENEGOTIATION_INFO 0xFF01
  13697. /*
  13698. * Size defines
  13699. */
  13700. #if !defined(MBEDTLS_PSK_MAX_LEN)
  13701. #define MBEDTLS_PSK_MAX_LEN 32 /* 256 bits */
  13702. #endif
  13703. /* Dummy type used only for its size */
  13704. union mbedtls_ssl_premaster_secret
  13705. {
  13706. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  13707. unsigned char _pms_rsa[48]; /* RFC 5246 8.1.1 */
  13708. #endif
  13709. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
  13710. unsigned char _pms_dhm[MBEDTLS_MPI_MAX_SIZE]; /* RFC 5246 8.1.2 */
  13711. #endif
  13712. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  13713. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  13714. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  13715. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  13716. unsigned char _pms_ecdh[MBEDTLS_ECP_MAX_BYTES]; /* RFC 4492 5.10 */
  13717. #endif
  13718. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  13719. unsigned char _pms_psk[4 + 2 * MBEDTLS_PSK_MAX_LEN]; /* RFC 4279 2 */
  13720. #endif
  13721. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  13722. unsigned char _pms_dhe_psk[4 + MBEDTLS_MPI_MAX_SIZE
  13723. + MBEDTLS_PSK_MAX_LEN]; /* RFC 4279 3 */
  13724. #endif
  13725. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  13726. unsigned char _pms_rsa_psk[52 + MBEDTLS_PSK_MAX_LEN]; /* RFC 4279 4 */
  13727. #endif
  13728. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  13729. unsigned char _pms_ecdhe_psk[4 + MBEDTLS_ECP_MAX_BYTES
  13730. + MBEDTLS_PSK_MAX_LEN]; /* RFC 5489 2 */
  13731. #endif
  13732. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  13733. unsigned char _pms_ecjpake[32]; /* Thread spec: SHA-256 output */
  13734. #endif
  13735. };
  13736. #define MBEDTLS_PREMASTER_SIZE sizeof( union mbedtls_ssl_premaster_secret )
  13737. #ifdef __cplusplus
  13738. extern "C" {
  13739. #endif
  13740. /*
  13741. * SSL state machine
  13742. */
  13743. typedef enum
  13744. {
  13745. MBEDTLS_SSL_HELLO_REQUEST,
  13746. MBEDTLS_SSL_CLIENT_HELLO,
  13747. MBEDTLS_SSL_SERVER_HELLO,
  13748. MBEDTLS_SSL_SERVER_CERTIFICATE,
  13749. MBEDTLS_SSL_SERVER_KEY_EXCHANGE,
  13750. MBEDTLS_SSL_CERTIFICATE_REQUEST,
  13751. MBEDTLS_SSL_SERVER_HELLO_DONE,
  13752. MBEDTLS_SSL_CLIENT_CERTIFICATE,
  13753. MBEDTLS_SSL_CLIENT_KEY_EXCHANGE,
  13754. MBEDTLS_SSL_CERTIFICATE_VERIFY,
  13755. MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC,
  13756. MBEDTLS_SSL_CLIENT_FINISHED,
  13757. MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC,
  13758. MBEDTLS_SSL_SERVER_FINISHED,
  13759. MBEDTLS_SSL_FLUSH_BUFFERS,
  13760. MBEDTLS_SSL_HANDSHAKE_WRAPUP,
  13761. MBEDTLS_SSL_HANDSHAKE_OVER,
  13762. MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET,
  13763. MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT,
  13764. }
  13765. mbedtls_ssl_states;
  13766. /* Defined below */
  13767. typedef struct mbedtls_ssl_session mbedtls_ssl_session;
  13768. typedef struct mbedtls_ssl_context mbedtls_ssl_context;
  13769. typedef struct mbedtls_ssl_config mbedtls_ssl_config;
  13770. /* Defined in ssl_internal.h */
  13771. typedef struct mbedtls_ssl_transform mbedtls_ssl_transform;
  13772. typedef struct mbedtls_ssl_handshake_params mbedtls_ssl_handshake_params;
  13773. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  13774. typedef struct mbedtls_ssl_key_cert mbedtls_ssl_key_cert;
  13775. #endif
  13776. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  13777. typedef struct mbedtls_ssl_flight_item mbedtls_ssl_flight_item;
  13778. #endif
  13779. /*
  13780. * This structure is used for storing current session data.
  13781. */
  13782. struct mbedtls_ssl_session
  13783. {
  13784. #if defined(MBEDTLS_HAVE_TIME)
  13785. time_t start; /*!< starting time */
  13786. #endif
  13787. int ciphersuite; /*!< chosen ciphersuite */
  13788. int compression; /*!< chosen compression */
  13789. size_t id_len; /*!< session id length */
  13790. unsigned char id[32]; /*!< session identifier */
  13791. unsigned char master[48]; /*!< the master secret */
  13792. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  13793. mbedtls_x509_crt *peer_cert; /*!< peer X.509 cert chain */
  13794. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  13795. uint32_t verify_result; /*!< verification result */
  13796. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
  13797. unsigned char *ticket; /*!< RFC 5077 session ticket */
  13798. size_t ticket_len; /*!< session ticket length */
  13799. uint32_t ticket_lifetime; /*!< ticket lifetime hint */
  13800. #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
  13801. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  13802. unsigned char mfl_code; /*!< MaxFragmentLength negotiated by peer */
  13803. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  13804. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  13805. int trunc_hmac; /*!< flag for truncated hmac activation */
  13806. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  13807. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  13808. int encrypt_then_mac; /*!< flag for EtM activation */
  13809. #endif
  13810. };
  13811. /**
  13812. * SSL/TLS configuration to be shared between mbedtls_ssl_context structures.
  13813. */
  13814. struct mbedtls_ssl_config
  13815. {
  13816. /* Group items by size (largest first) to minimize padding overhead */
  13817. /*
  13818. * Pointers
  13819. */
  13820. const int *ciphersuite_list[4]; /*!< allowed ciphersuites per version */
  13821. /** Callback for printing debug output */
  13822. void (*f_dbg)(void *, int, const char *, int, const char *);
  13823. void *p_dbg; /*!< context for the debug function */
  13824. /** Callback for getting (pseudo-)random numbers */
  13825. int (*f_rng)(void *, unsigned char *, size_t);
  13826. void *p_rng; /*!< context for the RNG function */
  13827. /** Callback to retrieve a session from the cache */
  13828. int (*f_get_cache)(void *, mbedtls_ssl_session *);
  13829. /** Callback to store a session into the cache */
  13830. int (*f_set_cache)(void *, const mbedtls_ssl_session *);
  13831. void *p_cache; /*!< context for cache callbacks */
  13832. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  13833. /** Callback for setting cert according to SNI extension */
  13834. int (*f_sni)(void *, mbedtls_ssl_context *, const unsigned char *, size_t);
  13835. void *p_sni; /*!< context for SNI callback */
  13836. #endif
  13837. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  13838. /** Callback to customize X.509 certificate chain verification */
  13839. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
  13840. void *p_vrfy; /*!< context for X.509 verify calllback */
  13841. #endif
  13842. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  13843. /** Callback to retrieve PSK key from identity */
  13844. int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, size_t);
  13845. void *p_psk; /*!< context for PSK callback */
  13846. #endif
  13847. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
  13848. /** Callback to create & write a cookie for ClientHello veirifcation */
  13849. int (*f_cookie_write)( void *, unsigned char **, unsigned char *,
  13850. const unsigned char *, size_t );
  13851. /** Callback to verify validity of a ClientHello cookie */
  13852. int (*f_cookie_check)( void *, const unsigned char *, size_t,
  13853. const unsigned char *, size_t );
  13854. void *p_cookie; /*!< context for the cookie callbacks */
  13855. #endif
  13856. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_SRV_C)
  13857. /** Callback to create & write a session ticket */
  13858. int (*f_ticket_write)( void *, const mbedtls_ssl_session *,
  13859. unsigned char *, const unsigned char *, size_t *, uint32_t * );
  13860. /** Callback to parse a session ticket into a session structure */
  13861. int (*f_ticket_parse)( void *, mbedtls_ssl_session *, unsigned char *, size_t);
  13862. void *p_ticket; /*!< context for the ticket callbacks */
  13863. #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_SRV_C */
  13864. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  13865. /** Callback to export key block and master secret */
  13866. int (*f_export_keys)( void *, const unsigned char *,
  13867. const unsigned char *, size_t, size_t, size_t );
  13868. void *p_export_keys; /*!< context for key export callback */
  13869. #endif
  13870. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  13871. const mbedtls_x509_crt_profile *cert_profile; /*!< verification profile */
  13872. mbedtls_ssl_key_cert *key_cert; /*!< own certificate/key pair(s) */
  13873. mbedtls_x509_crt *ca_chain; /*!< trusted CAs */
  13874. mbedtls_x509_crl *ca_crl; /*!< trusted CAs CRLs */
  13875. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  13876. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  13877. const int *sig_hashes; /*!< allowed signature hashes */
  13878. #endif
  13879. #if defined(MBEDTLS_ECP_C)
  13880. const mbedtls_ecp_group_id *curve_list; /*!< allowed curves */
  13881. #endif
  13882. #if defined(MBEDTLS_DHM_C)
  13883. mbedtls_mpi dhm_P; /*!< prime modulus for DHM */
  13884. mbedtls_mpi dhm_G; /*!< generator for DHM */
  13885. #endif
  13886. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  13887. unsigned char *psk; /*!< pre-shared key */
  13888. size_t psk_len; /*!< length of the pre-shared key */
  13889. unsigned char *psk_identity; /*!< identity for PSK negotiation */
  13890. size_t psk_identity_len;/*!< length of identity */
  13891. #endif
  13892. #if defined(MBEDTLS_SSL_ALPN)
  13893. const char **alpn_list; /*!< ordered list of protocols */
  13894. #endif
  13895. /*
  13896. * Numerical settings (int then char)
  13897. */
  13898. uint32_t read_timeout; /*!< timeout for mbedtls_ssl_read (ms) */
  13899. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  13900. uint32_t hs_timeout_min; /*!< initial value of the handshake
  13901. retransmission timeout (ms) */
  13902. uint32_t hs_timeout_max; /*!< maximum value of the handshake
  13903. retransmission timeout (ms) */
  13904. #endif
  13905. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  13906. int renego_max_records; /*!< grace period for renegotiation */
  13907. unsigned char renego_period[8]; /*!< value of the record counters
  13908. that triggers renegotiation */
  13909. #endif
  13910. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  13911. unsigned int badmac_limit; /*!< limit of records with a bad MAC */
  13912. #endif
  13913. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
  13914. unsigned int dhm_min_bitlen; /*!< min. bit length of the DHM prime */
  13915. #endif
  13916. unsigned char max_major_ver; /*!< max. major version used */
  13917. unsigned char max_minor_ver; /*!< max. minor version used */
  13918. unsigned char min_major_ver; /*!< min. major version used */
  13919. unsigned char min_minor_ver; /*!< min. minor version used */
  13920. /*
  13921. * Flags (bitfields)
  13922. */
  13923. unsigned int endpoint : 1; /*!< 0: client, 1: server */
  13924. unsigned int transport : 1; /*!< stream (TLS) or datagram (DTLS) */
  13925. unsigned int authmode : 2; /*!< MBEDTLS_SSL_VERIFY_XXX */
  13926. /* needed even with renego disabled for LEGACY_BREAK_HANDSHAKE */
  13927. unsigned int allow_legacy_renegotiation : 2 ; /*!< MBEDTLS_LEGACY_XXX */
  13928. #if defined(MBEDTLS_ARC4_C)
  13929. unsigned int arc4_disabled : 1; /*!< blacklist RC4 ciphersuites? */
  13930. #endif
  13931. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  13932. unsigned int mfl_code : 3; /*!< desired fragment length */
  13933. #endif
  13934. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  13935. unsigned int encrypt_then_mac : 1 ; /*!< negotiate encrypt-then-mac? */
  13936. #endif
  13937. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  13938. unsigned int extended_ms : 1; /*!< negotiate extended master secret? */
  13939. #endif
  13940. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  13941. unsigned int anti_replay : 1; /*!< detect and prevent replay? */
  13942. #endif
  13943. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  13944. unsigned int cbc_record_splitting : 1; /*!< do cbc record splitting */
  13945. #endif
  13946. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  13947. unsigned int disable_renegotiation : 1; /*!< disable renegotiation? */
  13948. #endif
  13949. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  13950. unsigned int trunc_hmac : 1; /*!< negotiate truncated hmac? */
  13951. #endif
  13952. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  13953. unsigned int session_tickets : 1; /*!< use session tickets? */
  13954. #endif
  13955. #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
  13956. unsigned int fallback : 1; /*!< is this a fallback? */
  13957. #endif
  13958. };
  13959. struct mbedtls_ssl_context
  13960. {
  13961. const mbedtls_ssl_config *conf; /*!< configuration information */
  13962. /*
  13963. * Miscellaneous
  13964. */
  13965. int state; /*!< SSL handshake: current state */
  13966. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  13967. int renego_status; /*!< Initial, in progress, pending? */
  13968. int renego_records_seen; /*!< Records since renego request, or with DTLS,
  13969. number of retransmissions of request if
  13970. renego_max_records is < 0 */
  13971. #endif
  13972. int major_ver; /*!< equal to MBEDTLS_SSL_MAJOR_VERSION_3 */
  13973. int minor_ver; /*!< either 0 (SSL3) or 1 (TLS1.0) */
  13974. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  13975. unsigned badmac_seen; /*!< records with a bad MAC received */
  13976. #endif
  13977. /*
  13978. * Callbacks
  13979. */
  13980. int (*f_send)(void *, const unsigned char *, size_t);
  13981. int (*f_recv)(void *, unsigned char *, size_t);
  13982. int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t);
  13983. void *p_bio; /*!< context for I/O operations */
  13984. /*
  13985. * Session layer
  13986. */
  13987. mbedtls_ssl_session *session_in; /*!< current session data (in) */
  13988. mbedtls_ssl_session *session_out; /*!< current session data (out) */
  13989. mbedtls_ssl_session *session; /*!< negotiated session data */
  13990. mbedtls_ssl_session *session_negotiate; /*!< session data in negotiation */
  13991. mbedtls_ssl_handshake_params *handshake; /*!< params required only during
  13992. the handshake process */
  13993. /*
  13994. * Record layer transformations
  13995. */
  13996. mbedtls_ssl_transform *transform_in; /*!< current transform params (in) */
  13997. mbedtls_ssl_transform *transform_out; /*!< current transform params (in) */
  13998. mbedtls_ssl_transform *transform; /*!< negotiated transform params */
  13999. mbedtls_ssl_transform *transform_negotiate; /*!< transform params in negotiation */
  14000. /*
  14001. * Timers
  14002. */
  14003. void *p_timer; /*!< context for the timer callbacks */
  14004. void (*f_set_timer)(void *, uint32_t, uint32_t); /*!< set timer callback */
  14005. int (*f_get_timer)(void *); /*!< get timer callback */
  14006. /*
  14007. * Record layer (incoming data)
  14008. */
  14009. unsigned char *in_buf; /*!< input buffer */
  14010. unsigned char *in_ctr; /*!< 64-bit incoming message counter
  14011. TLS: maintained by us
  14012. DTLS: read from peer */
  14013. unsigned char *in_hdr; /*!< start of record header */
  14014. unsigned char *in_len; /*!< two-bytes message length field */
  14015. unsigned char *in_iv; /*!< ivlen-byte IV */
  14016. unsigned char *in_msg; /*!< message contents (in_iv+ivlen) */
  14017. unsigned char *in_offt; /*!< read offset in application data */
  14018. int in_msgtype; /*!< record header: message type */
  14019. size_t in_msglen; /*!< record header: message length */
  14020. size_t in_left; /*!< amount of data read so far */
  14021. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  14022. uint16_t in_epoch; /*!< DTLS epoch for incoming records */
  14023. size_t next_record_offset; /*!< offset of the next record in datagram
  14024. (equal to in_left if none) */
  14025. #endif
  14026. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  14027. uint64_t in_window_top; /*!< last validated record seq_num */
  14028. uint64_t in_window; /*!< bitmask for replay detection */
  14029. #endif
  14030. size_t in_hslen; /*!< current handshake message length,
  14031. including the handshake header */
  14032. int nb_zero; /*!< # of 0-length encrypted messages */
  14033. int record_read; /*!< record is already present */
  14034. /*
  14035. * Record layer (outgoing data)
  14036. */
  14037. unsigned char *out_buf; /*!< output buffer */
  14038. unsigned char *out_ctr; /*!< 64-bit outgoing message counter */
  14039. unsigned char *out_hdr; /*!< start of record header */
  14040. unsigned char *out_len; /*!< two-bytes message length field */
  14041. unsigned char *out_iv; /*!< ivlen-byte IV */
  14042. unsigned char *out_msg; /*!< message contents (out_iv+ivlen) */
  14043. int out_msgtype; /*!< record header: message type */
  14044. size_t out_msglen; /*!< record header: message length */
  14045. size_t out_left; /*!< amount of data not yet written */
  14046. #if defined(MBEDTLS_ZLIB_SUPPORT)
  14047. unsigned char *compress_buf; /*!< zlib data buffer */
  14048. #endif
  14049. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  14050. signed char split_done; /*!< current record already splitted? */
  14051. #endif
  14052. /*
  14053. * PKI layer
  14054. */
  14055. int client_auth; /*!< flag for client auth. */
  14056. /*
  14057. * User settings
  14058. */
  14059. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  14060. char *hostname; /*!< expected peer CN for verification
  14061. (and SNI if available) */
  14062. #endif
  14063. #if defined(MBEDTLS_SSL_ALPN)
  14064. const char *alpn_chosen; /*!< negotiated protocol */
  14065. #endif
  14066. /*
  14067. * Information for DTLS hello verify
  14068. */
  14069. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
  14070. unsigned char *cli_id; /*!< transport-level ID of the client */
  14071. size_t cli_id_len; /*!< length of cli_id */
  14072. #endif
  14073. /*
  14074. * Secure renegotiation
  14075. */
  14076. /* needed to know when to send extension on server */
  14077. int secure_renegotiation; /*!< does peer support legacy or
  14078. secure renegotiation */
  14079. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  14080. size_t verify_data_len; /*!< length of verify data stored */
  14081. char own_verify_data[MBEDTLS_SSL_VERIFY_DATA_MAX_LEN]; /*!< previous handshake verify data */
  14082. char peer_verify_data[MBEDTLS_SSL_VERIFY_DATA_MAX_LEN]; /*!< previous handshake verify data */
  14083. #endif
  14084. void *appData;
  14085. };
  14086. #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
  14087. #define MBEDTLS_SSL_CHANNEL_OUTBOUND 0
  14088. #define MBEDTLS_SSL_CHANNEL_INBOUND 1
  14089. extern int (*mbedtls_ssl_hw_record_init)(mbedtls_ssl_context *ssl,
  14090. const unsigned char *key_enc, const unsigned char *key_dec,
  14091. size_t keylen,
  14092. const unsigned char *iv_enc, const unsigned char *iv_dec,
  14093. size_t ivlen,
  14094. const unsigned char *mac_enc, const unsigned char *mac_dec,
  14095. size_t maclen);
  14096. extern int (*mbedtls_ssl_hw_record_activate)(mbedtls_ssl_context *ssl, int direction);
  14097. extern int (*mbedtls_ssl_hw_record_reset)(mbedtls_ssl_context *ssl);
  14098. extern int (*mbedtls_ssl_hw_record_write)(mbedtls_ssl_context *ssl);
  14099. extern int (*mbedtls_ssl_hw_record_read)(mbedtls_ssl_context *ssl);
  14100. extern int (*mbedtls_ssl_hw_record_finish)(mbedtls_ssl_context *ssl);
  14101. #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
  14102. /**
  14103. * \brief Returns the list of ciphersuites supported by the SSL/TLS module.
  14104. *
  14105. * \return a statically allocated array of ciphersuites, the last
  14106. * entry is 0.
  14107. */
  14108. const int *mbedtls_ssl_list_ciphersuites( void );
  14109. /**
  14110. * \brief Return the name of the ciphersuite associated with the
  14111. * given ID
  14112. *
  14113. * \param ciphersuite_id SSL ciphersuite ID
  14114. *
  14115. * \return a string containing the ciphersuite name
  14116. */
  14117. const char *mbedtls_ssl_get_ciphersuite_name( const int ciphersuite_id );
  14118. /**
  14119. * \brief Return the ID of the ciphersuite associated with the
  14120. * given name
  14121. *
  14122. * \param ciphersuite_name SSL ciphersuite name
  14123. *
  14124. * \return the ID with the ciphersuite or 0 if not found
  14125. */
  14126. int mbedtls_ssl_get_ciphersuite_id( const char *ciphersuite_name );
  14127. /**
  14128. * \brief Initialize an SSL context
  14129. * Just makes the context ready for mbedtls_ssl_setup() or
  14130. * mbedtls_ssl_free()
  14131. *
  14132. * \param ssl SSL context
  14133. */
  14134. void mbedtls_ssl_init( mbedtls_ssl_context *ssl );
  14135. /**
  14136. * \brief Set up an SSL context for use
  14137. *
  14138. * \note No copy of the configuration context is made, it can be
  14139. * shared by many mbedtls_ssl_context structures.
  14140. *
  14141. * \warning Modifying the conf structure after is has been used in this
  14142. * function is unsupported!
  14143. *
  14144. * \param ssl SSL context
  14145. * \param conf SSL configuration to use
  14146. *
  14147. * \return 0 if successful, or MBEDTLS_ERR_SSL_ALLOC_FAILED if
  14148. * memory allocation failed
  14149. */
  14150. int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
  14151. const mbedtls_ssl_config *conf );
  14152. /**
  14153. * \brief Reset an already initialized SSL context for re-use
  14154. * while retaining application-set variables, function
  14155. * pointers and data.
  14156. *
  14157. * \param ssl SSL context
  14158. * \return 0 if successful, or POLASSL_ERR_SSL_MALLOC_FAILED,
  14159. MBEDTLS_ERR_SSL_HW_ACCEL_FAILED or
  14160. * MBEDTLS_ERR_SSL_COMPRESSION_FAILED
  14161. */
  14162. int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl );
  14163. /**
  14164. * \brief Set the current endpoint type
  14165. *
  14166. * \param conf SSL configuration
  14167. * \param endpoint must be MBEDTLS_SSL_IS_CLIENT or MBEDTLS_SSL_IS_SERVER
  14168. */
  14169. void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint );
  14170. /**
  14171. * \brief Set the transport type (TLS or DTLS).
  14172. * Default: TLS
  14173. *
  14174. * \note For DTLS, you must either provide a recv callback that
  14175. * doesn't block, or one that handles timeouts, see
  14176. * \c mbedtls_ssl_set_bio(). You also need to provide timer
  14177. * callbacks with \c mbedtls_ssl_set_timer_cb().
  14178. *
  14179. * \param conf SSL configuration
  14180. * \param transport transport type:
  14181. * MBEDTLS_SSL_TRANSPORT_STREAM for TLS,
  14182. * MBEDTLS_SSL_TRANSPORT_DATAGRAM for DTLS.
  14183. */
  14184. void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport );
  14185. /**
  14186. * \brief Set the certificate verification mode
  14187. * Default: NONE on server, REQUIRED on client
  14188. *
  14189. * \param conf SSL configuration
  14190. * \param authmode can be:
  14191. *
  14192. * MBEDTLS_SSL_VERIFY_NONE: peer certificate is not checked
  14193. * (default on server)
  14194. * (insecure on client)
  14195. *
  14196. * MBEDTLS_SSL_VERIFY_OPTIONAL: peer certificate is checked, however the
  14197. * handshake continues even if verification failed;
  14198. * mbedtls_ssl_get_verify_result() can be called after the
  14199. * handshake is complete.
  14200. *
  14201. * MBEDTLS_SSL_VERIFY_REQUIRED: peer *must* present a valid certificate,
  14202. * handshake is aborted if verification failed.
  14203. *
  14204. * \note On client, MBEDTLS_SSL_VERIFY_REQUIRED is the recommended mode.
  14205. * With MBEDTLS_SSL_VERIFY_OPTIONAL, the user needs to call mbedtls_ssl_get_verify_result() at
  14206. * the right time(s), which may not be obvious, while REQUIRED always perform
  14207. * the verification as soon as possible. For example, REQUIRED was protecting
  14208. * against the "triple handshake" attack even before it was found.
  14209. */
  14210. void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode );
  14211. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  14212. /**
  14213. * \brief Set the verification callback (Optional).
  14214. *
  14215. * If set, the verify callback is called for each
  14216. * certificate in the chain. For implementation
  14217. * information, please see \c x509parse_verify()
  14218. *
  14219. * \param conf SSL configuration
  14220. * \param f_vrfy verification function
  14221. * \param p_vrfy verification parameter
  14222. */
  14223. void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
  14224. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  14225. void *p_vrfy );
  14226. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  14227. /**
  14228. * \brief Set the random number generator callback
  14229. *
  14230. * \param conf SSL configuration
  14231. * \param f_rng RNG function
  14232. * \param p_rng RNG parameter
  14233. */
  14234. void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
  14235. int (*f_rng)(void *, unsigned char *, size_t),
  14236. void *p_rng );
  14237. /**
  14238. * \brief Set the debug callback
  14239. *
  14240. * The callback has the following argument:
  14241. * void * opaque context for the callback
  14242. * int debug level
  14243. * const char * file name
  14244. * int line number
  14245. * const char * message
  14246. *
  14247. * \param conf SSL configuration
  14248. * \param f_dbg debug function
  14249. * \param p_dbg debug parameter
  14250. */
  14251. void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
  14252. void (*f_dbg)(void *, int, const char *, int, const char *),
  14253. void *p_dbg );
  14254. /**
  14255. * \brief Set the underlying BIO callbacks for write, read and
  14256. * read-with-timeout.
  14257. *
  14258. * \param ssl SSL context
  14259. * \param p_bio parameter (context) shared by BIO callbacks
  14260. * \param f_send write callback
  14261. * \param f_recv read callback
  14262. * \param f_recv_timeout blocking read callback with timeout.
  14263. * The last argument is the timeout in milliseconds,
  14264. * 0 means no timeout (block forever until a message comes)
  14265. *
  14266. * \note One of f_recv or f_recv_timeout can be NULL, in which case
  14267. * the other is used. If both are non-NULL, f_recv_timeout is
  14268. * used and f_recv is ignored (as if it were NULL).
  14269. *
  14270. * \note The two most common use cases are:
  14271. * - non-blocking I/O, f_recv != NULL, f_recv_timeout == NULL
  14272. * - blocking I/O, f_recv == NULL, f_recv_timout != NULL
  14273. *
  14274. * \note For DTLS, you need to provide either a non-NULL
  14275. * f_recv_timeout callback, or a f_recv that doesn't block.
  14276. */
  14277. void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
  14278. void *p_bio,
  14279. int (*f_send)(void *, const unsigned char *, size_t),
  14280. int (*f_recv)(void *, unsigned char *, size_t),
  14281. int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t) );
  14282. /**
  14283. * \brief Set the timeout period for mbedtls_ssl_read()
  14284. * (Default: no timeout.)
  14285. *
  14286. * \param conf SSL configuration context
  14287. * \param timeout Timeout value in milliseconds.
  14288. * Use 0 for no timeout (default).
  14289. *
  14290. * \note With blocking I/O, this will only work if a non-NULL
  14291. * \c f_recv_timeout was set with \c mbedtls_ssl_set_bio().
  14292. * With non-blocking I/O, this will only work if timer
  14293. * callbacks were set with \c mbedtls_ssl_set_timer_cb().
  14294. *
  14295. * \note With non-blocking I/O, you may also skip this function
  14296. * altogether and handle timeouts at the application layer.
  14297. */
  14298. void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout );
  14299. /**
  14300. * \brief Set the timer callbacks
  14301. * (Mandatory for DTLS.)
  14302. *
  14303. * \param ssl SSL context
  14304. * \param p_timer parameter (context) shared by timer callback
  14305. * \param f_set_timer set timer callback
  14306. * Accepts an intermediate and a final delay in milliseconcs
  14307. * If the final delay is 0, cancels the running timer.
  14308. * \param f_get_timer get timer callback. Must return:
  14309. * -1 if cancelled
  14310. * 0 if none of the delays is expired
  14311. * 1 if the intermediate delay only is expired
  14312. * 2 if the final delay is expired
  14313. */
  14314. void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
  14315. void *p_timer,
  14316. void (*f_set_timer)(void *, uint32_t int_ms, uint32_t fin_ms),
  14317. int (*f_get_timer)(void *) );
  14318. /**
  14319. * \brief Callback type: generate and write session ticket
  14320. *
  14321. * \note This describes what a callback implementation should do.
  14322. * This callback should generate and encrypted and
  14323. * authenticated ticket for the session and write it to the
  14324. * output buffer. Here, ticket means the opaque ticket part
  14325. * of the NewSessionTicket structure of RFC 5077.
  14326. *
  14327. * \param p_ticket Context for the callback
  14328. * \param session SSL session to bo written in the ticket
  14329. * \param start Start of the outpur buffer
  14330. * \param end End of the output buffer
  14331. * \param tlen On exit, holds the length written
  14332. * \param lifetime On exit, holds the lifetime of the ticket in seconds
  14333. *
  14334. * \return 0 if successful, or
  14335. * a specific MBEDTLS_ERR_XXX code.
  14336. */
  14337. typedef int mbedtls_ssl_ticket_write_t( void *p_ticket,
  14338. const mbedtls_ssl_session *session,
  14339. unsigned char *start,
  14340. const unsigned char *end,
  14341. size_t *tlen,
  14342. uint32_t *lifetime );
  14343. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  14344. /**
  14345. * \brief Callback type: Export key block and master secret
  14346. *
  14347. * \note This is required for certain uses of TLS, e.g. EAP-TLS
  14348. * (RFC 5216) and Thread. The key pointers are ephemeral and
  14349. * therefore must not be stored. The master secret and keys
  14350. * should not be used directly except as an input to a key
  14351. * derivation function.
  14352. *
  14353. * \param p_expkey Context for the callback
  14354. * \param ms Pointer to master secret (fixed length: 48 bytes)
  14355. * \param kb Pointer to key block, see RFC 5246 section 6.3
  14356. * (variable length: 2 * maclen + 2 * keylen + 2 * ivlen).
  14357. * \param maclen MAC length
  14358. * \param keylen Key length
  14359. * \param ivlen IV length
  14360. *
  14361. * \return 0 if successful, or
  14362. * a specific MBEDTLS_ERR_XXX code.
  14363. */
  14364. typedef int mbedtls_ssl_export_keys_t( void *p_expkey,
  14365. const unsigned char *ms,
  14366. const unsigned char *kb,
  14367. size_t maclen,
  14368. size_t keylen,
  14369. size_t ivlen );
  14370. #endif /* MBEDTLS_SSL_EXPORT_KEYS */
  14371. /**
  14372. * \brief Callback type: parse and load session ticket
  14373. *
  14374. * \note This describes what a callback implementation should do.
  14375. * This callback should parse a session ticket as generated
  14376. * by the corresponding mbedtls_ssl_ticket_write_t function,
  14377. * and, if the ticket is authentic and valid, load the
  14378. * session.
  14379. *
  14380. * \note The implementation is allowed to modify the first len
  14381. * bytes of the input buffer, eg to use it as a temporary
  14382. * area for the decrypted ticket contents.
  14383. *
  14384. * \param p_ticket Context for the callback
  14385. * \param session SSL session to be loaded
  14386. * \param buf Start of the buffer containing the ticket
  14387. * \param len Length of the ticket.
  14388. *
  14389. * \return 0 if successful, or
  14390. * MBEDTLS_ERR_SSL_INVALID_MAC if not authentic, or
  14391. * MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED if expired, or
  14392. * any other non-zero code for other failures.
  14393. */
  14394. typedef int mbedtls_ssl_ticket_parse_t( void *p_ticket,
  14395. mbedtls_ssl_session *session,
  14396. unsigned char *buf,
  14397. size_t len );
  14398. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_SRV_C)
  14399. /**
  14400. * \brief Configure SSL session ticket callbacks (server only).
  14401. * (Default: none.)
  14402. *
  14403. * \note On server, session tickets are enabled by providing
  14404. * non-NULL callbacks.
  14405. *
  14406. * \note On client, use \c mbedtls_ssl_conf_session_tickets().
  14407. *
  14408. * \param conf SSL configuration context
  14409. * \param f_ticket_write Callback for writing a ticket
  14410. * \param f_ticket_parse Callback for parsing a ticket
  14411. * \param p_ticket Context shared by the two callbacks
  14412. */
  14413. void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
  14414. mbedtls_ssl_ticket_write_t *f_ticket_write,
  14415. mbedtls_ssl_ticket_parse_t *f_ticket_parse,
  14416. void *p_ticket );
  14417. #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_SRV_C */
  14418. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  14419. /**
  14420. * \brief Configure key export callback.
  14421. * (Default: none.)
  14422. *
  14423. * \note See \c mbedtls_ssl_export_keys_t.
  14424. *
  14425. * \param conf SSL configuration context
  14426. * \param f_export_keys Callback for exporting keys
  14427. * \param p_export_keys Context for the callback
  14428. */
  14429. void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
  14430. mbedtls_ssl_export_keys_t *f_export_keys,
  14431. void *p_export_keys );
  14432. #endif /* MBEDTLS_SSL_EXPORT_KEYS */
  14433. /**
  14434. * \brief Callback type: generate a cookie
  14435. *
  14436. * \param ctx Context for the callback
  14437. * \param p Buffer to write to,
  14438. * must be updated to point right after the cookie
  14439. * \param end Pointer to one past the end of the output buffer
  14440. * \param info Client ID info that was passed to
  14441. * \c mbedtls_ssl_set_client_transport_id()
  14442. * \param ilen Length of info in bytes
  14443. *
  14444. * \return The callback must return 0 on success,
  14445. * or a negative error code.
  14446. */
  14447. typedef int mbedtls_ssl_cookie_write_t( void *ctx,
  14448. unsigned char **p, unsigned char *end,
  14449. const unsigned char *info, size_t ilen );
  14450. /**
  14451. * \brief Callback type: verify a cookie
  14452. *
  14453. * \param ctx Context for the callback
  14454. * \param cookie Cookie to verify
  14455. * \param clen Length of cookie
  14456. * \param info Client ID info that was passed to
  14457. * \c mbedtls_ssl_set_client_transport_id()
  14458. * \param ilen Length of info in bytes
  14459. *
  14460. * \return The callback must return 0 if cookie is valid,
  14461. * or a negative error code.
  14462. */
  14463. typedef int mbedtls_ssl_cookie_check_t( void *ctx,
  14464. const unsigned char *cookie, size_t clen,
  14465. const unsigned char *info, size_t ilen );
  14466. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
  14467. /**
  14468. * \brief Register callbacks for DTLS cookies
  14469. * (Server only. DTLS only.)
  14470. *
  14471. * Default: dummy callbacks that fail, in order to force you to
  14472. * register working callbacks (and initialize their context).
  14473. *
  14474. * To disable HelloVerifyRequest, register NULL callbacks.
  14475. *
  14476. * \warning Disabling hello verification allows your server to be used
  14477. * for amplification in DoS attacks against other hosts.
  14478. * Only disable if you known this can't happen in your
  14479. * particular environment.
  14480. *
  14481. * \note See comments on \c mbedtls_ssl_handshake() about handling
  14482. * the MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED that is expected
  14483. * on the first handshake attempt when this is enabled.
  14484. *
  14485. * \note This is also necessary to handle client reconnection from
  14486. * the same port as described in RFC 6347 section 4.2.8 (only
  14487. * the variant with cookies is supported currently). See
  14488. * comments on \c mbedtls_ssl_read() for details.
  14489. *
  14490. * \param conf SSL configuration
  14491. * \param f_cookie_write Cookie write callback
  14492. * \param f_cookie_check Cookie check callback
  14493. * \param p_cookie Context for both callbacks
  14494. */
  14495. void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf,
  14496. mbedtls_ssl_cookie_write_t *f_cookie_write,
  14497. mbedtls_ssl_cookie_check_t *f_cookie_check,
  14498. void *p_cookie );
  14499. /**
  14500. * \brief Set client's transport-level identification info.
  14501. * (Server only. DTLS only.)
  14502. *
  14503. * This is usually the IP address (and port), but could be
  14504. * anything identify the client depending on the underlying
  14505. * network stack. Used for HelloVerifyRequest with DTLS.
  14506. * This is *not* used to route the actual packets.
  14507. *
  14508. * \param ssl SSL context
  14509. * \param info Transport-level info identifying the client (eg IP + port)
  14510. * \param ilen Length of info in bytes
  14511. *
  14512. * \note An internal copy is made, so the info buffer can be reused.
  14513. *
  14514. * \return 0 on success,
  14515. * MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used on client,
  14516. * MBEDTLS_ERR_SSL_ALLOC_FAILED if out of memory.
  14517. */
  14518. int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl,
  14519. const unsigned char *info,
  14520. size_t ilen );
  14521. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
  14522. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  14523. /**
  14524. * \brief Enable or disable anti-replay protection for DTLS.
  14525. * (DTLS only, no effect on TLS.)
  14526. * Default: enabled.
  14527. *
  14528. * \param conf SSL configuration
  14529. * \param mode MBEDTLS_SSL_ANTI_REPLAY_ENABLED or MBEDTLS_SSL_ANTI_REPLAY_DISABLED.
  14530. *
  14531. * \warning Disabling this is a security risk unless the application
  14532. * protocol handles duplicated packets in a safe way. You
  14533. * should not disable this without careful consideration.
  14534. * However, if your application already detects duplicated
  14535. * packets and needs information about them to adjust its
  14536. * transmission strategy, then you'll want to disable this.
  14537. */
  14538. void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode );
  14539. #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
  14540. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  14541. /**
  14542. * \brief Set a limit on the number of records with a bad MAC
  14543. * before terminating the connection.
  14544. * (DTLS only, no effect on TLS.)
  14545. * Default: 0 (disabled).
  14546. *
  14547. * \param conf SSL configuration
  14548. * \param limit Limit, or 0 to disable.
  14549. *
  14550. * \note If the limit is N, then the connection is terminated when
  14551. * the Nth non-authentic record is seen.
  14552. *
  14553. * \note Records with an invalid header are not counted, only the
  14554. * ones going through the authentication-decryption phase.
  14555. *
  14556. * \note This is a security trade-off related to the fact that it's
  14557. * often relatively easy for an active attacker ot inject UDP
  14558. * datagrams. On one hand, setting a low limit here makes it
  14559. * easier for such an attacker to forcibly terminated a
  14560. * connection. On the other hand, a high limit or no limit
  14561. * might make us waste resources checking authentication on
  14562. * many bogus packets.
  14563. */
  14564. void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit );
  14565. #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
  14566. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  14567. /**
  14568. * \brief Set retransmit timeout values for the DTLS handshale.
  14569. * (DTLS only, no effect on TLS.)
  14570. *
  14571. * \param conf SSL configuration
  14572. * \param min Initial timeout value in milliseconds.
  14573. * Default: 1000 (1 second).
  14574. * \param max Maximum timeout value in milliseconds.
  14575. * Default: 60000 (60 seconds).
  14576. *
  14577. * \note Default values are from RFC 6347 section 4.2.4.1.
  14578. *
  14579. * \note Higher values for initial timeout may increase average
  14580. * handshake latency. Lower values may increase the risk of
  14581. * network congestion by causing more retransmissions.
  14582. */
  14583. void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max );
  14584. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  14585. #if defined(MBEDTLS_SSL_SRV_C)
  14586. /**
  14587. * \brief Set the session cache callbacks (server-side only)
  14588. * If not set, no session resuming is done (except if session
  14589. * tickets are enabled too).
  14590. *
  14591. * The session cache has the responsibility to check for stale
  14592. * entries based on timeout. See RFC 5246 for recommendations.
  14593. *
  14594. * Warning: session.peer_cert is cleared by the SSL/TLS layer on
  14595. * connection shutdown, so do not cache the pointer! Either set
  14596. * it to NULL or make a full copy of the certificate.
  14597. *
  14598. * The get callback is called once during the initial handshake
  14599. * to enable session resuming. The get function has the
  14600. * following parameters: (void *parameter, mbedtls_ssl_session *session)
  14601. * If a valid entry is found, it should fill the master of
  14602. * the session object with the cached values and return 0,
  14603. * return 1 otherwise. Optionally peer_cert can be set as well
  14604. * if it is properly present in cache entry.
  14605. *
  14606. * The set callback is called once during the initial handshake
  14607. * to enable session resuming after the entire handshake has
  14608. * been finished. The set function has the following parameters:
  14609. * (void *parameter, const mbedtls_ssl_session *session). The function
  14610. * should create a cache entry for future retrieval based on
  14611. * the data in the session structure and should keep in mind
  14612. * that the mbedtls_ssl_session object presented (and all its referenced
  14613. * data) is cleared by the SSL/TLS layer when the connection is
  14614. * terminated. It is recommended to add metadata to determine if
  14615. * an entry is still valid in the future. Return 0 if
  14616. * successfully cached, return 1 otherwise.
  14617. *
  14618. * \param conf SSL configuration
  14619. * \param p_cache parmater (context) for both callbacks
  14620. * \param f_get_cache session get callback
  14621. * \param f_set_cache session set callback
  14622. */
  14623. void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
  14624. void *p_cache,
  14625. int (*f_get_cache)(void *, mbedtls_ssl_session *),
  14626. int (*f_set_cache)(void *, const mbedtls_ssl_session *) );
  14627. #endif /* MBEDTLS_SSL_SRV_C */
  14628. #if defined(MBEDTLS_SSL_CLI_C)
  14629. /**
  14630. * \brief Request resumption of session (client-side only)
  14631. * Session data is copied from presented session structure.
  14632. *
  14633. * \param ssl SSL context
  14634. * \param session session context
  14635. *
  14636. * \return 0 if successful,
  14637. * MBEDTLS_ERR_SSL_ALLOC_FAILED if memory allocation failed,
  14638. * MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used server-side or
  14639. * arguments are otherwise invalid
  14640. *
  14641. * \sa mbedtls_ssl_get_session()
  14642. */
  14643. int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session );
  14644. #endif /* MBEDTLS_SSL_CLI_C */
  14645. /**
  14646. * \brief Set the list of allowed ciphersuites and the preference
  14647. * order. First in the list has the highest preference.
  14648. * (Overrides all version specific lists)
  14649. *
  14650. * The ciphersuites array is not copied, and must remain
  14651. * valid for the lifetime of the ssl_config.
  14652. *
  14653. * Note: The server uses its own preferences
  14654. * over the preference of the client unless
  14655. * MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE is defined!
  14656. *
  14657. * \param conf SSL configuration
  14658. * \param ciphersuites 0-terminated list of allowed ciphersuites
  14659. */
  14660. void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
  14661. const int *ciphersuites );
  14662. /**
  14663. * \brief Set the list of allowed ciphersuites and the
  14664. * preference order for a specific version of the protocol.
  14665. * (Only useful on the server side)
  14666. *
  14667. * The ciphersuites array is not copied, and must remain
  14668. * valid for the lifetime of the ssl_config.
  14669. *
  14670. * \param conf SSL configuration
  14671. * \param ciphersuites 0-terminated list of allowed ciphersuites
  14672. * \param major Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3
  14673. * supported)
  14674. * \param minor Minor version number (MBEDTLS_SSL_MINOR_VERSION_0,
  14675. * MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2,
  14676. * MBEDTLS_SSL_MINOR_VERSION_3 supported)
  14677. *
  14678. * \note With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0
  14679. * and MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2
  14680. */
  14681. void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
  14682. const int *ciphersuites,
  14683. int major, int minor );
  14684. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  14685. /**
  14686. * \brief Set the X.509 security profile used for verification
  14687. *
  14688. * \note The restrictions are enforced for all certificates in the
  14689. * chain. However, signatures in the handshake are not covered
  14690. * by this setting but by \b mbedtls_ssl_conf_sig_hashes().
  14691. *
  14692. * \param conf SSL configuration
  14693. * \param profile Profile to use
  14694. */
  14695. void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
  14696. const mbedtls_x509_crt_profile *profile );
  14697. /**
  14698. * \brief Set the data required to verify peer certificate
  14699. *
  14700. * \param conf SSL configuration
  14701. * \param ca_chain trusted CA chain (meaning all fully trusted top-level CAs)
  14702. * \param ca_crl trusted CA CRLs
  14703. */
  14704. void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
  14705. mbedtls_x509_crt *ca_chain,
  14706. mbedtls_x509_crl *ca_crl );
  14707. /**
  14708. * \brief Set own certificate chain and private key
  14709. *
  14710. * \note own_cert should contain in order from the bottom up your
  14711. * certificate chain. The top certificate (self-signed)
  14712. * can be omitted.
  14713. *
  14714. * \note On server, this function can be called multiple times to
  14715. * provision more than one cert/key pair (eg one ECDSA, one
  14716. * RSA with SHA-256, one RSA with SHA-1). An adequate
  14717. * certificate will be selected according to the client's
  14718. * advertised capabilities. In case mutliple certificates are
  14719. * adequate, preference is given to the one set by the first
  14720. * call to this function, then second, etc.
  14721. *
  14722. * \note On client, only the first call has any effect.
  14723. *
  14724. * \param conf SSL configuration
  14725. * \param own_cert own public certificate chain
  14726. * \param pk_key own private key
  14727. *
  14728. * \return 0 on success or MBEDTLS_ERR_SSL_ALLOC_FAILED
  14729. */
  14730. int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
  14731. mbedtls_x509_crt *own_cert,
  14732. mbedtls_pk_context *pk_key );
  14733. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  14734. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  14735. /**
  14736. * \brief Set the Pre Shared Key (PSK) and the expected identity name
  14737. *
  14738. * \note This is mainly useful for clients. Servers will usually
  14739. * want to use \c mbedtls_ssl_conf_psk_cb() instead.
  14740. *
  14741. * \param conf SSL configuration
  14742. * \param psk pointer to the pre-shared key
  14743. * \param psk_len pre-shared key length
  14744. * \param psk_identity pointer to the pre-shared key identity
  14745. * \param psk_identity_len identity key length
  14746. *
  14747. * \return 0 if successful or MBEDTLS_ERR_SSL_ALLOC_FAILED
  14748. */
  14749. int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
  14750. const unsigned char *psk, size_t psk_len,
  14751. const unsigned char *psk_identity, size_t psk_identity_len );
  14752. /**
  14753. * \brief Set the Pre Shared Key (PSK) for the current handshake
  14754. *
  14755. * \note This should only be called inside the PSK callback,
  14756. * ie the function passed to \c mbedtls_ssl_conf_psk_cb().
  14757. *
  14758. * \param ssl SSL context
  14759. * \param psk pointer to the pre-shared key
  14760. * \param psk_len pre-shared key length
  14761. *
  14762. * \return 0 if successful or MBEDTLS_ERR_SSL_ALLOC_FAILED
  14763. */
  14764. int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
  14765. const unsigned char *psk, size_t psk_len );
  14766. /**
  14767. * \brief Set the PSK callback (server-side only).
  14768. *
  14769. * If set, the PSK callback is called for each
  14770. * handshake where a PSK ciphersuite was negotiated.
  14771. * The caller provides the identity received and wants to
  14772. * receive the actual PSK data and length.
  14773. *
  14774. * The callback has the following parameters: (void *parameter,
  14775. * mbedtls_ssl_context *ssl, const unsigned char *psk_identity,
  14776. * size_t identity_len)
  14777. * If a valid PSK identity is found, the callback should use
  14778. * \c mbedtls_ssl_set_hs_psk() on the ssl context to set the
  14779. * correct PSK and return 0.
  14780. * Any other return value will result in a denied PSK identity.
  14781. *
  14782. * \note If you set a PSK callback using this function, then you
  14783. * don't need to set a PSK key and identity using
  14784. * \c mbedtls_ssl_conf_psk().
  14785. *
  14786. * \param conf SSL configuration
  14787. * \param f_psk PSK identity function
  14788. * \param p_psk PSK identity parameter
  14789. */
  14790. void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
  14791. int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
  14792. size_t),
  14793. void *p_psk );
  14794. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  14795. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
  14796. /**
  14797. * \brief Set the Diffie-Hellman public P and G values,
  14798. * read as hexadecimal strings (server-side only)
  14799. * (Default: MBEDTLS_DHM_RFC5114_MODP_2048_[PG])
  14800. *
  14801. * \param conf SSL configuration
  14802. * \param dhm_P Diffie-Hellman-Merkle modulus
  14803. * \param dhm_G Diffie-Hellman-Merkle generator
  14804. *
  14805. * \return 0 if successful
  14806. */
  14807. int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G );
  14808. /**
  14809. * \brief Set the Diffie-Hellman public P and G values,
  14810. * read from existing context (server-side only)
  14811. *
  14812. * \param conf SSL configuration
  14813. * \param dhm_ctx Diffie-Hellman-Merkle context
  14814. *
  14815. * \return 0 if successful
  14816. */
  14817. int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx );
  14818. #endif /* MBEDTLS_DHM_C && defined(MBEDTLS_SSL_SRV_C) */
  14819. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
  14820. /**
  14821. * \brief Set the minimum length for Diffie-Hellman parameters.
  14822. * (Client-side only.)
  14823. * (Default: 1024 bits.)
  14824. *
  14825. * \param conf SSL configuration
  14826. * \param bitlen Minimum bit length of the DHM prime
  14827. */
  14828. void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
  14829. unsigned int bitlen );
  14830. #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
  14831. #if defined(MBEDTLS_ECP_C)
  14832. /**
  14833. * \brief Set the allowed curves in order of preference.
  14834. * (Default: all defined curves.)
  14835. *
  14836. * On server: this only affects selection of the ECDHE curve;
  14837. * the curves used for ECDH and ECDSA are determined by the
  14838. * list of available certificates instead.
  14839. *
  14840. * On client: this affects the list of curves offered for any
  14841. * use. The server can override our preference order.
  14842. *
  14843. * Both sides: limits the set of curves accepted for use in
  14844. * ECDHE and in the peer's end-entity certificate.
  14845. *
  14846. * \note This has no influence on which curves are allowed inside the
  14847. * certificate chains, see \c mbedtls_ssl_conf_cert_profile()
  14848. * for that. For the end-entity certificate however, the key
  14849. * will be accepted only if it is allowed both by this list
  14850. * and by the cert profile.
  14851. *
  14852. * \note This list should be ordered by decreasing preference
  14853. * (preferred curve first).
  14854. *
  14855. * \param conf SSL configuration
  14856. * \param curves Ordered list of allowed curves,
  14857. * terminated by MBEDTLS_ECP_DP_NONE.
  14858. */
  14859. void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
  14860. const mbedtls_ecp_group_id *curves );
  14861. #endif /* MBEDTLS_ECP_C */
  14862. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  14863. /**
  14864. * \brief Set the allowed hashes for signatures during the handshake.
  14865. * (Default: all available hashes except MD5.)
  14866. *
  14867. * \note This only affects which hashes are offered and can be used
  14868. * for signatures during the handshake. Hashes for message
  14869. * authentication and the TLS PRF are controlled by the
  14870. * ciphersuite, see \c mbedtls_ssl_conf_ciphersuites(). Hashes
  14871. * used for certificate signature are controlled by the
  14872. * verification profile, see \c mbedtls_ssl_conf_cert_profile().
  14873. *
  14874. * \note This list should be ordered by decreasing preference
  14875. * (preferred hash first).
  14876. *
  14877. * \param conf SSL configuration
  14878. * \param hashes Ordered list of allowed signature hashes,
  14879. * terminated by \c MBEDTLS_MD_NONE.
  14880. */
  14881. void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
  14882. const int *hashes );
  14883. #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
  14884. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  14885. /**
  14886. * \brief Set hostname for ServerName TLS extension
  14887. * (client-side only)
  14888. *
  14889. *
  14890. * \param ssl SSL context
  14891. * \param hostname the server hostname
  14892. *
  14893. * \return 0 if successful or MBEDTLS_ERR_SSL_ALLOC_FAILED
  14894. */
  14895. int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname );
  14896. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  14897. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  14898. /**
  14899. * \brief Set own certificate and key for the current handshake
  14900. *
  14901. * \note Same as \c mbedtls_ssl_conf_own_cert() but for use within
  14902. * the SNI callback.
  14903. *
  14904. * \param ssl SSL context
  14905. * \param own_cert own public certificate chain
  14906. * \param pk_key own private key
  14907. *
  14908. * \return 0 on success or MBEDTLS_ERR_SSL_ALLOC_FAILED
  14909. */
  14910. int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
  14911. mbedtls_x509_crt *own_cert,
  14912. mbedtls_pk_context *pk_key );
  14913. /**
  14914. * \brief Set the data required to verify peer certificate for the
  14915. * current handshake
  14916. *
  14917. * \note Same as \c mbedtls_ssl_conf_ca_chain() but for use within
  14918. * the SNI callback.
  14919. *
  14920. * \param ssl SSL context
  14921. * \param ca_chain trusted CA chain (meaning all fully trusted top-level CAs)
  14922. * \param ca_crl trusted CA CRLs
  14923. */
  14924. void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
  14925. mbedtls_x509_crt *ca_chain,
  14926. mbedtls_x509_crl *ca_crl );
  14927. /**
  14928. * \brief Set authmode for the current handshake.
  14929. *
  14930. * \note Same as \c mbedtls_ssl_conf_authmode() but for use within
  14931. * the SNI callback.
  14932. *
  14933. * \param ssl SSL context
  14934. * \param authmode MBEDTLS_SSL_VERIFY_NONE, MBEDTLS_SSL_VERIFY_OPTIONAL or
  14935. * MBEDTLS_SSL_VERIFY_REQUIRED
  14936. */
  14937. void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
  14938. int authmode );
  14939. /**
  14940. * \brief Set server side ServerName TLS extension callback
  14941. * (optional, server-side only).
  14942. *
  14943. * If set, the ServerName callback is called whenever the
  14944. * server receives a ServerName TLS extension from the client
  14945. * during a handshake. The ServerName callback has the
  14946. * following parameters: (void *parameter, mbedtls_ssl_context *ssl,
  14947. * const unsigned char *hostname, size_t len). If a suitable
  14948. * certificate is found, the callback must set the
  14949. * certificate(s) and key(s) to use with \c
  14950. * mbedtls_ssl_set_hs_own_cert() (can be called repeatedly),
  14951. * and may optionally adjust the CA and associated CRL with \c
  14952. * mbedtls_ssl_set_hs_ca_chain() as well as the client
  14953. * authentication mode with \c mbedtls_ssl_set_hs_authmode(),
  14954. * then must return 0. If no matching name is found, the
  14955. * callback must either set a default cert, or
  14956. * return non-zero to abort the handshake at this point.
  14957. *
  14958. * \param conf SSL configuration
  14959. * \param f_sni verification function
  14960. * \param p_sni verification parameter
  14961. */
  14962. void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
  14963. int (*f_sni)(void *, mbedtls_ssl_context *, const unsigned char *,
  14964. size_t),
  14965. void *p_sni );
  14966. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  14967. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  14968. /**
  14969. * \brief Set the EC J-PAKE password for current handshake.
  14970. *
  14971. * \note An internal copy is made, and destroyed as soon as the
  14972. * handshake is completed, or when the SSL context is reset or
  14973. * freed.
  14974. *
  14975. * \note The SSL context needs to be already set up. The right place
  14976. * to call this function is between \c mbedtls_ssl_setup() or
  14977. * \c mbedtls_ssl_reset() and \c mbedtls_ssl_handshake().
  14978. *
  14979. * \param ssl SSL context
  14980. * \param pw EC J-PAKE password (pre-shared secret)
  14981. * \param pw_len length of pw in bytes
  14982. *
  14983. * \return 0 on success, or a negative error code.
  14984. */
  14985. int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
  14986. const unsigned char *pw,
  14987. size_t pw_len );
  14988. #endif /*MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  14989. #if defined(MBEDTLS_SSL_ALPN)
  14990. /**
  14991. * \brief Set the supported Application Layer Protocols.
  14992. *
  14993. * \param conf SSL configuration
  14994. * \param protos NULL-terminated list of supported protocols,
  14995. * in decreasing preference order.
  14996. *
  14997. * \return 0 on success, or MBEDTLS_ERR_SSL_BAD_INPUT_DATA.
  14998. */
  14999. int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos );
  15000. /**
  15001. * \brief Get the name of the negotiated Application Layer Protocol.
  15002. * This function should be called after the handshake is
  15003. * completed.
  15004. *
  15005. * \param ssl SSL context
  15006. *
  15007. * \return Protcol name, or NULL if no protocol was negotiated.
  15008. */
  15009. const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl );
  15010. #endif /* MBEDTLS_SSL_ALPN */
  15011. /**
  15012. * \brief Set the maximum supported version sent from the client side
  15013. * and/or accepted at the server side
  15014. * (Default: MBEDTLS_SSL_MAX_MAJOR_VERSION, MBEDTLS_SSL_MAX_MINOR_VERSION)
  15015. *
  15016. * \note This ignores ciphersuites from higher versions.
  15017. *
  15018. * \note With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0 and
  15019. * MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2
  15020. *
  15021. * \param conf SSL configuration
  15022. * \param major Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3 supported)
  15023. * \param minor Minor version number (MBEDTLS_SSL_MINOR_VERSION_0,
  15024. * MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2,
  15025. * MBEDTLS_SSL_MINOR_VERSION_3 supported)
  15026. */
  15027. void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor );
  15028. /**
  15029. * \brief Set the minimum accepted SSL/TLS protocol version
  15030. * (Default: TLS 1.0)
  15031. *
  15032. * \note Input outside of the SSL_MAX_XXXXX_VERSION and
  15033. * SSL_MIN_XXXXX_VERSION range is ignored.
  15034. *
  15035. * \note MBEDTLS_SSL_MINOR_VERSION_0 (SSL v3) should be avoided.
  15036. *
  15037. * \note With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0 and
  15038. * MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2
  15039. *
  15040. * \param conf SSL configuration
  15041. * \param major Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3 supported)
  15042. * \param minor Minor version number (MBEDTLS_SSL_MINOR_VERSION_0,
  15043. * MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2,
  15044. * MBEDTLS_SSL_MINOR_VERSION_3 supported)
  15045. */
  15046. void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor );
  15047. #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
  15048. /**
  15049. * \brief Set the fallback flag (client-side only).
  15050. * (Default: MBEDTLS_SSL_IS_NOT_FALLBACK).
  15051. *
  15052. * \note Set to MBEDTLS_SSL_IS_FALLBACK when preparing a fallback
  15053. * connection, that is a connection with max_version set to a
  15054. * lower value than the value you're willing to use. Such
  15055. * fallback connections are not recommended but are sometimes
  15056. * necessary to interoperate with buggy (version-intolerant)
  15057. * servers.
  15058. *
  15059. * \warning You should NOT set this to MBEDTLS_SSL_IS_FALLBACK for
  15060. * non-fallback connections! This would appear to work for a
  15061. * while, then cause failures when the server is upgraded to
  15062. * support a newer TLS version.
  15063. *
  15064. * \param conf SSL configuration
  15065. * \param fallback MBEDTLS_SSL_IS_NOT_FALLBACK or MBEDTLS_SSL_IS_FALLBACK
  15066. */
  15067. void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback );
  15068. #endif /* MBEDTLS_SSL_FALLBACK_SCSV && MBEDTLS_SSL_CLI_C */
  15069. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  15070. /**
  15071. * \brief Enable or disable Encrypt-then-MAC
  15072. * (Default: MBEDTLS_SSL_ETM_ENABLED)
  15073. *
  15074. * \note This should always be enabled, it is a security
  15075. * improvement, and should not cause any interoperability
  15076. * issue (used only if the peer supports it too).
  15077. *
  15078. * \param conf SSL configuration
  15079. * \param etm MBEDTLS_SSL_ETM_ENABLED or MBEDTLS_SSL_ETM_DISABLED
  15080. */
  15081. void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm );
  15082. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  15083. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  15084. /**
  15085. * \brief Enable or disable Extended Master Secret negotiation.
  15086. * (Default: MBEDTLS_SSL_EXTENDED_MS_ENABLED)
  15087. *
  15088. * \note This should always be enabled, it is a security fix to the
  15089. * protocol, and should not cause any interoperability issue
  15090. * (used only if the peer supports it too).
  15091. *
  15092. * \param conf SSL configuration
  15093. * \param ems MBEDTLS_SSL_EXTENDED_MS_ENABLED or MBEDTLS_SSL_EXTENDED_MS_DISABLED
  15094. */
  15095. void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems );
  15096. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  15097. #if defined(MBEDTLS_ARC4_C)
  15098. /**
  15099. * \brief Disable or enable support for RC4
  15100. * (Default: MBEDTLS_SSL_ARC4_DISABLED)
  15101. *
  15102. * \warning Use of RC4 in (D)TLS has been prohibited by RFC ????
  15103. * for security reasons. Use at your own risks.
  15104. *
  15105. * \note This function will likely be removed in future versions as
  15106. * RC4 will then be disabled by default at compile time.
  15107. *
  15108. * \param conf SSL configuration
  15109. * \param arc4 MBEDTLS_SSL_ARC4_ENABLED or MBEDTLS_SSL_ARC4_DISABLED
  15110. */
  15111. void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 );
  15112. #endif /* MBEDTLS_ARC4_C */
  15113. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  15114. /**
  15115. * \brief Set the maximum fragment length to emit and/or negotiate
  15116. * (Default: MBEDTLS_SSL_MAX_CONTENT_LEN, usually 2^14 bytes)
  15117. * (Server: set maximum fragment length to emit,
  15118. * usually negotiated by the client during handshake
  15119. * (Client: set maximum fragment length to emit *and*
  15120. * negotiate with the server during handshake)
  15121. *
  15122. * \param conf SSL configuration
  15123. * \param mfl_code Code for maximum fragment length (allowed values:
  15124. * MBEDTLS_SSL_MAX_FRAG_LEN_512, MBEDTLS_SSL_MAX_FRAG_LEN_1024,
  15125. * MBEDTLS_SSL_MAX_FRAG_LEN_2048, MBEDTLS_SSL_MAX_FRAG_LEN_4096)
  15126. *
  15127. * \return 0 if successful or MBEDTLS_ERR_SSL_BAD_INPUT_DATA
  15128. */
  15129. int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code );
  15130. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  15131. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  15132. /**
  15133. * \brief Activate negotiation of truncated HMAC
  15134. * (Default: MBEDTLS_SSL_TRUNC_HMAC_DISABLED)
  15135. *
  15136. * \param conf SSL configuration
  15137. * \param truncate Enable or disable (MBEDTLS_SSL_TRUNC_HMAC_ENABLED or
  15138. * MBEDTLS_SSL_TRUNC_HMAC_DISABLED)
  15139. */
  15140. void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate );
  15141. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  15142. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  15143. /**
  15144. * \brief Enable / Disable 1/n-1 record splitting
  15145. * (Default: MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED)
  15146. *
  15147. * \note Only affects SSLv3 and TLS 1.0, not higher versions.
  15148. * Does not affect non-CBC ciphersuites in any version.
  15149. *
  15150. * \param conf SSL configuration
  15151. * \param split MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED or
  15152. * MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED
  15153. */
  15154. void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split );
  15155. #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
  15156. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
  15157. /**
  15158. * \brief Enable / Disable session tickets (client only).
  15159. * (Default: MBEDTLS_SSL_SESSION_TICKETS_ENABLED.)
  15160. *
  15161. * \note On server, use \c mbedtls_ssl_conf_session_tickets_cb().
  15162. *
  15163. * \param conf SSL configuration
  15164. * \param use_tickets Enable or disable (MBEDTLS_SSL_SESSION_TICKETS_ENABLED or
  15165. * MBEDTLS_SSL_SESSION_TICKETS_DISABLED)
  15166. */
  15167. void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets );
  15168. #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
  15169. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  15170. /**
  15171. * \brief Enable / Disable renegotiation support for connection when
  15172. * initiated by peer
  15173. * (Default: MBEDTLS_SSL_RENEGOTIATION_DISABLED)
  15174. *
  15175. * \warning It is recommended to always disable renegotation unless you
  15176. * know you need it and you know what you're doing. In the
  15177. * past, there has been several issues associated with
  15178. * renegotiation or a poor understanding of its properties.
  15179. *
  15180. * \note Server-side, enabling renegotiation also makes the server
  15181. * susceptible to a resource DoS by a malicious client.
  15182. *
  15183. * \param conf SSL configuration
  15184. * \param renegotiation Enable or disable (MBEDTLS_SSL_RENEGOTIATION_ENABLED or
  15185. * MBEDTLS_SSL_RENEGOTIATION_DISABLED)
  15186. */
  15187. void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation );
  15188. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  15189. /**
  15190. * \brief Prevent or allow legacy renegotiation.
  15191. * (Default: MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION)
  15192. *
  15193. * MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION allows connections to
  15194. * be established even if the peer does not support
  15195. * secure renegotiation, but does not allow renegotiation
  15196. * to take place if not secure.
  15197. * (Interoperable and secure option)
  15198. *
  15199. * MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION allows renegotiations
  15200. * with non-upgraded peers. Allowing legacy renegotiation
  15201. * makes the connection vulnerable to specific man in the
  15202. * middle attacks. (See RFC 5746)
  15203. * (Most interoperable and least secure option)
  15204. *
  15205. * MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE breaks off connections
  15206. * if peer does not support secure renegotiation. Results
  15207. * in interoperability issues with non-upgraded peers
  15208. * that do not support renegotiation altogether.
  15209. * (Most secure option, interoperability issues)
  15210. *
  15211. * \param conf SSL configuration
  15212. * \param allow_legacy Prevent or allow (SSL_NO_LEGACY_RENEGOTIATION,
  15213. * SSL_ALLOW_LEGACY_RENEGOTIATION or
  15214. * MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE)
  15215. */
  15216. void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy );
  15217. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  15218. /**
  15219. * \brief Enforce renegotiation requests.
  15220. * (Default: enforced, max_records = 16)
  15221. *
  15222. * When we request a renegotiation, the peer can comply or
  15223. * ignore the request. This function allows us to decide
  15224. * whether to enforce our renegotiation requests by closing
  15225. * the connection if the peer doesn't comply.
  15226. *
  15227. * However, records could already be in transit from the peer
  15228. * when the request is emitted. In order to increase
  15229. * reliability, we can accept a number of records before the
  15230. * expected handshake records.
  15231. *
  15232. * The optimal value is highly dependent on the specific usage
  15233. * scenario.
  15234. *
  15235. * \note With DTLS and server-initiated renegotiation, the
  15236. * HelloRequest is retransmited every time mbedtls_ssl_read() times
  15237. * out or receives Application Data, until:
  15238. * - max_records records have beens seen, if it is >= 0, or
  15239. * - the number of retransmits that would happen during an
  15240. * actual handshake has been reached.
  15241. * Please remember the request might be lost a few times
  15242. * if you consider setting max_records to a really low value.
  15243. *
  15244. * \warning On client, the grace period can only happen during
  15245. * mbedtls_ssl_read(), as opposed to mbedtls_ssl_write() and mbedtls_ssl_renegotiate()
  15246. * which always behave as if max_record was 0. The reason is,
  15247. * if we receive application data from the server, we need a
  15248. * place to write it, which only happens during mbedtls_ssl_read().
  15249. *
  15250. * \param conf SSL configuration
  15251. * \param max_records Use MBEDTLS_SSL_RENEGOTIATION_NOT_ENFORCED if you don't want to
  15252. * enforce renegotiation, or a non-negative value to enforce
  15253. * it but allow for a grace period of max_records records.
  15254. */
  15255. void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records );
  15256. /**
  15257. * \brief Set record counter threshold for periodic renegotiation.
  15258. * (Default: 2^64 - 256.)
  15259. *
  15260. * Renegotiation is automatically triggered when a record
  15261. * counter (outgoing or ingoing) crosses the defined
  15262. * threshold. The default value is meant to prevent the
  15263. * connection from being closed when the counter is about to
  15264. * reached its maximal value (it is not allowed to wrap).
  15265. *
  15266. * Lower values can be used to enforce policies such as "keys
  15267. * must be refreshed every N packets with cipher X".
  15268. *
  15269. * \param conf SSL configuration
  15270. * \param period The threshold value: a big-endian 64-bit number.
  15271. * Set to 2^64 - 1 to disable periodic renegotiation
  15272. */
  15273. void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
  15274. const unsigned char period[8] );
  15275. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  15276. /**
  15277. * \brief Return the number of data bytes available to read
  15278. *
  15279. * \param ssl SSL context
  15280. *
  15281. * \return how many bytes are available in the read buffer
  15282. */
  15283. size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl );
  15284. /**
  15285. * \brief Return the result of the certificate verification
  15286. *
  15287. * \param ssl SSL context
  15288. *
  15289. * \return 0 if successful,
  15290. * -1 if result is not available (eg because the handshake was
  15291. * aborted too early), or
  15292. * a combination of BADCERT_xxx and BADCRL_xxx flags, see
  15293. * x509.h
  15294. */
  15295. uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl );
  15296. /**
  15297. * \brief Return the name of the current ciphersuite
  15298. *
  15299. * \param ssl SSL context
  15300. *
  15301. * \return a string containing the ciphersuite name
  15302. */
  15303. const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl );
  15304. /**
  15305. * \brief Return the current SSL version (SSLv3/TLSv1/etc)
  15306. *
  15307. * \param ssl SSL context
  15308. *
  15309. * \return a string containing the SSL version
  15310. */
  15311. const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl );
  15312. /**
  15313. * \brief Return the (maximum) number of bytes added by the record
  15314. * layer: header + encryption/MAC overhead (inc. padding)
  15315. *
  15316. * \param ssl SSL context
  15317. *
  15318. * \return Current maximum record expansion in bytes, or
  15319. * MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE if compression is
  15320. * enabled, which makes expansion much less predictable
  15321. */
  15322. int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl );
  15323. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  15324. /**
  15325. * \brief Return the maximum fragment length (payload, in bytes).
  15326. * This is the value negotiated with peer if any,
  15327. * or the locally configured value.
  15328. *
  15329. * \note With DTLS, \c mbedtls_ssl_write() will return an error if
  15330. * called with a larger length value.
  15331. * With TLS, \c mbedtls_ssl_write() will fragment the input if
  15332. * necessary and return the number of bytes written; it is up
  15333. * to the caller to call \c mbedtls_ssl_write() again in
  15334. * order to send the remaining bytes if any.
  15335. *
  15336. * \param ssl SSL context
  15337. *
  15338. * \return Current maximum fragment length.
  15339. */
  15340. size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl );
  15341. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  15342. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  15343. /**
  15344. * \brief Return the peer certificate from the current connection
  15345. *
  15346. * Note: Can be NULL in case no certificate was sent during
  15347. * the handshake. Different calls for the same connection can
  15348. * return the same or different pointers for the same
  15349. * certificate and even a different certificate altogether.
  15350. * The peer cert CAN change in a single connection if
  15351. * renegotiation is performed.
  15352. *
  15353. * \param ssl SSL context
  15354. *
  15355. * \return the current peer certificate
  15356. */
  15357. const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl );
  15358. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  15359. #if defined(MBEDTLS_SSL_CLI_C)
  15360. /**
  15361. * \brief Save session in order to resume it later (client-side only)
  15362. * Session data is copied to presented session structure.
  15363. *
  15364. * \warning Currently, peer certificate is lost in the operation.
  15365. *
  15366. * \param ssl SSL context
  15367. * \param session session context
  15368. *
  15369. * \return 0 if successful,
  15370. * MBEDTLS_ERR_SSL_ALLOC_FAILED if memory allocation failed,
  15371. * MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used server-side or
  15372. * arguments are otherwise invalid
  15373. *
  15374. * \sa mbedtls_ssl_set_session()
  15375. */
  15376. int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *session );
  15377. #endif /* MBEDTLS_SSL_CLI_C */
  15378. /**
  15379. * \brief Perform the SSL handshake
  15380. *
  15381. * \param ssl SSL context
  15382. *
  15383. * \return 0 if successful, or
  15384. * MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE, or
  15385. * MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED (see below), or
  15386. * a specific SSL error code.
  15387. *
  15388. * \note If this function returns something other than 0 or
  15389. * MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context
  15390. * becomes unusable, and you should either free it or call
  15391. * \c mbedtls_ssl_session_reset() on it before re-using it for
  15392. * a new connection; the current connection must be closed.
  15393. *
  15394. * \note If DTLS is in use, then you may choose to handle
  15395. * MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED specially for logging
  15396. * purposes, as it is an expected return value rather than an
  15397. * actual error, but you still need to reset/free the context.
  15398. */
  15399. int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl );
  15400. /**
  15401. * \brief Perform a single step of the SSL handshake
  15402. *
  15403. * \note The state of the context (ssl->state) will be at
  15404. * the following state after execution of this function.
  15405. * Do not call this function if state is MBEDTLS_SSL_HANDSHAKE_OVER.
  15406. *
  15407. * \note If this function returns something other than 0 or
  15408. * MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context
  15409. * becomes unusable, and you should either free it or call
  15410. * \c mbedtls_ssl_session_reset() on it before re-using it for
  15411. * a new connection; the current connection must be closed.
  15412. *
  15413. * \param ssl SSL context
  15414. *
  15415. * \return 0 if successful, or
  15416. * MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE, or
  15417. * a specific SSL error code.
  15418. */
  15419. int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl );
  15420. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  15421. /**
  15422. * \brief Initiate an SSL renegotiation on the running connection.
  15423. * Client: perform the renegotiation right now.
  15424. * Server: request renegotiation, which will be performed
  15425. * during the next call to mbedtls_ssl_read() if honored by client.
  15426. *
  15427. * \param ssl SSL context
  15428. *
  15429. * \return 0 if successful, or any mbedtls_ssl_handshake() return value.
  15430. *
  15431. * \note If this function returns something other than 0 or
  15432. * MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context
  15433. * becomes unusable, and you should either free it or call
  15434. * \c mbedtls_ssl_session_reset() on it before re-using it for
  15435. * a new connection; the current connection must be closed.
  15436. */
  15437. int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl );
  15438. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  15439. /**
  15440. * \brief Read at most 'len' application data bytes
  15441. *
  15442. * \param ssl SSL context
  15443. * \param buf buffer that will hold the data
  15444. * \param len maximum number of bytes to read
  15445. *
  15446. * \return the number of bytes read, or
  15447. * 0 for EOF, or
  15448. * MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE, or
  15449. * MBEDTLS_ERR_SSL_CLIENT_RECONNECT (see below), or
  15450. * another negative error code.
  15451. *
  15452. * \note If this function returns something other than a positive
  15453. * value or MBEDTLS_ERR_SSL_WANT_READ/WRITE or
  15454. * MBEDTLS_ERR_SSL_CLIENT_RECONNECT, then the ssl context
  15455. * becomes unusable, and you should either free it or call
  15456. * \c mbedtls_ssl_session_reset() on it before re-using it for
  15457. * a new connection; the current connection must be closed.
  15458. *
  15459. * \note When this function return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
  15460. * (which can only happen server-side), it means that a client
  15461. * is initiating a new connection using the same source port.
  15462. * You can either treat that as a connection close and wait
  15463. * for the client to resend a ClientHello, or directly
  15464. * continue with \c mbedtls_ssl_handshake() with the same
  15465. * context (as it has beeen reset internally). Either way, you
  15466. * should make sure this is seen by the application as a new
  15467. * connection: application state, if any, should be reset, and
  15468. * most importantly the identity of the client must be checked
  15469. * again. WARNING: not validating the identity of the client
  15470. * again, or not transmitting the new identity to the
  15471. * application layer, would allow authentication bypass!
  15472. */
  15473. int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len );
  15474. /**
  15475. * \brief Try to write exactly 'len' application data bytes
  15476. *
  15477. * \warning This function will do partial writes in some cases. If the
  15478. * return value is non-negative but less than length, the
  15479. * function must be called again with updated arguments:
  15480. * buf + ret, len - ret (if ret is the return value) until
  15481. * it returns a value equal to the last 'len' argument.
  15482. *
  15483. * \param ssl SSL context
  15484. * \param buf buffer holding the data
  15485. * \param len how many bytes must be written
  15486. *
  15487. * \return the number of bytes actually written (may be less than len),
  15488. * or MBEDTLS_ERR_SSL_WANT_WRITE of MBEDTLS_ERR_SSL_WANT_READ,
  15489. * or another negative error code.
  15490. *
  15491. * \note If this function returns something other than a positive
  15492. * value or MBEDTLS_ERR_SSL_WANT_READ/WRITE, the ssl context
  15493. * becomes unusable, and you should either free it or call
  15494. * \c mbedtls_ssl_session_reset() on it before re-using it for
  15495. * a new connection; the current connection must be closed.
  15496. *
  15497. * \note When this function returns MBEDTLS_ERR_SSL_WANT_WRITE/READ,
  15498. * it must be called later with the *same* arguments,
  15499. * until it returns a positive value.
  15500. *
  15501. * \note If the requested length is greater than the maximum
  15502. * fragment length (either the built-in limit or the one set
  15503. * or negotiated with the peer), then:
  15504. * - with TLS, less bytes than requested are written.
  15505. * - with DTLS, MBEDTLS_ERR_SSL_BAD_INPUT_DATA is returned.
  15506. * \c mbedtls_ssl_get_max_frag_len() may be used to query the
  15507. * active maximum fragment length.
  15508. */
  15509. int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len );
  15510. /**
  15511. * \brief Send an alert message
  15512. *
  15513. * \param ssl SSL context
  15514. * \param level The alert level of the message
  15515. * (MBEDTLS_SSL_ALERT_LEVEL_WARNING or MBEDTLS_SSL_ALERT_LEVEL_FATAL)
  15516. * \param message The alert message (SSL_ALERT_MSG_*)
  15517. *
  15518. * \return 0 if successful, or a specific SSL error code.
  15519. *
  15520. * \note If this function returns something other than 0 or
  15521. * MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context
  15522. * becomes unusable, and you should either free it or call
  15523. * \c mbedtls_ssl_session_reset() on it before re-using it for
  15524. * a new connection; the current connection must be closed.
  15525. */
  15526. int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
  15527. unsigned char level,
  15528. unsigned char message );
  15529. /**
  15530. * \brief Notify the peer that the connection is being closed
  15531. *
  15532. * \param ssl SSL context
  15533. *
  15534. * \return 0 if successful, or a specific SSL error code.
  15535. *
  15536. * \note If this function returns something other than 0 or
  15537. * MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context
  15538. * becomes unusable, and you should either free it or call
  15539. * \c mbedtls_ssl_session_reset() on it before re-using it for
  15540. * a new connection; the current connection must be closed.
  15541. */
  15542. int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl );
  15543. /**
  15544. * \brief Free referenced items in an SSL context and clear memory
  15545. *
  15546. * \param ssl SSL context
  15547. */
  15548. void mbedtls_ssl_free( mbedtls_ssl_context *ssl );
  15549. /**
  15550. * \brief Initialize an SSL configuration context
  15551. * Just makes the context ready for
  15552. * mbedtls_ssl_config_defaults() or mbedtls_ssl_config_free().
  15553. *
  15554. * \note You need to call mbedtls_ssl_config_defaults() unless you
  15555. * manually set all of the relevent fields yourself.
  15556. *
  15557. * \param conf SSL configuration context
  15558. */
  15559. void mbedtls_ssl_config_init( mbedtls_ssl_config *conf );
  15560. /**
  15561. * \brief Load reasonnable default SSL configuration values.
  15562. * (You need to call mbedtls_ssl_config_init() first.)
  15563. *
  15564. * \param conf SSL configuration context
  15565. * \param endpoint MBEDTLS_SSL_IS_CLIENT or MBEDTLS_SSL_IS_SERVER
  15566. * \param transport MBEDTLS_SSL_TRANSPORT_STREAM for TLS, or
  15567. * MBEDTLS_SSL_TRANSPORT_DATAGRAM for DTLS
  15568. * \param preset a MBEDTLS_SSL_PRESET_XXX value
  15569. * (currently unused).
  15570. *
  15571. * \note See \c mbedtls_ssl_conf_transport() for notes on DTLS.
  15572. *
  15573. * \return 0 if successful, or
  15574. * MBEDTLS_ERR_XXX_ALLOC_FAILED on memory allocation error.
  15575. */
  15576. int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
  15577. int endpoint, int transport, int preset );
  15578. /**
  15579. * \brief Free an SSL configuration context
  15580. *
  15581. * \param conf SSL configuration context
  15582. */
  15583. void mbedtls_ssl_config_free( mbedtls_ssl_config *conf );
  15584. /**
  15585. * \brief Initialize SSL session structure
  15586. *
  15587. * \param session SSL session
  15588. */
  15589. void mbedtls_ssl_session_init( mbedtls_ssl_session *session );
  15590. /**
  15591. * \brief Free referenced items in an SSL session including the
  15592. * peer certificate and clear memory
  15593. *
  15594. * \param session SSL session
  15595. */
  15596. void mbedtls_ssl_session_free( mbedtls_ssl_session *session );
  15597. #ifdef __cplusplus
  15598. }
  15599. #endif
  15600. #endif /* ssl.h */
  15601. /********* Start of file include/mbedtls/ssl_cookie.h ************/
  15602. /**
  15603. * \file ssl_cookie.h
  15604. *
  15605. * \brief DTLS cookie callbacks implementation
  15606. *
  15607. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  15608. * SPDX-License-Identifier: Apache-2.0
  15609. *
  15610. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  15611. * not use this file except in compliance with the License.
  15612. * You may obtain a copy of the License at
  15613. *
  15614. * http://www.apache.org/licenses/LICENSE-2.0
  15615. *
  15616. * Unless required by applicable law or agreed to in writing, software
  15617. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15618. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15619. * See the License for the specific language governing permissions and
  15620. * limitations under the License.
  15621. *
  15622. * This file is part of mbed TLS (https://tls.mbed.org)
  15623. */
  15624. #ifndef MBEDTLS_SSL_COOKIE_H
  15625. #define MBEDTLS_SSL_COOKIE_H
  15626. #if defined(MBEDTLS_THREADING_C)
  15627. #endif
  15628. /**
  15629. * \name SECTION: Module settings
  15630. *
  15631. * The configuration options you can set for this module are in this section.
  15632. * Either change them in config.h or define them on the compiler command line.
  15633. * \{
  15634. */
  15635. #ifndef MBEDTLS_SSL_COOKIE_TIMEOUT
  15636. #define MBEDTLS_SSL_COOKIE_TIMEOUT 60 /**< Default expiration delay of DTLS cookies, in seconds if HAVE_TIME, or in number of cookies issued */
  15637. #endif
  15638. /* \} name SECTION: Module settings */
  15639. #ifdef __cplusplus
  15640. extern "C" {
  15641. #endif
  15642. /**
  15643. * \brief Context for the default cookie functions.
  15644. */
  15645. typedef struct
  15646. {
  15647. mbedtls_md_context_t hmac_ctx; /*!< context for the HMAC portion */
  15648. #if !defined(MBEDTLS_HAVE_TIME)
  15649. unsigned long serial; /*!< serial number for expiration */
  15650. #endif
  15651. unsigned long timeout; /*!< timeout delay, in seconds if HAVE_TIME,
  15652. or in number of tickets issued */
  15653. #if defined(MBEDTLS_THREADING_C)
  15654. mbedtls_threading_mutex_t mutex;
  15655. #endif
  15656. } mbedtls_ssl_cookie_ctx;
  15657. /**
  15658. * \brief Initialize cookie context
  15659. */
  15660. void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx );
  15661. /**
  15662. * \brief Setup cookie context (generate keys)
  15663. */
  15664. int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
  15665. int (*f_rng)(void *, unsigned char *, size_t),
  15666. void *p_rng );
  15667. /**
  15668. * \brief Set expiration delay for cookies
  15669. * (Default MBEDTLS_SSL_COOKIE_TIMEOUT)
  15670. *
  15671. * \param ctx Cookie contex
  15672. * \param delay Delay, in seconds if HAVE_TIME, or in number of cookies
  15673. * issued in the meantime.
  15674. * 0 to disable expiration (NOT recommended)
  15675. */
  15676. void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay );
  15677. /**
  15678. * \brief Free cookie context
  15679. */
  15680. void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx );
  15681. /**
  15682. * \brief Generate cookie, see \c mbedtls_ssl_cookie_write_t
  15683. */
  15684. mbedtls_ssl_cookie_write_t mbedtls_ssl_cookie_write;
  15685. /**
  15686. * \brief Verify cookie, see \c mbedtls_ssl_cookie_write_t
  15687. */
  15688. mbedtls_ssl_cookie_check_t mbedtls_ssl_cookie_check;
  15689. #ifdef __cplusplus
  15690. }
  15691. #endif
  15692. #endif /* ssl_cookie.h */
  15693. /********* Start of file include/mbedtls/ssl_internal.h ************/
  15694. /**
  15695. * \file ssl_ticket.h
  15696. *
  15697. * \brief Internal functions shared by the SSL modules
  15698. *
  15699. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  15700. * SPDX-License-Identifier: Apache-2.0
  15701. *
  15702. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  15703. * not use this file except in compliance with the License.
  15704. * You may obtain a copy of the License at
  15705. *
  15706. * http://www.apache.org/licenses/LICENSE-2.0
  15707. *
  15708. * Unless required by applicable law or agreed to in writing, software
  15709. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15710. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15711. * See the License for the specific language governing permissions and
  15712. * limitations under the License.
  15713. *
  15714. * This file is part of mbed TLS (https://tls.mbed.org)
  15715. */
  15716. #ifndef MBEDTLS_SSL_INTERNAL_H
  15717. #define MBEDTLS_SSL_INTERNAL_H
  15718. #if defined(MBEDTLS_MD5_C)
  15719. #endif
  15720. #if defined(MBEDTLS_SHA1_C)
  15721. #endif
  15722. #if defined(MBEDTLS_SHA256_C)
  15723. #endif
  15724. #if defined(MBEDTLS_SHA512_C)
  15725. #endif
  15726. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  15727. #endif
  15728. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  15729. !defined(inline) && !defined(__cplusplus)
  15730. #define inline __inline
  15731. #endif
  15732. /* Determine minimum supported version */
  15733. #define MBEDTLS_SSL_MIN_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
  15734. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  15735. #define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_0
  15736. #else
  15737. #if defined(MBEDTLS_SSL_PROTO_TLS1)
  15738. #define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
  15739. #else
  15740. #if defined(MBEDTLS_SSL_PROTO_TLS1_1)
  15741. #define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_2
  15742. #else
  15743. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  15744. #define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
  15745. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  15746. #endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
  15747. #endif /* MBEDTLS_SSL_PROTO_TLS1 */
  15748. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  15749. /* Determine maximum supported version */
  15750. #define MBEDTLS_SSL_MAX_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
  15751. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  15752. #define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
  15753. #else
  15754. #if defined(MBEDTLS_SSL_PROTO_TLS1_1)
  15755. #define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_2
  15756. #else
  15757. #if defined(MBEDTLS_SSL_PROTO_TLS1)
  15758. #define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
  15759. #else
  15760. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  15761. #define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_0
  15762. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  15763. #endif /* MBEDTLS_SSL_PROTO_TLS1 */
  15764. #endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
  15765. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  15766. #define MBEDTLS_SSL_INITIAL_HANDSHAKE 0
  15767. #define MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS 1 /* In progress */
  15768. #define MBEDTLS_SSL_RENEGOTIATION_DONE 2 /* Done or aborted */
  15769. #define MBEDTLS_SSL_RENEGOTIATION_PENDING 3 /* Requested (server only) */
  15770. /*
  15771. * DTLS retransmission states, see RFC 6347 4.2.4
  15772. *
  15773. * The SENDING state is merged in PREPARING for initial sends,
  15774. * but is distinct for resends.
  15775. *
  15776. * Note: initial state is wrong for server, but is not used anyway.
  15777. */
  15778. #define MBEDTLS_SSL_RETRANS_PREPARING 0
  15779. #define MBEDTLS_SSL_RETRANS_SENDING 1
  15780. #define MBEDTLS_SSL_RETRANS_WAITING 2
  15781. #define MBEDTLS_SSL_RETRANS_FINISHED 3
  15782. /*
  15783. * Allow extra bytes for record, authentication and encryption overhead:
  15784. * counter (8) + header (5) + IV(16) + MAC (16-48) + padding (0-256)
  15785. * and allow for a maximum of 1024 of compression expansion if
  15786. * enabled.
  15787. */
  15788. #if defined(MBEDTLS_ZLIB_SUPPORT)
  15789. #define MBEDTLS_SSL_COMPRESSION_ADD 1024
  15790. #else
  15791. #define MBEDTLS_SSL_COMPRESSION_ADD 0
  15792. #endif
  15793. #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_MODE_CBC)
  15794. /* Ciphersuites using HMAC */
  15795. #if defined(MBEDTLS_SHA512_C)
  15796. #define MBEDTLS_SSL_MAC_ADD 48 /* SHA-384 used for HMAC */
  15797. #elif defined(MBEDTLS_SHA256_C)
  15798. #define MBEDTLS_SSL_MAC_ADD 32 /* SHA-256 used for HMAC */
  15799. #else
  15800. #define MBEDTLS_SSL_MAC_ADD 20 /* SHA-1 used for HMAC */
  15801. #endif
  15802. #else
  15803. /* AEAD ciphersuites: GCM and CCM use a 128 bits tag */
  15804. #define MBEDTLS_SSL_MAC_ADD 16
  15805. #endif
  15806. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  15807. #define MBEDTLS_SSL_PADDING_ADD 256
  15808. #else
  15809. #define MBEDTLS_SSL_PADDING_ADD 0
  15810. #endif
  15811. #define MBEDTLS_SSL_BUFFER_LEN ( MBEDTLS_SSL_MAX_CONTENT_LEN \
  15812. + MBEDTLS_SSL_COMPRESSION_ADD \
  15813. + 29 /* counter + header + IV */ \
  15814. + MBEDTLS_SSL_MAC_ADD \
  15815. + MBEDTLS_SSL_PADDING_ADD \
  15816. )
  15817. /*
  15818. * TLS extension flags (for extensions with outgoing ServerHello content
  15819. * that need it (e.g. for RENEGOTIATION_INFO the server already knows because
  15820. * of state of the renegotiation flag, so no indicator is required)
  15821. */
  15822. #define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT (1 << 0)
  15823. #define MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK (1 << 1)
  15824. #ifdef __cplusplus
  15825. extern "C" {
  15826. #endif
  15827. /*
  15828. * This structure contains the parameters only needed during handshake.
  15829. */
  15830. struct mbedtls_ssl_handshake_params
  15831. {
  15832. /*
  15833. * Handshake specific crypto variables
  15834. */
  15835. int sig_alg; /*!< Hash algorithm for signature */
  15836. int cert_type; /*!< Requested cert type */
  15837. int verify_sig_alg; /*!< Signature algorithm for verify */
  15838. #if defined(MBEDTLS_DHM_C)
  15839. mbedtls_dhm_context dhm_ctx; /*!< DHM key exchange */
  15840. #endif
  15841. #if defined(MBEDTLS_ECDH_C)
  15842. mbedtls_ecdh_context ecdh_ctx; /*!< ECDH key exchange */
  15843. #endif
  15844. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  15845. mbedtls_ecjpake_context ecjpake_ctx; /*!< EC J-PAKE key exchange */
  15846. #if defined(MBEDTLS_SSL_CLI_C)
  15847. unsigned char *ecjpake_cache; /*!< Cache for ClientHello ext */
  15848. size_t ecjpake_cache_len; /*!< Length of cached data */
  15849. #endif
  15850. #endif
  15851. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  15852. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  15853. const mbedtls_ecp_curve_info **curves; /*!< Supported elliptic curves */
  15854. #endif
  15855. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  15856. unsigned char *psk; /*!< PSK from the callback */
  15857. size_t psk_len; /*!< Length of PSK from callback */
  15858. #endif
  15859. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  15860. mbedtls_ssl_key_cert *key_cert; /*!< chosen key/cert pair (server) */
  15861. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  15862. int sni_authmode; /*!< authmode from SNI callback */
  15863. mbedtls_ssl_key_cert *sni_key_cert; /*!< key/cert list from SNI */
  15864. mbedtls_x509_crt *sni_ca_chain; /*!< trusted CAs from SNI callback */
  15865. mbedtls_x509_crl *sni_ca_crl; /*!< trusted CAs CRLs from SNI */
  15866. #endif
  15867. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  15868. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  15869. unsigned int out_msg_seq; /*!< Outgoing handshake sequence number */
  15870. unsigned int in_msg_seq; /*!< Incoming handshake sequence number */
  15871. unsigned char *verify_cookie; /*!< Cli: HelloVerifyRequest cookie
  15872. Srv: unused */
  15873. unsigned char verify_cookie_len; /*!< Cli: cookie length
  15874. Srv: flag for sending a cookie */
  15875. unsigned char *hs_msg; /*!< Reassembled handshake message */
  15876. uint32_t retransmit_timeout; /*!< Current value of timeout */
  15877. unsigned char retransmit_state; /*!< Retransmission state */
  15878. mbedtls_ssl_flight_item *flight; /*!< Current outgoing flight */
  15879. mbedtls_ssl_flight_item *cur_msg; /*!< Current message in flight */
  15880. unsigned int in_flight_start_seq; /*!< Minimum message sequence in the
  15881. flight being received */
  15882. mbedtls_ssl_transform *alt_transform_out; /*!< Alternative transform for
  15883. resending messages */
  15884. unsigned char alt_out_ctr[8]; /*!< Alternative record epoch/counter
  15885. for resending messages */
  15886. #endif
  15887. /*
  15888. * Checksum contexts
  15889. */
  15890. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  15891. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  15892. mbedtls_md5_context fin_md5;
  15893. mbedtls_sha1_context fin_sha1;
  15894. #endif
  15895. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  15896. #if defined(MBEDTLS_SHA256_C)
  15897. mbedtls_sha256_context fin_sha256;
  15898. #endif
  15899. #if defined(MBEDTLS_SHA512_C)
  15900. mbedtls_sha512_context fin_sha512;
  15901. #endif
  15902. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  15903. void (*update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t);
  15904. void (*calc_verify)(mbedtls_ssl_context *, unsigned char *);
  15905. void (*calc_finished)(mbedtls_ssl_context *, unsigned char *, int);
  15906. int (*tls_prf)(const unsigned char *, size_t, const char *,
  15907. const unsigned char *, size_t,
  15908. unsigned char *, size_t);
  15909. size_t pmslen; /*!< premaster length */
  15910. unsigned char randbytes[64]; /*!< random bytes */
  15911. unsigned char premaster[MBEDTLS_PREMASTER_SIZE];
  15912. /*!< premaster secret */
  15913. int resume; /*!< session resume indicator*/
  15914. int max_major_ver; /*!< max. major version client*/
  15915. int max_minor_ver; /*!< max. minor version client*/
  15916. int cli_exts; /*!< client extension presence*/
  15917. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  15918. int new_session_ticket; /*!< use NewSessionTicket? */
  15919. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  15920. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  15921. int extended_ms; /*!< use Extended Master Secret? */
  15922. #endif
  15923. };
  15924. /*
  15925. * This structure contains a full set of runtime transform parameters
  15926. * either in negotiation or active.
  15927. */
  15928. struct mbedtls_ssl_transform
  15929. {
  15930. /*
  15931. * Session specific crypto layer
  15932. */
  15933. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  15934. /*!< Chosen cipersuite_info */
  15935. unsigned int keylen; /*!< symmetric key length (bytes) */
  15936. size_t minlen; /*!< min. ciphertext length */
  15937. size_t ivlen; /*!< IV length */
  15938. size_t fixed_ivlen; /*!< Fixed part of IV (AEAD) */
  15939. size_t maclen; /*!< MAC length */
  15940. unsigned char iv_enc[16]; /*!< IV (encryption) */
  15941. unsigned char iv_dec[16]; /*!< IV (decryption) */
  15942. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  15943. /* Needed only for SSL v3.0 secret */
  15944. unsigned char mac_enc[20]; /*!< SSL v3.0 secret (enc) */
  15945. unsigned char mac_dec[20]; /*!< SSL v3.0 secret (dec) */
  15946. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  15947. mbedtls_md_context_t md_ctx_enc; /*!< MAC (encryption) */
  15948. mbedtls_md_context_t md_ctx_dec; /*!< MAC (decryption) */
  15949. mbedtls_cipher_context_t cipher_ctx_enc; /*!< encryption context */
  15950. mbedtls_cipher_context_t cipher_ctx_dec; /*!< decryption context */
  15951. /*
  15952. * Session specific compression layer
  15953. */
  15954. #if defined(MBEDTLS_ZLIB_SUPPORT)
  15955. z_stream ctx_deflate; /*!< compression context */
  15956. z_stream ctx_inflate; /*!< decompression context */
  15957. #endif
  15958. };
  15959. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  15960. /*
  15961. * List of certificate + private key pairs
  15962. */
  15963. struct mbedtls_ssl_key_cert
  15964. {
  15965. mbedtls_x509_crt *cert; /*!< cert */
  15966. mbedtls_pk_context *key; /*!< private key */
  15967. mbedtls_ssl_key_cert *next; /*!< next key/cert pair */
  15968. };
  15969. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  15970. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  15971. /*
  15972. * List of handshake messages kept around for resending
  15973. */
  15974. struct mbedtls_ssl_flight_item
  15975. {
  15976. unsigned char *p; /*!< message, including handshake headers */
  15977. size_t len; /*!< length of p */
  15978. unsigned char type; /*!< type of the message: handshake or CCS */
  15979. mbedtls_ssl_flight_item *next; /*!< next handshake message(s) */
  15980. };
  15981. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  15982. /**
  15983. * \brief Free referenced items in an SSL transform context and clear
  15984. * memory
  15985. *
  15986. * \param transform SSL transform context
  15987. */
  15988. void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform );
  15989. /**
  15990. * \brief Free referenced items in an SSL handshake context and clear
  15991. * memory
  15992. *
  15993. * \param handshake SSL handshake context
  15994. */
  15995. void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake );
  15996. int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl );
  15997. int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl );
  15998. void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl );
  15999. int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl );
  16000. void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl );
  16001. int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl );
  16002. int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl );
  16003. int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want );
  16004. int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl );
  16005. int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl );
  16006. int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl );
  16007. int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl );
  16008. int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl );
  16009. int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl );
  16010. int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl );
  16011. int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl );
  16012. void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
  16013. const mbedtls_ssl_ciphersuite_t *ciphersuite_info );
  16014. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  16015. int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex );
  16016. #endif
  16017. #if defined(MBEDTLS_PK_C)
  16018. unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk );
  16019. mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig );
  16020. #endif
  16021. mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash );
  16022. unsigned char mbedtls_ssl_hash_from_md_alg( int md );
  16023. #if defined(MBEDTLS_ECP_C)
  16024. int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id );
  16025. #endif
  16026. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  16027. int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
  16028. mbedtls_md_type_t md );
  16029. #endif
  16030. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  16031. static inline mbedtls_pk_context *mbedtls_ssl_own_key( mbedtls_ssl_context *ssl )
  16032. {
  16033. mbedtls_ssl_key_cert *key_cert;
  16034. if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
  16035. key_cert = ssl->handshake->key_cert;
  16036. else
  16037. key_cert = ssl->conf->key_cert;
  16038. return( key_cert == NULL ? NULL : key_cert->key );
  16039. }
  16040. static inline mbedtls_x509_crt *mbedtls_ssl_own_cert( mbedtls_ssl_context *ssl )
  16041. {
  16042. mbedtls_ssl_key_cert *key_cert;
  16043. if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
  16044. key_cert = ssl->handshake->key_cert;
  16045. else
  16046. key_cert = ssl->conf->key_cert;
  16047. return( key_cert == NULL ? NULL : key_cert->cert );
  16048. }
  16049. /*
  16050. * Check usage of a certificate wrt extensions:
  16051. * keyUsage, extendedKeyUsage (later), and nSCertType (later).
  16052. *
  16053. * Warning: cert_endpoint is the endpoint of the cert (ie, of our peer when we
  16054. * check a cert we received from them)!
  16055. *
  16056. * Return 0 if everything is OK, -1 if not.
  16057. */
  16058. int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
  16059. const mbedtls_ssl_ciphersuite_t *ciphersuite,
  16060. int cert_endpoint,
  16061. uint32_t *flags );
  16062. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  16063. void mbedtls_ssl_write_version( int major, int minor, int transport,
  16064. unsigned char ver[2] );
  16065. void mbedtls_ssl_read_version( int *major, int *minor, int transport,
  16066. const unsigned char ver[2] );
  16067. static inline size_t mbedtls_ssl_hdr_len( const mbedtls_ssl_context *ssl )
  16068. {
  16069. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  16070. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  16071. return( 13 );
  16072. #else
  16073. ((void) ssl);
  16074. #endif
  16075. return( 5 );
  16076. }
  16077. static inline size_t mbedtls_ssl_hs_hdr_len( const mbedtls_ssl_context *ssl )
  16078. {
  16079. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  16080. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  16081. return( 12 );
  16082. #else
  16083. ((void) ssl);
  16084. #endif
  16085. return( 4 );
  16086. }
  16087. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  16088. void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl );
  16089. void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl );
  16090. int mbedtls_ssl_resend( mbedtls_ssl_context *ssl );
  16091. #endif
  16092. /* Visible for testing purposes only */
  16093. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  16094. int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl );
  16095. void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl );
  16096. #endif
  16097. /* constant-time buffer comparison */
  16098. static inline int mbedtls_ssl_safer_memcmp( const void *a, const void *b, size_t n )
  16099. {
  16100. size_t i;
  16101. const unsigned char *A = (const unsigned char *) a;
  16102. const unsigned char *B = (const unsigned char *) b;
  16103. unsigned char diff = 0;
  16104. for( i = 0; i < n; i++ )
  16105. diff |= A[i] ^ B[i];
  16106. return( diff );
  16107. }
  16108. #ifdef __cplusplus
  16109. }
  16110. #endif
  16111. #endif /* ssl_internal.h */
  16112. /********* Start of file include/mbedtls/ssl_cache.h ************/
  16113. /**
  16114. * \file ssl_cache.h
  16115. *
  16116. * \brief SSL session cache implementation
  16117. *
  16118. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  16119. * SPDX-License-Identifier: Apache-2.0
  16120. *
  16121. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  16122. * not use this file except in compliance with the License.
  16123. * You may obtain a copy of the License at
  16124. *
  16125. * http://www.apache.org/licenses/LICENSE-2.0
  16126. *
  16127. * Unless required by applicable law or agreed to in writing, software
  16128. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16129. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16130. * See the License for the specific language governing permissions and
  16131. * limitations under the License.
  16132. *
  16133. * This file is part of mbed TLS (https://tls.mbed.org)
  16134. */
  16135. #ifndef MBEDTLS_SSL_CACHE_H
  16136. #define MBEDTLS_SSL_CACHE_H
  16137. #if defined(MBEDTLS_THREADING_C)
  16138. #endif
  16139. /**
  16140. * \name SECTION: Module settings
  16141. *
  16142. * The configuration options you can set for this module are in this section.
  16143. * Either change them in config.h or define them on the compiler command line.
  16144. * \{
  16145. */
  16146. #if !defined(MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT)
  16147. #define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT 86400 /*!< 1 day */
  16148. #endif
  16149. #if !defined(MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES)
  16150. #define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /*!< Maximum entries in cache */
  16151. #endif
  16152. /* \} name SECTION: Module settings */
  16153. #ifdef __cplusplus
  16154. extern "C" {
  16155. #endif
  16156. typedef struct mbedtls_ssl_cache_context mbedtls_ssl_cache_context;
  16157. typedef struct mbedtls_ssl_cache_entry mbedtls_ssl_cache_entry;
  16158. /**
  16159. * \brief This structure is used for storing cache entries
  16160. */
  16161. struct mbedtls_ssl_cache_entry
  16162. {
  16163. #if defined(MBEDTLS_HAVE_TIME)
  16164. time_t timestamp; /*!< entry timestamp */
  16165. #endif
  16166. mbedtls_ssl_session session; /*!< entry session */
  16167. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  16168. mbedtls_x509_buf peer_cert; /*!< entry peer_cert */
  16169. #endif
  16170. mbedtls_ssl_cache_entry *next; /*!< chain pointer */
  16171. };
  16172. /**
  16173. * \brief Cache context
  16174. */
  16175. struct mbedtls_ssl_cache_context
  16176. {
  16177. mbedtls_ssl_cache_entry *chain; /*!< start of the chain */
  16178. int timeout; /*!< cache entry timeout */
  16179. int max_entries; /*!< maximum entries */
  16180. #if defined(MBEDTLS_THREADING_C)
  16181. mbedtls_threading_mutex_t mutex; /*!< mutex */
  16182. #endif
  16183. };
  16184. /**
  16185. * \brief Initialize an SSL cache context
  16186. *
  16187. * \param cache SSL cache context
  16188. */
  16189. void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache );
  16190. /**
  16191. * \brief Cache get callback implementation
  16192. * (Thread-safe if MBEDTLS_THREADING_C is enabled)
  16193. *
  16194. * \param data SSL cache context
  16195. * \param session session to retrieve entry for
  16196. */
  16197. int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session );
  16198. /**
  16199. * \brief Cache set callback implementation
  16200. * (Thread-safe if MBEDTLS_THREADING_C is enabled)
  16201. *
  16202. * \param data SSL cache context
  16203. * \param session session to store entry for
  16204. */
  16205. int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session );
  16206. #if defined(MBEDTLS_HAVE_TIME)
  16207. /**
  16208. * \brief Set the cache timeout
  16209. * (Default: MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT (1 day))
  16210. *
  16211. * A timeout of 0 indicates no timeout.
  16212. *
  16213. * \param cache SSL cache context
  16214. * \param timeout cache entry timeout in seconds
  16215. */
  16216. void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout );
  16217. #endif /* MBEDTLS_HAVE_TIME */
  16218. /**
  16219. * \brief Set the maximum number of cache entries
  16220. * (Default: MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES (50))
  16221. *
  16222. * \param cache SSL cache context
  16223. * \param max cache entry maximum
  16224. */
  16225. void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max );
  16226. /**
  16227. * \brief Free referenced items in a cache context and clear memory
  16228. *
  16229. * \param cache SSL cache context
  16230. */
  16231. void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache );
  16232. #ifdef __cplusplus
  16233. }
  16234. #endif
  16235. #endif /* ssl_cache.h */
  16236. /********* Start of file include/mbedtls/ssl_ticket.h ************/
  16237. /**
  16238. * \file ssl_ticket.h
  16239. *
  16240. * \brief TLS server ticket callbacks implementation
  16241. *
  16242. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  16243. * SPDX-License-Identifier: Apache-2.0
  16244. *
  16245. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  16246. * not use this file except in compliance with the License.
  16247. * You may obtain a copy of the License at
  16248. *
  16249. * http://www.apache.org/licenses/LICENSE-2.0
  16250. *
  16251. * Unless required by applicable law or agreed to in writing, software
  16252. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16253. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16254. * See the License for the specific language governing permissions and
  16255. * limitations under the License.
  16256. *
  16257. * This file is part of mbed TLS (https://tls.mbed.org)
  16258. */
  16259. #ifndef MBEDTLS_SSL_TICKET_H
  16260. #define MBEDTLS_SSL_TICKET_H
  16261. /*
  16262. * This implementation of the session ticket callbacks includes key
  16263. * management, rotating the keys periodically in order to preserve forward
  16264. * secrecy, when MBEDTLS_HAVE_TIME is defined.
  16265. */
  16266. #if defined(MBEDTLS_THREADING_C)
  16267. #endif
  16268. #ifdef __cplusplus
  16269. extern "C" {
  16270. #endif
  16271. /**
  16272. * \brief Information for session ticket protection
  16273. */
  16274. typedef struct
  16275. {
  16276. unsigned char name[4]; /*!< random key identifier */
  16277. uint32_t generation_time; /*!< key generation timestamp (seconds) */
  16278. mbedtls_cipher_context_t ctx; /*!< context for auth enc/decryption */
  16279. }
  16280. mbedtls_ssl_ticket_key;
  16281. /**
  16282. * \brief Context for session ticket handling functions
  16283. */
  16284. typedef struct
  16285. {
  16286. mbedtls_ssl_ticket_key keys[2]; /*!< ticket protection keys */
  16287. unsigned char active; /*!< index of the currently active key */
  16288. uint32_t ticket_lifetime; /*!< lifetime of tickets in seconds */
  16289. /** Callback for getting (pseudo-)random numbers */
  16290. int (*f_rng)(void *, unsigned char *, size_t);
  16291. void *p_rng; /*!< context for the RNG function */
  16292. #if defined(MBEDTLS_THREADING_C)
  16293. mbedtls_threading_mutex_t mutex;
  16294. #endif
  16295. }
  16296. mbedtls_ssl_ticket_context;
  16297. /**
  16298. * \brief Initialize a ticket context.
  16299. * (Just make it ready for mbedtls_ssl_ticket_setup()
  16300. * or mbedtls_ssl_ticket_free().)
  16301. *
  16302. * \param ctx Context to be initialized
  16303. */
  16304. void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx );
  16305. /**
  16306. * \brief Prepare context to be actually used
  16307. *
  16308. * \param ctx Context to be set up
  16309. * \param f_rng RNG callback function
  16310. * \param p_rng RNG callback context
  16311. * \param cipher AEAD cipher to use for ticket protection.
  16312. * Recommended value: MBEDTLS_CIPHER_AES_256_GCM.
  16313. * \param lifetime Tickets lifetime in seconds
  16314. * Recommended value: 86400 (one day).
  16315. *
  16316. * \note It is highly recommended to select a cipher that is at
  16317. * least as strong as the the strongest ciphersuite
  16318. * supported. Usually that means a 256-bit key.
  16319. *
  16320. * \note The lifetime of the keys is twice the lifetime of tickets.
  16321. * It is recommended to pick a reasonnable lifetime so as not
  16322. * to negate the benefits of forward secrecy.
  16323. *
  16324. * \return 0 if successful,
  16325. * or a specific MBEDTLS_ERR_XXX error code
  16326. */
  16327. int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx,
  16328. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  16329. mbedtls_cipher_type_t cipher,
  16330. uint32_t lifetime );
  16331. /**
  16332. * \brief Implementation of the ticket write callback
  16333. *
  16334. * \note See \c mbedlts_ssl_ticket_write_t for description
  16335. */
  16336. mbedtls_ssl_ticket_write_t mbedtls_ssl_ticket_write;
  16337. /**
  16338. * \brief Implementation of the ticket parse callback
  16339. *
  16340. * \note See \c mbedlts_ssl_ticket_parse_t for description
  16341. */
  16342. mbedtls_ssl_ticket_parse_t mbedtls_ssl_ticket_parse;
  16343. /**
  16344. * \brief Free a context's content and zeroize it.
  16345. *
  16346. * \param ctx Context to be cleaned up
  16347. */
  16348. void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx );
  16349. #ifdef __cplusplus
  16350. }
  16351. #endif
  16352. #endif /* ssl_ticket.h */
  16353. /********* Start of file include/mbedtls/debug.h ************/
  16354. /**
  16355. * \file debug.h
  16356. *
  16357. * \brief Debug functions
  16358. *
  16359. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  16360. * SPDX-License-Identifier: Apache-2.0
  16361. *
  16362. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  16363. * not use this file except in compliance with the License.
  16364. * You may obtain a copy of the License at
  16365. *
  16366. * http://www.apache.org/licenses/LICENSE-2.0
  16367. *
  16368. * Unless required by applicable law or agreed to in writing, software
  16369. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16370. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16371. * See the License for the specific language governing permissions and
  16372. * limitations under the License.
  16373. *
  16374. * This file is part of mbed TLS (https://tls.mbed.org)
  16375. */
  16376. #ifndef MBEDTLS_DEBUG_H
  16377. #define MBEDTLS_DEBUG_H
  16378. #if !defined(MBEDTLS_CONFIG_FILE)
  16379. #else
  16380. #endif
  16381. #if defined(MBEDTLS_ECP_C)
  16382. #endif
  16383. #if defined(MBEDTLS_DEBUG_C)
  16384. #define MBEDTLS_DEBUG_STRIP_PARENS( ... ) __VA_ARGS__
  16385. #define MBEDTLS_SSL_DEBUG_MSG( level, args ) \
  16386. mbedtls_debug_print_msg( ssl, level, __FILE__, __LINE__, \
  16387. MBEDTLS_DEBUG_STRIP_PARENS args )
  16388. #define MBEDTLS_SSL_DEBUG_RET( level, text, ret ) \
  16389. mbedtls_debug_print_ret( ssl, level, __FILE__, __LINE__, text, ret )
  16390. #define MBEDTLS_SSL_DEBUG_BUF( level, text, buf, len ) \
  16391. mbedtls_debug_print_buf( ssl, level, __FILE__, __LINE__, text, buf, len )
  16392. #if defined(MBEDTLS_BIGNUM_C)
  16393. #define MBEDTLS_SSL_DEBUG_MPI( level, text, X ) \
  16394. mbedtls_debug_print_mpi( ssl, level, __FILE__, __LINE__, text, X )
  16395. #endif
  16396. #if defined(MBEDTLS_ECP_C)
  16397. #define MBEDTLS_SSL_DEBUG_ECP( level, text, X ) \
  16398. mbedtls_debug_print_ecp( ssl, level, __FILE__, __LINE__, text, X )
  16399. #endif
  16400. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  16401. #define MBEDTLS_SSL_DEBUG_CRT( level, text, crt ) \
  16402. mbedtls_debug_print_crt( ssl, level, __FILE__, __LINE__, text, crt )
  16403. #endif
  16404. #else /* MBEDTLS_DEBUG_C */
  16405. #define MBEDTLS_SSL_DEBUG_MSG( level, args ) do { } while( 0 )
  16406. #define MBEDTLS_SSL_DEBUG_RET( level, text, ret ) do { } while( 0 )
  16407. #define MBEDTLS_SSL_DEBUG_BUF( level, text, buf, len ) do { } while( 0 )
  16408. #define MBEDTLS_SSL_DEBUG_MPI( level, text, X ) do { } while( 0 )
  16409. #define MBEDTLS_SSL_DEBUG_ECP( level, text, X ) do { } while( 0 )
  16410. #define MBEDTLS_SSL_DEBUG_CRT( level, text, crt ) do { } while( 0 )
  16411. #endif /* MBEDTLS_DEBUG_C */
  16412. #ifdef __cplusplus
  16413. extern "C" {
  16414. #endif
  16415. /**
  16416. * \brief Set the level threshold to handle globally. Messages that have a
  16417. * level over the threshold value are ignored.
  16418. * (Default value: 0 (No debug))
  16419. *
  16420. * \param threshold maximum level of messages to pass on
  16421. */
  16422. void mbedtls_debug_set_threshold( int threshold );
  16423. void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
  16424. const char *file, int line,
  16425. const char *format, ... );
  16426. void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
  16427. const char *file, int line,
  16428. const char *text, int ret );
  16429. void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
  16430. const char *file, int line, const char *text,
  16431. const unsigned char *buf, size_t len );
  16432. #if defined(MBEDTLS_BIGNUM_C)
  16433. void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
  16434. const char *file, int line,
  16435. const char *text, const mbedtls_mpi *X );
  16436. #endif
  16437. #if defined(MBEDTLS_ECP_C)
  16438. void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
  16439. const char *file, int line,
  16440. const char *text, const mbedtls_ecp_point *X );
  16441. #endif
  16442. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  16443. void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
  16444. const char *file, int line,
  16445. const char *text, const mbedtls_x509_crt *crt );
  16446. #endif
  16447. #ifdef __cplusplus
  16448. }
  16449. #endif
  16450. #endif /* debug.h */
  16451. /********* Start of file include/mbedtls/blowfish.h ************/
  16452. /**
  16453. * \file blowfish.h
  16454. *
  16455. * \brief Blowfish block cipher
  16456. *
  16457. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  16458. * SPDX-License-Identifier: Apache-2.0
  16459. *
  16460. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  16461. * not use this file except in compliance with the License.
  16462. * You may obtain a copy of the License at
  16463. *
  16464. * http://www.apache.org/licenses/LICENSE-2.0
  16465. *
  16466. * Unless required by applicable law or agreed to in writing, software
  16467. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16468. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16469. * See the License for the specific language governing permissions and
  16470. * limitations under the License.
  16471. *
  16472. * This file is part of mbed TLS (https://tls.mbed.org)
  16473. */
  16474. #ifndef MBEDTLS_BLOWFISH_H
  16475. #define MBEDTLS_BLOWFISH_H
  16476. #if !defined(MBEDTLS_CONFIG_FILE)
  16477. #else
  16478. #endif
  16479. #include <stddef.h>
  16480. #if !VXWORKS
  16481. #include <stdint.h>
  16482. #endif
  16483. #define MBEDTLS_BLOWFISH_ENCRYPT 1
  16484. #define MBEDTLS_BLOWFISH_DECRYPT 0
  16485. #define MBEDTLS_BLOWFISH_MAX_KEY_BITS 448
  16486. #define MBEDTLS_BLOWFISH_MIN_KEY_BITS 32
  16487. #define MBEDTLS_BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */
  16488. #define MBEDTLS_BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */
  16489. #define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */
  16490. #define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
  16491. #if !defined(MBEDTLS_BLOWFISH_ALT)
  16492. // Regular implementation
  16493. //
  16494. #ifdef __cplusplus
  16495. extern "C" {
  16496. #endif
  16497. /**
  16498. * \brief Blowfish context structure
  16499. */
  16500. typedef struct
  16501. {
  16502. uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
  16503. uint32_t S[4][256]; /*!< key dependent S-boxes */
  16504. }
  16505. mbedtls_blowfish_context;
  16506. /**
  16507. * \brief Initialize Blowfish context
  16508. *
  16509. * \param ctx Blowfish context to be initialized
  16510. */
  16511. void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx );
  16512. /**
  16513. * \brief Clear Blowfish context
  16514. *
  16515. * \param ctx Blowfish context to be cleared
  16516. */
  16517. void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx );
  16518. /**
  16519. * \brief Blowfish key schedule
  16520. *
  16521. * \param ctx Blowfish context to be initialized
  16522. * \param key encryption key
  16523. * \param keybits must be between 32 and 448 bits
  16524. *
  16525. * \return 0 if successful, or MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH
  16526. */
  16527. int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
  16528. unsigned int keybits );
  16529. /**
  16530. * \brief Blowfish-ECB block encryption/decryption
  16531. *
  16532. * \param ctx Blowfish context
  16533. * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
  16534. * \param input 8-byte input block
  16535. * \param output 8-byte output block
  16536. *
  16537. * \return 0 if successful
  16538. */
  16539. int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
  16540. int mode,
  16541. const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
  16542. unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] );
  16543. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  16544. /**
  16545. * \brief Blowfish-CBC buffer encryption/decryption
  16546. * Length should be a multiple of the block
  16547. * size (8 bytes)
  16548. *
  16549. * \note Upon exit, the content of the IV is updated so that you can
  16550. * call the function same function again on the following
  16551. * block(s) of data and get the same result as if it was
  16552. * encrypted in one call. This allows a "streaming" usage.
  16553. * If on the other hand you need to retain the contents of the
  16554. * IV, you should either save it manually or use the cipher
  16555. * module instead.
  16556. *
  16557. * \param ctx Blowfish context
  16558. * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
  16559. * \param length length of the input data
  16560. * \param iv initialization vector (updated after use)
  16561. * \param input buffer holding the input data
  16562. * \param output buffer holding the output data
  16563. *
  16564. * \return 0 if successful, or
  16565. * MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH
  16566. */
  16567. int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
  16568. int mode,
  16569. size_t length,
  16570. unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
  16571. const unsigned char *input,
  16572. unsigned char *output );
  16573. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  16574. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  16575. /**
  16576. * \brief Blowfish CFB buffer encryption/decryption.
  16577. *
  16578. * \note Upon exit, the content of the IV is updated so that you can
  16579. * call the function same function again on the following
  16580. * block(s) of data and get the same result as if it was
  16581. * encrypted in one call. This allows a "streaming" usage.
  16582. * If on the other hand you need to retain the contents of the
  16583. * IV, you should either save it manually or use the cipher
  16584. * module instead.
  16585. *
  16586. * \param ctx Blowfish context
  16587. * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
  16588. * \param length length of the input data
  16589. * \param iv_off offset in IV (updated after use)
  16590. * \param iv initialization vector (updated after use)
  16591. * \param input buffer holding the input data
  16592. * \param output buffer holding the output data
  16593. *
  16594. * \return 0 if successful
  16595. */
  16596. int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
  16597. int mode,
  16598. size_t length,
  16599. size_t *iv_off,
  16600. unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
  16601. const unsigned char *input,
  16602. unsigned char *output );
  16603. #endif /*MBEDTLS_CIPHER_MODE_CFB */
  16604. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  16605. /**
  16606. * \brief Blowfish-CTR buffer encryption/decryption
  16607. *
  16608. * Warning: You have to keep the maximum use of your counter in mind!
  16609. *
  16610. * \param ctx Blowfish context
  16611. * \param length The length of the data
  16612. * \param nc_off The offset in the current stream_block (for resuming
  16613. * within current cipher stream). The offset pointer to
  16614. * should be 0 at the start of a stream.
  16615. * \param nonce_counter The 64-bit nonce and counter.
  16616. * \param stream_block The saved stream-block for resuming. Is overwritten
  16617. * by the function.
  16618. * \param input The input data stream
  16619. * \param output The output data stream
  16620. *
  16621. * \return 0 if successful
  16622. */
  16623. int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
  16624. size_t length,
  16625. size_t *nc_off,
  16626. unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
  16627. unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
  16628. const unsigned char *input,
  16629. unsigned char *output );
  16630. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  16631. #ifdef __cplusplus
  16632. }
  16633. #endif
  16634. #else /* MBEDTLS_BLOWFISH_ALT */
  16635. #endif /* MBEDTLS_BLOWFISH_ALT */
  16636. #endif /* blowfish.h */
  16637. /********* Start of file include/mbedtls/ccm.h ************/
  16638. /**
  16639. * \file ccm.h
  16640. *
  16641. * \brief Counter with CBC-MAC (CCM) for 128-bit block ciphers
  16642. *
  16643. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  16644. * SPDX-License-Identifier: Apache-2.0
  16645. *
  16646. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  16647. * not use this file except in compliance with the License.
  16648. * You may obtain a copy of the License at
  16649. *
  16650. * http://www.apache.org/licenses/LICENSE-2.0
  16651. *
  16652. * Unless required by applicable law or agreed to in writing, software
  16653. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16654. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16655. * See the License for the specific language governing permissions and
  16656. * limitations under the License.
  16657. *
  16658. * This file is part of mbed TLS (https://tls.mbed.org)
  16659. */
  16660. #ifndef MBEDTLS_CCM_H
  16661. #define MBEDTLS_CCM_H
  16662. #define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to function. */
  16663. #define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */
  16664. #ifdef __cplusplus
  16665. extern "C" {
  16666. #endif
  16667. /**
  16668. * \brief CCM context structure
  16669. */
  16670. typedef struct {
  16671. mbedtls_cipher_context_t cipher_ctx; /*!< cipher context used */
  16672. }
  16673. mbedtls_ccm_context;
  16674. /**
  16675. * \brief Initialize CCM context (just makes references valid)
  16676. * Makes the context ready for mbedtls_ccm_setkey() or
  16677. * mbedtls_ccm_free().
  16678. *
  16679. * \param ctx CCM context to initialize
  16680. */
  16681. void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
  16682. /**
  16683. * \brief CCM initialization (encryption and decryption)
  16684. *
  16685. * \param ctx CCM context to be initialized
  16686. * \param cipher cipher to use (a 128-bit block cipher)
  16687. * \param key encryption key
  16688. * \param keybits key size in bits (must be acceptable by the cipher)
  16689. *
  16690. * \return 0 if successful, or a cipher specific error code
  16691. */
  16692. int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
  16693. mbedtls_cipher_id_t cipher,
  16694. const unsigned char *key,
  16695. unsigned int keybits );
  16696. /**
  16697. * \brief Free a CCM context and underlying cipher sub-context
  16698. *
  16699. * \param ctx CCM context to free
  16700. */
  16701. void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
  16702. /**
  16703. * \brief CCM buffer encryption
  16704. *
  16705. * \param ctx CCM context
  16706. * \param length length of the input data in bytes
  16707. * \param iv nonce (initialization vector)
  16708. * \param iv_len length of IV in bytes
  16709. * must be 2, 3, 4, 5, 6, 7 or 8
  16710. * \param add additional data
  16711. * \param add_len length of additional data in bytes
  16712. * must be less than 2^16 - 2^8
  16713. * \param input buffer holding the input data
  16714. * \param output buffer for holding the output data
  16715. * must be at least 'length' bytes wide
  16716. * \param tag buffer for holding the tag
  16717. * \param tag_len length of the tag to generate in bytes
  16718. * must be 4, 6, 8, 10, 14 or 16
  16719. *
  16720. * \note The tag is written to a separate buffer. To get the tag
  16721. * concatenated with the output as in the CCM spec, use
  16722. * tag = output + length and make sure the output buffer is
  16723. * at least length + tag_len wide.
  16724. *
  16725. * \return 0 if successful
  16726. */
  16727. int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
  16728. const unsigned char *iv, size_t iv_len,
  16729. const unsigned char *add, size_t add_len,
  16730. const unsigned char *input, unsigned char *output,
  16731. unsigned char *tag, size_t tag_len );
  16732. /**
  16733. * \brief CCM buffer authenticated decryption
  16734. *
  16735. * \param ctx CCM context
  16736. * \param length length of the input data
  16737. * \param iv initialization vector
  16738. * \param iv_len length of IV
  16739. * \param add additional data
  16740. * \param add_len length of additional data
  16741. * \param input buffer holding the input data
  16742. * \param output buffer for holding the output data
  16743. * \param tag buffer holding the tag
  16744. * \param tag_len length of the tag
  16745. *
  16746. * \return 0 if successful and authenticated,
  16747. * MBEDTLS_ERR_CCM_AUTH_FAILED if tag does not match
  16748. */
  16749. int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
  16750. const unsigned char *iv, size_t iv_len,
  16751. const unsigned char *add, size_t add_len,
  16752. const unsigned char *input, unsigned char *output,
  16753. const unsigned char *tag, size_t tag_len );
  16754. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  16755. /**
  16756. * \brief Checkup routine
  16757. *
  16758. * \return 0 if successful, or 1 if the test failed
  16759. */
  16760. int mbedtls_ccm_self_test( int verbose );
  16761. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  16762. #ifdef __cplusplus
  16763. }
  16764. #endif
  16765. #endif /* MBEDTLS_CCM_H */
  16766. /********* Start of file include/mbedtls/gcm.h ************/
  16767. /**
  16768. * \file gcm.h
  16769. *
  16770. * \brief Galois/Counter mode for 128-bit block ciphers
  16771. *
  16772. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  16773. * SPDX-License-Identifier: Apache-2.0
  16774. *
  16775. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  16776. * not use this file except in compliance with the License.
  16777. * You may obtain a copy of the License at
  16778. *
  16779. * http://www.apache.org/licenses/LICENSE-2.0
  16780. *
  16781. * Unless required by applicable law or agreed to in writing, software
  16782. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16783. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16784. * See the License for the specific language governing permissions and
  16785. * limitations under the License.
  16786. *
  16787. * This file is part of mbed TLS (https://tls.mbed.org)
  16788. */
  16789. #ifndef MBEDTLS_GCM_H
  16790. #define MBEDTLS_GCM_H
  16791. #if !VXWORKS
  16792. #include <stdint.h>
  16793. #endif
  16794. #define MBEDTLS_GCM_ENCRYPT 1
  16795. #define MBEDTLS_GCM_DECRYPT 0
  16796. #define MBEDTLS_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */
  16797. #define MBEDTLS_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */
  16798. #ifdef __cplusplus
  16799. extern "C" {
  16800. #endif
  16801. /**
  16802. * \brief GCM context structure
  16803. */
  16804. typedef struct {
  16805. mbedtls_cipher_context_t cipher_ctx;/*!< cipher context used */
  16806. uint64_t HL[16]; /*!< Precalculated HTable */
  16807. uint64_t HH[16]; /*!< Precalculated HTable */
  16808. uint64_t len; /*!< Total data length */
  16809. uint64_t add_len; /*!< Total add length */
  16810. unsigned char base_ectr[16];/*!< First ECTR for tag */
  16811. unsigned char y[16]; /*!< Y working value */
  16812. unsigned char buf[16]; /*!< buf working value */
  16813. int mode; /*!< Encrypt or Decrypt */
  16814. }
  16815. mbedtls_gcm_context;
  16816. /**
  16817. * \brief Initialize GCM context (just makes references valid)
  16818. * Makes the context ready for mbedtls_gcm_setkey() or
  16819. * mbedtls_gcm_free().
  16820. *
  16821. * \param ctx GCM context to initialize
  16822. */
  16823. void mbedtls_gcm_init( mbedtls_gcm_context *ctx );
  16824. /**
  16825. * \brief GCM initialization (encryption)
  16826. *
  16827. * \param ctx GCM context to be initialized
  16828. * \param cipher cipher to use (a 128-bit block cipher)
  16829. * \param key encryption key
  16830. * \param keybits must be 128, 192 or 256
  16831. *
  16832. * \return 0 if successful, or a cipher specific error code
  16833. */
  16834. int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx,
  16835. mbedtls_cipher_id_t cipher,
  16836. const unsigned char *key,
  16837. unsigned int keybits );
  16838. /**
  16839. * \brief GCM buffer encryption/decryption using a block cipher
  16840. *
  16841. * \note On encryption, the output buffer can be the same as the input buffer.
  16842. * On decryption, the output buffer cannot be the same as input buffer.
  16843. * If buffers overlap, the output buffer must trail at least 8 bytes
  16844. * behind the input buffer.
  16845. *
  16846. * \param ctx GCM context
  16847. * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT
  16848. * \param length length of the input data
  16849. * \param iv initialization vector
  16850. * \param iv_len length of IV
  16851. * \param add additional data
  16852. * \param add_len length of additional data
  16853. * \param input buffer holding the input data
  16854. * \param output buffer for holding the output data
  16855. * \param tag_len length of the tag to generate
  16856. * \param tag buffer for holding the tag
  16857. *
  16858. * \return 0 if successful
  16859. */
  16860. int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
  16861. int mode,
  16862. size_t length,
  16863. const unsigned char *iv,
  16864. size_t iv_len,
  16865. const unsigned char *add,
  16866. size_t add_len,
  16867. const unsigned char *input,
  16868. unsigned char *output,
  16869. size_t tag_len,
  16870. unsigned char *tag );
  16871. /**
  16872. * \brief GCM buffer authenticated decryption using a block cipher
  16873. *
  16874. * \note On decryption, the output buffer cannot be the same as input buffer.
  16875. * If buffers overlap, the output buffer must trail at least 8 bytes
  16876. * behind the input buffer.
  16877. *
  16878. * \param ctx GCM context
  16879. * \param length length of the input data
  16880. * \param iv initialization vector
  16881. * \param iv_len length of IV
  16882. * \param add additional data
  16883. * \param add_len length of additional data
  16884. * \param tag buffer holding the tag
  16885. * \param tag_len length of the tag
  16886. * \param input buffer holding the input data
  16887. * \param output buffer for holding the output data
  16888. *
  16889. * \return 0 if successful and authenticated,
  16890. * MBEDTLS_ERR_GCM_AUTH_FAILED if tag does not match
  16891. */
  16892. int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
  16893. size_t length,
  16894. const unsigned char *iv,
  16895. size_t iv_len,
  16896. const unsigned char *add,
  16897. size_t add_len,
  16898. const unsigned char *tag,
  16899. size_t tag_len,
  16900. const unsigned char *input,
  16901. unsigned char *output );
  16902. /**
  16903. * \brief Generic GCM stream start function
  16904. *
  16905. * \param ctx GCM context
  16906. * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT
  16907. * \param iv initialization vector
  16908. * \param iv_len length of IV
  16909. * \param add additional data (or NULL if length is 0)
  16910. * \param add_len length of additional data
  16911. *
  16912. * \return 0 if successful
  16913. */
  16914. int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
  16915. int mode,
  16916. const unsigned char *iv,
  16917. size_t iv_len,
  16918. const unsigned char *add,
  16919. size_t add_len );
  16920. /**
  16921. * \brief Generic GCM update function. Encrypts/decrypts using the
  16922. * given GCM context. Expects input to be a multiple of 16
  16923. * bytes! Only the last call before mbedtls_gcm_finish() can be less
  16924. * than 16 bytes!
  16925. *
  16926. * \note On decryption, the output buffer cannot be the same as input buffer.
  16927. * If buffers overlap, the output buffer must trail at least 8 bytes
  16928. * behind the input buffer.
  16929. *
  16930. * \param ctx GCM context
  16931. * \param length length of the input data
  16932. * \param input buffer holding the input data
  16933. * \param output buffer for holding the output data
  16934. *
  16935. * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT
  16936. */
  16937. int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
  16938. size_t length,
  16939. const unsigned char *input,
  16940. unsigned char *output );
  16941. /**
  16942. * \brief Generic GCM finalisation function. Wraps up the GCM stream
  16943. * and generates the tag. The tag can have a maximum length of
  16944. * 16 bytes.
  16945. *
  16946. * \param ctx GCM context
  16947. * \param tag buffer for holding the tag (may be NULL if tag_len is 0)
  16948. * \param tag_len length of the tag to generate
  16949. *
  16950. * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT
  16951. */
  16952. int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
  16953. unsigned char *tag,
  16954. size_t tag_len );
  16955. /**
  16956. * \brief Free a GCM context and underlying cipher sub-context
  16957. *
  16958. * \param ctx GCM context to free
  16959. */
  16960. void mbedtls_gcm_free( mbedtls_gcm_context *ctx );
  16961. /**
  16962. * \brief Checkup routine
  16963. *
  16964. * \return 0 if successful, or 1 if the test failed
  16965. */
  16966. int mbedtls_gcm_self_test( int verbose );
  16967. #ifdef __cplusplus
  16968. }
  16969. #endif
  16970. #endif /* gcm.h */
  16971. /********* Start of file include/mbedtls/pem.h ************/
  16972. /**
  16973. * \file pem.h
  16974. *
  16975. * \brief Privacy Enhanced Mail (PEM) decoding
  16976. *
  16977. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  16978. * SPDX-License-Identifier: Apache-2.0
  16979. *
  16980. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  16981. * not use this file except in compliance with the License.
  16982. * You may obtain a copy of the License at
  16983. *
  16984. * http://www.apache.org/licenses/LICENSE-2.0
  16985. *
  16986. * Unless required by applicable law or agreed to in writing, software
  16987. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16988. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16989. * See the License for the specific language governing permissions and
  16990. * limitations under the License.
  16991. *
  16992. * This file is part of mbed TLS (https://tls.mbed.org)
  16993. */
  16994. #ifndef MBEDTLS_PEM_H
  16995. #define MBEDTLS_PEM_H
  16996. #include <stddef.h>
  16997. /**
  16998. * \name PEM Error codes
  16999. * These error codes are returned in case of errors reading the
  17000. * PEM data.
  17001. * \{
  17002. */
  17003. #define MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT -0x1080 /**< No PEM header or footer found. */
  17004. #define MBEDTLS_ERR_PEM_INVALID_DATA -0x1100 /**< PEM string is not as expected. */
  17005. #define MBEDTLS_ERR_PEM_ALLOC_FAILED -0x1180 /**< Failed to allocate memory. */
  17006. #define MBEDTLS_ERR_PEM_INVALID_ENC_IV -0x1200 /**< RSA IV is not in hex-format. */
  17007. #define MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG -0x1280 /**< Unsupported key encryption algorithm. */
  17008. #define MBEDTLS_ERR_PEM_PASSWORD_REQUIRED -0x1300 /**< Private key password can't be empty. */
  17009. #define MBEDTLS_ERR_PEM_PASSWORD_MISMATCH -0x1380 /**< Given private key password does not allow for correct decryption. */
  17010. #define MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE -0x1400 /**< Unavailable feature, e.g. hashing/encryption combination. */
  17011. #define MBEDTLS_ERR_PEM_BAD_INPUT_DATA -0x1480 /**< Bad input parameters to function. */
  17012. /* \} name */
  17013. #ifdef __cplusplus
  17014. extern "C" {
  17015. #endif
  17016. #if defined(MBEDTLS_PEM_PARSE_C)
  17017. /**
  17018. * \brief PEM context structure
  17019. */
  17020. typedef struct
  17021. {
  17022. unsigned char *buf; /*!< buffer for decoded data */
  17023. size_t buflen; /*!< length of the buffer */
  17024. unsigned char *info; /*!< buffer for extra header information */
  17025. }
  17026. mbedtls_pem_context;
  17027. /**
  17028. * \brief PEM context setup
  17029. *
  17030. * \param ctx context to be initialized
  17031. */
  17032. void mbedtls_pem_init( mbedtls_pem_context *ctx );
  17033. /**
  17034. * \brief Read a buffer for PEM information and store the resulting
  17035. * data into the specified context buffers.
  17036. *
  17037. * \param ctx context to use
  17038. * \param header header string to seek and expect
  17039. * \param footer footer string to seek and expect
  17040. * \param data source data to look in (must be nul-terminated)
  17041. * \param pwd password for decryption (can be NULL)
  17042. * \param pwdlen length of password
  17043. * \param use_len destination for total length used (set after header is
  17044. * correctly read, so unless you get
  17045. * MBEDTLS_ERR_PEM_BAD_INPUT_DATA or
  17046. * MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT, use_len is
  17047. * the length to skip)
  17048. *
  17049. * \note Attempts to check password correctness by verifying if
  17050. * the decrypted text starts with an ASN.1 sequence of
  17051. * appropriate length
  17052. *
  17053. * \return 0 on success, or a specific PEM error code
  17054. */
  17055. int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
  17056. const unsigned char *data,
  17057. const unsigned char *pwd,
  17058. size_t pwdlen, size_t *use_len );
  17059. /**
  17060. * \brief PEM context memory freeing
  17061. *
  17062. * \param ctx context to be freed
  17063. */
  17064. void mbedtls_pem_free( mbedtls_pem_context *ctx );
  17065. #endif /* MBEDTLS_PEM_PARSE_C */
  17066. #if defined(MBEDTLS_PEM_WRITE_C)
  17067. /**
  17068. * \brief Write a buffer of PEM information from a DER encoded
  17069. * buffer.
  17070. *
  17071. * \param header header string to write
  17072. * \param footer footer string to write
  17073. * \param der_data DER data to write
  17074. * \param der_len length of the DER data
  17075. * \param buf buffer to write to
  17076. * \param buf_len length of output buffer
  17077. * \param olen total length written / required (if buf_len is not enough)
  17078. *
  17079. * \return 0 on success, or a specific PEM or BASE64 error code. On
  17080. * MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL olen is the required
  17081. * size.
  17082. */
  17083. int mbedtls_pem_write_buffer( const char *header, const char *footer,
  17084. const unsigned char *der_data, size_t der_len,
  17085. unsigned char *buf, size_t buf_len, size_t *olen );
  17086. #endif /* MBEDTLS_PEM_WRITE_C */
  17087. #ifdef __cplusplus
  17088. }
  17089. #endif
  17090. #endif /* pem.h */
  17091. /********* Start of file include/mbedtls/asn1write.h ************/
  17092. /**
  17093. * \file asn1write.h
  17094. *
  17095. * \brief ASN.1 buffer writing functionality
  17096. *
  17097. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  17098. * SPDX-License-Identifier: Apache-2.0
  17099. *
  17100. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  17101. * not use this file except in compliance with the License.
  17102. * You may obtain a copy of the License at
  17103. *
  17104. * http://www.apache.org/licenses/LICENSE-2.0
  17105. *
  17106. * Unless required by applicable law or agreed to in writing, software
  17107. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17108. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17109. * See the License for the specific language governing permissions and
  17110. * limitations under the License.
  17111. *
  17112. * This file is part of mbed TLS (https://tls.mbed.org)
  17113. */
  17114. #ifndef MBEDTLS_ASN1_WRITE_H
  17115. #define MBEDTLS_ASN1_WRITE_H
  17116. #define MBEDTLS_ASN1_CHK_ADD(g, f) do { if( ( ret = f ) < 0 ) return( ret ); else \
  17117. g += ret; } while( 0 )
  17118. #ifdef __cplusplus
  17119. extern "C" {
  17120. #endif
  17121. /**
  17122. * \brief Write a length field in ASN.1 format
  17123. * Note: function works backwards in data buffer
  17124. *
  17125. * \param p reference to current position pointer
  17126. * \param start start of the buffer (for bounds-checking)
  17127. * \param len the length to write
  17128. *
  17129. * \return the length written or a negative error code
  17130. */
  17131. int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len );
  17132. /**
  17133. * \brief Write a ASN.1 tag in ASN.1 format
  17134. * Note: function works backwards in data buffer
  17135. *
  17136. * \param p reference to current position pointer
  17137. * \param start start of the buffer (for bounds-checking)
  17138. * \param tag the tag to write
  17139. *
  17140. * \return the length written or a negative error code
  17141. */
  17142. int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start,
  17143. unsigned char tag );
  17144. /**
  17145. * \brief Write raw buffer data
  17146. * Note: function works backwards in data buffer
  17147. *
  17148. * \param p reference to current position pointer
  17149. * \param start start of the buffer (for bounds-checking)
  17150. * \param buf data buffer to write
  17151. * \param size length of the data buffer
  17152. *
  17153. * \return the length written or a negative error code
  17154. */
  17155. int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
  17156. const unsigned char *buf, size_t size );
  17157. #if defined(MBEDTLS_BIGNUM_C)
  17158. /**
  17159. * \brief Write a big number (MBEDTLS_ASN1_INTEGER) in ASN.1 format
  17160. * Note: function works backwards in data buffer
  17161. *
  17162. * \param p reference to current position pointer
  17163. * \param start start of the buffer (for bounds-checking)
  17164. * \param X the MPI to write
  17165. *
  17166. * \return the length written or a negative error code
  17167. */
  17168. int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X );
  17169. #endif /* MBEDTLS_BIGNUM_C */
  17170. /**
  17171. * \brief Write a NULL tag (MBEDTLS_ASN1_NULL) with zero data in ASN.1 format
  17172. * Note: function works backwards in data buffer
  17173. *
  17174. * \param p reference to current position pointer
  17175. * \param start start of the buffer (for bounds-checking)
  17176. *
  17177. * \return the length written or a negative error code
  17178. */
  17179. int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start );
  17180. /**
  17181. * \brief Write an OID tag (MBEDTLS_ASN1_OID) and data in ASN.1 format
  17182. * Note: function works backwards in data buffer
  17183. *
  17184. * \param p reference to current position pointer
  17185. * \param start start of the buffer (for bounds-checking)
  17186. * \param oid the OID to write
  17187. * \param oid_len length of the OID
  17188. *
  17189. * \return the length written or a negative error code
  17190. */
  17191. int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
  17192. const char *oid, size_t oid_len );
  17193. /**
  17194. * \brief Write an AlgorithmIdentifier sequence in ASN.1 format
  17195. * Note: function works backwards in data buffer
  17196. *
  17197. * \param p reference to current position pointer
  17198. * \param start start of the buffer (for bounds-checking)
  17199. * \param oid the OID of the algorithm
  17200. * \param oid_len length of the OID
  17201. * \param par_len length of parameters, which must be already written.
  17202. * If 0, NULL parameters are added
  17203. *
  17204. * \return the length written or a negative error code
  17205. */
  17206. int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
  17207. const char *oid, size_t oid_len,
  17208. size_t par_len );
  17209. /**
  17210. * \brief Write a boolean tag (MBEDTLS_ASN1_BOOLEAN) and value in ASN.1 format
  17211. * Note: function works backwards in data buffer
  17212. *
  17213. * \param p reference to current position pointer
  17214. * \param start start of the buffer (for bounds-checking)
  17215. * \param boolean 0 or 1
  17216. *
  17217. * \return the length written or a negative error code
  17218. */
  17219. int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean );
  17220. /**
  17221. * \brief Write an int tag (MBEDTLS_ASN1_INTEGER) and value in ASN.1 format
  17222. * Note: function works backwards in data buffer
  17223. *
  17224. * \param p reference to current position pointer
  17225. * \param start start of the buffer (for bounds-checking)
  17226. * \param val the integer value
  17227. *
  17228. * \return the length written or a negative error code
  17229. */
  17230. int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
  17231. /**
  17232. * \brief Write a printable string tag (MBEDTLS_ASN1_PRINTABLE_STRING) and
  17233. * value in ASN.1 format
  17234. * Note: function works backwards in data buffer
  17235. *
  17236. * \param p reference to current position pointer
  17237. * \param start start of the buffer (for bounds-checking)
  17238. * \param text the text to write
  17239. * \param text_len length of the text
  17240. *
  17241. * \return the length written or a negative error code
  17242. */
  17243. int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
  17244. const char *text, size_t text_len );
  17245. /**
  17246. * \brief Write an IA5 string tag (MBEDTLS_ASN1_IA5_STRING) and
  17247. * value in ASN.1 format
  17248. * Note: function works backwards in data buffer
  17249. *
  17250. * \param p reference to current position pointer
  17251. * \param start start of the buffer (for bounds-checking)
  17252. * \param text the text to write
  17253. * \param text_len length of the text
  17254. *
  17255. * \return the length written or a negative error code
  17256. */
  17257. int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
  17258. const char *text, size_t text_len );
  17259. /**
  17260. * \brief Write a bitstring tag (MBEDTLS_ASN1_BIT_STRING) and
  17261. * value in ASN.1 format
  17262. * Note: function works backwards in data buffer
  17263. *
  17264. * \param p reference to current position pointer
  17265. * \param start start of the buffer (for bounds-checking)
  17266. * \param buf the bitstring
  17267. * \param bits the total number of bits in the bitstring
  17268. *
  17269. * \return the length written or a negative error code
  17270. */
  17271. int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
  17272. const unsigned char *buf, size_t bits );
  17273. /**
  17274. * \brief Write an octet string tag (MBEDTLS_ASN1_OCTET_STRING) and
  17275. * value in ASN.1 format
  17276. * Note: function works backwards in data buffer
  17277. *
  17278. * \param p reference to current position pointer
  17279. * \param start start of the buffer (for bounds-checking)
  17280. * \param buf data buffer to write
  17281. * \param size length of the data buffer
  17282. *
  17283. * \return the length written or a negative error code
  17284. */
  17285. int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
  17286. const unsigned char *buf, size_t size );
  17287. /**
  17288. * \brief Create or find a specific named_data entry for writing in a
  17289. * sequence or list based on the OID. If not already in there,
  17290. * a new entry is added to the head of the list.
  17291. * Warning: Destructive behaviour for the val data!
  17292. *
  17293. * \param list Pointer to the location of the head of the list to seek
  17294. * through (will be updated in case of a new entry)
  17295. * \param oid The OID to look for
  17296. * \param oid_len Size of the OID
  17297. * \param val Data to store (can be NULL if you want to fill it by hand)
  17298. * \param val_len Minimum length of the data buffer needed
  17299. *
  17300. * \return NULL if if there was a memory allocation error, or a pointer
  17301. * to the new / existing entry.
  17302. */
  17303. mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list,
  17304. const char *oid, size_t oid_len,
  17305. const unsigned char *val,
  17306. size_t val_len );
  17307. #ifdef __cplusplus
  17308. }
  17309. #endif
  17310. #endif /* MBEDTLS_ASN1_WRITE_H */
  17311. /********* Start of file include/mbedtls/hmac_drbg.h ************/
  17312. /**
  17313. * \file hmac_drbg.h
  17314. *
  17315. * \brief HMAC_DRBG (NIST SP 800-90A)
  17316. *
  17317. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  17318. * SPDX-License-Identifier: Apache-2.0
  17319. *
  17320. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  17321. * not use this file except in compliance with the License.
  17322. * You may obtain a copy of the License at
  17323. *
  17324. * http://www.apache.org/licenses/LICENSE-2.0
  17325. *
  17326. * Unless required by applicable law or agreed to in writing, software
  17327. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17328. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17329. * See the License for the specific language governing permissions and
  17330. * limitations under the License.
  17331. *
  17332. * This file is part of mbed TLS (https://tls.mbed.org)
  17333. */
  17334. #ifndef MBEDTLS_HMAC_DRBG_H
  17335. #define MBEDTLS_HMAC_DRBG_H
  17336. #if defined(MBEDTLS_THREADING_C)
  17337. #endif
  17338. /*
  17339. * Error codes
  17340. */
  17341. #define MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG -0x0003 /**< Too many random requested in single call. */
  17342. #define MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG -0x0005 /**< Input too large (Entropy + additional). */
  17343. #define MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR -0x0007 /**< Read/write error in file. */
  17344. #define MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED -0x0009 /**< The entropy source failed. */
  17345. /**
  17346. * \name SECTION: Module settings
  17347. *
  17348. * The configuration options you can set for this module are in this section.
  17349. * Either change them in config.h or define them on the compiler command line.
  17350. * \{
  17351. */
  17352. #if !defined(MBEDTLS_HMAC_DRBG_RESEED_INTERVAL)
  17353. #define MBEDTLS_HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
  17354. #endif
  17355. #if !defined(MBEDTLS_HMAC_DRBG_MAX_INPUT)
  17356. #define MBEDTLS_HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
  17357. #endif
  17358. #if !defined(MBEDTLS_HMAC_DRBG_MAX_REQUEST)
  17359. #define MBEDTLS_HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
  17360. #endif
  17361. #if !defined(MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT)
  17362. #define MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
  17363. #endif
  17364. /* \} name SECTION: Module settings */
  17365. #define MBEDTLS_HMAC_DRBG_PR_OFF 0 /**< No prediction resistance */
  17366. #define MBEDTLS_HMAC_DRBG_PR_ON 1 /**< Prediction resistance enabled */
  17367. #ifdef __cplusplus
  17368. extern "C" {
  17369. #endif
  17370. /**
  17371. * HMAC_DRBG context.
  17372. */
  17373. typedef struct
  17374. {
  17375. /* Working state: the key K is not stored explicitely,
  17376. * but is implied by the HMAC context */
  17377. mbedtls_md_context_t md_ctx; /*!< HMAC context (inc. K) */
  17378. unsigned char V[MBEDTLS_MD_MAX_SIZE]; /*!< V in the spec */
  17379. int reseed_counter; /*!< reseed counter */
  17380. /* Administrative state */
  17381. size_t entropy_len; /*!< entropy bytes grabbed on each (re)seed */
  17382. int prediction_resistance; /*!< enable prediction resistance (Automatic
  17383. reseed before every random generation) */
  17384. int reseed_interval; /*!< reseed interval */
  17385. /* Callbacks */
  17386. int (*f_entropy)(void *, unsigned char *, size_t); /*!< entropy function */
  17387. void *p_entropy; /*!< context for the entropy function */
  17388. #if defined(MBEDTLS_THREADING_C)
  17389. mbedtls_threading_mutex_t mutex;
  17390. #endif
  17391. } mbedtls_hmac_drbg_context;
  17392. /**
  17393. * \brief HMAC_DRBG context initialization
  17394. * Makes the context ready for mbedtls_hmac_drbg_seed(),
  17395. * mbedtls_hmac_drbg_seed_buf() or
  17396. * mbedtls_hmac_drbg_free().
  17397. *
  17398. * \param ctx HMAC_DRBG context to be initialized
  17399. */
  17400. void mbedtls_hmac_drbg_init( mbedtls_hmac_drbg_context *ctx );
  17401. /**
  17402. * \brief HMAC_DRBG initial seeding
  17403. * Seed and setup entropy source for future reseeds.
  17404. *
  17405. * \param ctx HMAC_DRBG context to be seeded
  17406. * \param md_info MD algorithm to use for HMAC_DRBG
  17407. * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
  17408. * length)
  17409. * \param p_entropy Entropy context
  17410. * \param custom Personalization data (Device specific identifiers)
  17411. * (Can be NULL)
  17412. * \param len Length of personalization data
  17413. *
  17414. * \note The "security strength" as defined by NIST is set to:
  17415. * 128 bits if md_alg is SHA-1,
  17416. * 192 bits if md_alg is SHA-224,
  17417. * 256 bits if md_alg is SHA-256 or higher.
  17418. * Note that SHA-256 is just as efficient as SHA-224.
  17419. *
  17420. * \return 0 if successful, or
  17421. * MBEDTLS_ERR_MD_BAD_INPUT_DATA, or
  17422. * MBEDTLS_ERR_MD_ALLOC_FAILED, or
  17423. * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED.
  17424. */
  17425. int mbedtls_hmac_drbg_seed( mbedtls_hmac_drbg_context *ctx,
  17426. const mbedtls_md_info_t * md_info,
  17427. int (*f_entropy)(void *, unsigned char *, size_t),
  17428. void *p_entropy,
  17429. const unsigned char *custom,
  17430. size_t len );
  17431. /**
  17432. * \brief Initilisation of simpified HMAC_DRBG (never reseeds).
  17433. * (For use with deterministic ECDSA.)
  17434. *
  17435. * \param ctx HMAC_DRBG context to be initialised
  17436. * \param md_info MD algorithm to use for HMAC_DRBG
  17437. * \param data Concatenation of entropy string and additional data
  17438. * \param data_len Length of data in bytes
  17439. *
  17440. * \return 0 if successful, or
  17441. * MBEDTLS_ERR_MD_BAD_INPUT_DATA, or
  17442. * MBEDTLS_ERR_MD_ALLOC_FAILED.
  17443. */
  17444. int mbedtls_hmac_drbg_seed_buf( mbedtls_hmac_drbg_context *ctx,
  17445. const mbedtls_md_info_t * md_info,
  17446. const unsigned char *data, size_t data_len );
  17447. /**
  17448. * \brief Enable / disable prediction resistance (Default: Off)
  17449. *
  17450. * Note: If enabled, entropy is used for ctx->entropy_len before each call!
  17451. * Only use this if you have ample supply of good entropy!
  17452. *
  17453. * \param ctx HMAC_DRBG context
  17454. * \param resistance MBEDTLS_HMAC_DRBG_PR_ON or MBEDTLS_HMAC_DRBG_PR_OFF
  17455. */
  17456. void mbedtls_hmac_drbg_set_prediction_resistance( mbedtls_hmac_drbg_context *ctx,
  17457. int resistance );
  17458. /**
  17459. * \brief Set the amount of entropy grabbed on each reseed
  17460. * (Default: given by the security strength, which
  17461. * depends on the hash used, see \c mbedtls_hmac_drbg_init() )
  17462. *
  17463. * \param ctx HMAC_DRBG context
  17464. * \param len Amount of entropy to grab, in bytes
  17465. */
  17466. void mbedtls_hmac_drbg_set_entropy_len( mbedtls_hmac_drbg_context *ctx,
  17467. size_t len );
  17468. /**
  17469. * \brief Set the reseed interval
  17470. * (Default: MBEDTLS_HMAC_DRBG_RESEED_INTERVAL)
  17471. *
  17472. * \param ctx HMAC_DRBG context
  17473. * \param interval Reseed interval
  17474. */
  17475. void mbedtls_hmac_drbg_set_reseed_interval( mbedtls_hmac_drbg_context *ctx,
  17476. int interval );
  17477. /**
  17478. * \brief HMAC_DRBG update state
  17479. *
  17480. * \param ctx HMAC_DRBG context
  17481. * \param additional Additional data to update state with, or NULL
  17482. * \param add_len Length of additional data, or 0
  17483. *
  17484. * \note Additional data is optional, pass NULL and 0 as second
  17485. * third argument if no additional data is being used.
  17486. */
  17487. void mbedtls_hmac_drbg_update( mbedtls_hmac_drbg_context *ctx,
  17488. const unsigned char *additional, size_t add_len );
  17489. /**
  17490. * \brief HMAC_DRBG reseeding (extracts data from entropy source)
  17491. *
  17492. * \param ctx HMAC_DRBG context
  17493. * \param additional Additional data to add to state (Can be NULL)
  17494. * \param len Length of additional data
  17495. *
  17496. * \return 0 if successful, or
  17497. * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
  17498. */
  17499. int mbedtls_hmac_drbg_reseed( mbedtls_hmac_drbg_context *ctx,
  17500. const unsigned char *additional, size_t len );
  17501. /**
  17502. * \brief HMAC_DRBG generate random with additional update input
  17503. *
  17504. * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
  17505. *
  17506. * \param p_rng HMAC_DRBG context
  17507. * \param output Buffer to fill
  17508. * \param output_len Length of the buffer
  17509. * \param additional Additional data to update with (can be NULL)
  17510. * \param add_len Length of additional data (can be 0)
  17511. *
  17512. * \return 0 if successful, or
  17513. * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
  17514. * MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG, or
  17515. * MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG.
  17516. */
  17517. int mbedtls_hmac_drbg_random_with_add( void *p_rng,
  17518. unsigned char *output, size_t output_len,
  17519. const unsigned char *additional,
  17520. size_t add_len );
  17521. /**
  17522. * \brief HMAC_DRBG generate random
  17523. *
  17524. * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
  17525. *
  17526. * \param p_rng HMAC_DRBG context
  17527. * \param output Buffer to fill
  17528. * \param out_len Length of the buffer
  17529. *
  17530. * \return 0 if successful, or
  17531. * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
  17532. * MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG
  17533. */
  17534. int mbedtls_hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len );
  17535. /**
  17536. * \brief Free an HMAC_DRBG context
  17537. *
  17538. * \param ctx HMAC_DRBG context to free.
  17539. */
  17540. void mbedtls_hmac_drbg_free( mbedtls_hmac_drbg_context *ctx );
  17541. #if defined(MBEDTLS_FS_IO)
  17542. /**
  17543. * \brief Write a seed file
  17544. *
  17545. * \param ctx HMAC_DRBG context
  17546. * \param path Name of the file
  17547. *
  17548. * \return 0 if successful, 1 on file error, or
  17549. * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
  17550. */
  17551. int mbedtls_hmac_drbg_write_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path );
  17552. /**
  17553. * \brief Read and update a seed file. Seed is added to this
  17554. * instance
  17555. *
  17556. * \param ctx HMAC_DRBG context
  17557. * \param path Name of the file
  17558. *
  17559. * \return 0 if successful, 1 on file error,
  17560. * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED or
  17561. * MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG
  17562. */
  17563. int mbedtls_hmac_drbg_update_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path );
  17564. #endif /* MBEDTLS_FS_IO */
  17565. #if defined(MBEDTLS_SELF_TEST)
  17566. /**
  17567. * \brief Checkup routine
  17568. *
  17569. * \return 0 if successful, or 1 if the test failed
  17570. */
  17571. int mbedtls_hmac_drbg_self_test( int verbose );
  17572. #endif
  17573. #ifdef __cplusplus
  17574. }
  17575. #endif
  17576. #endif /* hmac_drbg.h */
  17577. /********* Start of file include/mbedtls/pkcs12.h ************/
  17578. /**
  17579. * \file pkcs12.h
  17580. *
  17581. * \brief PKCS#12 Personal Information Exchange Syntax
  17582. *
  17583. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  17584. * SPDX-License-Identifier: Apache-2.0
  17585. *
  17586. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  17587. * not use this file except in compliance with the License.
  17588. * You may obtain a copy of the License at
  17589. *
  17590. * http://www.apache.org/licenses/LICENSE-2.0
  17591. *
  17592. * Unless required by applicable law or agreed to in writing, software
  17593. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17594. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17595. * See the License for the specific language governing permissions and
  17596. * limitations under the License.
  17597. *
  17598. * This file is part of mbed TLS (https://tls.mbed.org)
  17599. */
  17600. #ifndef MBEDTLS_PKCS12_H
  17601. #define MBEDTLS_PKCS12_H
  17602. #include <stddef.h>
  17603. #define MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA -0x1F80 /**< Bad input parameters to function. */
  17604. #define MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE -0x1F00 /**< Feature not available, e.g. unsupported encryption scheme. */
  17605. #define MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT -0x1E80 /**< PBE ASN.1 data not as expected. */
  17606. #define MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH -0x1E00 /**< Given private key password does not allow for correct decryption. */
  17607. #define MBEDTLS_PKCS12_DERIVE_KEY 1 /**< encryption/decryption key */
  17608. #define MBEDTLS_PKCS12_DERIVE_IV 2 /**< initialization vector */
  17609. #define MBEDTLS_PKCS12_DERIVE_MAC_KEY 3 /**< integrity / MAC key */
  17610. #define MBEDTLS_PKCS12_PBE_DECRYPT 0
  17611. #define MBEDTLS_PKCS12_PBE_ENCRYPT 1
  17612. #ifdef __cplusplus
  17613. extern "C" {
  17614. #endif
  17615. /**
  17616. * \brief PKCS12 Password Based function (encryption / decryption)
  17617. * for pbeWithSHAAnd128BitRC4
  17618. *
  17619. * \param pbe_params an ASN1 buffer containing the pkcs-12PbeParams structure
  17620. * \param mode either MBEDTLS_PKCS12_PBE_ENCRYPT or MBEDTLS_PKCS12_PBE_DECRYPT
  17621. * \param pwd the password used (may be NULL if no password is used)
  17622. * \param pwdlen length of the password (may be 0)
  17623. * \param input the input data
  17624. * \param len data length
  17625. * \param output the output buffer
  17626. *
  17627. * \return 0 if successful, or a MBEDTLS_ERR_XXX code
  17628. */
  17629. int mbedtls_pkcs12_pbe_sha1_rc4_128( mbedtls_asn1_buf *pbe_params, int mode,
  17630. const unsigned char *pwd, size_t pwdlen,
  17631. const unsigned char *input, size_t len,
  17632. unsigned char *output );
  17633. /**
  17634. * \brief PKCS12 Password Based function (encryption / decryption)
  17635. * for cipher-based and mbedtls_md-based PBE's
  17636. *
  17637. * \param pbe_params an ASN1 buffer containing the pkcs-12PbeParams structure
  17638. * \param mode either MBEDTLS_PKCS12_PBE_ENCRYPT or MBEDTLS_PKCS12_PBE_DECRYPT
  17639. * \param cipher_type the cipher used
  17640. * \param md_type the mbedtls_md used
  17641. * \param pwd the password used (may be NULL if no password is used)
  17642. * \param pwdlen length of the password (may be 0)
  17643. * \param input the input data
  17644. * \param len data length
  17645. * \param output the output buffer
  17646. *
  17647. * \return 0 if successful, or a MBEDTLS_ERR_XXX code
  17648. */
  17649. int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode,
  17650. mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
  17651. const unsigned char *pwd, size_t pwdlen,
  17652. const unsigned char *input, size_t len,
  17653. unsigned char *output );
  17654. /**
  17655. * \brief The PKCS#12 derivation function uses a password and a salt
  17656. * to produce pseudo-random bits for a particular "purpose".
  17657. *
  17658. * Depending on the given id, this function can produce an
  17659. * encryption/decryption key, an nitialization vector or an
  17660. * integrity key.
  17661. *
  17662. * \param data buffer to store the derived data in
  17663. * \param datalen length to fill
  17664. * \param pwd password to use (may be NULL if no password is used)
  17665. * \param pwdlen length of the password (may be 0)
  17666. * \param salt salt buffer to use
  17667. * \param saltlen length of the salt
  17668. * \param mbedtls_md mbedtls_md type to use during the derivation
  17669. * \param id id that describes the purpose (can be MBEDTLS_PKCS12_DERIVE_KEY,
  17670. * MBEDTLS_PKCS12_DERIVE_IV or MBEDTLS_PKCS12_DERIVE_MAC_KEY)
  17671. * \param iterations number of iterations
  17672. *
  17673. * \return 0 if successful, or a MD, BIGNUM type error.
  17674. */
  17675. int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
  17676. const unsigned char *pwd, size_t pwdlen,
  17677. const unsigned char *salt, size_t saltlen,
  17678. mbedtls_md_type_t mbedtls_md, int id, int iterations );
  17679. #ifdef __cplusplus
  17680. }
  17681. #endif
  17682. #endif /* pkcs12.h */
  17683. /********* Start of file include/mbedtls/pkcs11.h ************/
  17684. /**
  17685. * \file pkcs11.h
  17686. *
  17687. * \brief Wrapper for PKCS#11 library libpkcs11-helper
  17688. *
  17689. * \author Adriaan de Jong <dejong@fox-it.com>
  17690. *
  17691. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  17692. * SPDX-License-Identifier: Apache-2.0
  17693. *
  17694. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  17695. * not use this file except in compliance with the License.
  17696. * You may obtain a copy of the License at
  17697. *
  17698. * http://www.apache.org/licenses/LICENSE-2.0
  17699. *
  17700. * Unless required by applicable law or agreed to in writing, software
  17701. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17702. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17703. * See the License for the specific language governing permissions and
  17704. * limitations under the License.
  17705. *
  17706. * This file is part of mbed TLS (https://tls.mbed.org)
  17707. */
  17708. #ifndef MBEDTLS_PKCS11_H
  17709. #define MBEDTLS_PKCS11_H
  17710. #if !defined(MBEDTLS_CONFIG_FILE)
  17711. #else
  17712. #endif
  17713. #if defined(MBEDTLS_PKCS11_C)
  17714. #include <pkcs11-helper-1.0/pkcs11h-certificate.h>
  17715. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  17716. !defined(inline) && !defined(__cplusplus)
  17717. #define inline __inline
  17718. #endif
  17719. #ifdef __cplusplus
  17720. extern "C" {
  17721. #endif
  17722. /**
  17723. * Context for PKCS #11 private keys.
  17724. */
  17725. typedef struct {
  17726. pkcs11h_certificate_t pkcs11h_cert;
  17727. int len;
  17728. } mbedtls_pkcs11_context;
  17729. /**
  17730. * Initialize a mbedtls_pkcs11_context.
  17731. * (Just making memory references valid.)
  17732. */
  17733. void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx );
  17734. /**
  17735. * Fill in a mbed TLS certificate, based on the given PKCS11 helper certificate.
  17736. *
  17737. * \param cert X.509 certificate to fill
  17738. * \param pkcs11h_cert PKCS #11 helper certificate
  17739. *
  17740. * \return 0 on success.
  17741. */
  17742. int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11h_cert );
  17743. /**
  17744. * Set up a mbedtls_pkcs11_context storing the given certificate. Note that the
  17745. * mbedtls_pkcs11_context will take over control of the certificate, freeing it when
  17746. * done.
  17747. *
  17748. * \param priv_key Private key structure to fill.
  17749. * \param pkcs11_cert PKCS #11 helper certificate
  17750. *
  17751. * \return 0 on success
  17752. */
  17753. int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
  17754. pkcs11h_certificate_t pkcs11_cert );
  17755. /**
  17756. * Free the contents of the given private key context. Note that the structure
  17757. * itself is not freed.
  17758. *
  17759. * \param priv_key Private key structure to cleanup
  17760. */
  17761. void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key );
  17762. /**
  17763. * \brief Do an RSA private key decrypt, then remove the message
  17764. * padding
  17765. *
  17766. * \param ctx PKCS #11 context
  17767. * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature
  17768. * \param input buffer holding the encrypted data
  17769. * \param output buffer that will hold the plaintext
  17770. * \param olen will contain the plaintext length
  17771. * \param output_max_len maximum length of the output buffer
  17772. *
  17773. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  17774. *
  17775. * \note The output buffer must be as large as the size
  17776. * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
  17777. * an error is thrown.
  17778. */
  17779. int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
  17780. int mode, size_t *olen,
  17781. const unsigned char *input,
  17782. unsigned char *output,
  17783. size_t output_max_len );
  17784. /**
  17785. * \brief Do a private RSA to sign a message digest
  17786. *
  17787. * \param ctx PKCS #11 context
  17788. * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature
  17789. * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
  17790. * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
  17791. * \param hash buffer holding the message digest
  17792. * \param sig buffer that will hold the ciphertext
  17793. *
  17794. * \return 0 if the signing operation was successful,
  17795. * or an MBEDTLS_ERR_RSA_XXX error code
  17796. *
  17797. * \note The "sig" buffer must be as large as the size
  17798. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  17799. */
  17800. int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
  17801. int mode,
  17802. mbedtls_md_type_t md_alg,
  17803. unsigned int hashlen,
  17804. const unsigned char *hash,
  17805. unsigned char *sig );
  17806. /**
  17807. * SSL/TLS wrappers for PKCS#11 functions
  17808. */
  17809. static inline int mbedtls_ssl_pkcs11_decrypt( void *ctx, int mode, size_t *olen,
  17810. const unsigned char *input, unsigned char *output,
  17811. size_t output_max_len )
  17812. {
  17813. return mbedtls_pkcs11_decrypt( (mbedtls_pkcs11_context *) ctx, mode, olen, input, output,
  17814. output_max_len );
  17815. }
  17816. static inline int mbedtls_ssl_pkcs11_sign( void *ctx,
  17817. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  17818. int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
  17819. const unsigned char *hash, unsigned char *sig )
  17820. {
  17821. ((void) f_rng);
  17822. ((void) p_rng);
  17823. return mbedtls_pkcs11_sign( (mbedtls_pkcs11_context *) ctx, mode, md_alg,
  17824. hashlen, hash, sig );
  17825. }
  17826. static inline size_t mbedtls_ssl_pkcs11_key_len( void *ctx )
  17827. {
  17828. return ( (mbedtls_pkcs11_context *) ctx )->len;
  17829. }
  17830. #ifdef __cplusplus
  17831. }
  17832. #endif
  17833. #endif /* MBEDTLS_PKCS11_C */
  17834. #endif /* MBEDTLS_PKCS11_H */
  17835. /********* Start of file include/mbedtls/pkcs5.h ************/
  17836. /**
  17837. * \file pkcs5.h
  17838. *
  17839. * \brief PKCS#5 functions
  17840. *
  17841. * \author Mathias Olsson <mathias@kompetensum.com>
  17842. *
  17843. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  17844. * SPDX-License-Identifier: Apache-2.0
  17845. *
  17846. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  17847. * not use this file except in compliance with the License.
  17848. * You may obtain a copy of the License at
  17849. *
  17850. * http://www.apache.org/licenses/LICENSE-2.0
  17851. *
  17852. * Unless required by applicable law or agreed to in writing, software
  17853. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17854. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17855. * See the License for the specific language governing permissions and
  17856. * limitations under the License.
  17857. *
  17858. * This file is part of mbed TLS (https://tls.mbed.org)
  17859. */
  17860. #ifndef MBEDTLS_PKCS5_H
  17861. #define MBEDTLS_PKCS5_H
  17862. #include <stddef.h>
  17863. #if !VXWORKS
  17864. #include <stdint.h>
  17865. #endif
  17866. #define MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA -0x2f80 /**< Bad input parameters to function. */
  17867. #define MBEDTLS_ERR_PKCS5_INVALID_FORMAT -0x2f00 /**< Unexpected ASN.1 data. */
  17868. #define MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE -0x2e80 /**< Requested encryption or digest alg not available. */
  17869. #define MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH -0x2e00 /**< Given private key password does not allow for correct decryption. */
  17870. #define MBEDTLS_PKCS5_DECRYPT 0
  17871. #define MBEDTLS_PKCS5_ENCRYPT 1
  17872. #ifdef __cplusplus
  17873. extern "C" {
  17874. #endif
  17875. /**
  17876. * \brief PKCS#5 PBES2 function
  17877. *
  17878. * \param pbe_params the ASN.1 algorithm parameters
  17879. * \param mode either MBEDTLS_PKCS5_DECRYPT or MBEDTLS_PKCS5_ENCRYPT
  17880. * \param pwd password to use when generating key
  17881. * \param pwdlen length of password
  17882. * \param data data to process
  17883. * \param datalen length of data
  17884. * \param output output buffer
  17885. *
  17886. * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails.
  17887. */
  17888. int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode,
  17889. const unsigned char *pwd, size_t pwdlen,
  17890. const unsigned char *data, size_t datalen,
  17891. unsigned char *output );
  17892. /**
  17893. * \brief PKCS#5 PBKDF2 using HMAC
  17894. *
  17895. * \param ctx Generic HMAC context
  17896. * \param password Password to use when generating key
  17897. * \param plen Length of password
  17898. * \param salt Salt to use when generating key
  17899. * \param slen Length of salt
  17900. * \param iteration_count Iteration count
  17901. * \param key_length Length of generated key in bytes
  17902. * \param output Generated key. Must be at least as big as key_length
  17903. *
  17904. * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails.
  17905. */
  17906. int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx, const unsigned char *password,
  17907. size_t plen, const unsigned char *salt, size_t slen,
  17908. unsigned int iteration_count,
  17909. uint32_t key_length, unsigned char *output );
  17910. /**
  17911. * \brief Checkup routine
  17912. *
  17913. * \return 0 if successful, or 1 if the test failed
  17914. */
  17915. int mbedtls_pkcs5_self_test( int verbose );
  17916. #ifdef __cplusplus
  17917. }
  17918. #endif
  17919. #endif /* pkcs5.h */
  17920. /********* Start of file include/mbedtls/oid.h ************/
  17921. /**
  17922. * \file oid.h
  17923. *
  17924. * \brief Object Identifier (OID) database
  17925. *
  17926. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  17927. * SPDX-License-Identifier: Apache-2.0
  17928. *
  17929. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  17930. * not use this file except in compliance with the License.
  17931. * You may obtain a copy of the License at
  17932. *
  17933. * http://www.apache.org/licenses/LICENSE-2.0
  17934. *
  17935. * Unless required by applicable law or agreed to in writing, software
  17936. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17937. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17938. * See the License for the specific language governing permissions and
  17939. * limitations under the License.
  17940. *
  17941. * This file is part of mbed TLS (https://tls.mbed.org)
  17942. */
  17943. #ifndef MBEDTLS_OID_H
  17944. #define MBEDTLS_OID_H
  17945. #if !defined(MBEDTLS_CONFIG_FILE)
  17946. #else
  17947. #endif
  17948. #include <stddef.h>
  17949. #if defined(MBEDTLS_CIPHER_C)
  17950. #endif
  17951. #if defined(MBEDTLS_MD_C)
  17952. #endif
  17953. #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C)
  17954. #endif
  17955. #define MBEDTLS_ERR_OID_NOT_FOUND -0x002E /**< OID is not found. */
  17956. #define MBEDTLS_ERR_OID_BUF_TOO_SMALL -0x000B /**< output buffer is too small */
  17957. /*
  17958. * Top level OID tuples
  17959. */
  17960. #define MBEDTLS_OID_ISO_MEMBER_BODIES "\x2a" /* {iso(1) member-body(2)} */
  17961. #define MBEDTLS_OID_ISO_IDENTIFIED_ORG "\x2b" /* {iso(1) identified-organization(3)} */
  17962. #define MBEDTLS_OID_ISO_CCITT_DS "\x55" /* {joint-iso-ccitt(2) ds(5)} */
  17963. #define MBEDTLS_OID_ISO_ITU_COUNTRY "\x60" /* {joint-iso-itu-t(2) country(16)} */
  17964. /*
  17965. * ISO Member bodies OID parts
  17966. */
  17967. #define MBEDTLS_OID_COUNTRY_US "\x86\x48" /* {us(840)} */
  17968. #define MBEDTLS_OID_ORG_RSA_DATA_SECURITY "\x86\xf7\x0d" /* {rsadsi(113549)} */
  17969. #define MBEDTLS_OID_RSA_COMPANY MBEDTLS_OID_ISO_MEMBER_BODIES MBEDTLS_OID_COUNTRY_US \
  17970. MBEDTLS_OID_ORG_RSA_DATA_SECURITY /* {iso(1) member-body(2) us(840) rsadsi(113549)} */
  17971. #define MBEDTLS_OID_ORG_ANSI_X9_62 "\xce\x3d" /* ansi-X9-62(10045) */
  17972. #define MBEDTLS_OID_ANSI_X9_62 MBEDTLS_OID_ISO_MEMBER_BODIES MBEDTLS_OID_COUNTRY_US \
  17973. MBEDTLS_OID_ORG_ANSI_X9_62
  17974. /*
  17975. * ISO Identified organization OID parts
  17976. */
  17977. #define MBEDTLS_OID_ORG_DOD "\x06" /* {dod(6)} */
  17978. #define MBEDTLS_OID_ORG_OIW "\x0e"
  17979. #define MBEDTLS_OID_OIW_SECSIG MBEDTLS_OID_ORG_OIW "\x03"
  17980. #define MBEDTLS_OID_OIW_SECSIG_ALG MBEDTLS_OID_OIW_SECSIG "\x02"
  17981. #define MBEDTLS_OID_OIW_SECSIG_SHA1 MBEDTLS_OID_OIW_SECSIG_ALG "\x1a"
  17982. #define MBEDTLS_OID_ORG_CERTICOM "\x81\x04" /* certicom(132) */
  17983. #define MBEDTLS_OID_CERTICOM MBEDTLS_OID_ISO_IDENTIFIED_ORG MBEDTLS_OID_ORG_CERTICOM
  17984. #define MBEDTLS_OID_ORG_TELETRUST "\x24" /* teletrust(36) */
  17985. #define MBEDTLS_OID_TELETRUST MBEDTLS_OID_ISO_IDENTIFIED_ORG MBEDTLS_OID_ORG_TELETRUST
  17986. /*
  17987. * ISO ITU OID parts
  17988. */
  17989. #define MBEDTLS_OID_ORGANIZATION "\x01" /* {organization(1)} */
  17990. #define MBEDTLS_OID_ISO_ITU_US_ORG MBEDTLS_OID_ISO_ITU_COUNTRY MBEDTLS_OID_COUNTRY_US MBEDTLS_OID_ORGANIZATION /* {joint-iso-itu-t(2) country(16) us(840) organization(1)} */
  17991. #define MBEDTLS_OID_ORG_GOV "\x65" /* {gov(101)} */
  17992. #define MBEDTLS_OID_GOV MBEDTLS_OID_ISO_ITU_US_ORG MBEDTLS_OID_ORG_GOV /* {joint-iso-itu-t(2) country(16) us(840) organization(1) gov(101)} */
  17993. #define MBEDTLS_OID_ORG_NETSCAPE "\x86\xF8\x42" /* {netscape(113730)} */
  17994. #define MBEDTLS_OID_NETSCAPE MBEDTLS_OID_ISO_ITU_US_ORG MBEDTLS_OID_ORG_NETSCAPE /* Netscape OID {joint-iso-itu-t(2) country(16) us(840) organization(1) netscape(113730)} */
  17995. /* ISO arc for standard certificate and CRL extensions */
  17996. #define MBEDTLS_OID_ID_CE MBEDTLS_OID_ISO_CCITT_DS "\x1D" /**< id-ce OBJECT IDENTIFIER ::= {joint-iso-ccitt(2) ds(5) 29} */
  17997. /**
  17998. * Private Internet Extensions
  17999. * { iso(1) identified-organization(3) dod(6) internet(1)
  18000. * security(5) mechanisms(5) pkix(7) }
  18001. */
  18002. #define MBEDTLS_OID_PKIX MBEDTLS_OID_ISO_IDENTIFIED_ORG MBEDTLS_OID_ORG_DOD "\x01\x05\x05\x07"
  18003. /*
  18004. * Arc for standard naming attributes
  18005. */
  18006. #define MBEDTLS_OID_AT MBEDTLS_OID_ISO_CCITT_DS "\x04" /**< id-at OBJECT IDENTIFIER ::= {joint-iso-ccitt(2) ds(5) 4} */
  18007. #define MBEDTLS_OID_AT_CN MBEDTLS_OID_AT "\x03" /**< id-at-commonName AttributeType:= {id-at 3} */
  18008. #define MBEDTLS_OID_AT_SUR_NAME MBEDTLS_OID_AT "\x04" /**< id-at-surName AttributeType:= {id-at 4} */
  18009. #define MBEDTLS_OID_AT_SERIAL_NUMBER MBEDTLS_OID_AT "\x05" /**< id-at-serialNumber AttributeType:= {id-at 5} */
  18010. #define MBEDTLS_OID_AT_COUNTRY MBEDTLS_OID_AT "\x06" /**< id-at-countryName AttributeType:= {id-at 6} */
  18011. #define MBEDTLS_OID_AT_LOCALITY MBEDTLS_OID_AT "\x07" /**< id-at-locality AttributeType:= {id-at 7} */
  18012. #define MBEDTLS_OID_AT_STATE MBEDTLS_OID_AT "\x08" /**< id-at-state AttributeType:= {id-at 8} */
  18013. #define MBEDTLS_OID_AT_ORGANIZATION MBEDTLS_OID_AT "\x0A" /**< id-at-organizationName AttributeType:= {id-at 10} */
  18014. #define MBEDTLS_OID_AT_ORG_UNIT MBEDTLS_OID_AT "\x0B" /**< id-at-organizationalUnitName AttributeType:= {id-at 11} */
  18015. #define MBEDTLS_OID_AT_TITLE MBEDTLS_OID_AT "\x0C" /**< id-at-title AttributeType:= {id-at 12} */
  18016. #define MBEDTLS_OID_AT_POSTAL_ADDRESS MBEDTLS_OID_AT "\x10" /**< id-at-postalAddress AttributeType:= {id-at 16} */
  18017. #define MBEDTLS_OID_AT_POSTAL_CODE MBEDTLS_OID_AT "\x11" /**< id-at-postalCode AttributeType:= {id-at 17} */
  18018. #define MBEDTLS_OID_AT_GIVEN_NAME MBEDTLS_OID_AT "\x2A" /**< id-at-givenName AttributeType:= {id-at 42} */
  18019. #define MBEDTLS_OID_AT_INITIALS MBEDTLS_OID_AT "\x2B" /**< id-at-initials AttributeType:= {id-at 43} */
  18020. #define MBEDTLS_OID_AT_GENERATION_QUALIFIER MBEDTLS_OID_AT "\x2C" /**< id-at-generationQualifier AttributeType:= {id-at 44} */
  18021. #define MBEDTLS_OID_AT_UNIQUE_IDENTIFIER MBEDTLS_OID_AT "\x2D" /**< id-at-uniqueIdentifier AttributType:= {id-at 45} */
  18022. #define MBEDTLS_OID_AT_DN_QUALIFIER MBEDTLS_OID_AT "\x2E" /**< id-at-dnQualifier AttributeType:= {id-at 46} */
  18023. #define MBEDTLS_OID_AT_PSEUDONYM MBEDTLS_OID_AT "\x41" /**< id-at-pseudonym AttributeType:= {id-at 65} */
  18024. #define MBEDTLS_OID_DOMAIN_COMPONENT "\x09\x92\x26\x89\x93\xF2\x2C\x64\x01\x19" /** id-domainComponent AttributeType:= {itu-t(0) data(9) pss(2342) ucl(19200300) pilot(100) pilotAttributeType(1) domainComponent(25)} */
  18025. /*
  18026. * OIDs for standard certificate extensions
  18027. */
  18028. #define MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER MBEDTLS_OID_ID_CE "\x23" /**< id-ce-authorityKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 35 } */
  18029. #define MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER MBEDTLS_OID_ID_CE "\x0E" /**< id-ce-subjectKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 14 } */
  18030. #define MBEDTLS_OID_KEY_USAGE MBEDTLS_OID_ID_CE "\x0F" /**< id-ce-keyUsage OBJECT IDENTIFIER ::= { id-ce 15 } */
  18031. #define MBEDTLS_OID_CERTIFICATE_POLICIES MBEDTLS_OID_ID_CE "\x20" /**< id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 } */
  18032. #define MBEDTLS_OID_POLICY_MAPPINGS MBEDTLS_OID_ID_CE "\x21" /**< id-ce-policyMappings OBJECT IDENTIFIER ::= { id-ce 33 } */
  18033. #define MBEDTLS_OID_SUBJECT_ALT_NAME MBEDTLS_OID_ID_CE "\x11" /**< id-ce-subjectAltName OBJECT IDENTIFIER ::= { id-ce 17 } */
  18034. #define MBEDTLS_OID_ISSUER_ALT_NAME MBEDTLS_OID_ID_CE "\x12" /**< id-ce-issuerAltName OBJECT IDENTIFIER ::= { id-ce 18 } */
  18035. #define MBEDTLS_OID_SUBJECT_DIRECTORY_ATTRS MBEDTLS_OID_ID_CE "\x09" /**< id-ce-subjectDirectoryAttributes OBJECT IDENTIFIER ::= { id-ce 9 } */
  18036. #define MBEDTLS_OID_BASIC_CONSTRAINTS MBEDTLS_OID_ID_CE "\x13" /**< id-ce-basicConstraints OBJECT IDENTIFIER ::= { id-ce 19 } */
  18037. #define MBEDTLS_OID_NAME_CONSTRAINTS MBEDTLS_OID_ID_CE "\x1E" /**< id-ce-nameConstraints OBJECT IDENTIFIER ::= { id-ce 30 } */
  18038. #define MBEDTLS_OID_POLICY_CONSTRAINTS MBEDTLS_OID_ID_CE "\x24" /**< id-ce-policyConstraints OBJECT IDENTIFIER ::= { id-ce 36 } */
  18039. #define MBEDTLS_OID_EXTENDED_KEY_USAGE MBEDTLS_OID_ID_CE "\x25" /**< id-ce-extKeyUsage OBJECT IDENTIFIER ::= { id-ce 37 } */
  18040. #define MBEDTLS_OID_CRL_DISTRIBUTION_POINTS MBEDTLS_OID_ID_CE "\x1F" /**< id-ce-cRLDistributionPoints OBJECT IDENTIFIER ::= { id-ce 31 } */
  18041. #define MBEDTLS_OID_INIHIBIT_ANYPOLICY MBEDTLS_OID_ID_CE "\x36" /**< id-ce-inhibitAnyPolicy OBJECT IDENTIFIER ::= { id-ce 54 } */
  18042. #define MBEDTLS_OID_FRESHEST_CRL MBEDTLS_OID_ID_CE "\x2E" /**< id-ce-freshestCRL OBJECT IDENTIFIER ::= { id-ce 46 } */
  18043. /*
  18044. * Netscape certificate extensions
  18045. */
  18046. #define MBEDTLS_OID_NS_CERT MBEDTLS_OID_NETSCAPE "\x01"
  18047. #define MBEDTLS_OID_NS_CERT_TYPE MBEDTLS_OID_NS_CERT "\x01"
  18048. #define MBEDTLS_OID_NS_BASE_URL MBEDTLS_OID_NS_CERT "\x02"
  18049. #define MBEDTLS_OID_NS_REVOCATION_URL MBEDTLS_OID_NS_CERT "\x03"
  18050. #define MBEDTLS_OID_NS_CA_REVOCATION_URL MBEDTLS_OID_NS_CERT "\x04"
  18051. #define MBEDTLS_OID_NS_RENEWAL_URL MBEDTLS_OID_NS_CERT "\x07"
  18052. #define MBEDTLS_OID_NS_CA_POLICY_URL MBEDTLS_OID_NS_CERT "\x08"
  18053. #define MBEDTLS_OID_NS_SSL_SERVER_NAME MBEDTLS_OID_NS_CERT "\x0C"
  18054. #define MBEDTLS_OID_NS_COMMENT MBEDTLS_OID_NS_CERT "\x0D"
  18055. #define MBEDTLS_OID_NS_DATA_TYPE MBEDTLS_OID_NETSCAPE "\x02"
  18056. #define MBEDTLS_OID_NS_CERT_SEQUENCE MBEDTLS_OID_NS_DATA_TYPE "\x05"
  18057. /*
  18058. * OIDs for CRL extensions
  18059. */
  18060. #define MBEDTLS_OID_PRIVATE_KEY_USAGE_PERIOD MBEDTLS_OID_ID_CE "\x10"
  18061. #define MBEDTLS_OID_CRL_NUMBER MBEDTLS_OID_ID_CE "\x14" /**< id-ce-cRLNumber OBJECT IDENTIFIER ::= { id-ce 20 } */
  18062. /*
  18063. * X.509 v3 Extended key usage OIDs
  18064. */
  18065. #define MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE MBEDTLS_OID_EXTENDED_KEY_USAGE "\x00" /**< anyExtendedKeyUsage OBJECT IDENTIFIER ::= { id-ce-extKeyUsage 0 } */
  18066. #define MBEDTLS_OID_KP MBEDTLS_OID_PKIX "\x03" /**< id-kp OBJECT IDENTIFIER ::= { id-pkix 3 } */
  18067. #define MBEDTLS_OID_SERVER_AUTH MBEDTLS_OID_KP "\x01" /**< id-kp-serverAuth OBJECT IDENTIFIER ::= { id-kp 1 } */
  18068. #define MBEDTLS_OID_CLIENT_AUTH MBEDTLS_OID_KP "\x02" /**< id-kp-clientAuth OBJECT IDENTIFIER ::= { id-kp 2 } */
  18069. #define MBEDTLS_OID_CODE_SIGNING MBEDTLS_OID_KP "\x03" /**< id-kp-codeSigning OBJECT IDENTIFIER ::= { id-kp 3 } */
  18070. #define MBEDTLS_OID_EMAIL_PROTECTION MBEDTLS_OID_KP "\x04" /**< id-kp-emailProtection OBJECT IDENTIFIER ::= { id-kp 4 } */
  18071. #define MBEDTLS_OID_TIME_STAMPING MBEDTLS_OID_KP "\x08" /**< id-kp-timeStamping OBJECT IDENTIFIER ::= { id-kp 8 } */
  18072. #define MBEDTLS_OID_OCSP_SIGNING MBEDTLS_OID_KP "\x09" /**< id-kp-OCSPSigning OBJECT IDENTIFIER ::= { id-kp 9 } */
  18073. /*
  18074. * PKCS definition OIDs
  18075. */
  18076. #define MBEDTLS_OID_PKCS MBEDTLS_OID_RSA_COMPANY "\x01" /**< pkcs OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840) rsadsi(113549) 1 } */
  18077. #define MBEDTLS_OID_PKCS1 MBEDTLS_OID_PKCS "\x01" /**< pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) 1 } */
  18078. #define MBEDTLS_OID_PKCS5 MBEDTLS_OID_PKCS "\x05" /**< pkcs-5 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) 5 } */
  18079. #define MBEDTLS_OID_PKCS9 MBEDTLS_OID_PKCS "\x09" /**< pkcs-9 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) 9 } */
  18080. #define MBEDTLS_OID_PKCS12 MBEDTLS_OID_PKCS "\x0c" /**< pkcs-12 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) 12 } */
  18081. /*
  18082. * PKCS#1 OIDs
  18083. */
  18084. #define MBEDTLS_OID_PKCS1_RSA MBEDTLS_OID_PKCS1 "\x01" /**< rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 } */
  18085. #define MBEDTLS_OID_PKCS1_MD2 MBEDTLS_OID_PKCS1 "\x02" /**< md2WithRSAEncryption ::= { pkcs-1 2 } */
  18086. #define MBEDTLS_OID_PKCS1_MD4 MBEDTLS_OID_PKCS1 "\x03" /**< md4WithRSAEncryption ::= { pkcs-1 3 } */
  18087. #define MBEDTLS_OID_PKCS1_MD5 MBEDTLS_OID_PKCS1 "\x04" /**< md5WithRSAEncryption ::= { pkcs-1 4 } */
  18088. #define MBEDTLS_OID_PKCS1_SHA1 MBEDTLS_OID_PKCS1 "\x05" /**< sha1WithRSAEncryption ::= { pkcs-1 5 } */
  18089. #define MBEDTLS_OID_PKCS1_SHA224 MBEDTLS_OID_PKCS1 "\x0e" /**< sha224WithRSAEncryption ::= { pkcs-1 14 } */
  18090. #define MBEDTLS_OID_PKCS1_SHA256 MBEDTLS_OID_PKCS1 "\x0b" /**< sha256WithRSAEncryption ::= { pkcs-1 11 } */
  18091. #define MBEDTLS_OID_PKCS1_SHA384 MBEDTLS_OID_PKCS1 "\x0c" /**< sha384WithRSAEncryption ::= { pkcs-1 12 } */
  18092. #define MBEDTLS_OID_PKCS1_SHA512 MBEDTLS_OID_PKCS1 "\x0d" /**< sha512WithRSAEncryption ::= { pkcs-1 13 } */
  18093. #define MBEDTLS_OID_RSA_SHA_OBS "\x2B\x0E\x03\x02\x1D"
  18094. #define MBEDTLS_OID_PKCS9_EMAIL MBEDTLS_OID_PKCS9 "\x01" /**< emailAddress AttributeType ::= { pkcs-9 1 } */
  18095. /* RFC 4055 */
  18096. #define MBEDTLS_OID_RSASSA_PSS MBEDTLS_OID_PKCS1 "\x0a" /**< id-RSASSA-PSS ::= { pkcs-1 10 } */
  18097. #define MBEDTLS_OID_MGF1 MBEDTLS_OID_PKCS1 "\x08" /**< id-mgf1 ::= { pkcs-1 8 } */
  18098. /*
  18099. * Digest algorithms
  18100. */
  18101. #define MBEDTLS_OID_DIGEST_ALG_MD2 MBEDTLS_OID_RSA_COMPANY "\x02\x02" /**< id-mbedtls_md2 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840) rsadsi(113549) digestAlgorithm(2) 2 } */
  18102. #define MBEDTLS_OID_DIGEST_ALG_MD4 MBEDTLS_OID_RSA_COMPANY "\x02\x04" /**< id-mbedtls_md4 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840) rsadsi(113549) digestAlgorithm(2) 4 } */
  18103. #define MBEDTLS_OID_DIGEST_ALG_MD5 MBEDTLS_OID_RSA_COMPANY "\x02\x05" /**< id-mbedtls_md5 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840) rsadsi(113549) digestAlgorithm(2) 5 } */
  18104. #define MBEDTLS_OID_DIGEST_ALG_SHA1 MBEDTLS_OID_ISO_IDENTIFIED_ORG MBEDTLS_OID_OIW_SECSIG_SHA1 /**< id-mbedtls_sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3) oiw(14) secsig(3) algorithms(2) 26 } */
  18105. #define MBEDTLS_OID_DIGEST_ALG_SHA224 MBEDTLS_OID_GOV "\x03\x04\x02\x04" /**< id-sha224 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840) organization(1) gov(101) csor(3) nistalgorithm(4) hashalgs(2) 4 } */
  18106. #define MBEDTLS_OID_DIGEST_ALG_SHA256 MBEDTLS_OID_GOV "\x03\x04\x02\x01" /**< id-mbedtls_sha256 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840) organization(1) gov(101) csor(3) nistalgorithm(4) hashalgs(2) 1 } */
  18107. #define MBEDTLS_OID_DIGEST_ALG_SHA384 MBEDTLS_OID_GOV "\x03\x04\x02\x02" /**< id-sha384 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840) organization(1) gov(101) csor(3) nistalgorithm(4) hashalgs(2) 2 } */
  18108. #define MBEDTLS_OID_DIGEST_ALG_SHA512 MBEDTLS_OID_GOV "\x03\x04\x02\x03" /**< id-mbedtls_sha512 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840) organization(1) gov(101) csor(3) nistalgorithm(4) hashalgs(2) 3 } */
  18109. #define MBEDTLS_OID_HMAC_SHA1 MBEDTLS_OID_RSA_COMPANY "\x02\x07" /**< id-hmacWithSHA1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840) rsadsi(113549) digestAlgorithm(2) 7 } */
  18110. /*
  18111. * Encryption algorithms
  18112. */
  18113. #define MBEDTLS_OID_DES_CBC MBEDTLS_OID_ISO_IDENTIFIED_ORG MBEDTLS_OID_OIW_SECSIG_ALG "\x07" /**< desCBC OBJECT IDENTIFIER ::= { iso(1) identified-organization(3) oiw(14) secsig(3) algorithms(2) 7 } */
  18114. #define MBEDTLS_OID_DES_EDE3_CBC MBEDTLS_OID_RSA_COMPANY "\x03\x07" /**< des-ede3-cbc OBJECT IDENTIFIER ::= { iso(1) member-body(2) -- us(840) rsadsi(113549) encryptionAlgorithm(3) 7 } */
  18115. /*
  18116. * PKCS#5 OIDs
  18117. */
  18118. #define MBEDTLS_OID_PKCS5_PBKDF2 MBEDTLS_OID_PKCS5 "\x0c" /**< id-PBKDF2 OBJECT IDENTIFIER ::= {pkcs-5 12} */
  18119. #define MBEDTLS_OID_PKCS5_PBES2 MBEDTLS_OID_PKCS5 "\x0d" /**< id-PBES2 OBJECT IDENTIFIER ::= {pkcs-5 13} */
  18120. #define MBEDTLS_OID_PKCS5_PBMAC1 MBEDTLS_OID_PKCS5 "\x0e" /**< id-PBMAC1 OBJECT IDENTIFIER ::= {pkcs-5 14} */
  18121. /*
  18122. * PKCS#5 PBES1 algorithms
  18123. */
  18124. #define MBEDTLS_OID_PKCS5_PBE_MD2_DES_CBC MBEDTLS_OID_PKCS5 "\x01" /**< pbeWithMD2AndDES-CBC OBJECT IDENTIFIER ::= {pkcs-5 1} */
  18125. #define MBEDTLS_OID_PKCS5_PBE_MD2_RC2_CBC MBEDTLS_OID_PKCS5 "\x04" /**< pbeWithMD2AndRC2-CBC OBJECT IDENTIFIER ::= {pkcs-5 4} */
  18126. #define MBEDTLS_OID_PKCS5_PBE_MD5_DES_CBC MBEDTLS_OID_PKCS5 "\x03" /**< pbeWithMD5AndDES-CBC OBJECT IDENTIFIER ::= {pkcs-5 3} */
  18127. #define MBEDTLS_OID_PKCS5_PBE_MD5_RC2_CBC MBEDTLS_OID_PKCS5 "\x06" /**< pbeWithMD5AndRC2-CBC OBJECT IDENTIFIER ::= {pkcs-5 6} */
  18128. #define MBEDTLS_OID_PKCS5_PBE_SHA1_DES_CBC MBEDTLS_OID_PKCS5 "\x0a" /**< pbeWithSHA1AndDES-CBC OBJECT IDENTIFIER ::= {pkcs-5 10} */
  18129. #define MBEDTLS_OID_PKCS5_PBE_SHA1_RC2_CBC MBEDTLS_OID_PKCS5 "\x0b" /**< pbeWithSHA1AndRC2-CBC OBJECT IDENTIFIER ::= {pkcs-5 11} */
  18130. /*
  18131. * PKCS#8 OIDs
  18132. */
  18133. #define MBEDTLS_OID_PKCS9_CSR_EXT_REQ MBEDTLS_OID_PKCS9 "\x0e" /**< extensionRequest OBJECT IDENTIFIER ::= {pkcs-9 14} */
  18134. /*
  18135. * PKCS#12 PBE OIDs
  18136. */
  18137. #define MBEDTLS_OID_PKCS12_PBE MBEDTLS_OID_PKCS12 "\x01" /**< pkcs-12PbeIds OBJECT IDENTIFIER ::= {pkcs-12 1} */
  18138. #define MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128 MBEDTLS_OID_PKCS12_PBE "\x01" /**< pbeWithSHAAnd128BitRC4 OBJECT IDENTIFIER ::= {pkcs-12PbeIds 1} */
  18139. #define MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_40 MBEDTLS_OID_PKCS12_PBE "\x02" /**< pbeWithSHAAnd40BitRC4 OBJECT IDENTIFIER ::= {pkcs-12PbeIds 2} */
  18140. #define MBEDTLS_OID_PKCS12_PBE_SHA1_DES3_EDE_CBC MBEDTLS_OID_PKCS12_PBE "\x03" /**< pbeWithSHAAnd3-KeyTripleDES-CBC OBJECT IDENTIFIER ::= {pkcs-12PbeIds 3} */
  18141. #define MBEDTLS_OID_PKCS12_PBE_SHA1_DES2_EDE_CBC MBEDTLS_OID_PKCS12_PBE "\x04" /**< pbeWithSHAAnd2-KeyTripleDES-CBC OBJECT IDENTIFIER ::= {pkcs-12PbeIds 4} */
  18142. #define MBEDTLS_OID_PKCS12_PBE_SHA1_RC2_128_CBC MBEDTLS_OID_PKCS12_PBE "\x05" /**< pbeWithSHAAnd128BitRC2-CBC OBJECT IDENTIFIER ::= {pkcs-12PbeIds 5} */
  18143. #define MBEDTLS_OID_PKCS12_PBE_SHA1_RC2_40_CBC MBEDTLS_OID_PKCS12_PBE "\x06" /**< pbeWithSHAAnd40BitRC2-CBC OBJECT IDENTIFIER ::= {pkcs-12PbeIds 6} */
  18144. /*
  18145. * EC key algorithms from RFC 5480
  18146. */
  18147. /* id-ecPublicKey OBJECT IDENTIFIER ::= {
  18148. * iso(1) member-body(2) us(840) ansi-X9-62(10045) keyType(2) 1 } */
  18149. #define MBEDTLS_OID_EC_ALG_UNRESTRICTED MBEDTLS_OID_ANSI_X9_62 "\x02\01"
  18150. /* id-ecDH OBJECT IDENTIFIER ::= {
  18151. * iso(1) identified-organization(3) certicom(132)
  18152. * schemes(1) ecdh(12) } */
  18153. #define MBEDTLS_OID_EC_ALG_ECDH MBEDTLS_OID_CERTICOM "\x01\x0c"
  18154. /*
  18155. * ECParameters namedCurve identifiers, from RFC 5480, RFC 5639, and SEC2
  18156. */
  18157. /* secp192r1 OBJECT IDENTIFIER ::= {
  18158. * iso(1) member-body(2) us(840) ansi-X9-62(10045) curves(3) prime(1) 1 } */
  18159. #define MBEDTLS_OID_EC_GRP_SECP192R1 MBEDTLS_OID_ANSI_X9_62 "\x03\x01\x01"
  18160. /* secp224r1 OBJECT IDENTIFIER ::= {
  18161. * iso(1) identified-organization(3) certicom(132) curve(0) 33 } */
  18162. #define MBEDTLS_OID_EC_GRP_SECP224R1 MBEDTLS_OID_CERTICOM "\x00\x21"
  18163. /* secp256r1 OBJECT IDENTIFIER ::= {
  18164. * iso(1) member-body(2) us(840) ansi-X9-62(10045) curves(3) prime(1) 7 } */
  18165. #define MBEDTLS_OID_EC_GRP_SECP256R1 MBEDTLS_OID_ANSI_X9_62 "\x03\x01\x07"
  18166. /* secp384r1 OBJECT IDENTIFIER ::= {
  18167. * iso(1) identified-organization(3) certicom(132) curve(0) 34 } */
  18168. #define MBEDTLS_OID_EC_GRP_SECP384R1 MBEDTLS_OID_CERTICOM "\x00\x22"
  18169. /* secp521r1 OBJECT IDENTIFIER ::= {
  18170. * iso(1) identified-organization(3) certicom(132) curve(0) 35 } */
  18171. #define MBEDTLS_OID_EC_GRP_SECP521R1 MBEDTLS_OID_CERTICOM "\x00\x23"
  18172. /* secp192k1 OBJECT IDENTIFIER ::= {
  18173. * iso(1) identified-organization(3) certicom(132) curve(0) 31 } */
  18174. #define MBEDTLS_OID_EC_GRP_SECP192K1 MBEDTLS_OID_CERTICOM "\x00\x1f"
  18175. /* secp224k1 OBJECT IDENTIFIER ::= {
  18176. * iso(1) identified-organization(3) certicom(132) curve(0) 32 } */
  18177. #define MBEDTLS_OID_EC_GRP_SECP224K1 MBEDTLS_OID_CERTICOM "\x00\x20"
  18178. /* secp256k1 OBJECT IDENTIFIER ::= {
  18179. * iso(1) identified-organization(3) certicom(132) curve(0) 10 } */
  18180. #define MBEDTLS_OID_EC_GRP_SECP256K1 MBEDTLS_OID_CERTICOM "\x00\x0a"
  18181. /* RFC 5639 4.1
  18182. * ecStdCurvesAndGeneration OBJECT IDENTIFIER::= {iso(1)
  18183. * identified-organization(3) teletrust(36) algorithm(3) signature-
  18184. * algorithm(3) ecSign(2) 8}
  18185. * ellipticCurve OBJECT IDENTIFIER ::= {ecStdCurvesAndGeneration 1}
  18186. * versionOne OBJECT IDENTIFIER ::= {ellipticCurve 1} */
  18187. #define MBEDTLS_OID_EC_BRAINPOOL_V1 MBEDTLS_OID_TELETRUST "\x03\x03\x02\x08\x01\x01"
  18188. /* brainpoolP256r1 OBJECT IDENTIFIER ::= {versionOne 7} */
  18189. #define MBEDTLS_OID_EC_GRP_BP256R1 MBEDTLS_OID_EC_BRAINPOOL_V1 "\x07"
  18190. /* brainpoolP384r1 OBJECT IDENTIFIER ::= {versionOne 11} */
  18191. #define MBEDTLS_OID_EC_GRP_BP384R1 MBEDTLS_OID_EC_BRAINPOOL_V1 "\x0B"
  18192. /* brainpoolP512r1 OBJECT IDENTIFIER ::= {versionOne 13} */
  18193. #define MBEDTLS_OID_EC_GRP_BP512R1 MBEDTLS_OID_EC_BRAINPOOL_V1 "\x0D"
  18194. /*
  18195. * SEC1 C.1
  18196. *
  18197. * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
  18198. * id-fieldType OBJECT IDENTIFIER ::= { ansi-X9-62 fieldType(1)}
  18199. */
  18200. #define MBEDTLS_OID_ANSI_X9_62_FIELD_TYPE MBEDTLS_OID_ANSI_X9_62 "\x01"
  18201. #define MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD MBEDTLS_OID_ANSI_X9_62_FIELD_TYPE "\x01"
  18202. /*
  18203. * ECDSA signature identifiers, from RFC 5480
  18204. */
  18205. #define MBEDTLS_OID_ANSI_X9_62_SIG MBEDTLS_OID_ANSI_X9_62 "\x04" /* signatures(4) */
  18206. #define MBEDTLS_OID_ANSI_X9_62_SIG_SHA2 MBEDTLS_OID_ANSI_X9_62_SIG "\x03" /* ecdsa-with-SHA2(3) */
  18207. /* ecdsa-with-SHA1 OBJECT IDENTIFIER ::= {
  18208. * iso(1) member-body(2) us(840) ansi-X9-62(10045) signatures(4) 1 } */
  18209. #define MBEDTLS_OID_ECDSA_SHA1 MBEDTLS_OID_ANSI_X9_62_SIG "\x01"
  18210. /* ecdsa-with-SHA224 OBJECT IDENTIFIER ::= {
  18211. * iso(1) member-body(2) us(840) ansi-X9-62(10045) signatures(4)
  18212. * ecdsa-with-SHA2(3) 1 } */
  18213. #define MBEDTLS_OID_ECDSA_SHA224 MBEDTLS_OID_ANSI_X9_62_SIG_SHA2 "\x01"
  18214. /* ecdsa-with-SHA256 OBJECT IDENTIFIER ::= {
  18215. * iso(1) member-body(2) us(840) ansi-X9-62(10045) signatures(4)
  18216. * ecdsa-with-SHA2(3) 2 } */
  18217. #define MBEDTLS_OID_ECDSA_SHA256 MBEDTLS_OID_ANSI_X9_62_SIG_SHA2 "\x02"
  18218. /* ecdsa-with-SHA384 OBJECT IDENTIFIER ::= {
  18219. * iso(1) member-body(2) us(840) ansi-X9-62(10045) signatures(4)
  18220. * ecdsa-with-SHA2(3) 3 } */
  18221. #define MBEDTLS_OID_ECDSA_SHA384 MBEDTLS_OID_ANSI_X9_62_SIG_SHA2 "\x03"
  18222. /* ecdsa-with-SHA512 OBJECT IDENTIFIER ::= {
  18223. * iso(1) member-body(2) us(840) ansi-X9-62(10045) signatures(4)
  18224. * ecdsa-with-SHA2(3) 4 } */
  18225. #define MBEDTLS_OID_ECDSA_SHA512 MBEDTLS_OID_ANSI_X9_62_SIG_SHA2 "\x04"
  18226. #ifdef __cplusplus
  18227. extern "C" {
  18228. #endif
  18229. /**
  18230. * \brief Base OID descriptor structure
  18231. */
  18232. typedef struct {
  18233. const char *asn1; /*!< OID ASN.1 representation */
  18234. size_t asn1_len; /*!< length of asn1 */
  18235. const char *name; /*!< official name (e.g. from RFC) */
  18236. const char *description; /*!< human friendly description */
  18237. } mbedtls_oid_descriptor_t;
  18238. /**
  18239. * \brief Translate an ASN.1 OID into its numeric representation
  18240. * (e.g. "\x2A\x86\x48\x86\xF7\x0D" into "1.2.840.113549")
  18241. *
  18242. * \param buf buffer to put representation in
  18243. * \param size size of the buffer
  18244. * \param oid OID to translate
  18245. *
  18246. * \return Length of the string written (excluding final NULL) or
  18247. * MBEDTLS_ERR_OID_BUF_TOO_SMALL in case of error
  18248. */
  18249. int mbedtls_oid_get_numeric_string( char *buf, size_t size, const mbedtls_asn1_buf *oid );
  18250. #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C)
  18251. /**
  18252. * \brief Translate an X.509 extension OID into local values
  18253. *
  18254. * \param oid OID to use
  18255. * \param ext_type place to store the extension type
  18256. *
  18257. * \return 0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  18258. */
  18259. int mbedtls_oid_get_x509_ext_type( const mbedtls_asn1_buf *oid, int *ext_type );
  18260. #endif
  18261. /**
  18262. * \brief Translate an X.509 attribute type OID into the short name
  18263. * (e.g. the OID for an X520 Common Name into "CN")
  18264. *
  18265. * \param oid OID to use
  18266. * \param short_name place to store the string pointer
  18267. *
  18268. * \return 0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  18269. */
  18270. int mbedtls_oid_get_attr_short_name( const mbedtls_asn1_buf *oid, const char **short_name );
  18271. /**
  18272. * \brief Translate PublicKeyAlgorithm OID into pk_type
  18273. *
  18274. * \param oid OID to use
  18275. * \param pk_alg place to store public key algorithm
  18276. *
  18277. * \return 0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  18278. */
  18279. int mbedtls_oid_get_pk_alg( const mbedtls_asn1_buf *oid, mbedtls_pk_type_t *pk_alg );
  18280. /**
  18281. * \brief Translate pk_type into PublicKeyAlgorithm OID
  18282. *
  18283. * \param pk_alg Public key type to look for
  18284. * \param oid place to store ASN.1 OID string pointer
  18285. * \param olen length of the OID
  18286. *
  18287. * \return 0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  18288. */
  18289. int mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_type_t pk_alg,
  18290. const char **oid, size_t *olen );
  18291. #if defined(MBEDTLS_ECP_C)
  18292. /**
  18293. * \brief Translate NamedCurve OID into an EC group identifier
  18294. *
  18295. * \param oid OID to use
  18296. * \param grp_id place to store group id
  18297. *
  18298. * \return 0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  18299. */
  18300. int mbedtls_oid_get_ec_grp( const mbedtls_asn1_buf *oid, mbedtls_ecp_group_id *grp_id );
  18301. /**
  18302. * \brief Translate EC group identifier into NamedCurve OID
  18303. *
  18304. * \param grp_id EC group identifier
  18305. * \param oid place to store ASN.1 OID string pointer
  18306. * \param olen length of the OID
  18307. *
  18308. * \return 0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  18309. */
  18310. int mbedtls_oid_get_oid_by_ec_grp( mbedtls_ecp_group_id grp_id,
  18311. const char **oid, size_t *olen );
  18312. #endif /* MBEDTLS_ECP_C */
  18313. #if defined(MBEDTLS_MD_C)
  18314. /**
  18315. * \brief Translate SignatureAlgorithm OID into md_type and pk_type
  18316. *
  18317. * \param oid OID to use
  18318. * \param md_alg place to store message digest algorithm
  18319. * \param pk_alg place to store public key algorithm
  18320. *
  18321. * \return 0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  18322. */
  18323. int mbedtls_oid_get_sig_alg( const mbedtls_asn1_buf *oid,
  18324. mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg );
  18325. /**
  18326. * \brief Translate SignatureAlgorithm OID into description
  18327. *
  18328. * \param oid OID to use
  18329. * \param desc place to store string pointer
  18330. *
  18331. * \return 0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  18332. */
  18333. int mbedtls_oid_get_sig_alg_desc( const mbedtls_asn1_buf *oid, const char **desc );
  18334. /**
  18335. * \brief Translate md_type and pk_type into SignatureAlgorithm OID
  18336. *
  18337. * \param md_alg message digest algorithm
  18338. * \param pk_alg public key algorithm
  18339. * \param oid place to store ASN.1 OID string pointer
  18340. * \param olen length of the OID
  18341. *
  18342. * \return 0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  18343. */
  18344. int mbedtls_oid_get_oid_by_sig_alg( mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
  18345. const char **oid, size_t *olen );
  18346. /**
  18347. * \brief Translate hash algorithm OID into md_type
  18348. *
  18349. * \param oid OID to use
  18350. * \param md_alg place to store message digest algorithm
  18351. *
  18352. * \return 0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  18353. */
  18354. int mbedtls_oid_get_md_alg( const mbedtls_asn1_buf *oid, mbedtls_md_type_t *md_alg );
  18355. #endif /* MBEDTLS_MD_C */
  18356. /**
  18357. * \brief Translate Extended Key Usage OID into description
  18358. *
  18359. * \param oid OID to use
  18360. * \param desc place to store string pointer
  18361. *
  18362. * \return 0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  18363. */
  18364. int mbedtls_oid_get_extended_key_usage( const mbedtls_asn1_buf *oid, const char **desc );
  18365. /**
  18366. * \brief Translate md_type into hash algorithm OID
  18367. *
  18368. * \param md_alg message digest algorithm
  18369. * \param oid place to store ASN.1 OID string pointer
  18370. * \param olen length of the OID
  18371. *
  18372. * \return 0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  18373. */
  18374. int mbedtls_oid_get_oid_by_md( mbedtls_md_type_t md_alg, const char **oid, size_t *olen );
  18375. #if defined(MBEDTLS_CIPHER_C)
  18376. /**
  18377. * \brief Translate encryption algorithm OID into cipher_type
  18378. *
  18379. * \param oid OID to use
  18380. * \param cipher_alg place to store cipher algorithm
  18381. *
  18382. * \return 0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  18383. */
  18384. int mbedtls_oid_get_cipher_alg( const mbedtls_asn1_buf *oid, mbedtls_cipher_type_t *cipher_alg );
  18385. #endif /* MBEDTLS_CIPHER_C */
  18386. #if defined(MBEDTLS_PKCS12_C)
  18387. /**
  18388. * \brief Translate PKCS#12 PBE algorithm OID into md_type and
  18389. * cipher_type
  18390. *
  18391. * \param oid OID to use
  18392. * \param md_alg place to store message digest algorithm
  18393. * \param cipher_alg place to store cipher algorithm
  18394. *
  18395. * \return 0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  18396. */
  18397. int mbedtls_oid_get_pkcs12_pbe_alg( const mbedtls_asn1_buf *oid, mbedtls_md_type_t *md_alg,
  18398. mbedtls_cipher_type_t *cipher_alg );
  18399. #endif /* MBEDTLS_PKCS12_C */
  18400. #ifdef __cplusplus
  18401. }
  18402. #endif
  18403. #endif /* oid.h */
  18404. /********* Start of file include/mbedtls/ripemd160.h ************/
  18405. /**
  18406. * \file ripemd160.h
  18407. *
  18408. * \brief RIPE MD-160 message digest
  18409. *
  18410. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  18411. * SPDX-License-Identifier: Apache-2.0
  18412. *
  18413. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  18414. * not use this file except in compliance with the License.
  18415. * You may obtain a copy of the License at
  18416. *
  18417. * http://www.apache.org/licenses/LICENSE-2.0
  18418. *
  18419. * Unless required by applicable law or agreed to in writing, software
  18420. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18421. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18422. * See the License for the specific language governing permissions and
  18423. * limitations under the License.
  18424. *
  18425. * This file is part of mbed TLS (https://tls.mbed.org)
  18426. */
  18427. #ifndef MBEDTLS_RIPEMD160_H
  18428. #define MBEDTLS_RIPEMD160_H
  18429. #if !defined(MBEDTLS_CONFIG_FILE)
  18430. #else
  18431. #endif
  18432. #include <stddef.h>
  18433. #if !VXWORKS
  18434. #include <stdint.h>
  18435. #endif
  18436. #if !defined(MBEDTLS_RIPEMD160_ALT)
  18437. // Regular implementation
  18438. //
  18439. #ifdef __cplusplus
  18440. extern "C" {
  18441. #endif
  18442. /**
  18443. * \brief RIPEMD-160 context structure
  18444. */
  18445. typedef struct
  18446. {
  18447. uint32_t total[2]; /*!< number of bytes processed */
  18448. uint32_t state[5]; /*!< intermediate digest state */
  18449. unsigned char buffer[64]; /*!< data block being processed */
  18450. }
  18451. mbedtls_ripemd160_context;
  18452. /**
  18453. * \brief Initialize RIPEMD-160 context
  18454. *
  18455. * \param ctx RIPEMD-160 context to be initialized
  18456. */
  18457. void mbedtls_ripemd160_init( mbedtls_ripemd160_context *ctx );
  18458. /**
  18459. * \brief Clear RIPEMD-160 context
  18460. *
  18461. * \param ctx RIPEMD-160 context to be cleared
  18462. */
  18463. void mbedtls_ripemd160_free( mbedtls_ripemd160_context *ctx );
  18464. /**
  18465. * \brief Clone (the state of) an RIPEMD-160 context
  18466. *
  18467. * \param dst The destination context
  18468. * \param src The context to be cloned
  18469. */
  18470. void mbedtls_ripemd160_clone( mbedtls_ripemd160_context *dst,
  18471. const mbedtls_ripemd160_context *src );
  18472. /**
  18473. * \brief RIPEMD-160 context setup
  18474. *
  18475. * \param ctx context to be initialized
  18476. */
  18477. void mbedtls_ripemd160_starts( mbedtls_ripemd160_context *ctx );
  18478. /**
  18479. * \brief RIPEMD-160 process buffer
  18480. *
  18481. * \param ctx RIPEMD-160 context
  18482. * \param input buffer holding the data
  18483. * \param ilen length of the input data
  18484. */
  18485. void mbedtls_ripemd160_update( mbedtls_ripemd160_context *ctx,
  18486. const unsigned char *input, size_t ilen );
  18487. /**
  18488. * \brief RIPEMD-160 final digest
  18489. *
  18490. * \param ctx RIPEMD-160 context
  18491. * \param output RIPEMD-160 checksum result
  18492. */
  18493. void mbedtls_ripemd160_finish( mbedtls_ripemd160_context *ctx, unsigned char output[20] );
  18494. /* Internal use */
  18495. void mbedtls_ripemd160_process( mbedtls_ripemd160_context *ctx, const unsigned char data[64] );
  18496. #ifdef __cplusplus
  18497. }
  18498. #endif
  18499. #else /* MBEDTLS_RIPEMD160_ALT */
  18500. #endif /* MBEDTLS_RIPEMD160_ALT */
  18501. #ifdef __cplusplus
  18502. extern "C" {
  18503. #endif
  18504. /**
  18505. * \brief Output = RIPEMD-160( input buffer )
  18506. *
  18507. * \param input buffer holding the data
  18508. * \param ilen length of the input data
  18509. * \param output RIPEMD-160 checksum result
  18510. */
  18511. void mbedtls_ripemd160( const unsigned char *input, size_t ilen,
  18512. unsigned char output[20] );
  18513. /**
  18514. * \brief Checkup routine
  18515. *
  18516. * \return 0 if successful, or 1 if the test failed
  18517. */
  18518. int mbedtls_ripemd160_self_test( int verbose );
  18519. #ifdef __cplusplus
  18520. }
  18521. #endif
  18522. #endif /* mbedtls_ripemd160.h */
  18523. /********* Start of file include/mbedtls/version.h ************/
  18524. /**
  18525. * \file version.h
  18526. *
  18527. * \brief Run-time version information
  18528. *
  18529. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  18530. * SPDX-License-Identifier: Apache-2.0
  18531. *
  18532. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  18533. * not use this file except in compliance with the License.
  18534. * You may obtain a copy of the License at
  18535. *
  18536. * http://www.apache.org/licenses/LICENSE-2.0
  18537. *
  18538. * Unless required by applicable law or agreed to in writing, software
  18539. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18540. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18541. * See the License for the specific language governing permissions and
  18542. * limitations under the License.
  18543. *
  18544. * This file is part of mbed TLS (https://tls.mbed.org)
  18545. */
  18546. /*
  18547. * This set of compile-time defines and run-time variables can be used to
  18548. * determine the version number of the mbed TLS library used.
  18549. */
  18550. #ifndef MBEDTLS_VERSION_H
  18551. #define MBEDTLS_VERSION_H
  18552. #if !defined(MBEDTLS_CONFIG_FILE)
  18553. #else
  18554. #endif
  18555. /**
  18556. * The version number x.y.z is split into three parts.
  18557. * Major, Minor, Patchlevel
  18558. */
  18559. #define MBEDTLS_VERSION_MAJOR 2
  18560. #define MBEDTLS_VERSION_MINOR 2
  18561. #define MBEDTLS_VERSION_PATCH 1
  18562. /**
  18563. * The single version number has the following structure:
  18564. * MMNNPP00
  18565. * Major version | Minor version | Patch version
  18566. */
  18567. #define MBEDTLS_VERSION_NUMBER 0x02020100
  18568. #define MBEDTLS_VERSION_STRING "2.2.1"
  18569. #define MBEDTLS_VERSION_STRING_FULL "mbed TLS 2.2.1"
  18570. #if defined(MBEDTLS_VERSION_C)
  18571. #ifdef __cplusplus
  18572. extern "C" {
  18573. #endif
  18574. /**
  18575. * Get the version number.
  18576. *
  18577. * \return The constructed version number in the format
  18578. * MMNNPP00 (Major, Minor, Patch).
  18579. */
  18580. unsigned int mbedtls_version_get_number( void );
  18581. /**
  18582. * Get the version string ("x.y.z").
  18583. *
  18584. * \param string The string that will receive the value.
  18585. * (Should be at least 9 bytes in size)
  18586. */
  18587. void mbedtls_version_get_string( char *string );
  18588. /**
  18589. * Get the full version string ("mbed TLS x.y.z").
  18590. *
  18591. * \param string The string that will receive the value. The mbed TLS version
  18592. * string will use 18 bytes AT MOST including a terminating
  18593. * null byte.
  18594. * (So the buffer should be at least 18 bytes to receive this
  18595. * version string).
  18596. */
  18597. void mbedtls_version_get_string_full( char *string );
  18598. /**
  18599. * \brief Check if support for a feature was compiled into this
  18600. * mbed TLS binary. This allows you to see at runtime if the
  18601. * library was for instance compiled with or without
  18602. * Multi-threading support.
  18603. *
  18604. * \note only checks against defines in the sections "System
  18605. * support", "mbed TLS modules" and "mbed TLS feature
  18606. * support" in config.h
  18607. *
  18608. * \param feature The string for the define to check (e.g. "MBEDTLS_AES_C")
  18609. *
  18610. * \return 0 if the feature is present,
  18611. * -1 if the feature is not present and
  18612. * -2 if support for feature checking as a whole was not
  18613. * compiled in.
  18614. */
  18615. int mbedtls_version_check_feature( const char *feature );
  18616. #ifdef __cplusplus
  18617. }
  18618. #endif
  18619. #endif /* MBEDTLS_VERSION_C */
  18620. #endif /* version.h */
  18621. #endif /* ME_COM_MBEDTLS */