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.
 
 
 

935 lines
42 KiB

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