stm32f4xx_hal_cryp.c 169 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_cryp.c
  4. * @author MCD Application Team
  5. * @brief CRYP HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Cryptography (CRYP) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + AES processing functions
  10. * + DES processing functions
  11. * + TDES processing functions
  12. * + DMA callback functions
  13. * + CRYP IRQ handler management
  14. * + Peripheral State functions
  15. *
  16. @verbatim
  17. ==============================================================================
  18. ##### How to use this driver #####
  19. ==============================================================================
  20. [..]
  21. The CRYP HAL driver can be used as follows:
  22. (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
  23. (##) Enable the CRYP interface clock using __HAL_RCC_CRYP_CLK_ENABLE()
  24. (##) In case of using interrupts (e.g. HAL_CRYP_AESECB_Encrypt_IT())
  25. (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
  26. (+++) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ()
  27. (+++) In CRYP IRQ handler, call HAL_CRYP_IRQHandler()
  28. (##) In case of using DMA to control data transfer (e.g. HAL_CRYP_AESECB_Encrypt_DMA())
  29. (+++) Enable the DMAx interface clock using __DMAx_CLK_ENABLE()
  30. (+++) Configure and enable two DMA streams one for managing data transfer from
  31. memory to peripheral (input stream) and another stream for managing data
  32. transfer from peripheral to memory (output stream)
  33. (+++) Associate the initialized DMA handle to the CRYP DMA handle
  34. using __HAL_LINKDMA()
  35. (+++) Configure the priority and enable the NVIC for the transfer complete
  36. interrupt on the two DMA Streams. The output stream should have higher
  37. priority than the input stream HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()
  38. (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures mainly:
  39. (##) The data type: 1-bit, 8-bit, 16-bit and 32-bit
  40. (##) The key size: 128, 192 and 256. This parameter is relevant only for AES
  41. (##) The encryption/decryption key. It's size depends on the algorithm
  42. used for encryption/decryption
  43. (##) The initialization vector (counter). It is not used ECB mode.
  44. (#)Three processing (encryption/decryption) functions are available:
  45. (##) Polling mode: encryption and decryption APIs are blocking functions
  46. i.e. they process the data and wait till the processing is finished,
  47. e.g. HAL_CRYP_AESCBC_Encrypt()
  48. (##) Interrupt mode: encryption and decryption APIs are not blocking functions
  49. i.e. they process the data under interrupt,
  50. e.g. HAL_CRYP_AESCBC_Encrypt_IT()
  51. (##) DMA mode: encryption and decryption APIs are not blocking functions
  52. i.e. the data transfer is ensured by DMA,
  53. e.g. HAL_CRYP_AESCBC_Encrypt_DMA()
  54. (#)When the processing function is called at first time after HAL_CRYP_Init()
  55. the CRYP peripheral is initialized and processes the buffer in input.
  56. At second call, the processing function performs an append of the already
  57. processed buffer.
  58. When a new data block is to be processed, call HAL_CRYP_Init() then the
  59. processing function.
  60. (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
  61. @endverbatim
  62. ******************************************************************************
  63. * @attention
  64. *
  65. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  66. *
  67. * Redistribution and use in source and binary forms, with or without modification,
  68. * are permitted provided that the following conditions are met:
  69. * 1. Redistributions of source code must retain the above copyright notice,
  70. * this list of conditions and the following disclaimer.
  71. * 2. Redistributions in binary form must reproduce the above copyright notice,
  72. * this list of conditions and the following disclaimer in the documentation
  73. * and/or other materials provided with the distribution.
  74. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  75. * may be used to endorse or promote products derived from this software
  76. * without specific prior written permission.
  77. *
  78. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  79. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  80. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  81. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  82. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  83. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  84. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  85. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  86. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  87. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  88. *
  89. ******************************************************************************
  90. */
  91. /* Includes ------------------------------------------------------------------*/
  92. #include "stm32f4xx_hal.h"
  93. /** @addtogroup STM32F4xx_HAL_Driver
  94. * @{
  95. */
  96. #ifdef HAL_CRYP_MODULE_ENABLED
  97. #if defined(CRYP)
  98. /** @defgroup CRYP CRYP
  99. * @brief CRYP HAL module driver.
  100. * @{
  101. */
  102. /* Private typedef -----------------------------------------------------------*/
  103. /* Private define ------------------------------------------------------------*/
  104. /** @addtogroup CRYP_Private_define
  105. * @{
  106. */
  107. #define CRYP_TIMEOUT_VALUE 1U
  108. /**
  109. * @}
  110. */
  111. /* Private macro -------------------------------------------------------------*/
  112. /* Private variables ---------------------------------------------------------*/
  113. /* Private function prototypes -----------------------------------------------*/
  114. /** @addtogroup CRYP_Private_Functions_prototypes
  115. * @{
  116. */
  117. static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector, uint32_t IVSize);
  118. static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize);
  119. static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout);
  120. static HAL_StatusTypeDef CRYP_ProcessData2Words(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout);
  121. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma);
  122. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma);
  123. static void CRYP_DMAError(DMA_HandleTypeDef *hdma);
  124. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
  125. static void CRYP_SetTDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction);
  126. static void CRYP_SetTDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction);
  127. static void CRYP_SetDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction);
  128. static void CRYP_SetDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction);
  129. /**
  130. * @}
  131. */
  132. /* Private functions ---------------------------------------------------------*/
  133. /** @addtogroup CRYP_Private_Functions
  134. * @{
  135. */
  136. /**
  137. * @brief DMA CRYP Input Data process complete callback.
  138. * @param hdma DMA handle
  139. * @retval None
  140. */
  141. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma)
  142. {
  143. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  144. /* Disable the DMA transfer for input FIFO request by resetting the DIEN bit
  145. in the DMACR register */
  146. hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DIEN);
  147. /* Call input data transfer complete callback */
  148. HAL_CRYP_InCpltCallback(hcryp);
  149. }
  150. /**
  151. * @brief DMA CRYP Output Data process complete callback.
  152. * @param hdma DMA handle
  153. * @retval None
  154. */
  155. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma)
  156. {
  157. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  158. /* Disable the DMA transfer for output FIFO request by resetting the DOEN bit
  159. in the DMACR register */
  160. hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DOEN);
  161. /* Disable CRYP */
  162. __HAL_CRYP_DISABLE(hcryp);
  163. /* Change the CRYP state to ready */
  164. hcryp->State = HAL_CRYP_STATE_READY;
  165. /* Call output data transfer complete callback */
  166. HAL_CRYP_OutCpltCallback(hcryp);
  167. }
  168. /**
  169. * @brief DMA CRYP communication error callback.
  170. * @param hdma DMA handle
  171. * @retval None
  172. */
  173. static void CRYP_DMAError(DMA_HandleTypeDef *hdma)
  174. {
  175. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  176. hcryp->State= HAL_CRYP_STATE_READY;
  177. HAL_CRYP_ErrorCallback(hcryp);
  178. }
  179. /**
  180. * @brief Writes the Key in Key registers.
  181. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  182. * the configuration information for CRYP module
  183. * @param Key Pointer to Key buffer
  184. * @param KeySize Size of Key
  185. * @retval None
  186. */
  187. static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize)
  188. {
  189. uint32_t keyaddr = (uint32_t)Key;
  190. switch(KeySize)
  191. {
  192. case CRYP_KEYSIZE_256B:
  193. /* Key Initialisation */
  194. hcryp->Instance->K0LR = __REV(*(uint32_t*)(keyaddr));
  195. keyaddr+=4U;
  196. hcryp->Instance->K0RR = __REV(*(uint32_t*)(keyaddr));
  197. keyaddr+=4U;
  198. hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr));
  199. keyaddr+=4U;
  200. hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr));
  201. keyaddr+=4U;
  202. hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));
  203. keyaddr+=4U;
  204. hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));
  205. keyaddr+=4U;
  206. hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));
  207. keyaddr+=4U;
  208. hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr));
  209. break;
  210. case CRYP_KEYSIZE_192B:
  211. hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr));
  212. keyaddr+=4U;
  213. hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr));
  214. keyaddr+=4U;
  215. hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));
  216. keyaddr+=4U;
  217. hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));
  218. keyaddr+=4U;
  219. hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));
  220. keyaddr+=4U;
  221. hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr));
  222. break;
  223. case CRYP_KEYSIZE_128B:
  224. hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));
  225. keyaddr+=4U;
  226. hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));
  227. keyaddr+=4U;
  228. hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));
  229. keyaddr+=4U;
  230. hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr));
  231. break;
  232. default:
  233. break;
  234. }
  235. }
  236. /**
  237. * @brief Writes the InitVector/InitCounter in IV registers.
  238. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  239. * the configuration information for CRYP module
  240. * @param InitVector Pointer to InitVector/InitCounter buffer
  241. * @param IVSize Size of the InitVector/InitCounter
  242. * @retval None
  243. */
  244. static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector, uint32_t IVSize)
  245. {
  246. uint32_t ivaddr = (uint32_t)InitVector;
  247. switch(IVSize)
  248. {
  249. case CRYP_KEYSIZE_128B:
  250. hcryp->Instance->IV0LR = __REV(*(uint32_t*)(ivaddr));
  251. ivaddr+=4U;
  252. hcryp->Instance->IV0RR = __REV(*(uint32_t*)(ivaddr));
  253. ivaddr+=4U;
  254. hcryp->Instance->IV1LR = __REV(*(uint32_t*)(ivaddr));
  255. ivaddr+=4U;
  256. hcryp->Instance->IV1RR = __REV(*(uint32_t*)(ivaddr));
  257. break;
  258. /* Whatever key size 192 or 256, Init vector is written in IV0LR and IV0RR */
  259. case CRYP_KEYSIZE_192B:
  260. hcryp->Instance->IV0LR = __REV(*(uint32_t*)(ivaddr));
  261. ivaddr+=4U;
  262. hcryp->Instance->IV0RR = __REV(*(uint32_t*)(ivaddr));
  263. break;
  264. case CRYP_KEYSIZE_256B:
  265. hcryp->Instance->IV0LR = __REV(*(uint32_t*)(ivaddr));
  266. ivaddr+=4U;
  267. hcryp->Instance->IV0RR = __REV(*(uint32_t*)(ivaddr));
  268. break;
  269. default:
  270. break;
  271. }
  272. }
  273. /**
  274. * @brief Process Data: Writes Input data in polling mode and read the output data
  275. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  276. * the configuration information for CRYP module
  277. * @param Input Pointer to the Input buffer
  278. * @param Ilength Length of the Input buffer, must be a multiple of 16.
  279. * @param Output Pointer to the returned buffer
  280. * @param Timeout Timeout value
  281. * @retval None
  282. */
  283. static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout)
  284. {
  285. uint32_t tickstart = 0U;
  286. uint32_t i = 0U;
  287. uint32_t inputaddr = (uint32_t)Input;
  288. uint32_t outputaddr = (uint32_t)Output;
  289. for(i=0U; (i < Ilength); i+=16U)
  290. {
  291. /* Write the Input block in the IN FIFO */
  292. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  293. inputaddr+=4U;
  294. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  295. inputaddr+=4U;
  296. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  297. inputaddr+=4U;
  298. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  299. inputaddr+=4U;
  300. /* Get tick */
  301. tickstart = HAL_GetTick();
  302. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
  303. {
  304. /* Check for the Timeout */
  305. if(Timeout != HAL_MAX_DELAY)
  306. {
  307. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  308. {
  309. /* Change state */
  310. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  311. /* Process Unlocked */
  312. __HAL_UNLOCK(hcryp);
  313. return HAL_TIMEOUT;
  314. }
  315. }
  316. }
  317. /* Read the Output block from the Output FIFO */
  318. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  319. outputaddr+=4U;
  320. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  321. outputaddr+=4U;
  322. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  323. outputaddr+=4U;
  324. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  325. outputaddr+=4U;
  326. }
  327. /* Return function status */
  328. return HAL_OK;
  329. }
  330. /**
  331. * @brief Process Data: Write Input data in polling mode.
  332. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  333. * the configuration information for CRYP module
  334. * @param Input Pointer to the Input buffer
  335. * @param Ilength Length of the Input buffer, must be a multiple of 8
  336. * @param Output Pointer to the returned buffer
  337. * @param Timeout Specify Timeout value
  338. * @retval None
  339. */
  340. static HAL_StatusTypeDef CRYP_ProcessData2Words(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout)
  341. {
  342. uint32_t tickstart = 0U;
  343. uint32_t i = 0U;
  344. uint32_t inputaddr = (uint32_t)Input;
  345. uint32_t outputaddr = (uint32_t)Output;
  346. for(i=0U; (i < Ilength); i+=8U)
  347. {
  348. /* Write the Input block in the IN FIFO */
  349. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  350. inputaddr+=4U;
  351. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  352. inputaddr+=4U;
  353. /* Get tick */
  354. tickstart = HAL_GetTick();
  355. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
  356. {
  357. /* Check for the Timeout */
  358. if(Timeout != HAL_MAX_DELAY)
  359. {
  360. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  361. {
  362. /* Change state */
  363. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  364. /* Process Unlocked */
  365. __HAL_UNLOCK(hcryp);
  366. return HAL_TIMEOUT;
  367. }
  368. }
  369. }
  370. /* Read the Output block from the Output FIFO */
  371. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  372. outputaddr+=4U;
  373. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  374. outputaddr+=4U;
  375. }
  376. /* Return function status */
  377. return HAL_OK;
  378. }
  379. /**
  380. * @brief Set the DMA configuration and start the DMA transfer
  381. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  382. * the configuration information for CRYP module
  383. * @param inputaddr address of the Input buffer
  384. * @param Size Size of the Input buffer, must be a multiple of 16.
  385. * @param outputaddr address of the Output buffer
  386. * @retval None
  387. */
  388. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
  389. {
  390. /* Set the CRYP DMA transfer complete callback */
  391. hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt;
  392. /* Set the DMA error callback */
  393. hcryp->hdmain->XferErrorCallback = CRYP_DMAError;
  394. /* Set the CRYP DMA transfer complete callback */
  395. hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt;
  396. /* Set the DMA error callback */
  397. hcryp->hdmaout->XferErrorCallback = CRYP_DMAError;
  398. /* Enable CRYP */
  399. __HAL_CRYP_ENABLE(hcryp);
  400. /* Enable the DMA In DMA Stream */
  401. HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DR, Size/4U);
  402. /* Enable In DMA request */
  403. hcryp->Instance->DMACR = (CRYP_DMACR_DIEN);
  404. /* Enable the DMA Out DMA Stream */
  405. HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUT, outputaddr, Size/4U);
  406. /* Enable Out DMA request */
  407. hcryp->Instance->DMACR |= CRYP_DMACR_DOEN;
  408. }
  409. /**
  410. * @brief Sets the CRYP peripheral in DES ECB mode.
  411. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  412. * the configuration information for CRYP module
  413. * @param Direction Encryption or decryption
  414. * @retval None
  415. */
  416. static void CRYP_SetDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction)
  417. {
  418. /* Check if initialization phase has already been performed */
  419. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  420. {
  421. /* Set the CRYP peripheral in AES ECB mode */
  422. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_DES_ECB | Direction);
  423. /* Set the key */
  424. hcryp->Instance->K1LR = __REV(*(uint32_t*)(hcryp->Init.pKey));
  425. hcryp->Instance->K1RR = __REV(*(uint32_t*)(hcryp->Init.pKey+4U));
  426. /* Flush FIFO */
  427. __HAL_CRYP_FIFO_FLUSH(hcryp);
  428. /* Set the phase */
  429. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  430. }
  431. }
  432. /**
  433. * @brief Sets the CRYP peripheral in DES CBC mode.
  434. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  435. * the configuration information for CRYP module
  436. * @param Direction Encryption or decryption
  437. * @retval None
  438. */
  439. static void CRYP_SetDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction)
  440. {
  441. /* Check if initialization phase has already been performed */
  442. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  443. {
  444. /* Set the CRYP peripheral in AES ECB mode */
  445. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_DES_CBC | Direction);
  446. /* Set the key */
  447. hcryp->Instance->K1LR = __REV(*(uint32_t*)(hcryp->Init.pKey));
  448. hcryp->Instance->K1RR = __REV(*(uint32_t*)(hcryp->Init.pKey+4U));
  449. /* Set the Initialization Vector */
  450. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_256B);
  451. /* Flush FIFO */
  452. __HAL_CRYP_FIFO_FLUSH(hcryp);
  453. /* Set the phase */
  454. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  455. }
  456. }
  457. /**
  458. * @brief Sets the CRYP peripheral in TDES ECB mode.
  459. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  460. * the configuration information for CRYP module
  461. * @param Direction Encryption or decryption
  462. * @retval None
  463. */
  464. static void CRYP_SetTDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction)
  465. {
  466. /* Check if initialization phase has already been performed */
  467. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  468. {
  469. /* Set the CRYP peripheral in AES ECB mode */
  470. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_TDES_ECB | Direction);
  471. /* Set the key */
  472. CRYP_SetKey(hcryp, hcryp->Init.pKey, CRYP_KEYSIZE_192B);
  473. /* Flush FIFO */
  474. __HAL_CRYP_FIFO_FLUSH(hcryp);
  475. /* Set the phase */
  476. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  477. }
  478. }
  479. /**
  480. * @brief Sets the CRYP peripheral in TDES CBC mode
  481. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  482. * the configuration information for CRYP module
  483. * @param Direction Encryption or decryption
  484. * @retval None
  485. */
  486. static void CRYP_SetTDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction)
  487. {
  488. /* Check if initialization phase has already been performed */
  489. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  490. {
  491. /* Set the CRYP peripheral in AES CBC mode */
  492. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_TDES_CBC | Direction);
  493. /* Set the key */
  494. CRYP_SetKey(hcryp, hcryp->Init.pKey, CRYP_KEYSIZE_192B);
  495. /* Set the Initialization Vector */
  496. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_256B);
  497. /* Flush FIFO */
  498. __HAL_CRYP_FIFO_FLUSH(hcryp);
  499. /* Set the phase */
  500. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  501. }
  502. }
  503. /**
  504. * @}
  505. */
  506. /* Exported functions --------------------------------------------------------*/
  507. /** @addtogroup CRYP_Exported_Functions
  508. * @{
  509. */
  510. /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions
  511. * @brief Initialization and Configuration functions.
  512. *
  513. @verbatim
  514. ==============================================================================
  515. ##### Initialization and de-initialization functions #####
  516. ==============================================================================
  517. [..] This section provides functions allowing to:
  518. (+) Initialize the CRYP according to the specified parameters
  519. in the CRYP_InitTypeDef and creates the associated handle
  520. (+) DeInitialize the CRYP peripheral
  521. (+) Initialize the CRYP MSP
  522. (+) DeInitialize CRYP MSP
  523. @endverbatim
  524. * @{
  525. */
  526. /**
  527. * @brief Initializes the CRYP according to the specified
  528. * parameters in the CRYP_InitTypeDef and creates the associated handle.
  529. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  530. * the configuration information for CRYP module
  531. * @retval HAL status
  532. */
  533. HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
  534. {
  535. /* Check the CRYP handle allocation */
  536. if(hcryp == NULL)
  537. {
  538. return HAL_ERROR;
  539. }
  540. /* Check the parameters */
  541. assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
  542. assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
  543. if(hcryp->State == HAL_CRYP_STATE_RESET)
  544. {
  545. /* Allocate lock resource and initialize it */
  546. hcryp->Lock = HAL_UNLOCKED;
  547. /* Init the low level hardware */
  548. HAL_CRYP_MspInit(hcryp);
  549. }
  550. /* Change the CRYP state */
  551. hcryp->State = HAL_CRYP_STATE_BUSY;
  552. /* Set the key size and data type*/
  553. CRYP->CR = (uint32_t) (hcryp->Init.KeySize | hcryp->Init.DataType);
  554. /* Reset CrypInCount and CrypOutCount */
  555. hcryp->CrypInCount = 0U;
  556. hcryp->CrypOutCount = 0U;
  557. /* Change the CRYP state */
  558. hcryp->State = HAL_CRYP_STATE_READY;
  559. /* Set the default CRYP phase */
  560. hcryp->Phase = HAL_CRYP_PHASE_READY;
  561. /* Return function status */
  562. return HAL_OK;
  563. }
  564. /**
  565. * @brief DeInitializes the CRYP peripheral.
  566. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  567. * the configuration information for CRYP module
  568. * @retval HAL status
  569. */
  570. HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
  571. {
  572. /* Check the CRYP handle allocation */
  573. if(hcryp == NULL)
  574. {
  575. return HAL_ERROR;
  576. }
  577. /* Change the CRYP state */
  578. hcryp->State = HAL_CRYP_STATE_BUSY;
  579. /* Set the default CRYP phase */
  580. hcryp->Phase = HAL_CRYP_PHASE_READY;
  581. /* Reset CrypInCount and CrypOutCount */
  582. hcryp->CrypInCount = 0U;
  583. hcryp->CrypOutCount = 0U;
  584. /* Disable the CRYP Peripheral Clock */
  585. __HAL_CRYP_DISABLE(hcryp);
  586. /* DeInit the low level hardware: CLOCK, NVIC.*/
  587. HAL_CRYP_MspDeInit(hcryp);
  588. /* Change the CRYP state */
  589. hcryp->State = HAL_CRYP_STATE_RESET;
  590. /* Release Lock */
  591. __HAL_UNLOCK(hcryp);
  592. /* Return function status */
  593. return HAL_OK;
  594. }
  595. /**
  596. * @brief Initializes the CRYP MSP.
  597. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  598. * the configuration information for CRYP module
  599. * @retval None
  600. */
  601. __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
  602. {
  603. /* Prevent unused argument(s) compilation warning */
  604. UNUSED(hcryp);
  605. /* NOTE : This function Should not be modified, when the callback is needed,
  606. the HAL_CRYP_MspInit could be implemented in the user file
  607. */
  608. }
  609. /**
  610. * @brief DeInitializes CRYP MSP.
  611. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  612. * the configuration information for CRYP module
  613. * @retval None
  614. */
  615. __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
  616. {
  617. /* Prevent unused argument(s) compilation warning */
  618. UNUSED(hcryp);
  619. /* NOTE : This function Should not be modified, when the callback is needed,
  620. the HAL_CRYP_MspDeInit could be implemented in the user file
  621. */
  622. }
  623. /**
  624. * @}
  625. */
  626. /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions
  627. * @brief processing functions.
  628. *
  629. @verbatim
  630. ==============================================================================
  631. ##### AES processing functions #####
  632. ==============================================================================
  633. [..] This section provides functions allowing to:
  634. (+) Encrypt plaintext using AES-128/192/256 using chaining modes
  635. (+) Decrypt cyphertext using AES-128/192/256 using chaining modes
  636. [..] Three processing functions are available:
  637. (+) Polling mode
  638. (+) Interrupt mode
  639. (+) DMA mode
  640. @endverbatim
  641. * @{
  642. */
  643. /**
  644. * @brief Initializes the CRYP peripheral in AES ECB encryption mode
  645. * then encrypt pPlainData. The cypher data are available in pCypherData
  646. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  647. * the configuration information for CRYP module
  648. * @param pPlainData Pointer to the plaintext buffer
  649. * @param Size Length of the plaintext buffer, must be a multiple of 16.
  650. * @param pCypherData Pointer to the cyphertext buffer
  651. * @param Timeout Specify Timeout value
  652. * @retval HAL status
  653. */
  654. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  655. {
  656. /* Process Locked */
  657. __HAL_LOCK(hcryp);
  658. /* Change the CRYP state */
  659. hcryp->State = HAL_CRYP_STATE_BUSY;
  660. /* Check if initialization phase has already been performed */
  661. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  662. {
  663. /* Set the key */
  664. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  665. /* Set the CRYP peripheral in AES ECB mode */
  666. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB);
  667. /* Flush FIFO */
  668. __HAL_CRYP_FIFO_FLUSH(hcryp);
  669. /* Enable CRYP */
  670. __HAL_CRYP_ENABLE(hcryp);
  671. /* Set the phase */
  672. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  673. }
  674. /* Write Plain Data and Get Cypher Data */
  675. if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  676. {
  677. return HAL_TIMEOUT;
  678. }
  679. /* Change the CRYP state */
  680. hcryp->State = HAL_CRYP_STATE_READY;
  681. /* Process Unlocked */
  682. __HAL_UNLOCK(hcryp);
  683. /* Return function status */
  684. return HAL_OK;
  685. }
  686. /**
  687. * @brief Initializes the CRYP peripheral in AES CBC encryption mode
  688. * then encrypt pPlainData. The cypher data are available in pCypherData
  689. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  690. * the configuration information for CRYP module
  691. * @param pPlainData Pointer to the plaintext buffer
  692. * @param Size Length of the plaintext buffer, must be a multiple of 16.
  693. * @param pCypherData Pointer to the cyphertext buffer
  694. * @param Timeout Specify Timeout value
  695. * @retval HAL status
  696. */
  697. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  698. {
  699. /* Process Locked */
  700. __HAL_LOCK(hcryp);
  701. /* Change the CRYP state */
  702. hcryp->State = HAL_CRYP_STATE_BUSY;
  703. /* Check if initialization phase has already been performed */
  704. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  705. {
  706. /* Set the key */
  707. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  708. /* Set the CRYP peripheral in AES ECB mode */
  709. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC);
  710. /* Set the Initialization Vector */
  711. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  712. /* Flush FIFO */
  713. __HAL_CRYP_FIFO_FLUSH(hcryp);
  714. /* Enable CRYP */
  715. __HAL_CRYP_ENABLE(hcryp);
  716. /* Set the phase */
  717. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  718. }
  719. /* Write Plain Data and Get Cypher Data */
  720. if(CRYP_ProcessData(hcryp,pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  721. {
  722. return HAL_TIMEOUT;
  723. }
  724. /* Change the CRYP state */
  725. hcryp->State = HAL_CRYP_STATE_READY;
  726. /* Process Unlocked */
  727. __HAL_UNLOCK(hcryp);
  728. /* Return function status */
  729. return HAL_OK;
  730. }
  731. /**
  732. * @brief Initializes the CRYP peripheral in AES CTR encryption mode
  733. * then encrypt pPlainData. The cypher data are available in pCypherData
  734. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  735. * the configuration information for CRYP module
  736. * @param pPlainData Pointer to the plaintext buffer
  737. * @param Size Length of the plaintext buffer, must be a multiple of 16.
  738. * @param pCypherData Pointer to the cyphertext buffer
  739. * @param Timeout Specify Timeout value
  740. * @retval HAL status
  741. */
  742. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  743. {
  744. /* Process Locked */
  745. __HAL_LOCK(hcryp);
  746. /* Change the CRYP state */
  747. hcryp->State = HAL_CRYP_STATE_BUSY;
  748. /* Check if initialization phase has already been performed */
  749. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  750. {
  751. /* Set the key */
  752. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  753. /* Set the CRYP peripheral in AES ECB mode */
  754. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR);
  755. /* Set the Initialization Vector */
  756. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  757. /* Flush FIFO */
  758. __HAL_CRYP_FIFO_FLUSH(hcryp);
  759. /* Enable CRYP */
  760. __HAL_CRYP_ENABLE(hcryp);
  761. /* Set the phase */
  762. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  763. }
  764. /* Write Plain Data and Get Cypher Data */
  765. if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  766. {
  767. return HAL_TIMEOUT;
  768. }
  769. /* Change the CRYP state */
  770. hcryp->State = HAL_CRYP_STATE_READY;
  771. /* Process Unlocked */
  772. __HAL_UNLOCK(hcryp);
  773. /* Return function status */
  774. return HAL_OK;
  775. }
  776. /**
  777. * @brief Initializes the CRYP peripheral in AES ECB decryption mode
  778. * then decrypted pCypherData. The cypher data are available in pPlainData
  779. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  780. * the configuration information for CRYP module
  781. * @param pCypherData Pointer to the cyphertext buffer
  782. * @param Size Length of the plaintext buffer, must be a multiple of 16.
  783. * @param pPlainData Pointer to the plaintext buffer
  784. * @param Timeout Specify Timeout value
  785. * @retval HAL status
  786. */
  787. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  788. {
  789. uint32_t tickstart = 0U;
  790. /* Process Locked */
  791. __HAL_LOCK(hcryp);
  792. /* Change the CRYP state */
  793. hcryp->State = HAL_CRYP_STATE_BUSY;
  794. /* Check if initialization phase has already been performed */
  795. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  796. {
  797. /* Set the key */
  798. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  799. /* Set the CRYP peripheral in AES Key mode */
  800. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  801. /* Enable CRYP */
  802. __HAL_CRYP_ENABLE(hcryp);
  803. /* Get tick */
  804. tickstart = HAL_GetTick();
  805. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  806. {
  807. /* Check for the Timeout */
  808. if(Timeout != HAL_MAX_DELAY)
  809. {
  810. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  811. {
  812. /* Change state */
  813. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  814. /* Process Unlocked */
  815. __HAL_UNLOCK(hcryp);
  816. return HAL_TIMEOUT;
  817. }
  818. }
  819. }
  820. /* Disable CRYP */
  821. __HAL_CRYP_DISABLE(hcryp);
  822. /* Reset the ALGOMODE bits*/
  823. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  824. /* Set the CRYP peripheral in AES ECB decryption mode */
  825. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR);
  826. /* Flush FIFO */
  827. __HAL_CRYP_FIFO_FLUSH(hcryp);
  828. /* Enable CRYP */
  829. __HAL_CRYP_ENABLE(hcryp);
  830. /* Set the phase */
  831. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  832. }
  833. /* Write Plain Data and Get Cypher Data */
  834. if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  835. {
  836. return HAL_TIMEOUT;
  837. }
  838. /* Change the CRYP state */
  839. hcryp->State = HAL_CRYP_STATE_READY;
  840. /* Process Unlocked */
  841. __HAL_UNLOCK(hcryp);
  842. /* Return function status */
  843. return HAL_OK;
  844. }
  845. /**
  846. * @brief Initializes the CRYP peripheral in AES ECB decryption mode
  847. * then decrypted pCypherData. The cypher data are available in pPlainData
  848. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  849. * the configuration information for CRYP module
  850. * @param pCypherData Pointer to the cyphertext buffer
  851. * @param Size Length of the plaintext buffer, must be a multiple of 16.
  852. * @param pPlainData Pointer to the plaintext buffer
  853. * @param Timeout Specify Timeout value
  854. * @retval HAL status
  855. */
  856. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  857. {
  858. uint32_t tickstart = 0U;
  859. /* Process Locked */
  860. __HAL_LOCK(hcryp);
  861. /* Change the CRYP state */
  862. hcryp->State = HAL_CRYP_STATE_BUSY;
  863. /* Check if initialization phase has already been performed */
  864. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  865. {
  866. /* Set the key */
  867. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  868. /* Set the CRYP peripheral in AES Key mode */
  869. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  870. /* Enable CRYP */
  871. __HAL_CRYP_ENABLE(hcryp);
  872. /* Get tick */
  873. tickstart = HAL_GetTick();
  874. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  875. {
  876. /* Check for the Timeout */
  877. if(Timeout != HAL_MAX_DELAY)
  878. {
  879. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  880. {
  881. /* Change state */
  882. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  883. /* Process Unlocked */
  884. __HAL_UNLOCK(hcryp);
  885. return HAL_TIMEOUT;
  886. }
  887. }
  888. }
  889. /* Reset the ALGOMODE bits*/
  890. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  891. /* Set the CRYP peripheral in AES CBC decryption mode */
  892. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR);
  893. /* Set the Initialization Vector */
  894. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  895. /* Flush FIFO */
  896. __HAL_CRYP_FIFO_FLUSH(hcryp);
  897. /* Enable CRYP */
  898. __HAL_CRYP_ENABLE(hcryp);
  899. /* Set the phase */
  900. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  901. }
  902. /* Write Plain Data and Get Cypher Data */
  903. if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  904. {
  905. return HAL_TIMEOUT;
  906. }
  907. /* Change the CRYP state */
  908. hcryp->State = HAL_CRYP_STATE_READY;
  909. /* Process Unlocked */
  910. __HAL_UNLOCK(hcryp);
  911. /* Return function status */
  912. return HAL_OK;
  913. }
  914. /**
  915. * @brief Initializes the CRYP peripheral in AES CTR decryption mode
  916. * then decrypted pCypherData. The cypher data are available in pPlainData
  917. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  918. * the configuration information for CRYP module
  919. * @param pCypherData Pointer to the cyphertext buffer
  920. * @param Size Length of the plaintext buffer, must be a multiple of 16.
  921. * @param pPlainData Pointer to the plaintext buffer
  922. * @param Timeout Specify Timeout value
  923. * @retval HAL status
  924. */
  925. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  926. {
  927. /* Process Locked */
  928. __HAL_LOCK(hcryp);
  929. /* Check if initialization phase has already been performed */
  930. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  931. {
  932. /* Change the CRYP state */
  933. hcryp->State = HAL_CRYP_STATE_BUSY;
  934. /* Set the key */
  935. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  936. /* Set the CRYP peripheral in AES CTR mode */
  937. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR);
  938. /* Set the Initialization Vector */
  939. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  940. /* Flush FIFO */
  941. __HAL_CRYP_FIFO_FLUSH(hcryp);
  942. /* Enable CRYP */
  943. __HAL_CRYP_ENABLE(hcryp);
  944. /* Set the phase */
  945. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  946. }
  947. /* Write Plain Data and Get Cypher Data */
  948. if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  949. {
  950. return HAL_TIMEOUT;
  951. }
  952. /* Change the CRYP state */
  953. hcryp->State = HAL_CRYP_STATE_READY;
  954. /* Process Unlocked */
  955. __HAL_UNLOCK(hcryp);
  956. /* Return function status */
  957. return HAL_OK;
  958. }
  959. /**
  960. * @brief Initializes the CRYP peripheral in AES ECB encryption mode using Interrupt.
  961. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  962. * the configuration information for CRYP module
  963. * @param pPlainData Pointer to the plaintext buffer
  964. * @param Size Length of the plaintext buffer, must be a multiple of 16 bytes
  965. * @param pCypherData Pointer to the cyphertext buffer
  966. * @retval HAL status
  967. */
  968. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  969. {
  970. uint32_t inputaddr;
  971. uint32_t outputaddr;
  972. if(hcryp->State == HAL_CRYP_STATE_READY)
  973. {
  974. /* Process Locked */
  975. __HAL_LOCK(hcryp);
  976. hcryp->CrypInCount = Size;
  977. hcryp->pCrypInBuffPtr = pPlainData;
  978. hcryp->pCrypOutBuffPtr = pCypherData;
  979. hcryp->CrypOutCount = Size;
  980. /* Change the CRYP state */
  981. hcryp->State = HAL_CRYP_STATE_BUSY;
  982. /* Check if initialization phase has already been performed */
  983. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  984. {
  985. /* Set the key */
  986. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  987. /* Set the CRYP peripheral in AES ECB mode */
  988. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB);
  989. /* Flush FIFO */
  990. __HAL_CRYP_FIFO_FLUSH(hcryp);
  991. /* Set the phase */
  992. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  993. }
  994. /* Enable Interrupts */
  995. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  996. /* Enable CRYP */
  997. __HAL_CRYP_ENABLE(hcryp);
  998. /* Return function status */
  999. return HAL_OK;
  1000. }
  1001. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1002. {
  1003. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1004. /* Write the Input block in the IN FIFO */
  1005. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1006. inputaddr+=4U;
  1007. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1008. inputaddr+=4U;
  1009. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1010. inputaddr+=4U;
  1011. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1012. hcryp->pCrypInBuffPtr += 16U;
  1013. hcryp->CrypInCount -= 16U;
  1014. if(hcryp->CrypInCount == 0U)
  1015. {
  1016. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1017. /* Call the Input data transfer complete callback */
  1018. HAL_CRYP_InCpltCallback(hcryp);
  1019. }
  1020. }
  1021. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1022. {
  1023. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1024. /* Read the Output block from the Output FIFO */
  1025. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1026. outputaddr+=4U;
  1027. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1028. outputaddr+=4U;
  1029. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1030. outputaddr+=4U;
  1031. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1032. hcryp->pCrypOutBuffPtr += 16U;
  1033. hcryp->CrypOutCount -= 16U;
  1034. if(hcryp->CrypOutCount == 0U)
  1035. {
  1036. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1037. /* Process Locked */
  1038. __HAL_UNLOCK(hcryp);
  1039. /* Change the CRYP state */
  1040. hcryp->State = HAL_CRYP_STATE_READY;
  1041. /* Call Input transfer complete callback */
  1042. HAL_CRYP_OutCpltCallback(hcryp);
  1043. }
  1044. }
  1045. /* Return function status */
  1046. return HAL_OK;
  1047. }
  1048. /**
  1049. * @brief Initializes the CRYP peripheral in AES CBC encryption mode using Interrupt.
  1050. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1051. * the configuration information for CRYP module
  1052. * @param pPlainData Pointer to the plaintext buffer
  1053. * @param Size Length of the plaintext buffer, must be a multiple of 16 bytes
  1054. * @param pCypherData Pointer to the cyphertext buffer
  1055. * @retval HAL status
  1056. */
  1057. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1058. {
  1059. uint32_t inputaddr;
  1060. uint32_t outputaddr;
  1061. if(hcryp->State == HAL_CRYP_STATE_READY)
  1062. {
  1063. /* Process Locked */
  1064. __HAL_LOCK(hcryp);
  1065. hcryp->CrypInCount = Size;
  1066. hcryp->pCrypInBuffPtr = pPlainData;
  1067. hcryp->pCrypOutBuffPtr = pCypherData;
  1068. hcryp->CrypOutCount = Size;
  1069. /* Change the CRYP state */
  1070. hcryp->State = HAL_CRYP_STATE_BUSY;
  1071. /* Check if initialization phase has already been performed */
  1072. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1073. {
  1074. /* Set the key */
  1075. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1076. /* Set the CRYP peripheral in AES CBC mode */
  1077. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC);
  1078. /* Set the Initialization Vector */
  1079. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1080. /* Flush FIFO */
  1081. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1082. /* Set the phase */
  1083. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1084. }
  1085. /* Enable Interrupts */
  1086. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1087. /* Enable CRYP */
  1088. __HAL_CRYP_ENABLE(hcryp);
  1089. /* Return function status */
  1090. return HAL_OK;
  1091. }
  1092. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1093. {
  1094. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1095. /* Write the Input block in the IN FIFO */
  1096. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1097. inputaddr+=4U;
  1098. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1099. inputaddr+=4U;
  1100. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1101. inputaddr+=4U;
  1102. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1103. hcryp->pCrypInBuffPtr += 16U;
  1104. hcryp->CrypInCount -= 16U;
  1105. if(hcryp->CrypInCount == 0U)
  1106. {
  1107. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1108. /* Call the Input data transfer complete callback */
  1109. HAL_CRYP_InCpltCallback(hcryp);
  1110. }
  1111. }
  1112. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1113. {
  1114. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1115. /* Read the Output block from the Output FIFO */
  1116. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1117. outputaddr+=4U;
  1118. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1119. outputaddr+=4U;
  1120. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1121. outputaddr+=4U;
  1122. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1123. hcryp->pCrypOutBuffPtr += 16U;
  1124. hcryp->CrypOutCount -= 16U;
  1125. if(hcryp->CrypOutCount == 0U)
  1126. {
  1127. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1128. /* Process Locked */
  1129. __HAL_UNLOCK(hcryp);
  1130. /* Change the CRYP state */
  1131. hcryp->State = HAL_CRYP_STATE_READY;
  1132. /* Call Input transfer complete callback */
  1133. HAL_CRYP_OutCpltCallback(hcryp);
  1134. }
  1135. }
  1136. /* Return function status */
  1137. return HAL_OK;
  1138. }
  1139. /**
  1140. * @brief Initializes the CRYP peripheral in AES CTR encryption mode using Interrupt.
  1141. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1142. * the configuration information for CRYP module
  1143. * @param pPlainData Pointer to the plaintext buffer
  1144. * @param Size Length of the plaintext buffer, must be a multiple of 16 bytes
  1145. * @param pCypherData Pointer to the cyphertext buffer
  1146. * @retval HAL status
  1147. */
  1148. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1149. {
  1150. uint32_t inputaddr;
  1151. uint32_t outputaddr;
  1152. if(hcryp->State == HAL_CRYP_STATE_READY)
  1153. {
  1154. /* Process Locked */
  1155. __HAL_LOCK(hcryp);
  1156. hcryp->CrypInCount = Size;
  1157. hcryp->pCrypInBuffPtr = pPlainData;
  1158. hcryp->pCrypOutBuffPtr = pCypherData;
  1159. hcryp->CrypOutCount = Size;
  1160. /* Change the CRYP state */
  1161. hcryp->State = HAL_CRYP_STATE_BUSY;
  1162. /* Check if initialization phase has already been performed */
  1163. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1164. {
  1165. /* Set the key */
  1166. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1167. /* Set the CRYP peripheral in AES CTR mode */
  1168. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR);
  1169. /* Set the Initialization Vector */
  1170. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1171. /* Flush FIFO */
  1172. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1173. /* Set the phase */
  1174. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1175. }
  1176. /* Enable Interrupts */
  1177. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1178. /* Enable CRYP */
  1179. __HAL_CRYP_ENABLE(hcryp);
  1180. /* Return function status */
  1181. return HAL_OK;
  1182. }
  1183. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1184. {
  1185. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1186. /* Write the Input block in the IN FIFO */
  1187. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1188. inputaddr+=4U;
  1189. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1190. inputaddr+=4U;
  1191. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1192. inputaddr+=4U;
  1193. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1194. hcryp->pCrypInBuffPtr += 16U;
  1195. hcryp->CrypInCount -= 16U;
  1196. if(hcryp->CrypInCount == 0U)
  1197. {
  1198. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1199. /* Call the Input data transfer complete callback */
  1200. HAL_CRYP_InCpltCallback(hcryp);
  1201. }
  1202. }
  1203. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1204. {
  1205. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1206. /* Read the Output block from the Output FIFO */
  1207. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1208. outputaddr+=4U;
  1209. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1210. outputaddr+=4U;
  1211. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1212. outputaddr+=4U;
  1213. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1214. hcryp->pCrypOutBuffPtr += 16U;
  1215. hcryp->CrypOutCount -= 16U;
  1216. if(hcryp->CrypOutCount == 0U)
  1217. {
  1218. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1219. /* Process Unlocked */
  1220. __HAL_UNLOCK(hcryp);
  1221. /* Change the CRYP state */
  1222. hcryp->State = HAL_CRYP_STATE_READY;
  1223. /* Call Input transfer complete callback */
  1224. HAL_CRYP_OutCpltCallback(hcryp);
  1225. }
  1226. }
  1227. /* Return function status */
  1228. return HAL_OK;
  1229. }
  1230. /**
  1231. * @brief Initializes the CRYP peripheral in AES ECB decryption mode using Interrupt.
  1232. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1233. * the configuration information for CRYP module
  1234. * @param pCypherData Pointer to the cyphertext buffer
  1235. * @param Size Length of the plaintext buffer, must be a multiple of 16.
  1236. * @param pPlainData Pointer to the plaintext buffer
  1237. * @retval HAL status
  1238. */
  1239. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1240. {
  1241. uint32_t tickstart = 0U;
  1242. uint32_t inputaddr;
  1243. uint32_t outputaddr;
  1244. if(hcryp->State == HAL_CRYP_STATE_READY)
  1245. {
  1246. /* Process Locked */
  1247. __HAL_LOCK(hcryp);
  1248. hcryp->CrypInCount = Size;
  1249. hcryp->pCrypInBuffPtr = pCypherData;
  1250. hcryp->pCrypOutBuffPtr = pPlainData;
  1251. hcryp->CrypOutCount = Size;
  1252. /* Change the CRYP state */
  1253. hcryp->State = HAL_CRYP_STATE_BUSY;
  1254. /* Check if initialization phase has already been performed */
  1255. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1256. {
  1257. /* Set the key */
  1258. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1259. /* Set the CRYP peripheral in AES Key mode */
  1260. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  1261. /* Enable CRYP */
  1262. __HAL_CRYP_ENABLE(hcryp);
  1263. /* Get tick */
  1264. tickstart = HAL_GetTick();
  1265. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  1266. {
  1267. /* Check for the Timeout */
  1268. if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)
  1269. {
  1270. /* Change state */
  1271. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1272. /* Process Unlocked */
  1273. __HAL_UNLOCK(hcryp);
  1274. return HAL_TIMEOUT;
  1275. }
  1276. }
  1277. /* Reset the ALGOMODE bits*/
  1278. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  1279. /* Set the CRYP peripheral in AES ECB decryption mode */
  1280. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR);
  1281. /* Flush FIFO */
  1282. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1283. /* Set the phase */
  1284. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1285. }
  1286. /* Enable Interrupts */
  1287. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1288. /* Enable CRYP */
  1289. __HAL_CRYP_ENABLE(hcryp);
  1290. /* Return function status */
  1291. return HAL_OK;
  1292. }
  1293. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1294. {
  1295. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1296. /* Write the Input block in the IN FIFO */
  1297. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1298. inputaddr+=4U;
  1299. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1300. inputaddr+=4U;
  1301. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1302. inputaddr+=4U;
  1303. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1304. hcryp->pCrypInBuffPtr += 16U;
  1305. hcryp->CrypInCount -= 16U;
  1306. if(hcryp->CrypInCount == 0U)
  1307. {
  1308. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1309. /* Call the Input data transfer complete callback */
  1310. HAL_CRYP_InCpltCallback(hcryp);
  1311. }
  1312. }
  1313. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1314. {
  1315. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1316. /* Read the Output block from the Output FIFO */
  1317. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1318. outputaddr+=4U;
  1319. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1320. outputaddr+=4U;
  1321. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1322. outputaddr+=4U;
  1323. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1324. hcryp->pCrypOutBuffPtr += 16U;
  1325. hcryp->CrypOutCount -= 16U;
  1326. if(hcryp->CrypOutCount == 0U)
  1327. {
  1328. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1329. /* Process Unlocked */
  1330. __HAL_UNLOCK(hcryp);
  1331. /* Change the CRYP state */
  1332. hcryp->State = HAL_CRYP_STATE_READY;
  1333. /* Call Input transfer complete callback */
  1334. HAL_CRYP_OutCpltCallback(hcryp);
  1335. }
  1336. }
  1337. /* Return function status */
  1338. return HAL_OK;
  1339. }
  1340. /**
  1341. * @brief Initializes the CRYP peripheral in AES CBC decryption mode using IT.
  1342. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1343. * the configuration information for CRYP module
  1344. * @param pCypherData Pointer to the cyphertext buffer
  1345. * @param Size Length of the plaintext buffer, must be a multiple of 16
  1346. * @param pPlainData Pointer to the plaintext buffer
  1347. * @retval HAL status
  1348. */
  1349. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1350. {
  1351. uint32_t tickstart = 0U;
  1352. uint32_t inputaddr;
  1353. uint32_t outputaddr;
  1354. if(hcryp->State == HAL_CRYP_STATE_READY)
  1355. {
  1356. /* Process Locked */
  1357. __HAL_LOCK(hcryp);
  1358. /* Get the buffer addresses and sizes */
  1359. hcryp->CrypInCount = Size;
  1360. hcryp->pCrypInBuffPtr = pCypherData;
  1361. hcryp->pCrypOutBuffPtr = pPlainData;
  1362. hcryp->CrypOutCount = Size;
  1363. /* Change the CRYP state */
  1364. hcryp->State = HAL_CRYP_STATE_BUSY;
  1365. /* Check if initialization phase has already been performed */
  1366. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1367. {
  1368. /* Set the key */
  1369. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1370. /* Set the CRYP peripheral in AES Key mode */
  1371. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  1372. /* Enable CRYP */
  1373. __HAL_CRYP_ENABLE(hcryp);
  1374. /* Get tick */
  1375. tickstart = HAL_GetTick();
  1376. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  1377. {
  1378. /* Check for the Timeout */
  1379. if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)
  1380. {
  1381. /* Change state */
  1382. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1383. /* Process Unlocked */
  1384. __HAL_UNLOCK(hcryp);
  1385. return HAL_TIMEOUT;
  1386. }
  1387. }
  1388. /* Reset the ALGOMODE bits*/
  1389. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  1390. /* Set the CRYP peripheral in AES CBC decryption mode */
  1391. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR);
  1392. /* Set the Initialization Vector */
  1393. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1394. /* Flush FIFO */
  1395. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1396. /* Enable CRYP */
  1397. __HAL_CRYP_ENABLE(hcryp);
  1398. /* Set the phase */
  1399. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1400. }
  1401. /* Enable Interrupts */
  1402. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1403. /* Enable CRYP */
  1404. __HAL_CRYP_ENABLE(hcryp);
  1405. /* Return function status */
  1406. return HAL_OK;
  1407. }
  1408. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1409. {
  1410. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1411. /* Write the Input block in the IN FIFO */
  1412. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1413. inputaddr+=4U;
  1414. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1415. inputaddr+=4U;
  1416. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1417. inputaddr+=4U;
  1418. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1419. hcryp->pCrypInBuffPtr += 16U;
  1420. hcryp->CrypInCount -= 16U;
  1421. if(hcryp->CrypInCount == 0U)
  1422. {
  1423. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1424. /* Call the Input data transfer complete callback */
  1425. HAL_CRYP_InCpltCallback(hcryp);
  1426. }
  1427. }
  1428. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1429. {
  1430. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1431. /* Read the Output block from the Output FIFO */
  1432. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1433. outputaddr+=4U;
  1434. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1435. outputaddr+=4U;
  1436. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1437. outputaddr+=4U;
  1438. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1439. hcryp->pCrypOutBuffPtr += 16U;
  1440. hcryp->CrypOutCount -= 16U;
  1441. if(hcryp->CrypOutCount == 0U)
  1442. {
  1443. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1444. /* Process Unlocked */
  1445. __HAL_UNLOCK(hcryp);
  1446. /* Change the CRYP state */
  1447. hcryp->State = HAL_CRYP_STATE_READY;
  1448. /* Call Input transfer complete callback */
  1449. HAL_CRYP_OutCpltCallback(hcryp);
  1450. }
  1451. }
  1452. /* Return function status */
  1453. return HAL_OK;
  1454. }
  1455. /**
  1456. * @brief Initializes the CRYP peripheral in AES CTR decryption mode using Interrupt.
  1457. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1458. * the configuration information for CRYP module
  1459. * @param pCypherData Pointer to the cyphertext buffer
  1460. * @param Size Length of the plaintext buffer, must be a multiple of 16
  1461. * @param pPlainData Pointer to the plaintext buffer
  1462. * @retval HAL status
  1463. */
  1464. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1465. {
  1466. uint32_t inputaddr;
  1467. uint32_t outputaddr;
  1468. if(hcryp->State == HAL_CRYP_STATE_READY)
  1469. {
  1470. /* Process Locked */
  1471. __HAL_LOCK(hcryp);
  1472. /* Get the buffer addresses and sizes */
  1473. hcryp->CrypInCount = Size;
  1474. hcryp->pCrypInBuffPtr = pCypherData;
  1475. hcryp->pCrypOutBuffPtr = pPlainData;
  1476. hcryp->CrypOutCount = Size;
  1477. /* Change the CRYP state */
  1478. hcryp->State = HAL_CRYP_STATE_BUSY;
  1479. /* Check if initialization phase has already been performed */
  1480. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1481. {
  1482. /* Set the key */
  1483. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1484. /* Set the CRYP peripheral in AES CTR mode */
  1485. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR);
  1486. /* Set the Initialization Vector */
  1487. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1488. /* Flush FIFO */
  1489. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1490. /* Set the phase */
  1491. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1492. }
  1493. /* Enable Interrupts */
  1494. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1495. /* Enable CRYP */
  1496. __HAL_CRYP_ENABLE(hcryp);
  1497. /* Return function status */
  1498. return HAL_OK;
  1499. }
  1500. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1501. {
  1502. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1503. /* Write the Input block in the IN FIFO */
  1504. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1505. inputaddr+=4U;
  1506. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1507. inputaddr+=4U;
  1508. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1509. inputaddr+=4U;
  1510. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1511. hcryp->pCrypInBuffPtr += 16U;
  1512. hcryp->CrypInCount -= 16U;
  1513. if(hcryp->CrypInCount == 0U)
  1514. {
  1515. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1516. /* Call the Input data transfer complete callback */
  1517. HAL_CRYP_InCpltCallback(hcryp);
  1518. }
  1519. }
  1520. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1521. {
  1522. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1523. /* Read the Output block from the Output FIFO */
  1524. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1525. outputaddr+=4U;
  1526. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1527. outputaddr+=4U;
  1528. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1529. outputaddr+=4U;
  1530. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1531. hcryp->pCrypOutBuffPtr += 16U;
  1532. hcryp->CrypOutCount -= 16U;
  1533. if(hcryp->CrypOutCount == 0U)
  1534. {
  1535. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1536. /* Process Unlocked */
  1537. __HAL_UNLOCK(hcryp);
  1538. /* Change the CRYP state */
  1539. hcryp->State = HAL_CRYP_STATE_READY;
  1540. /* Call Input transfer complete callback */
  1541. HAL_CRYP_OutCpltCallback(hcryp);
  1542. }
  1543. }
  1544. /* Return function status */
  1545. return HAL_OK;
  1546. }
  1547. /**
  1548. * @brief Initializes the CRYP peripheral in AES ECB encryption mode using DMA.
  1549. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1550. * the configuration information for CRYP module
  1551. * @param pPlainData Pointer to the plaintext buffer
  1552. * @param Size Length of the plaintext buffer, must be a multiple of 16 bytes
  1553. * @param pCypherData Pointer to the cyphertext buffer
  1554. * @retval HAL status
  1555. */
  1556. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1557. {
  1558. uint32_t inputaddr;
  1559. uint32_t outputaddr;
  1560. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1561. {
  1562. /* Process Locked */
  1563. __HAL_LOCK(hcryp);
  1564. inputaddr = (uint32_t)pPlainData;
  1565. outputaddr = (uint32_t)pCypherData;
  1566. /* Change the CRYP state */
  1567. hcryp->State = HAL_CRYP_STATE_BUSY;
  1568. /* Check if initialization phase has already been performed */
  1569. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1570. {
  1571. /* Set the key */
  1572. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1573. /* Set the CRYP peripheral in AES ECB mode */
  1574. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB);
  1575. /* Flush FIFO */
  1576. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1577. /* Set the phase */
  1578. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1579. }
  1580. /* Set the input and output addresses and start DMA transfer */
  1581. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1582. /* Process Unlocked */
  1583. __HAL_UNLOCK(hcryp);
  1584. /* Return function status */
  1585. return HAL_OK;
  1586. }
  1587. else
  1588. {
  1589. return HAL_ERROR;
  1590. }
  1591. }
  1592. /**
  1593. * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA.
  1594. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1595. * the configuration information for CRYP module
  1596. * @param pPlainData Pointer to the plaintext buffer
  1597. * @param Size Length of the plaintext buffer, must be a multiple of 16.
  1598. * @param pCypherData Pointer to the cyphertext buffer
  1599. * @retval HAL status
  1600. */
  1601. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1602. {
  1603. uint32_t inputaddr;
  1604. uint32_t outputaddr;
  1605. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1606. {
  1607. /* Process Locked */
  1608. __HAL_LOCK(hcryp);
  1609. inputaddr = (uint32_t)pPlainData;
  1610. outputaddr = (uint32_t)pCypherData;
  1611. /* Change the CRYP state */
  1612. hcryp->State = HAL_CRYP_STATE_BUSY;
  1613. /* Check if initialization phase has already been performed */
  1614. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1615. {
  1616. /* Set the key */
  1617. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1618. /* Set the CRYP peripheral in AES ECB mode */
  1619. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC);
  1620. /* Set the Initialization Vector */
  1621. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1622. /* Flush FIFO */
  1623. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1624. /* Set the phase */
  1625. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1626. }
  1627. /* Set the input and output addresses and start DMA transfer */
  1628. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1629. /* Process Unlocked */
  1630. __HAL_UNLOCK(hcryp);
  1631. /* Return function status */
  1632. return HAL_OK;
  1633. }
  1634. else
  1635. {
  1636. return HAL_ERROR;
  1637. }
  1638. }
  1639. /**
  1640. * @brief Initializes the CRYP peripheral in AES CTR encryption mode using DMA.
  1641. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1642. * the configuration information for CRYP module
  1643. * @param pPlainData Pointer to the plaintext buffer
  1644. * @param Size Length of the plaintext buffer, must be a multiple of 16.
  1645. * @param pCypherData Pointer to the cyphertext buffer
  1646. * @retval HAL status
  1647. */
  1648. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1649. {
  1650. uint32_t inputaddr;
  1651. uint32_t outputaddr;
  1652. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1653. {
  1654. /* Process Locked */
  1655. __HAL_LOCK(hcryp);
  1656. inputaddr = (uint32_t)pPlainData;
  1657. outputaddr = (uint32_t)pCypherData;
  1658. /* Change the CRYP state */
  1659. hcryp->State = HAL_CRYP_STATE_BUSY;
  1660. /* Check if initialization phase has already been performed */
  1661. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1662. {
  1663. /* Set the key */
  1664. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1665. /* Set the CRYP peripheral in AES ECB mode */
  1666. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR);
  1667. /* Set the Initialization Vector */
  1668. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1669. /* Flush FIFO */
  1670. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1671. /* Set the phase */
  1672. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1673. }
  1674. /* Set the input and output addresses and start DMA transfer */
  1675. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1676. /* Process Unlocked */
  1677. __HAL_UNLOCK(hcryp);
  1678. /* Return function status */
  1679. return HAL_OK;
  1680. }
  1681. else
  1682. {
  1683. return HAL_ERROR;
  1684. }
  1685. }
  1686. /**
  1687. * @brief Initializes the CRYP peripheral in AES ECB decryption mode using DMA.
  1688. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1689. * the configuration information for CRYP module
  1690. * @param pCypherData Pointer to the cyphertext buffer
  1691. * @param Size Length of the plaintext buffer, must be a multiple of 16 bytes
  1692. * @param pPlainData Pointer to the plaintext buffer
  1693. * @retval HAL status
  1694. */
  1695. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1696. {
  1697. uint32_t tickstart = 0U;
  1698. uint32_t inputaddr;
  1699. uint32_t outputaddr;
  1700. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1701. {
  1702. /* Process Locked */
  1703. __HAL_LOCK(hcryp);
  1704. inputaddr = (uint32_t)pCypherData;
  1705. outputaddr = (uint32_t)pPlainData;
  1706. /* Change the CRYP state */
  1707. hcryp->State = HAL_CRYP_STATE_BUSY;
  1708. /* Check if initialization phase has already been performed */
  1709. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1710. {
  1711. /* Set the key */
  1712. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1713. /* Set the CRYP peripheral in AES Key mode */
  1714. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  1715. /* Enable CRYP */
  1716. __HAL_CRYP_ENABLE(hcryp);
  1717. /* Get tick */
  1718. tickstart = HAL_GetTick();
  1719. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  1720. {
  1721. /* Check for the Timeout */
  1722. if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)
  1723. {
  1724. /* Change state */
  1725. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1726. /* Process Unlocked */
  1727. __HAL_UNLOCK(hcryp);
  1728. return HAL_TIMEOUT;
  1729. }
  1730. }
  1731. /* Reset the ALGOMODE bits*/
  1732. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  1733. /* Set the CRYP peripheral in AES ECB decryption mode */
  1734. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR);
  1735. /* Flush FIFO */
  1736. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1737. /* Set the phase */
  1738. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1739. }
  1740. /* Set the input and output addresses and start DMA transfer */
  1741. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1742. /* Process Unlocked */
  1743. __HAL_UNLOCK(hcryp);
  1744. /* Return function status */
  1745. return HAL_OK;
  1746. }
  1747. else
  1748. {
  1749. return HAL_ERROR;
  1750. }
  1751. }
  1752. /**
  1753. * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA.
  1754. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1755. * the configuration information for CRYP module
  1756. * @param pCypherData Pointer to the cyphertext buffer
  1757. * @param Size Length of the plaintext buffer, must be a multiple of 16 bytes
  1758. * @param pPlainData Pointer to the plaintext buffer
  1759. * @retval HAL status
  1760. */
  1761. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1762. {
  1763. uint32_t tickstart = 0U;
  1764. uint32_t inputaddr;
  1765. uint32_t outputaddr;
  1766. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1767. {
  1768. /* Process Locked */
  1769. __HAL_LOCK(hcryp);
  1770. inputaddr = (uint32_t)pCypherData;
  1771. outputaddr = (uint32_t)pPlainData;
  1772. /* Change the CRYP state */
  1773. hcryp->State = HAL_CRYP_STATE_BUSY;
  1774. /* Check if initialization phase has already been performed */
  1775. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1776. {
  1777. /* Set the key */
  1778. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1779. /* Set the CRYP peripheral in AES Key mode */
  1780. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  1781. /* Enable CRYP */
  1782. __HAL_CRYP_ENABLE(hcryp);
  1783. /* Get tick */
  1784. tickstart = HAL_GetTick();
  1785. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  1786. {
  1787. /* Check for the Timeout */
  1788. if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)
  1789. {
  1790. /* Change state */
  1791. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1792. /* Process Unlocked */
  1793. __HAL_UNLOCK(hcryp);
  1794. return HAL_TIMEOUT;
  1795. }
  1796. }
  1797. /* Reset the ALGOMODE bits*/
  1798. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  1799. /* Set the CRYP peripheral in AES CBC decryption mode */
  1800. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR);
  1801. /* Set the Initialization Vector */
  1802. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1803. /* Flush FIFO */
  1804. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1805. /* Set the phase */
  1806. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1807. }
  1808. /* Set the input and output addresses and start DMA transfer */
  1809. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1810. /* Process Unlocked */
  1811. __HAL_UNLOCK(hcryp);
  1812. /* Return function status */
  1813. return HAL_OK;
  1814. }
  1815. else
  1816. {
  1817. return HAL_ERROR;
  1818. }
  1819. }
  1820. /**
  1821. * @brief Initializes the CRYP peripheral in AES CTR decryption mode using DMA.
  1822. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1823. * the configuration information for CRYP module
  1824. * @param pCypherData Pointer to the cyphertext buffer
  1825. * @param Size Length of the plaintext buffer, must be a multiple of 16
  1826. * @param pPlainData Pointer to the plaintext buffer
  1827. * @retval HAL status
  1828. */
  1829. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1830. {
  1831. uint32_t inputaddr;
  1832. uint32_t outputaddr;
  1833. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1834. {
  1835. /* Process Locked */
  1836. __HAL_LOCK(hcryp);
  1837. inputaddr = (uint32_t)pCypherData;
  1838. outputaddr = (uint32_t)pPlainData;
  1839. /* Change the CRYP state */
  1840. hcryp->State = HAL_CRYP_STATE_BUSY;
  1841. /* Check if initialization phase has already been performed */
  1842. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1843. {
  1844. /* Set the key */
  1845. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1846. /* Set the CRYP peripheral in AES CTR mode */
  1847. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR);
  1848. /* Set the Initialization Vector */
  1849. CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
  1850. /* Flush FIFO */
  1851. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1852. /* Set the phase */
  1853. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1854. }
  1855. /* Set the input and output addresses and start DMA transfer */
  1856. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1857. /* Process Unlocked */
  1858. __HAL_UNLOCK(hcryp);
  1859. /* Return function status */
  1860. return HAL_OK;
  1861. }
  1862. else
  1863. {
  1864. return HAL_ERROR;
  1865. }
  1866. }
  1867. /**
  1868. * @}
  1869. */
  1870. /** @defgroup CRYP_Exported_Functions_Group3 DES processing functions
  1871. * @brief processing functions.
  1872. *
  1873. @verbatim
  1874. ==============================================================================
  1875. ##### DES processing functions #####
  1876. ==============================================================================
  1877. [..] This section provides functions allowing to:
  1878. (+) Encrypt plaintext using DES using ECB or CBC chaining modes
  1879. (+) Decrypt cyphertext using ECB or CBC chaining modes
  1880. [..] Three processing functions are available:
  1881. (+) Polling mode
  1882. (+) Interrupt mode
  1883. (+) DMA mode
  1884. @endverbatim
  1885. * @{
  1886. */
  1887. /**
  1888. * @brief Initializes the CRYP peripheral in DES ECB encryption mode.
  1889. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1890. * the configuration information for CRYP module
  1891. * @param pPlainData Pointer to the plaintext buffer
  1892. * @param Size Length of the plaintext buffer, must be a multiple of 8
  1893. * @param pCypherData Pointer to the cyphertext buffer
  1894. * @param Timeout Specify Timeout value
  1895. * @retval HAL status
  1896. */
  1897. HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  1898. {
  1899. /* Process Locked */
  1900. __HAL_LOCK(hcryp);
  1901. /* Change the CRYP state */
  1902. hcryp->State = HAL_CRYP_STATE_BUSY;
  1903. /* Set CRYP peripheral in DES ECB encryption mode */
  1904. CRYP_SetDESECBMode(hcryp, 0U);
  1905. /* Enable CRYP */
  1906. __HAL_CRYP_ENABLE(hcryp);
  1907. /* Write Plain Data and Get Cypher Data */
  1908. if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  1909. {
  1910. return HAL_TIMEOUT;
  1911. }
  1912. /* Change the CRYP state */
  1913. hcryp->State = HAL_CRYP_STATE_READY;
  1914. /* Process Unlocked */
  1915. __HAL_UNLOCK(hcryp);
  1916. /* Return function status */
  1917. return HAL_OK;
  1918. }
  1919. /**
  1920. * @brief Initializes the CRYP peripheral in DES ECB decryption mode.
  1921. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1922. * the configuration information for CRYP module
  1923. * @param pCypherData Pointer to the cyphertext buffer
  1924. * @param Size Length of the plaintext buffer, must be a multiple of 8
  1925. * @param pPlainData Pointer to the plaintext buffer
  1926. * @param Timeout Specify Timeout value
  1927. * @retval HAL status
  1928. */
  1929. HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  1930. {
  1931. /* Process Locked */
  1932. __HAL_LOCK(hcryp);
  1933. /* Change the CRYP state */
  1934. hcryp->State = HAL_CRYP_STATE_BUSY;
  1935. /* Set CRYP peripheral in DES ECB decryption mode */
  1936. CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR);
  1937. /* Enable CRYP */
  1938. __HAL_CRYP_ENABLE(hcryp);
  1939. /* Write Plain Data and Get Cypher Data */
  1940. if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  1941. {
  1942. return HAL_TIMEOUT;
  1943. }
  1944. /* Change the CRYP state */
  1945. hcryp->State = HAL_CRYP_STATE_READY;
  1946. /* Process Unlocked */
  1947. __HAL_UNLOCK(hcryp);
  1948. /* Return function status */
  1949. return HAL_OK;
  1950. }
  1951. /**
  1952. * @brief Initializes the CRYP peripheral in DES CBC encryption mode.
  1953. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1954. * the configuration information for CRYP module
  1955. * @param pPlainData Pointer to the plaintext buffer
  1956. * @param Size Length of the plaintext buffer, must be a multiple of 8
  1957. * @param pCypherData Pointer to the cyphertext buffer
  1958. * @param Timeout Specify Timeout value
  1959. * @retval HAL status
  1960. */
  1961. HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  1962. {
  1963. /* Process Locked */
  1964. __HAL_LOCK(hcryp);
  1965. /* Change the CRYP state */
  1966. hcryp->State = HAL_CRYP_STATE_BUSY;
  1967. /* Set CRYP peripheral in DES CBC encryption mode */
  1968. CRYP_SetDESCBCMode(hcryp, 0U);
  1969. /* Enable CRYP */
  1970. __HAL_CRYP_ENABLE(hcryp);
  1971. /* Write Plain Data and Get Cypher Data */
  1972. if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  1973. {
  1974. return HAL_TIMEOUT;
  1975. }
  1976. /* Change the CRYP state */
  1977. hcryp->State = HAL_CRYP_STATE_READY;
  1978. /* Process Unlocked */
  1979. __HAL_UNLOCK(hcryp);
  1980. /* Return function status */
  1981. return HAL_OK;
  1982. }
  1983. /**
  1984. * @brief Initializes the CRYP peripheral in DES ECB decryption mode.
  1985. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1986. * the configuration information for CRYP module
  1987. * @param pCypherData Pointer to the cyphertext buffer
  1988. * @param Size Length of the plaintext buffer, must be a multiple of 8
  1989. * @param pPlainData Pointer to the plaintext buffer
  1990. * @param Timeout Specify Timeout value
  1991. * @retval HAL status
  1992. */
  1993. HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  1994. {
  1995. /* Process Locked */
  1996. __HAL_LOCK(hcryp);
  1997. /* Change the CRYP state */
  1998. hcryp->State = HAL_CRYP_STATE_BUSY;
  1999. /* Set CRYP peripheral in DES CBC decryption mode */
  2000. CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR);
  2001. /* Enable CRYP */
  2002. __HAL_CRYP_ENABLE(hcryp);
  2003. /* Write Plain Data and Get Cypher Data */
  2004. if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  2005. {
  2006. return HAL_TIMEOUT;
  2007. }
  2008. /* Change the CRYP state */
  2009. hcryp->State = HAL_CRYP_STATE_READY;
  2010. /* Process Unlocked */
  2011. __HAL_UNLOCK(hcryp);
  2012. /* Return function status */
  2013. return HAL_OK;
  2014. }
  2015. /**
  2016. * @brief Initializes the CRYP peripheral in DES ECB encryption mode using IT.
  2017. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2018. * the configuration information for CRYP module
  2019. * @param pPlainData Pointer to the plaintext buffer
  2020. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2021. * @param pCypherData Pointer to the cyphertext buffer
  2022. * @retval HAL status
  2023. */
  2024. HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2025. {
  2026. uint32_t inputaddr;
  2027. uint32_t outputaddr;
  2028. if(hcryp->State == HAL_CRYP_STATE_READY)
  2029. {
  2030. /* Process Locked */
  2031. __HAL_LOCK(hcryp);
  2032. hcryp->CrypInCount = Size;
  2033. hcryp->pCrypInBuffPtr = pPlainData;
  2034. hcryp->pCrypOutBuffPtr = pCypherData;
  2035. hcryp->CrypOutCount = Size;
  2036. /* Change the CRYP state */
  2037. hcryp->State = HAL_CRYP_STATE_BUSY;
  2038. /* Set CRYP peripheral in DES ECB encryption mode */
  2039. CRYP_SetDESECBMode(hcryp, 0U);
  2040. /* Enable Interrupts */
  2041. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2042. /* Enable CRYP */
  2043. __HAL_CRYP_ENABLE(hcryp);
  2044. /* Return function status */
  2045. return HAL_OK;
  2046. }
  2047. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2048. {
  2049. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2050. /* Write the Input block in the IN FIFO */
  2051. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2052. inputaddr+=4U;
  2053. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2054. hcryp->pCrypInBuffPtr += 8U;
  2055. hcryp->CrypInCount -= 8U;
  2056. if(hcryp->CrypInCount == 0U)
  2057. {
  2058. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2059. /* Call the Input data transfer complete callback */
  2060. HAL_CRYP_InCpltCallback(hcryp);
  2061. }
  2062. }
  2063. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2064. {
  2065. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2066. /* Read the Output block from the Output FIFO */
  2067. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2068. outputaddr+=4U;
  2069. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2070. hcryp->pCrypOutBuffPtr += 8U;
  2071. hcryp->CrypOutCount -= 8U;
  2072. if(hcryp->CrypOutCount == 0U)
  2073. {
  2074. /* Disable IT */
  2075. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2076. /* Disable CRYP */
  2077. __HAL_CRYP_DISABLE(hcryp);
  2078. /* Process Unlocked */
  2079. __HAL_UNLOCK(hcryp);
  2080. /* Change the CRYP state */
  2081. hcryp->State = HAL_CRYP_STATE_READY;
  2082. /* Call Input transfer complete callback */
  2083. HAL_CRYP_OutCpltCallback(hcryp);
  2084. }
  2085. }
  2086. /* Return function status */
  2087. return HAL_OK;
  2088. }
  2089. /**
  2090. * @brief Initializes the CRYP peripheral in DES CBC encryption mode using interrupt.
  2091. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2092. * the configuration information for CRYP module
  2093. * @param pPlainData Pointer to the plaintext buffer
  2094. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2095. * @param pCypherData Pointer to the cyphertext buffer
  2096. * @retval HAL status
  2097. */
  2098. HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2099. {
  2100. uint32_t inputaddr;
  2101. uint32_t outputaddr;
  2102. if(hcryp->State == HAL_CRYP_STATE_READY)
  2103. {
  2104. /* Process Locked */
  2105. __HAL_LOCK(hcryp);
  2106. hcryp->CrypInCount = Size;
  2107. hcryp->pCrypInBuffPtr = pPlainData;
  2108. hcryp->pCrypOutBuffPtr = pCypherData;
  2109. hcryp->CrypOutCount = Size;
  2110. /* Change the CRYP state */
  2111. hcryp->State = HAL_CRYP_STATE_BUSY;
  2112. /* Set CRYP peripheral in DES CBC encryption mode */
  2113. CRYP_SetDESCBCMode(hcryp, 0U);
  2114. /* Enable Interrupts */
  2115. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2116. /* Enable CRYP */
  2117. __HAL_CRYP_ENABLE(hcryp);
  2118. /* Return function status */
  2119. return HAL_OK;
  2120. }
  2121. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2122. {
  2123. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2124. /* Write the Input block in the IN FIFO */
  2125. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2126. inputaddr+=4U;
  2127. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2128. hcryp->pCrypInBuffPtr += 8U;
  2129. hcryp->CrypInCount -= 8U;
  2130. if(hcryp->CrypInCount == 0U)
  2131. {
  2132. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2133. /* Call the Input data transfer complete callback */
  2134. HAL_CRYP_InCpltCallback(hcryp);
  2135. }
  2136. }
  2137. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2138. {
  2139. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2140. /* Read the Output block from the Output FIFO */
  2141. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2142. outputaddr+=4U;
  2143. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2144. hcryp->pCrypOutBuffPtr += 8U;
  2145. hcryp->CrypOutCount -= 8U;
  2146. if(hcryp->CrypOutCount == 0U)
  2147. {
  2148. /* Disable IT */
  2149. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2150. /* Disable CRYP */
  2151. __HAL_CRYP_DISABLE(hcryp);
  2152. /* Process Unlocked */
  2153. __HAL_UNLOCK(hcryp);
  2154. /* Change the CRYP state */
  2155. hcryp->State = HAL_CRYP_STATE_READY;
  2156. /* Call Input transfer complete callback */
  2157. HAL_CRYP_OutCpltCallback(hcryp);
  2158. }
  2159. }
  2160. /* Return function status */
  2161. return HAL_OK;
  2162. }
  2163. /**
  2164. * @brief Initializes the CRYP peripheral in DES ECB decryption mode using IT.
  2165. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2166. * the configuration information for CRYP module
  2167. * @param pPlainData Pointer to the plaintext buffer
  2168. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2169. * @param pCypherData Pointer to the cyphertext buffer
  2170. * @retval HAL status
  2171. */
  2172. HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2173. {
  2174. uint32_t inputaddr;
  2175. uint32_t outputaddr;
  2176. if(hcryp->State == HAL_CRYP_STATE_READY)
  2177. {
  2178. /* Process Locked */
  2179. __HAL_LOCK(hcryp);
  2180. hcryp->CrypInCount = Size;
  2181. hcryp->pCrypInBuffPtr = pCypherData;
  2182. hcryp->pCrypOutBuffPtr = pPlainData;
  2183. hcryp->CrypOutCount = Size;
  2184. /* Change the CRYP state */
  2185. hcryp->State = HAL_CRYP_STATE_BUSY;
  2186. /* Set CRYP peripheral in DES ECB decryption mode */
  2187. CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR);
  2188. /* Enable Interrupts */
  2189. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2190. /* Enable CRYP */
  2191. __HAL_CRYP_ENABLE(hcryp);
  2192. /* Return function status */
  2193. return HAL_OK;
  2194. }
  2195. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2196. {
  2197. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2198. /* Write the Input block in the IN FIFO */
  2199. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2200. inputaddr+=4U;
  2201. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2202. hcryp->pCrypInBuffPtr += 8U;
  2203. hcryp->CrypInCount -= 8U;
  2204. if(hcryp->CrypInCount == 0U)
  2205. {
  2206. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2207. /* Call the Input data transfer complete callback */
  2208. HAL_CRYP_InCpltCallback(hcryp);
  2209. }
  2210. }
  2211. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2212. {
  2213. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2214. /* Read the Output block from the Output FIFO */
  2215. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2216. outputaddr+=4U;
  2217. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2218. hcryp->pCrypOutBuffPtr += 8U;
  2219. hcryp->CrypOutCount -= 8U;
  2220. if(hcryp->CrypOutCount == 0U)
  2221. {
  2222. /* Disable IT */
  2223. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2224. /* Disable CRYP */
  2225. __HAL_CRYP_DISABLE(hcryp);
  2226. /* Process Unlocked */
  2227. __HAL_UNLOCK(hcryp);
  2228. /* Change the CRYP state */
  2229. hcryp->State = HAL_CRYP_STATE_READY;
  2230. /* Call Input transfer complete callback */
  2231. HAL_CRYP_OutCpltCallback(hcryp);
  2232. }
  2233. }
  2234. /* Return function status */
  2235. return HAL_OK;
  2236. }
  2237. /**
  2238. * @brief Initializes the CRYP peripheral in DES ECB decryption mode using interrupt.
  2239. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2240. * the configuration information for CRYP module
  2241. * @param pPlainData Pointer to the plaintext buffer
  2242. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2243. * @param pCypherData Pointer to the cyphertext buffer
  2244. * @retval HAL status
  2245. */
  2246. HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2247. {
  2248. uint32_t inputaddr;
  2249. uint32_t outputaddr;
  2250. if(hcryp->State == HAL_CRYP_STATE_READY)
  2251. {
  2252. /* Process Locked */
  2253. __HAL_LOCK(hcryp);
  2254. hcryp->CrypInCount = Size;
  2255. hcryp->pCrypInBuffPtr = pCypherData;
  2256. hcryp->pCrypOutBuffPtr = pPlainData;
  2257. hcryp->CrypOutCount = Size;
  2258. /* Change the CRYP state */
  2259. hcryp->State = HAL_CRYP_STATE_BUSY;
  2260. /* Set CRYP peripheral in DES CBC decryption mode */
  2261. CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR);
  2262. /* Enable Interrupts */
  2263. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2264. /* Enable CRYP */
  2265. __HAL_CRYP_ENABLE(hcryp);
  2266. /* Return function status */
  2267. return HAL_OK;
  2268. }
  2269. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2270. {
  2271. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2272. /* Write the Input block in the IN FIFO */
  2273. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2274. inputaddr+=4U;
  2275. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2276. hcryp->pCrypInBuffPtr += 8U;
  2277. hcryp->CrypInCount -= 8U;
  2278. if(hcryp->CrypInCount == 0U)
  2279. {
  2280. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2281. /* Call the Input data transfer complete callback */
  2282. HAL_CRYP_InCpltCallback(hcryp);
  2283. }
  2284. }
  2285. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2286. {
  2287. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2288. /* Read the Output block from the Output FIFO */
  2289. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2290. outputaddr+=4U;
  2291. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2292. hcryp->pCrypOutBuffPtr += 8U;
  2293. hcryp->CrypOutCount -= 8U;
  2294. if(hcryp->CrypOutCount == 0U)
  2295. {
  2296. /* Disable IT */
  2297. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2298. /* Disable CRYP */
  2299. __HAL_CRYP_DISABLE(hcryp);
  2300. /* Process Unlocked */
  2301. __HAL_UNLOCK(hcryp);
  2302. /* Change the CRYP state */
  2303. hcryp->State = HAL_CRYP_STATE_READY;
  2304. /* Call Input transfer complete callback */
  2305. HAL_CRYP_OutCpltCallback(hcryp);
  2306. }
  2307. }
  2308. /* Return function status */
  2309. return HAL_OK;
  2310. }
  2311. /**
  2312. * @brief Initializes the CRYP peripheral in DES ECB encryption mode using DMA.
  2313. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2314. * the configuration information for CRYP module
  2315. * @param pPlainData Pointer to the plaintext buffer
  2316. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2317. * @param pCypherData Pointer to the cyphertext buffer
  2318. * @retval HAL status
  2319. */
  2320. HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2321. {
  2322. uint32_t inputaddr;
  2323. uint32_t outputaddr;
  2324. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2325. {
  2326. /* Process Locked */
  2327. __HAL_LOCK(hcryp);
  2328. inputaddr = (uint32_t)pPlainData;
  2329. outputaddr = (uint32_t)pCypherData;
  2330. /* Change the CRYP state */
  2331. hcryp->State = HAL_CRYP_STATE_BUSY;
  2332. /* Set CRYP peripheral in DES ECB encryption mode */
  2333. CRYP_SetDESECBMode(hcryp, 0U);
  2334. /* Set the input and output addresses and start DMA transfer */
  2335. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2336. /* Process Unlocked */
  2337. __HAL_UNLOCK(hcryp);
  2338. /* Return function status */
  2339. return HAL_OK;
  2340. }
  2341. else
  2342. {
  2343. return HAL_ERROR;
  2344. }
  2345. }
  2346. /**
  2347. * @brief Initializes the CRYP peripheral in DES CBC encryption mode using DMA.
  2348. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2349. * the configuration information for CRYP module
  2350. * @param pPlainData Pointer to the plaintext buffer
  2351. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2352. * @param pCypherData Pointer to the cyphertext buffer
  2353. * @retval HAL status
  2354. */
  2355. HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2356. {
  2357. uint32_t inputaddr;
  2358. uint32_t outputaddr;
  2359. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2360. {
  2361. /* Process Locked */
  2362. __HAL_LOCK(hcryp);
  2363. inputaddr = (uint32_t)pPlainData;
  2364. outputaddr = (uint32_t)pCypherData;
  2365. /* Change the CRYP state */
  2366. hcryp->State = HAL_CRYP_STATE_BUSY;
  2367. /* Set CRYP peripheral in DES CBC encryption mode */
  2368. CRYP_SetDESCBCMode(hcryp, 0U);
  2369. /* Set the input and output addresses and start DMA transfer */
  2370. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2371. /* Process Unlocked */
  2372. __HAL_UNLOCK(hcryp);
  2373. /* Return function status */
  2374. return HAL_OK;
  2375. }
  2376. else
  2377. {
  2378. return HAL_ERROR;
  2379. }
  2380. }
  2381. /**
  2382. * @brief Initializes the CRYP peripheral in DES ECB decryption mode using DMA.
  2383. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2384. * the configuration information for CRYP module
  2385. * @param pPlainData Pointer to the plaintext buffer
  2386. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2387. * @param pCypherData Pointer to the cyphertext buffer
  2388. * @retval HAL status
  2389. */
  2390. HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2391. {
  2392. uint32_t inputaddr;
  2393. uint32_t outputaddr;
  2394. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2395. {
  2396. /* Process Locked */
  2397. __HAL_LOCK(hcryp);
  2398. inputaddr = (uint32_t)pCypherData;
  2399. outputaddr = (uint32_t)pPlainData;
  2400. /* Change the CRYP state */
  2401. hcryp->State = HAL_CRYP_STATE_BUSY;
  2402. /* Set CRYP peripheral in DES ECB decryption mode */
  2403. CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR);
  2404. /* Set the input and output addresses and start DMA transfer */
  2405. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2406. /* Process Unlocked */
  2407. __HAL_UNLOCK(hcryp);
  2408. /* Return function status */
  2409. return HAL_OK;
  2410. }
  2411. else
  2412. {
  2413. return HAL_ERROR;
  2414. }
  2415. }
  2416. /**
  2417. * @brief Initializes the CRYP peripheral in DES ECB decryption mode using DMA.
  2418. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2419. * the configuration information for CRYP module
  2420. * @param pPlainData Pointer to the plaintext buffer
  2421. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2422. * @param pCypherData Pointer to the cyphertext buffer
  2423. * @retval HAL status
  2424. */
  2425. HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2426. {
  2427. uint32_t inputaddr;
  2428. uint32_t outputaddr;
  2429. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2430. {
  2431. /* Process Locked */
  2432. __HAL_LOCK(hcryp);
  2433. inputaddr = (uint32_t)pCypherData;
  2434. outputaddr = (uint32_t)pPlainData;
  2435. /* Change the CRYP state */
  2436. hcryp->State = HAL_CRYP_STATE_BUSY;
  2437. /* Set CRYP peripheral in DES CBC decryption mode */
  2438. CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR);
  2439. /* Set the input and output addresses and start DMA transfer */
  2440. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2441. /* Process Unlocked */
  2442. __HAL_UNLOCK(hcryp);
  2443. /* Return function status */
  2444. return HAL_OK;
  2445. }
  2446. else
  2447. {
  2448. return HAL_ERROR;
  2449. }
  2450. }
  2451. /**
  2452. * @}
  2453. */
  2454. /** @defgroup CRYP_Exported_Functions_Group4 TDES processing functions
  2455. * @brief processing functions.
  2456. *
  2457. @verbatim
  2458. ==============================================================================
  2459. ##### TDES processing functions #####
  2460. ==============================================================================
  2461. [..] This section provides functions allowing to:
  2462. (+) Encrypt plaintext using TDES based on ECB or CBC chaining modes
  2463. (+) Decrypt cyphertext using TDES based on ECB or CBC chaining modes
  2464. [..] Three processing functions are available:
  2465. (+) Polling mode
  2466. (+) Interrupt mode
  2467. (+) DMA mode
  2468. @endverbatim
  2469. * @{
  2470. */
  2471. /**
  2472. * @brief Initializes the CRYP peripheral in TDES ECB encryption mode
  2473. * then encrypt pPlainData. The cypher data are available in pCypherData
  2474. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2475. * the configuration information for CRYP module
  2476. * @param pPlainData Pointer to the plaintext buffer
  2477. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2478. * @param pCypherData Pointer to the cyphertext buffer
  2479. * @param Timeout Specify Timeout value
  2480. * @retval HAL status
  2481. */
  2482. HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  2483. {
  2484. /* Process Locked */
  2485. __HAL_LOCK(hcryp);
  2486. /* Change the CRYP state */
  2487. hcryp->State = HAL_CRYP_STATE_BUSY;
  2488. /* Set CRYP peripheral in TDES ECB encryption mode */
  2489. CRYP_SetTDESECBMode(hcryp, 0U);
  2490. /* Enable CRYP */
  2491. __HAL_CRYP_ENABLE(hcryp);
  2492. /* Write Plain Data and Get Cypher Data */
  2493. if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  2494. {
  2495. return HAL_TIMEOUT;
  2496. }
  2497. /* Change the CRYP state */
  2498. hcryp->State = HAL_CRYP_STATE_READY;
  2499. /* Process Unlocked */
  2500. __HAL_UNLOCK(hcryp);
  2501. /* Return function status */
  2502. return HAL_OK;
  2503. }
  2504. /**
  2505. * @brief Initializes the CRYP peripheral in TDES ECB decryption mode
  2506. * then decrypted pCypherData. The cypher data are available in pPlainData
  2507. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2508. * the configuration information for CRYP module
  2509. * @param pPlainData Pointer to the plaintext buffer
  2510. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2511. * @param pCypherData Pointer to the cyphertext buffer
  2512. * @param Timeout Specify Timeout value
  2513. * @retval HAL status
  2514. */
  2515. HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  2516. {
  2517. /* Process Locked */
  2518. __HAL_LOCK(hcryp);
  2519. /* Change the CRYP state */
  2520. hcryp->State = HAL_CRYP_STATE_BUSY;
  2521. /* Set CRYP peripheral in TDES ECB decryption mode */
  2522. CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR);
  2523. /* Enable CRYP */
  2524. __HAL_CRYP_ENABLE(hcryp);
  2525. /* Write Cypher Data and Get Plain Data */
  2526. if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  2527. {
  2528. return HAL_TIMEOUT;
  2529. }
  2530. /* Change the CRYP state */
  2531. hcryp->State = HAL_CRYP_STATE_READY;
  2532. /* Process Unlocked */
  2533. __HAL_UNLOCK(hcryp);
  2534. /* Return function status */
  2535. return HAL_OK;
  2536. }
  2537. /**
  2538. * @brief Initializes the CRYP peripheral in TDES CBC encryption mode
  2539. * then encrypt pPlainData. The cypher data are available in pCypherData
  2540. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2541. * the configuration information for CRYP module
  2542. * @param pPlainData Pointer to the plaintext buffer
  2543. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2544. * @param pCypherData Pointer to the cyphertext buffer
  2545. * @param Timeout Specify Timeout value
  2546. * @retval HAL status
  2547. */
  2548. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  2549. {
  2550. /* Process Locked */
  2551. __HAL_LOCK(hcryp);
  2552. /* Change the CRYP state */
  2553. hcryp->State = HAL_CRYP_STATE_BUSY;
  2554. /* Set CRYP peripheral in TDES CBC encryption mode */
  2555. CRYP_SetTDESCBCMode(hcryp, 0U);
  2556. /* Enable CRYP */
  2557. __HAL_CRYP_ENABLE(hcryp);
  2558. /* Write Plain Data and Get Cypher Data */
  2559. if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  2560. {
  2561. return HAL_TIMEOUT;
  2562. }
  2563. /* Change the CRYP state */
  2564. hcryp->State = HAL_CRYP_STATE_READY;
  2565. /* Process Unlocked */
  2566. __HAL_UNLOCK(hcryp);
  2567. /* Return function status */
  2568. return HAL_OK;
  2569. }
  2570. /**
  2571. * @brief Initializes the CRYP peripheral in TDES CBC decryption mode
  2572. * then decrypted pCypherData. The cypher data are available in pPlainData
  2573. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2574. * the configuration information for CRYP module
  2575. * @param pCypherData Pointer to the cyphertext buffer
  2576. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2577. * @param pPlainData Pointer to the plaintext buffer
  2578. * @param Timeout Specify Timeout value
  2579. * @retval HAL status
  2580. */
  2581. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  2582. {
  2583. /* Process Locked */
  2584. __HAL_LOCK(hcryp);
  2585. /* Change the CRYP state */
  2586. hcryp->State = HAL_CRYP_STATE_BUSY;
  2587. /* Set CRYP peripheral in TDES CBC decryption mode */
  2588. CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR);
  2589. /* Enable CRYP */
  2590. __HAL_CRYP_ENABLE(hcryp);
  2591. /* Write Cypher Data and Get Plain Data */
  2592. if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  2593. {
  2594. return HAL_TIMEOUT;
  2595. }
  2596. /* Change the CRYP state */
  2597. hcryp->State = HAL_CRYP_STATE_READY;
  2598. /* Process Unlocked */
  2599. __HAL_UNLOCK(hcryp);
  2600. /* Return function status */
  2601. return HAL_OK;
  2602. }
  2603. /**
  2604. * @brief Initializes the CRYP peripheral in TDES ECB encryption mode using interrupt.
  2605. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2606. * the configuration information for CRYP module
  2607. * @param pPlainData Pointer to the plaintext buffer
  2608. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2609. * @param pCypherData Pointer to the cyphertext buffer
  2610. * @retval HAL status
  2611. */
  2612. HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2613. {
  2614. uint32_t inputaddr;
  2615. uint32_t outputaddr;
  2616. if(hcryp->State == HAL_CRYP_STATE_READY)
  2617. {
  2618. /* Process Locked */
  2619. __HAL_LOCK(hcryp);
  2620. hcryp->CrypInCount = Size;
  2621. hcryp->pCrypInBuffPtr = pPlainData;
  2622. hcryp->pCrypOutBuffPtr = pCypherData;
  2623. hcryp->CrypOutCount = Size;
  2624. /* Change the CRYP state */
  2625. hcryp->State = HAL_CRYP_STATE_BUSY;
  2626. /* Set CRYP peripheral in TDES ECB encryption mode */
  2627. CRYP_SetTDESECBMode(hcryp, 0U);
  2628. /* Enable Interrupts */
  2629. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2630. /* Enable CRYP */
  2631. __HAL_CRYP_ENABLE(hcryp);
  2632. /* Return function status */
  2633. return HAL_OK;
  2634. }
  2635. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2636. {
  2637. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2638. /* Write the Input block in the IN FIFO */
  2639. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2640. inputaddr+=4U;
  2641. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2642. hcryp->pCrypInBuffPtr += 8U;
  2643. hcryp->CrypInCount -= 8U;
  2644. if(hcryp->CrypInCount == 0U)
  2645. {
  2646. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2647. /* Call the Input data transfer complete callback */
  2648. HAL_CRYP_InCpltCallback(hcryp);
  2649. }
  2650. }
  2651. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2652. {
  2653. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2654. /* Read the Output block from the Output FIFO */
  2655. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2656. outputaddr+=4U;
  2657. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2658. hcryp->pCrypOutBuffPtr += 8U;
  2659. hcryp->CrypOutCount -= 8U;
  2660. if(hcryp->CrypOutCount == 0U)
  2661. {
  2662. /* Disable IT */
  2663. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2664. /* Disable CRYP */
  2665. __HAL_CRYP_DISABLE(hcryp);
  2666. /* Process Unlocked */
  2667. __HAL_UNLOCK(hcryp);
  2668. /* Change the CRYP state */
  2669. hcryp->State = HAL_CRYP_STATE_READY;
  2670. /* Call the Output data transfer complete callback */
  2671. HAL_CRYP_OutCpltCallback(hcryp);
  2672. }
  2673. }
  2674. /* Return function status */
  2675. return HAL_OK;
  2676. }
  2677. /**
  2678. * @brief Initializes the CRYP peripheral in TDES CBC encryption mode.
  2679. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2680. * the configuration information for CRYP module
  2681. * @param pPlainData Pointer to the plaintext buffer
  2682. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2683. * @param pCypherData Pointer to the cyphertext buffer
  2684. * @retval HAL status
  2685. */
  2686. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2687. {
  2688. uint32_t inputaddr;
  2689. uint32_t outputaddr;
  2690. if(hcryp->State == HAL_CRYP_STATE_READY)
  2691. {
  2692. /* Process Locked */
  2693. __HAL_LOCK(hcryp);
  2694. hcryp->CrypInCount = Size;
  2695. hcryp->pCrypInBuffPtr = pPlainData;
  2696. hcryp->pCrypOutBuffPtr = pCypherData;
  2697. hcryp->CrypOutCount = Size;
  2698. /* Change the CRYP state */
  2699. hcryp->State = HAL_CRYP_STATE_BUSY;
  2700. /* Set CRYP peripheral in TDES CBC encryption mode */
  2701. CRYP_SetTDESCBCMode(hcryp, 0U);
  2702. /* Enable Interrupts */
  2703. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2704. /* Enable CRYP */
  2705. __HAL_CRYP_ENABLE(hcryp);
  2706. /* Return function status */
  2707. return HAL_OK;
  2708. }
  2709. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2710. {
  2711. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2712. /* Write the Input block in the IN FIFO */
  2713. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2714. inputaddr+=4U;
  2715. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2716. hcryp->pCrypInBuffPtr += 8U;
  2717. hcryp->CrypInCount -= 8U;
  2718. if(hcryp->CrypInCount == 0U)
  2719. {
  2720. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2721. /* Call the Input data transfer complete callback */
  2722. HAL_CRYP_InCpltCallback(hcryp);
  2723. }
  2724. }
  2725. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2726. {
  2727. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2728. /* Read the Output block from the Output FIFO */
  2729. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2730. outputaddr+=4U;
  2731. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2732. hcryp->pCrypOutBuffPtr += 8U;
  2733. hcryp->CrypOutCount -= 8U;
  2734. if(hcryp->CrypOutCount == 0U)
  2735. {
  2736. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2737. /* Disable CRYP */
  2738. __HAL_CRYP_DISABLE(hcryp);
  2739. /* Process Unlocked */
  2740. __HAL_UNLOCK(hcryp);
  2741. /* Change the CRYP state */
  2742. hcryp->State = HAL_CRYP_STATE_READY;
  2743. /* Call Input transfer complete callback */
  2744. HAL_CRYP_OutCpltCallback(hcryp);
  2745. }
  2746. }
  2747. /* Return function status */
  2748. return HAL_OK;
  2749. }
  2750. /**
  2751. * @brief Initializes the CRYP peripheral in TDES ECB decryption mode.
  2752. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2753. * the configuration information for CRYP module
  2754. * @param pPlainData Pointer to the plaintext buffer
  2755. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2756. * @param pCypherData Pointer to the cyphertext buffer
  2757. * @retval HAL status
  2758. */
  2759. HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2760. {
  2761. uint32_t inputaddr;
  2762. uint32_t outputaddr;
  2763. if(hcryp->State == HAL_CRYP_STATE_READY)
  2764. {
  2765. /* Process Locked */
  2766. __HAL_LOCK(hcryp);
  2767. hcryp->CrypInCount = Size;
  2768. hcryp->pCrypInBuffPtr = pCypherData;
  2769. hcryp->pCrypOutBuffPtr = pPlainData;
  2770. hcryp->CrypOutCount = Size;
  2771. /* Change the CRYP state */
  2772. hcryp->State = HAL_CRYP_STATE_BUSY;
  2773. /* Set CRYP peripheral in TDES ECB decryption mode */
  2774. CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR);
  2775. /* Enable Interrupts */
  2776. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2777. /* Enable CRYP */
  2778. __HAL_CRYP_ENABLE(hcryp);
  2779. /* Return function status */
  2780. return HAL_OK;
  2781. }
  2782. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2783. {
  2784. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2785. /* Write the Input block in the IN FIFO */
  2786. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2787. inputaddr+=4U;
  2788. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2789. hcryp->pCrypInBuffPtr += 8U;
  2790. hcryp->CrypInCount -= 8U;
  2791. if(hcryp->CrypInCount == 0U)
  2792. {
  2793. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2794. /* Call the Input data transfer complete callback */
  2795. HAL_CRYP_InCpltCallback(hcryp);
  2796. }
  2797. }
  2798. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2799. {
  2800. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2801. /* Read the Output block from the Output FIFO */
  2802. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2803. outputaddr+=4U;
  2804. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2805. hcryp->pCrypOutBuffPtr += 8U;
  2806. hcryp->CrypOutCount -= 8U;
  2807. if(hcryp->CrypOutCount == 0U)
  2808. {
  2809. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2810. /* Disable CRYP */
  2811. __HAL_CRYP_DISABLE(hcryp);
  2812. /* Process Unlocked */
  2813. __HAL_UNLOCK(hcryp);
  2814. /* Change the CRYP state */
  2815. hcryp->State = HAL_CRYP_STATE_READY;
  2816. /* Call Input transfer complete callback */
  2817. HAL_CRYP_OutCpltCallback(hcryp);
  2818. }
  2819. }
  2820. /* Return function status */
  2821. return HAL_OK;
  2822. }
  2823. /**
  2824. * @brief Initializes the CRYP peripheral in TDES CBC decryption mode.
  2825. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2826. * the configuration information for CRYP module
  2827. * @param pCypherData Pointer to the cyphertext buffer
  2828. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2829. * @param pPlainData Pointer to the plaintext buffer
  2830. * @retval HAL status
  2831. */
  2832. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2833. {
  2834. uint32_t inputaddr;
  2835. uint32_t outputaddr;
  2836. if(hcryp->State == HAL_CRYP_STATE_READY)
  2837. {
  2838. /* Process Locked */
  2839. __HAL_LOCK(hcryp);
  2840. hcryp->CrypInCount = Size;
  2841. hcryp->pCrypInBuffPtr = pCypherData;
  2842. hcryp->pCrypOutBuffPtr = pPlainData;
  2843. hcryp->CrypOutCount = Size;
  2844. /* Change the CRYP state */
  2845. hcryp->State = HAL_CRYP_STATE_BUSY;
  2846. /* Set CRYP peripheral in TDES CBC decryption mode */
  2847. CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR);
  2848. /* Enable Interrupts */
  2849. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2850. /* Enable CRYP */
  2851. __HAL_CRYP_ENABLE(hcryp);
  2852. /* Return function status */
  2853. return HAL_OK;
  2854. }
  2855. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  2856. {
  2857. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2858. /* Write the Input block in the IN FIFO */
  2859. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2860. inputaddr+=4U;
  2861. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  2862. hcryp->pCrypInBuffPtr += 8U;
  2863. hcryp->CrypInCount -= 8U;
  2864. if(hcryp->CrypInCount == 0U)
  2865. {
  2866. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2867. /* Call the Input data transfer complete callback */
  2868. HAL_CRYP_InCpltCallback(hcryp);
  2869. }
  2870. }
  2871. else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  2872. {
  2873. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2874. /* Read the Output block from the Output FIFO */
  2875. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2876. outputaddr+=4U;
  2877. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  2878. hcryp->pCrypOutBuffPtr += 8U;
  2879. hcryp->CrypOutCount -= 8U;
  2880. if(hcryp->CrypOutCount == 0U)
  2881. {
  2882. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2883. /* Disable CRYP */
  2884. __HAL_CRYP_DISABLE(hcryp);
  2885. /* Process Unlocked */
  2886. __HAL_UNLOCK(hcryp);
  2887. /* Change the CRYP state */
  2888. hcryp->State = HAL_CRYP_STATE_READY;
  2889. /* Call Input transfer complete callback */
  2890. HAL_CRYP_OutCpltCallback(hcryp);
  2891. }
  2892. }
  2893. /* Return function status */
  2894. return HAL_OK;
  2895. }
  2896. /**
  2897. * @brief Initializes the CRYP peripheral in TDES ECB encryption mode using DMA.
  2898. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2899. * the configuration information for CRYP module
  2900. * @param pPlainData Pointer to the plaintext buffer
  2901. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2902. * @param pCypherData Pointer to the cyphertext buffer
  2903. * @retval HAL status
  2904. */
  2905. HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2906. {
  2907. uint32_t inputaddr;
  2908. uint32_t outputaddr;
  2909. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2910. {
  2911. /* Process Locked */
  2912. __HAL_LOCK(hcryp);
  2913. inputaddr = (uint32_t)pPlainData;
  2914. outputaddr = (uint32_t)pCypherData;
  2915. /* Change the CRYP state */
  2916. hcryp->State = HAL_CRYP_STATE_BUSY;
  2917. /* Set CRYP peripheral in TDES ECB encryption mode */
  2918. CRYP_SetTDESECBMode(hcryp, 0U);
  2919. /* Set the input and output addresses and start DMA transfer */
  2920. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2921. /* Process Unlocked */
  2922. __HAL_UNLOCK(hcryp);
  2923. /* Return function status */
  2924. return HAL_OK;
  2925. }
  2926. else
  2927. {
  2928. return HAL_ERROR;
  2929. }
  2930. }
  2931. /**
  2932. * @brief Initializes the CRYP peripheral in TDES CBC encryption mode using DMA.
  2933. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2934. * the configuration information for CRYP module
  2935. * @param pPlainData Pointer to the plaintext buffer
  2936. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2937. * @param pCypherData Pointer to the cyphertext buffer
  2938. * @retval HAL status
  2939. */
  2940. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2941. {
  2942. uint32_t inputaddr;
  2943. uint32_t outputaddr;
  2944. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2945. {
  2946. /* Process Locked */
  2947. __HAL_LOCK(hcryp);
  2948. inputaddr = (uint32_t)pPlainData;
  2949. outputaddr = (uint32_t)pCypherData;
  2950. /* Change the CRYP state */
  2951. hcryp->State = HAL_CRYP_STATE_BUSY;
  2952. /* Set CRYP peripheral in TDES CBC encryption mode */
  2953. CRYP_SetTDESCBCMode(hcryp, 0U);
  2954. /* Set the input and output addresses and start DMA transfer */
  2955. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2956. /* Process Unlocked */
  2957. __HAL_UNLOCK(hcryp);
  2958. /* Return function status */
  2959. return HAL_OK;
  2960. }
  2961. else
  2962. {
  2963. return HAL_ERROR;
  2964. }
  2965. }
  2966. /**
  2967. * @brief Initializes the CRYP peripheral in TDES ECB decryption mode using DMA.
  2968. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2969. * the configuration information for CRYP module
  2970. * @param pPlainData Pointer to the plaintext buffer
  2971. * @param Size Length of the plaintext buffer, must be a multiple of 8
  2972. * @param pCypherData Pointer to the cyphertext buffer
  2973. * @retval HAL status
  2974. */
  2975. HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2976. {
  2977. uint32_t inputaddr;
  2978. uint32_t outputaddr;
  2979. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2980. {
  2981. /* Process Locked */
  2982. __HAL_LOCK(hcryp);
  2983. inputaddr = (uint32_t)pCypherData;
  2984. outputaddr = (uint32_t)pPlainData;
  2985. /* Change the CRYP state */
  2986. hcryp->State = HAL_CRYP_STATE_BUSY;
  2987. /* Set CRYP peripheral in TDES ECB decryption mode */
  2988. CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR);
  2989. /* Set the input and output addresses and start DMA transfer */
  2990. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2991. /* Process Unlocked */
  2992. __HAL_UNLOCK(hcryp);
  2993. /* Return function status */
  2994. return HAL_OK;
  2995. }
  2996. else
  2997. {
  2998. return HAL_ERROR;
  2999. }
  3000. }
  3001. /**
  3002. * @brief Initializes the CRYP peripheral in TDES CBC decryption mode using DMA.
  3003. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3004. * the configuration information for CRYP module
  3005. * @param pCypherData Pointer to the cyphertext buffer
  3006. * @param Size Length of the plaintext buffer, must be a multiple of 8
  3007. * @param pPlainData Pointer to the plaintext buffer
  3008. * @retval HAL status
  3009. */
  3010. HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  3011. {
  3012. uint32_t inputaddr;
  3013. uint32_t outputaddr;
  3014. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  3015. {
  3016. /* Process Locked */
  3017. __HAL_LOCK(hcryp);
  3018. inputaddr = (uint32_t)pCypherData;
  3019. outputaddr = (uint32_t)pPlainData;
  3020. /* Change the CRYP state */
  3021. hcryp->State = HAL_CRYP_STATE_BUSY;
  3022. /* Set CRYP peripheral in TDES CBC decryption mode */
  3023. CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR);
  3024. /* Set the input and output addresses and start DMA transfer */
  3025. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  3026. /* Process Unlocked */
  3027. __HAL_UNLOCK(hcryp);
  3028. /* Return function status */
  3029. return HAL_OK;
  3030. }
  3031. else
  3032. {
  3033. return HAL_ERROR;
  3034. }
  3035. }
  3036. /**
  3037. * @}
  3038. */
  3039. /** @defgroup CRYP_Exported_Functions_Group5 DMA callback functions
  3040. * @brief DMA callback functions.
  3041. *
  3042. @verbatim
  3043. ==============================================================================
  3044. ##### DMA callback functions #####
  3045. ==============================================================================
  3046. [..] This section provides DMA callback functions:
  3047. (+) DMA Input data transfer complete
  3048. (+) DMA Output data transfer complete
  3049. (+) DMA error
  3050. @endverbatim
  3051. * @{
  3052. */
  3053. /**
  3054. * @brief Input FIFO transfer completed callbacks.
  3055. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3056. * the configuration information for CRYP module
  3057. * @retval None
  3058. */
  3059. __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
  3060. {
  3061. /* Prevent unused argument(s) compilation warning */
  3062. UNUSED(hcryp);
  3063. /* NOTE : This function Should not be modified, when the callback is needed,
  3064. the HAL_CRYP_InCpltCallback could be implemented in the user file
  3065. */
  3066. }
  3067. /**
  3068. * @brief Output FIFO transfer completed callbacks.
  3069. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3070. * the configuration information for CRYP module
  3071. * @retval None
  3072. */
  3073. __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
  3074. {
  3075. /* Prevent unused argument(s) compilation warning */
  3076. UNUSED(hcryp);
  3077. /* NOTE : This function Should not be modified, when the callback is needed,
  3078. the HAL_CRYP_OutCpltCallback could be implemented in the user file
  3079. */
  3080. }
  3081. /**
  3082. * @brief CRYP error callbacks.
  3083. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3084. * the configuration information for CRYP module
  3085. * @retval None
  3086. */
  3087. __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
  3088. {
  3089. /* Prevent unused argument(s) compilation warning */
  3090. UNUSED(hcryp);
  3091. /* NOTE : This function Should not be modified, when the callback is needed,
  3092. the HAL_CRYP_ErrorCallback could be implemented in the user file
  3093. */
  3094. }
  3095. /**
  3096. * @}
  3097. */
  3098. /** @defgroup CRYP_Exported_Functions_Group6 CRYP IRQ handler management
  3099. * @brief CRYP IRQ handler.
  3100. *
  3101. @verbatim
  3102. ==============================================================================
  3103. ##### CRYP IRQ handler management #####
  3104. ==============================================================================
  3105. [..] This section provides CRYP IRQ handler function.
  3106. @endverbatim
  3107. * @{
  3108. */
  3109. /**
  3110. * @brief This function handles CRYP interrupt request.
  3111. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3112. * the configuration information for CRYP module
  3113. * @retval None
  3114. */
  3115. void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
  3116. {
  3117. switch(CRYP->CR & CRYP_CR_ALGOMODE_DIRECTION)
  3118. {
  3119. case CRYP_CR_ALGOMODE_TDES_ECB_ENCRYPT:
  3120. HAL_CRYP_TDESECB_Encrypt_IT(hcryp, NULL, 0U, NULL);
  3121. break;
  3122. case CRYP_CR_ALGOMODE_TDES_ECB_DECRYPT:
  3123. HAL_CRYP_TDESECB_Decrypt_IT(hcryp, NULL, 0U, NULL);
  3124. break;
  3125. case CRYP_CR_ALGOMODE_TDES_CBC_ENCRYPT:
  3126. HAL_CRYP_TDESCBC_Encrypt_IT(hcryp, NULL, 0U, NULL);
  3127. break;
  3128. case CRYP_CR_ALGOMODE_TDES_CBC_DECRYPT:
  3129. HAL_CRYP_TDESCBC_Decrypt_IT(hcryp, NULL, 0U, NULL);
  3130. break;
  3131. case CRYP_CR_ALGOMODE_DES_ECB_ENCRYPT:
  3132. HAL_CRYP_DESECB_Encrypt_IT(hcryp, NULL, 0U, NULL);
  3133. break;
  3134. case CRYP_CR_ALGOMODE_DES_ECB_DECRYPT:
  3135. HAL_CRYP_DESECB_Decrypt_IT(hcryp, NULL, 0U, NULL);
  3136. break;
  3137. case CRYP_CR_ALGOMODE_DES_CBC_ENCRYPT:
  3138. HAL_CRYP_DESCBC_Encrypt_IT(hcryp, NULL, 0U, NULL);
  3139. break;
  3140. case CRYP_CR_ALGOMODE_DES_CBC_DECRYPT:
  3141. HAL_CRYP_DESCBC_Decrypt_IT(hcryp, NULL, 0U, NULL);
  3142. break;
  3143. case CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT:
  3144. HAL_CRYP_AESECB_Encrypt_IT(hcryp, NULL, 0U, NULL);
  3145. break;
  3146. case CRYP_CR_ALGOMODE_AES_ECB_DECRYPT:
  3147. HAL_CRYP_AESECB_Decrypt_IT(hcryp, NULL, 0U, NULL);
  3148. break;
  3149. case CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT:
  3150. HAL_CRYP_AESCBC_Encrypt_IT(hcryp, NULL, 0U, NULL);
  3151. break;
  3152. case CRYP_CR_ALGOMODE_AES_CBC_DECRYPT:
  3153. HAL_CRYP_AESCBC_Decrypt_IT(hcryp, NULL, 0U, NULL);
  3154. break;
  3155. case CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT:
  3156. HAL_CRYP_AESCTR_Encrypt_IT(hcryp, NULL, 0U, NULL);
  3157. break;
  3158. case CRYP_CR_ALGOMODE_AES_CTR_DECRYPT:
  3159. HAL_CRYP_AESCTR_Decrypt_IT(hcryp, NULL, 0U, NULL);
  3160. break;
  3161. default:
  3162. break;
  3163. }
  3164. }
  3165. /**
  3166. * @}
  3167. */
  3168. /** @defgroup CRYP_Exported_Functions_Group7 Peripheral State functions
  3169. * @brief Peripheral State functions.
  3170. *
  3171. @verbatim
  3172. ==============================================================================
  3173. ##### Peripheral State functions #####
  3174. ==============================================================================
  3175. [..]
  3176. This subsection permits to get in run-time the status of the peripheral.
  3177. @endverbatim
  3178. * @{
  3179. */
  3180. /**
  3181. * @brief Returns the CRYP state.
  3182. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3183. * the configuration information for CRYP module
  3184. * @retval HAL state
  3185. */
  3186. HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
  3187. {
  3188. return hcryp->State;
  3189. }
  3190. /**
  3191. * @}
  3192. */
  3193. /**
  3194. * @}
  3195. */
  3196. #endif /* CRYP */
  3197. #if defined (AES)
  3198. /** @defgroup AES AES
  3199. * @brief AES HAL module driver.
  3200. * @{
  3201. */
  3202. /* Private typedef -----------------------------------------------------------*/
  3203. /* Private define ------------------------------------------------------------*/
  3204. /* Private macro -------------------------------------------------------------*/
  3205. /* Private variables ---------------------------------------------------------*/
  3206. /* Private functions --------------------------------------------------------*/
  3207. /** @defgroup CRYP_Private_Functions CRYP Private Functions
  3208. * @{
  3209. */
  3210. static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp);
  3211. static HAL_StatusTypeDef CRYP_SetKey(CRYP_HandleTypeDef *hcryp);
  3212. static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp);
  3213. /**
  3214. * @}
  3215. */
  3216. /* Exported functions ---------------------------------------------------------*/
  3217. /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
  3218. * @{
  3219. */
  3220. /** @defgroup CRYP_Exported_Functions_Group1 Initialization and deinitialization functions
  3221. * @brief Initialization and Configuration functions.
  3222. *
  3223. @verbatim
  3224. ==============================================================================
  3225. ##### Initialization and deinitialization functions #####
  3226. ==============================================================================
  3227. [..] This section provides functions allowing to:
  3228. (+) Initialize the CRYP according to the specified parameters
  3229. in the CRYP_InitTypeDef and creates the associated handle
  3230. (+) DeInitialize the CRYP peripheral
  3231. (+) Initialize the CRYP MSP (MCU Specific Package)
  3232. (+) De-Initialize the CRYP MSP
  3233. [..]
  3234. (@) Specific care must be taken to format the key and the Initialization Vector IV!
  3235. [..] If the key is defined as a 128-bit long array key[127..0] = {b127 ... b0} where
  3236. b127 is the MSB and b0 the LSB, the key must be stored in MCU memory
  3237. (+) as a sequence of words where the MSB word comes first (occupies the
  3238. lowest memory address)
  3239. (+) where each word is byte-swapped:
  3240. (++) address n+0 : 0b b103 .. b96 b111 .. b104 b119 .. b112 b127 .. b120
  3241. (++) address n+4 : 0b b71 .. b64 b79 .. b72 b87 .. b80 b95 .. b88
  3242. (++) address n+8 : 0b b39 .. b32 b47 .. b40 b55 .. b48 b63 .. b56
  3243. (++) address n+C : 0b b7 .. b0 b15 .. b8 b23 .. b16 b31 .. b24
  3244. [..] Hereafter, another illustration when considering a 128-bit long key made of 16 bytes {B15..B0}.
  3245. The 4 32-bit words that make the key must be stored as follows in MCU memory:
  3246. (+) address n+0 : 0x B12 B13 B14 B15
  3247. (+) address n+4 : 0x B8 B9 B10 B11
  3248. (+) address n+8 : 0x B4 B5 B6 B7
  3249. (+) address n+C : 0x B0 B1 B2 B3
  3250. [..] which leads to the expected setting
  3251. (+) AES_KEYR3 = 0x B15 B14 B13 B12
  3252. (+) AES_KEYR2 = 0x B11 B10 B9 B8
  3253. (+) AES_KEYR1 = 0x B7 B6 B5 B4
  3254. (+) AES_KEYR0 = 0x B3 B2 B1 B0
  3255. [..] Same format must be applied for a 256-bit long key made of 32 bytes {B31..B0}.
  3256. The 8 32-bit words that make the key must be stored as follows in MCU memory:
  3257. (+) address n+00 : 0x B28 B29 B30 B31
  3258. (+) address n+04 : 0x B24 B25 B26 B27
  3259. (+) address n+08 : 0x B20 B21 B22 B23
  3260. (+) address n+0C : 0x B16 B17 B18 B19
  3261. (+) address n+10 : 0x B12 B13 B14 B15
  3262. (+) address n+14 : 0x B8 B9 B10 B11
  3263. (+) address n+18 : 0x B4 B5 B6 B7
  3264. (+) address n+1C : 0x B0 B1 B2 B3
  3265. [..] which leads to the expected setting
  3266. (+) AES_KEYR7 = 0x B31 B30 B29 B28
  3267. (+) AES_KEYR6 = 0x B27 B26 B25 B24
  3268. (+) AES_KEYR5 = 0x B23 B22 B21 B20
  3269. (+) AES_KEYR4 = 0x B19 B18 B17 B16
  3270. (+) AES_KEYR3 = 0x B15 B14 B13 B12
  3271. (+) AES_KEYR2 = 0x B11 B10 B9 B8
  3272. (+) AES_KEYR1 = 0x B7 B6 B5 B4
  3273. (+) AES_KEYR0 = 0x B3 B2 B1 B0
  3274. [..] Initialization Vector IV (4 32-bit words) format must follow the same as
  3275. that of a 128-bit long key.
  3276. [..]
  3277. @endverbatim
  3278. * @{
  3279. */
  3280. /**
  3281. * @brief Initialize the CRYP according to the specified
  3282. * parameters in the CRYP_InitTypeDef and initialize the associated handle.
  3283. * @note Specific care must be taken to format the key and the Initialization Vector IV
  3284. * stored in the MCU memory before calling HAL_CRYP_Init(). Refer to explanations
  3285. * hereabove.
  3286. * @retval HAL status
  3287. */
  3288. HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
  3289. {
  3290. /* Check the CRYP handle allocation */
  3291. if(hcryp == NULL)
  3292. {
  3293. return HAL_ERROR;
  3294. }
  3295. /* Check the instance */
  3296. assert_param(IS_AES_ALL_INSTANCE(hcryp->Instance));
  3297. /* Check the parameters */
  3298. assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
  3299. assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
  3300. assert_param(IS_CRYP_ALGOMODE(hcryp->Init.OperatingMode));
  3301. /* ChainingMode parameter is irrelevant when mode is set to Key derivation */
  3302. if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
  3303. {
  3304. assert_param(IS_CRYP_CHAINMODE(hcryp->Init.ChainingMode));
  3305. }
  3306. assert_param(IS_CRYP_WRITE(hcryp->Init.KeyWriteFlag));
  3307. /*========================================================*/
  3308. /* Check the proper operating/chaining modes combinations */
  3309. /*========================================================*/
  3310. /* Check the proper chaining when the operating mode is key derivation and decryption */
  3311. #if defined(AES_CR_NPBLB)
  3312. if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\
  3313. ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR) \
  3314. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) \
  3315. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)))
  3316. #else
  3317. if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\
  3318. ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR) \
  3319. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) \
  3320. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)))
  3321. #endif
  3322. {
  3323. return HAL_ERROR;
  3324. }
  3325. /* Check that key derivation is not set in CMAC mode or CCM mode when applicable */
  3326. #if defined(AES_CR_NPBLB)
  3327. if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  3328. && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC))
  3329. #else
  3330. if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  3331. && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
  3332. #endif
  3333. {
  3334. return HAL_ERROR;
  3335. }
  3336. /*================*/
  3337. /* Initialization */
  3338. /*================*/
  3339. /* Initialization start */
  3340. if(hcryp->State == HAL_CRYP_STATE_RESET)
  3341. {
  3342. /* Allocate lock resource and initialize it */
  3343. hcryp->Lock = HAL_UNLOCKED;
  3344. /* Init the low level hardware */
  3345. HAL_CRYP_MspInit(hcryp);
  3346. }
  3347. /* Change the CRYP state */
  3348. hcryp->State = HAL_CRYP_STATE_BUSY;
  3349. /* Disable the Peripheral */
  3350. __HAL_CRYP_DISABLE();
  3351. /*=============================================================*/
  3352. /* AES initialization common to all operating modes */
  3353. /*=============================================================*/
  3354. /* Set the Key size selection */
  3355. MODIFY_REG(hcryp->Instance->CR, AES_CR_KEYSIZE, hcryp->Init.KeySize);
  3356. /* Set the default CRYP phase when this parameter is not used.
  3357. Phase is updated below in case of GCM/GMAC/CMAC(/CCM) setting. */
  3358. hcryp->Phase = HAL_CRYP_PHASE_NOT_USED;
  3359. /*=============================================================*/
  3360. /* Carry on the initialization based on the AES operating mode */
  3361. /*=============================================================*/
  3362. /* Key derivation */
  3363. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  3364. {
  3365. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_ALGOMODE_KEYDERIVATION);
  3366. /* Configure the Key registers */
  3367. if (CRYP_SetKey(hcryp) != HAL_OK)
  3368. {
  3369. return HAL_ERROR;
  3370. }
  3371. }
  3372. else
  3373. /* Encryption / Decryption (with or without key derivation) / authentication */
  3374. {
  3375. /* Set data type, operating and chaining modes.
  3376. In case of GCM or GMAC, data type is forced to 0b00 */
  3377. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  3378. {
  3379. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
  3380. }
  3381. else
  3382. {
  3383. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.DataType|hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
  3384. }
  3385. /* Specify the encryption/decryption phase in case of Galois counter mode (GCM),
  3386. Galois message authentication code (GMAC), cipher message authentication code (CMAC)
  3387. or Counter with Cipher Mode (CCM) when applicable */
  3388. #if defined(AES_CR_NPBLB)
  3389. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  3390. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC))
  3391. #else
  3392. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  3393. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
  3394. #endif
  3395. {
  3396. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, hcryp->Init.GCMCMACPhase);
  3397. hcryp->Phase = HAL_CRYP_PHASE_START;
  3398. }
  3399. /* Configure the Key registers if no need to bypass this step */
  3400. if (hcryp->Init.KeyWriteFlag == CRYP_KEY_WRITE_ENABLE)
  3401. {
  3402. if (CRYP_SetKey(hcryp) != HAL_OK)
  3403. {
  3404. return HAL_ERROR;
  3405. }
  3406. }
  3407. /* If applicable, configure the Initialization Vector */
  3408. if (hcryp->Init.ChainingMode != CRYP_CHAINMODE_AES_ECB)
  3409. {
  3410. if (CRYP_SetInitVector(hcryp) != HAL_OK)
  3411. {
  3412. return HAL_ERROR;
  3413. }
  3414. }
  3415. }
  3416. #if defined(AES_CR_NPBLB)
  3417. /* Clear NPBLB field */
  3418. CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
  3419. #endif
  3420. /* Reset CrypInCount and CrypOutCount */
  3421. hcryp->CrypInCount = 0U;
  3422. hcryp->CrypOutCount = 0U;
  3423. /* Reset ErrorCode field */
  3424. hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
  3425. /* Reset Mode suspension request */
  3426. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  3427. /* Change the CRYP state */
  3428. hcryp->State = HAL_CRYP_STATE_READY;
  3429. /* Enable the Peripheral */
  3430. __HAL_CRYP_ENABLE();
  3431. /* Return function status */
  3432. return HAL_OK;
  3433. }
  3434. /**
  3435. * @brief DeInitialize the CRYP peripheral.
  3436. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3437. * the configuration information for CRYP module
  3438. * @retval HAL status
  3439. */
  3440. HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
  3441. {
  3442. /* Check the CRYP handle allocation */
  3443. if(hcryp == NULL)
  3444. {
  3445. return HAL_ERROR;
  3446. }
  3447. /* Change the CRYP state */
  3448. hcryp->State = HAL_CRYP_STATE_BUSY;
  3449. /* Set the default CRYP phase */
  3450. hcryp->Phase = HAL_CRYP_PHASE_READY;
  3451. /* Reset CrypInCount and CrypOutCount */
  3452. hcryp->CrypInCount = 0U;
  3453. hcryp->CrypOutCount = 0U;
  3454. /* Disable the CRYP Peripheral Clock */
  3455. __HAL_CRYP_DISABLE();
  3456. /* DeInit the low level hardware: CLOCK, NVIC.*/
  3457. HAL_CRYP_MspDeInit(hcryp);
  3458. /* Change the CRYP state */
  3459. hcryp->State = HAL_CRYP_STATE_RESET;
  3460. /* Release Lock */
  3461. __HAL_UNLOCK(hcryp);
  3462. /* Return function status */
  3463. return HAL_OK;
  3464. }
  3465. /**
  3466. * @brief Initialize the CRYP MSP.
  3467. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3468. * the configuration information for CRYP module
  3469. * @retval None
  3470. */
  3471. __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
  3472. {
  3473. /* Prevent unused argument(s) compilation warning */
  3474. UNUSED(hcryp);
  3475. /* NOTE : This function should not be modified; when the callback is needed,
  3476. the HAL_CRYP_MspInit can be implemented in the user file
  3477. */
  3478. }
  3479. /**
  3480. * @brief DeInitialize CRYP MSP.
  3481. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3482. * the configuration information for CRYP module
  3483. * @retval None
  3484. */
  3485. __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
  3486. {
  3487. /* Prevent unused argument(s) compilation warning */
  3488. UNUSED(hcryp);
  3489. /* NOTE : This function should not be modified; when the callback is needed,
  3490. the HAL_CRYP_MspDeInit can be implemented in the user file
  3491. */
  3492. }
  3493. /**
  3494. * @}
  3495. */
  3496. /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions
  3497. * @brief Processing functions.
  3498. *
  3499. @verbatim
  3500. ==============================================================================
  3501. ##### AES processing functions #####
  3502. ==============================================================================
  3503. [..] This section provides functions allowing to:
  3504. (+) Encrypt plaintext using AES algorithm in different chaining modes
  3505. (+) Decrypt cyphertext using AES algorithm in different chaining modes
  3506. [..] Three processing functions are available:
  3507. (+) Polling mode
  3508. (+) Interrupt mode
  3509. (+) DMA mode
  3510. @endverbatim
  3511. * @{
  3512. */
  3513. /**
  3514. * @brief Encrypt pPlainData in AES ECB encryption mode. The cypher data are available in pCypherData.
  3515. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3516. * the configuration information for CRYP module
  3517. * @param pPlainData Pointer to the plaintext buffer
  3518. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3519. * @param pCypherData Pointer to the cyphertext buffer
  3520. * @param Timeout Specify Timeout value
  3521. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3522. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  3523. * @retval HAL status
  3524. */
  3525. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  3526. {
  3527. /* Re-initialize AES IP with proper parameters */
  3528. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3529. {
  3530. return HAL_ERROR;
  3531. }
  3532. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  3533. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  3534. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3535. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3536. {
  3537. return HAL_ERROR;
  3538. }
  3539. return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
  3540. }
  3541. /**
  3542. * @brief Encrypt pPlainData in AES CBC encryption mode with key derivation. The cypher data are available in pCypherData.
  3543. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3544. * the configuration information for CRYP module
  3545. * @param pPlainData Pointer to the plaintext buffer
  3546. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3547. * @param pCypherData Pointer to the cyphertext buffer
  3548. * @param Timeout Specify Timeout value
  3549. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3550. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  3551. * @retval HAL status
  3552. */
  3553. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  3554. {
  3555. /* Re-initialize AES IP with proper parameters */
  3556. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3557. {
  3558. return HAL_ERROR;
  3559. }
  3560. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  3561. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  3562. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3563. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3564. {
  3565. return HAL_ERROR;
  3566. }
  3567. return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
  3568. }
  3569. /**
  3570. * @brief Encrypt pPlainData in AES CTR encryption mode. The cypher data are available in pCypherData
  3571. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3572. * the configuration information for CRYP module
  3573. * @param pPlainData Pointer to the plaintext buffer
  3574. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3575. * @param pCypherData Pointer to the cyphertext buffer
  3576. * @param Timeout Specify Timeout value
  3577. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3578. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  3579. * @retval HAL status
  3580. */
  3581. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  3582. {
  3583. /* Re-initialize AES IP with proper parameters */
  3584. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3585. {
  3586. return HAL_ERROR;
  3587. }
  3588. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  3589. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  3590. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3591. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3592. {
  3593. return HAL_ERROR;
  3594. }
  3595. return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
  3596. }
  3597. /**
  3598. * @brief Decrypt pCypherData in AES ECB decryption mode with key derivation,
  3599. * the decyphered data are available in pPlainData.
  3600. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3601. * the configuration information for CRYP module
  3602. * @param pCypherData Pointer to the cyphertext buffer
  3603. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3604. * @param pPlainData Pointer to the plaintext buffer
  3605. * @param Timeout Specify Timeout value
  3606. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3607. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  3608. * @retval HAL status
  3609. */
  3610. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  3611. {
  3612. /* Re-initialize AES IP with proper parameters */
  3613. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3614. {
  3615. return HAL_ERROR;
  3616. }
  3617. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  3618. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  3619. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3620. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3621. {
  3622. return HAL_ERROR;
  3623. }
  3624. return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
  3625. }
  3626. /**
  3627. * @brief Decrypt pCypherData in AES ECB decryption mode with key derivation,
  3628. * the decyphered data are available in pPlainData.
  3629. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3630. * the configuration information for CRYP module
  3631. * @param pCypherData Pointer to the cyphertext buffer
  3632. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3633. * @param pPlainData Pointer to the plaintext buffer
  3634. * @param Timeout Specify Timeout value
  3635. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3636. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  3637. * @retval HAL status
  3638. */
  3639. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  3640. {
  3641. /* Re-initialize AES IP with proper parameters */
  3642. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3643. {
  3644. return HAL_ERROR;
  3645. }
  3646. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  3647. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  3648. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3649. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3650. {
  3651. return HAL_ERROR;
  3652. }
  3653. return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
  3654. }
  3655. /**
  3656. * @brief Decrypt pCypherData in AES CTR decryption mode,
  3657. * the decyphered data are available in pPlainData.
  3658. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3659. * the configuration information for CRYP module
  3660. * @param pCypherData Pointer to the cyphertext buffer
  3661. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3662. * @param pPlainData Pointer to the plaintext buffer
  3663. * @param Timeout Specify Timeout value
  3664. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3665. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  3666. * @retval HAL status
  3667. */
  3668. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  3669. {
  3670. /* Re-initialize AES IP with proper parameters */
  3671. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3672. {
  3673. return HAL_ERROR;
  3674. }
  3675. hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
  3676. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  3677. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3678. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3679. {
  3680. return HAL_ERROR;
  3681. }
  3682. return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
  3683. }
  3684. /**
  3685. * @brief Encrypt pPlainData in AES ECB encryption mode using Interrupt,
  3686. * the cypher data are available in pCypherData.
  3687. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3688. * the configuration information for CRYP module
  3689. * @param pPlainData Pointer to the plaintext buffer
  3690. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3691. * @param pCypherData Pointer to the cyphertext buffer
  3692. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3693. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  3694. * @retval HAL status
  3695. */
  3696. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  3697. {
  3698. /* Re-initialize AES IP with proper parameters */
  3699. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3700. {
  3701. return HAL_ERROR;
  3702. }
  3703. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  3704. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  3705. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3706. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3707. {
  3708. return HAL_ERROR;
  3709. }
  3710. return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
  3711. }
  3712. /**
  3713. * @brief Encrypt pPlainData in AES CBC encryption mode using Interrupt,
  3714. * the cypher data are available in pCypherData.
  3715. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3716. * the configuration information for CRYP module
  3717. * @param pPlainData Pointer to the plaintext buffer
  3718. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3719. * @param pCypherData Pointer to the cyphertext buffer
  3720. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3721. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  3722. * @retval HAL status
  3723. */
  3724. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  3725. {
  3726. /* Re-initialize AES IP with proper parameters */
  3727. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3728. {
  3729. return HAL_ERROR;
  3730. }
  3731. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  3732. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  3733. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3734. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3735. {
  3736. return HAL_ERROR;
  3737. }
  3738. return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
  3739. }
  3740. /**
  3741. * @brief Encrypt pPlainData in AES CTR encryption mode using Interrupt,
  3742. * the cypher data are available in pCypherData.
  3743. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3744. * the configuration information for CRYP module
  3745. * @param pPlainData Pointer to the plaintext buffer
  3746. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3747. * @param pCypherData Pointer to the cyphertext buffer
  3748. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3749. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  3750. * @retval HAL status
  3751. */
  3752. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  3753. {
  3754. /* Re-initialize AES IP with proper parameters */
  3755. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3756. {
  3757. return HAL_ERROR;
  3758. }
  3759. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  3760. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  3761. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3762. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3763. {
  3764. return HAL_ERROR;
  3765. }
  3766. return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
  3767. }
  3768. /**
  3769. * @brief Decrypt pCypherData in AES ECB decryption mode using Interrupt,
  3770. * the decyphered data are available in pPlainData.
  3771. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3772. * the configuration information for CRYP module
  3773. * @param pCypherData Pointer to the cyphertext buffer
  3774. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3775. * @param pPlainData Pointer to the plaintext buffer.
  3776. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3777. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  3778. * @retval HAL status
  3779. */
  3780. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  3781. {
  3782. /* Re-initialize AES IP with proper parameters */
  3783. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3784. {
  3785. return HAL_ERROR;
  3786. }
  3787. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  3788. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  3789. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3790. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3791. {
  3792. return HAL_ERROR;
  3793. }
  3794. return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
  3795. }
  3796. /**
  3797. * @brief Decrypt pCypherData in AES CBC decryption mode using Interrupt,
  3798. * the decyphered data are available in pPlainData.
  3799. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3800. * the configuration information for CRYP module
  3801. * @param pCypherData Pointer to the cyphertext buffer
  3802. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3803. * @param pPlainData Pointer to the plaintext buffer
  3804. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3805. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  3806. * @retval HAL status
  3807. */
  3808. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  3809. {
  3810. /* Re-initialize AES IP with proper parameters */
  3811. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3812. {
  3813. return HAL_ERROR;
  3814. }
  3815. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  3816. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  3817. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3818. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3819. {
  3820. return HAL_ERROR;
  3821. }
  3822. return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
  3823. }
  3824. /**
  3825. * @brief Decrypt pCypherData in AES CTR decryption mode using Interrupt,
  3826. * the decyphered data are available in pPlainData.
  3827. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3828. * the configuration information for CRYP module
  3829. * @param pCypherData Pointer to the cyphertext buffer
  3830. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3831. * @param pPlainData Pointer to the plaintext buffer
  3832. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3833. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  3834. * @retval HAL status
  3835. */
  3836. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  3837. {
  3838. /* Re-initialize AES IP with proper parameters */
  3839. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3840. {
  3841. return HAL_ERROR;
  3842. }
  3843. hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
  3844. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  3845. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3846. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3847. {
  3848. return HAL_ERROR;
  3849. }
  3850. return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
  3851. }
  3852. /**
  3853. * @brief Encrypt pPlainData in AES ECB encryption mode using DMA,
  3854. * the cypher data are available in pCypherData.
  3855. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3856. * the configuration information for CRYP module
  3857. * @param pPlainData Pointer to the plaintext buffer
  3858. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3859. * @param pCypherData Pointer to the cyphertext buffer
  3860. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3861. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  3862. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  3863. * @retval HAL status
  3864. */
  3865. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  3866. {
  3867. /* Re-initialize AES IP with proper parameters */
  3868. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3869. {
  3870. return HAL_ERROR;
  3871. }
  3872. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  3873. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  3874. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3875. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3876. {
  3877. return HAL_ERROR;
  3878. }
  3879. return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
  3880. }
  3881. /**
  3882. * @brief Encrypt pPlainData in AES CBC encryption mode using DMA,
  3883. * the cypher data are available in pCypherData.
  3884. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3885. * the configuration information for CRYP module
  3886. * @param pPlainData Pointer to the plaintext buffer
  3887. * @param Size Length of the plaintext buffer, must be a multiple of 16.
  3888. * @param pCypherData Pointer to the cyphertext buffer
  3889. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3890. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  3891. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  3892. * @retval HAL status
  3893. */
  3894. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  3895. {
  3896. /* Re-initialize AES IP with proper parameters */
  3897. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3898. {
  3899. return HAL_ERROR;
  3900. }
  3901. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  3902. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  3903. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3904. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3905. {
  3906. return HAL_ERROR;
  3907. }
  3908. return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
  3909. }
  3910. /**
  3911. * @brief Encrypt pPlainData in AES CTR encryption mode using DMA,
  3912. * the cypher data are available in pCypherData.
  3913. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3914. * the configuration information for CRYP module
  3915. * @param pPlainData Pointer to the plaintext buffer
  3916. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3917. * @param pCypherData Pointer to the cyphertext buffer.
  3918. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3919. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  3920. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  3921. * @retval HAL status
  3922. */
  3923. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  3924. {
  3925. /* Re-initialize AES IP with proper parameters */
  3926. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3927. {
  3928. return HAL_ERROR;
  3929. }
  3930. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  3931. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  3932. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3933. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3934. {
  3935. return HAL_ERROR;
  3936. }
  3937. return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
  3938. }
  3939. /**
  3940. * @brief Decrypt pCypherData in AES ECB decryption mode using DMA,
  3941. * the decyphered data are available in pPlainData.
  3942. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3943. * the configuration information for CRYP module
  3944. * @param pCypherData Pointer to the cyphertext buffer
  3945. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3946. * @param pPlainData Pointer to the plaintext buffer
  3947. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3948. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  3949. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  3950. * @retval HAL status
  3951. */
  3952. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  3953. {
  3954. /* Re-initialize AES IP with proper parameters */
  3955. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3956. {
  3957. return HAL_ERROR;
  3958. }
  3959. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  3960. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  3961. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3962. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3963. {
  3964. return HAL_ERROR;
  3965. }
  3966. return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
  3967. }
  3968. /**
  3969. * @brief Decrypt pCypherData in AES CBC decryption mode using DMA,
  3970. * the decyphered data are available in pPlainData.
  3971. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3972. * the configuration information for CRYP module
  3973. * @param pCypherData Pointer to the cyphertext buffer
  3974. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  3975. * @param pPlainData Pointer to the plaintext buffer
  3976. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  3977. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  3978. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  3979. * @retval HAL status
  3980. */
  3981. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  3982. {
  3983. /* Re-initialize AES IP with proper parameters */
  3984. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  3985. {
  3986. return HAL_ERROR;
  3987. }
  3988. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  3989. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  3990. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  3991. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  3992. {
  3993. return HAL_ERROR;
  3994. }
  3995. return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
  3996. }
  3997. /**
  3998. * @brief Decrypt pCypherData in AES CTR decryption mode using DMA,
  3999. * the decyphered data are available in pPlainData.
  4000. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4001. * the configuration information for CRYP module
  4002. * @param pCypherData Pointer to the cyphertext buffer
  4003. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  4004. * @param pPlainData Pointer to the plaintext buffer
  4005. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  4006. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  4007. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  4008. * @retval HAL status
  4009. */
  4010. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  4011. {
  4012. /* Re-initialize AES IP with proper parameters */
  4013. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  4014. {
  4015. return HAL_ERROR;
  4016. }
  4017. hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
  4018. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  4019. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  4020. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  4021. {
  4022. return HAL_ERROR;
  4023. }
  4024. return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
  4025. }
  4026. /**
  4027. * @}
  4028. */
  4029. /** @defgroup CRYP_Exported_Functions_Group3 Callback functions
  4030. * @brief Callback functions.
  4031. *
  4032. @verbatim
  4033. ==============================================================================
  4034. ##### Callback functions #####
  4035. ==============================================================================
  4036. [..] This section provides Interruption and DMA callback functions:
  4037. (+) DMA Input data transfer complete
  4038. (+) DMA Output data transfer complete
  4039. (+) DMA or Interrupt error
  4040. @endverbatim
  4041. * @{
  4042. */
  4043. /**
  4044. * @brief CRYP error callback.
  4045. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4046. * the configuration information for CRYP module
  4047. * @retval None
  4048. */
  4049. __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
  4050. {
  4051. /* Prevent unused argument(s) compilation warning */
  4052. UNUSED(hcryp);
  4053. /* NOTE : This function should not be modified; when the callback is needed,
  4054. the HAL_CRYP_ErrorCallback can be implemented in the user file
  4055. */
  4056. }
  4057. /**
  4058. * @brief Input DMA transfer complete callback.
  4059. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4060. * the configuration information for CRYP module
  4061. * @retval None
  4062. */
  4063. __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
  4064. {
  4065. /* Prevent unused argument(s) compilation warning */
  4066. UNUSED(hcryp);
  4067. /* NOTE : This function should not be modified; when the callback is needed,
  4068. the HAL_CRYP_InCpltCallback can be implemented in the user file
  4069. */
  4070. }
  4071. /**
  4072. * @brief Output DMA transfer complete callback.
  4073. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4074. * the configuration information for CRYP module
  4075. * @retval None
  4076. */
  4077. __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
  4078. {
  4079. /* Prevent unused argument(s) compilation warning */
  4080. UNUSED(hcryp);
  4081. /* NOTE : This function should not be modified; when the callback is needed,
  4082. the HAL_CRYP_OutCpltCallback can be implemented in the user file
  4083. */
  4084. }
  4085. /**
  4086. * @}
  4087. */
  4088. /** @defgroup CRYP_Exported_Functions_Group4 CRYP IRQ handler
  4089. * @brief AES IRQ handler.
  4090. *
  4091. @verbatim
  4092. ==============================================================================
  4093. ##### AES IRQ handler management #####
  4094. ==============================================================================
  4095. [..] This section provides AES IRQ handler function.
  4096. @endverbatim
  4097. * @{
  4098. */
  4099. /**
  4100. * @brief Handle AES interrupt request.
  4101. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4102. * the configuration information for CRYP module
  4103. * @retval None
  4104. */
  4105. void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
  4106. {
  4107. /* Check if error occurred */
  4108. if (__HAL_CRYP_GET_IT_SOURCE(CRYP_IT_ERRIE) != RESET)
  4109. {
  4110. /* If Write Error occurred */
  4111. if (__HAL_CRYP_GET_FLAG(CRYP_IT_WRERR) != RESET)
  4112. {
  4113. hcryp->ErrorCode |= HAL_CRYP_WRITE_ERROR;
  4114. hcryp->State = HAL_CRYP_STATE_ERROR;
  4115. }
  4116. /* If Read Error occurred */
  4117. if (__HAL_CRYP_GET_FLAG(CRYP_IT_RDERR) != RESET)
  4118. {
  4119. hcryp->ErrorCode |= HAL_CRYP_READ_ERROR;
  4120. hcryp->State = HAL_CRYP_STATE_ERROR;
  4121. }
  4122. /* If an error has been reported */
  4123. if (hcryp->State == HAL_CRYP_STATE_ERROR)
  4124. {
  4125. /* Disable Error and Computation Complete Interrupts */
  4126. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4127. /* Clear all Interrupt flags */
  4128. __HAL_CRYP_CLEAR_FLAG(CRYP_ERR_CLEAR|CRYP_CCF_CLEAR);
  4129. /* Process Unlocked */
  4130. __HAL_UNLOCK(hcryp);
  4131. HAL_CRYP_ErrorCallback(hcryp);
  4132. return;
  4133. }
  4134. }
  4135. /* Check if computation complete interrupt is enabled
  4136. and if the computation complete flag is raised */
  4137. if((__HAL_CRYP_GET_FLAG(CRYP_IT_CCF) != RESET) && (__HAL_CRYP_GET_IT_SOURCE(CRYP_IT_CCFIE) != RESET))
  4138. {
  4139. #if defined(AES_CR_NPBLB)
  4140. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  4141. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC))
  4142. #else
  4143. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  4144. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
  4145. #endif
  4146. {
  4147. /* To ensure proper suspension requests management, CCF flag
  4148. is reset in CRYP_AES_Auth_IT() according to the current
  4149. phase under handling */
  4150. CRYP_AES_Auth_IT(hcryp);
  4151. }
  4152. else
  4153. {
  4154. /* Clear Computation Complete Flag */
  4155. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4156. CRYP_AES_IT(hcryp);
  4157. }
  4158. }
  4159. }
  4160. /**
  4161. * @}
  4162. */
  4163. /** @defgroup CRYP_Exported_Functions_Group5 Peripheral State functions
  4164. * @brief Peripheral State functions.
  4165. *
  4166. @verbatim
  4167. ==============================================================================
  4168. ##### Peripheral State functions #####
  4169. ==============================================================================
  4170. [..]
  4171. This subsection permits to get in run-time the status of the peripheral.
  4172. @endverbatim
  4173. * @{
  4174. */
  4175. /**
  4176. * @brief Return the CRYP handle state.
  4177. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4178. * the configuration information for CRYP module
  4179. * @retval HAL state
  4180. */
  4181. HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
  4182. {
  4183. /* Return CRYP handle state */
  4184. return hcryp->State;
  4185. }
  4186. /**
  4187. * @brief Return the CRYP peripheral error.
  4188. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4189. * the configuration information for CRYP module
  4190. * @note The returned error is a bit-map combination of possible errors
  4191. * @retval Error bit-map
  4192. */
  4193. uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp)
  4194. {
  4195. return hcryp->ErrorCode;
  4196. }
  4197. /**
  4198. * @}
  4199. */
  4200. /**
  4201. * @}
  4202. */
  4203. /** @addtogroup CRYP_Private_Functions
  4204. * @{
  4205. */
  4206. /**
  4207. * @brief Write the Key in KeyRx registers.
  4208. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4209. * the configuration information for CRYP module
  4210. * @retval None
  4211. */
  4212. static HAL_StatusTypeDef CRYP_SetKey(CRYP_HandleTypeDef *hcryp)
  4213. {
  4214. uint32_t keyaddr = 0x0U;
  4215. if ((uint32_t)(hcryp->Init.pKey == NULL))
  4216. {
  4217. return HAL_ERROR;
  4218. }
  4219. keyaddr = (uint32_t)(hcryp->Init.pKey);
  4220. if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
  4221. {
  4222. hcryp->Instance->KEYR7 = __REV(*(uint32_t*)(keyaddr));
  4223. keyaddr+=4U;
  4224. hcryp->Instance->KEYR6 = __REV(*(uint32_t*)(keyaddr));
  4225. keyaddr+=4U;
  4226. hcryp->Instance->KEYR5 = __REV(*(uint32_t*)(keyaddr));
  4227. keyaddr+=4U;
  4228. hcryp->Instance->KEYR4 = __REV(*(uint32_t*)(keyaddr));
  4229. keyaddr+=4U;
  4230. }
  4231. hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr));
  4232. keyaddr+=4U;
  4233. hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr));
  4234. keyaddr+=4U;
  4235. hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr));
  4236. keyaddr+=4U;
  4237. hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr));
  4238. return HAL_OK;
  4239. }
  4240. /**
  4241. * @brief Write the InitVector/InitCounter in IVRx registers.
  4242. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4243. * the configuration information for CRYP module
  4244. * @retval None
  4245. */
  4246. static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp)
  4247. {
  4248. uint32_t ivaddr = 0x0U;
  4249. #if !defined(AES_CR_NPBLB)
  4250. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  4251. {
  4252. hcryp->Instance->IVR3 = 0U;
  4253. hcryp->Instance->IVR2 = 0U;
  4254. hcryp->Instance->IVR1 = 0U;
  4255. hcryp->Instance->IVR0 = 0U;
  4256. }
  4257. else
  4258. #endif
  4259. {
  4260. if (hcryp->Init.pInitVect == NULL)
  4261. {
  4262. return HAL_ERROR;
  4263. }
  4264. ivaddr = (uint32_t)(hcryp->Init.pInitVect);
  4265. hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr));
  4266. ivaddr+=4U;
  4267. hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr));
  4268. ivaddr+=4U;
  4269. hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr));
  4270. ivaddr+=4U;
  4271. hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr));
  4272. }
  4273. return HAL_OK;
  4274. }
  4275. /**
  4276. * @brief Handle CRYP block input/output data handling under interruption.
  4277. * @note The function is called under interruption only, once
  4278. * interruptions have been enabled by HAL_CRYPEx_AES_IT().
  4279. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4280. * the configuration information for CRYP module.
  4281. * @retval HAL status
  4282. */
  4283. static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp)
  4284. {
  4285. uint32_t inputaddr = 0U;
  4286. uint32_t outputaddr = 0U;
  4287. if(hcryp->State == HAL_CRYP_STATE_BUSY)
  4288. {
  4289. if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
  4290. {
  4291. /* Get the output data address */
  4292. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  4293. /* Read the last available output block from the Data Output Register */
  4294. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4295. outputaddr+=4U;
  4296. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4297. outputaddr+=4U;
  4298. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4299. outputaddr+=4U;
  4300. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4301. hcryp->pCrypOutBuffPtr += 16U;
  4302. hcryp->CrypOutCount -= 16U;
  4303. }
  4304. else
  4305. {
  4306. /* Read the derived key from the Key registers */
  4307. if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
  4308. {
  4309. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR7);
  4310. outputaddr+=4U;
  4311. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR6);
  4312. outputaddr+=4U;
  4313. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR5);
  4314. outputaddr+=4U;
  4315. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR4);
  4316. outputaddr+=4U;
  4317. }
  4318. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR3);
  4319. outputaddr+=4U;
  4320. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR2);
  4321. outputaddr+=4U;
  4322. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR1);
  4323. outputaddr+=4U;
  4324. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR0);
  4325. }
  4326. /* In case of ciphering or deciphering, check if all output text has been retrieved;
  4327. In case of key derivation, stop right there */
  4328. if ((hcryp->CrypOutCount == 0U) || (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION))
  4329. {
  4330. /* Disable Computation Complete Flag and Errors Interrupts */
  4331. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4332. /* Change the CRYP state */
  4333. hcryp->State = HAL_CRYP_STATE_READY;
  4334. /* Process Unlocked */
  4335. __HAL_UNLOCK(hcryp);
  4336. /* Call computation complete callback */
  4337. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  4338. return HAL_OK;
  4339. }
  4340. /* If suspension flag has been raised, suspend processing */
  4341. else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
  4342. {
  4343. /* reset ModeSuspend */
  4344. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  4345. /* Disable Computation Complete Flag and Errors Interrupts */
  4346. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4347. /* Change the CRYP state */
  4348. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  4349. /* Process Unlocked */
  4350. __HAL_UNLOCK(hcryp);
  4351. return HAL_OK;
  4352. }
  4353. else /* Process the rest of input data */
  4354. {
  4355. /* Get the Intput data address */
  4356. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  4357. /* Increment/decrement instance pointer/counter */
  4358. hcryp->pCrypInBuffPtr += 16U;
  4359. hcryp->CrypInCount -= 16U;
  4360. /* Write the next input block in the Data Input register */
  4361. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4362. inputaddr+=4U;
  4363. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4364. inputaddr+=4U;
  4365. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4366. inputaddr+=4U;
  4367. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4368. return HAL_OK;
  4369. }
  4370. }
  4371. else
  4372. {
  4373. return HAL_BUSY;
  4374. }
  4375. }
  4376. /**
  4377. * @}
  4378. */
  4379. #endif /* AES */
  4380. #endif /* HAL_CRYP_MODULE_ENABLED */
  4381. /**
  4382. * @}
  4383. */
  4384. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/