fsl_caam.c 292 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2021 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_caam.h"
  9. #include "fsl_clock.h"
  10. #if defined(FSL_FEATURE_HAS_L1CACHE) || defined(__DCACHE_PRESENT)
  11. #include "fsl_cache.h"
  12. #endif
  13. /*******************************************************************************
  14. * Definitions
  15. ******************************************************************************/
  16. /* Component ID definition, used by tools. */
  17. #ifndef FSL_COMPONENT_ID
  18. #define FSL_COMPONENT_ID "platform.drivers.caam"
  19. #endif
  20. /*! Compile time sizeof() check */
  21. #define BUILD_ASSURE(condition, msg) extern int msg[1 - 2 * (!(condition))] __attribute__((unused))
  22. /*! AESA XCBC-MAC or CMAC request CLASS 1 (default) or CLASS 2 CHA */
  23. #ifndef CAAM_AES_MAC_CLASS
  24. #define CAAM_AES_MAC_CLASS_1 0x02000000u
  25. #define CAAM_AES_MAC_CLASS_2 0x04000000u
  26. #define CAAM_AES_MAC_CLASS CAAM_AES_MAC_CLASS_1
  27. #endif
  28. /*! IRBAR and ORBAR job ring registers are 64-bit. these macros access least significant address 32-bit word. */
  29. #define IRBAR0 *(((volatile uint32_t *)(uint32_t) & (base->JOBRING[0].IRBAR_JR)) + 1)
  30. #define ORBAR0 *(((volatile uint32_t *)(uint32_t) & (base->JOBRING[0].ORBAR_JR)) + 1)
  31. #define IRBAR1 *(((volatile uint32_t *)(uint32_t) & (base->JOBRING[1].IRBAR_JR)) + 1)
  32. #define ORBAR1 *(((volatile uint32_t *)(uint32_t) & (base->JOBRING[1].ORBAR_JR)) + 1)
  33. #define IRBAR2 *(((volatile uint32_t *)(uint32_t) & (base->JOBRING[2].IRBAR_JR)) + 1)
  34. #define ORBAR2 *(((volatile uint32_t *)(uint32_t) & (base->JOBRING[2].ORBAR_JR)) + 1)
  35. #define IRBAR3 *(((volatile uint32_t *)(uint32_t) & (base->JOBRING[3].IRBAR_JR)) + 1)
  36. #define ORBAR3 *(((volatile uint32_t *)(uint32_t) & (base->JOBRING[3].ORBAR_JR)) + 1)
  37. /*! Job Descriptor defines */
  38. #define DESC_SIZE_MASK 0x0000003Fu
  39. #define DESC_KEY_SIZE_MASK 0x3FFu
  40. #define DESC_PAYLOAD_SIZE_MASK 0x0000FFFFu
  41. #define DESC_LC1_MASK 0x00020000u
  42. #define DESC_TAG_SIZE_MASK 0xFFu
  43. #define DESC_HALT 0xA0C00000u
  44. #define DESC_JUMP_2 0xA0000002u
  45. #define DESC_JUMP_4 0xA0000004u
  46. #define DESC_JUMP_6 0xA0000006u
  47. typedef enum _caam_algorithm
  48. {
  49. kCAAM_AlgorithmAES = 0x10u << 16,
  50. kCAAM_AlgorithmDES = 0x20u << 16,
  51. kCAAM_Algorithm3DES = 0x21u << 16,
  52. kCAAM_AlgorithmSHA1 = 0x41u << 16,
  53. kCAAM_AlgorithmSHA224 = 0x42u << 16,
  54. kCAAM_AlgorithmSHA256 = 0x43u << 16,
  55. kCAAM_AlgorithmSHA384 = 0x44u << 16,
  56. kCAAM_AlgorithmSHA512 = 0x45u << 16,
  57. } caam_algorithm_t;
  58. typedef enum _caam_aai_symmetric_alg
  59. {
  60. kCAAM_ModeCTR = 0x00U << 4,
  61. kCAAM_ModeCBC = 0x10U << 4,
  62. kCAAM_ModeECB = 0x20U << 4,
  63. kCAAM_ModeCFB = 0x30U << 4,
  64. kCAAM_ModeOFB = 0x40U << 4,
  65. kCAAM_ModeCMAC = 0x60U << 4,
  66. kCAAM_ModeXCBCMAC = 0x70U << 4,
  67. kCAAM_ModeCCM = 0x80U << 4,
  68. kCAAM_ModeGCM = 0x90U << 4,
  69. } caam_aai_symmetric_alg_t;
  70. typedef enum _caam_algorithm_state
  71. {
  72. kCAAM_AlgStateUpdate = 0u,
  73. kCAAM_AlgStateInit = 1u,
  74. kCAAM_AlgStateFinal = 2u,
  75. kCAAM_AlgStateInitFinal = 3u,
  76. } caam_algorithm_state_t;
  77. /*******************************************************************************
  78. * HASH Definitions
  79. ******************************************************************************/
  80. enum _caam_sha_digest_len
  81. {
  82. kCAAM_RunLenSha1 = 28u,
  83. kCAAM_OutLenSha1 = 20u,
  84. kCAAM_RunLenSha224 = 40u,
  85. kCAAM_OutLenSha224 = 28u,
  86. kCAAM_RunLenSha256 = 40u,
  87. kCAAM_OutLenSha256 = 32u,
  88. kCAAM_RunLenSha384 = 64u,
  89. kCAAM_OutLenSha384 = 48u,
  90. kCAAM_RunLenSha512 = 64u,
  91. kCAAM_OutLenSha512 = 64u,
  92. };
  93. /*! Internal states of the HASH creation process */
  94. typedef enum _caam_hash_algo_state
  95. {
  96. kCAAM_HashInit = 1u, /*!< Key in the HASH context is the input key. */
  97. kCAAM_HashUpdate, /*!< HASH context has algorithm specific context: MAC, K2 and K3 (XCBC-MAC), MAC and L (CMAC),
  98. running digest (MDHA). Key in the HASH context is the derived key. */
  99. } caam_hash_algo_state_t;
  100. /*! 64-byte block represented as byte array or 16 32-bit words */
  101. typedef union _caam_hash_block
  102. {
  103. uint32_t w[CAAM_HASH_BLOCK_SIZE / 4]; /*!< array of 32-bit words */
  104. uint8_t b[CAAM_HASH_BLOCK_SIZE]; /*!< byte array */
  105. } caam_hash_block_t;
  106. /*! Definitions of indexes into hash context array */
  107. typedef enum _caam_hash_ctx_indexes
  108. {
  109. kCAAM_HashCtxKeyStartIdx = 12, /*!< context word array index where key is stored */
  110. kCAAM_HashCtxKeySize = 20, /*!< context word array index where key size is stored */
  111. kCAAM_HashCtxNumWords = 21, /*!< number of context array 32-bit words */
  112. } caam_hash_ctx_indexes;
  113. typedef struct _caam_hash_ctx_internal
  114. {
  115. caam_hash_block_t blk; /*!< memory buffer. only full 64-byte blocks are written to CAAM during hash updates */
  116. uint32_t word[kCAAM_HashCtxNumWords]; /*!< CAAM module context that needs to be saved/restored between CAAM jobs */
  117. uint32_t blksz; /*!< number of valid bytes in memory buffer */
  118. CAAM_Type *base; /*!< CAAM peripheral base address */
  119. caam_handle_t *handle; /*!< CAAM handle (specifies jobRing and optional callback function) */
  120. caam_hash_algo_t algo; /*!< selected algorithm from the set of supported algorithms in caam_hash_algo_t */
  121. caam_hash_algo_state_t state; /*!< finite machine state of the hash software process */
  122. } caam_hash_ctx_internal_t;
  123. /*! Definitions of indexes into hash job descriptor */
  124. enum _caam_hash_sgt_index
  125. {
  126. kCAAM_HashDescriptorSgtIdx = 14u, /*!< Index of the hash job descriptor[] where the two entry SGT starts. */
  127. };
  128. /*! One entry in the SGT */
  129. typedef struct _caam_sgt_entry
  130. {
  131. /* 64-bit address. */
  132. uint32_t address_h;
  133. uint32_t address_l;
  134. uint32_t length;
  135. uint32_t offset;
  136. } caam_sgt_entry_t;
  137. /*! Definitions SGT entry type */
  138. typedef enum _caam_hash_sgt_entry_type
  139. {
  140. kCAAM_HashSgtEntryNotLast = 0u, /*!< Do not set the Final Bit in SGT entries */
  141. kCAAM_HashSgtEntryLast = 1u, /*!< Sets Final Bit in the last SGT entry */
  142. } caam_hash_sgt_entry_type_t;
  143. /*! Two entry SGT, embedded in the hash job descriptor */
  144. typedef caam_sgt_entry_t caam_hash_internal_sgt_t[2];
  145. /*! Definitions of SGT type */
  146. typedef enum _caam_hash_sgt_type
  147. {
  148. kCAAM_HashSgtInternal = 0u, /*!< Two entry SGT is copied into the hash job descriptor. */
  149. kCAAM_HashSgtExternal = 1u, /*!< Use external SGT. */
  150. } caam_hash_sgt_type_t;
  151. enum _caam_hash_non_blocking_sgt_entries
  152. {
  153. kCAAM_HashSgtMaxCtxEntries =
  154. (sizeof(caam_hash_block_t) + sizeof(uint32_t) * kCAAM_HashCtxKeyStartIdx) / sizeof(caam_sgt_entry_t),
  155. };
  156. /*******************************************************************************
  157. * Variables
  158. ******************************************************************************/
  159. static caam_job_ring_interface_t *s_jr0 = NULL; /*!< Pointer to job ring interface 0. */
  160. static uint32_t s_jrIndex0 = 0; /*!< Current index in the input job ring 0. */
  161. static caam_job_ring_interface_t *s_jr1 = NULL; /*!< Pointer to job ring interface 1. */
  162. static uint32_t s_jrIndex1 = 0; /*!< Current index in the input job ring 1. */
  163. static caam_job_ring_interface_t *s_jr2 = NULL; /*!< Pointer to job ring interface 2. */
  164. static uint32_t s_jrIndex2 = 0; /*!< Current index in the input job ring 2. */
  165. static caam_job_ring_interface_t *s_jr3 = NULL; /*!< Pointer to job ring interface 3. */
  166. static uint32_t s_jrIndex3 = 0; /*!< Current index in the input job ring 3. */
  167. /*******************************************************************************
  168. * Code
  169. ******************************************************************************/
  170. /*******************************************************************************
  171. * CAAM Common code static
  172. ******************************************************************************/
  173. /* Macros and functions computing data offset for descriptors */
  174. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET)
  175. #include "fsl_memory.h"
  176. #ifndef FSL_MEM_M4_TCM_OFFSET
  177. #define CAAM_OFFSET 0U
  178. #else
  179. #define CAAM_OFFSET FSL_MEM_M4_TCM_OFFSET
  180. #endif
  181. uint32_t ADD_OFFSET(uint32_t addr)
  182. {
  183. if (addr > FSL_MEM_M4_TCM_END)
  184. {
  185. return addr;
  186. }
  187. else if (addr < FSL_MEM_M4_TCM_BEGIN)
  188. {
  189. return addr;
  190. }
  191. return addr + CAAM_OFFSET;
  192. }
  193. uint32_t ADD_OFFSET_SIZE(uint32_t addr, uint32_t size)
  194. {
  195. if ((addr + size) > FSL_MEM_M4_TCM_END)
  196. {
  197. return addr;
  198. }
  199. else if (addr < FSL_MEM_M4_TCM_BEGIN)
  200. {
  201. return addr;
  202. }
  203. return addr + CAAM_OFFSET;
  204. }
  205. #else /* !defined(FLS_FEATURE_CAAM_OFFSET) */
  206. uint32_t ADD_OFFSET(uint32_t addr);
  207. uint32_t ADD_OFFSET(uint32_t addr)
  208. {
  209. return addr;
  210. }
  211. uint32_t ADD_OFFSET_SIZE(uint32_t addr);
  212. uint32_t ADD_OFFSET_SIZE(uint32_t addr)
  213. {
  214. return addr;
  215. }
  216. #endif /* FLS_FEATURE_CAAM_OFFSET */
  217. #if 0
  218. /* for build without string.h memcpy() */
  219. static void caam_memcpy(void *dst, const void *src, size_t size)
  220. {
  221. register uint8_t *to = (uint8_t *)(uintptr_t)dst;
  222. register const uint8_t *from = (const uint8_t *)(uintptr_t)src;
  223. /* if it is possible to move data with 32-bit aligned access, do it so */
  224. if ((size >= sizeof(uint32_t)) && (0u == ((uintptr_t)dst & 0x3u)) && (0u == ((uintptr_t)src & 0x3u)))
  225. {
  226. register uint32_t *to32 = (uint32_t *)(uintptr_t)dst;
  227. register const uint32_t *from32 = (const uint32_t *)(uintptr_t)src;
  228. while (size >= sizeof(uint32_t))
  229. {
  230. *to32 = *from32;
  231. size -= sizeof(uint32_t);
  232. to32++;
  233. from32++;
  234. }
  235. to = (uint8_t *)(uintptr_t)to32;
  236. from = (const uint8_t *)(uintptr_t)from32;
  237. }
  238. while (size)
  239. {
  240. *to = *from;
  241. size--;
  242. to++;
  243. from++;
  244. }
  245. }
  246. #else
  247. #include <string.h>
  248. #define caam_memcpy memcpy
  249. #endif
  250. static void caam_job_ring_set_base_address_and_size(CAAM_Type *base,
  251. caam_job_ring_t jr,
  252. uint32_t *inputRingBase,
  253. size_t inputRingSize,
  254. uint32_t *outputRingBase,
  255. uint32_t outputRingSize)
  256. {
  257. if (kCAAM_JobRing0 == jr)
  258. {
  259. IRBAR0 = ADD_OFFSET((uint32_t)inputRingBase);
  260. base->JOBRING[0].IRSR_JR = inputRingSize;
  261. ORBAR0 = ADD_OFFSET((uint32_t)outputRingBase);
  262. base->JOBRING[0].ORSR_JR = outputRingSize;
  263. }
  264. if (kCAAM_JobRing1 == jr)
  265. {
  266. IRBAR1 = (uintptr_t)ADD_OFFSET((uint32_t)inputRingBase);
  267. base->JOBRING[1].IRSR_JR = inputRingSize;
  268. ORBAR1 = (uintptr_t)ADD_OFFSET((uint32_t)outputRingBase);
  269. base->JOBRING[1].ORSR_JR = outputRingSize;
  270. }
  271. if (kCAAM_JobRing2 == jr)
  272. {
  273. IRBAR2 = (uintptr_t)ADD_OFFSET((uint32_t)inputRingBase);
  274. base->JOBRING[2].IRSR_JR = inputRingSize;
  275. ORBAR2 = (uintptr_t)ADD_OFFSET((uint32_t)outputRingBase);
  276. base->JOBRING[2].ORSR_JR = outputRingSize;
  277. }
  278. if (kCAAM_JobRing3 == jr)
  279. {
  280. IRBAR3 = (uintptr_t)ADD_OFFSET((uint32_t)inputRingBase);
  281. base->JOBRING[3].IRSR_JR = inputRingSize;
  282. ORBAR3 = (uintptr_t)ADD_OFFSET((uint32_t)outputRingBase);
  283. base->JOBRING[3].ORSR_JR = outputRingSize;
  284. }
  285. }
  286. static inline void caam_input_ring_set_jobs_added(CAAM_Type *base, caam_job_ring_t jr, uint32_t numJobsAdded)
  287. {
  288. /* Data and Instruction Synchronization Barrier to make sure */
  289. /* that the descriptor will be loaded into CAAM in time*/
  290. __ISB();
  291. __DSB();
  292. if (kCAAM_JobRing0 == jr)
  293. {
  294. base->JOBRING[0].IRJAR_JR = numJobsAdded;
  295. }
  296. if (kCAAM_JobRing1 == jr)
  297. {
  298. base->JOBRING[1].IRJAR_JR = numJobsAdded;
  299. }
  300. if (kCAAM_JobRing2 == jr)
  301. {
  302. base->JOBRING[2].IRJAR_JR = numJobsAdded;
  303. }
  304. if (kCAAM_JobRing3 == jr)
  305. {
  306. base->JOBRING[3].IRJAR_JR = numJobsAdded;
  307. }
  308. }
  309. static inline void caam_output_ring_set_jobs_removed(CAAM_Type *base, caam_job_ring_t jr, uint32_t numJobsRemoved)
  310. {
  311. if (kCAAM_JobRing0 == jr)
  312. {
  313. base->JOBRING[0].ORJRR_JR = numJobsRemoved;
  314. }
  315. if (kCAAM_JobRing1 == jr)
  316. {
  317. base->JOBRING[1].ORJRR_JR = numJobsRemoved;
  318. }
  319. if (kCAAM_JobRing2 == jr)
  320. {
  321. base->JOBRING[2].ORJRR_JR = numJobsRemoved;
  322. }
  323. if (kCAAM_JobRing3 == jr)
  324. {
  325. base->JOBRING[3].ORJRR_JR = numJobsRemoved;
  326. }
  327. }
  328. static uint32_t caam_output_ring_get_slots_full(CAAM_Type *base, caam_job_ring_t jr)
  329. {
  330. uint32_t retVal = 0;
  331. if (kCAAM_JobRing0 == jr)
  332. {
  333. retVal = base->JOBRING[0].ORSFR_JR;
  334. }
  335. if (kCAAM_JobRing1 == jr)
  336. {
  337. retVal = base->JOBRING[1].ORSFR_JR;
  338. }
  339. if (kCAAM_JobRing2 == jr)
  340. {
  341. retVal = base->JOBRING[2].ORSFR_JR;
  342. }
  343. if (kCAAM_JobRing3 == jr)
  344. {
  345. retVal = base->JOBRING[3].ORSFR_JR;
  346. }
  347. return retVal;
  348. }
  349. /*!
  350. * @brief Tests the correct key size.
  351. *
  352. * This function tests the correct key size.
  353. * @param keySize Input key length in bytes.
  354. * @return True if the key length is supported, false if not.
  355. */
  356. bool caam_check_key_size(const uint32_t keySize);
  357. bool caam_check_key_size(const uint32_t keySize)
  358. {
  359. return ((keySize == 16u) || ((keySize == 24u)) || ((keySize == 32u)));
  360. }
  361. static status_t caam_in_job_ring_add(CAAM_Type *base, caam_job_ring_t jobRing, uint32_t *descaddr)
  362. {
  363. /* adding new job to the s_inJobRing[] must be atomic
  364. * as this is global variable
  365. */
  366. uint32_t currPriMask = DisableGlobalIRQ();
  367. #if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
  368. bool DCacheEnableFlag = false;
  369. /* Disable D cache. */
  370. if (SCB_CCR_DC_Msk == (SCB_CCR_DC_Msk & SCB->CCR))
  371. {
  372. SCB_DisableDCache();
  373. DCacheEnableFlag = true;
  374. }
  375. #endif /* __DCACHE_PRESENT */
  376. #if defined(FSL_FEATURE_LMEM_HAS_SYSTEMBUS_CACHE) && (FSL_FEATURE_LMEM_HAS_SYSTEMBUS_CACHE > 0U)
  377. #if defined(FSL_FEATURE_HAS_L1CACHE) && (FSL_FEATURE_HAS_L1CACHE > 0U)
  378. L1CACHE_DisableSystemCache();
  379. #endif /* FSL_FEATURE_LMEM_HAS_SYSTEMBUS_CACHE */
  380. #endif /* FSL_FEATURE_HAS_L1CACHE */
  381. if (kCAAM_JobRing0 == jobRing)
  382. {
  383. s_jr0->inputJobRing[s_jrIndex0] = (ADD_OFFSET((uint32_t)descaddr));
  384. s_jrIndex0++;
  385. if (s_jrIndex0 >= ARRAY_SIZE(s_jr0->inputJobRing))
  386. {
  387. s_jrIndex0 = 0;
  388. }
  389. }
  390. else if (kCAAM_JobRing1 == jobRing)
  391. {
  392. s_jr1->inputJobRing[s_jrIndex1] = ADD_OFFSET((uint32_t)descaddr);
  393. s_jrIndex1++;
  394. if (s_jrIndex1 >= ARRAY_SIZE(s_jr1->inputJobRing))
  395. {
  396. s_jrIndex1 = 0;
  397. }
  398. }
  399. else if (kCAAM_JobRing2 == jobRing)
  400. {
  401. s_jr2->inputJobRing[s_jrIndex2] = ADD_OFFSET((uint32_t)descaddr);
  402. s_jrIndex2++;
  403. if (s_jrIndex2 >= ARRAY_SIZE(s_jr2->inputJobRing))
  404. {
  405. s_jrIndex2 = 0;
  406. }
  407. }
  408. else if (kCAAM_JobRing3 == jobRing)
  409. {
  410. s_jr3->inputJobRing[s_jrIndex3] = ADD_OFFSET((uint32_t)descaddr);
  411. s_jrIndex3++;
  412. if (s_jrIndex3 >= ARRAY_SIZE(s_jr3->inputJobRing))
  413. {
  414. s_jrIndex3 = 0;
  415. }
  416. }
  417. else
  418. {
  419. EnableGlobalIRQ(currPriMask);
  420. return kStatus_InvalidArgument;
  421. }
  422. caam_input_ring_set_jobs_added(base, jobRing, 1);
  423. #if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
  424. if (DCacheEnableFlag)
  425. {
  426. /* Enable D cache. */
  427. SCB_EnableDCache();
  428. }
  429. #endif /* __DCACHE_PRESENT */
  430. #if defined(FSL_FEATURE_LMEM_HAS_SYSTEMBUS_CACHE) && (FSL_FEATURE_LMEM_HAS_SYSTEMBUS_CACHE > 0U)
  431. #if defined(FSL_FEATURE_HAS_L1CACHE) && (FSL_FEATURE_HAS_L1CACHE > 0U)
  432. L1CACHE_EnableSystemCache();
  433. #endif /* FSL_FEATURE_LMEM_HAS_SYSTEMBUS_CACHE */
  434. #endif /* FSL_FEATURE_HAS_L1CACHE */
  435. /* Enable IRQ */
  436. EnableGlobalIRQ(currPriMask);
  437. return kStatus_Success;
  438. }
  439. /* this function shall be only called inside CAAM driver critical section
  440. * because it accesses global variables.
  441. */
  442. static status_t caam_out_job_ring_remove(CAAM_Type *base, caam_job_ring_t jobRing, int outIndex)
  443. {
  444. if (kCAAM_JobRing0 == jobRing)
  445. {
  446. s_jr0->outputJobRing[outIndex++] = 0; /* clear descriptor address */
  447. s_jr0->outputJobRing[outIndex] = 0; /* clear status */
  448. }
  449. else if (kCAAM_JobRing1 == jobRing)
  450. {
  451. s_jr1->outputJobRing[outIndex++] = 0; /* clear descriptor address */
  452. s_jr1->outputJobRing[outIndex] = 0; /* clear status */
  453. }
  454. else if (kCAAM_JobRing2 == jobRing)
  455. {
  456. s_jr2->outputJobRing[outIndex++] = 0; /* clear descriptor address */
  457. s_jr2->outputJobRing[outIndex] = 0; /* clear status */
  458. }
  459. else if (kCAAM_JobRing3 == jobRing)
  460. {
  461. s_jr3->outputJobRing[outIndex++] = 0; /* clear descriptor address */
  462. s_jr3->outputJobRing[outIndex] = 0; /* clear status */
  463. }
  464. else
  465. {
  466. /* Intentional empty */
  467. }
  468. caam_output_ring_set_jobs_removed(base, jobRing, 1);
  469. return 0;
  470. }
  471. static status_t caam_out_job_ring_test_and_remove(
  472. CAAM_Type *base, caam_job_ring_t jobRing, uint32_t *descriptor, bool *wait, bool *found)
  473. {
  474. uint32_t currPriMask = DisableGlobalIRQ();
  475. uint32_t i;
  476. status_t status;
  477. *found = false;
  478. *wait = true;
  479. status = kStatus_Success;
  480. uint32_t *jr;
  481. uint32_t jrEntries;
  482. if (jobRing == kCAAM_JobRing0)
  483. {
  484. jr = s_jr0->outputJobRing;
  485. jrEntries = ARRAY_SIZE(s_jr0->outputJobRing);
  486. }
  487. else if (jobRing == kCAAM_JobRing1)
  488. {
  489. jr = s_jr1->outputJobRing;
  490. jrEntries = ARRAY_SIZE(s_jr1->outputJobRing);
  491. }
  492. else if (jobRing == kCAAM_JobRing2)
  493. {
  494. jr = s_jr2->outputJobRing;
  495. jrEntries = ARRAY_SIZE(s_jr2->outputJobRing);
  496. }
  497. else if (jobRing == kCAAM_JobRing3)
  498. {
  499. jr = s_jr3->outputJobRing;
  500. jrEntries = ARRAY_SIZE(s_jr3->outputJobRing);
  501. }
  502. else
  503. {
  504. return kStatus_InvalidArgument;
  505. }
  506. /* check if an interrupt or other thread consumed the result that we just saw */
  507. if ((caam_output_ring_get_slots_full(base, jobRing)) != 0U)
  508. {
  509. /* check if our descriptor is in the output job ring
  510. * look from the beginning of the out job ring
  511. */
  512. i = 0;
  513. while ((!*found) && (i < jrEntries))
  514. {
  515. if (jr[i] == (uint32_t)descriptor)
  516. {
  517. *found = true;
  518. *wait = false;
  519. /* check for error in status word */
  520. if ((jr[i + 1U]) != 0U)
  521. {
  522. /* printf("Error 0x%lx\r\n", jr[i + 1]); */
  523. /* for JMP/HALT commands with User specified status, return the user status, just to allow the
  524. * software to test for user status termination status words */
  525. /* This is used by PKHA PrimalityTest to report a candidate is believed not being prime */
  526. if (0x30000000u == (jr[i + 1U] & 0xff000000u))
  527. {
  528. status = (int32_t)jr[i + 1U];
  529. }
  530. else
  531. {
  532. status = kStatus_Fail;
  533. }
  534. }
  535. (void)caam_out_job_ring_remove(base, jobRing, (int)i);
  536. }
  537. else
  538. {
  539. /* try next result */
  540. i += 2u;
  541. }
  542. }
  543. }
  544. EnableGlobalIRQ(currPriMask);
  545. return status;
  546. }
  547. typedef union _caam_xcm_block_t
  548. {
  549. uint32_t w[4]; /*!< CAAM context register is 16 bytes written as four 32-bit words */
  550. uint8_t b[16]; /*!< 16 octets block for CCM B0 and CTR0 and for GCM */
  551. } caam_xcm_block_t;
  552. static uint32_t swap_bytes(uint32_t in)
  553. {
  554. return (((in & 0x000000ffu) << 24) | ((in & 0x0000ff00u) << 8) | ((in & 0x00ff0000u) >> 8) |
  555. ((in & 0xff000000u) >> 24));
  556. }
  557. /*!
  558. * @brief AES GCM check validity of input arguments.
  559. *
  560. * This function checks the validity of input arguments.
  561. *
  562. * @param base LTC peripheral base address.
  563. * @param src Source address (plaintext or ciphertext).
  564. * @param iv Initialization vector address.
  565. * @param aad Additional authenticated data address.
  566. * @param dst Destination address (plaintext or ciphertext).
  567. * @param inputSize Size of input (same size as output) data in bytes.
  568. * @param ivSize Size of Initialization vector in bytes.
  569. * @param aadSize Size of additional data in bytes.
  570. * @param tagSize Size of the GCM tag in bytes.
  571. */
  572. static status_t caam_aes_gcm_check_input_args(CAAM_Type *base,
  573. const uint8_t *src,
  574. const uint8_t *iv,
  575. const uint8_t *aad,
  576. uint8_t *dst,
  577. size_t inputSize,
  578. size_t ivSize,
  579. size_t aadSize,
  580. size_t tagSize)
  581. {
  582. if (base == NULL)
  583. {
  584. return kStatus_InvalidArgument;
  585. }
  586. /* tag can be NULL to skip tag processing */
  587. if (((ivSize != 0U) && (iv == NULL)) || ((aadSize != 0U) && (aad == NULL)) ||
  588. ((inputSize != 0U) && ((src == NULL) || (dst == NULL))))
  589. {
  590. return kStatus_InvalidArgument;
  591. }
  592. /* octet length of tag (tagSize) must be element of 4,8,12,13,14,15,16 */
  593. if (((tagSize > 16u) || (tagSize < 12u)) && (tagSize != 4u) && (tagSize != 8u))
  594. {
  595. return kStatus_InvalidArgument;
  596. }
  597. /* no IV AAD DATA makes no sense */
  598. if (0U == (inputSize + ivSize + aadSize))
  599. {
  600. return kStatus_InvalidArgument;
  601. }
  602. /* check length of input strings. This is more strict than the GCM specificaiton due to 32-bit architecture.
  603. * The API interface would work on 64-bit architecture as well, but as it has not been tested, let it limit to
  604. * 32-bits.
  605. */
  606. if (!((ivSize >= 1u) && (sizeof(size_t) <= 4u)))
  607. {
  608. return kStatus_InvalidArgument;
  609. }
  610. return kStatus_Success;
  611. }
  612. static const uint32_t templateAesGcm[] = {
  613. /* 00 */ 0xB0800000u, /* HEADER */
  614. /* 01 */ 0x02000000u, /* KEY */
  615. /* 02 */ 0x00000000u, /* place: key address */
  616. /* 03 */ 0x82100908u, /* OPERATION: AES GCM Decrypt Finalize */
  617. /* 04 */ 0x12830004u, /* LOAD Class 1 ICV Size Register by IMM data */
  618. /* 05 */ 0x00000000u, /* place: received ICV size */
  619. /* 06 */ 0x22210000u, /* FIFO LOAD IV (flush) */
  620. /* 07 */ 0x00000000u, /* place: IV address */
  621. /* 08 */ 0x22310000u, /* FIFO LOAD AAD (flush) */
  622. /* 09 */ 0x00000000u, /* place: AAD address */
  623. /* 10 */ 0x22530000u, /* FIFO LOAD message */
  624. /* 11 */ 0x00000000u, /* place: message address */
  625. /* 12 */ 0x00000000u, /* place: message size */
  626. /* 13 */ 0x60700000u, /* FIFO STORE Message */
  627. /* 14 */ 0x00000000u, /* place: destination address */
  628. /* 15 */ 0x00000000u, /* place: destination size */
  629. /* 16 */ 0xA3001201u, /* JMP always to next command. Load/store checkpoint. Class 1 done checkpoint. */
  630. /* For encryption, write the computed and encrypted MAC to user buffer */
  631. /* For decryption, compare the computed tag with the received tag, CICV-only job. */
  632. /* 17 */ 0x10880004u, /* LOAD Immediate to Clear Written Register. */
  633. /* 18 */ 0x08000004u, /* value for Clear Written Register: C1D and C1DS bits are set */
  634. /* 19 */ 0x12820004u, /* LOAD Immediate to C1DS Register. */
  635. /* 20 */ 0x00000000u, /* zero data size */
  636. /* 21 */ 0x12830004u, /* LOAD Class 1 ICV Size Register by IMM data */
  637. /* 22 */ 0x00000000u, /* place: received ICV size */
  638. /* 23 */ 0x82100902u, /* OPERATION: AES GCM Decrypt Update ICV_TEST */
  639. /* 24 */ 0x223B0000u, /* FIFO LOAD ICV */
  640. /* 25 */ 0x00000000u, /* place: received ICV address */
  641. };
  642. status_t caam_aes_gcm_non_blocking(CAAM_Type *base,
  643. caam_handle_t *handle,
  644. caam_desc_aes_gcm_t descriptor,
  645. const uint8_t *input,
  646. uint8_t *output,
  647. size_t size,
  648. const uint8_t *iv,
  649. size_t ivSize,
  650. const uint8_t *aad,
  651. size_t aadSize,
  652. const uint8_t *key,
  653. size_t keySize,
  654. uint32_t tag,
  655. size_t tagSize,
  656. int encrypt);
  657. status_t caam_aes_gcm_non_blocking(CAAM_Type *base,
  658. caam_handle_t *handle,
  659. caam_desc_aes_gcm_t descriptor,
  660. const uint8_t *input,
  661. uint8_t *output,
  662. size_t size,
  663. const uint8_t *iv,
  664. size_t ivSize,
  665. const uint8_t *aad,
  666. size_t aadSize,
  667. const uint8_t *key,
  668. size_t keySize,
  669. uint32_t tag,
  670. size_t tagSize,
  671. int encrypt)
  672. {
  673. BUILD_ASSURE(sizeof(templateAesGcm) <= sizeof(caam_desc_aes_gcm_t), caam_desc_aes_gcm_t_size);
  674. status_t status;
  675. status = caam_aes_gcm_check_input_args(base, input, iv, aad, output, size, ivSize, aadSize, tagSize);
  676. if (status != kStatus_Success)
  677. {
  678. return status;
  679. }
  680. /* get template descriptor and it's size */
  681. uint32_t descriptorSize = ARRAY_SIZE(templateAesGcm);
  682. (void)caam_memcpy(descriptor, templateAesGcm, sizeof(templateAesGcm));
  683. /* add descriptor size */
  684. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  685. /* key address and key size */
  686. descriptor[1] |= (keySize & DESC_KEY_SIZE_MASK);
  687. descriptor[2] = ADD_OFFSET((uint32_t)key);
  688. /* Encrypt decrypt */
  689. if (0 != encrypt)
  690. {
  691. descriptor[3] |= 1u; /* ENC */
  692. }
  693. /* ICV Size */
  694. descriptor[5] = tagSize;
  695. bool ivLast = (aadSize == 0U) && (size == 0U);
  696. bool aadLast = (size == 0U);
  697. /* IV address and size */
  698. descriptor[6] |= (ivSize & DESC_PAYLOAD_SIZE_MASK);
  699. descriptor[7] = ADD_OFFSET((uint32_t)iv);
  700. if (ivLast)
  701. {
  702. descriptor[6] |= DESC_LC1_MASK; /* LC1 */
  703. }
  704. /* AAD address and size */
  705. descriptor[8] |= (aadSize & DESC_PAYLOAD_SIZE_MASK);
  706. descriptor[9] = ADD_OFFSET((uint32_t)aad);
  707. if ((!ivLast) && aadLast)
  708. {
  709. descriptor[8] |= DESC_LC1_MASK; /* LC1 */
  710. }
  711. /* Message source address and size */
  712. descriptor[11] = ADD_OFFSET((uint32_t)input);
  713. descriptor[12] = size;
  714. /* Message destination address and size */
  715. descriptor[14] = ADD_OFFSET((uint32_t)output);
  716. descriptor[15] = size;
  717. if (tag != 0U)
  718. {
  719. if (encrypt == 0)
  720. {
  721. descriptor[22] = tagSize;
  722. descriptor[24] |= (tagSize & DESC_TAG_SIZE_MASK);
  723. descriptor[25] = ADD_OFFSET((uint32_t)tag);
  724. }
  725. else
  726. {
  727. /* For encryption change the command to FIFO STORE, as tag data needs to be put into tag output */
  728. descriptor[16] = 0x52200000u | (tagSize & DESC_TAG_SIZE_MASK); /* STORE from Class 1 context to tag */
  729. descriptor[17] = ADD_OFFSET((uint32_t)tag); /* place: tag address */
  730. ;
  731. descriptor[18] = DESC_HALT; /* always halt with status 0x0 (normal) */
  732. }
  733. }
  734. else
  735. {
  736. /* tag is NULL, skip tag processing */
  737. descriptor[16] = DESC_HALT; /* always halt with status 0x0 (normal) */
  738. }
  739. /* add operation specified by descriptor to CAAM Job Ring */
  740. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  741. }
  742. static status_t caam_aes_ccm_check_input_args(CAAM_Type *base,
  743. const uint8_t *src,
  744. const uint8_t *iv,
  745. const uint8_t *key,
  746. uint8_t *dst,
  747. size_t ivSize,
  748. size_t aadSize,
  749. size_t keySize,
  750. size_t tagSize)
  751. {
  752. if (base == NULL)
  753. {
  754. return kStatus_InvalidArgument;
  755. }
  756. /* tag can be NULL to skip tag processing */
  757. if ((src == NULL) || (iv == NULL) || (key == NULL) || (dst == NULL))
  758. {
  759. return kStatus_InvalidArgument;
  760. }
  761. /* size of Nonce (ivSize) must be element of 7,8,9,10,11,12,13 */
  762. if ((ivSize < 7u) || (ivSize > 13u))
  763. {
  764. return kStatus_InvalidArgument;
  765. }
  766. /* octet length of MAC (tagSize) must be element of 4,6,8,10,12,14,16 for tag processing or zero to skip tag
  767. * processing */
  768. if (((tagSize > 0U) && (tagSize < 4u)) || (tagSize > 16u) || ((tagSize & 1u) != 0U))
  769. {
  770. return kStatus_InvalidArgument;
  771. }
  772. /* check if keySize is supported */
  773. if (!caam_check_key_size(keySize))
  774. {
  775. return kStatus_InvalidArgument;
  776. }
  777. /* AESA does not support more AAD than this */
  778. if (aadSize >= 65280u)
  779. {
  780. return kStatus_InvalidArgument;
  781. }
  782. return kStatus_Success;
  783. }
  784. static void caam_aes_ccm_context_init(
  785. uint32_t inputSize, const uint8_t *iv, uint32_t ivSize, uint32_t aadSize, uint32_t tagSize, void *b0, void *ctr0)
  786. {
  787. caam_xcm_block_t blk;
  788. caam_xcm_block_t blkZero = {{0x0u, 0x0u, 0x0u, 0x0u}};
  789. uint8_t q; /* octet length of binary representation of the octet length of the payload. computed as (15 - n), where
  790. n is length of nonce(=ivSize) */
  791. uint8_t flags; /* flags field in B0 and CTR0 */
  792. /* compute B0 */
  793. (void)caam_memcpy(&blk, &blkZero, sizeof(blk));
  794. /* tagSize - size of output MAC */
  795. q = 15U - (uint8_t)ivSize;
  796. flags = (uint8_t)(8U * ((tagSize - 2U) / 2U) + q - 1U); /* 8*M' + L' */
  797. if (aadSize != 0U)
  798. {
  799. flags |= 0x40U; /* Adata */
  800. }
  801. blk.b[0] = flags; /* flags field */
  802. blk.w[3] = swap_bytes(inputSize); /* message size, most significant byte first */
  803. (void)caam_memcpy(&blk.b[1], iv, ivSize); /* nonce field */
  804. /* Write B0 data to the context register.
  805. */
  806. (void)caam_memcpy(b0, (void *)&blk.b[0], 16);
  807. /* Write CTR0 to the context register.
  808. */
  809. (void)caam_memcpy(&blk, &blkZero, sizeof(blk)); /* ctr(0) field = zero */
  810. blk.b[0] = q - 1U; /* flags field */
  811. (void)caam_memcpy(&blk.b[1], iv, ivSize); /* nonce field */
  812. (void)caam_memcpy(ctr0, (void *)&blk.b[0], 16);
  813. }
  814. static const uint32_t templateAesCcm[] = {
  815. /* 00 */ 0xB0800000u, /* HEADER */
  816. /* 01 */ 0x02000000u, /* KEY */
  817. /* 02 */ 0x00000000u, /* place: key address */
  818. /* 03 */ 0x12A00010u, /* LOAD 16 immediate bytes of B0 to Class 1 Context Register. Offset 0 bytes. */
  819. /* 04 */ 0x00000000u, /* place: B0[0-3] */
  820. /* 05 */ 0x00000000u, /* place: B0[4-7] */
  821. /* 06 */ 0x00000000u, /* place: B0[8-11] */
  822. /* 07 */ 0x00000000u, /* place: B0[12-15] */
  823. /* 08 */ 0x12A01010u, /* LOAD 16 immediate bytes of CTR0 to Class 1 Context Register. Offset 16 bytes. */
  824. /* 09 */ 0x00000000u, /* place: CTR0[0-3] */
  825. /* 10 */ 0x00000000u, /* place: CTR0[4-7] */
  826. /* 11 */ 0x00000000u, /* place: CTR0[8-11] */
  827. /* 12 */ 0x00000000u, /* place: CTR0[12-15] */
  828. /* 13 */ 0x8210080Cu, /* OPERATION: AES CCM Decrypt Initialize/Finalize */
  829. /* 14 */ 0x22B00004u, /* FIFO LOAD additional authentication data. Immediate 32-bit word with aadSize encoded */
  830. /* 15 */ 0x00000000u, /* place: encoded aadSize followed by first byte(s) of authentication data */
  831. /* 16 */ 0x22310000u, /* FIFO LOAD additional authentication data. Flush as this is last data of AAD type. */
  832. /* 17 */ 0x00000000u, /* place: AAD address */
  833. /* 18 */ 0x22530000u, /* FIFO LOAD message */
  834. /* 19 */ 0x00000000u, /* place: message address */
  835. /* 20 */ 0x00000000u, /* place: message size */
  836. /* 21 */ 0x60700000u, /* FIFO STORE Message */
  837. /* 22 */ 0x00000000u, /* place: destination address */
  838. /* 23 */ 0x00000000u, /* place: destination size */
  839. /* For encryption, write the computed and encrypted MAC to user buffer */
  840. /* 24 */ 0x52202000u, /* STORE from Class 1 context to tag */
  841. /* 25 */ 0x00000000u, /* place: tag address */
  842. /* For decryption, compare the computed tag with the received tag */
  843. };
  844. status_t caam_aes_ccm_non_blocking(CAAM_Type *base,
  845. caam_handle_t *handle,
  846. caam_desc_aes_ccm_t descriptor,
  847. const uint8_t *input,
  848. uint8_t *output,
  849. size_t size,
  850. const uint8_t *iv,
  851. size_t ivSize,
  852. const uint8_t *aad,
  853. size_t aadSize,
  854. const uint8_t *key,
  855. size_t keySize,
  856. uint32_t tag,
  857. size_t tagSize,
  858. int encrypt);
  859. status_t caam_aes_ccm_non_blocking(CAAM_Type *base,
  860. caam_handle_t *handle,
  861. caam_desc_aes_ccm_t descriptor,
  862. const uint8_t *input,
  863. uint8_t *output,
  864. size_t size,
  865. const uint8_t *iv,
  866. size_t ivSize,
  867. const uint8_t *aad,
  868. size_t aadSize,
  869. const uint8_t *key,
  870. size_t keySize,
  871. uint32_t tag,
  872. size_t tagSize,
  873. int encrypt)
  874. {
  875. BUILD_ASSURE(sizeof(templateAesCcm) <= sizeof(caam_desc_aes_ccm_t), caam_desc_aes_ccm_t_size);
  876. status_t status;
  877. /* get template descriptor and it's size */
  878. uint32_t descriptorSize = ARRAY_SIZE(templateAesCcm);
  879. (void)caam_memcpy(descriptor, templateAesCcm, sizeof(templateAesCcm));
  880. status = caam_aes_ccm_check_input_args(base, input, iv, key, output, ivSize, aadSize, keySize, tagSize);
  881. if (status != kStatus_Success)
  882. {
  883. return status;
  884. }
  885. /* add descriptor size */
  886. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  887. /* key address and key size */
  888. descriptor[1] |= (keySize & DESC_KEY_SIZE_MASK);
  889. descriptor[2] = ADD_OFFSET((uint32_t)key);
  890. /* B0 and CTR0 */
  891. caam_aes_ccm_context_init(size, iv, ivSize, aadSize, tagSize, &descriptor[4], &descriptor[9]);
  892. /* Encrypt decrypt */
  893. if (encrypt != 0)
  894. {
  895. descriptor[13] |= 1u; /* ENC */
  896. }
  897. else if (tag != 0U)
  898. {
  899. descriptor[13] |= 2u; /* ICV_TEST */
  900. }
  901. else
  902. {
  903. /* decrypt with tag NULL (skip tag processing). nothing needs to be changed in descriptor[13] for this case */
  904. }
  905. /* AAD address and size */
  906. /* encoding is two octets, msbyte first */
  907. if (aadSize != 0U)
  908. {
  909. uint32_t swapped = swap_bytes(aadSize);
  910. uint32_t sz;
  911. (void)caam_memcpy(&descriptor[15], (uint32_t *)(uintptr_t)(((uint8_t *)&swapped) + sizeof(uint16_t)),
  912. sizeof(uint16_t));
  913. sz = aadSize > 2u ? 2u : aadSize; /* limit aad to the end of 16 bytes blk */
  914. (void)caam_memcpy(((uint8_t *)&descriptor[15]) + 2, aad, sz); /* fill B1 with aad */
  915. /* track consumed AAD. sz bytes have been moved to fifo. */
  916. aadSize -= sz;
  917. aad += sz;
  918. if (aadSize == 0U)
  919. {
  920. /* in case aadSize is 1 or 2, we add Flush bit to the command and skip FIFO LOAD AAD */
  921. descriptor[14] |= 0x00010000U; /* Flush (last AAD data) */
  922. descriptor[16] = DESC_JUMP_2; /* jump to current index + 2 (=18) */
  923. }
  924. else
  925. {
  926. descriptor[16] |= (aadSize & DESC_PAYLOAD_SIZE_MASK);
  927. descriptor[17] = ADD_OFFSET((uint32_t)aad);
  928. }
  929. }
  930. else
  931. {
  932. /* no AAD, jump directly to message */
  933. descriptor[14] = DESC_JUMP_4; /* jump to current index + 4 (=18) */
  934. }
  935. /* Message source address and size */
  936. descriptor[19] = ADD_OFFSET((uint32_t)input);
  937. descriptor[20] = size;
  938. /* Message destination address and size */
  939. descriptor[22] = ADD_OFFSET((uint32_t)output);
  940. descriptor[23] = size;
  941. if (tag != 0U)
  942. {
  943. /* For decryption change the command to FIFO LOAD, as tag data needs to be put into input FIFO */
  944. if (encrypt == 0)
  945. {
  946. /* FIFO LOAD ICV */
  947. descriptor[24] = 0x223B0000u;
  948. }
  949. descriptor[24] |= (tagSize & DESC_TAG_SIZE_MASK);
  950. descriptor[25] = ADD_OFFSET((uint32_t)tag);
  951. }
  952. else
  953. {
  954. /* tag is NULL, skip tag processing */
  955. descriptor[24] = DESC_HALT; /* always halt with status 0x0 (normal) */
  956. }
  957. /* add operation specified by descriptor to CAAM Job Ring */
  958. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  959. }
  960. /*!
  961. * brief Encrypts AES and tags using CCM block mode.
  962. *
  963. * Puts AES CCM encrypt and tag descriptor to CAAM input job ring.
  964. *
  965. * param base CAAM peripheral base address
  966. * param handle Handle used for this request. Specifies jobRing.
  967. * param[out] descriptor Memory for the CAAM descriptor.
  968. * param plaintext Input plain text to encrypt
  969. * param[out] ciphertext Output cipher text.
  970. * param size Size of input and output data in bytes. Zero means authentication only.
  971. * param iv Nonce
  972. * param ivSize Length of the Nonce in bytes. Must be 7, 8, 9, 10, 11, 12, or 13.
  973. * param aad Input additional authentication data. Can be NULL if aadSize is zero.
  974. * param aadSize Input size in bytes of AAD. Zero means data mode only (authentication skipped).
  975. * param key Input key to use for encryption
  976. * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
  977. * param[out] tag Generated output tag. Set to NULL to skip tag processing.
  978. * param tagSize Input size of the tag to generate, in bytes. Must be 4, 6, 8, 10, 12, 14, or 16.
  979. * return Status from job descriptor push
  980. */
  981. status_t CAAM_AES_EncryptTagCcmNonBlocking(CAAM_Type *base,
  982. caam_handle_t *handle,
  983. caam_desc_aes_ccm_t descriptor,
  984. const uint8_t *plaintext,
  985. uint8_t *ciphertext,
  986. size_t size,
  987. const uint8_t *iv,
  988. size_t ivSize,
  989. const uint8_t *aad,
  990. size_t aadSize,
  991. const uint8_t *key,
  992. size_t keySize,
  993. uint8_t *tag,
  994. size_t tagSize)
  995. {
  996. return caam_aes_ccm_non_blocking(base, handle, descriptor, plaintext, ciphertext, size, iv, ivSize, aad, aadSize,
  997. key, keySize, (uint32_t)tag, tagSize, 1);
  998. }
  999. /*!
  1000. * brief Decrypts AES and authenticates using CCM block mode.
  1001. *
  1002. * Puts AES CCM decrypt and check tag descriptor to CAAM input job ring.
  1003. *
  1004. * param base CAAM peripheral base address
  1005. * param handle Handle used for this request. Specifies jobRing.
  1006. * param[out] descriptor Memory for the CAAM descriptor.
  1007. * param ciphertext Input cipher text to decrypt
  1008. * param[out] plaintext Output plain text.
  1009. * param size Size of input and output data in bytes. Zero means authentication data only.
  1010. * param iv Nonce
  1011. * param ivSize Length of the Nonce in bytes. Must be 7, 8, 9, 10, 11, 12, or 13.
  1012. * param aad Input additional authentication data. Can be NULL if aadSize is zero.
  1013. * param aadSize Input size in bytes of AAD. Zero means data mode only (authentication data skipped).
  1014. * param key Input key to use for decryption
  1015. * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
  1016. * param tag Received tag. Set to NULL to skip tag processing.
  1017. * param tagSize Input size of the received tag to compare with the computed tag, in bytes. Must be 4, 6, 8, 10, 12,
  1018. * 14, or 16.
  1019. * return Status from job descriptor push
  1020. */
  1021. status_t CAAM_AES_DecryptTagCcmNonBlocking(CAAM_Type *base,
  1022. caam_handle_t *handle,
  1023. caam_desc_aes_ccm_t descriptor,
  1024. const uint8_t *ciphertext,
  1025. uint8_t *plaintext,
  1026. size_t size,
  1027. const uint8_t *iv,
  1028. size_t ivSize,
  1029. const uint8_t *aad,
  1030. size_t aadSize,
  1031. const uint8_t *key,
  1032. size_t keySize,
  1033. const uint8_t *tag,
  1034. size_t tagSize)
  1035. {
  1036. return caam_aes_ccm_non_blocking(base, handle, descriptor, ciphertext, plaintext, size, iv, ivSize, aad, aadSize,
  1037. key, keySize, (uint32_t)tag, tagSize, 0);
  1038. }
  1039. static const uint32_t templateAesCtr[] = {
  1040. /* 00 */ 0xB0800000u, /* HEADER */
  1041. /* 01 */ 0x02000000u, /* KEY */
  1042. /* 02 */ 0x00000000u, /* place: key address */
  1043. /* 03 */ 0x12201010u, /* LOAD 16 bytes of CTR0 to Class 1 Context Register. Offset 16 bytes. */
  1044. /* 04 */ 0x00000000u, /* place: CTR0 address */
  1045. /* 05 */ 0x82100000u, /* OPERATION: AES CTR (de)crypt in Update mode */
  1046. /* 06 */ 0x22530000u, /* FIFO LOAD Message */
  1047. /* 07 */ 0x00000000u, /* place: source address */
  1048. /* 08 */ 0x00000000u, /* place: source size */
  1049. /* 09 */ 0x60700000u, /* FIFO STORE Message */
  1050. /* 10 */ 0x00000000u, /* place: destination address */
  1051. /* 11 */ 0x00000000u, /* place: destination size */
  1052. /* 12 */ 0xA2000001u, /* JMP always to next command. Done checkpoint (wait for Class 1 Done) */
  1053. /* 13 */ 0x10880004u, /* LOAD Immediate to Clear Written Register. */
  1054. /* 14 */ 0x08000004u, /* value for Clear Written Register: C1D and C1DS bits are set */
  1055. /* 15 */ 0x22930010u, /* FIFO LOAD Message Immediate 16 bytes */
  1056. /* 16 */ 0x00000000u, /* all zeroes 0-3 */
  1057. /* 17 */ 0x00000000u, /* all zeroes 4-7 */
  1058. /* 18 */ 0x00000000u, /* all zeroes 8-11 */
  1059. /* 19 */ 0x00000000u, /* all zeroes 12-15 */
  1060. /* 20 */ 0x60300010u, /* FIFO STORE Message 16 bytes */
  1061. /* 21 */ 0x00000000u, /* place: counterlast[] block address */
  1062. /* 22 */ 0x82100000u, /* OPERATION: AES CTR (de)crypt in Update mode */
  1063. /* 23 */ 0x52201010u, /* STORE 16 bytes of CTRi from Class 1 Context Register offset 16 bytes. */
  1064. /* 24 */ 0x00000000u, /* place: CTRi address */
  1065. };
  1066. /*!
  1067. * brief Encrypts or decrypts AES using CTR block mode.
  1068. *
  1069. * Encrypts or decrypts AES using CTR block mode.
  1070. * AES CTR mode uses only forward AES cipher and same algorithm for encryption and decryption.
  1071. * The only difference between encryption and decryption is that, for encryption, the input argument
  1072. * is plain text and the output argument is cipher text. For decryption, the input argument is cipher text
  1073. * and the output argument is plain text.
  1074. *
  1075. * Puts AES CTR crypt descriptor to CAAM input job ring.
  1076. *
  1077. * param base CAAM peripheral base address
  1078. * param handle Handle used for this request. Specifies jobRing.
  1079. * param[out] descriptor Memory for the CAAM descriptor.
  1080. * param input Input data for CTR block mode
  1081. * param[out] output Output data for CTR block mode
  1082. * param size Size of input and output data in bytes
  1083. * param[in,out] counter Input counter (updates on return)
  1084. * param key Input key to use for forward AES cipher
  1085. * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
  1086. * param[out] counterlast Output cipher of last counter, for chained CTR calls. NULL can be passed if chained calls are
  1087. * not used.
  1088. * param[out] szLeft Output number of bytes in left unused in counterlast block. NULL can be passed if chained calls
  1089. * are not used.
  1090. * return Status from job descriptor push
  1091. */
  1092. status_t CAAM_AES_CryptCtrNonBlocking(CAAM_Type *base,
  1093. caam_handle_t *handle,
  1094. caam_desc_aes_ctr_t descriptor,
  1095. const uint8_t *input,
  1096. uint8_t *output,
  1097. size_t size,
  1098. uint8_t *counter,
  1099. const uint8_t *key,
  1100. size_t keySize,
  1101. uint8_t *counterlast,
  1102. size_t *szLeft)
  1103. {
  1104. BUILD_ASSURE(sizeof(templateAesCtr) <= sizeof(caam_desc_aes_ctr_t), caam_desc_aes_ctr_t_size);
  1105. uint32_t descriptorSize;
  1106. if (!caam_check_key_size(keySize))
  1107. {
  1108. return kStatus_InvalidArgument;
  1109. }
  1110. /* get template descriptor and it's size */
  1111. descriptorSize = ARRAY_SIZE(templateAesCtr);
  1112. (void)caam_memcpy(descriptor, templateAesCtr, sizeof(templateAesCtr));
  1113. /* add descriptor size */
  1114. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  1115. /* key address and key size */
  1116. descriptor[1] |= (keySize & DESC_KEY_SIZE_MASK);
  1117. descriptor[2] = ADD_OFFSET((uint32_t)key);
  1118. /* descriptor[3] configures 16 bytes length for CTR0 in templateAesCtr */
  1119. descriptor[4] = ADD_OFFSET((uint32_t)counter);
  1120. /* source address and size */
  1121. descriptor[7] = ADD_OFFSET((uint32_t)input);
  1122. descriptor[8] = size;
  1123. /* destination address and size */
  1124. descriptor[10] = ADD_OFFSET((uint32_t)output);
  1125. descriptor[11] = size;
  1126. /* AES CTR Crypt OPERATION in descriptor[5]
  1127. * Algorithm State (AS) in template is Update (0h)
  1128. * Only in case we are chaining the AES CTR calls (counterlast[] != NULL),
  1129. * we have to change the algorithm state to Finalize (2h)
  1130. * and so the CTRi for the last message block is not written to Class 1 Context.
  1131. * This allows us to repeat AES CTR of the last CTRi, with destination to counterlast[],
  1132. * and with using all zeroes in message data, the counterlast[] gets ECB of the last CTRi.
  1133. */
  1134. /* if counterlast or szLeft is NULL, the caller is not interested in AES of last counter
  1135. * Thus, we can skip the counterlast processing
  1136. * and only read the last CTRi from context.
  1137. * So, we replace descriptor[11] with a jump command to STORE
  1138. */
  1139. if ((counterlast == NULL) || (szLeft == NULL))
  1140. {
  1141. /* To create an unconditional jump, use TEST TYPE = 00 (all specified conditions true) and
  1142. clear all TEST CONDITION bits because the tested condition is considered to be true if
  1143. no test condition bits are set. */
  1144. descriptor[12] = 0xA000000Bu; /* jump to current index + 11 (=23) */
  1145. }
  1146. else
  1147. {
  1148. uint32_t lastSize;
  1149. descriptor[5] |= 0x08u; /* finalize */
  1150. descriptor[21] = ADD_OFFSET((uint32_t)counterlast);
  1151. lastSize = size % 16u;
  1152. if (lastSize != 0U)
  1153. {
  1154. *szLeft = 16u - lastSize;
  1155. }
  1156. else
  1157. {
  1158. *szLeft = 0;
  1159. /* descriptor[12] = 0xA000000Bu; */ /* jump to current index + 11 (=23) */
  1160. }
  1161. }
  1162. /* read last CTRi from AES back to caller */
  1163. descriptor[24] = ADD_OFFSET((uint32_t)counter);
  1164. /* add operation specified by descriptor to CAAM Job Ring */
  1165. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  1166. }
  1167. static const uint32_t templateAesEcb[] = {
  1168. /* 00 */ 0xB0800000u, /* HEADER */
  1169. /* 01 */ 0x02000000u, /* KEY */
  1170. /* 02 */ 0x00000000u, /* place: key address */
  1171. /* 03 */ 0x22530000u, /* FIFO LOAD Message with EXT size */
  1172. /* 04 */ 0x00000000u, /* place: source address */
  1173. /* 05 */ 0x00000000u, /* place: source size */
  1174. /* 06 */ 0x60700000u, /* FIFO STORE Message with EXT size */
  1175. /* 07 */ 0x00000000u, /* place: destination address */
  1176. /* 08 */ 0x00000000u, /* place: destination size */
  1177. /* 09 */ 0x82100200u, /* OPERATION: AES ECB Decrypt */
  1178. };
  1179. /*!
  1180. * brief Encrypts AES using the ECB block mode.
  1181. *
  1182. * Puts AES ECB encrypt descriptor to CAAM input job ring.
  1183. *
  1184. * param base CAAM peripheral base address
  1185. * param plaintext Input plain text to encrypt
  1186. * param[out] descriptor Memory for the CAAM descriptor.
  1187. * param[out] ciphertext Output cipher text
  1188. * param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  1189. * param key Input key to use for encryption
  1190. * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
  1191. * return Status from job descriptor push
  1192. */
  1193. status_t CAAM_AES_EncryptEcbNonBlocking(CAAM_Type *base,
  1194. caam_handle_t *handle,
  1195. caam_desc_aes_ecb_t descriptor,
  1196. const uint8_t *plaintext,
  1197. uint8_t *ciphertext,
  1198. size_t size,
  1199. const uint8_t *key,
  1200. size_t keySize)
  1201. {
  1202. BUILD_ASSURE(sizeof(templateAesEcb) <= sizeof(caam_desc_aes_ecb_t), caam_desc_aes_ecb_t_size);
  1203. uint32_t descriptorSize;
  1204. if (!caam_check_key_size(keySize))
  1205. {
  1206. return kStatus_InvalidArgument;
  1207. }
  1208. /* ECB mode, size must be non-zero 16-byte multiple */
  1209. if (0U != (size % 16u))
  1210. {
  1211. return kStatus_InvalidArgument;
  1212. }
  1213. descriptorSize = ARRAY_SIZE(templateAesEcb);
  1214. (void)caam_memcpy(descriptor, templateAesEcb, sizeof(templateAesEcb));
  1215. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  1216. descriptor[1] |= (keySize & DESC_KEY_SIZE_MASK);
  1217. descriptor[2] = (uint32_t)ADD_OFFSET((uint32_t)key);
  1218. /* descriptor[3] FIFO LOAD copied from template */
  1219. descriptor[4] = (uint32_t)ADD_OFFSET((uint32_t)plaintext);
  1220. descriptor[5] = size; /* FIFO LOAD EXT size */
  1221. /* descriptor[6] FIFO STORE copied from template */
  1222. descriptor[7] = (uint32_t)ADD_OFFSET((uint32_t)ciphertext);
  1223. descriptor[8] = size; /* FIFO STORE EXT size */
  1224. descriptor[9] |= 1u; /* add ENC bit to specify Encrypt OPERATION */
  1225. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  1226. }
  1227. /*!
  1228. * brief Decrypts AES using ECB block mode.
  1229. *
  1230. * Puts AES ECB decrypt descriptor to CAAM input job ring.
  1231. *
  1232. * param base CAAM peripheral base address
  1233. * param handle Handle used for this request. Specifies jobRing.
  1234. * param[out] descriptor Memory for the CAAM descriptor.
  1235. * param ciphertext Input cipher text to decrypt
  1236. * param[out] plaintext Output plain text
  1237. * param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  1238. * param key Input key.
  1239. * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
  1240. * return Status from job descriptor push
  1241. */
  1242. status_t CAAM_AES_DecryptEcbNonBlocking(CAAM_Type *base,
  1243. caam_handle_t *handle,
  1244. caam_desc_aes_ecb_t descriptor,
  1245. const uint8_t *ciphertext,
  1246. uint8_t *plaintext,
  1247. size_t size,
  1248. const uint8_t *key,
  1249. size_t keySize)
  1250. {
  1251. uint32_t descriptorSize;
  1252. if (!caam_check_key_size(keySize))
  1253. {
  1254. return kStatus_InvalidArgument;
  1255. }
  1256. /* ECB mode, size must be non-zero 16-byte multiple */
  1257. if (0U != (size % 16u))
  1258. {
  1259. return kStatus_InvalidArgument;
  1260. }
  1261. descriptorSize = ARRAY_SIZE(templateAesEcb);
  1262. (void)caam_memcpy(descriptor, templateAesEcb, sizeof(templateAesEcb));
  1263. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  1264. descriptor[1] |= (keySize & DESC_KEY_SIZE_MASK);
  1265. descriptor[2] = (uint32_t)ADD_OFFSET((uint32_t)key);
  1266. /* descriptor[3] FIFO LOAD copied from template */
  1267. descriptor[4] = (uint32_t)ADD_OFFSET((uint32_t)ciphertext);
  1268. descriptor[5] = size;
  1269. /* descriptor[6] FIFO STORE copied from template */
  1270. descriptor[7] = (uint32_t)ADD_OFFSET((uint32_t)plaintext);
  1271. descriptor[8] = size; /* FIFO STORE EXT size */
  1272. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  1273. }
  1274. static const uint32_t templateAesCbc[] = {
  1275. /* 00 */ 0xB0800000u, /* HEADER */
  1276. /* 01 */ 0x02000000u, /* KEY */
  1277. /* 02 */ 0x00000000u, /* place: key address */
  1278. /* 03 */ 0x12200010u, /* LOAD 16 bytes of iv to Class 1 Context Register */
  1279. /* 04 */ 0x00000000u, /* place: iv address */
  1280. /* 05 */ 0x22530000u, /* FIFO LOAD Message */
  1281. /* 06 */ 0x00000000u, /* place: source address */
  1282. /* 07 */ 0x00000000u, /* place: source size */
  1283. /* 08 */ 0x60700000u, /* FIFO STORE Message */
  1284. /* 09 */ 0x00000000u, /* place: destination address */
  1285. /* 10 */ 0x00000000u, /* place: destination size */
  1286. /* 11 */ 0x82100100u, /* OPERATION: AES CBC Decrypt */
  1287. };
  1288. /*!
  1289. * brief Encrypts AES using CBC block mode.
  1290. *
  1291. * Puts AES CBC encrypt descriptor to CAAM input job ring.
  1292. *
  1293. * param base CAAM peripheral base address
  1294. * param handle Handle used for this request. Specifies jobRing.
  1295. * param[out] descriptor Memory for the CAAM descriptor.
  1296. * param plaintext Input plain text to encrypt
  1297. * param[out] ciphertext Output cipher text
  1298. * param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  1299. * param iv Input initial vector to combine with the first input block.
  1300. * param key Input key to use for encryption
  1301. * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
  1302. * return Status from job descriptor push
  1303. */
  1304. status_t CAAM_AES_EncryptCbcNonBlocking(CAAM_Type *base,
  1305. caam_handle_t *handle,
  1306. caam_desc_aes_cbc_t descriptor,
  1307. const uint8_t *plaintext,
  1308. uint8_t *ciphertext,
  1309. size_t size,
  1310. const uint8_t *iv,
  1311. const uint8_t *key,
  1312. size_t keySize)
  1313. {
  1314. BUILD_ASSURE(sizeof(templateAesCbc) <= sizeof(caam_desc_aes_cbc_t), caam_desc_aes_cbc_t_size);
  1315. uint32_t descriptorSize;
  1316. if (!caam_check_key_size(keySize))
  1317. {
  1318. return kStatus_InvalidArgument;
  1319. }
  1320. /* CBC mode, size must be non-zero 16-byte multiple */
  1321. if (0U != (size % 16u))
  1322. {
  1323. return kStatus_InvalidArgument;
  1324. }
  1325. /* get template descriptor and it's size */
  1326. descriptorSize = ARRAY_SIZE(templateAesCbc);
  1327. (void)caam_memcpy(descriptor, templateAesCbc, sizeof(templateAesCbc));
  1328. /* add descriptor size */
  1329. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  1330. /* key address and key size */
  1331. descriptor[1] |= (keySize & DESC_KEY_SIZE_MASK);
  1332. descriptor[2] = (uint32_t)ADD_OFFSET((uint32_t)key);
  1333. /* descriptor[3] configures 16 bytes length for IV in templateAesCbc */
  1334. descriptor[4] = (uint32_t)ADD_OFFSET((uint32_t)iv);
  1335. /* source address and size */
  1336. descriptor[6] = (uint32_t)ADD_OFFSET((uint32_t)plaintext);
  1337. descriptor[7] = size;
  1338. /* destination address and size */
  1339. descriptor[9] = (uint32_t)ADD_OFFSET((uint32_t)ciphertext);
  1340. descriptor[10] = size;
  1341. /* AES CBC */
  1342. descriptor[11] |= 1u; /* add ENC bit to specify Encrypt OPERATION */
  1343. /* add operation specified by descriptor to CAAM Job Ring */
  1344. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  1345. }
  1346. /*!
  1347. * brief Decrypts AES using CBC block mode.
  1348. *
  1349. * Puts AES CBC decrypt descriptor to CAAM input job ring.
  1350. *
  1351. * param base CAAM peripheral base address
  1352. * param handle Handle used for this request. Specifies jobRing.
  1353. * param[out] descriptor Memory for the CAAM descriptor.
  1354. * param ciphertext Input cipher text to decrypt
  1355. * param[out] plaintext Output plain text
  1356. * param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  1357. * param iv Input initial vector to combine with the first input block.
  1358. * param key Input key to use for decryption
  1359. * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
  1360. * return Status from job descriptor push
  1361. */
  1362. status_t CAAM_AES_DecryptCbcNonBlocking(CAAM_Type *base,
  1363. caam_handle_t *handle,
  1364. caam_desc_aes_cbc_t descriptor,
  1365. const uint8_t *ciphertext,
  1366. uint8_t *plaintext,
  1367. size_t size,
  1368. const uint8_t *iv,
  1369. const uint8_t *key,
  1370. size_t keySize)
  1371. {
  1372. uint32_t descriptorSize;
  1373. if (!caam_check_key_size(keySize))
  1374. {
  1375. return kStatus_InvalidArgument;
  1376. }
  1377. /* CBC mode, size must be non-zero 16-byte multiple */
  1378. if (0U != (size % 16u))
  1379. {
  1380. return kStatus_InvalidArgument;
  1381. }
  1382. /* get template descriptor and it's size */
  1383. descriptorSize = ARRAY_SIZE(templateAesCbc);
  1384. (void)caam_memcpy(descriptor, templateAesCbc, sizeof(templateAesCbc));
  1385. /* add descriptor size */
  1386. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  1387. /* key address and key size */
  1388. descriptor[1] |= (keySize & DESC_KEY_SIZE_MASK);
  1389. descriptor[2] = ADD_OFFSET((uint32_t)key);
  1390. /* descriptor[3] configures 16 bytes length for IV in templateAesCbc */
  1391. descriptor[4] = ADD_OFFSET((uint32_t)iv);
  1392. /* source address and size */
  1393. descriptor[6] = ADD_OFFSET((uint32_t)ciphertext);
  1394. descriptor[7] = size;
  1395. /* destination address and size */
  1396. descriptor[9] = ADD_OFFSET((uint32_t)plaintext);
  1397. descriptor[10] = size;
  1398. /* AES CBC Decrypt OPERATION in descriptor[11] */
  1399. /* add operation specified by descriptor to CAAM Job Ring */
  1400. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  1401. }
  1402. /*!
  1403. * brief Encrypts AES and tags using GCM block mode.
  1404. *
  1405. * Encrypts AES and optionally tags using GCM block mode. If plaintext is NULL, only the GHASH is calculated and output
  1406. * in the 'tag' field.
  1407. * Puts AES GCM encrypt and tag descriptor to CAAM input job ring.
  1408. *
  1409. * param base CAAM peripheral base address
  1410. * param handle Handle used for this request. Specifies jobRing.
  1411. * param[out] descriptor Memory for the CAAM descriptor.
  1412. * param plaintext Input plain text to encrypt
  1413. * param[out] ciphertext Output cipher text.
  1414. * param size Size of input and output data in bytes
  1415. * param iv Input initial vector
  1416. * param ivSize Size of the IV
  1417. * param aad Input additional authentication data
  1418. * param aadSize Input size in bytes of AAD
  1419. * param key Input key to use for encryption
  1420. * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
  1421. * param[out] tag Output hash tag. Set to NULL to skip tag processing.
  1422. * param tagSize Input size of the tag to generate, in bytes. Must be 4,8,12,13,14,15 or 16.
  1423. * return Status from job descriptor push
  1424. */
  1425. status_t CAAM_AES_EncryptTagGcmNonBlocking(CAAM_Type *base,
  1426. caam_handle_t *handle,
  1427. caam_desc_aes_gcm_t descriptor,
  1428. const uint8_t *plaintext,
  1429. uint8_t *ciphertext,
  1430. size_t size,
  1431. const uint8_t *iv,
  1432. size_t ivSize,
  1433. const uint8_t *aad,
  1434. size_t aadSize,
  1435. const uint8_t *key,
  1436. size_t keySize,
  1437. uint8_t *tag,
  1438. size_t tagSize)
  1439. {
  1440. return caam_aes_gcm_non_blocking(base, handle, descriptor, plaintext, ciphertext, size, iv, ivSize, aad, aadSize,
  1441. key, keySize, (uint32_t)tag, tagSize, 1);
  1442. }
  1443. /*!
  1444. * brief Decrypts AES and authenticates using GCM block mode.
  1445. *
  1446. * Decrypts AES and optionally authenticates using GCM block mode. If ciphertext is NULL, only the GHASH is calculated
  1447. * and compared with the received GHASH in 'tag' field.
  1448. * Puts AES GCM decrypt and check tag descriptor to CAAM input job ring.
  1449. *
  1450. * param base CAAM peripheral base address
  1451. * param handle Handle used for this request. Specifies jobRing.
  1452. * param[out] descriptor Memory for the CAAM descriptor.
  1453. * param ciphertext Input cipher text to decrypt
  1454. * param[out] plaintext Output plain text.
  1455. * param size Size of input and output data in bytes
  1456. * param iv Input initial vector
  1457. * param ivSize Size of the IV
  1458. * param aad Input additional authentication data
  1459. * param aadSize Input size in bytes of AAD
  1460. * param key Input key to use for encryption
  1461. * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
  1462. * param tag Input hash tag to compare. Set to NULL to skip tag processing.
  1463. * param tagSize Input size of the tag, in bytes. Must be 4, 8, 12, 13, 14, 15, or 16.
  1464. * return Status from job descriptor push
  1465. */
  1466. status_t CAAM_AES_DecryptTagGcmNonBlocking(CAAM_Type *base,
  1467. caam_handle_t *handle,
  1468. caam_desc_aes_gcm_t descriptor,
  1469. const uint8_t *ciphertext,
  1470. uint8_t *plaintext,
  1471. size_t size,
  1472. const uint8_t *iv,
  1473. size_t ivSize,
  1474. const uint8_t *aad,
  1475. size_t aadSize,
  1476. const uint8_t *key,
  1477. size_t keySize,
  1478. const uint8_t *tag,
  1479. size_t tagSize)
  1480. {
  1481. return caam_aes_gcm_non_blocking(base, handle, descriptor, ciphertext, plaintext, size, iv, ivSize, aad, aadSize,
  1482. key, keySize, (uint32_t)tag, tagSize, 0);
  1483. }
  1484. /*!
  1485. * brief Gets the default configuration structure.
  1486. *
  1487. * This function initializes the CAAM configuration structure to a default value. The default
  1488. * values are as follows.
  1489. * caamConfig->rngSampleMode = kCAAM_RNG_SampleModeVonNeumann;
  1490. * caamConfig->rngRingOscDiv = kCAAM_RNG_RingOscDiv4;
  1491. *
  1492. * param[out] config Pointer to configuration structure.
  1493. */
  1494. void CAAM_GetDefaultConfig(caam_config_t *config)
  1495. {
  1496. /* Initializes the configure structure to zero. */
  1497. (void)memset(config, 0, sizeof(*config));
  1498. caam_config_t userConfig = {
  1499. {NULL, NULL, NULL, NULL}, kCAAM_RNG_SampleModeVonNeumann, kCAAM_RNG_RingOscDiv4, true, true, true, true,
  1500. kCAAM_NormalOperationBlobs,
  1501. };
  1502. *config = userConfig;
  1503. }
  1504. /*!
  1505. * brief Initializes the CAAM driver.
  1506. *
  1507. * This function initializes the CAAM driver, including CAAM's internal RNG.
  1508. * param base CAAM peripheral base address
  1509. * param config Pointer to configuration structure.
  1510. * return kStatus_Success the CAAM Init has completed with zero termination status word
  1511. * return kStatus_Fail the CAAM Init has completed with non-zero termination status word
  1512. */
  1513. status_t CAAM_Init(CAAM_Type *base, const caam_config_t *config)
  1514. {
  1515. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  1516. CLOCK_EnableClock(kCLOCK_Caam);
  1517. #endif
  1518. status_t status = kStatus_Fail;
  1519. base->MCFGR = 0x80000000u; /* reset */
  1520. base->MCFGR = 0x90000000u; /* reset DMA */
  1521. base->MCFGR = 0x00082300u; /* (reset value) */
  1522. /* job ring interface 0 is mandatory */
  1523. assert(config->jobRingInterface[0]);
  1524. if (NULL == config->jobRingInterface[0])
  1525. {
  1526. return kStatus_Fail; /* return if user wishes to use the job ring but does not configure it. */
  1527. }
  1528. /* Job Rings Configuration
  1529. * number of entries in both input and output ring is equal.
  1530. * Note the size of an entry is different. an entry in the input ring is a 32-bit word.
  1531. * an entry in the output ring is two 32-bit words. (descriptor pointer followed by termination status word)
  1532. */
  1533. s_jr0 = config->jobRingInterface[0];
  1534. (void)memset(s_jr0, 0, sizeof(*s_jr0));
  1535. s_jrIndex0 = 0;
  1536. caam_job_ring_set_base_address_and_size(base, kCAAM_JobRing0, s_jr0->inputJobRing, ARRAY_SIZE(s_jr0->inputJobRing),
  1537. s_jr0->outputJobRing, ARRAY_SIZE(s_jr0->outputJobRing) / 2U);
  1538. if (config->jobRingInterface[1] != NULL)
  1539. {
  1540. s_jr1 = config->jobRingInterface[1];
  1541. (void)memset(s_jr1, 0, sizeof(*s_jr1));
  1542. s_jrIndex1 = 0;
  1543. caam_job_ring_set_base_address_and_size(base, kCAAM_JobRing1, s_jr1->inputJobRing,
  1544. ARRAY_SIZE(s_jr1->inputJobRing), s_jr1->outputJobRing,
  1545. ARRAY_SIZE(s_jr1->outputJobRing) / 2U);
  1546. }
  1547. if (config->jobRingInterface[2] != NULL)
  1548. {
  1549. s_jr2 = config->jobRingInterface[2];
  1550. (void)memset(s_jr2, 0, sizeof(*s_jr2));
  1551. s_jrIndex2 = 0;
  1552. caam_job_ring_set_base_address_and_size(base, kCAAM_JobRing2, s_jr2->inputJobRing,
  1553. ARRAY_SIZE(s_jr2->inputJobRing), s_jr2->outputJobRing,
  1554. ARRAY_SIZE(s_jr2->outputJobRing) / 2U);
  1555. }
  1556. if (config->jobRingInterface[3] != NULL)
  1557. {
  1558. s_jr3 = config->jobRingInterface[3];
  1559. (void)memset(s_jr3, 0, sizeof(*s_jr3));
  1560. s_jrIndex3 = 0;
  1561. caam_job_ring_set_base_address_and_size(base, kCAAM_JobRing3, s_jr3->inputJobRing,
  1562. ARRAY_SIZE(s_jr3->inputJobRing), s_jr3->outputJobRing,
  1563. ARRAY_SIZE(s_jr3->outputJobRing) / 2U);
  1564. }
  1565. /*
  1566. * Instantiate RNG in normal (non-deterministic) mode and load the JDKEK, TDKEK and TDSK registers
  1567. * this step is required for example
  1568. * for FIFO STORE command to be able to store Key register as Black key
  1569. * for example during AES XCBC-MAC context switch (need to store derived key K1 to memory)
  1570. */
  1571. caam_rng_config_t rngConfig;
  1572. (void)CAAM_RNG_GetDefaultConfig(&rngConfig);
  1573. /* reset RNG */
  1574. base->RTMCTL = CAAM_RTMCTL_PRGM_MASK | CAAM_RTMCTL_ERR_MASK | CAAM_RTMCTL_RST_DEF_MASK |
  1575. CAAM_RTMCTL_SAMP_MODE(kCAAM_RNG_SampleModeRaw);
  1576. base->RTMCTL = CAAM_RTMCTL_ERR_MASK | CAAM_RTMCTL_SAMP_MODE(config->rngSampleMode) |
  1577. CAAM_RTMCTL_OSC_DIV(config->rngRingOscDiv);
  1578. caam_handle_t handle;
  1579. handle.jobRing = kCAAM_JobRing0;
  1580. /* Check if Instantiated State Handle 0 or 1 has been instantiated */
  1581. if ((base->RDSTA & (CAAM_RDSTA_IF0_MASK | CAAM_RDSTA_IF1_MASK)) == 0U)
  1582. {
  1583. status = CAAM_RNG_Init(base, &handle, kCAAM_RngStateHandle0, &rngConfig);
  1584. if (status != kStatus_Success)
  1585. {
  1586. return status;
  1587. }
  1588. }
  1589. else
  1590. {
  1591. status = kStatus_Success;
  1592. }
  1593. /* Check if JDKEK, TDKEK and TDSK are already generated, generate if not */
  1594. /* Note: second secure keys generate per one PoR will generate Secure Key error */
  1595. if ((base->JDKEKR[0U] == 0U) && (base->TDKEKR[0U] == 0U) && (base->TDSKR[0U] == 0U))
  1596. {
  1597. /* Note: Secure key is cleared only during POR reset */
  1598. status = CAAM_RNG_GenerateSecureKey(base, &handle, NULL);
  1599. if (status != kStatus_Success)
  1600. {
  1601. return status;
  1602. }
  1603. }
  1604. else
  1605. {
  1606. status = kStatus_Success;
  1607. }
  1608. /* set RANDDPAR bit for the AESA to reseed its DPA mask using data from kCAAM_RngStateHandle0 */
  1609. /* also set other bits to 1 for security */
  1610. base->SCFGR =
  1611. #if defined(FSL_FEATURE_CAAM_HAS_RANDDPAR) && (FSL_FEATURE_CAAM_HAS_RANDDPAR > 0)
  1612. CAAM_SCFGR_RANDDPAR(config->scfgrRandomDpaResistance) |
  1613. #endif /* FSL_FEATURE_CAAM_HAS_RANDDPAR */
  1614. #if defined(FSL_FEATURE_CAAM_HAS_RDB) && (FSL_FEATURE_CAAM_HAS_RDB > 0)
  1615. CAAM_SCFGR_RDB(config->scfgrEnableRandomDataBuffer) |
  1616. #endif /* FSL_FEATURE_CAAM_HAS_RDB */
  1617. CAAM_SCFGR_LCK_TRNG(config->scfgrLockTrngProgramMode) | CAAM_SCFGR_RNGSH0(config->scfgrRandomRngStateHandle0) |
  1618. CAAM_SCFGR_PRIBLOB(config->scfgrPriblob);
  1619. return status;
  1620. }
  1621. /*!
  1622. * brief Wait for a CAAM job to complete.
  1623. *
  1624. * This function polls CAAM output ring for a specific job.
  1625. *
  1626. * The CAAM job ring is specified by the jobRing field in the caam_handle_t structure.
  1627. * The job to be waited is specified by it's descriptor address.
  1628. *
  1629. * This function has two modes, determined by the mode argument.
  1630. * In blocking mode, the function polls the specified jobRing until the descriptor
  1631. * is available in the CAAM output job ring.
  1632. * In non-blocking mode, it polls the output ring once and returns status
  1633. * immediately.
  1634. *
  1635. * The function can be called from multiple threads or interrupt service routines,
  1636. * as internally it uses global critical section (global interrupt disable enable)
  1637. * to protect it's operation against concurrent accesses.
  1638. * The global interrupt is disabled only when the descriptor is found
  1639. * in the output ring, for a very short time, to remove the descriptor from the output ring
  1640. * safely.
  1641. *
  1642. * param base CAAM peripheral base address
  1643. * param handle Data structure with CAAM jobRing used for this request
  1644. * param descriptor
  1645. * param mode Blocking and non-blocking mode. Zero is blocking. Non-zero is non-blocking.
  1646. * return kStatus_Success the CAAM job has completed with zero job termination status word
  1647. * return kStatus_Fail the CAAM job has completed with non-zero job termination status word
  1648. * return kStatus_Again In non-blocking mode, the job is not ready in the CAAM Output Ring
  1649. */
  1650. status_t CAAM_Wait(CAAM_Type *base, caam_handle_t *handle, uint32_t *descriptor, caam_wait_mode_t mode)
  1651. {
  1652. /* poll output ring for the specified job descriptor */
  1653. status_t status;
  1654. bool wait;
  1655. bool found;
  1656. wait = true;
  1657. status = kStatus_Success;
  1658. found = false;
  1659. while (wait)
  1660. {
  1661. /* any result available on this job ring? */
  1662. if ((caam_output_ring_get_slots_full(base, handle->jobRing)) != 0U)
  1663. {
  1664. status = caam_out_job_ring_test_and_remove(base, handle->jobRing,
  1665. (uint32_t *)ADD_OFFSET((uint32_t)descriptor), &wait, &found);
  1666. }
  1667. /* non-blocking mode polls output ring once */
  1668. if (mode == kCAAM_Nonblocking)
  1669. {
  1670. wait = false; /* exit the while() */
  1671. if (!found)
  1672. {
  1673. status = kStatus_CAAM_Again; /* job not in the tested ring */
  1674. }
  1675. }
  1676. }
  1677. return status;
  1678. }
  1679. /*!
  1680. * brief Deinitializes the CAAM driver.
  1681. * This function deinitializes the CAAM driver.
  1682. * param base CAAM peripheral base address
  1683. * return kStatus_Success the CAAM Deinit has completed with zero termination status word
  1684. * return kStatus_Fail the CAAM Deinit has completed with non-zero termination status word
  1685. */
  1686. status_t CAAM_Deinit(CAAM_Type *base)
  1687. {
  1688. caam_handle_t handle;
  1689. handle.jobRing = kCAAM_JobRing0;
  1690. status_t status = kStatus_Fail;
  1691. status = CAAM_RNG_Deinit(base, &handle, kCAAM_RngStateHandle0);
  1692. if (status != kStatus_Success)
  1693. {
  1694. return status;
  1695. }
  1696. base->JOBRING[0].JRCR_JR = CAAM_JRCR_JR_RESET_MASK;
  1697. base->JOBRING[0].JRCR_JR = CAAM_JRCR_JR_RESET_MASK;
  1698. base->JOBRING[1].JRCR_JR = CAAM_JRCR_JR_RESET_MASK;
  1699. base->JOBRING[1].JRCR_JR = CAAM_JRCR_JR_RESET_MASK;
  1700. base->DRR = CAAM_DRR_RST0_MASK;
  1701. base->MCFGR = CAAM_MCFGR_SWRST_MASK; /* reset */
  1702. base->MCFGR = CAAM_MCFGR_SWRST_MASK | CAAM_MCFGR_DMA_RST_MASK; /* reset DMA */
  1703. base->MCFGR = 0x00082300u; /* (reset value) */
  1704. while (0U == (base->RTMCTL & CAAM_RTMCTL_TSTOP_OK_MASK))
  1705. {
  1706. }
  1707. /* reset RNG */
  1708. base->RTMCTL = CAAM_RTMCTL_PRGM_MASK | CAAM_RTMCTL_ERR_MASK | CAAM_RTMCTL_RST_DEF_MASK |
  1709. CAAM_RTMCTL_SAMP_MODE(kCAAM_RNG_SampleModeRaw);
  1710. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  1711. CLOCK_DisableClock(kCLOCK_Caam);
  1712. #endif
  1713. return status;
  1714. }
  1715. /*!
  1716. * brief External Key Transfer.
  1717. *
  1718. * This function loads the given key source to an CAAM external destination via a private interface,
  1719. * such as Inline Encryption Engine IEE Private Key bus.
  1720. *
  1721. * The CAAM job ring is specified by the jobRing field in the caam_handle_t structure.
  1722. *
  1723. * This function is blocking.
  1724. *
  1725. * param base CAAM peripheral base address
  1726. * param handle Data structure with CAAM jobRing used for this request.
  1727. * param keySource The source from which the key will be obtained.
  1728. * param keySize Size of the key in bytes.
  1729. * return kStatus_Success the CAAM job has completed with zero job termination status word
  1730. * return kStatus_Fail the CAAM job has completed with non-zero job termination status word
  1731. */
  1732. status_t CAAM_ExternalKeyTransfer(CAAM_Type *base,
  1733. caam_handle_t *handle,
  1734. caam_ext_key_xfr_source_t keySource,
  1735. size_t keySize)
  1736. {
  1737. caam_desc_aes_ecb_t descBuf = {0};
  1738. status_t status;
  1739. descBuf[0] = 0xB0800002u; /* HEADER */
  1740. descBuf[1] = 0x40000000u; /* EXT KEY XFR command. */
  1741. switch (keySource)
  1742. {
  1743. case kCAAM_ExtKeyXfr_KeyRegisterClass1:
  1744. case kCAAM_ExtKeyXfr_KeyRegisterClass2:
  1745. case kCAAM_ExtKeyXfr_PkhaRamE:
  1746. descBuf[1] |= ((uint32_t)keySource << 16) | keySize;
  1747. status = kStatus_Success;
  1748. break;
  1749. default:
  1750. status = kStatus_InvalidArgument;
  1751. break;
  1752. }
  1753. if (status != kStatus_Success)
  1754. {
  1755. return status;
  1756. }
  1757. /* schedule the job and block wait for result */
  1758. do
  1759. {
  1760. status = caam_in_job_ring_add(base, handle->jobRing, &descBuf[0]);
  1761. } while (status != kStatus_Success);
  1762. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  1763. }
  1764. /*!
  1765. * brief Encrypts AES using the ECB block mode.
  1766. *
  1767. * Encrypts AES using the ECB block mode.
  1768. *
  1769. * param base CAAM peripheral base address
  1770. * param plaintext Input plain text to encrypt
  1771. * param[out] ciphertext Output cipher text
  1772. * param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  1773. * param key Input key to use for encryption
  1774. * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
  1775. * return Status from encrypt operation
  1776. */
  1777. status_t CAAM_AES_EncryptEcb(CAAM_Type *base,
  1778. caam_handle_t *handle,
  1779. const uint8_t *plaintext,
  1780. uint8_t *ciphertext,
  1781. size_t size,
  1782. const uint8_t *key,
  1783. size_t keySize)
  1784. {
  1785. caam_desc_aes_ecb_t descBuf;
  1786. status_t status;
  1787. do
  1788. {
  1789. status = CAAM_AES_EncryptEcbNonBlocking(base, handle, descBuf, plaintext, ciphertext, size, key, keySize);
  1790. } while (status == kStatus_CAAM_Again);
  1791. if (status != 0)
  1792. {
  1793. return status;
  1794. }
  1795. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  1796. }
  1797. /*!
  1798. * brief Decrypts AES using ECB block mode.
  1799. *
  1800. * Decrypts AES using ECB block mode.
  1801. *
  1802. * param base CAAM peripheral base address
  1803. * param handle Handle used for this request. Specifies jobRing.
  1804. * param ciphertext Input cipher text to decrypt
  1805. * param[out] plaintext Output plain text
  1806. * param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  1807. * param key Input key.
  1808. * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
  1809. * return Status from decrypt operation
  1810. */
  1811. status_t CAAM_AES_DecryptEcb(CAAM_Type *base,
  1812. caam_handle_t *handle,
  1813. const uint8_t *ciphertext,
  1814. uint8_t *plaintext,
  1815. size_t size,
  1816. const uint8_t *key,
  1817. size_t keySize)
  1818. {
  1819. caam_desc_aes_ecb_t descBuf;
  1820. status_t status;
  1821. do
  1822. {
  1823. status = CAAM_AES_DecryptEcbNonBlocking(base, handle, descBuf, ciphertext, plaintext, size, key, keySize);
  1824. } while (status == kStatus_CAAM_Again);
  1825. if (status != 0)
  1826. {
  1827. return status;
  1828. }
  1829. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  1830. }
  1831. /*!
  1832. * brief Encrypts AES using CBC block mode.
  1833. *
  1834. * param base CAAM peripheral base address
  1835. * param handle Handle used for this request. Specifies jobRing.
  1836. * param plaintext Input plain text to encrypt
  1837. * param[out] ciphertext Output cipher text
  1838. * param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  1839. * param iv Input initial vector to combine with the first input block.
  1840. * param key Input key to use for encryption
  1841. * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
  1842. * return Status from encrypt operation
  1843. */
  1844. status_t CAAM_AES_EncryptCbc(CAAM_Type *base,
  1845. caam_handle_t *handle,
  1846. const uint8_t *plaintext,
  1847. uint8_t *ciphertext,
  1848. size_t size,
  1849. const uint8_t iv[16],
  1850. const uint8_t *key,
  1851. size_t keySize)
  1852. {
  1853. caam_desc_aes_cbc_t descBuf;
  1854. status_t status;
  1855. do
  1856. {
  1857. status = CAAM_AES_EncryptCbcNonBlocking(base, handle, descBuf, plaintext, ciphertext, size, iv, key, keySize);
  1858. } while (status == kStatus_CAAM_Again);
  1859. if (status != 0)
  1860. {
  1861. return status;
  1862. }
  1863. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  1864. }
  1865. /*!
  1866. * brief Decrypts AES using CBC block mode.
  1867. *
  1868. * param base CAAM peripheral base address
  1869. * param handle Handle used for this request. Specifies jobRing.
  1870. * param ciphertext Input cipher text to decrypt
  1871. * param[out] plaintext Output plain text
  1872. * param size Size of input and output data in bytes. Must be multiple of 16 bytes.
  1873. * param iv Input initial vector to combine with the first input block.
  1874. * param key Input key to use for decryption
  1875. * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
  1876. * return Status from decrypt operation
  1877. */
  1878. status_t CAAM_AES_DecryptCbc(CAAM_Type *base,
  1879. caam_handle_t *handle,
  1880. const uint8_t *ciphertext,
  1881. uint8_t *plaintext,
  1882. size_t size,
  1883. const uint8_t iv[16],
  1884. const uint8_t *key,
  1885. size_t keySize)
  1886. {
  1887. caam_desc_aes_cbc_t descBuf;
  1888. status_t status;
  1889. do
  1890. {
  1891. status = CAAM_AES_DecryptCbcNonBlocking(base, handle, descBuf, ciphertext, plaintext, size, iv, key, keySize);
  1892. } while (status == kStatus_CAAM_Again);
  1893. if (status != 0)
  1894. {
  1895. return status;
  1896. }
  1897. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  1898. }
  1899. /*!
  1900. * brief Encrypts or decrypts AES using CTR block mode.
  1901. *
  1902. * Encrypts or decrypts AES using CTR block mode.
  1903. * AES CTR mode uses only forward AES cipher and same algorithm for encryption and decryption.
  1904. * The only difference between encryption and decryption is that, for encryption, the input argument
  1905. * is plain text and the output argument is cipher text. For decryption, the input argument is cipher text
  1906. * and the output argument is plain text.
  1907. *
  1908. * param base CAAM peripheral base address
  1909. * param handle Handle used for this request. Specifies jobRing.
  1910. * param input Input data for CTR block mode
  1911. * param[out] output Output data for CTR block mode
  1912. * param size Size of input and output data in bytes
  1913. * param[in,out] counter Input counter (updates on return)
  1914. * param key Input key to use for forward AES cipher
  1915. * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
  1916. * param[out] counterlast Output cipher of last counter, for chained CTR calls. NULL can be passed if chained calls are
  1917. * not used.
  1918. * param[out] szLeft Output number of bytes in left unused in counterlast block. NULL can be passed if chained calls
  1919. * are not used.
  1920. * return Status from encrypt operation
  1921. */
  1922. status_t CAAM_AES_CryptCtr(CAAM_Type *base,
  1923. caam_handle_t *handle,
  1924. const uint8_t *input,
  1925. uint8_t *output,
  1926. size_t size,
  1927. uint8_t counter[16],
  1928. const uint8_t *key,
  1929. size_t keySize,
  1930. uint8_t counterlast[16],
  1931. size_t *szLeft)
  1932. {
  1933. caam_desc_aes_ctr_t descBuf;
  1934. status_t status;
  1935. do
  1936. {
  1937. status = CAAM_AES_CryptCtrNonBlocking(base, handle, descBuf, input, output, size, counter, key, keySize,
  1938. counterlast, szLeft);
  1939. } while (status == kStatus_CAAM_Again);
  1940. if (status != 0)
  1941. {
  1942. return status;
  1943. }
  1944. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  1945. }
  1946. /*!
  1947. * brief Encrypts AES and tags using CCM block mode.
  1948. *
  1949. * Encrypts AES and optionally tags using CCM block mode.
  1950. *
  1951. * param base CAAM peripheral base address
  1952. * param handle Handle used for this request. Specifies jobRing.
  1953. * param plaintext Input plain text to encrypt
  1954. * param[out] ciphertext Output cipher text.
  1955. * param size Size of input and output data in bytes. Zero means authentication only.
  1956. * param iv Nonce
  1957. * param ivSize Length of the Nonce in bytes. Must be 7, 8, 9, 10, 11, 12, or 13.
  1958. * param aad Input additional authentication data. Can be NULL if aadSize is zero.
  1959. * param aadSize Input size in bytes of AAD. Zero means data mode only (authentication skipped).
  1960. * param key Input key to use for encryption
  1961. * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
  1962. * param[out] tag Generated output tag. Set to NULL to skip tag processing.
  1963. * param tagSize Input size of the tag to generate, in bytes. Must be 4, 6, 8, 10, 12, 14, or 16.
  1964. * return Status from encrypt operation
  1965. */
  1966. status_t CAAM_AES_EncryptTagCcm(CAAM_Type *base,
  1967. caam_handle_t *handle,
  1968. const uint8_t *plaintext,
  1969. uint8_t *ciphertext,
  1970. size_t size,
  1971. const uint8_t *iv,
  1972. size_t ivSize,
  1973. const uint8_t *aad,
  1974. size_t aadSize,
  1975. const uint8_t *key,
  1976. size_t keySize,
  1977. uint8_t *tag,
  1978. size_t tagSize)
  1979. {
  1980. caam_desc_aes_ccm_t descBuf;
  1981. status_t status;
  1982. do
  1983. {
  1984. status = CAAM_AES_EncryptTagCcmNonBlocking(base, handle, descBuf, plaintext, ciphertext, size, iv, ivSize, aad,
  1985. aadSize, key, keySize, tag, tagSize);
  1986. } while (status == kStatus_CAAM_Again);
  1987. if (status != 0)
  1988. {
  1989. return status;
  1990. }
  1991. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  1992. }
  1993. /*!
  1994. * brief Decrypts AES and authenticates using CCM block mode.
  1995. *
  1996. * Decrypts AES and optionally authenticates using CCM block mode.
  1997. *
  1998. * param base CAAM peripheral base address
  1999. * param handle Handle used for this request. Specifies jobRing.
  2000. * param ciphertext Input cipher text to decrypt
  2001. * param[out] plaintext Output plain text.
  2002. * param size Size of input and output data in bytes. Zero means authentication data only.
  2003. * param iv Nonce
  2004. * param ivSize Length of the Nonce in bytes. Must be 7, 8, 9, 10, 11, 12, or 13.
  2005. * param aad Input additional authentication data. Can be NULL if aadSize is zero.
  2006. * param aadSize Input size in bytes of AAD. Zero means data mode only (authentication data skipped).
  2007. * param key Input key to use for decryption
  2008. * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
  2009. * param tag Received tag. Set to NULL to skip tag processing.
  2010. * param tagSize Input size of the received tag to compare with the computed tag, in bytes. Must be 4, 6, 8, 10, 12,
  2011. * 14, or 16.
  2012. * return Status from decrypt operation
  2013. */
  2014. status_t CAAM_AES_DecryptTagCcm(CAAM_Type *base,
  2015. caam_handle_t *handle,
  2016. const uint8_t *ciphertext,
  2017. uint8_t *plaintext,
  2018. size_t size,
  2019. const uint8_t *iv,
  2020. size_t ivSize,
  2021. const uint8_t *aad,
  2022. size_t aadSize,
  2023. const uint8_t *key,
  2024. size_t keySize,
  2025. const uint8_t *tag,
  2026. size_t tagSize)
  2027. {
  2028. caam_desc_aes_ccm_t descBuf;
  2029. status_t status;
  2030. do
  2031. {
  2032. status = CAAM_AES_DecryptTagCcmNonBlocking(base, handle, descBuf, ciphertext, plaintext, size, iv, ivSize, aad,
  2033. aadSize, key, keySize, tag, tagSize);
  2034. } while (status == kStatus_CAAM_Again);
  2035. if (status != 0)
  2036. {
  2037. return status;
  2038. }
  2039. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  2040. }
  2041. /*!
  2042. * brief Encrypts AES and tags using GCM block mode.
  2043. *
  2044. * Encrypts AES and optionally tags using GCM block mode. If plaintext is NULL, only the GHASH is calculated and output
  2045. * in the 'tag' field.
  2046. *
  2047. * param base CAAM peripheral base address
  2048. * param handle Handle used for this request. Specifies jobRing.
  2049. * param plaintext Input plain text to encrypt
  2050. * param[out] ciphertext Output cipher text.
  2051. * param size Size of input and output data in bytes
  2052. * param iv Input initial vector
  2053. * param ivSize Size of the IV
  2054. * param aad Input additional authentication data
  2055. * param aadSize Input size in bytes of AAD
  2056. * param key Input key to use for encryption
  2057. * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
  2058. * param[out] tag Output hash tag. Set to NULL to skip tag processing.
  2059. * param tagSize Input size of the tag to generate, in bytes. Must be 4,8,12,13,14,15 or 16.
  2060. * return Status from encrypt operation
  2061. */
  2062. status_t CAAM_AES_EncryptTagGcm(CAAM_Type *base,
  2063. caam_handle_t *handle,
  2064. const uint8_t *plaintext,
  2065. uint8_t *ciphertext,
  2066. size_t size,
  2067. const uint8_t *iv,
  2068. size_t ivSize,
  2069. const uint8_t *aad,
  2070. size_t aadSize,
  2071. const uint8_t *key,
  2072. size_t keySize,
  2073. uint8_t *tag,
  2074. size_t tagSize)
  2075. {
  2076. caam_desc_aes_gcm_t descBuf;
  2077. status_t status;
  2078. do
  2079. {
  2080. status = CAAM_AES_EncryptTagGcmNonBlocking(base, handle, descBuf, plaintext, ciphertext, size, iv, ivSize, aad,
  2081. aadSize, key, keySize, tag, tagSize);
  2082. } while (status == kStatus_CAAM_Again);
  2083. if (status != 0)
  2084. {
  2085. return status;
  2086. }
  2087. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  2088. }
  2089. /*!
  2090. * brief Decrypts AES and authenticates using GCM block mode.
  2091. *
  2092. * Decrypts AES and optionally authenticates using GCM block mode. If ciphertext is NULL, only the GHASH is calculated
  2093. * and compared with the received GHASH in 'tag' field.
  2094. *
  2095. * param base CAAM peripheral base address
  2096. * param handle Handle used for this request. Specifies jobRing.
  2097. * param ciphertext Input cipher text to decrypt
  2098. * param[out] plaintext Output plain text.
  2099. * param size Size of input and output data in bytes
  2100. * param iv Input initial vector
  2101. * param ivSize Size of the IV
  2102. * param aad Input additional authentication data
  2103. * param aadSize Input size in bytes of AAD
  2104. * param key Input key to use for encryption
  2105. * param keySize Size of the input key, in bytes. Must be 16, 24, or 32.
  2106. * param tag Input hash tag to compare. Set to NULL to skip tag processing.
  2107. * param tagSize Input size of the tag, in bytes. Must be 4, 8, 12, 13, 14, 15, or 16.
  2108. * return Status from decrypt operation
  2109. */
  2110. status_t CAAM_AES_DecryptTagGcm(CAAM_Type *base,
  2111. caam_handle_t *handle,
  2112. const uint8_t *ciphertext,
  2113. uint8_t *plaintext,
  2114. size_t size,
  2115. const uint8_t *iv,
  2116. size_t ivSize,
  2117. const uint8_t *aad,
  2118. size_t aadSize,
  2119. const uint8_t *key,
  2120. size_t keySize,
  2121. const uint8_t *tag,
  2122. size_t tagSize)
  2123. {
  2124. caam_desc_aes_gcm_t descBuf;
  2125. status_t status;
  2126. do
  2127. {
  2128. status = CAAM_AES_DecryptTagGcmNonBlocking(base, handle, descBuf, ciphertext, plaintext, size, iv, ivSize, aad,
  2129. aadSize, key, keySize, tag, tagSize);
  2130. } while (status == kStatus_CAAM_Again);
  2131. if (status != 0)
  2132. {
  2133. return status;
  2134. }
  2135. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  2136. }
  2137. /*******************************************************************************
  2138. * HASH Code static
  2139. ******************************************************************************/
  2140. static status_t caam_hash_check_input_alg(caam_hash_algo_t algo)
  2141. {
  2142. if ((algo != kCAAM_XcbcMac) && (algo != kCAAM_Cmac) && (algo != kCAAM_Sha1) && (algo != kCAAM_Sha224) &&
  2143. (algo != kCAAM_Sha256) && (algo != kCAAM_Sha384) && (algo != kCAAM_Sha512))
  2144. {
  2145. return kStatus_InvalidArgument;
  2146. }
  2147. return kStatus_Success;
  2148. }
  2149. static inline bool caam_hash_alg_is_cmac(caam_hash_algo_t algo)
  2150. {
  2151. return ((algo == kCAAM_XcbcMac) || (algo == kCAAM_Cmac));
  2152. }
  2153. static inline bool caam_hash_alg_is_sha(caam_hash_algo_t algo)
  2154. {
  2155. return ((algo == kCAAM_Sha1) || (algo == kCAAM_Sha224) || (algo == kCAAM_Sha256) || (algo == kCAAM_Sha384) ||
  2156. (algo == kCAAM_Sha512));
  2157. }
  2158. static status_t caam_hash_check_input_args(
  2159. CAAM_Type *base, caam_hash_ctx_t *ctx, caam_hash_algo_t algo, const uint8_t *key, uint32_t keySize)
  2160. {
  2161. /* Check validity of input algorithm */
  2162. if (kStatus_Success != caam_hash_check_input_alg(algo))
  2163. {
  2164. return kStatus_InvalidArgument;
  2165. }
  2166. if ((NULL == ctx) || (NULL == base))
  2167. {
  2168. return kStatus_InvalidArgument;
  2169. }
  2170. if (caam_hash_alg_is_cmac(algo))
  2171. {
  2172. if ((NULL == key) || (!caam_check_key_size(keySize)))
  2173. {
  2174. return kStatus_InvalidArgument;
  2175. }
  2176. }
  2177. return kStatus_Success;
  2178. }
  2179. static status_t caam_hash_check_context(caam_hash_ctx_internal_t *ctxInternal, const uint8_t *data)
  2180. {
  2181. if ((NULL == data) || (NULL == ctxInternal) || (NULL == ctxInternal->base) ||
  2182. (kStatus_Success != caam_hash_check_input_alg(ctxInternal->algo)))
  2183. {
  2184. return kStatus_InvalidArgument;
  2185. }
  2186. return kStatus_Success;
  2187. }
  2188. static uint32_t caam_hash_algo2mode(caam_hash_algo_t algo, uint32_t algState, uint32_t *algOutSize)
  2189. {
  2190. uint32_t modeReg = 0u;
  2191. uint32_t outSize = 0u;
  2192. /* Set CAAM algorithm */
  2193. switch (algo)
  2194. {
  2195. case kCAAM_XcbcMac:
  2196. modeReg = (uint32_t)kCAAM_AlgorithmAES | (uint32_t)kCAAM_ModeXCBCMAC;
  2197. outSize = 16u;
  2198. break;
  2199. case kCAAM_Cmac:
  2200. modeReg = (uint32_t)kCAAM_AlgorithmAES | (uint32_t)kCAAM_ModeCMAC;
  2201. outSize = 16u;
  2202. break;
  2203. case kCAAM_Sha1:
  2204. modeReg = (uint32_t)kCAAM_AlgorithmSHA1;
  2205. outSize = (uint32_t)kCAAM_OutLenSha1;
  2206. break;
  2207. case kCAAM_Sha224:
  2208. modeReg = (uint32_t)kCAAM_AlgorithmSHA224;
  2209. outSize = (uint32_t)kCAAM_OutLenSha224;
  2210. break;
  2211. case kCAAM_Sha256:
  2212. modeReg = (uint32_t)kCAAM_AlgorithmSHA256;
  2213. outSize = (uint32_t)kCAAM_OutLenSha256;
  2214. break;
  2215. case kCAAM_Sha384:
  2216. modeReg = (uint32_t)kCAAM_AlgorithmSHA384;
  2217. outSize = (uint32_t)kCAAM_OutLenSha384;
  2218. break;
  2219. case kCAAM_Sha512:
  2220. modeReg = (uint32_t)kCAAM_AlgorithmSHA512;
  2221. outSize = (uint32_t)kCAAM_OutLenSha512;
  2222. break;
  2223. default:
  2224. /* All the cases have been listed above, the default clause should not be reached. */
  2225. break;
  2226. }
  2227. modeReg |= algState;
  2228. if (algOutSize != NULL)
  2229. {
  2230. *algOutSize = outSize;
  2231. }
  2232. return modeReg;
  2233. }
  2234. static uint32_t caam_hash_algo2ctx_size(caam_hash_algo_t algo, uint32_t how)
  2235. {
  2236. uint32_t ctxSize = 0u;
  2237. /* Size of context in bytes for context switching */
  2238. switch (algo)
  2239. {
  2240. case kCAAM_XcbcMac:
  2241. if (how == 0U)
  2242. {
  2243. ctxSize = 48u; /* add K3 and K2 */
  2244. }
  2245. else
  2246. {
  2247. ctxSize = 16u; /* only running or final MAC during UPDATE or FINALIZE or INITIALIZE/FINALIZE */
  2248. }
  2249. break;
  2250. case kCAAM_Cmac:
  2251. if (how == 0U)
  2252. {
  2253. ctxSize = 32u; /* add L */
  2254. }
  2255. else
  2256. {
  2257. ctxSize = 16u; /* only running or final MAC during UPDATE or FINALIZE or INITIALIZE/FINALIZE */
  2258. }
  2259. break;
  2260. /* MDHA use of the Context Register
  2261. The Context Register stores the current digest and running message length. The running
  2262. message length will be 8 bytes immediately following the active digest. The digest size is
  2263. defined as follows:
  2264. MD5: 16 bytes
  2265. SHA-1: 20 bytes
  2266. SHA-224: 28 bytes final digest; 32 bytes running digest
  2267. SHA-256: 32 bytes
  2268. SHA-384: 48 bytes final digest; 64 bytes running digest
  2269. SHA-512: 64 bytes */
  2270. case kCAAM_Sha1:
  2271. ctxSize = 28u; /* 8 + 20 */
  2272. break;
  2273. case kCAAM_Sha224:
  2274. case kCAAM_Sha256:
  2275. ctxSize = 40u; /* 8 + 32 */
  2276. break;
  2277. case kCAAM_Sha384:
  2278. case kCAAM_Sha512:
  2279. ctxSize = 72u; /* 8 + 64 */
  2280. break;
  2281. default:
  2282. /* All the cases have been listed above, the default clause should not be reached. */
  2283. break;
  2284. }
  2285. return ctxSize;
  2286. }
  2287. static const uint32_t templateHash[] = {
  2288. /* 00 */ 0xB0800000u, /* HEADER */
  2289. /* 01 */ 0x00000000u, /* KEY */
  2290. /* 02 */ 0x00000000u, /* place: key address */
  2291. /* 03 */ 0x10200000u, /* LOAD bytes to Class Context Register. Offset 0 bytes. */
  2292. /* 04 */ 0x00000000u, /* place: context address */
  2293. /* 05 */ 0x80000000u, /* OPERATION (place either AES MAC or MDHA SHA) */
  2294. /* 06 */ 0x21570000u, /* FIFO LOAD Class Message via SGT and EXT length */
  2295. /* 07 */ 0x00000000u, /* place: SGT address */
  2296. /* 08 */ 0x00000000u, /* place: FIFO LOAD EXT Length size */
  2297. /* 09 */ 0x50200000u, /* STORE bytes from Class Context Register offset 0 bytes. */
  2298. /* 10 */ 0x00000000u, /* place: context address */
  2299. /* 11 */ 0x60240000u, /* FIFO STORE from KEY to memory. */
  2300. /* 12 */ 0x00000000u, /* place: derived key address ECB encrypted */
  2301. /* 13 */ 0xA0C00000u, /* halt always with status 0 */
  2302. /* 14 */ 0x00000000u, /* SGT entry 0 word 0 */
  2303. /* 15 */ 0x00000000u, /* SGT entry 0 word 1 */
  2304. /* 16 */ 0x00000000u, /* SGT entry 0 word 2 */
  2305. /* 17 */ 0x00000000u, /* SGT entry 0 word 3 */
  2306. /* 18 */ 0x00000000u, /* SGT entry 1 word 0 */
  2307. /* 19 */ 0x00000000u, /* SGT entry 1 word 1 */
  2308. /* 20 */ 0x00000000u, /* SGT entry 1 word 2 */
  2309. /* 21 */ 0x00000000u, /* SGT entry 1 word 3 */
  2310. };
  2311. /*!
  2312. * @brief Add data chunk to SGT table. Append after uncomplete block in ctxInternal if there is any.
  2313. *
  2314. * @param ctxInternal uncomplete block in the hash context - to be inserted before new data chunk
  2315. * @param input new data chunk to insert
  2316. * @param inputSize size in bytes of new data chunk to insert
  2317. * @param numRemain number of bytes that remain in the last uncomplete block
  2318. * @param algState in FINALIZE or INITIALIZE/FINALIZE we add also last uncomplete block bytes
  2319. * @param sgt address of the SGT
  2320. * @param last last call to this function adds Final Bit
  2321. */
  2322. static uint32_t caam_hash_sgt_insert(caam_hash_ctx_internal_t *ctxInternal,
  2323. const uint8_t *input,
  2324. size_t inputSize,
  2325. size_t *numRemain,
  2326. caam_algorithm_state_t algState,
  2327. caam_sgt_entry_t *sgt,
  2328. caam_hash_sgt_entry_type_t last)
  2329. {
  2330. /* configure SGT
  2331. * *64 bytes multiple in kCAAM_HashInit or kCAAM_HashUpdate
  2332. * *arbitrary amount of data in kCAAM_HashInitFinal or kCAAM_HashFinal
  2333. * min 1 and max 2 SGT entries
  2334. * 1) if there is any data in the context buffer, use it as one entry
  2335. * 2) input as one entry
  2336. */
  2337. uint32_t numBlocks;
  2338. uint32_t remain;
  2339. uint32_t num;
  2340. uint32_t currSgtEntry;
  2341. uint32_t ctxBlksz = (ctxInternal != NULL) ? ctxInternal->blksz : 0U;
  2342. uint32_t ctxBlkAddr = (ctxInternal != NULL) ? (uint32_t)&ctxInternal->blk.b[0] : 0U;
  2343. currSgtEntry = 0;
  2344. numBlocks = (inputSize + ctxBlksz) / CAAM_HASH_BLOCK_SIZE;
  2345. remain = (inputSize + ctxBlksz) % CAAM_HASH_BLOCK_SIZE;
  2346. /* number of bytes for processing
  2347. * only full block multiple in INITIALIZE or UPDATE
  2348. * any size in INITIALIZE/FINALIZE or FINALIZE
  2349. */
  2350. num = (CAAM_HASH_BLOCK_SIZE * numBlocks);
  2351. if ((algState == kCAAM_AlgStateInitFinal) || (algState == kCAAM_AlgStateFinal))
  2352. {
  2353. num += remain; /* add also uncomplete bytes from last block in one of FINALIZE states */
  2354. remain = 0;
  2355. }
  2356. if (numRemain != NULL)
  2357. {
  2358. *numRemain = remain;
  2359. }
  2360. if ((ctxBlksz != 0U) || (0U == ctxBlksz + inputSize))
  2361. {
  2362. sgt[currSgtEntry].address_l = ADD_OFFSET(ctxBlkAddr);
  2363. sgt[currSgtEntry].length = ctxBlksz;
  2364. if ((kCAAM_HashSgtEntryLast == last) && (0U == inputSize))
  2365. {
  2366. sgt[currSgtEntry].length |= 0x40000000u; /* Final SG entry */
  2367. }
  2368. currSgtEntry++;
  2369. }
  2370. if (inputSize != 0U)
  2371. {
  2372. /* number of bytes for processing
  2373. * only full block multiple in INITIALIZE or UPDATE
  2374. * any size in INITIALIZE/FINALIZE or FINALIZE
  2375. */
  2376. sgt[currSgtEntry].address_l = ADD_OFFSET((uint32_t)input);
  2377. sgt[currSgtEntry].length = inputSize - remain;
  2378. if (kCAAM_HashSgtEntryLast == last)
  2379. {
  2380. sgt[currSgtEntry].length |= 0x40000000u; /* Final SG entry */
  2381. sgt[currSgtEntry].offset = 0x80000000u;
  2382. }
  2383. }
  2384. return num; /* no of bytes processed in total by these 1 or 2 SGT entries */
  2385. }
  2386. /*!
  2387. * @brief Create job descriptor for the HASH request and schedule at CAAM job ring
  2388. *
  2389. *
  2390. */
  2391. static status_t caam_hash_schedule_input_data(CAAM_Type *base,
  2392. caam_handle_t *handle,
  2393. caam_hash_algo_t algo,
  2394. caam_sgt_entry_t *sgt,
  2395. uint32_t dataSize,
  2396. caam_hash_sgt_type_t sgtType,
  2397. caam_algorithm_state_t algState,
  2398. caam_desc_hash_t descriptor,
  2399. size_t *outputSize,
  2400. void *output,
  2401. void *context,
  2402. uint32_t keyAddr,
  2403. uint32_t keySize)
  2404. {
  2405. BUILD_ASSURE(sizeof(templateHash) <= sizeof(caam_desc_hash_t), caam_desc_hash_t_size);
  2406. uint32_t descriptorSize = ARRAY_SIZE(templateHash);
  2407. uint32_t algOutSize = 0;
  2408. bool isSha = caam_hash_alg_is_sha(algo); /* MDHA engine */
  2409. /* how many bytes to read from context register
  2410. * we need caam_hash_algo2ctx_size() to return
  2411. * full context size (to be used for context restore in descriptor[3])
  2412. */
  2413. uint32_t caamCtxSz = caam_hash_algo2ctx_size(algo, 0 /* full context */);
  2414. (void)caam_memcpy(descriptor, templateHash, sizeof(templateHash));
  2415. /* MDHA is always Class 2 CHA, AESA configured at build time as Class 1 CHA */
  2416. uint32_t hashClass = isSha ? 0x04000000u : CAAM_AES_MAC_CLASS;
  2417. /* add class to all commands that need it */
  2418. descriptor[1] |= hashClass;
  2419. descriptor[3] |= hashClass;
  2420. descriptor[5] |= hashClass;
  2421. descriptor[6] |= hashClass;
  2422. descriptor[9] |= hashClass;
  2423. descriptor[11] |= hashClass;
  2424. /* add descriptor size */
  2425. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  2426. /* kCAAM_AlgStateInit or kCAAM_AlgStateInitFinal needs to skip context load as there is no context */
  2427. if ((algState == kCAAM_AlgStateInit) || (algState == kCAAM_AlgStateInitFinal))
  2428. {
  2429. if (isSha)
  2430. {
  2431. /* HEADER can jump directly to MDHA operation */
  2432. descriptor[0] |= 0x00050000U; /* JUMP to descriptor[5] */
  2433. }
  2434. else
  2435. {
  2436. /* load KEY, then directly to AESA MAC operation */
  2437. descriptor[1] |= (keySize & DESC_KEY_SIZE_MASK);
  2438. descriptor[2] = ADD_OFFSET(keyAddr);
  2439. descriptor[3] = DESC_JUMP_2; /* JUMP to descriptor[5] */
  2440. }
  2441. }
  2442. else
  2443. {
  2444. if (isSha)
  2445. {
  2446. /* MDHA SHA in Update state skips loading the KEY, as MDHA SHA has no configurable key
  2447. * HEADER can jump directly to context restore
  2448. */
  2449. descriptor[0] |= 0x00030000U; /* JUMP to descriptor[3] */
  2450. /* descriptor[1] = 0xA0000002u; */ /* JUMP to descriptor[3] */
  2451. }
  2452. else
  2453. {
  2454. /* load KEY */
  2455. descriptor[1] |= (keySize & DESC_KEY_SIZE_MASK);
  2456. descriptor[2] = ADD_OFFSET(keyAddr);
  2457. /* XCBC-MAC K1 derived key has been ECB encrypted (black key)
  2458. * so it needs decrypt
  2459. */
  2460. if (kCAAM_XcbcMac == algo)
  2461. {
  2462. descriptor[1] |= (uint32_t)1u << 22; /* ENC */
  2463. }
  2464. }
  2465. /* context restore */
  2466. descriptor[3] |= caamCtxSz;
  2467. descriptor[4] = ADD_OFFSET((uint32_t)(uint32_t *)context);
  2468. }
  2469. /* OPERATION:
  2470. * alg MDHA or AESA
  2471. * mode INITIALIZE or UPDATE or FINALIZE or INITIALIZE/FINALIZE in algState argument
  2472. */
  2473. /* ALGORITHM OPERATION | CLASS | alg | aai | algState */
  2474. descriptor[5] |= caam_hash_algo2mode(algo, (uint32_t)algState << 2, &algOutSize);
  2475. /* configure SGT */
  2476. descriptor[8] = dataSize;
  2477. if (kCAAM_HashSgtInternal == sgtType)
  2478. {
  2479. descriptor[7] = ADD_OFFSET(
  2480. (uint32_t)&descriptor[(uint32_t)kCAAM_HashDescriptorSgtIdx]); /* use SGT embedded in the job descriptor */
  2481. (void)caam_memcpy(&descriptor[(uint32_t)kCAAM_HashDescriptorSgtIdx], (const uint32_t *)(uintptr_t)sgt,
  2482. sizeof(caam_hash_internal_sgt_t));
  2483. }
  2484. else
  2485. {
  2486. descriptor[7] = ADD_OFFSET((uint32_t)sgt);
  2487. }
  2488. /* save context: context switch init or running or result */
  2489. if ((kCAAM_AlgStateFinal == algState) || (kCAAM_AlgStateInitFinal == algState))
  2490. {
  2491. if (outputSize != NULL)
  2492. {
  2493. if (algOutSize < *outputSize)
  2494. {
  2495. *outputSize = algOutSize;
  2496. }
  2497. else
  2498. {
  2499. algOutSize = *outputSize;
  2500. }
  2501. }
  2502. caamCtxSz = algOutSize;
  2503. }
  2504. else
  2505. {
  2506. uint32_t how = (algState == kCAAM_AlgStateInit) ? 0U : 1U; /* context switch needs full, then running */
  2507. caamCtxSz = caam_hash_algo2ctx_size(algo, how);
  2508. }
  2509. descriptor[9] |= caamCtxSz;
  2510. if ((kCAAM_AlgStateFinal == algState) || (kCAAM_AlgStateInitFinal == algState))
  2511. {
  2512. /* final result write to output */
  2513. descriptor[10] = ADD_OFFSET((uint32_t)(uint32_t *)output);
  2514. }
  2515. else
  2516. {
  2517. /* context switch write to ctxInternal */
  2518. descriptor[10] = ADD_OFFSET((uint32_t)(uint32_t *)context);
  2519. }
  2520. /* save the derived key K1 in XCBC-MAC. only if context switch. */
  2521. if ((kCAAM_AlgStateInit == algState) && (kCAAM_XcbcMac == algo))
  2522. {
  2523. descriptor[11] |= (keySize & DESC_KEY_SIZE_MASK);
  2524. descriptor[12] = ADD_OFFSET((uint32_t)&keyAddr);
  2525. }
  2526. else
  2527. {
  2528. descriptor[11] = ADD_OFFSET(descriptor[13]); /* always halt with status 0x0 (normal) */
  2529. }
  2530. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  2531. }
  2532. /*!
  2533. * @brief Add uncomplete block (ctxInternal), then append new data (to current hash).
  2534. *
  2535. *
  2536. */
  2537. static status_t caam_hash_append_data(caam_hash_ctx_internal_t *ctxInternal,
  2538. const uint8_t *input,
  2539. size_t inputSize,
  2540. caam_algorithm_state_t algState,
  2541. caam_desc_hash_t descriptor,
  2542. size_t *numRemain,
  2543. void *output,
  2544. size_t *outputSize)
  2545. {
  2546. caam_hash_internal_sgt_t sgt;
  2547. (void)memset(&sgt, 0, sizeof(sgt));
  2548. size_t num = caam_hash_sgt_insert(ctxInternal, input, inputSize, numRemain, algState, sgt, kCAAM_HashSgtEntryLast);
  2549. return caam_hash_schedule_input_data(ctxInternal->base, ctxInternal->handle, ctxInternal->algo, sgt, num,
  2550. kCAAM_HashSgtInternal, algState, descriptor, outputSize, output,
  2551. &ctxInternal->word[0], (uint32_t)&ctxInternal->word[kCAAM_HashCtxKeyStartIdx],
  2552. ctxInternal->word[kCAAM_HashCtxKeySize]);
  2553. }
  2554. /*!
  2555. * brief Initialize HASH context
  2556. *
  2557. * This function initializes the HASH.
  2558. * Key shall be supplied if the underlaying algoritm is AES XCBC-MAC or CMAC.
  2559. * Key shall be NULL if the underlaying algoritm is SHA.
  2560. *
  2561. * For XCBC-MAC, the key length must be 16. For CMAC, the key length can be
  2562. * the AES key lengths supported by AES engine. For MDHA the key length argument
  2563. * is ignored.
  2564. *
  2565. * This functions is used to initialize the context for both blocking and non-blocking
  2566. * CAAM_HASH API.
  2567. * For blocking CAAM HASH API, the HASH context contains all information required for context switch,
  2568. * such as running hash or MAC. For non-blocking CAAM HASH API, the HASH context is used
  2569. * to hold SGT. Therefore, the HASH context cannot be shared between blocking and non-blocking HASH API.
  2570. * With one HASH context, either use only blocking HASH API or only non-blocking HASH API.
  2571. *
  2572. *
  2573. * param base CAAM peripheral base address
  2574. * param handle Handle used for this request.
  2575. * param[out] ctx Output hash context
  2576. * param algo Underlaying algorithm to use for hash computation.
  2577. * param key Input key (NULL if underlaying algorithm is SHA)
  2578. * param keySize Size of input key in bytes
  2579. * return Status of initialization
  2580. */
  2581. status_t CAAM_HASH_Init(CAAM_Type *base,
  2582. caam_handle_t *handle,
  2583. caam_hash_ctx_t *ctx,
  2584. caam_hash_algo_t algo,
  2585. const uint8_t *key,
  2586. size_t keySize)
  2587. {
  2588. status_t ret;
  2589. caam_hash_ctx_internal_t *ctxInternal;
  2590. uint32_t i;
  2591. ret = caam_hash_check_input_args(base, ctx, algo, key, keySize);
  2592. if (ret != kStatus_Success)
  2593. {
  2594. return ret;
  2595. }
  2596. /* set algorithm in context struct for later use */
  2597. ctxInternal = (caam_hash_ctx_internal_t *)(uint32_t)ctx;
  2598. ctxInternal->algo = algo;
  2599. for (i = 0U; i < (uint32_t)kCAAM_HashCtxNumWords; i++)
  2600. {
  2601. ctxInternal->word[i] = 0u;
  2602. }
  2603. /* Steps required only using AES engine */
  2604. if (caam_hash_alg_is_cmac(algo))
  2605. {
  2606. /* store input key and key length in context struct for later use */
  2607. ctxInternal->word[kCAAM_HashCtxKeySize] = keySize;
  2608. (void)caam_memcpy(&ctxInternal->word[kCAAM_HashCtxKeyStartIdx], (const uint32_t *)(uintptr_t)key, keySize);
  2609. }
  2610. ctxInternal->blksz = 0u;
  2611. for (i = 0; i < ARRAY_SIZE(ctxInternal->blk.w); i++)
  2612. {
  2613. ctxInternal->blk.w[i] = 0u;
  2614. }
  2615. ctxInternal->state = kCAAM_HashInit;
  2616. ctxInternal->base = base;
  2617. ctxInternal->handle = handle;
  2618. return kStatus_Success;
  2619. }
  2620. /*!
  2621. * brief Add data to current HASH
  2622. *
  2623. * Add data to current HASH. This can be called repeatedly with an arbitrary amount of data to be
  2624. * hashed. The functions blocks. If it returns kStatus_Success, the running hash or mac
  2625. * has been updated (CAAM has processed the input data), so the memory at input pointer
  2626. * can be released back to system. The context is updated with the running hash or mac
  2627. * and with all necessary information to support possible context switch.
  2628. *
  2629. * param[in,out] ctx HASH context
  2630. * param input Input data
  2631. * param inputSize Size of input data in bytes
  2632. * return Status of the hash update operation
  2633. */
  2634. status_t CAAM_HASH_Update(caam_hash_ctx_t *ctx, const uint8_t *input, size_t inputSize)
  2635. {
  2636. caam_desc_hash_t descBuf;
  2637. status_t status;
  2638. caam_hash_ctx_internal_t *ctxInternal;
  2639. bool isUpdateState;
  2640. size_t numRemain = 0;
  2641. /* compile time check for the correct structure size */
  2642. BUILD_ASSURE(sizeof(caam_hash_ctx_internal_t) <= sizeof(caam_hash_ctx_t), caam_hash_ctx_internal_t_size);
  2643. if (0U == inputSize)
  2644. {
  2645. return kStatus_Success;
  2646. }
  2647. /* we do caam_memcpy() input stream, up to buffer size
  2648. * of 64 bytes. then if I have more I have to
  2649. * 1) load Class 2 context
  2650. * 2) schedule CAAM job with INITIALIZE or UPDATE mode (simple if only 64 bytes block is processed. SG table for 2
  2651. * and more)
  2652. * 3) in step 2 process all full 64 bytes blocks
  2653. * 4) copy last not-full buffer size data to buffer.
  2654. * 5) save Class 2 context
  2655. */
  2656. ctxInternal = (caam_hash_ctx_internal_t *)(uint32_t)ctx;
  2657. status = caam_hash_check_context(ctxInternal, input);
  2658. if (kStatus_Success != status)
  2659. {
  2660. return status;
  2661. }
  2662. /* if we are still less than 64 bytes, keep only in context */
  2663. if ((ctxInternal->blksz + inputSize) <= CAAM_HASH_BLOCK_SIZE)
  2664. {
  2665. (void)caam_memcpy((&ctxInternal->blk.b[0]) + ctxInternal->blksz, input, inputSize);
  2666. ctxInternal->blksz += inputSize;
  2667. return status;
  2668. }
  2669. else
  2670. {
  2671. isUpdateState = ctxInternal->state == kCAAM_HashUpdate;
  2672. if (!isUpdateState)
  2673. {
  2674. /* Step 2: schedule CAAM job in INITIALIZE mode.
  2675. */
  2676. ctxInternal->state = kCAAM_HashUpdate;
  2677. /* skip load context as there is no running context yet. */
  2678. status = caam_hash_append_data(ctxInternal, input, inputSize, kCAAM_AlgStateInit, descBuf, &numRemain, NULL,
  2679. NULL);
  2680. }
  2681. }
  2682. if (kStatus_Success != status)
  2683. {
  2684. return status;
  2685. }
  2686. if (isUpdateState)
  2687. {
  2688. /* Step 2: schedule CAAM job in UPDATE mode.
  2689. */
  2690. /* process input data and save CAAM context to context structure */
  2691. status =
  2692. caam_hash_append_data(ctxInternal, input, inputSize, kCAAM_AlgStateUpdate, descBuf, &numRemain, NULL, NULL);
  2693. if (status != kStatus_Success)
  2694. {
  2695. return status;
  2696. }
  2697. }
  2698. /* blocking wait */
  2699. status = CAAM_Wait(ctxInternal->base, ctxInternal->handle, descBuf, kCAAM_Blocking);
  2700. if (status != kStatus_Success)
  2701. {
  2702. return status;
  2703. }
  2704. /* after job is complete, copy numRemain bytes at the end of the input[] to the context */
  2705. (void)caam_memcpy((&ctxInternal->blk.b[0]), input + inputSize - numRemain, numRemain);
  2706. ctxInternal->blksz = numRemain;
  2707. return status;
  2708. }
  2709. /*!
  2710. * brief Add input address and size to input data table
  2711. *
  2712. * Add data input pointer to a table maintained internally in the context.
  2713. * Each call of this function creates one entry in the table.
  2714. * The entry consists of the input pointer and inputSize.
  2715. * All entries created by one or multiple calls of this function can be processed
  2716. * in one call to CAAM_HASH_FinishNonBlocking() function.
  2717. * Individual entries can point to non-continuous data in the memory.
  2718. * The processing will occur in the order in which the CAAM_HASH_UpdateNonBlocking()
  2719. * have been called.
  2720. *
  2721. * Memory pointers will be later accessed by CAAM (at time of CAAM_HASH_FinishNonBlocking()),
  2722. * so the memory must stay valid
  2723. * until CAAM_HASH_FinishNonBlocking() has been called and CAAM completes the processing.
  2724. *
  2725. * param[in,out] ctx HASH context
  2726. * param input Input data
  2727. * param inputSize Size of input data in bytes
  2728. * return Status of the hash update operation
  2729. */
  2730. status_t CAAM_HASH_UpdateNonBlocking(caam_hash_ctx_t *ctx, const uint8_t *input, size_t inputSize)
  2731. {
  2732. status_t status;
  2733. caam_hash_ctx_internal_t *ctxInternal;
  2734. if (0U == inputSize)
  2735. {
  2736. return kStatus_Success;
  2737. }
  2738. /* runtime input validity check */
  2739. ctxInternal = (caam_hash_ctx_internal_t *)(uint32_t)ctx;
  2740. status = caam_hash_check_context(ctxInternal, input);
  2741. if (kStatus_Success != status)
  2742. {
  2743. return status;
  2744. }
  2745. /* Add input data chunk to SGT */
  2746. uint32_t currSgtEntry = ctxInternal->blksz;
  2747. if (currSgtEntry >= (uint32_t)kCAAM_HashSgtMaxCtxEntries)
  2748. {
  2749. return kStatus_InvalidArgument;
  2750. }
  2751. caam_sgt_entry_t *sgt = &((caam_sgt_entry_t *)(uint32_t)ctxInternal)[currSgtEntry];
  2752. (void)caam_hash_sgt_insert(NULL, input, inputSize, NULL, kCAAM_AlgStateInitFinal, sgt,
  2753. kCAAM_HashSgtEntryNotLast /* not last. we don't know if this is the last chunk */);
  2754. if (inputSize != 0U)
  2755. {
  2756. ctxInternal->blksz++;
  2757. }
  2758. return status;
  2759. }
  2760. /*!
  2761. * brief Finalize hashing
  2762. *
  2763. * Outputs the final hash (computed by CAAM_HASH_Update()) and erases the context.
  2764. *
  2765. * param[in,out] ctx Input hash context
  2766. * param[out] output Output hash data
  2767. * param[out] outputSize Output parameter storing the size of the output hash in bytes
  2768. * return Status of the hash finish operation
  2769. */
  2770. status_t CAAM_HASH_Finish(caam_hash_ctx_t *ctx, uint8_t *output, size_t *outputSize)
  2771. {
  2772. status_t status;
  2773. caam_hash_ctx_internal_t *ctxInternal;
  2774. caam_desc_hash_t descBuf;
  2775. caam_algorithm_state_t algState;
  2776. /* runtime input validity check */
  2777. ctxInternal = (caam_hash_ctx_internal_t *)(uint32_t)ctx;
  2778. status = caam_hash_check_context(ctxInternal, output);
  2779. if (kStatus_Success != status)
  2780. {
  2781. return status;
  2782. }
  2783. /* determine algorithm state to configure
  2784. * based on prior processing.
  2785. * If at least one full block has been processed during HASH_Update() then the state in ctxInternal
  2786. * will be set to kCAAM_HashUpdate and so we will configure FINALIZE algorithm state.
  2787. * Otherwise there is data only in the ctxInternal that we can process in INITIALIZE/FINALIZE.
  2788. */
  2789. if (ctxInternal->state == kCAAM_HashInit)
  2790. {
  2791. algState = kCAAM_AlgStateInitFinal;
  2792. }
  2793. else
  2794. {
  2795. algState = kCAAM_AlgStateFinal;
  2796. }
  2797. status = caam_hash_append_data(
  2798. ctxInternal, NULL, 0, /* we process only blksz bytes in ctxInternal, so giving NULL and zero size here */
  2799. algState, descBuf, NULL, output, outputSize);
  2800. if (kStatus_Success != status)
  2801. {
  2802. return status;
  2803. }
  2804. /* blocking wait */
  2805. status = CAAM_Wait(ctxInternal->base, ctxInternal->handle, descBuf, kCAAM_Blocking);
  2806. (void)memset(ctx, 0, sizeof(caam_hash_ctx_t));
  2807. return status;
  2808. }
  2809. /*!
  2810. * brief Finalize hashing
  2811. *
  2812. * The actual algorithm is computed with all input data, the memory pointers
  2813. * are accessed by CAAM after the function returns.
  2814. * The input data chunks have been specified by prior calls to CAAM_HASH_UpdateNonBlocking().
  2815. * The function schedules the request at CAAM, then returns.
  2816. * After a while, when the CAAM completes processing of the input data chunks,
  2817. * the result is written to the output[] array, outputSize is written and the context
  2818. * is cleared.
  2819. *
  2820. * param[in,out] ctx Input hash context
  2821. * param[out] descriptor Memory for the CAAM descriptor.
  2822. * param[out] output Output hash data
  2823. * param[out] outputSize Output parameter storing the size of the output hash in bytes
  2824. * return Status of the hash finish operation
  2825. */
  2826. status_t CAAM_HASH_FinishNonBlocking(caam_hash_ctx_t *ctx,
  2827. caam_desc_hash_t descriptor,
  2828. uint8_t *output,
  2829. size_t *outputSize)
  2830. {
  2831. status_t status;
  2832. caam_hash_ctx_internal_t *ctxInternal;
  2833. /* runtime input validity check */
  2834. ctxInternal = (caam_hash_ctx_internal_t *)(uint32_t)ctx;
  2835. status = caam_hash_check_context(ctxInternal, output);
  2836. if (kStatus_Success != status)
  2837. {
  2838. return status;
  2839. }
  2840. uint32_t currSgtEntry = ctxInternal->blksz;
  2841. if (currSgtEntry > (uint32_t)kCAAM_HashSgtMaxCtxEntries)
  2842. {
  2843. return kStatus_InvalidArgument;
  2844. }
  2845. caam_sgt_entry_t *sgt = &((caam_sgt_entry_t *)(uint32_t)ctxInternal)[0];
  2846. /* mark currSgtEntry with Final Bit */
  2847. uint32_t i;
  2848. uint32_t totalLength = 0;
  2849. for (i = 0; i < currSgtEntry; i++)
  2850. {
  2851. totalLength += sgt[i].length;
  2852. }
  2853. sgt[currSgtEntry].length |= 0x40000000u; /* Final SG entry */
  2854. status = caam_hash_schedule_input_data(ctxInternal->base, ctxInternal->handle, ctxInternal->algo, sgt, totalLength,
  2855. kCAAM_HashSgtExternal, kCAAM_AlgStateInitFinal, descriptor, outputSize,
  2856. output, NULL, (uint32_t)&ctxInternal->word[kCAAM_HashCtxKeyStartIdx],
  2857. ctxInternal->word[kCAAM_HashCtxKeySize]);
  2858. return status;
  2859. }
  2860. /*!
  2861. * brief Create HASH on given data
  2862. *
  2863. * Perform the full keyed XCBC-MAC/CMAC or SHA in one function call.
  2864. *
  2865. * Key shall be supplied if the underlaying algoritm is AES XCBC-MAC or CMAC.
  2866. * Key shall be NULL if the underlaying algoritm is SHA.
  2867. *
  2868. * For XCBC-MAC, the key length must be 16. For CMAC, the key length can be
  2869. * the AES key lengths supported by AES engine. For MDHA the key length argument
  2870. * is ignored.
  2871. *
  2872. * The function is blocking.
  2873. *
  2874. * param base CAAM peripheral base address
  2875. * param handle Handle used for this request.
  2876. * param algo Underlaying algorithm to use for hash computation.
  2877. * param input Input data
  2878. * param inputSize Size of input data in bytes
  2879. * param key Input key (NULL if underlaying algorithm is SHA)
  2880. * param keySize Size of input key in bytes
  2881. * param[out] output Output hash data
  2882. * param[out] outputSize Output parameter storing the size of the output hash in bytes
  2883. * return Status of the one call hash operation.
  2884. */
  2885. status_t CAAM_HASH(CAAM_Type *base,
  2886. caam_handle_t *handle,
  2887. caam_hash_algo_t algo,
  2888. const uint8_t *input,
  2889. size_t inputSize,
  2890. const uint8_t *key,
  2891. size_t keySize,
  2892. uint8_t *output,
  2893. size_t *outputSize)
  2894. {
  2895. status_t status;
  2896. caam_desc_hash_t descBuf;
  2897. status = CAAM_HASH_NonBlocking(base, handle, descBuf, algo, input, inputSize, key, keySize, output, outputSize);
  2898. if (kStatus_Success != status)
  2899. {
  2900. return status;
  2901. }
  2902. status = CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  2903. return status;
  2904. }
  2905. /*!
  2906. * brief Create HASH on given data
  2907. *
  2908. * Perform the full keyed XCBC-MAC/CMAC or SHA in one function call.
  2909. *
  2910. * Key shall be supplied if the underlaying algoritm is AES XCBC-MAC or CMAC.
  2911. * Key shall be NULL if the underlaying algoritm is SHA.
  2912. *
  2913. * For XCBC-MAC, the key length must be 16. For CMAC, the key length can be
  2914. * the AES key lengths supported by AES engine. For MDHA the key length argument
  2915. * is ignored.
  2916. *
  2917. * The function is non-blocking. The request is scheduled at CAAM.
  2918. *
  2919. * param base CAAM peripheral base address
  2920. * param handle Handle used for this request.
  2921. * param[out] descriptor Memory for the CAAM descriptor.
  2922. * param algo Underlaying algorithm to use for hash computation.
  2923. * param input Input data
  2924. * param inputSize Size of input data in bytes
  2925. * param key Input key (NULL if underlaying algorithm is SHA)
  2926. * param keySize Size of input key in bytes
  2927. * param[out] output Output hash data
  2928. * param[out] outputSize Output parameter storing the size of the output hash in bytes
  2929. * return Status of the one call hash operation.
  2930. */
  2931. status_t CAAM_HASH_NonBlocking(CAAM_Type *base,
  2932. caam_handle_t *handle,
  2933. caam_desc_hash_t descriptor,
  2934. caam_hash_algo_t algo,
  2935. const uint8_t *input,
  2936. size_t inputSize,
  2937. const uint8_t *key,
  2938. size_t keySize,
  2939. uint8_t *output,
  2940. size_t *outputSize)
  2941. {
  2942. status_t status;
  2943. caam_algorithm_state_t algState;
  2944. caam_hash_internal_sgt_t sgt;
  2945. (void)memset(&sgt, 0, sizeof(sgt));
  2946. algState = kCAAM_AlgStateInitFinal;
  2947. uint32_t num = caam_hash_sgt_insert(NULL, /* no ctxInternal data to pre-pend before input data chunk */
  2948. input, inputSize, /* data and size in bytes */
  2949. NULL, /* all data is processed during kCAAM_AlgStateInitFinal, nothing remain */
  2950. algState, sgt, kCAAM_HashSgtEntryLast); /* sgt table, entry 0 word 0 */
  2951. /* schedule the request at CAAM */
  2952. status = caam_hash_schedule_input_data(base, handle, algo, sgt, num, kCAAM_HashSgtInternal, algState, descriptor,
  2953. outputSize, output, NULL, (uint32_t)key, keySize);
  2954. return status;
  2955. }
  2956. /*******************************************************************************
  2957. * RNG Code public
  2958. ******************************************************************************/
  2959. /*!
  2960. * brief Initializes user configuration structure to default.
  2961. *
  2962. * This function initializes the configure structure to default value. the default
  2963. * value are:
  2964. * code
  2965. * config->autoReseedInterval = 0;
  2966. * config->personalString = NULL;
  2967. * endcode
  2968. *
  2969. * param config User configuration structure.
  2970. * return status of the request
  2971. */
  2972. status_t CAAM_RNG_GetDefaultConfig(caam_rng_config_t *config)
  2973. {
  2974. status_t status;
  2975. if (config != NULL)
  2976. {
  2977. config->autoReseedInterval = 0; /* zero means hardware default of 10.000.000 will be used */
  2978. config->personalString = NULL;
  2979. status = kStatus_Success;
  2980. }
  2981. else
  2982. {
  2983. status = kStatus_InvalidArgument;
  2984. }
  2985. return status;
  2986. }
  2987. /*!
  2988. * brief Instantiate the CAAM RNG state handle
  2989. *
  2990. * This function instantiates CAAM RNG state handle.
  2991. * The function is blocking and returns after CAAM has processed the request.
  2992. *
  2993. * param base CAAM peripheral base address
  2994. * param handle CAAM jobRing used for this request
  2995. * param stateHandle RNG state handle to instantiate
  2996. * param config Pointer to configuration structure.
  2997. * return Status of the request
  2998. */
  2999. status_t CAAM_RNG_Init(CAAM_Type *base,
  3000. caam_handle_t *handle,
  3001. caam_rng_state_handle_t stateHandle,
  3002. const caam_rng_config_t *config)
  3003. {
  3004. status_t status;
  3005. /* create job descriptor */
  3006. caam_desc_rng_t rngInstantiate = {0};
  3007. rngInstantiate[0] = 0xB0800006u;
  3008. rngInstantiate[1] = 0x12200020u; /* LOAD 32 bytes of to Class 1 Context Register. Offset 0 bytes. */
  3009. rngInstantiate[2] = (uint32_t)ADD_OFFSET((uint32_t)config->personalString);
  3010. rngInstantiate[3] = 0x12820004u; /* LOAD Immediate 4 bytes to Class 1 Data Size Register. */
  3011. rngInstantiate[4] = config->autoReseedInterval; /* value for the Class 1 Data Size Register */
  3012. rngInstantiate[5] = 0x82500006u; /* RNG instantiate state handle:
  3013. * TST=0 for normal non-deterministic mode
  3014. * PR=1 for prediction resistance
  3015. */
  3016. if (kCAAM_RngStateHandle1 == stateHandle)
  3017. {
  3018. rngInstantiate[5] |= 1u << 4;
  3019. }
  3020. /* default auto reseed interval */
  3021. if (config->autoReseedInterval == 0U)
  3022. {
  3023. rngInstantiate[3] = DESC_JUMP_2; /* jump to current index + 2 (=5) */
  3024. }
  3025. /* optional personalization string present */
  3026. if ((config->personalString) != NULL)
  3027. {
  3028. rngInstantiate[5] |= (uint32_t)1u << 10; /* set PS bit in ALG OPERATION (AS=01 Instantiate) */
  3029. }
  3030. else
  3031. {
  3032. rngInstantiate[1] = DESC_JUMP_2; /* jump to current index + 2 (=3) */
  3033. }
  3034. /* schedule the job and block wait for result */
  3035. do
  3036. {
  3037. status = caam_in_job_ring_add(base, handle->jobRing, &rngInstantiate[0]);
  3038. } while (status != kStatus_Success);
  3039. status = CAAM_Wait(base, handle, &rngInstantiate[0], kCAAM_Blocking);
  3040. return status;
  3041. }
  3042. /*!
  3043. * brief Uninstantiate the CAAM RNG state handle
  3044. *
  3045. * This function uninstantiates CAAM RNG state handle.
  3046. * The function is blocking and returns after CAAM has processed the request.
  3047. *
  3048. * param base CAAM peripheral base address
  3049. * param handle jobRing used for this request.
  3050. * param stateHandle RNG state handle to uninstantiate
  3051. * return Status of the request
  3052. */
  3053. status_t CAAM_RNG_Deinit(CAAM_Type *base, caam_handle_t *handle, caam_rng_state_handle_t stateHandle)
  3054. {
  3055. status_t status;
  3056. /* create job descriptor */
  3057. caam_desc_rng_t rngUninstantiate = {0};
  3058. rngUninstantiate[0] = 0xB0800002u; /* HEADER */
  3059. rngUninstantiate[1] = 0x8250000Cu; /* ALG OPERATION: RNG uninstantiate state handle (AS=11 Uninstantiate) */
  3060. if (kCAAM_RngStateHandle1 == stateHandle)
  3061. {
  3062. rngUninstantiate[1] |= 1u << 4;
  3063. }
  3064. /* schedule the job and block wait for result */
  3065. do
  3066. {
  3067. status = caam_in_job_ring_add(base, handle->jobRing, &rngUninstantiate[0]);
  3068. } while (status != kStatus_Success);
  3069. status = CAAM_Wait(base, handle, &rngUninstantiate[0], kCAAM_Blocking);
  3070. return status;
  3071. }
  3072. /*!
  3073. * brief Generate Secure Key
  3074. *
  3075. * This function generates random data writes it to Secure Key registers.
  3076. * The function is blocking and returns after CAAM has processed the request.
  3077. * RNG state handle 0 is always used.
  3078. *
  3079. * param base CAAM peripheral base address
  3080. * param handle jobRing used for this request
  3081. * param additionalEntropy NULL or Pointer to optional 256-bit additional entropy.
  3082. * return Status of the request
  3083. */
  3084. status_t CAAM_RNG_GenerateSecureKey(CAAM_Type *base, caam_handle_t *handle, caam_rng_generic256_t additionalEntropy)
  3085. {
  3086. status_t status;
  3087. /* create job descriptor */
  3088. caam_desc_rng_t rngGenSeckey = {0};
  3089. rngGenSeckey[0] = 0xB0800004u; /* HEADER */
  3090. rngGenSeckey[1] = 0x12200020u; /* LOAD 32 bytes of to Class 1 Context Register. Offset 0 bytes. */
  3091. rngGenSeckey[2] = ADD_OFFSET((uint32_t)additionalEntropy);
  3092. rngGenSeckey[3] = 0x82501000u; /* set SK bit in ALG OPERATION (AS=00 Generate) */
  3093. /* optional additional input included */
  3094. if ((additionalEntropy) != NULL)
  3095. {
  3096. rngGenSeckey[3] |= (uint32_t)1u << 11; /* set AI bit in ALG OPERATION */
  3097. }
  3098. else
  3099. {
  3100. rngGenSeckey[1] = DESC_JUMP_2; /* jump to current index + 2 (=3) */
  3101. }
  3102. /* schedule the job and block wait for result */
  3103. do
  3104. {
  3105. status = caam_in_job_ring_add(base, handle->jobRing, &rngGenSeckey[0]);
  3106. } while (status != kStatus_Success);
  3107. status = CAAM_Wait(base, handle, &rngGenSeckey[0], kCAAM_Blocking);
  3108. return status;
  3109. }
  3110. /*!
  3111. * brief Reseed the CAAM RNG state handle
  3112. *
  3113. * This function reseeds the CAAM RNG state handle.
  3114. * For a state handle in nondeterministic mode, the DRNG is seeded with 384 bits of
  3115. * entropy from the TRNG and an optional 256-bit additional input from the descriptor
  3116. * via the Class 1 Context Register.
  3117. *
  3118. * The function is blocking and returns after CAAM has processed the request.
  3119. *
  3120. * param base CAAM peripheral base address
  3121. * param handle jobRing used for this request
  3122. * param stateHandle RNG state handle to reseed
  3123. * param additionalEntropy NULL or Pointer to optional 256-bit additional entropy.
  3124. * return Status of the request
  3125. */
  3126. status_t CAAM_RNG_Reseed(CAAM_Type *base,
  3127. caam_handle_t *handle,
  3128. caam_rng_state_handle_t stateHandle,
  3129. caam_rng_generic256_t additionalEntropy)
  3130. {
  3131. status_t status;
  3132. /* create job descriptor */
  3133. caam_desc_rng_t rngReseed = {0};
  3134. rngReseed[0] = 0xB0800004u; /* HEADER */
  3135. rngReseed[1] = 0x12200020u; /* LOAD 32 bytes of to Class 1 Context Register. Offset 0 bytes. */
  3136. rngReseed[2] = ADD_OFFSET((uint32_t)additionalEntropy);
  3137. rngReseed[3] = 0x8250000Au; /* ALG OPERATION: RNG reseed state handle (AS=10 Reseed) */
  3138. /* optional additional input included */
  3139. if ((additionalEntropy) != NULL)
  3140. {
  3141. rngReseed[3] |= (uint32_t)1u << 11; /* set AI bit in ALG OPERATION */
  3142. }
  3143. else
  3144. {
  3145. rngReseed[1] = DESC_JUMP_2; /* jump to current index + 2 (=3) */
  3146. }
  3147. /* select state handle */
  3148. if (kCAAM_RngStateHandle1 == stateHandle)
  3149. {
  3150. rngReseed[3] |= 1u << 4;
  3151. }
  3152. /* schedule the job and block wait for result */
  3153. do
  3154. {
  3155. status = caam_in_job_ring_add(base, handle->jobRing, &rngReseed[0]);
  3156. } while (status != kStatus_Success);
  3157. status = CAAM_Wait(base, handle, &rngReseed[0], kCAAM_Blocking);
  3158. return status;
  3159. }
  3160. /*!
  3161. * brief Get random data
  3162. *
  3163. * This function gets random data from CAAM RNG.
  3164. *
  3165. * The function is blocking and returns after CAAM has generated the requested data or an error occurred.
  3166. *
  3167. * param base CAAM peripheral base address
  3168. * param handle jobRing used for this request
  3169. * param stateHandle RNG state handle used to generate random data
  3170. * param[out] data Pointer address used to store random data
  3171. * param dataSize Size of the buffer pointed by the data parameter
  3172. * param dataType Type of random data to be generated
  3173. * param additionalEntropy NULL or Pointer to optional 256-bit additional entropy.
  3174. * return Status of the request
  3175. */
  3176. status_t CAAM_RNG_GetRandomData(CAAM_Type *base,
  3177. caam_handle_t *handle,
  3178. caam_rng_state_handle_t stateHandle,
  3179. void *data,
  3180. size_t dataSize,
  3181. caam_rng_random_type_t dataType,
  3182. caam_rng_generic256_t additionalEntropy)
  3183. {
  3184. status_t status;
  3185. caam_desc_rng_t descBuf;
  3186. do
  3187. {
  3188. status = CAAM_RNG_GetRandomDataNonBlocking(base, handle, stateHandle, descBuf, data, dataSize, dataType,
  3189. additionalEntropy);
  3190. } while (status == kStatus_CAAM_Again);
  3191. if (kStatus_Success != status)
  3192. {
  3193. return status;
  3194. }
  3195. status = CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  3196. return status;
  3197. }
  3198. static const uint32_t templateRng[] = {
  3199. /* 00 */ 0xB0800000u, /* HEADER */
  3200. /* 01 */ 0x12200020u, /* LOAD 32 bytes of to Class 1 Context Register. Offset 0 bytes. */
  3201. /* 02 */ 0x00000000u, /* place: additional input address */
  3202. /* 03 */ 0x12820004u, /* LOAD Class 1 Data Size Register by IMM data */
  3203. /* 04 */ 0x00000000u, /* place: data size to generate */
  3204. /* 05 */ 0x82500002u, /* RNG generate */
  3205. /* 06 */ 0x60700000u, /* FIFO STORE message */
  3206. /* 07 */ 0x00000000u, /* place: destination address */
  3207. /* 08 */ 0x00000000u, /* place: destination size */
  3208. };
  3209. /*!
  3210. * brief Request random data
  3211. *
  3212. * This function schedules the request for random data from CAAM RNG.
  3213. * Memory at memory pointers will be accessed by CAAM shortly after this function
  3214. * returns, according to actual CAAM schedule.
  3215. *
  3216. * param base CAAM peripheral base address
  3217. * param handle RNG handle used for this request
  3218. * param stateHandle RNG state handle used to generate random data
  3219. * param[out] descriptor memory for CAAM commands
  3220. * param[out] data Pointer address used to store random data
  3221. * param dataSize Size of the buffer pointed by the data parameter, in bytes.
  3222. * param dataType Type of random data to be generated.
  3223. * param additionalEntropy NULL or Pointer to optional 256-bit additional entropy.
  3224. * return status of the request
  3225. */
  3226. status_t CAAM_RNG_GetRandomDataNonBlocking(CAAM_Type *base,
  3227. caam_handle_t *handle,
  3228. caam_rng_state_handle_t stateHandle,
  3229. caam_desc_rng_t descriptor,
  3230. void *data,
  3231. size_t dataSize,
  3232. caam_rng_random_type_t dataType,
  3233. caam_rng_generic256_t additionalEntropy)
  3234. {
  3235. /* create job descriptor */
  3236. BUILD_ASSURE(sizeof(templateRng) <= sizeof(caam_desc_rng_t), caam_desc_rng_t_size);
  3237. uint32_t descriptorSize = ARRAY_SIZE(templateRng);
  3238. (void)caam_memcpy(descriptor, templateRng, sizeof(templateRng));
  3239. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  3240. /* optional additional input included */
  3241. if (additionalEntropy != NULL)
  3242. {
  3243. descriptor[2] = ADD_OFFSET((uint32_t)additionalEntropy);
  3244. descriptor[5] |= (uint32_t)1U << 11; /* set AI bit in ALG OPERATION */
  3245. }
  3246. else
  3247. {
  3248. descriptor[0] |= (uint32_t)3u << 16; /* start at index 3 */
  3249. }
  3250. descriptor[4] = dataSize; /* Generate OPERATION */
  3251. descriptor[7] = ADD_OFFSET((uint32_t)(uint32_t *)data);
  3252. descriptor[8] = dataSize; /* FIFO STORE */
  3253. /* select state handle */
  3254. if (kCAAM_RngStateHandle1 == stateHandle)
  3255. {
  3256. descriptor[5] |= 1u << 4;
  3257. }
  3258. /* configure type of data */
  3259. if (dataType == kCAAM_RngDataNonZero)
  3260. {
  3261. descriptor[5] |= (uint32_t)1u << 8; /* set NZB bit in ALG OPERATION */
  3262. }
  3263. if (dataType == kCAAM_RngDataOddParity)
  3264. {
  3265. descriptor[5] |= (uint32_t)1u << 9; /* set OBP bit in ALG OPERATION */
  3266. }
  3267. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  3268. }
  3269. static const uint32_t templateCipherDes[] = {
  3270. /* 00 */ 0xB0800000u, /* HEADER */
  3271. /* 01 */ 0x02800000u, /* KEY Class 1 IMM */
  3272. /* 02 */ 0x00000000u, /* IMM key1 0-3 */
  3273. /* 03 */ 0x00000000u, /* IMM key1 4-8 */
  3274. /* 04 */ 0x00000000u, /* IMM key2 0-3 */
  3275. /* 05 */ 0x00000000u, /* IMM key2 4-8 */
  3276. /* 06 */ 0x00000000u, /* IMM key3 0-3 */
  3277. /* 07 */ 0x00000000u, /* IMM key3 4-8 */
  3278. /* 08 */ 0x12200008u, /* LOAD 8 bytes of iv to Class 1 Context Register */
  3279. /* 09 */ 0x00000000u, /* place: iv address */
  3280. /* 10 */ 0x22130000u, /* FIFO LOAD Message */
  3281. /* 11 */ 0x00000000u, /* place: source address */
  3282. /* 12 */ 0x60300000u, /* FIFO STORE Message */
  3283. /* 13 */ 0x00000000u, /* place: destination address */
  3284. /* 14 */ 0x82200000u, /* OPERATION: DES Decrypt, AS = zeroes, AAI = zeroes (CTR) */
  3285. };
  3286. /*******************************************************************************
  3287. * DES Code public
  3288. ******************************************************************************/
  3289. /*!
  3290. * brief Encrypts DES using ECB block mode.
  3291. *
  3292. * Encrypts DES using ECB block mode.
  3293. *
  3294. * param base CAAM peripheral base address
  3295. * param handle Handle used for this request. Specifies jobRing.
  3296. * param plaintext Input plaintext to encrypt
  3297. * param[out] ciphertext Output ciphertext
  3298. * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
  3299. * param key Input key to use for encryption
  3300. * return Status from encrypt/decrypt operation
  3301. */
  3302. status_t CAAM_DES_EncryptEcb(CAAM_Type *base,
  3303. caam_handle_t *handle,
  3304. const uint8_t *plaintext,
  3305. uint8_t *ciphertext,
  3306. size_t size,
  3307. const uint8_t key[CAAM_DES_KEY_SIZE])
  3308. {
  3309. caam_desc_cipher_des_t descBuf;
  3310. status_t status;
  3311. status = CAAM_DES_EncryptEcbNonBlocking(base, handle, descBuf, plaintext, ciphertext, size, key);
  3312. if (status != 0)
  3313. {
  3314. return status;
  3315. }
  3316. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  3317. }
  3318. /*!
  3319. * brief Encrypts DES using ECB block mode.
  3320. *
  3321. * Encrypts DES using ECB block mode.
  3322. *
  3323. * param base CAAM peripheral base address
  3324. * param handle Handle used for this request. Specifies jobRing.
  3325. * param[out] descriptor memory for CAAM commands
  3326. * param plaintext Input plaintext to encrypt
  3327. * param[out] ciphertext Output ciphertext
  3328. * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
  3329. * param key Input key to use for encryption
  3330. * return Status from descriptor push
  3331. */
  3332. status_t CAAM_DES_EncryptEcbNonBlocking(CAAM_Type *base,
  3333. caam_handle_t *handle,
  3334. caam_desc_cipher_des_t descriptor,
  3335. const uint8_t *plaintext,
  3336. uint8_t *ciphertext,
  3337. size_t size,
  3338. const uint8_t key[CAAM_DES_KEY_SIZE])
  3339. {
  3340. BUILD_ASSURE(sizeof(caam_desc_cipher_des_t) >= sizeof(templateCipherDes), caam_desc_cipher_des_t_size);
  3341. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  3342. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  3343. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  3344. * Note: You can still call several times instead
  3345. */
  3346. if (size > 0xFFFFUL)
  3347. {
  3348. return kStatus_CAAM_DataOverflow;
  3349. }
  3350. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  3351. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  3352. descriptor[1] |= CAAM_DES_KEY_SIZE;
  3353. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key, CAAM_DES_KEY_SIZE);
  3354. descriptor[4] = DESC_JUMP_6; /* ECB has no context, jump to currIdx+6 = 10 (FIFO LOAD) */
  3355. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  3356. descriptor[11] = ADD_OFFSET((uint32_t)plaintext);
  3357. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  3358. descriptor[13] = ADD_OFFSET((uint32_t)ciphertext);
  3359. descriptor[14] |= 0x201u; /* add ENC bit to specify Encrypt OPERATION, AAI = 20h */
  3360. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  3361. }
  3362. /*!
  3363. * brief Decrypts DES using ECB block mode.
  3364. *
  3365. * Decrypts DES using ECB block mode.
  3366. *
  3367. * param base CAAM peripheral base address
  3368. * param handle Handle used for this request. Specifies jobRing.
  3369. * param ciphertext Input ciphertext to decrypt
  3370. * param[out] plaintext Output plaintext
  3371. * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
  3372. * param key Input key to use for decryption
  3373. * return Status from encrypt/decrypt operation
  3374. */
  3375. status_t CAAM_DES_DecryptEcb(CAAM_Type *base,
  3376. caam_handle_t *handle,
  3377. const uint8_t *ciphertext,
  3378. uint8_t *plaintext,
  3379. size_t size,
  3380. const uint8_t key[CAAM_DES_KEY_SIZE])
  3381. {
  3382. caam_desc_cipher_des_t descBuf;
  3383. status_t status;
  3384. status = CAAM_DES_DecryptEcbNonBlocking(base, handle, descBuf, ciphertext, plaintext, size, key);
  3385. if (status != 0)
  3386. {
  3387. return status;
  3388. }
  3389. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  3390. }
  3391. /*!
  3392. * brief Decrypts DES using ECB block mode.
  3393. *
  3394. * Decrypts DES using ECB block mode.
  3395. *
  3396. * param base CAAM peripheral base address
  3397. * param handle Handle used for this request. Specifies jobRing.
  3398. * param[out] descriptor memory for CAAM commands
  3399. * param ciphertext Input ciphertext to decrypt
  3400. * param[out] plaintext Output plaintext
  3401. * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
  3402. * param key Input key to use for decryption
  3403. * return Status from descriptor push
  3404. */
  3405. status_t CAAM_DES_DecryptEcbNonBlocking(CAAM_Type *base,
  3406. caam_handle_t *handle,
  3407. caam_desc_cipher_des_t descriptor,
  3408. const uint8_t *ciphertext,
  3409. uint8_t *plaintext,
  3410. size_t size,
  3411. const uint8_t key[CAAM_DES_KEY_SIZE])
  3412. {
  3413. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  3414. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  3415. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  3416. * Note: You can still call several times instead
  3417. */
  3418. if (size > 0xFFFFUL)
  3419. {
  3420. return kStatus_CAAM_DataOverflow;
  3421. }
  3422. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  3423. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  3424. descriptor[1] |= CAAM_DES_KEY_SIZE;
  3425. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key, CAAM_DES_KEY_SIZE);
  3426. descriptor[4] = DESC_JUMP_6; /* ECB has no context, jump to currIdx+6 = 10 (FIFO LOAD) */
  3427. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  3428. descriptor[11] = ADD_OFFSET((uint32_t)ciphertext);
  3429. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  3430. descriptor[13] = ADD_OFFSET((uint32_t)plaintext);
  3431. descriptor[14] |= (uint32_t)kCAAM_ModeECB; /* AAI = 20h */
  3432. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  3433. }
  3434. /*!
  3435. * brief Encrypts DES using CBC block mode.
  3436. *
  3437. * Encrypts DES using CBC block mode.
  3438. *
  3439. * param base CAAM peripheral base address
  3440. * param handle Handle used for this request. Specifies jobRing.
  3441. * param plaintext Input plaintext to encrypt
  3442. * param[out] ciphertext Ouput ciphertext
  3443. * param size Size of input and output data in bytes
  3444. * param iv Input initial vector to combine with the first plaintext block.
  3445. * The iv does not need to be secret, but it must be unpredictable.
  3446. * param key Input key to use for encryption
  3447. * return Status from encrypt/decrypt operation
  3448. */
  3449. status_t CAAM_DES_EncryptCbc(CAAM_Type *base,
  3450. caam_handle_t *handle,
  3451. const uint8_t *plaintext,
  3452. uint8_t *ciphertext,
  3453. size_t size,
  3454. const uint8_t iv[CAAM_DES_IV_SIZE],
  3455. const uint8_t key[CAAM_DES_KEY_SIZE])
  3456. {
  3457. status_t status;
  3458. caam_desc_cipher_des_t descBuf;
  3459. do
  3460. {
  3461. status = CAAM_DES_EncryptCbcNonBlocking(base, handle, descBuf, plaintext, ciphertext, size, iv, key);
  3462. } while (status == kStatus_CAAM_Again);
  3463. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  3464. }
  3465. /*!
  3466. * brief Encrypts DES using CBC block mode.
  3467. *
  3468. * Encrypts DES using CBC block mode.
  3469. *
  3470. * param base CAAM peripheral base address
  3471. * param handle Handle used for this request. Specifies jobRing.
  3472. * param[out] descriptor memory for CAAM commands
  3473. * param plaintext Input plaintext to encrypt
  3474. * param[out] ciphertext Ouput ciphertext
  3475. * param size Size of input and output data in bytes
  3476. * param iv Input initial vector to combine with the first plaintext block.
  3477. * The iv does not need to be secret, but it must be unpredictable.
  3478. * param key Input key to use for encryption
  3479. * return Status from descriptor push
  3480. */
  3481. status_t CAAM_DES_EncryptCbcNonBlocking(CAAM_Type *base,
  3482. caam_handle_t *handle,
  3483. caam_desc_cipher_des_t descriptor,
  3484. const uint8_t *plaintext,
  3485. uint8_t *ciphertext,
  3486. size_t size,
  3487. const uint8_t iv[CAAM_DES_IV_SIZE],
  3488. const uint8_t key[CAAM_DES_KEY_SIZE])
  3489. {
  3490. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  3491. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  3492. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  3493. * Note: You can still call several times instead
  3494. */
  3495. if (size > 0xFFFFUL)
  3496. {
  3497. return kStatus_CAAM_DataOverflow;
  3498. }
  3499. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  3500. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  3501. descriptor[1] |= CAAM_DES_KEY_SIZE;
  3502. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key, CAAM_DES_KEY_SIZE);
  3503. descriptor[4] = DESC_JUMP_4; /* context, jump to currIdx+4 = 8 (LOAD) */
  3504. descriptor[9] = ADD_OFFSET((uint32_t)iv);
  3505. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  3506. descriptor[11] = ADD_OFFSET((uint32_t)plaintext);
  3507. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  3508. descriptor[13] = ADD_OFFSET((uint32_t)ciphertext);
  3509. descriptor[14] |= (uint32_t)kCAAM_ModeCBC; /* AAI = 10h */
  3510. descriptor[14] |= 1u; /* add ENC bit to specify Encrypt OPERATION */
  3511. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  3512. }
  3513. /*!
  3514. * brief Decrypts DES using CBC block mode.
  3515. *
  3516. * Decrypts DES using CBC block mode.
  3517. *
  3518. * param base CAAM peripheral base address
  3519. * param handle Handle used for this request. Specifies jobRing.
  3520. * param ciphertext Input ciphertext to decrypt
  3521. * param[out] plaintext Output plaintext
  3522. * param size Size of input data in bytes
  3523. * param iv Input initial vector to combine with the first plaintext block.
  3524. * The iv does not need to be secret, but it must be unpredictable.
  3525. * param key Input key to use for decryption
  3526. * return Status from encrypt/decrypt operation
  3527. */
  3528. status_t CAAM_DES_DecryptCbc(CAAM_Type *base,
  3529. caam_handle_t *handle,
  3530. const uint8_t *ciphertext,
  3531. uint8_t *plaintext,
  3532. size_t size,
  3533. const uint8_t iv[CAAM_DES_IV_SIZE],
  3534. const uint8_t key[CAAM_DES_KEY_SIZE])
  3535. {
  3536. status_t status;
  3537. caam_desc_cipher_des_t descBuf;
  3538. do
  3539. {
  3540. status = CAAM_DES_DecryptCbcNonBlocking(base, handle, descBuf, ciphertext, plaintext, size, iv, key);
  3541. } while (status == kStatus_CAAM_Again);
  3542. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  3543. }
  3544. /*!
  3545. * brief Decrypts DES using CBC block mode.
  3546. *
  3547. * Decrypts DES using CBC block mode.
  3548. *
  3549. * param base CAAM peripheral base address
  3550. * param handle Handle used for this request. Specifies jobRing.
  3551. * param[out] descriptor memory for CAAM commands
  3552. * param ciphertext Input ciphertext to decrypt
  3553. * param[out] plaintext Output plaintext
  3554. * param size Size of input data in bytes
  3555. * param iv Input initial vector to combine with the first plaintext block.
  3556. * The iv does not need to be secret, but it must be unpredictable.
  3557. * param key Input key to use for decryption
  3558. * return Status from descriptor push
  3559. */
  3560. status_t CAAM_DES_DecryptCbcNonBlocking(CAAM_Type *base,
  3561. caam_handle_t *handle,
  3562. caam_desc_cipher_des_t descriptor,
  3563. const uint8_t *ciphertext,
  3564. uint8_t *plaintext,
  3565. size_t size,
  3566. const uint8_t iv[CAAM_DES_IV_SIZE],
  3567. const uint8_t key[CAAM_DES_KEY_SIZE])
  3568. {
  3569. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  3570. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  3571. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  3572. * Note: You can still call several times instead
  3573. */
  3574. if (size > 0xFFFFUL)
  3575. {
  3576. return kStatus_CAAM_DataOverflow;
  3577. }
  3578. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  3579. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  3580. descriptor[1] |= CAAM_DES_KEY_SIZE;
  3581. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key, CAAM_DES_KEY_SIZE);
  3582. descriptor[4] = DESC_JUMP_4; /* context, jump to currIdx+4 = 8 (LOAD) */
  3583. descriptor[9] = ADD_OFFSET((uint32_t)iv);
  3584. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  3585. descriptor[11] = ADD_OFFSET((uint32_t)ciphertext);
  3586. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  3587. descriptor[13] = ADD_OFFSET((uint32_t)plaintext);
  3588. descriptor[14] |= (uint32_t)kCAAM_ModeCBC; /* AAI = 10h */
  3589. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  3590. }
  3591. /*!
  3592. * brief Encrypts DES using CFB block mode.
  3593. *
  3594. * Encrypts DES using CFB block mode.
  3595. *
  3596. * param base CAAM peripheral base address
  3597. * param handle Handle used for this request. Specifies jobRing.
  3598. * param plaintext Input plaintext to encrypt
  3599. * param size Size of input data in bytes
  3600. * param iv Input initial block.
  3601. * param key Input key to use for encryption
  3602. * param[out] ciphertext Output ciphertext
  3603. * return Status from encrypt/decrypt operation
  3604. */
  3605. status_t CAAM_DES_EncryptCfb(CAAM_Type *base,
  3606. caam_handle_t *handle,
  3607. const uint8_t *plaintext,
  3608. uint8_t *ciphertext,
  3609. size_t size,
  3610. const uint8_t iv[CAAM_DES_IV_SIZE],
  3611. const uint8_t key[CAAM_DES_KEY_SIZE])
  3612. {
  3613. status_t status;
  3614. caam_desc_cipher_des_t descBuf;
  3615. do
  3616. {
  3617. status = CAAM_DES_EncryptCfbNonBlocking(base, handle, descBuf, plaintext, ciphertext, size, iv, key);
  3618. } while (status == kStatus_CAAM_Again);
  3619. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  3620. }
  3621. /*!
  3622. * brief Encrypts DES using CFB block mode.
  3623. *
  3624. * Encrypts DES using CFB block mode.
  3625. *
  3626. * param base CAAM peripheral base address
  3627. * param handle Handle used for this request. Specifies jobRing.
  3628. * param[out] descriptor memory for CAAM commands
  3629. * param plaintext Input plaintext to encrypt
  3630. * param size Size of input data in bytes
  3631. * param iv Input initial block.
  3632. * param key Input key to use for encryption
  3633. * param[out] ciphertext Output ciphertext
  3634. * return Status from descriptor push
  3635. */
  3636. status_t CAAM_DES_EncryptCfbNonBlocking(CAAM_Type *base,
  3637. caam_handle_t *handle,
  3638. caam_desc_cipher_des_t descriptor,
  3639. const uint8_t *plaintext,
  3640. uint8_t *ciphertext,
  3641. size_t size,
  3642. const uint8_t iv[CAAM_DES_IV_SIZE],
  3643. const uint8_t key[CAAM_DES_KEY_SIZE])
  3644. {
  3645. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  3646. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  3647. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  3648. * Note: You can still call several times instead
  3649. */
  3650. if (size > 0xFFFFUL)
  3651. {
  3652. return kStatus_CAAM_DataOverflow;
  3653. }
  3654. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  3655. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  3656. descriptor[1] |= CAAM_DES_KEY_SIZE;
  3657. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key, CAAM_DES_KEY_SIZE);
  3658. descriptor[4] = DESC_JUMP_4; /* context, jump to currIdx+4 = 8 (LOAD) */
  3659. descriptor[9] = ADD_OFFSET((uint32_t)iv);
  3660. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  3661. descriptor[11] = ADD_OFFSET((uint32_t)plaintext);
  3662. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  3663. descriptor[13] = ADD_OFFSET((uint32_t)ciphertext);
  3664. descriptor[14] |= (uint32_t)kCAAM_ModeCFB; /* AAI = 30h = CFB */
  3665. descriptor[14] |= 1u; /* add ENC bit to specify Encrypt OPERATION */
  3666. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  3667. }
  3668. /*!
  3669. * brief Decrypts DES using CFB block mode.
  3670. *
  3671. * Decrypts DES using CFB block mode.
  3672. *
  3673. * param base CAAM peripheral base address
  3674. * param handle Handle used for this request. Specifies jobRing.
  3675. * param ciphertext Input ciphertext to decrypt
  3676. * param[out] plaintext Output plaintext
  3677. * param size Size of input and output data in bytes
  3678. * param iv Input initial block.
  3679. * param key Input key to use for decryption
  3680. * return Status from encrypt/decrypt operation
  3681. */
  3682. status_t CAAM_DES_DecryptCfb(CAAM_Type *base,
  3683. caam_handle_t *handle,
  3684. const uint8_t *ciphertext,
  3685. uint8_t *plaintext,
  3686. size_t size,
  3687. const uint8_t iv[CAAM_DES_IV_SIZE],
  3688. const uint8_t key[CAAM_DES_KEY_SIZE])
  3689. {
  3690. status_t status;
  3691. caam_desc_cipher_des_t descBuf;
  3692. do
  3693. {
  3694. status = CAAM_DES_DecryptCfbNonBlocking(base, handle, descBuf, ciphertext, plaintext, size, iv, key);
  3695. } while (status == kStatus_CAAM_Again);
  3696. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  3697. }
  3698. /*!
  3699. * brief Decrypts DES using CFB block mode.
  3700. *
  3701. * Decrypts DES using CFB block mode.
  3702. *
  3703. * param base CAAM peripheral base address
  3704. * param handle Handle used for this request. Specifies jobRing.
  3705. * param[out] descriptor memory for CAAM commands
  3706. * param ciphertext Input ciphertext to decrypt
  3707. * param[out] plaintext Output plaintext
  3708. * param size Size of input and output data in bytes
  3709. * param iv Input initial block.
  3710. * param key Input key to use for decryption
  3711. * return Status from descriptor push
  3712. */
  3713. status_t CAAM_DES_DecryptCfbNonBlocking(CAAM_Type *base,
  3714. caam_handle_t *handle,
  3715. caam_desc_cipher_des_t descriptor,
  3716. const uint8_t *ciphertext,
  3717. uint8_t *plaintext,
  3718. size_t size,
  3719. const uint8_t iv[CAAM_DES_IV_SIZE],
  3720. const uint8_t key[CAAM_DES_KEY_SIZE])
  3721. {
  3722. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  3723. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  3724. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  3725. * Note: You can still call several times instead
  3726. */
  3727. if (size > 0xFFFFUL)
  3728. {
  3729. return kStatus_CAAM_DataOverflow;
  3730. }
  3731. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  3732. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  3733. descriptor[1] |= CAAM_DES_KEY_SIZE;
  3734. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key, CAAM_DES_KEY_SIZE);
  3735. descriptor[4] = DESC_JUMP_4; /* context, jump to currIdx+4 = 8 (LOAD) */
  3736. descriptor[9] = ADD_OFFSET((uint32_t)iv);
  3737. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  3738. descriptor[11] = ADD_OFFSET((uint32_t)ciphertext);
  3739. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  3740. descriptor[13] = ADD_OFFSET((uint32_t)plaintext);
  3741. descriptor[14] |= (uint32_t)kCAAM_ModeCFB; /* AAI = 30h = CFB */
  3742. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  3743. }
  3744. /*!
  3745. * brief Encrypts DES using OFB block mode.
  3746. *
  3747. * Encrypts DES using OFB block mode.
  3748. *
  3749. * param base CAAM peripheral base address
  3750. * param handle Handle used for this request. Specifies jobRing.
  3751. * param plaintext Input plaintext to encrypt
  3752. * param[out] ciphertext Output ciphertext
  3753. * param size Size of input and output data in bytes
  3754. * param iv Input unique input vector. The OFB mode requires that the IV be unique
  3755. * for each execution of the mode under the given key.
  3756. * param key Input key to use for encryption
  3757. * return Status from encrypt/decrypt operation
  3758. */
  3759. status_t CAAM_DES_EncryptOfb(CAAM_Type *base,
  3760. caam_handle_t *handle,
  3761. const uint8_t *plaintext,
  3762. uint8_t *ciphertext,
  3763. size_t size,
  3764. const uint8_t iv[CAAM_DES_IV_SIZE],
  3765. const uint8_t key[CAAM_DES_KEY_SIZE])
  3766. {
  3767. status_t status;
  3768. caam_desc_cipher_des_t descBuf;
  3769. do
  3770. {
  3771. status = CAAM_DES_EncryptOfbNonBlocking(base, handle, descBuf, plaintext, ciphertext, size, iv, key);
  3772. } while (status == kStatus_CAAM_Again);
  3773. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  3774. }
  3775. /*!
  3776. * brief Encrypts DES using OFB block mode.
  3777. *
  3778. * Encrypts DES using OFB block mode.
  3779. *
  3780. * param base CAAM peripheral base address
  3781. * param handle Handle used for this request. Specifies jobRing.
  3782. * param[out] descriptor memory for CAAM commands
  3783. * param plaintext Input plaintext to encrypt
  3784. * param[out] ciphertext Output ciphertext
  3785. * param size Size of input and output data in bytes
  3786. * param iv Input unique input vector. The OFB mode requires that the IV be unique
  3787. * for each execution of the mode under the given key.
  3788. * param key Input key to use for encryption
  3789. * return Status from descriptor push
  3790. */
  3791. status_t CAAM_DES_EncryptOfbNonBlocking(CAAM_Type *base,
  3792. caam_handle_t *handle,
  3793. caam_desc_cipher_des_t descriptor,
  3794. const uint8_t *plaintext,
  3795. uint8_t *ciphertext,
  3796. size_t size,
  3797. const uint8_t iv[CAAM_DES_IV_SIZE],
  3798. const uint8_t key[CAAM_DES_KEY_SIZE])
  3799. {
  3800. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  3801. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  3802. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  3803. * Note: You can still call several times instead
  3804. */
  3805. if (size > 0xFFFFUL)
  3806. {
  3807. return kStatus_CAAM_DataOverflow;
  3808. }
  3809. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  3810. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  3811. descriptor[1] |= CAAM_DES_KEY_SIZE;
  3812. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key, CAAM_DES_KEY_SIZE);
  3813. descriptor[4] = DESC_JUMP_4; /* context, jump to currIdx+4 = 8 (LOAD) */
  3814. descriptor[9] = ADD_OFFSET((uint32_t)iv);
  3815. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  3816. descriptor[11] = ADD_OFFSET((uint32_t)plaintext);
  3817. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  3818. descriptor[13] = ADD_OFFSET((uint32_t)ciphertext);
  3819. descriptor[14] |= (uint32_t)kCAAM_ModeOFB; /* AAI = 40h = OFB */
  3820. descriptor[14] |= 1u; /* add ENC bit to specify Encrypt OPERATION */
  3821. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  3822. }
  3823. /*!
  3824. * brief Decrypts DES using OFB block mode.
  3825. *
  3826. * Decrypts DES using OFB block mode.
  3827. *
  3828. * param base CAAM peripheral base address
  3829. * param handle Handle used for this request. Specifies jobRing.
  3830. * param ciphertext Input ciphertext to decrypt
  3831. * param[out] plaintext Output plaintext
  3832. * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
  3833. * param iv Input unique input vector. The OFB mode requires that the IV be unique
  3834. * for each execution of the mode under the given key.
  3835. * param key Input key to use for decryption
  3836. * return Status from encrypt/decrypt operation
  3837. */
  3838. status_t CAAM_DES_DecryptOfb(CAAM_Type *base,
  3839. caam_handle_t *handle,
  3840. const uint8_t *ciphertext,
  3841. uint8_t *plaintext,
  3842. size_t size,
  3843. const uint8_t iv[CAAM_DES_IV_SIZE],
  3844. const uint8_t key[CAAM_DES_KEY_SIZE])
  3845. {
  3846. status_t status;
  3847. caam_desc_cipher_des_t descBuf;
  3848. do
  3849. {
  3850. status = CAAM_DES_DecryptOfbNonBlocking(base, handle, descBuf, ciphertext, plaintext, size, iv, key);
  3851. } while (status == kStatus_CAAM_Again);
  3852. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  3853. }
  3854. /*!
  3855. * brief Decrypts DES using OFB block mode.
  3856. *
  3857. * Decrypts DES using OFB block mode.
  3858. *
  3859. * param base CAAM peripheral base address
  3860. * param handle Handle used for this request. Specifies jobRing.
  3861. * param[out] descriptor memory for CAAM commands
  3862. * param ciphertext Input ciphertext to decrypt
  3863. * param[out] plaintext Output plaintext
  3864. * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
  3865. * param iv Input unique input vector. The OFB mode requires that the IV be unique
  3866. * for each execution of the mode under the given key.
  3867. * param key Input key to use for decryption
  3868. * return Status from descriptor push
  3869. */
  3870. status_t CAAM_DES_DecryptOfbNonBlocking(CAAM_Type *base,
  3871. caam_handle_t *handle,
  3872. caam_desc_cipher_des_t descriptor,
  3873. const uint8_t *ciphertext,
  3874. uint8_t *plaintext,
  3875. size_t size,
  3876. const uint8_t iv[CAAM_DES_IV_SIZE],
  3877. const uint8_t key[CAAM_DES_KEY_SIZE])
  3878. {
  3879. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  3880. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  3881. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  3882. * Note: You can still call several times instead
  3883. */
  3884. if (size > 0xFFFFUL)
  3885. {
  3886. return kStatus_CAAM_DataOverflow;
  3887. }
  3888. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  3889. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  3890. descriptor[1] |= CAAM_DES_KEY_SIZE;
  3891. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key, CAAM_DES_KEY_SIZE);
  3892. descriptor[4] = DESC_JUMP_4; /* context, jump to currIdx+4 = 8 (LOAD) */
  3893. descriptor[9] = ADD_OFFSET((uint32_t)iv);
  3894. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  3895. descriptor[11] = ADD_OFFSET((uint32_t)ciphertext);
  3896. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  3897. descriptor[13] = ADD_OFFSET((uint32_t)plaintext);
  3898. descriptor[14] |= (uint32_t)kCAAM_ModeOFB; /* AAI = 40h = OFB */
  3899. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  3900. }
  3901. /*!
  3902. * brief Encrypts triple DES using ECB block mode with two keys.
  3903. *
  3904. * Encrypts triple DES using ECB block mode with two keys.
  3905. *
  3906. * param base CAAM peripheral base address
  3907. * param handle Handle used for this request. Specifies jobRing.
  3908. * param plaintext Input plaintext to encrypt
  3909. * param[out] ciphertext Output ciphertext
  3910. * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
  3911. * param key1 First input key for key bundle
  3912. * param key2 Second input key for key bundle
  3913. * return Status from encrypt/decrypt operation
  3914. */
  3915. status_t CAAM_DES2_EncryptEcb(CAAM_Type *base,
  3916. caam_handle_t *handle,
  3917. const uint8_t *plaintext,
  3918. uint8_t *ciphertext,
  3919. size_t size,
  3920. const uint8_t key1[CAAM_DES_KEY_SIZE],
  3921. const uint8_t key2[CAAM_DES_KEY_SIZE])
  3922. {
  3923. caam_desc_cipher_des_t descBuf;
  3924. status_t status;
  3925. status = CAAM_DES2_EncryptEcbNonBlocking(base, handle, descBuf, plaintext, ciphertext, size, key1, key2);
  3926. if (status != 0)
  3927. {
  3928. return status;
  3929. }
  3930. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  3931. }
  3932. /*!
  3933. * brief Encrypts triple DES using ECB block mode with two keys.
  3934. *
  3935. * Encrypts triple DES using ECB block mode with two keys.
  3936. *
  3937. * param base CAAM peripheral base address
  3938. * param handle Handle used for this request. Specifies jobRing.
  3939. * param[out] descriptor memory for CAAM commands
  3940. * param plaintext Input plaintext to encrypt
  3941. * param[out] ciphertext Output ciphertext
  3942. * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
  3943. * param key1 First input key for key bundle
  3944. * param key2 Second input key for key bundle
  3945. * return Status from descriptor push
  3946. */
  3947. status_t CAAM_DES2_EncryptEcbNonBlocking(CAAM_Type *base,
  3948. caam_handle_t *handle,
  3949. caam_desc_cipher_des_t descriptor,
  3950. const uint8_t *plaintext,
  3951. uint8_t *ciphertext,
  3952. size_t size,
  3953. const uint8_t key1[CAAM_DES_KEY_SIZE],
  3954. const uint8_t key2[CAAM_DES_KEY_SIZE])
  3955. {
  3956. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  3957. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  3958. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  3959. * Note: You can still call several times instead
  3960. */
  3961. if (size > 0xFFFFUL)
  3962. {
  3963. return kStatus_CAAM_DataOverflow;
  3964. }
  3965. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  3966. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  3967. descriptor[1] |= 2U * CAAM_DES_KEY_SIZE;
  3968. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key1, CAAM_DES_KEY_SIZE);
  3969. (void)caam_memcpy(&descriptor[4], (const uint32_t *)(uintptr_t)key2, CAAM_DES_KEY_SIZE);
  3970. descriptor[6] = DESC_JUMP_4; /* ECB has no context, jump to currIdx+4 = 10 (FIFO LOAD) */
  3971. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  3972. descriptor[11] = ADD_OFFSET((uint32_t)plaintext);
  3973. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  3974. descriptor[13] = ADD_OFFSET((uint32_t)ciphertext);
  3975. descriptor[14] |= 0x201u; /* add ENC bit to specify Encrypt OPERATION, AAI = 20h */
  3976. descriptor[14] |= 0x10000U; /* 3DES */
  3977. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  3978. }
  3979. /*!
  3980. * brief Decrypts triple DES using ECB block mode with two keys.
  3981. *
  3982. * Decrypts triple DES using ECB block mode with two keys.
  3983. *
  3984. * param base CAAM peripheral base address
  3985. * param handle Handle used for this request. Specifies jobRing.
  3986. * param ciphertext Input ciphertext to decrypt
  3987. * param[out] plaintext Output plaintext
  3988. * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
  3989. * param key1 First input key for key bundle
  3990. * param key2 Second input key for key bundle
  3991. * return Status from encrypt/decrypt operation
  3992. */
  3993. status_t CAAM_DES2_DecryptEcb(CAAM_Type *base,
  3994. caam_handle_t *handle,
  3995. const uint8_t *ciphertext,
  3996. uint8_t *plaintext,
  3997. size_t size,
  3998. const uint8_t key1[CAAM_DES_KEY_SIZE],
  3999. const uint8_t key2[CAAM_DES_KEY_SIZE])
  4000. {
  4001. caam_desc_cipher_des_t descBuf;
  4002. status_t status;
  4003. status = CAAM_DES2_DecryptEcbNonBlocking(base, handle, descBuf, ciphertext, plaintext, size, key1, key2);
  4004. if (status != 0)
  4005. {
  4006. return status;
  4007. }
  4008. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  4009. }
  4010. /*!
  4011. * brief Decrypts triple DES using ECB block mode with two keys.
  4012. *
  4013. * Decrypts triple DES using ECB block mode with two keys.
  4014. *
  4015. * param base CAAM peripheral base address
  4016. * param handle Handle used for this request. Specifies jobRing.
  4017. * param[out] descriptor memory for CAAM commands
  4018. * param ciphertext Input ciphertext to decrypt
  4019. * param[out] plaintext Output plaintext
  4020. * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
  4021. * param key1 First input key for key bundle
  4022. * param key2 Second input key for key bundle
  4023. * return Status from descriptor push
  4024. */
  4025. status_t CAAM_DES2_DecryptEcbNonBlocking(CAAM_Type *base,
  4026. caam_handle_t *handle,
  4027. caam_desc_cipher_des_t descriptor,
  4028. const uint8_t *ciphertext,
  4029. uint8_t *plaintext,
  4030. size_t size,
  4031. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4032. const uint8_t key2[CAAM_DES_KEY_SIZE])
  4033. {
  4034. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  4035. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  4036. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  4037. * Note: You can still call several times instead
  4038. */
  4039. if (size > 0xFFFFUL)
  4040. {
  4041. return kStatus_CAAM_DataOverflow;
  4042. }
  4043. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  4044. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  4045. descriptor[1] |= 2U * CAAM_DES_KEY_SIZE;
  4046. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key1, CAAM_DES_KEY_SIZE);
  4047. (void)caam_memcpy(&descriptor[4], (const uint32_t *)(uintptr_t)key2, CAAM_DES_KEY_SIZE);
  4048. descriptor[6] = DESC_JUMP_4; /* ECB has no context, jump to currIdx+4 = 10 (FIFO LOAD) */
  4049. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4050. descriptor[11] = ADD_OFFSET((uint32_t)ciphertext);
  4051. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4052. descriptor[13] = ADD_OFFSET((uint32_t)plaintext);
  4053. descriptor[14] |= (uint32_t)kCAAM_ModeECB; /* AAI = 20h */
  4054. descriptor[14] |= 0x10000U; /* 3DES */
  4055. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  4056. }
  4057. /*!
  4058. * brief Encrypts triple DES using CBC block mode with two keys.
  4059. *
  4060. * Encrypts triple DES using CBC block mode with two keys.
  4061. *
  4062. * param base CAAM peripheral base address
  4063. * param handle Handle used for this request. Specifies jobRing.
  4064. * param plaintext Input plaintext to encrypt
  4065. * param[out] ciphertext Output ciphertext
  4066. * param size Size of input and output data in bytes
  4067. * param iv Input initial vector to combine with the first plaintext block.
  4068. * The iv does not need to be secret, but it must be unpredictable.
  4069. * param key1 First input key for key bundle
  4070. * param key2 Second input key for key bundle
  4071. * return Status from encrypt/decrypt operation
  4072. */
  4073. status_t CAAM_DES2_EncryptCbc(CAAM_Type *base,
  4074. caam_handle_t *handle,
  4075. const uint8_t *plaintext,
  4076. uint8_t *ciphertext,
  4077. size_t size,
  4078. const uint8_t iv[CAAM_DES_IV_SIZE],
  4079. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4080. const uint8_t key2[CAAM_DES_KEY_SIZE])
  4081. {
  4082. status_t status;
  4083. caam_desc_cipher_des_t descBuf;
  4084. do
  4085. {
  4086. status = CAAM_DES2_EncryptCbcNonBlocking(base, handle, descBuf, plaintext, ciphertext, size, iv, key1, key2);
  4087. } while (status == kStatus_CAAM_Again);
  4088. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  4089. }
  4090. /*!
  4091. * brief Encrypts triple DES using CBC block mode with two keys.
  4092. *
  4093. * Encrypts triple DES using CBC block mode with two keys.
  4094. *
  4095. * param base CAAM peripheral base address
  4096. * param handle Handle used for this request. Specifies jobRing.
  4097. * param[out] descriptor memory for CAAM commands
  4098. * param plaintext Input plaintext to encrypt
  4099. * param[out] ciphertext Output ciphertext
  4100. * param size Size of input and output data in bytes
  4101. * param iv Input initial vector to combine with the first plaintext block.
  4102. * The iv does not need to be secret, but it must be unpredictable.
  4103. * param key1 First input key for key bundle
  4104. * param key2 Second input key for key bundle
  4105. * return Status from descriptor push
  4106. */
  4107. status_t CAAM_DES2_EncryptCbcNonBlocking(CAAM_Type *base,
  4108. caam_handle_t *handle,
  4109. caam_desc_cipher_des_t descriptor,
  4110. const uint8_t *plaintext,
  4111. uint8_t *ciphertext,
  4112. size_t size,
  4113. const uint8_t iv[CAAM_DES_IV_SIZE],
  4114. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4115. const uint8_t key2[CAAM_DES_KEY_SIZE])
  4116. {
  4117. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  4118. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  4119. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  4120. * Note: You can still call several times instead
  4121. */
  4122. if (size > 0xFFFFUL)
  4123. {
  4124. return kStatus_CAAM_DataOverflow;
  4125. }
  4126. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  4127. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  4128. descriptor[1] |= 2U * CAAM_DES_KEY_SIZE;
  4129. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key1, CAAM_DES_KEY_SIZE);
  4130. (void)caam_memcpy(&descriptor[4], (const uint32_t *)(uintptr_t)key2, CAAM_DES_KEY_SIZE);
  4131. descriptor[6] = DESC_JUMP_2; /* context, jump to currIdx+2 = 8 (LOAD) */
  4132. descriptor[9] = ADD_OFFSET((uint32_t)iv);
  4133. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4134. descriptor[11] = ADD_OFFSET((uint32_t)plaintext);
  4135. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4136. descriptor[13] = ADD_OFFSET((uint32_t)ciphertext);
  4137. descriptor[14] |= (uint32_t)kCAAM_ModeCBC; /* AAI = 10h */
  4138. descriptor[14] |= 1u; /* add ENC bit to specify Encrypt OPERATION */
  4139. descriptor[14] |= 0x10000U; /* 3DES */
  4140. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  4141. }
  4142. /*!
  4143. * brief Decrypts triple DES using CBC block mode with two keys.
  4144. *
  4145. * Decrypts triple DES using CBC block mode with two keys.
  4146. *
  4147. * param base CAAM peripheral base address
  4148. * param handle Handle used for this request. Specifies jobRing.
  4149. * param ciphertext Input ciphertext to decrypt
  4150. * param[out] plaintext Output plaintext
  4151. * param size Size of input and output data in bytes
  4152. * param iv Input initial vector to combine with the first plaintext block.
  4153. * The iv does not need to be secret, but it must be unpredictable.
  4154. * param key1 First input key for key bundle
  4155. * param key2 Second input key for key bundle
  4156. * return Status from encrypt/decrypt operation
  4157. */
  4158. status_t CAAM_DES2_DecryptCbc(CAAM_Type *base,
  4159. caam_handle_t *handle,
  4160. const uint8_t *ciphertext,
  4161. uint8_t *plaintext,
  4162. size_t size,
  4163. const uint8_t iv[CAAM_DES_IV_SIZE],
  4164. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4165. const uint8_t key2[CAAM_DES_KEY_SIZE])
  4166. {
  4167. status_t status;
  4168. caam_desc_cipher_des_t descBuf;
  4169. do
  4170. {
  4171. status = CAAM_DES2_DecryptCbcNonBlocking(base, handle, descBuf, ciphertext, plaintext, size, iv, key1, key2);
  4172. } while (status == kStatus_CAAM_Again);
  4173. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  4174. }
  4175. /*!
  4176. * brief Decrypts triple DES using CBC block mode with two keys.
  4177. *
  4178. * Decrypts triple DES using CBC block mode with two keys.
  4179. *
  4180. * param base CAAM peripheral base address
  4181. * param handle Handle used for this request. Specifies jobRing.
  4182. * param[out] descriptor memory for CAAM commands
  4183. * param ciphertext Input ciphertext to decrypt
  4184. * param[out] plaintext Output plaintext
  4185. * param size Size of input and output data in bytes
  4186. * param iv Input initial vector to combine with the first plaintext block.
  4187. * The iv does not need to be secret, but it must be unpredictable.
  4188. * param key1 First input key for key bundle
  4189. * param key2 Second input key for key bundle
  4190. * return Status from descriptor push
  4191. */
  4192. status_t CAAM_DES2_DecryptCbcNonBlocking(CAAM_Type *base,
  4193. caam_handle_t *handle,
  4194. caam_desc_cipher_des_t descriptor,
  4195. const uint8_t *ciphertext,
  4196. uint8_t *plaintext,
  4197. size_t size,
  4198. const uint8_t iv[CAAM_DES_IV_SIZE],
  4199. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4200. const uint8_t key2[CAAM_DES_KEY_SIZE])
  4201. {
  4202. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  4203. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  4204. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  4205. * Note: You can still call several times instead
  4206. */
  4207. if (size > 0xFFFFUL)
  4208. {
  4209. return kStatus_CAAM_DataOverflow;
  4210. }
  4211. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  4212. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  4213. descriptor[1] |= 2U * CAAM_DES_KEY_SIZE;
  4214. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key1, CAAM_DES_KEY_SIZE);
  4215. (void)caam_memcpy(&descriptor[4], (const uint32_t *)(uintptr_t)key2, CAAM_DES_KEY_SIZE);
  4216. descriptor[6] = DESC_JUMP_2; /* context, jump to currIdx+2 = 8 (LOAD) */
  4217. descriptor[9] = ADD_OFFSET((uint32_t)iv);
  4218. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4219. descriptor[11] = ADD_OFFSET((uint32_t)ciphertext);
  4220. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4221. descriptor[13] = ADD_OFFSET((uint32_t)plaintext);
  4222. descriptor[14] |= (uint32_t)kCAAM_ModeCBC; /* AAI = 10h */
  4223. descriptor[14] |= 0x10000U; /* 3DES */
  4224. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  4225. }
  4226. /*!
  4227. * brief Encrypts triple DES using CFB block mode with two keys.
  4228. *
  4229. * Encrypts triple DES using CFB block mode with two keys.
  4230. *
  4231. * param base CAAM peripheral base address
  4232. * param handle Handle used for this request. Specifies jobRing.
  4233. * param plaintext Input plaintext to encrypt
  4234. * param[out] ciphertext Output ciphertext
  4235. * param size Size of input and output data in bytes
  4236. * param iv Input initial block.
  4237. * param key1 First input key for key bundle
  4238. * param key2 Second input key for key bundle
  4239. * return Status from encrypt/decrypt operation
  4240. */
  4241. status_t CAAM_DES2_EncryptCfb(CAAM_Type *base,
  4242. caam_handle_t *handle,
  4243. const uint8_t *plaintext,
  4244. uint8_t *ciphertext,
  4245. size_t size,
  4246. const uint8_t iv[CAAM_DES_IV_SIZE],
  4247. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4248. const uint8_t key2[CAAM_DES_KEY_SIZE])
  4249. {
  4250. status_t status;
  4251. caam_desc_cipher_des_t descBuf;
  4252. do
  4253. {
  4254. status = CAAM_DES2_EncryptCfbNonBlocking(base, handle, descBuf, plaintext, ciphertext, size, iv, key1, key2);
  4255. } while (status == kStatus_CAAM_Again);
  4256. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  4257. }
  4258. /*!
  4259. * brief Encrypts triple DES using CFB block mode with two keys.
  4260. *
  4261. * Encrypts triple DES using CFB block mode with two keys.
  4262. *
  4263. * param base CAAM peripheral base address
  4264. * param handle Handle used for this request. Specifies jobRing.
  4265. * param[out] descriptor memory for CAAM commands
  4266. * param plaintext Input plaintext to encrypt
  4267. * param[out] ciphertext Output ciphertext
  4268. * param size Size of input and output data in bytes
  4269. * param iv Input initial block.
  4270. * param key1 First input key for key bundle
  4271. * param key2 Second input key for key bundle
  4272. * return Status from descriptor push
  4273. */
  4274. status_t CAAM_DES2_EncryptCfbNonBlocking(CAAM_Type *base,
  4275. caam_handle_t *handle,
  4276. caam_desc_cipher_des_t descriptor,
  4277. const uint8_t *plaintext,
  4278. uint8_t *ciphertext,
  4279. size_t size,
  4280. const uint8_t iv[CAAM_DES_IV_SIZE],
  4281. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4282. const uint8_t key2[CAAM_DES_KEY_SIZE])
  4283. {
  4284. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  4285. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  4286. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  4287. * Note: You can still call several times instead
  4288. */
  4289. if (size > 0xFFFFUL)
  4290. {
  4291. return kStatus_CAAM_DataOverflow;
  4292. }
  4293. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  4294. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  4295. descriptor[1] |= 2U * CAAM_DES_KEY_SIZE;
  4296. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key1, CAAM_DES_KEY_SIZE);
  4297. (void)caam_memcpy(&descriptor[4], (const uint32_t *)(uintptr_t)key2, CAAM_DES_KEY_SIZE);
  4298. descriptor[6] = DESC_JUMP_2; /* context, jump to currIdx+2 = 8 (LOAD) */
  4299. descriptor[9] = ADD_OFFSET((uint32_t)iv);
  4300. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4301. descriptor[11] = ADD_OFFSET((uint32_t)plaintext);
  4302. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4303. descriptor[13] = ADD_OFFSET((uint32_t)ciphertext);
  4304. descriptor[14] |= (uint32_t)kCAAM_ModeCFB; /* AAI = 30h = CFB */
  4305. descriptor[14] |= 1u; /* add ENC bit to specify Encrypt OPERATION */
  4306. descriptor[14] |= 0x10000U; /* 3DES */
  4307. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  4308. }
  4309. /*!
  4310. * brief Decrypts triple DES using CFB block mode with two keys.
  4311. *
  4312. * Decrypts triple DES using CFB block mode with two keys.
  4313. *
  4314. * param base CAAM peripheral base address
  4315. * param handle Handle used for this request. Specifies jobRing.
  4316. * param ciphertext Input ciphertext to decrypt
  4317. * param[out] plaintext Output plaintext
  4318. * param size Size of input and output data in bytes
  4319. * param iv Input initial block.
  4320. * param key1 First input key for key bundle
  4321. * param key2 Second input key for key bundle
  4322. * return Status from encrypt/decrypt operation
  4323. */
  4324. status_t CAAM_DES2_DecryptCfb(CAAM_Type *base,
  4325. caam_handle_t *handle,
  4326. const uint8_t *ciphertext,
  4327. uint8_t *plaintext,
  4328. size_t size,
  4329. const uint8_t iv[CAAM_DES_IV_SIZE],
  4330. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4331. const uint8_t key2[CAAM_DES_KEY_SIZE])
  4332. {
  4333. status_t status;
  4334. caam_desc_cipher_des_t descBuf;
  4335. do
  4336. {
  4337. status = CAAM_DES2_DecryptCfbNonBlocking(base, handle, descBuf, ciphertext, plaintext, size, iv, key1, key2);
  4338. } while (status == kStatus_CAAM_Again);
  4339. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  4340. }
  4341. /*!
  4342. * brief Decrypts triple DES using CFB block mode with two keys.
  4343. *
  4344. * Decrypts triple DES using CFB block mode with two keys.
  4345. *
  4346. * param base CAAM peripheral base address
  4347. * param handle Handle used for this request. Specifies jobRing.
  4348. * param[out] descriptor memory for CAAM commands
  4349. * param ciphertext Input ciphertext to decrypt
  4350. * param[out] plaintext Output plaintext
  4351. * param size Size of input and output data in bytes
  4352. * param iv Input initial block.
  4353. * param key1 First input key for key bundle
  4354. * param key2 Second input key for key bundle
  4355. * return Status from descriptor push
  4356. */
  4357. status_t CAAM_DES2_DecryptCfbNonBlocking(CAAM_Type *base,
  4358. caam_handle_t *handle,
  4359. caam_desc_cipher_des_t descriptor,
  4360. const uint8_t *ciphertext,
  4361. uint8_t *plaintext,
  4362. size_t size,
  4363. const uint8_t iv[CAAM_DES_IV_SIZE],
  4364. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4365. const uint8_t key2[CAAM_DES_KEY_SIZE])
  4366. {
  4367. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  4368. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  4369. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  4370. * Note: You can still call several times instead
  4371. */
  4372. if (size > 0xFFFFUL)
  4373. {
  4374. return kStatus_CAAM_DataOverflow;
  4375. }
  4376. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  4377. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  4378. descriptor[1] |= 2U * CAAM_DES_KEY_SIZE;
  4379. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key1, CAAM_DES_KEY_SIZE);
  4380. (void)caam_memcpy(&descriptor[4], (const uint32_t *)(uintptr_t)key2, CAAM_DES_KEY_SIZE);
  4381. descriptor[6] = DESC_JUMP_2; /* context, jump to currIdx+2 = 8 (LOAD) */
  4382. descriptor[9] = ADD_OFFSET((uint32_t)iv);
  4383. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4384. descriptor[11] = ADD_OFFSET((uint32_t)ciphertext);
  4385. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4386. descriptor[13] = ADD_OFFSET((uint32_t)plaintext);
  4387. descriptor[14] |= (uint32_t)kCAAM_ModeCFB; /* AAI = 30h = CFB */
  4388. descriptor[14] |= 0x10000U; /* 3DES */
  4389. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  4390. }
  4391. /*!
  4392. * brief Encrypts triple DES using OFB block mode with two keys.
  4393. *
  4394. * Encrypts triple DES using OFB block mode with two keys.
  4395. *
  4396. * param base CAAM peripheral base address
  4397. * param handle Handle used for this request. Specifies jobRing.
  4398. * param plaintext Input plaintext to encrypt
  4399. * param[out] ciphertext Output ciphertext
  4400. * param size Size of input and output data in bytes
  4401. * param iv Input unique input vector. The OFB mode requires that the IV be unique
  4402. * for each execution of the mode under the given key.
  4403. * param key1 First input key for key bundle
  4404. * param key2 Second input key for key bundle
  4405. * return Status from encrypt/decrypt operation
  4406. */
  4407. status_t CAAM_DES2_EncryptOfb(CAAM_Type *base,
  4408. caam_handle_t *handle,
  4409. const uint8_t *plaintext,
  4410. uint8_t *ciphertext,
  4411. size_t size,
  4412. const uint8_t iv[CAAM_DES_IV_SIZE],
  4413. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4414. const uint8_t key2[CAAM_DES_KEY_SIZE])
  4415. {
  4416. status_t status;
  4417. caam_desc_cipher_des_t descBuf;
  4418. do
  4419. {
  4420. status = CAAM_DES2_EncryptOfbNonBlocking(base, handle, descBuf, plaintext, ciphertext, size, iv, key1, key2);
  4421. } while (status == kStatus_CAAM_Again);
  4422. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  4423. }
  4424. /*!
  4425. * brief Encrypts triple DES using OFB block mode with two keys.
  4426. *
  4427. * Encrypts triple DES using OFB block mode with two keys.
  4428. *
  4429. * param base CAAM peripheral base address
  4430. * param handle Handle used for this request. Specifies jobRing.
  4431. * param[out] descriptor memory for CAAM commands
  4432. * param plaintext Input plaintext to encrypt
  4433. * param[out] ciphertext Output ciphertext
  4434. * param size Size of input and output data in bytes
  4435. * param iv Input unique input vector. The OFB mode requires that the IV be unique
  4436. * for each execution of the mode under the given key.
  4437. * param key1 First input key for key bundle
  4438. * param key2 Second input key for key bundle
  4439. * return Status from descriptor push
  4440. */
  4441. status_t CAAM_DES2_EncryptOfbNonBlocking(CAAM_Type *base,
  4442. caam_handle_t *handle,
  4443. caam_desc_cipher_des_t descriptor,
  4444. const uint8_t *plaintext,
  4445. uint8_t *ciphertext,
  4446. size_t size,
  4447. const uint8_t iv[CAAM_DES_IV_SIZE],
  4448. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4449. const uint8_t key2[CAAM_DES_KEY_SIZE])
  4450. {
  4451. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  4452. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  4453. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  4454. * Note: You can still call several times instead
  4455. */
  4456. if (size > 0xFFFFUL)
  4457. {
  4458. return kStatus_CAAM_DataOverflow;
  4459. }
  4460. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  4461. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  4462. descriptor[1] |= 2U * CAAM_DES_KEY_SIZE;
  4463. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key1, CAAM_DES_KEY_SIZE);
  4464. (void)caam_memcpy(&descriptor[4], (const uint32_t *)(uintptr_t)key2, CAAM_DES_KEY_SIZE);
  4465. descriptor[6] = DESC_JUMP_2; /* context, jump to currIdx+2 = 8 (LOAD) */
  4466. descriptor[9] = ADD_OFFSET((uint32_t)iv);
  4467. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4468. descriptor[11] = ADD_OFFSET((uint32_t)plaintext);
  4469. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4470. descriptor[13] = ADD_OFFSET((uint32_t)ciphertext);
  4471. descriptor[14] |= (uint32_t)kCAAM_ModeOFB; /* AAI = 40h = OFB */
  4472. descriptor[14] |= 1u; /* add ENC bit to specify Encrypt OPERATION */
  4473. descriptor[14] |= 0x10000U; /* 3DES */
  4474. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  4475. }
  4476. /*!
  4477. * brief Decrypts triple DES using OFB block mode with two keys.
  4478. *
  4479. * Decrypts triple DES using OFB block mode with two keys.
  4480. *
  4481. * param base CAAM peripheral base address
  4482. * param handle Handle used for this request. Specifies jobRing.
  4483. * param ciphertext Input ciphertext to decrypt
  4484. * param[out] plaintext Output plaintext
  4485. * param size Size of input and output data in bytes
  4486. * param iv Input unique input vector. The OFB mode requires that the IV be unique
  4487. * for each execution of the mode under the given key.
  4488. * param key1 First input key for key bundle
  4489. * param key2 Second input key for key bundle
  4490. * return Status from encrypt/decrypt operation
  4491. */
  4492. status_t CAAM_DES2_DecryptOfb(CAAM_Type *base,
  4493. caam_handle_t *handle,
  4494. const uint8_t *ciphertext,
  4495. uint8_t *plaintext,
  4496. size_t size,
  4497. const uint8_t iv[CAAM_DES_IV_SIZE],
  4498. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4499. const uint8_t key2[CAAM_DES_KEY_SIZE])
  4500. {
  4501. status_t status;
  4502. caam_desc_cipher_des_t descBuf;
  4503. do
  4504. {
  4505. status = CAAM_DES2_DecryptOfbNonBlocking(base, handle, descBuf, ciphertext, plaintext, size, iv, key1, key2);
  4506. } while (status == kStatus_CAAM_Again);
  4507. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  4508. }
  4509. /*!
  4510. * brief Decrypts triple DES using OFB block mode with two keys.
  4511. *
  4512. * Decrypts triple DES using OFB block mode with two keys.
  4513. *
  4514. * param base CAAM peripheral base address
  4515. * param handle Handle used for this request. Specifies jobRing.
  4516. * param[out] descriptor memory for CAAM commands
  4517. * param ciphertext Input ciphertext to decrypt
  4518. * param[out] plaintext Output plaintext
  4519. * param size Size of input and output data in bytes
  4520. * param iv Input unique input vector. The OFB mode requires that the IV be unique
  4521. * for each execution of the mode under the given key.
  4522. * param key1 First input key for key bundle
  4523. * param key2 Second input key for key bundle
  4524. * return Status from descriptor push
  4525. */
  4526. status_t CAAM_DES2_DecryptOfbNonBlocking(CAAM_Type *base,
  4527. caam_handle_t *handle,
  4528. caam_desc_cipher_des_t descriptor,
  4529. const uint8_t *ciphertext,
  4530. uint8_t *plaintext,
  4531. size_t size,
  4532. const uint8_t iv[CAAM_DES_IV_SIZE],
  4533. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4534. const uint8_t key2[CAAM_DES_KEY_SIZE])
  4535. {
  4536. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  4537. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  4538. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  4539. * Note: You can still call several times instead
  4540. */
  4541. if (size > 0xFFFFUL)
  4542. {
  4543. return kStatus_CAAM_DataOverflow;
  4544. }
  4545. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  4546. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  4547. descriptor[1] |= 2U * CAAM_DES_KEY_SIZE;
  4548. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key1, CAAM_DES_KEY_SIZE);
  4549. (void)caam_memcpy(&descriptor[4], (const uint32_t *)(uintptr_t)key2, CAAM_DES_KEY_SIZE);
  4550. descriptor[6] = DESC_JUMP_2; /* context, jump to currIdx+2 = 8 (LOAD) */
  4551. descriptor[9] = ADD_OFFSET((uint32_t)iv);
  4552. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4553. descriptor[11] = ADD_OFFSET((uint32_t)ciphertext);
  4554. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4555. descriptor[13] = ADD_OFFSET((uint32_t)plaintext);
  4556. descriptor[14] |= (uint32_t)kCAAM_ModeOFB; /* AAI = 40h = OFB */
  4557. descriptor[14] |= 0x10000U; /* 3DES */
  4558. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  4559. }
  4560. /*!
  4561. * brief Encrypts triple DES using ECB block mode with three keys.
  4562. *
  4563. * Encrypts triple DES using ECB block mode with three keys.
  4564. *
  4565. * param base CAAM peripheral base address
  4566. * param handle Handle used for this request. Specifies jobRing.
  4567. * param plaintext Input plaintext to encrypt
  4568. * param[out] ciphertext Output ciphertext
  4569. * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
  4570. * param key1 First input key for key bundle
  4571. * param key2 Second input key for key bundle
  4572. * param key3 Third input key for key bundle
  4573. * return Status from encrypt/decrypt operation
  4574. */
  4575. status_t CAAM_DES3_EncryptEcb(CAAM_Type *base,
  4576. caam_handle_t *handle,
  4577. const uint8_t *plaintext,
  4578. uint8_t *ciphertext,
  4579. size_t size,
  4580. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4581. const uint8_t key2[CAAM_DES_KEY_SIZE],
  4582. const uint8_t key3[CAAM_DES_KEY_SIZE])
  4583. {
  4584. caam_desc_cipher_des_t descBuf;
  4585. status_t status;
  4586. status = CAAM_DES3_EncryptEcbNonBlocking(base, handle, descBuf, plaintext, ciphertext, size, key1, key2, key3);
  4587. if (status != 0)
  4588. {
  4589. return status;
  4590. }
  4591. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  4592. }
  4593. /*!
  4594. * brief Encrypts triple DES using ECB block mode with three keys.
  4595. *
  4596. * Encrypts triple DES using ECB block mode with three keys.
  4597. *
  4598. * param base CAAM peripheral base address
  4599. * param handle Handle used for this request. Specifies jobRing.
  4600. * param[out] descriptor memory for CAAM commands
  4601. * param plaintext Input plaintext to encrypt
  4602. * param[out] ciphertext Output ciphertext
  4603. * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
  4604. * param key1 First input key for key bundle
  4605. * param key2 Second input key for key bundle
  4606. * param key3 Third input key for key bundle
  4607. * return Status from descriptor push
  4608. */
  4609. status_t CAAM_DES3_EncryptEcbNonBlocking(CAAM_Type *base,
  4610. caam_handle_t *handle,
  4611. caam_desc_cipher_des_t descriptor,
  4612. const uint8_t *plaintext,
  4613. uint8_t *ciphertext,
  4614. size_t size,
  4615. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4616. const uint8_t key2[CAAM_DES_KEY_SIZE],
  4617. const uint8_t key3[CAAM_DES_KEY_SIZE])
  4618. {
  4619. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  4620. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  4621. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  4622. * Note: You can still call several times instead
  4623. */
  4624. if (size > 0xFFFFUL)
  4625. {
  4626. return kStatus_CAAM_DataOverflow;
  4627. }
  4628. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  4629. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  4630. descriptor[1] |= 3U * CAAM_DES_KEY_SIZE;
  4631. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key1, CAAM_DES_KEY_SIZE);
  4632. (void)caam_memcpy(&descriptor[4], (const uint32_t *)(uintptr_t)key2, CAAM_DES_KEY_SIZE);
  4633. (void)caam_memcpy(&descriptor[6], (const uint32_t *)(uintptr_t)key3, CAAM_DES_KEY_SIZE);
  4634. descriptor[8] = DESC_JUMP_2; /* ECB has no context, jump to currIdx+2 = 10 (FIFO LOAD) */
  4635. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4636. descriptor[11] = ADD_OFFSET((uint32_t)plaintext);
  4637. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4638. descriptor[13] = ADD_OFFSET((uint32_t)ciphertext);
  4639. descriptor[14] |= 0x201u; /* add ENC bit to specify Encrypt OPERATION, AAI = 20h */
  4640. descriptor[14] |= 0x10000U; /* 3DES */
  4641. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  4642. }
  4643. /*!
  4644. * brief Decrypts triple DES using ECB block mode with three keys.
  4645. *
  4646. * Decrypts triple DES using ECB block mode with three keys.
  4647. *
  4648. * param base CAAM peripheral base address
  4649. * param handle Handle used for this request. Specifies jobRing.
  4650. * param ciphertext Input ciphertext to decrypt
  4651. * param[out] plaintext Output plaintext
  4652. * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
  4653. * param key1 First input key for key bundle
  4654. * param key2 Second input key for key bundle
  4655. * param key3 Third input key for key bundle
  4656. * return Status from encrypt/decrypt operation
  4657. */
  4658. status_t CAAM_DES3_DecryptEcb(CAAM_Type *base,
  4659. caam_handle_t *handle,
  4660. const uint8_t *ciphertext,
  4661. uint8_t *plaintext,
  4662. size_t size,
  4663. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4664. const uint8_t key2[CAAM_DES_KEY_SIZE],
  4665. const uint8_t key3[CAAM_DES_KEY_SIZE])
  4666. {
  4667. caam_desc_cipher_des_t descBuf;
  4668. status_t status;
  4669. status = CAAM_DES3_DecryptEcbNonBlocking(base, handle, descBuf, ciphertext, plaintext, size, key1, key2, key3);
  4670. if (status != 0)
  4671. {
  4672. return status;
  4673. }
  4674. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  4675. }
  4676. /*!
  4677. * brief Decrypts triple DES using ECB block mode with three keys.
  4678. *
  4679. * Decrypts triple DES using ECB block mode with three keys.
  4680. *
  4681. * param base CAAM peripheral base address
  4682. * param handle Handle used for this request. Specifies jobRing.
  4683. * param[out] descriptor memory for CAAM commands
  4684. * param ciphertext Input ciphertext to decrypt
  4685. * param[out] plaintext Output plaintext
  4686. * param size Size of input and output data in bytes. Must be multiple of 8 bytes.
  4687. * param key1 First input key for key bundle
  4688. * param key2 Second input key for key bundle
  4689. * param key3 Third input key for key bundle
  4690. * return Status from descriptor push
  4691. */
  4692. status_t CAAM_DES3_DecryptEcbNonBlocking(CAAM_Type *base,
  4693. caam_handle_t *handle,
  4694. caam_desc_cipher_des_t descriptor,
  4695. const uint8_t *ciphertext,
  4696. uint8_t *plaintext,
  4697. size_t size,
  4698. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4699. const uint8_t key2[CAAM_DES_KEY_SIZE],
  4700. const uint8_t key3[CAAM_DES_KEY_SIZE])
  4701. {
  4702. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  4703. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  4704. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  4705. * Note: You can still call several times instead
  4706. */
  4707. if (size > 0xFFFFUL)
  4708. {
  4709. return kStatus_CAAM_DataOverflow;
  4710. }
  4711. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  4712. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  4713. descriptor[1] |= 3U * CAAM_DES_KEY_SIZE;
  4714. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key1, CAAM_DES_KEY_SIZE);
  4715. (void)caam_memcpy(&descriptor[4], (const uint32_t *)(uintptr_t)key2, CAAM_DES_KEY_SIZE);
  4716. (void)caam_memcpy(&descriptor[6], (const uint32_t *)(uintptr_t)key3, CAAM_DES_KEY_SIZE);
  4717. descriptor[8] = DESC_JUMP_2; /* ECB has no context, jump to currIdx+2 = 10 (FIFO LOAD) */
  4718. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4719. descriptor[11] = ADD_OFFSET((uint32_t)ciphertext);
  4720. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4721. descriptor[13] = ADD_OFFSET((uint32_t)plaintext);
  4722. descriptor[14] |= (uint32_t)kCAAM_ModeECB; /* AAI = 20h */
  4723. descriptor[14] |= 0x10000U; /* 3DES */
  4724. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  4725. }
  4726. /*!
  4727. * brief Encrypts triple DES using CBC block mode with three keys.
  4728. *
  4729. * Encrypts triple DES using CBC block mode with three keys.
  4730. *
  4731. * param base CAAM peripheral base address
  4732. * param handle Handle used for this request. Specifies jobRing.
  4733. * param plaintext Input plaintext to encrypt
  4734. * param[out] ciphertext Output ciphertext
  4735. * param size Size of input data in bytes
  4736. * param iv Input initial vector to combine with the first plaintext block.
  4737. * The iv does not need to be secret, but it must be unpredictable.
  4738. * param key1 First input key for key bundle
  4739. * param key2 Second input key for key bundle
  4740. * param key3 Third input key for key bundle
  4741. * return Status from encrypt/decrypt operation
  4742. */
  4743. status_t CAAM_DES3_EncryptCbc(CAAM_Type *base,
  4744. caam_handle_t *handle,
  4745. const uint8_t *plaintext,
  4746. uint8_t *ciphertext,
  4747. size_t size,
  4748. const uint8_t iv[CAAM_DES_IV_SIZE],
  4749. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4750. const uint8_t key2[CAAM_DES_KEY_SIZE],
  4751. const uint8_t key3[CAAM_DES_KEY_SIZE])
  4752. {
  4753. status_t status;
  4754. caam_desc_cipher_des_t descBuf;
  4755. do
  4756. {
  4757. status =
  4758. CAAM_DES3_EncryptCbcNonBlocking(base, handle, descBuf, plaintext, ciphertext, size, iv, key1, key2, key3);
  4759. } while (status == kStatus_CAAM_Again);
  4760. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  4761. }
  4762. /*!
  4763. * brief Encrypts triple DES using CBC block mode with three keys.
  4764. *
  4765. * Encrypts triple DES using CBC block mode with three keys.
  4766. *
  4767. * param base CAAM peripheral base address
  4768. * param handle Handle used for this request. Specifies jobRing.
  4769. * param[out] descriptor memory for CAAM commands
  4770. * param plaintext Input plaintext to encrypt
  4771. * param[out] ciphertext Output ciphertext
  4772. * param size Size of input data in bytes
  4773. * param iv Input initial vector to combine with the first plaintext block.
  4774. * The iv does not need to be secret, but it must be unpredictable.
  4775. * param key1 First input key for key bundle
  4776. * param key2 Second input key for key bundle
  4777. * param key3 Third input key for key bundle
  4778. * return Status from descriptor push
  4779. */
  4780. status_t CAAM_DES3_EncryptCbcNonBlocking(CAAM_Type *base,
  4781. caam_handle_t *handle,
  4782. caam_desc_cipher_des_t descriptor,
  4783. const uint8_t *plaintext,
  4784. uint8_t *ciphertext,
  4785. size_t size,
  4786. const uint8_t iv[CAAM_DES_IV_SIZE],
  4787. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4788. const uint8_t key2[CAAM_DES_KEY_SIZE],
  4789. const uint8_t key3[CAAM_DES_KEY_SIZE])
  4790. {
  4791. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  4792. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  4793. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  4794. * Note: You can still call several times instead
  4795. */
  4796. if (size > 0xFFFFUL)
  4797. {
  4798. return kStatus_CAAM_DataOverflow;
  4799. }
  4800. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  4801. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  4802. descriptor[1] |= 3U * CAAM_DES_KEY_SIZE;
  4803. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key1, CAAM_DES_KEY_SIZE);
  4804. (void)caam_memcpy(&descriptor[4], (const uint32_t *)(uintptr_t)key2, CAAM_DES_KEY_SIZE);
  4805. (void)caam_memcpy(&descriptor[6], (const uint32_t *)(uintptr_t)key3, CAAM_DES_KEY_SIZE);
  4806. descriptor[9] = ADD_OFFSET((uint32_t)iv);
  4807. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4808. descriptor[11] = ADD_OFFSET((uint32_t)plaintext);
  4809. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4810. descriptor[13] = ADD_OFFSET((uint32_t)ciphertext);
  4811. descriptor[14] |= (uint32_t)kCAAM_ModeCBC; /* AAI = 10h */
  4812. descriptor[14] |= 1u; /* add ENC bit to specify Encrypt OPERATION */
  4813. descriptor[14] |= 0x10000U; /* 3DES */
  4814. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  4815. }
  4816. /*!
  4817. * brief Decrypts triple DES using CBC block mode with three keys.
  4818. *
  4819. * Decrypts triple DES using CBC block mode with three keys.
  4820. *
  4821. * param base CAAM peripheral base address
  4822. * param handle Handle used for this request. Specifies jobRing.
  4823. * param ciphertext Input ciphertext to decrypt
  4824. * param[out] plaintext Output plaintext
  4825. * param size Size of input and output data in bytes
  4826. * param iv Input initial vector to combine with the first plaintext block.
  4827. * The iv does not need to be secret, but it must be unpredictable.
  4828. * param key1 First input key for key bundle
  4829. * param key2 Second input key for key bundle
  4830. * param key3 Third input key for key bundle
  4831. * return Status from encrypt/decrypt operation
  4832. */
  4833. status_t CAAM_DES3_DecryptCbc(CAAM_Type *base,
  4834. caam_handle_t *handle,
  4835. const uint8_t *ciphertext,
  4836. uint8_t *plaintext,
  4837. size_t size,
  4838. const uint8_t iv[CAAM_DES_IV_SIZE],
  4839. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4840. const uint8_t key2[CAAM_DES_KEY_SIZE],
  4841. const uint8_t key3[CAAM_DES_KEY_SIZE])
  4842. {
  4843. status_t status;
  4844. caam_desc_cipher_des_t descBuf;
  4845. do
  4846. {
  4847. status =
  4848. CAAM_DES3_DecryptCbcNonBlocking(base, handle, descBuf, ciphertext, plaintext, size, iv, key1, key2, key3);
  4849. } while (status == kStatus_CAAM_Again);
  4850. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  4851. }
  4852. /*!
  4853. * brief Decrypts triple DES using CBC block mode with three keys.
  4854. *
  4855. * Decrypts triple DES using CBC block mode with three keys.
  4856. *
  4857. * param base CAAM peripheral base address
  4858. * param handle Handle used for this request. Specifies jobRing.
  4859. * param[out] descriptor memory for CAAM commands
  4860. * param ciphertext Input ciphertext to decrypt
  4861. * param[out] plaintext Output plaintext
  4862. * param size Size of input and output data in bytes
  4863. * param iv Input initial vector to combine with the first plaintext block.
  4864. * The iv does not need to be secret, but it must be unpredictable.
  4865. * param key1 First input key for key bundle
  4866. * param key2 Second input key for key bundle
  4867. * param key3 Third input key for key bundle
  4868. * return Status from descriptor push
  4869. */
  4870. status_t CAAM_DES3_DecryptCbcNonBlocking(CAAM_Type *base,
  4871. caam_handle_t *handle,
  4872. caam_desc_cipher_des_t descriptor,
  4873. const uint8_t *ciphertext,
  4874. uint8_t *plaintext,
  4875. size_t size,
  4876. const uint8_t iv[CAAM_DES_IV_SIZE],
  4877. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4878. const uint8_t key2[CAAM_DES_KEY_SIZE],
  4879. const uint8_t key3[CAAM_DES_KEY_SIZE])
  4880. {
  4881. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  4882. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  4883. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  4884. * Note: You can still call several times instead
  4885. */
  4886. if (size > 0xFFFFUL)
  4887. {
  4888. return kStatus_CAAM_DataOverflow;
  4889. }
  4890. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  4891. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  4892. descriptor[1] |= 3U * CAAM_DES_KEY_SIZE;
  4893. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key1, CAAM_DES_KEY_SIZE);
  4894. (void)caam_memcpy(&descriptor[4], (const uint32_t *)(uintptr_t)key2, CAAM_DES_KEY_SIZE);
  4895. (void)caam_memcpy(&descriptor[6], (const uint32_t *)(uintptr_t)key3, CAAM_DES_KEY_SIZE);
  4896. descriptor[9] = ADD_OFFSET((uint32_t)iv);
  4897. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4898. descriptor[11] = ADD_OFFSET((uint32_t)ciphertext);
  4899. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4900. descriptor[13] = ADD_OFFSET((uint32_t)plaintext);
  4901. descriptor[14] |= (uint32_t)kCAAM_ModeCBC; /* AAI = 10h */
  4902. descriptor[14] |= 0x10000U; /* 3DES */
  4903. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  4904. }
  4905. /*!
  4906. * brief Encrypts triple DES using CFB block mode with three keys.
  4907. *
  4908. * Encrypts triple DES using CFB block mode with three keys.
  4909. *
  4910. * param base CAAM peripheral base address
  4911. * param plaintext Input plaintext to encrypt
  4912. * param[out] ciphertext Output ciphertext
  4913. * param size Size of input and ouput data in bytes
  4914. * param iv Input initial block.
  4915. * param key1 First input key for key bundle
  4916. * param key2 Second input key for key bundle
  4917. * param key3 Third input key for key bundle
  4918. * return Status from encrypt/decrypt operation
  4919. */
  4920. status_t CAAM_DES3_EncryptCfb(CAAM_Type *base,
  4921. caam_handle_t *handle,
  4922. const uint8_t *plaintext,
  4923. uint8_t *ciphertext,
  4924. size_t size,
  4925. const uint8_t iv[CAAM_DES_IV_SIZE],
  4926. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4927. const uint8_t key2[CAAM_DES_KEY_SIZE],
  4928. const uint8_t key3[CAAM_DES_KEY_SIZE])
  4929. {
  4930. status_t status;
  4931. caam_desc_cipher_des_t descBuf;
  4932. do
  4933. {
  4934. status =
  4935. CAAM_DES3_EncryptCfbNonBlocking(base, handle, descBuf, plaintext, ciphertext, size, iv, key1, key2, key3);
  4936. } while (status == kStatus_CAAM_Again);
  4937. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  4938. }
  4939. /*!
  4940. * brief Encrypts triple DES using CFB block mode with three keys.
  4941. *
  4942. * Encrypts triple DES using CFB block mode with three keys.
  4943. *
  4944. * param base CAAM peripheral base address
  4945. * param handle Handle used for this request. Specifies jobRing.
  4946. * param[out] descriptor memory for CAAM commands
  4947. * param plaintext Input plaintext to encrypt
  4948. * param[out] ciphertext Output ciphertext
  4949. * param size Size of input and ouput data in bytes
  4950. * param iv Input initial block.
  4951. * param key1 First input key for key bundle
  4952. * param key2 Second input key for key bundle
  4953. * param key3 Third input key for key bundle
  4954. * return Status from descriptor push
  4955. */
  4956. status_t CAAM_DES3_EncryptCfbNonBlocking(CAAM_Type *base,
  4957. caam_handle_t *handle,
  4958. caam_desc_cipher_des_t descriptor,
  4959. const uint8_t *plaintext,
  4960. uint8_t *ciphertext,
  4961. size_t size,
  4962. const uint8_t iv[CAAM_DES_IV_SIZE],
  4963. const uint8_t key1[CAAM_DES_KEY_SIZE],
  4964. const uint8_t key2[CAAM_DES_KEY_SIZE],
  4965. const uint8_t key3[CAAM_DES_KEY_SIZE])
  4966. {
  4967. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  4968. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  4969. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  4970. * Note: You can still call several times instead
  4971. */
  4972. if (size > 0xFFFFUL)
  4973. {
  4974. return kStatus_CAAM_DataOverflow;
  4975. }
  4976. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  4977. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  4978. descriptor[1] |= 3U * CAAM_DES_KEY_SIZE;
  4979. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key1, CAAM_DES_KEY_SIZE);
  4980. (void)caam_memcpy(&descriptor[4], (const uint32_t *)(uintptr_t)key2, CAAM_DES_KEY_SIZE);
  4981. (void)caam_memcpy(&descriptor[6], (const uint32_t *)(uintptr_t)key3, CAAM_DES_KEY_SIZE);
  4982. descriptor[9] = ADD_OFFSET((uint32_t)iv);
  4983. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4984. descriptor[11] = ADD_OFFSET((uint32_t)plaintext);
  4985. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  4986. descriptor[13] = ADD_OFFSET((uint32_t)ciphertext);
  4987. descriptor[14] |= (uint32_t)kCAAM_ModeCFB; /* AAI = 30h = CFB */
  4988. descriptor[14] |= 1u; /* add ENC bit to specify Encrypt OPERATION */
  4989. descriptor[14] |= 0x10000U; /* 3DES */
  4990. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  4991. }
  4992. /*!
  4993. * brief Decrypts triple DES using CFB block mode with three keys.
  4994. *
  4995. * Decrypts triple DES using CFB block mode with three keys.
  4996. *
  4997. * param base CAAM peripheral base address
  4998. * param handle Handle used for this request. Specifies jobRing.
  4999. * param ciphertext Input ciphertext to decrypt
  5000. * param[out] plaintext Output plaintext
  5001. * param size Size of input data in bytes
  5002. * param iv Input initial block.
  5003. * param key1 First input key for key bundle
  5004. * param key2 Second input key for key bundle
  5005. * param key3 Third input key for key bundle
  5006. * return Status from encrypt/decrypt operation
  5007. */
  5008. status_t CAAM_DES3_DecryptCfb(CAAM_Type *base,
  5009. caam_handle_t *handle,
  5010. const uint8_t *ciphertext,
  5011. uint8_t *plaintext,
  5012. size_t size,
  5013. const uint8_t iv[CAAM_DES_IV_SIZE],
  5014. const uint8_t key1[CAAM_DES_KEY_SIZE],
  5015. const uint8_t key2[CAAM_DES_KEY_SIZE],
  5016. const uint8_t key3[CAAM_DES_KEY_SIZE])
  5017. {
  5018. status_t status;
  5019. caam_desc_cipher_des_t descBuf;
  5020. do
  5021. {
  5022. status =
  5023. CAAM_DES3_DecryptCfbNonBlocking(base, handle, descBuf, ciphertext, plaintext, size, iv, key1, key2, key3);
  5024. } while (status == kStatus_CAAM_Again);
  5025. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  5026. }
  5027. /*!
  5028. * brief Decrypts triple DES using CFB block mode with three keys.
  5029. *
  5030. * Decrypts triple DES using CFB block mode with three keys.
  5031. *
  5032. * param base CAAM peripheral base address
  5033. * param handle Handle used for this request. Specifies jobRing.
  5034. * param[out] descriptor memory for CAAM commands
  5035. * param ciphertext Input ciphertext to decrypt
  5036. * param[out] plaintext Output plaintext
  5037. * param size Size of input data in bytes
  5038. * param iv Input initial block.
  5039. * param key1 First input key for key bundle
  5040. * param key2 Second input key for key bundle
  5041. * param key3 Third input key for key bundle
  5042. * return Status from descriptor push
  5043. */
  5044. status_t CAAM_DES3_DecryptCfbNonBlocking(CAAM_Type *base,
  5045. caam_handle_t *handle,
  5046. caam_desc_cipher_des_t descriptor,
  5047. const uint8_t *ciphertext,
  5048. uint8_t *plaintext,
  5049. size_t size,
  5050. const uint8_t iv[CAAM_DES_IV_SIZE],
  5051. const uint8_t key1[CAAM_DES_KEY_SIZE],
  5052. const uint8_t key2[CAAM_DES_KEY_SIZE],
  5053. const uint8_t key3[CAAM_DES_KEY_SIZE])
  5054. {
  5055. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  5056. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  5057. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  5058. * Note: You can still call several times instead
  5059. */
  5060. if (size > 0xFFFFUL)
  5061. {
  5062. return kStatus_CAAM_DataOverflow;
  5063. }
  5064. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  5065. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  5066. descriptor[1] |= 3U * CAAM_DES_KEY_SIZE;
  5067. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key1, CAAM_DES_KEY_SIZE);
  5068. (void)caam_memcpy(&descriptor[4], (const uint32_t *)(uintptr_t)key2, CAAM_DES_KEY_SIZE);
  5069. (void)caam_memcpy(&descriptor[6], (const uint32_t *)(uintptr_t)key3, CAAM_DES_KEY_SIZE);
  5070. descriptor[9] = ADD_OFFSET((uint32_t)iv);
  5071. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  5072. descriptor[11] = ADD_OFFSET((uint32_t)ciphertext);
  5073. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  5074. descriptor[13] = ADD_OFFSET((uint32_t)plaintext);
  5075. descriptor[14] |= (uint32_t)kCAAM_ModeCFB; /* AAI = 30h = CFB */
  5076. descriptor[14] |= 0x10000U; /* 3DES */
  5077. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  5078. }
  5079. /*!
  5080. * brief Encrypts triple DES using OFB block mode with three keys.
  5081. *
  5082. * Encrypts triple DES using OFB block mode with three keys.
  5083. *
  5084. * param base CAAM peripheral base address
  5085. * param plaintext Input plaintext to encrypt
  5086. * param[out] ciphertext Output ciphertext
  5087. * param size Size of input and output data in bytes
  5088. * param iv Input unique input vector. The OFB mode requires that the IV be unique
  5089. * for each execution of the mode under the given key.
  5090. * param key1 First input key for key bundle
  5091. * param key2 Second input key for key bundle
  5092. * param key3 Third input key for key bundle
  5093. * return Status from encrypt/decrypt operation
  5094. */
  5095. status_t CAAM_DES3_EncryptOfb(CAAM_Type *base,
  5096. caam_handle_t *handle,
  5097. const uint8_t *plaintext,
  5098. uint8_t *ciphertext,
  5099. size_t size,
  5100. const uint8_t iv[CAAM_DES_IV_SIZE],
  5101. const uint8_t key1[CAAM_DES_KEY_SIZE],
  5102. const uint8_t key2[CAAM_DES_KEY_SIZE],
  5103. const uint8_t key3[CAAM_DES_KEY_SIZE])
  5104. {
  5105. status_t status;
  5106. caam_desc_cipher_des_t descBuf;
  5107. do
  5108. {
  5109. status =
  5110. CAAM_DES3_EncryptOfbNonBlocking(base, handle, descBuf, plaintext, ciphertext, size, iv, key1, key2, key3);
  5111. } while (status == kStatus_CAAM_Again);
  5112. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  5113. }
  5114. /*!
  5115. * brief Encrypts triple DES using OFB block mode with three keys.
  5116. *
  5117. * Encrypts triple DES using OFB block mode with three keys.
  5118. *
  5119. * param base CAAM peripheral base address
  5120. * param handle Handle used for this request. Specifies jobRing.
  5121. * param[out] descriptor memory for CAAM commands
  5122. * param plaintext Input plaintext to encrypt
  5123. * param[out] ciphertext Output ciphertext
  5124. * param size Size of input and output data in bytes
  5125. * param iv Input unique input vector. The OFB mode requires that the IV be unique
  5126. * for each execution of the mode under the given key.
  5127. * param key1 First input key for key bundle
  5128. * param key2 Second input key for key bundle
  5129. * param key3 Third input key for key bundle
  5130. * return Status from descriptor push
  5131. */
  5132. status_t CAAM_DES3_EncryptOfbNonBlocking(CAAM_Type *base,
  5133. caam_handle_t *handle,
  5134. caam_desc_cipher_des_t descriptor,
  5135. const uint8_t *plaintext,
  5136. uint8_t *ciphertext,
  5137. size_t size,
  5138. const uint8_t iv[CAAM_DES_IV_SIZE],
  5139. const uint8_t key1[CAAM_DES_KEY_SIZE],
  5140. const uint8_t key2[CAAM_DES_KEY_SIZE],
  5141. const uint8_t key3[CAAM_DES_KEY_SIZE])
  5142. {
  5143. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  5144. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  5145. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  5146. * Note: You can still call several times instead
  5147. */
  5148. if (size > 0xFFFFUL)
  5149. {
  5150. return kStatus_CAAM_DataOverflow;
  5151. }
  5152. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  5153. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  5154. descriptor[1] |= 3U * CAAM_DES_KEY_SIZE;
  5155. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key1, CAAM_DES_KEY_SIZE);
  5156. (void)caam_memcpy(&descriptor[4], (const uint32_t *)(uintptr_t)key2, CAAM_DES_KEY_SIZE);
  5157. (void)caam_memcpy(&descriptor[6], (const uint32_t *)(uintptr_t)key3, CAAM_DES_KEY_SIZE);
  5158. descriptor[9] = ADD_OFFSET((uint32_t)iv);
  5159. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  5160. descriptor[11] = ADD_OFFSET((uint32_t)plaintext);
  5161. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  5162. descriptor[13] = ADD_OFFSET((uint32_t)ciphertext);
  5163. descriptor[14] |= (uint32_t)kCAAM_ModeOFB; /* AAI = 40h = OFB */
  5164. descriptor[14] |= 1u; /* add ENC bit to specify Encrypt OPERATION */
  5165. descriptor[14] |= 0x10000U; /* 3DES */
  5166. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  5167. }
  5168. /*!
  5169. * brief Decrypts triple DES using OFB block mode with three keys.
  5170. *
  5171. * Decrypts triple DES using OFB block mode with three keys.
  5172. *
  5173. * param base CAAM peripheral base address
  5174. * param handle Handle used for this request. Specifies jobRing.
  5175. * param ciphertext Input ciphertext to decrypt
  5176. * param[out] plaintext Output plaintext
  5177. * param size Size of input and output data in bytes
  5178. * param iv Input unique input vector. The OFB mode requires that the IV be unique
  5179. * for each execution of the mode under the given key.
  5180. * param key1 First input key for key bundle
  5181. * param key2 Second input key for key bundle
  5182. * param key3 Third input key for key bundle
  5183. * return Status from encrypt/decrypt operation
  5184. */
  5185. status_t CAAM_DES3_DecryptOfb(CAAM_Type *base,
  5186. caam_handle_t *handle,
  5187. const uint8_t *ciphertext,
  5188. uint8_t *plaintext,
  5189. size_t size,
  5190. const uint8_t iv[CAAM_DES_IV_SIZE],
  5191. const uint8_t key1[CAAM_DES_KEY_SIZE],
  5192. const uint8_t key2[CAAM_DES_KEY_SIZE],
  5193. const uint8_t key3[CAAM_DES_KEY_SIZE])
  5194. {
  5195. status_t status;
  5196. caam_desc_cipher_des_t descBuf;
  5197. do
  5198. {
  5199. status =
  5200. CAAM_DES3_DecryptOfbNonBlocking(base, handle, descBuf, ciphertext, plaintext, size, iv, key1, key2, key3);
  5201. } while (status == kStatus_CAAM_Again);
  5202. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  5203. }
  5204. /*!
  5205. * brief Decrypts triple DES using OFB block mode with three keys.
  5206. *
  5207. * Decrypts triple DES using OFB block mode with three keys.
  5208. *
  5209. * param base CAAM peripheral base address
  5210. * param handle Handle used for this request. Specifies jobRing.
  5211. * param[out] descriptor memory for CAAM commands
  5212. * param ciphertext Input ciphertext to decrypt
  5213. * param[out] plaintext Output plaintext
  5214. * param size Size of input and output data in bytes
  5215. * param iv Input unique input vector. The OFB mode requires that the IV be unique
  5216. * for each execution of the mode under the given key.
  5217. * param key1 First input key for key bundle
  5218. * param key2 Second input key for key bundle
  5219. * param key3 Third input key for key bundle
  5220. * return Status from descriptor push
  5221. */
  5222. status_t CAAM_DES3_DecryptOfbNonBlocking(CAAM_Type *base,
  5223. caam_handle_t *handle,
  5224. caam_desc_cipher_des_t descriptor,
  5225. const uint8_t *ciphertext,
  5226. uint8_t *plaintext,
  5227. size_t size,
  5228. const uint8_t iv[CAAM_DES_IV_SIZE],
  5229. const uint8_t key1[CAAM_DES_KEY_SIZE],
  5230. const uint8_t key2[CAAM_DES_KEY_SIZE],
  5231. const uint8_t key3[CAAM_DES_KEY_SIZE])
  5232. {
  5233. uint32_t descriptorSize = ARRAY_SIZE(templateCipherDes);
  5234. /* DES do not support EXTENDED lenght in FIFO LOAD/STORE command.
  5235. * Data lenght limit is 2^16 bytes = 65 536 bytes.
  5236. * Note: You can still call several times instead
  5237. */
  5238. if (size > 0xFFFFUL)
  5239. {
  5240. return kStatus_CAAM_DataOverflow;
  5241. }
  5242. (void)caam_memcpy(descriptor, templateCipherDes, sizeof(templateCipherDes));
  5243. descriptor[0] |= (descriptorSize & DESC_SIZE_MASK);
  5244. descriptor[1] |= 3U * CAAM_DES_KEY_SIZE;
  5245. (void)caam_memcpy(&descriptor[2], (const uint32_t *)(uintptr_t)key1, CAAM_DES_KEY_SIZE);
  5246. (void)caam_memcpy(&descriptor[4], (const uint32_t *)(uintptr_t)key2, CAAM_DES_KEY_SIZE);
  5247. (void)caam_memcpy(&descriptor[6], (const uint32_t *)(uintptr_t)key3, CAAM_DES_KEY_SIZE);
  5248. descriptor[9] = ADD_OFFSET((uint32_t)iv);
  5249. descriptor[10] |= (size & DESC_PAYLOAD_SIZE_MASK);
  5250. descriptor[11] = ADD_OFFSET((uint32_t)ciphertext);
  5251. descriptor[12] |= (size & DESC_PAYLOAD_SIZE_MASK);
  5252. descriptor[13] = ADD_OFFSET((uint32_t)plaintext);
  5253. descriptor[14] |= (uint32_t)kCAAM_ModeOFB; /* AAI = 40h = OFB */
  5254. descriptor[14] |= 0x10000U; /* 3DES */
  5255. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  5256. }
  5257. #define DESC_HEADER 0xB0800000u
  5258. #define DESC_HEADER_ADD_DESCLEN(cmdWord, len) ((cmdWord) |= (len))
  5259. #define DESC_FLOADA 0x220C0000u
  5260. #define DESC_FLOADB 0x220D0000u
  5261. #define DESC_FLOADN 0x22080000u
  5262. #define DESC_KEY_E_ 0x02010000u
  5263. #define DESC_STOREB 0x600D0000u
  5264. #define DESC_STORE_ 0x52110004u
  5265. #define DESC_ADD_LEN(cmdWord, len) ((cmdWord) |= (len))
  5266. #define DESC_SET_ADDR(cmdWord, addr) ((cmdWord) = ADD_OFFSET((uint32_t)(addr)))
  5267. #define DESC_FLOADA0 0x22000000u
  5268. #define DESC_FLOADA1 0x22010000u
  5269. #define DESC_FLOADA3 0x22030000u
  5270. #define DESC_FLOADB0 0x22040000u
  5271. #define DESC_FLOADB1 0x22050000u
  5272. #define DESC_FLOADB2 0x22060000u
  5273. #define DESC_FLOADB3 0x22070000u
  5274. #define DESC_STOREB1 0x60050000u
  5275. #define DESC_STOREB2 0x60060000u
  5276. static const uint32_t templateArithmeticPKHA[] = {
  5277. /* 00 */ DESC_HEADER, /* HEADER */
  5278. /* 01 */ 0x81800001u, /* OPERATION: PKHA OPERATION: clear memory function. */
  5279. /* 02 */ 0xA2000001u, /* JMP always to next command. Done checkpoint (wait for Class 1 Done) */
  5280. /* 03 */ DESC_FLOADN, /* FIFO LOAD PKHA N */
  5281. /* 04 */ 0x00000000u, /* place: N address */
  5282. /* 05 */ DESC_FLOADA, /* FIFO LOAD PKHA A */
  5283. /* 06 */ 0x00000000u, /* place: A address */
  5284. /* 07 */ DESC_FLOADB, /* FIFO LOAD PKHA B */
  5285. /* 08 */ 0x00000000u, /* place: B address */
  5286. /* 09 */ DESC_KEY_E_, /* KEY PKHA E */
  5287. /* 10 */ 0x00000000u, /* place: E address */
  5288. /* 11 */ 0x81800000u, /* OPERATION: PKHA OPERATION: Arithmetic Functions. */
  5289. /* 12 */ DESC_STOREB, /* FIFO STORE PKHA B */
  5290. /* 13 */ 0x00000000u, /* place: result address */
  5291. };
  5292. /*! @brief PKHA functions - arithmetic, copy/clear memory. */
  5293. typedef enum _caam_pkha_func_t
  5294. {
  5295. kCAAM_PKHA_ClearMem = 1U,
  5296. kCAAM_PKHA_ArithModAdd = 2U, /*!< (A + B) mod N */
  5297. kCAAM_PKHA_ArithModSub1 = 3U, /*!< (A - B) mod N */
  5298. kCAAM_PKHA_ArithModSub2 = 4U, /*!< (B - A) mod N */
  5299. kCAAM_PKHA_ArithModMul = 5U, /*!< (A x B) mod N */
  5300. kCAAM_PKHA_ArithModExp = 6U, /*!< (A^E) mod N */
  5301. kCAAM_PKHA_ArithModRed = 7U, /*!< (A) mod N */
  5302. kCAAM_PKHA_ArithModInv = 8U, /*!< (A^-1) mod N */
  5303. kCAAM_PKHA_ArithEccAdd = 9U, /*!< (P1 + P2) */
  5304. kCAAM_PKHA_ArithEccDouble = 10U, /*!< (P2 + P2) */
  5305. kCAAM_PKHA_ArithEccMul = 11U, /*!< (E x P1) */
  5306. kCAAM_PKHA_ArithModR2 = 12U, /*!< (R^2 mod N) */
  5307. kCAAM_PKHA_ArithGcd = 14U, /*!< GCD (A, N) */
  5308. kCAAM_PKHA_ArithPrimalityTest = 15U, /*!< Miller-Rabin */
  5309. kCAAM_PKHA_CopyMemSizeN = 16U,
  5310. kCAAM_PKHA_CopyMemSizeSrc = 17U,
  5311. } caam_pkha_func_t;
  5312. /*! @brief Register areas for PKHA clear memory operations. */
  5313. typedef enum _caam_pkha_reg_area
  5314. {
  5315. kCAAM_PKHA_RegA = 8U,
  5316. kCAAM_PKHA_RegB = 4U,
  5317. kCAAM_PKHA_RegE = 2U,
  5318. kCAAM_PKHA_RegN = 1U,
  5319. kCAAM_PKHA_RegAll = kCAAM_PKHA_RegA | kCAAM_PKHA_RegB | kCAAM_PKHA_RegE | kCAAM_PKHA_RegN,
  5320. } caam_pkha_reg_area_t;
  5321. /*! @brief Quadrant areas for 4096-bit registers for PKHA copy memory
  5322. * operations. */
  5323. typedef enum _caam_pkha_quad_area_t
  5324. {
  5325. kCAAM_PKHA_Quad0 = 0U,
  5326. kCAAM_PKHA_Quad1 = 1U,
  5327. kCAAM_PKHA_Quad2 = 2U,
  5328. kCAAM_PKHA_Quad3 = 3U,
  5329. } caam_pkha_quad_area_t;
  5330. /*! @brief User-supplied (R^2 mod N) input or CAAM should calculate. */
  5331. typedef enum _caam_pkha_r2_t
  5332. {
  5333. kCAAM_PKHA_CalcR2 = 0U, /*!< Calculate (R^2 mod N) */
  5334. kCAAM_PKHA_InputR2 = 1U /*!< (R^2 mod N) supplied as input */
  5335. } caam_pkha_r2_t;
  5336. /*! @brief CAAM PKHA parameters */
  5337. typedef struct _caam_pkha_mode_params_t
  5338. {
  5339. caam_pkha_func_t func;
  5340. caam_pkha_f2m_t arithType;
  5341. caam_pkha_montgomery_form_t montFormIn;
  5342. caam_pkha_montgomery_form_t montFormOut;
  5343. caam_pkha_reg_area_t srcReg;
  5344. caam_pkha_quad_area_t srcQuad;
  5345. caam_pkha_reg_area_t dstReg;
  5346. caam_pkha_quad_area_t dstQuad;
  5347. caam_pkha_timing_t equalTime;
  5348. caam_pkha_r2_t r2modn;
  5349. } caam_pkha_mode_params_t;
  5350. static void caam_pkha_default_parms(caam_pkha_mode_params_t *params)
  5351. {
  5352. params->func = (caam_pkha_func_t)0;
  5353. params->arithType = kCAAM_PKHA_IntegerArith;
  5354. params->montFormIn = kCAAM_PKHA_NormalValue;
  5355. params->montFormOut = kCAAM_PKHA_NormalValue;
  5356. params->srcReg = kCAAM_PKHA_RegAll;
  5357. params->srcQuad = kCAAM_PKHA_Quad0;
  5358. params->dstReg = kCAAM_PKHA_RegAll;
  5359. params->dstQuad = kCAAM_PKHA_Quad0;
  5360. params->equalTime = kCAAM_PKHA_NoTimingEqualized;
  5361. params->r2modn = kCAAM_PKHA_CalcR2;
  5362. }
  5363. static void caam_pkha_mode_set_src_reg_copy(uint32_t *outMode, caam_pkha_reg_area_t reg)
  5364. {
  5365. int i = 0;
  5366. do
  5367. {
  5368. reg = (caam_pkha_reg_area_t)(uint32_t)(((uint32_t)reg) >> 1u);
  5369. i++;
  5370. } while (0U != (uint32_t)reg);
  5371. i = 4 - i;
  5372. /* Source register must not be E. */
  5373. if (i != 2)
  5374. {
  5375. *outMode |= ((uint32_t)i << 17u);
  5376. }
  5377. }
  5378. static void caam_pkha_mode_set_dst_reg_copy(uint32_t *outMode, caam_pkha_reg_area_t reg)
  5379. {
  5380. int i = 0;
  5381. do
  5382. {
  5383. reg = (caam_pkha_reg_area_t)(uint32_t)(((uint32_t)reg) >> 1u);
  5384. i++;
  5385. } while (0U != (uint32_t)reg);
  5386. i = 4 - i;
  5387. *outMode |= ((uint32_t)i << 10u);
  5388. }
  5389. static void caam_pkha_mode_set_src_seg_copy(uint32_t *outMode, const caam_pkha_quad_area_t quad)
  5390. {
  5391. *outMode |= ((uint32_t)quad << 8u);
  5392. }
  5393. static void caam_pkha_mode_set_dst_seg_copy(uint32_t *outMode, const caam_pkha_quad_area_t quad)
  5394. {
  5395. *outMode |= ((uint32_t)quad << 6u);
  5396. }
  5397. static uint32_t caam_pkha_get_mode(const caam_pkha_mode_params_t *params)
  5398. {
  5399. uint32_t modeReg;
  5400. /* Set the PKHA algorithm and the appropriate function. */
  5401. modeReg = (uint32_t)params->func;
  5402. if ((params->func == kCAAM_PKHA_CopyMemSizeN) || (params->func == kCAAM_PKHA_CopyMemSizeSrc))
  5403. {
  5404. /* Set source and destination registers and quads. */
  5405. caam_pkha_mode_set_src_reg_copy(&modeReg, params->srcReg);
  5406. caam_pkha_mode_set_dst_reg_copy(&modeReg, params->dstReg);
  5407. caam_pkha_mode_set_src_seg_copy(&modeReg, params->srcQuad);
  5408. caam_pkha_mode_set_dst_seg_copy(&modeReg, params->dstQuad);
  5409. }
  5410. else
  5411. {
  5412. /* Set the arithmetic type - integer or binary polynomial (F2m). */
  5413. modeReg |= ((uint32_t)params->arithType << 17u);
  5414. /* Set to use Montgomery form of inputs and/or outputs. */
  5415. modeReg |= ((uint32_t)params->montFormIn << 19u);
  5416. modeReg |= ((uint32_t)params->montFormOut << 18u);
  5417. /* Set to use pre-computed R2modN */
  5418. modeReg |= ((uint32_t)params->r2modn << 16u);
  5419. }
  5420. modeReg |= ((uint32_t)params->equalTime << 10u);
  5421. return modeReg;
  5422. }
  5423. enum _caam_user_specified_status
  5424. {
  5425. /* the value below is used as LOCAL_OFFSET field for the JMP/HALT command, in which we test the PRM flag */
  5426. kCAAM_UserSpecifiedStatus_NotPrime = 0x55u,
  5427. /* the value below is returned in Job termination status word in case PrimalityTest result is NotPrime.
  5428. */
  5429. kCAAM_StatusNotPrime = 0x30000000u | kCAAM_UserSpecifiedStatus_NotPrime,
  5430. };
  5431. static status_t caam_pkha_algorithm_operation_command(CAAM_Type *base,
  5432. caam_handle_t *handle,
  5433. caam_desc_pkha_t descriptor,
  5434. const uint8_t *A,
  5435. size_t sizeA,
  5436. const uint8_t *B,
  5437. size_t sizeB,
  5438. const uint8_t *N,
  5439. size_t sizeN,
  5440. const uint8_t *E,
  5441. size_t sizeE,
  5442. caam_pkha_mode_params_t *params,
  5443. uint8_t *result,
  5444. size_t *resultSize)
  5445. {
  5446. uint32_t clearMask = 0;
  5447. uint32_t descriptorSize = ARRAY_SIZE(templateArithmeticPKHA);
  5448. BUILD_ASSURE(sizeof(caam_desc_pkha_t) >= sizeof(templateArithmeticPKHA), caam_desc_pkha_t_size_too_low);
  5449. /* initialize descriptor from template */
  5450. (void)caam_memcpy(descriptor, templateArithmeticPKHA, sizeof(templateArithmeticPKHA));
  5451. /* add descriptor lenght in bytes to HEADER descriptor command */
  5452. DESC_HEADER_ADD_DESCLEN(descriptor[0], descriptorSize);
  5453. /* input data */
  5454. if ((N != NULL) && (sizeN != 0U))
  5455. {
  5456. clearMask |= (uint32_t)1u << 16; /* add Nram bit to PKHA_MODE_MS */
  5457. DESC_ADD_LEN(descriptor[3], sizeN);
  5458. DESC_SET_ADDR(descriptor[4], N);
  5459. }
  5460. else
  5461. {
  5462. /* jump to descriptor[4] */
  5463. descriptor[3] = DESC_JUMP_2; /* jump to current index + 2 (=4) */
  5464. }
  5465. if ((A != NULL) && (sizeA != 0U))
  5466. {
  5467. clearMask |= (uint32_t)1u << 19; /* add Aram bit to PKHA_MODE_MS */
  5468. DESC_ADD_LEN(descriptor[5], sizeA);
  5469. DESC_SET_ADDR(descriptor[6], A);
  5470. }
  5471. else
  5472. {
  5473. /* jump to descriptor[6] */
  5474. descriptor[5] = DESC_JUMP_2; /* jump to current index + 2 (=6) */
  5475. }
  5476. if ((B != NULL) && (sizeB != 0U))
  5477. {
  5478. clearMask |= (uint32_t)1u << 18; /* add Bram bit to PKHA_MODE_MS */
  5479. DESC_ADD_LEN(descriptor[7], sizeB);
  5480. DESC_SET_ADDR(descriptor[8], B);
  5481. }
  5482. else
  5483. {
  5484. /* jump to descriptor[8] */
  5485. descriptor[7] = DESC_JUMP_2; /* jump to current index + 2 (=8) */
  5486. }
  5487. if ((E != NULL) && (sizeE != 0U))
  5488. {
  5489. clearMask |= (uint32_t)1u << 17; /* add Eram bit to PKHA_MODE_MS */
  5490. DESC_ADD_LEN(descriptor[9], sizeE);
  5491. DESC_SET_ADDR(descriptor[10], E);
  5492. }
  5493. else
  5494. {
  5495. /* jump to descriptor[11] */
  5496. descriptor[9] = DESC_JUMP_2; /* jump to current index + 2 (=11) */
  5497. }
  5498. /* add registers to clear into the pkha clear memory function */
  5499. descriptor[1] |= clearMask;
  5500. /* add functions details for pkha arithmetic functions */
  5501. descriptor[11] |= caam_pkha_get_mode(params);
  5502. /* RESULTS */
  5503. if ((result != NULL) && (resultSize != NULL))
  5504. {
  5505. /* We don't know the size of result at this point. But, we know it will be <= modulus. */
  5506. DESC_ADD_LEN(descriptor[12], sizeN);
  5507. DESC_SET_ADDR(descriptor[13], result);
  5508. *resultSize = sizeN;
  5509. }
  5510. else
  5511. {
  5512. /* special case for Primality Test - instead of reading result, check PRM bit and return user-specified status
  5513. * if it is set. */
  5514. /* conditional HALT, return user-specificed status if condition evaluated is true. this condition checks if (PRM
  5515. * is false). */
  5516. descriptor[12] = 0xA0C12000u;
  5517. descriptor[12] |= (uint32_t)kCAAM_UserSpecifiedStatus_NotPrime;
  5518. descriptor[13] = DESC_HALT; /* always halt with status 0x0 (normal) */
  5519. }
  5520. /* schedule the job */
  5521. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  5522. }
  5523. static const uint32_t templateArithmeticECC[] = {
  5524. /* 00 */ DESC_HEADER, /* HEADER */
  5525. /* 01 */ 0x818F0001u, /* OPERATION: PKHA OPERATION: clear memory function. Clear ABNE. */
  5526. /* 02 */ 0xA2000001u, /* JMP always to next command. Done checkpoint (wait for Class 1 Done) */
  5527. /* 03 */ DESC_FLOADN, /* FIFO LOAD PKHA N */
  5528. /* 04 */ 0x00000000u, /* place: N address */
  5529. /* 05 */ DESC_FLOADA0, /* FIFO LOAD A0 */
  5530. /* 06 */ 0x00000000u, /* place: A0 address */
  5531. /* 07 */ DESC_FLOADA1, /* FIFO LOAD A1 */
  5532. /* 08 */ 0x00000000u, /* place: A1 address */
  5533. /* 09 */ DESC_FLOADA3, /* FIFO LOAD PKHA A3 */
  5534. /* 10 */ 0x00000000u, /* place: A3 address */
  5535. /* 11 */ DESC_FLOADB0, /* FIFO LOAD PKHA B0 */
  5536. /* 12 */ 0x00000000u, /* place: B0 address */
  5537. /* 13 */ DESC_FLOADB1, /* FIFO LOAD PKHA B1 */
  5538. /* 14 */ 0x00000000u, /* place: B1 address */
  5539. /* 15 */ DESC_FLOADB2, /* FIFO LOAD PKHA B2 */
  5540. /* 16 */ 0x00000000u, /* place: B2 address */
  5541. /* 17 */ DESC_FLOADB3, /* FIFO LOAD PKHA B3 */
  5542. /* 18 */ 0x00000000u, /* place: B3 address */
  5543. /* 19 */ DESC_KEY_E_, /* KEY PKHA E */
  5544. /* 20 */ 0x00000000u, /* place: E address */
  5545. /* 21 */ 0x81800000u, /* OPERATION: PKHA OPERATION: Arithmetic Functions. */
  5546. /* 22 */ DESC_STOREB1, /* FIFO STORE PKHA B1 */
  5547. /* 23 */ 0x00000000u, /* place: result X address */
  5548. /* 24 */ DESC_STOREB2, /* FIFO STORE PKHA B2 */
  5549. /* 25 */ 0x00000000u, /* place: result Y address */
  5550. };
  5551. static status_t caam_pkha_ecc_algorithm_operation_command(CAAM_Type *base,
  5552. caam_handle_t *handle,
  5553. caam_desc_pkha_ecc_t descriptor,
  5554. const caam_pkha_ecc_point_t *A,
  5555. const caam_pkha_ecc_point_t *B,
  5556. const uint8_t *E,
  5557. size_t sizeE,
  5558. const uint8_t *N,
  5559. const uint8_t *R2modN_B1,
  5560. const uint8_t *R2modN_B3,
  5561. const uint8_t *aCurveParam,
  5562. const uint8_t *bCurveParam,
  5563. size_t size,
  5564. caam_pkha_ecc_point_t *result,
  5565. caam_pkha_mode_params_t *params)
  5566. {
  5567. uint32_t descriptorSize = ARRAY_SIZE(templateArithmeticECC);
  5568. BUILD_ASSURE(sizeof(caam_desc_pkha_ecc_t) >= sizeof(templateArithmeticECC), caam_desc_pkha_ecc_t_size_too_low);
  5569. /* initialize descriptor from template */
  5570. (void)caam_memcpy(descriptor, templateArithmeticECC, sizeof(templateArithmeticECC));
  5571. /* add descriptor lenght in bytes to HEADER descriptor command */
  5572. DESC_HEADER_ADD_DESCLEN(descriptor[0], descriptorSize);
  5573. /* N = modulus */
  5574. DESC_ADD_LEN(descriptor[3], size);
  5575. DESC_SET_ADDR(descriptor[4], N);
  5576. /* [A0, A1] first point in affine coordinates */
  5577. if (A != NULL)
  5578. {
  5579. DESC_ADD_LEN(descriptor[5], size);
  5580. DESC_SET_ADDR(descriptor[6], A->X);
  5581. DESC_ADD_LEN(descriptor[7], size);
  5582. DESC_SET_ADDR(descriptor[8], A->Y);
  5583. }
  5584. else
  5585. {
  5586. /* jump to descriptor[9] */
  5587. descriptor[5] = DESC_JUMP_4; /* jump to current index + 4 (=9) */
  5588. }
  5589. /* aCurveParam */
  5590. DESC_ADD_LEN(descriptor[9], size);
  5591. DESC_SET_ADDR(descriptor[10], aCurveParam);
  5592. /* bCurveParam */
  5593. DESC_ADD_LEN(descriptor[11], size);
  5594. DESC_SET_ADDR(descriptor[12], bCurveParam);
  5595. /* [B1, B2] second point in affine coordinates */
  5596. if (B != NULL)
  5597. {
  5598. DESC_ADD_LEN(descriptor[13], size);
  5599. DESC_SET_ADDR(descriptor[14], B->X);
  5600. DESC_ADD_LEN(descriptor[15], size);
  5601. DESC_SET_ADDR(descriptor[16], B->Y);
  5602. }
  5603. else if (R2modN_B1 != NULL) /* R2modN for ECC_MOD_MUL goes to B1 */
  5604. {
  5605. DESC_ADD_LEN(descriptor[13], size);
  5606. DESC_SET_ADDR(descriptor[14], R2modN_B1);
  5607. /* jump to descriptor[17] */
  5608. descriptor[15] = DESC_JUMP_2; /* jump to current index + 2 (=17) */
  5609. }
  5610. else
  5611. {
  5612. /* jump to descriptor[17] */
  5613. descriptor[13] = DESC_JUMP_4; /* jump to current index + 4 (=17) */
  5614. }
  5615. if (R2modN_B3 != NULL)
  5616. {
  5617. DESC_ADD_LEN(descriptor[17], size);
  5618. DESC_SET_ADDR(descriptor[18], R2modN_B3);
  5619. }
  5620. else
  5621. {
  5622. /* jump to descriptor[19] */
  5623. descriptor[17] = DESC_JUMP_2; /* jump to current index + 2 (=19) */
  5624. }
  5625. if ((E != NULL) && (sizeE != 0U))
  5626. {
  5627. DESC_ADD_LEN(descriptor[19], sizeE);
  5628. DESC_SET_ADDR(descriptor[20], E);
  5629. }
  5630. else
  5631. {
  5632. /* jump to descriptor[21] */
  5633. descriptor[19] = DESC_JUMP_2; /* jump to current index + 2 (=21) */
  5634. }
  5635. /* add functions details for pkha arithmetic functions */
  5636. descriptor[21] |= caam_pkha_get_mode(params);
  5637. /* store [B1, B2] result point */
  5638. DESC_ADD_LEN(descriptor[22], size);
  5639. DESC_SET_ADDR(descriptor[23], result->X);
  5640. DESC_ADD_LEN(descriptor[24], size);
  5641. DESC_SET_ADDR(descriptor[25], result->Y);
  5642. /* schedule the job */
  5643. return caam_in_job_ring_add(base, handle->jobRing, &descriptor[0]);
  5644. }
  5645. /*!
  5646. * addtogroup caam_driver_pkha
  5647. * {
  5648. */
  5649. int CAAM_PKHA_CompareBigNum(const uint8_t *a, size_t sizeA, const uint8_t *b, size_t sizeB)
  5650. {
  5651. int retval = 0;
  5652. /* skip zero msbytes - integer a */
  5653. while ((sizeA != 0U) && (0u == a[0]))
  5654. {
  5655. sizeA--;
  5656. a++;
  5657. }
  5658. /* skip zero msbytes - integer b */
  5659. while ((sizeB != 0U) && (0u == b[0]))
  5660. {
  5661. sizeB--;
  5662. b++;
  5663. }
  5664. if (sizeA > sizeB)
  5665. {
  5666. retval = 1;
  5667. } /* int a has more non-zero bytes, thus it is bigger than b */
  5668. else if (sizeA < sizeB)
  5669. {
  5670. retval = -1;
  5671. } /* int b has more non-zero bytes, thus it is bigger than a */
  5672. else if (sizeA == 0U)
  5673. {
  5674. retval = 0;
  5675. } /* sizeA = sizeB = 0 */
  5676. else
  5677. {
  5678. int n;
  5679. uint32_t equal;
  5680. int val;
  5681. n = (int)sizeA - 1;
  5682. equal = 0;
  5683. /* compare all bytes - does not leak (in time domain) how many bytes equal */
  5684. /* move from lsbyte to msbyte */
  5685. while (n >= 0)
  5686. {
  5687. uint32_t chXor = ((uint32_t)a[n] ^ (uint32_t)b[n]);
  5688. equal |= chXor;
  5689. val = (int)chXor * ((int)a[n] - (int)b[n]);
  5690. if (val < 0)
  5691. {
  5692. retval = -1;
  5693. }
  5694. if (val > 0)
  5695. {
  5696. retval = 1;
  5697. }
  5698. if (val == 0)
  5699. {
  5700. val = 1;
  5701. }
  5702. if (val != 0)
  5703. {
  5704. n--;
  5705. }
  5706. }
  5707. if (0U == equal)
  5708. {
  5709. retval = 0;
  5710. }
  5711. }
  5712. return (retval);
  5713. }
  5714. status_t CAAM_PKHA_ModAddNonBlocking(CAAM_Type *base,
  5715. caam_handle_t *handle,
  5716. caam_desc_pkha_t descriptor,
  5717. const uint8_t *A,
  5718. size_t sizeA,
  5719. const uint8_t *B,
  5720. size_t sizeB,
  5721. const uint8_t *N,
  5722. size_t sizeN,
  5723. uint8_t *result,
  5724. size_t *resultSize,
  5725. caam_pkha_f2m_t arithType);
  5726. status_t CAAM_PKHA_ModAddNonBlocking(CAAM_Type *base,
  5727. caam_handle_t *handle,
  5728. caam_desc_pkha_t descriptor,
  5729. const uint8_t *A,
  5730. size_t sizeA,
  5731. const uint8_t *B,
  5732. size_t sizeB,
  5733. const uint8_t *N,
  5734. size_t sizeN,
  5735. uint8_t *result,
  5736. size_t *resultSize,
  5737. caam_pkha_f2m_t arithType)
  5738. {
  5739. caam_pkha_mode_params_t params;
  5740. status_t status;
  5741. if (arithType == kCAAM_PKHA_IntegerArith)
  5742. {
  5743. if (CAAM_PKHA_CompareBigNum(A, sizeA, N, sizeN) >= 0)
  5744. {
  5745. return (kStatus_InvalidArgument);
  5746. }
  5747. if (CAAM_PKHA_CompareBigNum(B, sizeB, N, sizeN) >= 0)
  5748. {
  5749. return (kStatus_InvalidArgument);
  5750. }
  5751. }
  5752. caam_pkha_default_parms(&params);
  5753. params.func = kCAAM_PKHA_ArithModAdd;
  5754. params.arithType = arithType;
  5755. status = caam_pkha_algorithm_operation_command(base, handle, descriptor, A, sizeA, B, sizeB, N, sizeN, NULL, 0,
  5756. &params, result, resultSize);
  5757. return status;
  5758. }
  5759. /*!
  5760. * brief Performs modular addition - (A + B) mod N.
  5761. *
  5762. * This function performs modular addition of (A + B) mod N, with either
  5763. * integer or binary polynomial (F2m) inputs. In the F2m form, this function is
  5764. * equivalent to a bitwise XOR and it is functionally the same as subtraction.
  5765. *
  5766. * param base CAAM peripheral base address
  5767. * param A first addend (integer or binary polynomial)
  5768. * param sizeA Size of A in bytes
  5769. * param B second addend (integer or binary polynomial)
  5770. * param sizeB Size of B in bytes
  5771. * param N modulus.
  5772. * param sizeN Size of N in bytes.
  5773. * param[out] result Output array to store result of operation
  5774. * param[out] resultSize Output size of operation in bytes
  5775. * param arithType Type of arithmetic to perform (integer or F2m)
  5776. * return Operation status.
  5777. */
  5778. status_t CAAM_PKHA_ModAdd(CAAM_Type *base,
  5779. caam_handle_t *handle,
  5780. const uint8_t *A,
  5781. size_t sizeA,
  5782. const uint8_t *B,
  5783. size_t sizeB,
  5784. const uint8_t *N,
  5785. size_t sizeN,
  5786. uint8_t *result,
  5787. size_t *resultSize,
  5788. caam_pkha_f2m_t arithType)
  5789. {
  5790. caam_desc_pkha_t descBuf;
  5791. status_t status;
  5792. do
  5793. {
  5794. status = CAAM_PKHA_ModAddNonBlocking(base, handle, descBuf, A, sizeA, B, sizeB, N, sizeN, result, resultSize,
  5795. arithType);
  5796. } while (status == kStatus_CAAM_Again);
  5797. if (status != kStatus_Success)
  5798. {
  5799. return status;
  5800. }
  5801. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  5802. }
  5803. status_t CAAM_PKHA_ModSub1NonBlocking(CAAM_Type *base,
  5804. caam_handle_t *handle,
  5805. caam_desc_pkha_t descriptor,
  5806. const uint8_t *A,
  5807. size_t sizeA,
  5808. const uint8_t *B,
  5809. size_t sizeB,
  5810. const uint8_t *N,
  5811. size_t sizeN,
  5812. uint8_t *result,
  5813. size_t *resultSize);
  5814. status_t CAAM_PKHA_ModSub1NonBlocking(CAAM_Type *base,
  5815. caam_handle_t *handle,
  5816. caam_desc_pkha_t descriptor,
  5817. const uint8_t *A,
  5818. size_t sizeA,
  5819. const uint8_t *B,
  5820. size_t sizeB,
  5821. const uint8_t *N,
  5822. size_t sizeN,
  5823. uint8_t *result,
  5824. size_t *resultSize)
  5825. {
  5826. caam_pkha_mode_params_t params;
  5827. status_t status;
  5828. if (CAAM_PKHA_CompareBigNum(A, sizeA, N, sizeN) >= 0)
  5829. {
  5830. return (kStatus_InvalidArgument);
  5831. }
  5832. if (CAAM_PKHA_CompareBigNum(B, sizeB, N, sizeN) >= 0)
  5833. {
  5834. return (kStatus_InvalidArgument);
  5835. }
  5836. caam_pkha_default_parms(&params);
  5837. params.func = kCAAM_PKHA_ArithModSub1;
  5838. status = caam_pkha_algorithm_operation_command(base, handle, descriptor, A, sizeA, B, sizeB, N, sizeN, NULL, 0,
  5839. &params, result, resultSize);
  5840. return status;
  5841. }
  5842. /*!
  5843. * brief Performs modular subtraction - (A - B) mod N.
  5844. *
  5845. * This function performs modular subtraction of (A - B) mod N with
  5846. * integer inputs.
  5847. *
  5848. * param base CAAM peripheral base address
  5849. * param A first addend (integer or binary polynomial)
  5850. * param sizeA Size of A in bytes
  5851. * param B second addend (integer or binary polynomial)
  5852. * param sizeB Size of B in bytes
  5853. * param N modulus
  5854. * param sizeN Size of N in bytes
  5855. * param[out] result Output array to store result of operation
  5856. * param[out] resultSize Output size of operation in bytes
  5857. * return Operation status.
  5858. */
  5859. status_t CAAM_PKHA_ModSub1(CAAM_Type *base,
  5860. caam_handle_t *handle,
  5861. const uint8_t *A,
  5862. size_t sizeA,
  5863. const uint8_t *B,
  5864. size_t sizeB,
  5865. const uint8_t *N,
  5866. size_t sizeN,
  5867. uint8_t *result,
  5868. size_t *resultSize)
  5869. {
  5870. caam_desc_pkha_t descBuf;
  5871. status_t status;
  5872. do
  5873. {
  5874. status = CAAM_PKHA_ModSub1NonBlocking(base, handle, descBuf, A, sizeA, B, sizeB, N, sizeN, result, resultSize);
  5875. } while (status == kStatus_CAAM_Again);
  5876. if (status != kStatus_Success)
  5877. {
  5878. return status;
  5879. }
  5880. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  5881. }
  5882. status_t CAAM_PKHA_ModSub2NonBlocking(CAAM_Type *base,
  5883. caam_handle_t *handle,
  5884. caam_desc_pkha_t descriptor,
  5885. const uint8_t *A,
  5886. size_t sizeA,
  5887. const uint8_t *B,
  5888. size_t sizeB,
  5889. const uint8_t *N,
  5890. size_t sizeN,
  5891. uint8_t *result,
  5892. size_t *resultSize);
  5893. status_t CAAM_PKHA_ModSub2NonBlocking(CAAM_Type *base,
  5894. caam_handle_t *handle,
  5895. caam_desc_pkha_t descriptor,
  5896. const uint8_t *A,
  5897. size_t sizeA,
  5898. const uint8_t *B,
  5899. size_t sizeB,
  5900. const uint8_t *N,
  5901. size_t sizeN,
  5902. uint8_t *result,
  5903. size_t *resultSize)
  5904. {
  5905. caam_pkha_mode_params_t params;
  5906. status_t status;
  5907. caam_pkha_default_parms(&params);
  5908. params.func = kCAAM_PKHA_ArithModSub2;
  5909. status = caam_pkha_algorithm_operation_command(base, handle, descriptor, A, sizeA, B, sizeB, N, sizeN, NULL, 0,
  5910. &params, result, resultSize);
  5911. return status;
  5912. }
  5913. /*!
  5914. * brief Performs modular subtraction - (B - A) mod N.
  5915. *
  5916. * This function performs modular subtraction of (B - A) mod N,
  5917. * with integer inputs.
  5918. *
  5919. * param base CAAM peripheral base address
  5920. * param A first addend (integer or binary polynomial)
  5921. * param sizeA Size of A in bytes
  5922. * param B second addend (integer or binary polynomial)
  5923. * param sizeB Size of B in bytes
  5924. * param N modulus
  5925. * param sizeN Size of N in bytes
  5926. * param[out] result Output array to store result of operation
  5927. * param[out] resultSize Output size of operation in bytes
  5928. * return Operation status.
  5929. */
  5930. status_t CAAM_PKHA_ModSub2(CAAM_Type *base,
  5931. caam_handle_t *handle,
  5932. const uint8_t *A,
  5933. size_t sizeA,
  5934. const uint8_t *B,
  5935. size_t sizeB,
  5936. const uint8_t *N,
  5937. size_t sizeN,
  5938. uint8_t *result,
  5939. size_t *resultSize)
  5940. {
  5941. caam_desc_pkha_t descBuf;
  5942. status_t status;
  5943. do
  5944. {
  5945. status = CAAM_PKHA_ModSub2NonBlocking(base, handle, descBuf, A, sizeA, B, sizeB, N, sizeN, result, resultSize);
  5946. } while (status == kStatus_CAAM_Again);
  5947. if (status != kStatus_Success)
  5948. {
  5949. return status;
  5950. }
  5951. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  5952. }
  5953. status_t CAAM_PKHA_ModMulNonBlocking(CAAM_Type *base,
  5954. caam_handle_t *handle,
  5955. caam_desc_pkha_t descriptor,
  5956. const uint8_t *A,
  5957. size_t sizeA,
  5958. const uint8_t *B,
  5959. size_t sizeB,
  5960. const uint8_t *N,
  5961. size_t sizeN,
  5962. uint8_t *result,
  5963. size_t *resultSize,
  5964. caam_pkha_f2m_t arithType,
  5965. caam_pkha_montgomery_form_t montIn,
  5966. caam_pkha_montgomery_form_t montOut,
  5967. caam_pkha_timing_t equalTime);
  5968. status_t CAAM_PKHA_ModMulNonBlocking(CAAM_Type *base,
  5969. caam_handle_t *handle,
  5970. caam_desc_pkha_t descriptor,
  5971. const uint8_t *A,
  5972. size_t sizeA,
  5973. const uint8_t *B,
  5974. size_t sizeB,
  5975. const uint8_t *N,
  5976. size_t sizeN,
  5977. uint8_t *result,
  5978. size_t *resultSize,
  5979. caam_pkha_f2m_t arithType,
  5980. caam_pkha_montgomery_form_t montIn,
  5981. caam_pkha_montgomery_form_t montOut,
  5982. caam_pkha_timing_t equalTime)
  5983. {
  5984. caam_pkha_mode_params_t params;
  5985. status_t status;
  5986. if (arithType == kCAAM_PKHA_IntegerArith)
  5987. {
  5988. if (CAAM_PKHA_CompareBigNum(A, sizeA, N, sizeN) >= 0)
  5989. {
  5990. return (kStatus_InvalidArgument);
  5991. }
  5992. if (CAAM_PKHA_CompareBigNum(B, sizeB, N, sizeN) >= 0)
  5993. {
  5994. return (kStatus_InvalidArgument);
  5995. }
  5996. }
  5997. caam_pkha_default_parms(&params);
  5998. params.func = kCAAM_PKHA_ArithModMul;
  5999. params.arithType = arithType;
  6000. params.montFormIn = montIn;
  6001. params.montFormOut = montOut;
  6002. params.equalTime = equalTime;
  6003. status = caam_pkha_algorithm_operation_command(base, handle, descriptor, A, sizeA, B, sizeB, N, sizeN, NULL, 0,
  6004. &params, result, resultSize);
  6005. return status;
  6006. }
  6007. /*!
  6008. * brief Performs modular multiplication - (A x B) mod N.
  6009. *
  6010. * This function performs modular multiplication with either integer or
  6011. * binary polynomial (F2m) inputs. It can optionally specify whether inputs
  6012. * and/or outputs will be in Montgomery form or not.
  6013. *
  6014. * param base CAAM peripheral base address
  6015. * param A first addend (integer or binary polynomial)
  6016. * param sizeA Size of A in bytes
  6017. * param B second addend (integer or binary polynomial)
  6018. * param sizeB Size of B in bytes
  6019. * param N modulus.
  6020. * param sizeN Size of N in bytes
  6021. * param[out] result Output array to store result of operation
  6022. * param[out] resultSize Output size of operation in bytes
  6023. * param arithType Type of arithmetic to perform (integer or F2m)
  6024. * param montIn Format of inputs
  6025. * param montOut Format of output
  6026. * param equalTime Run the function time equalized or no timing equalization. This argument is ignored for F2m modular
  6027. * multiplication.
  6028. * return Operation status.
  6029. */
  6030. status_t CAAM_PKHA_ModMul(CAAM_Type *base,
  6031. caam_handle_t *handle,
  6032. const uint8_t *A,
  6033. size_t sizeA,
  6034. const uint8_t *B,
  6035. size_t sizeB,
  6036. const uint8_t *N,
  6037. size_t sizeN,
  6038. uint8_t *result,
  6039. size_t *resultSize,
  6040. caam_pkha_f2m_t arithType,
  6041. caam_pkha_montgomery_form_t montIn,
  6042. caam_pkha_montgomery_form_t montOut,
  6043. caam_pkha_timing_t equalTime)
  6044. {
  6045. caam_desc_pkha_t descBuf;
  6046. status_t status;
  6047. do
  6048. {
  6049. status = CAAM_PKHA_ModMulNonBlocking(base, handle, descBuf, A, sizeA, B, sizeB, N, sizeN, result, resultSize,
  6050. arithType, montIn, montOut, equalTime);
  6051. } while (status == kStatus_CAAM_Again);
  6052. if (status != kStatus_Success)
  6053. {
  6054. return status;
  6055. }
  6056. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  6057. }
  6058. status_t CAAM_PKHA_ModR2NonBlocking(CAAM_Type *base,
  6059. caam_handle_t *handle,
  6060. caam_desc_pkha_t descriptor,
  6061. const uint8_t *N,
  6062. size_t sizeN,
  6063. uint8_t *result,
  6064. size_t *resultSize,
  6065. caam_pkha_f2m_t arithType);
  6066. status_t CAAM_PKHA_ModR2NonBlocking(CAAM_Type *base,
  6067. caam_handle_t *handle,
  6068. caam_desc_pkha_t descriptor,
  6069. const uint8_t *N,
  6070. size_t sizeN,
  6071. uint8_t *result,
  6072. size_t *resultSize,
  6073. caam_pkha_f2m_t arithType)
  6074. {
  6075. status_t status;
  6076. caam_pkha_mode_params_t params;
  6077. caam_pkha_default_parms(&params);
  6078. params.func = kCAAM_PKHA_ArithModR2;
  6079. params.arithType = arithType;
  6080. status = caam_pkha_algorithm_operation_command(base, handle, descriptor, NULL, 0, NULL, 0, N, sizeN, NULL, 0,
  6081. &params, result, resultSize);
  6082. return status;
  6083. }
  6084. /*!
  6085. * brief Computes integer Montgomery factor R^2 mod N.
  6086. *
  6087. * This function computes a constant to assist in converting operands
  6088. * into the Montgomery residue system representation.
  6089. *
  6090. * param base CAAM peripheral base address
  6091. * param N modulus
  6092. * param sizeN Size of N in bytes
  6093. * param[out] result Output array to store result of operation
  6094. * param[out] resultSize Output size of operation in bytes
  6095. * param arithType Type of arithmetic to perform (integer or F2m)
  6096. * return Operation status.
  6097. */
  6098. status_t CAAM_PKHA_ModR2(CAAM_Type *base,
  6099. caam_handle_t *handle,
  6100. const uint8_t *N,
  6101. size_t sizeN,
  6102. uint8_t *result,
  6103. size_t *resultSize,
  6104. caam_pkha_f2m_t arithType)
  6105. {
  6106. caam_desc_pkha_t descBuf;
  6107. status_t status;
  6108. do
  6109. {
  6110. status = CAAM_PKHA_ModR2NonBlocking(base, handle, descBuf, N, sizeN, result, resultSize, arithType);
  6111. } while (status == kStatus_CAAM_Again);
  6112. if (status != kStatus_Success)
  6113. {
  6114. return status;
  6115. }
  6116. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  6117. }
  6118. status_t CAAM_PKHA_ModExpNonBlocking(CAAM_Type *base,
  6119. caam_handle_t *handle,
  6120. caam_desc_pkha_t descriptor,
  6121. const uint8_t *A,
  6122. size_t sizeA,
  6123. const uint8_t *N,
  6124. size_t sizeN,
  6125. const uint8_t *E,
  6126. size_t sizeE,
  6127. uint8_t *result,
  6128. size_t *resultSize,
  6129. caam_pkha_f2m_t arithType,
  6130. caam_pkha_montgomery_form_t montIn,
  6131. caam_pkha_timing_t equalTime);
  6132. status_t CAAM_PKHA_ModExpNonBlocking(CAAM_Type *base,
  6133. caam_handle_t *handle,
  6134. caam_desc_pkha_t descriptor,
  6135. const uint8_t *A,
  6136. size_t sizeA,
  6137. const uint8_t *N,
  6138. size_t sizeN,
  6139. const uint8_t *E,
  6140. size_t sizeE,
  6141. uint8_t *result,
  6142. size_t *resultSize,
  6143. caam_pkha_f2m_t arithType,
  6144. caam_pkha_montgomery_form_t montIn,
  6145. caam_pkha_timing_t equalTime)
  6146. {
  6147. caam_pkha_mode_params_t params;
  6148. status_t status;
  6149. if (arithType == kCAAM_PKHA_IntegerArith)
  6150. {
  6151. if (CAAM_PKHA_CompareBigNum(A, sizeA, N, sizeN) >= 0)
  6152. {
  6153. return (kStatus_InvalidArgument);
  6154. }
  6155. }
  6156. caam_pkha_default_parms(&params);
  6157. params.func = kCAAM_PKHA_ArithModExp;
  6158. params.arithType = arithType;
  6159. params.montFormIn = montIn;
  6160. params.equalTime = equalTime;
  6161. status = caam_pkha_algorithm_operation_command(base, handle, descriptor, A, sizeA, NULL, 0, N, sizeN, E, sizeE,
  6162. &params, result, resultSize);
  6163. return status;
  6164. }
  6165. /*!
  6166. * brief Performs modular exponentiation - (A^E) mod N.
  6167. *
  6168. * This function performs modular exponentiation with either integer or
  6169. * binary polynomial (F2m) inputs.
  6170. *
  6171. * param base CAAM peripheral base address
  6172. * param A first addend (integer or binary polynomial)
  6173. * param sizeA Size of A in bytes
  6174. * param N modulus
  6175. * param sizeN Size of N in bytes
  6176. * param E exponent
  6177. * param sizeE Size of E in bytes
  6178. * param[out] result Output array to store result of operation
  6179. * param[out] resultSize Output size of operation in bytes
  6180. * param montIn Format of A input (normal or Montgomery)
  6181. * param arithType Type of arithmetic to perform (integer or F2m)
  6182. * param equalTime Run the function time equalized or no timing equalization.
  6183. * return Operation status.
  6184. */
  6185. status_t CAAM_PKHA_ModExp(CAAM_Type *base,
  6186. caam_handle_t *handle,
  6187. const uint8_t *A,
  6188. size_t sizeA,
  6189. const uint8_t *N,
  6190. size_t sizeN,
  6191. const uint8_t *E,
  6192. size_t sizeE,
  6193. uint8_t *result,
  6194. size_t *resultSize,
  6195. caam_pkha_f2m_t arithType,
  6196. caam_pkha_montgomery_form_t montIn,
  6197. caam_pkha_timing_t equalTime)
  6198. {
  6199. caam_desc_pkha_t descBuf;
  6200. status_t status;
  6201. do
  6202. {
  6203. status = CAAM_PKHA_ModExpNonBlocking(base, handle, descBuf, A, sizeA, N, sizeN, E, sizeE, result, resultSize,
  6204. arithType, montIn, equalTime);
  6205. } while (status == kStatus_CAAM_Again);
  6206. if (status != kStatus_Success)
  6207. {
  6208. return status;
  6209. }
  6210. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  6211. }
  6212. status_t CAAM_PKHA_ModRedNonBlocking(CAAM_Type *base,
  6213. caam_handle_t *handle,
  6214. caam_desc_pkha_t descriptor,
  6215. const uint8_t *A,
  6216. size_t sizeA,
  6217. const uint8_t *N,
  6218. size_t sizeN,
  6219. uint8_t *result,
  6220. size_t *resultSize,
  6221. caam_pkha_f2m_t arithType);
  6222. status_t CAAM_PKHA_ModRedNonBlocking(CAAM_Type *base,
  6223. caam_handle_t *handle,
  6224. caam_desc_pkha_t descriptor,
  6225. const uint8_t *A,
  6226. size_t sizeA,
  6227. const uint8_t *N,
  6228. size_t sizeN,
  6229. uint8_t *result,
  6230. size_t *resultSize,
  6231. caam_pkha_f2m_t arithType)
  6232. {
  6233. caam_pkha_mode_params_t params;
  6234. status_t status;
  6235. caam_pkha_default_parms(&params);
  6236. params.func = kCAAM_PKHA_ArithModRed;
  6237. params.arithType = arithType;
  6238. status = caam_pkha_algorithm_operation_command(base, handle, descriptor, A, sizeA, NULL, 0, N, sizeN, NULL, 0,
  6239. &params, result, resultSize);
  6240. return status;
  6241. }
  6242. /*!
  6243. * brief Performs modular reduction - (A) mod N.
  6244. *
  6245. * This function performs modular reduction with either integer or
  6246. * binary polynomial (F2m) inputs.
  6247. *
  6248. * param base CAAM peripheral base address
  6249. * param A first addend (integer or binary polynomial)
  6250. * param sizeA Size of A in bytes
  6251. * param N modulus
  6252. * param sizeN Size of N in bytes
  6253. * param[out] result Output array to store result of operation
  6254. * param[out] resultSize Output size of operation in bytes
  6255. * param arithType Type of arithmetic to perform (integer or F2m)
  6256. * return Operation status.
  6257. */
  6258. status_t CAAM_PKHA_ModRed(CAAM_Type *base,
  6259. caam_handle_t *handle,
  6260. const uint8_t *A,
  6261. size_t sizeA,
  6262. const uint8_t *N,
  6263. size_t sizeN,
  6264. uint8_t *result,
  6265. size_t *resultSize,
  6266. caam_pkha_f2m_t arithType)
  6267. {
  6268. caam_desc_pkha_t descBuf;
  6269. status_t status;
  6270. do
  6271. {
  6272. status = CAAM_PKHA_ModRedNonBlocking(base, handle, descBuf, A, sizeA, N, sizeN, result, resultSize, arithType);
  6273. } while (status == kStatus_CAAM_Again);
  6274. if (status != kStatus_Success)
  6275. {
  6276. return status;
  6277. }
  6278. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  6279. }
  6280. status_t CAAM_PKHA_ModInvNonBlocking(CAAM_Type *base,
  6281. caam_handle_t *handle,
  6282. caam_desc_pkha_t descriptor,
  6283. const uint8_t *A,
  6284. size_t sizeA,
  6285. const uint8_t *N,
  6286. size_t sizeN,
  6287. uint8_t *result,
  6288. size_t *resultSize,
  6289. caam_pkha_f2m_t arithType);
  6290. status_t CAAM_PKHA_ModInvNonBlocking(CAAM_Type *base,
  6291. caam_handle_t *handle,
  6292. caam_desc_pkha_t descriptor,
  6293. const uint8_t *A,
  6294. size_t sizeA,
  6295. const uint8_t *N,
  6296. size_t sizeN,
  6297. uint8_t *result,
  6298. size_t *resultSize,
  6299. caam_pkha_f2m_t arithType)
  6300. {
  6301. caam_pkha_mode_params_t params;
  6302. status_t status;
  6303. caam_pkha_default_parms(&params);
  6304. params.func = kCAAM_PKHA_ArithModInv;
  6305. params.arithType = arithType;
  6306. status = caam_pkha_algorithm_operation_command(base, handle, descriptor, A, sizeA, NULL, 0, N, sizeN, NULL, 0,
  6307. &params, result, resultSize);
  6308. return status;
  6309. }
  6310. /*!
  6311. * brief Performs modular inversion - (A^-1) mod N.
  6312. *
  6313. * This function performs modular inversion with either integer or
  6314. * binary polynomial (F2m) inputs.
  6315. *
  6316. * param base CAAM peripheral base address
  6317. * param A first addend (integer or binary polynomial)
  6318. * param sizeA Size of A in bytes
  6319. * param N modulus
  6320. * param sizeN Size of N in bytes
  6321. * param[out] result Output array to store result of operation
  6322. * param[out] resultSize Output size of operation in bytes
  6323. * param arithType Type of arithmetic to perform (integer or F2m)
  6324. * return Operation status.
  6325. */
  6326. status_t CAAM_PKHA_ModInv(CAAM_Type *base,
  6327. caam_handle_t *handle,
  6328. const uint8_t *A,
  6329. size_t sizeA,
  6330. const uint8_t *N,
  6331. size_t sizeN,
  6332. uint8_t *result,
  6333. size_t *resultSize,
  6334. caam_pkha_f2m_t arithType)
  6335. {
  6336. caam_desc_pkha_t descBuf;
  6337. status_t status;
  6338. do
  6339. {
  6340. status = CAAM_PKHA_ModInvNonBlocking(base, handle, descBuf, A, sizeA, N, sizeN, result, resultSize, arithType);
  6341. } while (status == kStatus_CAAM_Again);
  6342. if (status != kStatus_Success)
  6343. {
  6344. return status;
  6345. }
  6346. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  6347. }
  6348. status_t CAAM_PKHA_ModGcdNonBlocking(CAAM_Type *base,
  6349. caam_handle_t *handle,
  6350. caam_desc_pkha_t descriptor,
  6351. const uint8_t *A,
  6352. size_t sizeA,
  6353. const uint8_t *N,
  6354. size_t sizeN,
  6355. uint8_t *result,
  6356. size_t *resultSize,
  6357. caam_pkha_f2m_t arithType);
  6358. status_t CAAM_PKHA_ModGcdNonBlocking(CAAM_Type *base,
  6359. caam_handle_t *handle,
  6360. caam_desc_pkha_t descriptor,
  6361. const uint8_t *A,
  6362. size_t sizeA,
  6363. const uint8_t *N,
  6364. size_t sizeN,
  6365. uint8_t *result,
  6366. size_t *resultSize,
  6367. caam_pkha_f2m_t arithType)
  6368. {
  6369. caam_pkha_mode_params_t params;
  6370. status_t status;
  6371. caam_pkha_default_parms(&params);
  6372. params.func = kCAAM_PKHA_ArithGcd;
  6373. params.arithType = arithType;
  6374. status = caam_pkha_algorithm_operation_command(base, handle, descriptor, A, sizeA, NULL, 0, N, sizeN, NULL, 0,
  6375. &params, result, resultSize);
  6376. return status;
  6377. }
  6378. /*!
  6379. * brief Calculates the greatest common divisor - GCD (A, N).
  6380. *
  6381. * This function calculates the greatest common divisor of two inputs with
  6382. * either integer or binary polynomial (F2m) inputs.
  6383. *
  6384. * param base CAAM peripheral base address
  6385. * param A first value (must be smaller than or equal to N)
  6386. * param sizeA Size of A in bytes
  6387. * param N second value (must be non-zero)
  6388. * param sizeN Size of N in bytes
  6389. * param[out] result Output array to store result of operation
  6390. * param[out] resultSize Output size of operation in bytes
  6391. * param arithType Type of arithmetic to perform (integer or F2m)
  6392. * return Operation status.
  6393. */
  6394. status_t CAAM_PKHA_ModGcd(CAAM_Type *base,
  6395. caam_handle_t *handle,
  6396. const uint8_t *A,
  6397. size_t sizeA,
  6398. const uint8_t *N,
  6399. size_t sizeN,
  6400. uint8_t *result,
  6401. size_t *resultSize,
  6402. caam_pkha_f2m_t arithType)
  6403. {
  6404. caam_desc_pkha_t descBuf;
  6405. status_t status;
  6406. do
  6407. {
  6408. status = CAAM_PKHA_ModGcdNonBlocking(base, handle, descBuf, A, sizeA, N, sizeN, result, resultSize, arithType);
  6409. } while (status == kStatus_CAAM_Again);
  6410. if (status != kStatus_Success)
  6411. {
  6412. return status;
  6413. }
  6414. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  6415. }
  6416. status_t CAAM_PKHA_PrimalityTestNonBlocking(CAAM_Type *base,
  6417. caam_handle_t *handle,
  6418. caam_desc_pkha_t descriptor,
  6419. const uint8_t *A,
  6420. size_t sizeA,
  6421. const uint8_t *B,
  6422. size_t sizeB,
  6423. const uint8_t *N,
  6424. size_t sizeN);
  6425. status_t CAAM_PKHA_PrimalityTestNonBlocking(CAAM_Type *base,
  6426. caam_handle_t *handle,
  6427. caam_desc_pkha_t descriptor,
  6428. const uint8_t *A,
  6429. size_t sizeA,
  6430. const uint8_t *B,
  6431. size_t sizeB,
  6432. const uint8_t *N,
  6433. size_t sizeN)
  6434. {
  6435. caam_pkha_mode_params_t params;
  6436. status_t status;
  6437. caam_pkha_default_parms(&params);
  6438. params.func = kCAAM_PKHA_ArithPrimalityTest;
  6439. status = caam_pkha_algorithm_operation_command(base, handle, descriptor, A, sizeA, B, sizeB, N, sizeN, NULL, 0,
  6440. &params, NULL, NULL);
  6441. return status;
  6442. }
  6443. /*!
  6444. * brief Executes Miller-Rabin primality test.
  6445. *
  6446. * This function calculates whether or not a candidate prime number is likely
  6447. * to be a prime.
  6448. *
  6449. * param base CAAM peripheral base address
  6450. * param A initial random seed
  6451. * param sizeA Size of A in bytes
  6452. * param B number of trial runs
  6453. * param sizeB Size of B in bytes
  6454. * param N candidate prime integer
  6455. * param sizeN Size of N in bytes
  6456. * param[out] res True if the value is likely prime or false otherwise
  6457. * return Operation status.
  6458. */
  6459. status_t CAAM_PKHA_PrimalityTest(CAAM_Type *base,
  6460. caam_handle_t *handle,
  6461. const uint8_t *A,
  6462. size_t sizeA,
  6463. const uint8_t *B,
  6464. size_t sizeB,
  6465. const uint8_t *N,
  6466. size_t sizeN,
  6467. bool *res)
  6468. {
  6469. caam_desc_pkha_t descBuf;
  6470. status_t status;
  6471. do
  6472. {
  6473. status = CAAM_PKHA_PrimalityTestNonBlocking(base, handle, descBuf, A, sizeA, B, sizeB, N, sizeN);
  6474. } while (status == kStatus_CAAM_Again);
  6475. if (status != kStatus_Success)
  6476. {
  6477. return status;
  6478. }
  6479. status = CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  6480. if (status == kStatus_Success)
  6481. {
  6482. /* this return code means that the candidate is believed to be prime. */
  6483. *res = true;
  6484. }
  6485. /* clear DESC INDEX field in the Job termination status word and check if it is our NotPrime user specified status
  6486. */
  6487. else if (((uint32_t)status & 0xffff00ffu) == (uint32_t)kCAAM_StatusNotPrime)
  6488. {
  6489. /* change status to Ok to upper layer caller. this return code means that the candidate is believed to not being
  6490. * prime. */
  6491. status = kStatus_Success;
  6492. *res = false;
  6493. }
  6494. else
  6495. {
  6496. *res = false;
  6497. }
  6498. return status;
  6499. }
  6500. status_t CAAM_PKHA_ECC_PointAddNonBlocking(CAAM_Type *base,
  6501. caam_handle_t *handle,
  6502. caam_desc_pkha_ecc_t descriptor,
  6503. const caam_pkha_ecc_point_t *A,
  6504. const caam_pkha_ecc_point_t *B,
  6505. const uint8_t *N,
  6506. const uint8_t *R2modN,
  6507. const uint8_t *aCurveParam,
  6508. const uint8_t *bCurveParam,
  6509. size_t size,
  6510. caam_pkha_f2m_t arithType,
  6511. caam_pkha_ecc_point_t *result);
  6512. status_t CAAM_PKHA_ECC_PointAddNonBlocking(CAAM_Type *base,
  6513. caam_handle_t *handle,
  6514. caam_desc_pkha_ecc_t descriptor,
  6515. const caam_pkha_ecc_point_t *A,
  6516. const caam_pkha_ecc_point_t *B,
  6517. const uint8_t *N,
  6518. const uint8_t *R2modN,
  6519. const uint8_t *aCurveParam,
  6520. const uint8_t *bCurveParam,
  6521. size_t size,
  6522. caam_pkha_f2m_t arithType,
  6523. caam_pkha_ecc_point_t *result)
  6524. {
  6525. caam_pkha_mode_params_t params;
  6526. status_t status;
  6527. caam_pkha_default_parms(&params);
  6528. params.func = kCAAM_PKHA_ArithEccAdd;
  6529. params.arithType = arithType;
  6530. params.r2modn = (R2modN != NULL) ? kCAAM_PKHA_InputR2 : kCAAM_PKHA_CalcR2;
  6531. status = caam_pkha_ecc_algorithm_operation_command(base, handle, descriptor, A, B, NULL, 0, N, NULL, R2modN,
  6532. aCurveParam, bCurveParam, size, result, &params);
  6533. return status;
  6534. }
  6535. /*!
  6536. * brief Adds elliptic curve points - A + B.
  6537. *
  6538. * This function performs ECC point addition over a prime field (Fp) or binary field (F2m) using
  6539. * affine coordinates.
  6540. *
  6541. * param base CAAM peripheral base address
  6542. * param A Left-hand point
  6543. * param B Right-hand point
  6544. * param N Prime modulus of the field
  6545. * param R2modN NULL (the function computes R2modN internally) or pointer to pre-computed R2modN (obtained from
  6546. * CAAM_PKHA_ModR2() function).
  6547. * param aCurveParam A parameter from curve equation
  6548. * param bCurveParam B parameter from curve equation (constant)
  6549. * param size Size in bytes of curve points and parameters
  6550. * param arithType Type of arithmetic to perform (integer or F2m)
  6551. * param[out] result Result point
  6552. * return Operation status.
  6553. */
  6554. status_t CAAM_PKHA_ECC_PointAdd(CAAM_Type *base,
  6555. caam_handle_t *handle,
  6556. const caam_pkha_ecc_point_t *A,
  6557. const caam_pkha_ecc_point_t *B,
  6558. const uint8_t *N,
  6559. const uint8_t *R2modN,
  6560. const uint8_t *aCurveParam,
  6561. const uint8_t *bCurveParam,
  6562. size_t size,
  6563. caam_pkha_f2m_t arithType,
  6564. caam_pkha_ecc_point_t *result)
  6565. {
  6566. caam_desc_pkha_ecc_t descBuf;
  6567. status_t status;
  6568. do
  6569. {
  6570. status = CAAM_PKHA_ECC_PointAddNonBlocking(base, handle, descBuf, A, B, N, R2modN, aCurveParam, bCurveParam,
  6571. size, arithType, result);
  6572. } while (status == kStatus_CAAM_Again);
  6573. if (status != kStatus_Success)
  6574. {
  6575. return status;
  6576. }
  6577. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  6578. }
  6579. status_t CAAM_PKHA_ECC_PointDoubleNonBlocking(CAAM_Type *base,
  6580. caam_handle_t *handle,
  6581. caam_desc_pkha_ecc_t descriptor,
  6582. const caam_pkha_ecc_point_t *B,
  6583. const uint8_t *N,
  6584. const uint8_t *aCurveParam,
  6585. const uint8_t *bCurveParam,
  6586. size_t size,
  6587. caam_pkha_f2m_t arithType,
  6588. caam_pkha_ecc_point_t *result);
  6589. status_t CAAM_PKHA_ECC_PointDoubleNonBlocking(CAAM_Type *base,
  6590. caam_handle_t *handle,
  6591. caam_desc_pkha_ecc_t descriptor,
  6592. const caam_pkha_ecc_point_t *B,
  6593. const uint8_t *N,
  6594. const uint8_t *aCurveParam,
  6595. const uint8_t *bCurveParam,
  6596. size_t size,
  6597. caam_pkha_f2m_t arithType,
  6598. caam_pkha_ecc_point_t *result)
  6599. {
  6600. caam_pkha_mode_params_t params;
  6601. status_t status;
  6602. caam_pkha_default_parms(&params);
  6603. params.func = kCAAM_PKHA_ArithEccDouble;
  6604. params.arithType = arithType;
  6605. status = caam_pkha_ecc_algorithm_operation_command(base, handle, descriptor, NULL, B, NULL, 0, N, NULL, NULL,
  6606. aCurveParam, bCurveParam, size, result, &params);
  6607. return status;
  6608. }
  6609. /*!
  6610. * brief Doubles elliptic curve points - B + B.
  6611. *
  6612. * This function performs ECC point doubling over a prime field (Fp) or binary field (F2m) using
  6613. * affine coordinates.
  6614. *
  6615. * param base CAAM peripheral base address
  6616. * param B Point to double
  6617. * param N Prime modulus of the field
  6618. * param aCurveParam A parameter from curve equation
  6619. * param bCurveParam B parameter from curve equation (constant)
  6620. * param size Size in bytes of curve points and parameters
  6621. * param arithType Type of arithmetic to perform (integer or F2m)
  6622. * param[out] result Result point
  6623. * return Operation status.
  6624. */
  6625. status_t CAAM_PKHA_ECC_PointDouble(CAAM_Type *base,
  6626. caam_handle_t *handle,
  6627. const caam_pkha_ecc_point_t *B,
  6628. const uint8_t *N,
  6629. const uint8_t *aCurveParam,
  6630. const uint8_t *bCurveParam,
  6631. size_t size,
  6632. caam_pkha_f2m_t arithType,
  6633. caam_pkha_ecc_point_t *result)
  6634. {
  6635. caam_desc_pkha_ecc_t descBuf;
  6636. status_t status;
  6637. do
  6638. {
  6639. status = CAAM_PKHA_ECC_PointDoubleNonBlocking(base, handle, descBuf, B, N, aCurveParam, bCurveParam, size,
  6640. arithType, result);
  6641. } while (status == kStatus_CAAM_Again);
  6642. if (status != kStatus_Success)
  6643. {
  6644. return status;
  6645. }
  6646. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  6647. }
  6648. status_t CAAM_PKHA_ECC_PointMulNonBlocking(CAAM_Type *base,
  6649. caam_handle_t *handle,
  6650. caam_desc_pkha_ecc_t descriptor,
  6651. const caam_pkha_ecc_point_t *A,
  6652. const uint8_t *E,
  6653. size_t sizeE,
  6654. const uint8_t *N,
  6655. const uint8_t *R2modN,
  6656. const uint8_t *aCurveParam,
  6657. const uint8_t *bCurveParam,
  6658. size_t size,
  6659. caam_pkha_timing_t equalTime,
  6660. caam_pkha_f2m_t arithType,
  6661. caam_pkha_ecc_point_t *result);
  6662. status_t CAAM_PKHA_ECC_PointMulNonBlocking(CAAM_Type *base,
  6663. caam_handle_t *handle,
  6664. caam_desc_pkha_ecc_t descriptor,
  6665. const caam_pkha_ecc_point_t *A,
  6666. const uint8_t *E,
  6667. size_t sizeE,
  6668. const uint8_t *N,
  6669. const uint8_t *R2modN,
  6670. const uint8_t *aCurveParam,
  6671. const uint8_t *bCurveParam,
  6672. size_t size,
  6673. caam_pkha_timing_t equalTime,
  6674. caam_pkha_f2m_t arithType,
  6675. caam_pkha_ecc_point_t *result)
  6676. {
  6677. caam_pkha_mode_params_t params;
  6678. status_t status;
  6679. caam_pkha_default_parms(&params);
  6680. params.func = kCAAM_PKHA_ArithEccMul;
  6681. params.equalTime = equalTime;
  6682. params.arithType = arithType;
  6683. params.r2modn = (R2modN != NULL) ? kCAAM_PKHA_InputR2 : kCAAM_PKHA_CalcR2;
  6684. status = caam_pkha_ecc_algorithm_operation_command(base, handle, descriptor, A, NULL, E, sizeE, N, R2modN, NULL,
  6685. aCurveParam, bCurveParam, size, result, &params);
  6686. return status;
  6687. }
  6688. /*!
  6689. * brief Multiplies an elliptic curve point by a scalar - E x (A0, A1).
  6690. *
  6691. * This function performs ECC point multiplication to multiply an ECC point by
  6692. * a scalar integer multiplier over a prime field (Fp) or a binary field (F2m).
  6693. *
  6694. * param base CAAM peripheral base address
  6695. * param A Point as multiplicand
  6696. * param E Scalar multiple
  6697. * param sizeE The size of E, in bytes
  6698. * param N Modulus, a prime number for the Fp field or Irreducible polynomial for F2m field.
  6699. * param R2modN NULL (the function computes R2modN internally) or pointer to pre-computed R2modN (obtained from
  6700. * CAAM_PKHA_ModR2() function).
  6701. * param aCurveParam A parameter from curve equation
  6702. * param bCurveParam B parameter from curve equation (C parameter for operation over F2m).
  6703. * param size Size in bytes of curve points and parameters
  6704. * param equalTime Run the function time equalized or no timing equalization.
  6705. * param arithType Type of arithmetic to perform (integer or F2m)
  6706. * param[out] result Result point
  6707. * return Operation status.
  6708. */
  6709. status_t CAAM_PKHA_ECC_PointMul(CAAM_Type *base,
  6710. caam_handle_t *handle,
  6711. const caam_pkha_ecc_point_t *A,
  6712. const uint8_t *E,
  6713. size_t sizeE,
  6714. const uint8_t *N,
  6715. const uint8_t *R2modN,
  6716. const uint8_t *aCurveParam,
  6717. const uint8_t *bCurveParam,
  6718. size_t size,
  6719. caam_pkha_timing_t equalTime,
  6720. caam_pkha_f2m_t arithType,
  6721. caam_pkha_ecc_point_t *result)
  6722. {
  6723. caam_desc_pkha_ecc_t descBuf;
  6724. status_t status;
  6725. do
  6726. {
  6727. status = CAAM_PKHA_ECC_PointMulNonBlocking(base, handle, descBuf, A, E, sizeE, N, R2modN, aCurveParam,
  6728. bCurveParam, size, equalTime, arithType, result);
  6729. } while (status == kStatus_CAAM_Again);
  6730. if (status != kStatus_Success)
  6731. {
  6732. return status;
  6733. }
  6734. return CAAM_Wait(base, handle, descBuf, kCAAM_Blocking);
  6735. }
  6736. /*!
  6737. * brief Converts from integer to Montgomery format.
  6738. *
  6739. * This function computes R2 mod N and optionally converts A or B into Montgomery format of A or B.
  6740. *
  6741. * param base CAAM peripheral base address
  6742. * param N modulus
  6743. * param sizeN size of N in bytes
  6744. * param[in,out] A The first input in non-Montgomery format. Output Montgomery format of the first input.
  6745. * param[in,out] sizeA pointer to size variable. On input it holds size of input A in bytes. On output it holds size of
  6746. * Montgomery format of A in bytes.
  6747. * param[in,out] B Second input in non-Montgomery format. Output Montgomery format of the second input.
  6748. * param[in,out] sizeB pointer to size variable. On input it holds size of input B in bytes. On output it holds size of
  6749. * Montgomery format of B in bytes.
  6750. * param[out] R2 Output Montgomery factor R2 mod N.
  6751. * param[out] sizeR2 pointer to size variable. On output it holds size of Montgomery factor R2 mod N in bytes.
  6752. * param equalTime Run the function time equalized or no timing equalization.
  6753. * param arithType Type of arithmetic to perform (integer or F2m)
  6754. * return Operation status.
  6755. */
  6756. status_t CAAM_PKHA_NormalToMontgomery(CAAM_Type *base,
  6757. caam_handle_t *handle,
  6758. const uint8_t *N,
  6759. size_t sizeN,
  6760. uint8_t *A,
  6761. size_t *sizeA,
  6762. uint8_t *B,
  6763. size_t *sizeB,
  6764. uint8_t *R2,
  6765. size_t *sizeR2,
  6766. caam_pkha_timing_t equalTime,
  6767. caam_pkha_f2m_t arithType)
  6768. {
  6769. status_t status;
  6770. /* need to convert our Integer inputs into Montgomery format */
  6771. if ((N != NULL) && (sizeN != 0U) && (R2 != NULL) && (sizeR2 != NULL))
  6772. {
  6773. /* 1. R2 = MOD_R2(N) */
  6774. status = CAAM_PKHA_ModR2(base, handle, N, sizeN, R2, sizeR2, arithType);
  6775. if (status != kStatus_Success)
  6776. {
  6777. return status;
  6778. }
  6779. /* 2. A(Montgomery) = MOD_MUL_IM_OM(A, R2, N) */
  6780. if ((A != NULL) && (sizeA != NULL))
  6781. {
  6782. status = CAAM_PKHA_ModMul(base, handle, A, *sizeA, R2, *sizeR2, N, sizeN, A, sizeA, arithType,
  6783. kCAAM_PKHA_MontgomeryFormat, kCAAM_PKHA_MontgomeryFormat, equalTime);
  6784. if (status != kStatus_Success)
  6785. {
  6786. return status;
  6787. }
  6788. }
  6789. /* 2. B(Montgomery) = MOD_MUL_IM_OM(B, R2, N) */
  6790. if ((B != NULL) && (sizeB != NULL))
  6791. {
  6792. status = CAAM_PKHA_ModMul(base, handle, B, *sizeB, R2, *sizeR2, N, sizeN, B, sizeB, arithType,
  6793. kCAAM_PKHA_MontgomeryFormat, kCAAM_PKHA_MontgomeryFormat, equalTime);
  6794. if (status != kStatus_Success)
  6795. {
  6796. return status;
  6797. }
  6798. }
  6799. }
  6800. else
  6801. {
  6802. status = kStatus_InvalidArgument;
  6803. }
  6804. return status;
  6805. }
  6806. /*!
  6807. * brief Converts from Montgomery format to int.
  6808. *
  6809. * This function converts Montgomery format of A or B into int A or B.
  6810. *
  6811. * param base CAAM peripheral base address
  6812. * param N modulus.
  6813. * param sizeN size of N modulus in bytes.
  6814. * param[in,out] A Input first number in Montgomery format. Output is non-Montgomery format.
  6815. * param[in,out] sizeA pointer to size variable. On input it holds size of the input A in bytes. On output it holds
  6816. * size of non-Montgomery A in bytes.
  6817. * param[in,out] B Input first number in Montgomery format. Output is non-Montgomery format.
  6818. * param[in,out] sizeB pointer to size variable. On input it holds size of the input B in bytes. On output it holds
  6819. * size of non-Montgomery B in bytes.
  6820. * param equalTime Run the function time equalized or no timing equalization.
  6821. * param arithType Type of arithmetic to perform (integer or F2m)
  6822. * return Operation status.
  6823. */
  6824. status_t CAAM_PKHA_MontgomeryToNormal(CAAM_Type *base,
  6825. caam_handle_t *handle,
  6826. const uint8_t *N,
  6827. size_t sizeN,
  6828. uint8_t *A,
  6829. size_t *sizeA,
  6830. uint8_t *B,
  6831. size_t *sizeB,
  6832. caam_pkha_timing_t equalTime,
  6833. caam_pkha_f2m_t arithType)
  6834. {
  6835. uint8_t one = 1;
  6836. status_t status = kStatus_InvalidArgument;
  6837. /* A = MOD_MUL_IM_OM(A(Montgomery), 1, N) */
  6838. if ((A != NULL) && (sizeA != NULL))
  6839. {
  6840. status = CAAM_PKHA_ModMul(base, handle, A, *sizeA, &one, sizeof(one), N, sizeN, A, sizeA, arithType,
  6841. kCAAM_PKHA_MontgomeryFormat, kCAAM_PKHA_MontgomeryFormat, equalTime);
  6842. if (kStatus_Success != status)
  6843. {
  6844. return status;
  6845. }
  6846. }
  6847. /* B = MOD_MUL_IM_OM(B(Montgomery), 1, N) */
  6848. if ((B != NULL) && (sizeB != NULL))
  6849. {
  6850. status = CAAM_PKHA_ModMul(base, handle, B, *sizeB, &one, sizeof(one), N, sizeN, B, sizeB, arithType,
  6851. kCAAM_PKHA_MontgomeryFormat, kCAAM_PKHA_MontgomeryFormat, equalTime);
  6852. if (kStatus_Success != status)
  6853. {
  6854. return status;
  6855. }
  6856. }
  6857. return status;
  6858. }