stm32l4xx_hal_hash.c 101 KB

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