stm32h7xx_hal_cryp.c 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_cryp.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 21-April-2017
  7. * @brief CRYP HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Cryptography (CRYP) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + AES processing functions
  12. * + DES processing functions
  13. * + TDES processing functions
  14. * + DMA callback functions
  15. * + CRYP IRQ handler management
  16. * + Peripheral State functions
  17. *
  18. @verbatim
  19. ==============================================================================
  20. ##### How to use this driver #####
  21. ==============================================================================
  22. [..]
  23. The CRYP HAL driver can be used in CRYP IP as follows:
  24. (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
  25. (##) Enable the CRYP interface clock using __HAL_RCC_CRYP_CLK_ENABLE()
  26. (##) In case of using interrupts (e.g. HAL_CRYP_Encrypt_IT())
  27. (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
  28. (+++) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ()
  29. (+++) In CRYP IRQ handler, call HAL_CRYP_IRQHandler()
  30. (##) In case of using DMA to control data transfer (e.g. HAL_CRYP_Encrypt_DMA())
  31. (+++) Enable the DMAx interface clock using __RCC_DMAx_CLK_ENABLE()
  32. (+++) Configure and enable two DMA streams one for managing data transfer from
  33. memory to peripheral (input stream) and another stream for managing data
  34. transfer from peripheral to memory (output stream)
  35. (+++) Associate the initialized DMA handle to the CRYP DMA handle
  36. using __HAL_LINKDMA()
  37. (+++) Configure the priority and enable the NVIC for the transfer complete
  38. interrupt on the two DMA Streams. The output stream should have higher
  39. priority than the input stream HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()
  40. (#)Initialize the CRYP according to the specified parameters :
  41. (##) The data type: 1-bit, 8-bit, 16-bit or 32-bit.
  42. (##) The key size: 128, 192 or 256.
  43. (##) The AlgoMode DES/ TDES Algorithm ECB/CBC or AES Algorithm ECB/CBC/CTR/GCM or CCM.
  44. (##) The initialization vector (counter). It is not used in ECB mode.
  45. (##) The key buffer used for encryption/decryption.
  46. (##) The Header used only in AES GCM and CCM Algorithm for authentication.
  47. (##) The HeaderSize The size of header buffer in word.
  48. (##) The B0 block is the first authentication block used only in AES CCM mode.
  49. (#)Three processing (encryption/decryption) functions are available:
  50. (##) Polling mode: encryption and decryption APIs are blocking functions
  51. i.e. they process the data and wait till the processing is finished,
  52. e.g. HAL_CRYP_Encrypt & HAL_CRYP_Decrypt
  53. (##) Interrupt mode: encryption and decryption APIs are not blocking functions
  54. i.e. they process the data under interrupt,
  55. e.g. HAL_CRYP_Encrypt_IT & HAL_CRYP_Decrypt_IT
  56. (##) DMA mode: encryption and decryption APIs are not blocking functions
  57. i.e. the data transfer is ensured by DMA,
  58. e.g. HAL_CRYP_Encrypt_DMA & HAL_CRYP_Decrypt_DMA
  59. (#)When the processing function is called at first time after HAL_CRYP_Init()
  60. the CRYP peripheral is configured and processes the buffer in input.
  61. At second call, no need to Initialize the CRYP, user have to get current configuration via
  62. HAL_CRYP_GetConfig() API, then only HAL_CRYP_SetConfig() is requested to set
  63. new parametres, finally user can start encryption/decryption.
  64. (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
  65. [..]
  66. The cryptographic processor supports following standards:
  67. (#) The data encryption standard (DES) and Triple-DES (TDES) supported only by CRYP1 IP:
  68. (##)64-bit data block processing
  69. (##) chaining modes supported :
  70. (+++) Electronic Code Book(ECB)
  71. (+++) Cipher Block Chaining (CBC)
  72. (##) keys length supported :64-bit, 128-bit and 192-bit.
  73. (#) The advanced encryption standard (AES) supported by CRYP1:
  74. (##)128-bit data block processing
  75. (##) chaining modes supported :
  76. (+++) Electronic Code Book(ECB)
  77. (+++) Cipher Block Chaining (CBC)
  78. (+++) Counter mode (CTR)
  79. (+++) Galois/counter mode (GCM/GMAC)
  80. (+++) Counter with Cipher Block Chaining-Message(CCM)
  81. (##) keys length Supported :
  82. (+++) for CRYP1 IP: 128-bit, 192-bit and 256-bit.
  83. [..] This section describes the AES Galois/counter mode (GCM) supported by both CRYP1 IP:
  84. (#) Algorithm supported :
  85. (##) Galois/counter mode (GCM)
  86. (##) Galois message authentication code (GMAC) :is exactly the same as
  87. GCM algorithm composed only by an header.
  88. (#) Four phases are performed in GCM :
  89. (##) Init phase: IP prepares the GCM hash subkey (H) and do the IV processing
  90. (##) Header phase: IP processes the Additional Authenticated Data (AAD), with hash
  91. computation only.
  92. (##) Payload phase: IP processes the plaintext (P) with hash computation + keystream
  93. encryption + data XORing. It works in a similar way for ciphertext (C).
  94. (##) Final phase: IP generates the authenticated tag (T) using the last block of data.
  95. (#) structure of message construction in GCM is defined as below :
  96. (##) 16 bytes Initial Counter Block (ICB)composed of IV and counter
  97. (##) The authenticated header A (also knows as Additional Authentication Data AAD)
  98. this part of the message is only authenticated, not encrypted.
  99. (##) The plaintext message P is both authenticated and encrypted as ciphertext.
  100. GCM standard specifies that ciphertext has same bit length as the plaintext.
  101. (##) The last block is composed of the length of A (on 64 bits) and the length of ciphertext
  102. (on 64 bits)
  103. [..] This section describe The AES Counter with Cipher Block Chaining-Message
  104. Authentication Code (CCM) supported by both CRYP1 IP:
  105. (#) Specific parameters for CCM :
  106. (##) B0 block : According to NIST Special Publication 800-38C,
  107. The first block B0 is formatted as follows, where l(m) is encoded in
  108. most-significant-byte first order(see below table 3)
  109. (+++) Q: a bit string representation of the octet length of P (plaintext)
  110. (+++) q The octet length of the binary representation of the octet length of the payload
  111. (+++) A nonce (N), n The octet length of the where n+q=15.
  112. (+++) Flags: most significant octet containing four flags for control information,
  113. (+++) t The octet length of the MAC.
  114. (##) B1 block (header) : associated data length(a) concatenated with Associated Data (A)
  115. the associated data length expressed in bytes (a) defined as below:
  116. (+++) If 0 < a < 216-28, then it is encoded as [a]16, i.e. two octets
  117. (+++) If 216-28 < a < 232, then it is encoded as 0xff || 0xfe || [a]32, i.e. six octets
  118. (+++) If 232 < a < 264, then it is encoded as 0xff || 0xff || [a]64, i.e. ten octets
  119. (##) CTRx block : control blocks
  120. (+++) Generation of CTR1 from first block B0 information :
  121. equal to B0 with first 5 bits zeroed and most significant bits storing octet
  122. length of P also zeroed, then incremented by one ( see below Table 4)
  123. (+++) Generation of CTR0: same as CTR1 with bit[0] set to zero.
  124. (#) Four phases are performed in CCM for CRYP1 IP:
  125. (##) Init phase: IP prepares the GCM hash subkey (H) and do the IV processing
  126. (##) Header phase: IP processes the Additional Authenticated Data (AAD), with hash
  127. computation only.
  128. (##) Payload phase: IP processes the plaintext (P) with hash computation + keystream
  129. encryption + data XORing. It works in a similar way for ciphertext (C).
  130. (##) Final phase: IP generates the authenticated tag (T) using the last block of data.
  131. @endverbatim
  132. Table 1. Initial Counter Block (ICB)
  133. +-------------------------------------------------------+
  134. | Initialization vector (IV) | Counter |
  135. |----------------|----------------|-----------|---------|
  136. 127 95 63 31 0
  137. Bit Number Register Contents
  138. ---------- --------------- -----------
  139. 127 ...96 CRYP_IV1R[31:0] ICB[127:96]
  140. 95 ...64 CRYP_IV1L[31:0] B0[95:64]
  141. 63 ... 32 CRYP_IV0R[31:0] ICB[63:32]
  142. 31 ... 0 CRYP_IV0L[31:0] ICB[31:0], where 32-bit counter= 0x2
  143. Table 2. GCM last block definition
  144. +-------------------------------------------------------------------+
  145. | Bit[0] | Bit[32] | Bit[64] | Bit[96] |
  146. |-----------|--------------------|-----------|----------------------|
  147. | 0x0 | Header length[31:0]| 0x0 | Payload length[31:0] |
  148. |-----------|--------------------|-----------|----------------------|
  149. Table 3. B0 block
  150. Octet Number Contents
  151. ------------ ---------
  152. 0 Flags
  153. 1 ... 15-q Nonce N
  154. 16-q ... 15 Q
  155. the Flags field is formatted as follows:
  156. Bit Number Contents
  157. ---------- ----------------------
  158. 7 Reserved (always zero)
  159. 6 Adata
  160. 5 ... 3 (t-2)/2
  161. 2 ... 0 [q-1]3
  162. Table 4. CTRx block
  163. Bit Number Register Contents
  164. ---------- --------------- -----------
  165. 127 ...96 CRYP_IV1R[31:0] B0[127:96], where Q length bits are set to 0, except for
  166. bit 0 that is set to 1
  167. 95 ...64 CRYP_IV1L[31:0] B0[95:64]
  168. 63 ... 32 CRYP_IV0R[31:0] B0[63:32]
  169. 31 ... 0 CRYP_IV0L[31:0] B0[31:0], where flag bits set to 0
  170. ******************************************************************************
  171. * @attention
  172. *
  173. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  174. *
  175. * Redistribution and use in source and binary forms, with or without modification,
  176. * are permitted provided that the following conditions are met:
  177. * 1. Redistributions of source code must retain the above copyright notice,
  178. * this list of conditions and the following disclaimer.
  179. * 2. Redistributions in binary form must reproduce the above copyright notice,
  180. * this list of conditions and the following disclaimer in the documentation
  181. * and/or other materials provided with the distribution.
  182. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  183. * may be used to endorse or promote products derived from this software
  184. * without specific prior written permission.
  185. *
  186. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  187. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  188. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  189. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  190. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  191. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  192. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  193. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  194. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  195. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  196. *
  197. ******************************************************************************
  198. */
  199. /* Includes ------------------------------------------------------------------*/
  200. #include "stm32h7xx_hal.h"
  201. /** @addtogroup STM32H7xx_HAL_Driver
  202. * @{
  203. */
  204. #if defined (CRYP)
  205. /** @defgroup CRYP CRYP
  206. * @brief CRYP HAL module driver.
  207. * @{
  208. */
  209. #ifdef HAL_CRYP_MODULE_ENABLED
  210. /* Private typedef -----------------------------------------------------------*/
  211. /* Private define ------------------------------------------------------------*/
  212. /** @addtogroup CRYP_Private_Defines
  213. * @{
  214. */
  215. #define CRYP_TIMEOUT_KEYPREPARATION 82U /*The latency of key preparation operation is 82 clock cycles.*/
  216. #define CRYP_TIMEOUT_GCMCCMINITPHASE 299U /* The latency of GCM/CCM init phase to prepare hash subkey is 299 clock cycles.*/
  217. #define CRYP_TIMEOUT_GCMCCMHEADERPHASE 290U /* The latency of GCM/CCM header phase is 290 clock cycles.*/
  218. #define CRYP_PHASE_READY 0x00000001U /*!< CRYP peripheral is ready for initialization. */
  219. #define CRYP_PHASE_PROCESS 0x00000002U /*!< CRYP peripheral is in processing phase */
  220. #define CRYP_PHASE_INIT 0x00000000U /*!< GCM/GMAC (or CCM) init phase */
  221. #define CRYP_PHASE_HEADER CRYP_CR_GCM_CCMPH_0 /*!< GCM/GMAC or CCM header phase */
  222. #define CRYP_PHASE_PAYLOAD CRYP_CR_GCM_CCMPH_1 /*!< GCM(/CCM) payload phase */
  223. #define CRYP_PHASE_FINAL CRYP_CR_GCM_CCMPH /*!< GCM/GMAC or CCM final phase */
  224. #define CRYP_OPERATINGMODE_ENCRYPT 0x00000000U /*!< Encryption mode */
  225. #define CRYP_OPERATINGMODE_DECRYPT CRYP_CR_ALGODIR /*!< Decryption */
  226. /* CTR1 information to use in CCM algorithm */
  227. #define CRYP_CCM_CTR1_0 0x07FFFFFFU
  228. #define CRYP_CCM_CTR1_1 0xFFFFFF00U
  229. #define CRYP_CCM_CTR1_2 0x00000001U
  230. /**
  231. * @}
  232. */
  233. /* Private macro -------------------------------------------------------------*/
  234. /** @addtogroup CRYP_Private_Macros
  235. * @{
  236. */
  237. #define CRYP_SET_PHASE(__HANDLE__, __PHASE__) do{(__HANDLE__)->Instance->CR &= (uint32_t)(~CRYP_CR_GCM_CCMPH);\
  238. (__HANDLE__)->Instance->CR |= (uint32_t)(__PHASE__);\
  239. }while(0)
  240. #define HAL_CRYP_FIFO_FLUSH(__HANDLE__) ((__HANDLE__)->Instance->CR |= CRYP_CR_FFLUSH)
  241. /**
  242. * @}
  243. */
  244. /* Private struct -------------------------------------------------------------*/
  245. /* Private variables ---------------------------------------------------------*/
  246. /* Private function prototypes -----------------------------------------------*/
  247. /** @addtogroup CRYP_Private_Functions_prototypes
  248. * @{
  249. */
  250. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
  251. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma);
  252. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma);
  253. static void CRYP_DMAError(DMA_HandleTypeDef *hdma);
  254. static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint32_t *Key, uint32_t KeySize);
  255. static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp);
  256. static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  257. static HAL_StatusTypeDef CRYP_GCMCCM_SetPayloadPhase_IT(CRYP_HandleTypeDef *hcryp);
  258. static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase_IT(CRYP_HandleTypeDef *hcryp);
  259. static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase_DMA(CRYP_HandleTypeDef *hcryp);
  260. static void CRYP_Workaround(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  261. static HAL_StatusTypeDef CRYP_AESGCM_Process_DMA(CRYP_HandleTypeDef *hcryp);
  262. static HAL_StatusTypeDef CRYP_AESGCM_Process_IT (CRYP_HandleTypeDef *hcryp);
  263. static HAL_StatusTypeDef CRYP_AESGCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  264. static HAL_StatusTypeDef CRYP_AESCCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  265. static HAL_StatusTypeDef CRYP_AESCCM_Process_IT(CRYP_HandleTypeDef *hcryp);
  266. static HAL_StatusTypeDef CRYP_AESCCM_Process_DMA(CRYP_HandleTypeDef *hcryp);
  267. static HAL_StatusTypeDef CRYP_AES_ProcessData(CRYP_HandleTypeDef *hcrypt, uint32_t Timeout);
  268. static HAL_StatusTypeDef CRYP_AES_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  269. static HAL_StatusTypeDef CRYP_AES_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  270. static HAL_StatusTypeDef CRYP_AES_Decrypt_IT(CRYP_HandleTypeDef *hcryp);
  271. static HAL_StatusTypeDef CRYP_AES_Encrypt_IT(CRYP_HandleTypeDef *hcryp);
  272. static HAL_StatusTypeDef CRYP_AES_Decrypt_DMA(CRYP_HandleTypeDef *hcryp);
  273. static HAL_StatusTypeDef CRYP_TDES_IT(CRYP_HandleTypeDef *hcryp);
  274. static HAL_StatusTypeDef CRYP_WaitOnIFEMFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  275. static HAL_StatusTypeDef CRYP_WaitOnBUSYFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  276. static HAL_StatusTypeDef CRYP_WaitOnOFNEFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  277. static HAL_StatusTypeDef CRYP_TDES_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  278. /**
  279. * @}
  280. */
  281. /* Exported functions ---------------------------------------------------------*/
  282. /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
  283. * @{
  284. */
  285. /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions
  286. * @brief CRYP Initialization and Configuration functions.
  287. *
  288. @verbatim
  289. ========================================================================================
  290. ##### Initialization, de-initialization and Set and Get configuration functions #####
  291. ========================================================================================
  292. [..] This section provides functions allowing to:
  293. (+) Initialize the CRYP
  294. (+) DeInitialize the CRYP
  295. (+) Initialize the CRYP MSP
  296. (+) DeInitialize the CRYP MSP
  297. (+) configure CRYP (HAL_CRYP_SetConfig) with the specified parameters in the CRYP_ConfigTypeDef
  298. Parameters which are configured in This section are :
  299. (++) Key size
  300. (++) Data Type : 32,16, 8 or 1bit
  301. (++) AlgoMode : for CRYP1 IP
  302. ECB and CBC in DES/TDES Standard
  303. ECB,CBC,CTR,GCM/GMAC and CCM in AES Standard.
  304. (+) Get CRYP configuration (HAL_CRYP_GetConfig) from the specified parameters in the CRYP_HandleTypeDef
  305. @endverbatim
  306. * @{
  307. */
  308. /**
  309. * @brief Initializes the CRYP according to the specified
  310. * parameters in the CRYP_ConfigTypeDef and creates the associated handle.
  311. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  312. * the configuration information for CRYP module
  313. * @retval HAL status
  314. */
  315. HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
  316. {
  317. /* Check the CRYP handle allocation */
  318. if(hcryp == NULL)
  319. {
  320. return HAL_ERROR;
  321. }
  322. /* Check parameters */
  323. assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
  324. assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
  325. assert_param(IS_CRYP_ALGORITHM(hcryp->Init.Algorithm));
  326. if(hcryp->State == HAL_CRYP_STATE_RESET)
  327. {
  328. /* Allocate lock resource and initialize it */
  329. hcryp->Lock = HAL_UNLOCKED;
  330. /* Init the low level hardware */
  331. HAL_CRYP_MspInit(hcryp);
  332. }
  333. /* Set the key size(This bit field is ‘don’t care’ in the DES or TDES modes) data type and Algorithm */
  334. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_DATATYPE|CRYP_CR_KEYSIZE|CRYP_CR_ALGOMODE, hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm);
  335. /* Reset Error Code field */
  336. hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
  337. /* Change the CRYP state */
  338. hcryp->State = HAL_CRYP_STATE_READY;
  339. /* Set the default CRYP phase */
  340. hcryp->Phase = CRYP_PHASE_READY;
  341. /* Return function status */
  342. return HAL_OK;
  343. }
  344. /**
  345. * @brief De-Initializes the CRYP peripheral.
  346. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  347. * the configuration information for CRYP module
  348. * @retval HAL status
  349. */
  350. HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
  351. {
  352. /* Check the CRYP handle allocation */
  353. if(hcryp == NULL)
  354. {
  355. return HAL_ERROR;
  356. }
  357. /* Set the default CRYP phase */
  358. hcryp->Phase = CRYP_PHASE_READY;
  359. /* Reset CrypInCount and CrypOutCount */
  360. hcryp->CrypInCount = 0;
  361. hcryp->CrypOutCount = 0;
  362. hcryp->CrypHeaderCount =0;
  363. /* Disable the CRYP peripheral clock */
  364. __HAL_CRYP_DISABLE(hcryp);
  365. /* DeInit the low level hardware: CLOCK, NVIC.*/
  366. HAL_CRYP_MspDeInit(hcryp);
  367. /* Change the CRYP state */
  368. hcryp->State = HAL_CRYP_STATE_RESET;
  369. /* Release Lock */
  370. __HAL_UNLOCK(hcryp);
  371. /* Return function status */
  372. return HAL_OK;
  373. }
  374. /**
  375. * @brief Configure the CRYP according to the specified
  376. * parameters in the CRYP_ConfigTypeDef
  377. * @param hcryp: pointer to a CRYP_HandleTypeDef structure
  378. * @param pConf: pointer to a CRYP_ConfigTypeDef structure that contains
  379. * the configuration information for CRYP module
  380. * @retval HAL status
  381. */
  382. HAL_StatusTypeDef HAL_CRYP_SetConfig(CRYP_HandleTypeDef *hcryp, CRYP_ConfigTypeDef *pConf )
  383. {
  384. /* Check the CRYP handle allocation */
  385. if((hcryp == NULL)|| (pConf == NULL) )
  386. {
  387. return HAL_ERROR;
  388. }
  389. /* Check parameters */
  390. assert_param(IS_CRYP_KEYSIZE(pConf->KeySize));
  391. assert_param(IS_CRYP_DATATYPE(pConf->DataType));
  392. assert_param(IS_CRYP_ALGORITHM(pConf->Algorithm));
  393. if(hcryp->State == HAL_CRYP_STATE_READY)
  394. {
  395. /* Change the CRYP state */
  396. hcryp->State = HAL_CRYP_STATE_BUSY;
  397. /* Process locked */
  398. __HAL_LOCK(hcryp);
  399. /* Set CRYP parameters */
  400. hcryp->Init.DataType = pConf->DataType;
  401. hcryp->Init.pKey = pConf->pKey;
  402. hcryp->Init.Algorithm = pConf->Algorithm;
  403. hcryp->Init.KeySize = pConf->KeySize;
  404. hcryp->Init.pInitVect = pConf->pInitVect;
  405. hcryp->Init.Header = pConf->Header;
  406. hcryp->Init.HeaderSize = pConf->HeaderSize;
  407. hcryp->Init.B0 = pConf->B0;
  408. /* Set the key size(This bit field is ‘don’t care’ in the DES or TDES modes) data type, AlgoMode and operating mode*/
  409. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_DATATYPE|CRYP_CR_KEYSIZE|CRYP_CR_ALGOMODE, hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm);
  410. /* Process Unlocked */
  411. __HAL_UNLOCK(hcryp);
  412. /* Reset Error Code field */
  413. hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
  414. /* Change the CRYP state */
  415. hcryp->State = HAL_CRYP_STATE_READY;
  416. /* Set the default CRYP phase */
  417. hcryp->Phase = CRYP_PHASE_READY;
  418. /* Return function status */
  419. return HAL_OK;
  420. }
  421. else
  422. {
  423. /* Process Unlocked */
  424. __HAL_UNLOCK(hcryp);
  425. /* Busy error code field */
  426. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  427. return HAL_ERROR;
  428. }
  429. }
  430. /**
  431. * @brief Get CRYP Configuration parameters in associated handle.
  432. * @param pConf: pointer to a CRYP_ConfigTypeDef structure
  433. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  434. * the configuration information for CRYP module
  435. * @retval HAL status
  436. */
  437. HAL_StatusTypeDef HAL_CRYP_GetConfig(CRYP_HandleTypeDef *hcryp, CRYP_ConfigTypeDef *pConf )
  438. {
  439. /* Check the CRYP handle allocation */
  440. if((hcryp == NULL)|| (pConf == NULL) )
  441. {
  442. return HAL_ERROR;
  443. }
  444. if(hcryp->State == HAL_CRYP_STATE_READY)
  445. {
  446. /* Change the CRYP state */
  447. hcryp->State = HAL_CRYP_STATE_BUSY;
  448. /* Process locked */
  449. __HAL_LOCK(hcryp);
  450. /* Get CRYP parameters */
  451. pConf->DataType = hcryp->Init.DataType;
  452. pConf->pKey = hcryp->Init.pKey;
  453. pConf->Algorithm = hcryp->Init.Algorithm;
  454. pConf->KeySize = hcryp->Init.KeySize ;
  455. pConf->pInitVect = hcryp->Init.pInitVect;
  456. pConf->Header = hcryp->Init.Header ;
  457. pConf->HeaderSize = hcryp->Init.HeaderSize;
  458. pConf->B0 = hcryp->Init.B0;
  459. /* Process Unlocked */
  460. __HAL_UNLOCK(hcryp);
  461. /* Change the CRYP state */
  462. hcryp->State = HAL_CRYP_STATE_READY;
  463. /* Return function status */
  464. return HAL_OK;
  465. }
  466. else
  467. {
  468. /* Process Unlocked */
  469. __HAL_UNLOCK(hcryp);
  470. /* Busy error code field */
  471. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  472. return HAL_ERROR;
  473. }
  474. }
  475. /**
  476. * @brief Initializes the CRYP MSP.
  477. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  478. * the configuration information for CRYP module
  479. * @retval None
  480. */
  481. __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
  482. {
  483. /* Prevent unused argument(s) compilation warning */
  484. UNUSED(hcryp);
  485. /* NOTE : This function Should not be modified, when the callback is needed,
  486. the HAL_CRYP_MspInit could be implemented in the user file
  487. */
  488. }
  489. /**
  490. * @brief DeInitializes CRYP MSP.
  491. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  492. * the configuration information for CRYP module
  493. * @retval None
  494. */
  495. __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
  496. {
  497. /* Prevent unused argument(s) compilation warning */
  498. UNUSED(hcryp);
  499. /* NOTE : This function Should not be modified, when the callback is needed,
  500. the HAL_CRYP_MspDeInit could be implemented in the user file
  501. */
  502. }
  503. /**
  504. * @}
  505. */
  506. /** @defgroup CRYP_Exported_Functions_Group2 Encrypt Decrypt functions
  507. * @brief CRYP processing functions.
  508. *
  509. @verbatim
  510. ==============================================================================
  511. ##### Encrypt Decrypt functions #####
  512. ==============================================================================
  513. [..] This section provides API allowing to Encrypt/Decrypt Data following
  514. Standard DES/TDES or AES, and Algorithm configured by the user:
  515. (+) Standard DES/TDES only supported by CRYP1 IP, below list of Algorithm supported :
  516. (++) Electronic Code Book(ECB)
  517. (++) Cipher Block Chaining (CBC)
  518. (+) Standard AES supported by CRYP1 IP , list of Algorithm supported:
  519. (++) Electronic Code Book(ECB)
  520. (++) Cipher Block Chaining (CBC)
  521. (++) Counter mode (CTR)
  522. (++) Cipher Block Chaining (CBC)
  523. (++) Counter mode (CTR)
  524. (++) Galois/counter mode (GCM)
  525. (++) Counter with Cipher Block Chaining-Message(CCM)
  526. [..] Three processing functions are available:
  527. (+) Polling mode : HAL_CRYP_Encrypt & HAL_CRYP_Decrypt
  528. (+) Interrupt mode : HAL_CRYP_Encrypt_IT & HAL_CRYP_Decrypt_IT
  529. (+) DMA mode : HAL_CRYP_Encrypt_DMA & HAL_CRYP_Decrypt_DMA
  530. @endverbatim
  531. * @{
  532. */
  533. /**
  534. * @brief Encryption mode.
  535. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  536. * the configuration information for CRYP module
  537. * @param Input: Pointer to the input buffer (plaintext)
  538. * @param Size: Length of the plaintext buffer in word.
  539. * @param Output: Pointer to the output buffer(ciphertext)
  540. * @param Timeout: Specify Timeout value
  541. * @retval HAL status
  542. */
  543. HAL_StatusTypeDef HAL_CRYP_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output, uint32_t Timeout)
  544. {
  545. uint32_t algo = 0U;
  546. HAL_StatusTypeDef state;
  547. if(hcryp->State == HAL_CRYP_STATE_READY)
  548. {
  549. /* Change state Busy */
  550. hcryp->State = HAL_CRYP_STATE_BUSY;
  551. /* Process locked */
  552. __HAL_LOCK(hcryp);
  553. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr, pCrypOutBuffPtr and Size parameters*/
  554. hcryp->CrypInCount = 0U;
  555. hcryp->CrypOutCount = 0U;
  556. hcryp->pCrypInBuffPtr = Input;
  557. hcryp->pCrypOutBuffPtr = Output;
  558. hcryp->Size = Size;
  559. /* Set Encryption operating mode*/
  560. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_ENCRYPT);
  561. /* algo get algorithm selected */
  562. algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
  563. switch(algo)
  564. {
  565. case CRYP_DES_ECB:
  566. case CRYP_DES_CBC:
  567. case CRYP_TDES_ECB:
  568. case CRYP_TDES_CBC:
  569. /*Set Key */
  570. hcryp->Instance->K1LR = *(uint32_t*)(hcryp->Init.pKey);
  571. hcryp->Instance->K1RR = *(uint32_t*)(hcryp->Init.pKey+1);
  572. if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  573. {
  574. hcryp->Instance->K2LR = *(uint32_t*)(hcryp->Init.pKey+2);
  575. hcryp->Instance->K2RR = *(uint32_t*)(hcryp->Init.pKey+3);
  576. hcryp->Instance->K3LR = *(uint32_t*)(hcryp->Init.pKey+4);
  577. hcryp->Instance->K3RR = *(uint32_t*)(hcryp->Init.pKey+5);
  578. }
  579. /*Set Initialization Vector (IV)*/
  580. if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  581. {
  582. hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
  583. hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
  584. }
  585. /* Flush FIFO */
  586. HAL_CRYP_FIFO_FLUSH(hcryp);
  587. /* Set the phase */
  588. hcryp->Phase = CRYP_PHASE_PROCESS;
  589. /* Statrt DES/TDES encryption process */
  590. state = CRYP_TDES_Process(hcryp,Timeout);
  591. break;
  592. case CRYP_AES_ECB:
  593. case CRYP_AES_CBC:
  594. case CRYP_AES_CTR:
  595. /* AES encryption */
  596. state = CRYP_AES_Encrypt(hcryp, Timeout);
  597. break;
  598. case CRYP_AES_GCM:
  599. /* AES GCM encryption */
  600. state = CRYP_AESGCM_Process(hcryp, Timeout);
  601. break;
  602. case CRYP_AES_CCM:
  603. /* AES CCM encryption */
  604. state = CRYP_AESCCM_Process(hcryp,Timeout);
  605. break;
  606. default:
  607. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  608. return HAL_ERROR;
  609. }
  610. if (state == HAL_OK)
  611. {
  612. /* Change the CRYP peripheral state */
  613. hcryp->State = HAL_CRYP_STATE_READY;
  614. /* Process unlocked */
  615. __HAL_UNLOCK(hcryp);
  616. }
  617. }
  618. else
  619. {
  620. /* Busy error code field */
  621. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  622. return HAL_ERROR;
  623. }
  624. /* Return function status */
  625. return HAL_OK;
  626. }
  627. /**
  628. * @brief Decryption mode.
  629. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  630. * the configuration information for CRYP module
  631. * @param Input: Pointer to the input buffer (ciphertext )
  632. * @param Size: Length of the plaintext buffer in word.
  633. * @param Output: Pointer to the output buffer(plaintext)
  634. * @param Timeout: Specify Timeout value
  635. * @retval HAL status
  636. */
  637. HAL_StatusTypeDef HAL_CRYP_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output, uint32_t Timeout)
  638. {
  639. HAL_StatusTypeDef state;
  640. uint32_t algo = 0U;
  641. if(hcryp->State == HAL_CRYP_STATE_READY)
  642. {
  643. /* Change state Busy */
  644. hcryp->State = HAL_CRYP_STATE_BUSY;
  645. /* Process locked */
  646. __HAL_LOCK(hcryp);
  647. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr, pCrypOutBuffPtr and Size parameters*/
  648. hcryp->CrypInCount = 0U;
  649. hcryp->CrypOutCount = 0U;
  650. hcryp->pCrypInBuffPtr = Input;
  651. hcryp->pCrypOutBuffPtr = Output;
  652. hcryp->Size = Size;
  653. /* Set Decryption operating mode*/
  654. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_DECRYPT);
  655. /* algo get algorithm selected */
  656. algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
  657. switch(algo)
  658. {
  659. case CRYP_DES_ECB:
  660. case CRYP_DES_CBC:
  661. case CRYP_TDES_ECB:
  662. case CRYP_TDES_CBC:
  663. /*Set Key */
  664. hcryp->Instance->K1LR = *(uint32_t*)(hcryp->Init.pKey);
  665. hcryp->Instance->K1RR = *(uint32_t*)(hcryp->Init.pKey+1);
  666. if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  667. {
  668. hcryp->Instance->K2LR = *(uint32_t*)(hcryp->Init.pKey+2);
  669. hcryp->Instance->K2RR = *(uint32_t*)(hcryp->Init.pKey+3);
  670. hcryp->Instance->K3LR = *(uint32_t*)(hcryp->Init.pKey+4);
  671. hcryp->Instance->K3RR = *(uint32_t*)(hcryp->Init.pKey+5);
  672. }
  673. /*Set Initialization Vector (IV)*/
  674. if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  675. {
  676. hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
  677. hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
  678. }
  679. /* Flush FIFO */
  680. HAL_CRYP_FIFO_FLUSH(hcryp);
  681. /* Set the phase */
  682. hcryp->Phase = CRYP_PHASE_PROCESS;
  683. /* Start DES/TDES decryption process */
  684. state = CRYP_TDES_Process(hcryp, Timeout);
  685. break;
  686. case CRYP_AES_ECB:
  687. case CRYP_AES_CBC:
  688. case CRYP_AES_CTR:
  689. /* AES decryption */
  690. state = CRYP_AES_Decrypt(hcryp, Timeout);
  691. break;
  692. case CRYP_AES_GCM:
  693. /* AES GCM decryption */
  694. state = CRYP_AESGCM_Process (hcryp, Timeout) ;
  695. break;
  696. case CRYP_AES_CCM:
  697. /* AES CCM decryption */
  698. state = CRYP_AESCCM_Process(hcryp, Timeout);
  699. break;
  700. default:
  701. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  702. return HAL_ERROR;
  703. }
  704. if (state == HAL_OK)
  705. {
  706. /* Change the CRYP peripheral state */
  707. hcryp->State = HAL_CRYP_STATE_READY;
  708. /* Process unlocked */
  709. __HAL_UNLOCK(hcryp);
  710. }
  711. }
  712. else
  713. {
  714. /* Busy error code field */
  715. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  716. return HAL_ERROR;
  717. }
  718. /* Return function status */
  719. return HAL_OK;
  720. }
  721. /**
  722. * @brief Encryption in interrupt mode.
  723. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  724. * the configuration information for CRYP module
  725. * @param Input: Pointer to the input buffer (plaintext)
  726. * @param Size: Length of the plaintext buffer in word
  727. * @param Output: Pointer to the output buffer(ciphertext)
  728. * @retval HAL status
  729. */
  730. HAL_StatusTypeDef HAL_CRYP_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
  731. {
  732. uint32_t algo = 0U;
  733. if(hcryp->State == HAL_CRYP_STATE_READY)
  734. {
  735. /* Change state Busy */
  736. hcryp->State = HAL_CRYP_STATE_BUSY;
  737. /* Process locked */
  738. __HAL_LOCK(hcryp);
  739. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr, pCrypOutBuffPtr and Size parameters*/
  740. hcryp->CrypInCount = 0U;
  741. hcryp->CrypOutCount = 0U;
  742. hcryp->pCrypInBuffPtr = Input;
  743. hcryp->pCrypOutBuffPtr = Output;
  744. hcryp->Size = Size;
  745. /* Set encryption operating mode*/
  746. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_ENCRYPT);
  747. /* algo get algorithm selected */
  748. algo = (hcryp->Instance->CR & CRYP_CR_ALGOMODE);
  749. switch(algo)
  750. {
  751. case CRYP_DES_ECB:
  752. case CRYP_DES_CBC:
  753. case CRYP_TDES_ECB:
  754. case CRYP_TDES_CBC:
  755. /*Set Key */
  756. hcryp->Instance->K1LR = *(uint32_t*)(hcryp->Init.pKey);
  757. hcryp->Instance->K1RR = *(uint32_t*)(hcryp->Init.pKey+1);
  758. if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  759. {
  760. hcryp->Instance->K2LR = *(uint32_t*)(hcryp->Init.pKey+2);
  761. hcryp->Instance->K2RR = *(uint32_t*)(hcryp->Init.pKey+3);
  762. hcryp->Instance->K3LR = *(uint32_t*)(hcryp->Init.pKey+4);
  763. hcryp->Instance->K3RR = *(uint32_t*)(hcryp->Init.pKey+5);
  764. }
  765. /* Set the Initialization Vector*/
  766. if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  767. {
  768. hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
  769. hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
  770. }
  771. /* Flush FIFO */
  772. HAL_CRYP_FIFO_FLUSH(hcryp);
  773. /* Set the phase */
  774. hcryp->Phase = CRYP_PHASE_PROCESS;
  775. /* Enable interrupts */
  776. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  777. /* Enable CRYP to start DES/TDES process*/
  778. __HAL_CRYP_ENABLE(hcryp);
  779. break;
  780. case CRYP_AES_ECB:
  781. case CRYP_AES_CBC:
  782. case CRYP_AES_CTR:
  783. CRYP_AES_Encrypt_IT(hcryp);
  784. break;
  785. case CRYP_AES_GCM:
  786. CRYP_AESGCM_Process_IT (hcryp) ;
  787. break;
  788. case CRYP_AES_CCM:
  789. CRYP_AESCCM_Process_IT(hcryp);
  790. break;
  791. default:
  792. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  793. return HAL_ERROR;
  794. }
  795. }
  796. else
  797. {
  798. /* Busy error code field */
  799. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  800. return HAL_ERROR;
  801. }
  802. /* Return function status */
  803. return HAL_OK;
  804. }
  805. /**
  806. * @brief Decryption in itnterrupt mode.
  807. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  808. * the configuration information for CRYP module
  809. * @param Input: Pointer to the input buffer (ciphertext )
  810. * @param Size: Length of the plaintext buffer in word.
  811. * @param Output: Pointer to the output buffer(plaintext)
  812. * @retval HAL status
  813. */
  814. HAL_StatusTypeDef HAL_CRYP_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
  815. {
  816. uint32_t algo = 0U;
  817. if(hcryp->State == HAL_CRYP_STATE_READY)
  818. {
  819. /* Change state Busy */
  820. hcryp->State = HAL_CRYP_STATE_BUSY;
  821. /* Process locked */
  822. __HAL_LOCK(hcryp);
  823. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr, pCrypOutBuffPtr and Size parameters*/
  824. hcryp->CrypInCount = 0U;
  825. hcryp->CrypOutCount = 0U;
  826. hcryp->pCrypInBuffPtr = Input;
  827. hcryp->pCrypOutBuffPtr = Output;
  828. hcryp->Size = Size;
  829. /* Set decryption operating mode*/
  830. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR,CRYP_OPERATINGMODE_DECRYPT);
  831. /* algo get algorithm selected */
  832. algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
  833. switch(algo)
  834. {
  835. case CRYP_DES_ECB:
  836. case CRYP_DES_CBC:
  837. case CRYP_TDES_ECB:
  838. case CRYP_TDES_CBC:
  839. /*Set Key */
  840. hcryp->Instance->K1LR = *(uint32_t*)(hcryp->Init.pKey);
  841. hcryp->Instance->K1RR = *(uint32_t*)(hcryp->Init.pKey+1);
  842. if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  843. {
  844. hcryp->Instance->K2LR = *(uint32_t*)(hcryp->Init.pKey+2);
  845. hcryp->Instance->K2RR = *(uint32_t*)(hcryp->Init.pKey+3);
  846. hcryp->Instance->K3LR = *(uint32_t*)(hcryp->Init.pKey+4);
  847. hcryp->Instance->K3RR = *(uint32_t*)(hcryp->Init.pKey+5);
  848. }
  849. /* Set the Initialization Vector*/
  850. if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  851. {
  852. hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
  853. hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
  854. }
  855. /* Flush FIFO */
  856. HAL_CRYP_FIFO_FLUSH(hcryp);
  857. /* Set the phase */
  858. hcryp->Phase = CRYP_PHASE_PROCESS;
  859. /* Enable interrupts */
  860. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  861. /* Enable CRYP and start DES/TDES process*/
  862. __HAL_CRYP_ENABLE(hcryp);
  863. break;
  864. case CRYP_AES_ECB:
  865. case CRYP_AES_CBC:
  866. case CRYP_AES_CTR:
  867. /* AES decryption */
  868. CRYP_AES_Decrypt_IT(hcryp);
  869. break;
  870. case CRYP_AES_GCM:
  871. /* AES GCM decryption */
  872. CRYP_AESGCM_Process_IT (hcryp) ;
  873. break;
  874. case CRYP_AES_CCM:
  875. /* AES CCMdecryption */
  876. CRYP_AESCCM_Process_IT(hcryp);
  877. break;
  878. default:
  879. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  880. return HAL_ERROR;
  881. }
  882. }
  883. else
  884. {
  885. /* Busy error code field */
  886. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  887. return HAL_ERROR;
  888. }
  889. /* Return function status */
  890. return HAL_OK;
  891. }
  892. /**
  893. * @brief Encryption in DMA mode.
  894. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  895. * the configuration information for CRYP module
  896. * @param Input: Pointer to the input buffer (plaintext)
  897. * @param Size: Length of the plaintext buffer in word.
  898. * @param Output: Pointer to the output buffer(ciphertext)
  899. * @retval HAL status
  900. */
  901. HAL_StatusTypeDef HAL_CRYP_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
  902. {
  903. uint32_t algo = 0U;
  904. if(hcryp->State == HAL_CRYP_STATE_READY)
  905. {
  906. /* Change state Busy */
  907. hcryp->State = HAL_CRYP_STATE_BUSY;
  908. /* Process locked */
  909. __HAL_LOCK(hcryp);
  910. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr, pCrypOutBuffPtr and Size parameters*/
  911. hcryp->CrypInCount = 0U;
  912. hcryp->CrypOutCount = 0U;
  913. hcryp->pCrypInBuffPtr = Input;
  914. hcryp->pCrypOutBuffPtr = Output;
  915. hcryp->Size = Size;
  916. /* Set encryption operating mode*/
  917. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_ENCRYPT);
  918. /* algo get algorithm selected */
  919. algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
  920. switch(algo)
  921. {
  922. case CRYP_DES_ECB:
  923. case CRYP_DES_CBC:
  924. case CRYP_TDES_ECB:
  925. case CRYP_TDES_CBC:
  926. /*Set Key */
  927. hcryp->Instance->K1LR = *(uint32_t*)(hcryp->Init.pKey);
  928. hcryp->Instance->K1RR = *(uint32_t*)(hcryp->Init.pKey+1);
  929. if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  930. {
  931. hcryp->Instance->K2LR = *(uint32_t*)(hcryp->Init.pKey+2);
  932. hcryp->Instance->K2RR = *(uint32_t*)(hcryp->Init.pKey+3);
  933. hcryp->Instance->K3LR = *(uint32_t*)(hcryp->Init.pKey+4);
  934. hcryp->Instance->K3RR = *(uint32_t*)(hcryp->Init.pKey+5);
  935. }
  936. /* Set the Initialization Vector*/
  937. if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  938. {
  939. hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
  940. hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
  941. }
  942. /* Flush FIFO */
  943. HAL_CRYP_FIFO_FLUSH(hcryp);
  944. /* Set the phase */
  945. hcryp->Phase = CRYP_PHASE_PROCESS;
  946. /* Start DMA process transfer for DES/TDES */
  947. CRYP_SetDMAConfig(hcryp, (uint32_t)( hcryp->pCrypInBuffPtr), (hcryp->Size), (uint32_t)(hcryp->pCrypOutBuffPtr));
  948. break;
  949. case CRYP_AES_ECB:
  950. case CRYP_AES_CBC:
  951. case CRYP_AES_CTR:
  952. /* Set the Key*/
  953. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  954. /* Set the Initialization Vector IV */
  955. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  956. {
  957. hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
  958. hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
  959. hcryp->Instance->IV1LR = *(uint32_t*)(hcryp->Init.pInitVect+2);
  960. hcryp->Instance->IV1RR = *(uint32_t*)(hcryp->Init.pInitVect+3);
  961. }
  962. /* Set the phase */
  963. hcryp->Phase = CRYP_PHASE_PROCESS;
  964. /* Start DMA process transfer for AES */
  965. CRYP_SetDMAConfig(hcryp, (uint32_t)( hcryp->pCrypInBuffPtr), Size, (uint32_t)(hcryp->pCrypOutBuffPtr));
  966. break;
  967. case CRYP_AES_GCM:
  968. /* AES GCM encryption */
  969. CRYP_AESGCM_Process_DMA (hcryp) ;
  970. break;
  971. case CRYP_AES_CCM:
  972. /* AES CCM encryption */
  973. CRYP_AESCCM_Process_DMA(hcryp);
  974. break;
  975. default:
  976. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  977. return HAL_ERROR;
  978. }
  979. }
  980. else
  981. {
  982. /* Busy error code field */
  983. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  984. return HAL_ERROR;
  985. }
  986. /* Return function status */
  987. return HAL_OK;
  988. }
  989. /**
  990. * @brief Decryption in DMA mode.
  991. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  992. * the configuration information for CRYP module
  993. * @param Input: Pointer to the input buffer (ciphertext )
  994. * @param Size: Length of the plaintext buffer in word
  995. * @param Output: Pointer to the output buffer(plaintext)
  996. * @retval HAL status
  997. */
  998. HAL_StatusTypeDef HAL_CRYP_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
  999. {
  1000. uint32_t algo = 0U;
  1001. if(hcryp->State == HAL_CRYP_STATE_READY)
  1002. {
  1003. /* Change state Busy */
  1004. hcryp->State = HAL_CRYP_STATE_BUSY;
  1005. /* Process locked */
  1006. __HAL_LOCK(hcryp);
  1007. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr, pCrypOutBuffPtr and Size parameters*/
  1008. hcryp->CrypInCount = 0U;
  1009. hcryp->CrypOutCount = 0U;
  1010. hcryp->pCrypInBuffPtr = Input;
  1011. hcryp->pCrypOutBuffPtr = Output;
  1012. hcryp->Size = Size;
  1013. /* Set decryption operating mode*/
  1014. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_DECRYPT);
  1015. /* algo get algorithm selected */
  1016. algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
  1017. switch(algo)
  1018. {
  1019. case CRYP_DES_ECB:
  1020. case CRYP_DES_CBC:
  1021. case CRYP_TDES_ECB:
  1022. case CRYP_TDES_CBC:
  1023. /*Set Key */
  1024. hcryp->Instance->K1LR = *(uint32_t*)(hcryp->Init.pKey);
  1025. hcryp->Instance->K1RR = *(uint32_t*)(hcryp->Init.pKey+1);
  1026. if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1027. {
  1028. hcryp->Instance->K2LR = *(uint32_t*)(hcryp->Init.pKey+2);
  1029. hcryp->Instance->K2RR = *(uint32_t*)(hcryp->Init.pKey+3);
  1030. hcryp->Instance->K3LR = *(uint32_t*)(hcryp->Init.pKey+4);
  1031. hcryp->Instance->K3RR = *(uint32_t*)(hcryp->Init.pKey+5);
  1032. }
  1033. /* Set the Initialization Vector*/
  1034. if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1035. {
  1036. hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
  1037. hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
  1038. }
  1039. /* Flush FIFO */
  1040. HAL_CRYP_FIFO_FLUSH(hcryp);
  1041. /* Set the phase */
  1042. hcryp->Phase = CRYP_PHASE_PROCESS;
  1043. /* Start DMA process transfer for DES/TDES */
  1044. CRYP_SetDMAConfig(hcryp, (uint32_t)( hcryp->pCrypInBuffPtr), (hcryp->Size), (uint32_t)(hcryp->pCrypOutBuffPtr));
  1045. break;
  1046. case CRYP_AES_ECB:
  1047. case CRYP_AES_CBC:
  1048. case CRYP_AES_CTR:
  1049. /* AES decryption */
  1050. CRYP_AES_Decrypt_DMA(hcryp);
  1051. break;
  1052. case CRYP_AES_GCM:
  1053. /* AES GCM decryption */
  1054. CRYP_AESGCM_Process_DMA (hcryp) ;
  1055. break;
  1056. case CRYP_AES_CCM:
  1057. /* AES CCM decryption */
  1058. CRYP_AESCCM_Process_DMA(hcryp);
  1059. break;
  1060. default:
  1061. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1062. return HAL_ERROR;
  1063. }
  1064. }
  1065. else
  1066. {
  1067. /* Busy error code field */
  1068. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1069. return HAL_ERROR;
  1070. }
  1071. /* Return function status */
  1072. return HAL_OK;
  1073. }
  1074. /**
  1075. * @}
  1076. */
  1077. /** @defgroup CRYP_Exported_Functions_Group3 CRYP IRQ handler management
  1078. * @brief CRYP IRQ handler.
  1079. *
  1080. @verbatim
  1081. ==============================================================================
  1082. ##### CRYP IRQ handler management #####
  1083. ==============================================================================
  1084. [..] This section provides CRYP IRQ handler and callback functions.
  1085. (+) HAL_CRYP_IRQHandler CRYP interrupt request
  1086. (+) HAL_CRYP_InCpltCallback input data transfer complete callback
  1087. (+) HAL_CRYP_OutCpltCallback output data transfer complete callback
  1088. (+) HAL_CRYP_ErrorCallback CRYP error callback
  1089. (+) HAL_CRYP_GetState return the CRYP state
  1090. (+) HAL_CRYP_GetError return the CRYP error code
  1091. @endverbatim
  1092. * @{
  1093. */
  1094. /**
  1095. * @brief This function handles cryptographic interrupt request.
  1096. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1097. * the configuration information for CRYP module
  1098. * @retval None
  1099. */
  1100. void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
  1101. {
  1102. uint32_t CurrentMode = 0U;
  1103. /*put CRYP_IT_OUTI flag status in CurrentMode variable*/
  1104. CurrentMode = __HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI);
  1105. if((__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI) != RESET)| (CurrentMode != RESET))
  1106. {
  1107. if ((hcryp->Init.Algorithm == CRYP_DES_ECB)|| (hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1108. {
  1109. CRYP_TDES_IT(hcryp); /* DES or TDES*/
  1110. }
  1111. else if((hcryp->Init.Algorithm == CRYP_AES_ECB) || (hcryp->Init.Algorithm == CRYP_AES_CBC) || (hcryp->Init.Algorithm == CRYP_AES_CTR))
  1112. {
  1113. CRYP_AES_IT(hcryp); /*AES*/
  1114. }
  1115. else if((hcryp->Init.Algorithm == CRYP_AES_GCM) ||(hcryp->Init.Algorithm == CRYP_CR_ALGOMODE_AES_CCM) )
  1116. {
  1117. /* if header phase */
  1118. if ((hcryp->Instance->CR & CRYP_PHASE_HEADER) == CRYP_PHASE_HEADER )
  1119. {
  1120. CRYP_GCMCCM_SetHeaderPhase_IT(hcryp);
  1121. }
  1122. else /* if payload phase */
  1123. {
  1124. CRYP_GCMCCM_SetPayloadPhase_IT(hcryp);
  1125. }
  1126. }
  1127. }
  1128. }
  1129. /**
  1130. * @brief Return the CRYP error code.
  1131. * @param hcryp : pointer to a CRYP_HandleTypeDef structure that contains
  1132. * the configuration information for the CRYP IP
  1133. * @retval CRYP error code
  1134. */
  1135. uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp)
  1136. {
  1137. return hcryp->ErrorCode;
  1138. }
  1139. /**
  1140. * @brief Returns the CRYP state.
  1141. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1142. * the configuration information for CRYP module.
  1143. * @retval HAL state
  1144. */
  1145. HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
  1146. {
  1147. return hcryp->State;
  1148. }
  1149. /**
  1150. * @brief Input FIFO transfer completed callback.
  1151. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1152. * the configuration information for CRYP module.
  1153. * @retval None
  1154. */
  1155. __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
  1156. {
  1157. /* Prevent unused argument(s) compilation warning */
  1158. UNUSED(hcryp);
  1159. /* NOTE : This function Should not be modified, when the callback is needed,
  1160. the HAL_CRYP_InCpltCallback could be implemented in the user file
  1161. */
  1162. }
  1163. /**
  1164. * @brief Output FIFO transfer completed callback.
  1165. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1166. * the configuration information for CRYP module.
  1167. * @retval None
  1168. */
  1169. __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
  1170. {
  1171. /* Prevent unused argument(s) compilation warning */
  1172. UNUSED(hcryp);
  1173. /* NOTE : This function Should not be modified, when the callback is needed,
  1174. the HAL_CRYP_OutCpltCallback could be implemented in the user file
  1175. */
  1176. }
  1177. /**
  1178. * @brief CRYP error callback.
  1179. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1180. * the configuration information for CRYP module.
  1181. * @retval None
  1182. */
  1183. __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
  1184. {
  1185. /* Prevent unused argument(s) compilation warning */
  1186. UNUSED(hcryp);
  1187. /* NOTE : This function Should not be modified, when the callback is needed,
  1188. the HAL_CRYP_ErrorCallback could be implemented in the user file
  1189. */
  1190. }
  1191. /**
  1192. * @}
  1193. */
  1194. /* Private functions ---------------------------------------------------------*/
  1195. /** @addtogroup CRYP_Private_Functions
  1196. * @{
  1197. */
  1198. /**
  1199. * @brief Encryption in ECB/CBC Algorithm with DES/TDES standard.
  1200. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1201. * the configuration information for CRYP module
  1202. * @param Timeout: Timeout value
  1203. * @retval HAL status
  1204. */
  1205. static HAL_StatusTypeDef CRYP_TDES_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  1206. {
  1207. uint32_t temp = 0U; /* Temporary CrypOutBuff */
  1208. /* Enable CRYP */
  1209. __HAL_CRYP_ENABLE(hcryp);
  1210. /*Start processing*/
  1211. while((hcryp->CrypInCount < hcryp->Size) && (hcryp->CrypOutCount < hcryp->Size))
  1212. {
  1213. /* Write plain data and get cipher data */
  1214. if(((hcryp->Instance->SR & CRYP_FLAG_IFNF ) != RESET) && (hcryp->CrypInCount < hcryp->Size))
  1215. {
  1216. /* Write the input block in the IN FIFO */
  1217. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  1218. hcryp->CrypInCount++;
  1219. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  1220. hcryp->CrypInCount++;
  1221. }
  1222. /* Wait for OFNE flag to be raised */
  1223. if(CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK)
  1224. {
  1225. /* Disable the CRYP peripheral clock */
  1226. __HAL_CRYP_DISABLE(hcryp);
  1227. /* Change state & errorCode*/
  1228. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  1229. hcryp->State = HAL_CRYP_STATE_READY;
  1230. /* Process unlocked */
  1231. __HAL_UNLOCK(hcryp);
  1232. HAL_CRYP_ErrorCallback(hcryp);
  1233. }
  1234. if(((hcryp->Instance->SR & CRYP_FLAG_OFNE ) != RESET) && (hcryp->CrypOutCount < hcryp->Size))
  1235. {
  1236. /* Read the output block from the Output FIFO and put them in temporary Buffer then get CrypOutBuff from temporary buffer */
  1237. temp = hcryp->Instance->DOUT;
  1238. *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount)) = temp;
  1239. hcryp->CrypOutCount++;
  1240. temp = hcryp->Instance->DOUT;
  1241. *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount)) = temp;
  1242. hcryp->CrypOutCount++;
  1243. }
  1244. }
  1245. /* Disable CRYP */
  1246. __HAL_CRYP_DISABLE(hcryp);
  1247. /* Change the CRYP state */
  1248. hcryp->State = HAL_CRYP_STATE_READY;
  1249. /* Return function status */
  1250. return HAL_OK;
  1251. }
  1252. /**
  1253. * @brief CRYP block input/output data handling under interruption with DES/TDES standard.
  1254. * @note The function is called under interruption only, once
  1255. * interruptions have been enabled by CRYP_Decrypt_IT() and CRYP_Encrypt_IT().
  1256. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1257. * the configuration information for CRYP module.
  1258. * @retval HAL status
  1259. */
  1260. static HAL_StatusTypeDef CRYP_TDES_IT(CRYP_HandleTypeDef *hcryp)
  1261. {
  1262. uint32_t temp = 0U; /* Temporary CrypOutBuff */
  1263. if(hcryp->State == HAL_CRYP_STATE_BUSY)
  1264. {
  1265. if((__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI) != RESET) && (__HAL_CRYP_GET_FLAG(hcryp, CRYP_FLAG_INRIS) != RESET))
  1266. {
  1267. /* Write input block in the IN FIFO */
  1268. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  1269. hcryp->CrypInCount++;
  1270. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  1271. hcryp->CrypInCount++;
  1272. if(hcryp->CrypInCount == hcryp->Size)
  1273. {
  1274. /* Disable interruption */
  1275. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1276. /* Call the input data transfer complete callback */
  1277. HAL_CRYP_InCpltCallback(hcryp);
  1278. }
  1279. }
  1280. if((__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI) != RESET)&& (__HAL_CRYP_GET_FLAG(hcryp, CRYP_FLAG_OUTRIS) != RESET))
  1281. {
  1282. /* Read the output block from the Output FIFO and put them in temporary Buffer then get CrypOutBuff from temporary buffer */
  1283. temp = hcryp->Instance->DOUT;
  1284. *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount)) = temp;
  1285. hcryp->CrypOutCount++;
  1286. temp = hcryp->Instance->DOUT;
  1287. *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount)) = temp;
  1288. hcryp->CrypOutCount++;
  1289. if(hcryp->CrypOutCount == hcryp->Size)
  1290. {
  1291. /* Disable interruption */
  1292. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1293. /* Disable CRYP */
  1294. __HAL_CRYP_DISABLE(hcryp);
  1295. /* Process unlocked */
  1296. __HAL_UNLOCK(hcryp);
  1297. /* Change the CRYP state */
  1298. hcryp->State = HAL_CRYP_STATE_READY;
  1299. /* Call output transfer complete callback */
  1300. HAL_CRYP_OutCpltCallback(hcryp);
  1301. }
  1302. }
  1303. return HAL_OK;
  1304. }
  1305. else
  1306. {
  1307. /* Busy error code field */
  1308. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1309. return HAL_ERROR;
  1310. }
  1311. }
  1312. /**
  1313. * @brief Encryption in ECB/CBC & CTR Algorithm with AES Standard
  1314. * @param hcryp: pointer to a CRYP_HandleTypeDef structure
  1315. * @param Timeout: specify Timeout value
  1316. * @retval HAL status
  1317. */
  1318. static HAL_StatusTypeDef CRYP_AES_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  1319. {
  1320. /* Set the Key*/
  1321. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1322. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  1323. {
  1324. /* Set the Initialization Vector*/
  1325. hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
  1326. hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
  1327. hcryp->Instance->IV1LR = *(uint32_t*)(hcryp->Init.pInitVect+2);
  1328. hcryp->Instance->IV1RR = *(uint32_t*)(hcryp->Init.pInitVect+3);
  1329. }
  1330. /* Set the phase */
  1331. hcryp->Phase = CRYP_PHASE_PROCESS;
  1332. /* Enable CRYP */
  1333. __HAL_CRYP_ENABLE(hcryp);
  1334. while((hcryp->CrypInCount < hcryp->Size) && (hcryp->CrypOutCount < hcryp->Size))
  1335. {
  1336. /* Write plain Ddta and get cipher data */
  1337. CRYP_AES_ProcessData(hcryp,Timeout);
  1338. }
  1339. /* Disable CRYP */
  1340. __HAL_CRYP_DISABLE(hcryp);
  1341. /* Change the CRYP state */
  1342. hcryp->State = HAL_CRYP_STATE_READY;
  1343. /* Return function status */
  1344. return HAL_OK;
  1345. }
  1346. /**
  1347. * @brief Encryption in ECB/CBC & CTR mode with AES Standard using interrupt mode
  1348. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1349. * the configuration information for CRYP module
  1350. * @retval HAL status
  1351. */
  1352. static HAL_StatusTypeDef CRYP_AES_Encrypt_IT(CRYP_HandleTypeDef *hcryp)
  1353. {
  1354. /* Set the Key*/
  1355. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1356. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  1357. {
  1358. /* Set the Initialization Vector*/
  1359. hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
  1360. hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
  1361. hcryp->Instance->IV1LR = *(uint32_t*)(hcryp->Init.pInitVect+2);
  1362. hcryp->Instance->IV1RR = *(uint32_t*)(hcryp->Init.pInitVect+3);
  1363. }
  1364. /* Set the phase */
  1365. hcryp->Phase = CRYP_PHASE_PROCESS;
  1366. if(hcryp->Size != 0U)
  1367. {
  1368. /* Enable interrupts */
  1369. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1370. /* Enable CRYP */
  1371. __HAL_CRYP_ENABLE(hcryp);
  1372. }
  1373. else
  1374. {
  1375. /* Change the CRYP state */
  1376. hcryp->State = HAL_CRYP_STATE_READY;
  1377. /* Process unlocked */
  1378. __HAL_UNLOCK(hcryp);
  1379. }
  1380. /* Return function status */
  1381. return HAL_OK;
  1382. }
  1383. /**
  1384. * @brief Decryption in ECB/CBC & CTR mode with AES Standard
  1385. * @param hcryp: pointer to a CRYP_HandleTypeDef structure
  1386. * @param Timeout: Specify Timeout value
  1387. * @retval HAL status
  1388. */
  1389. static HAL_StatusTypeDef CRYP_AES_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout )
  1390. {
  1391. /* Key preparation for ECB/CBC */
  1392. if (hcryp->Init.Algorithm != CRYP_AES_CTR) /*ECB or CBC*/
  1393. {
  1394. /* change ALGOMODE to key preparation for decryption*/
  1395. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_CR_ALGOMODE_AES_KEY );
  1396. /* Set the Key*/
  1397. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1398. /* Enable CRYP */
  1399. __HAL_CRYP_ENABLE(hcryp);
  1400. /* Wait for BUSY flag to be raised */
  1401. if(CRYP_WaitOnBUSYFlag(hcryp, Timeout) != HAL_OK)
  1402. {
  1403. /* Disable the CRYP peripheral clock */
  1404. __HAL_CRYP_DISABLE(hcryp);
  1405. /* Change state */
  1406. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  1407. hcryp->State = HAL_CRYP_STATE_READY;
  1408. /* Process unlocked */
  1409. __HAL_UNLOCK(hcryp);
  1410. return HAL_ERROR;
  1411. }
  1412. /* Turn back to ALGOMODE of the configuration */
  1413. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, hcryp->Init.Algorithm );
  1414. }
  1415. else /*Algorithm CTR */
  1416. {
  1417. /* Set the Key*/
  1418. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1419. }
  1420. /* Set IV */
  1421. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  1422. {
  1423. /* Set the Initialization Vector*/
  1424. hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
  1425. hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
  1426. hcryp->Instance->IV1LR = *(uint32_t*)(hcryp->Init.pInitVect+2);
  1427. hcryp->Instance->IV1RR = *(uint32_t*)(hcryp->Init.pInitVect+3);
  1428. }
  1429. /* Set the phase */
  1430. hcryp->Phase = CRYP_PHASE_PROCESS;
  1431. /* Enable CRYP */
  1432. __HAL_CRYP_ENABLE(hcryp);
  1433. while((hcryp->CrypInCount < hcryp->Size) && (hcryp->CrypOutCount < hcryp->Size))
  1434. {
  1435. /* Write plain data and get cipher data */
  1436. CRYP_AES_ProcessData(hcryp,Timeout);
  1437. }
  1438. /* Disable CRYP */
  1439. __HAL_CRYP_DISABLE(hcryp);
  1440. /* Change the CRYP state */
  1441. hcryp->State = HAL_CRYP_STATE_READY;
  1442. /* Return function status */
  1443. return HAL_OK;
  1444. }
  1445. /**
  1446. * @brief Decryption in ECB/CBC & CTR mode with AES Standard using interrupt mode
  1447. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1448. * the configuration information for CRYP module
  1449. * @retval HAL status
  1450. */
  1451. static HAL_StatusTypeDef CRYP_AES_Decrypt_IT(CRYP_HandleTypeDef *hcryp)
  1452. {
  1453. __IO uint32_t count = 0U;
  1454. /* Key preparation for ECB/CBC */
  1455. if (hcryp->Init.Algorithm != CRYP_AES_CTR)
  1456. {
  1457. /* change ALGOMODE to key preparation for decryption*/
  1458. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_CR_ALGOMODE_AES_KEY );
  1459. /* Set the Key*/
  1460. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1461. /* Enable CRYP */
  1462. __HAL_CRYP_ENABLE(hcryp);
  1463. /* Wait for BUSY flag to be raised */
  1464. count = CRYP_TIMEOUT_KEYPREPARATION;
  1465. do
  1466. {
  1467. if(count-- == 0U)
  1468. {
  1469. /* Change state */
  1470. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  1471. hcryp->State = HAL_CRYP_STATE_READY;
  1472. /* Process unlocked */
  1473. __HAL_UNLOCK(hcryp);
  1474. return HAL_ERROR;
  1475. }
  1476. }
  1477. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY));
  1478. /* Turn back to ALGOMODE of the configuration */
  1479. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, hcryp->Init.Algorithm );
  1480. }
  1481. else /*Algorithm CTR */
  1482. {
  1483. /* Set the Key*/
  1484. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1485. }
  1486. /* Set IV */
  1487. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  1488. {
  1489. /* Set the Initialization Vector*/
  1490. hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
  1491. hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
  1492. hcryp->Instance->IV1LR = *(uint32_t*)(hcryp->Init.pInitVect+2);
  1493. hcryp->Instance->IV1RR = *(uint32_t*)(hcryp->Init.pInitVect+3);
  1494. }
  1495. /* Set the phase */
  1496. hcryp->Phase = CRYP_PHASE_PROCESS;
  1497. if(hcryp->Size != 0)
  1498. {
  1499. /* Enable interrupts */
  1500. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1501. /* Enable CRYP */
  1502. __HAL_CRYP_ENABLE(hcryp);
  1503. }
  1504. else
  1505. {
  1506. /* Process locked */
  1507. __HAL_UNLOCK(hcryp);
  1508. /* Change the CRYP state */
  1509. hcryp->State = HAL_CRYP_STATE_READY;
  1510. }
  1511. /* Return function status */
  1512. return HAL_OK;
  1513. }
  1514. /**
  1515. * @brief Decryption in ECB/CBC & CTR mode with AES Standard using DMA mode
  1516. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1517. * the configuration information for CRYP module
  1518. * @retval HAL status
  1519. */
  1520. static HAL_StatusTypeDef CRYP_AES_Decrypt_DMA(CRYP_HandleTypeDef *hcryp)
  1521. {
  1522. __IO uint32_t count = 0U;
  1523. /* Key preparation for ECB/CBC */
  1524. if (hcryp->Init.Algorithm != CRYP_AES_CTR)
  1525. {
  1526. /* change ALGOMODE to key preparation for decryption*/
  1527. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_CR_ALGOMODE_AES_KEY );
  1528. /* Set the Key*/
  1529. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1530. /* Enable CRYP */
  1531. __HAL_CRYP_ENABLE(hcryp);
  1532. /* Wait for BUSY flag to be raised */
  1533. count = CRYP_TIMEOUT_KEYPREPARATION;
  1534. do
  1535. {
  1536. if(count-- == 0U)
  1537. {
  1538. /* Disable the CRYP peripheral clock */
  1539. __HAL_CRYP_DISABLE(hcryp);
  1540. /* Change state */
  1541. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  1542. hcryp->State = HAL_CRYP_STATE_READY;
  1543. /* Process unlocked */
  1544. __HAL_UNLOCK(hcryp);
  1545. return HAL_ERROR;
  1546. }
  1547. }
  1548. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY));
  1549. /* Turn back to ALGOMODE of the configuration */
  1550. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, hcryp->Init.Algorithm );
  1551. }
  1552. else /*Algorithm CTR */
  1553. {
  1554. /* Set the Key*/
  1555. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1556. }
  1557. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  1558. {
  1559. /* Set the Initialization Vector*/
  1560. hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
  1561. hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
  1562. hcryp->Instance->IV1LR = *(uint32_t*)(hcryp->Init.pInitVect+2);
  1563. hcryp->Instance->IV1RR = *(uint32_t*)(hcryp->Init.pInitVect+3);
  1564. }
  1565. /* Set the phase */
  1566. hcryp->Phase = CRYP_PHASE_PROCESS;
  1567. if(hcryp->Size != 0)
  1568. {
  1569. /* Set the input and output addresses and start DMA transfer */
  1570. CRYP_SetDMAConfig(hcryp, (uint32_t)( hcryp->pCrypInBuffPtr), hcryp->Size, (uint32_t)(hcryp->pCrypOutBuffPtr));
  1571. }
  1572. else
  1573. {
  1574. /* Process unlocked */
  1575. __HAL_UNLOCK(hcryp);
  1576. /* Change the CRYP state */
  1577. hcryp->State = HAL_CRYP_STATE_READY;
  1578. }
  1579. /* Return function status */
  1580. return HAL_OK;
  1581. }
  1582. /**
  1583. * @brief DMA CRYP input data process complete callback.
  1584. * @param hdma: DMA handle
  1585. * @retval None
  1586. */
  1587. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma)
  1588. {
  1589. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1590. /* Disable the DMA transfer for input FIFO request by resetting the DIEN bit
  1591. in the DMACR register */
  1592. hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DIEN);
  1593. /* Call input data transfer complete callback */
  1594. HAL_CRYP_InCpltCallback(hcryp);
  1595. }
  1596. /**
  1597. * @brief DMA CRYP output data process complete callback.
  1598. * @param hdma: DMA handle
  1599. * @retval None
  1600. */
  1601. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma)
  1602. {
  1603. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1604. /* Disable the DMA transfer for output FIFO request by resetting
  1605. the DOEN bit in the DMACR register */
  1606. hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DOEN);
  1607. if((hcryp->Init.Algorithm & CRYP_AES_GCM) != CRYP_AES_GCM)
  1608. {
  1609. /* Disable CRYP (not allowed in GCM)*/
  1610. __HAL_CRYP_DISABLE(hcryp);
  1611. }
  1612. /* Change the CRYP state to ready */
  1613. hcryp->State = HAL_CRYP_STATE_READY;
  1614. /* Process unlocked */
  1615. __HAL_UNLOCK(hcryp);
  1616. /* Call output data transfer complete callback */
  1617. HAL_CRYP_OutCpltCallback(hcryp);
  1618. }
  1619. /**
  1620. * @brief DMA CRYP communication error callback.
  1621. * @param hdma: DMA handle
  1622. * @retval None
  1623. */
  1624. static void CRYP_DMAError(DMA_HandleTypeDef *hdma)
  1625. {
  1626. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1627. /* Change the CRYP peripheral state */
  1628. hcryp->State= HAL_CRYP_STATE_READY;
  1629. /* DMA error code field */
  1630. hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA;
  1631. /* Call error callback */
  1632. HAL_CRYP_ErrorCallback(hcryp);
  1633. }
  1634. /**
  1635. * @brief Set the DMA configuration and start the DMA transfer
  1636. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1637. * the configuration information for CRYP module
  1638. * @param inputaddr: address of the input buffer
  1639. * @param Size: size of the input buffer, must be a multiple of 16.
  1640. * @param outputaddr: address of the output buffer
  1641. * @retval None
  1642. */
  1643. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
  1644. {
  1645. /* Set the CRYP DMA transfer complete callback */
  1646. hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt;
  1647. /* Set the DMA input error callback */
  1648. hcryp->hdmain->XferErrorCallback = CRYP_DMAError;
  1649. /* Set the CRYP DMA transfer complete callback */
  1650. hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt;
  1651. /* Set the DMA output error callback */
  1652. hcryp->hdmaout->XferErrorCallback = CRYP_DMAError;
  1653. /* Enable CRYP */
  1654. __HAL_CRYP_ENABLE(hcryp);
  1655. /* Enable the input DMA Stream */
  1656. HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DIN, Size);
  1657. /* Enable the output DMA Stream */
  1658. HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUT, outputaddr, Size);
  1659. /* Enable In/Out DMA request */
  1660. hcryp->Instance->DMACR = CRYP_DMACR_DOEN | CRYP_DMACR_DIEN;
  1661. }
  1662. /**
  1663. * @brief Process Data: Write Input data in polling mode and used in AES functions.
  1664. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1665. * the configuration information for CRYP module
  1666. * @param Timeout: Specify Timeout value
  1667. * @retval None
  1668. */
  1669. static HAL_StatusTypeDef CRYP_AES_ProcessData(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  1670. {
  1671. uint32_t temp = 0U; /* Temporary CrypOutBuff */
  1672. if(((hcryp->Instance->SR & CRYP_FLAG_IFNF ) != RESET) && (hcryp->CrypInCount < hcryp->Size))
  1673. {
  1674. /* Write the input block in the IN FIFO */
  1675. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  1676. hcryp->CrypInCount++;
  1677. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  1678. hcryp->CrypInCount++;
  1679. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  1680. hcryp->CrypInCount++;
  1681. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  1682. hcryp->CrypInCount++;
  1683. }
  1684. /* Wait for OFNE flag to be raised */
  1685. if(CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK)
  1686. {
  1687. /* Disable the CRYP peripheral clock */
  1688. __HAL_CRYP_DISABLE(hcryp);
  1689. /* Change state & error code*/
  1690. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  1691. hcryp->State = HAL_CRYP_STATE_READY;
  1692. /* Process unlocked */
  1693. __HAL_UNLOCK(hcryp);
  1694. HAL_CRYP_ErrorCallback(hcryp);
  1695. }
  1696. if(((hcryp->Instance->SR & CRYP_FLAG_OFNE ) != RESET) && (hcryp->CrypOutCount < hcryp->Size))
  1697. {
  1698. /* Read the output block from the Output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer */
  1699. temp = hcryp->Instance->DOUT;
  1700. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
  1701. hcryp->CrypOutCount++;
  1702. temp = hcryp->Instance->DOUT;
  1703. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
  1704. hcryp->CrypOutCount++;
  1705. temp = hcryp->Instance->DOUT;
  1706. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
  1707. hcryp->CrypOutCount++;
  1708. temp = hcryp->Instance->DOUT;
  1709. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
  1710. hcryp->CrypOutCount++;
  1711. }
  1712. /* Return function status */
  1713. return HAL_OK;
  1714. }
  1715. /**
  1716. * @brief Handle CRYP block input/output data handling under interruption.
  1717. * @note The function is called under interruption only, once
  1718. * interruptions have been enabled by HAL_CRYP_Encrypt_IT or HAL_CRYP_Decrypt_IT.
  1719. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1720. * the configuration information for CRYP module.
  1721. * @retval HAL status
  1722. */
  1723. static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp)
  1724. {
  1725. uint32_t temp = 0U; /* Temporary CrypOutBuff */
  1726. if(hcryp->State == HAL_CRYP_STATE_BUSY)
  1727. {
  1728. if(((hcryp->Instance->SR & CRYP_FLAG_IFNF ) != RESET) && (hcryp->CrypInCount < hcryp->Size))
  1729. {
  1730. /* Write the input block in the IN FIFO */
  1731. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  1732. hcryp->CrypInCount++;
  1733. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  1734. hcryp->CrypInCount++;
  1735. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  1736. hcryp->CrypInCount++;
  1737. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  1738. hcryp->CrypInCount++;
  1739. if(hcryp->CrypInCount == hcryp->Size)
  1740. {
  1741. /* Disable interrupts */
  1742. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1743. /* Call the input data transfer complete callback */
  1744. HAL_CRYP_InCpltCallback(hcryp);
  1745. }
  1746. }
  1747. if(((hcryp->Instance->SR & CRYP_FLAG_OFNE ) != RESET) && (hcryp->CrypOutCount < hcryp->Size))
  1748. {
  1749. /* Read the output block from the output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer */
  1750. temp = hcryp->Instance->DOUT;
  1751. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
  1752. hcryp->CrypOutCount++;
  1753. temp = hcryp->Instance->DOUT;
  1754. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
  1755. hcryp->CrypOutCount++;
  1756. temp = hcryp->Instance->DOUT;
  1757. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
  1758. hcryp->CrypOutCount++;
  1759. temp = hcryp->Instance->DOUT;
  1760. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
  1761. hcryp->CrypOutCount++;
  1762. if(hcryp->CrypOutCount == hcryp->Size)
  1763. {
  1764. /* Disable interrupts */
  1765. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1766. /* Change the CRYP state */
  1767. hcryp->State = HAL_CRYP_STATE_READY;
  1768. /* Disable CRYP */
  1769. __HAL_CRYP_DISABLE(hcryp);
  1770. /* Process unlocked */
  1771. __HAL_UNLOCK(hcryp);
  1772. /* Call output transfer complete callback */
  1773. HAL_CRYP_OutCpltCallback(hcryp);
  1774. }
  1775. }
  1776. /* Return function status */
  1777. return HAL_OK;
  1778. }
  1779. else
  1780. {
  1781. /* Busy error code field */
  1782. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1783. return HAL_ERROR;
  1784. }
  1785. }
  1786. /**
  1787. * @brief Writes Key in Key registers.
  1788. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1789. * the configuration information for CRYP module
  1790. * @param Key: Pointer to Key buffer
  1791. * @param KeySize: Size of Key
  1792. * @retval None
  1793. */
  1794. static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint32_t *Key, uint32_t KeySize)
  1795. {
  1796. switch(KeySize)
  1797. {
  1798. case CRYP_KEYSIZE_256B:
  1799. hcryp->Instance->K0LR = *(uint32_t*)(hcryp->Init.pKey);
  1800. hcryp->Instance->K0RR = *(uint32_t*)(hcryp->Init.pKey+1);
  1801. hcryp->Instance->K1LR = *(uint32_t*)(hcryp->Init.pKey+2);
  1802. hcryp->Instance->K1RR = *(uint32_t*)(hcryp->Init.pKey+3);
  1803. hcryp->Instance->K2LR = *(uint32_t*)(hcryp->Init.pKey+4);
  1804. hcryp->Instance->K2RR = *(uint32_t*)(hcryp->Init.pKey+5);
  1805. hcryp->Instance->K3LR = *(uint32_t*)(hcryp->Init.pKey+6);
  1806. hcryp->Instance->K3RR = *(uint32_t*)(hcryp->Init.pKey+7);
  1807. break;
  1808. case CRYP_KEYSIZE_192B:
  1809. hcryp->Instance->K1LR = *(uint32_t*)(hcryp->Init.pKey);
  1810. hcryp->Instance->K1RR = *(uint32_t*)(hcryp->Init.pKey+1);
  1811. hcryp->Instance->K2LR = *(uint32_t*)(hcryp->Init.pKey+2);
  1812. hcryp->Instance->K2RR = *(uint32_t*)(hcryp->Init.pKey+3);
  1813. hcryp->Instance->K3LR = *(uint32_t*)(hcryp->Init.pKey+4);
  1814. hcryp->Instance->K3RR = *(uint32_t*)(hcryp->Init.pKey+5);
  1815. break;
  1816. case CRYP_KEYSIZE_128B:
  1817. hcryp->Instance->K2LR = *(uint32_t*)(hcryp->Init.pKey);
  1818. hcryp->Instance->K2RR = *(uint32_t*)(hcryp->Init.pKey+1);
  1819. hcryp->Instance->K3LR = *(uint32_t*)(hcryp->Init.pKey+2);
  1820. hcryp->Instance->K3RR = *(uint32_t*)(hcryp->Init.pKey+3);
  1821. break;
  1822. default:
  1823. break;
  1824. }
  1825. }
  1826. /**
  1827. * @brief Encryption/Decryption process in AES GCM mode and prepare the authentication TAG
  1828. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1829. * the configuration information for CRYP module
  1830. * @param Timeout: Timeout duration
  1831. * @retval HAL status
  1832. */
  1833. static HAL_StatusTypeDef CRYP_AESGCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  1834. {
  1835. uint32_t tickstart = 0U;
  1836. uint32_t payloadlength = 0U;
  1837. /* Reset CrypHeaderCount */
  1838. hcryp->CrypHeaderCount = 0U;
  1839. /****************************** Init phase **********************************/
  1840. CRYP_SET_PHASE(hcryp,CRYP_PHASE_INIT);
  1841. /* Set the key */
  1842. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1843. /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/
  1844. hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
  1845. hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
  1846. hcryp->Instance->IV1LR = *(uint32_t*)(hcryp->Init.pInitVect+2);
  1847. hcryp->Instance->IV1RR = *(uint32_t*)(hcryp->Init.pInitVect+3);
  1848. /* Enable the CRYP peripheral */
  1849. __HAL_CRYP_ENABLE(hcryp);
  1850. /* Get tick */
  1851. tickstart = HAL_GetTick();
  1852. /*Wait for the CRYPEN bit to be cleared*/
  1853. while((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  1854. {
  1855. /* Check for the Timeout */
  1856. if(Timeout != HAL_MAX_DELAY)
  1857. {
  1858. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1859. {
  1860. /* Disable the CRYP peripheral clock */
  1861. __HAL_CRYP_DISABLE(hcryp);
  1862. /* Change state */
  1863. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  1864. hcryp->State = HAL_CRYP_STATE_READY;
  1865. /* Process unlocked */
  1866. __HAL_UNLOCK(hcryp);
  1867. return HAL_ERROR;
  1868. }
  1869. }
  1870. }
  1871. /************************ Header phase *************************************/
  1872. if(CRYP_GCMCCM_SetHeaderPhase(hcryp, Timeout) != HAL_OK)
  1873. {
  1874. return HAL_ERROR;
  1875. }
  1876. /*************************Payload phase ************************************/
  1877. /* Set the phase */
  1878. hcryp->Phase = CRYP_PHASE_PROCESS;
  1879. /* Disable the CRYP peripheral */
  1880. __HAL_CRYP_DISABLE(hcryp);
  1881. /* Select payload phase once the header phase is performed */
  1882. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  1883. /* Enable the CRYP peripheral */
  1884. __HAL_CRYP_ENABLE(hcryp);
  1885. /* Get tick */
  1886. tickstart = HAL_GetTick();
  1887. /* Write input data and get output Data */
  1888. if ((hcryp->Size % 4U) == 0U)
  1889. {
  1890. while((hcryp->CrypInCount < hcryp->Size) && (hcryp->CrypOutCount < hcryp->Size))
  1891. {
  1892. /* Write plain data and get cipher data */
  1893. CRYP_AES_ProcessData(hcryp,Timeout);
  1894. /* Check for the Timeout */
  1895. if(Timeout != HAL_MAX_DELAY)
  1896. {
  1897. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1898. {
  1899. /* Disable the CRYP peripheral clock */
  1900. __HAL_CRYP_DISABLE(hcryp);
  1901. /* Change state & error code */
  1902. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  1903. hcryp->State = HAL_CRYP_STATE_READY;
  1904. /* Process unlocked */
  1905. __HAL_UNLOCK(hcryp);
  1906. return HAL_ERROR;
  1907. }
  1908. }
  1909. }
  1910. }
  1911. else
  1912. {
  1913. payloadlength = (((hcryp->Size)/4U)*4U) ;
  1914. /*Write input block in the IN FIFO without last block */
  1915. while((hcryp->CrypInCount < payloadlength) && (hcryp->CrypOutCount < payloadlength))
  1916. {
  1917. /* Write input Data and get output Data */
  1918. CRYP_AES_ProcessData(hcryp,Timeout);
  1919. /* Check for the Timeout */
  1920. if(Timeout != HAL_MAX_DELAY)
  1921. {
  1922. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1923. {
  1924. /* Disable the CRYP peripheral clock */
  1925. __HAL_CRYP_DISABLE(hcryp);
  1926. /* Change state */
  1927. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  1928. hcryp->State = HAL_CRYP_STATE_READY;
  1929. /* Process unlocked */
  1930. __HAL_UNLOCK(hcryp);
  1931. return HAL_ERROR;
  1932. }
  1933. }
  1934. }
  1935. /* Workaround 2 : CRYP1 & AES generates correct TAG for GCM mode only when input block size is multiple of
  1936. 128 bits. If lthe size of the last block of payload is inferior to 128 bits, when GCM encryption
  1937. is selected, then the TAG message will be wrong.*/
  1938. CRYP_Workaround(hcryp,Timeout);
  1939. }
  1940. /* Return function status */
  1941. return HAL_OK;
  1942. }
  1943. /**
  1944. * @brief Encryption/Decryption process in AES GCM mode and prepare the authentication TAG in interrupt mode
  1945. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1946. * the configuration information for CRYP module
  1947. * @retval HAL status
  1948. */
  1949. static HAL_StatusTypeDef CRYP_AESGCM_Process_IT(CRYP_HandleTypeDef *hcryp)
  1950. {
  1951. __IO uint32_t count = 0U;
  1952. /* Reset CrypHeaderCount */
  1953. hcryp->CrypHeaderCount =0U;
  1954. /******************************* Init phase *********************************/
  1955. CRYP_SET_PHASE(hcryp,CRYP_PHASE_INIT);
  1956. /* Set the key */
  1957. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1958. /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/
  1959. hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
  1960. hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
  1961. hcryp->Instance->IV1LR = *(uint32_t*)(hcryp->Init.pInitVect+2);
  1962. hcryp->Instance->IV1RR = *(uint32_t*)(hcryp->Init.pInitVect+3);
  1963. /* Enable the CRYP peripheral */
  1964. __HAL_CRYP_ENABLE(hcryp);
  1965. /*Wait for the CRYPEN bit to be cleared*/
  1966. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  1967. do
  1968. {
  1969. if(count-- == 0U)
  1970. {
  1971. /* Disable the CRYP peripheral clock */
  1972. __HAL_CRYP_DISABLE(hcryp);
  1973. /* Change state */
  1974. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  1975. hcryp->State = HAL_CRYP_STATE_READY;
  1976. /* Process unlocked */
  1977. __HAL_UNLOCK(hcryp);
  1978. return HAL_ERROR;
  1979. }
  1980. }
  1981. while((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
  1982. /***************************** Header phase *********************************/
  1983. /* Select header phase */
  1984. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  1985. /* Enable interrupts */
  1986. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI);
  1987. /* Enable CRYP */
  1988. __HAL_CRYP_ENABLE(hcryp);
  1989. /* Return function status */
  1990. return HAL_OK;
  1991. }
  1992. /**
  1993. * @brief Encryption/Decryption process in AES GCM mode and prepare the authentication TAG using DMA
  1994. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1995. * the configuration information for CRYP module
  1996. * @retval HAL status
  1997. */
  1998. static HAL_StatusTypeDef CRYP_AESGCM_Process_DMA(CRYP_HandleTypeDef *hcryp)
  1999. {
  2000. __IO uint32_t count = 0U;
  2001. uint32_t payloadlength = 0U;
  2002. /* Reset CrypHeaderCount */
  2003. hcryp->CrypHeaderCount = 0U;
  2004. /*************************** Init phase ************************************/
  2005. CRYP_SET_PHASE(hcryp,CRYP_PHASE_INIT);
  2006. /* Set the key */
  2007. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  2008. /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/
  2009. hcryp->Instance->IV0LR = *(uint32_t*)(hcryp->Init.pInitVect);
  2010. hcryp->Instance->IV0RR = *(uint32_t*)(hcryp->Init.pInitVect+1);
  2011. hcryp->Instance->IV1LR = *(uint32_t*)(hcryp->Init.pInitVect+2);
  2012. hcryp->Instance->IV1RR = *(uint32_t*)(hcryp->Init.pInitVect+3);
  2013. /* Enable the CRYP peripheral */
  2014. __HAL_CRYP_ENABLE(hcryp);
  2015. /*Wait for the CRYPEN bit to be cleared*/
  2016. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  2017. do
  2018. {
  2019. if(count-- == 0U)
  2020. {
  2021. /* Disable the CRYP peripheral clock */
  2022. __HAL_CRYP_DISABLE(hcryp);
  2023. /* Change state */
  2024. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2025. hcryp->State = HAL_CRYP_STATE_READY;
  2026. /* Process unlocked */
  2027. __HAL_UNLOCK(hcryp);
  2028. return HAL_ERROR;
  2029. }
  2030. }
  2031. while((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
  2032. /************************ Header phase *************************************/
  2033. if(CRYP_GCMCCM_SetHeaderPhase_DMA(hcryp) != HAL_OK)
  2034. {
  2035. return HAL_ERROR;
  2036. }
  2037. /************************ Payload phase ************************************/
  2038. /* Set the phase */
  2039. hcryp->Phase = CRYP_PHASE_PROCESS;
  2040. /* Disable the CRYP peripheral */
  2041. __HAL_CRYP_DISABLE(hcryp);
  2042. /* Select payload phase once the header phase is performed */
  2043. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  2044. if(hcryp->Size != 0U)
  2045. {
  2046. /* Size should be %4 otherwise Tag will be incorrectly generated for GCM Encryption:
  2047. Workaround is implemented in polling mode, so if last block of
  2048. payload <128bit don't use DMA mode otherwise TAG is incorrectly generated . */
  2049. /* Set the input and output addresses and start DMA transfer */
  2050. if ((hcryp->Size % 4U) == 0U)
  2051. {
  2052. CRYP_SetDMAConfig(hcryp, (uint32_t)( hcryp->pCrypInBuffPtr), hcryp->Size, (uint32_t)(hcryp->pCrypOutBuffPtr));
  2053. }
  2054. else /*to compute last word<128bits, otherwise it will not be encrypted/decrypted */
  2055. {
  2056. payloadlength = (hcryp->Size)+(4-(hcryp->Size)%4U) ;
  2057. /* Set the input and output addresses and start DMA transfer, pCrypOutBuffPtr size should be %4 */
  2058. CRYP_SetDMAConfig(hcryp, (uint32_t)( hcryp->pCrypInBuffPtr), payloadlength, (uint32_t)(hcryp->pCrypOutBuffPtr));
  2059. }
  2060. }
  2061. else
  2062. {
  2063. /* Process unLocked */
  2064. __HAL_UNLOCK(hcryp);
  2065. /* Change the CRYP state and phase */
  2066. hcryp->State = HAL_CRYP_STATE_READY;
  2067. }
  2068. /* Return function status */
  2069. return HAL_OK;
  2070. }
  2071. /**
  2072. * @brief AES CCM encryption/decryption processing in polling mode
  2073. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2074. * the configuration information for CRYP module
  2075. * @param Timeout: Timeout duration
  2076. * @retval HAL status
  2077. */
  2078. static HAL_StatusTypeDef CRYP_AESCCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  2079. {
  2080. uint32_t tickstart = 0U;
  2081. uint32_t payloadlength =0U;
  2082. /* Reset CrypHeaderCount */
  2083. hcryp->CrypHeaderCount = 0U;
  2084. /********************** Init phase ******************************************/
  2085. CRYP_SET_PHASE(hcryp,CRYP_PHASE_INIT);
  2086. /* Set the key */
  2087. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  2088. /* Set the initialization vector (IV) with CTR1 information */
  2089. hcryp->Instance->IV0LR = (hcryp->Init.B0[0]) & CRYP_CCM_CTR1_0;
  2090. hcryp->Instance->IV0RR = hcryp->Init.B0[1];
  2091. hcryp->Instance->IV1LR = hcryp->Init.B0[2];
  2092. hcryp->Instance->IV1RR = (hcryp->Init.B0[3] & CRYP_CCM_CTR1_1)| CRYP_CCM_CTR1_2;
  2093. /* Enable the CRYP peripheral */
  2094. __HAL_CRYP_ENABLE(hcryp);
  2095. /*Write B0 packet into CRYP_DR*/
  2096. if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
  2097. {
  2098. hcryp->Instance->DIN = __REV(*(uint32_t*)(hcryp->Init.B0));
  2099. hcryp->Instance->DIN = __REV( *(uint32_t*)(hcryp->Init.B0+1));
  2100. hcryp->Instance->DIN = __REV(*(uint32_t*)(hcryp->Init.B0+2));
  2101. hcryp->Instance->DIN = __REV(*(uint32_t*)(hcryp->Init.B0+3));
  2102. }
  2103. else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
  2104. {
  2105. hcryp->Instance->DIN = __ROR(*(uint32_t*)(hcryp->Init.B0), 16);
  2106. hcryp->Instance->DIN = __ROR( *(uint32_t*)(hcryp->Init.B0+1), 16);
  2107. hcryp->Instance->DIN = __ROR(*(uint32_t*)(hcryp->Init.B0+2), 16);
  2108. hcryp->Instance->DIN = __ROR(*(uint32_t*)(hcryp->Init.B0+3), 16);
  2109. }
  2110. else if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
  2111. {
  2112. hcryp->Instance->DIN = __RBIT(*(uint32_t*)(hcryp->Init.B0));
  2113. hcryp->Instance->DIN = __RBIT( *(uint32_t*)(hcryp->Init.B0+1));
  2114. hcryp->Instance->DIN = __RBIT(*(uint32_t*)(hcryp->Init.B0+2));
  2115. hcryp->Instance->DIN = __RBIT(*(uint32_t*)(hcryp->Init.B0+3));
  2116. }
  2117. else
  2118. {
  2119. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.B0);
  2120. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.B0+1);
  2121. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.B0+2);
  2122. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.B0+3);
  2123. }
  2124. /* Get tick */
  2125. tickstart = HAL_GetTick();
  2126. /*Wait for the CRYPEN bit to be cleared*/
  2127. while((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  2128. {
  2129. /* Check for the Timeout */
  2130. if(Timeout != HAL_MAX_DELAY)
  2131. {
  2132. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  2133. {
  2134. /* Disable the CRYP peripheral clock */
  2135. __HAL_CRYP_DISABLE(hcryp);
  2136. /* Change state */
  2137. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2138. hcryp->State = HAL_CRYP_STATE_READY;
  2139. /* Process unlocked */
  2140. __HAL_UNLOCK(hcryp);
  2141. return HAL_ERROR;
  2142. }
  2143. }
  2144. }
  2145. /************************* Header phase *************************************/
  2146. /* Header block(B1) : associated data length expressed in bytes concatenated
  2147. with Associated Data (A)*/
  2148. if(CRYP_GCMCCM_SetHeaderPhase(hcryp, Timeout) != HAL_OK)
  2149. {
  2150. return HAL_ERROR;
  2151. }
  2152. /********************** Payload phase ***************************************/
  2153. /* Set the phase */
  2154. hcryp->Phase = CRYP_PHASE_PROCESS;
  2155. /* Disable the CRYP peripheral */
  2156. __HAL_CRYP_DISABLE(hcryp);
  2157. /* Select payload phase once the header phase is performed */
  2158. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  2159. /* Enable the CRYP peripheral */
  2160. __HAL_CRYP_ENABLE(hcryp);
  2161. /* Get tick */
  2162. tickstart = HAL_GetTick();
  2163. /* Write input data and get output data */
  2164. if((hcryp->Size % 4U) == 0U)
  2165. {
  2166. while((hcryp->CrypInCount < hcryp->Size) && (hcryp->CrypOutCount < hcryp->Size))
  2167. {
  2168. /* Write plain data and get cipher data */
  2169. CRYP_AES_ProcessData(hcryp,Timeout);
  2170. /* Check for the Timeout */
  2171. if(Timeout != HAL_MAX_DELAY)
  2172. {
  2173. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  2174. {
  2175. /* Disable the CRYP peripheral clock */
  2176. __HAL_CRYP_DISABLE(hcryp);
  2177. /* Change state */
  2178. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2179. hcryp->State = HAL_CRYP_STATE_READY;
  2180. /* Process unlocked */
  2181. __HAL_UNLOCK(hcryp);
  2182. return HAL_ERROR;
  2183. }
  2184. }
  2185. }
  2186. }
  2187. else
  2188. {
  2189. if(hcryp->Size > 4U)
  2190. {
  2191. payloadlength = (((hcryp->Size)/4)*4) ;
  2192. /*Write input block in the IN FIFO without last block */
  2193. while((hcryp->CrypInCount < payloadlength) && (hcryp->CrypOutCount < payloadlength))
  2194. {
  2195. /* Write input data and get output data */
  2196. CRYP_AES_ProcessData(hcryp,Timeout);
  2197. /* Check for the Timeout */
  2198. if(Timeout != HAL_MAX_DELAY)
  2199. {
  2200. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  2201. {
  2202. /* Disable the CRYP peripheral clock */
  2203. __HAL_CRYP_DISABLE(hcryp);
  2204. /* Change state */
  2205. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2206. hcryp->State = HAL_CRYP_STATE_READY;
  2207. /* Process unlocked */
  2208. __HAL_UNLOCK(hcryp);
  2209. return HAL_ERROR;
  2210. }
  2211. }
  2212. }
  2213. }
  2214. /* CRYP Workaround : CRYP1 generates correct TAG during CCM decryption only when ciphertext blocks size is multiple of
  2215. 128 bits. If lthe size of the last block of payload is inferior to 128 bits, when CCM decryption
  2216. is selected, then the TAG message will be wrong.*/
  2217. CRYP_Workaround(hcryp,Timeout);
  2218. }
  2219. /* Return function status */
  2220. return HAL_OK;
  2221. }
  2222. /**
  2223. * @brief AES CCM encryption/decryption process in interrupt mode
  2224. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2225. * the configuration information for CRYP module
  2226. * @retval HAL status
  2227. */
  2228. static HAL_StatusTypeDef CRYP_AESCCM_Process_IT(CRYP_HandleTypeDef *hcryp)
  2229. {
  2230. __IO uint32_t count = 0U;
  2231. /* Reset CrypHeaderCount */
  2232. hcryp->CrypHeaderCount = 0U;
  2233. /************ Init phase ************/
  2234. CRYP_SET_PHASE(hcryp,CRYP_PHASE_INIT);
  2235. /* Set the key */
  2236. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  2237. /* Set the initialization vector (IV) with CTR1 information */
  2238. hcryp->Instance->IV0LR = (hcryp->Init.B0[0]) & CRYP_CCM_CTR1_0;
  2239. hcryp->Instance->IV0RR = hcryp->Init.B0[1];
  2240. hcryp->Instance->IV1LR = hcryp->Init.B0[2];
  2241. hcryp->Instance->IV1RR = (hcryp->Init.B0[3] & CRYP_CCM_CTR1_1)| CRYP_CCM_CTR1_2;
  2242. /* Enable the CRYP peripheral */
  2243. __HAL_CRYP_ENABLE(hcryp);
  2244. /*Write the B0 packet into CRYP_DR*/
  2245. if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
  2246. {
  2247. hcryp->Instance->DIN = __REV(*(uint32_t*)(hcryp->Init.B0));
  2248. hcryp->Instance->DIN = __REV( *(uint32_t*)(hcryp->Init.B0+1));
  2249. hcryp->Instance->DIN = __REV(*(uint32_t*)(hcryp->Init.B0+2));
  2250. hcryp->Instance->DIN = __REV(*(uint32_t*)(hcryp->Init.B0+3));
  2251. }
  2252. else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
  2253. {
  2254. hcryp->Instance->DIN = __ROR(*(uint32_t*)(hcryp->Init.B0), 16);
  2255. hcryp->Instance->DIN = __ROR( *(uint32_t*)(hcryp->Init.B0+1), 16);
  2256. hcryp->Instance->DIN = __ROR(*(uint32_t*)(hcryp->Init.B0+2), 16);
  2257. hcryp->Instance->DIN = __ROR(*(uint32_t*)(hcryp->Init.B0+3), 16);
  2258. }
  2259. else if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
  2260. {
  2261. hcryp->Instance->DIN = __RBIT(*(uint32_t*)(hcryp->Init.B0));
  2262. hcryp->Instance->DIN = __RBIT( *(uint32_t*)(hcryp->Init.B0+1));
  2263. hcryp->Instance->DIN = __RBIT(*(uint32_t*)(hcryp->Init.B0+2));
  2264. hcryp->Instance->DIN = __RBIT(*(uint32_t*)(hcryp->Init.B0+3));
  2265. }
  2266. else
  2267. {
  2268. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.B0);
  2269. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.B0+1);
  2270. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.B0+2);
  2271. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.B0+3);
  2272. }
  2273. /*Wait for the CRYPEN bit to be cleared*/
  2274. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  2275. do
  2276. {
  2277. if(count-- == 0U)
  2278. {
  2279. /* Disable the CRYP peripheral clock */
  2280. __HAL_CRYP_DISABLE(hcryp);
  2281. /* Change state */
  2282. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2283. hcryp->State = HAL_CRYP_STATE_READY;
  2284. /* Process unlocked */
  2285. __HAL_UNLOCK(hcryp);
  2286. return HAL_ERROR;
  2287. }
  2288. }
  2289. while((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
  2290. /* Select header phase */
  2291. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  2292. /* Enable interrupts */
  2293. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI);
  2294. /* Enable CRYP */
  2295. __HAL_CRYP_ENABLE(hcryp);
  2296. /* Return function status */
  2297. return HAL_OK;
  2298. }
  2299. /**
  2300. * @brief AES CCM encryption/decryption process in DMA mode
  2301. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2302. * the configuration information for CRYP module
  2303. * @retval HAL status
  2304. */
  2305. static HAL_StatusTypeDef CRYP_AESCCM_Process_DMA(CRYP_HandleTypeDef *hcryp)
  2306. {
  2307. uint32_t payloadlength = 0U;
  2308. __IO uint32_t count = 0U;
  2309. /* Reset CrypHeaderCount */
  2310. hcryp->CrypHeaderCount = 0U;
  2311. /************************** Init phase **************************************/
  2312. CRYP_SET_PHASE(hcryp,CRYP_PHASE_INIT);
  2313. /* Set the key */
  2314. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  2315. /* Set the initialization vector (IV) with CTR1 information */
  2316. hcryp->Instance->IV0LR = (hcryp->Init.B0[0]) & CRYP_CCM_CTR1_0;
  2317. hcryp->Instance->IV0RR = hcryp->Init.B0[1];
  2318. hcryp->Instance->IV1LR = hcryp->Init.B0[2];
  2319. hcryp->Instance->IV1RR = (hcryp->Init.B0[3] & CRYP_CCM_CTR1_1)| CRYP_CCM_CTR1_2;
  2320. /* Enable the CRYP peripheral */
  2321. __HAL_CRYP_ENABLE(hcryp);
  2322. /*Write the B0 packet into CRYP_DR*/
  2323. if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
  2324. {
  2325. hcryp->Instance->DIN = __REV(*(uint32_t*)(hcryp->Init.B0));
  2326. hcryp->Instance->DIN = __REV( *(uint32_t*)(hcryp->Init.B0+1));
  2327. hcryp->Instance->DIN = __REV(*(uint32_t*)(hcryp->Init.B0+2));
  2328. hcryp->Instance->DIN = __REV(*(uint32_t*)(hcryp->Init.B0+3));
  2329. }
  2330. else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
  2331. {
  2332. hcryp->Instance->DIN = __ROR(*(uint32_t*)(hcryp->Init.B0), 16);
  2333. hcryp->Instance->DIN = __ROR( *(uint32_t*)(hcryp->Init.B0+1), 16);
  2334. hcryp->Instance->DIN = __ROR(*(uint32_t*)(hcryp->Init.B0+2), 16);
  2335. hcryp->Instance->DIN = __ROR(*(uint32_t*)(hcryp->Init.B0+3), 16);
  2336. }
  2337. else if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
  2338. {
  2339. hcryp->Instance->DIN = __RBIT(*(uint32_t*)(hcryp->Init.B0));
  2340. hcryp->Instance->DIN = __RBIT( *(uint32_t*)(hcryp->Init.B0+1));
  2341. hcryp->Instance->DIN = __RBIT(*(uint32_t*)(hcryp->Init.B0+2));
  2342. hcryp->Instance->DIN = __RBIT(*(uint32_t*)(hcryp->Init.B0+3));
  2343. }
  2344. else
  2345. {
  2346. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.B0);
  2347. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.B0+1);
  2348. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.B0+2);
  2349. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.B0+3);
  2350. }
  2351. /*Wait for the CRYPEN bit to be cleared*/
  2352. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  2353. do
  2354. {
  2355. if(count-- == 0U)
  2356. {
  2357. /* Disable the CRYP peripheral clock */
  2358. __HAL_CRYP_DISABLE(hcryp);
  2359. /* Change state */
  2360. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2361. hcryp->State = HAL_CRYP_STATE_READY;
  2362. /* Process unlocked */
  2363. __HAL_UNLOCK(hcryp);
  2364. return HAL_ERROR;
  2365. }
  2366. }
  2367. while((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
  2368. /********************* Header phase *****************************************/
  2369. if(CRYP_GCMCCM_SetHeaderPhase_DMA(hcryp) != HAL_OK)
  2370. {
  2371. return HAL_ERROR;
  2372. }
  2373. /******************** Payload phase *****************************************/
  2374. /* Set the phase */
  2375. hcryp->Phase = CRYP_PHASE_PROCESS;
  2376. /* Disable the CRYP peripheral */
  2377. __HAL_CRYP_DISABLE(hcryp);
  2378. /* Select payload phase once the header phase is performed */
  2379. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  2380. if(hcryp->Size != 0U)
  2381. {
  2382. /* Size should be %4 otherwise Tag will be incorrectly generated for GCM Encryption & CCM Decryption
  2383. Workaround is implemented in polling mode, so if last block of
  2384. payload <128bit don't use HAL_CRYP_AESGCM_DMA otherwise TAG is incorrectly generated for GCM Encryption. */
  2385. /* Set the input and output addresses and start DMA transfer */
  2386. if ((hcryp->Size % 4U) == 0U)
  2387. {
  2388. CRYP_SetDMAConfig(hcryp, (uint32_t)( hcryp->pCrypInBuffPtr), hcryp->Size, (uint32_t)(hcryp->pCrypOutBuffPtr));
  2389. }
  2390. else
  2391. {
  2392. payloadlength = (hcryp->Size)+(4-(hcryp->Size %4)) ;
  2393. /* Set the input and output addresses and start DMA transfer, pCrypOutBuffPtr size should be %4*/
  2394. CRYP_SetDMAConfig(hcryp, (uint32_t)( hcryp->pCrypInBuffPtr), payloadlength, (uint32_t)(hcryp->pCrypOutBuffPtr));
  2395. }
  2396. }
  2397. else /*Size = 0*/
  2398. {
  2399. /* Process unlocked */
  2400. __HAL_UNLOCK(hcryp);
  2401. /* Change the CRYP state and phase */
  2402. hcryp->State = HAL_CRYP_STATE_READY;
  2403. }
  2404. /* Return function status */
  2405. return HAL_OK;
  2406. }
  2407. /**
  2408. * @brief Sets the payload phase in iterrupt mode
  2409. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2410. * the configuration information for CRYP module
  2411. * @retval state
  2412. */
  2413. static HAL_StatusTypeDef CRYP_GCMCCM_SetPayloadPhase_IT(CRYP_HandleTypeDef *hcryp)
  2414. {
  2415. uint32_t loopcounter = 0U;
  2416. uint32_t temp = 0U; /* Temporary CrypOutBuff */
  2417. /***************************** Payload phase *******************************/
  2418. if(hcryp->Size == 0U)
  2419. {
  2420. /* Disable interrupts */
  2421. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI| CRYP_IT_OUTI);
  2422. /* Process unlocked */
  2423. __HAL_UNLOCK(hcryp);
  2424. /* Change the CRYP state */
  2425. hcryp->State = HAL_CRYP_STATE_READY;
  2426. }
  2427. else if ((hcryp->Size) - (hcryp->CrypInCount) >= 4)
  2428. {
  2429. /* Write the input block in the IN FIFO */
  2430. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  2431. hcryp->CrypInCount++;
  2432. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  2433. hcryp->CrypInCount++;
  2434. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  2435. hcryp->CrypInCount++;
  2436. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  2437. hcryp->CrypInCount++;
  2438. if(hcryp->Size == hcryp->CrypInCount)
  2439. {
  2440. /* Disable interrupts */
  2441. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2442. /* Call the input data transfer complete callback */
  2443. HAL_CRYP_InCpltCallback(hcryp);
  2444. }
  2445. if(hcryp->CrypOutCount < hcryp->Size)
  2446. {
  2447. /* Read the output block from the Output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer */
  2448. temp = hcryp->Instance->DOUT;
  2449. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
  2450. hcryp->CrypOutCount++;
  2451. temp = hcryp->Instance->DOUT;
  2452. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
  2453. hcryp->CrypOutCount++;
  2454. temp = hcryp->Instance->DOUT;
  2455. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
  2456. hcryp->CrypOutCount++;
  2457. temp = hcryp->Instance->DOUT;
  2458. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp;
  2459. hcryp->CrypOutCount++;
  2460. if (hcryp->Size == hcryp->CrypOutCount)
  2461. {
  2462. /* Disable interrupts */
  2463. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2464. /* Change the CRYP state */
  2465. hcryp->State = HAL_CRYP_STATE_READY;
  2466. /* Disable CRYP */
  2467. __HAL_CRYP_DISABLE(hcryp);
  2468. /* Process unlocked */
  2469. __HAL_UNLOCK(hcryp);
  2470. /* Call output transfer complete callback */
  2471. HAL_CRYP_OutCpltCallback(hcryp);
  2472. }
  2473. }
  2474. }
  2475. else if ((hcryp->Size %4U )!= 0U)
  2476. {
  2477. /* Size should be %4 otherwise TAG will be incorrectly generated for GCM Encryption & CCM Decryption
  2478. Workaround is implemented in polling mode, so if last block of
  2479. payload <128bit don't use CRYP_AESGCM_Encrypt_IT otherwise TAG is incorrectly generated. */
  2480. /* Last block optionally pad the data with zeros*/
  2481. for(loopcounter = 0U; loopcounter < (hcryp->Size %4 ); loopcounter++)
  2482. {
  2483. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  2484. hcryp->CrypInCount++;
  2485. }
  2486. while(loopcounter < 4U )
  2487. {
  2488. /* Pad the data with zeros to have a complete block */
  2489. hcryp->Instance->DIN = 0x0U;
  2490. loopcounter++;
  2491. }
  2492. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2493. if((hcryp->Instance->SR & CRYP_FLAG_OFNE ) != RESET)
  2494. {
  2495. for(loopcounter = 0U; loopcounter < 4U; loopcounter++)
  2496. {
  2497. /* Read the output block from the output FIFO and put them in temporary buffer */
  2498. temp= hcryp->Instance->DOUT;
  2499. /*get CrypOutBuff from temporary buffer */
  2500. *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount))=temp;
  2501. hcryp->CrypOutCount++;
  2502. }
  2503. }
  2504. if(hcryp->CrypOutCount >= hcryp->Size)
  2505. {
  2506. /* Disable interrupts */
  2507. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI|CRYP_IT_INI);
  2508. /* Change the CRYP peripheral state */
  2509. hcryp->State = HAL_CRYP_STATE_READY;
  2510. /* Process unlocked */
  2511. __HAL_UNLOCK(hcryp);
  2512. /* Call output transfer complete callback */
  2513. HAL_CRYP_OutCpltCallback(hcryp);
  2514. }
  2515. }
  2516. /* Return function status */
  2517. return HAL_OK;
  2518. }
  2519. /**
  2520. * @brief Sets the header phase in polling mode
  2521. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2522. * the configuration information for CRYP module(Header & HeaderSize)
  2523. * @param Timeout: Timeout value
  2524. * @retval state
  2525. */
  2526. static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  2527. {
  2528. uint32_t loopcounter = 0U;
  2529. /***************************** Header phase for GCM/GMAC or CCM *********************************/
  2530. if((hcryp->Init.HeaderSize != 0U))
  2531. {
  2532. /* Select header phase */
  2533. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  2534. /* Enable the CRYP peripheral */
  2535. __HAL_CRYP_ENABLE(hcryp);
  2536. if ((hcryp->Init.HeaderSize %4U )== 0U)
  2537. {
  2538. /* HeaderSize %4, no padding */
  2539. for(loopcounter = 0U; (loopcounter < hcryp->Init.HeaderSize); loopcounter+=4)
  2540. {
  2541. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2542. hcryp->CrypHeaderCount++ ;
  2543. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2544. hcryp->CrypHeaderCount++ ;
  2545. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2546. hcryp->CrypHeaderCount++ ;
  2547. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2548. hcryp->CrypHeaderCount++ ;
  2549. /* Wait for IFEM to be raised */
  2550. if(CRYP_WaitOnIFEMFlag(hcryp, Timeout) != HAL_OK)
  2551. {
  2552. /* Disable the CRYP peripheral clock */
  2553. __HAL_CRYP_DISABLE(hcryp);
  2554. /* Change state */
  2555. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2556. hcryp->State = HAL_CRYP_STATE_READY;
  2557. /* Process unlocked */
  2558. __HAL_UNLOCK(hcryp);
  2559. return HAL_ERROR;
  2560. }
  2561. }
  2562. }
  2563. else
  2564. {
  2565. /*Write header block in the IN FIFO without last block */
  2566. for(loopcounter = 0U; (loopcounter < ((hcryp->Init.HeaderSize)-(hcryp->Init.HeaderSize %4U ))); loopcounter+= 4U)
  2567. {
  2568. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2569. hcryp->CrypHeaderCount++ ;
  2570. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2571. hcryp->CrypHeaderCount++ ;
  2572. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2573. hcryp->CrypHeaderCount++ ;
  2574. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2575. hcryp->CrypHeaderCount++ ;
  2576. /* Wait for IFEM to be raised */
  2577. if(CRYP_WaitOnIFEMFlag(hcryp, Timeout) != HAL_OK)
  2578. {
  2579. /* Disable the CRYP peripheral clock */
  2580. __HAL_CRYP_DISABLE(hcryp);
  2581. /* Change state */
  2582. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2583. hcryp->State = HAL_CRYP_STATE_READY;
  2584. /* Process unlocked */
  2585. __HAL_UNLOCK(hcryp);
  2586. return HAL_ERROR;
  2587. }
  2588. }
  2589. /* Last block optionally pad the data with zeros*/
  2590. for(loopcounter = 0U; (loopcounter < (hcryp->Init.HeaderSize %4U )); loopcounter++)
  2591. {
  2592. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2593. hcryp->CrypHeaderCount++ ;
  2594. }
  2595. while(loopcounter <4U )
  2596. {
  2597. /* pad the data with zeros to have a complete block */
  2598. hcryp->Instance->DIN = 0x0U;
  2599. loopcounter++;
  2600. }
  2601. /* Wait for CCF IFEM to be raised */
  2602. if(CRYP_WaitOnIFEMFlag(hcryp, Timeout) != HAL_OK)
  2603. {
  2604. /* Disable the CRYP peripheral clock */
  2605. __HAL_CRYP_DISABLE(hcryp);
  2606. /* Change state */
  2607. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2608. hcryp->State = HAL_CRYP_STATE_READY;
  2609. /* Process unlocked */
  2610. __HAL_UNLOCK(hcryp);
  2611. return HAL_ERROR;
  2612. }
  2613. }
  2614. /* Wait until the complete message has been processed */
  2615. if(CRYP_WaitOnBUSYFlag(hcryp, Timeout) != HAL_OK)
  2616. {
  2617. /* Disable the CRYP peripheral clock */
  2618. __HAL_CRYP_DISABLE(hcryp);
  2619. /* Change state */
  2620. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2621. hcryp->State = HAL_CRYP_STATE_READY;
  2622. /* Process unlocked & return error */
  2623. __HAL_UNLOCK(hcryp);
  2624. return HAL_ERROR;
  2625. }
  2626. }
  2627. /* Return function status */
  2628. return HAL_OK;
  2629. }
  2630. /**
  2631. * @brief Sets the header phase when using DMA in process
  2632. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2633. * the configuration information for CRYP module(Header & HeaderSize)
  2634. * @retval None
  2635. */
  2636. static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase_DMA(CRYP_HandleTypeDef *hcryp)
  2637. {
  2638. __IO uint32_t count = 0U;
  2639. uint32_t loopcounter = 0U;
  2640. /***************************** Header phase for GCM/GMAC or CCM *********************************/
  2641. if((hcryp->Init.HeaderSize != 0U))
  2642. {
  2643. /* Select header phase */
  2644. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  2645. /* Enable the CRYP peripheral */
  2646. __HAL_CRYP_ENABLE(hcryp);
  2647. if ((hcryp->Init.HeaderSize %4U )== 0U)
  2648. {
  2649. /* HeaderSize %4, no padding */
  2650. for(loopcounter = 0U; (loopcounter < hcryp->Init.HeaderSize); loopcounter+=4)
  2651. {
  2652. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2653. hcryp->CrypHeaderCount++ ;
  2654. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2655. hcryp->CrypHeaderCount++ ;
  2656. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2657. hcryp->CrypHeaderCount++ ;
  2658. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2659. hcryp->CrypHeaderCount++ ;
  2660. /* Wait for IFEM to be raised */
  2661. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  2662. do
  2663. {
  2664. if(count-- == 0U)
  2665. {
  2666. /* Disable the CRYP peripheral clock */
  2667. __HAL_CRYP_DISABLE(hcryp);
  2668. /* Change state */
  2669. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2670. hcryp->State = HAL_CRYP_STATE_READY;
  2671. /* Process unlocked */
  2672. __HAL_UNLOCK(hcryp);
  2673. return HAL_ERROR;
  2674. }
  2675. }
  2676. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM));
  2677. }
  2678. }
  2679. else
  2680. {
  2681. /*Write header block in the IN FIFO without last block */
  2682. for(loopcounter = 0U; (loopcounter < ((hcryp->Init.HeaderSize)-(hcryp->Init.HeaderSize %4 ))); loopcounter+=4)
  2683. {
  2684. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2685. hcryp->CrypHeaderCount++ ;
  2686. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2687. hcryp->CrypHeaderCount++ ;
  2688. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2689. hcryp->CrypHeaderCount++ ;
  2690. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2691. hcryp->CrypHeaderCount++ ;
  2692. /* Wait for IFEM to be raised */
  2693. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  2694. do
  2695. {
  2696. if(count-- == 0U)
  2697. {
  2698. /* Disable the CRYP peripheral clock */
  2699. __HAL_CRYP_DISABLE(hcryp);
  2700. /* Change state */
  2701. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2702. hcryp->State = HAL_CRYP_STATE_READY;
  2703. /* Process unlocked */
  2704. __HAL_UNLOCK(hcryp);
  2705. return HAL_ERROR;
  2706. }
  2707. }
  2708. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM));
  2709. }
  2710. /* Last block optionally pad the data with zeros*/
  2711. for(loopcounter = 0U; (loopcounter < (hcryp->Init.HeaderSize %4U )); loopcounter++)
  2712. {
  2713. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2714. hcryp->CrypHeaderCount++ ;
  2715. }
  2716. while(loopcounter < 4U )
  2717. {
  2718. /* Pad the data with zeros to have a complete block */
  2719. hcryp->Instance->DIN = 0x0U;
  2720. loopcounter++;
  2721. }
  2722. /* Wait for IFEM to be raised */
  2723. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  2724. do
  2725. {
  2726. if(count-- == 0U)
  2727. {
  2728. /* Disable the CRYP peripheral clock */
  2729. __HAL_CRYP_DISABLE(hcryp);
  2730. /* Change state */
  2731. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2732. hcryp->State = HAL_CRYP_STATE_READY;
  2733. /* Process unlocked */
  2734. __HAL_UNLOCK(hcryp);
  2735. return HAL_ERROR;
  2736. }
  2737. }
  2738. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM));
  2739. }
  2740. /* Wait until the complete message has been processed */
  2741. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  2742. do
  2743. {
  2744. if(count-- == 0U)
  2745. {
  2746. /* Disable the CRYP peripheral clock */
  2747. __HAL_CRYP_DISABLE(hcryp);
  2748. /* Change state */
  2749. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2750. hcryp->State = HAL_CRYP_STATE_READY;
  2751. /* Process unlocked */
  2752. __HAL_UNLOCK(hcryp);
  2753. return HAL_ERROR;
  2754. }
  2755. }
  2756. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY));
  2757. }
  2758. /* Return function status */
  2759. return HAL_OK;
  2760. }
  2761. /**
  2762. * @brief Sets the header phase in interrupt mode
  2763. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2764. * the configuration information for CRYP module(Header & HeaderSize)
  2765. * @retval None
  2766. */
  2767. static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase_IT(CRYP_HandleTypeDef *hcryp)
  2768. {
  2769. uint32_t loopcounter = 0U;
  2770. /***************************** Header phase *********************************/
  2771. if(hcryp->Init.HeaderSize == hcryp->CrypHeaderCount)
  2772. {
  2773. /* Disable interrupts */
  2774. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI );
  2775. /* Disable the CRYP peripheral */
  2776. __HAL_CRYP_DISABLE(hcryp);
  2777. /* Set the phase */
  2778. hcryp->Phase = CRYP_PHASE_PROCESS;
  2779. /* Select payload phase once the header phase is performed */
  2780. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  2781. /* Enable Interrupts */
  2782. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI );
  2783. /* Enable the CRYP peripheral */
  2784. __HAL_CRYP_ENABLE(hcryp);
  2785. }
  2786. else if ((hcryp->Init.HeaderSize) - (hcryp->CrypHeaderCount) >= 4U)
  2787. { /* HeaderSize %4, no padding */
  2788. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount );
  2789. hcryp->CrypHeaderCount++ ;
  2790. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2791. hcryp->CrypHeaderCount++ ;
  2792. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2793. hcryp->CrypHeaderCount++ ;
  2794. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2795. hcryp->CrypHeaderCount++ ;
  2796. }
  2797. else
  2798. {
  2799. /* Last block optionally pad the data with zeros*/
  2800. for(loopcounter = 0U; loopcounter < (hcryp->Init.HeaderSize %4U ); loopcounter++)
  2801. {
  2802. hcryp->Instance->DIN = *(uint32_t*)(hcryp->Init.Header+ hcryp->CrypHeaderCount);
  2803. hcryp->CrypHeaderCount++ ;
  2804. }
  2805. while(loopcounter <4U )
  2806. {
  2807. /* Pad the data with zeros to have a complete block */
  2808. hcryp->Instance->DIN = 0x0U;
  2809. loopcounter++;
  2810. }
  2811. }
  2812. /* Return function status */
  2813. return HAL_OK;
  2814. }
  2815. /**
  2816. * @brief Workaround used for GCM/CCM mode.
  2817. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2818. * the configuration information for CRYP module
  2819. * @param Timeout: Timeout value
  2820. * @retval None
  2821. */
  2822. static void CRYP_Workaround(CRYP_HandleTypeDef *hcryp, uint32_t Timeout )
  2823. {
  2824. uint32_t plength = 0U;
  2825. uint32_t iv1temp = 0U;
  2826. uint32_t temp[4] = {0};
  2827. uint32_t temp2[4]= {0};
  2828. uint32_t intermediate_data[4]={0};
  2829. uint32_t index = 0U;
  2830. /* Workaround 2, case GCM encryption */
  2831. if (hcryp->Init.Algorithm == CRYP_AES_GCM)
  2832. {
  2833. if((hcryp->Instance->CR & CRYP_CR_ALGODIR) == CRYP_OPERATINGMODE_ENCRYPT)
  2834. {/*Workaround in order to properly compute authentication tags while doing
  2835. a GCM encryption with the last block of payload size inferior to 128 bits*/
  2836. /* Disable CRYP to start the final phase */
  2837. __HAL_CRYP_DISABLE(hcryp);
  2838. /*Load CRYP_IV1R register content in a temporary variable. Decrement the value
  2839. by 1 and reinsert the result in CRYP_IV1R register*/
  2840. hcryp->Instance->IV1RR = 0x5U;
  2841. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_AES_CTR);
  2842. /* Enable CRYP to start the final phase */
  2843. __HAL_CRYP_ENABLE(hcryp);
  2844. }
  2845. /* Last block optionally pad the data with zeros*/
  2846. for(index=0; index < (hcryp->Size % 4); index ++)
  2847. {
  2848. /* Write the last input block in the IN FIFO */
  2849. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  2850. hcryp->CrypInCount++;
  2851. }
  2852. while(index < 4U)
  2853. {
  2854. /* Pad the data with zeros to have a complete block */
  2855. hcryp->Instance->DIN = 0U;
  2856. index++;
  2857. }
  2858. /* Wait for OFNE flag to be raised */
  2859. if(CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK)
  2860. {
  2861. /* Disable the CRYP peripheral clock */
  2862. __HAL_CRYP_DISABLE(hcryp);
  2863. /* Change state */
  2864. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2865. hcryp->State = HAL_CRYP_STATE_READY;
  2866. /* Process Unlocked */
  2867. __HAL_UNLOCK(hcryp);
  2868. HAL_CRYP_ErrorCallback(hcryp);
  2869. }
  2870. if(((hcryp->Instance->SR & CRYP_FLAG_OFNE ) != RESET) && (hcryp->CrypOutCount < hcryp->Size))
  2871. {
  2872. for(index=0U; index< 4U;index++)
  2873. {
  2874. /* Read the output block from the output FIFO */
  2875. intermediate_data[index] = hcryp->Instance->DOUT;
  2876. /* Intermediate data buffer to be used in for the workaround*/
  2877. *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount))=intermediate_data[index];
  2878. hcryp->CrypOutCount++;
  2879. }
  2880. }
  2881. if((hcryp->Instance->CR & CRYP_CR_ALGODIR) == CRYP_OPERATINGMODE_ENCRYPT)
  2882. {
  2883. /*workaround in order to properly compute authentication tags while doing
  2884. a GCM encryption with the last block of payload size inferior to 128 bits*/
  2885. /* Change the AES mode to GCM mode and Select Final phase */
  2886. /* configured CHMOD GCM */
  2887. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_AES_GCM);
  2888. /* configured final phase */
  2889. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_GCM_CCMPH, CRYP_PHASE_FINAL);
  2890. for (index=0U; index < (hcryp->Size % 4U); index ++)
  2891. {
  2892. /*Write the intermediate_data in the IN FIFO */
  2893. hcryp->Instance->DIN=intermediate_data[index];
  2894. }
  2895. while(index < 4U)
  2896. {
  2897. /* Pad the data with zeros to have a complete block */
  2898. hcryp->Instance->DIN = 0x0U;
  2899. index++;
  2900. }
  2901. /* Wait for OFNE flag to be raised */
  2902. if(CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK)
  2903. {
  2904. /* Disable the CRYP peripheral clock */
  2905. __HAL_CRYP_DISABLE(hcryp);
  2906. /* Change state */
  2907. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2908. hcryp->State = HAL_CRYP_STATE_READY;
  2909. /* Process unlocked */
  2910. __HAL_UNLOCK(hcryp);
  2911. HAL_CRYP_ErrorCallback(hcryp);
  2912. }
  2913. if((hcryp->Instance->SR & CRYP_FLAG_OFNE ) != RESET)
  2914. {
  2915. for( index=0U; index< 4U;index++)
  2916. {
  2917. intermediate_data[index]=hcryp->Instance->DOUT;
  2918. }
  2919. }
  2920. }
  2921. } /* End of GCM encryption */
  2922. else{ /* Workaround 2, case CCM decryption, in order to properly compute
  2923. authentication tags while doing a CCM decryption with the last block
  2924. of payload size inferior to 128 bits*/
  2925. if((hcryp->Instance->CR & CRYP_CR_ALGODIR) == CRYP_OPERATINGMODE_DECRYPT)
  2926. {
  2927. iv1temp = hcryp->Instance->CSGCMCCM7R;
  2928. /* Disable CRYP to start the final phase */
  2929. __HAL_CRYP_DISABLE(hcryp);
  2930. temp[0]= hcryp->Instance->CSGCMCCM0R;
  2931. temp[1]= hcryp->Instance->CSGCMCCM1R;
  2932. temp[2]= hcryp->Instance->CSGCMCCM2R;
  2933. temp[3]= hcryp->Instance->CSGCMCCM3R;
  2934. hcryp->Instance->IV1RR= iv1temp;
  2935. /* Configured CHMOD CTR */
  2936. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_AES_CTR);
  2937. /* Enable CRYP to start the final phase */
  2938. __HAL_CRYP_ENABLE(hcryp);
  2939. }
  2940. /* Last block optionally pad the data with zeros*/
  2941. for(index=0; index < (hcryp->Size % 4); index ++)
  2942. {
  2943. /* Write the last Input block in the IN FIFO */
  2944. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  2945. hcryp->CrypInCount++;
  2946. }
  2947. while(index < 4)
  2948. {
  2949. /* Pad the data with zeros to have a complete block */
  2950. hcryp->Instance->DIN = 0;
  2951. index++;
  2952. }
  2953. /* Wait for OFNE flag to be raised */
  2954. if(CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK)
  2955. {
  2956. /* Disable the CRYP peripheral clock */
  2957. __HAL_CRYP_DISABLE(hcryp);
  2958. /* Change state */
  2959. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2960. hcryp->State = HAL_CRYP_STATE_READY;
  2961. /* Process Unlocked */
  2962. __HAL_UNLOCK(hcryp);
  2963. HAL_CRYP_ErrorCallback(hcryp);
  2964. }
  2965. if(((hcryp->Instance->SR & CRYP_FLAG_OFNE ) != RESET) && (hcryp->CrypOutCount < hcryp->Size))
  2966. {
  2967. for(index=0U; index< 4U;index++)
  2968. {
  2969. /* Read the Output block from the Output FIFO */
  2970. intermediate_data[index] = hcryp->Instance->DOUT;
  2971. /*intermediate data buffer to be used in for the workaround*/
  2972. *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount))=intermediate_data[index];
  2973. hcryp->CrypOutCount++;
  2974. }
  2975. }
  2976. if((hcryp->Instance->CR & CRYP_CR_ALGODIR) == CRYP_OPERATINGMODE_DECRYPT)
  2977. {
  2978. temp2[0]= hcryp->Instance->CSGCMCCM0R;
  2979. temp2[1]= hcryp->Instance->CSGCMCCM1R;
  2980. temp2[2]= hcryp->Instance->CSGCMCCM2R;
  2981. temp2[3]= hcryp->Instance->CSGCMCCM3R;
  2982. /* configured CHMOD CCM */
  2983. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_AES_CCM);
  2984. /* configured Header phase */
  2985. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_GCM_CCMPH, CRYP_PHASE_HEADER);
  2986. plength=(hcryp->Init.B0[3] & 0x000000FFU);
  2987. /*set to zero the bits corresponding to the padded bits*/
  2988. for(index = (hcryp->Size % 4U); index<4U; index ++)
  2989. {
  2990. intermediate_data[index] =0U;
  2991. }
  2992. if ((plength %4U)==1U)
  2993. {
  2994. intermediate_data[(hcryp->Size % 4U)-1U] = intermediate_data[(hcryp->Size % 4U)-1U] & 0xFF000000U;
  2995. }
  2996. if ((plength %4U)==2U)
  2997. {
  2998. intermediate_data[(hcryp->Size % 4U)-1U] = intermediate_data[(hcryp->Size % 4U)-1U] & 0xFFFF0000U;
  2999. }
  3000. if ((plength %4U)==3U)
  3001. {
  3002. intermediate_data[(hcryp->Size % 4U)-1U] = intermediate_data[(hcryp->Size % 4U)-1U] & 0xFFFFFF00U;
  3003. }
  3004. ;
  3005. for(index=0U; index < 4U ; index ++)
  3006. {
  3007. intermediate_data[index] ^= temp[index];
  3008. intermediate_data[index] ^= temp2[index];
  3009. }
  3010. for(index = 0U; index < 4U; index ++)
  3011. {
  3012. /* Write the last Input block in the IN FIFO */
  3013. hcryp->Instance->DIN = intermediate_data[index] ;
  3014. }
  3015. /* Wait for BUSY flag to be raised */
  3016. if(CRYP_WaitOnBUSYFlag(hcryp, Timeout) != HAL_OK)
  3017. {
  3018. /* Disable the CRYP peripheral clock */
  3019. __HAL_CRYP_DISABLE(hcryp);
  3020. /* Change state */
  3021. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3022. hcryp->State = HAL_CRYP_STATE_READY;
  3023. /* Process Unlocked */
  3024. __HAL_UNLOCK(hcryp);
  3025. HAL_CRYP_ErrorCallback(hcryp);
  3026. }
  3027. }
  3028. } /* End of CCM WKA*/
  3029. /* Process Unlocked */
  3030. __HAL_UNLOCK(hcryp);
  3031. }
  3032. /**
  3033. * @brief Handle CRYP hardware block Timeout when waiting for IFEM flag to be raised.
  3034. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3035. * the configuration information for CRYP module.
  3036. * @param Timeout: Timeout duration.
  3037. * @retval HAL status
  3038. */
  3039. static HAL_StatusTypeDef CRYP_WaitOnIFEMFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  3040. {
  3041. uint32_t tickstart = 0U;
  3042. /* Get timeout */
  3043. tickstart = HAL_GetTick();
  3044. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))
  3045. {
  3046. /* Check for the Timeout */
  3047. if(Timeout != HAL_MAX_DELAY)
  3048. {
  3049. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  3050. {
  3051. return HAL_ERROR;
  3052. }
  3053. }
  3054. }
  3055. return HAL_OK;
  3056. }
  3057. /**
  3058. * @brief Handle CRYP hardware block Timeout when waiting for BUSY flag to be raised.
  3059. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3060. * the configuration information for CRYP module.
  3061. * @param Timeout: Timeout duration.
  3062. * @retval HAL status
  3063. */
  3064. static HAL_StatusTypeDef CRYP_WaitOnBUSYFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  3065. {
  3066. uint32_t tickstart = 0U;
  3067. /* Get timeout */
  3068. tickstart = HAL_GetTick();
  3069. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  3070. {
  3071. /* Check for the Timeout */
  3072. if(Timeout != HAL_MAX_DELAY)
  3073. {
  3074. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  3075. {
  3076. return HAL_ERROR;
  3077. }
  3078. }
  3079. }
  3080. return HAL_OK;
  3081. }
  3082. /**
  3083. * @brief Handle CRYP hardware block Timeout when waiting for OFNE flag to be raised.
  3084. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3085. * the configuration information for CRYP module.
  3086. * @param Timeout: Timeout duration.
  3087. * @retval HAL status
  3088. */
  3089. static HAL_StatusTypeDef CRYP_WaitOnOFNEFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  3090. {
  3091. uint32_t tickstart = 0U;
  3092. /* Get timeout */
  3093. tickstart = HAL_GetTick();
  3094. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
  3095. {
  3096. /* Check for the Timeout */
  3097. if(Timeout != HAL_MAX_DELAY)
  3098. {
  3099. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  3100. {
  3101. return HAL_ERROR;
  3102. }
  3103. }
  3104. }
  3105. return HAL_OK;
  3106. }
  3107. /**
  3108. * @}
  3109. */
  3110. /**
  3111. * @}
  3112. */
  3113. /**
  3114. * @}
  3115. */
  3116. #endif /* HAL_CRYP_MODULE_ENABLED */
  3117. /**
  3118. * @}
  3119. */
  3120. #endif /* CRYP*/
  3121. /**
  3122. * @}
  3123. */
  3124. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/