stm32f4xx_hal_hash.c 129 KB

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