stm32f4xx_hal_hash.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_hash.c
  4. * @author MCD Application Team
  5. * @version V1.4.3
  6. * @date 11-December-2015
  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 functions
  11. * + HASH/HMAC Processing functions by algorithm using polling mode
  12. * + HASH/HMAC functions by algorithm using interrupt mode
  13. * + HASH/HMAC functions by algorithm using DMA mode
  14. * + Peripheral State functions
  15. *
  16. @verbatim
  17. ==============================================================================
  18. ##### How to use this driver #####
  19. ==============================================================================
  20. [..]
  21. The HASH HAL driver can be used as follows:
  22. (#)Initialize the HASH low level resources by implementing the HAL_HASH_MspInit():
  23. (##) Enable the HASH interface clock using __HAL_RCC_HASH_CLK_ENABLE()
  24. (##) In case of using processing APIs based on interrupts (e.g. HAL_HMAC_SHA1_Start_IT())
  25. (+++) Configure the HASH interrupt priority using HAL_NVIC_SetPriority()
  26. (+++) Enable the HASH IRQ handler using HAL_NVIC_EnableIRQ()
  27. (+++) In HASH IRQ handler, call HAL_HASH_IRQHandler()
  28. (##) In case of using DMA to control data transfer (e.g. HAL_HMAC_SHA1_Start_DMA())
  29. (+++) Enable the DMAx interface clock using __DMAx_CLK_ENABLE()
  30. (+++) Configure and enable one DMA stream one for managing data transfer from
  31. memory to peripheral (input stream). Managing data transfer from
  32. peripheral to memory can be performed only using CPU
  33. (+++) Associate the initialized DMA handle to the HASH DMA handle
  34. using __HAL_LINKDMA()
  35. (+++) Configure the priority and enable the NVIC for the transfer complete
  36. interrupt on the DMA Stream using HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()
  37. (#)Initialize the HASH HAL using HAL_HASH_Init(). This function configures mainly:
  38. (##) The data type: 1-bit, 8-bit, 16-bit and 32-bit.
  39. (##) For HMAC, the encryption key.
  40. (##) For HMAC, the key size used for encryption.
  41. (#)Three processing functions are available:
  42. (##) Polling mode: processing APIs are blocking functions
  43. i.e. they process the data and wait till the digest computation is finished
  44. e.g. HAL_HASH_SHA1_Start()
  45. (##) Interrupt mode: encryption and decryption APIs are not blocking functions
  46. i.e. they process the data under interrupt
  47. e.g. HAL_HASH_SHA1_Start_IT()
  48. (##) DMA mode: processing APIs are not blocking functions and the CPU is
  49. not used for data transfer i.e. the data transfer is ensured by DMA
  50. e.g. HAL_HASH_SHA1_Start_DMA()
  51. (#)When the processing function is called at first time after HAL_HASH_Init()
  52. the HASH peripheral is initialized and processes the buffer in input.
  53. After that, the digest computation is started.
  54. When processing multi-buffer use the accumulate function to write the
  55. data in the peripheral without starting the digest computation. In last
  56. buffer use the start function to input the last buffer ans start the digest
  57. computation.
  58. (##) e.g. HAL_HASH_SHA1_Accumulate() : write 1st data buffer in the peripheral without starting the digest computation
  59. (##) write (n-1)th data buffer in the peripheral without starting the digest computation
  60. (##) HAL_HASH_SHA1_Start() : write (n)th data buffer in the peripheral and start the digest computation
  61. (#)In HMAC mode, there is no Accumulate API. Only Start API is available.
  62. (#)In case of using DMA, call the DMA start processing e.g. HAL_HASH_SHA1_Start_DMA().
  63. After that, call the finish function in order to get the digest value
  64. e.g. HAL_HASH_SHA1_Finish()
  65. (#)Call HAL_HASH_DeInit() to deinitialize the HASH peripheral.
  66. @endverbatim
  67. ******************************************************************************
  68. * @attention
  69. *
  70. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  71. *
  72. * Redistribution and use in source and binary forms, with or without modification,
  73. * are permitted provided that the following conditions are met:
  74. * 1. Redistributions of source code must retain the above copyright notice,
  75. * this list of conditions and the following disclaimer.
  76. * 2. Redistributions in binary form must reproduce the above copyright notice,
  77. * this list of conditions and the following disclaimer in the documentation
  78. * and/or other materials provided with the distribution.
  79. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  80. * may be used to endorse or promote products derived from this software
  81. * without specific prior written permission.
  82. *
  83. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  84. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  85. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  86. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  87. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  88. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  89. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  90. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  91. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  92. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  93. *
  94. ******************************************************************************
  95. */
  96. /* Includes ------------------------------------------------------------------*/
  97. #include "stm32f4xx_hal.h"
  98. /** @addtogroup STM32F4xx_HAL_Driver
  99. * @{
  100. */
  101. /** @defgroup HASH HASH
  102. * @brief HASH HAL module driver.
  103. * @{
  104. */
  105. #ifdef HAL_HASH_MODULE_ENABLED
  106. #if defined(STM32F415xx) || defined(STM32F417xx) || defined(STM32F437xx) || defined(STM32F439xx) || defined(STM32F479xx)
  107. /* Private typedef -----------------------------------------------------------*/
  108. /* Private define ------------------------------------------------------------*/
  109. /* Private macro -------------------------------------------------------------*/
  110. /* Private variables ---------------------------------------------------------*/
  111. /* Private function prototypes -----------------------------------------------*/
  112. /** @defgroup HASH_Private_Functions HASH Private Functions
  113. * @{
  114. */
  115. static void HASH_DMAXferCplt(DMA_HandleTypeDef *hdma);
  116. static void HASH_DMAError(DMA_HandleTypeDef *hdma);
  117. static void HASH_GetDigest(uint8_t *pMsgDigest, uint8_t Size);
  118. static void HASH_WriteData(uint8_t *pInBuffer, uint32_t Size);
  119. /**
  120. * @}
  121. */
  122. /* Private functions ---------------------------------------------------------*/
  123. /** @addtogroup HASH_Private_Functions
  124. * @{
  125. */
  126. /**
  127. * @brief DMA HASH Input Data complete callback.
  128. * @param hdma: DMA handle
  129. * @retval None
  130. */
  131. static void HASH_DMAXferCplt(DMA_HandleTypeDef *hdma)
  132. {
  133. HASH_HandleTypeDef* hhash = ( HASH_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  134. uint32_t inputaddr = 0;
  135. uint32_t buffersize = 0;
  136. if((HASH->CR & HASH_CR_MODE) != HASH_CR_MODE)
  137. {
  138. /* Disable the DMA transfer */
  139. HASH->CR &= (uint32_t)(~HASH_CR_DMAE);
  140. /* Change HASH peripheral state */
  141. hhash->State = HAL_HASH_STATE_READY;
  142. /* Call Input data transfer complete callback */
  143. HAL_HASH_InCpltCallback(hhash);
  144. }
  145. else
  146. {
  147. /* Increment Interrupt counter */
  148. hhash->HashInCount++;
  149. /* Disable the DMA transfer before starting the next transfer */
  150. HASH->CR &= (uint32_t)(~HASH_CR_DMAE);
  151. if(hhash->HashInCount <= 2)
  152. {
  153. /* In case HashInCount = 1, set the DMA to transfer data to HASH DIN register */
  154. if(hhash->HashInCount == 1)
  155. {
  156. inputaddr = (uint32_t)hhash->pHashInBuffPtr;
  157. buffersize = hhash->HashBuffSize;
  158. }
  159. /* In case HashInCount = 2, set the DMA to transfer key to HASH DIN register */
  160. else if(hhash->HashInCount == 2)
  161. {
  162. inputaddr = (uint32_t)hhash->Init.pKey;
  163. buffersize = hhash->Init.KeySize;
  164. }
  165. /* Configure the number of valid bits in last word of the message */
  166. MODIFY_REG(HASH->STR, HASH_STR_NBLW, 8 * (buffersize % 4));
  167. /* Set the HASH DMA transfer complete */
  168. hhash->hdmain->XferCpltCallback = HASH_DMAXferCplt;
  169. /* Enable the DMA In DMA Stream */
  170. HAL_DMA_Start_IT(hhash->hdmain, inputaddr, (uint32_t)&HASH->DIN, (buffersize%4 ? (buffersize+3)/4:buffersize/4));
  171. /* Enable DMA requests */
  172. HASH->CR |= (HASH_CR_DMAE);
  173. }
  174. else
  175. {
  176. /* Disable the DMA transfer */
  177. HASH->CR &= (uint32_t)(~HASH_CR_DMAE);
  178. /* Reset the InCount */
  179. hhash->HashInCount = 0;
  180. /* Change HASH peripheral state */
  181. hhash->State = HAL_HASH_STATE_READY;
  182. /* Call Input data transfer complete callback */
  183. HAL_HASH_InCpltCallback(hhash);
  184. }
  185. }
  186. }
  187. /**
  188. * @brief DMA HASH communication error callback.
  189. * @param hdma: DMA handle
  190. * @retval None
  191. */
  192. static void HASH_DMAError(DMA_HandleTypeDef *hdma)
  193. {
  194. HASH_HandleTypeDef* hhash = ( HASH_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  195. hhash->State= HAL_HASH_STATE_READY;
  196. HAL_HASH_ErrorCallback(hhash);
  197. }
  198. /**
  199. * @brief Writes the input buffer in data register.
  200. * @param pInBuffer: Pointer to input buffer
  201. * @param Size: The size of input buffer
  202. * @retval None
  203. */
  204. static void HASH_WriteData(uint8_t *pInBuffer, uint32_t Size)
  205. {
  206. uint32_t buffercounter;
  207. uint32_t inputaddr = (uint32_t) pInBuffer;
  208. for(buffercounter = 0; buffercounter < Size; buffercounter+=4)
  209. {
  210. HASH->DIN = *(uint32_t*)inputaddr;
  211. inputaddr+=4;
  212. }
  213. }
  214. /**
  215. * @brief Provides the message digest result.
  216. * @param pMsgDigest: Pointer to the message digest
  217. * @param Size: The size of the message digest in bytes
  218. * @retval None
  219. */
  220. static void HASH_GetDigest(uint8_t *pMsgDigest, uint8_t Size)
  221. {
  222. uint32_t msgdigest = (uint32_t)pMsgDigest;
  223. switch(Size)
  224. {
  225. case 16:
  226. /* Read the message digest */
  227. *(uint32_t*)(msgdigest) = __REV(HASH->HR[0]);
  228. msgdigest+=4;
  229. *(uint32_t*)(msgdigest) = __REV(HASH->HR[1]);
  230. msgdigest+=4;
  231. *(uint32_t*)(msgdigest) = __REV(HASH->HR[2]);
  232. msgdigest+=4;
  233. *(uint32_t*)(msgdigest) = __REV(HASH->HR[3]);
  234. break;
  235. case 20:
  236. /* Read the message digest */
  237. *(uint32_t*)(msgdigest) = __REV(HASH->HR[0]);
  238. msgdigest+=4;
  239. *(uint32_t*)(msgdigest) = __REV(HASH->HR[1]);
  240. msgdigest+=4;
  241. *(uint32_t*)(msgdigest) = __REV(HASH->HR[2]);
  242. msgdigest+=4;
  243. *(uint32_t*)(msgdigest) = __REV(HASH->HR[3]);
  244. msgdigest+=4;
  245. *(uint32_t*)(msgdigest) = __REV(HASH->HR[4]);
  246. break;
  247. case 28:
  248. /* Read the message digest */
  249. *(uint32_t*)(msgdigest) = __REV(HASH->HR[0]);
  250. msgdigest+=4;
  251. *(uint32_t*)(msgdigest) = __REV(HASH->HR[1]);
  252. msgdigest+=4;
  253. *(uint32_t*)(msgdigest) = __REV(HASH->HR[2]);
  254. msgdigest+=4;
  255. *(uint32_t*)(msgdigest) = __REV(HASH->HR[3]);
  256. msgdigest+=4;
  257. *(uint32_t*)(msgdigest) = __REV(HASH->HR[4]);
  258. msgdigest+=4;
  259. *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[5]);
  260. msgdigest+=4;
  261. *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[6]);
  262. break;
  263. case 32:
  264. /* Read the message digest */
  265. *(uint32_t*)(msgdigest) = __REV(HASH->HR[0]);
  266. msgdigest+=4;
  267. *(uint32_t*)(msgdigest) = __REV(HASH->HR[1]);
  268. msgdigest+=4;
  269. *(uint32_t*)(msgdigest) = __REV(HASH->HR[2]);
  270. msgdigest+=4;
  271. *(uint32_t*)(msgdigest) = __REV(HASH->HR[3]);
  272. msgdigest+=4;
  273. *(uint32_t*)(msgdigest) = __REV(HASH->HR[4]);
  274. msgdigest+=4;
  275. *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[5]);
  276. msgdigest+=4;
  277. *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[6]);
  278. msgdigest+=4;
  279. *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[7]);
  280. break;
  281. default:
  282. break;
  283. }
  284. }
  285. /**
  286. * @}
  287. */
  288. /* Exported functions --------------------------------------------------------*/
  289. /** @addtogroup HASH_Exported_Functions
  290. * @{
  291. */
  292. /** @addtogroup HASH_Exported_Functions_Group1 Initialization and de-initialization functions
  293. * @brief Initialization and Configuration functions.
  294. *
  295. @verbatim
  296. ===============================================================================
  297. ##### Initialization and de-initialization functions #####
  298. ===============================================================================
  299. [..] This section provides functions allowing to:
  300. (+) Initialize the HASH according to the specified parameters
  301. in the HASH_InitTypeDef and creates the associated handle.
  302. (+) DeInitialize the HASH peripheral.
  303. (+) Initialize the HASH MSP.
  304. (+) DeInitialize HASH MSP.
  305. @endverbatim
  306. * @{
  307. */
  308. /**
  309. * @brief Initializes the HASH according to the specified parameters in the
  310. HASH_HandleTypeDef and creates the associated handle.
  311. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  312. * the configuration information for HASH module
  313. * @retval HAL status
  314. */
  315. HAL_StatusTypeDef HAL_HASH_Init(HASH_HandleTypeDef *hhash)
  316. {
  317. /* Check the hash handle allocation */
  318. if(hhash == NULL)
  319. {
  320. return HAL_ERROR;
  321. }
  322. /* Check the parameters */
  323. assert_param(IS_HASH_DATATYPE(hhash->Init.DataType));
  324. if(hhash->State == HAL_HASH_STATE_RESET)
  325. {
  326. /* Allocate lock resource and initialize it */
  327. hhash->Lock = HAL_UNLOCKED;
  328. /* Init the low level hardware */
  329. HAL_HASH_MspInit(hhash);
  330. }
  331. /* Change the HASH state */
  332. hhash->State = HAL_HASH_STATE_BUSY;
  333. /* Reset HashInCount, HashBuffSize and HashITCounter */
  334. hhash->HashInCount = 0;
  335. hhash->HashBuffSize = 0;
  336. hhash->HashITCounter = 0;
  337. /* Set the data type */
  338. HASH->CR |= (uint32_t) (hhash->Init.DataType);
  339. /* Change the HASH state */
  340. hhash->State = HAL_HASH_STATE_READY;
  341. /* Set the default HASH phase */
  342. hhash->Phase = HAL_HASH_PHASE_READY;
  343. /* Return function status */
  344. return HAL_OK;
  345. }
  346. /**
  347. * @brief DeInitializes the HASH peripheral.
  348. * @note This API must be called before starting a new processing.
  349. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  350. * the configuration information for HASH module
  351. * @retval HAL status
  352. */
  353. HAL_StatusTypeDef HAL_HASH_DeInit(HASH_HandleTypeDef *hhash)
  354. {
  355. /* Check the HASH handle allocation */
  356. if(hhash == NULL)
  357. {
  358. return HAL_ERROR;
  359. }
  360. /* Change the HASH state */
  361. hhash->State = HAL_HASH_STATE_BUSY;
  362. /* Set the default HASH phase */
  363. hhash->Phase = HAL_HASH_PHASE_READY;
  364. /* Reset HashInCount, HashBuffSize and HashITCounter */
  365. hhash->HashInCount = 0;
  366. hhash->HashBuffSize = 0;
  367. hhash->HashITCounter = 0;
  368. /* DeInit the low level hardware */
  369. HAL_HASH_MspDeInit(hhash);
  370. /* Change the HASH state */
  371. hhash->State = HAL_HASH_STATE_RESET;
  372. /* Release Lock */
  373. __HAL_UNLOCK(hhash);
  374. /* Return function status */
  375. return HAL_OK;
  376. }
  377. /**
  378. * @brief Initializes the HASH MSP.
  379. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  380. * the configuration information for HASH module
  381. * @retval None
  382. */
  383. __weak void HAL_HASH_MspInit(HASH_HandleTypeDef *hhash)
  384. {
  385. /* Prevent unused argument(s) compilation warning */
  386. UNUSED(hhash);
  387. /* NOTE: This function Should not be modified, when the callback is needed,
  388. the HAL_HASH_MspInit could be implemented in the user file
  389. */
  390. }
  391. /**
  392. * @brief DeInitializes HASH MSP.
  393. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  394. * the configuration information for HASH module
  395. * @retval None
  396. */
  397. __weak void HAL_HASH_MspDeInit(HASH_HandleTypeDef *hhash)
  398. {
  399. /* Prevent unused argument(s) compilation warning */
  400. UNUSED(hhash);
  401. /* NOTE: This function Should not be modified, when the callback is needed,
  402. the HAL_HASH_MspDeInit could be implemented in the user file
  403. */
  404. }
  405. /**
  406. * @brief Input data transfer complete callback.
  407. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  408. * the configuration information for HASH module
  409. * @retval None
  410. */
  411. __weak void HAL_HASH_InCpltCallback(HASH_HandleTypeDef *hhash)
  412. {
  413. /* Prevent unused argument(s) compilation warning */
  414. UNUSED(hhash);
  415. /* NOTE: This function Should not be modified, when the callback is needed,
  416. the HAL_HASH_InCpltCallback could be implemented in the user file
  417. */
  418. }
  419. /**
  420. * @brief Data transfer Error callback.
  421. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  422. * the configuration information for HASH module
  423. * @retval None
  424. */
  425. __weak void HAL_HASH_ErrorCallback(HASH_HandleTypeDef *hhash)
  426. {
  427. /* Prevent unused argument(s) compilation warning */
  428. UNUSED(hhash);
  429. /* NOTE: This function Should not be modified, when the callback is needed,
  430. the HAL_HASH_ErrorCallback could be implemented in the user file
  431. */
  432. }
  433. /**
  434. * @brief Digest computation complete callback. It is used only with interrupt.
  435. * @note This callback is not relevant with DMA.
  436. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  437. * the configuration information for HASH module
  438. * @retval None
  439. */
  440. __weak void HAL_HASH_DgstCpltCallback(HASH_HandleTypeDef *hhash)
  441. {
  442. /* Prevent unused argument(s) compilation warning */
  443. UNUSED(hhash);
  444. /* NOTE: This function Should not be modified, when the callback is needed,
  445. the HAL_HASH_DgstCpltCallback could be implemented in the user file
  446. */
  447. }
  448. /**
  449. * @}
  450. */
  451. /** @defgroup HASH_Exported_Functions_Group2 HASH processing functions using polling mode
  452. * @brief processing functions using polling mode
  453. *
  454. @verbatim
  455. ===============================================================================
  456. ##### HASH processing using polling mode functions#####
  457. ===============================================================================
  458. [..] This section provides functions allowing to calculate in polling mode
  459. the hash value using one of the following algorithms:
  460. (+) MD5
  461. (+) SHA1
  462. @endverbatim
  463. * @{
  464. */
  465. /**
  466. * @brief Initializes the HASH peripheral in MD5 mode then processes pInBuffer.
  467. The digest is available in pOutBuffer.
  468. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  469. * the configuration information for HASH module
  470. * @param pInBuffer: Pointer to the input buffer (buffer to be hashed).
  471. * @param Size: Length of the input buffer in bytes.
  472. * If the Size is multiple of 64 bytes, appending the input buffer is possible.
  473. * If the Size is not multiple of 64 bytes, the padding is managed by hardware
  474. * and appending the input buffer is no more possible.
  475. * @param pOutBuffer: Pointer to the computed digest. Its size must be 16 bytes.
  476. * @param Timeout: Timeout value
  477. * @retval HAL status
  478. */
  479. HAL_StatusTypeDef HAL_HASH_MD5_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout)
  480. {
  481. uint32_t tickstart = 0;
  482. /* Process Locked */
  483. __HAL_LOCK(hhash);
  484. /* Change the HASH state */
  485. hhash->State = HAL_HASH_STATE_BUSY;
  486. /* Check if initialization phase has already been performed */
  487. if(hhash->Phase == HAL_HASH_PHASE_READY)
  488. {
  489. /* Select the MD5 mode and reset the HASH processor core, so that the HASH will be ready to compute
  490. the message digest of a new message */
  491. HASH->CR |= HASH_ALGOSELECTION_MD5 | HASH_CR_INIT;
  492. }
  493. /* Set the phase */
  494. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  495. /* Configure the number of valid bits in last word of the message */
  496. __HAL_HASH_SET_NBVALIDBITS(Size);
  497. /* Write input buffer in data register */
  498. HASH_WriteData(pInBuffer, Size);
  499. /* Start the digest calculation */
  500. __HAL_HASH_START_DIGEST();
  501. /* Get tick */
  502. tickstart = HAL_GetTick();
  503. while(HAL_IS_BIT_SET(HASH->SR, HASH_FLAG_BUSY))
  504. {
  505. /* Check for the Timeout */
  506. if(Timeout != HAL_MAX_DELAY)
  507. {
  508. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  509. {
  510. /* Change state */
  511. hhash->State = HAL_HASH_STATE_TIMEOUT;
  512. /* Process Unlocked */
  513. __HAL_UNLOCK(hhash);
  514. return HAL_TIMEOUT;
  515. }
  516. }
  517. }
  518. /* Read the message digest */
  519. HASH_GetDigest(pOutBuffer, 16);
  520. /* Change the HASH state */
  521. hhash->State = HAL_HASH_STATE_READY;
  522. /* Process Unlocked */
  523. __HAL_UNLOCK(hhash);
  524. /* Return function status */
  525. return HAL_OK;
  526. }
  527. /**
  528. * @brief Initializes the HASH peripheral in MD5 mode then writes the pInBuffer.
  529. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  530. * the configuration information for HASH module
  531. * @param pInBuffer: Pointer to the input buffer (buffer to be hashed).
  532. * @param Size: Length of the input buffer in bytes.
  533. * If the Size is multiple of 64 bytes, appending the input buffer is possible.
  534. * If the Size is not multiple of 64 bytes, the padding is managed by hardware
  535. * and appending the input buffer is no more possible.
  536. * @retval HAL status
  537. */
  538. HAL_StatusTypeDef HAL_HASH_MD5_Accumulate(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  539. {
  540. /* Process Locked */
  541. __HAL_LOCK(hhash);
  542. /* Change the HASH state */
  543. hhash->State = HAL_HASH_STATE_BUSY;
  544. /* Check if initialization phase has already been performed */
  545. if(hhash->Phase == HAL_HASH_PHASE_READY)
  546. {
  547. /* Select the MD5 mode and reset the HASH processor core, so that the HASH will be ready to compute
  548. the message digest of a new message */
  549. HASH->CR |= HASH_ALGOSELECTION_MD5 | HASH_CR_INIT;
  550. }
  551. /* Set the phase */
  552. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  553. /* Configure the number of valid bits in last word of the message */
  554. __HAL_HASH_SET_NBVALIDBITS(Size);
  555. /* Write input buffer in data register */
  556. HASH_WriteData(pInBuffer, Size);
  557. /* Change the HASH state */
  558. hhash->State = HAL_HASH_STATE_READY;
  559. /* Process Unlocked */
  560. __HAL_UNLOCK(hhash);
  561. /* Return function status */
  562. return HAL_OK;
  563. }
  564. /**
  565. * @brief Initializes the HASH peripheral in SHA1 mode then processes pInBuffer.
  566. The digest is available in pOutBuffer.
  567. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  568. * the configuration information for HASH module
  569. * @param pInBuffer: Pointer to the input buffer (buffer to be hashed).
  570. * @param Size: Length of the input buffer in bytes.
  571. * If the Size is not multiple of 64 bytes, the padding is managed by hardware.
  572. * @param pOutBuffer: Pointer to the computed digest. Its size must be 20 bytes.
  573. * @param Timeout: Timeout value
  574. * @retval HAL status
  575. */
  576. HAL_StatusTypeDef HAL_HASH_SHA1_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout)
  577. {
  578. uint32_t tickstart = 0;
  579. /* Process Locked */
  580. __HAL_LOCK(hhash);
  581. /* Change the HASH state */
  582. hhash->State = HAL_HASH_STATE_BUSY;
  583. /* Check if initialization phase has already been performed */
  584. if(hhash->Phase == HAL_HASH_PHASE_READY)
  585. {
  586. /* Select the SHA1 mode and reset the HASH processor core, so that the HASH will be ready to compute
  587. the message digest of a new message */
  588. HASH->CR |= HASH_ALGOSELECTION_SHA1 | HASH_CR_INIT;
  589. }
  590. /* Set the phase */
  591. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  592. /* Configure the number of valid bits in last word of the message */
  593. __HAL_HASH_SET_NBVALIDBITS(Size);
  594. /* Write input buffer in data register */
  595. HASH_WriteData(pInBuffer, Size);
  596. /* Start the digest calculation */
  597. __HAL_HASH_START_DIGEST();
  598. /* Get tick */
  599. tickstart = HAL_GetTick();
  600. while(HAL_IS_BIT_SET(HASH->SR, HASH_FLAG_BUSY))
  601. {
  602. /* Check for the Timeout */
  603. if(Timeout != HAL_MAX_DELAY)
  604. {
  605. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  606. {
  607. /* Change state */
  608. hhash->State = HAL_HASH_STATE_TIMEOUT;
  609. /* Process Unlocked */
  610. __HAL_UNLOCK(hhash);
  611. return HAL_TIMEOUT;
  612. }
  613. }
  614. }
  615. /* Read the message digest */
  616. HASH_GetDigest(pOutBuffer, 20);
  617. /* Change the HASH state */
  618. hhash->State = HAL_HASH_STATE_READY;
  619. /* Process Unlocked */
  620. __HAL_UNLOCK(hhash);
  621. /* Return function status */
  622. return HAL_OK;
  623. }
  624. /**
  625. * @brief Initializes the HASH peripheral in SHA1 mode then processes pInBuffer.
  626. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  627. * the configuration information for HASH module
  628. * @param pInBuffer: Pointer to the input buffer (buffer to be hashed).
  629. * @param Size: Length of the input buffer in bytes.
  630. * If the Size is not multiple of 64 bytes, the padding is managed by hardware.
  631. * @note Input buffer size in bytes must be a multiple of 4 otherwise the digest computation is corrupted.
  632. * @retval HAL status
  633. */
  634. HAL_StatusTypeDef HAL_HASH_SHA1_Accumulate(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  635. {
  636. /* Check the parameters */
  637. assert_param(IS_HASH_SHA1_BUFFER_SIZE(Size));
  638. /* Process Locked */
  639. __HAL_LOCK(hhash);
  640. /* Change the HASH state */
  641. hhash->State = HAL_HASH_STATE_BUSY;
  642. /* Check if initialization phase has already been performed */
  643. if(hhash->Phase == HAL_HASH_PHASE_READY)
  644. {
  645. /* Select the SHA1 mode and reset the HASH processor core, so that the HASH will be ready to compute
  646. the message digest of a new message */
  647. HASH->CR |= HASH_ALGOSELECTION_SHA1 | HASH_CR_INIT;
  648. }
  649. /* Set the phase */
  650. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  651. /* Configure the number of valid bits in last word of the message */
  652. __HAL_HASH_SET_NBVALIDBITS(Size);
  653. /* Write input buffer in data register */
  654. HASH_WriteData(pInBuffer, Size);
  655. /* Change the HASH state */
  656. hhash->State = HAL_HASH_STATE_READY;
  657. /* Process Unlocked */
  658. __HAL_UNLOCK(hhash);
  659. /* Return function status */
  660. return HAL_OK;
  661. }
  662. /**
  663. * @}
  664. */
  665. /** @defgroup HASH_Exported_Functions_Group3 HASH processing functions using interrupt mode
  666. * @brief processing functions using interrupt mode.
  667. *
  668. @verbatim
  669. ===============================================================================
  670. ##### HASH processing using interrupt mode functions #####
  671. ===============================================================================
  672. [..] This section provides functions allowing to calculate in interrupt mode
  673. the hash value using one of the following algorithms:
  674. (+) MD5
  675. (+) SHA1
  676. @endverbatim
  677. * @{
  678. */
  679. /**
  680. * @brief Initializes the HASH peripheral in MD5 mode then processes pInBuffer.
  681. * The digest is available in pOutBuffer.
  682. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  683. * the configuration information for HASH module
  684. * @param pInBuffer: Pointer to the input buffer (buffer to be hashed).
  685. * @param Size: Length of the input buffer in bytes.
  686. * If the Size is not multiple of 64 bytes, the padding is managed by hardware.
  687. * @param pOutBuffer: Pointer to the computed digest. Its size must be 16 bytes.
  688. * @retval HAL status
  689. */
  690. HAL_StatusTypeDef HAL_HASH_MD5_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer)
  691. {
  692. uint32_t inputaddr;
  693. uint32_t outputaddr;
  694. uint32_t buffercounter;
  695. uint32_t inputcounter;
  696. /* Process Locked */
  697. __HAL_LOCK(hhash);
  698. if(hhash->State == HAL_HASH_STATE_READY)
  699. {
  700. /* Change the HASH state */
  701. hhash->State = HAL_HASH_STATE_BUSY;
  702. hhash->HashInCount = Size;
  703. hhash->pHashInBuffPtr = pInBuffer;
  704. hhash->pHashOutBuffPtr = pOutBuffer;
  705. /* Check if initialization phase has already been performed */
  706. if(hhash->Phase == HAL_HASH_PHASE_READY)
  707. {
  708. /* Select the SHA1 mode */
  709. HASH->CR |= HASH_ALGOSELECTION_MD5;
  710. /* Reset the HASH processor core, so that the HASH will be ready to compute
  711. the message digest of a new message */
  712. HASH->CR |= HASH_CR_INIT;
  713. }
  714. /* Reset interrupt counter */
  715. hhash->HashITCounter = 0;
  716. /* Set the phase */
  717. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  718. /* Process Unlocked */
  719. __HAL_UNLOCK(hhash);
  720. /* Enable Interrupts */
  721. HASH->IMR = (HASH_IT_DINI | HASH_IT_DCI);
  722. /* Return function status */
  723. return HAL_OK;
  724. }
  725. if(__HAL_HASH_GET_FLAG(HASH_FLAG_DCIS))
  726. {
  727. outputaddr = (uint32_t)hhash->pHashOutBuffPtr;
  728. /* Read the Output block from the Output FIFO */
  729. *(uint32_t*)(outputaddr) = __REV(HASH->HR[0]);
  730. outputaddr+=4;
  731. *(uint32_t*)(outputaddr) = __REV(HASH->HR[1]);
  732. outputaddr+=4;
  733. *(uint32_t*)(outputaddr) = __REV(HASH->HR[2]);
  734. outputaddr+=4;
  735. *(uint32_t*)(outputaddr) = __REV(HASH->HR[3]);
  736. if(hhash->HashInCount == 0)
  737. {
  738. /* Disable Interrupts */
  739. HASH->IMR = 0;
  740. /* Change the HASH state */
  741. hhash->State = HAL_HASH_STATE_READY;
  742. /* Call digest computation complete callback */
  743. HAL_HASH_DgstCpltCallback(hhash);
  744. /* Process Unlocked */
  745. __HAL_UNLOCK(hhash);
  746. /* Return function status */
  747. return HAL_OK;
  748. }
  749. }
  750. if(__HAL_HASH_GET_FLAG(HASH_FLAG_DINIS))
  751. {
  752. if(hhash->HashInCount >= 68)
  753. {
  754. inputaddr = (uint32_t)hhash->pHashInBuffPtr;
  755. /* Write the Input block in the Data IN register */
  756. for(buffercounter = 0; buffercounter < 64; buffercounter+=4)
  757. {
  758. HASH->DIN = *(uint32_t*)inputaddr;
  759. inputaddr+=4;
  760. }
  761. if(hhash->HashITCounter == 0)
  762. {
  763. HASH->DIN = *(uint32_t*)inputaddr;
  764. if(hhash->HashInCount >= 68)
  765. {
  766. /* Decrement buffer counter */
  767. hhash->HashInCount -= 68;
  768. hhash->pHashInBuffPtr+= 68;
  769. }
  770. else
  771. {
  772. hhash->HashInCount = 0;
  773. hhash->pHashInBuffPtr+= hhash->HashInCount;
  774. }
  775. /* Set Interrupt counter */
  776. hhash->HashITCounter = 1;
  777. }
  778. else
  779. {
  780. /* Decrement buffer counter */
  781. hhash->HashInCount -= 64;
  782. hhash->pHashInBuffPtr+= 64;
  783. }
  784. }
  785. else
  786. {
  787. /* Get the buffer address */
  788. inputaddr = (uint32_t)hhash->pHashInBuffPtr;
  789. /* Get the buffer counter */
  790. inputcounter = hhash->HashInCount;
  791. /* Disable Interrupts */
  792. HASH->IMR &= ~(HASH_IT_DINI);
  793. /* Configure the number of valid bits in last word of the message */
  794. __HAL_HASH_SET_NBVALIDBITS(inputcounter);
  795. if((inputcounter > 4) && (inputcounter%4))
  796. {
  797. inputcounter = (inputcounter+4-inputcounter%4);
  798. }
  799. else if ((inputcounter < 4) && (inputcounter != 0))
  800. {
  801. inputcounter = 4;
  802. }
  803. /* Write the Input block in the Data IN register */
  804. for(buffercounter = 0; buffercounter < inputcounter/4; buffercounter++)
  805. {
  806. HASH->DIN = *(uint32_t*)inputaddr;
  807. inputaddr+=4;
  808. }
  809. /* Start the digest calculation */
  810. __HAL_HASH_START_DIGEST();
  811. /* Reset buffer counter */
  812. hhash->HashInCount = 0;
  813. /* Call Input data transfer complete callback */
  814. HAL_HASH_InCpltCallback(hhash);
  815. }
  816. }
  817. /* Process Unlocked */
  818. __HAL_UNLOCK(hhash);
  819. /* Return function status */
  820. return HAL_OK;
  821. }
  822. /**
  823. * @brief Initializes the HASH peripheral in SHA1 mode then processes pInBuffer.
  824. * The digest is available in pOutBuffer.
  825. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  826. * the configuration information for HASH module
  827. * @param pInBuffer: Pointer to the input buffer (buffer to be hashed).
  828. * @param Size: Length of the input buffer in bytes.
  829. * If the Size is not multiple of 64 bytes, the padding is managed by hardware.
  830. * @param pOutBuffer: Pointer to the computed digest. Its size must be 20 bytes.
  831. * @retval HAL status
  832. */
  833. HAL_StatusTypeDef HAL_HASH_SHA1_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer)
  834. {
  835. uint32_t inputaddr;
  836. uint32_t outputaddr;
  837. uint32_t buffercounter;
  838. uint32_t inputcounter;
  839. /* Process Locked */
  840. __HAL_LOCK(hhash);
  841. if(hhash->State == HAL_HASH_STATE_READY)
  842. {
  843. /* Change the HASH state */
  844. hhash->State = HAL_HASH_STATE_BUSY;
  845. hhash->HashInCount = Size;
  846. hhash->pHashInBuffPtr = pInBuffer;
  847. hhash->pHashOutBuffPtr = pOutBuffer;
  848. /* Check if initialization phase has already been performed */
  849. if(hhash->Phase == HAL_HASH_PHASE_READY)
  850. {
  851. /* Select the SHA1 mode */
  852. HASH->CR |= HASH_ALGOSELECTION_SHA1;
  853. /* Reset the HASH processor core, so that the HASH will be ready to compute
  854. the message digest of a new message */
  855. HASH->CR |= HASH_CR_INIT;
  856. }
  857. /* Reset interrupt counter */
  858. hhash->HashITCounter = 0;
  859. /* Set the phase */
  860. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  861. /* Process Unlocked */
  862. __HAL_UNLOCK(hhash);
  863. /* Enable Interrupts */
  864. HASH->IMR = (HASH_IT_DINI | HASH_IT_DCI);
  865. /* Return function status */
  866. return HAL_OK;
  867. }
  868. if(__HAL_HASH_GET_FLAG(HASH_FLAG_DCIS))
  869. {
  870. outputaddr = (uint32_t)hhash->pHashOutBuffPtr;
  871. /* Read the Output block from the Output FIFO */
  872. *(uint32_t*)(outputaddr) = __REV(HASH->HR[0]);
  873. outputaddr+=4;
  874. *(uint32_t*)(outputaddr) = __REV(HASH->HR[1]);
  875. outputaddr+=4;
  876. *(uint32_t*)(outputaddr) = __REV(HASH->HR[2]);
  877. outputaddr+=4;
  878. *(uint32_t*)(outputaddr) = __REV(HASH->HR[3]);
  879. outputaddr+=4;
  880. *(uint32_t*)(outputaddr) = __REV(HASH->HR[4]);
  881. if(hhash->HashInCount == 0)
  882. {
  883. /* Disable Interrupts */
  884. HASH->IMR = 0;
  885. /* Change the HASH state */
  886. hhash->State = HAL_HASH_STATE_READY;
  887. /* Call digest computation complete callback */
  888. HAL_HASH_DgstCpltCallback(hhash);
  889. /* Process Unlocked */
  890. __HAL_UNLOCK(hhash);
  891. /* Return function status */
  892. return HAL_OK;
  893. }
  894. }
  895. if(__HAL_HASH_GET_FLAG(HASH_FLAG_DINIS))
  896. {
  897. if(hhash->HashInCount >= 68)
  898. {
  899. inputaddr = (uint32_t)hhash->pHashInBuffPtr;
  900. /* Write the Input block in the Data IN register */
  901. for(buffercounter = 0; buffercounter < 64; buffercounter+=4)
  902. {
  903. HASH->DIN = *(uint32_t*)inputaddr;
  904. inputaddr+=4;
  905. }
  906. if(hhash->HashITCounter == 0)
  907. {
  908. HASH->DIN = *(uint32_t*)inputaddr;
  909. if(hhash->HashInCount >= 68)
  910. {
  911. /* Decrement buffer counter */
  912. hhash->HashInCount -= 68;
  913. hhash->pHashInBuffPtr+= 68;
  914. }
  915. else
  916. {
  917. hhash->HashInCount = 0;
  918. hhash->pHashInBuffPtr+= hhash->HashInCount;
  919. }
  920. /* Set Interrupt counter */
  921. hhash->HashITCounter = 1;
  922. }
  923. else
  924. {
  925. /* Decrement buffer counter */
  926. hhash->HashInCount -= 64;
  927. hhash->pHashInBuffPtr+= 64;
  928. }
  929. }
  930. else
  931. {
  932. /* Get the buffer address */
  933. inputaddr = (uint32_t)hhash->pHashInBuffPtr;
  934. /* Get the buffer counter */
  935. inputcounter = hhash->HashInCount;
  936. /* Disable Interrupts */
  937. HASH->IMR &= ~(HASH_IT_DINI);
  938. /* Configure the number of valid bits in last word of the message */
  939. __HAL_HASH_SET_NBVALIDBITS(inputcounter);
  940. if((inputcounter > 4) && (inputcounter%4))
  941. {
  942. inputcounter = (inputcounter+4-inputcounter%4);
  943. }
  944. else if ((inputcounter < 4) && (inputcounter != 0))
  945. {
  946. inputcounter = 4;
  947. }
  948. /* Write the Input block in the Data IN register */
  949. for(buffercounter = 0; buffercounter < inputcounter/4; buffercounter++)
  950. {
  951. HASH->DIN = *(uint32_t*)inputaddr;
  952. inputaddr+=4;
  953. }
  954. /* Start the digest calculation */
  955. __HAL_HASH_START_DIGEST();
  956. /* Reset buffer counter */
  957. hhash->HashInCount = 0;
  958. /* Call Input data transfer complete callback */
  959. HAL_HASH_InCpltCallback(hhash);
  960. }
  961. }
  962. /* Process Unlocked */
  963. __HAL_UNLOCK(hhash);
  964. /* Return function status */
  965. return HAL_OK;
  966. }
  967. /**
  968. * @brief This function handles HASH interrupt request.
  969. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  970. * the configuration information for HASH module
  971. * @retval None
  972. */
  973. void HAL_HASH_IRQHandler(HASH_HandleTypeDef *hhash)
  974. {
  975. switch(HASH->CR & HASH_CR_ALGO)
  976. {
  977. case HASH_ALGOSELECTION_MD5:
  978. HAL_HASH_MD5_Start_IT(hhash, NULL, 0, NULL);
  979. break;
  980. case HASH_ALGOSELECTION_SHA1:
  981. HAL_HASH_SHA1_Start_IT(hhash, NULL, 0, NULL);
  982. break;
  983. default:
  984. break;
  985. }
  986. }
  987. /**
  988. * @}
  989. */
  990. /** @defgroup HASH_Exported_Functions_Group4 HASH processing functions using DMA mode
  991. * @brief processing functions using DMA mode.
  992. *
  993. @verbatim
  994. ===============================================================================
  995. ##### HASH processing using DMA mode functions #####
  996. ===============================================================================
  997. [..] This section provides functions allowing to calculate in DMA mode
  998. the hash value using one of the following algorithms:
  999. (+) MD5
  1000. (+) SHA1
  1001. @endverbatim
  1002. * @{
  1003. */
  1004. /**
  1005. * @brief Initializes the HASH peripheral in MD5 mode then enables DMA to
  1006. control data transfer. Use HAL_HASH_MD5_Finish() to get the digest.
  1007. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  1008. * the configuration information for HASH module
  1009. * @param pInBuffer: Pointer to the input buffer (buffer to be hashed).
  1010. * @param Size: Length of the input buffer in bytes.
  1011. * If the Size is not multiple of 64 bytes, the padding is managed by hardware.
  1012. * @retval HAL status
  1013. */
  1014. HAL_StatusTypeDef HAL_HASH_MD5_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  1015. {
  1016. uint32_t inputaddr = (uint32_t)pInBuffer;
  1017. /* Process Locked */
  1018. __HAL_LOCK(hhash);
  1019. /* Change the HASH state */
  1020. hhash->State = HAL_HASH_STATE_BUSY;
  1021. /* Check if initialization phase has already been performed */
  1022. if(hhash->Phase == HAL_HASH_PHASE_READY)
  1023. {
  1024. /* Select the MD5 mode and reset the HASH processor core, so that the HASH will be ready to compute
  1025. the message digest of a new message */
  1026. HASH->CR |= HASH_ALGOSELECTION_MD5 | HASH_CR_INIT;
  1027. }
  1028. /* Configure the number of valid bits in last word of the message */
  1029. __HAL_HASH_SET_NBVALIDBITS(Size);
  1030. /* Set the phase */
  1031. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  1032. /* Set the HASH DMA transfer complete callback */
  1033. hhash->hdmain->XferCpltCallback = HASH_DMAXferCplt;
  1034. /* Set the DMA error callback */
  1035. hhash->hdmain->XferErrorCallback = HASH_DMAError;
  1036. /* Enable the DMA In DMA Stream */
  1037. HAL_DMA_Start_IT(hhash->hdmain, inputaddr, (uint32_t)&HASH->DIN, (Size%4 ? (Size+3)/4:Size/4));
  1038. /* Enable DMA requests */
  1039. HASH->CR |= (HASH_CR_DMAE);
  1040. /* Process Unlocked */
  1041. __HAL_UNLOCK(hhash);
  1042. /* Return function status */
  1043. return HAL_OK;
  1044. }
  1045. /**
  1046. * @brief Returns the computed digest in MD5 mode
  1047. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  1048. * the configuration information for HASH module
  1049. * @param pOutBuffer: Pointer to the computed digest. Its size must be 16 bytes.
  1050. * @param Timeout: Timeout value
  1051. * @retval HAL status
  1052. */
  1053. HAL_StatusTypeDef HAL_HASH_MD5_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout)
  1054. {
  1055. uint32_t tickstart = 0;
  1056. /* Process Locked */
  1057. __HAL_LOCK(hhash);
  1058. /* Change HASH peripheral state */
  1059. hhash->State = HAL_HASH_STATE_BUSY;
  1060. /* Get tick */
  1061. tickstart = HAL_GetTick();
  1062. while(HAL_IS_BIT_CLR(HASH->SR, HASH_FLAG_DCIS))
  1063. {
  1064. /* Check for the Timeout */
  1065. if(Timeout != HAL_MAX_DELAY)
  1066. {
  1067. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1068. {
  1069. /* Change state */
  1070. hhash->State = HAL_HASH_STATE_TIMEOUT;
  1071. /* Process Unlocked */
  1072. __HAL_UNLOCK(hhash);
  1073. return HAL_TIMEOUT;
  1074. }
  1075. }
  1076. }
  1077. /* Read the message digest */
  1078. HASH_GetDigest(pOutBuffer, 16);
  1079. /* Change HASH peripheral state */
  1080. hhash->State = HAL_HASH_STATE_READY;
  1081. /* Process Unlocked */
  1082. __HAL_UNLOCK(hhash);
  1083. /* Return function status */
  1084. return HAL_OK;
  1085. }
  1086. /**
  1087. * @brief Initializes the HASH peripheral in SHA1 mode then enables DMA to
  1088. control data transfer. Use HAL_HASH_SHA1_Finish() to get the digest.
  1089. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  1090. * the configuration information for HASH module
  1091. * @param pInBuffer: Pointer to the input buffer (buffer to be hashed).
  1092. * @param Size: Length of the input buffer in bytes.
  1093. * If the Size is not multiple of 64 bytes, the padding is managed by hardware.
  1094. * @retval HAL status
  1095. */
  1096. HAL_StatusTypeDef HAL_HASH_SHA1_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  1097. {
  1098. uint32_t inputaddr = (uint32_t)pInBuffer;
  1099. /* Process Locked */
  1100. __HAL_LOCK(hhash);
  1101. /* Change the HASH state */
  1102. hhash->State = HAL_HASH_STATE_BUSY;
  1103. /* Check if initialization phase has already been performed */
  1104. if(hhash->Phase == HAL_HASH_PHASE_READY)
  1105. {
  1106. /* Select the SHA1 mode and reset the HASH processor core, so that the HASH will be ready to compute
  1107. the message digest of a new message */
  1108. HASH->CR |= HASH_ALGOSELECTION_SHA1;
  1109. HASH->CR |= HASH_CR_INIT;
  1110. }
  1111. /* Configure the number of valid bits in last word of the message */
  1112. __HAL_HASH_SET_NBVALIDBITS(Size);
  1113. /* Set the phase */
  1114. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  1115. /* Set the HASH DMA transfer complete callback */
  1116. hhash->hdmain->XferCpltCallback = HASH_DMAXferCplt;
  1117. /* Set the DMA error callback */
  1118. hhash->hdmain->XferErrorCallback = HASH_DMAError;
  1119. /* Enable the DMA In DMA Stream */
  1120. HAL_DMA_Start_IT(hhash->hdmain, inputaddr, (uint32_t)&HASH->DIN, (Size%4 ? (Size+3)/4:Size/4));
  1121. /* Enable DMA requests */
  1122. HASH->CR |= (HASH_CR_DMAE);
  1123. /* Process Unlocked */
  1124. __HAL_UNLOCK(hhash);
  1125. /* Return function status */
  1126. return HAL_OK;
  1127. }
  1128. /**
  1129. * @brief Returns the computed digest in SHA1 mode.
  1130. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  1131. * the configuration information for HASH module
  1132. * @param pOutBuffer: Pointer to the computed digest. Its size must be 20 bytes.
  1133. * @param Timeout: Timeout value
  1134. * @retval HAL status
  1135. */
  1136. HAL_StatusTypeDef HAL_HASH_SHA1_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout)
  1137. {
  1138. uint32_t tickstart = 0;
  1139. /* Process Locked */
  1140. __HAL_LOCK(hhash);
  1141. /* Change HASH peripheral state */
  1142. hhash->State = HAL_HASH_STATE_BUSY;
  1143. /* Get tick */
  1144. tickstart = HAL_GetTick();
  1145. while(HAL_IS_BIT_CLR(HASH->SR, HASH_FLAG_DCIS))
  1146. {
  1147. /* Check for the Timeout */
  1148. if(Timeout != HAL_MAX_DELAY)
  1149. {
  1150. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1151. {
  1152. /* Change state */
  1153. hhash->State = HAL_HASH_STATE_TIMEOUT;
  1154. /* Process Unlocked */
  1155. __HAL_UNLOCK(hhash);
  1156. return HAL_TIMEOUT;
  1157. }
  1158. }
  1159. }
  1160. /* Read the message digest */
  1161. HASH_GetDigest(pOutBuffer, 20);
  1162. /* Change HASH peripheral state */
  1163. hhash->State = HAL_HASH_STATE_READY;
  1164. /* Process UnLock */
  1165. __HAL_UNLOCK(hhash);
  1166. /* Return function status */
  1167. return HAL_OK;
  1168. }
  1169. /**
  1170. * @}
  1171. */
  1172. /** @defgroup HASH_Exported_Functions_Group5 HASH-MAC (HMAC) processing functions using polling mode
  1173. * @brief HMAC processing functions using polling mode .
  1174. *
  1175. @verbatim
  1176. ===============================================================================
  1177. ##### HMAC processing using polling mode functions #####
  1178. ===============================================================================
  1179. [..] This section provides functions allowing to calculate in polling mode
  1180. the HMAC value using one of the following algorithms:
  1181. (+) MD5
  1182. (+) SHA1
  1183. @endverbatim
  1184. * @{
  1185. */
  1186. /**
  1187. * @brief Initializes the HASH peripheral in HMAC MD5 mode
  1188. * then processes pInBuffer. The digest is available in pOutBuffer
  1189. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  1190. * the configuration information for HASH module
  1191. * @param pInBuffer: Pointer to the input buffer (buffer to be hashed).
  1192. * @param Size: Length of the input buffer in bytes.
  1193. * If the Size is not multiple of 64 bytes, the padding is managed by hardware.
  1194. * @param pOutBuffer: Pointer to the computed digest. Its size must be 20 bytes.
  1195. * @param Timeout: Timeout value
  1196. * @retval HAL status
  1197. */
  1198. HAL_StatusTypeDef HAL_HMAC_MD5_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout)
  1199. {
  1200. uint32_t tickstart = 0;
  1201. /* Process Locked */
  1202. __HAL_LOCK(hhash);
  1203. /* Change the HASH state */
  1204. hhash->State = HAL_HASH_STATE_BUSY;
  1205. /* Check if initialization phase has already been performed */
  1206. if(hhash->Phase == HAL_HASH_PHASE_READY)
  1207. {
  1208. /* Check if key size is greater than 64 bytes */
  1209. if(hhash->Init.KeySize > 64)
  1210. {
  1211. /* Select the HMAC MD5 mode */
  1212. HASH->CR |= (HASH_ALGOSELECTION_MD5 | HASH_ALGOMODE_HMAC | HASH_HMAC_KEYTYPE_LONGKEY | HASH_CR_INIT);
  1213. }
  1214. else
  1215. {
  1216. /* Select the HMAC MD5 mode */
  1217. HASH->CR |= (HASH_ALGOSELECTION_MD5 | HASH_ALGOMODE_HMAC | HASH_CR_INIT);
  1218. }
  1219. }
  1220. /* Set the phase */
  1221. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  1222. /************************** STEP 1 ******************************************/
  1223. /* Configure the number of valid bits in last word of the message */
  1224. __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize);
  1225. /* Write input buffer in data register */
  1226. HASH_WriteData(hhash->Init.pKey, hhash->Init.KeySize);
  1227. /* Start the digest calculation */
  1228. __HAL_HASH_START_DIGEST();
  1229. /* Get tick */
  1230. tickstart = HAL_GetTick();
  1231. while(HAL_IS_BIT_SET(HASH->SR, HASH_FLAG_BUSY))
  1232. {
  1233. /* Check for the Timeout */
  1234. if(Timeout != HAL_MAX_DELAY)
  1235. {
  1236. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1237. {
  1238. /* Change state */
  1239. hhash->State = HAL_HASH_STATE_TIMEOUT;
  1240. /* Process Unlocked */
  1241. __HAL_UNLOCK(hhash);
  1242. return HAL_TIMEOUT;
  1243. }
  1244. }
  1245. }
  1246. /************************** STEP 2 ******************************************/
  1247. /* Configure the number of valid bits in last word of the message */
  1248. __HAL_HASH_SET_NBVALIDBITS(Size);
  1249. /* Write input buffer in data register */
  1250. HASH_WriteData(pInBuffer, Size);
  1251. /* Start the digest calculation */
  1252. __HAL_HASH_START_DIGEST();
  1253. /* Get tick */
  1254. tickstart = HAL_GetTick();
  1255. while(HAL_IS_BIT_SET(HASH->SR, HASH_FLAG_BUSY))
  1256. {
  1257. /* Check for the Timeout */
  1258. if(Timeout != HAL_MAX_DELAY)
  1259. {
  1260. if((HAL_GetTick() - tickstart ) > Timeout)
  1261. {
  1262. /* Change state */
  1263. hhash->State = HAL_HASH_STATE_TIMEOUT;
  1264. /* Process Unlocked */
  1265. __HAL_UNLOCK(hhash);
  1266. return HAL_TIMEOUT;
  1267. }
  1268. }
  1269. }
  1270. /************************** STEP 3 ******************************************/
  1271. /* Configure the number of valid bits in last word of the message */
  1272. __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize);
  1273. /* Write input buffer in data register */
  1274. HASH_WriteData(hhash->Init.pKey, hhash->Init.KeySize);
  1275. /* Start the digest calculation */
  1276. __HAL_HASH_START_DIGEST();
  1277. /* Get tick */
  1278. tickstart = HAL_GetTick();
  1279. while(HAL_IS_BIT_SET(HASH->SR, HASH_FLAG_BUSY))
  1280. {
  1281. /* Check for the Timeout */
  1282. if(Timeout != HAL_MAX_DELAY)
  1283. {
  1284. if((HAL_GetTick() - tickstart ) > Timeout)
  1285. {
  1286. /* Change state */
  1287. hhash->State = HAL_HASH_STATE_TIMEOUT;
  1288. /* Process Unlocked */
  1289. __HAL_UNLOCK(hhash);
  1290. return HAL_TIMEOUT;
  1291. }
  1292. }
  1293. }
  1294. /* Read the message digest */
  1295. HASH_GetDigest(pOutBuffer, 16);
  1296. /* Change the HASH state */
  1297. hhash->State = HAL_HASH_STATE_READY;
  1298. /* Process Unlocked */
  1299. __HAL_UNLOCK(hhash);
  1300. /* Return function status */
  1301. return HAL_OK;
  1302. }
  1303. /**
  1304. * @brief Initializes the HASH peripheral in HMAC SHA1 mode
  1305. * then processes pInBuffer. The digest is available in pOutBuffer.
  1306. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  1307. * the configuration information for HASH module
  1308. * @param pInBuffer: Pointer to the input buffer (buffer to be hashed).
  1309. * @param Size: Length of the input buffer in bytes.
  1310. * If the Size is not multiple of 64 bytes, the padding is managed by hardware.
  1311. * @param pOutBuffer: Pointer to the computed digest. Its size must be 20 bytes.
  1312. * @param Timeout: Timeout value
  1313. * @retval HAL status
  1314. */
  1315. HAL_StatusTypeDef HAL_HMAC_SHA1_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout)
  1316. {
  1317. uint32_t tickstart = 0;
  1318. /* Process Locked */
  1319. __HAL_LOCK(hhash);
  1320. /* Change the HASH state */
  1321. hhash->State = HAL_HASH_STATE_BUSY;
  1322. /* Check if initialization phase has already been performed */
  1323. if(hhash->Phase == HAL_HASH_PHASE_READY)
  1324. {
  1325. /* Check if key size is greater than 64 bytes */
  1326. if(hhash->Init.KeySize > 64)
  1327. {
  1328. /* Select the HMAC SHA1 mode */
  1329. HASH->CR |= (HASH_ALGOSELECTION_SHA1 | HASH_ALGOMODE_HMAC | HASH_HMAC_KEYTYPE_LONGKEY | HASH_CR_INIT);
  1330. }
  1331. else
  1332. {
  1333. /* Select the HMAC SHA1 mode */
  1334. HASH->CR |= (HASH_ALGOSELECTION_SHA1 | HASH_ALGOMODE_HMAC | HASH_CR_INIT);
  1335. }
  1336. }
  1337. /* Set the phase */
  1338. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  1339. /************************** STEP 1 ******************************************/
  1340. /* Configure the number of valid bits in last word of the message */
  1341. __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize);
  1342. /* Write input buffer in data register */
  1343. HASH_WriteData(hhash->Init.pKey, hhash->Init.KeySize);
  1344. /* Start the digest calculation */
  1345. __HAL_HASH_START_DIGEST();
  1346. /* Get tick */
  1347. tickstart = HAL_GetTick();
  1348. while(HAL_IS_BIT_SET(HASH->SR, HASH_FLAG_BUSY))
  1349. {
  1350. /* Check for the Timeout */
  1351. if(Timeout != HAL_MAX_DELAY)
  1352. {
  1353. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1354. {
  1355. /* Change state */
  1356. hhash->State = HAL_HASH_STATE_TIMEOUT;
  1357. /* Process Unlocked */
  1358. __HAL_UNLOCK(hhash);
  1359. return HAL_TIMEOUT;
  1360. }
  1361. }
  1362. }
  1363. /************************** STEP 2 ******************************************/
  1364. /* Configure the number of valid bits in last word of the message */
  1365. __HAL_HASH_SET_NBVALIDBITS(Size);
  1366. /* Write input buffer in data register */
  1367. HASH_WriteData(pInBuffer, Size);
  1368. /* Start the digest calculation */
  1369. __HAL_HASH_START_DIGEST();
  1370. /* Get tick */
  1371. tickstart = HAL_GetTick();
  1372. while(HAL_IS_BIT_SET(HASH->SR, HASH_FLAG_BUSY))
  1373. {
  1374. /* Check for the Timeout */
  1375. if(Timeout != HAL_MAX_DELAY)
  1376. {
  1377. if((HAL_GetTick() - tickstart ) > Timeout)
  1378. {
  1379. /* Change state */
  1380. hhash->State = HAL_HASH_STATE_TIMEOUT;
  1381. /* Process Unlocked */
  1382. __HAL_UNLOCK(hhash);
  1383. return HAL_TIMEOUT;
  1384. }
  1385. }
  1386. }
  1387. /************************** STEP 3 ******************************************/
  1388. /* Configure the number of valid bits in last word of the message */
  1389. __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize);
  1390. /* Write input buffer in data register */
  1391. HASH_WriteData(hhash->Init.pKey, hhash->Init.KeySize);
  1392. /* Start the digest calculation */
  1393. __HAL_HASH_START_DIGEST();
  1394. /* Get tick */
  1395. tickstart = HAL_GetTick();
  1396. while(HAL_IS_BIT_SET(HASH->SR, HASH_FLAG_BUSY))
  1397. {
  1398. /* Check for the Timeout */
  1399. if(Timeout != HAL_MAX_DELAY)
  1400. {
  1401. if((HAL_GetTick() - tickstart ) > Timeout)
  1402. {
  1403. /* Change state */
  1404. hhash->State = HAL_HASH_STATE_TIMEOUT;
  1405. /* Process Unlocked */
  1406. __HAL_UNLOCK(hhash);
  1407. return HAL_TIMEOUT;
  1408. }
  1409. }
  1410. }
  1411. /* Read the message digest */
  1412. HASH_GetDigest(pOutBuffer, 20);
  1413. /* Change the HASH state */
  1414. hhash->State = HAL_HASH_STATE_READY;
  1415. /* Process Unlocked */
  1416. __HAL_UNLOCK(hhash);
  1417. /* Return function status */
  1418. return HAL_OK;
  1419. }
  1420. /**
  1421. * @}
  1422. */
  1423. /** @defgroup HASH_Exported_Functions_Group6 HASH-MAC (HMAC) processing functions using DMA mode
  1424. * @brief HMAC processing functions using DMA mode .
  1425. *
  1426. @verbatim
  1427. ===============================================================================
  1428. ##### HMAC processing using DMA mode functions #####
  1429. ===============================================================================
  1430. [..] This section provides functions allowing to calculate in DMA mode
  1431. the HMAC value using one of the following algorithms:
  1432. (+) MD5
  1433. (+) SHA1
  1434. @endverbatim
  1435. * @{
  1436. */
  1437. /**
  1438. * @brief Initializes the HASH peripheral in HMAC MD5 mode
  1439. * then enables DMA to control data transfer.
  1440. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  1441. * the configuration information for HASH module
  1442. * @param pInBuffer: Pointer to the input buffer (buffer to be hashed).
  1443. * @param Size: Length of the input buffer in bytes.
  1444. * If the Size is not multiple of 64 bytes, the padding is managed by hardware.
  1445. * @retval HAL status
  1446. */
  1447. HAL_StatusTypeDef HAL_HMAC_MD5_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  1448. {
  1449. uint32_t inputaddr = 0;
  1450. /* Process Locked */
  1451. __HAL_LOCK(hhash);
  1452. /* Change the HASH state */
  1453. hhash->State = HAL_HASH_STATE_BUSY;
  1454. /* Save buffer pointer and size in handle */
  1455. hhash->pHashInBuffPtr = pInBuffer;
  1456. hhash->HashBuffSize = Size;
  1457. hhash->HashInCount = 0;
  1458. /* Check if initialization phase has already been performed */
  1459. if(hhash->Phase == HAL_HASH_PHASE_READY)
  1460. {
  1461. /* Check if key size is greater than 64 bytes */
  1462. if(hhash->Init.KeySize > 64)
  1463. {
  1464. /* Select the HMAC MD5 mode */
  1465. HASH->CR |= (HASH_ALGOSELECTION_MD5 | HASH_ALGOMODE_HMAC | HASH_HMAC_KEYTYPE_LONGKEY | HASH_CR_INIT);
  1466. }
  1467. else
  1468. {
  1469. /* Select the HMAC MD5 mode */
  1470. HASH->CR |= (HASH_ALGOSELECTION_MD5 | HASH_ALGOMODE_HMAC | HASH_CR_INIT);
  1471. }
  1472. }
  1473. /* Set the phase */
  1474. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  1475. /* Configure the number of valid bits in last word of the message */
  1476. __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize);
  1477. /* Get the key address */
  1478. inputaddr = (uint32_t)(hhash->Init.pKey);
  1479. /* Set the HASH DMA transfer complete callback */
  1480. hhash->hdmain->XferCpltCallback = HASH_DMAXferCplt;
  1481. /* Set the DMA error callback */
  1482. hhash->hdmain->XferErrorCallback = HASH_DMAError;
  1483. /* Enable the DMA In DMA Stream */
  1484. HAL_DMA_Start_IT(hhash->hdmain, inputaddr, (uint32_t)&HASH->DIN, (hhash->Init.KeySize%4 ? (hhash->Init.KeySize+3)/4:hhash->Init.KeySize/4));
  1485. /* Enable DMA requests */
  1486. HASH->CR |= (HASH_CR_DMAE);
  1487. /* Process Unlocked */
  1488. __HAL_UNLOCK(hhash);
  1489. /* Return function status */
  1490. return HAL_OK;
  1491. }
  1492. /**
  1493. * @brief Initializes the HASH peripheral in HMAC SHA1 mode
  1494. * then enables DMA to control data transfer.
  1495. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  1496. * the configuration information for HASH module
  1497. * @param pInBuffer: Pointer to the input buffer (buffer to be hashed).
  1498. * @param Size: Length of the input buffer in bytes.
  1499. * If the Size is not multiple of 64 bytes, the padding is managed by hardware.
  1500. * @retval HAL status
  1501. */
  1502. HAL_StatusTypeDef HAL_HMAC_SHA1_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  1503. {
  1504. uint32_t inputaddr = 0;
  1505. /* Process Locked */
  1506. __HAL_LOCK(hhash);
  1507. /* Change the HASH state */
  1508. hhash->State = HAL_HASH_STATE_BUSY;
  1509. /* Save buffer pointer and size in handle */
  1510. hhash->pHashInBuffPtr = pInBuffer;
  1511. hhash->HashBuffSize = Size;
  1512. hhash->HashInCount = 0;
  1513. /* Check if initialization phase has already been performed */
  1514. if(hhash->Phase == HAL_HASH_PHASE_READY)
  1515. {
  1516. /* Check if key size is greater than 64 bytes */
  1517. if(hhash->Init.KeySize > 64)
  1518. {
  1519. /* Select the HMAC SHA1 mode */
  1520. HASH->CR |= (HASH_ALGOSELECTION_SHA1 | HASH_ALGOMODE_HMAC | HASH_HMAC_KEYTYPE_LONGKEY | HASH_CR_INIT);
  1521. }
  1522. else
  1523. {
  1524. /* Select the HMAC SHA1 mode */
  1525. HASH->CR |= (HASH_ALGOSELECTION_SHA1 | HASH_ALGOMODE_HMAC | HASH_CR_INIT);
  1526. }
  1527. }
  1528. /* Set the phase */
  1529. hhash->Phase = HAL_HASH_PHASE_PROCESS;
  1530. /* Configure the number of valid bits in last word of the message */
  1531. __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize);
  1532. /* Get the key address */
  1533. inputaddr = (uint32_t)(hhash->Init.pKey);
  1534. /* Set the HASH DMA transfer complete callback */
  1535. hhash->hdmain->XferCpltCallback = HASH_DMAXferCplt;
  1536. /* Set the DMA error callback */
  1537. hhash->hdmain->XferErrorCallback = HASH_DMAError;
  1538. /* Enable the DMA In DMA Stream */
  1539. HAL_DMA_Start_IT(hhash->hdmain, inputaddr, (uint32_t)&HASH->DIN, (hhash->Init.KeySize%4 ? (hhash->Init.KeySize+3)/4:hhash->Init.KeySize/4));
  1540. /* Enable DMA requests */
  1541. HASH->CR |= (HASH_CR_DMAE);
  1542. /* Process Unlocked */
  1543. __HAL_UNLOCK(hhash);
  1544. /* Return function status */
  1545. return HAL_OK;
  1546. }
  1547. /**
  1548. * @}
  1549. */
  1550. /** @defgroup HASH_Exported_Functions_Group7 Peripheral State functions
  1551. * @brief Peripheral State functions.
  1552. *
  1553. @verbatim
  1554. ===============================================================================
  1555. ##### Peripheral State functions #####
  1556. ===============================================================================
  1557. [..]
  1558. This subsection permits to get in run-time the status of the peripheral.
  1559. @endverbatim
  1560. * @{
  1561. */
  1562. /**
  1563. * @brief return the HASH state
  1564. * @param hhash: pointer to a HASH_HandleTypeDef structure that contains
  1565. * the configuration information for HASH module
  1566. * @retval HAL state
  1567. */
  1568. HAL_HASH_StateTypeDef HAL_HASH_GetState(HASH_HandleTypeDef *hhash)
  1569. {
  1570. return hhash->State;
  1571. }
  1572. /**
  1573. * @}
  1574. */
  1575. /**
  1576. * @}
  1577. */
  1578. #endif /* STM32F415xx || STM32F417xx || STM32F437xx || STM32F439xx || STM32F479xx */
  1579. #endif /* HAL_HASH_MODULE_ENABLED */
  1580. /**
  1581. * @}
  1582. */
  1583. /**
  1584. * @}
  1585. */
  1586. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/