fsl_ltc.c 136 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_ltc.h"
  35. /*******************************************************************************
  36. * Definitions
  37. ******************************************************************************/
  38. /* Component ID definition, used by tools. */
  39. #ifndef FSL_COMPONENT_ID
  40. #define FSL_COMPONENT_ID "platform.drivers.ltc"
  41. #endif
  42. #define LTC_FIFO_SZ_MAX_DOWN_ALGN (0xff0u)
  43. #define LTC_AES_GCM_TYPE_AAD 55
  44. #define LTC_AES_GCM_TYPE_IV 0
  45. #define LTC_CCM_TAG_IDX 8 /*! For CCM encryption, the encrypted final MAC is written to the context word 8-11 */
  46. #define LTC_GCM_TAG_IDX 0 /*! For GCM encryption, the encrypted final MAC is written to the context word 0-3 */
  47. enum _ltc_md_dk_bit_shift
  48. {
  49. kLTC_ModeRegBitShiftDK = 12U,
  50. };
  51. /*! @brief LTC status flags */
  52. enum _ltc_status_flag
  53. {
  54. kLTC_StatusAesBusy = 1U << LTC_STA_AB_SHIFT,
  55. #if defined(FSL_FEATURE_LTC_HAS_DES) && FSL_FEATURE_LTC_HAS_DES
  56. kLTC_StatusDesBusy = 1U << LTC_STA_DB_SHIFT,
  57. #endif /* FSL_FEATURE_LTC_HAS_DES */
  58. #if defined(FSL_FEATURE_LTC_HAS_PKHA) && FSL_FEATURE_LTC_HAS_PKHA
  59. kLTC_StatusPkhaBusy = 1U << LTC_STA_PB_SHIFT,
  60. #endif /* FSL_FEATURE_LTC_HAS_PKHA */
  61. #if defined(FSL_FEATURE_LTC_HAS_SHA) && FSL_FEATURE_LTC_HAS_SHA
  62. kLTC_StatusMdhaBusy = 1U << LTC_STA_MB_SHIFT,
  63. #endif /* FSL_FEATURE_LTC_HAS_SHA */
  64. kLTC_StatusDoneIsr = 1U << LTC_STA_DI_SHIFT,
  65. kLTC_StatusErrorIsr = 1U << LTC_STA_EI_SHIFT,
  66. #if defined(FSL_FEATURE_LTC_HAS_PKHA) && FSL_FEATURE_LTC_HAS_PKHA
  67. kLTC_StatusPublicKeyPrime = 1U << LTC_STA_PKP_SHIFT,
  68. kLTC_StatusPublicKeyOpOne = 1U << LTC_STA_PKO_SHIFT,
  69. kLTC_StatusPublicKeyOpZero = 1U << LTC_STA_PKZ_SHIFT,
  70. #endif /* FSL_FEATURE_LTC_HAS_PKHA */
  71. kLTC_StatusAll = LTC_STA_AB_MASK |
  72. #if defined(FSL_FEATURE_LTC_HAS_DES) && FSL_FEATURE_LTC_HAS_DES
  73. LTC_STA_DB_MASK |
  74. #endif /* FSL_FEATURE_LTC_HAS_DES */
  75. #if defined(FSL_FEATURE_LTC_HAS_SHA) && FSL_FEATURE_LTC_HAS_SHA
  76. LTC_STA_MB_MASK |
  77. #endif /* FSL_FEATURE_LTC_HAS_SHA */
  78. LTC_STA_DI_MASK | LTC_STA_EI_MASK
  79. #if defined(FSL_FEATURE_LTC_HAS_PKHA) && FSL_FEATURE_LTC_HAS_PKHA
  80. |
  81. LTC_STA_PB_MASK | LTC_STA_PKP_MASK | LTC_STA_PKO_MASK | LTC_STA_PKZ_MASK
  82. #endif /* FSL_FEATURE_LTC_HAS_PKHA */
  83. };
  84. /*! @brief LTC clear register */
  85. typedef enum _ltc_clear_written
  86. {
  87. kLTC_ClearMode = 1U << LTC_CW_CM_SHIFT,
  88. kLTC_ClearDataSize = 1U << LTC_CW_CDS_SHIFT,
  89. kLTC_ClearIcvSize = 1U << LTC_CW_CICV_SHIFT,
  90. kLTC_ClearContext = 1U << LTC_CW_CCR_SHIFT,
  91. kLTC_ClearKey = 1U << LTC_CW_CKR_SHIFT,
  92. #if defined(FSL_FEATURE_LTC_HAS_PKHA) && FSL_FEATURE_LTC_HAS_PKHA
  93. kLTC_ClearPkhaSizeA = 1U << LTC_CW_CPKA_SHIFT,
  94. kLTC_ClearPkhaSizeB = 1U << LTC_CW_CPKB_SHIFT,
  95. kLTC_ClearPkhaSizeN = 1U << LTC_CW_CPKN_SHIFT,
  96. kLTC_ClearPkhaSizeE = 1U << LTC_CW_CPKE_SHIFT,
  97. kLTC_ClearAllSize = (int)kLTC_ClearPkhaSizeA | kLTC_ClearPkhaSizeB | kLTC_ClearPkhaSizeN | kLTC_ClearPkhaSizeE,
  98. #endif /* FSL_FEATURE_LTC_HAS_PKHA */
  99. kLTC_ClearOutputFifo = 1U << LTC_CW_COF_SHIFT,
  100. kLTC_ClearInputFifo = (int)(1U << LTC_CW_CIF_SHIFT),
  101. kLTC_ClearAll = (int)(LTC_CW_CM_MASK | LTC_CW_CDS_MASK | LTC_CW_CICV_MASK | LTC_CW_CCR_MASK | LTC_CW_CKR_MASK |
  102. #if defined(FSL_FEATURE_LTC_HAS_PKHA) && FSL_FEATURE_LTC_HAS_PKHA
  103. LTC_CW_CPKA_MASK | LTC_CW_CPKB_MASK | LTC_CW_CPKN_MASK | LTC_CW_CPKE_MASK |
  104. #endif /* FSL_FEATURE_LTC_HAS_PKHA */
  105. LTC_CW_COF_MASK | LTC_CW_CIF_MASK)
  106. } ltc_clear_written_t;
  107. enum _ltc_ctrl_swap
  108. {
  109. kLTC_CtrlSwapAll =
  110. LTC_CTL_IFS_MASK | LTC_CTL_OFS_MASK | LTC_CTL_KIS_MASK | LTC_CTL_KOS_MASK | LTC_CTL_CIS_MASK | LTC_CTL_COS_MASK,
  111. };
  112. /*! @brief Type used in GCM and CCM modes.
  113. Content of a block is established via individual bytes and moved to LTC
  114. IFIFO by moving 32-bit words.
  115. */
  116. typedef union _ltc_xcm_block_t
  117. {
  118. uint32_t w[4]; /*!< LTC context register is 16 bytes written as four 32-bit words */
  119. uint8_t b[16]; /*!< 16 octets block for CCM B0 and CTR0 and for GCM */
  120. } ltc_xcm_block_t;
  121. #if defined(FSL_FEATURE_LTC_HAS_PKHA) && FSL_FEATURE_LTC_HAS_PKHA
  122. /*! @brief PKHA functions - arithmetic, copy/clear memory. */
  123. typedef enum _ltc_pkha_func_t
  124. {
  125. kLTC_PKHA_ClearMem = 1U,
  126. kLTC_PKHA_ArithModAdd = 2U, /*!< (A + B) mod N */
  127. kLTC_PKHA_ArithModSub1 = 3U, /*!< (A - B) mod N */
  128. kLTC_PKHA_ArithModSub2 = 4U, /*!< (B - A) mod N */
  129. kLTC_PKHA_ArithModMul = 5U, /*!< (A x B) mod N */
  130. kLTC_PKHA_ArithModExp = 6U, /*!< (A^E) mod N */
  131. kLTC_PKHA_ArithModRed = 7U, /*!< (A) mod N */
  132. kLTC_PKHA_ArithModInv = 8U, /*!< (A^-1) mod N */
  133. kLTC_PKHA_ArithEccAdd = 9U, /*!< (P1 + P2) */
  134. kLTC_PKHA_ArithEccDouble = 10U, /*!< (P2 + P2) */
  135. kLTC_PKHA_ArithEccMul = 11U, /*!< (E x P1) */
  136. kLTC_PKHA_ArithModR2 = 12U, /*!< (R^2 mod N) */
  137. kLTC_PKHA_ArithGcd = 14U, /*!< GCD (A, N) */
  138. kLTC_PKHA_ArithPrimalityTest = 15U, /*!< Miller-Rabin */
  139. kLTC_PKHA_CopyMemSizeN = 16U,
  140. kLTC_PKHA_CopyMemSizeSrc = 17U,
  141. } ltc_pkha_func_t;
  142. /*! @brief Register areas for PKHA clear memory operations. */
  143. typedef enum _ltc_pkha_reg_area
  144. {
  145. kLTC_PKHA_RegA = 8U,
  146. kLTC_PKHA_RegB = 4U,
  147. kLTC_PKHA_RegE = 2U,
  148. kLTC_PKHA_RegN = 1U,
  149. kLTC_PKHA_RegAll = kLTC_PKHA_RegA | kLTC_PKHA_RegB | kLTC_PKHA_RegE | kLTC_PKHA_RegN,
  150. } ltc_pkha_reg_area_t;
  151. /*! @brief Quadrant areas for 2048-bit registers for PKHA copy memory
  152. * operations. */
  153. typedef enum _ltc_pkha_quad_area_t
  154. {
  155. kLTC_PKHA_Quad0 = 0U,
  156. kLTC_PKHA_Quad1 = 1U,
  157. kLTC_PKHA_Quad2 = 2U,
  158. kLTC_PKHA_Quad3 = 3U,
  159. } ltc_pkha_quad_area_t;
  160. /*! @brief User-supplied (R^2 mod N) input or LTC should calculate. */
  161. typedef enum _ltc_pkha_r2_t
  162. {
  163. kLTC_PKHA_CalcR2 = 0U, /*!< Calculate (R^2 mod N) */
  164. kLTC_PKHA_InputR2 = 1U /*!< (R^2 mod N) supplied as input */
  165. } ltc_pkha_r2_t;
  166. /*! @brief LTC PKHA parameters */
  167. typedef struct _ltc_pkha_mode_params_t
  168. {
  169. ltc_pkha_func_t func;
  170. ltc_pkha_f2m_t arithType;
  171. ltc_pkha_montgomery_form_t montFormIn;
  172. ltc_pkha_montgomery_form_t montFormOut;
  173. ltc_pkha_reg_area_t srcReg;
  174. ltc_pkha_quad_area_t srcQuad;
  175. ltc_pkha_reg_area_t dstReg;
  176. ltc_pkha_quad_area_t dstQuad;
  177. ltc_pkha_timing_t equalTime;
  178. ltc_pkha_r2_t r2modn;
  179. } ltc_pkha_mode_params_t;
  180. #endif /* FSL_FEATURE_LTC_HAS_PKHA */
  181. /*******************************************************************************
  182. * Prototypes
  183. ******************************************************************************/
  184. #if defined(FSL_FEATURE_LTC_HAS_PKHA) && FSL_FEATURE_LTC_HAS_PKHA
  185. static status_t ltc_pkha_clear_regabne(LTC_Type *base, bool A, bool B, bool N, bool E);
  186. #endif /* FSL_FEATURE_LTC_HAS_PKHA */
  187. /*******************************************************************************
  188. * Code
  189. ******************************************************************************/
  190. /*******************************************************************************
  191. * LTC Common code static
  192. ******************************************************************************/
  193. /*!
  194. * @brief Tests the correct key size.
  195. *
  196. * This function tests the correct key size.
  197. * @param keySize Input key length in bytes.
  198. * @return True if the key length is supported, false if not.
  199. */
  200. bool ltc_check_key_size(const uint32_t keySize)
  201. {
  202. return ((keySize == 16u)
  203. #if defined(FSL_FEATURE_LTC_HAS_AES192) && FSL_FEATURE_LTC_HAS_AES192
  204. || ((keySize == 24u))
  205. #endif /* FSL_FEATURE_LTC_HAS_AES192 */
  206. #if defined(FSL_FEATURE_LTC_HAS_AES256) && FSL_FEATURE_LTC_HAS_AES256
  207. || ((keySize == 32u))
  208. #endif /* FSL_FEATURE_LTC_HAS_AES256 */
  209. );
  210. }
  211. /*! @brief LTC driver wait mechanism. */
  212. status_t ltc_wait(LTC_Type *base)
  213. {
  214. status_t status;
  215. bool error = false;
  216. bool done = false;
  217. /* Wait for 'done' or 'error' flag. */
  218. while ((!error) && (!done))
  219. {
  220. uint32_t temp32 = base->STA;
  221. error = temp32 & LTC_STA_EI_MASK;
  222. done = temp32 & LTC_STA_DI_MASK;
  223. }
  224. if (error)
  225. {
  226. base->COM = LTC_COM_ALL_MASK; /* Reset all engine to clear the error flag */
  227. status = kStatus_Fail;
  228. }
  229. else /* 'done' */
  230. {
  231. status = kStatus_Success;
  232. base->CW = kLTC_ClearDataSize;
  233. /* Clear 'done' interrupt status. This also clears the mode register. */
  234. base->STA = kLTC_StatusDoneIsr;
  235. }
  236. return status;
  237. }
  238. /*!
  239. * @brief Clears the LTC module.
  240. * This function can be used to clear all sensitive data from theLTC module, such as private keys. It is called
  241. * internally by the LTC driver in case of an error or operation complete.
  242. * @param base LTC peripheral base address
  243. * @param pkha Include LTC PKHA register clear. If there is no PKHA, the argument is ignored.
  244. */
  245. void ltc_clear_all(LTC_Type *base, bool addPKHA)
  246. {
  247. base->CW = (uint32_t)kLTC_ClearAll;
  248. #if defined(FSL_FEATURE_LTC_HAS_PKHA) && FSL_FEATURE_LTC_HAS_PKHA
  249. if (addPKHA)
  250. {
  251. ltc_pkha_clear_regabne(base, true, true, true, true);
  252. }
  253. #endif /* FSL_FEATURE_LTC_HAS_PKHA */
  254. }
  255. void ltc_memcpy(void *dst, const void *src, size_t size)
  256. {
  257. #if defined(__cplusplus)
  258. register uint8_t *to = (uint8_t *)dst;
  259. register const uint8_t *from = (const uint8_t *)src;
  260. #else
  261. register uint8_t *to = dst;
  262. register const uint8_t *from = src;
  263. #endif
  264. while (size)
  265. {
  266. *to = *from;
  267. size--;
  268. to++;
  269. from++;
  270. }
  271. }
  272. /*!
  273. * @brief Reads an unaligned word.
  274. *
  275. * This function creates a 32-bit word from an input array of four bytes.
  276. *
  277. * @param src Input array of four bytes. The array can start at any address in memory.
  278. * @return 32-bit unsigned int created from the input byte array.
  279. */
  280. /* Force lower optimization for Keil, otherwise it replaces inline LDR with LDM */
  281. #if defined(__CC_ARM)
  282. #pragma push
  283. #pragma O0
  284. #endif
  285. static inline uint32_t ltc_get_word_from_unaligned(const uint8_t *srcAddr)
  286. {
  287. #if (!(defined(__CORTEX_M)) || (defined(__CORTEX_M) && (__CORTEX_M == 0)))
  288. register const uint8_t *src = srcAddr;
  289. /* Cortex M0 does not support misaligned loads */
  290. if ((uint32_t)src & 0x3u)
  291. {
  292. union _align_bytes_t
  293. {
  294. uint32_t word;
  295. uint8_t byte[sizeof(uint32_t)];
  296. } my_bytes;
  297. my_bytes.byte[0] = *src;
  298. my_bytes.byte[1] = *(src + 1);
  299. my_bytes.byte[2] = *(src + 2);
  300. my_bytes.byte[3] = *(src + 3);
  301. return my_bytes.word;
  302. }
  303. else
  304. {
  305. /* addr aligned to 0-modulo-4 so it is safe to type cast */
  306. return *((const uint32_t *)src);
  307. }
  308. #elif defined(__CC_ARM)
  309. /* -O3 optimization in Keil 5.15 and 5.16a uses LDM instruction here (LDM r4!, {r0})
  310. * which is wrong, because srcAddr might be unaligned.
  311. * LDM on unaligned address causes hard-fault. in contrary,
  312. * LDR supports unaligned address on Cortex M4 */
  313. register uint32_t retVal;
  314. __asm
  315. {
  316. LDR retVal, [srcAddr]
  317. }
  318. return retVal;
  319. #else
  320. return *((const uint32_t *)srcAddr);
  321. #endif
  322. }
  323. /* End lower optimization */
  324. #if defined(__CC_ARM)
  325. #pragma pop
  326. #endif
  327. /*!
  328. * @brief Converts a 32-bit word into a byte array.
  329. *
  330. * This function creates an output array of four bytes from an input 32-bit word.
  331. *
  332. * @param srcWord Input 32-bit unsigned integer.
  333. * @param dst Output array of four bytes. The array can start at any address in memory.
  334. */
  335. static inline void ltc_set_unaligned_from_word(uint32_t srcWord, uint8_t *dstAddr)
  336. {
  337. #if (!(defined(__CORTEX_M)) || (defined(__CORTEX_M) && (__CORTEX_M == 0)))
  338. register uint8_t *dst = dstAddr;
  339. /* Cortex M0 does not support misaligned stores */
  340. if ((uint32_t)dst & 0x3u)
  341. {
  342. *dst++ = (srcWord & 0x000000FFU);
  343. *dst++ = (srcWord & 0x0000FF00U) >> 8;
  344. *dst++ = (srcWord & 0x00FF0000U) >> 16;
  345. *dst++ = (srcWord & 0xFF000000U) >> 24;
  346. }
  347. else
  348. {
  349. *((uint32_t *)dstAddr) = srcWord; /* addr aligned to 0-modulo-4 so it is safe to type cast */
  350. }
  351. #elif defined(__CC_ARM)
  352. __asm
  353. {
  354. STR srcWord, [dstAddr]
  355. }
  356. return;
  357. #else
  358. *((uint32_t *)dstAddr) = srcWord;
  359. #endif
  360. }
  361. /*!
  362. * @brief Sets the LTC keys.
  363. *
  364. * This function writes the LTC keys into the key register. The keys should
  365. * be written before the key size.
  366. *
  367. * @param base LTC peripheral base address
  368. * @param key Key
  369. * @param keySize Number of bytes for all keys to be loaded (maximum 32, must be a
  370. * multiple of 4).
  371. * @returns Key set status
  372. */
  373. static status_t ltc_set_key(LTC_Type *base, const uint8_t *key, uint8_t keySize)
  374. {
  375. int32_t i;
  376. for (i = 0; i < (keySize / 4); i++)
  377. {
  378. base->KEY[i] = ltc_get_word_from_unaligned(key + i * sizeof(uint32_t));
  379. }
  380. return kStatus_Success;
  381. }
  382. /*!
  383. * @brief Gets the LTC keys.
  384. *
  385. * This function retrieves the LTC keys from the key register.
  386. *
  387. * @param base LTC peripheral base address
  388. * @param key Array of data to store keys
  389. * @param keySize Number of bytes of keys to retrieve
  390. * @returns Key set status
  391. */
  392. static status_t ltc_get_key(LTC_Type *base, uint8_t *key, uint8_t keySize)
  393. {
  394. int32_t i;
  395. for (i = 0; i < (keySize / 4); i++)
  396. {
  397. ltc_set_unaligned_from_word(base->KEY[i], key + i * sizeof(uint32_t));
  398. }
  399. return kStatus_Success;
  400. }
  401. /*!
  402. * @brief Writes the LTC context register;
  403. *
  404. * The LTC context register is a 512 bit (64 byte) register that holds
  405. * internal context for the crypto engine. The meaning varies based on the
  406. * algorithm and operating state being used. This register is written by the
  407. * driver/application to load state such as IV, counter, and so on. Then, it is
  408. * updated by the internal crypto engine as needed.
  409. *
  410. * @param base LTC peripheral base address
  411. * @param data Data to write
  412. * @param dataSize Size of data to write in bytes
  413. * @param startIndex Starting word (4-byte) index into the 16-word register.
  414. * @return Status of write
  415. */
  416. status_t ltc_set_context(LTC_Type *base, const uint8_t *data, uint8_t dataSize, uint8_t startIndex)
  417. {
  418. int32_t i;
  419. int32_t j;
  420. int32_t szLeft;
  421. /* Context register is 16 words in size (64 bytes). Ensure we are only
  422. * writing a valid amount of data. */
  423. if (startIndex + (dataSize / 4) >= 16)
  424. {
  425. return kStatus_InvalidArgument;
  426. }
  427. j = 0;
  428. szLeft = dataSize % 4;
  429. for (i = startIndex; i < (startIndex + dataSize / 4); i++)
  430. {
  431. base->CTX[i] = ltc_get_word_from_unaligned(data + j);
  432. j += sizeof(uint32_t);
  433. }
  434. if (szLeft)
  435. {
  436. uint32_t context_data = {0};
  437. ltc_memcpy(&context_data, data + j, szLeft);
  438. base->CTX[i] = context_data;
  439. }
  440. return kStatus_Success;
  441. }
  442. /*!
  443. * @brief Reads the LTC context register.
  444. *
  445. * The LTC context register is a 512 bit (64 byte) register that holds
  446. * internal context for the crypto engine. The meaning varies based on the
  447. * algorithm and operating state being used. This register is written by the
  448. * driver/application to load state such as IV, counter, and so on. Then, it is
  449. * updated by the internal crypto engine as needed.
  450. *
  451. * @param base LTC peripheral base address
  452. * @param data Destination of read data
  453. * @param dataSize Size of data to read in bytes
  454. * @param startIndex Starting word (4-byte) index into the 16-word register.
  455. * @return Status of read
  456. */
  457. status_t ltc_get_context(LTC_Type *base, uint8_t *dest, uint8_t dataSize, uint8_t startIndex)
  458. {
  459. int32_t i;
  460. int32_t j;
  461. int32_t szLeft;
  462. uint32_t rdCtx;
  463. /* Context register is 16 words in size (64 bytes). Ensure we are only
  464. * writing a valid amount of data. */
  465. if (startIndex + (dataSize / 4) >= 16)
  466. {
  467. return kStatus_InvalidArgument;
  468. }
  469. j = 0;
  470. szLeft = dataSize % 4;
  471. for (i = startIndex; i < (startIndex + dataSize / 4); i++)
  472. {
  473. ltc_set_unaligned_from_word(base->CTX[i], dest + j);
  474. j += sizeof(uint32_t);
  475. }
  476. if (szLeft)
  477. {
  478. rdCtx = 0;
  479. rdCtx = base->CTX[i];
  480. ltc_memcpy(dest + j, &rdCtx, szLeft);
  481. }
  482. return kStatus_Success;
  483. }
  484. static status_t ltc_symmetric_alg_state(LTC_Type *base,
  485. const uint8_t *key,
  486. uint8_t keySize,
  487. ltc_algorithm_t alg,
  488. ltc_mode_symmetric_alg_t mode,
  489. ltc_mode_encrypt_t enc,
  490. ltc_mode_algorithm_state_t as)
  491. {
  492. ltc_mode_t modeReg;
  493. /* Clear internal register states. */
  494. base->CW = (uint32_t)kLTC_ClearAll;
  495. /* Set byte swap on for several registers we will be reading and writing
  496. * user data to/from. */
  497. base->CTL |= kLTC_CtrlSwapAll;
  498. /* Write the key in place. */
  499. ltc_set_key(base, key, keySize);
  500. /* Write the key size. This must be done after writing the key, and this
  501. * action locks the ability to modify the key registers. */
  502. base->KS = keySize;
  503. /* Clear the 'done' interrupt. */
  504. base->STA = kLTC_StatusDoneIsr;
  505. /* Set the proper block and algorithm mode. */
  506. modeReg = (uint32_t)alg | (uint32_t)enc | (uint32_t)as | (uint32_t)mode;
  507. /* Write the mode register to the hardware. */
  508. base->MD = modeReg;
  509. return kStatus_Success;
  510. }
  511. /*!
  512. * @brief Initializes the LTC for symmetric encrypt/decrypt operation. Mode is set to UPDATE.
  513. *
  514. * @param base LTC peripheral base address
  515. * @param key Input key to use for encryption
  516. * @param keySize Size of the input key, in bytes. Must be 8, 16, 24, or 32.
  517. * @param alg Symmetric algorithm
  518. * @param mode Symmetric block mode
  519. * @param enc Encrypt/decrypt control
  520. * @return Status
  521. */
  522. status_t ltc_symmetric_update(LTC_Type *base,
  523. const uint8_t *key,
  524. uint8_t keySize,
  525. ltc_algorithm_t alg,
  526. ltc_mode_symmetric_alg_t mode,
  527. ltc_mode_encrypt_t enc)
  528. {
  529. return ltc_symmetric_alg_state(base, key, keySize, alg, mode, enc, kLTC_ModeUpdate);
  530. }
  531. #if defined(FSL_FEATURE_LTC_HAS_GCM) && FSL_FEATURE_LTC_HAS_GCM
  532. /*!
  533. * @brief Initializes the LTC for symmetric encrypt/decrypt operation. Mode is set to FINALIZE.
  534. *
  535. * @param base LTC peripheral base address
  536. * @param key Input key to use for encryption
  537. * @param keySize Size of the input key, in bytes. Must be 8, 16, 24, or 32.
  538. * @param alg Symmetric algorithm
  539. * @param mode Symmetric block mode
  540. * @param enc Encrypt/decrypt control
  541. * @return Status
  542. */
  543. static status_t ltc_symmetric_final(LTC_Type *base,
  544. const uint8_t *key,
  545. uint8_t keySize,
  546. ltc_algorithm_t alg,
  547. ltc_mode_symmetric_alg_t mode,
  548. ltc_mode_encrypt_t enc)
  549. {
  550. return ltc_symmetric_alg_state(base, key, keySize, alg, mode, enc, kLTC_ModeFinalize);
  551. }
  552. #endif /* FSL_FEATURE_LTC_HAS_GCM */
  553. /*!
  554. * @brief Initializes the LTC for symmetric encrypt/decrypt operation. Mode is set to INITIALIZE.
  555. *
  556. * @param base LTC peripheral base address
  557. * @param key Input key to use for encryption
  558. * @param keySize Size of the input key, in bytes. Must be 8, 16, 24, or 32.
  559. * @param alg Symmetric algorithm
  560. * @param mode Symmetric block mode
  561. * @param enc Encrypt/decrypt control
  562. * @return Status
  563. */
  564. static status_t ltc_symmetric_init(LTC_Type *base,
  565. const uint8_t *key,
  566. uint8_t keySize,
  567. ltc_algorithm_t alg,
  568. ltc_mode_symmetric_alg_t mode,
  569. ltc_mode_encrypt_t enc)
  570. {
  571. return ltc_symmetric_alg_state(base, key, keySize, alg, mode, enc, kLTC_ModeInit);
  572. }
  573. /*!
  574. * @brief Initializes the LTC for symmetric encrypt/decrypt operation. Mode is set to INITIALIZE/FINALIZE.
  575. *
  576. * @param base LTC peripheral base address
  577. * @param key Input key to use for encryption
  578. * @param keySize Size of the input key, in bytes. Must be 8, 16, 24, or 32.
  579. * @param alg Symmetric algorithm
  580. * @param mode Symmetric block mode
  581. * @param enc Encrypt/decrypt control
  582. * @return Status
  583. */
  584. static status_t ltc_symmetric_init_final(LTC_Type *base,
  585. const uint8_t *key,
  586. uint8_t keySize,
  587. ltc_algorithm_t alg,
  588. ltc_mode_symmetric_alg_t mode,
  589. ltc_mode_encrypt_t enc)
  590. {
  591. return ltc_symmetric_alg_state(base, key, keySize, alg, mode, enc, kLTC_ModeInitFinal);
  592. }
  593. void ltc_symmetric_process(LTC_Type *base, uint32_t inSize, const uint8_t **inData, uint8_t **outData)
  594. {
  595. uint32_t outSize;
  596. uint32_t fifoData;
  597. uint32_t fifoStatus;
  598. register const uint8_t *in = *inData;
  599. register uint8_t *out = *outData;
  600. outSize = inSize;
  601. while ((outSize > 0) || (inSize > 0))
  602. {
  603. fifoStatus = base->FIFOSTA;
  604. /* Check output FIFO level to make sure there is at least an entry
  605. * ready to be read. */
  606. if (fifoStatus & LTC_FIFOSTA_OFL_MASK)
  607. {
  608. /* Read data from the output FIFO. */
  609. if (outSize > 0)
  610. {
  611. if (outSize >= sizeof(uint32_t))
  612. {
  613. ltc_set_unaligned_from_word(base->OFIFO, out);
  614. out += sizeof(uint32_t);
  615. outSize -= sizeof(uint32_t);
  616. }
  617. else /* (outSize > 0) && (outSize < 4) */
  618. {
  619. fifoData = base->OFIFO;
  620. ltc_memcpy(out, &fifoData, outSize);
  621. out += outSize;
  622. outSize = 0;
  623. }
  624. }
  625. }
  626. /* Check input FIFO status to see if it is full. We can
  627. * only write more data when both input and output FIFOs are not at a full state.
  628. * At the same time we are sure Output FIFO is not full because we have poped at least one entry
  629. * by the while loop above.
  630. */
  631. if (!(fifoStatus & LTC_FIFOSTA_IFF_MASK))
  632. {
  633. /* Copy data to the input FIFO.
  634. * Data can only be copied one word at a time, so pad the data
  635. * appropriately if it is less than this size. */
  636. if (inSize > 0)
  637. {
  638. if (inSize >= sizeof(uint32_t))
  639. {
  640. base->IFIFO = ltc_get_word_from_unaligned(in);
  641. inSize -= sizeof(uint32_t);
  642. in += sizeof(uint32_t);
  643. }
  644. else /* (inSize > 0) && (inSize < 4) */
  645. {
  646. fifoData = 0;
  647. ltc_memcpy(&fifoData, in, inSize);
  648. base->IFIFO = fifoData;
  649. in += inSize;
  650. inSize = 0;
  651. }
  652. }
  653. }
  654. }
  655. *inData = in;
  656. *outData = out;
  657. }
  658. /*!
  659. * @brief Processes symmetric data through LTC AES and DES engines.
  660. *
  661. * @param base LTC peripheral base address
  662. * @param inData Input data
  663. * @param inSize Size of input data, in bytes
  664. * @param outData Output data
  665. * @return Status from encrypt/decrypt operation
  666. */
  667. status_t ltc_symmetric_process_data(LTC_Type *base, const uint8_t *inData, uint32_t inSize, uint8_t *outData)
  668. {
  669. uint32_t lastSize;
  670. if ((!inData) || (!outData))
  671. {
  672. return kStatus_InvalidArgument;
  673. }
  674. /* Write the data size. */
  675. base->DS = inSize;
  676. /* Split the inSize into full 16-byte chunks and last incomplete block due to LTC AES OFIFO errata */
  677. if (inSize <= 16u)
  678. {
  679. lastSize = inSize;
  680. inSize = 0;
  681. }
  682. else
  683. {
  684. /* Process all 16-byte data chunks. */
  685. lastSize = inSize % 16u;
  686. if (lastSize == 0)
  687. {
  688. lastSize = 16;
  689. inSize -= 16;
  690. }
  691. else
  692. {
  693. inSize -= lastSize; /* inSize will be rounded down to 16 byte boundary. remaining bytes in lastSize */
  694. }
  695. }
  696. ltc_symmetric_process(base, inSize, &inData, &outData);
  697. ltc_symmetric_process(base, lastSize, &inData, &outData);
  698. return ltc_wait(base);
  699. }
  700. /*!
  701. * @brief Splits the LTC job into sessions. Used for CBC, CTR, CFB, OFB cipher block modes.
  702. *
  703. * @param base LTC peripheral base address
  704. * @param inData Input data to process.
  705. * @param inSize Input size of the input buffer.
  706. * @param outData Output data buffer.
  707. */
  708. static status_t ltc_process_message_in_sessions(LTC_Type *base,
  709. const uint8_t *inData,
  710. uint32_t inSize,
  711. uint8_t *outData)
  712. {
  713. uint32_t sz;
  714. status_t retval;
  715. ltc_mode_t modeReg; /* read and write LTC mode register */
  716. sz = LTC_FIFO_SZ_MAX_DOWN_ALGN;
  717. modeReg = base->MD;
  718. retval = kStatus_Success;
  719. while (inSize)
  720. {
  721. if (inSize <= sz)
  722. {
  723. retval = ltc_symmetric_process_data(base, inData, inSize, outData);
  724. if (kStatus_Success != retval)
  725. {
  726. return retval;
  727. }
  728. inSize = 0;
  729. }
  730. else
  731. {
  732. retval = ltc_symmetric_process_data(base, inData, sz, outData);
  733. if (kStatus_Success != retval)
  734. {
  735. return retval;
  736. }
  737. inData += sz;
  738. inSize -= sz;
  739. outData += sz;
  740. base->MD = modeReg;
  741. }
  742. }
  743. return retval;
  744. }
  745. static void ltc_move_block_to_ififo(LTC_Type *base, const ltc_xcm_block_t *blk, uint32_t num_bytes)
  746. {
  747. uint32_t i = 0;
  748. uint32_t words;
  749. words = num_bytes / 4u;
  750. if (num_bytes % 4u)
  751. {
  752. words++;
  753. }
  754. if (words > 4)
  755. {
  756. words = 4;
  757. }
  758. while (i < words)
  759. {
  760. if (0U == (base->FIFOSTA & LTC_FIFOSTA_IFF_MASK))
  761. {
  762. /* Copy data to the input FIFO. */
  763. base->IFIFO = blk->w[i++];
  764. }
  765. }
  766. }
  767. static void ltc_move_to_ififo(LTC_Type *base, const uint8_t *data, uint32_t dataSize)
  768. {
  769. ltc_xcm_block_t blk;
  770. ltc_xcm_block_t blkZero = {{0x0u, 0x0u, 0x0u, 0x0u}};
  771. while (dataSize)
  772. {
  773. if (dataSize > 16u)
  774. {
  775. ltc_memcpy(&blk, data, 16u);
  776. dataSize -= 16u;
  777. data += 16u;
  778. }
  779. else
  780. {
  781. ltc_memcpy(&blk, &blkZero, sizeof(ltc_xcm_block_t)); /* memset blk to zeroes */
  782. ltc_memcpy(&blk, data, dataSize);
  783. dataSize = 0;
  784. }
  785. ltc_move_block_to_ififo(base, &blk, sizeof(ltc_xcm_block_t));
  786. }
  787. }
  788. /*!
  789. * @brief Processes symmetric data through LTC AES in multiple sessions.
  790. *
  791. * Specific for AES CCM and GCM modes as they need to update mode register.
  792. *
  793. * @param base LTC peripheral base address
  794. * @param inData Input data
  795. * @param inSize Size of input data, in bytes
  796. * @param outData Output data
  797. * @param lastAs The LTC Algorithm state to be set sup for last block during message processing in multiple sessions.
  798. * For CCM it is kLTC_ModeFinalize. For GCM it is kLTC_ModeInitFinal.
  799. * @return Status from encrypt/decrypt operation
  800. */
  801. static status_t ltc_symmetric_process_data_multiple(LTC_Type *base,
  802. const uint8_t *inData,
  803. uint32_t inSize,
  804. uint8_t *outData,
  805. ltc_mode_t modeReg,
  806. ltc_mode_algorithm_state_t lastAs)
  807. {
  808. uint32_t fifoConsumed;
  809. uint32_t lastSize;
  810. uint32_t sz;
  811. uint32_t max_ltc_fifo_size;
  812. ltc_mode_algorithm_state_t fsm;
  813. status_t status;
  814. if ((!inData) || (!outData))
  815. {
  816. return kStatus_InvalidArgument;
  817. }
  818. if (!((kLTC_ModeFinalize == lastAs) || (kLTC_ModeInitFinal == lastAs)))
  819. {
  820. return kStatus_InvalidArgument;
  821. }
  822. if (0 == inSize)
  823. {
  824. return kStatus_Success;
  825. }
  826. if (inSize <= 16u)
  827. {
  828. fsm = lastAs;
  829. lastSize = inSize;
  830. }
  831. else
  832. {
  833. fsm = (ltc_mode_algorithm_state_t)(
  834. modeReg &
  835. LTC_MD_AS_MASK); /* this will be either kLTC_ModeInit or kLTC_ModeUpdate, based on prior processing */
  836. /* Process all 16-byte data chunks. */
  837. lastSize = inSize % 16u;
  838. if (lastSize == 0u)
  839. {
  840. lastSize = 16u;
  841. inSize -= 16u;
  842. }
  843. else
  844. {
  845. inSize -= lastSize; /* inSize will be rounded down to 16 byte boundary. remaining bytes in lastSize */
  846. }
  847. }
  848. max_ltc_fifo_size = LTC_FIFO_SZ_MAX_DOWN_ALGN;
  849. fifoConsumed = base->DS;
  850. while (lastSize)
  851. {
  852. switch (fsm)
  853. {
  854. case kLTC_ModeUpdate:
  855. case kLTC_ModeInit:
  856. while (inSize)
  857. {
  858. if (inSize > (max_ltc_fifo_size - fifoConsumed))
  859. {
  860. sz = (max_ltc_fifo_size - fifoConsumed);
  861. }
  862. else
  863. {
  864. sz = inSize;
  865. }
  866. base->DS = sz;
  867. ltc_symmetric_process(base, sz, &inData, &outData);
  868. inSize -= sz;
  869. fifoConsumed = 0;
  870. /* after we completed INITIALIZE job, are there still any data left? */
  871. if (inSize)
  872. {
  873. fsm = kLTC_ModeUpdate;
  874. status = ltc_wait(base);
  875. if (kStatus_Success != status)
  876. {
  877. return status;
  878. }
  879. modeReg &= ~LTC_MD_AS_MASK;
  880. modeReg |= (uint32_t)fsm;
  881. base->MD = modeReg;
  882. }
  883. else
  884. {
  885. fsm = lastAs;
  886. }
  887. }
  888. break;
  889. case kLTC_ModeFinalize:
  890. case kLTC_ModeInitFinal:
  891. /* process last block in FINALIZE */
  892. status = ltc_wait(base);
  893. if (kStatus_Success != status)
  894. {
  895. return status;
  896. }
  897. modeReg &= ~LTC_MD_AS_MASK;
  898. modeReg |= (uint32_t)lastAs;
  899. base->MD = modeReg;
  900. base->DS = lastSize;
  901. ltc_symmetric_process(base, lastSize, &inData, &outData);
  902. lastSize = 0;
  903. break;
  904. default:
  905. break;
  906. }
  907. }
  908. status = ltc_wait(base);
  909. return status;
  910. }
  911. /*!
  912. * @brief Receives MAC compare.
  913. *
  914. * This function is a sub-process of CCM and GCM decryption.
  915. * It compares received MAC with the MAC computed during decryption.
  916. *
  917. * @param base LTC peripheral base address
  918. * @param tag Received MAC.
  919. * @param tagSize Number of bytes in the received MAC.
  920. * @param modeReg LTC Mode Register current value. It is modified and written to LTC Mode Register.
  921. */
  922. static status_t ltc_aes_received_mac_compare(LTC_Type *base, const uint8_t *tag, uint32_t tagSize, ltc_mode_t modeReg)
  923. {
  924. ltc_xcm_block_t blk = {{0x0u, 0x0u, 0x0u, 0x0u}};
  925. base->CW = kLTC_ClearDataSize;
  926. base->STA = kLTC_StatusDoneIsr;
  927. modeReg &= ~LTC_MD_AS_MASK;
  928. modeReg |= (uint32_t)kLTC_ModeUpdate | LTC_MD_ICV_TEST_MASK;
  929. base->MD = modeReg;
  930. base->DS = 0u;
  931. base->ICVS = tagSize;
  932. ltc_memcpy(&blk.b[0], &tag[0], tagSize);
  933. ltc_move_block_to_ififo(base, &blk, tagSize);
  934. return ltc_wait(base);
  935. }
  936. /*!
  937. * @brief Processes tag during AES GCM and CCM.
  938. *
  939. * This function is a sub-process of CCM and GCM encryption and decryption.
  940. * For encryption, it writes computed MAC to the output tag.
  941. * For decryption, it compares the received MAC with the computed MAC.
  942. *
  943. * @param base LTC peripheral base address
  944. * @param[in,out] tag Output computed MAC during encryption or Input received MAC during decryption.
  945. * @param tagSize Size of MAC buffer in bytes.
  946. * @param modeReg LTC Mode Register current value. It is checked to read Enc/Dec bit.
  947. * It is modified and written to LTC Mode Register during decryption.
  948. * @param ctx Index to LTC context registers with computed MAC for encryption process.
  949. */
  950. static status_t ltc_aes_process_tag(LTC_Type *base, uint8_t *tag, uint32_t tagSize, ltc_mode_t modeReg, uint32_t ctx)
  951. {
  952. status_t status = kStatus_Success;
  953. if (tag)
  954. {
  955. /* For decrypt, compare received MAC with the computed MAC. */
  956. if (kLTC_ModeDecrypt == (modeReg & LTC_MD_ENC_MASK))
  957. {
  958. status = ltc_aes_received_mac_compare(base, tag, tagSize, modeReg);
  959. }
  960. else /* FSL_AES_GCM_TYPE_ENCRYPT */
  961. {
  962. /* For encryption, write the computed and encrypted MAC to user buffer */
  963. ltc_get_context(base, &tag[0], tagSize, ctx);
  964. }
  965. }
  966. return status;
  967. }
  968. /*******************************************************************************
  969. * LTC Common code public
  970. ******************************************************************************/
  971. void LTC_Init(LTC_Type *base)
  972. {
  973. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  974. /* ungate clock */
  975. CLOCK_EnableClock(kCLOCK_Ltc0);
  976. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  977. }
  978. void LTC_Deinit(LTC_Type *base)
  979. {
  980. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  981. /* gate clock */
  982. CLOCK_DisableClock(kCLOCK_Ltc0);
  983. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  984. }
  985. #if defined(FSL_FEATURE_LTC_HAS_DPAMS) && FSL_FEATURE_LTC_HAS_DPAMS
  986. void LTC_SetDpaMaskSeed(LTC_Type *base, uint32_t mask)
  987. {
  988. base->DPAMS = mask;
  989. /* second write as workaround for DPA mask re-seed errata */
  990. base->DPAMS = mask;
  991. }
  992. #endif /* FSL_FEATURE_LTC_HAS_DPAMS */
  993. /*******************************************************************************
  994. * AES Code static
  995. ******************************************************************************/
  996. static status_t ltc_aes_decrypt_ecb(LTC_Type *base,
  997. const uint8_t *ciphertext,
  998. uint8_t *plaintext,
  999. uint32_t size,
  1000. const uint8_t *key,
  1001. uint32_t keySize,
  1002. ltc_aes_key_t keyType)
  1003. {
  1004. status_t retval;
  1005. /* Initialize algorithm state. */
  1006. ltc_symmetric_update(base, key, keySize, kLTC_AlgorithmAES, kLTC_ModeECB, kLTC_ModeDecrypt);
  1007. /* set DK bit in the LTC Mode Register AAI field for directly loaded decrypt keys */
  1008. if (keyType == kLTC_DecryptKey)
  1009. {
  1010. base->MD |= (1U << kLTC_ModeRegBitShiftDK);
  1011. }
  1012. /* Process data and return status. */
  1013. retval = ltc_process_message_in_sessions(base, &ciphertext[0], size, &plaintext[0]);
  1014. return retval;
  1015. }
  1016. /*******************************************************************************
  1017. * AES Code public
  1018. ******************************************************************************/
  1019. status_t LTC_AES_GenerateDecryptKey(LTC_Type *base, const uint8_t *encryptKey, uint8_t *decryptKey, uint32_t keySize)
  1020. {
  1021. uint8_t plaintext[LTC_AES_BLOCK_SIZE];
  1022. uint8_t ciphertext[LTC_AES_BLOCK_SIZE];
  1023. status_t status;
  1024. if (!ltc_check_key_size(keySize))
  1025. {
  1026. return kStatus_InvalidArgument;
  1027. }
  1028. /* ECB decrypt with encrypt key will convert the key in LTC context into decrypt form of the key */
  1029. status = ltc_aes_decrypt_ecb(base, ciphertext, plaintext, LTC_AES_BLOCK_SIZE, encryptKey, keySize, kLTC_EncryptKey);
  1030. /* now there is decrypt form of the key in the LTC context, so take it */
  1031. ltc_get_key(base, decryptKey, keySize);
  1032. ltc_clear_all(base, false);
  1033. return status;
  1034. }
  1035. status_t LTC_AES_EncryptEcb(
  1036. LTC_Type *base, const uint8_t *plaintext, uint8_t *ciphertext, uint32_t size, const uint8_t *key, uint32_t keySize)
  1037. {
  1038. status_t retval;
  1039. if (!ltc_check_key_size(keySize))
  1040. {
  1041. return kStatus_InvalidArgument;
  1042. }
  1043. /* ECB mode, size must be 16-byte multiple */
  1044. if ((size < 16u) || (size % 16u))
  1045. {
  1046. return kStatus_InvalidArgument;
  1047. }
  1048. /* Initialize algorithm state. */
  1049. ltc_symmetric_update(base, key, keySize, kLTC_AlgorithmAES, kLTC_ModeECB, kLTC_ModeEncrypt);
  1050. /* Process data and return status. */
  1051. retval = ltc_process_message_in_sessions(base, &plaintext[0], size, &ciphertext[0]);
  1052. ltc_clear_all(base, false);
  1053. return retval;
  1054. }
  1055. status_t LTC_AES_DecryptEcb(LTC_Type *base,
  1056. const uint8_t *ciphertext,
  1057. uint8_t *plaintext,
  1058. uint32_t size,
  1059. const uint8_t *key,
  1060. uint32_t keySize,
  1061. ltc_aes_key_t keyType)
  1062. {
  1063. status_t status;
  1064. if (!ltc_check_key_size(keySize))
  1065. {
  1066. return kStatus_InvalidArgument;
  1067. }
  1068. /* ECB mode, size must be 16-byte multiple */
  1069. if ((size < 16u) || (size % 16u))
  1070. {
  1071. return kStatus_InvalidArgument;
  1072. }
  1073. status = ltc_aes_decrypt_ecb(base, ciphertext, plaintext, size, key, keySize, keyType);
  1074. ltc_clear_all(base, false);
  1075. return status;
  1076. }
  1077. status_t LTC_AES_EncryptCbc(LTC_Type *base,
  1078. const uint8_t *plaintext,
  1079. uint8_t *ciphertext,
  1080. uint32_t size,
  1081. const uint8_t iv[LTC_AES_IV_SIZE],
  1082. const uint8_t *key,
  1083. uint32_t keySize)
  1084. {
  1085. status_t retval;
  1086. if (!ltc_check_key_size(keySize))
  1087. {
  1088. return kStatus_InvalidArgument;
  1089. }
  1090. /* CBC mode, size must be 16-byte multiple */
  1091. if ((size < 16u) || (size % 16u))
  1092. {
  1093. return kStatus_InvalidArgument;
  1094. }
  1095. /* Initialize algorithm state. */
  1096. ltc_symmetric_update(base, key, keySize, kLTC_AlgorithmAES, kLTC_ModeCBC, kLTC_ModeEncrypt);
  1097. /* Write IV data to the context register. */
  1098. ltc_set_context(base, &iv[0], LTC_AES_IV_SIZE, 0);
  1099. /* Process data and return status. */
  1100. retval = ltc_process_message_in_sessions(base, &plaintext[0], size, &ciphertext[0]);
  1101. ltc_clear_all(base, false);
  1102. return retval;
  1103. }
  1104. status_t LTC_AES_DecryptCbc(LTC_Type *base,
  1105. const uint8_t *ciphertext,
  1106. uint8_t *plaintext,
  1107. uint32_t size,
  1108. const uint8_t iv[LTC_AES_IV_SIZE],
  1109. const uint8_t *key,
  1110. uint32_t keySize,
  1111. ltc_aes_key_t keyType)
  1112. {
  1113. status_t retval;
  1114. if (!ltc_check_key_size(keySize))
  1115. {
  1116. return kStatus_InvalidArgument;
  1117. }
  1118. /* CBC mode, size must be 16-byte multiple */
  1119. if ((size < 16u) || (size % 16u))
  1120. {
  1121. return kStatus_InvalidArgument;
  1122. }
  1123. /* set DK bit in the LTC Mode Register AAI field for directly loaded decrypt keys */
  1124. if (keyType == kLTC_DecryptKey)
  1125. {
  1126. base->MD |= (1U << kLTC_ModeRegBitShiftDK);
  1127. }
  1128. /* Initialize algorithm state. */
  1129. ltc_symmetric_update(base, key, keySize, kLTC_AlgorithmAES, kLTC_ModeCBC, kLTC_ModeDecrypt);
  1130. /* Write IV data to the context register. */
  1131. ltc_set_context(base, &iv[0], LTC_AES_IV_SIZE, 0);
  1132. /* Process data and return status. */
  1133. retval = ltc_process_message_in_sessions(base, &ciphertext[0], size, &plaintext[0]);
  1134. ltc_clear_all(base, false);
  1135. return retval;
  1136. }
  1137. status_t LTC_AES_CryptCtr(LTC_Type *base,
  1138. const uint8_t *input,
  1139. uint8_t *output,
  1140. uint32_t size,
  1141. uint8_t counter[LTC_AES_BLOCK_SIZE],
  1142. const uint8_t *key,
  1143. uint32_t keySize,
  1144. uint8_t counterlast[LTC_AES_BLOCK_SIZE],
  1145. uint32_t *szLeft)
  1146. {
  1147. status_t retval;
  1148. uint32_t lastSize;
  1149. if (!ltc_check_key_size(keySize))
  1150. {
  1151. return kStatus_InvalidArgument;
  1152. }
  1153. lastSize = 0U;
  1154. if (counterlast != NULL)
  1155. {
  1156. /* Split the size into full 16-byte chunks and last incomplete block due to LTC AES OFIFO errata */
  1157. if (size <= 16U)
  1158. {
  1159. lastSize = size;
  1160. size = 0U;
  1161. }
  1162. else
  1163. {
  1164. /* Process all 16-byte data chunks. */
  1165. lastSize = size % 16U;
  1166. if (lastSize == 0U)
  1167. {
  1168. lastSize = 16U;
  1169. size -= 16U;
  1170. }
  1171. else
  1172. {
  1173. size -= lastSize; /* size will be rounded down to 16 byte boundary. remaining bytes in lastSize */
  1174. }
  1175. }
  1176. }
  1177. /* Initialize algorithm state. */
  1178. ltc_symmetric_update(base, key, keySize, kLTC_AlgorithmAES, kLTC_ModeCTR, kLTC_ModeEncrypt);
  1179. /* Write initial counter data to the context register.
  1180. * NOTE the counter values start at 4-bytes offset into the context. */
  1181. ltc_set_context(base, &counter[0], 16U, 4U);
  1182. /* Process data and return status. */
  1183. retval = ltc_process_message_in_sessions(base, input, size, output);
  1184. if (kStatus_Success != retval)
  1185. {
  1186. return retval;
  1187. }
  1188. input += size;
  1189. output += size;
  1190. if ((counterlast != NULL) && lastSize)
  1191. {
  1192. uint8_t zeroes[16] = {0};
  1193. ltc_mode_t modeReg;
  1194. modeReg = (uint32_t)kLTC_AlgorithmAES | (uint32_t)kLTC_ModeCTR | (uint32_t)kLTC_ModeEncrypt;
  1195. /* Write the mode register to the hardware. */
  1196. base->MD = modeReg | (uint32_t)kLTC_ModeFinalize;
  1197. /* context is re-used (CTRi) */
  1198. /* Process data and return status. */
  1199. retval = ltc_symmetric_process_data(base, input, lastSize, output);
  1200. if (kStatus_Success != retval)
  1201. {
  1202. return retval;
  1203. }
  1204. if (szLeft)
  1205. {
  1206. *szLeft = 16U - lastSize;
  1207. }
  1208. /* Initialize algorithm state. */
  1209. base->MD = modeReg | (uint32_t)kLTC_ModeUpdate;
  1210. /* context is re-used (CTRi) */
  1211. /* Process data and return status. */
  1212. retval = ltc_symmetric_process_data(base, zeroes, 16U, counterlast);
  1213. }
  1214. ltc_get_context(base, &counter[0], 16U, 4U);
  1215. ltc_clear_all(base, false);
  1216. return retval;
  1217. }
  1218. #if defined(FSL_FEATURE_LTC_HAS_GCM) && FSL_FEATURE_LTC_HAS_GCM
  1219. /*******************************************************************************
  1220. * GCM Code static
  1221. ******************************************************************************/
  1222. static status_t ltc_aes_gcm_check_input_args(LTC_Type *base,
  1223. const uint8_t *src,
  1224. const uint8_t *iv,
  1225. const uint8_t *aad,
  1226. const uint8_t *key,
  1227. uint8_t *dst,
  1228. uint32_t inputSize,
  1229. uint32_t ivSize,
  1230. uint32_t aadSize,
  1231. uint32_t keySize,
  1232. uint32_t tagSize)
  1233. {
  1234. if (!base)
  1235. {
  1236. return kStatus_InvalidArgument;
  1237. }
  1238. /* tag can be NULL to skip tag processing */
  1239. if ((!key) || (ivSize && (!iv)) || (aadSize && (!aad)) || (inputSize && ((!src) || (!dst))))
  1240. {
  1241. return kStatus_InvalidArgument;
  1242. }
  1243. /* octet length of tag (tagSize) must be element of 4,8,12,13,14,15,16 */
  1244. if (((tagSize > 16u) || (tagSize < 12u)) && (tagSize != 4u) && (tagSize != 8u))
  1245. {
  1246. return kStatus_InvalidArgument;
  1247. }
  1248. /* check if keySize is supported */
  1249. if (!ltc_check_key_size(keySize))
  1250. {
  1251. return kStatus_InvalidArgument;
  1252. }
  1253. /* no IV AAD DATA makes no sense */
  1254. if (0 == (inputSize + ivSize + aadSize))
  1255. {
  1256. return kStatus_InvalidArgument;
  1257. }
  1258. return kStatus_Success;
  1259. }
  1260. /*!
  1261. * @brief Process Wrapper for void (*pfunc)(LTC_Type*, uint32_t, bool). Sets IV Size register.
  1262. */
  1263. static void ivsize_next(LTC_Type *base, uint32_t ivSize, bool iv_only)
  1264. {
  1265. base->IVSZ = LTC_IVSZ_IL(iv_only) | ((ivSize)&LTC_DS_DS_MASK);
  1266. }
  1267. /*!
  1268. * @brief Process Wrapper for void (*pfunc)(LTC_Type*, uint32_t, bool). Sets AAD Size register.
  1269. */
  1270. static void aadsize_next(LTC_Type *base, uint32_t aadSize, bool aad_only)
  1271. {
  1272. base->AADSZ = LTC_AADSZ_AL(aad_only) | ((aadSize)&LTC_DS_DS_MASK);
  1273. }
  1274. /*!
  1275. * @brief Process IV or AAD string in multi-session.
  1276. *
  1277. * @param base LTC peripheral base address
  1278. * @param iv IV or AAD data
  1279. * @param ivSize Size in bytes of IV or AAD data
  1280. * @param modeReg LTC peripheral Mode register value
  1281. * @param iv_only IV only or AAD only flag
  1282. * @param type selects between IV or AAD
  1283. */
  1284. static status_t ltc_aes_gcm_process_iv_aad(
  1285. LTC_Type *base, const uint8_t *iv, uint32_t ivSize, ltc_mode_t modeReg, bool iv_only, int type, ltc_mode_t modeLast)
  1286. {
  1287. uint32_t sz;
  1288. status_t retval;
  1289. void (*next_size_func)(LTC_Type *ltcBase, uint32_t nextSize, bool authOnly);
  1290. if ((NULL == iv) || (ivSize == 0))
  1291. {
  1292. return kStatus_InvalidArgument;
  1293. }
  1294. sz = LTC_FIFO_SZ_MAX_DOWN_ALGN;
  1295. next_size_func = type == LTC_AES_GCM_TYPE_AAD ? aadsize_next : ivsize_next;
  1296. while (ivSize)
  1297. {
  1298. if (ivSize < sz)
  1299. {
  1300. modeReg &= ~LTC_MD_AS_MASK;
  1301. modeReg |= modeLast;
  1302. base->MD = modeReg;
  1303. next_size_func(base, ivSize, iv_only);
  1304. ltc_move_to_ififo(base, iv, ivSize);
  1305. ivSize = 0;
  1306. }
  1307. else
  1308. {
  1309. /* set algorithm state to UPDATE */
  1310. modeReg &= ~LTC_MD_AS_MASK;
  1311. modeReg |= kLTC_ModeUpdate;
  1312. base->MD = modeReg;
  1313. next_size_func(base, (uint16_t)sz, true);
  1314. ltc_move_to_ififo(base, iv, sz);
  1315. ivSize -= sz;
  1316. iv += sz;
  1317. }
  1318. retval = ltc_wait(base);
  1319. if (kStatus_Success != retval)
  1320. {
  1321. return retval;
  1322. }
  1323. } /* end while */
  1324. return kStatus_Success;
  1325. }
  1326. static status_t ltc_aes_gcm_process(LTC_Type *base,
  1327. ltc_mode_encrypt_t encryptMode,
  1328. const uint8_t *src,
  1329. uint32_t inputSize,
  1330. const uint8_t *iv,
  1331. uint32_t ivSize,
  1332. const uint8_t *aad,
  1333. uint32_t aadSize,
  1334. const uint8_t *key,
  1335. uint32_t keySize,
  1336. uint8_t *dst,
  1337. uint8_t *tag,
  1338. uint32_t tagSize)
  1339. {
  1340. status_t retval; /* return value */
  1341. uint32_t max_ltc_fifo_sz; /* maximum data size that we can put to LTC FIFO in one session. 12-bit limit. */
  1342. ltc_mode_t modeReg; /* read and write LTC mode register */
  1343. bool single_ses_proc_all; /* iv, aad and src data can be processed in one session */
  1344. bool iv_only;
  1345. bool aad_only;
  1346. retval = ltc_aes_gcm_check_input_args(base, src, iv, aad, key, dst, inputSize, ivSize, aadSize, keySize, tagSize);
  1347. /* API input validation */
  1348. if (kStatus_Success != retval)
  1349. {
  1350. return retval;
  1351. }
  1352. max_ltc_fifo_sz = LTC_DS_DS_MASK; /* 12-bit field limit */
  1353. /*
  1354. * Write value to LTC AADSIZE (rounded up to next 16 byte boundary)
  1355. * plus the write value to LTC IV (rounded up to next 16 byte boundary)
  1356. * plus the inputSize. If the result is less than max_ltc_fifo_sz
  1357. * then all can be processed in one session FINALIZE.
  1358. * Otherwise, we have to split into multiple session, going through UPDATE(s), INITIALIZE, UPDATE(s) and FINALIZE.
  1359. */
  1360. single_ses_proc_all =
  1361. (((aadSize + 15u) & 0xfffffff0u) + ((ivSize + 15u) & 0xfffffff0u) + inputSize) <= max_ltc_fifo_sz;
  1362. /* setup key, algorithm and set the alg.state */
  1363. if (single_ses_proc_all)
  1364. {
  1365. ltc_symmetric_final(base, key, keySize, kLTC_AlgorithmAES, kLTC_ModeGCM, encryptMode);
  1366. modeReg = base->MD;
  1367. iv_only = (aadSize == 0) && (inputSize == 0);
  1368. aad_only = (inputSize == 0);
  1369. /* DS_MASK here is not a bug. IV size field can be written with more than 4-bits,
  1370. * as the IVSZ write value, aligned to next 16 bytes boundary, is written also to the Data Size.
  1371. * For example, I can write 22 to IVSZ, 32 will be written to Data Size and IVSZ will have value 6, which is 22
  1372. * mod 16.
  1373. */
  1374. base->IVSZ = LTC_IVSZ_IL(iv_only) | ((ivSize)&LTC_DS_DS_MASK);
  1375. ltc_move_to_ififo(base, iv, ivSize);
  1376. if (iv_only && ivSize)
  1377. {
  1378. retval = ltc_wait(base);
  1379. if (kStatus_Success != retval)
  1380. {
  1381. return retval;
  1382. }
  1383. }
  1384. base->AADSZ = LTC_AADSZ_AL(aad_only) | ((aadSize)&LTC_DS_DS_MASK);
  1385. ltc_move_to_ififo(base, aad, aadSize);
  1386. if (aad_only && aadSize)
  1387. {
  1388. retval = ltc_wait(base);
  1389. if (kStatus_Success != retval)
  1390. {
  1391. return retval;
  1392. }
  1393. }
  1394. if (inputSize)
  1395. {
  1396. /* Workaround for the LTC Data Size register update errata TKT261180 */
  1397. while (16U < base->DS)
  1398. {
  1399. }
  1400. ltc_symmetric_process_data(base, &src[0], inputSize, &dst[0]);
  1401. }
  1402. }
  1403. else
  1404. {
  1405. ltc_symmetric_init(base, key, keySize, kLTC_AlgorithmAES, kLTC_ModeGCM, encryptMode);
  1406. modeReg = base->MD;
  1407. /* process IV */
  1408. if (ivSize)
  1409. {
  1410. /* last chunk of IV is always INITIALIZE (for GHASH to occur) */
  1411. retval = ltc_aes_gcm_process_iv_aad(base, iv, ivSize, modeReg, true, LTC_AES_GCM_TYPE_IV, kLTC_ModeInit);
  1412. if (kStatus_Success != retval)
  1413. {
  1414. return retval;
  1415. }
  1416. }
  1417. /* process AAD */
  1418. if (aadSize)
  1419. {
  1420. /* AS mode to process last chunk of AAD. it differs if we are in GMAC or GCM */
  1421. ltc_mode_t lastModeReg;
  1422. if (0 == inputSize)
  1423. {
  1424. /* if there is no DATA, set mode to compute final MAC. this is GMAC mode */
  1425. lastModeReg = kLTC_ModeInitFinal;
  1426. }
  1427. else
  1428. {
  1429. /* there are confidential DATA. so process last chunk of AAD in UPDATE mode */
  1430. lastModeReg = kLTC_ModeUpdate;
  1431. }
  1432. retval = ltc_aes_gcm_process_iv_aad(base, aad, aadSize, modeReg, true, LTC_AES_GCM_TYPE_AAD, lastModeReg);
  1433. if (kStatus_Success != retval)
  1434. {
  1435. return retval;
  1436. }
  1437. }
  1438. /* there are DATA. */
  1439. if (inputSize)
  1440. {
  1441. /* set algorithm state to UPDATE */
  1442. modeReg &= ~LTC_MD_AS_MASK;
  1443. modeReg |= kLTC_ModeUpdate;
  1444. base->MD = modeReg;
  1445. retval =
  1446. ltc_symmetric_process_data_multiple(base, &src[0], inputSize, &dst[0], modeReg, kLTC_ModeInitFinal);
  1447. }
  1448. }
  1449. if (kStatus_Success != retval)
  1450. {
  1451. return retval;
  1452. }
  1453. retval = ltc_aes_process_tag(base, tag, tagSize, modeReg, LTC_GCM_TAG_IDX);
  1454. return retval;
  1455. }
  1456. /*******************************************************************************
  1457. * GCM Code public
  1458. ******************************************************************************/
  1459. status_t LTC_AES_EncryptTagGcm(LTC_Type *base,
  1460. const uint8_t *plaintext,
  1461. uint8_t *ciphertext,
  1462. uint32_t size,
  1463. const uint8_t *iv,
  1464. uint32_t ivSize,
  1465. const uint8_t *aad,
  1466. uint32_t aadSize,
  1467. const uint8_t *key,
  1468. uint32_t keySize,
  1469. uint8_t *tag,
  1470. uint32_t tagSize)
  1471. {
  1472. status_t status;
  1473. status = ltc_aes_gcm_process(base, kLTC_ModeEncrypt, plaintext, size, iv, ivSize, aad, aadSize, key, keySize,
  1474. ciphertext, tag, tagSize);
  1475. ltc_clear_all(base, false);
  1476. return status;
  1477. }
  1478. status_t LTC_AES_DecryptTagGcm(LTC_Type *base,
  1479. const uint8_t *ciphertext,
  1480. uint8_t *plaintext,
  1481. uint32_t size,
  1482. const uint8_t *iv,
  1483. uint32_t ivSize,
  1484. const uint8_t *aad,
  1485. uint32_t aadSize,
  1486. const uint8_t *key,
  1487. uint32_t keySize,
  1488. const uint8_t *tag,
  1489. uint32_t tagSize)
  1490. {
  1491. uint8_t temp_tag[16] = {0}; /* max. octet length of Integrity Check Value ICV (tag) is 16 */
  1492. uint8_t *tag_ptr;
  1493. status_t status;
  1494. tag_ptr = NULL;
  1495. if (tag)
  1496. {
  1497. ltc_memcpy(temp_tag, tag, tagSize);
  1498. tag_ptr = &temp_tag[0];
  1499. }
  1500. status = ltc_aes_gcm_process(base, kLTC_ModeDecrypt, ciphertext, size, iv, ivSize, aad, aadSize, key, keySize,
  1501. plaintext, tag_ptr, tagSize);
  1502. ltc_clear_all(base, false);
  1503. return status;
  1504. }
  1505. #endif /* FSL_FEATURE_LTC_HAS_GCM */
  1506. /*******************************************************************************
  1507. * CCM Code static
  1508. ******************************************************************************/
  1509. static status_t ltc_aes_ccm_check_input_args(LTC_Type *base,
  1510. const uint8_t *src,
  1511. const uint8_t *iv,
  1512. const uint8_t *key,
  1513. uint8_t *dst,
  1514. uint32_t ivSize,
  1515. uint32_t aadSize,
  1516. uint32_t keySize,
  1517. uint32_t tagSize)
  1518. {
  1519. if (!base)
  1520. {
  1521. return kStatus_InvalidArgument;
  1522. }
  1523. /* tag can be NULL to skip tag processing */
  1524. if ((!src) || (!iv) || (!key) || (!dst))
  1525. {
  1526. return kStatus_InvalidArgument;
  1527. }
  1528. /* size of Nonce (ivSize) must be element of 7,8,9,10,11,12,13 */
  1529. if ((ivSize < 7u) || (ivSize > 13u))
  1530. {
  1531. return kStatus_InvalidArgument;
  1532. }
  1533. /* octet length of MAC (tagSize) must be element of 4,6,8,10,12,14,16 for tag processing or zero to skip tag
  1534. * processing */
  1535. if (((tagSize > 0) && (tagSize < 4u)) || (tagSize > 16u) || (tagSize & 1u))
  1536. {
  1537. return kStatus_InvalidArgument;
  1538. }
  1539. /* check if keySize is supported */
  1540. if (!ltc_check_key_size(keySize))
  1541. {
  1542. return kStatus_InvalidArgument;
  1543. }
  1544. /* LTC does not support more AAD than this */
  1545. if (aadSize >= 65280u)
  1546. {
  1547. return kStatus_InvalidArgument;
  1548. }
  1549. return kStatus_Success;
  1550. }
  1551. static uint32_t swap_bytes(uint32_t in)
  1552. {
  1553. return (((in & 0x000000ffu) << 24) | ((in & 0x0000ff00u) << 8) | ((in & 0x00ff0000u) >> 8) |
  1554. ((in & 0xff000000u) >> 24));
  1555. }
  1556. static void ltc_aes_ccm_context_init(
  1557. LTC_Type *base, uint32_t inputSize, const uint8_t *iv, uint32_t ivSize, uint32_t aadSize, uint32_t tagSize)
  1558. {
  1559. ltc_xcm_block_t blk;
  1560. ltc_xcm_block_t blkZero = {{0x0u, 0x0u, 0x0u, 0x0u}};
  1561. int q; /* octet length of binary representation of the octet length of the payload. computed as (15 - n), where n is
  1562. length of nonce(=ivSize) */
  1563. uint8_t flags; /* flags field in B0 and CTR0 */
  1564. /* compute B0 */
  1565. ltc_memcpy(&blk, &blkZero, sizeof(blk));
  1566. /* tagSize - size of output MAC */
  1567. q = 15 - ivSize;
  1568. flags = (uint8_t)(8 * ((tagSize - 2) / 2) + q - 1); /* 8*M' + L' */
  1569. if (aadSize)
  1570. {
  1571. flags |= 0x40; /* Adata */
  1572. }
  1573. blk.b[0] = flags; /* flags field */
  1574. blk.w[3] = swap_bytes(inputSize); /* message size, most significant byte first */
  1575. ltc_memcpy(&blk.b[1], iv, ivSize); /* nonce field */
  1576. /* Write B0 data to the context register.
  1577. */
  1578. ltc_set_context(base, &blk.b[0], 16, 0);
  1579. /* Write CTR0 to the context register.
  1580. */
  1581. ltc_memcpy(&blk, &blkZero, sizeof(blk)); /* ctr(0) field = zero */
  1582. blk.b[0] = q - 1; /* flags field */
  1583. ltc_memcpy(&blk.b[1], iv, ivSize); /* nonce field */
  1584. ltc_set_context(base, &blk.b[0], 16, 4);
  1585. }
  1586. static status_t ltc_aes_ccm_process_aad(
  1587. LTC_Type *base, uint32_t inputSize, const uint8_t *aad, uint32_t aadSize, ltc_mode_t *modeReg)
  1588. {
  1589. ltc_xcm_block_t blk = {{0x0u, 0x0u, 0x0u, 0x0u}};
  1590. uint32_t swapped; /* holds byte swap of uint32_t */
  1591. status_t retval;
  1592. if (aadSize)
  1593. {
  1594. bool aad_only;
  1595. bool aad_single_session;
  1596. uint32_t sz = 0;
  1597. aad_only = inputSize == 0u;
  1598. aad_single_session = (((aadSize + 2u) + 15u) & 0xfffffff0u) <= LTC_FIFO_SZ_MAX_DOWN_ALGN;
  1599. /* limit by CCM spec: 2^16 - 2^8 = 65280 */
  1600. /* encoding is two octets, msbyte first */
  1601. swapped = swap_bytes(aadSize);
  1602. ltc_memcpy(&blk.b[0], ((uint8_t *)&swapped) + sizeof(uint16_t), sizeof(uint16_t));
  1603. sz = aadSize > 14u ? 14u : aadSize; /* limit aad to the end of 16 bytes blk */
  1604. ltc_memcpy(&blk.b[2], aad, sz); /* fill B1 with aad */
  1605. if (aad_single_session)
  1606. {
  1607. base->AADSZ = LTC_AADSZ_AL(aad_only) | ((aadSize + 2U) & LTC_DS_DS_MASK);
  1608. /* move first AAD block (16 bytes block B1) to FIFO */
  1609. ltc_move_block_to_ififo(base, &blk, sizeof(blk));
  1610. }
  1611. else
  1612. {
  1613. base->AADSZ = LTC_AADSZ_AL(true) | (16U);
  1614. /* move first AAD block (16 bytes block B1) to FIFO */
  1615. ltc_move_block_to_ififo(base, &blk, sizeof(blk));
  1616. }
  1617. /* track consumed AAD. sz bytes have been moved to fifo. */
  1618. aadSize -= sz;
  1619. aad += sz;
  1620. if (aad_single_session)
  1621. {
  1622. /* move remaining AAD to FIFO, then return, to continue with MDATA */
  1623. ltc_move_to_ififo(base, aad, aadSize);
  1624. }
  1625. else if (aadSize == 0u)
  1626. {
  1627. retval = ltc_wait(base);
  1628. if (kStatus_Success != retval)
  1629. {
  1630. return retval;
  1631. }
  1632. }
  1633. else
  1634. {
  1635. while (aadSize)
  1636. {
  1637. retval = ltc_wait(base);
  1638. if (kStatus_Success != retval)
  1639. {
  1640. return retval;
  1641. }
  1642. *modeReg &= ~LTC_MD_AS_MASK;
  1643. *modeReg |= (uint32_t)kLTC_ModeUpdate;
  1644. base->MD = *modeReg;
  1645. sz = LTC_FIFO_SZ_MAX_DOWN_ALGN;
  1646. if (aadSize < sz)
  1647. {
  1648. base->AADSZ = LTC_AADSZ_AL(aad_only) | (aadSize & LTC_DS_DS_MASK);
  1649. ltc_move_to_ififo(base, aad, aadSize);
  1650. aadSize = 0;
  1651. }
  1652. else
  1653. {
  1654. base->AADSZ = LTC_AADSZ_AL(true) | (sz & LTC_DS_DS_MASK);
  1655. ltc_move_to_ififo(base, aad, sz);
  1656. aadSize -= sz;
  1657. aad += sz;
  1658. }
  1659. } /* end while */
  1660. } /* end else */
  1661. } /* end if */
  1662. return kStatus_Success;
  1663. }
  1664. static status_t ltc_aes_ccm_process(LTC_Type *base,
  1665. ltc_mode_encrypt_t encryptMode,
  1666. const uint8_t *src,
  1667. uint32_t inputSize,
  1668. const uint8_t *iv,
  1669. uint32_t ivSize,
  1670. const uint8_t *aad,
  1671. uint32_t aadSize,
  1672. const uint8_t *key,
  1673. uint32_t keySize,
  1674. uint8_t *dst,
  1675. uint8_t *tag,
  1676. uint32_t tagSize)
  1677. {
  1678. status_t retval; /* return value */
  1679. uint32_t max_ltc_fifo_sz; /* maximum data size that we can put to LTC FIFO in one session. 12-bit limit. */
  1680. ltc_mode_t modeReg; /* read and write LTC mode register */
  1681. bool single_ses_proc_all; /* aad and src data can be processed in one session */
  1682. retval = ltc_aes_ccm_check_input_args(base, src, iv, key, dst, ivSize, aadSize, keySize, tagSize);
  1683. /* API input validation */
  1684. if (kStatus_Success != retval)
  1685. {
  1686. return retval;
  1687. }
  1688. max_ltc_fifo_sz = LTC_DS_DS_MASK; /* 12-bit field limit */
  1689. /* Write value to LTC AADSIZE will be (aadSize+2) value.
  1690. * The value will be rounded up to next 16 byte boundary and added to Data Size register.
  1691. * We then add inputSize to Data Size register. If the resulting Data Size is less than max_ltc_fifo_sz
  1692. * then all can be processed in one session INITIALIZE/FINALIZE.
  1693. * Otherwise, we have to split into multiple session, going through INITIALIZE, UPDATE (if required) and FINALIZE.
  1694. */
  1695. single_ses_proc_all = ((((aadSize + 2) + 15u) & 0xfffffff0u) + inputSize) <= max_ltc_fifo_sz;
  1696. /* setup key, algorithm and set the alg.state to INITIALIZE */
  1697. if (single_ses_proc_all)
  1698. {
  1699. ltc_symmetric_init_final(base, key, keySize, kLTC_AlgorithmAES, kLTC_ModeCCM, encryptMode);
  1700. }
  1701. else
  1702. {
  1703. ltc_symmetric_init(base, key, keySize, kLTC_AlgorithmAES, kLTC_ModeCCM, encryptMode);
  1704. }
  1705. modeReg = base->MD;
  1706. /* Initialize LTC context for AES CCM: block B0 and initial counter CTR0 */
  1707. ltc_aes_ccm_context_init(base, inputSize, iv, ivSize, aadSize, tagSize);
  1708. /* Process additional authentication data, if there are any.
  1709. * Need to split the job into individual sessions of up to 4096 bytes, due to LTC IFIFO data size limit.
  1710. */
  1711. retval = ltc_aes_ccm_process_aad(base, inputSize, aad, aadSize, &modeReg);
  1712. if (kStatus_Success != retval)
  1713. {
  1714. return retval;
  1715. }
  1716. /* Workaround for the LTC Data Size register update errata TKT261180 */
  1717. if (inputSize)
  1718. {
  1719. while (16u < base->DS)
  1720. {
  1721. }
  1722. }
  1723. /* Process message */
  1724. if (single_ses_proc_all)
  1725. {
  1726. retval = ltc_symmetric_process_data(base, &src[0], inputSize, &dst[0]);
  1727. }
  1728. else
  1729. {
  1730. retval = ltc_symmetric_process_data_multiple(base, &src[0], inputSize, &dst[0], modeReg, kLTC_ModeFinalize);
  1731. }
  1732. if (kStatus_Success != retval)
  1733. {
  1734. return retval;
  1735. }
  1736. retval = ltc_aes_process_tag(base, tag, tagSize, modeReg, LTC_CCM_TAG_IDX);
  1737. return retval;
  1738. }
  1739. /*******************************************************************************
  1740. * CCM Code public
  1741. ******************************************************************************/
  1742. status_t LTC_AES_EncryptTagCcm(LTC_Type *base,
  1743. const uint8_t *plaintext,
  1744. uint8_t *ciphertext,
  1745. uint32_t size,
  1746. const uint8_t *iv,
  1747. uint32_t ivSize,
  1748. const uint8_t *aad,
  1749. uint32_t aadSize,
  1750. const uint8_t *key,
  1751. uint32_t keySize,
  1752. uint8_t *tag,
  1753. uint32_t tagSize)
  1754. {
  1755. status_t status;
  1756. status = ltc_aes_ccm_process(base, kLTC_ModeEncrypt, plaintext, size, iv, ivSize, aad, aadSize, key, keySize,
  1757. ciphertext, tag, tagSize);
  1758. ltc_clear_all(base, false);
  1759. return status;
  1760. }
  1761. status_t LTC_AES_DecryptTagCcm(LTC_Type *base,
  1762. const uint8_t *ciphertext,
  1763. uint8_t *plaintext,
  1764. uint32_t size,
  1765. const uint8_t *iv,
  1766. uint32_t ivSize,
  1767. const uint8_t *aad,
  1768. uint32_t aadSize,
  1769. const uint8_t *key,
  1770. uint32_t keySize,
  1771. const uint8_t *tag,
  1772. uint32_t tagSize)
  1773. {
  1774. uint8_t temp_tag[16] = {0}; /* max. octet length of MAC (tag) is 16 */
  1775. uint8_t *tag_ptr;
  1776. status_t status;
  1777. tag_ptr = NULL;
  1778. if (tag)
  1779. {
  1780. ltc_memcpy(temp_tag, tag, tagSize);
  1781. tag_ptr = &temp_tag[0];
  1782. }
  1783. status = ltc_aes_ccm_process(base, kLTC_ModeDecrypt, ciphertext, size, iv, ivSize, aad, aadSize, key, keySize,
  1784. plaintext, tag_ptr, tagSize);
  1785. ltc_clear_all(base, false);
  1786. return status;
  1787. }
  1788. #if defined(FSL_FEATURE_LTC_HAS_DES) && FSL_FEATURE_LTC_HAS_DES
  1789. /*******************************************************************************
  1790. * DES / 3DES Code static
  1791. ******************************************************************************/
  1792. static status_t ltc_des_process(LTC_Type *base,
  1793. const uint8_t *input,
  1794. uint8_t *output,
  1795. uint32_t size,
  1796. const uint8_t iv[LTC_DES_IV_SIZE],
  1797. const uint8_t key[LTC_DES_KEY_SIZE],
  1798. ltc_mode_symmetric_alg_t modeAs,
  1799. ltc_mode_encrypt_t modeEnc)
  1800. {
  1801. status_t retval;
  1802. /* all but OFB, size must be 8-byte multiple */
  1803. if ((modeAs != kLTC_ModeOFB) && ((size < 8u) || (size % 8u)))
  1804. {
  1805. return kStatus_InvalidArgument;
  1806. }
  1807. /* Initialize algorithm state. */
  1808. ltc_symmetric_update(base, &key[0], LTC_DES_KEY_SIZE, kLTC_AlgorithmDES, modeAs, modeEnc);
  1809. if ((modeAs != kLTC_ModeECB))
  1810. {
  1811. ltc_set_context(base, iv, LTC_DES_IV_SIZE, 0);
  1812. }
  1813. /* Process data and return status. */
  1814. retval = ltc_process_message_in_sessions(base, input, size, output);
  1815. ltc_clear_all(base, false);
  1816. return retval;
  1817. }
  1818. status_t ltc_3des_check_input_args(ltc_mode_symmetric_alg_t modeAs,
  1819. uint32_t size,
  1820. const uint8_t *key1,
  1821. const uint8_t *key2)
  1822. {
  1823. /* all but OFB, size must be 8-byte multiple */
  1824. if ((modeAs != kLTC_ModeOFB) && ((size < 8u) || (size % 8u)))
  1825. {
  1826. return kStatus_InvalidArgument;
  1827. }
  1828. if ((key1 == NULL) || (key2 == NULL))
  1829. {
  1830. return kStatus_InvalidArgument;
  1831. }
  1832. return kStatus_Success;
  1833. }
  1834. static status_t ltc_3des_process(LTC_Type *base,
  1835. const uint8_t *input,
  1836. uint8_t *output,
  1837. uint32_t size,
  1838. const uint8_t iv[LTC_DES_IV_SIZE],
  1839. const uint8_t key1[LTC_DES_KEY_SIZE],
  1840. const uint8_t key2[LTC_DES_KEY_SIZE],
  1841. const uint8_t key3[LTC_DES_KEY_SIZE],
  1842. ltc_mode_symmetric_alg_t modeAs,
  1843. ltc_mode_encrypt_t modeEnc)
  1844. {
  1845. status_t retval;
  1846. uint8_t key[LTC_DES_KEY_SIZE * 3];
  1847. uint8_t keySize = LTC_DES_KEY_SIZE * 2;
  1848. retval = ltc_3des_check_input_args(modeAs, size, key1, key2);
  1849. if (kStatus_Success != retval)
  1850. {
  1851. return retval;
  1852. }
  1853. ltc_memcpy(&key[0], &key1[0], LTC_DES_KEY_SIZE);
  1854. ltc_memcpy(&key[LTC_DES_KEY_SIZE], &key2[0], LTC_DES_KEY_SIZE);
  1855. if (key3)
  1856. {
  1857. ltc_memcpy(&key[LTC_DES_KEY_SIZE * 2], &key3[0], LTC_DES_KEY_SIZE);
  1858. keySize = sizeof(key);
  1859. }
  1860. /* Initialize algorithm state. */
  1861. ltc_symmetric_update(base, &key[0], keySize, kLTC_Algorithm3DES, modeAs, modeEnc);
  1862. if ((modeAs != kLTC_ModeECB))
  1863. {
  1864. ltc_set_context(base, iv, LTC_DES_IV_SIZE, 0);
  1865. }
  1866. /* Process data and return status. */
  1867. retval = ltc_process_message_in_sessions(base, input, size, output);
  1868. ltc_clear_all(base, false);
  1869. return retval;
  1870. }
  1871. /*******************************************************************************
  1872. * DES / 3DES Code public
  1873. ******************************************************************************/
  1874. status_t LTC_DES_EncryptEcb(
  1875. LTC_Type *base, const uint8_t *plaintext, uint8_t *ciphertext, uint32_t size, const uint8_t key[LTC_DES_KEY_SIZE])
  1876. {
  1877. return ltc_des_process(base, plaintext, ciphertext, size, NULL, key, kLTC_ModeECB, kLTC_ModeEncrypt);
  1878. }
  1879. status_t LTC_DES_DecryptEcb(
  1880. LTC_Type *base, const uint8_t *ciphertext, uint8_t *plaintext, uint32_t size, const uint8_t key[LTC_DES_KEY_SIZE])
  1881. {
  1882. return ltc_des_process(base, ciphertext, plaintext, size, NULL, key, kLTC_ModeECB, kLTC_ModeDecrypt);
  1883. }
  1884. status_t LTC_DES_EncryptCbc(LTC_Type *base,
  1885. const uint8_t *plaintext,
  1886. uint8_t *ciphertext,
  1887. uint32_t size,
  1888. const uint8_t iv[LTC_DES_IV_SIZE],
  1889. const uint8_t key[LTC_DES_KEY_SIZE])
  1890. {
  1891. return ltc_des_process(base, plaintext, ciphertext, size, iv, key, kLTC_ModeCBC, kLTC_ModeEncrypt);
  1892. }
  1893. status_t LTC_DES_DecryptCbc(LTC_Type *base,
  1894. const uint8_t *ciphertext,
  1895. uint8_t *plaintext,
  1896. uint32_t size,
  1897. const uint8_t iv[LTC_DES_IV_SIZE],
  1898. const uint8_t key[LTC_DES_KEY_SIZE])
  1899. {
  1900. return ltc_des_process(base, ciphertext, plaintext, size, iv, key, kLTC_ModeCBC, kLTC_ModeDecrypt);
  1901. }
  1902. status_t LTC_DES_EncryptCfb(LTC_Type *base,
  1903. const uint8_t *plaintext,
  1904. uint8_t *ciphertext,
  1905. uint32_t size,
  1906. const uint8_t iv[LTC_DES_IV_SIZE],
  1907. const uint8_t key[LTC_DES_KEY_SIZE])
  1908. {
  1909. return ltc_des_process(base, plaintext, ciphertext, size, iv, key, kLTC_ModeCFB, kLTC_ModeEncrypt);
  1910. }
  1911. status_t LTC_DES_DecryptCfb(LTC_Type *base,
  1912. const uint8_t *ciphertext,
  1913. uint8_t *plaintext,
  1914. uint32_t size,
  1915. const uint8_t iv[LTC_DES_IV_SIZE],
  1916. const uint8_t key[LTC_DES_KEY_SIZE])
  1917. {
  1918. return ltc_des_process(base, ciphertext, plaintext, size, iv, key, kLTC_ModeCFB, kLTC_ModeDecrypt);
  1919. }
  1920. status_t LTC_DES_EncryptOfb(LTC_Type *base,
  1921. const uint8_t *plaintext,
  1922. uint8_t *ciphertext,
  1923. uint32_t size,
  1924. const uint8_t iv[LTC_DES_IV_SIZE],
  1925. const uint8_t key[LTC_DES_KEY_SIZE])
  1926. {
  1927. return ltc_des_process(base, plaintext, ciphertext, size, iv, key, kLTC_ModeOFB, kLTC_ModeEncrypt);
  1928. }
  1929. status_t LTC_DES_DecryptOfb(LTC_Type *base,
  1930. const uint8_t *ciphertext,
  1931. uint8_t *plaintext,
  1932. uint32_t size,
  1933. const uint8_t iv[LTC_DES_IV_SIZE],
  1934. const uint8_t key[LTC_DES_KEY_SIZE])
  1935. {
  1936. return ltc_des_process(base, ciphertext, plaintext, size, iv, key, kLTC_ModeOFB, kLTC_ModeDecrypt);
  1937. }
  1938. status_t LTC_DES2_EncryptEcb(LTC_Type *base,
  1939. const uint8_t *plaintext,
  1940. uint8_t *ciphertext,
  1941. uint32_t size,
  1942. const uint8_t key1[LTC_DES_KEY_SIZE],
  1943. const uint8_t key2[LTC_DES_KEY_SIZE])
  1944. {
  1945. return ltc_3des_process(base, plaintext, ciphertext, size, NULL, key1, key2, NULL, kLTC_ModeECB, kLTC_ModeEncrypt);
  1946. }
  1947. status_t LTC_DES3_EncryptEcb(LTC_Type *base,
  1948. const uint8_t *plaintext,
  1949. uint8_t *ciphertext,
  1950. uint32_t size,
  1951. const uint8_t key1[LTC_DES_KEY_SIZE],
  1952. const uint8_t key2[LTC_DES_KEY_SIZE],
  1953. const uint8_t key3[LTC_DES_KEY_SIZE])
  1954. {
  1955. return ltc_3des_process(base, plaintext, ciphertext, size, NULL, key1, key2, key3, kLTC_ModeECB, kLTC_ModeEncrypt);
  1956. }
  1957. status_t LTC_DES2_DecryptEcb(LTC_Type *base,
  1958. const uint8_t *ciphertext,
  1959. uint8_t *plaintext,
  1960. uint32_t size,
  1961. const uint8_t key1[LTC_DES_KEY_SIZE],
  1962. const uint8_t key2[LTC_DES_KEY_SIZE])
  1963. {
  1964. return ltc_3des_process(base, ciphertext, plaintext, size, NULL, key1, key2, NULL, kLTC_ModeECB, kLTC_ModeDecrypt);
  1965. }
  1966. status_t LTC_DES3_DecryptEcb(LTC_Type *base,
  1967. const uint8_t *ciphertext,
  1968. uint8_t *plaintext,
  1969. uint32_t size,
  1970. const uint8_t key1[LTC_DES_KEY_SIZE],
  1971. const uint8_t key2[LTC_DES_KEY_SIZE],
  1972. const uint8_t key3[LTC_DES_KEY_SIZE])
  1973. {
  1974. return ltc_3des_process(base, ciphertext, plaintext, size, NULL, key1, key2, key3, kLTC_ModeECB, kLTC_ModeDecrypt);
  1975. }
  1976. status_t LTC_DES2_EncryptCbc(LTC_Type *base,
  1977. const uint8_t *plaintext,
  1978. uint8_t *ciphertext,
  1979. uint32_t size,
  1980. const uint8_t iv[LTC_DES_IV_SIZE],
  1981. const uint8_t key1[LTC_DES_KEY_SIZE],
  1982. const uint8_t key2[LTC_DES_KEY_SIZE])
  1983. {
  1984. return ltc_3des_process(base, plaintext, ciphertext, size, iv, key1, key2, NULL, kLTC_ModeCBC, kLTC_ModeEncrypt);
  1985. }
  1986. status_t LTC_DES3_EncryptCbc(LTC_Type *base,
  1987. const uint8_t *plaintext,
  1988. uint8_t *ciphertext,
  1989. uint32_t size,
  1990. const uint8_t iv[LTC_DES_IV_SIZE],
  1991. const uint8_t key1[LTC_DES_KEY_SIZE],
  1992. const uint8_t key2[LTC_DES_KEY_SIZE],
  1993. const uint8_t key3[LTC_DES_KEY_SIZE])
  1994. {
  1995. return ltc_3des_process(base, plaintext, ciphertext, size, iv, key1, key2, key3, kLTC_ModeCBC, kLTC_ModeEncrypt);
  1996. }
  1997. status_t LTC_DES2_DecryptCbc(LTC_Type *base,
  1998. const uint8_t *ciphertext,
  1999. uint8_t *plaintext,
  2000. uint32_t size,
  2001. const uint8_t iv[LTC_DES_IV_SIZE],
  2002. const uint8_t key1[LTC_DES_KEY_SIZE],
  2003. const uint8_t key2[LTC_DES_KEY_SIZE])
  2004. {
  2005. return ltc_3des_process(base, ciphertext, plaintext, size, iv, key1, key2, NULL, kLTC_ModeCBC, kLTC_ModeDecrypt);
  2006. }
  2007. status_t LTC_DES3_DecryptCbc(LTC_Type *base,
  2008. const uint8_t *ciphertext,
  2009. uint8_t *plaintext,
  2010. uint32_t size,
  2011. const uint8_t iv[LTC_DES_IV_SIZE],
  2012. const uint8_t key1[LTC_DES_KEY_SIZE],
  2013. const uint8_t key2[LTC_DES_KEY_SIZE],
  2014. const uint8_t key3[LTC_DES_KEY_SIZE])
  2015. {
  2016. return ltc_3des_process(base, ciphertext, plaintext, size, iv, key1, key2, key3, kLTC_ModeCBC, kLTC_ModeDecrypt);
  2017. }
  2018. status_t LTC_DES2_EncryptCfb(LTC_Type *base,
  2019. const uint8_t *plaintext,
  2020. uint8_t *ciphertext,
  2021. uint32_t size,
  2022. const uint8_t iv[LTC_DES_IV_SIZE],
  2023. const uint8_t key1[LTC_DES_KEY_SIZE],
  2024. const uint8_t key2[LTC_DES_KEY_SIZE])
  2025. {
  2026. return ltc_3des_process(base, plaintext, ciphertext, size, iv, key1, key2, NULL, kLTC_ModeCFB, kLTC_ModeEncrypt);
  2027. }
  2028. status_t LTC_DES3_EncryptCfb(LTC_Type *base,
  2029. const uint8_t *plaintext,
  2030. uint8_t *ciphertext,
  2031. uint32_t size,
  2032. const uint8_t iv[LTC_DES_IV_SIZE],
  2033. const uint8_t key1[LTC_DES_KEY_SIZE],
  2034. const uint8_t key2[LTC_DES_KEY_SIZE],
  2035. const uint8_t key3[LTC_DES_KEY_SIZE])
  2036. {
  2037. return ltc_3des_process(base, plaintext, ciphertext, size, iv, key1, key2, key3, kLTC_ModeCFB, kLTC_ModeEncrypt);
  2038. }
  2039. status_t LTC_DES2_DecryptCfb(LTC_Type *base,
  2040. const uint8_t *ciphertext,
  2041. uint8_t *plaintext,
  2042. uint32_t size,
  2043. const uint8_t iv[LTC_DES_IV_SIZE],
  2044. const uint8_t key1[LTC_DES_KEY_SIZE],
  2045. const uint8_t key2[LTC_DES_KEY_SIZE])
  2046. {
  2047. return ltc_3des_process(base, ciphertext, plaintext, size, iv, key1, key2, NULL, kLTC_ModeCFB, kLTC_ModeDecrypt);
  2048. }
  2049. status_t LTC_DES3_DecryptCfb(LTC_Type *base,
  2050. const uint8_t *ciphertext,
  2051. uint8_t *plaintext,
  2052. uint32_t size,
  2053. const uint8_t iv[LTC_DES_IV_SIZE],
  2054. const uint8_t key1[LTC_DES_KEY_SIZE],
  2055. const uint8_t key2[LTC_DES_KEY_SIZE],
  2056. const uint8_t key3[LTC_DES_KEY_SIZE])
  2057. {
  2058. return ltc_3des_process(base, ciphertext, plaintext, size, iv, key1, key2, key3, kLTC_ModeCFB, kLTC_ModeDecrypt);
  2059. }
  2060. status_t LTC_DES2_EncryptOfb(LTC_Type *base,
  2061. const uint8_t *plaintext,
  2062. uint8_t *ciphertext,
  2063. uint32_t size,
  2064. const uint8_t iv[LTC_DES_IV_SIZE],
  2065. const uint8_t key1[LTC_DES_KEY_SIZE],
  2066. const uint8_t key2[LTC_DES_KEY_SIZE])
  2067. {
  2068. return ltc_3des_process(base, plaintext, ciphertext, size, iv, key1, key2, NULL, kLTC_ModeOFB, kLTC_ModeEncrypt);
  2069. }
  2070. status_t LTC_DES3_EncryptOfb(LTC_Type *base,
  2071. const uint8_t *plaintext,
  2072. uint8_t *ciphertext,
  2073. uint32_t size,
  2074. const uint8_t iv[LTC_DES_IV_SIZE],
  2075. const uint8_t key1[LTC_DES_KEY_SIZE],
  2076. const uint8_t key2[LTC_DES_KEY_SIZE],
  2077. const uint8_t key3[LTC_DES_KEY_SIZE])
  2078. {
  2079. return ltc_3des_process(base, plaintext, ciphertext, size, iv, key1, key2, key3, kLTC_ModeOFB, kLTC_ModeEncrypt);
  2080. }
  2081. status_t LTC_DES2_DecryptOfb(LTC_Type *base,
  2082. const uint8_t *ciphertext,
  2083. uint8_t *plaintext,
  2084. uint32_t size,
  2085. const uint8_t iv[LTC_DES_IV_SIZE],
  2086. const uint8_t key1[LTC_DES_KEY_SIZE],
  2087. const uint8_t key2[LTC_DES_KEY_SIZE])
  2088. {
  2089. return ltc_3des_process(base, ciphertext, plaintext, size, iv, key1, key2, NULL, kLTC_ModeOFB, kLTC_ModeDecrypt);
  2090. }
  2091. status_t LTC_DES3_DecryptOfb(LTC_Type *base,
  2092. const uint8_t *ciphertext,
  2093. uint8_t *plaintext,
  2094. uint32_t size,
  2095. const uint8_t iv[LTC_DES_IV_SIZE],
  2096. const uint8_t key1[LTC_DES_KEY_SIZE],
  2097. const uint8_t key2[LTC_DES_KEY_SIZE],
  2098. const uint8_t key3[LTC_DES_KEY_SIZE])
  2099. {
  2100. return ltc_3des_process(base, ciphertext, plaintext, size, iv, key1, key2, key3, kLTC_ModeOFB, kLTC_ModeDecrypt);
  2101. }
  2102. #endif /* FSL_FEATURE_LTC_HAS_DES */
  2103. /*******************************************************************************
  2104. * HASH Definitions
  2105. ******************************************************************************/
  2106. #if defined(FSL_FEATURE_LTC_HAS_SHA) && FSL_FEATURE_LTC_HAS_SHA
  2107. #define LTC_SHA_BLOCK_SIZE 64 /*!< SHA-1, SHA-224 & SHA-256 block size */
  2108. #define LTC_HASH_BLOCK_SIZE LTC_SHA_BLOCK_SIZE /*!< LTC hash block size */
  2109. enum _ltc_sha_digest_len
  2110. {
  2111. kLTC_RunLenSha1 = 28u,
  2112. kLTC_OutLenSha1 = 20u,
  2113. kLTC_RunLenSha224 = 40u,
  2114. kLTC_OutLenSha224 = 28u,
  2115. kLTC_RunLenSha256 = 40u,
  2116. kLTC_OutLenSha256 = 32u,
  2117. };
  2118. #else
  2119. #define LTC_HASH_BLOCK_SIZE LTC_AES_BLOCK_SIZE /*!< LTC hash block size */
  2120. #endif /* FSL_FEATURE_LTC_HAS_SHA */
  2121. /*! Internal states of the HASH creation process */
  2122. typedef enum _ltc_hash_algo_state
  2123. {
  2124. kLTC_HashInit = 1u, /*!< Key in the HASH context is the input key. */
  2125. kLTC_HashUpdate, /*!< HASH context has algorithm specific context: MAC, K2 and K3 (XCBC-MAC), MAC and L (CMAC),
  2126. running digest (MDHA). Key in the HASH context is the derived key. */
  2127. } ltc_hash_algo_state_t;
  2128. /*! 16/64-byte block represented as byte array or 4/16 32-bit words */
  2129. typedef union _ltc_hash_block
  2130. {
  2131. uint32_t w[LTC_HASH_BLOCK_SIZE / 4]; /*!< array of 32-bit words */
  2132. uint8_t b[LTC_HASH_BLOCK_SIZE]; /*!< byte array */
  2133. } ltc_hash_block_t;
  2134. /*! Definitions of indexes into hash context array */
  2135. typedef enum _ltc_hash_ctx_indexes
  2136. {
  2137. kLTC_HashCtxKeyStartIdx = 12, /*!< context word array index where key is stored */
  2138. kLTC_HashCtxKeySize = 20, /*!< context word array index where key size is stored */
  2139. kLTC_HashCtxNumWords = 21, /*!< number of context array 32-bit words */
  2140. } ltc_hash_ctx_indexes;
  2141. typedef struct _ltc_hash_ctx_internal
  2142. {
  2143. ltc_hash_block_t blk; /*!< memory buffer. only full 64/16-byte blocks are written to LTC during hash updates */
  2144. uint32_t blksz; /*!< number of valid bytes in memory buffer */
  2145. LTC_Type *base; /*!< LTC peripheral base address */
  2146. ltc_hash_algo_t algo; /*!< selected algorithm from the set of supported algorithms in ltc_drv_hash_algo */
  2147. ltc_hash_algo_state_t state; /*!< finite machine state of the hash software process */
  2148. uint32_t word[kLTC_HashCtxNumWords]; /*!< LTC module context that needs to be saved/restored between LTC jobs */
  2149. } ltc_hash_ctx_internal_t;
  2150. /*******************************************************************************
  2151. * HASH Code static
  2152. ******************************************************************************/
  2153. static status_t ltc_hash_check_input_alg(ltc_hash_algo_t algo)
  2154. {
  2155. if ((algo != kLTC_XcbcMac) && (algo != kLTC_Cmac)
  2156. #if defined(FSL_FEATURE_LTC_HAS_SHA) && FSL_FEATURE_LTC_HAS_SHA
  2157. && (algo != kLTC_Sha1) && (algo != kLTC_Sha224) && (algo != kLTC_Sha256)
  2158. #endif /* FSL_FEATURE_LTC_HAS_SHA */
  2159. )
  2160. {
  2161. return kStatus_InvalidArgument;
  2162. }
  2163. return kStatus_Success;
  2164. }
  2165. static inline bool ltc_hash_alg_is_cmac(ltc_hash_algo_t algo)
  2166. {
  2167. return ((algo == kLTC_XcbcMac) || (algo == kLTC_Cmac));
  2168. }
  2169. #if defined(FSL_FEATURE_LTC_HAS_SHA) && FSL_FEATURE_LTC_HAS_SHA
  2170. static inline bool ltc_hash_alg_is_sha(ltc_hash_algo_t algo)
  2171. {
  2172. return ((algo == kLTC_Sha1) || (algo == kLTC_Sha224) || (algo == kLTC_Sha256));
  2173. }
  2174. #endif /* FSL_FEATURE_LTC_HAS_SHA */
  2175. static status_t ltc_hash_check_input_args(
  2176. LTC_Type *base, ltc_hash_ctx_t *ctx, ltc_hash_algo_t algo, const uint8_t *key, uint32_t keySize)
  2177. {
  2178. /* Check validity of input algorithm */
  2179. if (kStatus_Success != ltc_hash_check_input_alg(algo))
  2180. {
  2181. return kStatus_InvalidArgument;
  2182. }
  2183. if ((NULL == ctx) || (NULL == base))
  2184. {
  2185. return kStatus_InvalidArgument;
  2186. }
  2187. if (ltc_hash_alg_is_cmac(algo))
  2188. {
  2189. if ((NULL == key) || (!ltc_check_key_size(keySize)))
  2190. {
  2191. return kStatus_InvalidArgument;
  2192. }
  2193. }
  2194. return kStatus_Success;
  2195. }
  2196. static status_t ltc_hash_check_context(ltc_hash_ctx_internal_t *ctxInternal, const uint8_t *data)
  2197. {
  2198. if ((NULL == data) || (NULL == ctxInternal) || (NULL == ctxInternal->base) ||
  2199. (kStatus_Success != ltc_hash_check_input_alg(ctxInternal->algo)))
  2200. {
  2201. return kStatus_InvalidArgument;
  2202. }
  2203. return kStatus_Success;
  2204. }
  2205. static uint32_t ltc_hash_algo2mode(ltc_hash_algo_t algo, ltc_mode_algorithm_state_t asMode, uint32_t *algOutSize)
  2206. {
  2207. uint32_t modeReg = 0u;
  2208. uint32_t outSize = 0u;
  2209. /* Set LTC algorithm */
  2210. switch (algo)
  2211. {
  2212. case kLTC_XcbcMac:
  2213. modeReg = (uint32_t)kLTC_AlgorithmAES | (uint32_t)kLTC_ModeXCBCMAC;
  2214. outSize = 16u;
  2215. break;
  2216. case kLTC_Cmac:
  2217. modeReg = (uint32_t)kLTC_AlgorithmAES | (uint32_t)kLTC_ModeCMAC;
  2218. outSize = 16u;
  2219. break;
  2220. #if defined(FSL_FEATURE_LTC_HAS_SHA) && FSL_FEATURE_LTC_HAS_SHA
  2221. case kLTC_Sha1:
  2222. modeReg = (uint32_t)kLTC_AlgorithmSHA1;
  2223. outSize = kLTC_OutLenSha1;
  2224. break;
  2225. case kLTC_Sha224:
  2226. modeReg = (uint32_t)kLTC_AlgorithmSHA224;
  2227. outSize = kLTC_OutLenSha224;
  2228. break;
  2229. case kLTC_Sha256:
  2230. modeReg = (uint32_t)kLTC_AlgorithmSHA256;
  2231. outSize = kLTC_OutLenSha256;
  2232. break;
  2233. #endif /* FSL_FEATURE_LTC_HAS_SHA */
  2234. default:
  2235. break;
  2236. }
  2237. modeReg |= (uint32_t)asMode;
  2238. if (algOutSize)
  2239. {
  2240. *algOutSize = outSize;
  2241. }
  2242. return modeReg;
  2243. }
  2244. static void ltc_hash_engine_init(ltc_hash_ctx_internal_t *ctx)
  2245. {
  2246. uint8_t *key;
  2247. uint32_t keySize;
  2248. LTC_Type *base;
  2249. ltc_mode_symmetric_alg_t algo;
  2250. base = ctx->base;
  2251. #if defined(FSL_FEATURE_LTC_HAS_SHA) && FSL_FEATURE_LTC_HAS_SHA
  2252. if (ltc_hash_alg_is_cmac(ctx->algo))
  2253. {
  2254. #endif /* FSL_FEATURE_LTC_HAS_SHA */
  2255. /*
  2256. * word[kLtcCmacCtxKeySize] = key_length
  2257. * word[1-8] = key
  2258. */
  2259. keySize = ctx->word[kLTC_HashCtxKeySize];
  2260. key = (uint8_t *)&ctx->word[kLTC_HashCtxKeyStartIdx];
  2261. /* set LTC mode register to INITIALIZE */
  2262. algo = (ctx->algo == kLTC_XcbcMac) ? kLTC_ModeXCBCMAC : kLTC_ModeCMAC;
  2263. ltc_symmetric_init(base, key, keySize, kLTC_AlgorithmAES, algo, kLTC_ModeEncrypt);
  2264. #if defined(FSL_FEATURE_LTC_HAS_SHA) && FSL_FEATURE_LTC_HAS_SHA
  2265. }
  2266. else if (ltc_hash_alg_is_sha(ctx->algo))
  2267. {
  2268. /* Clear internal register states. */
  2269. base->CW = (uint32_t)kLTC_ClearAll;
  2270. /* Set byte swap on for several registers we will be reading and writing
  2271. * user data to/from. */
  2272. base->CTL |= kLTC_CtrlSwapAll;
  2273. }
  2274. else
  2275. {
  2276. /* do nothing in this case */
  2277. }
  2278. #endif /* FSL_FEATURE_LTC_HAS_SHA */
  2279. }
  2280. static void ltc_hash_save_context(ltc_hash_ctx_internal_t *ctx)
  2281. {
  2282. uint32_t sz;
  2283. LTC_Type *base;
  2284. base = ctx->base;
  2285. /* Get context size */
  2286. switch (ctx->algo)
  2287. {
  2288. case kLTC_XcbcMac:
  2289. /*
  2290. * word[0-3] = mac
  2291. * word[3-7] = k3
  2292. * word[8-11] = k2
  2293. * word[kLtcCmacCtxKeySize] = keySize
  2294. */
  2295. sz = 12 * sizeof(uint32_t);
  2296. break;
  2297. case kLTC_Cmac:
  2298. /*
  2299. * word[0-3] = mac
  2300. * word[3-7] = L */
  2301. sz = 8 * sizeof(uint32_t);
  2302. break;
  2303. #if defined(FSL_FEATURE_LTC_HAS_SHA) && FSL_FEATURE_LTC_HAS_SHA
  2304. case kLTC_Sha1:
  2305. sz = (kLTC_RunLenSha1);
  2306. break;
  2307. case kLTC_Sha224:
  2308. sz = (kLTC_RunLenSha224);
  2309. break;
  2310. case kLTC_Sha256:
  2311. sz = (kLTC_RunLenSha256);
  2312. break;
  2313. #endif /* FSL_FEATURE_LTC_HAS_SHA */
  2314. default:
  2315. sz = 0;
  2316. break;
  2317. }
  2318. ltc_get_context(base, (uint8_t *)&ctx->word[0], sz, 0);
  2319. if (true == ltc_hash_alg_is_cmac(ctx->algo))
  2320. {
  2321. /* word[12-19] = key */
  2322. ltc_get_key(base, (uint8_t *)&ctx->word[kLTC_HashCtxKeyStartIdx], ctx->word[kLTC_HashCtxKeySize]);
  2323. }
  2324. }
  2325. static void ltc_hash_restore_context(ltc_hash_ctx_internal_t *ctx)
  2326. {
  2327. uint32_t sz;
  2328. uint32_t keySize;
  2329. LTC_Type *base;
  2330. base = ctx->base;
  2331. /* Get context size */
  2332. switch (ctx->algo)
  2333. {
  2334. case kLTC_XcbcMac:
  2335. /*
  2336. * word[0-3] = mac
  2337. * word[3-7] = k3
  2338. * word[8-11] = k2
  2339. * word[kLtcCmacCtxKeySize] = keySize
  2340. */
  2341. sz = 12 * sizeof(uint32_t);
  2342. break;
  2343. case kLTC_Cmac:
  2344. /*
  2345. * word[0-3] = mac
  2346. * word[3-7] = L */
  2347. sz = 8 * sizeof(uint32_t);
  2348. break;
  2349. #if defined(FSL_FEATURE_LTC_HAS_SHA) && FSL_FEATURE_LTC_HAS_SHA
  2350. case kLTC_Sha1:
  2351. sz = (kLTC_RunLenSha1);
  2352. break;
  2353. case kLTC_Sha224:
  2354. sz = (kLTC_RunLenSha224);
  2355. break;
  2356. case kLTC_Sha256:
  2357. sz = (kLTC_RunLenSha256);
  2358. break;
  2359. #endif /* FSL_FEATURE_LTC_HAS_SHA */
  2360. default:
  2361. sz = 0;
  2362. break;
  2363. }
  2364. ltc_set_context(base, (const uint8_t *)&ctx->word[0], sz, 0);
  2365. if (ltc_hash_alg_is_cmac(ctx->algo))
  2366. {
  2367. /*
  2368. * word[12-19] = key
  2369. * word[kLtcCmacCtxKeySize] = keySize
  2370. */
  2371. base->CW = kLTC_ClearKey; /* clear Key and Key Size registers */
  2372. keySize = ctx->word[kLTC_HashCtxKeySize];
  2373. /* Write the key in place. */
  2374. ltc_set_key(base, (const uint8_t *)&ctx->word[kLTC_HashCtxKeyStartIdx], keySize);
  2375. /* Write the key size. This must be done after writing the key, and this
  2376. * action locks the ability to modify the key registers. */
  2377. base->KS = keySize;
  2378. }
  2379. }
  2380. static void ltc_hash_prepare_context_switch(LTC_Type *base)
  2381. {
  2382. base->CW = (uint32_t)kLTC_ClearDataSize | (uint32_t)kLTC_ClearMode;
  2383. base->STA = kLTC_StatusDoneIsr;
  2384. }
  2385. static uint32_t ltc_hash_get_block_size(ltc_hash_algo_t algo)
  2386. {
  2387. if ((algo == kLTC_XcbcMac) || (algo == kLTC_Cmac))
  2388. {
  2389. return (uint32_t)LTC_AES_BLOCK_SIZE;
  2390. }
  2391. #if defined(FSL_FEATURE_LTC_HAS_SHA) && FSL_FEATURE_LTC_HAS_SHA
  2392. else if ((algo == kLTC_Sha1) || (algo == kLTC_Sha224) || (algo == kLTC_Sha256))
  2393. {
  2394. return (uint32_t)LTC_SHA_BLOCK_SIZE;
  2395. }
  2396. else
  2397. {
  2398. return 0;
  2399. }
  2400. #else
  2401. return 0;
  2402. #endif
  2403. }
  2404. static void ltc_hash_block_to_ififo(LTC_Type *base, const ltc_hash_block_t *blk, uint32_t numBytes, uint32_t blockSize)
  2405. {
  2406. uint32_t i = 0;
  2407. uint32_t words;
  2408. words = numBytes / 4u;
  2409. if (numBytes % 4u)
  2410. {
  2411. words++;
  2412. }
  2413. if (words > blockSize / 4u)
  2414. {
  2415. words = blockSize / 4u;
  2416. }
  2417. while (i < words)
  2418. {
  2419. if (0U == (base->FIFOSTA & LTC_FIFOSTA_IFF_MASK))
  2420. {
  2421. /* Copy data to the input FIFO. */
  2422. base->IFIFO = blk->w[i++];
  2423. }
  2424. }
  2425. }
  2426. static void ltc_hash_move_to_ififo(ltc_hash_ctx_internal_t *ctx,
  2427. const uint8_t *data,
  2428. uint32_t dataSize,
  2429. uint32_t blockSize)
  2430. {
  2431. ltc_hash_block_t blkZero;
  2432. uint32_t i;
  2433. for (i = 0; i < ARRAY_SIZE(blkZero.w); i++)
  2434. {
  2435. blkZero.w[i] = 0;
  2436. }
  2437. while (dataSize)
  2438. {
  2439. if (dataSize >= blockSize)
  2440. {
  2441. ltc_memcpy(&ctx->blk, data, blockSize);
  2442. ltc_hash_block_to_ififo(ctx->base, &ctx->blk, blockSize, blockSize);
  2443. dataSize -= blockSize;
  2444. data += blockSize;
  2445. }
  2446. else
  2447. {
  2448. /* last incomplete 16/64-bytes block of this message chunk */
  2449. ltc_memcpy(&ctx->blk, &blkZero, sizeof(ctx->blk));
  2450. ltc_memcpy(&ctx->blk, data, dataSize);
  2451. ctx->blksz = dataSize;
  2452. dataSize = 0;
  2453. }
  2454. }
  2455. }
  2456. static status_t ltc_hash_merge_and_flush_buf(ltc_hash_ctx_internal_t *ctx,
  2457. const uint8_t *input,
  2458. uint32_t inputSize,
  2459. ltc_mode_t modeReg,
  2460. uint32_t blockSize,
  2461. uint32_t *consumedSize)
  2462. {
  2463. uint32_t sz;
  2464. LTC_Type *base;
  2465. status_t status = kStatus_Success;
  2466. base = ctx->base;
  2467. sz = 0;
  2468. if (ctx->blksz)
  2469. {
  2470. sz = blockSize - ctx->blksz;
  2471. if (sz > inputSize)
  2472. {
  2473. sz = inputSize;
  2474. }
  2475. ltc_memcpy(ctx->blk.b + ctx->blksz, input, sz);
  2476. input += sz;
  2477. inputSize -= sz;
  2478. ctx->blksz += sz;
  2479. if (ctx->blksz == blockSize)
  2480. {
  2481. base->DS = blockSize;
  2482. ltc_hash_block_to_ififo(base, &ctx->blk, blockSize, blockSize);
  2483. ctx->blksz = 0;
  2484. status = ltc_wait(base);
  2485. if (kStatus_Success != status)
  2486. {
  2487. return status;
  2488. }
  2489. /* if there is still inputSize left, make sure LTC alg.state is set to UPDATE and continue */
  2490. if (inputSize)
  2491. {
  2492. /* set algorithm state to UPDATE */
  2493. modeReg &= ~LTC_MD_AS_MASK;
  2494. modeReg |= kLTC_ModeUpdate;
  2495. base->MD = modeReg;
  2496. }
  2497. }
  2498. }
  2499. if (consumedSize)
  2500. {
  2501. *consumedSize = sz;
  2502. }
  2503. return status;
  2504. }
  2505. static status_t ltc_hash_move_rest_to_context(
  2506. ltc_hash_ctx_internal_t *ctx, const uint8_t *data, uint32_t dataSize, ltc_mode_t modeReg, uint32_t blockSize)
  2507. {
  2508. status_t status = kStatus_Success;
  2509. ltc_hash_block_t blkZero;
  2510. uint32_t i;
  2511. /* make blkZero clear */
  2512. for (i = 0; i < ARRAY_SIZE(blkZero.w); i++)
  2513. {
  2514. blkZero.w[i] = 0;
  2515. }
  2516. while (dataSize)
  2517. {
  2518. if (dataSize > blockSize)
  2519. {
  2520. dataSize -= blockSize;
  2521. data += blockSize;
  2522. }
  2523. else
  2524. {
  2525. if (dataSize + ctx->blksz > blockSize)
  2526. {
  2527. uint32_t sz = 0;
  2528. status = ltc_hash_merge_and_flush_buf(ctx, data, dataSize, modeReg, blockSize, &sz);
  2529. if (kStatus_Success != status)
  2530. {
  2531. return status;
  2532. }
  2533. data += sz;
  2534. dataSize -= sz;
  2535. }
  2536. /* last incomplete 16/64-bytes block of this message chunk */
  2537. ltc_memcpy(&ctx->blk, &blkZero, blockSize);
  2538. ltc_memcpy(&ctx->blk, data, dataSize);
  2539. ctx->blksz = dataSize;
  2540. dataSize = 0;
  2541. }
  2542. }
  2543. return status;
  2544. }
  2545. static status_t ltc_hash_process_input_data(ltc_hash_ctx_internal_t *ctx,
  2546. const uint8_t *input,
  2547. uint32_t inputSize,
  2548. ltc_mode_t modeReg)
  2549. {
  2550. uint32_t sz = 0;
  2551. LTC_Type *base;
  2552. uint32_t blockSize = 0;
  2553. status_t status = kStatus_Success;
  2554. blockSize = ltc_hash_get_block_size(ctx->algo);
  2555. if (blockSize == 0)
  2556. {
  2557. return kStatus_Fail;
  2558. }
  2559. base = ctx->base;
  2560. /* fill context struct blk and flush to LTC ififo in case it is full block */
  2561. status = ltc_hash_merge_and_flush_buf(ctx, input, inputSize, modeReg, blockSize, &sz);
  2562. if (kStatus_Success != status)
  2563. {
  2564. return status;
  2565. }
  2566. input += sz;
  2567. inputSize -= sz;
  2568. /* if there is still more than or equal to 64 bytes, move each 64 bytes through LTC */
  2569. sz = LTC_DS_DS_MASK + 1u - LTC_HASH_BLOCK_SIZE;
  2570. while (inputSize)
  2571. {
  2572. if (inputSize < sz)
  2573. {
  2574. uint32_t lastSize;
  2575. lastSize = inputSize % blockSize;
  2576. if (lastSize == 0)
  2577. {
  2578. lastSize = blockSize;
  2579. }
  2580. inputSize -= lastSize;
  2581. if (inputSize)
  2582. {
  2583. /* move all complete blocks to ififo. */
  2584. base->DS = inputSize;
  2585. ltc_hash_move_to_ififo(ctx, input, inputSize, blockSize);
  2586. status = ltc_wait(base);
  2587. if (kStatus_Success != status)
  2588. {
  2589. return status;
  2590. }
  2591. input += inputSize;
  2592. }
  2593. /* keep last (in)complete 16-bytes block in context struct. */
  2594. /* when 3rd argument of cmac_move_to_ififo() is <= 16 bytes, it only stores the data to context struct */
  2595. status = ltc_hash_move_rest_to_context(ctx, input, lastSize, modeReg, blockSize);
  2596. if (kStatus_Success != status)
  2597. {
  2598. return status;
  2599. }
  2600. inputSize = 0;
  2601. }
  2602. else
  2603. {
  2604. base->DS = sz;
  2605. ltc_hash_move_to_ififo(ctx, input, sz, blockSize);
  2606. inputSize -= sz;
  2607. input += sz;
  2608. status = ltc_wait(base);
  2609. if (kStatus_Success != status)
  2610. {
  2611. return status;
  2612. }
  2613. /* set algorithm state to UPDATE */
  2614. modeReg &= ~LTC_MD_AS_MASK;
  2615. modeReg |= kLTC_ModeUpdate;
  2616. base->MD = modeReg;
  2617. }
  2618. } /* end while */
  2619. return status;
  2620. }
  2621. /*******************************************************************************
  2622. * HASH Code public
  2623. ******************************************************************************/
  2624. status_t LTC_HASH_Init(LTC_Type *base, ltc_hash_ctx_t *ctx, ltc_hash_algo_t algo, const uint8_t *key, uint32_t keySize)
  2625. {
  2626. status_t ret;
  2627. ltc_hash_ctx_internal_t *ctxInternal;
  2628. uint32_t i;
  2629. ret = ltc_hash_check_input_args(base, ctx, algo, key, keySize);
  2630. if (ret != kStatus_Success)
  2631. {
  2632. return ret;
  2633. }
  2634. /* set algorithm in context struct for later use */
  2635. ctxInternal = (ltc_hash_ctx_internal_t *)ctx;
  2636. ctxInternal->algo = algo;
  2637. for (i = 0; i < kLTC_HashCtxNumWords; i++)
  2638. {
  2639. ctxInternal->word[i] = 0u;
  2640. }
  2641. /* Steps required only using AES engine */
  2642. if (ltc_hash_alg_is_cmac(algo))
  2643. {
  2644. /* store input key and key length in context struct for later use */
  2645. ctxInternal->word[kLTC_HashCtxKeySize] = keySize;
  2646. ltc_memcpy(&ctxInternal->word[kLTC_HashCtxKeyStartIdx], key, keySize);
  2647. }
  2648. ctxInternal->blksz = 0u;
  2649. for (i = 0; i < sizeof(ctxInternal->blk.w) / sizeof(ctxInternal->blk.w[0]); i++)
  2650. {
  2651. ctxInternal->blk.w[0] = 0u;
  2652. }
  2653. ctxInternal->state = kLTC_HashInit;
  2654. ctxInternal->base = base;
  2655. return kStatus_Success;
  2656. }
  2657. status_t LTC_HASH_Update(ltc_hash_ctx_t *ctx, const uint8_t *input, uint32_t inputSize)
  2658. {
  2659. bool isUpdateState;
  2660. ltc_mode_t modeReg = 0; /* read and write LTC mode register */
  2661. LTC_Type *base;
  2662. status_t status;
  2663. ltc_hash_ctx_internal_t *ctxInternal;
  2664. uint32_t blockSize;
  2665. ctxInternal = (ltc_hash_ctx_internal_t *)ctx;
  2666. status = ltc_hash_check_context(ctxInternal, input);
  2667. if (kStatus_Success != status)
  2668. {
  2669. return status;
  2670. }
  2671. base = ctxInternal->base;
  2672. blockSize = ltc_hash_get_block_size(ctxInternal->algo);
  2673. /* if we are still less than 64 bytes, keep only in context */
  2674. if ((ctxInternal->blksz + inputSize) <= blockSize)
  2675. {
  2676. ltc_memcpy((&ctxInternal->blk.b[0]) + ctxInternal->blksz, input, inputSize);
  2677. ctxInternal->blksz += inputSize;
  2678. return status;
  2679. }
  2680. else
  2681. {
  2682. isUpdateState = ctxInternal->state == kLTC_HashUpdate;
  2683. if (ctxInternal->state == kLTC_HashInit)
  2684. {
  2685. /* set LTC mode register to INITIALIZE job */
  2686. ltc_hash_engine_init(ctxInternal);
  2687. #if defined(FSL_FEATURE_LTC_HAS_SHA) && FSL_FEATURE_LTC_HAS_SHA
  2688. if (ltc_hash_alg_is_cmac(ctxInternal->algo))
  2689. {
  2690. #endif /* FSL_FEATURE_LTC_HAS_SHA */
  2691. ctxInternal->state = kLTC_HashUpdate;
  2692. isUpdateState = true;
  2693. base->DS = 0u;
  2694. status = ltc_wait(base);
  2695. #if defined(FSL_FEATURE_LTC_HAS_SHA) && FSL_FEATURE_LTC_HAS_SHA
  2696. }
  2697. else
  2698. {
  2699. /* Set the proper block and algorithm mode. */
  2700. modeReg = ltc_hash_algo2mode(ctxInternal->algo, kLTC_ModeInit, NULL);
  2701. base->MD = modeReg;
  2702. ctxInternal->state = kLTC_HashUpdate;
  2703. status = ltc_hash_process_input_data(ctxInternal, input, inputSize, modeReg);
  2704. ltc_hash_save_context(ctxInternal);
  2705. }
  2706. #endif /* FSL_FEATURE_LTC_HAS_SHA */
  2707. }
  2708. else if (isUpdateState)
  2709. {
  2710. /* restore LTC context from context struct */
  2711. ltc_hash_restore_context(ctxInternal);
  2712. }
  2713. else
  2714. {
  2715. /* nothing special at this place */
  2716. }
  2717. }
  2718. if (kStatus_Success != status)
  2719. {
  2720. return status;
  2721. }
  2722. if (isUpdateState)
  2723. {
  2724. /* set LTC mode register to UPDATE job */
  2725. ltc_hash_prepare_context_switch(base);
  2726. base->CW = kLTC_ClearDataSize;
  2727. modeReg = ltc_hash_algo2mode(ctxInternal->algo, kLTC_ModeUpdate, NULL);
  2728. base->MD = modeReg;
  2729. /* process input data and save LTC context to context structure */
  2730. status = ltc_hash_process_input_data(ctxInternal, input, inputSize, modeReg);
  2731. ltc_hash_save_context(ctxInternal);
  2732. }
  2733. ltc_clear_all(base, false);
  2734. return status;
  2735. }
  2736. status_t LTC_HASH_Finish(ltc_hash_ctx_t *ctx, uint8_t *output, uint32_t *outputSize)
  2737. {
  2738. ltc_mode_t modeReg; /* read and write LTC mode register */
  2739. LTC_Type *base;
  2740. uint32_t algOutSize = 0;
  2741. status_t status;
  2742. ltc_hash_ctx_internal_t *ctxInternal;
  2743. uint32_t *ctxW;
  2744. uint32_t i;
  2745. ctxInternal = (ltc_hash_ctx_internal_t *)ctx;
  2746. status = ltc_hash_check_context(ctxInternal, output);
  2747. if (kStatus_Success != status)
  2748. {
  2749. return status;
  2750. }
  2751. base = ctxInternal->base;
  2752. ltc_hash_prepare_context_switch(base);
  2753. base->CW = kLTC_ClearDataSize;
  2754. if (ctxInternal->state == kLTC_HashInit)
  2755. {
  2756. ltc_hash_engine_init(ctxInternal);
  2757. #if defined(FSL_FEATURE_LTC_HAS_SHA) && FSL_FEATURE_LTC_HAS_SHA
  2758. if (ltc_hash_alg_is_cmac(ctxInternal->algo))
  2759. {
  2760. #endif /* FSL_FEATURE_LTC_HAS_SHA */
  2761. base->DS = 0u;
  2762. status = ltc_wait(base);
  2763. if (kStatus_Success != status)
  2764. {
  2765. return status;
  2766. }
  2767. modeReg = ltc_hash_algo2mode(ctxInternal->algo, kLTC_ModeFinalize, &algOutSize);
  2768. #if defined(FSL_FEATURE_LTC_HAS_SHA) && FSL_FEATURE_LTC_HAS_SHA
  2769. }
  2770. else
  2771. {
  2772. modeReg = ltc_hash_algo2mode(ctxInternal->algo, kLTC_ModeInitFinal, &algOutSize);
  2773. }
  2774. #endif /* FSL_FEATURE_LTC_HAS_SHA */
  2775. base->MD = modeReg;
  2776. }
  2777. else
  2778. {
  2779. modeReg = ltc_hash_algo2mode(ctxInternal->algo, kLTC_ModeFinalize, &algOutSize);
  2780. base->MD = modeReg;
  2781. /* restore LTC context from context struct */
  2782. ltc_hash_restore_context(ctxInternal);
  2783. }
  2784. /* flush message last incomplete block, if there is any, or write zero to data size register. */
  2785. base->DS = ctxInternal->blksz;
  2786. ltc_hash_block_to_ififo(base, &ctxInternal->blk, ctxInternal->blksz, ltc_hash_get_block_size(ctxInternal->algo));
  2787. /* Wait for finish of the encryption */
  2788. status = ltc_wait(base);
  2789. if (outputSize)
  2790. {
  2791. if (algOutSize < *outputSize)
  2792. {
  2793. *outputSize = algOutSize;
  2794. }
  2795. else
  2796. {
  2797. algOutSize = *outputSize;
  2798. }
  2799. }
  2800. ltc_get_context(base, &output[0], algOutSize, 0u);
  2801. ctxW = (uint32_t *)ctx;
  2802. for (i = 0; i < LTC_HASH_CTX_SIZE; i++)
  2803. {
  2804. ctxW[i] = 0u;
  2805. }
  2806. ltc_clear_all(base, false);
  2807. return status;
  2808. }
  2809. status_t LTC_HASH(LTC_Type *base,
  2810. ltc_hash_algo_t algo,
  2811. const uint8_t *input,
  2812. uint32_t inputSize,
  2813. const uint8_t *key,
  2814. uint32_t keySize,
  2815. uint8_t *output,
  2816. uint32_t *outputSize)
  2817. {
  2818. status_t status;
  2819. ltc_hash_ctx_t ctx;
  2820. status = LTC_HASH_Init(base, &ctx, algo, key, keySize);
  2821. if (status != kStatus_Success)
  2822. {
  2823. return status;
  2824. }
  2825. status = LTC_HASH_Update(&ctx, input, inputSize);
  2826. if (status != kStatus_Success)
  2827. {
  2828. return status;
  2829. }
  2830. status = LTC_HASH_Finish(&ctx, output, outputSize);
  2831. return status;
  2832. }
  2833. /*******************************************************************************
  2834. * PKHA Code static
  2835. ******************************************************************************/
  2836. #if defined(FSL_FEATURE_LTC_HAS_PKHA) && FSL_FEATURE_LTC_HAS_PKHA
  2837. static status_t ltc_pkha_clear_regabne(LTC_Type *base, bool A, bool B, bool N, bool E)
  2838. {
  2839. ltc_mode_t mode;
  2840. /* Set the PKHA algorithm and the appropriate function. */
  2841. mode = (uint32_t)kLTC_AlgorithmPKHA | 1U;
  2842. /* Set ram area to clear. Clear all. */
  2843. if (A)
  2844. {
  2845. mode |= 1U << 19U;
  2846. }
  2847. if (B)
  2848. {
  2849. mode |= 1U << 18U;
  2850. }
  2851. if (N)
  2852. {
  2853. mode |= 1U << 16U;
  2854. }
  2855. if (E)
  2856. {
  2857. mode |= 1U << 17U;
  2858. }
  2859. /* Write the mode register to the hardware.
  2860. * NOTE: This will begin the operation. */
  2861. base->MDPK = mode;
  2862. /* Wait for 'done' */
  2863. return ltc_wait(base);
  2864. }
  2865. static void ltc_pkha_default_parms(ltc_pkha_mode_params_t *params)
  2866. {
  2867. params->func = (ltc_pkha_func_t)0;
  2868. params->arithType = kLTC_PKHA_IntegerArith;
  2869. params->montFormIn = kLTC_PKHA_NormalValue;
  2870. params->montFormOut = kLTC_PKHA_NormalValue;
  2871. params->srcReg = kLTC_PKHA_RegAll;
  2872. params->srcQuad = kLTC_PKHA_Quad0;
  2873. params->dstReg = kLTC_PKHA_RegAll;
  2874. params->dstQuad = kLTC_PKHA_Quad0;
  2875. params->equalTime = kLTC_PKHA_NoTimingEqualized;
  2876. params->r2modn = kLTC_PKHA_CalcR2;
  2877. }
  2878. static void ltc_pkha_write_word(LTC_Type *base, ltc_pkha_reg_area_t reg, uint8_t index, uint32_t data)
  2879. {
  2880. switch (reg)
  2881. {
  2882. case kLTC_PKHA_RegA:
  2883. base->PKA[index] = data;
  2884. break;
  2885. case kLTC_PKHA_RegB:
  2886. base->PKB[index] = data;
  2887. break;
  2888. case kLTC_PKHA_RegN:
  2889. base->PKN[index] = data;
  2890. break;
  2891. case kLTC_PKHA_RegE:
  2892. base->PKE[index] = data;
  2893. break;
  2894. default:
  2895. break;
  2896. }
  2897. }
  2898. static uint32_t ltc_pkha_read_word(LTC_Type *base, ltc_pkha_reg_area_t reg, uint8_t index)
  2899. {
  2900. uint32_t retval;
  2901. switch (reg)
  2902. {
  2903. case kLTC_PKHA_RegA:
  2904. retval = base->PKA[index];
  2905. break;
  2906. case kLTC_PKHA_RegB:
  2907. retval = base->PKB[index];
  2908. break;
  2909. case kLTC_PKHA_RegN:
  2910. retval = base->PKN[index];
  2911. break;
  2912. case kLTC_PKHA_RegE:
  2913. retval = base->PKE[index];
  2914. break;
  2915. default:
  2916. retval = 0;
  2917. break;
  2918. }
  2919. return retval;
  2920. }
  2921. static status_t ltc_pkha_write_reg(
  2922. LTC_Type *base, ltc_pkha_reg_area_t reg, uint8_t quad, const uint8_t *data, uint16_t dataSize)
  2923. {
  2924. /* Select the word-based start index for each quadrant of 64 bytes. */
  2925. uint8_t startIndex = (quad * 16u);
  2926. uint32_t outWord;
  2927. while (dataSize > 0)
  2928. {
  2929. if (dataSize >= sizeof(uint32_t))
  2930. {
  2931. ltc_pkha_write_word(base, reg, startIndex++, ltc_get_word_from_unaligned(data));
  2932. dataSize -= sizeof(uint32_t);
  2933. data += sizeof(uint32_t);
  2934. }
  2935. else /* (dataSize > 0) && (dataSize < 4) */
  2936. {
  2937. outWord = 0;
  2938. ltc_memcpy(&outWord, data, dataSize);
  2939. ltc_pkha_write_word(base, reg, startIndex, outWord);
  2940. dataSize = 0;
  2941. }
  2942. }
  2943. return kStatus_Success;
  2944. }
  2945. static void ltc_pkha_read_reg(LTC_Type *base, ltc_pkha_reg_area_t reg, uint8_t quad, uint8_t *data, uint16_t dataSize)
  2946. {
  2947. /* Select the word-based start index for each quadrant of 64 bytes. */
  2948. uint8_t startIndex = (quad * 16u);
  2949. uint16_t calcSize;
  2950. uint32_t word;
  2951. while (dataSize > 0)
  2952. {
  2953. word = ltc_pkha_read_word(base, reg, startIndex++);
  2954. calcSize = (dataSize >= sizeof(uint32_t)) ? sizeof(uint32_t) : dataSize;
  2955. ltc_memcpy(data, &word, calcSize);
  2956. data += calcSize;
  2957. dataSize -= calcSize;
  2958. }
  2959. }
  2960. static void ltc_pkha_init_data(LTC_Type *base,
  2961. const uint8_t *A,
  2962. uint16_t sizeA,
  2963. const uint8_t *B,
  2964. uint16_t sizeB,
  2965. const uint8_t *N,
  2966. uint16_t sizeN,
  2967. const uint8_t *E,
  2968. uint16_t sizeE)
  2969. {
  2970. uint32_t clearMask = kLTC_ClearMode; /* clear Mode Register */
  2971. /* Clear internal register states. */
  2972. if (sizeA)
  2973. {
  2974. clearMask |= kLTC_ClearPkhaSizeA;
  2975. }
  2976. if (sizeB)
  2977. {
  2978. clearMask |= kLTC_ClearPkhaSizeB;
  2979. }
  2980. if (sizeN)
  2981. {
  2982. clearMask |= kLTC_ClearPkhaSizeN;
  2983. }
  2984. if (sizeE)
  2985. {
  2986. clearMask |= kLTC_ClearPkhaSizeE;
  2987. }
  2988. base->CW = clearMask;
  2989. base->STA = kLTC_StatusDoneIsr;
  2990. ltc_pkha_clear_regabne(base, A, B, N, E);
  2991. /* Write register sizes. */
  2992. /* Write modulus (N) and A and B register arguments. */
  2993. if (sizeN)
  2994. {
  2995. base->PKNSZ = sizeN;
  2996. if (N)
  2997. {
  2998. ltc_pkha_write_reg(base, kLTC_PKHA_RegN, 0, N, sizeN);
  2999. }
  3000. }
  3001. if (sizeA)
  3002. {
  3003. base->PKASZ = sizeA;
  3004. if (A)
  3005. {
  3006. ltc_pkha_write_reg(base, kLTC_PKHA_RegA, 0, A, sizeA);
  3007. }
  3008. }
  3009. if (sizeB)
  3010. {
  3011. base->PKBSZ = sizeB;
  3012. if (B)
  3013. {
  3014. ltc_pkha_write_reg(base, kLTC_PKHA_RegB, 0, B, sizeB);
  3015. }
  3016. }
  3017. if (sizeE)
  3018. {
  3019. base->PKESZ = sizeE;
  3020. if (E)
  3021. {
  3022. ltc_pkha_write_reg(base, kLTC_PKHA_RegE, 0, E, sizeE);
  3023. }
  3024. }
  3025. }
  3026. static void ltc_pkha_mode_set_src_reg_copy(ltc_mode_t *outMode, ltc_pkha_reg_area_t reg)
  3027. {
  3028. int i = 0;
  3029. do
  3030. {
  3031. reg = (ltc_pkha_reg_area_t)(((uint32_t)reg) >> 1u);
  3032. i++;
  3033. } while (reg);
  3034. i = 4 - i;
  3035. /* Source register must not be E. */
  3036. if (i != 2)
  3037. {
  3038. *outMode |= ((uint32_t)i << 17u);
  3039. }
  3040. }
  3041. static void ltc_pkha_mode_set_dst_reg_copy(ltc_mode_t *outMode, ltc_pkha_reg_area_t reg)
  3042. {
  3043. int i = 0;
  3044. do
  3045. {
  3046. reg = (ltc_pkha_reg_area_t)(((uint32_t)reg) >> 1u);
  3047. i++;
  3048. } while (reg);
  3049. i = 4 - i;
  3050. *outMode |= ((uint32_t)i << 10u);
  3051. }
  3052. static void ltc_pkha_mode_set_src_seg_copy(ltc_mode_t *outMode, const ltc_pkha_quad_area_t quad)
  3053. {
  3054. *outMode |= ((uint32_t)quad << 8u);
  3055. }
  3056. static void ltc_pkha_mode_set_dst_seg_copy(ltc_mode_t *outMode, const ltc_pkha_quad_area_t quad)
  3057. {
  3058. *outMode |= ((uint32_t)quad << 6u);
  3059. }
  3060. /*!
  3061. * @brief Starts the PKHA operation.
  3062. *
  3063. * This function starts an operation configured by the params parameter.
  3064. *
  3065. * @param base LTC peripheral base address
  3066. * @param params Configuration structure containing all settings required for PKHA operation.
  3067. */
  3068. static status_t ltc_pkha_init_mode(LTC_Type *base, const ltc_pkha_mode_params_t *params)
  3069. {
  3070. ltc_mode_t modeReg;
  3071. status_t retval;
  3072. /* Set the PKHA algorithm and the appropriate function. */
  3073. modeReg = kLTC_AlgorithmPKHA;
  3074. modeReg |= (uint32_t)params->func;
  3075. if ((params->func == kLTC_PKHA_CopyMemSizeN) || (params->func == kLTC_PKHA_CopyMemSizeSrc))
  3076. {
  3077. /* Set source and destination registers and quads. */
  3078. ltc_pkha_mode_set_src_reg_copy(&modeReg, params->srcReg);
  3079. ltc_pkha_mode_set_dst_reg_copy(&modeReg, params->dstReg);
  3080. ltc_pkha_mode_set_src_seg_copy(&modeReg, params->srcQuad);
  3081. ltc_pkha_mode_set_dst_seg_copy(&modeReg, params->dstQuad);
  3082. }
  3083. else
  3084. {
  3085. /* Set the arithmetic type - integer or binary polynomial (F2m). */
  3086. modeReg |= ((uint32_t)params->arithType << 17u);
  3087. /* Set to use Montgomery form of inputs and/or outputs. */
  3088. modeReg |= ((uint32_t)params->montFormIn << 19u);
  3089. modeReg |= ((uint32_t)params->montFormOut << 18u);
  3090. /* Set to use pre-computed R2modN */
  3091. modeReg |= ((uint32_t)params->r2modn << 16u);
  3092. }
  3093. modeReg |= ((uint32_t)params->equalTime << 10u);
  3094. /* Write the mode register to the hardware.
  3095. * NOTE: This will begin the operation. */
  3096. base->MDPK = modeReg;
  3097. retval = ltc_wait(base);
  3098. return (retval);
  3099. }
  3100. static status_t ltc_pkha_modR2(
  3101. LTC_Type *base, const uint8_t *N, uint16_t sizeN, uint8_t *result, uint16_t *resultSize, ltc_pkha_f2m_t arithType)
  3102. {
  3103. status_t status;
  3104. ltc_pkha_mode_params_t params;
  3105. ltc_pkha_default_parms(&params);
  3106. params.func = kLTC_PKHA_ArithModR2;
  3107. params.arithType = arithType;
  3108. ltc_pkha_init_data(base, NULL, 0, NULL, 0, N, sizeN, NULL, 0);
  3109. status = ltc_pkha_init_mode(base, &params);
  3110. if (status == kStatus_Success)
  3111. {
  3112. /* Read the result and size from register B0. */
  3113. if (resultSize && result)
  3114. {
  3115. *resultSize = base->PKBSZ;
  3116. /* Read the data from the result register into place. */
  3117. ltc_pkha_read_reg(base, kLTC_PKHA_RegB, 0, result, *resultSize);
  3118. }
  3119. }
  3120. return status;
  3121. }
  3122. static status_t ltc_pkha_modmul(LTC_Type *base,
  3123. const uint8_t *A,
  3124. uint16_t sizeA,
  3125. const uint8_t *B,
  3126. uint16_t sizeB,
  3127. const uint8_t *N,
  3128. uint16_t sizeN,
  3129. uint8_t *result,
  3130. uint16_t *resultSize,
  3131. ltc_pkha_f2m_t arithType,
  3132. ltc_pkha_montgomery_form_t montIn,
  3133. ltc_pkha_montgomery_form_t montOut,
  3134. ltc_pkha_timing_t equalTime)
  3135. {
  3136. ltc_pkha_mode_params_t params;
  3137. status_t status;
  3138. if (arithType == kLTC_PKHA_IntegerArith)
  3139. {
  3140. if (LTC_PKHA_CompareBigNum(A, sizeA, N, sizeN) >= 0)
  3141. {
  3142. return (kStatus_InvalidArgument);
  3143. }
  3144. if (LTC_PKHA_CompareBigNum(B, sizeB, N, sizeN) >= 0)
  3145. {
  3146. return (kStatus_InvalidArgument);
  3147. }
  3148. }
  3149. ltc_pkha_default_parms(&params);
  3150. params.func = kLTC_PKHA_ArithModMul;
  3151. params.arithType = arithType;
  3152. params.montFormIn = montIn;
  3153. params.montFormOut = montOut;
  3154. params.equalTime = equalTime;
  3155. ltc_pkha_init_data(base, A, sizeA, B, sizeB, N, sizeN, NULL, 0);
  3156. status = ltc_pkha_init_mode(base, &params);
  3157. if (status == kStatus_Success)
  3158. {
  3159. /* Read the result and size from register B0. */
  3160. if (resultSize && result)
  3161. {
  3162. *resultSize = base->PKBSZ;
  3163. /* Read the data from the result register into place. */
  3164. ltc_pkha_read_reg(base, kLTC_PKHA_RegB, 0, result, *resultSize);
  3165. }
  3166. }
  3167. return status;
  3168. }
  3169. /*******************************************************************************
  3170. * PKHA Code public
  3171. ******************************************************************************/
  3172. int LTC_PKHA_CompareBigNum(const uint8_t *a, size_t sizeA, const uint8_t *b, size_t sizeB)
  3173. {
  3174. int retval = 0;
  3175. /* skip zero msbytes - integer a */
  3176. while ((sizeA) && (0u == a[sizeA - 1]))
  3177. {
  3178. sizeA--;
  3179. }
  3180. /* skip zero msbytes - integer b */
  3181. while ((sizeB) && (0u == b[sizeB - 1]))
  3182. {
  3183. sizeB--;
  3184. }
  3185. if (sizeA > sizeB)
  3186. {
  3187. retval = 1;
  3188. } /* int a has more non-zero bytes, thus it is bigger than b */
  3189. else if (sizeA < sizeB)
  3190. {
  3191. retval = -1;
  3192. } /* int b has more non-zero bytes, thus it is bigger than a */
  3193. else if (sizeA == 0)
  3194. {
  3195. retval = 0;
  3196. } /* sizeA = sizeB = 0 */
  3197. else
  3198. {
  3199. int n;
  3200. int i;
  3201. int val;
  3202. uint32_t equal;
  3203. n = sizeA - 1;
  3204. i = 0;
  3205. equal = 0;
  3206. while (n >= 0)
  3207. {
  3208. uint32_t chXor = a[i] ^ b[i];
  3209. equal |= chXor;
  3210. val = (int)chXor * (a[i] - b[i]);
  3211. if (val < 0)
  3212. {
  3213. *(volatile int*)&retval = -1;
  3214. }
  3215. if (val > 0)
  3216. {
  3217. *(volatile int*)&retval = 1;
  3218. }
  3219. if (val == 0)
  3220. {
  3221. *(volatile int*)&val = 1;
  3222. }
  3223. i++;
  3224. n--;
  3225. }
  3226. if (0 == equal)
  3227. {
  3228. retval = 0;
  3229. }
  3230. }
  3231. return (retval);
  3232. }
  3233. status_t LTC_PKHA_NormalToMontgomery(LTC_Type *base,
  3234. const uint8_t *N,
  3235. uint16_t sizeN,
  3236. uint8_t *A,
  3237. uint16_t *sizeA,
  3238. uint8_t *B,
  3239. uint16_t *sizeB,
  3240. uint8_t *R2,
  3241. uint16_t *sizeR2,
  3242. ltc_pkha_timing_t equalTime,
  3243. ltc_pkha_f2m_t arithType)
  3244. {
  3245. status_t status;
  3246. /* need to convert our Integer inputs into Montgomery format */
  3247. if (N && sizeN && R2 && sizeR2)
  3248. {
  3249. /* 1. R2 = MOD_R2(N) */
  3250. status = ltc_pkha_modR2(base, N, sizeN, R2, sizeR2, arithType);
  3251. if (status != kStatus_Success)
  3252. {
  3253. return status;
  3254. }
  3255. /* 2. A(Montgomery) = MOD_MUL_IM_OM(A, R2, N) */
  3256. if (A && sizeA)
  3257. {
  3258. status = ltc_pkha_modmul(base, A, *sizeA, R2, *sizeR2, N, sizeN, A, sizeA, arithType,
  3259. kLTC_PKHA_MontgomeryFormat, kLTC_PKHA_MontgomeryFormat, equalTime);
  3260. if (status != kStatus_Success)
  3261. {
  3262. return status;
  3263. }
  3264. }
  3265. /* 2. B(Montgomery) = MOD_MUL_IM_OM(B, R2, N) */
  3266. if (B && sizeB)
  3267. {
  3268. status = ltc_pkha_modmul(base, B, *sizeB, R2, *sizeR2, N, sizeN, B, sizeB, arithType,
  3269. kLTC_PKHA_MontgomeryFormat, kLTC_PKHA_MontgomeryFormat, equalTime);
  3270. if (status != kStatus_Success)
  3271. {
  3272. return status;
  3273. }
  3274. }
  3275. ltc_clear_all(base, true);
  3276. }
  3277. else
  3278. {
  3279. status = kStatus_InvalidArgument;
  3280. }
  3281. return status;
  3282. }
  3283. status_t LTC_PKHA_MontgomeryToNormal(LTC_Type *base,
  3284. const uint8_t *N,
  3285. uint16_t sizeN,
  3286. uint8_t *A,
  3287. uint16_t *sizeA,
  3288. uint8_t *B,
  3289. uint16_t *sizeB,
  3290. ltc_pkha_timing_t equalTime,
  3291. ltc_pkha_f2m_t arithType)
  3292. {
  3293. uint8_t one = 1;
  3294. status_t status = kStatus_InvalidArgument;
  3295. /* A = MOD_MUL_IM_OM(A(Montgomery), 1, N) */
  3296. if (A && sizeA)
  3297. {
  3298. status = ltc_pkha_modmul(base, A, *sizeA, &one, sizeof(one), N, sizeN, A, sizeA, arithType,
  3299. kLTC_PKHA_MontgomeryFormat, kLTC_PKHA_MontgomeryFormat, equalTime);
  3300. if (kStatus_Success != status)
  3301. {
  3302. return status;
  3303. }
  3304. }
  3305. /* B = MOD_MUL_IM_OM(B(Montgomery), 1, N) */
  3306. if (B && sizeB)
  3307. {
  3308. status = ltc_pkha_modmul(base, B, *sizeB, &one, sizeof(one), N, sizeN, B, sizeB, arithType,
  3309. kLTC_PKHA_MontgomeryFormat, kLTC_PKHA_MontgomeryFormat, equalTime);
  3310. if (kStatus_Success != status)
  3311. {
  3312. return status;
  3313. }
  3314. }
  3315. ltc_clear_all(base, true);
  3316. return status;
  3317. }
  3318. status_t LTC_PKHA_ModAdd(LTC_Type *base,
  3319. const uint8_t *A,
  3320. uint16_t sizeA,
  3321. const uint8_t *B,
  3322. uint16_t sizeB,
  3323. const uint8_t *N,
  3324. uint16_t sizeN,
  3325. uint8_t *result,
  3326. uint16_t *resultSize,
  3327. ltc_pkha_f2m_t arithType)
  3328. {
  3329. ltc_pkha_mode_params_t params;
  3330. status_t status;
  3331. if (arithType == kLTC_PKHA_IntegerArith)
  3332. {
  3333. if (LTC_PKHA_CompareBigNum(A, sizeA, N, sizeN) >= 0)
  3334. {
  3335. return (kStatus_InvalidArgument);
  3336. }
  3337. if (LTC_PKHA_CompareBigNum(B, sizeB, N, sizeN) >= 0)
  3338. {
  3339. return (kStatus_InvalidArgument);
  3340. }
  3341. }
  3342. ltc_pkha_default_parms(&params);
  3343. params.func = kLTC_PKHA_ArithModAdd;
  3344. params.arithType = arithType;
  3345. ltc_pkha_init_data(base, A, sizeA, B, sizeB, N, sizeN, NULL, 0);
  3346. status = ltc_pkha_init_mode(base, &params);
  3347. if (status == kStatus_Success)
  3348. {
  3349. /* Read the result and size from register B0. */
  3350. if (resultSize && result)
  3351. {
  3352. *resultSize = base->PKBSZ;
  3353. /* Read the data from the result register into place. */
  3354. ltc_pkha_read_reg(base, kLTC_PKHA_RegB, 0, result, *resultSize);
  3355. }
  3356. }
  3357. ltc_clear_all(base, true);
  3358. return status;
  3359. }
  3360. status_t LTC_PKHA_ModSub1(LTC_Type *base,
  3361. const uint8_t *A,
  3362. uint16_t sizeA,
  3363. const uint8_t *B,
  3364. uint16_t sizeB,
  3365. const uint8_t *N,
  3366. uint16_t sizeN,
  3367. uint8_t *result,
  3368. uint16_t *resultSize)
  3369. {
  3370. ltc_pkha_mode_params_t params;
  3371. status_t status;
  3372. if (LTC_PKHA_CompareBigNum(A, sizeA, N, sizeN) >= 0)
  3373. {
  3374. return (kStatus_InvalidArgument);
  3375. }
  3376. if (LTC_PKHA_CompareBigNum(B, sizeB, N, sizeN) >= 0)
  3377. {
  3378. return (kStatus_InvalidArgument);
  3379. }
  3380. ltc_pkha_default_parms(&params);
  3381. params.func = kLTC_PKHA_ArithModSub1;
  3382. ltc_pkha_init_data(base, A, sizeA, B, sizeB, N, sizeN, NULL, 0);
  3383. status = ltc_pkha_init_mode(base, &params);
  3384. if (status == kStatus_Success)
  3385. {
  3386. /* Read the result and size from register B0. */
  3387. if (resultSize && result)
  3388. {
  3389. *resultSize = base->PKBSZ;
  3390. /* Read the data from the result register into place. */
  3391. ltc_pkha_read_reg(base, kLTC_PKHA_RegB, 0, result, *resultSize);
  3392. }
  3393. }
  3394. ltc_clear_all(base, true);
  3395. return status;
  3396. }
  3397. status_t LTC_PKHA_ModSub2(LTC_Type *base,
  3398. const uint8_t *A,
  3399. uint16_t sizeA,
  3400. const uint8_t *B,
  3401. uint16_t sizeB,
  3402. const uint8_t *N,
  3403. uint16_t sizeN,
  3404. uint8_t *result,
  3405. uint16_t *resultSize)
  3406. {
  3407. ltc_pkha_mode_params_t params;
  3408. status_t status;
  3409. ltc_pkha_default_parms(&params);
  3410. params.func = kLTC_PKHA_ArithModSub2;
  3411. ltc_pkha_init_data(base, A, sizeA, B, sizeB, N, sizeN, NULL, 0);
  3412. status = ltc_pkha_init_mode(base, &params);
  3413. if (status == kStatus_Success)
  3414. {
  3415. /* Read the result and size from register B0. */
  3416. if (resultSize && result)
  3417. {
  3418. *resultSize = base->PKBSZ;
  3419. /* Read the data from the result register into place. */
  3420. ltc_pkha_read_reg(base, kLTC_PKHA_RegB, 0, result, *resultSize);
  3421. }
  3422. }
  3423. ltc_clear_all(base, true);
  3424. return status;
  3425. }
  3426. status_t LTC_PKHA_ModMul(LTC_Type *base,
  3427. const uint8_t *A,
  3428. uint16_t sizeA,
  3429. const uint8_t *B,
  3430. uint16_t sizeB,
  3431. const uint8_t *N,
  3432. uint16_t sizeN,
  3433. uint8_t *result,
  3434. uint16_t *resultSize,
  3435. ltc_pkha_f2m_t arithType,
  3436. ltc_pkha_montgomery_form_t montIn,
  3437. ltc_pkha_montgomery_form_t montOut,
  3438. ltc_pkha_timing_t equalTime)
  3439. {
  3440. status_t status;
  3441. status =
  3442. ltc_pkha_modmul(base, A, sizeA, B, sizeB, N, sizeN, result, resultSize, arithType, montIn, montOut, equalTime);
  3443. ltc_clear_all(base, true);
  3444. return status;
  3445. }
  3446. status_t LTC_PKHA_ModExp(LTC_Type *base,
  3447. const uint8_t *A,
  3448. uint16_t sizeA,
  3449. const uint8_t *N,
  3450. uint16_t sizeN,
  3451. const uint8_t *E,
  3452. uint16_t sizeE,
  3453. uint8_t *result,
  3454. uint16_t *resultSize,
  3455. ltc_pkha_f2m_t arithType,
  3456. ltc_pkha_montgomery_form_t montIn,
  3457. ltc_pkha_timing_t equalTime)
  3458. {
  3459. ltc_pkha_mode_params_t params;
  3460. status_t status;
  3461. if (arithType == kLTC_PKHA_IntegerArith)
  3462. {
  3463. if (LTC_PKHA_CompareBigNum(A, sizeA, N, sizeN) >= 0)
  3464. {
  3465. return (kStatus_InvalidArgument);
  3466. }
  3467. }
  3468. ltc_pkha_default_parms(&params);
  3469. params.func = kLTC_PKHA_ArithModExp;
  3470. params.arithType = arithType;
  3471. params.montFormIn = montIn;
  3472. params.equalTime = equalTime;
  3473. ltc_pkha_init_data(base, A, sizeA, NULL, 0, N, sizeN, E, sizeE);
  3474. status = ltc_pkha_init_mode(base, &params);
  3475. if (status == kStatus_Success)
  3476. {
  3477. /* Read the result and size from register B0. */
  3478. if (resultSize && result)
  3479. {
  3480. *resultSize = base->PKBSZ;
  3481. /* Read the data from the result register into place. */
  3482. ltc_pkha_read_reg(base, kLTC_PKHA_RegB, 0, result, *resultSize);
  3483. }
  3484. }
  3485. ltc_clear_all(base, true);
  3486. return status;
  3487. }
  3488. status_t LTC_PKHA_ModRed(LTC_Type *base,
  3489. const uint8_t *A,
  3490. uint16_t sizeA,
  3491. const uint8_t *N,
  3492. uint16_t sizeN,
  3493. uint8_t *result,
  3494. uint16_t *resultSize,
  3495. ltc_pkha_f2m_t arithType)
  3496. {
  3497. ltc_pkha_mode_params_t params;
  3498. status_t status;
  3499. ltc_pkha_default_parms(&params);
  3500. params.func = kLTC_PKHA_ArithModRed;
  3501. params.arithType = arithType;
  3502. ltc_pkha_init_data(base, A, sizeA, NULL, 0, N, sizeN, NULL, 0);
  3503. status = ltc_pkha_init_mode(base, &params);
  3504. if (status == kStatus_Success)
  3505. {
  3506. /* Read the result and size from register B0. */
  3507. if (resultSize && result)
  3508. {
  3509. *resultSize = base->PKBSZ;
  3510. /* Read the data from the result register into place. */
  3511. ltc_pkha_read_reg(base, kLTC_PKHA_RegB, 0, result, *resultSize);
  3512. }
  3513. }
  3514. ltc_clear_all(base, true);
  3515. return status;
  3516. }
  3517. status_t LTC_PKHA_ModInv(LTC_Type *base,
  3518. const uint8_t *A,
  3519. uint16_t sizeA,
  3520. const uint8_t *N,
  3521. uint16_t sizeN,
  3522. uint8_t *result,
  3523. uint16_t *resultSize,
  3524. ltc_pkha_f2m_t arithType)
  3525. {
  3526. ltc_pkha_mode_params_t params;
  3527. status_t status;
  3528. /* A must be less than N -> LTC_PKHA_CompareBigNum() must return -1 */
  3529. if (arithType == kLTC_PKHA_IntegerArith)
  3530. {
  3531. if (LTC_PKHA_CompareBigNum(A, sizeA, N, sizeN) >= 0)
  3532. {
  3533. return (kStatus_InvalidArgument);
  3534. }
  3535. }
  3536. ltc_pkha_default_parms(&params);
  3537. params.func = kLTC_PKHA_ArithModInv;
  3538. params.arithType = arithType;
  3539. ltc_pkha_init_data(base, A, sizeA, NULL, 0, N, sizeN, NULL, 0);
  3540. status = ltc_pkha_init_mode(base, &params);
  3541. if (status == kStatus_Success)
  3542. {
  3543. /* Read the result and size from register B0. */
  3544. if (resultSize && result)
  3545. {
  3546. *resultSize = base->PKBSZ;
  3547. /* Read the data from the result register into place. */
  3548. ltc_pkha_read_reg(base, kLTC_PKHA_RegB, 0, result, *resultSize);
  3549. }
  3550. }
  3551. ltc_clear_all(base, true);
  3552. return status;
  3553. }
  3554. status_t LTC_PKHA_ModR2(
  3555. LTC_Type *base, const uint8_t *N, uint16_t sizeN, uint8_t *result, uint16_t *resultSize, ltc_pkha_f2m_t arithType)
  3556. {
  3557. status_t status;
  3558. status = ltc_pkha_modR2(base, N, sizeN, result, resultSize, arithType);
  3559. ltc_clear_all(base, true);
  3560. return status;
  3561. }
  3562. status_t LTC_PKHA_GCD(LTC_Type *base,
  3563. const uint8_t *A,
  3564. uint16_t sizeA,
  3565. const uint8_t *N,
  3566. uint16_t sizeN,
  3567. uint8_t *result,
  3568. uint16_t *resultSize,
  3569. ltc_pkha_f2m_t arithType)
  3570. {
  3571. ltc_pkha_mode_params_t params;
  3572. status_t status;
  3573. ltc_pkha_default_parms(&params);
  3574. params.func = kLTC_PKHA_ArithGcd;
  3575. params.arithType = arithType;
  3576. ltc_pkha_init_data(base, A, sizeA, NULL, 0, N, sizeN, NULL, 0);
  3577. status = ltc_pkha_init_mode(base, &params);
  3578. if (status == kStatus_Success)
  3579. {
  3580. /* Read the result and size from register B0. */
  3581. if (resultSize && result)
  3582. {
  3583. *resultSize = base->PKBSZ;
  3584. /* Read the data from the result register into place. */
  3585. ltc_pkha_read_reg(base, kLTC_PKHA_RegB, 0, result, *resultSize);
  3586. }
  3587. }
  3588. ltc_clear_all(base, true);
  3589. return status;
  3590. }
  3591. status_t LTC_PKHA_PrimalityTest(LTC_Type *base,
  3592. const uint8_t *A,
  3593. uint16_t sizeA,
  3594. const uint8_t *B,
  3595. uint16_t sizeB,
  3596. const uint8_t *N,
  3597. uint16_t sizeN,
  3598. bool *res)
  3599. {
  3600. uint8_t result;
  3601. ltc_pkha_mode_params_t params;
  3602. status_t status;
  3603. ltc_pkha_default_parms(&params);
  3604. params.func = kLTC_PKHA_ArithPrimalityTest;
  3605. ltc_pkha_init_data(base, A, sizeA, B, sizeB, N, sizeN, NULL, 0);
  3606. status = ltc_pkha_init_mode(base, &params);
  3607. if (status == kStatus_Success)
  3608. {
  3609. /* Read the data from the result register into place. */
  3610. ltc_pkha_read_reg(base, kLTC_PKHA_RegB, 0, &result, 1);
  3611. *res = (bool)result;
  3612. }
  3613. ltc_clear_all(base, true);
  3614. return status;
  3615. }
  3616. status_t LTC_PKHA_ECC_PointAdd(LTC_Type *base,
  3617. const ltc_pkha_ecc_point_t *A,
  3618. const ltc_pkha_ecc_point_t *B,
  3619. const uint8_t *N,
  3620. const uint8_t *R2modN,
  3621. const uint8_t *aCurveParam,
  3622. const uint8_t *bCurveParam,
  3623. uint8_t size,
  3624. ltc_pkha_f2m_t arithType,
  3625. ltc_pkha_ecc_point_t *result)
  3626. {
  3627. ltc_pkha_mode_params_t params;
  3628. uint32_t clearMask;
  3629. status_t status;
  3630. ltc_pkha_default_parms(&params);
  3631. params.func = kLTC_PKHA_ArithEccAdd;
  3632. params.arithType = arithType;
  3633. params.r2modn = R2modN ? kLTC_PKHA_InputR2 : kLTC_PKHA_CalcR2;
  3634. clearMask = kLTC_ClearMode;
  3635. /* Clear internal register states. */
  3636. clearMask |= kLTC_ClearPkhaSizeA;
  3637. clearMask |= kLTC_ClearPkhaSizeB;
  3638. clearMask |= kLTC_ClearPkhaSizeN;
  3639. clearMask |= kLTC_ClearPkhaSizeE;
  3640. base->CW = clearMask;
  3641. base->STA = kLTC_StatusDoneIsr;
  3642. ltc_pkha_clear_regabne(base, true, true, true, false);
  3643. /* sizeN should be less than 64 bytes. */
  3644. base->PKNSZ = size;
  3645. ltc_pkha_write_reg(base, kLTC_PKHA_RegN, 0, N, size);
  3646. base->PKASZ = size;
  3647. ltc_pkha_write_reg(base, kLTC_PKHA_RegA, 0, A->X, size);
  3648. ltc_pkha_write_reg(base, kLTC_PKHA_RegA, 1, A->Y, size);
  3649. ltc_pkha_write_reg(base, kLTC_PKHA_RegA, 3, aCurveParam, size);
  3650. base->PKBSZ = size;
  3651. ltc_pkha_write_reg(base, kLTC_PKHA_RegB, 0, bCurveParam, size);
  3652. ltc_pkha_write_reg(base, kLTC_PKHA_RegB, 1, B->X, size);
  3653. ltc_pkha_write_reg(base, kLTC_PKHA_RegB, 2, B->Y, size);
  3654. if (R2modN)
  3655. {
  3656. ltc_pkha_write_reg(base, kLTC_PKHA_RegB, 3, R2modN, size);
  3657. }
  3658. status = ltc_pkha_init_mode(base, &params);
  3659. if (status == kStatus_Success)
  3660. {
  3661. /* Read the data from the result register into place. */
  3662. ltc_pkha_read_reg(base, kLTC_PKHA_RegB, 1, result->X, size);
  3663. ltc_pkha_read_reg(base, kLTC_PKHA_RegB, 2, result->Y, size);
  3664. }
  3665. ltc_clear_all(base, true);
  3666. return status;
  3667. }
  3668. status_t LTC_PKHA_ECC_PointDouble(LTC_Type *base,
  3669. const ltc_pkha_ecc_point_t *B,
  3670. const uint8_t *N,
  3671. const uint8_t *aCurveParam,
  3672. const uint8_t *bCurveParam,
  3673. uint8_t size,
  3674. ltc_pkha_f2m_t arithType,
  3675. ltc_pkha_ecc_point_t *result)
  3676. {
  3677. ltc_pkha_mode_params_t params;
  3678. uint32_t clearMask;
  3679. status_t status;
  3680. ltc_pkha_default_parms(&params);
  3681. params.func = kLTC_PKHA_ArithEccDouble;
  3682. params.arithType = arithType;
  3683. clearMask = kLTC_ClearMode;
  3684. /* Clear internal register states. */
  3685. clearMask |= kLTC_ClearPkhaSizeA;
  3686. clearMask |= kLTC_ClearPkhaSizeB;
  3687. clearMask |= kLTC_ClearPkhaSizeN;
  3688. clearMask |= kLTC_ClearPkhaSizeE;
  3689. base->CW = clearMask;
  3690. base->STA = kLTC_StatusDoneIsr;
  3691. ltc_pkha_clear_regabne(base, true, true, true, false);
  3692. /* sizeN should be less than 64 bytes. */
  3693. base->PKNSZ = size;
  3694. ltc_pkha_write_reg(base, kLTC_PKHA_RegN, 0, N, size);
  3695. base->PKASZ = size;
  3696. ltc_pkha_write_reg(base, kLTC_PKHA_RegA, 3, aCurveParam, size);
  3697. base->PKBSZ = size;
  3698. ltc_pkha_write_reg(base, kLTC_PKHA_RegB, 0, bCurveParam, size);
  3699. ltc_pkha_write_reg(base, kLTC_PKHA_RegB, 1, B->X, size);
  3700. ltc_pkha_write_reg(base, kLTC_PKHA_RegB, 2, B->Y, size);
  3701. status = ltc_pkha_init_mode(base, &params);
  3702. if (status == kStatus_Success)
  3703. {
  3704. /* Read the data from the result register into place. */
  3705. ltc_pkha_read_reg(base, kLTC_PKHA_RegB, 1, result->X, size);
  3706. ltc_pkha_read_reg(base, kLTC_PKHA_RegB, 2, result->Y, size);
  3707. }
  3708. ltc_clear_all(base, true);
  3709. return status;
  3710. }
  3711. status_t LTC_PKHA_ECC_PointMul(LTC_Type *base,
  3712. const ltc_pkha_ecc_point_t *A,
  3713. const uint8_t *E,
  3714. uint8_t sizeE,
  3715. const uint8_t *N,
  3716. const uint8_t *R2modN,
  3717. const uint8_t *aCurveParam,
  3718. const uint8_t *bCurveParam,
  3719. uint8_t size,
  3720. ltc_pkha_timing_t equalTime,
  3721. ltc_pkha_f2m_t arithType,
  3722. ltc_pkha_ecc_point_t *result,
  3723. bool *infinity)
  3724. {
  3725. ltc_pkha_mode_params_t params;
  3726. uint32_t clearMask;
  3727. status_t status;
  3728. ltc_pkha_default_parms(&params);
  3729. params.func = kLTC_PKHA_ArithEccMul;
  3730. params.equalTime = equalTime;
  3731. params.arithType = arithType;
  3732. params.r2modn = R2modN ? kLTC_PKHA_InputR2 : kLTC_PKHA_CalcR2;
  3733. clearMask = kLTC_ClearMode;
  3734. /* Clear internal register states. */
  3735. clearMask |= kLTC_ClearPkhaSizeA;
  3736. clearMask |= kLTC_ClearPkhaSizeB;
  3737. clearMask |= kLTC_ClearPkhaSizeN;
  3738. clearMask |= kLTC_ClearPkhaSizeE;
  3739. base->CW = clearMask;
  3740. base->STA = kLTC_StatusDoneIsr;
  3741. ltc_pkha_clear_regabne(base, true, true, true, true);
  3742. /* sizeN should be less than 64 bytes. */
  3743. base->PKNSZ = size;
  3744. ltc_pkha_write_reg(base, kLTC_PKHA_RegN, 0, N, size);
  3745. base->PKESZ = sizeE;
  3746. ltc_pkha_write_reg(base, kLTC_PKHA_RegE, 0, E, sizeE);
  3747. base->PKASZ = size;
  3748. ltc_pkha_write_reg(base, kLTC_PKHA_RegA, 0, A->X, size);
  3749. ltc_pkha_write_reg(base, kLTC_PKHA_RegA, 1, A->Y, size);
  3750. ltc_pkha_write_reg(base, kLTC_PKHA_RegA, 3, aCurveParam, size);
  3751. base->PKBSZ = size;
  3752. ltc_pkha_write_reg(base, kLTC_PKHA_RegB, 0, bCurveParam, size);
  3753. if (R2modN)
  3754. {
  3755. ltc_pkha_write_reg(base, kLTC_PKHA_RegB, 1, R2modN, size);
  3756. }
  3757. status = ltc_pkha_init_mode(base, &params);
  3758. if (status == kStatus_Success)
  3759. {
  3760. /* Read the data from the result register into place. */
  3761. ltc_pkha_read_reg(base, kLTC_PKHA_RegB, 1, result->X, size);
  3762. ltc_pkha_read_reg(base, kLTC_PKHA_RegB, 2, result->Y, size);
  3763. if (infinity)
  3764. {
  3765. *infinity = (bool)(base->STA & kLTC_StatusPublicKeyOpZero);
  3766. }
  3767. }
  3768. ltc_clear_all(base, true);
  3769. return status;
  3770. }
  3771. #endif /* FSL_FEATURE_LTC_HAS_PKHA */