stm32f4xx_hal_hash.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_hash.h
  4. * @author MCD Application Team
  5. * @version V1.6.0
  6. * @date 04-November-2016
  7. * @brief Header file of HASH HAL module.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ******************************************************************************
  36. */
  37. /* Define to prevent recursive inclusion -------------------------------------*/
  38. #ifndef __STM32F4xx_HAL_HASH_H
  39. #define __STM32F4xx_HAL_HASH_H
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. #if defined(STM32F415xx) || defined(STM32F417xx) || defined(STM32F437xx) || defined(STM32F439xx) || defined(STM32F479xx)
  44. /* Includes ------------------------------------------------------------------*/
  45. #include "stm32f4xx_hal_def.h"
  46. /** @addtogroup STM32F4xx_HAL_Driver
  47. * @{
  48. */
  49. /** @addtogroup HASH
  50. * @brief HASH HAL module driver
  51. * @{
  52. */
  53. /* Exported types ------------------------------------------------------------*/
  54. /** @defgroup HASH_Exported_Types HASH Exported Types
  55. * @{
  56. */
  57. /** @defgroup HASH_Exported_Types_Group1 HASH Configuration Structure definition
  58. * @{
  59. */
  60. typedef struct
  61. {
  62. uint32_t DataType; /*!< 32-bit data, 16-bit data, 8-bit data or 1-bit string.
  63. This parameter can be a value of @ref HASH_Data_Type */
  64. uint32_t KeySize; /*!< The key size is used only in HMAC operation */
  65. uint8_t* pKey; /*!< The key is used only in HMAC operation */
  66. }HASH_InitTypeDef;
  67. /**
  68. * @}
  69. */
  70. /** @defgroup HASH_Exported_Types_Group2 HASH State structures definition
  71. * @{
  72. */
  73. typedef enum
  74. {
  75. HAL_HASH_STATE_RESET = 0x00U, /*!< HASH not yet initialized or disabled */
  76. HAL_HASH_STATE_READY = 0x01U, /*!< HASH initialized and ready for use */
  77. HAL_HASH_STATE_BUSY = 0x02U, /*!< HASH internal process is ongoing */
  78. HAL_HASH_STATE_TIMEOUT = 0x03U, /*!< HASH timeout state */
  79. HAL_HASH_STATE_ERROR = 0x04U /*!< HASH error state */
  80. }HAL_HASH_StateTypeDef;
  81. /**
  82. * @}
  83. */
  84. /** @defgroup HASH_Exported_Types_Group3 HASH phase structures definition
  85. * @{
  86. */
  87. typedef enum
  88. {
  89. HAL_HASH_PHASE_READY = 0x01U, /*!< HASH peripheral is ready for initialization */
  90. HAL_HASH_PHASE_PROCESS = 0x02U /*!< HASH peripheral is in processing phase */
  91. }HAL_HASH_PhaseTypeDef;
  92. /**
  93. * @}
  94. */
  95. /** @defgroup HASH_Exported_Types_Group4 HASH Handle structures definition
  96. * @{
  97. */
  98. typedef struct
  99. {
  100. HASH_InitTypeDef Init; /*!< HASH required parameters */
  101. uint8_t *pHashInBuffPtr; /*!< Pointer to input buffer */
  102. uint8_t *pHashOutBuffPtr; /*!< Pointer to input buffer */
  103. __IO uint32_t HashBuffSize; /*!< Size of buffer to be processed */
  104. __IO uint32_t HashInCount; /*!< Counter of inputed data */
  105. __IO uint32_t HashITCounter; /*!< Counter of issued interrupts */
  106. HAL_StatusTypeDef Status; /*!< HASH peripheral status */
  107. HAL_HASH_PhaseTypeDef Phase; /*!< HASH peripheral phase */
  108. DMA_HandleTypeDef *hdmain; /*!< HASH In DMA handle parameters */
  109. HAL_LockTypeDef Lock; /*!< HASH locking object */
  110. __IO HAL_HASH_StateTypeDef State; /*!< HASH peripheral state */
  111. } HASH_HandleTypeDef;
  112. /**
  113. * @}
  114. */
  115. /**
  116. * @}
  117. */
  118. /* Exported constants --------------------------------------------------------*/
  119. /** @defgroup HASH_Exported_Constants HASH Exported Constants
  120. * @{
  121. */
  122. /** @defgroup HASH_Exported_Constants_Group1 HASH Algorithm Selection
  123. * @{
  124. */
  125. #define HASH_ALGOSELECTION_SHA1 ((uint32_t)0x00000000U) /*!< HASH function is SHA1 */
  126. #define HASH_ALGOSELECTION_SHA224 HASH_CR_ALGO_1 /*!< HASH function is SHA224 */
  127. #define HASH_ALGOSELECTION_SHA256 HASH_CR_ALGO /*!< HASH function is SHA256 */
  128. #define HASH_ALGOSELECTION_MD5 HASH_CR_ALGO_0 /*!< HASH function is MD5 */
  129. /**
  130. * @}
  131. */
  132. /** @defgroup HASH_Exported_Constants_Group2 HASH Algorithm Mode
  133. * @{
  134. */
  135. #define HASH_ALGOMODE_HASH ((uint32_t)0x00000000U) /*!< Algorithm is HASH */
  136. #define HASH_ALGOMODE_HMAC HASH_CR_MODE /*!< Algorithm is HMAC */
  137. /**
  138. * @}
  139. */
  140. /** @defgroup HASH_Data_Type HASH Data Type
  141. * @{
  142. */
  143. #define HASH_DATATYPE_32B ((uint32_t)0x00000000U) /*!< 32-bit data. No swapping */
  144. #define HASH_DATATYPE_16B HASH_CR_DATATYPE_0 /*!< 16-bit data. Each half word is swapped */
  145. #define HASH_DATATYPE_8B HASH_CR_DATATYPE_1 /*!< 8-bit data. All bytes are swapped */
  146. #define HASH_DATATYPE_1B HASH_CR_DATATYPE /*!< 1-bit data. In the word all bits are swapped */
  147. /**
  148. * @}
  149. */
  150. /** @defgroup HASH_Exported_Constants_Group4 HASH HMAC Long key
  151. * @brief HASH HMAC Long key used only for HMAC mode
  152. * @{
  153. */
  154. #define HASH_HMAC_KEYTYPE_SHORTKEY ((uint32_t)0x00000000U) /*!< HMAC Key is <= 64 bytes */
  155. #define HASH_HMAC_KEYTYPE_LONGKEY HASH_CR_LKEY /*!< HMAC Key is > 64 bytes */
  156. /**
  157. * @}
  158. */
  159. /** @defgroup HASH_Exported_Constants_Group5 HASH Flags definition
  160. * @{
  161. */
  162. #define HASH_FLAG_DINIS HASH_SR_DINIS /*!< 16 locations are free in the DIN : A new block can be entered into the input buffer */
  163. #define HASH_FLAG_DCIS HASH_SR_DCIS /*!< Digest calculation complete */
  164. #define HASH_FLAG_DMAS HASH_SR_DMAS /*!< DMA interface is enabled (DMAE=1) or a transfer is ongoing */
  165. #define HASH_FLAG_BUSY HASH_SR_BUSY /*!< The hash core is Busy : processing a block of data */
  166. #define HASH_FLAG_DINNE HASH_CR_DINNE /*!< DIN not empty : The input buffer contains at least one word of data */
  167. /**
  168. * @}
  169. */
  170. /** @defgroup HASH_Exported_Constants_Group6 HASH Interrupts definition
  171. * @{
  172. */
  173. #define HASH_IT_DINI HASH_IMR_DINIE /*!< A new block can be entered into the input buffer (DIN) */
  174. #define HASH_IT_DCI HASH_IMR_DCIE /*!< Digest calculation complete */
  175. /**
  176. * @}
  177. */
  178. /**
  179. * @}
  180. */
  181. /* Exported macro ------------------------------------------------------------*/
  182. /** @defgroup HASH_Exported_Macros HASH Exported Macros
  183. * @{
  184. */
  185. /** @brief Reset HASH handle state
  186. * @param __HANDLE__: specifies the HASH handle.
  187. * @retval None
  188. */
  189. #define __HAL_HASH_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_HASH_STATE_RESET)
  190. /** @brief Check whether the specified HASH flag is set or not.
  191. * @param __FLAG__: specifies the flag to check.
  192. * This parameter can be one of the following values:
  193. * @arg HASH_FLAG_DINIS: A new block can be entered into the input buffer.
  194. * @arg HASH_FLAG_DCIS: Digest calculation complete
  195. * @arg HASH_FLAG_DMAS: DMA interface is enabled (DMAE=1) or a transfer is ongoing
  196. * @arg HASH_FLAG_BUSY: The hash core is Busy : processing a block of data
  197. * @arg HASH_FLAG_DINNE: DIN not empty : The input buffer contains at least one word of data
  198. * @retval The new state of __FLAG__ (TRUE or FALSE).
  199. */
  200. #define __HAL_HASH_GET_FLAG(__FLAG__) (((__FLAG__) > 8U) ? ((HASH->CR & (__FLAG__)) == (__FLAG__)) :\
  201. ((HASH->SR & (__FLAG__)) == (__FLAG__)))
  202. /**
  203. * @brief Enable the multiple DMA mode.
  204. * This feature is available only in STM32F429x and STM32F439x devices.
  205. * @retval None
  206. */
  207. #define __HAL_HASH_SET_MDMAT() HASH->CR |= HASH_CR_MDMAT
  208. /**
  209. * @brief Disable the multiple DMA mode.
  210. * @retval None
  211. */
  212. #define __HAL_HASH_RESET_MDMAT() HASH->CR &= (uint32_t)(~HASH_CR_MDMAT)
  213. /**
  214. * @brief Start the digest computation
  215. * @retval None
  216. */
  217. #define __HAL_HASH_START_DIGEST() HASH->STR |= HASH_STR_DCAL
  218. /**
  219. * @brief Set the number of valid bits in last word written in Data register
  220. * @param SIZE: size in byte of last data written in Data register.
  221. * @retval None
  222. */
  223. #define __HAL_HASH_SET_NBVALIDBITS(SIZE) do{HASH->STR &= ~(HASH_STR_NBLW);\
  224. HASH->STR |= 8U * ((SIZE) % 4U);\
  225. }while(0)
  226. /**
  227. * @}
  228. */
  229. /* Include HASH HAL Extension module */
  230. #include "stm32f4xx_hal_hash_ex.h"
  231. /* Exported functions --------------------------------------------------------*/
  232. /** @defgroup HASH_Exported_Functions HASH Exported Functions
  233. * @{
  234. */
  235. /** @addtogroup HASH_Exported_Functions_Group1
  236. * @{
  237. */
  238. HAL_StatusTypeDef HAL_HASH_Init(HASH_HandleTypeDef *hhash);
  239. HAL_StatusTypeDef HAL_HASH_DeInit(HASH_HandleTypeDef *hhash);
  240. /**
  241. * @}
  242. */
  243. /** @addtogroup HASH_Exported_Functions_Group2
  244. * @{
  245. */
  246. HAL_StatusTypeDef HAL_HASH_SHA1_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout);
  247. HAL_StatusTypeDef HAL_HASH_MD5_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout);
  248. HAL_StatusTypeDef HAL_HASH_MD5_Accumulate(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
  249. HAL_StatusTypeDef HAL_HASH_SHA1_Accumulate(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
  250. /**
  251. * @}
  252. */
  253. /** @addtogroup HASH_Exported_Functions_Group3
  254. * @{
  255. */
  256. HAL_StatusTypeDef HAL_HMAC_SHA1_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout);
  257. HAL_StatusTypeDef HAL_HMAC_MD5_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout);
  258. /**
  259. * @}
  260. */
  261. /** @addtogroup HASH_Exported_Functions_Group4
  262. * @{
  263. */
  264. HAL_StatusTypeDef HAL_HASH_SHA1_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer);
  265. HAL_StatusTypeDef HAL_HASH_MD5_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer);
  266. /**
  267. * @}
  268. */
  269. /** @addtogroup HASH_Exported_Functions_Group5
  270. * @{
  271. */
  272. HAL_StatusTypeDef HAL_HASH_SHA1_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
  273. HAL_StatusTypeDef HAL_HASH_SHA1_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout);
  274. HAL_StatusTypeDef HAL_HASH_MD5_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
  275. HAL_StatusTypeDef HAL_HASH_MD5_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout);
  276. /**
  277. * @}
  278. */
  279. /** @addtogroup HASH_Exported_Functions_Group6
  280. * @{
  281. */
  282. HAL_StatusTypeDef HAL_HMAC_SHA1_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
  283. HAL_StatusTypeDef HAL_HMAC_MD5_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
  284. /**
  285. * @}
  286. */
  287. /** @addtogroup HASH_Exported_Functions_Group7
  288. * @{
  289. */
  290. void HAL_HASH_IRQHandler(HASH_HandleTypeDef *hhash);
  291. /**
  292. * @}
  293. */
  294. /** @addtogroup HASH_Exported_Functions_Group8
  295. * @{
  296. */
  297. HAL_HASH_StateTypeDef HAL_HASH_GetState(HASH_HandleTypeDef *hhash);
  298. void HAL_HASH_MspInit(HASH_HandleTypeDef *hhash);
  299. void HAL_HASH_MspDeInit(HASH_HandleTypeDef *hhash);
  300. void HAL_HASH_InCpltCallback(HASH_HandleTypeDef *hhash);
  301. void HAL_HASH_DgstCpltCallback(HASH_HandleTypeDef *hhash);
  302. void HAL_HASH_ErrorCallback(HASH_HandleTypeDef *hhash);
  303. /**
  304. * @}
  305. */
  306. /**
  307. * @}
  308. */
  309. /* Private types -------------------------------------------------------------*/
  310. /** @defgroup HASH_Private_Types HASH Private Types
  311. * @{
  312. */
  313. /**
  314. * @}
  315. */
  316. /* Private variables ---------------------------------------------------------*/
  317. /** @defgroup HASH_Private_Variables HASH Private Variables
  318. * @{
  319. */
  320. /**
  321. * @}
  322. */
  323. /* Private constants ---------------------------------------------------------*/
  324. /** @defgroup HASH_Private_Constants HASH Private Constants
  325. * @{
  326. */
  327. /**
  328. * @}
  329. */
  330. /* Private macros ------------------------------------------------------------*/
  331. /** @defgroup HASH_Private_Macros HASH Private Macros
  332. * @{
  333. */
  334. #define IS_HASH_ALGOSELECTION(__ALGOSELECTION__) (((__ALGOSELECTION__) == HASH_ALGOSELECTION_SHA1) || \
  335. ((__ALGOSELECTION__) == HASH_ALGOSELECTION_SHA224) || \
  336. ((__ALGOSELECTION__) == HASH_ALGOSELECTION_SHA256) || \
  337. ((__ALGOSELECTION__) == HASH_ALGOSELECTION_MD5))
  338. #define IS_HASH_ALGOMODE(__ALGOMODE__) (((__ALGOMODE__) == HASH_ALGOMODE_HASH) || \
  339. ((__ALGOMODE__) == HASH_ALGOMODE_HMAC))
  340. #define IS_HASH_DATATYPE(__DATATYPE__) (((__DATATYPE__) == HASH_DATATYPE_32B)|| \
  341. ((__DATATYPE__) == HASH_DATATYPE_16B)|| \
  342. ((__DATATYPE__) == HASH_DATATYPE_8B) || \
  343. ((__DATATYPE__) == HASH_DATATYPE_1B))
  344. #define IS_HASH_HMAC_KEYTYPE(__KEYTYPE__) (((__KEYTYPE__) == HASH_HMAC_KEYTYPE_SHORTKEY) || \
  345. ((__KEYTYPE__) == HASH_HMAC_KEYTYPE_LONGKEY))
  346. #define IS_HASH_SHA1_BUFFER_SIZE(__SIZE__) ((((__SIZE__)%4) != 0U)? 0U: 1U)
  347. /**
  348. * @}
  349. */
  350. /* Private functions ---------------------------------------------------------*/
  351. /** @defgroup HASH_Private_Functions HASH Private Functions
  352. * @{
  353. */
  354. /**
  355. * @}
  356. */
  357. #endif /* STM32F415xx || STM32F417xx || STM32F437xx || STM32F439xx || STM32F479xx */
  358. /**
  359. * @}
  360. */
  361. /**
  362. * @}
  363. */
  364. #ifdef __cplusplus
  365. }
  366. #endif
  367. #endif /* __STM32F4xx_HAL_HASH_H */
  368. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/