You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1639 lines
50 KiB

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