2 ******************************************************************************
3 * @file stm32f4xx_hash.h
4 * @author MCD Application Team
7 * @brief This file contains all the functions prototypes for the HASH
9 ******************************************************************************
12 * <h2><center>© COPYRIGHT 2016 STMicroelectronics</center></h2>
14 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
15 * You may not use this file except in compliance with the License.
16 * You may obtain a copy of the License at:
18 * http://www.st.com/software_license_agreement_liberty_v2
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
26 ******************************************************************************
29 /* Define to prevent recursive inclusion -------------------------------------*/
30 #ifndef __STM32F4xx_HASH_H
31 #define __STM32F4xx_HASH_H
37 /* Includes ------------------------------------------------------------------*/
38 #include "stm32f4xx.h"
40 /** @addtogroup STM32F4xx_StdPeriph_Driver
48 /* Exported types ------------------------------------------------------------*/
51 * @brief HASH Init structure definition
55 uint32_t HASH_AlgoSelection
; /*!< SHA-1, SHA-224, SHA-256 or MD5. This parameter
56 can be a value of @ref HASH_Algo_Selection */
57 uint32_t HASH_AlgoMode
; /*!< HASH or HMAC. This parameter can be a value
58 of @ref HASH_processor_Algorithm_Mode */
59 uint32_t HASH_DataType
; /*!< 32-bit data, 16-bit data, 8-bit data or
60 bit string. This parameter can be a value of
61 @ref HASH_Data_Type */
62 uint32_t HASH_HMACKeyType
; /*!< HMAC Short key or HMAC Long Key. This parameter
63 can be a value of @ref HASH_HMAC_Long_key_only_for_HMAC_mode */
67 * @brief HASH message digest result structure definition
71 uint32_t Data
[8]; /*!< Message digest result : 8x 32bit wors for SHA-256,
72 7x 32bit wors for SHA-224,
73 5x 32bit words for SHA-1 or
74 4x 32bit words for MD5 */
78 * @brief HASH context swapping structure definition
85 uint32_t HASH_CSR
[54];
88 /* Exported constants --------------------------------------------------------*/
90 /** @defgroup HASH_Exported_Constants
94 /** @defgroup HASH_Algo_Selection
97 #define HASH_AlgoSelection_SHA1 ((uint32_t)0x0000) /*!< HASH function is SHA1 */
98 #define HASH_AlgoSelection_SHA224 HASH_CR_ALGO_1 /*!< HASH function is SHA224 */
99 #define HASH_AlgoSelection_SHA256 HASH_CR_ALGO /*!< HASH function is SHA256 */
100 #define HASH_AlgoSelection_MD5 HASH_CR_ALGO_0 /*!< HASH function is MD5 */
102 #define IS_HASH_ALGOSELECTION(ALGOSELECTION) (((ALGOSELECTION) == HASH_AlgoSelection_SHA1) || \
103 ((ALGOSELECTION) == HASH_AlgoSelection_SHA224) || \
104 ((ALGOSELECTION) == HASH_AlgoSelection_SHA256) || \
105 ((ALGOSELECTION) == HASH_AlgoSelection_MD5))
110 /** @defgroup HASH_processor_Algorithm_Mode
113 #define HASH_AlgoMode_HASH ((uint32_t)0x00000000) /*!< Algorithm is HASH */
114 #define HASH_AlgoMode_HMAC HASH_CR_MODE /*!< Algorithm is HMAC */
116 #define IS_HASH_ALGOMODE(ALGOMODE) (((ALGOMODE) == HASH_AlgoMode_HASH) || \
117 ((ALGOMODE) == HASH_AlgoMode_HMAC))
122 /** @defgroup HASH_Data_Type
125 #define HASH_DataType_32b ((uint32_t)0x0000) /*!< 32-bit data. No swapping */
126 #define HASH_DataType_16b HASH_CR_DATATYPE_0 /*!< 16-bit data. Each half word is swapped */
127 #define HASH_DataType_8b HASH_CR_DATATYPE_1 /*!< 8-bit data. All bytes are swapped */
128 #define HASH_DataType_1b HASH_CR_DATATYPE /*!< 1-bit data. In the word all bits are swapped */
130 #define IS_HASH_DATATYPE(DATATYPE) (((DATATYPE) == HASH_DataType_32b)|| \
131 ((DATATYPE) == HASH_DataType_16b)|| \
132 ((DATATYPE) == HASH_DataType_8b) || \
133 ((DATATYPE) == HASH_DataType_1b))
138 /** @defgroup HASH_HMAC_Long_key_only_for_HMAC_mode
141 #define HASH_HMACKeyType_ShortKey ((uint32_t)0x00000000) /*!< HMAC Key is <= 64 bytes */
142 #define HASH_HMACKeyType_LongKey HASH_CR_LKEY /*!< HMAC Key is > 64 bytes */
144 #define IS_HASH_HMAC_KEYTYPE(KEYTYPE) (((KEYTYPE) == HASH_HMACKeyType_ShortKey) || \
145 ((KEYTYPE) == HASH_HMACKeyType_LongKey))
150 /** @defgroup Number_of_valid_bits_in_last_word_of_the_message
153 #define IS_HASH_VALIDBITSNUMBER(VALIDBITS) ((VALIDBITS) <= 0x1F)
159 /** @defgroup HASH_interrupts_definition
162 #define HASH_IT_DINI HASH_IMR_DINIM /*!< A new block can be entered into the input buffer (DIN) */
163 #define HASH_IT_DCI HASH_IMR_DCIM /*!< Digest calculation complete */
165 #define IS_HASH_IT(IT) ((((IT) & (uint32_t)0xFFFFFFFC) == 0x00000000) && ((IT) != 0x00000000))
166 #define IS_HASH_GET_IT(IT) (((IT) == HASH_IT_DINI) || ((IT) == HASH_IT_DCI))
172 /** @defgroup HASH_flags_definition
175 #define HASH_FLAG_DINIS HASH_SR_DINIS /*!< 16 locations are free in the DIN : A new block can be entered into the input buffer */
176 #define HASH_FLAG_DCIS HASH_SR_DCIS /*!< Digest calculation complete */
177 #define HASH_FLAG_DMAS HASH_SR_DMAS /*!< DMA interface is enabled (DMAE=1) or a transfer is ongoing */
178 #define HASH_FLAG_BUSY HASH_SR_BUSY /*!< The hash core is Busy : processing a block of data */
179 #define HASH_FLAG_DINNE HASH_CR_DINNE /*!< DIN not empty : The input buffer contains at least one word of data */
181 #define IS_HASH_GET_FLAG(FLAG) (((FLAG) == HASH_FLAG_DINIS) || \
182 ((FLAG) == HASH_FLAG_DCIS) || \
183 ((FLAG) == HASH_FLAG_DMAS) || \
184 ((FLAG) == HASH_FLAG_BUSY) || \
185 ((FLAG) == HASH_FLAG_DINNE))
187 #define IS_HASH_CLEAR_FLAG(FLAG)(((FLAG) == HASH_FLAG_DINIS) || \
188 ((FLAG) == HASH_FLAG_DCIS))
198 /* Exported macro ------------------------------------------------------------*/
199 /* Exported functions --------------------------------------------------------*/
201 /* Function used to set the HASH configuration to the default reset state ****/
202 void HASH_DeInit(void);
204 /* HASH Configuration function ************************************************/
205 void HASH_Init(HASH_InitTypeDef
* HASH_InitStruct
);
206 void HASH_StructInit(HASH_InitTypeDef
* HASH_InitStruct
);
207 void HASH_Reset(void);
209 /* HASH Message Digest generation functions ***********************************/
210 void HASH_DataIn(uint32_t Data
);
211 uint8_t HASH_GetInFIFOWordsNbr(void);
212 void HASH_SetLastWordValidBitsNbr(uint16_t ValidNumber
);
213 void HASH_StartDigest(void);
214 void HASH_AutoStartDigest(FunctionalState NewState
);
215 void HASH_GetDigest(HASH_MsgDigest
* HASH_MessageDigest
);
217 /* HASH Context swapping functions ********************************************/
218 void HASH_SaveContext(HASH_Context
* HASH_ContextSave
);
219 void HASH_RestoreContext(HASH_Context
* HASH_ContextRestore
);
221 /* HASH DMA interface function ************************************************/
222 void HASH_DMACmd(FunctionalState NewState
);
224 /* HASH Interrupts and flags management functions *****************************/
225 void HASH_ITConfig(uint32_t HASH_IT
, FunctionalState NewState
);
226 FlagStatus
HASH_GetFlagStatus(uint32_t HASH_FLAG
);
227 void HASH_ClearFlag(uint32_t HASH_FLAG
);
228 ITStatus
HASH_GetITStatus(uint32_t HASH_IT
);
229 void HASH_ClearITPendingBit(uint32_t HASH_IT
);
231 /* High Level SHA1 functions **************************************************/
232 ErrorStatus
HASH_SHA1(uint8_t *Input
, uint32_t Ilen
, uint8_t Output
[20]);
233 ErrorStatus
HMAC_SHA1(uint8_t *Key
, uint32_t Keylen
,
234 uint8_t *Input
, uint32_t Ilen
,
237 /* High Level MD5 functions ***************************************************/
238 ErrorStatus
HASH_MD5(uint8_t *Input
, uint32_t Ilen
, uint8_t Output
[16]);
239 ErrorStatus
HMAC_MD5(uint8_t *Key
, uint32_t Keylen
,
240 uint8_t *Input
, uint32_t Ilen
,
247 #endif /*__STM32F4xx_HASH_H */
257 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/