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

917 lines
41 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_hash_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended HASH HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the HASH peripheral for SHA-224 and SHA-256
  8. * alogrithms:
  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. * Additionally, this file provides functions to manage HMAC
  13. * multi-buffer DMA-based processing for MD-5, SHA-1, SHA-224
  14. * and SHA-256.
  15. *
  16. *
  17. @verbatim
  18. ===============================================================================
  19. ##### HASH peripheral extended features #####
  20. ===============================================================================
  21. [..]
  22. The SHA-224 and SHA-256 HASH and HMAC processing can be carried out exactly
  23. the same way as for SHA-1 or MD-5 algorithms.
  24. (#) Three modes are available.
  25. (##) Polling mode: processing APIs are blocking functions
  26. i.e. they process the data and wait till the digest computation is finished,
  27. e.g. HAL_HASHEx_xxx_Start()
  28. (##) Interrupt mode: processing APIs are not blocking functions
  29. i.e. they process the data under interrupt,
  30. e.g. HAL_HASHEx_xxx_Start_IT()
  31. (##) DMA mode: processing APIs are not blocking functions and the CPU is
  32. not used for data transfer i.e. the data transfer is ensured by DMA,
  33. e.g. HAL_HASHEx_xxx_Start_DMA(). Note that in DMA mode, a call to
  34. HAL_HASHEx_xxx_Finish() is then required to retrieve the digest.
  35. (#)Multi-buffer processing is possible in polling and DMA mode.
  36. (##) In polling mode, only multi-buffer HASH processing is possible.
  37. API HAL_HASHEx_xxx_Accumulate() must be called for each input buffer, except for the last one.
  38. User must resort to HAL_HASHEx_xxx_Start() to enter the last one and retrieve as
  39. well the computed digest.
  40. (##) In DMA mode, multi-buffer HASH and HMAC processing are possible.
  41. (+++) HASH processing: once initialization is done, MDMAT bit must be set thru __HAL_HASH_SET_MDMAT() macro.
  42. From that point, each buffer can be fed to the IP thru HAL_HASHEx_xxx_Start_DMA() API.
  43. Before entering the last buffer, reset the MDMAT bit with __HAL_HASH_RESET_MDMAT()
  44. macro then wrap-up the HASH processing in feeding the last input buffer thru the
  45. same API HAL_HASHEx_xxx_Start_DMA(). The digest can then be retrieved with a call to
  46. API HAL_HASHEx_xxx_Finish().
  47. (+++) HMAC processing (MD-5, SHA-1, SHA-224 and SHA-256 must all resort to
  48. extended functions): after initialization, the key and the first input buffer are entered
  49. in the IP with the API HAL_HMACEx_xxx_Step1_2_DMA(). This carries out HMAC step 1 and
  50. starts step 2.
  51. The following buffers are next entered with the API HAL_HMACEx_xxx_Step2_DMA(). At this
  52. point, the HMAC processing is still carrying out step 2.
  53. Then, step 2 for the last input buffer and step 3 are carried out by a single call
  54. to HAL_HMACEx_xxx_Step2_3_DMA().
  55. The digest can finally be retrieved with a call to API HAL_HASH_xxx_Finish() for
  56. MD-5 and SHA-1, to HAL_HASHEx_xxx_Finish() for SHA-224 and SHA-256.
  57. @endverbatim
  58. ******************************************************************************
  59. * @attention
  60. *
  61. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  62. * All rights reserved.</center></h2>
  63. *
  64. * This software component is licensed by ST under BSD 3-Clause license,
  65. * the "License"; You may not use this file except in compliance with the
  66. * License. You may obtain a copy of the License at:
  67. * opensource.org/licenses/BSD-3-Clause
  68. *
  69. ******************************************************************************
  70. */
  71. /* Includes ------------------------------------------------------------------*/
  72. #include "stm32f4xx_hal.h"
  73. /** @addtogroup STM32F4xx_HAL_Driver
  74. * @{
  75. */
  76. #if defined (HASH)
  77. /** @defgroup HASHEx HASHEx
  78. * @brief HASH HAL extended module driver.
  79. * @{
  80. */
  81. #ifdef HAL_HASH_MODULE_ENABLED
  82. /* Private typedef -----------------------------------------------------------*/
  83. /* Private define ------------------------------------------------------------*/
  84. /* Private functions ---------------------------------------------------------*/
  85. #if defined (HASH_CR_MDMAT)
  86. /** @defgroup HASHEx_Exported_Functions HASH Extended Exported Functions
  87. * @{
  88. */
  89. /** @defgroup HASHEx_Exported_Functions_Group1 HASH extended processing functions in polling mode
  90. * @brief HASH extended processing functions using polling mode.
  91. *
  92. @verbatim
  93. ===============================================================================
  94. ##### Polling mode HASH extended processing functions #####
  95. ===============================================================================
  96. [..] This section provides functions allowing to calculate in polling mode
  97. the hash value using one of the following algorithms:
  98. (+) SHA224
  99. (++) HAL_HASHEx_SHA224_Start()
  100. (++) HAL_HASHEx_SHA224_Accumulate()
  101. (+) SHA256
  102. (++) HAL_HASHEx_SHA256_Start()
  103. (++) HAL_HASHEx_SHA256_Accumulate()
  104. [..] For a single buffer to be hashed, user can resort to HAL_HASH_xxx_Start().
  105. [..] In case of multi-buffer HASH processing (a single digest is computed while
  106. several buffers are fed to the IP), the user can resort to successive calls
  107. to HAL_HASHEx_xxx_Accumulate() and wrap-up the digest computation by a call
  108. to HAL_HASHEx_xxx_Start().
  109. @endverbatim
  110. * @{
  111. */
  112. /**
  113. * @brief Initialize the HASH peripheral in SHA224 mode, next process pInBuffer then
  114. * read the computed digest.
  115. * @note Digest is available in pOutBuffer.
  116. * @param hhash: HASH handle.
  117. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  118. * @param Size: length of the input buffer in bytes.
  119. * @param pOutBuffer: pointer to the computed digest. Digest size is 28 bytes.
  120. * @param Timeout: Timeout value
  121. * @retval HAL status
  122. */
  123. HAL_StatusTypeDef HAL_HASHEx_SHA224_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout)
  124. {
  125. return HASH_Start(hhash, pInBuffer, Size, pOutBuffer, Timeout, HASH_ALGOSELECTION_SHA224);
  126. }
  127. /**
  128. * @brief If not already done, initialize the HASH peripheral in SHA224 mode then
  129. * processes pInBuffer.
  130. * @note Consecutive calls to HAL_HASHEx_SHA224_Accumulate() can be used to feed
  131. * several input buffers back-to-back to the IP that will yield a single
  132. * HASH signature once all buffers have been entered. Wrap-up of input
  133. * buffers feeding and retrieval of digest is done by a call to
  134. * HAL_HASHEx_SHA224_Start().
  135. * @note Field hhash->Phase of HASH handle is tested to check whether or not
  136. * the IP has already been initialized.
  137. * @note Digest is not retrieved by this API, user must resort to HAL_HASHEx_SHA224_Start()
  138. * to read it, feeding at the same time the last input buffer to the IP.
  139. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  140. * HASH digest computation is corrupted. Only HAL_HASHEx_SHA224_Start() is able
  141. * to manage the ending buffer with a length in bytes not a multiple of 4.
  142. * @param hhash: HASH handle.
  143. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  144. * @param Size: length of the input buffer in bytes, must be a multiple of 4.
  145. * @retval HAL status
  146. */
  147. HAL_StatusTypeDef HAL_HASHEx_SHA224_Accumulate(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  148. {
  149. return HASH_Accumulate(hhash, pInBuffer, Size,HASH_ALGOSELECTION_SHA224);
  150. }
  151. /**
  152. * @brief Initialize the HASH peripheral in SHA256 mode, next process pInBuffer then
  153. * read the computed digest.
  154. * @note Digest is available in pOutBuffer.
  155. * @param hhash: HASH handle.
  156. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  157. * @param Size: length of the input buffer in bytes.
  158. * @param pOutBuffer: pointer to the computed digest. Digest size is 32 bytes.
  159. * @param Timeout: Timeout value
  160. * @retval HAL status
  161. */
  162. HAL_StatusTypeDef HAL_HASHEx_SHA256_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout)
  163. {
  164. return HASH_Start(hhash, pInBuffer, Size, pOutBuffer, Timeout, HASH_ALGOSELECTION_SHA256);
  165. }
  166. /**
  167. * @brief If not already done, initialize the HASH peripheral in SHA256 mode then
  168. * processes pInBuffer.
  169. * @note Consecutive calls to HAL_HASHEx_SHA256_Accumulate() can be used to feed
  170. * several input buffers back-to-back to the IP that will yield a single
  171. * HASH signature once all buffers have been entered. Wrap-up of input
  172. * buffers feeding and retrieval of digest is done by a call to
  173. * HAL_HASHEx_SHA256_Start().
  174. * @note Field hhash->Phase of HASH handle is tested to check whether or not
  175. * the IP has already been initialized.
  176. * @note Digest is not retrieved by this API, user must resort to HAL_HASHEx_SHA256_Start()
  177. * to read it, feeding at the same time the last input buffer to the IP.
  178. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  179. * HASH digest computation is corrupted. Only HAL_HASHEx_SHA256_Start() is able
  180. * to manage the ending buffer with a length in bytes not a multiple of 4.
  181. * @param hhash: HASH handle.
  182. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  183. * @param Size: length of the input buffer in bytes, must be a multiple of 4.
  184. * @retval HAL status
  185. */
  186. HAL_StatusTypeDef HAL_HASHEx_SHA256_Accumulate(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  187. {
  188. return HASH_Accumulate(hhash, pInBuffer, Size,HASH_ALGOSELECTION_SHA256);
  189. }
  190. /**
  191. * @}
  192. */
  193. /** @defgroup HASHEx_Exported_Functions_Group2 HASH extended processing functions in interrupt mode
  194. * @brief HASH extended processing functions using interrupt mode.
  195. *
  196. @verbatim
  197. ===============================================================================
  198. ##### Interruption mode HASH extended processing functions #####
  199. ===============================================================================
  200. [..] This section provides functions allowing to calculate in interrupt mode
  201. the hash value using one of the following algorithms:
  202. (+) SHA224
  203. (++) HAL_HASHEx_SHA224_Start_IT()
  204. (+) SHA256
  205. (++) HAL_HASHEx_SHA256_Start_IT()
  206. @endverbatim
  207. * @{
  208. */
  209. /**
  210. * @brief Initialize the HASH peripheral in SHA224 mode, next process pInBuffer then
  211. * read the computed digest in interruption mode.
  212. * @note Digest is available in pOutBuffer.
  213. * @param hhash: HASH handle.
  214. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  215. * @param Size: length of the input buffer in bytes.
  216. * @param pOutBuffer: pointer to the computed digest. Digest size is 28 bytes.
  217. * @retval HAL status
  218. */
  219. HAL_StatusTypeDef HAL_HASHEx_SHA224_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer)
  220. {
  221. return HASH_Start_IT(hhash, pInBuffer, Size, pOutBuffer,HASH_ALGOSELECTION_SHA224);
  222. }
  223. /**
  224. * @brief Initialize the HASH peripheral in SHA256 mode, next process pInBuffer then
  225. * read the computed digest in interruption mode.
  226. * @note Digest is available in pOutBuffer.
  227. * @param hhash: HASH handle.
  228. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  229. * @param Size: length of the input buffer in bytes.
  230. * @param pOutBuffer: pointer to the computed digest. Digest size is 32 bytes.
  231. * @retval HAL status
  232. */
  233. HAL_StatusTypeDef HAL_HASHEx_SHA256_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer)
  234. {
  235. return HASH_Start_IT(hhash, pInBuffer, Size, pOutBuffer,HASH_ALGOSELECTION_SHA256);
  236. }
  237. /**
  238. * @}
  239. */
  240. /** @defgroup HASHEx_Exported_Functions_Group3 HASH extended processing functions in DMA mode
  241. * @brief HASH extended processing functions using DMA mode.
  242. *
  243. @verbatim
  244. ===============================================================================
  245. ##### DMA mode HASH extended processing functionss #####
  246. ===============================================================================
  247. [..] This section provides functions allowing to calculate in DMA mode
  248. the hash value using one of the following algorithms:
  249. (+) SHA224
  250. (++) HAL_HASHEx_SHA224_Start_DMA()
  251. (++) HAL_HASHEx_SHA224_Finish()
  252. (+) SHA256
  253. (++) HAL_HASHEx_SHA256_Start_DMA()
  254. (++) HAL_HASHEx_SHA256_Finish()
  255. [..] When resorting to DMA mode to enter the data in the IP, user must resort
  256. to HAL_HASHEx_xxx_Start_DMA() then read the resulting digest with
  257. HAL_HASHEx_xxx_Finish().
  258. [..] In case of multi-buffer HASH processing, MDMAT bit must first be set before
  259. the successive calls to HAL_HASHEx_xxx_Start_DMA(). Then, MDMAT bit needs to be
  260. reset before the last call to HAL_HASHEx_xxx_Start_DMA(). Digest is finally
  261. retrieved thanks to HAL_HASHEx_xxx_Finish().
  262. @endverbatim
  263. * @{
  264. */
  265. /**
  266. * @brief Initialize the HASH peripheral in SHA224 mode then initiate a DMA transfer
  267. * to feed the input buffer to the IP.
  268. * @note Once the DMA transfer is finished, HAL_HASHEx_SHA224_Finish() API must
  269. * be called to retrieve the computed digest.
  270. * @param hhash: HASH handle.
  271. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  272. * @param Size: length of the input buffer in bytes.
  273. * @retval HAL status
  274. */
  275. HAL_StatusTypeDef HAL_HASHEx_SHA224_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  276. {
  277. return HASH_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_SHA224);
  278. }
  279. /**
  280. * @brief Return the computed digest in SHA224 mode.
  281. * @note The API waits for DCIS to be set then reads the computed digest.
  282. * @note HAL_HASHEx_SHA224_Finish() can be used as well to retrieve the digest in
  283. * HMAC SHA224 mode.
  284. * @param hhash: HASH handle.
  285. * @param pOutBuffer: pointer to the computed digest. Digest size is 28 bytes.
  286. * @param Timeout: Timeout value.
  287. * @retval HAL status
  288. */
  289. HAL_StatusTypeDef HAL_HASHEx_SHA224_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout)
  290. {
  291. return HASH_Finish(hhash, pOutBuffer, Timeout);
  292. }
  293. /**
  294. * @brief Initialize the HASH peripheral in SHA256 mode then initiate a DMA transfer
  295. * to feed the input buffer to the IP.
  296. * @note Once the DMA transfer is finished, HAL_HASHEx_SHA256_Finish() API must
  297. * be called to retrieve the computed digest.
  298. * @param hhash: HASH handle.
  299. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  300. * @param Size: length of the input buffer in bytes.
  301. * @retval HAL status
  302. */
  303. HAL_StatusTypeDef HAL_HASHEx_SHA256_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  304. {
  305. return HASH_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_SHA256);
  306. }
  307. /**
  308. * @brief Return the computed digest in SHA256 mode.
  309. * @note The API waits for DCIS to be set then reads the computed digest.
  310. * @note HAL_HASHEx_SHA256_Finish() can be used as well to retrieve the digest in
  311. * HMAC SHA256 mode.
  312. * @param hhash: HASH handle.
  313. * @param pOutBuffer: pointer to the computed digest. Digest size is 32 bytes.
  314. * @param Timeout: Timeout value.
  315. * @retval HAL status
  316. */
  317. HAL_StatusTypeDef HAL_HASHEx_SHA256_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout)
  318. {
  319. return HASH_Finish(hhash, pOutBuffer, Timeout);
  320. }
  321. /**
  322. * @}
  323. */
  324. /** @defgroup HASHEx_Exported_Functions_Group4 HMAC extended processing functions in polling mode
  325. * @brief HMAC extended processing functions using polling mode.
  326. *
  327. @verbatim
  328. ===============================================================================
  329. ##### Polling mode HMAC extended processing functions #####
  330. ===============================================================================
  331. [..] This section provides functions allowing to calculate in polling mode
  332. the HMAC value using one of the following algorithms:
  333. (+) SHA224
  334. (++) HAL_HMACEx_SHA224_Start()
  335. (+) SHA256
  336. (++) HAL_HMACEx_SHA256_Start()
  337. @endverbatim
  338. * @{
  339. */
  340. /**
  341. * @brief Initialize the HASH peripheral in HMAC SHA224 mode, next process pInBuffer then
  342. * read the computed digest.
  343. * @note Digest is available in pOutBuffer.
  344. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  345. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  346. * @param hhash: HASH handle.
  347. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  348. * @param Size: length of the input buffer in bytes.
  349. * @param pOutBuffer: pointer to the computed digest. Digest size is 28 bytes.
  350. * @param Timeout: Timeout value.
  351. * @retval HAL status
  352. */
  353. HAL_StatusTypeDef HAL_HMACEx_SHA224_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout)
  354. {
  355. return HMAC_Start(hhash, pInBuffer, Size, pOutBuffer, Timeout, HASH_ALGOSELECTION_SHA224);
  356. }
  357. /**
  358. * @brief Initialize the HASH peripheral in HMAC SHA256 mode, next process pInBuffer then
  359. * read the computed digest.
  360. * @note Digest is available in pOutBuffer.
  361. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  362. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  363. * @param hhash: HASH handle.
  364. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  365. * @param Size: length of the input buffer in bytes.
  366. * @param pOutBuffer: pointer to the computed digest. Digest size is 32 bytes.
  367. * @param Timeout: Timeout value.
  368. * @retval HAL status
  369. */
  370. HAL_StatusTypeDef HAL_HMACEx_SHA256_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout)
  371. {
  372. return HMAC_Start(hhash, pInBuffer, Size, pOutBuffer, Timeout, HASH_ALGOSELECTION_SHA256);
  373. }
  374. /**
  375. * @}
  376. */
  377. /** @defgroup HASHEx_Exported_Functions_Group5 HMAC extended processing functions in interrupt mode
  378. * @brief HMAC extended processing functions using interruption mode.
  379. *
  380. @verbatim
  381. ===============================================================================
  382. ##### Interrupt mode HMAC extended processing functions #####
  383. ===============================================================================
  384. [..] This section provides functions allowing to calculate in interrupt mode
  385. the HMAC value using one of the following algorithms:
  386. (+) SHA224
  387. (++) HAL_HMACEx_SHA224_Start_IT()
  388. (+) SHA256
  389. (++) HAL_HMACEx_SHA256_Start_IT()
  390. @endverbatim
  391. * @{
  392. */
  393. /**
  394. * @brief Initialize the HASH peripheral in HMAC SHA224 mode, next process pInBuffer then
  395. * read the computed digest in interrupt mode.
  396. * @note Digest is available in pOutBuffer.
  397. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  398. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  399. * @param hhash: HASH handle.
  400. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  401. * @param Size: length of the input buffer in bytes.
  402. * @param pOutBuffer: pointer to the computed digest. Digest size is 28 bytes.
  403. * @retval HAL status
  404. */
  405. HAL_StatusTypeDef HAL_HMACEx_SHA224_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer)
  406. {
  407. return HMAC_Start_IT(hhash, pInBuffer, Size, pOutBuffer, HASH_ALGOSELECTION_SHA224);
  408. }
  409. /**
  410. * @brief Initialize the HASH peripheral in HMAC SHA256 mode, next process pInBuffer then
  411. * read the computed digest in interrupt mode.
  412. * @note Digest is available in pOutBuffer.
  413. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  414. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  415. * @param hhash: HASH handle.
  416. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  417. * @param Size: length of the input buffer in bytes.
  418. * @param pOutBuffer: pointer to the computed digest. Digest size is 32 bytes.
  419. * @retval HAL status
  420. */
  421. HAL_StatusTypeDef HAL_HMACEx_SHA256_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer)
  422. {
  423. return HMAC_Start_IT(hhash, pInBuffer, Size, pOutBuffer, HASH_ALGOSELECTION_SHA256);
  424. }
  425. /**
  426. * @}
  427. */
  428. /** @defgroup HASHEx_Exported_Functions_Group6 HMAC extended processing functions in DMA mode
  429. * @brief HMAC extended processing functions using DMA mode.
  430. *
  431. @verbatim
  432. ===============================================================================
  433. ##### DMA mode HMAC extended processing functions #####
  434. ===============================================================================
  435. [..] This section provides functions allowing to calculate in DMA mode
  436. the HMAC value using one of the following algorithms:
  437. (+) SHA224
  438. (++) HAL_HMACEx_SHA224_Start_DMA()
  439. (+) SHA256
  440. (++) HAL_HMACEx_SHA256_Start_DMA()
  441. [..] When resorting to DMA mode to enter the data in the IP for HMAC processing,
  442. user must resort to HAL_HMACEx_xxx_Start_DMA() then read the resulting digest
  443. with HAL_HASHEx_xxx_Finish().
  444. @endverbatim
  445. * @{
  446. */
  447. /**
  448. * @brief Initialize the HASH peripheral in HMAC SHA224 mode then initiate the required
  449. * DMA transfers to feed the key and the input buffer to the IP.
  450. * @note Once the DMA transfers are finished (indicated by hhash->State set back
  451. * to HAL_HASH_STATE_READY), HAL_HASHEx_SHA224_Finish() API must be called to retrieve
  452. * the computed digest.
  453. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  454. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  455. * @note If MDMAT bit is set before calling this function (multi-buffer
  456. * HASH processing case), the input buffer size (in bytes) must be
  457. * a multiple of 4 otherwise, the HASH digest computation is corrupted.
  458. * For the processing of the last buffer of the thread, MDMAT bit must
  459. * be reset and the buffer length (in bytes) doesn't have to be a
  460. * multiple of 4.
  461. * @param hhash: HASH handle.
  462. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  463. * @param Size: length of the input buffer in bytes.
  464. * @retval HAL status
  465. */
  466. HAL_StatusTypeDef HAL_HMACEx_SHA224_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  467. {
  468. return HMAC_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_SHA224);
  469. }
  470. /**
  471. * @brief Initialize the HASH peripheral in HMAC SHA224 mode then initiate the required
  472. * DMA transfers to feed the key and the input buffer to the IP.
  473. * @note Once the DMA transfers are finished (indicated by hhash->State set back
  474. * to HAL_HASH_STATE_READY), HAL_HASHEx_SHA256_Finish() API must be called to retrieve
  475. * the computed digest.
  476. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  477. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  478. * @note If MDMAT bit is set before calling this function (multi-buffer
  479. * HASH processing case), the input buffer size (in bytes) must be
  480. * a multiple of 4 otherwise, the HASH digest computation is corrupted.
  481. * For the processing of the last buffer of the thread, MDMAT bit must
  482. * be reset and the buffer length (in bytes) doesn't have to be a
  483. * multiple of 4.
  484. * @param hhash: HASH handle.
  485. * @param pInBuffer: pointer to the input buffer (buffer to be hashed).
  486. * @param Size: length of the input buffer in bytes.
  487. * @retval HAL status
  488. */
  489. HAL_StatusTypeDef HAL_HMACEx_SHA256_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  490. {
  491. return HMAC_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_SHA256);
  492. }
  493. /**
  494. * @}
  495. */
  496. /** @defgroup HASHEx_Exported_Functions_Group7 Multi-buffer HMAC extended processing functions in DMA mode
  497. * @brief HMAC extended processing functions in multi-buffer DMA mode.
  498. *
  499. @verbatim
  500. ===============================================================================
  501. ##### Multi-buffer DMA mode HMAC extended processing functions #####
  502. ===============================================================================
  503. [..] This section provides functions to manage HMAC multi-buffer
  504. DMA-based processing for MD5, SHA1, SHA224 and SHA256 algorithms.
  505. (+) MD5
  506. (++) HAL_HMACEx_MD5_Step1_2_DMA()
  507. (++) HAL_HMACEx_MD5_Step2_DMA()
  508. (++) HAL_HMACEx_MD5_Step2_3_DMA()
  509. (+) SHA1
  510. (++) HAL_HMACEx_SHA1_Step1_2_DMA()
  511. (++) HAL_HMACEx_SHA1_Step2_DMA()
  512. (++) HAL_HMACEx_SHA1_Step2_3_DMA()
  513. (+) SHA256
  514. (++) HAL_HMACEx_SHA224_Step1_2_DMA()
  515. (++) HAL_HMACEx_SHA224_Step2_DMA()
  516. (++) HAL_HMACEx_SHA224_Step2_3_DMA()
  517. (+) SHA256
  518. (++) HAL_HMACEx_SHA256_Step1_2_DMA()
  519. (++) HAL_HMACEx_SHA256_Step2_DMA()
  520. (++) HAL_HMACEx_SHA256_Step2_3_DMA()
  521. [..] User must first start-up the multi-buffer DMA-based HMAC computation in
  522. calling HAL_HMACEx_xxx_Step1_2_DMA(). This carries out HMAC step 1 and
  523. intiates step 2 with the first input buffer.
  524. [..] The following buffers are next fed to the IP with a call to the API
  525. HAL_HMACEx_xxx_Step2_DMA(). There may be several consecutive calls
  526. to this API.
  527. [..] Multi-buffer DMA-based HMAC computation is wrapped up by a call to
  528. HAL_HMACEx_xxx_Step2_3_DMA(). This finishes step 2 in feeding the last input
  529. buffer to the IP then carries out step 3.
  530. [..] Digest is retrieved by a call to HAL_HASH_xxx_Finish() for MD-5 or
  531. SHA-1, to HAL_HASHEx_xxx_Finish() for SHA-224 or SHA-256.
  532. [..] If only two buffers need to be consecutively processed, a call to
  533. HAL_HMACEx_xxx_Step1_2_DMA() followed by a call to HAL_HMACEx_xxx_Step2_3_DMA()
  534. is sufficient.
  535. @endverbatim
  536. * @{
  537. */
  538. /**
  539. * @brief MD5 HMAC step 1 completion and step 2 start in multi-buffer DMA mode.
  540. * @note Step 1 consists in writing the inner hash function key in the IP,
  541. * step 2 consists in writing the message text.
  542. * @note The API carries out the HMAC step 1 then starts step 2 with
  543. * the first buffer entered to the IP. DCAL bit is not automatically set after
  544. * the message buffer feeding, allowing other messages DMA transfers to occur.
  545. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  546. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  547. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  548. * HASH digest computation is corrupted.
  549. * @param hhash: HASH handle.
  550. * @param pInBuffer: pointer to the input buffer (message buffer).
  551. * @param Size: length of the input buffer in bytes.
  552. * @retval HAL status
  553. */
  554. HAL_StatusTypeDef HAL_HMACEx_MD5_Step1_2_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  555. {
  556. hhash->DigestCalculationDisable = SET;
  557. return HMAC_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_MD5);
  558. }
  559. /**
  560. * @brief MD5 HMAC step 2 in multi-buffer DMA mode.
  561. * @note Step 2 consists in writing the message text in the IP.
  562. * @note The API carries on the HMAC step 2, applied to the buffer entered as input
  563. * parameter. DCAL bit is not automatically set after the message buffer feeding,
  564. * allowing other messages DMA transfers to occur.
  565. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  566. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  567. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  568. * HASH digest computation is corrupted.
  569. * @param hhash: HASH handle.
  570. * @param pInBuffer: pointer to the input buffer (message buffer).
  571. * @param Size: length of the input buffer in bytes.
  572. * @retval HAL status
  573. */
  574. HAL_StatusTypeDef HAL_HMACEx_MD5_Step2_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  575. {
  576. if (hhash->DigestCalculationDisable != SET)
  577. {
  578. return HAL_ERROR;
  579. }
  580. return HMAC_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_MD5);
  581. }
  582. /**
  583. * @brief MD5 HMAC step 2 wrap-up and step 3 completion in multi-buffer DMA mode.
  584. * @note Step 2 consists in writing the message text in the IP,
  585. * step 3 consists in writing the outer hash function key.
  586. * @note The API wraps up the HMAC step 2 in processing the buffer entered as input
  587. * parameter (the input buffer must be the last one of the multi-buffer thread)
  588. * then carries out HMAC step 3.
  589. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  590. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  591. * @note Once the DMA transfers are finished (indicated by hhash->State set back
  592. * to HAL_HASH_STATE_READY), HAL_HASHEx_SHA256_Finish() API must be called to retrieve
  593. * the computed digest.
  594. * @param hhash: HASH handle.
  595. * @param pInBuffer: pointer to the input buffer (message buffer).
  596. * @param Size: length of the input buffer in bytes.
  597. * @retval HAL status
  598. */
  599. HAL_StatusTypeDef HAL_HMACEx_MD5_Step2_3_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  600. {
  601. hhash->DigestCalculationDisable = RESET;
  602. return HMAC_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_MD5);
  603. }
  604. /**
  605. * @brief SHA1 HMAC step 1 completion and step 2 start in multi-buffer DMA mode.
  606. * @note Step 1 consists in writing the inner hash function key in the IP,
  607. * step 2 consists in writing the message text.
  608. * @note The API carries out the HMAC step 1 then starts step 2 with
  609. * the first buffer entered to the IP. DCAL bit is not automatically set after
  610. * the message buffer feeding, allowing other messages DMA transfers to occur.
  611. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  612. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  613. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  614. * HASH digest computation is corrupted.
  615. * @param hhash: HASH handle.
  616. * @param pInBuffer: pointer to the input buffer (message buffer).
  617. * @param Size: length of the input buffer in bytes.
  618. * @retval HAL status
  619. */
  620. HAL_StatusTypeDef HAL_HMACEx_SHA1_Step1_2_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  621. {
  622. hhash->DigestCalculationDisable = SET;
  623. return HMAC_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_SHA1);
  624. }
  625. /**
  626. * @brief SHA1 HMAC step 2 in multi-buffer DMA mode.
  627. * @note Step 2 consists in writing the message text in the IP.
  628. * @note The API carries on the HMAC step 2, applied to the buffer entered as input
  629. * parameter. DCAL bit is not automatically set after the message buffer feeding,
  630. * allowing other messages DMA transfers to occur.
  631. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  632. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  633. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  634. * HASH digest computation is corrupted.
  635. * @param hhash: HASH handle.
  636. * @param pInBuffer: pointer to the input buffer (message buffer).
  637. * @param Size: length of the input buffer in bytes.
  638. * @retval HAL status
  639. */
  640. HAL_StatusTypeDef HAL_HMACEx_SHA1_Step2_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  641. {
  642. if (hhash->DigestCalculationDisable != SET)
  643. {
  644. return HAL_ERROR;
  645. }
  646. return HMAC_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_SHA1);
  647. }
  648. /**
  649. * @brief SHA1 HMAC step 2 wrap-up and step 3 completion in multi-buffer DMA mode.
  650. * @note Step 2 consists in writing the message text in the IP,
  651. * step 3 consists in writing the outer hash function key.
  652. * @note The API wraps up the HMAC step 2 in processing the buffer entered as input
  653. * parameter (the input buffer must be the last one of the multi-buffer thread)
  654. * then carries out HMAC step 3.
  655. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  656. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  657. * @note Once the DMA transfers are finished (indicated by hhash->State set back
  658. * to HAL_HASH_STATE_READY), HAL_HASHEx_SHA256_Finish() API must be called to retrieve
  659. * the computed digest.
  660. * @param hhash: HASH handle.
  661. * @param pInBuffer: pointer to the input buffer (message buffer).
  662. * @param Size: length of the input buffer in bytes.
  663. * @retval HAL status
  664. */
  665. HAL_StatusTypeDef HAL_HMACEx_SHA1_Step2_3_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  666. {
  667. hhash->DigestCalculationDisable = RESET;
  668. return HMAC_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_SHA1);
  669. }
  670. /**
  671. * @brief SHA224 HMAC step 1 completion and step 2 start in multi-buffer DMA mode.
  672. * @note Step 1 consists in writing the inner hash function key in the IP,
  673. * step 2 consists in writing the message text.
  674. * @note The API carries out the HMAC step 1 then starts step 2 with
  675. * the first buffer entered to the IP. DCAL bit is not automatically set after
  676. * the message buffer feeding, allowing other messages DMA transfers to occur.
  677. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  678. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  679. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  680. * HASH digest computation is corrupted.
  681. * @param hhash: HASH handle.
  682. * @param pInBuffer: pointer to the input buffer (message buffer).
  683. * @param Size: length of the input buffer in bytes.
  684. * @retval HAL status
  685. */
  686. HAL_StatusTypeDef HAL_HMACEx_SHA224_Step1_2_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  687. {
  688. hhash->DigestCalculationDisable = SET;
  689. return HMAC_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_SHA224);
  690. }
  691. /**
  692. * @brief SHA224 HMAC step 2 in multi-buffer DMA mode.
  693. * @note Step 2 consists in writing the message text in the IP.
  694. * @note The API carries on the HMAC step 2, applied to the buffer entered as input
  695. * parameter. DCAL bit is not automatically set after the message buffer feeding,
  696. * allowing other messages DMA transfers to occur.
  697. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  698. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  699. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  700. * HASH digest computation is corrupted.
  701. * @param hhash: HASH handle.
  702. * @param pInBuffer: pointer to the input buffer (message buffer).
  703. * @param Size: length of the input buffer in bytes.
  704. * @retval HAL status
  705. */
  706. HAL_StatusTypeDef HAL_HMACEx_SHA224_Step2_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  707. {
  708. if (hhash->DigestCalculationDisable != SET)
  709. {
  710. return HAL_ERROR;
  711. }
  712. return HMAC_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_SHA224);
  713. }
  714. /**
  715. * @brief SHA224 HMAC step 2 wrap-up and step 3 completion in multi-buffer DMA mode.
  716. * @note Step 2 consists in writing the message text in the IP,
  717. * step 3 consists in writing the outer hash function key.
  718. * @note The API wraps up the HMAC step 2 in processing the buffer entered as input
  719. * parameter (the input buffer must be the last one of the multi-buffer thread)
  720. * then carries out HMAC step 3.
  721. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  722. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  723. * @note Once the DMA transfers are finished (indicated by hhash->State set back
  724. * to HAL_HASH_STATE_READY), HAL_HASHEx_SHA256_Finish() API must be called to retrieve
  725. * the computed digest.
  726. * @param hhash: HASH handle.
  727. * @param pInBuffer: pointer to the input buffer (message buffer).
  728. * @param Size: length of the input buffer in bytes.
  729. * @retval HAL status
  730. */
  731. HAL_StatusTypeDef HAL_HMACEx_SHA224_Step2_3_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  732. {
  733. hhash->DigestCalculationDisable = RESET;
  734. return HMAC_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_SHA224);
  735. }
  736. /**
  737. * @brief SHA256 HMAC step 1 completion and step 2 start in multi-buffer DMA mode.
  738. * @note Step 1 consists in writing the inner hash function key in the IP,
  739. * step 2 consists in writing the message text.
  740. * @note The API carries out the HMAC step 1 then starts step 2 with
  741. * the first buffer entered to the IP. DCAL bit is not automatically set after
  742. * the message buffer feeding, allowing other messages DMA transfers to occur.
  743. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  744. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  745. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  746. * HASH digest computation is corrupted.
  747. * @param hhash: HASH handle.
  748. * @param pInBuffer: pointer to the input buffer (message buffer).
  749. * @param Size: length of the input buffer in bytes.
  750. * @retval HAL status
  751. */
  752. HAL_StatusTypeDef HAL_HMACEx_SHA256_Step1_2_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  753. {
  754. hhash->DigestCalculationDisable = SET;
  755. return HMAC_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_SHA256);
  756. }
  757. /**
  758. * @brief SHA256 HMAC step 2 in multi-buffer DMA mode.
  759. * @note Step 2 consists in writing the message text in the IP.
  760. * @note The API carries on the HMAC step 2, applied to the buffer entered as input
  761. * parameter. DCAL bit is not automatically set after the message buffer feeding,
  762. * allowing other messages DMA transfers to occur.
  763. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  764. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  765. * @note The input buffer size (in bytes) must be a multiple of 4 otherwise, the
  766. * HASH digest computation is corrupted.
  767. * @param hhash: HASH handle.
  768. * @param pInBuffer: pointer to the input buffer (message buffer).
  769. * @param Size: length of the input buffer in bytes.
  770. * @retval HAL status
  771. */
  772. HAL_StatusTypeDef HAL_HMACEx_SHA256_Step2_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  773. {
  774. if (hhash->DigestCalculationDisable != SET)
  775. {
  776. return HAL_ERROR;
  777. }
  778. return HMAC_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_SHA256);
  779. }
  780. /**
  781. * @brief SHA256 HMAC step 2 wrap-up and step 3 completion in multi-buffer DMA mode.
  782. * @note Step 2 consists in writing the message text in the IP,
  783. * step 3 consists in writing the outer hash function key.
  784. * @note The API wraps up the HMAC step 2 in processing the buffer entered as input
  785. * parameter (the input buffer must be the last one of the multi-buffer thread)
  786. * then carries out HMAC step 3.
  787. * @note Same key is used for the inner and the outer hash functions; pointer to key and
  788. * key size are respectively stored in hhash->Init.pKey and hhash->Init.KeySize.
  789. * @note Once the DMA transfers are finished (indicated by hhash->State set back
  790. * to HAL_HASH_STATE_READY), HAL_HASHEx_SHA256_Finish() API must be called to retrieve
  791. * the computed digest.
  792. * @param hhash: HASH handle.
  793. * @param pInBuffer: pointer to the input buffer (message buffer).
  794. * @param Size: length of the input buffer in bytes.
  795. * @retval HAL status
  796. */
  797. HAL_StatusTypeDef HAL_HMACEx_SHA256_Step2_3_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size)
  798. {
  799. hhash->DigestCalculationDisable = RESET;
  800. return HMAC_Start_DMA(hhash, pInBuffer, Size, HASH_ALGOSELECTION_SHA256);
  801. }
  802. /**
  803. * @}
  804. */
  805. #endif /* MDMA defined*/
  806. /**
  807. * @}
  808. */
  809. #endif /* HAL_HASH_MODULE_ENABLED */
  810. /**
  811. * @}
  812. */
  813. #endif /* HASH*/
  814. /**
  815. * @}
  816. */
  817. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/