stm32f4xx_hal_hash.c 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_hash.c
  4. * @author MCD Application Team
  5. * @brief HASH HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the HASH peripheral:
  8. * + Initialization and de-initialization methods
  9. * + HASH or HMAC processing in polling mode
  10. * + HASH or HMAC processing in interrupt mode
  11. * + HASH or HMAC processing in DMA mode
  12. * + Peripheral State methods
  13. * + HASH or HMAC processing suspension/resumption
  14. *
  15. @verbatim
  16. ===============================================================================
  17. ##### How to use this driver #####
  18. ===============================================================================
  19. [..]
  20. The HASH HAL driver can be used as follows:
  21. (#)Initialize the HASH low level resources by implementing the HAL_HASH_MspInit():
  22. (##) Enable the HASH interface clock using __HASH_CLK_ENABLE()
  23. (##) When resorting to interrupt-based APIs (e.g. HAL_HASH_xxx_Start_IT())
  24. (+++) Configure the HASH interrupt priority using HAL_NVIC_SetPriority()
  25. (+++) Enable the HASH IRQ handler using HAL_NVIC_EnableIRQ()
  26. (+++) In HASH IRQ handler, call HAL_HASH_IRQHandler() API
  27. (##) When resorting to DMA-based APIs (e.g. HAL_HASH_xxx_Start_DMA())
  28. (+++) Enable the DMAx interface clock using
  29. __DMAx_CLK_ENABLE()
  30. (+++) Configure and enable one DMA stream to manage data transfer from
  31. memory to peripheral (input stream). Managing data transfer from
  32. peripheral to memory can be performed only using CPU.
  33. (+++) Associate the initialized DMA handle to the HASH DMA handle
  34. using __HAL_LINKDMA()
  35. (+++) Configure the priority and enable the NVIC for the transfer complete
  36. interrupt on the DMA Stream: use
  37. HAL_NVIC_SetPriority() and
  38. HAL_NVIC_EnableIRQ()
  39. (#)Initialize the HASH HAL using HAL_HASH_Init(). This function:
  40. (##) resorts to HAL_HASH_MspInit() for low-level initialization,
  41. (##) configures the data type: 1-bit, 8-bit, 16-bit or 32-bit.
  42. (#)Three processing schemes are available:
  43. (##) Polling mode: processing APIs are blocking functions
  44. i.e. they process the data and wait till the digest computation is finished,
  45. e.g. HAL_HASH_xxx_Start() for HASH or HAL_HMAC_xxx_Start() for HMAC
  46. (##) Interrupt mode: processing APIs are not blocking functions
  47. i.e. they process the data under interrupt,
  48. e.g. HAL_HASH_xxx_Start_IT() for HASH or HAL_HMAC_xxx_Start_IT() for HMAC
  49. (##) DMA mode: processing APIs are not blocking functions and the CPU is
  50. not used for data transfer i.e. the data transfer is ensured by DMA,
  51. e.g. HAL_HASH_xxx_Start_DMA() for HASH or HAL_HMAC_xxx_Start_DMA()
  52. for HMAC. Note that in DMA mode, a call to HAL_HASH_xxx_Finish()
  53. is then required to retrieve the digest.
  54. (#)When the processing function is called after HAL_HASH_Init(), the HASH peripheral is
  55. initialized and processes the buffer fed in input. When the input data have all been
  56. fed to the Peripheral, the digest computation can start.
  57. (#)Multi-buffer processing is possible in polling, interrupt and DMA modes.
  58. (##) In polling mode, only multi-buffer HASH processing is possible.
  59. API HAL_HASH_xxx_Accumulate() must be called for each input buffer, except for the last one.
  60. User must resort to HAL_HASH_xxx_Accumulate_End() to enter the last one and retrieve as
  61. well the computed digest.
  62. (##) In interrupt mode, API HAL_HASH_xxx_Accumulate_IT() must be called for each input buffer,
  63. except for the last one.
  64. User must resort to HAL_HASH_xxx_Accumulate_End_IT() to enter the last one and retrieve as
  65. well the computed digest.
  66. (##) In DMA mode, multi-buffer HASH and HMAC processing are possible.
  67. (+++) HASH processing: once initialization is done, MDMAT bit must be set thru __HAL_HASH_SET_MDMAT() macro.
  68. From that point, each buffer can be fed to the Peripheral thru HAL_HASH_xxx_Start_DMA() API.
  69. Before entering the last buffer, reset the MDMAT bit with __HAL_HASH_RESET_MDMAT()
  70. macro then wrap-up the HASH processing in feeding the last input buffer thru the
  71. same API HAL_HASH_xxx_Start_DMA(). The digest can then be retrieved with a call to
  72. API HAL_HASH_xxx_Finish().
  73. (+++) HMAC processing (requires to resort to extended functions):
  74. after initialization, the key and the first input buffer are entered
  75. in the Peripheral with the API HAL_HMACEx_xxx_Step1_2_DMA(). This carries out HMAC step 1 and
  76. starts step 2.
  77. The following buffers are next entered with the API HAL_HMACEx_xxx_Step2_DMA(). At this
  78. point, the HMAC processing is still carrying out step 2.
  79. Then, step 2 for the last input buffer and step 3 are carried out by a single call
  80. to HAL_HMACEx_xxx_Step2_3_DMA().
  81. The digest can finally be retrieved with a call to API HAL_HASH_xxx_Finish().
  82. (#)Context swapping.
  83. (##) Two APIs are available to suspend HASH or HMAC processing:
  84. (+++) HAL_HASH_SwFeed_ProcessSuspend() when data are entered by software (polling or IT mode),
  85. (+++) HAL_HASH_DMAFeed_ProcessSuspend() when data are entered by DMA.
  86. (##) When HASH or HMAC processing is suspended, HAL_HASH_ContextSaving() allows
  87. to save in memory the Peripheral context. This context can be restored afterwards
  88. to resume the HASH processing thanks to HAL_HASH_ContextRestoring().
  89. (##) Once the HASH Peripheral has been restored to the same configuration as that at suspension
  90. time, processing can be restarted with the same API call (same API, same handle,
  91. same parameters) as done before the suspension. Relevant parameters to restart at
  92. the proper location are internally saved in the HASH handle.
  93. (#)Call HAL_HASH_DeInit() to deinitialize the HASH peripheral.
  94. *** Remarks on message length ***
  95. ===================================
  96. [..]
  97. (#) HAL in interruption mode (interruptions driven)
  98. (##)Due to HASH peripheral hardware design, the peripheral interruption is triggered every 64 bytes.
  99. This is why, for driver implementation simplicity’s sake, user is requested to enter a message the
  100. length of which is a multiple of 4 bytes.
  101. (##) When the message length (in bytes) is not a multiple of words, a specific field exists in HASH_STR
  102. to specify which bits to discard at the end of the complete message to process only the message bits
  103. and not extra bits.
  104. (##) If user needs to perform a hash computation of a large input buffer that is spread around various places
  105. in memory and where each piece of this input buffer is not necessarily a multiple of 4 bytes in size, it
  106. becomes necessary to use a temporary buffer to format the data accordingly before feeding them to the Peripheral.
  107. It is advised to the user to
  108. (+++) achieve the first formatting operation by software then enter the data
  109. (+++) while the Peripheral is processing the first input set, carry out the second formatting operation by software, to be ready when DINIS occurs.
  110. (+++) repeat step 2 until the whole message is processed.
  111. [..]
  112. (#) HAL in DMA mode
  113. (##) Again, due to hardware design, the DMA transfer to feed the data can only be done on a word-basis.
  114. The same field described above in HASH_STR is used to specify which bits to discard at the end of the DMA transfer
  115. to process only the message bits and not extra bits. Due to hardware implementation, this is possible only at the
  116. end of the complete message. When several DMA transfers are needed to enter the message, this is not applicable at
  117. the end of the intermediary transfers.
  118. (##) Similarly to the interruption-driven mode, it is suggested to the user to format the consecutive chunks of data
  119. by software while the DMA transfer and processing is on-going for the first parts of the message. Due to the 32-bit alignment
  120. required for the DMA transfer, it is underlined that the software formatting operation is more complex than in the IT mode.
  121. *** Callback registration ***
  122. ===================================
  123. [..]
  124. (#) The compilation define USE_HAL_HASH_REGISTER_CALLBACKS when set to 1
  125. allows the user to configure dynamically the driver callbacks.
  126. Use function @ref HAL_HASH_RegisterCallback() to register a user callback.
  127. (#) Function @ref HAL_HASH_RegisterCallback() allows to register following callbacks:
  128. (+) InCpltCallback : callback for input completion.
  129. (+) DgstCpltCallback : callback for digest computation completion.
  130. (+) ErrorCallback : callback for error.
  131. (+) MspInitCallback : HASH MspInit.
  132. (+) MspDeInitCallback : HASH MspDeInit.
  133. This function takes as parameters the HAL peripheral handle, the Callback ID
  134. and a pointer to the user callback function.
  135. (#) Use function @ref HAL_HASH_UnRegisterCallback() to reset a callback to the default
  136. weak (surcharged) function.
  137. @ref HAL_HASH_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  138. and the Callback ID.
  139. This function allows to reset following callbacks:
  140. (+) InCpltCallback : callback for input completion.
  141. (+) DgstCpltCallback : callback for digest computation completion.
  142. (+) ErrorCallback : callback for error.
  143. (+) MspInitCallback : HASH MspInit.
  144. (+) MspDeInitCallback : HASH MspDeInit.
  145. (#) By default, after the @ref HAL_HASH_Init and if the state is HAL_HASH_STATE_RESET
  146. all callbacks are reset to the corresponding legacy weak (surcharged) functions:
  147. examples @ref HAL_HASH_InCpltCallback(), @ref HAL_HASH_DgstCpltCallback()
  148. Exception done for MspInit and MspDeInit callbacks that are respectively
  149. reset to the legacy weak (surcharged) functions in the @ref HAL_HASH_Init
  150. and @ref HAL_HASH_DeInit only when these callbacks are null (not registered beforehand)
  151. If not, MspInit or MspDeInit are not null, the @ref HAL_HASH_Init and @ref HAL_HASH_DeInit
  152. keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
  153. Callbacks can be registered/unregistered in READY state only.
  154. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  155. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  156. during the Init/DeInit.
  157. In that case first register the MspInit/MspDeInit user callbacks
  158. using @ref HAL_HASH_RegisterCallback before calling @ref HAL_HASH_DeInit
  159. or @ref HAL_HASH_Init function.
  160. When The compilation define USE_HAL_HASH_REGISTER_CALLBACKS is set to 0 or
  161. not defined, the callback registering feature is not available
  162. and weak (surcharged) callbacks are used.
  163. @endverbatim
  164. ******************************************************************************
  165. * @attention
  166. *
  167. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  168. * All rights reserved.</center></h2>
  169. *
  170. * This software component is licensed by ST under BSD 3-Clause license,
  171. * the "License"; You may not use this file except in compliance with the
  172. * License. You may obtain a copy of the License at:
  173. * opensource.org/licenses/BSD-3-Clause
  174. *
  175. ******************************************************************************
  176. */
  177. /* Includes ------------------------------------------------------------------*/
  178. #include "stm32f4xx_hal.h"
  179. /** @addtogroup STM32F4xx_HAL_Driver
  180. * @{
  181. */
  182. #if defined (HASH)
  183. /** @defgroup HASH HASH
  184. * @brief HASH HAL module driver.
  185. * @{
  186. */
  187. #ifdef HAL_HASH_MODULE_ENABLED
  188. /* Private typedef -----------------------------------------------------------*/
  189. /* Private define ------------------------------------------------------------*/
  190. /** @defgroup HASH_Private_Constants HASH Private Constants
  191. * @{
  192. */
  193. /** @defgroup HASH_Digest_Calculation_Status HASH Digest Calculation Status
  194. * @{
  195. */
  196. #define HASH_DIGEST_CALCULATION_NOT_STARTED ((uint32_t)0x00000000U) /*!< DCAL not set after input data written in DIN register */
  197. #define HASH_DIGEST_CALCULATION_STARTED ((uint32_t)0x00000001U) /*!< DCAL set after input data written in DIN register */
  198. /**
  199. * @}
  200. */
  201. /** @defgroup HASH_Number_Of_CSR_Registers HASH Number of Context Swap Registers
  202. * @{
  203. */
  204. #define HASH_NUMBER_OF_CSR_REGISTERS 54U /*!< Number of Context Swap Registers */
  205. /**
  206. * @}
  207. */
  208. /** @defgroup HASH_TimeOut_Value HASH TimeOut Value
  209. * @{
  210. */
  211. #define HASH_TIMEOUTVALUE 1000U /*!< Time-out value */
  212. /**
  213. * @}
  214. */
  215. /** @defgroup HASH_DMA_Suspension_Words_Limit HASH DMA suspension words limit
  216. * @{
  217. */
  218. #define HASH_DMA_SUSPENSION_WORDS_LIMIT 20U /*!< Number of words below which DMA suspension is aborted */
  219. /**
  220. * @}
  221. */
  222. /**
  223. * @}
  224. */
  225. /* Private macro -------------------------------------------------------------*/
  226. /* Private variables ---------------------------------------------------------*/
  227. /* Private function prototypes -----------------------------------------------*/
  228. /** @defgroup HASH_Private_Functions HASH Private Functions
  229. * @{
  230. */
  231. static void HASH_DMAXferCplt(DMA_HandleTypeDef *hdma);
  232. static void HASH_DMAError(DMA_HandleTypeDef *hdma);
  233. static void HASH_GetDigest(uint8_t *pMsgDigest, uint8_t Size);
  234. static HAL_StatusTypeDef HASH_WaitOnFlagUntilTimeout(HASH_HandleTypeDef *hhash, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
  235. static HAL_StatusTypeDef HASH_WriteData(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
  236. static HAL_StatusTypeDef HASH_IT(HASH_HandleTypeDef *hhash);
  237. static uint32_t HASH_Write_Block_Data(HASH_HandleTypeDef *hhash);
  238. static HAL_StatusTypeDef HMAC_Processing(HASH_HandleTypeDef *hhash, uint32_t Timeout);
  239. /**
  240. * @}
  241. */
  242. /** @defgroup HASH_Exported_Functions HASH Exported Functions
  243. * @{
  244. */
  245. /** @defgroup HASH_Exported_Functions_Group1 Initialization and de-initialization functions
  246. * @brief Initialization, configuration and call-back functions.
  247. *
  248. @verbatim
  249. ===============================================================================
  250. ##### Initialization and de-initialization functions #####
  251. ===============================================================================
  252. [..] This section provides functions allowing to:
  253. (+) Initialize the HASH according to the specified parameters
  254. in the HASH_InitTypeDef and create the associated handle
  255. (+) DeInitialize the HASH peripheral
  256. (+) Initialize the HASH MCU Specific Package (MSP)
  257. (+) DeInitialize the HASH MSP
  258. [..] This section provides as well call back functions definitions for user
  259. code to manage:
  260. (+) Input data transfer to Peripheral completion
  261. (+) Calculated digest retrieval completion
  262. (+) Error management
  263. @endverbatim
  264. * @{
  265. */
  266. /**
  267. * @brief Initialize the HASH according to the specified parameters in the
  268. HASH_HandleTypeDef and create the associated handle.
  269. * @note Only MDMAT and DATATYPE bits of HASH Peripheral are set by HAL_HASH_Init(),
  270. * other configuration bits are set by HASH or HMAC processing APIs.
  271. * @note MDMAT bit is systematically reset by HAL_HASH_Init(). To set it for
  272. * multi-buffer HASH processing, user needs to resort to
  273. * __HAL_HASH_SET_MDMAT() macro. For HMAC multi-buffer processing, the
  274. * relevant APIs manage themselves the MDMAT bit.
  275. * @param hhash HASH handle
  276. * @retval HAL status
  277. */
  278. HAL_StatusTypeDef HAL_HASH_Init(HASH_HandleTypeDef *hhash)
  279. {
  280. /* Check the hash handle allocation */
  281. if(hhash == NULL)
  282. {
  283. return HAL_ERROR;
  284. }
  285. /* Check the parameters */
  286. assert_param(IS_HASH_DATATYPE(hhash->Init.DataType));
  287. #if (USE_HAL_HASH_REGISTER_CALLBACKS == 1)
  288. if (hhash->State == HAL_HASH_STATE_RESET)
  289. {
  290. /* Allocate lock resource and initialize it */
  291. hhash->Lock = HAL_UNLOCKED;
  292. /* Reset Callback pointers in HAL_HASH_STATE_RESET only */
  293. hhash->InCpltCallback = HAL_HASH_InCpltCallback; /* Legacy weak (surcharged) input completion callback */
  294. hhash->DgstCpltCallback = HAL_HASH_DgstCpltCallback; /* Legacy weak (surcharged) digest computation completion callback */
  295. hhash->ErrorCallback = HAL_HASH_ErrorCallback; /* Legacy weak (surcharged) error callback */
  296. if(hhash->MspInitCallback == NULL)
  297. {
  298. hhash->MspInitCallback = HAL_HASH_MspInit;
  299. }
  300. /* Init the low level hardware */
  301. hhash->MspInitCallback(hhash);
  302. }
  303. #else
  304. if(hhash->State == HAL_HASH_STATE_RESET)
  305. {
  306. /* Allocate lock resource and initialize it */
  307. hhash->Lock = HAL_UNLOCKED;
  308. /* Init the low level hardware */
  309. HAL_HASH_MspInit(hhash);
  310. }
  311. #endif /* (USE_HAL_HASH_REGISTER_CALLBACKS) */
  312. /* Change the HASH state */
  313. hhash->State = HAL_HASH_STATE_BUSY;
  314. /* Reset HashInCount, HashITCounter, HashBuffSize and NbWordsAlreadyPushed */
  315. hhash->HashInCount = 0;
  316. hhash->HashBuffSize = 0;
  317. hhash->HashITCounter = 0;
  318. hhash->NbWordsAlreadyPushed = 0;
  319. /* Reset digest calculation bridle (MDMAT bit control) */
  320. hhash->DigestCalculationDisable = RESET;
  321. /* Set phase to READY */
  322. hhash->Phase = HAL_HASH_PHASE_READY;
  323. /* Reset suspension request flag */
  324. hhash->SuspendRequest = HAL_HASH_SUSPEND_NONE;
  325. /* Set the data type bit */
  326. MODIFY_REG(HASH->CR, HASH_CR_DATATYPE, hhash->Init.DataType);
  327. #if defined(HASH_CR_MDMAT)
  328. /* Reset MDMAT bit */
  329. __HAL_HASH_RESET_MDMAT();
  330. #endif
  331. /* Reset HASH handle status */
  332. hhash->Status = HAL_OK;
  333. /* Set the HASH state to Ready */
  334. hhash->State = HAL_HASH_STATE_READY;
  335. /* Initialise the error code */
  336. hhash->ErrorCode = HAL_HASH_ERROR_NONE;
  337. /* Return function status */
  338. return HAL_OK;
  339. }
  340. /**
  341. * @brief DeInitialize the HASH peripheral.
  342. * @param hhash HASH handle.
  343. * @retval HAL status
  344. */
  345. HAL_StatusTypeDef HAL_HASH_DeInit(HASH_HandleTypeDef *hhash)
  346. {
  347. /* Check the HASH handle allocation */
  348. if(hhash == NULL)
  349. {
  350. return HAL_ERROR;
  351. }
  352. /* Change the HASH state */
  353. hhash->State = HAL_HASH_STATE_BUSY;
  354. /* Set the default HASH phase */
  355. hhash->Phase = HAL_HASH_PHASE_READY;
  356. /* Reset HashInCount, HashITCounter and HashBuffSize */
  357. hhash->HashInCount = 0;
  358. hhash->HashBuffSize = 0;
  359. hhash->HashITCounter = 0;
  360. /* Reset digest calculation bridle (MDMAT bit control) */
  361. hhash->DigestCalculationDisable = RESET;
  362. #if (USE_HAL_HASH_REGISTER_CALLBACKS == 1)
  363. if(hhash->MspDeInitCallback == NULL)
  364. {
  365. hhash->MspDeInitCallback = HAL_HASH_MspDeInit;
  366. }
  367. /* DeInit the low level hardware */
  368. hhash->MspDeInitCallback(hhash);
  369. #else
  370. /* DeInit the low level hardware: CLOCK, NVIC */
  371. HAL_HASH_MspDeInit(hhash);
  372. #endif /* (USE_HAL_HASH_REGISTER_CALLBACKS) */
  373. /* Reset HASH handle status */
  374. hhash->Status = HAL_OK;
  375. /* Set the HASH state to Ready */
  376. hhash->State = HAL_HASH_STATE_RESET;
  377. /* Initialise the error code */
  378. hhash->ErrorCode = HAL_HASH_ERROR_NONE;
  379. /* Reset multi buffers accumulation flag */
  380. hhash->Accumulation = 0U;
  381. /* Return function status */
  382. return HAL_OK;
  383. }
  384. /**
  385. * @brief Initialize the HASH MSP.
  386. * @param hhash HASH handle.
  387. * @retval None
  388. */
  389. __weak void HAL_HASH_MspInit(HASH_HandleTypeDef *hhash)
  390. {
  391. /* Prevent unused argument(s) compilation warning */
  392. UNUSED(hhash);
  393. /* NOTE : This function should not be modified; when the callback is needed,
  394. HAL_HASH_MspInit() can be implemented in the user file.
  395. */
  396. }
  397. /**
  398. * @brief DeInitialize the HASH MSP.
  399. * @param hhash HASH handle.
  400. * @retval None
  401. */
  402. __weak void HAL_HASH_MspDeInit(HASH_HandleTypeDef *hhash)
  403. {
  404. /* Prevent unused argument(s) compilation warning */
  405. UNUSED(hhash);
  406. /* NOTE : This function should not be modified; when the callback is needed,
  407. HAL_HASH_MspDeInit() can be implemented in the user file.
  408. */
  409. }
  410. /**
  411. * @brief Input data transfer complete call back.
  412. * @note HAL_HASH_InCpltCallback() is called when the complete input message
  413. * has been fed to the Peripheral. This API is invoked only when input data are
  414. * entered under interruption or thru DMA.
  415. * @note In case of HASH or HMAC multi-buffer DMA feeding case (MDMAT bit set),
  416. * HAL_HASH_InCpltCallback() is called at the end of each buffer feeding
  417. * to the Peripheral.
  418. * @param hhash HASH handle.
  419. * @retval None
  420. */
  421. __weak void HAL_HASH_InCpltCallback(HASH_HandleTypeDef *hhash)
  422. {
  423. /* Prevent unused argument(s) compilation warning */
  424. UNUSED(hhash);
  425. /* NOTE : This function should not be modified; when the callback is needed,
  426. HAL_HASH_InCpltCallback() can be implemented in the user file.
  427. */
  428. }
  429. /**
  430. * @brief Digest computation complete call back.
  431. * @note HAL_HASH_DgstCpltCallback() is used under interruption, is not
  432. * relevant with DMA.
  433. * @param hhash HASH handle.
  434. * @retval None
  435. */
  436. __weak void HAL_HASH_DgstCpltCallback(HASH_HandleTypeDef *hhash)
  437. {
  438. /* Prevent unused argument(s) compilation warning */
  439. UNUSED(hhash);
  440. /* NOTE : This function should not be modified; when the callback is needed,
  441. HAL_HASH_DgstCpltCallback() can be implemented in the user file.
  442. */
  443. }
  444. /**
  445. * @brief Error callback.
  446. * @note Code user can resort to hhash->Status (HAL_ERROR, HAL_TIMEOUT,...)
  447. * to retrieve the error type.
  448. * @param hhash HASH handle.
  449. * @retval None
  450. */
  451. __weak void HAL_HASH_ErrorCallback(HASH_HandleTypeDef *hhash)
  452. {
  453. /* Prevent unused argument(s) compilation warning */
  454. UNUSED(hhash);
  455. /* NOTE : This function should not be modified; when the callback is needed,
  456. HAL_HASH_ErrorCallback() can be implemented in the user file.
  457. */
  458. }
  459. #if (USE_HAL_HASH_REGISTER_CALLBACKS == 1)
  460. /**
  461. * @brief Register a User HASH Callback
  462. * To be used instead of the weak (surcharged) predefined callback
  463. * @param hhash HASH handle
  464. * @param CallbackID ID of the callback to be registered
  465. * This parameter can be one of the following values:
  466. * @arg @ref HAL_HASH_INPUTCPLT_CB_ID HASH input completion Callback ID
  467. * @arg @ref HAL_HASH_DGSTCPLT_CB_ID HASH digest computation completion Callback ID
  468. * @arg @ref HAL_HASH_ERROR_CB_ID HASH error Callback ID
  469. * @arg @ref HAL_HASH_MSPINIT_CB_ID HASH MspInit callback ID
  470. * @arg @ref HAL_HASH_MSPDEINIT_CB_ID HASH MspDeInit callback ID
  471. * @param pCallback pointer to the Callback function
  472. * @retval status
  473. */
  474. HAL_StatusTypeDef HAL_HASH_RegisterCallback(HASH_HandleTypeDef *hhash, HAL_HASH_CallbackIDTypeDef CallbackID, pHASH_CallbackTypeDef pCallback)
  475. {
  476. HAL_StatusTypeDef status = HAL_OK;
  477. if(pCallback == NULL)
  478. {
  479. /* Update the error code */
  480. hhash->ErrorCode |= HAL_HASH_ERROR_INVALID_CALLBACK;
  481. return HAL_ERROR;
  482. }
  483. /* Process locked */
  484. __HAL_LOCK(hhash);
  485. if(HAL_HASH_STATE_READY == hhash->State)
  486. {
  487. switch (CallbackID)
  488. {
  489. case HAL_HASH_INPUTCPLT_CB_ID :
  490. hhash->InCpltCallback = pCallback;
  491. break;
  492. case HAL_HASH_DGSTCPLT_CB_ID :
  493. hhash->DgstCpltCallback = pCallback;
  494. break;
  495. case HAL_HASH_ERROR_CB_ID :
  496. hhash->ErrorCallback = pCallback;
  497. break;
  498. case HAL_HASH_MSPINIT_CB_ID :
  499. hhash->MspInitCallback = pCallback;
  500. break;
  501. case HAL_HASH_MSPDEINIT_CB_ID :
  502. hhash->MspDeInitCallback = pCallback;
  503. break;
  504. default :
  505. /* Update the error code */
  506. hhash->ErrorCode |= HAL_HASH_ERROR_INVALID_CALLBACK;
  507. /* update return status */
  508. status = HAL_ERROR;
  509. break;
  510. }
  511. }
  512. else if(HAL_HASH_STATE_RESET == hhash->State)
  513. {
  514. switch (CallbackID)
  515. {
  516. case HAL_HASH_MSPINIT_CB_ID :
  517. hhash->MspInitCallback = pCallback;
  518. break;
  519. case HAL_HASH_MSPDEINIT_CB_ID :
  520. hhash->MspDeInitCallback = pCallback;
  521. break;
  522. default :
  523. /* Update the error code */
  524. hhash->ErrorCode |= HAL_HASH_ERROR_INVALID_CALLBACK;
  525. /* update return status */
  526. status = HAL_ERROR;
  527. break;
  528. }
  529. }
  530. else
  531. {
  532. /* Update the error code */
  533. hhash->ErrorCode |= HAL_HASH_ERROR_INVALID_CALLBACK;
  534. /* update return status */
  535. status = HAL_ERROR;
  536. }
  537. /* Release Lock */
  538. __HAL_UNLOCK(hhash);
  539. return status;
  540. }
  541. /**
  542. * @brief Unregister a HASH Callback
  543. * HASH Callback is redirected to the weak (surcharged) predefined callback
  544. * @param hhash HASH handle
  545. * @param CallbackID ID of the callback to be unregistered
  546. * This parameter can be one of the following values:
  547. * @arg @ref HAL_HASH_INPUTCPLT_CB_ID HASH input completion Callback ID
  548. * @arg @ref HAL_HASH_DGSTCPLT_CB_ID HASH digest computation completion Callback ID
  549. * @arg @ref HAL_HASH_ERROR_CB_ID HASH error Callback ID
  550. * @arg @ref HAL_HASH_MSPINIT_CB_ID HASH MspInit callback ID
  551. * @arg @ref HAL_HASH_MSPDEINIT_CB_ID HASH MspDeInit callback ID
  552. * @retval status
  553. */
  554. HAL_StatusTypeDef HAL_HASH_UnRegisterCallback(HASH_HandleTypeDef *hhash, HAL_HASH_CallbackIDTypeDef CallbackID)
  555. {
  556. HAL_StatusTypeDef status = HAL_OK;
  557. /* Process locked */
  558. __HAL_LOCK(hhash);
  559. if(HAL_HASH_STATE_READY == hhash->State)
  560. {
  561. switch (CallbackID)
  562. {
  563. case HAL_HASH_INPUTCPLT_CB_ID :
  564. hhash->InCpltCallback = HAL_HASH_InCpltCallback; /* Legacy weak (surcharged) input completion callback */
  565. break;
  566. case HAL_HASH_DGSTCPLT_CB_ID :
  567. hhash->DgstCpltCallback = HAL_HASH_DgstCpltCallback; /* Legacy weak (surcharged) digest computation completion callback */
  568. break;
  569. case HAL_HASH_ERROR_CB_ID :
  570. hhash->ErrorCallback = HAL_HASH_ErrorCallback; /* Legacy weak (surcharged) error callback */
  571. break;
  572. case HAL_HASH_MSPINIT_CB_ID :
  573. hhash->MspInitCallback = HAL_HASH_MspInit; /* Legacy weak (surcharged) Msp Init */
  574. break;
  575. case HAL_HASH_MSPDEINIT_CB_ID :
  576. hhash->MspDeInitCallback = HAL_HASH_MspDeInit; /* Legacy weak (surcharged) Msp DeInit */
  577. break;
  578. default :
  579. /* Update the error code */
  580. hhash->ErrorCode |= HAL_HASH_ERROR_INVALID_CALLBACK;
  581. /* update return status */
  582. status = HAL_ERROR;
  583. break;
  584. }
  585. }
  586. else if(HAL_HASH_STATE_RESET == hhash->State)
  587. {
  588. switch (CallbackID)
  589. {
  590. case HAL_HASH_MSPINIT_CB_ID :
  591. hhash->MspInitCallback = HAL_HASH_MspInit; /* Legacy weak (surcharged) Msp Init */
  592. break;
  593. case HAL_HASH_MSPDEINIT_CB_ID :
  594. hhash->MspDeInitCallback = HAL_HASH_MspDeInit; /* Legacy weak (surcharged) Msp DeInit */
  595. break;
  596. default :
  597. /* Update the error code */
  598. hhash->ErrorCode |= HAL_HASH_ERROR_INVALID_CALLBACK;
  599. /* update return status */
  600. status = HAL_ERROR;
  601. break;
  602. }
  603. }
  604. else
  605. {
  606. /* Update the error code */
  607. hhash->ErrorCode |= HAL_HASH_ERROR_INVALID_CALLBACK;
  608. /* update return status */
  609. status = HAL_ERROR;
  610. }
  611. /* Release Lock */
  612. __HAL_UNLOCK(hhash);
  613. return status;
  614. }
  615. #endif /* USE_HAL_HASH_REGISTER_CALLBACKS */
  616. /**
  617. * @}
  618. */
  619. /** @defgroup HASH_Exported_Functions_Group2 HASH processing functions in polling mode
  620. * @brief HASH processing functions using polling mode.
  621. *
  622. @verbatim
  623. ===============================================================================
  624. ##### Polling mode HASH processing functions #####
  625. ===============================================================================
  626. [..] This section provides functions allowing to calculate in polling mode
  627. the hash value using one of the following algorithms:
  628. (+) MD5
  629. (++) HAL_HASH_MD5_Start()
  630. (++) HAL_HASH_MD5_Accmlt()
  631. (++) HAL_HASH_MD5_Accmlt_End()
  632. (+) SHA1
  633. (++) HAL_HASH_SHA1_Start()
  634. (++) HAL_HASH_SHA1_Accmlt()
  635. (++) HAL_HASH_SHA1_Accmlt_End()
  636. [..] For a single buffer to be hashed, user can resort to HAL_HASH_xxx_Start().
  637. [..] In case of multi-buffer HASH processing (a single digest is computed while
  638. several buffers are fed to the Peripheral), the user can resort to successive calls
  639. to HAL_HASH_xxx_Accumulate() and wrap-up the digest computation by a call
  640. to HAL_HASH_xxx_Accumulate_End().
  641. @endverbatim
  642. * @{
  643. */
  644. /**
  645. * @brief Initialize the HASH peripheral in MD5 mode, next process pInBuffer then
  646. * read the computed digest.
  647. * @note Digest is available in pOutBuffer.
  648. * @param hhash HASH handle.
  649. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  650. * @param Size length of the input buffer in bytes.
  651. * @param pOutBuffer pointer to the computed digest. Digest size is 16 bytes.
  652. * @param Timeout Timeout value
  653. * @retval HAL status
  654. */
  655. HAL_StatusTypeDef HAL_HASH_MD5_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout)
  656. {
  657. return HASH_Start(hhash, pInBuffer, Size, pOutBuffer, Timeout, HASH_ALGOSELECTION_MD5);
  658. }
  659. /**
  660. * @brief If not already done, initialize the HASH peripheral in MD5 mode then
  661. * processes pInBuffer.
  662. * @note Consecutive calls to HAL_HASH_MD5_Accmlt() can be used to feed
  663. * several input buffers back-to-back to the Peripheral that will yield a single
  664. * HASH signature once all buffers have been entered. Wrap-up of input
  665. * buffers feeding and retrieval of digest is done by a call to
  666. * HAL_HASH_MD5_Accmlt_End().
  667. * @note Field hhash->Phase of HASH handle is tested to check whether or not
  668. * the Peripheral has already been initialized.
  669. * @note Digest is not retrieved by this API, user must resort to HAL_HASH_MD5_Accmlt_End()
  670. * to read it, feeding at the same time the last input buffer to the Peripheral.
  671. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  672. * HASH digest computation is corrupted. Only HAL_HASH_MD5_Accmlt_End() is able
  673. * to manage the ending buffer with a length in bytes not a multiple of 4.
  674. * @param hhash HASH handle.
  675. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  676. * @param Size length of the input buffer in bytes, must be a multiple of 4.
  677. * @retval HAL status
  678. */
  679. HAL_StatusTypeDef HAL_HASH_MD5_Accmlt(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  680. {
  681. return HASH_Accumulate(hhash, pInBuffer, Size,HASH_ALGOSELECTION_MD5);
  682. }
  683. /**
  684. * @brief End computation of a single HASH signature after several calls to HAL_HASH_MD5_Accmlt() API.
  685. * @note Digest is available in pOutBuffer.
  686. * @param hhash HASH handle.
  687. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  688. * @param Size length of the input buffer in bytes.
  689. * @param pOutBuffer pointer to the computed digest. Digest size is 16 bytes.
  690. * @param Timeout Timeout value
  691. * @retval HAL status
  692. */
  693. HAL_StatusTypeDef HAL_HASH_MD5_Accmlt_End(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout)
  694. {
  695. return HASH_Start(hhash, pInBuffer, Size, pOutBuffer, Timeout, HASH_ALGOSELECTION_MD5);
  696. }
  697. /**
  698. * @brief Initialize the HASH peripheral in SHA1 mode, next process pInBuffer then
  699. * read the computed digest.
  700. * @note Digest is available in pOutBuffer.
  701. * @param hhash HASH handle.
  702. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  703. * @param Size length of the input buffer in bytes.
  704. * @param pOutBuffer pointer to the computed digest. Digest size is 20 bytes.
  705. * @param Timeout Timeout value
  706. * @retval HAL status
  707. */
  708. HAL_StatusTypeDef HAL_HASH_SHA1_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout)
  709. {
  710. return HASH_Start(hhash, pInBuffer, Size, pOutBuffer, Timeout, HASH_ALGOSELECTION_SHA1);
  711. }
  712. /**
  713. * @brief If not already done, initialize the HASH peripheral in SHA1 mode then
  714. * processes pInBuffer.
  715. * @note Consecutive calls to HAL_HASH_SHA1_Accmlt() can be used to feed
  716. * several input buffers back-to-back to the Peripheral that will yield a single
  717. * HASH signature once all buffers have been entered. Wrap-up of input
  718. * buffers feeding and retrieval of digest is done by a call to
  719. * HAL_HASH_SHA1_Accmlt_End().
  720. * @note Field hhash->Phase of HASH handle is tested to check whether or not
  721. * the Peripheral has already been initialized.
  722. * @note Digest is not retrieved by this API, user must resort to HAL_HASH_SHA1_Accmlt_End()
  723. * to read it, feeding at the same time the last input buffer to the Peripheral.
  724. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  725. * HASH digest computation is corrupted. Only HAL_HASH_SHA1_Accmlt_End() is able
  726. * to manage the ending buffer with a length in bytes not a multiple of 4.
  727. * @param hhash HASH handle.
  728. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  729. * @param Size length of the input buffer in bytes, must be a multiple of 4.
  730. * @retval HAL status
  731. */
  732. HAL_StatusTypeDef HAL_HASH_SHA1_Accmlt(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  733. {
  734. return HASH_Accumulate(hhash, pInBuffer, Size,HASH_ALGOSELECTION_SHA1);
  735. }
  736. /**
  737. * @brief End computation of a single HASH signature after several calls to HAL_HASH_SHA1_Accmlt() API.
  738. * @note Digest is available in pOutBuffer.
  739. * @param hhash HASH handle.
  740. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  741. * @param Size length of the input buffer in bytes.
  742. * @param pOutBuffer pointer to the computed digest. Digest size is 20 bytes.
  743. * @param Timeout Timeout value
  744. * @retval HAL status
  745. */
  746. HAL_StatusTypeDef HAL_HASH_SHA1_Accmlt_End(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout)
  747. {
  748. return HASH_Start(hhash, pInBuffer, Size, pOutBuffer, Timeout, HASH_ALGOSELECTION_SHA1);
  749. }
  750. /**
  751. * @}
  752. */
  753. /** @defgroup HASH_Exported_Functions_Group3 HASH processing functions in interrupt mode
  754. * @brief HASH processing functions using interrupt mode.
  755. *
  756. @verbatim
  757. ===============================================================================
  758. ##### Interruption mode HASH processing functions #####
  759. ===============================================================================
  760. [..] This section provides functions allowing to calculate in interrupt mode
  761. the hash value using one of the following algorithms:
  762. (+) MD5
  763. (++) HAL_HASH_MD5_Start_IT()
  764. (++) HAL_HASH_MD5_Accmlt_IT()
  765. (++) HAL_HASH_MD5_Accmlt_End_IT()
  766. (+) SHA1
  767. (++) HAL_HASH_SHA1_Start_IT()
  768. (++) HAL_HASH_SHA1_Accmlt_IT()
  769. (++) HAL_HASH_SHA1_Accmlt_End_IT()
  770. [..] API HAL_HASH_IRQHandler() manages each HASH interruption.
  771. [..] Note that HAL_HASH_IRQHandler() manages as well HASH Peripheral interruptions when in
  772. HMAC processing mode.
  773. @endverbatim
  774. * @{
  775. */
  776. /**
  777. * @brief Initialize the HASH peripheral in MD5 mode, next process pInBuffer then
  778. * read the computed digest in interruption mode.
  779. * @note Digest is available in pOutBuffer.
  780. * @param hhash HASH handle.
  781. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  782. * @param Size length of the input buffer in bytes.
  783. * @param pOutBuffer pointer to the computed digest. Digest size is 16 bytes.
  784. * @retval HAL status
  785. */
  786. HAL_StatusTypeDef HAL_HASH_MD5_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer)
  787. {
  788. return HASH_Start_IT(hhash, pInBuffer, Size, pOutBuffer,HASH_ALGOSELECTION_MD5);
  789. }
  790. /**
  791. * @brief If not already done, initialize the HASH peripheral in MD5 mode then
  792. * processes pInBuffer in interruption mode.
  793. * @note Consecutive calls to HAL_HASH_MD5_Accmlt_IT() can be used to feed
  794. * several input buffers back-to-back to the Peripheral that will yield a single
  795. * HASH signature once all buffers have been entered. Wrap-up of input
  796. * buffers feeding and retrieval of digest is done by a call to
  797. * HAL_HASH_MD5_Accmlt_End_IT().
  798. * @note Field hhash->Phase of HASH handle is tested to check whether or not
  799. * the Peripheral has already been initialized.
  800. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  801. * HASH digest computation is corrupted. Only HAL_HASH_MD5_Accmlt_End_IT() is able
  802. * to manage the ending buffer with a length in bytes not a multiple of 4.
  803. * @param hhash HASH handle.
  804. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  805. * @param Size length of the input buffer in bytes, must be a multiple of 4.
  806. * @retval HAL status
  807. */
  808. HAL_StatusTypeDef HAL_HASH_MD5_Accmlt_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  809. {
  810. return HASH_Accumulate_IT(hhash, pInBuffer, Size,HASH_ALGOSELECTION_MD5);
  811. }
  812. /**
  813. * @brief End computation of a single HASH signature after several calls to HAL_HASH_MD5_Accmlt_IT() API.
  814. * @note Digest is available in pOutBuffer.
  815. * @param hhash HASH handle.
  816. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  817. * @param Size length of the input buffer in bytes.
  818. * @param pOutBuffer pointer to the computed digest. Digest size is 16 bytes.
  819. * @retval HAL status
  820. */
  821. HAL_StatusTypeDef HAL_HASH_MD5_Accmlt_End_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer)
  822. {
  823. return HASH_Start_IT(hhash, pInBuffer, Size, pOutBuffer,HASH_ALGOSELECTION_MD5);
  824. }
  825. /**
  826. * @brief Initialize the HASH peripheral in SHA1 mode, next process pInBuffer then
  827. * read the computed digest in interruption mode.
  828. * @note Digest is available in pOutBuffer.
  829. * @param hhash HASH handle.
  830. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  831. * @param Size length of the input buffer in bytes.
  832. * @param pOutBuffer pointer to the computed digest. Digest size is 20 bytes.
  833. * @retval HAL status
  834. */
  835. HAL_StatusTypeDef HAL_HASH_SHA1_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer)
  836. {
  837. return HASH_Start_IT(hhash, pInBuffer, Size, pOutBuffer,HASH_ALGOSELECTION_SHA1);
  838. }
  839. /**
  840. * @brief If not already done, initialize the HASH peripheral in SHA1 mode then
  841. * processes pInBuffer in interruption mode.
  842. * @note Consecutive calls to HAL_HASH_SHA1_Accmlt_IT() can be used to feed
  843. * several input buffers back-to-back to the Peripheral that will yield a single
  844. * HASH signature once all buffers have been entered. Wrap-up of input
  845. * buffers feeding and retrieval of digest is done by a call to
  846. * HAL_HASH_SHA1_Accmlt_End_IT().
  847. * @note Field hhash->Phase of HASH handle is tested to check whether or not
  848. * the Peripheral has already been initialized.
  849. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  850. * HASH digest computation is corrupted. Only HAL_HASH_SHA1_Accmlt_End_IT() is able
  851. * to manage the ending buffer with a length in bytes not a multiple of 4.
  852. * @param hhash HASH handle.
  853. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  854. * @param Size length of the input buffer in bytes, must be a multiple of 4.
  855. * @retval HAL status
  856. */
  857. HAL_StatusTypeDef HAL_HASH_SHA1_Accmlt_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  858. {
  859. return HASH_Accumulate_IT(hhash, pInBuffer, Size,HASH_ALGOSELECTION_SHA1);
  860. }
  861. /**
  862. * @brief End computation of a single HASH signature after several calls to HAL_HASH_SHA1_Accmlt_IT() API.
  863. * @note Digest is available in pOutBuffer.
  864. * @param hhash HASH handle.
  865. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  866. * @param Size length of the input buffer in bytes.
  867. * @param pOutBuffer pointer to the computed digest. Digest size is 20 bytes.
  868. * @retval HAL status
  869. */
  870. HAL_StatusTypeDef HAL_HASH_SHA1_Accmlt_End_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer)
  871. {
  872. return HASH_Start_IT(hhash, pInBuffer, Size, pOutBuffer,HASH_ALGOSELECTION_SHA1);
  873. }
  874. /**
  875. * @brief Handle HASH interrupt request.
  876. * @param hhash HASH handle.
  877. * @note HAL_HASH_IRQHandler() handles interrupts in HMAC processing as well.
  878. * @note In case of error reported during the HASH interruption processing,
  879. * HAL_HASH_ErrorCallback() API is called so that user code can
  880. * manage the error. The error type is available in hhash->Status field.
  881. * @retval None
  882. */
  883. void HAL_HASH_IRQHandler(HASH_HandleTypeDef *hhash)
  884. {
  885. hhash->Status = HASH_IT(hhash);
  886. if (hhash->Status != HAL_OK)
  887. {
  888. hhash->ErrorCode |= HAL_HASH_ERROR_IT;
  889. #if (USE_HAL_HASH_REGISTER_CALLBACKS == 1)
  890. hhash->ErrorCallback(hhash);
  891. #else
  892. HAL_HASH_ErrorCallback(hhash);
  893. #endif /* USE_HAL_HASH_REGISTER_CALLBACKS */
  894. /* After error handling by code user, reset HASH handle HAL status */
  895. hhash->Status = HAL_OK;
  896. }
  897. }
  898. /**
  899. * @}
  900. */
  901. /** @defgroup HASH_Exported_Functions_Group4 HASH processing functions in DMA mode
  902. * @brief HASH processing functions using DMA mode.
  903. *
  904. @verbatim
  905. ===============================================================================
  906. ##### DMA mode HASH processing functions #####
  907. ===============================================================================
  908. [..] This section provides functions allowing to calculate in DMA mode
  909. the hash value using one of the following algorithms:
  910. (+) MD5
  911. (++) HAL_HASH_MD5_Start_DMA()
  912. (++) HAL_HASH_MD5_Finish()
  913. (+) SHA1
  914. (++) HAL_HASH_SHA1_Start_DMA()
  915. (++) HAL_HASH_SHA1_Finish()
  916. [..] When resorting to DMA mode to enter the data in the Peripheral, user must resort
  917. to HAL_HASH_xxx_Start_DMA() then read the resulting digest with
  918. HAL_HASH_xxx_Finish().
  919. [..] In case of multi-buffer HASH processing, MDMAT bit must first be set before
  920. the successive calls to HAL_HASH_xxx_Start_DMA(). Then, MDMAT bit needs to be
  921. reset before the last call to HAL_HASH_xxx_Start_DMA(). Digest is finally
  922. retrieved thanks to HAL_HASH_xxx_Finish().
  923. @endverbatim
  924. * @{
  925. */
  926. /**
  927. * @brief Initialize the HASH peripheral in MD5 mode then initiate a DMA transfer
  928. * to feed the input buffer to the Peripheral.
  929. * @note Once the DMA transfer is finished, HAL_HASH_MD5_Finish() API must
  930. * be called to retrieve the computed digest.
  931. * @param hhash HASH handle.
  932. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  933. * @param Size length of the input buffer in bytes.
  934. * @retval HAL status
  935. */
  936. HAL_StatusTypeDef HAL_HASH_MD5_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  937. {
  938. return HASH_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_MD5);
  939. }
  940. /**
  941. * @brief Return the computed digest in MD5 mode.
  942. * @note The API waits for DCIS to be set then reads the computed digest.
  943. * @note HAL_HASH_MD5_Finish() can be used as well to retrieve the digest in
  944. * HMAC MD5 mode.
  945. * @param hhash HASH handle.
  946. * @param pOutBuffer pointer to the computed digest. Digest size is 16 bytes.
  947. * @param Timeout Timeout value.
  948. * @retval HAL status
  949. */
  950. HAL_StatusTypeDef HAL_HASH_MD5_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout)
  951. {
  952. return HASH_Finish(hhash, pOutBuffer, Timeout);
  953. }
  954. /**
  955. * @brief Initialize the HASH peripheral in SHA1 mode then initiate a DMA transfer
  956. * to feed the input buffer to the Peripheral.
  957. * @note Once the DMA transfer is finished, HAL_HASH_SHA1_Finish() API must
  958. * be called to retrieve the computed digest.
  959. * @param hhash HASH handle.
  960. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  961. * @param Size length of the input buffer in bytes.
  962. * @retval HAL status
  963. */
  964. HAL_StatusTypeDef HAL_HASH_SHA1_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  965. {
  966. return HASH_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_SHA1);
  967. }
  968. /**
  969. * @brief Return the computed digest in SHA1 mode.
  970. * @note The API waits for DCIS to be set then reads the computed digest.
  971. * @note HAL_HASH_SHA1_Finish() can be used as well to retrieve the digest in
  972. * HMAC SHA1 mode.
  973. * @param hhash HASH handle.
  974. * @param pOutBuffer pointer to the computed digest. Digest size is 20 bytes.
  975. * @param Timeout Timeout value.
  976. * @retval HAL status
  977. */
  978. HAL_StatusTypeDef HAL_HASH_SHA1_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout)
  979. {
  980. return HASH_Finish(hhash, pOutBuffer, Timeout);
  981. }
  982. /**
  983. * @}
  984. */
  985. /** @defgroup HASH_Exported_Functions_Group5 HMAC processing functions in polling mode
  986. * @brief HMAC processing functions using polling mode.
  987. *
  988. @verbatim
  989. ===============================================================================
  990. ##### Polling mode HMAC processing functions #####
  991. ===============================================================================
  992. [..] This section provides functions allowing to calculate in polling mode
  993. the HMAC value using one of the following algorithms:
  994. (+) MD5
  995. (++) HAL_HMAC_MD5_Start()
  996. (+) SHA1
  997. (++) HAL_HMAC_SHA1_Start()
  998. @endverbatim
  999. * @{
  1000. */
  1001. /**
  1002. * @brief Initialize the HASH peripheral in HMAC MD5 mode, next process pInBuffer then
  1003. * read the computed digest.
  1004. * @note Digest is available in pOutBuffer.
  1005. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  1006. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  1007. * @param hhash HASH handle.
  1008. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  1009. * @param Size length of the input buffer in bytes.
  1010. * @param pOutBuffer pointer to the computed digest. Digest size is 16 bytes.
  1011. * @param Timeout Timeout value.
  1012. * @retval HAL status
  1013. */
  1014. HAL_StatusTypeDef HAL_HMAC_MD5_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout)
  1015. {
  1016. return HMAC_Start(hhash, pInBuffer, Size, pOutBuffer, Timeout, HASH_ALGOSELECTION_MD5);
  1017. }
  1018. /**
  1019. * @brief Initialize the HASH peripheral in HMAC SHA1 mode, next process pInBuffer then
  1020. * read the computed digest.
  1021. * @note Digest is available in pOutBuffer.
  1022. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  1023. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  1024. * @param hhash HASH handle.
  1025. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  1026. * @param Size length of the input buffer in bytes.
  1027. * @param pOutBuffer pointer to the computed digest. Digest size is 20 bytes.
  1028. * @param Timeout Timeout value.
  1029. * @retval HAL status
  1030. */
  1031. HAL_StatusTypeDef HAL_HMAC_SHA1_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout)
  1032. {
  1033. return HMAC_Start(hhash, pInBuffer, Size, pOutBuffer, Timeout, HASH_ALGOSELECTION_SHA1);
  1034. }
  1035. /**
  1036. * @}
  1037. */
  1038. /** @defgroup HASH_Exported_Functions_Group6 HMAC processing functions in interrupt mode
  1039. * @brief HMAC processing functions using interrupt mode.
  1040. *
  1041. @verbatim
  1042. ===============================================================================
  1043. ##### Interrupt mode HMAC processing functions #####
  1044. ===============================================================================
  1045. [..] This section provides functions allowing to calculate in interrupt mode
  1046. the HMAC value using one of the following algorithms:
  1047. (+) MD5
  1048. (++) HAL_HMAC_MD5_Start_IT()
  1049. (+) SHA1
  1050. (++) HAL_HMAC_SHA1_Start_IT()
  1051. @endverbatim
  1052. * @{
  1053. */
  1054. /**
  1055. * @brief Initialize the HASH peripheral in HMAC MD5 mode, next process pInBuffer then
  1056. * read the computed digest in interrupt mode.
  1057. * @note Digest is available in pOutBuffer.
  1058. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  1059. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  1060. * @param hhash HASH handle.
  1061. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  1062. * @param Size length of the input buffer in bytes.
  1063. * @param pOutBuffer pointer to the computed digest. Digest size is 16 bytes.
  1064. * @retval HAL status
  1065. */
  1066. HAL_StatusTypeDef HAL_HMAC_MD5_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer)
  1067. {
  1068. return HMAC_Start_IT(hhash, pInBuffer, Size, pOutBuffer, HASH_ALGOSELECTION_MD5);
  1069. }
  1070. /**
  1071. * @brief Initialize the HASH peripheral in HMAC SHA1 mode, next process pInBuffer then
  1072. * read the computed digest in interrupt mode.
  1073. * @note Digest is available in pOutBuffer.
  1074. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  1075. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  1076. * @param hhash HASH handle.
  1077. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  1078. * @param Size length of the input buffer in bytes.
  1079. * @param pOutBuffer pointer to the computed digest. Digest size is 20 bytes.
  1080. * @retval HAL status
  1081. */
  1082. HAL_StatusTypeDef HAL_HMAC_SHA1_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer)
  1083. {
  1084. return HMAC_Start_IT(hhash, pInBuffer, Size, pOutBuffer, HASH_ALGOSELECTION_SHA1);
  1085. }
  1086. /**
  1087. * @}
  1088. */
  1089. /** @defgroup HASH_Exported_Functions_Group7 HMAC processing functions in DMA mode
  1090. * @brief HMAC processing functions using DMA modes.
  1091. *
  1092. @verbatim
  1093. ===============================================================================
  1094. ##### DMA mode HMAC processing functions #####
  1095. ===============================================================================
  1096. [..] This section provides functions allowing to calculate in DMA mode
  1097. the HMAC value using one of the following algorithms:
  1098. (+) MD5
  1099. (++) HAL_HMAC_MD5_Start_DMA()
  1100. (+) SHA1
  1101. (++) HAL_HMAC_SHA1_Start_DMA()
  1102. [..] When resorting to DMA mode to enter the data in the Peripheral for HMAC processing,
  1103. user must resort to HAL_HMAC_xxx_Start_DMA() then read the resulting digest
  1104. with HAL_HASH_xxx_Finish().
  1105. @endverbatim
  1106. * @{
  1107. */
  1108. /**
  1109. * @brief Initialize the HASH peripheral in HMAC MD5 mode then initiate the required
  1110. * DMA transfers to feed the key and the input buffer to the Peripheral.
  1111. * @note Once the DMA transfers are finished (indicated by hhash->State set back
  1112. * to HAL_HASH_STATE_READY), HAL_HASH_MD5_Finish() API must be called to retrieve
  1113. * the computed digest.
  1114. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  1115. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  1116. * @note If MDMAT bit is set before calling this function (multi-buffer
  1117. * HASH processing case), the input buffer size (in bytes) must be
  1118. * a multiple of 4 otherwise, the HASH digest computation is corrupted.
  1119. * For the processing of the last buffer of the thread, MDMAT bit must
  1120. * be reset and the buffer length (in bytes) doesn't have to be a
  1121. * multiple of 4.
  1122. * @param hhash HASH handle.
  1123. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  1124. * @param Size length of the input buffer in bytes.
  1125. * @retval HAL status
  1126. */
  1127. HAL_StatusTypeDef HAL_HMAC_MD5_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  1128. {
  1129. return HMAC_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_MD5);
  1130. }
  1131. /**
  1132. * @brief Initialize the HASH peripheral in HMAC SHA1 mode then initiate the required
  1133. * DMA transfers to feed the key and the input buffer to the Peripheral.
  1134. * @note Once the DMA transfers are finished (indicated by hhash->State set back
  1135. * to HAL_HASH_STATE_READY), HAL_HASH_SHA1_Finish() API must be called to retrieve
  1136. * the computed digest.
  1137. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  1138. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  1139. * @note If MDMAT bit is set before calling this function (multi-buffer
  1140. * HASH processing case), the input buffer size (in bytes) must be
  1141. * a multiple of 4 otherwise, the HASH digest computation is corrupted.
  1142. * For the processing of the last buffer of the thread, MDMAT bit must
  1143. * be reset and the buffer length (in bytes) doesn't have to be a
  1144. * multiple of 4.
  1145. * @param hhash HASH handle.
  1146. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  1147. * @param Size length of the input buffer in bytes.
  1148. * @retval HAL status
  1149. */
  1150. HAL_StatusTypeDef HAL_HMAC_SHA1_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  1151. {
  1152. return HMAC_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_SHA1);
  1153. }
  1154. /**
  1155. * @}
  1156. */
  1157. /** @defgroup HASH_Exported_Functions_Group8 Peripheral states functions
  1158. * @brief Peripheral State functions.
  1159. *
  1160. @verbatim
  1161. ===============================================================================
  1162. ##### Peripheral State methods #####
  1163. ===============================================================================
  1164. [..]
  1165. This section permits to get in run-time the state and the peripheral handle
  1166. status of the peripheral:
  1167. (+) HAL_HASH_GetState()
  1168. (+) HAL_HASH_GetStatus()
  1169. [..]
  1170. Additionally, this subsection provides functions allowing to save and restore
  1171. the HASH or HMAC processing context in case of calculation suspension:
  1172. (+) HAL_HASH_ContextSaving()
  1173. (+) HAL_HASH_ContextRestoring()
  1174. [..]
  1175. This subsection provides functions allowing to suspend the HASH processing
  1176. (+) when input are fed to the Peripheral by software
  1177. (++) HAL_HASH_SwFeed_ProcessSuspend()
  1178. (+) when input are fed to the Peripheral by DMA
  1179. (++) HAL_HASH_DMAFeed_ProcessSuspend()
  1180. @endverbatim
  1181. * @{
  1182. */
  1183. /**
  1184. * @brief Return the HASH handle state.
  1185. * @note The API yields the current state of the handle (BUSY, READY,...).
  1186. * @param hhash HASH handle.
  1187. * @retval HAL HASH state
  1188. */
  1189. HAL_HASH_StateTypeDef HAL_HASH_GetState(HASH_HandleTypeDef *hhash)
  1190. {
  1191. return hhash->State;
  1192. }
  1193. /**
  1194. * @brief Return the HASH HAL status.
  1195. * @note The API yields the HAL status of the handle: it is the result of the
  1196. * latest HASH processing and allows to report any issue (e.g. HAL_TIMEOUT).
  1197. * @param hhash HASH handle.
  1198. * @retval HAL status
  1199. */
  1200. HAL_StatusTypeDef HAL_HASH_GetStatus(HASH_HandleTypeDef *hhash)
  1201. {
  1202. return hhash->Status;
  1203. }
  1204. /**
  1205. * @brief Save the HASH context in case of processing suspension.
  1206. * @param hhash HASH handle.
  1207. * @param pMemBuffer pointer to the memory buffer where the HASH context
  1208. * is saved.
  1209. * @note The IMR, STR, CR then all the CSR registers are saved
  1210. * in that order. Only the r/w bits are read to be restored later on.
  1211. * @note By default, all the context swap registers (there are
  1212. * HASH_NUMBER_OF_CSR_REGISTERS of those) are saved.
  1213. * @note pMemBuffer points to a buffer allocated by the user. The buffer size
  1214. * must be at least (HASH_NUMBER_OF_CSR_REGISTERS + 3) * 4 uint8 long.
  1215. * @retval None
  1216. */
  1217. void HAL_HASH_ContextSaving(HASH_HandleTypeDef *hhash, uint8_t* pMemBuffer)
  1218. {
  1219. uint32_t mem_ptr = (uint32_t)pMemBuffer;
  1220. uint32_t csr_ptr = (uint32_t)HASH->CSR;
  1221. uint32_t i;
  1222. /* Prevent unused argument(s) compilation warning */
  1223. UNUSED(hhash);
  1224. /* Save IMR register content */
  1225. *(uint32_t*)(mem_ptr) = READ_BIT(HASH->IMR,HASH_IT_DINI|HASH_IT_DCI);
  1226. mem_ptr+=4U;
  1227. /* Save STR register content */
  1228. *(uint32_t*)(mem_ptr) = READ_BIT(HASH->STR,HASH_STR_NBLW);
  1229. mem_ptr+=4U;
  1230. /* Save CR register content */
  1231. #if defined(HASH_CR_MDMAT)
  1232. *(uint32_t*)(mem_ptr) = READ_BIT(HASH->CR,HASH_CR_DMAE|HASH_CR_DATATYPE|HASH_CR_MODE|HASH_CR_ALGO|HASH_CR_LKEY|HASH_CR_MDMAT);
  1233. #else
  1234. *(uint32_t*)(mem_ptr) = READ_BIT(HASH->CR,HASH_CR_DMAE|HASH_CR_DATATYPE|HASH_CR_MODE|HASH_CR_ALGO|HASH_CR_LKEY);
  1235. #endif
  1236. mem_ptr+=4U;
  1237. /* By default, save all CSRs registers */
  1238. for (i = HASH_NUMBER_OF_CSR_REGISTERS; i >0U; i--)
  1239. {
  1240. *(uint32_t*)(mem_ptr) = *(uint32_t*)(csr_ptr);
  1241. mem_ptr+=4U;
  1242. csr_ptr+=4U;
  1243. }
  1244. }
  1245. /**
  1246. * @brief Restore the HASH context in case of processing resumption.
  1247. * @param hhash HASH handle.
  1248. * @param pMemBuffer pointer to the memory buffer where the HASH context
  1249. * is stored.
  1250. * @note The IMR, STR, CR then all the CSR registers are restored
  1251. * in that order. Only the r/w bits are restored.
  1252. * @note By default, all the context swap registers (HASH_NUMBER_OF_CSR_REGISTERS
  1253. * of those) are restored (all of them have been saved by default
  1254. * beforehand).
  1255. * @retval None
  1256. */
  1257. void HAL_HASH_ContextRestoring(HASH_HandleTypeDef *hhash, uint8_t* pMemBuffer)
  1258. {
  1259. uint32_t mem_ptr = (uint32_t)pMemBuffer;
  1260. uint32_t csr_ptr = (uint32_t)HASH->CSR;
  1261. uint32_t i;
  1262. /* Prevent unused argument(s) compilation warning */
  1263. UNUSED(hhash);
  1264. /* Restore IMR register content */
  1265. WRITE_REG(HASH->IMR, (*(uint32_t*)(mem_ptr)));
  1266. mem_ptr+=4U;
  1267. /* Restore STR register content */
  1268. WRITE_REG(HASH->STR, (*(uint32_t*)(mem_ptr)));
  1269. mem_ptr+=4U;
  1270. /* Restore CR register content */
  1271. WRITE_REG(HASH->CR, (*(uint32_t*)(mem_ptr)));
  1272. mem_ptr+=4U;
  1273. /* Reset the HASH processor before restoring the Context
  1274. Swap Registers (CSR) */
  1275. __HAL_HASH_INIT();
  1276. /* By default, restore all CSR registers */
  1277. for (i = HASH_NUMBER_OF_CSR_REGISTERS; i >0U; i--)
  1278. {
  1279. WRITE_REG((*(uint32_t*)(csr_ptr)), (*(uint32_t*)(mem_ptr)));
  1280. mem_ptr+=4U;
  1281. csr_ptr+=4U;
  1282. }
  1283. }
  1284. /**
  1285. * @brief Initiate HASH processing suspension when in polling or interruption mode.
  1286. * @param hhash HASH handle.
  1287. * @note Set the handle field SuspendRequest to the appropriate value so that
  1288. * the on-going HASH processing is suspended as soon as the required
  1289. * conditions are met. Note that the actual suspension is carried out
  1290. * by the functions HASH_WriteData() in polling mode and HASH_IT() in
  1291. * interruption mode.
  1292. * @retval None
  1293. */
  1294. void HAL_HASH_SwFeed_ProcessSuspend(HASH_HandleTypeDef *hhash)
  1295. {
  1296. /* Set Handle Suspend Request field */
  1297. hhash->SuspendRequest = HAL_HASH_SUSPEND;
  1298. }
  1299. /**
  1300. * @brief Suspend the HASH processing when in DMA mode.
  1301. * @param hhash HASH handle.
  1302. * @note When suspension attempt occurs at the very end of a DMA transfer and
  1303. * all the data have already been entered in the Peripheral, hhash->State is
  1304. * set to HAL_HASH_STATE_READY and the API returns HAL_ERROR. It is
  1305. * recommended to wrap-up the processing in reading the digest as usual.
  1306. * @retval HAL status
  1307. */
  1308. HAL_StatusTypeDef HAL_HASH_DMAFeed_ProcessSuspend(HASH_HandleTypeDef *hhash)
  1309. {
  1310. uint32_t tmp_remaining_DMATransferSize_inWords;
  1311. uint32_t tmp_initial_DMATransferSize_inWords;
  1312. uint32_t tmp_words_already_pushed;
  1313. if (hhash->State == HAL_HASH_STATE_READY)
  1314. {
  1315. return HAL_ERROR;
  1316. }
  1317. else
  1318. {
  1319. /* Make sure there is enough time to suspend the processing */
  1320. tmp_remaining_DMATransferSize_inWords = ((DMA_Stream_TypeDef *)hhash->hdmain->Instance)->NDTR;
  1321. if (tmp_remaining_DMATransferSize_inWords <= HASH_DMA_SUSPENSION_WORDS_LIMIT)
  1322. {
  1323. /* No suspension attempted since almost to the end of the transferred data. */
  1324. /* Best option for user code is to wrap up low priority message hashing */
  1325. return HAL_ERROR;
  1326. }
  1327. /* Wait for BUSY flag to be reset */
  1328. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_BUSY, SET, HASH_TIMEOUTVALUE) != HAL_OK)
  1329. {
  1330. return HAL_TIMEOUT;
  1331. }
  1332. if (__HAL_HASH_GET_FLAG(HASH_FLAG_DCIS) != RESET)
  1333. {
  1334. return HAL_ERROR;
  1335. }
  1336. /* Wait for BUSY flag to be set */
  1337. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_BUSY, RESET, HASH_TIMEOUTVALUE) != HAL_OK)
  1338. {
  1339. return HAL_TIMEOUT;
  1340. }
  1341. /* Disable DMA channel */
  1342. /* Note that the Abort function will
  1343. - Clear the transfer error flags
  1344. - Unlock
  1345. - Set the State
  1346. */
  1347. if (HAL_DMA_Abort(hhash->hdmain) !=HAL_OK)
  1348. {
  1349. return HAL_ERROR;
  1350. }
  1351. /* Clear DMAE bit */
  1352. CLEAR_BIT(HASH->CR,HASH_CR_DMAE);
  1353. /* Wait for BUSY flag to be reset */
  1354. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_BUSY, SET, HASH_TIMEOUTVALUE) != HAL_OK)
  1355. {
  1356. return HAL_TIMEOUT;
  1357. }
  1358. if (__HAL_HASH_GET_FLAG(HASH_FLAG_DCIS) != RESET)
  1359. {
  1360. return HAL_ERROR;
  1361. }
  1362. /* At this point, DMA interface is disabled and no transfer is on-going */
  1363. /* Retrieve from the DMA handle how many words remain to be written */
  1364. tmp_remaining_DMATransferSize_inWords = ((DMA_Stream_TypeDef *)hhash->hdmain->Instance)->NDTR;
  1365. if (tmp_remaining_DMATransferSize_inWords == 0U)
  1366. {
  1367. /* All the DMA transfer is actually done. Suspension occurred at the very end
  1368. of the transfer. Either the digest computation is about to start (HASH case)
  1369. or processing is about to move from one step to another (HMAC case).
  1370. In both cases, the processing can't be suspended at this point. It is
  1371. safer to
  1372. - retrieve the low priority block digest before starting the high
  1373. priority block processing (HASH case)
  1374. - re-attempt a new suspension (HMAC case)
  1375. */
  1376. return HAL_ERROR;
  1377. }
  1378. else
  1379. {
  1380. /* Compute how many words were supposed to be transferred by DMA */
  1381. tmp_initial_DMATransferSize_inWords = (((hhash->HashInCount%4U)!=0U) ? ((hhash->HashInCount+3U)/4U): (hhash->HashInCount/4U));
  1382. /* If discrepancy between the number of words reported by DMA Peripheral and the numbers of words entered as reported
  1383. by HASH Peripheral, correct it */
  1384. /* tmp_words_already_pushed reflects the number of words that were already pushed before
  1385. the start of DMA transfer (multi-buffer processing case) */
  1386. tmp_words_already_pushed = hhash->NbWordsAlreadyPushed;
  1387. if (((tmp_words_already_pushed + tmp_initial_DMATransferSize_inWords - tmp_remaining_DMATransferSize_inWords) %16U) != HASH_NBW_PUSHED())
  1388. {
  1389. tmp_remaining_DMATransferSize_inWords--; /* one less word to be transferred again */
  1390. }
  1391. /* Accordingly, update the input pointer that points at the next word to be transferred to the Peripheral by DMA */
  1392. hhash->pHashInBuffPtr += 4U * (tmp_initial_DMATransferSize_inWords - tmp_remaining_DMATransferSize_inWords) ;
  1393. /* And store in HashInCount the remaining size to transfer (in bytes) */
  1394. hhash->HashInCount = 4U * tmp_remaining_DMATransferSize_inWords;
  1395. }
  1396. /* Set State as suspended */
  1397. hhash->State = HAL_HASH_STATE_SUSPENDED;
  1398. return HAL_OK;
  1399. }
  1400. }
  1401. /**
  1402. * @brief Return the HASH handle error code.
  1403. * @param hhash pointer to a HASH_HandleTypeDef structure.
  1404. * @retval HASH Error Code
  1405. */
  1406. uint32_t HAL_HASH_GetError(HASH_HandleTypeDef *hhash)
  1407. {
  1408. /* Return HASH Error Code */
  1409. return hhash->ErrorCode;
  1410. }
  1411. /**
  1412. * @}
  1413. */
  1414. /**
  1415. * @}
  1416. */
  1417. /** @defgroup HASH_Private_Functions HASH Private Functions
  1418. * @{
  1419. */
  1420. /**
  1421. * @brief DMA HASH Input Data transfer completion callback.
  1422. * @param hdma DMA handle.
  1423. * @note In case of HMAC processing, HASH_DMAXferCplt() initiates
  1424. * the next DMA transfer for the following HMAC step.
  1425. * @retval None
  1426. */
  1427. static void HASH_DMAXferCplt(DMA_HandleTypeDef *hdma)
  1428. {
  1429. HASH_HandleTypeDef* hhash = ( HASH_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1430. uint32_t inputaddr;
  1431. uint32_t buffersize;
  1432. HAL_StatusTypeDef status ;
  1433. if (hhash->State != HAL_HASH_STATE_SUSPENDED)
  1434. {
  1435. /* Disable the DMA transfer */
  1436. CLEAR_BIT(HASH->CR, HASH_CR_DMAE);
  1437. if (READ_BIT(HASH->CR, HASH_CR_MODE) == 0U)
  1438. {
  1439. /* If no HMAC processing, input data transfer is now over */
  1440. /* Change the HASH state to ready */
  1441. hhash->State = HAL_HASH_STATE_READY;
  1442. /* Call Input data transfer complete call back */
  1443. #if (USE_HAL_HASH_REGISTER_CALLBACKS == 1)
  1444. hhash->InCpltCallback(hhash);
  1445. #else
  1446. HAL_HASH_InCpltCallback(hhash);
  1447. #endif /* USE_HAL_HASH_REGISTER_CALLBACKS */
  1448. }
  1449. else
  1450. {
  1451. /* HMAC processing: depending on the current HMAC step and whether or
  1452. not multi-buffer processing is on-going, the next step is initiated
  1453. and MDMAT bit is set. */
  1454. if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_3)
  1455. {
  1456. /* This is the end of HMAC processing */
  1457. /* Change the HASH state to ready */
  1458. hhash->State = HAL_HASH_STATE_READY;
  1459. /* Call Input data transfer complete call back
  1460. (note that the last DMA transfer was that of the key
  1461. for the outer HASH operation). */
  1462. #if (USE_HAL_HASH_REGISTER_CALLBACKS == 1)
  1463. hhash->InCpltCallback(hhash);
  1464. #else
  1465. HAL_HASH_InCpltCallback(hhash);
  1466. #endif /* USE_HAL_HASH_REGISTER_CALLBACKS */
  1467. return;
  1468. }
  1469. else if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_1)
  1470. {
  1471. inputaddr = (uint32_t)hhash->pHashMsgBuffPtr; /* DMA transfer start address */
  1472. buffersize = hhash->HashBuffSize; /* DMA transfer size (in bytes) */
  1473. hhash->Phase = HAL_HASH_PHASE_HMAC_STEP_2; /* Move phase from Step 1 to Step 2 */
  1474. /* In case of suspension request, save the new starting parameters */
  1475. hhash->HashInCount = hhash->HashBuffSize; /* Initial DMA transfer size (in bytes) */
  1476. hhash->pHashInBuffPtr = hhash->pHashMsgBuffPtr ; /* DMA transfer start address */
  1477. hhash->NbWordsAlreadyPushed = 0U; /* Reset number of words already pushed */
  1478. #if defined(HASH_CR_MDMAT)
  1479. /* Check whether or not digest calculation must be disabled (in case of multi-buffer HMAC processing) */
  1480. if (hhash->DigestCalculationDisable != RESET)
  1481. {
  1482. /* Digest calculation is disabled: Step 2 must start with MDMAT bit set,
  1483. no digest calculation will be triggered at the end of the input buffer feeding to the Peripheral */
  1484. __HAL_HASH_SET_MDMAT();
  1485. }
  1486. #endif
  1487. }
  1488. else /*case (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_2)*/
  1489. {
  1490. if (hhash->DigestCalculationDisable != RESET)
  1491. {
  1492. /* No automatic move to Step 3 as a new message buffer will be fed to the Peripheral
  1493. (case of multi-buffer HMAC processing):
  1494. DCAL must not be set.
  1495. Phase remains in Step 2, MDMAT remains set at this point.
  1496. Change the HASH state to ready and call Input data transfer complete call back. */
  1497. hhash->State = HAL_HASH_STATE_READY;
  1498. #if (USE_HAL_HASH_REGISTER_CALLBACKS == 1)
  1499. hhash->InCpltCallback(hhash);
  1500. #else
  1501. HAL_HASH_InCpltCallback(hhash);
  1502. #endif /* USE_HAL_HASH_REGISTER_CALLBACKS */
  1503. return ;
  1504. }
  1505. else
  1506. {
  1507. /* Digest calculation is not disabled (case of single buffer input or last buffer
  1508. of multi-buffer HMAC processing) */
  1509. inputaddr = (uint32_t)hhash->Init.pKey; /* DMA transfer start address */
  1510. buffersize = hhash->Init.KeySize; /* DMA transfer size (in bytes) */
  1511. hhash->Phase = HAL_HASH_PHASE_HMAC_STEP_3; /* Move phase from Step 2 to Step 3 */
  1512. /* In case of suspension request, save the new starting parameters */
  1513. hhash->HashInCount = hhash->Init.KeySize; /* Initial size for second DMA transfer (input data) */
  1514. hhash->pHashInBuffPtr = hhash->Init.pKey ; /* address passed to DMA, now entering data message */
  1515. hhash->NbWordsAlreadyPushed = 0U; /* Reset number of words already pushed */
  1516. }
  1517. }
  1518. /* Configure the Number of valid bits in last word of the message */
  1519. __HAL_HASH_SET_NBVALIDBITS(buffersize);
  1520. /* Set the HASH DMA transfert completion call back */
  1521. hhash->hdmain->XferCpltCallback = HASH_DMAXferCplt;
  1522. /* Enable the DMA In DMA Stream */
  1523. status = HAL_DMA_Start_IT(hhash->hdmain, inputaddr, (uint32_t)&HASH->DIN, (((buffersize %4U)!=0U) ? ((buffersize+(4U-(buffersize %4U)))/4U):(buffersize/4U)));
  1524. /* Enable DMA requests */
  1525. SET_BIT(HASH->CR, HASH_CR_DMAE);
  1526. /* Return function status */
  1527. if (status != HAL_OK)
  1528. {
  1529. /* Update HASH state machine to error */
  1530. hhash->State = HAL_HASH_STATE_ERROR;
  1531. }
  1532. else
  1533. {
  1534. /* Change HASH state */
  1535. hhash->State = HAL_HASH_STATE_READY;
  1536. }
  1537. }
  1538. }
  1539. return;
  1540. }
  1541. /**
  1542. * @brief DMA HASH communication error callback.
  1543. * @param hdma DMA handle.
  1544. * @note HASH_DMAError() callback invokes HAL_HASH_ErrorCallback() that
  1545. * can contain user code to manage the error.
  1546. * @retval None
  1547. */
  1548. static void HASH_DMAError(DMA_HandleTypeDef *hdma)
  1549. {
  1550. HASH_HandleTypeDef* hhash = ( HASH_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1551. if (hhash->State != HAL_HASH_STATE_SUSPENDED)
  1552. {
  1553. hhash->ErrorCode |= HAL_HASH_ERROR_DMA;
  1554. /* Set HASH state to ready to prevent any blocking issue in user code
  1555. present in HAL_HASH_ErrorCallback() */
  1556. hhash->State= HAL_HASH_STATE_READY;
  1557. /* Set HASH handle status to error */
  1558. hhash->Status = HAL_ERROR;
  1559. #if (USE_HAL_HASH_REGISTER_CALLBACKS == 1)
  1560. hhash->ErrorCallback(hhash);
  1561. #else
  1562. HAL_HASH_ErrorCallback(hhash);
  1563. #endif /* USE_HAL_HASH_REGISTER_CALLBACKS */
  1564. /* After error handling by code user, reset HASH handle HAL status */
  1565. hhash->Status = HAL_OK;
  1566. }
  1567. }
  1568. /**
  1569. * @brief Feed the input buffer to the HASH Peripheral.
  1570. * @param hhash HASH handle.
  1571. * @param pInBuffer pointer to input buffer.
  1572. * @param Size the size of input buffer in bytes.
  1573. * @note HASH_WriteData() regularly reads hhash->SuspendRequest to check whether
  1574. * or not the HASH processing must be suspended. If this is the case, the
  1575. * processing is suspended when possible and the Peripheral feeding point reached at
  1576. * suspension time is stored in the handle for resumption later on.
  1577. * @retval HAL status
  1578. */
  1579. static HAL_StatusTypeDef HASH_WriteData(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  1580. {
  1581. uint32_t buffercounter;
  1582. __IO uint32_t inputaddr = (uint32_t) pInBuffer;
  1583. for(buffercounter = 0U; buffercounter < Size; buffercounter+=4U)
  1584. {
  1585. /* Write input data 4 bytes at a time */
  1586. HASH->DIN = *(uint32_t*)inputaddr;
  1587. inputaddr+=4U;
  1588. /* If the suspension flag has been raised and if the processing is not about
  1589. to end, suspend processing */
  1590. if ((hhash->SuspendRequest == HAL_HASH_SUSPEND) && ((buffercounter+4U) < Size))
  1591. {
  1592. /* Wait for DINIS = 1, which occurs when 16 32-bit locations are free
  1593. in the input buffer */
  1594. if (__HAL_HASH_GET_FLAG(HASH_FLAG_DINIS))
  1595. {
  1596. /* Reset SuspendRequest */
  1597. hhash->SuspendRequest = HAL_HASH_SUSPEND_NONE;
  1598. /* Depending whether the key or the input data were fed to the Peripheral, the feeding point
  1599. reached at suspension time is not saved in the same handle fields */
  1600. if ((hhash->Phase == HAL_HASH_PHASE_PROCESS) || (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_2))
  1601. {
  1602. /* Save current reading and writing locations of Input and Output buffers */
  1603. hhash->pHashInBuffPtr = (uint8_t *)inputaddr;
  1604. /* Save the number of bytes that remain to be processed at this point */
  1605. hhash->HashInCount = Size - (buffercounter + 4U);
  1606. }
  1607. else if ((hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_1) || (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_3))
  1608. {
  1609. /* Save current reading and writing locations of Input and Output buffers */
  1610. hhash->pHashKeyBuffPtr = (uint8_t *)inputaddr;
  1611. /* Save the number of bytes that remain to be processed at this point */
  1612. hhash->HashKeyCount = Size - (buffercounter + 4U);
  1613. }
  1614. else
  1615. {
  1616. /* Unexpected phase: unlock process and report error */
  1617. hhash->State = HAL_HASH_STATE_READY;
  1618. __HAL_UNLOCK(hhash);
  1619. return HAL_ERROR;
  1620. }
  1621. /* Set the HASH state to Suspended and exit to stop entering data */
  1622. hhash->State = HAL_HASH_STATE_SUSPENDED;
  1623. return HAL_OK;
  1624. } /* if (__HAL_HASH_GET_FLAG(HASH_FLAG_DINIS)) */
  1625. } /* if ((hhash->SuspendRequest == HAL_HASH_SUSPEND) && ((buffercounter+4) < Size)) */
  1626. } /* for(buffercounter = 0; buffercounter < Size; buffercounter+=4) */
  1627. /* At this point, all the data have been entered to the Peripheral: exit */
  1628. return HAL_OK;
  1629. }
  1630. /**
  1631. * @brief Retrieve the message digest.
  1632. * @param pMsgDigest pointer to the computed digest.
  1633. * @param Size message digest size in bytes.
  1634. * @retval None
  1635. */
  1636. static void HASH_GetDigest(uint8_t *pMsgDigest, uint8_t Size)
  1637. {
  1638. uint32_t msgdigest = (uint32_t)pMsgDigest;
  1639. switch(Size)
  1640. {
  1641. /* Read the message digest */
  1642. case 16: /* MD5 */
  1643. *(uint32_t*)(msgdigest) = __REV(HASH->HR[0]);
  1644. msgdigest+=4U;
  1645. *(uint32_t*)(msgdigest) = __REV(HASH->HR[1]);
  1646. msgdigest+=4U;
  1647. *(uint32_t*)(msgdigest) = __REV(HASH->HR[2]);
  1648. msgdigest+=4U;
  1649. *(uint32_t*)(msgdigest) = __REV(HASH->HR[3]);
  1650. break;
  1651. case 20: /* SHA1 */
  1652. *(uint32_t*)(msgdigest) = __REV(HASH->HR[0]);
  1653. msgdigest+=4U;
  1654. *(uint32_t*)(msgdigest) = __REV(HASH->HR[1]);
  1655. msgdigest+=4U;
  1656. *(uint32_t*)(msgdigest) = __REV(HASH->HR[2]);
  1657. msgdigest+=4U;
  1658. *(uint32_t*)(msgdigest) = __REV(HASH->HR[3]);
  1659. msgdigest+=4U;
  1660. *(uint32_t*)(msgdigest) = __REV(HASH->HR[4]);
  1661. break;
  1662. case 28: /* SHA224 */
  1663. *(uint32_t*)(msgdigest) = __REV(HASH->HR[0]);
  1664. msgdigest+=4U;
  1665. *(uint32_t*)(msgdigest) = __REV(HASH->HR[1]);
  1666. msgdigest+=4U;
  1667. *(uint32_t*)(msgdigest) = __REV(HASH->HR[2]);
  1668. msgdigest+=4U;
  1669. *(uint32_t*)(msgdigest) = __REV(HASH->HR[3]);
  1670. msgdigest+=4U;
  1671. *(uint32_t*)(msgdigest) = __REV(HASH->HR[4]);
  1672. #if defined(HASH_CR_MDMAT)
  1673. msgdigest+=4U;
  1674. *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[5]);
  1675. msgdigest+=4U;
  1676. *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[6]);
  1677. #endif
  1678. break;
  1679. case 32: /* SHA256 */
  1680. *(uint32_t*)(msgdigest) = __REV(HASH->HR[0]);
  1681. msgdigest+=4U;
  1682. *(uint32_t*)(msgdigest) = __REV(HASH->HR[1]);
  1683. msgdigest+=4U;
  1684. *(uint32_t*)(msgdigest) = __REV(HASH->HR[2]);
  1685. msgdigest+=4U;
  1686. *(uint32_t*)(msgdigest) = __REV(HASH->HR[3]);
  1687. msgdigest+=4U;
  1688. *(uint32_t*)(msgdigest) = __REV(HASH->HR[4]);
  1689. #if defined(HASH_CR_MDMAT)
  1690. msgdigest+=4U;
  1691. *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[5]);
  1692. msgdigest+=4U;
  1693. *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[6]);
  1694. msgdigest+=4U;
  1695. *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[7]);
  1696. #endif
  1697. break;
  1698. default:
  1699. break;
  1700. }
  1701. }
  1702. /**
  1703. * @brief Handle HASH processing Timeout.
  1704. * @param hhash HASH handle.
  1705. * @param Flag specifies the HASH flag to check.
  1706. * @param Status the Flag status (SET or RESET).
  1707. * @param Timeout Timeout duration.
  1708. * @retval HAL status
  1709. */
  1710. static HAL_StatusTypeDef HASH_WaitOnFlagUntilTimeout(HASH_HandleTypeDef *hhash, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  1711. {
  1712. uint32_t tickstart = HAL_GetTick();
  1713. /* Wait until flag is set */
  1714. if(Status == RESET)
  1715. {
  1716. while(__HAL_HASH_GET_FLAG(Flag) == RESET)
  1717. {
  1718. /* Check for the Timeout */
  1719. if(Timeout != HAL_MAX_DELAY)
  1720. {
  1721. if(((HAL_GetTick()-tickstart) > Timeout) || (Timeout == 0U))
  1722. {
  1723. /* Set State to Ready to be able to restart later on */
  1724. hhash->State = HAL_HASH_STATE_READY;
  1725. /* Store time out issue in handle status */
  1726. hhash->Status = HAL_TIMEOUT;
  1727. /* Process Unlocked */
  1728. __HAL_UNLOCK(hhash);
  1729. return HAL_TIMEOUT;
  1730. }
  1731. }
  1732. }
  1733. }
  1734. else
  1735. {
  1736. while(__HAL_HASH_GET_FLAG(Flag) != RESET)
  1737. {
  1738. /* Check for the Timeout */
  1739. if(Timeout != HAL_MAX_DELAY)
  1740. {
  1741. if(((HAL_GetTick()-tickstart) > Timeout) || (Timeout == 0U))
  1742. {
  1743. /* Set State to Ready to be able to restart later on */
  1744. hhash->State = HAL_HASH_STATE_READY;
  1745. /* Store time out issue in handle status */
  1746. hhash->Status = HAL_TIMEOUT;
  1747. /* Process Unlocked */
  1748. __HAL_UNLOCK(hhash);
  1749. return HAL_TIMEOUT;
  1750. }
  1751. }
  1752. }
  1753. }
  1754. return HAL_OK;
  1755. }
  1756. /**
  1757. * @brief HASH processing in interruption mode.
  1758. * @param hhash HASH handle.
  1759. * @note HASH_IT() regularly reads hhash->SuspendRequest to check whether
  1760. * or not the HASH processing must be suspended. If this is the case, the
  1761. * processing is suspended when possible and the Peripheral feeding point reached at
  1762. * suspension time is stored in the handle for resumption later on.
  1763. * @retval HAL status
  1764. */
  1765. static HAL_StatusTypeDef HASH_IT(HASH_HandleTypeDef *hhash)
  1766. {
  1767. if (hhash->State == HAL_HASH_STATE_BUSY)
  1768. {
  1769. /* ITCounter must not be equal to 0 at this point. Report an error if this is the case. */
  1770. if(hhash->HashITCounter == 0U)
  1771. {
  1772. /* Disable Interrupts */
  1773. __HAL_HASH_DISABLE_IT(HASH_IT_DINI|HASH_IT_DCI);
  1774. /* HASH state set back to Ready to prevent any issue in user code
  1775. present in HAL_HASH_ErrorCallback() */
  1776. hhash->State = HAL_HASH_STATE_READY;
  1777. return HAL_ERROR;
  1778. }
  1779. else if (hhash->HashITCounter == 1U)
  1780. {
  1781. /* This is the first call to HASH_IT, the first input data are about to be
  1782. entered in the Peripheral. A specific processing is carried out at this point to
  1783. start-up the processing. */
  1784. hhash->HashITCounter = 2U;
  1785. }
  1786. else
  1787. {
  1788. /* Cruise speed reached, HashITCounter remains equal to 3 until the end of
  1789. the HASH processing or the end of the current step for HMAC processing. */
  1790. hhash->HashITCounter = 3U;
  1791. }
  1792. /* If digest is ready */
  1793. if (__HAL_HASH_GET_FLAG(HASH_FLAG_DCIS))
  1794. {
  1795. /* Read the digest */
  1796. HASH_GetDigest(hhash->pHashOutBuffPtr, HASH_DIGEST_LENGTH());
  1797. /* Disable Interrupts */
  1798. __HAL_HASH_DISABLE_IT(HASH_IT_DINI|HASH_IT_DCI);
  1799. /* Change the HASH state */
  1800. hhash->State = HAL_HASH_STATE_READY;
  1801. /* Reset HASH state machine */
  1802. hhash->Phase = HAL_HASH_PHASE_READY;
  1803. /* Call digest computation complete call back */
  1804. #if (USE_HAL_HASH_REGISTER_CALLBACKS == 1)
  1805. hhash->DgstCpltCallback(hhash);
  1806. #else
  1807. HAL_HASH_DgstCpltCallback(hhash);
  1808. #endif /* USE_HAL_HASH_REGISTER_CALLBACKS */
  1809. return HAL_OK;
  1810. }
  1811. /* If Peripheral ready to accept new data */
  1812. if (__HAL_HASH_GET_FLAG(HASH_FLAG_DINIS))
  1813. {
  1814. /* If the suspension flag has been raised and if the processing is not about
  1815. to end, suspend processing */
  1816. if ( (hhash->HashInCount != 0U) && (hhash->SuspendRequest == HAL_HASH_SUSPEND))
  1817. {
  1818. /* Disable Interrupts */
  1819. __HAL_HASH_DISABLE_IT(HASH_IT_DINI|HASH_IT_DCI);
  1820. /* Reset SuspendRequest */
  1821. hhash->SuspendRequest = HAL_HASH_SUSPEND_NONE;
  1822. /* Change the HASH state */
  1823. hhash->State = HAL_HASH_STATE_SUSPENDED;
  1824. return HAL_OK;
  1825. }
  1826. /* Enter input data in the Peripheral thru HASH_Write_Block_Data() call and
  1827. check whether the digest calculation has been triggered */
  1828. if (HASH_Write_Block_Data(hhash) == HASH_DIGEST_CALCULATION_STARTED)
  1829. {
  1830. /* Call Input data transfer complete call back
  1831. (called at the end of each step for HMAC) */
  1832. #if (USE_HAL_HASH_REGISTER_CALLBACKS == 1)
  1833. hhash->InCpltCallback(hhash);
  1834. #else
  1835. HAL_HASH_InCpltCallback(hhash);
  1836. #endif /* USE_HAL_HASH_REGISTER_CALLBACKS */
  1837. if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_1)
  1838. {
  1839. /* Wait until Peripheral is not busy anymore */
  1840. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_BUSY, SET, HASH_TIMEOUTVALUE) != HAL_OK)
  1841. {
  1842. /* Disable Interrupts */
  1843. __HAL_HASH_DISABLE_IT(HASH_IT_DINI|HASH_IT_DCI);
  1844. return HAL_TIMEOUT;
  1845. }
  1846. /* Initialization start for HMAC STEP 2 */
  1847. hhash->Phase = HAL_HASH_PHASE_HMAC_STEP_2; /* Move phase from Step 1 to Step 2 */
  1848. __HAL_HASH_SET_NBVALIDBITS(hhash->HashBuffSize); /* Set NBLW for the input message */
  1849. hhash->HashInCount = hhash->HashBuffSize; /* Set the input data size (in bytes) */
  1850. hhash->pHashInBuffPtr = hhash->pHashMsgBuffPtr; /* Set the input data address */
  1851. hhash->HashITCounter = 1; /* Set ITCounter to 1 to indicate the start of a new phase */
  1852. __HAL_HASH_ENABLE_IT(HASH_IT_DINI); /* Enable IT (was disabled in HASH_Write_Block_Data) */
  1853. }
  1854. else if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_2)
  1855. {
  1856. /* Wait until Peripheral is not busy anymore */
  1857. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_BUSY, SET, HASH_TIMEOUTVALUE) != HAL_OK)
  1858. {
  1859. /* Disable Interrupts */
  1860. __HAL_HASH_DISABLE_IT(HASH_IT_DINI|HASH_IT_DCI);
  1861. return HAL_TIMEOUT;
  1862. }
  1863. /* Initialization start for HMAC STEP 3 */
  1864. hhash->Phase = HAL_HASH_PHASE_HMAC_STEP_3; /* Move phase from Step 2 to Step 3 */
  1865. __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize); /* Set NBLW for the key */
  1866. hhash->HashInCount = hhash->Init.KeySize; /* Set the key size (in bytes) */
  1867. hhash->pHashInBuffPtr = hhash->Init.pKey; /* Set the key address */
  1868. hhash->HashITCounter = 1; /* Set ITCounter to 1 to indicate the start of a new phase */
  1869. __HAL_HASH_ENABLE_IT(HASH_IT_DINI); /* Enable IT (was disabled in HASH_Write_Block_Data) */
  1870. }
  1871. else
  1872. {
  1873. /* Nothing to do */
  1874. }
  1875. } /* if (HASH_Write_Block_Data(hhash) == HASH_DIGEST_CALCULATION_STARTED) */
  1876. } /* if (__HAL_HASH_GET_FLAG(HASH_FLAG_DINIS))*/
  1877. /* Return function status */
  1878. return HAL_OK;
  1879. }
  1880. else
  1881. {
  1882. return HAL_BUSY;
  1883. }
  1884. }
  1885. /**
  1886. * @brief Write a block of data in HASH Peripheral in interruption mode.
  1887. * @param hhash HASH handle.
  1888. * @note HASH_Write_Block_Data() is called under interruption by HASH_IT().
  1889. * @retval HAL status
  1890. */
  1891. static uint32_t HASH_Write_Block_Data(HASH_HandleTypeDef *hhash)
  1892. {
  1893. uint32_t inputaddr;
  1894. uint32_t buffercounter;
  1895. uint32_t inputcounter;
  1896. uint32_t ret = HASH_DIGEST_CALCULATION_NOT_STARTED;
  1897. /* If there are more than 64 bytes remaining to be entered */
  1898. if(hhash->HashInCount > 64U)
  1899. {
  1900. inputaddr = (uint32_t)hhash->pHashInBuffPtr;
  1901. /* Write the Input block in the Data IN register
  1902. (16 32-bit words, or 64 bytes are entered) */
  1903. for(buffercounter = 0U; buffercounter < 64U; buffercounter+=4U)
  1904. {
  1905. HASH->DIN = *(uint32_t*)inputaddr;
  1906. inputaddr+=4U;
  1907. }
  1908. /* If this is the start of input data entering, an additional word
  1909. must be entered to start up the HASH processing */
  1910. if(hhash->HashITCounter == 2U)
  1911. {
  1912. HASH->DIN = *(uint32_t*)inputaddr;
  1913. if(hhash->HashInCount >= 68U)
  1914. {
  1915. /* There are still data waiting to be entered in the Peripheral.
  1916. Decrement buffer counter and set pointer to the proper
  1917. memory location for the next data entering round. */
  1918. hhash->HashInCount -= 68U;
  1919. hhash->pHashInBuffPtr+= 68U;
  1920. }
  1921. else
  1922. {
  1923. /* All the input buffer has been fed to the HW. */
  1924. hhash->HashInCount = 0U;
  1925. }
  1926. }
  1927. else
  1928. {
  1929. /* 64 bytes have been entered and there are still some remaining:
  1930. Decrement buffer counter and set pointer to the proper
  1931. memory location for the next data entering round.*/
  1932. hhash->HashInCount -= 64U;
  1933. hhash->pHashInBuffPtr+= 64U;
  1934. }
  1935. }
  1936. else
  1937. {
  1938. /* 64 or less bytes remain to be entered. This is the last
  1939. data entering round. */
  1940. /* Get the buffer address */
  1941. inputaddr = (uint32_t)hhash->pHashInBuffPtr;
  1942. /* Get the buffer counter */
  1943. inputcounter = hhash->HashInCount;
  1944. /* Disable Interrupts */
  1945. __HAL_HASH_DISABLE_IT(HASH_IT_DINI);
  1946. /* Write the Input block in the Data IN register */
  1947. for(buffercounter = 0U; buffercounter < ((inputcounter+3U)/4U); buffercounter++)
  1948. {
  1949. HASH->DIN = *(uint32_t*)inputaddr;
  1950. inputaddr+=4U;
  1951. }
  1952. if (hhash->Accumulation == 1U)
  1953. {
  1954. /* Field accumulation is set, API only feeds data to the Peripheral and under interruption.
  1955. The digest computation will be started when the last buffer data are entered. */
  1956. /* Reset multi buffers accumulation flag */
  1957. hhash->Accumulation = 0U;
  1958. /* Change the HASH state */
  1959. hhash->State = HAL_HASH_STATE_READY;
  1960. /* Call Input data transfer complete call back */
  1961. #if (USE_HAL_HASH_REGISTER_CALLBACKS == 1)
  1962. hhash->InCpltCallback(hhash);
  1963. #else
  1964. HAL_HASH_InCpltCallback(hhash);
  1965. #endif /* USE_HAL_HASH_REGISTER_CALLBACKS */
  1966. }
  1967. else
  1968. {
  1969. /* Start the Digest calculation */
  1970. __HAL_HASH_START_DIGEST();
  1971. /* Return indication that digest calculation has started:
  1972. this return value triggers the call to Input data transfer
  1973. complete call back as well as the proper transition from
  1974. one step to another in HMAC mode. */
  1975. ret = HASH_DIGEST_CALCULATION_STARTED;
  1976. }
  1977. /* Reset buffer counter */
  1978. hhash->HashInCount = 0;
  1979. }
  1980. /* Return whether or digest calculation has started */
  1981. return ret;
  1982. }
  1983. /**
  1984. * @brief HMAC processing in polling mode.
  1985. * @param hhash HASH handle.
  1986. * @param Timeout Timeout value.
  1987. * @retval HAL status
  1988. */
  1989. static HAL_StatusTypeDef HMAC_Processing(HASH_HandleTypeDef *hhash, uint32_t Timeout)
  1990. {
  1991. /* Ensure first that Phase is correct */
  1992. if ((hhash->Phase != HAL_HASH_PHASE_HMAC_STEP_1) && (hhash->Phase != HAL_HASH_PHASE_HMAC_STEP_2) && (hhash->Phase != HAL_HASH_PHASE_HMAC_STEP_3))
  1993. {
  1994. /* Change the HASH state */
  1995. hhash->State = HAL_HASH_STATE_READY;
  1996. /* Process Unlock */
  1997. __HAL_UNLOCK(hhash);
  1998. /* Return function status */
  1999. return HAL_ERROR;
  2000. }
  2001. /* HMAC Step 1 processing */
  2002. if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_1)
  2003. {
  2004. /************************** STEP 1 ******************************************/
  2005. /* Configure the Number of valid bits in last word of the message */
  2006. __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize);
  2007. /* Write input buffer in Data register */
  2008. hhash->Status = HASH_WriteData(hhash, hhash->pHashKeyBuffPtr, hhash->HashKeyCount);
  2009. if (hhash->Status != HAL_OK)
  2010. {
  2011. return hhash->Status;
  2012. }
  2013. /* Check whether or not key entering process has been suspended */
  2014. if (hhash->State == HAL_HASH_STATE_SUSPENDED)
  2015. {
  2016. /* Process Unlocked */
  2017. __HAL_UNLOCK(hhash);
  2018. /* Stop right there and return function status */
  2019. return HAL_OK;
  2020. }
  2021. /* No processing suspension at this point: set DCAL bit. */
  2022. __HAL_HASH_START_DIGEST();
  2023. /* Wait for BUSY flag to be cleared */
  2024. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_BUSY, SET, Timeout) != HAL_OK)
  2025. {
  2026. return HAL_TIMEOUT;
  2027. }
  2028. /* Move from Step 1 to Step 2 */
  2029. hhash->Phase = HAL_HASH_PHASE_HMAC_STEP_2;
  2030. }
  2031. /* HMAC Step 2 processing.
  2032. After phase check, HMAC_Processing() may
  2033. - directly start up from this point in resumption case
  2034. if the same Step 2 processing was suspended previously
  2035. - or fall through from the Step 1 processing carried out hereabove */
  2036. if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_2)
  2037. {
  2038. /************************** STEP 2 ******************************************/
  2039. /* Configure the Number of valid bits in last word of the message */
  2040. __HAL_HASH_SET_NBVALIDBITS(hhash->HashBuffSize);
  2041. /* Write input buffer in Data register */
  2042. hhash->Status = HASH_WriteData(hhash, hhash->pHashInBuffPtr, hhash->HashInCount);
  2043. if (hhash->Status != HAL_OK)
  2044. {
  2045. return hhash->Status;
  2046. }
  2047. /* Check whether or not data entering process has been suspended */
  2048. if (hhash->State == HAL_HASH_STATE_SUSPENDED)
  2049. {
  2050. /* Process Unlocked */
  2051. __HAL_UNLOCK(hhash);
  2052. /* Stop right there and return function status */
  2053. return HAL_OK;
  2054. }
  2055. /* No processing suspension at this point: set DCAL bit. */
  2056. __HAL_HASH_START_DIGEST();
  2057. /* Wait for BUSY flag to be cleared */
  2058. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_BUSY, SET, Timeout) != HAL_OK)
  2059. {
  2060. return HAL_TIMEOUT;
  2061. }
  2062. /* Move from Step 2 to Step 3 */
  2063. hhash->Phase = HAL_HASH_PHASE_HMAC_STEP_3;
  2064. /* In case Step 1 phase was suspended then resumed,
  2065. set again Key input buffers and size before moving to
  2066. next step */
  2067. hhash->pHashKeyBuffPtr = hhash->Init.pKey;
  2068. hhash->HashKeyCount = hhash->Init.KeySize;
  2069. }
  2070. /* HMAC Step 3 processing.
  2071. After phase check, HMAC_Processing() may
  2072. - directly start up from this point in resumption case
  2073. if the same Step 3 processing was suspended previously
  2074. - or fall through from the Step 2 processing carried out hereabove */
  2075. if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_3)
  2076. {
  2077. /************************** STEP 3 ******************************************/
  2078. /* Configure the Number of valid bits in last word of the message */
  2079. __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize);
  2080. /* Write input buffer in Data register */
  2081. hhash->Status = HASH_WriteData(hhash, hhash->pHashKeyBuffPtr, hhash->HashKeyCount);
  2082. if (hhash->Status != HAL_OK)
  2083. {
  2084. return hhash->Status;
  2085. }
  2086. /* Check whether or not key entering process has been suspended */
  2087. if (hhash->State == HAL_HASH_STATE_SUSPENDED)
  2088. {
  2089. /* Process Unlocked */
  2090. __HAL_UNLOCK(hhash);
  2091. /* Stop right there and return function status */
  2092. return HAL_OK;
  2093. }
  2094. /* No processing suspension at this point: start the Digest calculation. */
  2095. __HAL_HASH_START_DIGEST();
  2096. /* Wait for DCIS flag to be set */
  2097. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_DCIS, RESET, Timeout) != HAL_OK)
  2098. {
  2099. return HAL_TIMEOUT;
  2100. }
  2101. /* Read the message digest */
  2102. HASH_GetDigest(hhash->pHashOutBuffPtr, HASH_DIGEST_LENGTH());
  2103. /* Reset HASH state machine */
  2104. hhash->Phase = HAL_HASH_PHASE_READY;
  2105. }
  2106. /* Change the HASH state */
  2107. hhash->State = HAL_HASH_STATE_READY;
  2108. /* Process Unlock */
  2109. __HAL_UNLOCK(hhash);
  2110. /* Return function status */
  2111. return HAL_OK;
  2112. }
  2113. /**
  2114. * @brief Initialize the HASH peripheral, next process pInBuffer then
  2115. * read the computed digest.
  2116. * @note Digest is available in pOutBuffer.
  2117. * @param hhash HASH handle.
  2118. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  2119. * @param Size length of the input buffer in bytes.
  2120. * @param pOutBuffer pointer to the computed digest.
  2121. * @param Timeout Timeout value.
  2122. * @param Algorithm HASH algorithm.
  2123. * @retval HAL status
  2124. */
  2125. HAL_StatusTypeDef HASH_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout, uint32_t Algorithm)
  2126. {
  2127. uint8_t *pInBuffer_tmp; /* input data address, input parameter of HASH_WriteData() */
  2128. uint32_t Size_tmp; /* input data size (in bytes), input parameter of HASH_WriteData() */
  2129. HAL_HASH_StateTypeDef State_tmp = hhash->State;
  2130. /* Initiate HASH processing in case of start or resumption */
  2131. if((State_tmp == HAL_HASH_STATE_READY) || (State_tmp == HAL_HASH_STATE_SUSPENDED))
  2132. {
  2133. /* Check input parameters */
  2134. if ((pInBuffer == NULL) || (pOutBuffer == NULL))
  2135. {
  2136. hhash->State = HAL_HASH_STATE_READY;
  2137. return HAL_ERROR;
  2138. }
  2139. /* Process Locked */
  2140. __HAL_LOCK(hhash);
  2141. /* Check if initialization phase has not been already performed */
  2142. if(hhash->Phase == HAL_HASH_PHASE_READY)
  2143. {
  2144. /* Change the HASH state */
  2145. hhash->State = HAL_HASH_STATE_BUSY;
  2146. /* Select the HASH algorithm, clear HMAC mode and long key selection bit, reset the HASH processor core */
  2147. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_CR_INIT);
  2148. /* Configure the number of valid bits in last word of the message */
  2149. __HAL_HASH_SET_NBVALIDBITS(Size);
  2150. /* pInBuffer_tmp and Size_tmp are initialized to be used afterwards as
  2151. input parameters of HASH_WriteData() */
  2152. pInBuffer_tmp = pInBuffer; /* pInBuffer_tmp is set to the input data address */
  2153. Size_tmp = Size; /* Size_tmp contains the input data size in bytes */
  2154. /* Set the phase */
  2155. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  2156. }
  2157. else if (hhash->Phase == HAL_HASH_PHASE_PROCESS)
  2158. {
  2159. /* if the Peripheral has already been initialized, two cases are possible */
  2160. /* Process resumption time ... */
  2161. if (hhash->State == HAL_HASH_STATE_SUSPENDED)
  2162. {
  2163. /* Since this is resumption, pInBuffer_tmp and Size_tmp are not set
  2164. to the API input parameters but to those saved beforehand by HASH_WriteData()
  2165. when the processing was suspended */
  2166. pInBuffer_tmp = hhash->pHashInBuffPtr;
  2167. Size_tmp = hhash->HashInCount;
  2168. }
  2169. /* ... or multi-buffer HASH processing end */
  2170. else
  2171. {
  2172. /* pInBuffer_tmp and Size_tmp are initialized to be used afterwards as
  2173. input parameters of HASH_WriteData() */
  2174. pInBuffer_tmp = pInBuffer;
  2175. Size_tmp = Size;
  2176. /* Configure the number of valid bits in last word of the message */
  2177. __HAL_HASH_SET_NBVALIDBITS(Size);
  2178. }
  2179. /* Change the HASH state */
  2180. hhash->State = HAL_HASH_STATE_BUSY;
  2181. }
  2182. else
  2183. {
  2184. /* Phase error */
  2185. hhash->State = HAL_HASH_STATE_READY;
  2186. /* Process Unlocked */
  2187. __HAL_UNLOCK(hhash);
  2188. /* Return function status */
  2189. return HAL_ERROR;
  2190. }
  2191. /* Write input buffer in Data register */
  2192. hhash->Status = HASH_WriteData(hhash, pInBuffer_tmp, Size_tmp);
  2193. if (hhash->Status != HAL_OK)
  2194. {
  2195. return hhash->Status;
  2196. }
  2197. /* If the process has not been suspended, carry on to digest calculation */
  2198. if (hhash->State != HAL_HASH_STATE_SUSPENDED)
  2199. {
  2200. /* Start the Digest calculation */
  2201. __HAL_HASH_START_DIGEST();
  2202. /* Wait for DCIS flag to be set */
  2203. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_DCIS, RESET, Timeout) != HAL_OK)
  2204. {
  2205. return HAL_TIMEOUT;
  2206. }
  2207. /* Read the message digest */
  2208. HASH_GetDigest(pOutBuffer, HASH_DIGEST_LENGTH());
  2209. /* Change the HASH state */
  2210. hhash->State = HAL_HASH_STATE_READY;
  2211. /* Reset HASH state machine */
  2212. hhash->Phase = HAL_HASH_PHASE_READY;
  2213. }
  2214. /* Process Unlocked */
  2215. __HAL_UNLOCK(hhash);
  2216. /* Return function status */
  2217. return HAL_OK;
  2218. }
  2219. else
  2220. {
  2221. return HAL_BUSY;
  2222. }
  2223. }
  2224. /**
  2225. * @brief If not already done, initialize the HASH peripheral then
  2226. * processes pInBuffer.
  2227. * @note Field hhash->Phase of HASH handle is tested to check whether or not
  2228. * the Peripheral has already been initialized.
  2229. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  2230. * HASH digest computation is corrupted.
  2231. * @param hhash HASH handle.
  2232. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  2233. * @param Size length of the input buffer in bytes, must be a multiple of 4.
  2234. * @param Algorithm HASH algorithm.
  2235. * @retval HAL status
  2236. */
  2237. HAL_StatusTypeDef HASH_Accumulate(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint32_t Algorithm)
  2238. {
  2239. uint8_t *pInBuffer_tmp; /* input data address, input parameter of HASH_WriteData() */
  2240. uint32_t Size_tmp; /* input data size (in bytes), input parameter of HASH_WriteData() */
  2241. HAL_HASH_StateTypeDef State_tmp = hhash->State;
  2242. /* Make sure the input buffer size (in bytes) is a multiple of 4 */
  2243. if ((Size % 4U) != 0U)
  2244. {
  2245. return HAL_ERROR;
  2246. }
  2247. /* Initiate HASH processing in case of start or resumption */
  2248. if((State_tmp == HAL_HASH_STATE_READY) || (State_tmp == HAL_HASH_STATE_SUSPENDED))
  2249. {
  2250. /* Check input parameters */
  2251. if ((pInBuffer == NULL) || (Size == 0U))
  2252. {
  2253. hhash->State = HAL_HASH_STATE_READY;
  2254. return HAL_ERROR;
  2255. }
  2256. /* Process Locked */
  2257. __HAL_LOCK(hhash);
  2258. /* If resuming the HASH processing */
  2259. if (hhash->State == HAL_HASH_STATE_SUSPENDED)
  2260. {
  2261. /* Change the HASH state */
  2262. hhash->State = HAL_HASH_STATE_BUSY;
  2263. /* Since this is resumption, pInBuffer_tmp and Size_tmp are not set
  2264. to the API input parameters but to those saved beforehand by HASH_WriteData()
  2265. when the processing was suspended */
  2266. pInBuffer_tmp = hhash->pHashInBuffPtr; /* pInBuffer_tmp is set to the input data address */
  2267. Size_tmp = hhash->HashInCount; /* Size_tmp contains the input data size in bytes */
  2268. }
  2269. else
  2270. {
  2271. /* Change the HASH state */
  2272. hhash->State = HAL_HASH_STATE_BUSY;
  2273. /* pInBuffer_tmp and Size_tmp are initialized to be used afterwards as
  2274. input parameters of HASH_WriteData() */
  2275. pInBuffer_tmp = pInBuffer; /* pInBuffer_tmp is set to the input data address */
  2276. Size_tmp = Size; /* Size_tmp contains the input data size in bytes */
  2277. /* Check if initialization phase has already be performed */
  2278. if(hhash->Phase == HAL_HASH_PHASE_READY)
  2279. {
  2280. /* Select the HASH algorithm, clear HMAC mode and long key selection bit, reset the HASH processor core */
  2281. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_CR_INIT);
  2282. }
  2283. /* Set the phase */
  2284. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  2285. }
  2286. /* Write input buffer in Data register */
  2287. hhash->Status = HASH_WriteData(hhash, pInBuffer_tmp, Size_tmp);
  2288. if (hhash->Status != HAL_OK)
  2289. {
  2290. return hhash->Status;
  2291. }
  2292. /* If the process has not been suspended, move the state to Ready */
  2293. if (hhash->State != HAL_HASH_STATE_SUSPENDED)
  2294. {
  2295. /* Change the HASH state */
  2296. hhash->State = HAL_HASH_STATE_READY;
  2297. }
  2298. /* Process Unlocked */
  2299. __HAL_UNLOCK(hhash);
  2300. /* Return function status */
  2301. return HAL_OK;
  2302. }
  2303. else
  2304. {
  2305. return HAL_BUSY;
  2306. }
  2307. }
  2308. /**
  2309. * @brief If not already done, initialize the HASH peripheral then
  2310. * processes pInBuffer in interruption mode.
  2311. * @note Field hhash->Phase of HASH handle is tested to check whether or not
  2312. * the Peripheral has already been initialized.
  2313. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  2314. * HASH digest computation is corrupted.
  2315. * @param hhash HASH handle.
  2316. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  2317. * @param Size length of the input buffer in bytes, must be a multiple of 4.
  2318. * @param Algorithm HASH algorithm.
  2319. * @retval HAL status
  2320. */
  2321. HAL_StatusTypeDef HASH_Accumulate_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint32_t Algorithm)
  2322. {
  2323. HAL_HASH_StateTypeDef State_tmp = hhash->State;
  2324. __IO uint32_t inputaddr = (uint32_t) pInBuffer;
  2325. uint32_t SizeVar = Size;
  2326. /* Make sure the input buffer size (in bytes) is a multiple of 4 */
  2327. if ((Size % 4U) != 0U)
  2328. {
  2329. return HAL_ERROR;
  2330. }
  2331. /* Initiate HASH processing in case of start or resumption */
  2332. if((State_tmp == HAL_HASH_STATE_READY) || (State_tmp == HAL_HASH_STATE_SUSPENDED))
  2333. {
  2334. /* Check input parameters */
  2335. if ((pInBuffer == NULL) || (Size == 0U))
  2336. {
  2337. hhash->State = HAL_HASH_STATE_READY;
  2338. return HAL_ERROR;
  2339. }
  2340. /* Process Locked */
  2341. __HAL_LOCK(hhash);
  2342. /* If resuming the HASH processing */
  2343. if (hhash->State == HAL_HASH_STATE_SUSPENDED)
  2344. {
  2345. /* Change the HASH state */
  2346. hhash->State = HAL_HASH_STATE_BUSY;
  2347. }
  2348. else
  2349. {
  2350. /* Change the HASH state */
  2351. hhash->State = HAL_HASH_STATE_BUSY;
  2352. /* Check if initialization phase has already be performed */
  2353. if(hhash->Phase == HAL_HASH_PHASE_READY)
  2354. {
  2355. /* Select the HASH algorithm, clear HMAC mode and long key selection bit, reset the HASH processor core */
  2356. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_CR_INIT);
  2357. hhash->HashITCounter = 1;
  2358. }
  2359. else
  2360. {
  2361. hhash->HashITCounter = 3; /* 'cruise-speed' reached during a previous buffer processing */
  2362. }
  2363. /* Set the phase */
  2364. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  2365. /* If DINIS is equal to 0 (for example if an incomplete block has been previously
  2366. fed to the Peripheral), the DINIE interruption won't be triggered when DINIE is set.
  2367. Therefore, first words are manually entered until DINIS raises, or until there
  2368. is not more data to enter. */
  2369. while((!(__HAL_HASH_GET_FLAG(HASH_FLAG_DINIS))) && (SizeVar > 0U))
  2370. {
  2371. /* Write input data 4 bytes at a time */
  2372. HASH->DIN = *(uint32_t*)inputaddr;
  2373. inputaddr+=4U;
  2374. SizeVar-=4U;
  2375. }
  2376. /* If DINIS is still not set or if all the data have been fed, stop here */
  2377. if ((!(__HAL_HASH_GET_FLAG(HASH_FLAG_DINIS))) || (SizeVar == 0U))
  2378. {
  2379. /* Change the HASH state */
  2380. hhash->State = HAL_HASH_STATE_READY;
  2381. /* Process Unlock */
  2382. __HAL_UNLOCK(hhash);
  2383. /* Return function status */
  2384. return HAL_OK;
  2385. }
  2386. /* otherwise, carry on in interrupt-mode */
  2387. hhash->HashInCount = SizeVar; /* Counter used to keep track of number of data
  2388. to be fed to the Peripheral */
  2389. hhash->pHashInBuffPtr = (uint8_t *)inputaddr; /* Points at data which will be fed to the Peripheral at
  2390. the next interruption */
  2391. /* In case of suspension, hhash->HashInCount and hhash->pHashInBuffPtr contain
  2392. the information describing where the HASH process is stopped.
  2393. These variables are used later on to resume the HASH processing at the
  2394. correct location. */
  2395. }
  2396. /* Set multi buffers accumulation flag */
  2397. hhash->Accumulation = 1U;
  2398. /* Process Unlock */
  2399. __HAL_UNLOCK(hhash);
  2400. /* Enable Data Input interrupt */
  2401. __HAL_HASH_ENABLE_IT(HASH_IT_DINI);
  2402. /* Return function status */
  2403. return HAL_OK;
  2404. }
  2405. else
  2406. {
  2407. return HAL_BUSY;
  2408. }
  2409. }
  2410. /**
  2411. * @brief Initialize the HASH peripheral, next process pInBuffer then
  2412. * read the computed digest in interruption mode.
  2413. * @note Digest is available in pOutBuffer.
  2414. * @param hhash HASH handle.
  2415. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  2416. * @param Size length of the input buffer in bytes.
  2417. * @param pOutBuffer pointer to the computed digest.
  2418. * @param Algorithm HASH algorithm.
  2419. * @retval HAL status
  2420. */
  2421. HAL_StatusTypeDef HASH_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Algorithm)
  2422. {
  2423. HAL_HASH_StateTypeDef State_tmp = hhash->State;
  2424. __IO uint32_t inputaddr = (uint32_t) pInBuffer;
  2425. uint32_t polling_step = 0U;
  2426. uint32_t initialization_skipped = 0U;
  2427. uint32_t SizeVar = Size;
  2428. /* If State is ready or suspended, start or resume IT-based HASH processing */
  2429. if((State_tmp == HAL_HASH_STATE_READY) || (State_tmp == HAL_HASH_STATE_SUSPENDED))
  2430. {
  2431. /* Check input parameters */
  2432. if ((pInBuffer == NULL) || (Size == 0U) || (pOutBuffer == NULL))
  2433. {
  2434. hhash->State = HAL_HASH_STATE_READY;
  2435. return HAL_ERROR;
  2436. }
  2437. /* Process Locked */
  2438. __HAL_LOCK(hhash);
  2439. /* Change the HASH state */
  2440. hhash->State = HAL_HASH_STATE_BUSY;
  2441. /* Initialize IT counter */
  2442. hhash->HashITCounter = 1;
  2443. /* Check if initialization phase has already be performed */
  2444. if(hhash->Phase == HAL_HASH_PHASE_READY)
  2445. {
  2446. /* Select the HASH algorithm, clear HMAC mode and long key selection bit, reset the HASH processor core */
  2447. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_CR_INIT);
  2448. /* Configure the number of valid bits in last word of the message */
  2449. __HAL_HASH_SET_NBVALIDBITS(SizeVar);
  2450. hhash->HashInCount = SizeVar; /* Counter used to keep track of number of data
  2451. to be fed to the Peripheral */
  2452. hhash->pHashInBuffPtr = pInBuffer; /* Points at data which will be fed to the Peripheral at
  2453. the next interruption */
  2454. /* In case of suspension, hhash->HashInCount and hhash->pHashInBuffPtr contain
  2455. the information describing where the HASH process is stopped.
  2456. These variables are used later on to resume the HASH processing at the
  2457. correct location. */
  2458. hhash->pHashOutBuffPtr = pOutBuffer; /* Points at the computed digest */
  2459. }
  2460. else
  2461. {
  2462. initialization_skipped = 1; /* info user later on in case of multi-buffer */
  2463. }
  2464. /* Set the phase */
  2465. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  2466. /* If DINIS is equal to 0 (for example if an incomplete block has been previously
  2467. fed to the Peripheral), the DINIE interruption won't be triggered when DINIE is set.
  2468. Therefore, first words are manually entered until DINIS raises. */
  2469. while((!(__HAL_HASH_GET_FLAG(HASH_FLAG_DINIS))) && (SizeVar > 3U))
  2470. {
  2471. polling_step = 1U; /* note that some words are entered before enabling the interrupt */
  2472. /* Write input data 4 bytes at a time */
  2473. HASH->DIN = *(uint32_t*)inputaddr;
  2474. inputaddr+=4U;
  2475. SizeVar-=4U;
  2476. }
  2477. if (polling_step == 1U)
  2478. {
  2479. if (SizeVar == 0U)
  2480. {
  2481. /* If all the data have been entered at this point, it only remains to
  2482. read the digest */
  2483. hhash->pHashOutBuffPtr = pOutBuffer; /* Points at the computed digest */
  2484. /* Start the Digest calculation */
  2485. __HAL_HASH_START_DIGEST();
  2486. /* Process Unlock */
  2487. __HAL_UNLOCK(hhash);
  2488. /* Enable Interrupts */
  2489. __HAL_HASH_ENABLE_IT(HASH_IT_DCI);
  2490. /* Return function status */
  2491. return HAL_OK;
  2492. }
  2493. else if (__HAL_HASH_GET_FLAG(HASH_FLAG_DINIS))
  2494. {
  2495. /* It remains data to enter and the Peripheral is ready to trigger DINIE,
  2496. carry on as usual.
  2497. Update HashInCount and pHashInBuffPtr accordingly. */
  2498. hhash->HashInCount = SizeVar;
  2499. hhash->pHashInBuffPtr = (uint8_t *)inputaddr;
  2500. __HAL_HASH_SET_NBVALIDBITS(SizeVar); /* Update the configuration of the number of valid bits in last word of the message */
  2501. hhash->pHashOutBuffPtr = pOutBuffer; /* Points at the computed digest */
  2502. if (initialization_skipped == 1U)
  2503. {
  2504. hhash->HashITCounter = 3; /* 'cruise-speed' reached during a previous buffer processing */
  2505. }
  2506. }
  2507. else
  2508. {
  2509. /* DINIS is not set but it remains a few data to enter (not enough for a full word).
  2510. Manually enter the last bytes before enabling DCIE. */
  2511. __HAL_HASH_SET_NBVALIDBITS(SizeVar);
  2512. HASH->DIN = *(uint32_t*)inputaddr;
  2513. /* Start the Digest calculation */
  2514. hhash->pHashOutBuffPtr = pOutBuffer; /* Points at the computed digest */
  2515. __HAL_HASH_START_DIGEST();
  2516. /* Process Unlock */
  2517. __HAL_UNLOCK(hhash);
  2518. /* Enable Interrupts */
  2519. __HAL_HASH_ENABLE_IT(HASH_IT_DCI);
  2520. /* Return function status */
  2521. return HAL_OK;
  2522. }
  2523. } /* if (polling_step == 1) */
  2524. /* Process Unlock */
  2525. __HAL_UNLOCK(hhash);
  2526. /* Enable Interrupts */
  2527. __HAL_HASH_ENABLE_IT(HASH_IT_DINI|HASH_IT_DCI);
  2528. /* Return function status */
  2529. return HAL_OK;
  2530. }
  2531. else
  2532. {
  2533. return HAL_BUSY;
  2534. }
  2535. }
  2536. /**
  2537. * @brief Initialize the HASH peripheral then initiate a DMA transfer
  2538. * to feed the input buffer to the Peripheral.
  2539. * @note If MDMAT bit is set before calling this function (multi-buffer
  2540. * HASH processing case), the input buffer size (in bytes) must be
  2541. * a multiple of 4 otherwise, the HASH digest computation is corrupted.
  2542. * For the processing of the last buffer of the thread, MDMAT bit must
  2543. * be reset and the buffer length (in bytes) doesn't have to be a
  2544. * multiple of 4.
  2545. * @param hhash HASH handle.
  2546. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  2547. * @param Size length of the input buffer in bytes.
  2548. * @param Algorithm HASH algorithm.
  2549. * @retval HAL status
  2550. */
  2551. HAL_StatusTypeDef HASH_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint32_t Algorithm)
  2552. {
  2553. uint32_t inputaddr;
  2554. uint32_t inputSize;
  2555. HAL_StatusTypeDef status ;
  2556. HAL_HASH_StateTypeDef State_tmp = hhash->State;
  2557. #if defined (HASH_CR_MDMAT)
  2558. /* Make sure the input buffer size (in bytes) is a multiple of 4 when MDMAT bit is set
  2559. (case of multi-buffer HASH processing) */
  2560. assert_param(IS_HASH_DMA_MULTIBUFFER_SIZE(Size));
  2561. #endif /* MDMA defined*/
  2562. /* If State is ready or suspended, start or resume polling-based HASH processing */
  2563. if((State_tmp == HAL_HASH_STATE_READY) || (State_tmp == HAL_HASH_STATE_SUSPENDED))
  2564. {
  2565. /* Check input parameters */
  2566. if ( (pInBuffer == NULL ) || (Size == 0U) ||
  2567. /* Check phase coherency. Phase must be
  2568. either READY (fresh start)
  2569. or PROCESS (multi-buffer HASH management) */
  2570. ((hhash->Phase != HAL_HASH_PHASE_READY) && (!(IS_HASH_PROCESSING(hhash)))))
  2571. {
  2572. hhash->State = HAL_HASH_STATE_READY;
  2573. return HAL_ERROR;
  2574. }
  2575. /* Process Locked */
  2576. __HAL_LOCK(hhash);
  2577. /* If not a resumption case */
  2578. if (hhash->State == HAL_HASH_STATE_READY)
  2579. {
  2580. /* Change the HASH state */
  2581. hhash->State = HAL_HASH_STATE_BUSY;
  2582. /* Check if initialization phase has already been performed.
  2583. If Phase is already set to HAL_HASH_PHASE_PROCESS, this means the
  2584. API is processing a new input data message in case of multi-buffer HASH
  2585. computation. */
  2586. if(hhash->Phase == HAL_HASH_PHASE_READY)
  2587. {
  2588. /* Select the HASH algorithm, clear HMAC mode and long key selection bit, reset the HASH processor core */
  2589. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_CR_INIT);
  2590. /* Set the phase */
  2591. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  2592. }
  2593. /* Configure the Number of valid bits in last word of the message */
  2594. __HAL_HASH_SET_NBVALIDBITS(Size);
  2595. inputaddr = (uint32_t)pInBuffer; /* DMA transfer start address */
  2596. inputSize = Size; /* DMA transfer size (in bytes) */
  2597. /* In case of suspension request, save the starting parameters */
  2598. hhash->pHashInBuffPtr = pInBuffer; /* DMA transfer start address */
  2599. hhash->HashInCount = Size; /* DMA transfer size (in bytes) */
  2600. }
  2601. /* If resumption case */
  2602. else
  2603. {
  2604. /* Change the HASH state */
  2605. hhash->State = HAL_HASH_STATE_BUSY;
  2606. /* Resumption case, inputaddr and inputSize are not set to the API input parameters
  2607. but to those saved beforehand by HAL_HASH_DMAFeed_ProcessSuspend() when the
  2608. processing was suspended */
  2609. inputaddr = (uint32_t)hhash->pHashInBuffPtr; /* DMA transfer start address */
  2610. inputSize = hhash->HashInCount; /* DMA transfer size (in bytes) */
  2611. }
  2612. /* Set the HASH DMA transfert complete callback */
  2613. hhash->hdmain->XferCpltCallback = HASH_DMAXferCplt;
  2614. /* Set the DMA error callback */
  2615. hhash->hdmain->XferErrorCallback = HASH_DMAError;
  2616. /* Store number of words already pushed to manage proper DMA processing suspension */
  2617. hhash->NbWordsAlreadyPushed = HASH_NBW_PUSHED();
  2618. /* Enable the DMA In DMA Stream */
  2619. status = HAL_DMA_Start_IT(hhash->hdmain, inputaddr, (uint32_t)&HASH->DIN, (((inputSize %4U)!=0U) ? ((inputSize+(4U-(inputSize %4U)))/4U):(inputSize/4U)));
  2620. /* Enable DMA requests */
  2621. SET_BIT(HASH->CR, HASH_CR_DMAE);
  2622. /* Process Unlock */
  2623. __HAL_UNLOCK(hhash);
  2624. /* Return function status */
  2625. if (status != HAL_OK)
  2626. {
  2627. /* Update HASH state machine to error */
  2628. hhash->State = HAL_HASH_STATE_ERROR;
  2629. }
  2630. return status;
  2631. }
  2632. else
  2633. {
  2634. return HAL_BUSY;
  2635. }
  2636. }
  2637. /**
  2638. * @brief Return the computed digest.
  2639. * @note The API waits for DCIS to be set then reads the computed digest.
  2640. * @param hhash HASH handle.
  2641. * @param pOutBuffer pointer to the computed digest.
  2642. * @param Timeout Timeout value.
  2643. * @retval HAL status
  2644. */
  2645. HAL_StatusTypeDef HASH_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout)
  2646. {
  2647. if(hhash->State == HAL_HASH_STATE_READY)
  2648. {
  2649. /* Check parameter */
  2650. if (pOutBuffer == NULL)
  2651. {
  2652. return HAL_ERROR;
  2653. }
  2654. /* Process Locked */
  2655. __HAL_LOCK(hhash);
  2656. /* Change the HASH state to busy */
  2657. hhash->State = HAL_HASH_STATE_BUSY;
  2658. /* Wait for DCIS flag to be set */
  2659. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_DCIS, RESET, Timeout) != HAL_OK)
  2660. {
  2661. return HAL_TIMEOUT;
  2662. }
  2663. /* Read the message digest */
  2664. HASH_GetDigest(pOutBuffer, HASH_DIGEST_LENGTH());
  2665. /* Change the HASH state to ready */
  2666. hhash->State = HAL_HASH_STATE_READY;
  2667. /* Reset HASH state machine */
  2668. hhash->Phase = HAL_HASH_PHASE_READY;
  2669. /* Process UnLock */
  2670. __HAL_UNLOCK(hhash);
  2671. /* Return function status */
  2672. return HAL_OK;
  2673. }
  2674. else
  2675. {
  2676. return HAL_BUSY;
  2677. }
  2678. }
  2679. /**
  2680. * @brief Initialize the HASH peripheral in HMAC mode, next process pInBuffer then
  2681. * read the computed digest.
  2682. * @note Digest is available in pOutBuffer.
  2683. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  2684. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  2685. * @param hhash HASH handle.
  2686. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  2687. * @param Size length of the input buffer in bytes.
  2688. * @param pOutBuffer pointer to the computed digest.
  2689. * @param Timeout Timeout value.
  2690. * @param Algorithm HASH algorithm.
  2691. * @retval HAL status
  2692. */
  2693. HAL_StatusTypeDef HMAC_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout, uint32_t Algorithm)
  2694. {
  2695. HAL_HASH_StateTypeDef State_tmp = hhash->State;
  2696. /* If State is ready or suspended, start or resume polling-based HASH processing */
  2697. if((State_tmp == HAL_HASH_STATE_READY) || (State_tmp == HAL_HASH_STATE_SUSPENDED))
  2698. {
  2699. /* Check input parameters */
  2700. if ((pInBuffer == NULL) || (Size == 0U) || (hhash->Init.pKey == NULL) || (hhash->Init.KeySize == 0U) || (pOutBuffer == NULL))
  2701. {
  2702. hhash->State = HAL_HASH_STATE_READY;
  2703. return HAL_ERROR;
  2704. }
  2705. /* Process Locked */
  2706. __HAL_LOCK(hhash);
  2707. /* Change the HASH state */
  2708. hhash->State = HAL_HASH_STATE_BUSY;
  2709. /* Check if initialization phase has already be performed */
  2710. if(hhash->Phase == HAL_HASH_PHASE_READY)
  2711. {
  2712. /* Check if key size is larger than 64 bytes, accordingly set LKEY and the other setting bits */
  2713. if(hhash->Init.KeySize > 64U)
  2714. {
  2715. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_ALGOMODE_HMAC | HASH_HMAC_KEYTYPE_LONGKEY | HASH_CR_INIT);
  2716. }
  2717. else
  2718. {
  2719. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_ALGOMODE_HMAC | HASH_CR_INIT);
  2720. }
  2721. /* Set the phase to Step 1 */
  2722. hhash->Phase = HAL_HASH_PHASE_HMAC_STEP_1;
  2723. /* Resort to hhash internal fields to feed the Peripheral.
  2724. Parameters will be updated in case of suspension to contain the proper
  2725. information at resumption time. */
  2726. hhash->pHashOutBuffPtr = pOutBuffer; /* Output digest address */
  2727. hhash->pHashInBuffPtr = pInBuffer; /* Input data address, HMAC_Processing input parameter for Step 2 */
  2728. hhash->HashInCount = Size; /* Input data size, HMAC_Processing input parameter for Step 2 */
  2729. hhash->HashBuffSize = Size; /* Store the input buffer size for the whole HMAC process */
  2730. hhash->pHashKeyBuffPtr = hhash->Init.pKey; /* Key address, HMAC_Processing input parameter for Step 1 and Step 3 */
  2731. hhash->HashKeyCount = hhash->Init.KeySize; /* Key size, HMAC_Processing input parameter for Step 1 and Step 3 */
  2732. }
  2733. /* Carry out HMAC processing */
  2734. return HMAC_Processing(hhash, Timeout);
  2735. }
  2736. else
  2737. {
  2738. return HAL_BUSY;
  2739. }
  2740. }
  2741. /**
  2742. * @brief Initialize the HASH peripheral in HMAC mode, next process pInBuffer then
  2743. * read the computed digest in interruption mode.
  2744. * @note Digest is available in pOutBuffer.
  2745. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  2746. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  2747. * @param hhash HASH handle.
  2748. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  2749. * @param Size length of the input buffer in bytes.
  2750. * @param pOutBuffer pointer to the computed digest.
  2751. * @param Algorithm HASH algorithm.
  2752. * @retval HAL status
  2753. */
  2754. HAL_StatusTypeDef HMAC_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Algorithm)
  2755. {
  2756. HAL_HASH_StateTypeDef State_tmp = hhash->State;
  2757. /* If State is ready or suspended, start or resume IT-based HASH processing */
  2758. if((State_tmp == HAL_HASH_STATE_READY) || (State_tmp == HAL_HASH_STATE_SUSPENDED))
  2759. {
  2760. /* Check input parameters */
  2761. if ((pInBuffer == NULL) || (Size == 0U) || (hhash->Init.pKey == NULL) || (hhash->Init.KeySize == 0U) || (pOutBuffer == NULL))
  2762. {
  2763. hhash->State = HAL_HASH_STATE_READY;
  2764. return HAL_ERROR;
  2765. }
  2766. /* Process Locked */
  2767. __HAL_LOCK(hhash);
  2768. /* Change the HASH state */
  2769. hhash->State = HAL_HASH_STATE_BUSY;
  2770. /* Initialize IT counter */
  2771. hhash->HashITCounter = 1;
  2772. /* Check if initialization phase has already be performed */
  2773. if (hhash->Phase == HAL_HASH_PHASE_READY)
  2774. {
  2775. /* Check if key size is larger than 64 bytes, accordingly set LKEY and the other setting bits */
  2776. if(hhash->Init.KeySize > 64U)
  2777. {
  2778. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_ALGOMODE_HMAC | HASH_HMAC_KEYTYPE_LONGKEY | HASH_CR_INIT);
  2779. }
  2780. else
  2781. {
  2782. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_ALGOMODE_HMAC | HASH_CR_INIT);
  2783. }
  2784. /* Resort to hhash internal fields hhash->pHashInBuffPtr and hhash->HashInCount
  2785. to feed the Peripheral whatever the HMAC step.
  2786. Lines below are set to start HMAC Step 1 processing where key is entered first. */
  2787. hhash->HashInCount = hhash->Init.KeySize; /* Key size */
  2788. hhash->pHashInBuffPtr = hhash->Init.pKey ; /* Key address */
  2789. /* Store input and output parameters in handle fields to manage steps transition
  2790. or possible HMAC suspension/resumption */
  2791. hhash->pHashKeyBuffPtr = hhash->Init.pKey; /* Key address */
  2792. hhash->pHashMsgBuffPtr = pInBuffer; /* Input message address */
  2793. hhash->HashBuffSize = Size; /* Input message size (in bytes) */
  2794. hhash->pHashOutBuffPtr = pOutBuffer; /* Output digest address */
  2795. /* Configure the number of valid bits in last word of the key */
  2796. __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize);
  2797. /* Set the phase to Step 1 */
  2798. hhash->Phase = HAL_HASH_PHASE_HMAC_STEP_1;
  2799. }
  2800. else if ((hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_1) || (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_3))
  2801. {
  2802. /* Restart IT-based HASH processing after Step 1 or Step 3 suspension */
  2803. }
  2804. else if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_2)
  2805. {
  2806. /* Restart IT-based HASH processing after Step 2 suspension */
  2807. }
  2808. else
  2809. {
  2810. /* Error report as phase incorrect */
  2811. /* Process Unlock */
  2812. __HAL_UNLOCK(hhash);
  2813. hhash->State = HAL_HASH_STATE_READY;
  2814. return HAL_ERROR;
  2815. }
  2816. /* Process Unlock */
  2817. __HAL_UNLOCK(hhash);
  2818. /* Enable Interrupts */
  2819. __HAL_HASH_ENABLE_IT(HASH_IT_DINI|HASH_IT_DCI);
  2820. /* Return function status */
  2821. return HAL_OK;
  2822. }
  2823. else
  2824. {
  2825. return HAL_BUSY;
  2826. }
  2827. }
  2828. /**
  2829. * @brief Initialize the HASH peripheral in HMAC mode then initiate the required
  2830. * DMA transfers to feed the key and the input buffer to the Peripheral.
  2831. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  2832. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  2833. * @note In case of multi-buffer HMAC processing, the input buffer size (in bytes) must
  2834. * be a multiple of 4 otherwise, the HASH digest computation is corrupted.
  2835. * Only the length of the last buffer of the thread doesn't have to be a
  2836. * multiple of 4.
  2837. * @param hhash HASH handle.
  2838. * @param pInBuffer pointer to the input buffer (buffer to be hashed).
  2839. * @param Size length of the input buffer in bytes.
  2840. * @param Algorithm HASH algorithm.
  2841. * @retval HAL status
  2842. */
  2843. HAL_StatusTypeDef HMAC_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint32_t Algorithm)
  2844. {
  2845. uint32_t inputaddr;
  2846. uint32_t inputSize;
  2847. HAL_StatusTypeDef status ;
  2848. HAL_HASH_StateTypeDef State_tmp = hhash->State;
  2849. /* Make sure the input buffer size (in bytes) is a multiple of 4 when digest calculation
  2850. is disabled (multi-buffer HMAC processing, MDMAT bit to be set) */
  2851. assert_param(IS_HMAC_DMA_MULTIBUFFER_SIZE(hhash, Size));
  2852. /* If State is ready or suspended, start or resume DMA-based HASH processing */
  2853. if((State_tmp == HAL_HASH_STATE_READY) || (State_tmp == HAL_HASH_STATE_SUSPENDED))
  2854. {
  2855. /* Check input parameters */
  2856. if ((pInBuffer == NULL ) || (Size == 0U) || (hhash->Init.pKey == NULL ) || (hhash->Init.KeySize == 0U) ||
  2857. /* Check phase coherency. Phase must be
  2858. either READY (fresh start)
  2859. or one of HMAC PROCESS steps (multi-buffer HASH management) */
  2860. ((hhash->Phase != HAL_HASH_PHASE_READY) && (!(IS_HMAC_PROCESSING(hhash)))))
  2861. {
  2862. hhash->State = HAL_HASH_STATE_READY;
  2863. return HAL_ERROR;
  2864. }
  2865. /* Process Locked */
  2866. __HAL_LOCK(hhash);
  2867. /* If not a case of resumption after suspension */
  2868. if (hhash->State == HAL_HASH_STATE_READY)
  2869. {
  2870. /* Check whether or not initialization phase has already be performed */
  2871. if(hhash->Phase == HAL_HASH_PHASE_READY)
  2872. {
  2873. /* Change the HASH state */
  2874. hhash->State = HAL_HASH_STATE_BUSY;
  2875. #if defined(HASH_CR_MDMAT)
  2876. /* Check if key size is larger than 64 bytes, accordingly set LKEY and the other setting bits.
  2877. At the same time, ensure MDMAT bit is cleared. */
  2878. if(hhash->Init.KeySize > 64U)
  2879. {
  2880. MODIFY_REG(HASH->CR, HASH_CR_MDMAT|HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_ALGOMODE_HMAC | HASH_HMAC_KEYTYPE_LONGKEY | HASH_CR_INIT);
  2881. }
  2882. else
  2883. {
  2884. MODIFY_REG(HASH->CR, HASH_CR_MDMAT|HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_ALGOMODE_HMAC | HASH_CR_INIT);
  2885. }
  2886. #else
  2887. /* Check if key size is larger than 64 bytes, accordingly set LKEY and the other setting bits */
  2888. if(hhash->Init.KeySize > 64U)
  2889. {
  2890. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_ALGOMODE_HMAC | HASH_HMAC_KEYTYPE_LONGKEY | HASH_CR_INIT);
  2891. }
  2892. else
  2893. {
  2894. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_ALGOMODE_HMAC | HASH_CR_INIT);
  2895. }
  2896. #endif
  2897. /* Store input aparameters in handle fields to manage steps transition
  2898. or possible HMAC suspension/resumption */
  2899. hhash->HashInCount = hhash->Init.KeySize; /* Initial size for first DMA transfer (key size) */
  2900. hhash->pHashKeyBuffPtr = hhash->Init.pKey; /* Key address */
  2901. hhash->pHashInBuffPtr = hhash->Init.pKey ; /* First address passed to DMA (key address at Step 1) */
  2902. hhash->pHashMsgBuffPtr = pInBuffer; /* Input data address */
  2903. hhash->HashBuffSize = Size; /* input data size (in bytes) */
  2904. /* Set DMA input parameters */
  2905. inputaddr = (uint32_t)(hhash->Init.pKey); /* Address passed to DMA (start by entering Key message) */
  2906. inputSize = hhash->Init.KeySize; /* Size for first DMA transfer (in bytes) */
  2907. /* Configure the number of valid bits in last word of the key */
  2908. __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize);
  2909. /* Set the phase to Step 1 */
  2910. hhash->Phase = HAL_HASH_PHASE_HMAC_STEP_1;
  2911. }
  2912. else if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_2)
  2913. {
  2914. /* Process a new input data message in case of multi-buffer HMAC processing
  2915. (this is not a resumption case) */
  2916. /* Change the HASH state */
  2917. hhash->State = HAL_HASH_STATE_BUSY;
  2918. /* Save input parameters to be able to manage possible suspension/resumption */
  2919. hhash->HashInCount = Size; /* Input message address */
  2920. hhash->pHashInBuffPtr = pInBuffer; /* Input message size in bytes */
  2921. /* Set DMA input parameters */
  2922. inputaddr = (uint32_t)pInBuffer; /* Input message address */
  2923. inputSize = Size; /* Input message size in bytes */
  2924. if (hhash->DigestCalculationDisable == RESET)
  2925. {
  2926. /* This means this is the last buffer of the multi-buffer sequence: DCAL needs to be set. */
  2927. #if defined(HASH_CR_MDMAT)
  2928. __HAL_HASH_RESET_MDMAT();
  2929. #endif
  2930. __HAL_HASH_SET_NBVALIDBITS(inputSize);
  2931. }
  2932. }
  2933. else
  2934. {
  2935. /* Phase not aligned with handle READY state */
  2936. __HAL_UNLOCK(hhash);
  2937. /* Return function status */
  2938. return HAL_ERROR;
  2939. }
  2940. }
  2941. else
  2942. {
  2943. /* Resumption case (phase may be Step 1, 2 or 3) */
  2944. /* Change the HASH state */
  2945. hhash->State = HAL_HASH_STATE_BUSY;
  2946. /* Set DMA input parameters at resumption location;
  2947. inputaddr and inputSize are not set to the API input parameters
  2948. but to those saved beforehand by HAL_HASH_DMAFeed_ProcessSuspend() when the
  2949. processing was suspended. */
  2950. inputaddr = (uint32_t)(hhash->pHashInBuffPtr); /* Input message address */
  2951. inputSize = hhash->HashInCount; /* Input message size in bytes */
  2952. }
  2953. /* Set the HASH DMA transfert complete callback */
  2954. hhash->hdmain->XferCpltCallback = HASH_DMAXferCplt;
  2955. /* Set the DMA error callback */
  2956. hhash->hdmain->XferErrorCallback = HASH_DMAError;
  2957. /* Store number of words already pushed to manage proper DMA processing suspension */
  2958. hhash->NbWordsAlreadyPushed = HASH_NBW_PUSHED();
  2959. /* Enable the DMA In DMA Stream */
  2960. status = HAL_DMA_Start_IT(hhash->hdmain, inputaddr, (uint32_t)&HASH->DIN, (((inputSize %4U)!=0U) ? ((inputSize+(4U-(inputSize %4U)))/4U):(inputSize/4U)));
  2961. /* Enable DMA requests */
  2962. SET_BIT(HASH->CR, HASH_CR_DMAE);
  2963. /* Process Unlocked */
  2964. __HAL_UNLOCK(hhash);
  2965. /* Return function status */
  2966. if (status != HAL_OK)
  2967. {
  2968. /* Update HASH state machine to error */
  2969. hhash->State = HAL_HASH_STATE_ERROR;
  2970. }
  2971. /* Return function status */
  2972. return status;
  2973. }
  2974. else
  2975. {
  2976. return HAL_BUSY;
  2977. }
  2978. }
  2979. /**
  2980. * @}
  2981. */
  2982. #endif /* HAL_HASH_MODULE_ENABLED */
  2983. /**
  2984. * @}
  2985. */
  2986. #endif /* HASH*/
  2987. /**
  2988. * @}
  2989. */
  2990. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/