stm32l4xx_hal_hash.c 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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 IP, the digest computation can start.
  57. (#)Multi-buffer processing is possible in polling and DMA mode.
  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_Start() to enter the last one and retrieve as
  61. well the computed digest.
  62. (##) In DMA mode, multi-buffer HASH and HMAC processing are possible.
  63. (+++) HASH processing: once initialization is done, MDMAT bit must be set thru __HAL_HASH_SET_MDMAT() macro.
  64. From that point, each buffer can be fed to the IP thru HAL_HASH_xxx_Start_DMA() API.
  65. Before entering the last buffer, reset the MDMAT bit with __HAL_HASH_RESET_MDMAT()
  66. macro then wrap-up the HASH processing in feeding the last input buffer thru the
  67. same API HAL_HASH_xxx_Start_DMA(). The digest can then be retrieved with a call to
  68. API HAL_HASH_xxx_Finish().
  69. (+++) HMAC processing (requires to resort to extended functions):
  70. after initialization, the key and the first input buffer are entered
  71. in the IP with the API HAL_HMACEx_xxx_Step1_2_DMA(). This carries out HMAC step 1 and
  72. starts step 2.
  73. The following buffers are next entered with the API HAL_HMACEx_xxx_Step2_DMA(). At this
  74. point, the HMAC processing is still carrying out step 2.
  75. Then, step 2 for the last input buffer and step 3 are carried out by a single call
  76. to HAL_HMACEx_xxx_Step2_3_DMA().
  77. The digest can finally be retrieved with a call to API HAL_HASH_xxx_Finish().
  78. (#)Context swapping.
  79. (##) Two APIs are available to suspend HASH or HMAC processing:
  80. (+++) HAL_HASH_SwFeed_ProcessSuspend() when data are entered by software (polling or IT mode),
  81. (+++) HAL_HASH_DMAFeed_ProcessSuspend() when data are entered by DMA.
  82. (##) When HASH or HMAC processing is suspended, HAL_HASH_ContextSaving() allows
  83. to save in memory the IP context. This context can be restored afterwards
  84. to resume the HASH processing thanks to HAL_HASH_ContextRestoring().
  85. (##) Once the HASH IP has been restored to the same configuration as that at suspension
  86. time, processing can be restarted with the same API call (same API, same handle,
  87. same parameters) as done before the suspension. Relevant parameters to restart at
  88. the proper location are internally saved in the HASH handle.
  89. (#)Call HAL_HASH_DeInit() to deinitialize the HASH peripheral.
  90. @endverbatim
  91. ******************************************************************************
  92. * @attention
  93. *
  94. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  95. *
  96. * Redistribution and use in source and binary forms, with or without modification,
  97. * are permitted provided that the following conditions are met:
  98. * 1. Redistributions of source code must retain the above copyright notice,
  99. * this list of conditions and the following disclaimer.
  100. * 2. Redistributions in binary form must reproduce the above copyright notice,
  101. * this list of conditions and the following disclaimer in the documentation
  102. * and/or other materials provided with the distribution.
  103. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  104. * may be used to endorse or promote products derived from this software
  105. * without specific prior written permission.
  106. *
  107. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  108. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  109. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  110. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  111. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  112. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  113. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  114. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  115. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  116. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  117. *
  118. ******************************************************************************
  119. */
  120. /* Includes ------------------------------------------------------------------*/
  121. #include "stm32l4xx_hal.h"
  122. #ifdef HAL_HASH_MODULE_ENABLED
  123. #if defined (STM32L4A6xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  124. /** @addtogroup STM32L4xx_HAL_Driver
  125. * @{
  126. */
  127. /** @defgroup HASH HASH
  128. * @brief HASH HAL module driver.
  129. * @{
  130. */
  131. /* Private typedef -----------------------------------------------------------*/
  132. /* Private define ------------------------------------------------------------*/
  133. /** @defgroup HASH_Private_Constants HASH Private Constants
  134. * @{
  135. */
  136. /** @defgroup HASH_Digest_Calculation_Status HASH Digest Calculation Status
  137. * @{
  138. */
  139. #define HASH_DIGEST_CALCULATION_NOT_STARTED ((uint32_t)0x00000000) /*!< DCAL not set after input data written in DIN register */
  140. #define HASH_DIGEST_CALCULATION_STARTED ((uint32_t)0x00000001) /*!< DCAL set after input data written in DIN register */
  141. /**
  142. * @}
  143. */
  144. /** @defgroup HASH_Number_Of_CSR_Registers HASH Number of Context Swap Registers
  145. * @{
  146. */
  147. #define HASH_NUMBER_OF_CSR_REGISTERS 54 /*!< Number of Context Swap Registers */
  148. /**
  149. * @}
  150. */
  151. /** @defgroup HASH_TimeOut_Value HASH TimeOut Value
  152. * @{
  153. */
  154. #define HASH_TIMEOUTVALUE 1000 /*!< Time-out value */
  155. /**
  156. * @}
  157. */
  158. /** @defgroup HASH_DMA_Suspension_Words_Limit HASH DMA suspension words limit
  159. * @{
  160. */
  161. #define HASH_DMA_SUSPENSION_WORDS_LIMIT 20 /*!< Number of words below which DMA suspension is aborted */
  162. /**
  163. * @}
  164. */
  165. /**
  166. * @}
  167. */
  168. /* Private macro -------------------------------------------------------------*/
  169. /* Private variables ---------------------------------------------------------*/
  170. /* Private function prototypes -----------------------------------------------*/
  171. /** @defgroup HASH_Private_Functions HASH Private Functions
  172. * @{
  173. */
  174. static void HASH_DMAXferCplt(DMA_HandleTypeDef *hdma);
  175. static void HASH_DMAError(DMA_HandleTypeDef *hdma);
  176. static void HASH_GetDigest(uint8_t *pMsgDigest, uint8_t Size);
  177. static HAL_StatusTypeDef HASH_WaitOnFlagUntilTimeout(HASH_HandleTypeDef *hhash, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
  178. static HAL_StatusTypeDef HASH_WriteData(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
  179. static HAL_StatusTypeDef HASH_IT(HASH_HandleTypeDef *hhash);
  180. static uint32_t HASH_Write_Block_Data(HASH_HandleTypeDef *hhash);
  181. static HAL_StatusTypeDef HMAC_Processing(HASH_HandleTypeDef *hhash, uint32_t Timeout);
  182. /**
  183. * @}
  184. */
  185. /** @defgroup HASH_Exported_Functions HASH Exported Functions
  186. * @{
  187. */
  188. /** @defgroup HASH_Exported_Functions_Group1 Initialization and de-initialization functions
  189. * @brief Initialization, configuration and call-back functions.
  190. *
  191. @verbatim
  192. ===============================================================================
  193. ##### Initialization and de-initialization functions #####
  194. ===============================================================================
  195. [..] This section provides functions allowing to:
  196. (+) Initialize the HASH according to the specified parameters
  197. in the HASH_InitTypeDef and create the associated handle
  198. (+) DeInitialize the HASH peripheral
  199. (+) Initialize the HASH MCU Specific Package (MSP)
  200. (+) DeInitialize the HASH MSP
  201. [..] This section provides as well call back functions definitions for user
  202. code to manage:
  203. (+) Input data transfer to IP completion
  204. (+) Calculated digest retrieval completion
  205. (+) Error management
  206. @endverbatim
  207. * @{
  208. */
  209. /**
  210. * @brief Initialize the HASH according to the specified parameters in the
  211. HASH_HandleTypeDef and create the associated handle.
  212. * @note Only MDMAT and DATATYPE bits of HASH IP are set by HAL_HASH_Init(),
  213. * other configuration bits are set by HASH or HMAC processing APIs.
  214. * @note MDMAT bit is systematically reset by HAL_HASH_Init(). To set it for
  215. * multi-buffer HASH processing, user needs to resort to
  216. * __HAL_HASH_SET_MDMAT() macro. For HMAC multi-buffer processing, the
  217. * relevant APIs manage themselves the MDMAT bit.
  218. * @param hhash: HASH handle
  219. * @retval HAL status
  220. */
  221. HAL_StatusTypeDef HAL_HASH_Init(HASH_HandleTypeDef *hhash)
  222. {
  223. /* Check the parameters */
  224. assert_param(IS_HASH_DATATYPE(hhash->Init.DataType));
  225. /* Check the hash handle allocation */
  226. if(hhash == NULL)
  227. {
  228. return HAL_ERROR;
  229. }
  230. if(hhash->State == HAL_HASH_STATE_RESET)
  231. {
  232. /* Allocate lock resource and initialize it */
  233. hhash->Lock = HAL_UNLOCKED;
  234. /* Init the low level hardware */
  235. HAL_HASH_MspInit(hhash);
  236. }
  237. /* Change the HASH state */
  238. hhash->State = HAL_HASH_STATE_BUSY;
  239. /* Reset HashInCount, HashITCounter, HashBuffSize and NbWordsAlreadyPushed */
  240. hhash->HashInCount = 0;
  241. hhash->HashBuffSize = 0;
  242. hhash->HashITCounter = 0;
  243. hhash->NbWordsAlreadyPushed = 0;
  244. /* Reset digest calculation bridle (MDMAT bit control) */
  245. hhash->DigestCalculationDisable = RESET;
  246. /* Set phase to READY */
  247. hhash->Phase = HAL_HASH_PHASE_READY;
  248. /* Set the data type and reset MDMAT bit */
  249. MODIFY_REG(HASH->CR, HASH_CR_DATATYPE|HASH_CR_MDMAT, hhash->Init.DataType);
  250. /* Reset HASH handle status */
  251. hhash->Status = HAL_OK;
  252. /* Set the HASH state to Ready */
  253. hhash->State = HAL_HASH_STATE_READY;
  254. /* Return function status */
  255. return HAL_OK;
  256. }
  257. /**
  258. * @brief DeInitialize the HASH peripheral.
  259. * @param hhash: HASH handle.
  260. * @retval HAL status
  261. */
  262. HAL_StatusTypeDef HAL_HASH_DeInit(HASH_HandleTypeDef *hhash)
  263. {
  264. /* Check the HASH handle allocation */
  265. if(hhash == NULL)
  266. {
  267. return HAL_ERROR;
  268. }
  269. /* Change the HASH state */
  270. hhash->State = HAL_HASH_STATE_BUSY;
  271. /* Set the default HASH phase */
  272. hhash->Phase = HAL_HASH_PHASE_READY;
  273. /* Reset HashInCount, HashITCounter and HashBuffSize */
  274. hhash->HashInCount = 0;
  275. hhash->HashBuffSize = 0;
  276. hhash->HashITCounter = 0;
  277. /* Reset digest calculation bridle (MDMAT bit control) */
  278. hhash->DigestCalculationDisable = RESET;
  279. /* DeInit the low level hardware: CLOCK, NVIC.*/
  280. HAL_HASH_MspDeInit(hhash);
  281. /* Reset HASH handle status */
  282. hhash->Status = HAL_OK;
  283. /* Set the HASH state to Ready */
  284. hhash->State = HAL_HASH_STATE_RESET;
  285. /* Return function status */
  286. return HAL_OK;
  287. }
  288. /**
  289. * @brief Initialize the HASH MSP.
  290. * @param hhash: HASH handle.
  291. * @retval None
  292. */
  293. __weak void HAL_HASH_MspInit(HASH_HandleTypeDef *hhash)
  294. {
  295. /* Prevent unused argument(s) compilation warning */
  296. UNUSED(hhash);
  297. /* NOTE : This function should not be modified; when the callback is needed,
  298. HAL_HASH_MspInit() can be implemented in the user file.
  299. */
  300. }
  301. /**
  302. * @brief DeInitialize the HASH MSP.
  303. * @param hhash: HASH handle.
  304. * @retval None
  305. */
  306. __weak void HAL_HASH_MspDeInit(HASH_HandleTypeDef *hhash)
  307. {
  308. /* Prevent unused argument(s) compilation warning */
  309. UNUSED(hhash);
  310. /* NOTE : This function should not be modified; when the callback is needed,
  311. HAL_HASH_MspDeInit() can be implemented in the user file.
  312. */
  313. }
  314. /**
  315. * @brief Input data transfer complete call back.
  316. * @note HAL_HASH_InCpltCallback() is called when the complete input message
  317. * has been fed to the IP. This API is invoked only when input data are
  318. * entered under interruption or thru DMA.
  319. * @note In case of HASH or HMAC multi-buffer DMA feeding case (MDMAT bit set),
  320. * HAL_HASH_InCpltCallback() is called at the end of each buffer feeding
  321. * to the IP.
  322. * @param hhash: HASH handle.
  323. * @retval None
  324. */
  325. __weak void HAL_HASH_InCpltCallback(HASH_HandleTypeDef *hhash)
  326. {
  327. /* Prevent unused argument(s) compilation warning */
  328. UNUSED(hhash);
  329. /* NOTE : This function should not be modified; when the callback is needed,
  330. HAL_HASH_InCpltCallback() can be implemented in the user file.
  331. */
  332. }
  333. /**
  334. * @brief Digest computation complete call back.
  335. * @note HAL_HASH_DgstCpltCallback() is used under interruption, is not
  336. * relevant with DMA.
  337. * @param hhash: HASH handle.
  338. * @retval None
  339. */
  340. __weak void HAL_HASH_DgstCpltCallback(HASH_HandleTypeDef *hhash)
  341. {
  342. /* Prevent unused argument(s) compilation warning */
  343. UNUSED(hhash);
  344. /* NOTE : This function should not be modified; when the callback is needed,
  345. HAL_HASH_DgstCpltCallback() can be implemented in the user file.
  346. */
  347. }
  348. /**
  349. * @brief Error callback.
  350. * @note Code user can resort to hhash->Status (HAL_ERROR, HAL_TIMEOUT,...)
  351. * to retrieve the error type.
  352. * @param hhash: HASH handle.
  353. * @retval None
  354. */
  355. __weak void HAL_HASH_ErrorCallback(HASH_HandleTypeDef *hhash)
  356. {
  357. /* Prevent unused argument(s) compilation warning */
  358. UNUSED(hhash);
  359. /* NOTE : This function should not be modified; when the callback is needed,
  360. HAL_HASH_ErrorCallback() can be implemented in the user file.
  361. */
  362. }
  363. /**
  364. * @}
  365. */
  366. /** @defgroup HASH_Exported_Functions_Group2 HASH processing functions in polling mode
  367. * @brief HASH processing functions using polling mode.
  368. *
  369. @verbatim
  370. ===============================================================================
  371. ##### Polling mode HASH processing functions #####
  372. ===============================================================================
  373. [..] This section provides functions allowing to calculate in polling mode
  374. the hash value using one of the following algorithms:
  375. (+) MD5
  376. (++) HAL_HASH_MD5_Start()
  377. (++) HAL_HASH_MD5_Accumulate()
  378. (+) SHA1
  379. (++) HAL_HASH_SHA1_Start()
  380. (++) HAL_HASH_SHA1_Accumulate()
  381. [..] For a single buffer to be hashed, user can resort to HAL_HASH_xxx_Start().
  382. [..] In case of multi-buffer HASH processing (a single digest is computed while
  383. several buffers are fed to the IP), the user can resort to successive calls
  384. to HAL_HASH_xxx_Accumulate() and wrap-up the digest computation by a call
  385. to HAL_HASH_xxx_Start().
  386. @endverbatim
  387. * @{
  388. */
  389. /**
  390. * @brief Initialize the HASH peripheral in MD5 mode, next process pInBuffer then
  391. * read the computed digest.
  392. * @note Digest is available in pOutBuffer.
  393. * @param hhash: HASH handle.
  394. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  395. * @param Size: length of the input buffer in bytes.
  396. * @param pOutBuffer: pointer to the computed digest. Digest size is 16 bytes.
  397. * @param Timeout: Timeout value
  398. * @retval HAL status
  399. */
  400. HAL_StatusTypeDef HAL_HASH_MD5_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout)
  401. {
  402. return HASH_Start(hhash, pInBuffer, Size, pOutBuffer, Timeout, HASH_ALGOSELECTION_MD5);
  403. }
  404. /**
  405. * @brief If not already done, initialize the HASH peripheral in MD5 mode then
  406. * processes pInBuffer.
  407. * @note Consecutive calls to HAL_HASH_MD5_Accumulate() can be used to feed
  408. * several input buffers back-to-back to the IP that will yield a single
  409. * HASH signature once all buffers have been entered. Wrap-up of input
  410. * buffers feeding and retrieval of digest is done by a call to
  411. * HAL_HASH_MD5_Start().
  412. * @note Field hhash->Phase of HASH handle is tested to check whether or not
  413. * the IP has already been initialized.
  414. * @note Digest is not retrieved by this API, user must resort to HAL_HASH_MD5_Start()
  415. * to read it, feeding at the same time the last input buffer to the IP.
  416. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  417. * HASH digest computation is corrupted. Only HAL_HASH_MD5_Start() is able
  418. * to manage the ending buffer with a length in bytes not a multiple of 4.
  419. * @param hhash: HASH handle.
  420. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  421. * @param Size: length of the input buffer in bytes, must be a multiple of 4.
  422. * @retval HAL status
  423. */
  424. HAL_StatusTypeDef HAL_HASH_MD5_Accumulate(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  425. {
  426. return HASH_Accumulate(hhash, pInBuffer, Size,HASH_ALGOSELECTION_MD5);
  427. }
  428. /**
  429. * @brief Initialize the HASH peripheral in SHA1 mode, next process pInBuffer then
  430. * read the computed digest.
  431. * @note Digest is available in pOutBuffer.
  432. * @param hhash: HASH handle.
  433. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  434. * @param Size: length of the input buffer in bytes.
  435. * @param pOutBuffer: pointer to the computed digest. Digest size is 20 bytes.
  436. * @param Timeout: Timeout value
  437. * @retval HAL status
  438. */
  439. HAL_StatusTypeDef HAL_HASH_SHA1_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout)
  440. {
  441. return HASH_Start(hhash, pInBuffer, Size, pOutBuffer, Timeout, HASH_ALGOSELECTION_SHA1);
  442. }
  443. /**
  444. * @brief If not already done, initialize the HASH peripheral in SHA1 mode then
  445. * processes pInBuffer.
  446. * @note Consecutive calls to HAL_HASH_SHA1_Accumulate() can be used to feed
  447. * several input buffers back-to-back to the IP that will yield a single
  448. * HASH signature once all buffers have been entered. Wrap-up of input
  449. * buffers feeding and retrieval of digest is done by a call to
  450. * HAL_HASH_SHA1_Start().
  451. * @note Field hhash->Phase of HASH handle is tested to check whether or not
  452. * the IP has already been initialized.
  453. * @note Digest is not retrieved by this API, user must resort to HAL_HASH_SHA1_Start()
  454. * to read it, feeding at the same time the last input buffer to the IP.
  455. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  456. * HASH digest computation is corrupted. Only HAL_HASH_SHA1_Start() is able
  457. * to manage the ending buffer with a length in bytes not a multiple of 4.
  458. * @param hhash: HASH handle.
  459. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  460. * @param Size: length of the input buffer in bytes, must be a multiple of 4.
  461. * @retval HAL status
  462. */
  463. HAL_StatusTypeDef HAL_HASH_SHA1_Accumulate(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  464. {
  465. return HASH_Accumulate(hhash, pInBuffer, Size,HASH_ALGOSELECTION_SHA1);
  466. }
  467. /**
  468. * @}
  469. */
  470. /** @defgroup HASH_Exported_Functions_Group3 HASH processing functions in interrupt mode
  471. * @brief HASH processing functions using interrupt mode.
  472. *
  473. @verbatim
  474. ===============================================================================
  475. ##### Interruption mode HASH processing functions #####
  476. ===============================================================================
  477. [..] This section provides functions allowing to calculate in interrupt mode
  478. the hash value using one of the following algorithms:
  479. (+) MD5
  480. (++) HAL_HASH_MD5_Start_IT()
  481. (+) SHA1
  482. (++) HAL_HASH_SHA1_Start_IT()
  483. [..] API HAL_HASH_IRQHandler() manages each HASH interruption.
  484. [..] Note that HAL_HASH_IRQHandler() manages as well HASH IP interruptions when in
  485. HMAC processing mode.
  486. @endverbatim
  487. * @{
  488. */
  489. /**
  490. * @brief Initialize the HASH peripheral in MD5 mode, next process pInBuffer then
  491. * read the computed digest in interruption mode.
  492. * @note Digest is available in pOutBuffer.
  493. * @param hhash: HASH handle.
  494. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  495. * @param Size: length of the input buffer in bytes.
  496. * @param pOutBuffer: pointer to the computed digest. Digest size is 16 bytes.
  497. * @retval HAL status
  498. */
  499. HAL_StatusTypeDef HAL_HASH_MD5_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer)
  500. {
  501. return HASH_Start_IT(hhash, pInBuffer, Size, pOutBuffer,HASH_ALGOSELECTION_MD5);
  502. }
  503. /**
  504. * @brief Initialize the HASH peripheral in SHA1 mode, next process pInBuffer then
  505. * read the computed digest in interruption mode.
  506. * @note Digest is available in pOutBuffer.
  507. * @param hhash: HASH handle.
  508. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  509. * @param Size: length of the input buffer in bytes.
  510. * @param pOutBuffer: pointer to the computed digest. Digest size is 20 bytes.
  511. * @retval HAL status
  512. */
  513. HAL_StatusTypeDef HAL_HASH_SHA1_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer)
  514. {
  515. return HASH_Start_IT(hhash, pInBuffer, Size, pOutBuffer,HASH_ALGOSELECTION_SHA1);
  516. }
  517. /**
  518. * @brief Handle HASH interrupt request.
  519. * @param hhash: HASH handle.
  520. * @note HAL_HASH_IRQHandler() handles interrupts in HMAC processing as well.
  521. * @note In case of error reported during the HASH interruption processing,
  522. * HAL_HASH_ErrorCallback() API is called so that user code can
  523. * manage the error. The error type is available in hhash->Status field.
  524. * @retval None
  525. */
  526. void HAL_HASH_IRQHandler(HASH_HandleTypeDef *hhash)
  527. {
  528. hhash->Status = HASH_IT(hhash);
  529. if (hhash->Status != HAL_OK)
  530. {
  531. HAL_HASH_ErrorCallback(hhash);
  532. /* After error handling by code user, reset HASH handle HAL status */
  533. hhash->Status = HAL_OK;
  534. }
  535. }
  536. /**
  537. * @}
  538. */
  539. /** @defgroup HASH_Exported_Functions_Group4 HASH processing functions in DMA mode
  540. * @brief HASH processing functions using DMA mode.
  541. *
  542. @verbatim
  543. ===============================================================================
  544. ##### DMA mode HASH processing functions #####
  545. ===============================================================================
  546. [..] This section provides functions allowing to calculate in DMA mode
  547. the hash value using one of the following algorithms:
  548. (+) MD5
  549. (++) HAL_HASH_MD5_Start_DMA()
  550. (++) HAL_HASH_MD5_Finish()
  551. (+) SHA1
  552. (++) HAL_HASH_SHA1_Start_DMA()
  553. (++) HAL_HASH_SHA1_Finish()
  554. [..] When resorting to DMA mode to enter the data in the IP, user must resort
  555. to HAL_HASH_xxx_Start_DMA() then read the resulting digest with
  556. HAL_HASH_xxx_Finish().
  557. [..] In case of multi-buffer HASH processing, MDMAT bit must first be set before
  558. the successive calls to HAL_HASH_xxx_Start_DMA(). Then, MDMAT bit needs to be
  559. reset before the last call to HAL_HASH_xxx_Start_DMA(). Digest is finally
  560. retrieved thanks to HAL_HASH_xxx_Finish().
  561. @endverbatim
  562. * @{
  563. */
  564. /**
  565. * @brief Initialize the HASH peripheral in MD5 mode then initiate a DMA transfer
  566. * to feed the input buffer to the IP.
  567. * @note Once the DMA transfer is finished, HAL_HASH_MD5_Finish() API must
  568. * be called to retrieve the computed digest.
  569. * @param hhash: HASH handle.
  570. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  571. * @param Size: length of the input buffer in bytes.
  572. * @retval HAL status
  573. */
  574. HAL_StatusTypeDef HAL_HASH_MD5_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  575. {
  576. return HASH_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_MD5);
  577. }
  578. /**
  579. * @brief Return the computed digest in MD5 mode.
  580. * @note The API waits for DCIS to be set then reads the computed digest.
  581. * @note HAL_HASH_MD5_Finish() can be used as well to retrieve the digest in
  582. * HMAC MD5 mode.
  583. * @param hhash: HASH handle.
  584. * @param pOutBuffer: pointer to the computed digest. Digest size is 16 bytes.
  585. * @param Timeout: Timeout value.
  586. * @retval HAL status
  587. */
  588. HAL_StatusTypeDef HAL_HASH_MD5_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout)
  589. {
  590. return HASH_Finish(hhash, pOutBuffer, Timeout);
  591. }
  592. /**
  593. * @brief Initialize the HASH peripheral in SHA1 mode then initiate a DMA transfer
  594. * to feed the input buffer to the IP.
  595. * @note Once the DMA transfer is finished, HAL_HASH_SHA1_Finish() API must
  596. * be called to retrieve the computed digest.
  597. * @param hhash: HASH handle.
  598. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  599. * @param Size: length of the input buffer in bytes.
  600. * @retval HAL status
  601. */
  602. HAL_StatusTypeDef HAL_HASH_SHA1_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  603. {
  604. return HASH_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_SHA1);
  605. }
  606. /**
  607. * @brief Return the computed digest in SHA1 mode.
  608. * @note The API waits for DCIS to be set then reads the computed digest.
  609. * @note HAL_HASH_SHA1_Finish() can be used as well to retrieve the digest in
  610. * HMAC SHA1 mode.
  611. * @param hhash: HASH handle.
  612. * @param pOutBuffer: pointer to the computed digest. Digest size is 20 bytes.
  613. * @param Timeout: Timeout value.
  614. * @retval HAL status
  615. */
  616. HAL_StatusTypeDef HAL_HASH_SHA1_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout)
  617. {
  618. return HASH_Finish(hhash, pOutBuffer, Timeout);
  619. }
  620. /**
  621. * @}
  622. */
  623. /** @defgroup HASH_Exported_Functions_Group5 HMAC processing functions in polling mode
  624. * @brief HMAC processing functions using polling mode.
  625. *
  626. @verbatim
  627. ===============================================================================
  628. ##### Polling mode HMAC processing functions #####
  629. ===============================================================================
  630. [..] This section provides functions allowing to calculate in polling mode
  631. the HMAC value using one of the following algorithms:
  632. (+) MD5
  633. (++) HAL_HMAC_MD5_Start()
  634. (+) SHA1
  635. (++) HAL_HMAC_SHA1_Start()
  636. @endverbatim
  637. * @{
  638. */
  639. /**
  640. * @brief Initialize the HASH peripheral in HMAC MD5 mode, next process pInBuffer then
  641. * read the computed digest.
  642. * @note Digest is available in pOutBuffer.
  643. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  644. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  645. * @param hhash: HASH handle.
  646. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  647. * @param Size: length of the input buffer in bytes.
  648. * @param pOutBuffer: pointer to the computed digest. Digest size is 16 bytes.
  649. * @param Timeout: Timeout value.
  650. * @retval HAL status
  651. */
  652. HAL_StatusTypeDef HAL_HMAC_MD5_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout)
  653. {
  654. return HMAC_Start(hhash, pInBuffer, Size, pOutBuffer, Timeout, HASH_ALGOSELECTION_MD5);
  655. }
  656. /**
  657. * @brief Initialize the HASH peripheral in HMAC SHA1 mode, next process pInBuffer then
  658. * read the computed digest.
  659. * @note Digest is available in pOutBuffer.
  660. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  661. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  662. * @param hhash: HASH handle.
  663. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  664. * @param Size: length of the input buffer in bytes.
  665. * @param pOutBuffer: pointer to the computed digest. Digest size is 20 bytes.
  666. * @param Timeout: Timeout value.
  667. * @retval HAL status
  668. */
  669. HAL_StatusTypeDef HAL_HMAC_SHA1_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout)
  670. {
  671. return HMAC_Start(hhash, pInBuffer, Size, pOutBuffer, Timeout, HASH_ALGOSELECTION_SHA1);
  672. }
  673. /**
  674. * @}
  675. */
  676. /** @defgroup HASH_Exported_Functions_Group6 HMAC processing functions in interrupt mode
  677. * @brief HMAC processing functions using interrupt mode.
  678. *
  679. @verbatim
  680. ===============================================================================
  681. ##### Interrupt mode HMAC processing functions #####
  682. ===============================================================================
  683. [..] This section provides functions allowing to calculate in interrupt mode
  684. the HMAC value using one of the following algorithms:
  685. (+) MD5
  686. (++) HAL_HMAC_MD5_Start_IT()
  687. (+) SHA1
  688. (++) HAL_HMAC_SHA1_Start_IT()
  689. @endverbatim
  690. * @{
  691. */
  692. /**
  693. * @brief Initialize the HASH peripheral in HMAC MD5 mode, next process pInBuffer then
  694. * read the computed digest in interrupt mode.
  695. * @note Digest is available in pOutBuffer.
  696. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  697. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  698. * @param hhash: HASH handle.
  699. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  700. * @param Size: length of the input buffer in bytes.
  701. * @param pOutBuffer: pointer to the computed digest. Digest size is 16 bytes.
  702. * @retval HAL status
  703. */
  704. HAL_StatusTypeDef HAL_HMAC_MD5_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer)
  705. {
  706. return HMAC_Start_IT(hhash, pInBuffer, Size, pOutBuffer, HASH_ALGOSELECTION_MD5);
  707. }
  708. /**
  709. * @brief Initialize the HASH peripheral in HMAC SHA1 mode, next process pInBuffer then
  710. * read the computed digest in interrupt mode.
  711. * @note Digest is available in pOutBuffer.
  712. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  713. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  714. * @param hhash: HASH handle.
  715. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  716. * @param Size: length of the input buffer in bytes.
  717. * @param pOutBuffer: pointer to the computed digest. Digest size is 20 bytes.
  718. * @retval HAL status
  719. */
  720. HAL_StatusTypeDef HAL_HMAC_SHA1_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer)
  721. {
  722. return HMAC_Start_IT(hhash, pInBuffer, Size, pOutBuffer, HASH_ALGOSELECTION_SHA1);
  723. }
  724. /**
  725. * @}
  726. */
  727. /** @defgroup HASH_Exported_Functions_Group7 HMAC processing functions in DMA mode
  728. * @brief HMAC processing functions using DMA modes.
  729. *
  730. @verbatim
  731. ===============================================================================
  732. ##### DMA mode HMAC processing functions #####
  733. ===============================================================================
  734. [..] This section provides functions allowing to calculate in DMA mode
  735. the HMAC value using one of the following algorithms:
  736. (+) MD5
  737. (++) HAL_HMAC_MD5_Start_DMA()
  738. (+) SHA1
  739. (++) HAL_HMAC_SHA1_Start_DMA()
  740. [..] When resorting to DMA mode to enter the data in the IP for HMAC processing,
  741. user must resort to HAL_HMAC_xxx_Start_DMA() then read the resulting digest
  742. with HAL_HASH_xxx_Finish().
  743. @endverbatim
  744. * @{
  745. */
  746. /**
  747. * @brief Initialize the HASH peripheral in HMAC MD5 mode then initiate the required
  748. * DMA transfers to feed the key and the input buffer to the IP.
  749. * @note Once the DMA transfers are finished (indicated by hhash->State set back
  750. * to HAL_HASH_STATE_READY), HAL_HASH_MD5_Finish() API must be called to retrieve
  751. * the computed digest.
  752. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  753. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  754. * @note If MDMAT bit is set before calling this function (multi-buffer
  755. * HASH processing case), the input buffer size (in bytes) must be
  756. * a multiple of 4 otherwise, the HASH digest computation is corrupted.
  757. * For the processing of the last buffer of the thread, MDMAT bit must
  758. * be reset and the buffer length (in bytes) doesn't have to be a
  759. * multiple of 4.
  760. * @param hhash: HASH handle.
  761. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  762. * @param Size: length of the input buffer in bytes.
  763. * @retval HAL status
  764. */
  765. HAL_StatusTypeDef HAL_HMAC_MD5_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  766. {
  767. return HMAC_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_MD5);
  768. }
  769. /**
  770. * @brief Initialize the HASH peripheral in HMAC SHA1 mode then initiate the required
  771. * DMA transfers to feed the key and the input buffer to the IP.
  772. * @note Once the DMA transfers are finished (indicated by hhash->State set back
  773. * to HAL_HASH_STATE_READY), HAL_HASH_SHA1_Finish() API must be called to retrieve
  774. * the computed digest.
  775. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  776. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  777. * @note If MDMAT bit is set before calling this function (multi-buffer
  778. * HASH processing case), the input buffer size (in bytes) must be
  779. * a multiple of 4 otherwise, the HASH digest computation is corrupted.
  780. * For the processing of the last buffer of the thread, MDMAT bit must
  781. * be reset and the buffer length (in bytes) doesn't have to be a
  782. * multiple of 4.
  783. * @param hhash: HASH handle.
  784. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  785. * @param Size: length of the input buffer in bytes.
  786. * @retval HAL status
  787. */
  788. HAL_StatusTypeDef HAL_HMAC_SHA1_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  789. {
  790. return HMAC_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_SHA1);
  791. }
  792. /**
  793. * @}
  794. */
  795. /** @defgroup HASH_Exported_Functions_Group8 Peripheral states functions
  796. * @brief Peripheral State functions.
  797. *
  798. @verbatim
  799. ===============================================================================
  800. ##### Peripheral State methods #####
  801. ===============================================================================
  802. [..]
  803. This section permits to get in run-time the state and the peripheral handle
  804. status of the peripheral:
  805. (+) HAL_HASH_GetState()
  806. (+) HAL_HASH_GetStatus()
  807. [..]
  808. Additionally, this subsection provides functions allowing to save and restore
  809. the HASH or HMAC processing context in case of calculation suspension:
  810. (+) HAL_HASH_ContextSaving()
  811. (+) HAL_HASH_ContextRestoring()
  812. [..]
  813. This subsection provides functions allowing to suspend the HASH processing
  814. (+) when input are fed to the IP by software
  815. (++) HAL_HASH_SwFeed_ProcessSuspend()
  816. (+) when input are fed to the IP by DMA
  817. (++) HAL_HASH_DMAFeed_ProcessSuspend()
  818. @endverbatim
  819. * @{
  820. */
  821. /**
  822. * @brief Return the HASH handle state.
  823. * @note The API yields the current state of the handle (BUSY, READY,...).
  824. * @param hhash: HASH handle.
  825. * @retval HAL HASH state
  826. */
  827. HAL_HASH_StateTypeDef HAL_HASH_GetState(HASH_HandleTypeDef *hhash)
  828. {
  829. return hhash->State;
  830. }
  831. /**
  832. * @brief Return the HASH HAL status.
  833. * @note The API yields the HAL status of the handle: it is the result of the
  834. * latest HASH processing and allows to report any issue (e.g. HAL_TIMEOUT).
  835. * @param hhash: HASH handle.
  836. * @retval HAL status
  837. */
  838. HAL_StatusTypeDef HAL_HASH_GetStatus(HASH_HandleTypeDef *hhash)
  839. {
  840. return hhash->Status;
  841. }
  842. /**
  843. * @brief Save the HASH context in case of processing suspension.
  844. * @param hhash: HASH handle.
  845. * @param pMemBuffer: pointer to the memory buffer where the HASH context
  846. * is saved.
  847. * @note The IMR, STR, CR then all the CSR registers are saved
  848. * in that order. Only the r/w bits are read to be restored later on.
  849. * @note By default, all the context swap registers (there are
  850. * HASH_NUMBER_OF_CSR_REGISTERS of those) are saved.
  851. * @note pMemBuffer points to a buffer allocated by the user. The buffer size
  852. * must be at least (HASH_NUMBER_OF_CSR_REGISTERS + 3) * 4 uint8 long.
  853. * @retval None
  854. */
  855. void HAL_HASH_ContextSaving(HASH_HandleTypeDef *hhash, uint8_t* pMemBuffer)
  856. {
  857. uint32_t mem_ptr = (uint32_t)pMemBuffer;
  858. uint32_t csr_ptr = (uint32_t)HASH->CSR;
  859. uint32_t i = 0;
  860. /* Prevent unused argument(s) compilation warning */
  861. UNUSED(hhash);
  862. /* Save IMR register content */
  863. *(uint32_t*)(mem_ptr) = READ_BIT(HASH->IMR,HASH_IT_DINI|HASH_IT_DCI);
  864. mem_ptr+=4;
  865. /* Save STR register content */
  866. *(uint32_t*)(mem_ptr) = READ_BIT(HASH->STR,HASH_STR_NBLW);
  867. mem_ptr+=4;
  868. /* Save CR register content */
  869. *(uint32_t*)(mem_ptr) = READ_BIT(HASH->CR,HASH_CR_DMAE|HASH_CR_DATATYPE|HASH_CR_MODE|HASH_CR_ALGO|HASH_CR_LKEY|HASH_CR_MDMAT);
  870. mem_ptr+=4;
  871. /* By default, save all CSRs registers */
  872. for (i = HASH_NUMBER_OF_CSR_REGISTERS; i >0; i--)
  873. {
  874. *(uint32_t*)(mem_ptr) = *(uint32_t*)(csr_ptr);
  875. mem_ptr+=4;
  876. csr_ptr+=4;
  877. }
  878. }
  879. /**
  880. * @brief Restore the HASH context in case of processing resumption.
  881. * @param hhash: HASH handle.
  882. * @param pMemBuffer: pointer to the memory buffer where the HASH context
  883. * is stored.
  884. * @note The IMR, STR, CR then all the CSR registers are restored
  885. * in that order. Only the r/w bits are restored.
  886. * @note By default, all the context swap registers (HASH_NUMBER_OF_CSR_REGISTERS
  887. * of those) are restored (all of them have been saved by default
  888. * beforehand).
  889. * @retval None
  890. */
  891. void HAL_HASH_ContextRestoring(HASH_HandleTypeDef *hhash, uint8_t* pMemBuffer)
  892. {
  893. uint32_t mem_ptr = (uint32_t)pMemBuffer;
  894. uint32_t csr_ptr = (uint32_t)HASH->CSR;
  895. uint32_t i = 0;
  896. /* Prevent unused argument(s) compilation warning */
  897. UNUSED(hhash);
  898. /* Restore IMR register content */
  899. WRITE_REG(HASH->IMR, (*(uint32_t*)(mem_ptr)));
  900. mem_ptr+=4;
  901. /* Restore STR register content */
  902. WRITE_REG(HASH->STR, (*(uint32_t*)(mem_ptr)));
  903. mem_ptr+=4;
  904. /* Restore CR register content */
  905. WRITE_REG(HASH->CR, (*(uint32_t*)(mem_ptr)));
  906. mem_ptr+=4;
  907. /* Reset the HASH processor before restoring the Context
  908. Swap Registers (CSR) */
  909. __HAL_HASH_INIT();
  910. /* By default, restore all CSR registers */
  911. for (i = HASH_NUMBER_OF_CSR_REGISTERS; i >0; i--)
  912. {
  913. WRITE_REG((*(uint32_t*)(csr_ptr)), (*(uint32_t*)(mem_ptr)));
  914. mem_ptr+=4;
  915. csr_ptr+=4;
  916. }
  917. }
  918. /**
  919. * @brief Initiate HASH processing suspension when in polling or interruption mode.
  920. * @param hhash: HASH handle.
  921. * @note Set the handle field SuspendRequest to the appropriate value so that
  922. * the on-going HASH processing is suspended as soon as the required
  923. * conditions are met. Note that the actual suspension is carried out
  924. * by the functions HASH_WriteData() in polling mode and HASH_IT() in
  925. * interruption mode.
  926. * @retval None
  927. */
  928. void HAL_HASH_SwFeed_ProcessSuspend(HASH_HandleTypeDef *hhash)
  929. {
  930. /* Set Handle Suspend Request field */
  931. hhash->SuspendRequest = HAL_HASH_SUSPEND;
  932. }
  933. /**
  934. * @brief Suspend the HASH processing when in DMA mode.
  935. * @param hhash: HASH handle.
  936. * @note When suspension attempt occurs at the very end of a DMA transfer and
  937. * all the data have already been entered in the IP, hhash->State is
  938. * set to HAL_HASH_STATE_READY and the API returns HAL_ERROR. It is
  939. * recommended to wrap-up the processing in reading the digest as usual.
  940. * @retval HAL status
  941. */
  942. HAL_StatusTypeDef HAL_HASH_DMAFeed_ProcessSuspend(HASH_HandleTypeDef *hhash)
  943. {
  944. uint32_t tmp_remaining_DMATransferSize_inWords = 0x0;
  945. uint32_t tmp_initial_DMATransferSize_inWords = 0x0;
  946. uint32_t tmp_words_already_pushed = 0x0;
  947. if (hhash->State == HAL_HASH_STATE_READY)
  948. {
  949. return HAL_ERROR;
  950. }
  951. else
  952. {
  953. /* Make sure there is enough time to suspend the processing */
  954. tmp_remaining_DMATransferSize_inWords = hhash->hdmain->Instance->CNDTR;
  955. if (tmp_remaining_DMATransferSize_inWords <= HASH_DMA_SUSPENSION_WORDS_LIMIT)
  956. {
  957. /* No suspension attempted since almost to the end of the transferred data. */
  958. /* Best option for user code is to wrap up low priority message hashing */
  959. return HAL_ERROR;
  960. }
  961. /* Wait for DMAS to be reset */
  962. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_BUSY, SET, HASH_TIMEOUTVALUE) != HAL_OK)
  963. {
  964. return HAL_TIMEOUT;
  965. }
  966. if (__HAL_HASH_GET_FLAG(HASH_FLAG_DCIS) != RESET)
  967. {
  968. return HAL_ERROR;
  969. }
  970. /* Wait for DMAS to be set */
  971. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_BUSY, RESET, HASH_TIMEOUTVALUE) != HAL_OK)
  972. {
  973. return HAL_TIMEOUT;
  974. }
  975. /* Disable DMA channel */
  976. HAL_DMA_Abort(hhash->hdmain);
  977. /* Clear DMAE bit */
  978. CLEAR_BIT(HASH->CR,HASH_CR_DMAE);
  979. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_BUSY, SET, HASH_TIMEOUTVALUE) != HAL_OK)
  980. {
  981. return HAL_TIMEOUT;
  982. }
  983. if (__HAL_HASH_GET_FLAG(HASH_FLAG_DCIS) != RESET)
  984. {
  985. return HAL_ERROR;
  986. }
  987. /* At this point, DMA interface is disabled and no transfer is on-going */
  988. /* Retrieve from the DMA handle how many words remain to be written */
  989. tmp_remaining_DMATransferSize_inWords = hhash->hdmain->Instance->CNDTR;
  990. if (tmp_remaining_DMATransferSize_inWords == 0)
  991. {
  992. /* All the DMA transfer is actually done. Suspension occurred at the very end
  993. of the transfer. Either the digest computation is about to start (HASH case)
  994. or processing is about to move from one step to another (HMAC case).
  995. In both cases, the processing can't be suspended at this point. It is
  996. safer to
  997. - retrieve the low priority block digest before starting the high
  998. priority block processing (HASH case)
  999. - re-attempt a new suspension (HMAC case)
  1000. */
  1001. return HAL_ERROR;
  1002. }
  1003. else
  1004. {
  1005. /* Compute how many words were supposed to be transferred by DMA */
  1006. tmp_initial_DMATransferSize_inWords = (hhash->HashInCount%4 ? (hhash->HashInCount+3)/4: hhash->HashInCount/4);
  1007. /* If discrepancy between the number of words reported by DMA IP and the numbers of words entered as reported
  1008. by HASH IP, correct it */
  1009. /* tmp_words_already_pushed reflects the number of words that were already pushed before
  1010. the start of DMA transfer (multi-buffer processing case) */
  1011. tmp_words_already_pushed = hhash->NbWordsAlreadyPushed;
  1012. if ((tmp_words_already_pushed + tmp_initial_DMATransferSize_inWords - tmp_remaining_DMATransferSize_inWords) %16 != HASH_NBW_PUSHED())
  1013. {
  1014. tmp_remaining_DMATransferSize_inWords--; /* one less word to be transferred again */
  1015. }
  1016. /* Accordingly, update the input pointer that points at the next word to be transferred to the IP by DMA */
  1017. hhash->pHashInBuffPtr += 4 * (tmp_initial_DMATransferSize_inWords - tmp_remaining_DMATransferSize_inWords) ;
  1018. /* And store in HashInCount the remaining size to transfer (in bytes) */
  1019. hhash->HashInCount = 4 * tmp_remaining_DMATransferSize_inWords;
  1020. }
  1021. /* Set State as suspended */
  1022. hhash->State = HAL_HASH_STATE_SUSPENDED;
  1023. return HAL_OK;
  1024. }
  1025. }
  1026. /**
  1027. * @}
  1028. */
  1029. /**
  1030. * @}
  1031. */
  1032. /** @defgroup HASH_Private_Functions HASH Private Functions
  1033. * @{
  1034. */
  1035. /**
  1036. * @brief DMA HASH Input Data transfer completion callback.
  1037. * @param hdma: DMA handle.
  1038. * @note In case of HMAC processing, HASH_DMAXferCplt() initiates
  1039. * the next DMA transfer for the following HMAC step.
  1040. * @retval None
  1041. */
  1042. static void HASH_DMAXferCplt(DMA_HandleTypeDef *hdma)
  1043. {
  1044. HASH_HandleTypeDef* hhash = ( HASH_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1045. uint32_t inputaddr = 0x0;
  1046. uint32_t buffersize = 0x0;
  1047. if (hhash->State != HAL_HASH_STATE_SUSPENDED)
  1048. {
  1049. /* Disable the DMA transfer */
  1050. CLEAR_BIT(HASH->CR, HASH_CR_DMAE);
  1051. if (READ_BIT(HASH->CR, HASH_CR_MODE) == RESET)
  1052. {
  1053. /* If no HMAC processing, input data transfer is now over */
  1054. /* Change the HASH state to ready */
  1055. hhash->State = HAL_HASH_STATE_READY;
  1056. /* Call Input data transfer complete call back */
  1057. HAL_HASH_InCpltCallback(hhash);
  1058. }
  1059. else
  1060. {
  1061. /* HMAC processing: depending on the current HMAC step and whether or
  1062. not multi-buffer processing is on-going, the next step is initiated
  1063. and MDMAT bit is set. */
  1064. if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_3)
  1065. {
  1066. /* This is the end of HMAC processing */
  1067. /* Change the HASH state to ready */
  1068. hhash->State = HAL_HASH_STATE_READY;
  1069. /* Call Input data transfer complete call back
  1070. (note that the last DMA transfer was that of the key
  1071. for the outer HASH operation). */
  1072. HAL_HASH_InCpltCallback(hhash);
  1073. return;
  1074. }
  1075. else if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_1)
  1076. {
  1077. inputaddr = (uint32_t)hhash->pHashMsgBuffPtr; /* DMA transfer start address */
  1078. buffersize = hhash->HashBuffSize; /* DMA transfer size (in bytes) */
  1079. hhash->Phase = HAL_HASH_PHASE_HMAC_STEP_2; /* Move phase from Step 1 to Step 2 */
  1080. /* In case of suspension request, save the new starting parameters */
  1081. hhash->HashInCount = hhash->HashBuffSize; /* Initial DMA transfer size (in bytes) */
  1082. hhash->pHashInBuffPtr = hhash->pHashMsgBuffPtr ; /* DMA transfer start address */
  1083. hhash->NbWordsAlreadyPushed = 0; /* Reset number of words already pushed */
  1084. /* Check whether or not digest calculation must be disabled (in case of multi-buffer HMAC processing) */
  1085. if (hhash->DigestCalculationDisable != RESET)
  1086. {
  1087. /* Digest calculation is disabled: Step 2 must start with MDMAT bit set,
  1088. no digest calculation will be triggered at the end of the input buffer feeding to the IP */
  1089. __HAL_HASH_SET_MDMAT();
  1090. }
  1091. }
  1092. else if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_2)
  1093. {
  1094. if (hhash->DigestCalculationDisable != RESET)
  1095. {
  1096. /* No automatic move to Step 3 as a new message buffer will be fed to the IP
  1097. (case of multi-buffer HMAC processing):
  1098. DCAL must not be set.
  1099. Phase remains in Step 2, MDMAT remains set at this point.
  1100. Change the HASH state to ready and call Input data transfer complete call back. */
  1101. hhash->State = HAL_HASH_STATE_READY;
  1102. HAL_HASH_InCpltCallback(hhash);
  1103. return ;
  1104. }
  1105. else
  1106. {
  1107. /* Digest calculation is not disabled (case of single buffer input or last buffer
  1108. of multi-buffer HMAC processing) */
  1109. inputaddr = (uint32_t)hhash->Init.pKey; /* DMA transfer start address */
  1110. buffersize = hhash->Init.KeySize; /* DMA transfer size (in bytes) */
  1111. hhash->Phase = HAL_HASH_PHASE_HMAC_STEP_3; /* Move phase from Step 2 to Step 3 */
  1112. /* In case of suspension request, save the new starting parameters */
  1113. hhash->HashInCount = hhash->Init.KeySize; /* Initial size for second DMA transfer (input data) */
  1114. hhash->pHashInBuffPtr = hhash->Init.pKey ; /* address passed to DMA, now entering data message */
  1115. hhash->NbWordsAlreadyPushed = 0; /* Reset number of words already pushed */
  1116. }
  1117. }
  1118. /* Configure the Number of valid bits in last word of the message */
  1119. __HAL_HASH_SET_NBVALIDBITS(buffersize);
  1120. /* Set the HASH DMA transfert completion call back */
  1121. hhash->hdmain->XferCpltCallback = HASH_DMAXferCplt;
  1122. /* Enable the DMA In DMA Stream */
  1123. HAL_DMA_Start_IT(hhash->hdmain, inputaddr, (uint32_t)&HASH->DIN, (buffersize%4 ? (buffersize+3)/4:buffersize/4));
  1124. /* Enable DMA requests */
  1125. SET_BIT(HASH->CR, HASH_CR_DMAE);
  1126. }
  1127. }
  1128. return;
  1129. }
  1130. /**
  1131. * @brief DMA HASH communication error callback.
  1132. * @param hdma: DMA handle.
  1133. * @note HASH_DMAError() callback invokes HAL_HASH_ErrorCallback() that
  1134. * can contain user code to manage the error.
  1135. * @retval None
  1136. */
  1137. static void HASH_DMAError(DMA_HandleTypeDef *hdma)
  1138. {
  1139. HASH_HandleTypeDef* hhash = ( HASH_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1140. if (hhash->State != HAL_HASH_STATE_SUSPENDED)
  1141. {
  1142. /* Set HASH state to ready to prevent any blocking issue in user code
  1143. present in HAL_HASH_ErrorCallback() */
  1144. hhash->State= HAL_HASH_STATE_READY;
  1145. /* Set HASH handle status to error */
  1146. hhash->Status = HAL_ERROR;
  1147. HAL_HASH_ErrorCallback(hhash);
  1148. /* After error handling by code user, reset HASH handle HAL status */
  1149. hhash->Status = HAL_OK;
  1150. }
  1151. }
  1152. /**
  1153. * @brief Feed the input buffer to the HASH IP.
  1154. * @param hhash: HASH handle.
  1155. * @param pInBuffer: pointer to input buffer.
  1156. * @param Size: the size of input buffer in bytes.
  1157. * @note HASH_WriteData() regularly reads hhash->SuspendRequest to check whether
  1158. * or not the HASH processing must be suspended. If this is the case, the
  1159. * processing is suspended when possible and the IP feeding point reached at
  1160. * suspension time is stored in the handle for resumption later on.
  1161. * @retval HAL status
  1162. */
  1163. static HAL_StatusTypeDef HASH_WriteData(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  1164. {
  1165. uint32_t buffercounter;
  1166. __IO uint32_t inputaddr = (uint32_t) pInBuffer;
  1167. for(buffercounter = 0; buffercounter < Size; buffercounter+=4)
  1168. {
  1169. /* Write input data 4 bytes at a time */
  1170. HASH->DIN = *(uint32_t*)inputaddr;
  1171. inputaddr+=4;
  1172. /* If the suspension flag has been raised and if the processing is not about
  1173. to end, suspend processing */
  1174. if ((hhash->SuspendRequest == HAL_HASH_SUSPEND) && ((buffercounter+4) < Size))
  1175. {
  1176. /* Wait for DINIS = 1, which occurs when 16 32-bit locations are free
  1177. in the input buffer */
  1178. if (__HAL_HASH_GET_FLAG(HASH_FLAG_DINIS))
  1179. {
  1180. /* Reset SuspendRequest */
  1181. hhash->SuspendRequest = HAL_HASH_SUSPEND_NONE;
  1182. /* Depending whether the key or the input data were fed to the IP, the feeding point
  1183. reached at suspension time is not saved in the same handle fields */
  1184. if ((hhash->Phase == HAL_HASH_PHASE_PROCESS) || (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_2))
  1185. {
  1186. /* Save current reading and writing locations of Input and Output buffers */
  1187. hhash->pHashInBuffPtr = (uint8_t *)inputaddr;
  1188. /* Save the number of bytes that remain to be processed at this point */
  1189. hhash->HashInCount = Size - (buffercounter + 4);
  1190. }
  1191. else if ((hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_1) || (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_3))
  1192. {
  1193. /* Save current reading and writing locations of Input and Output buffers */
  1194. hhash->pHashKeyBuffPtr = (uint8_t *)inputaddr;
  1195. /* Save the number of bytes that remain to be processed at this point */
  1196. hhash->HashKeyCount = Size - (buffercounter + 4);
  1197. }
  1198. else
  1199. {
  1200. /* Unexpected phase: unlock process and report error */
  1201. hhash->State = HAL_HASH_STATE_READY;
  1202. __HAL_UNLOCK(hhash);
  1203. return HAL_ERROR;
  1204. }
  1205. /* Set the HASH state to Suspended and exit to stop entering data */
  1206. hhash->State = HAL_HASH_STATE_SUSPENDED;
  1207. return HAL_OK;
  1208. } /* if (__HAL_HASH_GET_FLAG(HASH_FLAG_DINIS)) */
  1209. } /* if ((hhash->SuspendRequest == HAL_HASH_SUSPEND) && ((buffercounter+4) < Size)) */
  1210. } /* for(buffercounter = 0; buffercounter < Size; buffercounter+=4) */
  1211. /* At this point, all the data have been entered to the IP: exit */
  1212. return HAL_OK;
  1213. }
  1214. /**
  1215. * @brief Retrieve the message digest.
  1216. * @param pMsgDigest: pointer to the computed digest.
  1217. * @param Size: message digest size in bytes.
  1218. * @retval None
  1219. */
  1220. static void HASH_GetDigest(uint8_t *pMsgDigest, uint8_t Size)
  1221. {
  1222. uint32_t msgdigest = (uint32_t)pMsgDigest;
  1223. switch(Size)
  1224. {
  1225. /* Read the message digest */
  1226. case 16: /* MD5 */
  1227. *(uint32_t*)(msgdigest) = __REV(HASH->HR[0]);
  1228. msgdigest+=4;
  1229. *(uint32_t*)(msgdigest) = __REV(HASH->HR[1]);
  1230. msgdigest+=4;
  1231. *(uint32_t*)(msgdigest) = __REV(HASH->HR[2]);
  1232. msgdigest+=4;
  1233. *(uint32_t*)(msgdigest) = __REV(HASH->HR[3]);
  1234. break;
  1235. case 20: /* SHA1 */
  1236. *(uint32_t*)(msgdigest) = __REV(HASH->HR[0]);
  1237. msgdigest+=4;
  1238. *(uint32_t*)(msgdigest) = __REV(HASH->HR[1]);
  1239. msgdigest+=4;
  1240. *(uint32_t*)(msgdigest) = __REV(HASH->HR[2]);
  1241. msgdigest+=4;
  1242. *(uint32_t*)(msgdigest) = __REV(HASH->HR[3]);
  1243. msgdigest+=4;
  1244. *(uint32_t*)(msgdigest) = __REV(HASH->HR[4]);
  1245. break;
  1246. case 28: /* SHA224 */
  1247. *(uint32_t*)(msgdigest) = __REV(HASH->HR[0]);
  1248. msgdigest+=4;
  1249. *(uint32_t*)(msgdigest) = __REV(HASH->HR[1]);
  1250. msgdigest+=4;
  1251. *(uint32_t*)(msgdigest) = __REV(HASH->HR[2]);
  1252. msgdigest+=4;
  1253. *(uint32_t*)(msgdigest) = __REV(HASH->HR[3]);
  1254. msgdigest+=4;
  1255. *(uint32_t*)(msgdigest) = __REV(HASH->HR[4]);
  1256. msgdigest+=4;
  1257. *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[5]);
  1258. msgdigest+=4;
  1259. *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[6]);
  1260. break;
  1261. case 32: /* SHA256 */
  1262. *(uint32_t*)(msgdigest) = __REV(HASH->HR[0]);
  1263. msgdigest+=4;
  1264. *(uint32_t*)(msgdigest) = __REV(HASH->HR[1]);
  1265. msgdigest+=4;
  1266. *(uint32_t*)(msgdigest) = __REV(HASH->HR[2]);
  1267. msgdigest+=4;
  1268. *(uint32_t*)(msgdigest) = __REV(HASH->HR[3]);
  1269. msgdigest+=4;
  1270. *(uint32_t*)(msgdigest) = __REV(HASH->HR[4]);
  1271. msgdigest+=4;
  1272. *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[5]);
  1273. msgdigest+=4;
  1274. *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[6]);
  1275. msgdigest+=4;
  1276. *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[7]);
  1277. break;
  1278. default:
  1279. break;
  1280. }
  1281. }
  1282. /**
  1283. * @brief Handle HASH processing Timeout.
  1284. * @param hhash: HASH handle.
  1285. * @param Flag: specifies the HASH flag to check.
  1286. * @param Status: the Flag status (SET or RESET).
  1287. * @param Timeout: Timeout duration.
  1288. * @retval HAL status
  1289. */
  1290. static HAL_StatusTypeDef HASH_WaitOnFlagUntilTimeout(HASH_HandleTypeDef *hhash, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  1291. {
  1292. uint32_t tickstart = HAL_GetTick();
  1293. /* Wait until flag is set */
  1294. if(Status == RESET)
  1295. {
  1296. while(__HAL_HASH_GET_FLAG(Flag) == RESET)
  1297. {
  1298. /* Check for the Timeout */
  1299. if(Timeout != HAL_MAX_DELAY)
  1300. {
  1301. if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
  1302. {
  1303. /* Set State to Ready to be able to restart later on */
  1304. hhash->State = HAL_HASH_STATE_READY;
  1305. /* Store time out issue in handle status */
  1306. hhash->Status = HAL_TIMEOUT;
  1307. /* Process Unlocked */
  1308. __HAL_UNLOCK(hhash);
  1309. return HAL_TIMEOUT;
  1310. }
  1311. }
  1312. }
  1313. }
  1314. else
  1315. {
  1316. while(__HAL_HASH_GET_FLAG(Flag) != RESET)
  1317. {
  1318. /* Check for the Timeout */
  1319. if(Timeout != HAL_MAX_DELAY)
  1320. {
  1321. if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
  1322. {
  1323. /* Set State to Ready to be able to restart later on */
  1324. hhash->State = HAL_HASH_STATE_READY;
  1325. /* Store time out issue in handle status */
  1326. hhash->Status = HAL_TIMEOUT;
  1327. /* Process Unlocked */
  1328. __HAL_UNLOCK(hhash);
  1329. return HAL_TIMEOUT;
  1330. }
  1331. }
  1332. }
  1333. }
  1334. return HAL_OK;
  1335. }
  1336. /**
  1337. * @brief HASH processing in interruption mode.
  1338. * @param hhash: HASH handle.
  1339. * @note HASH_IT() regularly reads hhash->SuspendRequest to check whether
  1340. * or not the HASH processing must be suspended. If this is the case, the
  1341. * processing is suspended when possible and the IP feeding point reached at
  1342. * suspension time is stored in the handle for resumption later on.
  1343. * @retval HAL status
  1344. */
  1345. static HAL_StatusTypeDef HASH_IT(HASH_HandleTypeDef *hhash)
  1346. {
  1347. if (hhash->State == HAL_HASH_STATE_BUSY)
  1348. {
  1349. /* ITCounter must not be equal to 0 at this point. Report an error if this is the case. */
  1350. if(hhash->HashITCounter == 0)
  1351. {
  1352. /* Disable Interrupts */
  1353. __HAL_HASH_DISABLE_IT(HASH_IT_DINI|HASH_IT_DCI);
  1354. /* HASH state set back to Ready to prevent any issue in user code
  1355. present in HAL_HASH_ErrorCallback() */
  1356. hhash->State = HAL_HASH_STATE_READY;
  1357. return HAL_ERROR;
  1358. }
  1359. else if (hhash->HashITCounter == 1)
  1360. {
  1361. /* This is the first call to HASH_IT, the first input data are about to be
  1362. entered in the IP. A specific processing is carried out at this point to
  1363. start-up the processing. */
  1364. hhash->HashITCounter = 2;
  1365. }
  1366. else
  1367. {
  1368. /* Cruise speed reached, HashITCounter remains equal to 3 until the end of
  1369. the HASH processing or the end of the current step for HMAC processing. */
  1370. hhash->HashITCounter = 3;
  1371. }
  1372. /* If digest is ready */
  1373. if (__HAL_HASH_GET_FLAG(HASH_FLAG_DCIS))
  1374. {
  1375. /* Read the digest */
  1376. HASH_GetDigest(hhash->pHashOutBuffPtr, HASH_DIGEST_LENGTH());
  1377. /* Disable Interrupts */
  1378. __HAL_HASH_DISABLE_IT(HASH_IT_DINI|HASH_IT_DCI);
  1379. /* Change the HASH state */
  1380. hhash->State = HAL_HASH_STATE_READY;
  1381. /* Call digest computation complete call back */
  1382. HAL_HASH_DgstCpltCallback(hhash);
  1383. return HAL_OK;
  1384. }
  1385. /* If IP ready to accept new data */
  1386. if (__HAL_HASH_GET_FLAG(HASH_FLAG_DINIS))
  1387. {
  1388. /* If the suspension flag has been raised and if the processing is not about
  1389. to end, suspend processing */
  1390. if ((hhash->SuspendRequest == HAL_HASH_SUSPEND) && (hhash->HashInCount != 0))
  1391. {
  1392. /* Disable Interrupts */
  1393. __HAL_HASH_DISABLE_IT(HASH_IT_DINI|HASH_IT_DCI);
  1394. /* Reset SuspendRequest */
  1395. hhash->SuspendRequest = HAL_HASH_SUSPEND_NONE;
  1396. /* Change the HASH state */
  1397. hhash->State = HAL_HASH_STATE_SUSPENDED;
  1398. return HAL_OK;
  1399. }
  1400. /* Enter input data in the IP thru HASH_Write_Block_Data() call and
  1401. check whether the digest calculation has been triggered */
  1402. if (HASH_Write_Block_Data(hhash) == HASH_DIGEST_CALCULATION_STARTED)
  1403. {
  1404. /* Call Input data transfer complete call back
  1405. (called at the end of each step for HMAC) */
  1406. HAL_HASH_InCpltCallback(hhash);
  1407. if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_1)
  1408. {
  1409. /* Wait until IP is not busy anymore */
  1410. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_BUSY, SET, HASH_TIMEOUTVALUE) != HAL_OK)
  1411. {
  1412. /* Disable Interrupts */
  1413. __HAL_HASH_DISABLE_IT(HASH_IT_DINI|HASH_IT_DCI);
  1414. return HAL_TIMEOUT;
  1415. }
  1416. /* Initialization start for HMAC STEP 2 */
  1417. hhash->Phase = HAL_HASH_PHASE_HMAC_STEP_2; /* Move phase from Step 1 to Step 2 */
  1418. __HAL_HASH_SET_NBVALIDBITS(hhash->HashBuffSize); /* Set NBLW for the input message */
  1419. hhash->HashInCount = hhash->HashBuffSize; /* Set the input data size (in bytes) */
  1420. hhash->pHashInBuffPtr = hhash->pHashMsgBuffPtr; /* Set the input data address */
  1421. hhash->HashITCounter = 1; /* Set ITCounter to 1 to indicate the start of a new phase */
  1422. __HAL_HASH_ENABLE_IT(HASH_IT_DINI); /* Enable IT (was disabled in HASH_Write_Block_Data) */
  1423. }
  1424. else if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_2)
  1425. {
  1426. /* Wait until IP is not busy anymore */
  1427. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_BUSY, SET, HASH_TIMEOUTVALUE) != HAL_OK)
  1428. {
  1429. /* Disable Interrupts */
  1430. __HAL_HASH_DISABLE_IT(HASH_IT_DINI|HASH_IT_DCI);
  1431. return HAL_TIMEOUT;
  1432. }
  1433. /* Initialization start for HMAC STEP 3 */
  1434. hhash->Phase = HAL_HASH_PHASE_HMAC_STEP_3; /* Move phase from Step 2 to Step 3 */
  1435. __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize); /* Set NBLW for the key */
  1436. hhash->HashInCount = hhash->Init.KeySize; /* Set the key size (in bytes) */
  1437. hhash->pHashInBuffPtr = hhash->Init.pKey; /* Set the key address */
  1438. hhash->HashITCounter = 1; /* Set ITCounter to 1 to indicate the start of a new phase */
  1439. __HAL_HASH_ENABLE_IT(HASH_IT_DINI); /* Enable IT (was disabled in HASH_Write_Block_Data) */
  1440. }
  1441. } /* if (HASH_Write_Block_Data(hhash) == HASH_DIGEST_CALCULATION_STARTED) */
  1442. } /* if (__HAL_HASH_GET_FLAG(HASH_FLAG_DINIS))*/
  1443. /* Return function status */
  1444. return HAL_OK;
  1445. }
  1446. else
  1447. {
  1448. return HAL_BUSY;
  1449. }
  1450. }
  1451. /**
  1452. * @brief Write a block of data in HASH IP in interruption mode.
  1453. * @param hhash: HASH handle.
  1454. * @note HASH_Write_Block_Data() is called under interruption by HASH_IT().
  1455. * @retval HAL status
  1456. */
  1457. static uint32_t HASH_Write_Block_Data(HASH_HandleTypeDef *hhash)
  1458. {
  1459. uint32_t inputaddr;
  1460. uint32_t buffercounter;
  1461. uint32_t inputcounter;
  1462. uint32_t ret = HASH_DIGEST_CALCULATION_NOT_STARTED;
  1463. /* If there are more than 64 bytes remaining to be entered */
  1464. if(hhash->HashInCount > 64)
  1465. {
  1466. inputaddr = (uint32_t)hhash->pHashInBuffPtr;
  1467. /* Write the Input block in the Data IN register
  1468. (16 32-bit words, or 64 bytes are entered) */
  1469. for(buffercounter = 0; buffercounter < 64; buffercounter+=4)
  1470. {
  1471. HASH->DIN = *(uint32_t*)inputaddr;
  1472. inputaddr+=4;
  1473. }
  1474. /* If this is the start of input data entering, an additional word
  1475. must be entered to start up the HASH processing */
  1476. if(hhash->HashITCounter == 2)
  1477. {
  1478. HASH->DIN = *(uint32_t*)inputaddr;
  1479. inputaddr+=4;
  1480. if(hhash->HashInCount >= 68)
  1481. {
  1482. /* There are still data waiting to be entered in the IP.
  1483. Decrement buffer counter and set pointer to the proper
  1484. memory location for the next data entering round. */
  1485. hhash->HashInCount -= 68;
  1486. hhash->pHashInBuffPtr+= 68;
  1487. }
  1488. else
  1489. {
  1490. /* All the input buffer has been fed to the HW. */
  1491. hhash->HashInCount = 0;
  1492. }
  1493. }
  1494. else
  1495. {
  1496. /* 64 bytes have been entered and there are still some remaining:
  1497. Decrement buffer counter and set pointer to the proper
  1498. memory location for the next data entering round.*/
  1499. hhash->HashInCount -= 64;
  1500. hhash->pHashInBuffPtr+= 64;
  1501. }
  1502. }
  1503. else
  1504. {
  1505. /* 64 or less bytes remain to be entered. This is the last
  1506. data entering round. */
  1507. /* Get the buffer address */
  1508. inputaddr = (uint32_t)hhash->pHashInBuffPtr;
  1509. /* Get the buffer counter */
  1510. inputcounter = hhash->HashInCount;
  1511. /* Disable Interrupts */
  1512. __HAL_HASH_DISABLE_IT(HASH_IT_DINI);
  1513. /* Write the Input block in the Data IN register */
  1514. for(buffercounter = 0; buffercounter < (inputcounter+3)/4; buffercounter++)
  1515. {
  1516. HASH->DIN = *(uint32_t*)inputaddr;
  1517. inputaddr+=4;
  1518. }
  1519. /* Start the Digest calculation */
  1520. __HAL_HASH_START_DIGEST();
  1521. /* Return indication that digest calculation has started:
  1522. this return value triggers the call to Input data transfer
  1523. complete call back as well as the proper transition from
  1524. one step to another in HMAC mode. */
  1525. ret = HASH_DIGEST_CALCULATION_STARTED;
  1526. /* Reset buffer counter */
  1527. hhash->HashInCount = 0;
  1528. }
  1529. /* Return whether or digest calculation has started */
  1530. return ret;
  1531. }
  1532. /**
  1533. * @brief HMAC processing in polling mode.
  1534. * @param hhash: HASH handle.
  1535. * @param Timeout: Timeout value.
  1536. * @retval HAL status
  1537. */
  1538. static HAL_StatusTypeDef HMAC_Processing(HASH_HandleTypeDef *hhash, uint32_t Timeout)
  1539. {
  1540. /* Ensure first that Phase is correct */
  1541. if ((hhash->Phase != HAL_HASH_PHASE_HMAC_STEP_1) && (hhash->Phase != HAL_HASH_PHASE_HMAC_STEP_2) && (hhash->Phase != HAL_HASH_PHASE_HMAC_STEP_3))
  1542. {
  1543. /* Change the HASH state */
  1544. hhash->State = HAL_HASH_STATE_READY;
  1545. /* Process Unlock */
  1546. __HAL_UNLOCK(hhash);
  1547. /* Return function status */
  1548. return HAL_ERROR;
  1549. }
  1550. /* HMAC Step 1 processing */
  1551. if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_1)
  1552. {
  1553. /************************** STEP 1 ******************************************/
  1554. /* Configure the Number of valid bits in last word of the message */
  1555. __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize);
  1556. /* Write input buffer in Data register */
  1557. if ((hhash->Status = HASH_WriteData(hhash, hhash->pHashKeyBuffPtr, hhash->HashKeyCount)) != HAL_OK)
  1558. {
  1559. return hhash->Status;
  1560. }
  1561. /* Check whether or not key entering process has been suspended */
  1562. if (hhash->State == HAL_HASH_STATE_SUSPENDED)
  1563. {
  1564. /* Process Unlocked */
  1565. __HAL_UNLOCK(hhash);
  1566. /* Stop right there and return function status */
  1567. return HAL_OK;
  1568. }
  1569. /* No processing suspension at this point: set DCAL bit. */
  1570. __HAL_HASH_START_DIGEST();
  1571. /* Wait for BUSY flag to be cleared */
  1572. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_BUSY, SET, Timeout) != HAL_OK)
  1573. {
  1574. return HAL_TIMEOUT;
  1575. }
  1576. /* Move from Step 1 to Step 2 */
  1577. hhash->Phase = HAL_HASH_PHASE_HMAC_STEP_2;
  1578. }
  1579. /* HMAC Step 2 processing.
  1580. After phase check, HMAC_Processing() may
  1581. - directly start up from this point in resumption case
  1582. if the same Step 2 processing was suspended previously
  1583. - or fall through from the Step 1 processing carried out hereabove */
  1584. if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_2)
  1585. {
  1586. /************************** STEP 2 ******************************************/
  1587. /* Configure the Number of valid bits in last word of the message */
  1588. __HAL_HASH_SET_NBVALIDBITS(hhash->HashBuffSize);
  1589. /* Write input buffer in Data register */
  1590. if ((hhash->Status = HASH_WriteData(hhash, hhash->pHashInBuffPtr, hhash->HashInCount)) != HAL_OK)
  1591. {
  1592. return hhash->Status;
  1593. }
  1594. /* Check whether or not data entering process has been suspended */
  1595. if (hhash->State == HAL_HASH_STATE_SUSPENDED)
  1596. {
  1597. /* Process Unlocked */
  1598. __HAL_UNLOCK(hhash);
  1599. /* Stop right there and return function status */
  1600. return HAL_OK;
  1601. }
  1602. /* No processing suspension at this point: set DCAL bit. */
  1603. __HAL_HASH_START_DIGEST();
  1604. /* Wait for BUSY flag to be cleared */
  1605. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_BUSY, SET, Timeout) != HAL_OK)
  1606. {
  1607. return HAL_TIMEOUT;
  1608. }
  1609. /* Move from Step 2 to Step 3 */
  1610. hhash->Phase = HAL_HASH_PHASE_HMAC_STEP_3;
  1611. /* In case Step 1 phase was suspended then resumed,
  1612. set again Key input buffers and size before moving to
  1613. next step */
  1614. hhash->pHashKeyBuffPtr = hhash->Init.pKey;
  1615. hhash->HashKeyCount = hhash->Init.KeySize;
  1616. }
  1617. /* HMAC Step 3 processing.
  1618. After phase check, HMAC_Processing() may
  1619. - directly start up from this point in resumption case
  1620. if the same Step 3 processing was suspended previously
  1621. - or fall through from the Step 2 processing carried out hereabove */
  1622. if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_3)
  1623. {
  1624. /************************** STEP 3 ******************************************/
  1625. /* Configure the Number of valid bits in last word of the message */
  1626. __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize);
  1627. /* Write input buffer in Data register */
  1628. if ((hhash->Status = HASH_WriteData(hhash, hhash->pHashKeyBuffPtr, hhash->HashKeyCount)) != HAL_OK)
  1629. {
  1630. return hhash->Status;
  1631. }
  1632. /* Check whether or not key entering process has been suspended */
  1633. if (hhash->State == HAL_HASH_STATE_SUSPENDED)
  1634. {
  1635. /* Process Unlocked */
  1636. __HAL_UNLOCK(hhash);
  1637. /* Stop right there and return function status */
  1638. return HAL_OK;
  1639. }
  1640. /* No processing suspension at this point: start the Digest calculation. */
  1641. __HAL_HASH_START_DIGEST();
  1642. /* Wait for DCIS flag to be set */
  1643. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_DCIS, RESET, Timeout) != HAL_OK)
  1644. {
  1645. return HAL_TIMEOUT;
  1646. }
  1647. /* Read the message digest */
  1648. HASH_GetDigest(hhash->pHashOutBuffPtr, HASH_DIGEST_LENGTH());
  1649. }
  1650. /* Change the HASH state */
  1651. hhash->State = HAL_HASH_STATE_READY;
  1652. /* Process Unlock */
  1653. __HAL_UNLOCK(hhash);
  1654. /* Return function status */
  1655. return HAL_OK;
  1656. }
  1657. /**
  1658. * @brief Initialize the HASH peripheral, next process pInBuffer then
  1659. * read the computed digest.
  1660. * @note Digest is available in pOutBuffer.
  1661. * @param hhash: HASH handle.
  1662. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  1663. * @param Size: length of the input buffer in bytes.
  1664. * @param pOutBuffer: pointer to the computed digest.
  1665. * @param Timeout: Timeout value.
  1666. * @param Algorithm: HASH algorithm.
  1667. * @retval HAL status
  1668. */
  1669. HAL_StatusTypeDef HASH_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout, uint32_t Algorithm)
  1670. {
  1671. uint8_t *pInBuffer_tmp; /* input data address, input parameter of HASH_WriteData() */
  1672. uint32_t Size_tmp = 0x0; /* input data size (in bytes), input parameter of HASH_WriteData() */
  1673. /* Initiate HASH processing in case of start or resumption */
  1674. if((hhash->State == HAL_HASH_STATE_READY) || (hhash->State == HAL_HASH_STATE_SUSPENDED))
  1675. {
  1676. /* Check input parameters */
  1677. if ((pInBuffer == NULL) || (Size == 0) || (pOutBuffer == NULL))
  1678. {
  1679. hhash->State = HAL_HASH_STATE_READY;
  1680. return HAL_ERROR;
  1681. }
  1682. /* Process Locked */
  1683. __HAL_LOCK(hhash);
  1684. /* Check if initialization phase has not been already performed */
  1685. if(hhash->Phase == HAL_HASH_PHASE_READY)
  1686. {
  1687. /* Change the HASH state */
  1688. hhash->State = HAL_HASH_STATE_BUSY;
  1689. /* Select the HASH algorithm, clear HMAC mode and long key selection bit, reset the HASH processor core */
  1690. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_CR_INIT);
  1691. /* Configure the number of valid bits in last word of the message */
  1692. __HAL_HASH_SET_NBVALIDBITS(Size);
  1693. /* pInBuffer_tmp and Size_tmp are initialized to be used afterwards as
  1694. input parameters of HASH_WriteData() */
  1695. pInBuffer_tmp = pInBuffer; /* pInBuffer_tmp is set to the input data address */
  1696. Size_tmp = Size; /* Size_tmp contains the input data size in bytes */
  1697. /* Set the phase */
  1698. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  1699. }
  1700. else if (hhash->Phase == HAL_HASH_PHASE_PROCESS)
  1701. {
  1702. /* if the IP has already been initialized, two cases are possible */
  1703. /* Process resumption time ... */
  1704. if (hhash->State == HAL_HASH_STATE_SUSPENDED)
  1705. {
  1706. /* Since this is resumption, pInBuffer_tmp and Size_tmp are not set
  1707. to the API input parameters but to those saved beforehand by HASH_WriteData()
  1708. when the processing was suspended */
  1709. pInBuffer_tmp = hhash->pHashInBuffPtr;
  1710. Size_tmp = hhash->HashInCount;
  1711. }
  1712. /* ... or multi-buffer HASH processing end */
  1713. else
  1714. {
  1715. /* pInBuffer_tmp and Size_tmp are initialized to be used afterwards as
  1716. input parameters of HASH_WriteData() */
  1717. pInBuffer_tmp = pInBuffer;
  1718. Size_tmp = Size;
  1719. /* Configure the number of valid bits in last word of the message */
  1720. __HAL_HASH_SET_NBVALIDBITS(Size);
  1721. }
  1722. /* Change the HASH state */
  1723. hhash->State = HAL_HASH_STATE_BUSY;
  1724. }
  1725. else
  1726. {
  1727. /* Phase error */
  1728. hhash->State = HAL_HASH_STATE_READY;
  1729. /* Process Unlocked */
  1730. __HAL_UNLOCK(hhash);
  1731. /* Return function status */
  1732. return HAL_ERROR;
  1733. }
  1734. /* Write input buffer in Data register */
  1735. if ((hhash->Status = HASH_WriteData(hhash, pInBuffer_tmp, Size_tmp)) != HAL_OK)
  1736. {
  1737. return hhash->Status;
  1738. }
  1739. /* If the process has not been suspended, carry on to digest calculation */
  1740. if (hhash->State != HAL_HASH_STATE_SUSPENDED)
  1741. {
  1742. /* Start the Digest calculation */
  1743. __HAL_HASH_START_DIGEST();
  1744. /* Wait for DCIS flag to be set */
  1745. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_DCIS, RESET, Timeout) != HAL_OK)
  1746. {
  1747. return HAL_TIMEOUT;
  1748. }
  1749. /* Read the message digest */
  1750. HASH_GetDigest(pOutBuffer, HASH_DIGEST_LENGTH());
  1751. /* Change the HASH state */
  1752. hhash->State = HAL_HASH_STATE_READY;
  1753. }
  1754. /* Process Unlocked */
  1755. __HAL_UNLOCK(hhash);
  1756. /* Return function status */
  1757. return HAL_OK;
  1758. }
  1759. else
  1760. {
  1761. return HAL_BUSY;
  1762. }
  1763. }
  1764. /**
  1765. * @brief If not already done, initialize the HASH peripheral then
  1766. * processes pInBuffer.
  1767. * @note Field hhash->Phase of HASH handle is tested to check whether or not
  1768. * the IP has already been initialized.
  1769. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  1770. * HASH digest computation is corrupted.
  1771. * @param hhash: HASH handle.
  1772. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  1773. * @param Size: length of the input buffer in bytes, must be a multiple of 4.
  1774. * @param Algorithm: HASH algorithm.
  1775. * @retval HAL status
  1776. */
  1777. HAL_StatusTypeDef HASH_Accumulate(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint32_t Algorithm)
  1778. {
  1779. uint8_t *pInBuffer_tmp; /* input data address, input parameter of HASH_WriteData() */
  1780. uint32_t Size_tmp = 0x0; /* input data size (in bytes), input parameter of HASH_WriteData() */
  1781. /* Make sure the input buffer size (in bytes) is a multiple of 4 */
  1782. assert_param(IS_HASH_POLLING_MULTIBUFFER_SIZE(Size));
  1783. /* Initiate HASH processing in case of start or resumption */
  1784. if((hhash->State == HAL_HASH_STATE_READY) || (hhash->State == HAL_HASH_STATE_SUSPENDED))
  1785. {
  1786. /* Check input parameters */
  1787. if ((pInBuffer == NULL) || (Size == 0))
  1788. {
  1789. hhash->State = HAL_HASH_STATE_READY;
  1790. return HAL_ERROR;
  1791. }
  1792. /* Process Locked */
  1793. __HAL_LOCK(hhash);
  1794. /* If resuming the HASH processing */
  1795. if (hhash->State == HAL_HASH_STATE_SUSPENDED)
  1796. {
  1797. /* Change the HASH state */
  1798. hhash->State = HAL_HASH_STATE_BUSY;
  1799. /* Since this is resumption, pInBuffer_tmp and Size_tmp are not set
  1800. to the API input parameters but to those saved beforehand by HASH_WriteData()
  1801. when the processing was suspended */
  1802. pInBuffer_tmp = hhash->pHashInBuffPtr; /* pInBuffer_tmp is set to the input data address */
  1803. Size_tmp = hhash->HashInCount; /* Size_tmp contains the input data size in bytes */
  1804. }
  1805. else
  1806. {
  1807. /* Change the HASH state */
  1808. hhash->State = HAL_HASH_STATE_BUSY;
  1809. /* pInBuffer_tmp and Size_tmp are initialized to be used afterwards as
  1810. input parameters of HASH_WriteData() */
  1811. pInBuffer_tmp = pInBuffer; /* pInBuffer_tmp is set to the input data address */
  1812. Size_tmp = Size; /* Size_tmp contains the input data size in bytes */
  1813. /* Check if initialization phase has already be performed */
  1814. if(hhash->Phase == HAL_HASH_PHASE_READY)
  1815. {
  1816. /* Select the HASH algorithm, clear HMAC mode and long key selection bit, reset the HASH processor core */
  1817. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_CR_INIT);
  1818. }
  1819. /* Set the phase */
  1820. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  1821. }
  1822. /* Write input buffer in Data register */
  1823. if ((hhash->Status = HASH_WriteData(hhash, pInBuffer_tmp, Size_tmp)) != HAL_OK)
  1824. {
  1825. return hhash->Status;
  1826. }
  1827. /* If the process has not been suspended, move the state to Ready */
  1828. if (hhash->State != HAL_HASH_STATE_SUSPENDED)
  1829. {
  1830. /* Change the HASH state */
  1831. hhash->State = HAL_HASH_STATE_READY;
  1832. }
  1833. /* Process Unlocked */
  1834. __HAL_UNLOCK(hhash);
  1835. /* Return function status */
  1836. return HAL_OK;
  1837. }
  1838. else
  1839. {
  1840. return HAL_BUSY;
  1841. }
  1842. }
  1843. /**
  1844. * @brief Initialize the HASH peripheral, next process pInBuffer then
  1845. * read the computed digest in interruption mode.
  1846. * @note Digest is available in pOutBuffer.
  1847. * @param hhash: HASH handle.
  1848. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  1849. * @param Size: length of the input buffer in bytes.
  1850. * @param pOutBuffer: pointer to the computed digest.
  1851. * @param Algorithm: HASH algorithm.
  1852. * @retval HAL status
  1853. */
  1854. HAL_StatusTypeDef HASH_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Algorithm)
  1855. {
  1856. /* If State is ready or suspended, start or resume IT-based HASH processing */
  1857. if((hhash->State == HAL_HASH_STATE_READY) || (hhash->State == HAL_HASH_STATE_SUSPENDED))
  1858. {
  1859. /* Check input parameters */
  1860. if ((pInBuffer == NULL) || (Size == 0) || (pOutBuffer == NULL))
  1861. {
  1862. hhash->State = HAL_HASH_STATE_READY;
  1863. return HAL_ERROR;
  1864. }
  1865. /* Process Locked */
  1866. __HAL_LOCK(hhash);
  1867. /* Change the HASH state */
  1868. hhash->State = HAL_HASH_STATE_BUSY;
  1869. /* Initialize IT counter */
  1870. hhash->HashITCounter = 1;
  1871. /* Check if initialization phase has already be performed */
  1872. if(hhash->Phase == HAL_HASH_PHASE_READY)
  1873. {
  1874. /* Select the HASH algorithm, clear HMAC mode and long key selection bit, reset the HASH processor core */
  1875. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_CR_INIT);
  1876. /* Configure the number of valid bits in last word of the message */
  1877. __HAL_HASH_SET_NBVALIDBITS(Size);
  1878. hhash->HashInCount = Size; /* Counter used to keep track of number of data
  1879. to be fed to the IP */
  1880. hhash->pHashInBuffPtr = pInBuffer; /* Points at data which will be fed to the IP at
  1881. the next interruption */
  1882. /* In case of suspension, hhash->HashInCount and hhash->pHashInBuffPtr contain
  1883. the information describing where the HASH process is stopped.
  1884. These variables are used later on to resume the HASH processing at the
  1885. correct location. */
  1886. hhash->pHashOutBuffPtr = pOutBuffer; /* Points at the computed digest */
  1887. }
  1888. /* Set the phase */
  1889. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  1890. /* Process Unlock */
  1891. __HAL_UNLOCK(hhash);
  1892. /* Enable Interrupts */
  1893. __HAL_HASH_ENABLE_IT(HASH_IT_DINI|HASH_IT_DCI);
  1894. /* Return function status */
  1895. return HAL_OK;
  1896. }
  1897. else
  1898. {
  1899. return HAL_BUSY;
  1900. }
  1901. }
  1902. /**
  1903. * @brief Initialize the HASH peripheral then initiate a DMA transfer
  1904. * to feed the input buffer to the IP.
  1905. * @note If MDMAT bit is set before calling this function (multi-buffer
  1906. * HASH processing case), the input buffer size (in bytes) must be
  1907. * a multiple of 4 otherwise, the HASH digest computation is corrupted.
  1908. * For the processing of the last buffer of the thread, MDMAT bit must
  1909. * be reset and the buffer length (in bytes) doesn't have to be a
  1910. * multiple of 4.
  1911. * @param hhash: HASH handle.
  1912. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  1913. * @param Size: length of the input buffer in bytes.
  1914. * @param Algorithm: HASH algorithm.
  1915. * @retval HAL status
  1916. */
  1917. HAL_StatusTypeDef HASH_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint32_t Algorithm)
  1918. {
  1919. uint32_t inputaddr;
  1920. uint32_t inputSize = 0x0;
  1921. /* Make sure the input buffer size (in bytes) is a multiple of 4 when MDMAT bit is set
  1922. (case of multi-buffer HASH processing) */
  1923. assert_param(IS_HASH_DMA_MULTIBUFFER_SIZE(Size));
  1924. /* If State is ready or suspended, start or resume DMA-based HASH processing */
  1925. if ((hhash->State == HAL_HASH_STATE_READY) || (hhash->State == HAL_HASH_STATE_SUSPENDED))
  1926. {
  1927. /* Check input parameters */
  1928. if ( (pInBuffer == NULL ) || (Size == 0) ||
  1929. /* Check phase coherency. Phase must be
  1930. either READY (fresh start)
  1931. or PROCESS (multi-buffer HASH management) */
  1932. ((hhash->Phase != HAL_HASH_PHASE_READY) && (!(IS_HASH_PROCESSING(hhash)))))
  1933. {
  1934. hhash->State = HAL_HASH_STATE_READY;
  1935. return HAL_ERROR;
  1936. }
  1937. /* Process Locked */
  1938. __HAL_LOCK(hhash);
  1939. /* If not a resumption case */
  1940. if (hhash->State == HAL_HASH_STATE_READY)
  1941. {
  1942. /* Change the HASH state */
  1943. hhash->State = HAL_HASH_STATE_BUSY;
  1944. /* Check if initialization phase has already been performed.
  1945. If Phase is already set to HAL_HASH_PHASE_PROCESS, this means the
  1946. API is processing a new input data message in case of multi-buffer HASH
  1947. computation. */
  1948. if(hhash->Phase == HAL_HASH_PHASE_READY)
  1949. {
  1950. /* Select the HASH algorithm, clear HMAC mode and long key selection bit, reset the HASH processor core */
  1951. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_CR_INIT);
  1952. /* Set the phase */
  1953. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  1954. }
  1955. /* Configure the Number of valid bits in last word of the message */
  1956. __HAL_HASH_SET_NBVALIDBITS(Size);
  1957. inputaddr = (uint32_t)pInBuffer; /* DMA transfer start address */
  1958. inputSize = Size; /* DMA transfer size (in bytes) */
  1959. /* In case of suspension request, save the starting parameters */
  1960. hhash->pHashInBuffPtr = pInBuffer; /* DMA transfer start address */
  1961. hhash->HashInCount = Size; /* DMA transfer size (in bytes) */
  1962. }
  1963. /* If resumption case */
  1964. else
  1965. {
  1966. /* Change the HASH state */
  1967. hhash->State = HAL_HASH_STATE_BUSY;
  1968. /* Resumption case, inputaddr and inputSize are not set to the API input parameters
  1969. but to those saved beforehand by HAL_HASH_DMAFeed_ProcessSuspend() when the
  1970. processing was suspended */
  1971. inputaddr = (uint32_t)hhash->pHashInBuffPtr; /* DMA transfer start address */
  1972. inputSize = hhash->HashInCount; /* DMA transfer size (in bytes) */
  1973. }
  1974. /* Set the HASH DMA transfert complete callback */
  1975. hhash->hdmain->XferCpltCallback = HASH_DMAXferCplt;
  1976. /* Set the DMA error callback */
  1977. hhash->hdmain->XferErrorCallback = HASH_DMAError;
  1978. /* Store number of words already pushed to manage proper DMA processing suspension */
  1979. hhash->NbWordsAlreadyPushed = HASH_NBW_PUSHED();
  1980. /* Enable the DMA In DMA Stream */
  1981. HAL_DMA_Start_IT(hhash->hdmain, inputaddr, (uint32_t)&HASH->DIN, (inputSize%4 ? (inputSize+3)/4:inputSize/4));
  1982. /* Enable DMA requests */
  1983. SET_BIT(HASH->CR, HASH_CR_DMAE);
  1984. /* Process Unlock */
  1985. __HAL_UNLOCK(hhash);
  1986. /* Return function status */
  1987. return HAL_OK;
  1988. }
  1989. else
  1990. {
  1991. return HAL_BUSY;
  1992. }
  1993. }
  1994. /**
  1995. * @brief Return the computed digest.
  1996. * @note The API waits for DCIS to be set then reads the computed digest.
  1997. * @param hhash: HASH handle.
  1998. * @param pOutBuffer: pointer to the computed digest.
  1999. * @param Timeout: Timeout value.
  2000. * @retval HAL status
  2001. */
  2002. HAL_StatusTypeDef HASH_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout)
  2003. {
  2004. if(hhash->State == HAL_HASH_STATE_READY)
  2005. {
  2006. /* Check parameter */
  2007. if (pOutBuffer == NULL)
  2008. {
  2009. return HAL_ERROR;
  2010. }
  2011. /* Process Locked */
  2012. __HAL_LOCK(hhash);
  2013. /* Change the HASH state to busy */
  2014. hhash->State = HAL_HASH_STATE_BUSY;
  2015. /* Wait for DCIS flag to be set */
  2016. if (HASH_WaitOnFlagUntilTimeout(hhash, HASH_FLAG_DCIS, RESET, Timeout) != HAL_OK)
  2017. {
  2018. return HAL_TIMEOUT;
  2019. }
  2020. /* Read the message digest */
  2021. HASH_GetDigest(pOutBuffer, HASH_DIGEST_LENGTH());
  2022. /* Change the HASH state to ready */
  2023. hhash->State = HAL_HASH_STATE_READY;
  2024. /* Process UnLock */
  2025. __HAL_UNLOCK(hhash);
  2026. /* Return function status */
  2027. return HAL_OK;
  2028. }
  2029. else
  2030. {
  2031. return HAL_BUSY;
  2032. }
  2033. }
  2034. /**
  2035. * @brief Initialize the HASH peripheral in HMAC mode, next process pInBuffer then
  2036. * read the computed digest.
  2037. * @note Digest is available in pOutBuffer.
  2038. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  2039. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  2040. * @param hhash: HASH handle.
  2041. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  2042. * @param Size: length of the input buffer in bytes.
  2043. * @param pOutBuffer: pointer to the computed digest.
  2044. * @param Timeout: Timeout value.
  2045. * @param Algorithm: HASH algorithm.
  2046. * @retval HAL status
  2047. */
  2048. HAL_StatusTypeDef HMAC_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout, uint32_t Algorithm)
  2049. {
  2050. /* If State is ready or suspended, start or resume polling-based HASH processing */
  2051. if((hhash->State == HAL_HASH_STATE_READY) || (hhash->State == HAL_HASH_STATE_SUSPENDED))
  2052. {
  2053. /* Check input parameters */
  2054. if ((pInBuffer == NULL) || (Size == 0) || (hhash->Init.pKey == NULL) || (hhash->Init.KeySize == 0) || (pOutBuffer == NULL))
  2055. {
  2056. hhash->State = HAL_HASH_STATE_READY;
  2057. return HAL_ERROR;
  2058. }
  2059. /* Process Locked */
  2060. __HAL_LOCK(hhash);
  2061. /* Change the HASH state */
  2062. hhash->State = HAL_HASH_STATE_BUSY;
  2063. /* Check if initialization phase has already be performed */
  2064. if(hhash->Phase == HAL_HASH_PHASE_READY)
  2065. {
  2066. /* Check if key size is larger than 64 bytes, accordingly set LKEY and the other setting bits */
  2067. if(hhash->Init.KeySize > 64)
  2068. {
  2069. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_ALGOMODE_HMAC | HASH_HMAC_KEYTYPE_LONGKEY | HASH_CR_INIT);
  2070. }
  2071. else
  2072. {
  2073. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_ALGOMODE_HMAC | HASH_CR_INIT);
  2074. }
  2075. /* Set the phase to Step 1 */
  2076. hhash->Phase = HAL_HASH_PHASE_HMAC_STEP_1;
  2077. /* Resort to hhash internal fields to feed the IP.
  2078. Parameters will be updated in case of suspension to contain the proper
  2079. information at resumption time. */
  2080. hhash->pHashOutBuffPtr = pOutBuffer; /* Output digest address */
  2081. hhash->pHashInBuffPtr = pInBuffer; /* Input data address, HMAC_Processing input parameter for Step 2 */
  2082. hhash->HashInCount = Size; /* Input data size, HMAC_Processing input parameter for Step 2 */
  2083. hhash->HashBuffSize = Size; /* Store the input buffer size for the whole HMAC process */
  2084. hhash->pHashKeyBuffPtr = hhash->Init.pKey; /* Key address, HMAC_Processing input parameter for Step 1 and Step 3 */
  2085. hhash->HashKeyCount = hhash->Init.KeySize; /* Key size, HMAC_Processing input parameter for Step 1 and Step 3 */
  2086. }
  2087. /* Carry out HMAC processing */
  2088. return HMAC_Processing(hhash, Timeout);
  2089. }
  2090. else
  2091. {
  2092. return HAL_BUSY;
  2093. }
  2094. }
  2095. /**
  2096. * @brief Initialize the HASH peripheral in HMAC mode, next process pInBuffer then
  2097. * read the computed digest in interruption mode.
  2098. * @note Digest is available in pOutBuffer.
  2099. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  2100. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  2101. * @param hhash: HASH handle.
  2102. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  2103. * @param Size: length of the input buffer in bytes.
  2104. * @param pOutBuffer: pointer to the computed digest.
  2105. * @param Algorithm: HASH algorithm.
  2106. * @retval HAL status
  2107. */
  2108. HAL_StatusTypeDef HMAC_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Algorithm)
  2109. {
  2110. /* If State is ready or suspended, start or resume IT-based HASH processing */
  2111. if((hhash->State == HAL_HASH_STATE_READY) || (hhash->State == HAL_HASH_STATE_SUSPENDED))
  2112. {
  2113. /* Check input parameters */
  2114. if ((pInBuffer == NULL) || (Size == 0) || (hhash->Init.pKey == NULL) || (hhash->Init.KeySize == 0) || (pOutBuffer == NULL))
  2115. {
  2116. hhash->State = HAL_HASH_STATE_READY;
  2117. return HAL_ERROR;
  2118. }
  2119. /* Process Locked */
  2120. __HAL_LOCK(hhash);
  2121. /* Change the HASH state */
  2122. hhash->State = HAL_HASH_STATE_BUSY;
  2123. /* Initialize IT counter */
  2124. hhash->HashITCounter = 1;
  2125. /* Check if initialization phase has already be performed */
  2126. if (hhash->Phase == HAL_HASH_PHASE_READY)
  2127. {
  2128. /* Check if key size is larger than 64 bytes, accordingly set LKEY and the other setting bits */
  2129. if(hhash->Init.KeySize > 64)
  2130. {
  2131. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_ALGOMODE_HMAC | HASH_HMAC_KEYTYPE_LONGKEY | HASH_CR_INIT);
  2132. }
  2133. else
  2134. {
  2135. MODIFY_REG(HASH->CR, HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_ALGOMODE_HMAC | HASH_CR_INIT);
  2136. }
  2137. /* Resort to hhash internal fields hhash->pHashInBuffPtr and hhash->HashInCount
  2138. to feed the IP whatever the HMAC step.
  2139. Lines below are set to start HMAC Step 1 processing where key is entered first. */
  2140. hhash->HashInCount = hhash->Init.KeySize; /* Key size */
  2141. hhash->pHashInBuffPtr = hhash->Init.pKey ; /* Key address */
  2142. /* Store input and output parameters in handle fields to manage steps transition
  2143. or possible HMAC suspension/resumption */
  2144. hhash->pHashKeyBuffPtr = hhash->Init.pKey; /* Key address */
  2145. hhash->pHashMsgBuffPtr = pInBuffer; /* Input message address */
  2146. hhash->HashBuffSize = Size; /* Input message size (in bytes) */
  2147. hhash->pHashOutBuffPtr = pOutBuffer; /* Output digest address */
  2148. /* Configure the number of valid bits in last word of the key */
  2149. __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize);
  2150. /* Set the phase to Step 1 */
  2151. hhash->Phase = HAL_HASH_PHASE_HMAC_STEP_1;
  2152. }
  2153. else if ((hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_1) || (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_3))
  2154. {
  2155. /* Restart IT-based HASH processing after Step 1 or Step 3 suspension */
  2156. }
  2157. else if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_2)
  2158. {
  2159. /* Restart IT-based HASH processing after Step 2 suspension */
  2160. }
  2161. else
  2162. {
  2163. /* Error report as phase incorrect */
  2164. /* Process Unlock */
  2165. __HAL_UNLOCK(hhash);
  2166. hhash->State = HAL_HASH_STATE_READY;
  2167. return HAL_ERROR;
  2168. }
  2169. /* Process Unlock */
  2170. __HAL_UNLOCK(hhash);
  2171. /* Enable Interrupts */
  2172. __HAL_HASH_ENABLE_IT(HASH_IT_DINI|HASH_IT_DCI);
  2173. /* Return function status */
  2174. return HAL_OK;
  2175. }
  2176. else
  2177. {
  2178. return HAL_BUSY;
  2179. }
  2180. }
  2181. /**
  2182. * @brief Initialize the HASH peripheral in HMAC mode then initiate the required
  2183. * DMA transfers to feed the key and the input buffer to the IP.
  2184. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  2185. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  2186. * @note In case of multi-buffer HMAC processing, the input buffer size (in bytes) must
  2187. * be a multiple of 4 otherwise, the HASH digest computation is corrupted.
  2188. * Only the length of the last buffer of the thread doesn't have to be a
  2189. * multiple of 4.
  2190. * @param hhash: HASH handle.
  2191. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  2192. * @param Size: length of the input buffer in bytes.
  2193. * @param Algorithm: HASH algorithm.
  2194. * @retval HAL status
  2195. */
  2196. HAL_StatusTypeDef HMAC_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint32_t Algorithm)
  2197. {
  2198. uint32_t inputaddr;
  2199. uint32_t inputSize = 0x0;
  2200. /* Make sure the input buffer size (in bytes) is a multiple of 4 when digest calculation
  2201. is disabled (multi-buffer HMAC processing, MDMAT bit to be set) */
  2202. assert_param(IS_HMAC_DMA_MULTIBUFFER_SIZE(hhash, Size));
  2203. /* If State is ready or suspended, start or resume DMA-based HASH processing */
  2204. if ((hhash->State == HAL_HASH_STATE_READY) || (hhash->State == HAL_HASH_STATE_SUSPENDED))
  2205. {
  2206. /* Check input parameters */
  2207. if ((pInBuffer == NULL ) || (Size == 0) || (hhash->Init.pKey == NULL ) || (hhash->Init.KeySize == 0) ||
  2208. /* Check phase coherency. Phase must be
  2209. either READY (fresh start)
  2210. or one of HMAC PROCESS steps (multi-buffer HASH management) */
  2211. ((hhash->Phase != HAL_HASH_PHASE_READY) && (!(IS_HMAC_PROCESSING(hhash)))))
  2212. {
  2213. hhash->State = HAL_HASH_STATE_READY;
  2214. return HAL_ERROR;
  2215. }
  2216. /* Process Locked */
  2217. __HAL_LOCK(hhash);
  2218. /* If not a case of resumption after suspension */
  2219. if (hhash->State == HAL_HASH_STATE_READY)
  2220. {
  2221. /* Check whether or not initialization phase has already be performed */
  2222. if(hhash->Phase == HAL_HASH_PHASE_READY)
  2223. {
  2224. /* Change the HASH state */
  2225. hhash->State = HAL_HASH_STATE_BUSY;
  2226. /* Check if key size is larger than 64 bytes, accordingly set LKEY and the other setting bits.
  2227. At the same time, ensure MDMAT bit is cleared. */
  2228. if(hhash->Init.KeySize > 64)
  2229. {
  2230. MODIFY_REG(HASH->CR, HASH_CR_MDMAT|HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_ALGOMODE_HMAC | HASH_HMAC_KEYTYPE_LONGKEY | HASH_CR_INIT);
  2231. }
  2232. else
  2233. {
  2234. MODIFY_REG(HASH->CR, HASH_CR_MDMAT|HASH_CR_LKEY|HASH_CR_ALGO|HASH_CR_MODE|HASH_CR_INIT, Algorithm | HASH_ALGOMODE_HMAC | HASH_CR_INIT);
  2235. }
  2236. /* Store input aparameters in handle fields to manage steps transition
  2237. or possible HMAC suspension/resumption */
  2238. hhash->HashInCount = hhash->Init.KeySize; /* Initial size for first DMA transfer (key size) */
  2239. hhash->pHashKeyBuffPtr = hhash->Init.pKey; /* Key address */
  2240. hhash->pHashInBuffPtr = hhash->Init.pKey ; /* First address passed to DMA (key address at Step 1) */
  2241. hhash->pHashMsgBuffPtr = pInBuffer; /* Input data address */
  2242. hhash->HashBuffSize = Size; /* input data size (in bytes) */
  2243. /* Set DMA input parameters */
  2244. inputaddr = (uint32_t)(hhash->Init.pKey); /* Address passed to DMA (start by entering Key message) */
  2245. inputSize = hhash->Init.KeySize; /* Size for first DMA transfer (in bytes) */
  2246. /* Configure the number of valid bits in last word of the key */
  2247. __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize);
  2248. /* Set the phase to Step 1 */
  2249. hhash->Phase = HAL_HASH_PHASE_HMAC_STEP_1;
  2250. }
  2251. else if (hhash->Phase == HAL_HASH_PHASE_HMAC_STEP_2)
  2252. {
  2253. /* Process a new input data message in case of multi-buffer HMAC processing
  2254. (this is not a resumption case) */
  2255. /* Change the HASH state */
  2256. hhash->State = HAL_HASH_STATE_BUSY;
  2257. /* Save input parameters to be able to manage possible suspension/resumption */
  2258. hhash->HashInCount = Size; /* Input message address */
  2259. hhash->pHashInBuffPtr = pInBuffer; /* Input message size in bytes */
  2260. /* Set DMA input parameters */
  2261. inputaddr = (uint32_t)pInBuffer; /* Input message address */
  2262. inputSize = Size; /* Input message size in bytes */
  2263. if (hhash->DigestCalculationDisable == RESET)
  2264. {
  2265. /* This means this is the last buffer of the multi-buffer sequence: DCAL needs to be set. */
  2266. __HAL_HASH_RESET_MDMAT();
  2267. __HAL_HASH_SET_NBVALIDBITS(inputSize);
  2268. }
  2269. }
  2270. else
  2271. {
  2272. /* Phase not aligned with handle READY state */
  2273. __HAL_UNLOCK(hhash);
  2274. /* Return function status */
  2275. return HAL_ERROR;
  2276. }
  2277. }
  2278. else
  2279. {
  2280. /* Resumption case (phase may be Step 1, 2 or 3) */
  2281. /* Change the HASH state */
  2282. hhash->State = HAL_HASH_STATE_BUSY;
  2283. /* Set DMA input parameters at resumption location;
  2284. inputaddr and inputSize are not set to the API input parameters
  2285. but to those saved beforehand by HAL_HASH_DMAFeed_ProcessSuspend() when the
  2286. processing was suspended. */
  2287. inputaddr = (uint32_t)(hhash->pHashInBuffPtr); /* Input message address */
  2288. inputSize = hhash->HashInCount; /* Input message size in bytes */
  2289. }
  2290. /* Set the HASH DMA transfert complete callback */
  2291. hhash->hdmain->XferCpltCallback = HASH_DMAXferCplt;
  2292. /* Set the DMA error callback */
  2293. hhash->hdmain->XferErrorCallback = HASH_DMAError;
  2294. /* Store number of words already pushed to manage proper DMA processing suspension */
  2295. hhash->NbWordsAlreadyPushed = HASH_NBW_PUSHED();
  2296. /* Enable the DMA In DMA Stream */
  2297. HAL_DMA_Start_IT(hhash->hdmain, inputaddr, (uint32_t)&HASH->DIN, (inputSize%4 ? (inputSize+3)/4:inputSize/4));
  2298. /* Enable DMA requests */
  2299. SET_BIT(HASH->CR, HASH_CR_DMAE);
  2300. /* Process Unlocked */
  2301. __HAL_UNLOCK(hhash);
  2302. /* Return function status */
  2303. return HAL_OK;
  2304. }
  2305. else
  2306. {
  2307. return HAL_BUSY;
  2308. }
  2309. }
  2310. /**
  2311. * @}
  2312. */
  2313. /**
  2314. * @}
  2315. */
  2316. /**
  2317. * @}
  2318. */
  2319. #endif /* defined (STM32L4A6xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx) */
  2320. #endif /* HAL_HASH_MODULE_ENABLED */
  2321. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/