2 ******************************************************************************
3 * @file stm32f4xx_hal_hash.h
4 * @author MCD Application Team
7 * @brief Header file of HASH HAL module.
8 ******************************************************************************
11 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
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.
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.
35 ******************************************************************************
38 /* Define to prevent recursive inclusion -------------------------------------*/
39 #ifndef __STM32F4xx_HAL_HASH_H
40 #define __STM32F4xx_HAL_HASH_H
46 #if defined(STM32F415xx) || defined(STM32F417xx) || defined(STM32F437xx) || defined(STM32F439xx)
48 /* Includes ------------------------------------------------------------------*/
49 #include "stm32f4xx_hal_def.h"
51 /** @addtogroup STM32F4xx_HAL_Driver
59 /* Exported types ------------------------------------------------------------*/
62 * @brief HASH Configuration Structure definition
66 uint32_t DataType; /*!< 32-bit data, 16-bit data, 8-bit data or 1-bit string.
67 This parameter can be a value of @ref HASH_Data_Type */
69 uint32_t KeySize; /*!< The key size is used only in HMAC operation */
71 uint8_t* pKey; /*!< The key is used only in HMAC operation */
75 * @brief HAL State structures definition
79 HAL_HASH_STATE_RESET = 0x00, /*!< HASH not yet initialized or disabled */
80 HAL_HASH_STATE_READY = 0x01, /*!< HASH initialized and ready for use */
81 HAL_HASH_STATE_BUSY = 0x02, /*!< HASH internal process is ongoing */
82 HAL_HASH_STATE_TIMEOUT = 0x03, /*!< HASH timeout state */
83 HAL_HASH_STATE_ERROR = 0x04 /*!< HASH error state */
84 }HAL_HASH_STATETypeDef;
87 * @brief HAL phase structures definition
91 HAL_HASH_PHASE_READY = 0x01, /*!< HASH peripheral is ready for initialization */
92 HAL_HASH_PHASE_PROCESS = 0x02, /*!< HASH peripheral is in processing phase */
93 }HAL_HASHPhaseTypeDef;
96 * @brief HASH Handle Structure definition
100 HASH_InitTypeDef Init; /*!< HASH required parameters */
102 uint8_t *pHashInBuffPtr; /*!< Pointer to input buffer */
104 uint8_t *pHashOutBuffPtr; /*!< Pointer to input buffer */
106 __IO uint32_t HashBuffSize; /*!< Size of buffer to be processed */
108 __IO uint32_t HashInCount; /*!< Counter of inputed data */
110 __IO uint32_t HashITCounter; /*!< Counter of issued interrupts */
112 HAL_StatusTypeDef Status; /*!< HASH peripheral status */
114 HAL_HASHPhaseTypeDef Phase; /*!< HASH peripheral phase */
116 DMA_HandleTypeDef *hdmain; /*!< HASH In DMA handle parameters */
118 HAL_LockTypeDef Lock; /*!< HASH locking object */
120 __IO HAL_HASH_STATETypeDef State; /*!< HASH peripheral state */
121 } HASH_HandleTypeDef;
123 /* Exported constants --------------------------------------------------------*/
125 /** @defgroup HASH_Exported_Constants
129 /** @defgroup HASH_Algo_Selection
132 #define HASH_AlgoSelection_SHA1 ((uint32_t)0x0000) /*!< HASH function is SHA1 */
133 #define HASH_AlgoSelection_SHA224 HASH_CR_ALGO_1 /*!< HASH function is SHA224 */
134 #define HASH_AlgoSelection_SHA256 HASH_CR_ALGO /*!< HASH function is SHA256 */
135 #define HASH_AlgoSelection_MD5 HASH_CR_ALGO_0 /*!< HASH function is MD5 */
137 #define IS_HASH_ALGOSELECTION(ALGOSELECTION) (((ALGOSELECTION) == HASH_AlgoSelection_SHA1) || \
138 ((ALGOSELECTION) == HASH_AlgoSelection_SHA224) || \
139 ((ALGOSELECTION) == HASH_AlgoSelection_SHA256) || \
140 ((ALGOSELECTION) == HASH_AlgoSelection_MD5))
145 /** @defgroup HASH_Algorithm_Mode
148 #define HASH_AlgoMode_HASH ((uint32_t)0x00000000) /*!< Algorithm is HASH */
149 #define HASH_AlgoMode_HMAC HASH_CR_MODE /*!< Algorithm is HMAC */
151 #define IS_HASH_ALGOMODE(ALGOMODE) (((ALGOMODE) == HASH_AlgoMode_HASH) || \
152 ((ALGOMODE) == HASH_AlgoMode_HMAC))
157 /** @defgroup HASH_Data_Type
160 #define HASH_DATATYPE_32B ((uint32_t)0x0000) /*!< 32-bit data. No swapping */
161 #define HASH_DATATYPE_16B HASH_CR_DATATYPE_0 /*!< 16-bit data. Each half word is swapped */
162 #define HASH_DATATYPE_8B HASH_CR_DATATYPE_1 /*!< 8-bit data. All bytes are swapped */
163 #define HASH_DATATYPE_1B HASH_CR_DATATYPE /*!< 1-bit data. In the word all bits are swapped */
165 #define IS_HASH_DATATYPE(DATATYPE) (((DATATYPE) == HASH_DATATYPE_32B)|| \
166 ((DATATYPE) == HASH_DATATYPE_16B)|| \
167 ((DATATYPE) == HASH_DATATYPE_8B) || \
168 ((DATATYPE) == HASH_DATATYPE_1B))
173 /** @defgroup HASH_HMAC_Long_key_only_for_HMAC_mode
176 #define HASH_HMACKeyType_ShortKey ((uint32_t)0x00000000) /*!< HMAC Key is <= 64 bytes */
177 #define HASH_HMACKeyType_LongKey HASH_CR_LKEY /*!< HMAC Key is > 64 bytes */
179 #define IS_HASH_HMAC_KEYTYPE(KEYTYPE) (((KEYTYPE) == HASH_HMACKeyType_ShortKey) || \
180 ((KEYTYPE) == HASH_HMACKeyType_LongKey))
185 /** @defgroup HASH_flags_definition
188 #define HASH_FLAG_DINIS HASH_SR_DINIS /*!< 16 locations are free in the DIN : A new block can be entered into the input buffer */
189 #define HASH_FLAG_DCIS HASH_SR_DCIS /*!< Digest calculation complete */
190 #define HASH_FLAG_DMAS HASH_SR_DMAS /*!< DMA interface is enabled (DMAE=1) or a transfer is ongoing */
191 #define HASH_FLAG_BUSY HASH_SR_BUSY /*!< The hash core is Busy : processing a block of data */
192 #define HASH_FLAG_DINNE HASH_CR_DINNE /*!< DIN not empty : The input buffer contains at least one word of data */
197 /** @defgroup HASH_interrupts_definition
200 #define HASH_IT_DINI HASH_IMR_DINIM /*!< A new block can be entered into the input buffer (DIN) */
201 #define HASH_IT_DCI HASH_IMR_DCIM /*!< Digest calculation complete */
210 /* Exported macro ------------------------------------------------------------*/
212 /** @brief Reset HASH handle state
213 * @param __HANDLE__: specifies the HASH handle.
216 #define __HAL_HASH_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_HASH_STATE_RESET)
218 /** @brief Check whether the specified HASH flag is set or not.
219 * @param __FLAG__: specifies the flag to check.
220 * This parameter can be one of the following values:
221 * @arg HASH_FLAG_DINIS: A new block can be entered into the input buffer.
222 * @arg HASH_FLAG_DCIS: Digest calculation complete
223 * @arg HASH_FLAG_DMAS: DMA interface is enabled (DMAE=1) or a transfer is ongoing
224 * @arg HASH_FLAG_BUSY: The hash core is Busy : processing a block of data
225 * @arg HASH_FLAG_DINNE: DIN not empty : The input buffer contains at least one word of data
226 * @retval The new state of __FLAG__ (TRUE or FALSE).
228 #define __HAL_HASH_GET_FLAG(__FLAG__) ((HASH->SR & (__FLAG__)) == (__FLAG__))
231 * @brief Macros for HMAC finish.
235 #define HAL_HMAC_MD5_Finish HAL_HASH_MD5_Finish
236 #define HAL_HMAC_SHA1_Finish HAL_HASH_SHA1_Finish
237 #define HAL_HMAC_SHA224_Finish HAL_HASH_SHA224_Finish
238 #define HAL_HMAC_SHA256_Finish HAL_HASH_SHA256_Finish
241 * @brief Enable the multiple DMA mode.
242 * This feature is available only in STM32F429x and STM32F439x devices.
246 #define __HAL_HASH_SET_MDMAT() HASH->CR |= HASH_CR_MDMAT
249 * @brief Disable the multiple DMA mode.
253 #define __HAL_HASH_RESET_MDMAT() HASH->CR &= (uint32_t)(~HASH_CR_MDMAT)
256 * @brief Start the digest computation
260 #define __HAL_HASH_START_DIGEST() HASH->STR |= HASH_STR_DCAL
263 * @brief Set the number of valid bits in last word written in Data register
264 * @param SIZE: size in byte of last data written in Data register.
267 #define __HAL_HASH_SET_NBVALIDBITS(SIZE) do{HASH->STR &= ~(HASH_STR_NBW);\
268 HASH->STR |= 8 * ((SIZE) % 4);\
271 /* Include HASH HAL Extension module */
272 #include "stm32f4xx_hal_hash_ex.h"
274 /* Exported functions --------------------------------------------------------*/
276 /* Initialization and de-initialization functions **********************************/
277 HAL_StatusTypeDef HAL_HASH_Init(HASH_HandleTypeDef *hhash);
278 HAL_StatusTypeDef HAL_HASH_DeInit(HASH_HandleTypeDef *hhash);
280 /* HASH processing using polling *********************************************/
281 HAL_StatusTypeDef HAL_HASH_SHA1_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout);
282 HAL_StatusTypeDef HAL_HASH_MD5_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout);
283 HAL_StatusTypeDef HAL_HASH_MD5_Accumulate(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
284 HAL_StatusTypeDef HAL_HASH_SHA1_Accumulate(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
286 /* HASH-MAC processing using polling *****************************************/
287 HAL_StatusTypeDef HAL_HMAC_SHA1_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout);
288 HAL_StatusTypeDef HAL_HMAC_MD5_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout);
290 /* HASH processing using interrupt *******************************************/
291 HAL_StatusTypeDef HAL_HASH_SHA1_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer);
292 HAL_StatusTypeDef HAL_HASH_MD5_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer);
294 /* HASH processing using DMA *************************************************/
295 HAL_StatusTypeDef HAL_HASH_SHA1_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
296 HAL_StatusTypeDef HAL_HASH_SHA1_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout);
297 HAL_StatusTypeDef HAL_HASH_MD5_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
298 HAL_StatusTypeDef HAL_HASH_MD5_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout);
300 /* HASH-HMAC processing using DMA ********************************************/
301 HAL_StatusTypeDef HAL_HMAC_SHA1_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
302 HAL_StatusTypeDef HAL_HMAC_MD5_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
304 /* Processing functions ******************************************************/
305 void HAL_HASH_IRQHandler(HASH_HandleTypeDef *hhash);
307 /* Peripheral State functions ************************************************/
308 HAL_HASH_STATETypeDef HAL_HASH_GetState(HASH_HandleTypeDef *hhash);
309 void HAL_HASH_MspInit(HASH_HandleTypeDef *hhash);
310 void HAL_HASH_MspDeInit(HASH_HandleTypeDef *hhash);
311 void HAL_HASH_InCpltCallback(HASH_HandleTypeDef *hhash);
312 void HAL_HASH_DgstCpltCallback(HASH_HandleTypeDef *hhash);
313 void HAL_HASH_ErrorCallback(HASH_HandleTypeDef *hhash);
315 #endif /* STM32F415xx || STM32F417xx || STM32F437xx || STM32F439xx */
329 #endif /* __STM32F4xx_HAL_HASH_H */
331 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/