Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

3589 rader
132 KiB

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